Feat: Add Jello and Montserrat font families

- Register Jello Light/Regular/Medium and Montserrat Light/Regular/Medium/Bold, with weights lacking a bold file reusing a heavier face

- Add Render2D.getFont(FontFamily, size[, bold]) so features can pick a family, implemented by both the 1.8.9 and 26.2 backends

- Allow overriding the default family with -Dorangex.font.family (display name, constant name or font file name); Proxima stays the fallback

- Resolve CJK glyphs missing from the new Latin-only faces through the existing Noto fallback
This commit is contained in:
xiaozhou233 committed 2026-11-08 01:24:29 +08:00
1 parent 5f3721c294
commit 524d540c1a
12 files changed
+149 -9

No files matched your search

@@ -1,5 +1,7 @@
package cn.xiaozhou233.orangex.render.api; package cn.xiaozhou233.orangex.render.api;
import cn.xiaozhou233.orangex.render.font.FontFamily;
/** /**
* The 2D drawing context for one frame. * The 2D drawing context for one frame.
* *
@@ -16,10 +18,22 @@ public interface Render2D {
double getGuiScale(); double getGuiScale();
/**
* Font using the configured default family
* ({@code -Dorangex.font.family}, Proxima when unset).
*/
FontRenderer getFont(float size); FontRenderer getFont(float size);
FontRenderer getFont(float size, boolean bold); FontRenderer getFont(float size, boolean bold);
/**
* Font using an explicit bundled family, e.g.
* {@link FontFamily#MONTSERRAT} or {@link FontFamily#JELLO_LIGHT}.
*/
FontRenderer getFont(FontFamily family, float size);
FontRenderer getFont(FontFamily family, float size, boolean bold);
void fillRect(double x, double y, double width, double height, int argb); void fillRect(double x, double y, double width, double height, int argb);
void fillGradient(double x, double y, double width, double height, void fillGradient(double x, double y, double width, double height,
@@ -1,22 +1,60 @@
package cn.xiaozhou233.orangex.render.font; package cn.xiaozhou233.orangex.render.font;
import java.util.Locale;
/** /**
* Bundled font families. * Bundled font families.
*
* <p>Every family maps a regular face and a bold face. Weights without a
* dedicated bold file reuse a heavier (or the same) face, so asking for
* {@code bold} text never fails to load.</p>
*
* <p>Families can be selected globally with
* {@code -Dorangex.font.family=<name>}, where {@code <name>} is either the
* display name ({@code "Montserrat Light"}), the constant name
* ({@code MONTSERRAT_LIGHT}) or the file name ({@code Montserrat-Light.ttf}).
* Matching ignores case, spaces and punctuation.</p>
*/ */
public enum FontFamily { public enum FontFamily {
PROXIMA("assets/orangex/font/proxima.ttf", "assets/orangex/font/proximabd.ttf", false), PROXIMA("Proxima", "proxima.ttf", "proximabd.ttf", false),
NOTO("assets/orangex/font/noto.ttf", "assets/orangex/font/noto.ttf", true), NOTO("Noto Sans", "noto.ttf", "noto.ttf", true),
POPPINS("assets/orangex/font/poppins_rg.ttf", "assets/orangex/font/poppins_rg.ttf", false); POPPINS("Poppins", "poppins_rg.ttf", "poppins_rg.ttf", false),
JELLO("Jello", "JelloRegular.ttf", "JelloMedium.ttf", false),
JELLO_LIGHT("Jello Light", "JelloLight.ttf", "JelloMedium.ttf", false),
JELLO_MEDIUM("Jello Medium", "JelloMedium.ttf", "JelloMedium.ttf", false),
MONTSERRAT("Montserrat", "Montserrat-Regular.ttf", "Montserrat-Bold.ttf", false),
MONTSERRAT_LIGHT("Montserrat Light", "Montserrat-Light.ttf", "Montserrat-Bold.ttf", false),
MONTSERRAT_MEDIUM("Montserrat Medium", "Montserrat-Medium.ttf", "Montserrat-Bold.ttf", false);
private static final String FONT_DIRECTORY = "assets/orangex/font/";
private final String displayName;
private final String regularResource; private final String regularResource;
private final String boldResource; private final String boldResource;
private final boolean cjkCapable; private final boolean cjkCapable;
FontFamily(String regularResource, String boldResource, boolean cjkCapable) { private final String constantKey;
this.regularResource = regularResource; private final String displayKey;
this.boldResource = boldResource; private final String fileKey;
FontFamily(String displayName, String regularFile, String boldFile,
boolean cjkCapable) {
this.displayName = displayName;
this.regularResource = FONT_DIRECTORY + regularFile;
this.boldResource = FONT_DIRECTORY + boldFile;
this.cjkCapable = cjkCapable; this.cjkCapable = cjkCapable;
this.constantKey = normalize(name());
this.displayKey = normalize(displayName);
this.fileKey = normalize(stripExtension(regularFile));
}
/**
* Human readable name, suitable for configuration screens.
*/
public String getDisplayName() {
return displayName;
} }
public String getRegularResource() { public String getRegularResource() {
@@ -34,4 +72,37 @@ public enum FontFamily {
public boolean isCjkCapable() { public boolean isCjkCapable() {
return cjkCapable; return cjkCapable;
} }
/**
* Resolves a family from its display name, constant name or font file
* name. Matching is case-insensitive and ignores spaces/punctuation.
*
* @return the matching family, or {@code null} when nothing matches.
*/
public static FontFamily byName(String name) {
if (name == null) {
return null;
}
String key = normalize(stripExtension(name));
if (key.isEmpty()) {
return null;
}
for (FontFamily family : values()) {
if (key.equals(family.constantKey)
|| key.equals(family.displayKey)
|| key.equals(family.fileKey)) {
return family;
}
}
return null;
}
private static String stripExtension(String fileName) {
int dot = fileName.lastIndexOf('.');
return dot > 0 ? fileName.substring(0, dot) : fileName;
}
private static String normalize(String value) {
return value.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9]", "");
}
} }
@@ -22,18 +22,53 @@ public final class FontManager {
private static final int MAX_RASTER_HEIGHT = 64; private static final int MAX_RASTER_HEIGHT = 64;
private static final int MAX_CJK_RASTER_HEIGHT = 36; private static final int MAX_CJK_RASTER_HEIGHT = 36;
private static final FontFamily FALLBACK_FAMILY = FontFamily.PROXIMA;
private final FontBackend backend; private final FontBackend backend;
private final Map<Key, FontFace> faces = new HashMap<Key, FontFace>(); private final Map<Key, FontFace> faces = new HashMap<Key, FontFace>();
private final Map<String, byte[]> fontData = new HashMap<String, byte[]>(); private final Map<String, byte[]> fontData = new HashMap<String, byte[]>();
private FontFamily defaultFamily;
public FontManager(FontBackend backend) { public FontManager(FontBackend backend) {
this.backend = backend; this.backend = backend;
this.defaultFamily = resolveDefaultFamily();
}
private static FontFamily resolveDefaultFamily() {
String configured = System.getProperty("orangex.font.family");
if (configured == null || configured.trim().isEmpty()) {
return FALLBACK_FAMILY;
}
FontFamily family = FontFamily.byName(configured);
if (family == null) {
System.out.println("[OrangeX] Unknown font family '" + configured
+ "', falling back to " + FALLBACK_FAMILY.getDisplayName());
return FALLBACK_FAMILY;
}
return family;
}
/**
* The family used by {@code Render2D#getFont(size)}. Defaults to
* {@link #FALLBACK_FAMILY}, overridable with
* {@code -Dorangex.font.family=<name>}.
*/
public synchronized FontFamily getDefaultFamily() {
return defaultFamily;
}
/**
* Switches the family used by the size-only {@code getFont} overloads.
* Already created {@link FontFace} instances stay valid.
*/
public synchronized void setDefaultFamily(FontFamily defaultFamily) {
this.defaultFamily = defaultFamily != null ? defaultFamily : FALLBACK_FAMILY;
} }
public synchronized FontFace getFace(FontFamily family, float logicalSize, public synchronized FontFace getFace(FontFamily family, float logicalSize,
boolean bold, double guiScale) { boolean bold, double guiScale) {
if (family == null) { if (family == null) {
family = FontFamily.PROXIMA; family = defaultFamily;
} }
if (logicalSize <= 0.0f) { if (logicalSize <= 0.0f) {
throw new IllegalArgumentException("logicalSize must be positive"); throw new IllegalArgumentException("logicalSize must be positive");
@@ -107,7 +107,17 @@ final class V1_8_9Render2D implements Render2D {
@Override @Override
public FontRenderer getFont(float size, boolean bold) { public FontRenderer getFont(float size, boolean bold) {
FontFace face = fonts.getFace(FontFamily.PROXIMA, size, bold, guiScale); return getFont(fonts.getDefaultFamily(), size, bold);
}
@Override
public FontRenderer getFont(FontFamily family, float size) {
return getFont(family, size, false);
}
@Override
public FontRenderer getFont(FontFamily family, float size, boolean bold) {
FontFace face = fonts.getFace(family, size, bold, guiScale);
return new TrueTypeFontRenderer(fonts, face, bold, guiScale, drawer); return new TrueTypeFontRenderer(fonts, face, bold, guiScale, drawer);
} }
@@ -140,7 +140,17 @@ final class V26_2Render2D implements Render2D {
@Override @Override
public FontRenderer getFont(float size, boolean bold) { public FontRenderer getFont(float size, boolean bold) {
FontFace face = fonts.getFace(FontFamily.PROXIMA, size, bold, guiScale); return getFont(fonts.getDefaultFamily(), size, bold);
}
@Override
public FontRenderer getFont(FontFamily family, float size) {
return getFont(family, size, false);
}
@Override
public FontRenderer getFont(FontFamily family, float size, boolean bold) {
FontFace face = fonts.getFace(family, size, bold, guiScale);
return new TrueTypeFontRenderer(fonts, face, bold, guiScale, drawer); return new TrueTypeFontRenderer(fonts, face, bold, guiScale, drawer);
} }