v0.9.4: refactored code relating to font generators
This commit is contained in:
@@ -89,7 +89,7 @@ public class RenderedText extends Image {
|
|||||||
visible = true;
|
visible = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
font = Game.platform.getFont(size, text);
|
font = Game.platform.getFont(size, text, true, true);
|
||||||
|
|
||||||
if (font != null){
|
if (font != null){
|
||||||
GlyphLayout glyphs = new GlyphLayout( font, text);
|
GlyphLayout glyphs = new GlyphLayout( font, text);
|
||||||
|
|||||||
@@ -22,7 +22,13 @@
|
|||||||
package com.watabou.utils;
|
package com.watabou.utils;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.graphics.Pixmap;
|
||||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.PixmapPacker;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
|
||||||
|
import com.watabou.noosa.Game;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
public abstract class PlatformSupport {
|
public abstract class PlatformSupport {
|
||||||
|
|
||||||
@@ -47,15 +53,102 @@ public abstract class PlatformSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//TODO should consider spinning this into its own class, rather than platform support getting ever bigger
|
//TODO should consider spinning this into its own class, rather than platform support getting ever bigger
|
||||||
|
protected static HashMap<FreeTypeFontGenerator, HashMap<Integer, BitmapFont>> fonts;
|
||||||
|
|
||||||
|
protected int pageSize;
|
||||||
|
protected PixmapPacker packer;
|
||||||
|
protected boolean systemfont;
|
||||||
|
|
||||||
public abstract void setupFontGenerators(int pageSize, boolean systemFont );
|
public abstract void setupFontGenerators(int pageSize, boolean systemFont );
|
||||||
|
|
||||||
public abstract void resetGenerators();
|
protected abstract FreeTypeFontGenerator getGeneratorForString( String input );
|
||||||
|
|
||||||
public abstract void reloadGenerators();
|
|
||||||
|
|
||||||
public abstract BitmapFont getFont(int size, String text);
|
|
||||||
|
|
||||||
public abstract String[] splitforTextBlock( String text, boolean multiline );
|
public abstract String[] splitforTextBlock( String text, boolean multiline );
|
||||||
|
|
||||||
|
public void resetGenerators(){
|
||||||
|
resetGenerators( true );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetGenerators( boolean setupAfter ){
|
||||||
|
if (fonts != null) {
|
||||||
|
for (FreeTypeFontGenerator generator : fonts.keySet()) {
|
||||||
|
for (BitmapFont f : fonts.get(generator).values()) {
|
||||||
|
f.dispose();
|
||||||
|
}
|
||||||
|
fonts.get(generator).clear();
|
||||||
|
generator.dispose();
|
||||||
|
}
|
||||||
|
fonts.clear();
|
||||||
|
if (packer != null) {
|
||||||
|
for (PixmapPacker.Page p : packer.getPages()) {
|
||||||
|
p.getTexture().dispose();
|
||||||
|
}
|
||||||
|
packer.dispose();
|
||||||
|
}
|
||||||
|
fonts = null;
|
||||||
|
}
|
||||||
|
if (setupAfter) setupFontGenerators(pageSize, systemfont);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reloadGenerators(){
|
||||||
|
if (packer != null) {
|
||||||
|
for (FreeTypeFontGenerator generator : fonts.keySet()) {
|
||||||
|
for (BitmapFont f : fonts.get(generator).values()) {
|
||||||
|
f.dispose();
|
||||||
|
}
|
||||||
|
fonts.get(generator).clear();
|
||||||
|
}
|
||||||
|
if (packer != null) {
|
||||||
|
for (PixmapPacker.Page p : packer.getPages()) {
|
||||||
|
p.getTexture().dispose();
|
||||||
|
}
|
||||||
|
packer.dispose();
|
||||||
|
}
|
||||||
|
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//flipped is needed because Shattered's graphics are y-down, while GDX graphics are y-up.
|
||||||
|
//this is very confusing, I know.
|
||||||
|
public BitmapFont getFont(int size, String text, boolean flipped, boolean border) {
|
||||||
|
FreeTypeFontGenerator generator = getGeneratorForString(text);
|
||||||
|
|
||||||
|
if (generator == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int key = size;
|
||||||
|
if (border) key += Short.MAX_VALUE; //surely we'll never have a size above 32k
|
||||||
|
if (flipped) key = -key;
|
||||||
|
if (!fonts.get(generator).containsKey(key)) {
|
||||||
|
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
|
||||||
|
parameters.size = size;
|
||||||
|
parameters.flip = flipped;
|
||||||
|
if (border) {
|
||||||
|
parameters.borderWidth = parameters.size / 10f;
|
||||||
|
}
|
||||||
|
if (size >= 20){
|
||||||
|
parameters.renderCount = 2;
|
||||||
|
} else {
|
||||||
|
parameters.renderCount = 3;
|
||||||
|
}
|
||||||
|
parameters.hinting = FreeTypeFontGenerator.Hinting.None;
|
||||||
|
parameters.spaceX = -(int) parameters.borderWidth;
|
||||||
|
parameters.incremental = true;
|
||||||
|
parameters.characters = "<EFBFBD>";
|
||||||
|
parameters.packer = packer;
|
||||||
|
|
||||||
|
try {
|
||||||
|
BitmapFont font = generator.generateFont(parameters);
|
||||||
|
font.getData().missingGlyph = font.getData().getGlyph('<27>');
|
||||||
|
fonts.get(generator).put(key, font);
|
||||||
|
} catch ( Exception e ){
|
||||||
|
Game.reportException(e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fonts.get(generator).get(key);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -178,27 +178,14 @@ public class AndroidPlatformSupport extends PlatformSupport {
|
|||||||
|
|
||||||
/* FONT SUPPORT */
|
/* FONT SUPPORT */
|
||||||
|
|
||||||
private int pageSize;
|
|
||||||
private PixmapPacker packer;
|
|
||||||
private boolean systemfont;
|
|
||||||
|
|
||||||
//droid sans / roboto, or a custom pixel font, for use with Latin and Cyrillic languages
|
//droid sans / roboto, or a custom pixel font, for use with Latin and Cyrillic languages
|
||||||
private static FreeTypeFontGenerator basicFontGenerator;
|
private static FreeTypeFontGenerator basicFontGenerator;
|
||||||
private static HashMap<Integer, BitmapFont> basicFonts = new HashMap<>();
|
|
||||||
|
|
||||||
//droid sans / nanum gothic / noto sans, for use with Korean
|
//droid sans / nanum gothic / noto sans, for use with Korean
|
||||||
private static FreeTypeFontGenerator KRFontGenerator;
|
private static FreeTypeFontGenerator KRFontGenerator;
|
||||||
private static HashMap<Integer, BitmapFont> KRFonts = new HashMap<>();
|
|
||||||
|
|
||||||
//droid sans / noto sans, for use with Simplified Chinese
|
//droid sans / noto sans, for use with Simplified Chinese
|
||||||
private static FreeTypeFontGenerator SCFontGenerator;
|
private static FreeTypeFontGenerator SCFontGenerator;
|
||||||
private static HashMap<Integer, BitmapFont> SCFonts = new HashMap<>();
|
|
||||||
|
|
||||||
//droid sans / noto sans, for use with Japanese
|
//droid sans / noto sans, for use with Japanese
|
||||||
private static FreeTypeFontGenerator JPFontGenerator;
|
private static FreeTypeFontGenerator JPFontGenerator;
|
||||||
private static HashMap<Integer, BitmapFont> JPFonts = new HashMap<>();
|
|
||||||
|
|
||||||
private static HashMap<FreeTypeFontGenerator, HashMap<Integer, BitmapFont>> fonts;
|
|
||||||
|
|
||||||
//special logic for handling korean android 6.0 font oddities
|
//special logic for handling korean android 6.0 font oddities
|
||||||
private static boolean koreanAndroid6OTF = false;
|
private static boolean koreanAndroid6OTF = false;
|
||||||
@@ -211,23 +198,8 @@ public class AndroidPlatformSupport extends PlatformSupport {
|
|||||||
}
|
}
|
||||||
this.pageSize = pageSize;
|
this.pageSize = pageSize;
|
||||||
this.systemfont = systemfont;
|
this.systemfont = systemfont;
|
||||||
|
|
||||||
if (fonts != null){
|
resetGenerators(false);
|
||||||
for (FreeTypeFontGenerator generator : fonts.keySet()){
|
|
||||||
for (BitmapFont f : fonts.get(generator).values()){
|
|
||||||
f.dispose();
|
|
||||||
}
|
|
||||||
fonts.get(generator).clear();
|
|
||||||
generator.dispose();
|
|
||||||
}
|
|
||||||
fonts.clear();
|
|
||||||
if (packer != null){
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()){
|
|
||||||
p.getTexture().dispose();
|
|
||||||
}
|
|
||||||
packer.dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fonts = new HashMap<>();
|
fonts = new HashMap<>();
|
||||||
basicFontGenerator = KRFontGenerator = SCFontGenerator = JPFontGenerator = null;
|
basicFontGenerator = KRFontGenerator = SCFontGenerator = JPFontGenerator = null;
|
||||||
|
|
||||||
@@ -294,62 +266,21 @@ public class AndroidPlatformSupport extends PlatformSupport {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (basicFontGenerator != null) fonts.put(basicFontGenerator, basicFonts);
|
if (basicFontGenerator != null) fonts.put(basicFontGenerator, new HashMap<>());
|
||||||
if (KRFontGenerator != null) fonts.put(KRFontGenerator, KRFonts);
|
if (KRFontGenerator != null) fonts.put(KRFontGenerator, new HashMap<>());
|
||||||
if (SCFontGenerator != null) fonts.put(SCFontGenerator, SCFonts);
|
if (SCFontGenerator != null) fonts.put(SCFontGenerator, new HashMap<>());
|
||||||
if (JPFontGenerator != null) fonts.put(JPFontGenerator, JPFonts);
|
if (JPFontGenerator != null) fonts.put(JPFontGenerator, new HashMap<>());
|
||||||
|
|
||||||
//would be nice to use RGBA4444 to save memory, but this causes problems on some gpus =S
|
//would be nice to use RGBA4444 to save memory, but this causes problems on some gpus =S
|
||||||
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
|
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void resetGenerators() {
|
|
||||||
if (fonts != null) {
|
|
||||||
for (FreeTypeFontGenerator generator : fonts.keySet()) {
|
|
||||||
for (BitmapFont f : fonts.get(generator).values()) {
|
|
||||||
f.dispose();
|
|
||||||
}
|
|
||||||
fonts.get(generator).clear();
|
|
||||||
generator.dispose();
|
|
||||||
}
|
|
||||||
fonts.clear();
|
|
||||||
if (packer != null) {
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()) {
|
|
||||||
p.getTexture().dispose();
|
|
||||||
}
|
|
||||||
packer.dispose();
|
|
||||||
}
|
|
||||||
fonts = null;
|
|
||||||
}
|
|
||||||
setupFontGenerators(pageSize, systemfont);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
//FIXME it would be really nice to keep the local texture data and just re-send it to the GPU
|
|
||||||
public void reloadGenerators() {
|
|
||||||
if (packer != null) {
|
|
||||||
for (FreeTypeFontGenerator generator : fonts.keySet()) {
|
|
||||||
for (BitmapFont f : fonts.get(generator).values()) {
|
|
||||||
f.dispose();
|
|
||||||
}
|
|
||||||
fonts.get(generator).clear();
|
|
||||||
}
|
|
||||||
if (packer != null) {
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()) {
|
|
||||||
p.getTexture().dispose();
|
|
||||||
}
|
|
||||||
packer.dispose();
|
|
||||||
}
|
|
||||||
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Pattern KRMatcher = Pattern.compile("\\p{InHangul_Syllables}");
|
private static Pattern KRMatcher = Pattern.compile("\\p{InHangul_Syllables}");
|
||||||
private static Pattern SCMatcher = Pattern.compile("\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}");
|
private static Pattern SCMatcher = Pattern.compile("\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}");
|
||||||
private static Pattern JPMatcher = Pattern.compile("\\p{InHiragana}|\\p{InKatakana}");
|
private static Pattern JPMatcher = Pattern.compile("\\p{InHiragana}|\\p{InKatakana}");
|
||||||
|
|
||||||
private static FreeTypeFontGenerator getGeneratorForString( String input ){
|
@Override
|
||||||
|
protected FreeTypeFontGenerator getGeneratorForString( String input ){
|
||||||
if (KRMatcher.matcher(input).find()){
|
if (KRMatcher.matcher(input).find()){
|
||||||
return KRFontGenerator;
|
return KRFontGenerator;
|
||||||
} else if (SCMatcher.matcher(input).find()){
|
} else if (SCMatcher.matcher(input).find()){
|
||||||
@@ -361,49 +292,6 @@ public class AndroidPlatformSupport extends PlatformSupport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BitmapFont getFont(int size, String text) {
|
|
||||||
FreeTypeFontGenerator generator = getGeneratorForString(text);
|
|
||||||
|
|
||||||
if (generator == null){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fonts.get(generator).containsKey(size)) {
|
|
||||||
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
|
|
||||||
parameters.size = size;
|
|
||||||
parameters.flip = true;
|
|
||||||
parameters.borderWidth = parameters.size / 10f;
|
|
||||||
if (size >= 20){
|
|
||||||
parameters.renderCount = 2;
|
|
||||||
} else {
|
|
||||||
parameters.renderCount = 3;
|
|
||||||
}
|
|
||||||
parameters.hinting = FreeTypeFontGenerator.Hinting.None;
|
|
||||||
parameters.spaceX = -(int) parameters.borderWidth;
|
|
||||||
parameters.incremental = true;
|
|
||||||
if (generator == basicFontGenerator){
|
|
||||||
//if we're using latin/cyrillic, we can safely pre-generate some common letters
|
|
||||||
//(we define common as >4% frequency in english)
|
|
||||||
parameters.characters = "<EFBFBD>etaoinshrdl";
|
|
||||||
} else {
|
|
||||||
parameters.characters = "<EFBFBD>";
|
|
||||||
}
|
|
||||||
parameters.packer = packer;
|
|
||||||
|
|
||||||
try {
|
|
||||||
BitmapFont font = generator.generateFont(parameters);
|
|
||||||
font.getData().missingGlyph = font.getData().getGlyph('<27>');
|
|
||||||
fonts.get(generator).put(size, font);
|
|
||||||
} catch ( Exception e ){
|
|
||||||
Game.reportException(e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fonts.get(generator).get(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
//splits on newlines, underscores, and chinese/japaneses characters
|
//splits on newlines, underscores, and chinese/japaneses characters
|
||||||
private Pattern regularsplitter = Pattern.compile(
|
private Pattern regularsplitter = Pattern.compile(
|
||||||
"(?<=\n)|(?=\n)|(?<=_)|(?=_)|" +
|
"(?<=\n)|(?=\n)|(?<=_)|(?=_)|" +
|
||||||
|
|||||||
@@ -80,20 +80,13 @@ public class DesktopPlatformSupport extends PlatformSupport {
|
|||||||
callback.onSelect(true, result.replace("\r\n", "").replace("\n", ""));
|
callback.onSelect(true, result.replace("\r\n", "").replace("\n", ""));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int pageSize;
|
/* FONT SUPPORT */
|
||||||
private PixmapPacker packer;
|
|
||||||
private boolean systemfont;
|
|
||||||
|
|
||||||
//custom pixel font, for use with Latin and Cyrillic languages
|
//custom pixel font, for use with Latin and Cyrillic languages
|
||||||
private static FreeTypeFontGenerator basicFontGenerator;
|
private static FreeTypeFontGenerator basicFontGenerator;
|
||||||
private static HashMap<Integer, BitmapFont> basicFonts = new HashMap<>();
|
|
||||||
|
|
||||||
//droid sans fallback, for asian fonts
|
//droid sans fallback, for asian fonts
|
||||||
private static FreeTypeFontGenerator asianFontGenerator;
|
private static FreeTypeFontGenerator asianFontGenerator;
|
||||||
private static HashMap<Integer, BitmapFont> asianFonts = new HashMap<>();
|
|
||||||
|
|
||||||
private static HashMap<FreeTypeFontGenerator, HashMap<Integer, BitmapFont>> fonts;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setupFontGenerators(int pageSize, boolean systemfont) {
|
public void setupFontGenerators(int pageSize, boolean systemfont) {
|
||||||
@@ -103,23 +96,8 @@ public class DesktopPlatformSupport extends PlatformSupport {
|
|||||||
}
|
}
|
||||||
this.pageSize = pageSize;
|
this.pageSize = pageSize;
|
||||||
this.systemfont = systemfont;
|
this.systemfont = systemfont;
|
||||||
|
|
||||||
if (fonts != null){
|
resetGenerators(false);
|
||||||
for (FreeTypeFontGenerator generator : fonts.keySet()){
|
|
||||||
for (BitmapFont f : fonts.get(generator).values()){
|
|
||||||
f.dispose();
|
|
||||||
}
|
|
||||||
fonts.get(generator).clear();
|
|
||||||
generator.dispose();
|
|
||||||
}
|
|
||||||
fonts.clear();
|
|
||||||
if (packer != null){
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()){
|
|
||||||
p.getTexture().dispose();
|
|
||||||
}
|
|
||||||
packer.dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fonts = new HashMap<>();
|
fonts = new HashMap<>();
|
||||||
|
|
||||||
if (systemfont) {
|
if (systemfont) {
|
||||||
@@ -129,49 +107,18 @@ public class DesktopPlatformSupport extends PlatformSupport {
|
|||||||
asianFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/droid_sans.ttf"));
|
asianFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/droid_sans.ttf"));
|
||||||
}
|
}
|
||||||
|
|
||||||
fonts.put(basicFontGenerator, basicFonts);
|
fonts.put(basicFontGenerator, new HashMap<>());
|
||||||
fonts.put(asianFontGenerator, asianFonts);
|
fonts.put(asianFontGenerator, new HashMap<>());
|
||||||
|
|
||||||
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
|
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void resetGenerators() {
|
|
||||||
if (fonts != null) {
|
|
||||||
for (FreeTypeFontGenerator generator : fonts.keySet()) {
|
|
||||||
for (BitmapFont f : fonts.get(generator).values()) {
|
|
||||||
f.dispose();
|
|
||||||
}
|
|
||||||
fonts.get(generator).clear();
|
|
||||||
generator.dispose();
|
|
||||||
}
|
|
||||||
fonts.clear();
|
|
||||||
if (packer != null) {
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()) {
|
|
||||||
p.getTexture().dispose();
|
|
||||||
}
|
|
||||||
packer.dispose();
|
|
||||||
}
|
|
||||||
fonts = null;
|
|
||||||
}
|
|
||||||
setupFontGenerators(pageSize, systemfont);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reloadGenerators() {
|
|
||||||
if (packer != null) {
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()) {
|
|
||||||
p.getTexture().dispose();
|
|
||||||
p.updateTexture(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Pattern asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" +
|
private static Pattern asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" +
|
||||||
"\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}|" +
|
"\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}|" +
|
||||||
"\\p{InHiragana}|\\p{InKatakana}");
|
"\\p{InHiragana}|\\p{InKatakana}");
|
||||||
|
|
||||||
private static FreeTypeFontGenerator getGeneratorForString( String input ){
|
@Override
|
||||||
|
protected FreeTypeFontGenerator getGeneratorForString( String input ){
|
||||||
if (asianMatcher.matcher(input).find()){
|
if (asianMatcher.matcher(input).find()){
|
||||||
return asianFontGenerator;
|
return asianFontGenerator;
|
||||||
} else {
|
} else {
|
||||||
@@ -179,50 +126,6 @@ public class DesktopPlatformSupport extends PlatformSupport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BitmapFont getFont(int size, String text) {
|
|
||||||
FreeTypeFontGenerator generator = getGeneratorForString(text);
|
|
||||||
|
|
||||||
if (generator == null){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fonts.get(generator).containsKey(size)) {
|
|
||||||
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
|
|
||||||
parameters.size = size;
|
|
||||||
parameters.flip = true;
|
|
||||||
parameters.borderWidth = parameters.size / 10f;
|
|
||||||
if (size >= 20){
|
|
||||||
parameters.renderCount = 2;
|
|
||||||
} else {
|
|
||||||
parameters.renderCount = 3;
|
|
||||||
}
|
|
||||||
parameters.hinting = FreeTypeFontGenerator.Hinting.None;
|
|
||||||
parameters.spaceX = -(int) parameters.borderWidth;
|
|
||||||
parameters.incremental = true;
|
|
||||||
if (generator == basicFontGenerator){
|
|
||||||
//if we're using latin/cyrillic, we can safely pre-generate some common letters
|
|
||||||
//(we define common as >4% frequency in english)
|
|
||||||
parameters.characters = "<EFBFBD>etaoinshrdl";
|
|
||||||
} else {
|
|
||||||
parameters.characters = "<EFBFBD>";
|
|
||||||
}
|
|
||||||
parameters.packer = packer;
|
|
||||||
|
|
||||||
try {
|
|
||||||
BitmapFont font = generator.generateFont(parameters);
|
|
||||||
font.getData().missingGlyph = font.getData().getGlyph('<27>');
|
|
||||||
fonts.get(generator).put(size, font);
|
|
||||||
} catch ( Exception e ){
|
|
||||||
Game.reportException(e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fonts.get(generator).get(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
//splits on newlines, underscores, and chinese/japaneses characters
|
//splits on newlines, underscores, and chinese/japaneses characters
|
||||||
private Pattern regularsplitter = Pattern.compile(
|
private Pattern regularsplitter = Pattern.compile(
|
||||||
"(?<=\n)|(?=\n)|(?<=_)|(?=_)|" +
|
"(?<=\n)|(?=\n)|(?<=_)|(?=_)|" +
|
||||||
@@ -247,5 +150,4 @@ public class DesktopPlatformSupport extends PlatformSupport {
|
|||||||
return regularsplitter.split(text);
|
return regularsplitter.split(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,19 +66,12 @@ public class IOSPlatformSupport extends PlatformSupport {
|
|||||||
AudioServices.playSystemSound(1520);
|
AudioServices.playSystemSound(1520);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int pageSize;
|
/* FONT SUPPORT */
|
||||||
private PixmapPacker packer;
|
|
||||||
private boolean systemfont;
|
|
||||||
|
|
||||||
//custom pixel font, for use with Latin and Cyrillic languages
|
//custom pixel font, for use with Latin and Cyrillic languages
|
||||||
private static FreeTypeFontGenerator basicFontGenerator;
|
private static FreeTypeFontGenerator basicFontGenerator;
|
||||||
private static HashMap<Integer, BitmapFont> basicFonts = new HashMap<>();
|
|
||||||
|
|
||||||
//droid sans fallback, for asian fonts
|
//droid sans fallback, for asian fonts
|
||||||
private static FreeTypeFontGenerator asianFontGenerator;
|
private static FreeTypeFontGenerator asianFontGenerator;
|
||||||
private static HashMap<Integer, BitmapFont> asianFonts = new HashMap<>();
|
|
||||||
|
|
||||||
private static HashMap<FreeTypeFontGenerator, HashMap<Integer, BitmapFont>> fonts;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setupFontGenerators(int pageSize, boolean systemfont) {
|
public void setupFontGenerators(int pageSize, boolean systemfont) {
|
||||||
@@ -89,22 +82,7 @@ public class IOSPlatformSupport extends PlatformSupport {
|
|||||||
this.pageSize = pageSize;
|
this.pageSize = pageSize;
|
||||||
this.systemfont = systemfont;
|
this.systemfont = systemfont;
|
||||||
|
|
||||||
if (fonts != null){
|
resetGenerators(false);
|
||||||
for (FreeTypeFontGenerator generator : fonts.keySet()){
|
|
||||||
for (BitmapFont f : fonts.get(generator).values()){
|
|
||||||
f.dispose();
|
|
||||||
}
|
|
||||||
fonts.get(generator).clear();
|
|
||||||
generator.dispose();
|
|
||||||
}
|
|
||||||
fonts.clear();
|
|
||||||
if (packer != null){
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()){
|
|
||||||
p.getTexture().dispose();
|
|
||||||
}
|
|
||||||
packer.dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fonts = new HashMap<>();
|
fonts = new HashMap<>();
|
||||||
|
|
||||||
if (systemfont) {
|
if (systemfont) {
|
||||||
@@ -114,49 +92,18 @@ public class IOSPlatformSupport extends PlatformSupport {
|
|||||||
asianFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/droid_sans.ttf"));
|
asianFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/droid_sans.ttf"));
|
||||||
}
|
}
|
||||||
|
|
||||||
fonts.put(basicFontGenerator, basicFonts);
|
fonts.put(basicFontGenerator, new HashMap<>());
|
||||||
fonts.put(asianFontGenerator, asianFonts);
|
fonts.put(asianFontGenerator, new HashMap<>());
|
||||||
|
|
||||||
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
|
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void resetGenerators() {
|
|
||||||
if (fonts != null) {
|
|
||||||
for (FreeTypeFontGenerator generator : fonts.keySet()) {
|
|
||||||
for (BitmapFont f : fonts.get(generator).values()) {
|
|
||||||
f.dispose();
|
|
||||||
}
|
|
||||||
fonts.get(generator).clear();
|
|
||||||
generator.dispose();
|
|
||||||
}
|
|
||||||
fonts.clear();
|
|
||||||
if (packer != null) {
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()) {
|
|
||||||
p.getTexture().dispose();
|
|
||||||
}
|
|
||||||
packer.dispose();
|
|
||||||
}
|
|
||||||
fonts = null;
|
|
||||||
}
|
|
||||||
setupFontGenerators(pageSize, systemfont);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reloadGenerators() {
|
|
||||||
if (packer != null) {
|
|
||||||
for (PixmapPacker.Page p : packer.getPages()) {
|
|
||||||
p.getTexture().dispose();
|
|
||||||
p.updateTexture(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Pattern asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" +
|
private static Pattern asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" +
|
||||||
"\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}|" +
|
"\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}|" +
|
||||||
"\\p{InHiragana}|\\p{InKatakana}");
|
"\\p{InHiragana}|\\p{InKatakana}");
|
||||||
|
|
||||||
private static FreeTypeFontGenerator getGeneratorForString( String input ){
|
@Override
|
||||||
|
protected FreeTypeFontGenerator getGeneratorForString( String input ){
|
||||||
if (asianMatcher.matcher(input).find()){
|
if (asianMatcher.matcher(input).find()){
|
||||||
return asianFontGenerator;
|
return asianFontGenerator;
|
||||||
} else {
|
} else {
|
||||||
@@ -164,50 +111,6 @@ public class IOSPlatformSupport extends PlatformSupport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BitmapFont getFont(int size, String text) {
|
|
||||||
FreeTypeFontGenerator generator = getGeneratorForString(text);
|
|
||||||
|
|
||||||
if (generator == null){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fonts.get(generator).containsKey(size)) {
|
|
||||||
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
|
|
||||||
parameters.size = size;
|
|
||||||
parameters.flip = true;
|
|
||||||
parameters.borderWidth = parameters.size / 10f;
|
|
||||||
if (size >= 20){
|
|
||||||
parameters.renderCount = 2;
|
|
||||||
} else {
|
|
||||||
parameters.renderCount = 3;
|
|
||||||
}
|
|
||||||
parameters.hinting = FreeTypeFontGenerator.Hinting.None;
|
|
||||||
parameters.spaceX = -(int) parameters.borderWidth;
|
|
||||||
parameters.incremental = true;
|
|
||||||
if (generator == basicFontGenerator){
|
|
||||||
//if we're using latin/cyrillic, we can safely pre-generate some common letters
|
|
||||||
//(we define common as >4% frequency in english)
|
|
||||||
parameters.characters = "<EFBFBD>etaoinshrdl";
|
|
||||||
} else {
|
|
||||||
parameters.characters = "<EFBFBD>";
|
|
||||||
}
|
|
||||||
parameters.packer = packer;
|
|
||||||
|
|
||||||
try {
|
|
||||||
BitmapFont font = generator.generateFont(parameters);
|
|
||||||
font.getData().missingGlyph = font.getData().getGlyph('<27>');
|
|
||||||
fonts.get(generator).put(size, font);
|
|
||||||
} catch ( Exception e ){
|
|
||||||
Game.reportException(e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fonts.get(generator).get(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
//splits on newlines, underscores, and chinese/japaneses characters
|
//splits on newlines, underscores, and chinese/japaneses characters
|
||||||
private Pattern regularsplitter = Pattern.compile(
|
private Pattern regularsplitter = Pattern.compile(
|
||||||
"(?<=\n)|(?=\n)|(?<=_)|(?=_)|" +
|
"(?<=\n)|(?=\n)|(?<=_)|(?=_)|" +
|
||||||
|
|||||||
Reference in New Issue
Block a user