Font .java

net.minecraft.client.gui.Font (class): members: splitIgnoringLanguage, bidirectionalShaping, prepare8xTextOutline, PreparedTextBuilder, UNDER_EFFECT_DEPTH.

net.minecraft.client.guiclass385 linesclient12.8 KB

API Surface (20)

String bidirectionalShaping(final String text)
Font.PreparedText prepare8xTextOutline(final FormattedCharSequence str, final float x, final float y, final int outlineColor)
TextRenderable prepareBackground(final float x0, final float y0, final float x1, final float y1, final int color)
Font.PreparedText prepareText(String text, final float x, final float y, final int originalColor, final boolean drawShadow, final int backgroundColor)
Font.PreparedText prepareText(final FormattedCharSequence text, final float x, final float y, final int originalColor, final boolean drawShadow, final boolean includeEmpty, final int backgroundColor)
int width(final String str)
int width(final FormattedText text)
int width(final FormattedCharSequence text)
String plainSubstrByWidth(final String str, final int width, final boolean reverse)
String plainSubstrByWidth(final String str, final int width)
FormattedText substrByWidth(final FormattedText text, final int width)
int wordWrapHeight(final FormattedText input, final int textWidth)
List<FormattedCharSequence> split(final FormattedText input, final int maxWidth)
List<FormattedText> splitIgnoringLanguage(final FormattedText input, final int maxWidth)
boolean isBidirectional()
StringSplitter getSplitter()
boolean accept(final int position, final Style style, final int c)
boolean accept(final int position, final Style style, final BakedGlyph glyph)
void visit(final Font.GlyphVisitor visitor)
void discardEffects()

Fields & Constants (14)

EFFECT_DEPTH
OVER_EFFECT_DEPTH
UNDER_EFFECT_DEPTH
SHADOW_DEPTH
lineHeight
random
provider
splitter
text
x
y
originalColor
drawShadow
includeEmpty

Full Source

