TrueTypeGlyphProvider .java

com.mojang.blaze3d.font.TrueTypeGlyphProvider (class): members: GlyphEntry, fontMemory, oversample, shiftY, Glyph, face, glyphs, getSupportedGlyphs, close.

com.mojang.blaze3d.fontclass177 linesclient5.5 KB

Implements

API Surface (4)

void close()
IntSet getSupportedGlyphs()
GlyphInfo info()
BakedGlyph bake(final UnbakedGlyph.Stitcher stitcher)

Fields & Constants (5)

fontMemory
face
oversample
glyphs
shiftY

String literals (4)

"Loading glyph U+%06X"
"Glyph U+%06X not initialized"
"Provider already closed"
"Deleting face"

Full Source

TrueTypeGlyphProvider.java177 lines · Minecraft 26.3 · verbatim
1
package com.mojang.blaze3d.font;
2
3
import com.mojang.blaze3d.font.TrueTypeGlyphProvider.Glyph.1;
4
import it.unimi.dsi.fastutil.ints.IntArraySet;
5
import it.unimi.dsi.fastutil.ints.IntSet;
6
import java.nio.ByteBuffer;
7
import java.nio.IntBuffer;
8
import java.util.Locale;
9
import net.fabricmc.api.EnvType;
10
import net.fabricmc.api.Environment;
11
import net.minecraft.client.gui.font.CodepointMap;
12
import net.minecraft.client.gui.font.glyphs.BakedGlyph;
13
import net.minecraft.client.gui.font.glyphs.EmptyGlyph;
14
import net.minecraft.client.gui.font.providers.FreeTypeUtil;
15
import org.jspecify.annotations.Nullable;
16
import org.lwjgl.system.MemoryStack;
17
import org.lwjgl.system.MemoryUtil;
18
import org.lwjgl.util.freetype.FT_Bitmap;
19
import org.lwjgl.util.freetype.FT_Face;
20
import org.lwjgl.util.freetype.FT_GlyphSlot;
21
import org.lwjgl.util.freetype.FT_Vector;
22
import org.lwjgl.util.freetype.FreeType;
23
24
@Environment(EnvType.CLIENT)
25
public class TrueTypeGlyphProvider implements GlyphProvider {
26
	private @Nullable ByteBuffer fontMemory;
27
	private @Nullable FT_Face face;
28
	private final float oversample;
29
	private final CodepointMap<TrueTypeGlyphProvider.GlyphEntry> glyphs = new CodepointMap<>(
30
		TrueTypeGlyphProvider.GlyphEntry[]::new, TrueTypeGlyphProvider.GlyphEntry[][]::new
31
	);
32
33
	public TrueTypeGlyphProvider(
34
		final ByteBuffer fontMemory, final FT_Face face, final float size, final float oversample, final float shiftX, final float shiftY, final String skip
35
	) {
36
		this.fontMemory = fontMemory;
37
		this.face = face;
38
		this.oversample = oversample;
39
		IntSet skipSet = new IntArraySet();
40
		skip.codePoints().forEach(skipSet::add);
41
		int pixelsPerEm = Math.round(size * oversample);
42
		FreeType.FT_Set_Pixel_Sizes(face, pixelsPerEm, pixelsPerEm);
43
		float transformX = shiftX * oversample;
44
		float transformY = -shiftY * oversample;
45
46
		try (MemoryStack stack = MemoryStack.stackPush()) {
47
			FT_Vector vector = FreeTypeUtil.setVector(FT_Vector.malloc(stack), transformX, transformY);
48
			FreeType.FT_Set_Transform(face, null, vector);
49
			IntBuffer indexPtr = stack.mallocInt(1);
50
			int codepoint = (int)FreeType.FT_Get_First_Char(face, indexPtr);
51
52
			while (true) {
53
				int index = indexPtr.get(0);
54
				if (index == 0) {
55
					return;
56
				}
57
58
				if (!skipSet.contains(codepoint)) {
59
					this.glyphs.put(codepoint, new TrueTypeGlyphProvider.GlyphEntry(index));
60
				}
61
62
				codepoint = (int)FreeType.FT_Get_Next_Char(face, codepoint, indexPtr);
63
			}
64
		}
65
	}
66
67
	@Override
68
	public @Nullable UnbakedGlyph getGlyph(final int codepoint) {
69
		TrueTypeGlyphProvider.GlyphEntry entry = this.glyphs.get(codepoint);
70
		return entry != null ? this.getOrLoadGlyphInfo(codepoint, entry) : null;
71
	}
72
73
	private UnbakedGlyph getOrLoadGlyphInfo(final int codepoint, final TrueTypeGlyphProvider.GlyphEntry entry) {
74
		UnbakedGlyph result = entry.glyph;
75
		if (result == null) {
76
			FT_Face face = this.validateFontOpen();
77
			synchronized (face) {
78
				result = entry.glyph;
79
				if (result == null) {
80
					result = this.loadGlyph(codepoint, face, entry.index);
81
					entry.glyph = result;
82
				}
83
			}
84
		}
85
86
		return result;
87
	}
88
89
	private UnbakedGlyph loadGlyph(final int codepoint, final FT_Face face, final int index) {
90
		int errorCode = FreeType.FT_Load_Glyph(face, index, 4194312);
91
		if (errorCode != 0) {
92
			FreeTypeUtil.assertError(errorCode, String.format(Locale.ROOT, "Loading glyph U+%06X", codepoint));
93
		}
94
95
		FT_GlyphSlot glyph = face.glyph();
96
		if (glyph == null) {
97
			throw new NullPointerException(String.format(Locale.ROOT, "Glyph U+%06X not initialized", codepoint));
98
		}
99
100
		float scaledAdvance = FreeTypeUtil.x(glyph.advance());
101
		FT_Bitmap bitmap = glyph.bitmap();
102
		int left = glyph.bitmap_left();
103
		int top = glyph.bitmap_top();
104
		int width = bitmap.width();
105
		int height = bitmap.rows();
106
		return width > 0 && height > 0
107
			? new TrueTypeGlyphProvider.Glyph(left, top, width, height, scaledAdvance, index)
108
			: new EmptyGlyph(scaledAdvance / this.oversample);
109
	}
110
111
	private FT_Face validateFontOpen() {
112
		if (this.fontMemory != null && this.face != null) {
113
			return this.face;
114
		} else {
115
			throw new IllegalStateException("Provider already closed");
116
		}
117
	}
118
119
	@Override
120
	public void close() {
121
		if (this.face != null) {
122
			synchronized (FreeTypeUtil.LIBRARY_LOCK) {
123
				FreeTypeUtil.checkError(FreeType.FT_Done_Face(this.face), "Deleting face");
124
			}
125
126
			this.face = null;
127
		}
128
129
		MemoryUtil.memFree(this.fontMemory);
130
		this.fontMemory = null;
131
	}
132
133
	@Override
134
	public IntSet getSupportedGlyphs() {
135
		return this.glyphs.keySet();
136
	}
137
138
	@Environment(EnvType.CLIENT)
139
	private class Glyph implements UnbakedGlyph {
140
		private final int width;
141
		private final int height;
142
		private final float bearingX;
143
		private final float bearingY;
144
		private final GlyphInfo info;
145
		private final int index;
146
147
		private Glyph(final float left, final float top, final int width, final int height, final float advance, final int index) {
148
			this.width = width;
149
			this.height = height;
150
			this.info = GlyphInfo.simple(advance / TrueTypeGlyphProvider.this.oversample);
151
			this.bearingX = left / TrueTypeGlyphProvider.this.oversample;
152
			this.bearingY = top / TrueTypeGlyphProvider.this.oversample;
153
			this.index = index;
154
		}
155
156
		@Override
157
		public GlyphInfo info() {
158
			return this.info;
159
		}
160
161
		@Override
162
		public BakedGlyph bake(final UnbakedGlyph.Stitcher stitcher) {
163
			return stitcher.stitch(this.info, new 1(this));
164
		}
165
	}
166
167
	@Environment(EnvType.CLIENT)
168
	private static class GlyphEntry {
169
		private final int index;
170
		private volatile @Nullable UnbakedGlyph glyph;
171
172
		private GlyphEntry(final int index) {
173
			this.index = index;
174
		}
175
	}
176
}
177

Decompiled Minecraft 26.3 source of TrueTypeGlyphProvider (com/mojang/blaze3d/font/TrueTypeGlyphProvider.java) with Mojang official mappings, stored verbatim on the site.