Posted April 17, 20232 yr Hello, I am reaching out to you because I am unable to solve an update problem. I had a system for adding fonts (ttf) to Minecraft that worked very well for versions prior to 1.18. However, when I migrated to 1.18.2 and added new fonts, I am no longer able to "display" the characters, it only shows black squares. I hope someone can help me, you can see below all the code. I'm sure it's something simple but I really can't figure it out... (The code is really big) Spoiler package fr.pixor.api.client.font; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Map; import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.TextureUtil; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.Tesselator; import com.mojang.blaze3d.vertex.VertexFormat; import com.mojang.math.Matrix4f; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.DynamicTexture; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import javax.imageio.ImageIO; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL12.GL_CLAMP_TO_EDGE; public class GlyphPage { private Map<Character, Glyph> glyphs = new HashMap<>(); private int italicSpacing, texId, imgSize; public GlyphPage(Font font, char[] chars, boolean fractionalMetrics) { FontRenderContext fontRenderContext = new FontRenderContext(font.getTransform(), true, fractionalMetrics); double maxWidth = 0; double maxHeight = 0; for(char c : chars) { Rectangle2D bound = font.getStringBounds(Character.toString(c), fontRenderContext); maxWidth = Math.max(maxWidth, bound.getWidth()); maxHeight = Math.max(maxHeight, bound.getHeight()); } italicSpacing = font.isItalic() ? 5 : 0; imgSize = (int)Math.ceil(Math.sqrt((maxHeight + 2) * (maxWidth + italicSpacing) * chars.length)); BufferedImage bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_ARGB | BufferedImage.SCALE_FAST); Graphics2D graphics = bufferedImage.createGraphics(); graphics.setFont(font); graphics.setColor(new Color(255, 255, 255, 0)); graphics.fillRect(0, 0, imgSize, imgSize); graphics.setColor(Color.WHITE); graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); FontMetrics fontMetrics = graphics.getFontMetrics(); int posX = 1; int posY = 2; for(char c : chars) { Glyph glyph = new Glyph(); Rectangle2D bounds = fontMetrics.getStringBounds(Character.toString(c), graphics); glyph.width = (int)bounds.getWidth() + italicSpacing; glyph.height = (int)bounds.getHeight() + 2; if(posX + glyph.width >= imgSize) { posX = 1; posY += maxHeight + fontMetrics.getDescent() + 1; } glyph.x = posX; glyph.y = posY; graphics.drawString(Character.toString(c), posX, posY + fontMetrics.getAscent()); posX += glyph.width + 6; glyphs.put(c, glyph); } try { //ImageIO.write(bufferedImage, "png", new File("name_font_"+font.getPSName()+".png")); texId = loadTexture(bufferedImage); } catch (Exception e) { e.printStackTrace(); } } public float renderChar(Matrix4f matrix, char c, float x, float y, float red, float green, float blue, float alpha, float f) { Glyph glyph = glyphs.get(c); if(glyph == null) return 0; RenderSystem.setShaderTexture(0, texId); float pageX = glyph.x / (float)imgSize; float pageY = glyph.y / (float)imgSize; float pageWidth = glyph.width / (float)imgSize; float pageHeight = glyph.height / (float)imgSize; float width = glyph.width + f; float height = glyph.height; BufferBuilder buffer = Tesselator.getInstance().getBuilder(); buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_TEX); buffer.vertex(matrix, x, y + height, 0).color(red, green, blue, alpha).uv(pageX, pageY + pageHeight).endVertex(); buffer.vertex(matrix, x + width, y + height, 0).color(red, green, blue, alpha).uv(pageX + pageWidth, pageY + pageHeight).endVertex(); buffer.vertex(matrix, x + width, y, 0).color(red, green, blue, alpha).uv(pageX + pageWidth, pageY).endVertex(); buffer.vertex(matrix, x, y, 0).color(red, green, blue, alpha).uv(pageX, pageY).endVertex(); Tesselator.getInstance().end(); RenderSystem.setShaderTexture(0, 0); return width - italicSpacing; } public float getWidth(char c) { Glyph glyph = glyphs.get(c); return glyph == null ? 0 : glyph.width - italicSpacing; } private int loadTexture(BufferedImage image) throws Exception { int[] pixels = new int[image.getWidth() * image.getHeight()]; image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth()); ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); //4 for RGBA, 3 for RGB for(int y = 0; y < image.getHeight(); y++){ for(int x = 0; x < image.getWidth(); x++){ int pixel = pixels[y * image.getWidth() + x]; buffer.put((byte) ((pixel >> 16) & 0xFF)); // red component buffer.put((byte) ((pixel >> 8) & 0xFF)); // green component buffer.put((byte) (pixel & 0xFF)); // blue component buffer.put((byte) ((pixel >> 24) & 0xFF)); // alpha component. Only for RGBA } } buffer.flip(); //!! int textureID = TextureUtil.generateTextureId(); //TextureUtil.prepareImage(NativeImage.InternalGlFormat.RGBA, textureID, image.getWidth(), image.getHeight()); RenderSystem.setShaderTexture(0, textureID); RenderSystem.texParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); RenderSystem.texParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); RenderSystem.texParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); RenderSystem.texParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); RenderSystem.setShaderTexture(0, 0); return textureID; } private static class Glyph { private int x; private int y; private int width; private int height; } } package fr.pixor.api.client.font; import java.awt.Color; import java.util.Locale; import com.mojang.blaze3d.vertex.*; import com.mojang.math.Matrix4f; import org.lwjgl.opengl.GL30; import com.mojang.blaze3d.platform.GlStateManager; public class FontRenderer { public static final Tesselator TESSELLATOR = Tesselator.getInstance(); public static final BufferBuilder BUFFER_BUILDER = TESSELLATOR.getBuilder(); public static final String styleCodes = "0123456789abcdefklmnor"; public static final int[] colorCodes = new int[32]; static { for (int i = 0; i < 32; ++i) { int j = (i >> 3 & 1) * 85; int k = (i >> 2 & 1) * 170 + j; int l = (i >> 1 & 1) * 170 + j; int i1 = (i & 1) * 170 + j; if (i == 6) { k += 85; } if (i >= 16) { k /= 4; l /= 4; i1 /= 4; } colorCodes[i] = (k & 255) << 16 | (l & 255) << 8 | i1 & 255; } } public static void drawStringRight(PoseStack stack, CustomFont font, String text, double x, double y, Color color) { renderString(stack, font, text, x - font.getWidth(text), y, false, color); } public static void drawString(PoseStack matrices, CustomFont font, String text, double x, double y, Color color) { renderString(matrices, font, text, x, y, false, color); } public static void drawCenteredXString(PoseStack matrices, CustomFont font, String text, double x, double y, Color color) { renderString(matrices, font, text, x - font.getWidth(text) / 2, y, false, color); } public static void drawShadowedString(PoseStack matrices, CustomFont font, String text, double x, double y, Color color) { renderStringWithShadow(matrices, font, text, x, y, color, getShadowColor(color)); } public static void drawShadowedCenteredXString(PoseStack matrices, CustomFont font, String text, double x, double y, Color color) { renderStringWithShadow(matrices, font, text, x - font.getWidth(text) / 2, y, color, getShadowColor(color)); } public static void drawShadowedString(PoseStack matrices, CustomFont font, String text, double x, double y, Color color, Color shadowColor) { renderStringWithShadow(matrices, font, text, x, y, color, shadowColor); } public static void drawShadowedCenteredXString(PoseStack matrices, CustomFont font, String text, double x, double y, Color color, Color shadowColor) { renderStringWithShadow(matrices, font, text, x - font.getWidth(text) / 2, y, color, shadowColor); } private static void renderStringWithShadow(PoseStack matrices, CustomFont font, String text, double x, double y, Color color, Color shadowColor) { y -= 1f; renderString(matrices, font, text, x + 1.0f, y + 1.0f, true, shadowColor); renderString(matrices, font, text, x, y, false, color); } private static void renderString(PoseStack matrices, CustomFont font, String text, double x, double y, boolean shadow, Color color) { y -= font.getLifting(); x -= 1; float posX = (float)x * 2.0f; float posY = (float)y * 2.0f; float red = color.getRed() / 255.0f; float green = color.getGreen() / 255.0f; float blue = color.getBlue() / 255.0f; float alpha = color.getAlpha() / 255.0f; boolean boldStyle = false; boolean italicStyle = false; boolean strikethroughStyle = false; boolean underlineStyle = false; GlStateManager._enableBlend(); GlStateManager._blendFunc(GL30.GL_SRC_ALPHA, GL30.GL_ONE_MINUS_SRC_ALPHA); matrices.pushPose(); matrices.scale(0.5f, 0.5f, 1f); Matrix4f matrix = matrices.last().pose(); for(int i = 0; i < text.length(); i++) { char c0 = text.charAt(i); if (c0 == 167 && i + 1 < text.length() && styleCodes.indexOf(text.toLowerCase(Locale.ENGLISH).charAt(i + 1)) != -1) { int i1 = styleCodes.indexOf(text.toLowerCase(Locale.ENGLISH).charAt(i + 1)); if (i1 < 16) { boldStyle = false; strikethroughStyle = false; underlineStyle = false; italicStyle = false; if(shadow) { i1 += 16; } int j1 = colorCodes[i1]; red = (float) (j1 >> 16 & 255) / 255.0F; green = (float) (j1 >> 8 & 255) / 255.0F; blue = (float) (j1 & 255) / 255.0F; alpha = 1f; } else if (i1 == 16) { } else if (i1 == 17) { boldStyle = true; } else if (i1 == 18) { strikethroughStyle = true; } else if (i1 == 19) { underlineStyle = true; } else if (i1 == 20) { italicStyle = true; } else if(i1 == 21) { boldStyle = false; strikethroughStyle = false; underlineStyle = false; italicStyle = false; } i++; } else { float f = font.renderChar(matrix, c0, posX, posY, boldStyle, italicStyle, red, green, blue, alpha); if(strikethroughStyle) { float h = font.getFontHeight() + 2; GlStateManager._disableTexture(); BUFFER_BUILDER.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR); BUFFER_BUILDER.vertex(matrix, posX, posY + h + 3f, 0).color(red, green, blue, alpha).endVertex(); BUFFER_BUILDER.vertex(matrix, posX + f, posY + h + 3f, 0).color(red, green, blue, alpha).endVertex(); BUFFER_BUILDER.vertex(matrix, posX + f, posY + h, 0).color(red, green, blue, alpha).endVertex(); BUFFER_BUILDER.vertex(matrix, posX, posY + h, 0).color(red, green, blue, alpha).endVertex(); TESSELLATOR.end(); GlStateManager._enableTexture(); } if(underlineStyle) { float y1 = posY + font.getFontHeight() * 2 + 2; GlStateManager._disableTexture(); BUFFER_BUILDER.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR); BUFFER_BUILDER.vertex(matrix, posX, y1 + 4, 0).color(red, green, blue, alpha).endVertex(); BUFFER_BUILDER.vertex(matrix, posX + f, y1 + 4, 0).color(red, green, blue, alpha).endVertex(); BUFFER_BUILDER.vertex(matrix, posX + f, y1 + 2, 0).color(red, green, blue, alpha).endVertex(); BUFFER_BUILDER.vertex(matrix, posX, y1 + 2, 0).color(red, green, blue, alpha).endVertex(); TESSELLATOR.end(); GlStateManager._enableTexture(); } posX += f; } } matrices.popPose(); GlStateManager._disableBlend(); } public static Color getShadowColor(Color color) { return new Color((color.getRGB() & 16579836) >> 2 | color.getRGB() & -16777216); } } package fr.pixor.api.client.font; import java.awt.Font; import java.awt.FontFormatException; import java.io.IOException; import java.util.Locale; import com.mojang.math.Matrix4f; import fr.pixor.core.Constants; import fr.pixor.core.PixorCore; public class CustomFont { private GlyphPage regular, bold, italic, boldItalic; private float fontHeight, stretching, spacing, lifting; private String fontName; public enum Lang { ENG(new int[] {31, 126, 0, 0}), FULL(new int[] {0, 512, 1024, 1105}), RU_ENG(new int[] {31, 126, 1024, 1105}); private int[] charCodes; private Lang(int[] charCodes) { this.charCodes = charCodes; } public int[] getCharCodes() { return charCodes; } } public CustomFont(String fileName, int size, Lang lang, boolean fractionalMetrics, float stretching, float spacing, float lifting) { String path = "/assets/"+ Constants.MODID +"/font/".concat(fileName); Font regularFont = null; Font boldFont = null; Font italicFont = null; Font boldItalicFont = null; try { regularFont = Font.createFont(Font.TRUETYPE_FONT, PixorCore.class.getResourceAsStream(path)) .deriveFont(Font.PLAIN, size); boldFont = Font.createFont(Font.TRUETYPE_FONT, PixorCore.class.getResourceAsStream(path)) .deriveFont(Font.BOLD, size); italicFont = Font.createFont(Font.TRUETYPE_FONT, PixorCore.class.getResourceAsStream(path)) .deriveFont(Font.ITALIC, size); boldItalicFont = Font.createFont(Font.TRUETYPE_FONT, PixorCore.class.getResourceAsStream(path)) .deriveFont(Font.BOLD | Font.ITALIC, size); } catch (FontFormatException | IOException e) { e.printStackTrace(); } this.stretching = stretching; this.spacing = spacing; this.lifting = lifting; fontHeight = regularFont.getSize2D() / 2; fontName = regularFont.getFontName(Locale.ENGLISH); int[] codes = lang.getCharCodes(); char[] chars = new char[(codes[1] - codes[0] + codes[3] - codes[2])]; int c = 0; for(int i = codes[0] + 1; i <= codes[1]; i++) { chars[c] = (char)i; c++; } for(int i = codes[2] + 1; i <= codes[3]; i++) { chars[c] = (char)i; c++; } regular = new GlyphPage(regularFont, chars, fractionalMetrics); bold = new GlyphPage(boldFont, chars, fractionalMetrics); italic = new GlyphPage(italicFont, chars, fractionalMetrics); boldItalic = new GlyphPage(boldItalicFont, chars, fractionalMetrics); } public float renderChar(Matrix4f matrix, char c, float x, float y, boolean boldStyle, boolean italicStyle, float red, float green, float blue, float alpha) { GlyphPage current = getCurrentGP(boldStyle, italicStyle); float w = current.renderChar(matrix, c, x, y, red, green, blue, alpha, stretching) + spacing; return w; } public float getWidth(String text) { boolean boldStyle = false; boolean italicStyle = false; float width = 0.0f; for(int i = 0; i < text.length(); i++) { char c0 = text.charAt(i); if (c0 == 167 && i + 1 < text.length() && FontRenderer.styleCodes.indexOf(text.toLowerCase(Locale.ENGLISH).charAt(i + 1)) != -1) { int i1 = FontRenderer.styleCodes.indexOf(text.toLowerCase(Locale.ENGLISH).charAt(i + 1)); if(i1 < 16) { boldStyle = false; italicStyle = false; } else if(i1 == 17) { boldStyle = true; } else if(i1 == 20) { italicStyle = true; } else if(i1 == 21) { boldStyle = false; italicStyle = false; } i ++; } else { GlyphPage current = getCurrentGP(boldStyle, italicStyle); width += current.getWidth(c0) + spacing + stretching; } } return (width - spacing) / 2.0f; } public GlyphPage getCurrentGP(boolean boldStyle, boolean italicStyle) { GlyphPage current; if(boldStyle && italicStyle) current = boldItalic; else if(boldStyle) current = bold; else if(italicStyle) current = italic; else current = regular; return current; } public float getLifting() { return lifting; } public float getSpacing() { return spacing; } public String getFontName() { return fontName; } public float getFontHeight() { return fontHeight; } } Result of my BufferedImage (GlyphPage) (it works fine, but it seems like it can't load the texture or something...) Spoiler And this the result in game: Thanks for helping me... Edited April 17, 20232 yr by Snyker Fixed
April 17, 20232 yr I don't think you will get many people in a forum about forge modding volunteering to help you fix what is really just your own custom open gl font rendering code? I know little about Fonts in minecraft. But I do know you shouldn't be using AWT in minecraft. Or OpenGL directly. In minecraft Fonts are loaded using a resource pack, e.g. vanilla's fonts: https://github.com/misode/mcmeta/tree/assets/assets/minecraft/font and then displayed using chat Components and Style.withFont() - see for example EnchantmentNames for the weird enchanting clue text. Look at FontManager for the details of how fonts get loaded, and I guess TrueTypeGlyphProviderBuilder if you want ttf. Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
April 17, 20232 yr Author I think this isn't possible cause to load a ttf font you need to use the json file and you need to replace the "default.json" file ... and i want to keep the default font. I will try but i'm pretty sure this will not work Thanks for you repost you give me a little hope haha Edited April 17, 20232 yr by Snyker
April 17, 20232 yr default.json is the default font. If you want to replace that, you need to make sure your mod has ordering="AFTER" for the forge mod dependency and include a minecraft/font/default.json in your mod. https://forge.gemwire.uk/wiki/Mods.toml Otherwise, the font manager iterates over all files in the form namespace/font/fontname.json - you then choose the font using ResourceLocation namespace:fontname Edited April 17, 20232 yr by warjort Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
April 17, 20232 yr Author Ok so i try many things but no one worked... TTF file are placed on the same path of the json. All i try : 1.a) I follow what you said and replacing the default.json (in my modid/font/ AND minecraft/font) with the forge order to AFTER and BEFORE (notworking) : { "providers": [ { "type": "ttf", "file": "pixorcore:font_bitcell.ttf", //or minecraft:font_bitcell.ttf "shift": [0.0, -1.0], "size": 15, "oversample": 2.0 } ] } 1.b) I setup the Style to conform like EnchantmentNames : private static final ResourceLocation RHEAD = new ResourceLocation(Constants.MODID, "font/font_bitcell"); //ttf font private static final ResourceLocation RHEAD = new ResourceLocation(Constants.MODID, "font/bitcell"); //json public static final Style HEAD_STYLE = Style.EMPTY.withFont(RHEAD); 1.c) And applying it on my TextComponent#withStyle, the render is better but didn't work. i draw with the default font a text to debug and he didn't change at all. 2.a) Creating new "bitcell.json" in my modid/font with the content : (See 1.a) 2.b) I testing both path files like 1.b 2.c) Result are the same 1.c, nothing work Maybe i made some mistake with json files or other ? But i have trying everything i can, like changing path, name, path modid/minecraft i have always squares draw. i tested another font and its the same result.
April 17, 20232 yr You don't include the font/ in the ResourceLocation. Mojang put Fonts in resource packs so people can mod vanilla. I suggest you search the internet for ttf font resource packs and compare your config with theirs. You should also check the logs/debug.log for errors/warnings during resource pack loading. Finally, if you just post snippets (or worse describe your code in English) you aren't going to get much help. Put your code on github where we can see everything in context so we can confirm you did it correctly and maybe try it for ourselves if the problem isn't obvious from just reading the code. Edited April 17, 20232 yr by warjort Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
April 17, 20232 yr Quote with the forge order to AFTER and BEFORE (notworking) : The reason for putting AFTER in the mods.toml is so it puts your resource pack above forge one in the options/resource pack screen. It might be you already configured the resource packs and so it has already saved the wrong order in the options.txt? NOTE: This is only relevant if you want to override the vanilla fonts. Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
April 17, 20232 yr Author Oh damn it's work i just removed the font/ .... 😭 Thank you so much Edited April 17, 20232 yr by Snyker
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.