Font.java385 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui;
2
3
import com.ibm.icu.text.ArabicShaping;
4
import com.ibm.icu.text.ArabicShapingException;
5
import com.ibm.icu.text.Bidi;
6
import com.mojang.blaze3d.font.GlyphInfo;
7
import java.util.ArrayList;
8
import java.util.List;
9
import net.fabricmc.api.EnvType;
10
import net.fabricmc.api.Environment;
11
import net.minecraft.client.StringSplitter;
12
import net.minecraft.client.gui.font.EmptyArea;
13
import net.minecraft.client.gui.font.TextRenderable;
14
import net.minecraft.client.gui.font.glyphs.BakedGlyph;
15
import net.minecraft.client.gui.font.glyphs.EffectGlyph;
16
import net.minecraft.client.gui.navigation.ScreenRectangle;
17
import net.minecraft.locale.Language;
18
import net.minecraft.network.chat.FontDescription;
19
import net.minecraft.network.chat.FormattedText;
20
import net.minecraft.network.chat.Style;
21
import net.minecraft.network.chat.TextColor;
22
import net.minecraft.util.ARGB;
23
import net.minecraft.util.FormattedCharSequence;
24
import net.minecraft.util.FormattedCharSink;
25
import net.minecraft.util.Mth;
26
import net.minecraft.util.RandomSource;
27
import net.minecraft.util.StringDecomposer;
28
import org.jspecify.annotations.Nullable;
29
30
@Environment(EnvType.CLIENT)
31
public class Font {
32
	private static final float EFFECT_DEPTH = 0.01F;
33
	private static final float OVER_EFFECT_DEPTH = 0.01F;
34
	private static final float UNDER_EFFECT_DEPTH = -0.01F;
35
	public static final float SHADOW_DEPTH = 0.03F;
36
	public final int lineHeight = 9;
37
	private final RandomSource random = RandomSource.create();
38
	private final Font.Provider provider;
39
	private final StringSplitter splitter;
40
41
	public Font(final Font.Provider provider) {
42
		this.provider = provider;
43
		this.splitter = new StringSplitter((codepoint, style) -> this.getGlyphSource(style.getFont()).getGlyph(codepoint).info().getAdvance(style.isBold()));
44
	}
45
46
	private GlyphSource getGlyphSource(final FontDescription fontLocation) {
47
		return this.provider.glyphs(fontLocation);
48
	}
49
50
	public String bidirectionalShaping(final String text) {
51
		try {
52
			Bidi bidi = new Bidi(new ArabicShaping(8).shape(text), 127);
53
			bidi.setReorderingMode(0);
54
			return bidi.writeReordered(2);
55
		} catch (ArabicShapingException var3) {
56
			return text;
57
		}
58
	}
59
60
	public Font.PreparedText prepare8xTextOutline(final FormattedCharSequence str, final float x, final float y, final int outlineColor) {
61
		Font.PreparedTextBuilder outlineOutput = new Font.PreparedTextBuilder(0.0F, 0.0F, outlineColor, false, false);
62
63
		for (int xo = -1; xo <= 1; xo++) {
64
			for (int yo = -1; yo <= 1; yo++) {
65
				if (xo != 0 || yo != 0) {
66
					float[] startX = new float[]{x};
67
					int finalXo = xo;
68
					int finalYo = yo;
69
					str.accept((position, style, codepoint) -> {
70
						boolean bold = style.isBold();
71
						BakedGlyph glyph = this.getGlyph(codepoint, style);
72
						outlineOutput.x = startX[0] + finalXo * glyph.info().getShadowOffset();
73
						outlineOutput.y = y + finalYo * glyph.info().getShadowOffset();
74
						startX[0] += glyph.info().getAdvance(bold);
75
						return outlineOutput.accept(position, style.withColor(outlineColor), glyph);
76
					});
77
				}
78
			}
79
		}
80
81
		outlineOutput.discardEffects();
82
		return outlineOutput;
83
	}
84
85
	public TextRenderable prepareBackground(final float x0, final float y0, final float x1, final float y1, final int color) {
86
		return this.provider.effect().createEffect(x0, y0, x1, y1, -0.01F, color, 0, 0.0F);
87
	}
88
89
	private BakedGlyph getGlyph(final int codepoint, final Style style) {
90
		GlyphSource glyphSource = this.getGlyphSource(style.getFont());
91
		BakedGlyph glyph = glyphSource.getGlyph(codepoint);
92
		if (style.isObfuscated() && codepoint != 32) {
93
			int targetWidth = Mth.ceil(glyph.info().getAdvance(false));
94
			glyph = glyphSource.getRandomGlyph(this.random, targetWidth);
95
		}
96
97
		return glyph;
98
	}
99
100
	public Font.PreparedText prepareText(String text, final float x, final float y, final int originalColor, final boolean drawShadow, final int backgroundColor) {
101
		if (this.isBidirectional()) {
102
			text = this.bidirectionalShaping(text);
103
		}
104
105
		Font.PreparedTextBuilder output = new Font.PreparedTextBuilder(x, y, originalColor, backgroundColor, drawShadow, false);
106
		StringDecomposer.iterateFormatted(text, Style.EMPTY, output);
107
		return output;
108
	}
109
110
	public Font.PreparedText prepareText(
111
		final FormattedCharSequence text,
112
		final float x,
113
		final float y,
114
		final int originalColor,
115
		final boolean drawShadow,
116
		final boolean includeEmpty,
117
		final int backgroundColor
118
	) {
119
		Font.PreparedTextBuilder builder = new Font.PreparedTextBuilder(x, y, originalColor, backgroundColor, drawShadow, includeEmpty);
120
		text.accept(builder);
121
		return builder;
122
	}
123
124
	public int width(final String str) {
125
		return Mth.ceil(this.splitter.stringWidth(str));
126
	}
127
128
	public int width(final FormattedText text) {
129
		return Mth.ceil(this.splitter.stringWidth(text));
130
	}
131
132
	public int width(final FormattedCharSequence text) {
133
		return Mth.ceil(this.splitter.stringWidth(text));
134
	}
135
136
	public String plainSubstrByWidth(final String str, final int width, final boolean reverse) {
137
		return reverse ? this.splitter.plainTailByWidth(str, width, Style.EMPTY) : this.splitter.plainHeadByWidth(str, width, Style.EMPTY);
138
	}
139
140
	public String plainSubstrByWidth(final String str, final int width) {
141
		return this.splitter.plainHeadByWidth(str, width, Style.EMPTY);
142
	}
143
144
	public FormattedText substrByWidth(final FormattedText text, final int width) {
145
		return this.splitter.headByWidth(text, width, Style.EMPTY);
146
	}
147
148
	public int wordWrapHeight(final FormattedText input, final int textWidth) {
149
		return 9 * this.splitter.splitLines(input, textWidth, Style.EMPTY).size();
150
	}
151
152
	public List<FormattedCharSequence> split(final FormattedText input, final int maxWidth) {
153
		return Language.getInstance().getVisualOrder(this.splitter.splitLines(input, maxWidth, Style.EMPTY));
154
	}
155
156
	public List<FormattedText> splitIgnoringLanguage(final FormattedText input, final int maxWidth) {
157
		return this.splitter.splitLines(input, maxWidth, Style.EMPTY);
158
	}
159
160
	public boolean isBidirectional() {
161
		return Language.getInstance().isDefaultRightToLeft();
162
	}
163
164
	public StringSplitter getSplitter() {
165
		return this.splitter;
166
	}
167
168
	@Environment(EnvType.CLIENT)
169
	public enum DisplayMode {
170
		NORMAL,
171
		SEE_THROUGH,
172
		POLYGON_OFFSET;
173
	}
174
175
	@Environment(EnvType.CLIENT)
176
	public interface GlyphVisitor {
177
		default void acceptGlyph(final TextRenderable.Styled glyph) {
178
			this.acceptRenderable(glyph);
179
		}
180
181
		default void acceptEffect(final TextRenderable effect) {
182
			this.acceptRenderable(effect);
183
		}
184
185
		default void acceptRenderable(final TextRenderable renderable) {
186
		}
187
188
		default void acceptEmptyArea(final EmptyArea empty) {
189
		}
190
	}
191
192
	@Environment(EnvType.CLIENT)
193
	public interface PreparedText {
194
		void visit(Font.GlyphVisitor visitor);
195
196
		@Nullable ScreenRectangle bounds();
197
	}
198
199
	@Environment(EnvType.CLIENT)
200
	private class PreparedTextBuilder implements Font.PreparedText, FormattedCharSink {
201
		private final boolean drawShadow;
202
		private final int color;
203
		private final int backgroundColor;
204
		private final boolean includeEmpty;
205
		private float x;
206
		private float y;
207
		private float left = Float.MAX_VALUE;
208
		private float top = Float.MAX_VALUE;
209
		private float right = -Float.MAX_VALUE;
210
		private float bottom = -Float.MAX_VALUE;
211
		private float backgroundLeft = Float.MAX_VALUE;
212
		private float backgroundTop = Float.MAX_VALUE;
213
		private float backgroundRight = -Float.MAX_VALUE;
214
		private float backgroundBottom = -Float.MAX_VALUE;
215
		private final List<TextRenderable.Styled> glyphs = new ArrayList<>();
216
		private @Nullable List<TextRenderable> effects;
217
		private @Nullable List<EmptyArea> emptyAreas;
218
219
		public PreparedTextBuilder(final float x, final float y, final int color, final boolean drawShadow, final boolean includeEmpty) {
220
			this(x, y, color, 0, drawShadow, includeEmpty);
221
		}
222
223
		public PreparedTextBuilder(final float x, final float y, final int color, final int backgroundColor, final boolean drawShadow, final boolean includeEmpty) {
224
			this.x = x;
225
			this.y = y;
226
			this.drawShadow = drawShadow;
227
			this.color = color;
228
			this.backgroundColor = backgroundColor;
229
			this.includeEmpty = includeEmpty;
230
			this.markBackground(x, y, 0.0F);
231
		}
232
233
		private void markSize(final float left, final float top, final float right, final float bottom) {
234
			this.left = Math.min(this.left, left);
235
			this.top = Math.min(this.top, top);
236
			this.right = Math.max(this.right, right);
237
			this.bottom = Math.max(this.bottom, bottom);
238
		}
239
240
		private void markBackground(final float x, final float y, final float advance) {
241
			if (ARGB.alpha(this.backgroundColor) != 0) {
242
				this.backgroundLeft = Math.min(this.backgroundLeft, x - 1.0F);
243
				this.backgroundTop = Math.min(this.backgroundTop, y - 1.0F);
244
				this.backgroundRight = Math.max(this.backgroundRight, x + advance);
245
				this.backgroundBottom = Math.max(this.backgroundBottom, y + 9.0F);
246
				this.markSize(this.backgroundLeft, this.backgroundTop, this.backgroundRight, this.backgroundBottom);
247
			}
248
		}
249
250
		private void addGlyph(final TextRenderable.Styled instance) {
251
			this.glyphs.add(instance);
252
			this.markSize(instance.left(), instance.top(), instance.right(), instance.bottom());
253
		}
254
255
		private void addEffect(final TextRenderable effect) {
256
			if (this.effects == null) {
257
				this.effects = new ArrayList<>();
258
			}
259
260
			this.effects.add(effect);
261
			this.markSize(effect.left(), effect.top(), effect.right(), effect.bottom());
262
		}
263
264
		private void addEmptyGlyph(final EmptyArea empty) {
265
			if (this.emptyAreas == null) {
266
				this.emptyAreas = new ArrayList<>();
267
			}
268
269
			this.emptyAreas.add(empty);
270
		}
271
272
		@Override
273
		public boolean accept(final int position, final Style style, final int c) {
274
			BakedGlyph glyph = Font.this.getGlyph(c, style);
275
			return this.accept(position, style, glyph);
276
		}
277
278
		public boolean accept(final int position, final Style style, final BakedGlyph glyph) {
279
			GlyphInfo glyphInfo = glyph.info();
280
			boolean bold = style.isBold();
281
			TextColor styleColor = style.getColor();
282
			int textColor = this.getTextColor(styleColor);
283
			int shadowColor = this.getShadowColor(style, textColor);
284
			float advance = glyphInfo.getAdvance(bold);
285
			float effectX0 = position == 0 ? this.x - 1.0F : this.x;
286
			float shadowOffset = glyphInfo.getShadowOffset();
287
			float boldOffset = bold ? glyphInfo.getBoldOffset() : 0.0F;
288
			TextRenderable.Styled instance = glyph.createGlyph(this.x, this.y, textColor, shadowColor, style, boldOffset, shadowOffset);
289
			if (instance != null) {
290
				this.addGlyph(instance);
291
			} else if (this.includeEmpty) {
292
				this.addEmptyGlyph(new EmptyArea(this.x, this.y, advance, 7.0F, 9.0F, style));
293
			}
294
295
			this.markBackground(this.x, this.y, advance);
296
			if (style.isStrikethrough()) {
297
				this.addEffect(
298
					Font.this.provider.effect().createEffect(effectX0, this.y + 4.5F - 1.0F, this.x + advance, this.y + 4.5F, 0.01F, textColor, shadowColor, shadowOffset)
299
				);
300
			}
301
302
			if (style.isUnderlined()) {
303
				this.addEffect(
304
					Font.this.provider.effect().createEffect(effectX0, this.y + 9.0F - 1.0F, this.x + advance, this.y + 9.0F, 0.01F, textColor, shadowColor, shadowOffset)
305
				);
306
			}
307
308
			this.x += advance;
309
			return true;
310
		}
311
312
		@Override
313
		public void visit(final Font.GlyphVisitor visitor) {
314
			if (ARGB.alpha(this.backgroundColor) != 0) {
315
				visitor.acceptEffect(
316
					Font.this.provider
317
						.effect()
318
						.createEffect(this.backgroundLeft, this.backgroundTop, this.backgroundRight, this.backgroundBottom, -0.01F, this.backgroundColor, 0, 0.0F)
319
				);
320
			}
321
322
			for (TextRenderable.Styled glyph : this.glyphs) {
323
				visitor.acceptGlyph(glyph);
324
			}
325
326
			if (this.effects != null) {
327
				for (TextRenderable effect : this.effects) {
328
					visitor.acceptEffect(effect);
329
				}
330
			}
331
332
			if (this.emptyAreas != null) {
333
				for (EmptyArea emptyArea : this.emptyAreas) {
334
					visitor.acceptEmptyArea(emptyArea);
335
				}
336
			}
337
		}
338
339
		public void discardEffects() {
340
			this.effects = null;
341
		}
342
343
		private int getTextColor(final @Nullable TextColor textColor) {
344
			if (textColor != null) {
345
				int alpha = ARGB.alpha(this.color);
346
				int rgb = textColor.getValue();
347
				return ARGB.color(alpha, rgb);
348
			} else {
349
				return this.color;
350
			}
351
		}
352
353
		private int getShadowColor(final Style style, final int textColor) {
354
			Integer shadow = style.getShadowColor();
355
			if (shadow != null) {
356
				float textAlpha = ARGB.alphaFloat(textColor);
357
				float shadowAlpha = ARGB.alphaFloat(shadow);
358
				return textAlpha != 1.0F ? ARGB.color(ARGB.as8BitChannel(textAlpha * shadowAlpha), shadow) : shadow;
359
			} else {
360
				return this.drawShadow ? ARGB.scaleRGB(textColor, 0.25F) : 0;
361
			}
362
		}
363
364
		@Override
365
		public @Nullable ScreenRectangle bounds() {
366
			if (!(this.left >= this.right) && !(this.top >= this.bottom)) {
367
				int left = Mth.floor(this.left);
368
				int top = Mth.floor(this.top);
369
				int right = Mth.ceil(this.right);
370
				int bottom = Mth.ceil(this.bottom);
371
				return new ScreenRectangle(left, top, right - left, bottom - top);
372
			} else {
373
				return null;
374
			}
375
		}
376
	}
377
378
	@Environment(EnvType.CLIENT)
379
	public interface Provider {
380
		GlyphSource glyphs(FontDescription font);
381
382
		EffectGlyph effect();
383
	}
384
}
385

Decompiled Minecraft 26.3 source of Font (net/minecraft/client/gui/Font.java) with Mojang official mappings, stored verbatim on the site.