v0.7.5b: first major portion of rewriting text rendering

some rough edges still need smoothing out
This commit is contained in:
Evan Debenham
2019-10-10 22:20:23 -04:00
parent bbee1a1372
commit b985ed3bf5
14 changed files with 393 additions and 214 deletions

3
android/.gitignore vendored
View File

@@ -1,2 +1,3 @@
#LibGDX native dependancies
libgdx.so
libgdx.so
libgdx-freetype.so

View File

@@ -69,6 +69,7 @@ dependencies {
implementation project(':core')
implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
@@ -76,6 +77,11 @@ dependencies {
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
implementation "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
implementation "com.badlogicgames.gdx:gdx-controllers-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
}
// called every time gradle gets executed, takes the native dependencies of

View File

@@ -27,12 +27,20 @@ import android.os.Build;
import android.view.View;
import android.view.WindowManager;
import com.badlogic.gdx.Gdx;
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.shatteredpixel.shatteredpixeldungeon.SPDSettings;
import com.shatteredpixel.shatteredpixeldungeon.android.windows.WndAndroidTextInput;
import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene;
import com.watabou.noosa.Game;
import com.watabou.utils.PlatformSupport;
import java.util.HashMap;
import java.util.regex.Pattern;
public class AndroidPlatformSupport extends PlatformSupport {
public void updateDisplaySize(){
@@ -140,4 +148,147 @@ public class AndroidPlatformSupport extends PlatformSupport {
}
});
}
/* FONT SUPPORT */
private int pageSize;
private PixmapPacker packer;
private boolean systemfont;
//custom ttf or droid sans, for use with Latin and Cyrillic languages
private static FreeTypeFontGenerator latinAndCryllicFontGenerator;
private static HashMap<Integer, BitmapFont> pixelFonts = new HashMap<>();
//droid sans, for use with hangul languages (Korean)
private static FreeTypeFontGenerator hangulFontGenerator;
private static HashMap<Integer, BitmapFont> hangulFonts = new HashMap<>();
//droid sans, for use with han languages (Chinese, Japanese)
private static FreeTypeFontGenerator hanFontGenerator;
private static HashMap<Integer, BitmapFont> hanFonts = new HashMap<>();
private static HashMap<FreeTypeFontGenerator, HashMap<Integer, BitmapFont>> fonts;
public static Pattern hanMatcher = Pattern.compile("\\p{InHiragana}|\\p{InKatakana}|\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}");
public static Pattern hangulMatcher = Pattern.compile("\\p{InHangul_Syllables}");
@Override
public void setupFontGenerators(int pageSize, boolean 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 (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 = new HashMap<>();
if (systemfont){
latinAndCryllicFontGenerator = new FreeTypeFontGenerator(Gdx.files.absolute("/system/fonts/DroidSans.ttf"));
} else {
//FIXME need to add currency symbols
latinAndCryllicFontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("pixelfont.ttf"));
}
//android 7.0+. Finally back to normalcy, everything nicely in one .ttc
if (Gdx.files.absolute("/system/fonts/NotoSansCJK-Regular.ttc").exists()) {
//TODO why typeface #2 here? It seems to match DroidSansFallback.ttf the best, but why?
//might be that different languages prefer a different face, see about tweaking this?
hangulFontGenerator = hanFontGenerator = new FreeTypeFontGenerator(Gdx.files.absolute("/system/fonts/NotoSansCJK-Regular.ttc"), 2);
//android 6.0. Fonts are split over multiple .otf files, very awkward
} else if (Gdx.files.absolute("/system/fonts/NotoSansKR-Regular.otf").exists()) {
//FIXME all fonts are messed up here currently, need to fix this
hangulFontGenerator = new FreeTypeFontGenerator(Gdx.files.absolute("/system/fonts/NotoSansKR-Regular.otf"));
hanFontGenerator = new FreeTypeFontGenerator(Gdx.files.absolute("/system/fonts/NotoSansSC-Regular.otf"));
//android 4.4-5.1. Korean no longer broken with the addition of NanumGothic.
} else if (Gdx.files.absolute("/system/fonts/NanumGothic.ttf").exists()){
hangulFontGenerator = new FreeTypeFontGenerator(Gdx.files.absolute("/system/fonts/NanumGothic.ttf"));
hanFontGenerator = new FreeTypeFontGenerator(Gdx.files.absolute("/system/fonts/DroidSansFallback.ttf"));
//android 4.3-. Note that korean isn't in DroidSandFallback and is therefore unfixably broken on 4.2 and 4.3
} else if (Gdx.files.absolute("/system/fonts/DroidSansFallback.ttf").exists()) {
hangulFontGenerator = hanFontGenerator = new FreeTypeFontGenerator(Gdx.files.absolute("/system/fonts/DroidSansFallback.ttf"));
//shouldn't ever trigger, but just incase
} else {
hangulFontGenerator = hanFontGenerator = latinAndCryllicFontGenerator;
}
fonts.put(latinAndCryllicFontGenerator, pixelFonts);
fonts.put(hangulFontGenerator, hangulFonts);
fonts.put(hanFontGenerator, hanFonts);
//use RGBA4444 to save memory. Extra precision isn't needed here.
packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA4444, 1, false);
}
@Override
public void resetGenerators() {
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);
}
private static FreeTypeFontGenerator getGeneratorForString( String input ){
if (hanMatcher.matcher(input).find()){
return hanFontGenerator;
} else if (hangulMatcher.matcher(input).find()){
return hangulFontGenerator;
} else {
return latinAndCryllicFontGenerator;
}
}
@Override
public BitmapFont getFont(int size, String text) {
FreeTypeFontGenerator generator = getGeneratorForString(text);
if (!fonts.get(generator).containsKey(size)) {
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameters.size = size;
parameters.flip = true;
parameters.borderWidth = parameters.size / 10f;
parameters.renderCount = 3;
parameters.hinting = FreeTypeFontGenerator.Hinting.None;
parameters.spaceX = -(int) parameters.borderWidth;
parameters.incremental = true;
parameters.characters = "";
parameters.packer = packer;
fonts.get(generator).put(size, generator.generateFont(parameters));
}
return fonts.get(generator).get(size);
}
}

View File

@@ -22,6 +22,7 @@
package com.shatteredpixel.shatteredpixeldungeon.android.windows;
import android.app.Activity;
import android.graphics.Typeface;
import android.text.InputFilter;
import android.text.InputType;
import android.util.TypedValue;
@@ -39,12 +40,12 @@ import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidGraphics;
import com.shatteredpixel.shatteredpixeldungeon.SPDSettings;
import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon;
import com.shatteredpixel.shatteredpixeldungeon.android.AndroidLauncher;
import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene;
import com.shatteredpixel.shatteredpixeldungeon.ui.RedButton;
import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextMultiline;
import com.shatteredpixel.shatteredpixeldungeon.ui.Window;
import com.watabou.noosa.Game;
import com.watabou.noosa.RenderedText;
//This class makes use of the android EditText component to handle text input
//FIXME this window is currently android-specific, should generalize it
@@ -96,7 +97,9 @@ public class WndAndroidTextInput extends Window {
textInput = new EditText((AndroidApplication)Gdx.app);
textInput.setText( initialValue );
textInput.setTypeface( RenderedText.getFont() );
if (!SPDSettings.systemFont()){
textInput.setTypeface( Typeface.createFromAsset(AndroidLauncher.instance.getAssets(), "pixelfont.ttf") );
}
textInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
textInput.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES );