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;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.shatteredpixel.shatteredpixeldungeon.SPDSettings;
import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon;
@ -88,7 +90,17 @@ public class Messages {
while (keys.hasMoreElements()) {
String key = keys.nextElement();
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);
}
}

Binary file not shown.

View File

@ -23,25 +23,30 @@ apply plugin: "java"
sourceSets.main.java.srcDirs = [ "src/" ]
project.ext.mainClassName = "com.shatteredpixel.shatteredpixeldungeon.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../android/src/main/assets")
ext {
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'
task debug(type: JavaExec) {
main = project.mainClassName
task runDebug(type: JavaExec) {
main = mainClassName
classpath = sourceSets.main.runtimeClasspath
workingDir = project.assetsDir
sourceSets.main.resources.srcDirs = assetsDirs
ignoreExitValue = true
}
task releaseJAR(type: Jar) {
from sourceSets.main.output
from project.assetsDir
sourceSets.main.resources.srcDirs = assetsDirs
from { configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) } }
manifest {
attributes 'Main-Class': project.mainClassName
attributes 'Main-Class': mainClassName
attributes 'Specification-Name': appName
attributes 'Specification-Version': appVersionName
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.PixmapPacker;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.watabou.noosa.Game;
import com.watabou.utils.PlatformSupport;
import java.util.HashMap;
@ -52,24 +53,34 @@ public class DesktopPlatformSupport extends PlatformSupport {
private PixmapPacker packer;
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 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
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;
}
this.pageSize = pageSize;
this.systemfont = systemfont;
if (basicFontGenerator != null){
for (BitmapFont f : basicFonts.values()){
f.dispose();
if (fonts != null){
for (FreeTypeFontGenerator generator : fonts.keySet()){
for (BitmapFont f : fonts.get(generator).values()){
f.dispose();
}
fonts.get(generator).clear();
generator.dispose();
}
basicFonts.clear();
basicFontGenerator.dispose();
fonts.clear();
if (packer != null){
for (PixmapPacker.Page p : packer.getPages()){
p.getTexture().dispose();
@ -77,37 +88,59 @@ public class DesktopPlatformSupport extends PlatformSupport {
packer.dispose();
}
}
fonts = new HashMap<>();
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);
}
@Override
public void resetGenerators() {
if (basicFontGenerator != null) {
for (BitmapFont f : basicFonts.values()) {
for (FreeTypeFontGenerator generator : fonts.keySet()){
for (BitmapFont f : fonts.get(generator).values()){
f.dispose();
}
basicFonts.clear();
basicFontGenerator.dispose();
if (packer != null) {
for (PixmapPacker.Page p : packer.getPages()) {
p.getTexture().dispose();
}
packer.dispose();
}
fonts.get(generator).clear();
generator.dispose();
}
basicFontGenerator = null;
fonts.clear();
if (packer != null){
for (PixmapPacker.Page p : packer.getPages()){
p.getTexture().dispose();
}
packer.dispose();
}
fonts = null;
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
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();
parameters.size = size;
parameters.flip = true;
@ -116,15 +149,26 @@ public class DesktopPlatformSupport extends PlatformSupport {
parameters.hinting = FreeTypeFontGenerator.Hinting.None;
parameters.spaceX = -(int) parameters.borderWidth;
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;
BitmapFont font = generator.generateFont(parameters);
font.getData().missingGlyph = font.getData().getGlyph('<27>');
basicFonts.put(size, font);
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 basicFonts.get(size);
return fonts.get(generator).get(size);
}
//splits on newlines, underscores, and chinese/japaneses characters