Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ dependencies {
implementation(files("libs/steamworks4j-lwjgl3-1.10.0-SNAPSHOT.jar"))
// api("com.code-disaster.steamworks4j:steamworks4j:1.10.0-SNAPSHOT")
// api("com.code-disaster.steamworks4j:steamworks4j-lwjgl3:1.10.0-SNAPSHOT")

implementation("com.google.code.gson:gson:2.13.2") // Used for handling JSON
}

group = "com.aehmttw"
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/basewindow/BaseFontRenderer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package basewindow;

import lwjglwindow.FontRenderer;

public abstract class BaseFontRenderer
{
public boolean drawBox = false;
Expand All @@ -23,5 +25,9 @@ public BaseFontRenderer(BaseWindow h)

public abstract double getStringSizeY(double sY, String s);

public abstract void addFont(String imageFile, String chars, int[] charSizes);
public abstract void addFont(String id, String imageFile, String chars, int[] charSizes);

public abstract void addFont(String id, String imageFile, String chars, int[] charSizes, int hSpace);

public abstract void setDefaultFont(String id, String imageFile, String chars, int[] charSizes, int hSpace);
}
107 changes: 77 additions & 30 deletions src/main/java/lwjglwindow/FontRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ public class FontRenderer extends BaseFontRenderer
{
public static class FontInfo
{
public String chars;
public int[] charSizes;
public String image;
public final String id; // Font id
public final String chars;
public final int[] charSizes;
public final String image;
public float size = 16; // how many characters fit per horizontal line
public int hSpace = 2; // spacing between rows, increase this to 2 for antialiasing to prevent weird artifacts
public Map<Character, Integer> charIndexMap = new HashMap<>();
public final Map<Character, Integer> charIndexMap = new HashMap<>();

public FontInfo(String image, String chars, int[] charSizes)
public FontInfo(String id, String image, String chars, int[] charSizes, Integer hSpace)
{
this.id = id;
if (hSpace != null) this.hSpace = hSpace;
this.image = image;
this.chars = chars;
this.charSizes = charSizes;
Expand All @@ -30,50 +33,94 @@ public FontInfo(String image, String chars, int[] charSizes)
charIndexMap.put(chars.charAt(i), i);
}
}

public FontInfo(String id, String image, String chars, int[] charSizes) {
this(id, image, chars, charSizes, null);
}
}

private final List<FontInfo> fontInfos = new ArrayList<>();
private final FontInfo defaultFont;
private final Map<String, FontInfo> fontById = new HashMap<>(); // Use font id to locate the font
private FontInfo defaultFont;

public FontRenderer(LWJGLWindow h, String defaultFontFile)
public FontRenderer(LWJGLWindow h)
{
super(h);
}

/**
* Set the default font. Must be called before any drawing operations.
*/
@Override
public void setDefaultFont(String id, String imageFile, String chars, int[] charSizes, int hSpace)
{
if (defaultFont != null)
{
throw new IllegalStateException("Default font already set!");
}

defaultFont = new FontInfo(defaultFontFile,
" !\"#$%&'()*+,-./" +
"0123456789:;<=>?" +
"@ABCDEFGHIJKLMNO" +
"PQRSTUVWXYZ[\\]^_" +
"'abcdefghijklmno" +
"pqrstuvwxyz{|}~`" +
"âăîşţàçæèéêëïôœù" +
"úûüÿáíóñ¡¿äöå",
new int[]{
3, 2, 4, 5, 5, 6, 5, 2, 3, 3, 4, 5, 1, 5, 1, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 5, 5, 5, 5,
7, 5, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 5, 3, 5, 5,
2, 5, 5, 5, 5, 5, 4, 5, 5, 1, 5, 4, 2, 5, 5, 5,
5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 4, 1, 4, 6, 2,
5, 5, 5, 5, 3, 5, 5, 7, 5, 5, 5, 5, 3, 5, 7, 5,
5, 5, 5, 5, 5, 3, 5, 5, 3, 5, 5, 5, 5
});

fontInfos.add(defaultFont);
FontInfo info = new FontInfo(id, imageFile, chars, charSizes, hSpace);
fontInfos.add(info);
fontById.put(id, info);
defaultFont = info;
}

/**
* Add a new font to the renderer.
*
* @param id The font ID for lookup.
* @param imageFile The image file path.
* @param chars The characters to include in the font.
* @param charSizes The width of each character in (pixels / 4).
*/
public void addFont(String imageFile, String chars, int[] charSizes)
@Override
public void addFont(String id, String imageFile, String chars, int[] charSizes)
{
fontInfos.add(new FontInfo(imageFile, chars, charSizes));
FontInfo info = new FontInfo(id, imageFile, chars, charSizes);
fontInfos.add(info);
fontById.put(id, info);
}

/**
* Add a new font to the renderer.
*
* @param id The font ID for lookup.
* @param imageFile The image file path.
* @param chars The characters to include in the font.
* @param charSizes The width of each character in (pixels / 4).
* @param hSpace Spacing between rows
*/
@Override
public void addFont(String id, String imageFile, String chars, int[] charSizes, int hSpace)
{
FontInfo info = new FontInfo(id, imageFile, chars, charSizes, hSpace);
fontInfos.add(info);
fontById.put(id, info);
}

/**
* Get a font by its ID.
*
* @param id The font ID.
* @return The FontInfo, or null if not found.
*/
public FontInfo getFontById(String id)
{
return fontById.get(id);
}

/**
* Check if a font ID exists.
*
* @param id The font ID.
* @return true if the font exists.
*/
public boolean hasFontId(String id)
{
return fontById.containsKey(id);
}

@Override
public boolean supportsChar(char c)
{
for (FontInfo font : fontInfos)
Expand Down
Loading
Loading