From d47c8d1fac5d409cd81ccedd54da142a18f3a963 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sun, 11 Jul 2021 14:42:14 -0400 Subject: [PATCH] v0.9.4: reduced allocations when text is being matches to a font --- .../android/AndroidPlatformSupport.java | 13 +++++++------ .../desktop/DesktopPlatformSupport.java | 11 ++++------- .../ios/IOSPlatformSupport.java | 7 ++++--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/android/src/main/java/com/shatteredpixel/shatteredpixeldungeon/android/AndroidPlatformSupport.java b/android/src/main/java/com/shatteredpixel/shatteredpixeldungeon/android/AndroidPlatformSupport.java index 039464b57..0067ae957 100644 --- a/android/src/main/java/com/shatteredpixel/shatteredpixeldungeon/android/AndroidPlatformSupport.java +++ b/android/src/main/java/com/shatteredpixel/shatteredpixeldungeon/android/AndroidPlatformSupport.java @@ -42,6 +42,7 @@ import com.watabou.noosa.Game; import com.watabou.utils.PlatformSupport; import java.util.HashMap; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class AndroidPlatformSupport extends PlatformSupport { @@ -264,17 +265,17 @@ public class AndroidPlatformSupport extends PlatformSupport { packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false); } - 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 JPMatcher = Pattern.compile("\\p{InHiragana}|\\p{InKatakana}"); + private static Matcher KRMatcher = Pattern.compile("\\p{InHangul_Syllables}").matcher(""); + private static Matcher SCMatcher = Pattern.compile("\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}").matcher(""); + private static Matcher JPMatcher = Pattern.compile("\\p{InHiragana}|\\p{InKatakana}").matcher(""); @Override protected FreeTypeFontGenerator getGeneratorForString( String input ){ - if (KRMatcher.matcher(input).find()){ + if (KRMatcher.reset(input).find()){ return KRFontGenerator; - } else if (SCMatcher.matcher(input).find()){ + } else if (SCMatcher.reset(input).find()){ return SCFontGenerator; - } else if (JPMatcher.matcher(input).find()){ + } else if (JPMatcher.reset(input).find()){ return JPFontGenerator; } else { return basicFontGenerator; diff --git a/desktop/src/main/java/com/shatteredpixel/shatteredpixeldungeon/desktop/DesktopPlatformSupport.java b/desktop/src/main/java/com/shatteredpixel/shatteredpixeldungeon/desktop/DesktopPlatformSupport.java index 0da144921..47021073a 100644 --- a/desktop/src/main/java/com/shatteredpixel/shatteredpixeldungeon/desktop/DesktopPlatformSupport.java +++ b/desktop/src/main/java/com/shatteredpixel/shatteredpixeldungeon/desktop/DesktopPlatformSupport.java @@ -23,8 +23,6 @@ package com.shatteredpixel.shatteredpixeldungeon.desktop; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Pixmap; -import com.badlogic.gdx.graphics.Texture; -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; @@ -32,9 +30,8 @@ import com.watabou.noosa.Game; import com.watabou.utils.PlatformSupport; import com.watabou.utils.Point; -import org.lwjgl.util.tinyfd.TinyFileDialogs; - import java.util.HashMap; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class DesktopPlatformSupport extends PlatformSupport { @@ -98,13 +95,13 @@ public class DesktopPlatformSupport extends PlatformSupport { packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false); } - private static Pattern asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" + + private static Matcher asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" + "\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}|" + - "\\p{InHiragana}|\\p{InKatakana}"); + "\\p{InHiragana}|\\p{InKatakana}").matcher(""); @Override protected FreeTypeFontGenerator getGeneratorForString( String input ){ - if (asianMatcher.matcher(input).find()){ + if (asianMatcher.reset(input).find()){ return asianFontGenerator; } else { return basicFontGenerator; diff --git a/ios/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ios/IOSPlatformSupport.java b/ios/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ios/IOSPlatformSupport.java index cfeecb79c..97a16e0b8 100644 --- a/ios/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ios/IOSPlatformSupport.java +++ b/ios/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ios/IOSPlatformSupport.java @@ -18,6 +18,7 @@ import org.robovm.apple.systemconfiguration.SCNetworkReachabilityFlags; import org.robovm.apple.uikit.UIApplication; import java.util.HashMap; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class IOSPlatformSupport extends PlatformSupport { @@ -97,13 +98,13 @@ public class IOSPlatformSupport extends PlatformSupport { packer = new PixmapPacker(pageSize, pageSize, Pixmap.Format.RGBA8888, 1, false); } - private static Pattern asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" + + private static final Matcher asianMatcher = Pattern.compile("\\p{InHangul_Syllables}|" + "\\p{InCJK_Unified_Ideographs}|\\p{InCJK_Symbols_and_Punctuation}|\\p{InHalfwidth_and_Fullwidth_Forms}|" + - "\\p{InHiragana}|\\p{InKatakana}"); + "\\p{InHiragana}|\\p{InKatakana}").matcher(""); @Override protected FreeTypeFontGenerator getGeneratorForString( String input ){ - if (asianMatcher.matcher(input).find()){ + if (asianMatcher.reset(input).find()){ return asianFontGenerator; } else { return basicFontGenerator;