mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-27 23:33:03 +00:00
String method replacements (#169)
This commit is contained in:
1 parent
207eaef07d
commit
d8c270c609
5 files changed
+312
-1
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.util.EarlyConfig;
|
||||
import com.gtnewhorizon.gtnhmixins.builders.IMixins;
|
||||
import com.gtnewhorizon.gtnhmixins.builders.MixinBuilder;
|
||||
|
||||
@@ -20,7 +21,9 @@ public enum Mixins implements IMixins {
|
||||
GAME_RULES_API(Side.COMMON, "MixinGameRules"),
|
||||
DEBUG_TEXTURES(new MixinBuilder("Dump textures sizes")
|
||||
.addClientMixins("debug.MixinDynamicTexture", "debug.MixinTextureAtlasSprite").setPhase(Phase.EARLY)
|
||||
.setApplyIf(() -> Boolean.parseBoolean(System.getProperty("gtnhlib.debugtextures", "false"))));
|
||||
.setApplyIf(() -> Boolean.parseBoolean(System.getProperty("gtnhlib.debugtextures", "false")))),
|
||||
FONT_RENDERER(new MixinBuilder("Font rendering replacements").addClientMixins("MixinFontRenderer")
|
||||
.setPhase(Phase.EARLY).setApplyIf(() -> EarlyConfig.enableFontRendererMixin));
|
||||
|
||||
private final MixinBuilder builder;
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins.early;
|
||||
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Overwrite;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.util.font.FontRendering;
|
||||
import com.gtnewhorizon.gtnhlib.util.font.IFontParameters;
|
||||
|
||||
// Low priority to allow for other overrides
|
||||
@Mixin(value = FontRenderer.class, priority = 200)
|
||||
public abstract class MixinFontRenderer implements IFontParameters {
|
||||
|
||||
@Shadow
|
||||
public abstract int getCharWidth(char chr);
|
||||
|
||||
@Override
|
||||
public float getCharWidthFine(char chr) {
|
||||
return getCharWidth(chr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @author DeathFuel
|
||||
* @reason Replace with an implementation that respects color code reset rules and works with Angelica custom fonts
|
||||
*/
|
||||
@Overwrite
|
||||
public int getStringWidth(String text) {
|
||||
return FontRendering.getStringWidth(text, (FontRenderer) (Object) this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @author DeathFuel
|
||||
* @reason (as above)
|
||||
*/
|
||||
@Overwrite
|
||||
protected int sizeStringToWidth(String str, int wrapWidth) {
|
||||
return FontRendering.sizeStringToWidth(str, wrapWidth, (FontRenderer) (Object) this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @author DeathFuel
|
||||
* @reason (as above)
|
||||
*/
|
||||
@Overwrite
|
||||
public String trimStringToWidth(String str, int trimWidth, boolean reverse) {
|
||||
return FontRendering.trimStringToWidth(str, trimWidth, reverse, (FontRenderer) (Object) this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.gtnewhorizon.gtnhlib.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.util.Properties;
|
||||
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
// Based on EarlyConfig.java from Hodgepodge
|
||||
public final class EarlyConfig {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger("GTNHLibEarly");
|
||||
|
||||
public static final boolean enableFontRendererMixin;
|
||||
|
||||
static {
|
||||
Properties config = new Properties();
|
||||
File configLocation = new File(Launch.minecraftHome, "config/gtnhlibEarly.properties");
|
||||
try (Reader r = new BufferedReader(new FileReader(configLocation))) {
|
||||
config.load(r);
|
||||
} catch (FileNotFoundException e) {
|
||||
LOGGER.debug("No existing configuration file. Will use defaults");
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error reading configuration file. Will use defaults", e);
|
||||
}
|
||||
enableFontRendererMixin = Boolean.parseBoolean(config.getProperty("enableFontRendererMixin", "true"));
|
||||
config.setProperty("enableFontRendererMixin", String.valueOf(enableFontRendererMixin));
|
||||
try (Writer r = new BufferedWriter(new FileWriter(configLocation))) {
|
||||
config.store(r, "Config file for anything that's loaded before GTNHLibConfig");
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error writing configuration file", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package com.gtnewhorizon.gtnhlib.util.font;
|
||||
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
|
||||
// Common font rendering utilities that may be better-behaved than vanilla counterparts
|
||||
public class FontRendering {
|
||||
|
||||
private static final char FORMATTING_CHAR = 167; // §
|
||||
|
||||
public static boolean charInRange(char what, char fromInclusive, char toInclusive) {
|
||||
return (what >= fromInclusive) && (what <= toInclusive);
|
||||
}
|
||||
|
||||
private static boolean determineIfBold(boolean wasBold, char fmtChar) {
|
||||
char c = Character.toLowerCase(fmtChar);
|
||||
if (c == 'l') {
|
||||
return true;
|
||||
} else {
|
||||
final boolean is09 = charInRange(c, '0', '9');
|
||||
final boolean isAF = charInRange(c, 'a', 'f');
|
||||
if (c == 'r' || is09 || isAF) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return wasBold;
|
||||
}
|
||||
|
||||
/**
|
||||
* A getStringWidth implementation that respects formatting rules and works with custom fonts through Angelica.
|
||||
*/
|
||||
public static int getStringWidth(String str, FontRenderer fr) {
|
||||
|
||||
if (str == null || str.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
IFontParameters fontParams = (IFontParameters) fr;
|
||||
|
||||
float width = 0;
|
||||
boolean curBold = false;
|
||||
boolean spacingOmittedOnce = false;
|
||||
|
||||
for (int i = 0; i < str.length(); ++i) {
|
||||
char ch = str.charAt(i);
|
||||
float charWidth = fontParams.getCharWidthFine(ch);
|
||||
|
||||
if (charWidth < 0 && (i + 1) < str.length()) {
|
||||
i++;
|
||||
char fmtChar = str.charAt(i);
|
||||
curBold = determineIfBold(curBold, fmtChar);
|
||||
charWidth = 0;
|
||||
}
|
||||
|
||||
width += charWidth;
|
||||
|
||||
// Add glyph spacing (n-1) times for n characters
|
||||
if (charWidth > 0) {
|
||||
if (spacingOmittedOnce) {
|
||||
width += fontParams.getGlyphSpacing();
|
||||
} else {
|
||||
spacingOmittedOnce = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (curBold && charWidth > 0) {
|
||||
width += fontParams.getShadowOffset();
|
||||
}
|
||||
}
|
||||
|
||||
return (int) Math.ceil(width);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines how many characters from the string will fit into the specified width.
|
||||
*/
|
||||
public static int sizeStringToWidth(String str, int wrapWidth, FontRenderer fr) {
|
||||
|
||||
int originalStringLength = str.length();
|
||||
float width = 0;
|
||||
int i = 0;
|
||||
int lastBreakSpot = -1;
|
||||
boolean curBold = false;
|
||||
boolean spacingOmittedOnce = false;
|
||||
|
||||
IFontParameters fontParams = (IFontParameters) fr;
|
||||
|
||||
while (i < originalStringLength) {
|
||||
char currentChar = str.charAt(i);
|
||||
|
||||
if (currentChar == '\n') {
|
||||
lastBreakSpot = i;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (currentChar) {
|
||||
case FORMATTING_CHAR:
|
||||
if ((i + 1) < originalStringLength) {
|
||||
i++;
|
||||
char fmtChar = str.charAt(i);
|
||||
curBold = determineIfBold(curBold, fmtChar);
|
||||
}
|
||||
break;
|
||||
case ' ':
|
||||
lastBreakSpot = i;
|
||||
default:
|
||||
width += fontParams.getCharWidthFine(currentChar);
|
||||
|
||||
// Add glyph spacing (n-1) times for n characters
|
||||
if (spacingOmittedOnce) {
|
||||
width += fontParams.getGlyphSpacing();
|
||||
} else {
|
||||
spacingOmittedOnce = true;
|
||||
}
|
||||
|
||||
if (curBold) {
|
||||
width += fontParams.getShadowOffset();
|
||||
}
|
||||
}
|
||||
|
||||
if (Math.ceil(width) > wrapWidth) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i != originalStringLength && lastBreakSpot != -1 && lastBreakSpot < i) {
|
||||
return lastBreakSpot;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes characters from the string to trim its rendered length down to trimWidth, starting either from the end
|
||||
* (reverse = 0) or the beginning (reverse = 1).
|
||||
*/
|
||||
public static String trimStringToWidth(String str, int trimWidth, boolean reverse, FontRenderer fr) {
|
||||
|
||||
StringBuilder stringbuilder = new StringBuilder();
|
||||
float width = 0;
|
||||
int startOrEnd = reverse ? str.length() - 1 : 0;
|
||||
int increment = reverse ? -1 : 1;
|
||||
boolean curBold = false;
|
||||
boolean spacingOmittedOnce = false;
|
||||
boolean parsingFormatCode = false;
|
||||
|
||||
IFontParameters fontParams = (IFontParameters) fr;
|
||||
|
||||
for (int i = startOrEnd; i >= 0 && i < str.length() && width < trimWidth; i += increment) {
|
||||
char ch = str.charAt(i);
|
||||
float charWidth = fontParams.getCharWidthFine(ch);
|
||||
|
||||
if (parsingFormatCode) {
|
||||
parsingFormatCode = false;
|
||||
curBold = determineIfBold(curBold, ch);
|
||||
} else if (charWidth < 0) {
|
||||
parsingFormatCode = true;
|
||||
} else {
|
||||
width += charWidth;
|
||||
|
||||
// Add glyph spacing (n-1) times for n characters
|
||||
if (spacingOmittedOnce) {
|
||||
width += fontParams.getGlyphSpacing();
|
||||
} else {
|
||||
spacingOmittedOnce = true;
|
||||
}
|
||||
|
||||
if (curBold) {
|
||||
width += fontParams.getShadowOffset();
|
||||
}
|
||||
}
|
||||
|
||||
if (Math.ceil(width) > trimWidth) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (reverse) {
|
||||
stringbuilder.insert(0, ch);
|
||||
} else {
|
||||
stringbuilder.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
return stringbuilder.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.gtnewhorizon.gtnhlib.util.font;
|
||||
|
||||
/**
|
||||
* For use with Angelica's custom fonts
|
||||
*/
|
||||
public interface IFontParameters {
|
||||
|
||||
default float getGlyphScaleX() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
default float getGlyphScaleY() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
default float getGlyphSpacing() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
default float getWhitespaceScale() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
default float getShadowOffset() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
float getCharWidthFine(char chr);
|
||||
}
|
||||
Reference in new issue
Block a user