v0.7.5e: added desktop support for asian languages

This commit is contained in:
Evan Debenham 2019-10-23 20:34:06 -04:00
parent 7181b5b677
commit 9f8791978a
4 changed files with 95 additions and 34 deletions

View File

@ -21,6 +21,8 @@
package com.shatteredpixel.shatteredpixeldungeon.messages; package com.shatteredpixel.shatteredpixeldungeon.messages;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.shatteredpixel.shatteredpixeldungeon.SPDSettings; import com.shatteredpixel.shatteredpixeldungeon.SPDSettings;
import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon;
@ -88,7 +90,17 @@ public class Messages {
while (keys.hasMoreElements()) { while (keys.hasMoreElements()) {
String key = keys.nextElement(); String key = keys.nextElement();
String value = bundle.getString(key); String value = bundle.getString(key);
//TODO do all desktop platforms read as ISO, or only windows?
// should also move this to platform support, probably.
if (Gdx.app.getType() == Application.ApplicationType.Desktop) {
try {
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e) {
ShatteredPixelDungeon.reportException(e);
}
}
strings.put(key, value); strings.put(key, value);
} }
} }

Binary file not shown.

View File

@ -23,25 +23,30 @@ apply plugin: "java"
sourceSets.main.java.srcDirs = [ "src/" ] sourceSets.main.java.srcDirs = [ "src/" ]
project.ext.mainClassName = "com.shatteredpixel.shatteredpixeldungeon.desktop.DesktopLauncher" ext {
project.ext.assetsDir = new File("../android/src/main/assets") mainClassName = "com.shatteredpixel.shatteredpixeldungeon.desktop.DesktopLauncher"
//desktop-specific resources and shared resources
//TODO shared resources can probably be moved out of android module, right?
assetsDirs = ["../android/src/main/assets", "assets/"]
}
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
task debug(type: JavaExec) { task runDebug(type: JavaExec) {
main = project.mainClassName main = mainClassName
classpath = sourceSets.main.runtimeClasspath classpath = sourceSets.main.runtimeClasspath
workingDir = project.assetsDir sourceSets.main.resources.srcDirs = assetsDirs
ignoreExitValue = true ignoreExitValue = true
} }
task releaseJAR(type: Jar) { task releaseJAR(type: Jar) {
from sourceSets.main.output from sourceSets.main.output
from project.assetsDir sourceSets.main.resources.srcDirs = assetsDirs
from { configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) } } from { configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) } }
manifest { manifest {
attributes 'Main-Class': project.mainClassName attributes 'Main-Class': mainClassName
attributes 'Specification-Name': appName attributes 'Specification-Name': appName
attributes 'Specification-Version': appVersionName attributes 'Specification-Version': appVersionName
attributes 'Implementation-Version': appVersionCode attributes 'Implementation-Version': appVersionCode

View File

@ -26,6 +26,7 @@ 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.PixmapPacker;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.watabou.noosa.Game;
import com.watabou.utils.PlatformSupport; import com.watabou.utils.PlatformSupport;
import java.util.HashMap; import java.util.HashMap;
@ -52,24 +53,34 @@ public class DesktopPlatformSupport extends PlatformSupport {
private PixmapPacker packer; private PixmapPacker packer;
private boolean systemfont; private boolean systemfont;
//droid sans / roboto, or a 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<>(); private static HashMap<Integer, BitmapFont> basicFonts = new HashMap<>();
//droid sans fallback, for asian fonts
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) {
if (basicFontGenerator != null && this.pageSize == pageSize && this.systemfont == systemfont){ //don't bother doing anything if nothing has changed
if (fonts != null && this.pageSize == pageSize && this.systemfont == systemfont){
return; return;
} }
this.pageSize = pageSize; this.pageSize = pageSize;
this.systemfont = systemfont; this.systemfont = systemfont;
if (basicFontGenerator != null){ if (fonts != null){
for (BitmapFont f : basicFonts.values()){ for (FreeTypeFontGenerator generator : fonts.keySet()){
f.dispose(); for (BitmapFont f : fonts.get(generator).values()){
f.dispose();
}
fonts.get(generator).clear();
generator.dispose();
} }
basicFonts.clear(); fonts.clear();
basicFontGenerator.dispose();
if (packer != null){ if (packer != null){
for (PixmapPacker.Page p : packer.getPages()){ for (PixmapPacker.Page p : packer.getPages()){
p.getTexture().dispose(); p.getTexture().dispose();
@ -77,37 +88,59 @@ public class DesktopPlatformSupport extends PlatformSupport {
packer.dispose(); packer.dispose();
} }
} }
fonts = new HashMap<>();
basicFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("pixel_font.ttf")); basicFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("pixel_font.ttf"));
asianFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("DroidSansFallback.ttf"));
fonts.put(basicFontGenerator, basicFonts);
fonts.put(asianFontGenerator, asianFonts);
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false); packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false);
} }
@Override @Override
public void resetGenerators() { public void resetGenerators() {
if (basicFontGenerator != null) { for (FreeTypeFontGenerator generator : fonts.keySet()){
for (BitmapFont f : basicFonts.values()) { for (BitmapFont f : fonts.get(generator).values()){
f.dispose(); f.dispose();
} }
basicFonts.clear(); fonts.get(generator).clear();
basicFontGenerator.dispose(); generator.dispose();
if (packer != null) {
for (PixmapPacker.Page p : packer.getPages()) {
p.getTexture().dispose();
}
packer.dispose();
}
} }
basicFontGenerator = null; fonts.clear();
if (packer != null){
for (PixmapPacker.Page p : packer.getPages()){
p.getTexture().dispose();
}
packer.dispose();
}
fonts = null;
setupFontGenerators(pageSize, systemfont); setupFontGenerators(pageSize, systemfont);
} }
private static Pattern asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" +
"\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}|" +
"\\p{InHiragana}|\\p{InKatakana}");
private static FreeTypeFontGenerator getGeneratorForString( String input ){
if (asianMatcher.matcher(input).find()){
return asianFontGenerator;
} else {
return basicFontGenerator;
}
}
@Override @Override
public BitmapFont getFont(int size, String text) { public BitmapFont getFont(int size, String text) {
FreeTypeFontGenerator generator = basicFontGenerator; FreeTypeFontGenerator generator = getGeneratorForString(text);
if (!basicFonts.containsKey(size)) { if (generator == null){
return null;
}
if (!fonts.get(generator).containsKey(size)) {
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter(); FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameters.size = size; parameters.size = size;
parameters.flip = true; parameters.flip = true;
@ -116,15 +149,26 @@ public class DesktopPlatformSupport extends PlatformSupport {
parameters.hinting = FreeTypeFontGenerator.Hinting.None; parameters.hinting = FreeTypeFontGenerator.Hinting.None;
parameters.spaceX = -(int) parameters.borderWidth; parameters.spaceX = -(int) parameters.borderWidth;
parameters.incremental = true; parameters.incremental = true;
parameters.characters = ""; 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; parameters.packer = packer;
BitmapFont font = generator.generateFont(parameters); try {
font.getData().missingGlyph = font.getData().getGlyph('<27>'); BitmapFont font = generator.generateFont(parameters);
basicFonts.put(size, font); font.getData().missingGlyph = font.getData().getGlyph('<27>');
fonts.get(generator).put(size, font);
} catch ( Exception e ){
Game.reportException(e);
return null;
}
} }
return basicFonts.get(size); return fonts.get(generator).get(size);
} }
//splits on newlines, underscores, and chinese/japaneses characters //splits on newlines, underscores, and chinese/japaneses characters