-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathBasicFontRenderer.kt
More file actions
292 lines (254 loc) · 10.3 KB
/
Copy pathBasicFontRenderer.kt
File metadata and controls
292 lines (254 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package gg.essential.elementa.font
import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.constraints.ConstraintType
import gg.essential.elementa.constraints.resolution.ConstraintVisitor
import gg.essential.elementa.font.data.Font
import gg.essential.elementa.font.data.Glyph
import gg.essential.universal.UGraphics
import gg.essential.universal.UMatrixStack
import gg.essential.universal.render.UGpuSampler
import gg.essential.universal.render.URenderPipeline
import gg.essential.universal.shader.BlendState
import gg.essential.universal.vertex.UBufferBuilder
import gg.essential.universal.vertex.UVertexConsumer
import java.awt.Color
import kotlin.math.max
class BasicFontRenderer(
private val regularFont: Font
) : FontProvider {
/* Required by Elementa but unused for this type of constraint */
override var cachedValue: FontProvider = this
override var recalculate: Boolean = false
override var constrainTo: UIComponent? = null
override fun getStringWidth(string: String, pointSize: Float): Float {
return getStringDimensions(string, pointSize).first
}
override fun getStringHeight(string: String, pointSize: Float): Float {
return getStringDimensions(string, pointSize).second
}
private fun getStringDimensions(string: String, pointSize: Float): Pair<Float, Float> {
var width = 0f
var height = 0f
/*
10 point font is the default used in Elementa.
Adjust the point size based on this font's size.
*/
val currentPointSize = pointSize / 10 * regularFont.fontInfo.atlas.size
var i = 0
while (i < string.length) {
val char = string[i]
//Ignore formatting codes for purpose of calculating string dimensions
if (char == '\u00a7' && i + 1 < string.length) {
//not handled by this font renderer
i += 2
continue
}
val glyph = regularFont.fontInfo.glyphs[char.code]
if (glyph?.atlasBounds == null) {
i++
continue
}
val planeBounds = glyph.planeBounds
if (planeBounds != null) {
height = max((planeBounds.top - planeBounds.bottom) * currentPointSize, height)
}
//The last character should not have the whitespace to the right of it
//Added to the width. Instead, we only add the width of the character
val lastCorrection = if (i < string.length - 1) 0 else 1
//The texture atlas is used here because in the context of this implementation of the font renderer
//we do not need or want the full precision the msdf font renderer exports in. Instead, we care about
//calculating width based on the texture pixels
width += (((glyph.atlasBounds.right - glyph.atlasBounds.left - lastCorrection) / regularFont.fontInfo.atlas.size) * currentPointSize)
i++
}
return Pair(width, height)
}
fun getLineHeight(pointSize: Float): Float {
return regularFont.fontInfo.metrics.lineHeight * pointSize
}
override fun drawString(
matrixStack: UMatrixStack,
string: String,
color: Color,
x: Float,
y: Float,
originalPointSize: Float,
scale: Float,
shadow: Boolean,
shadowColor: Color?
) {
/*
10 point font is the default used in Elementa.
Adjust the point size based on this font's size.
*/
val scaledPointSize = originalPointSize / 10 * regularFont.fontInfo.atlas.size
/*
Moved one pixel up so that the main body of the text is in
the top left of the component. This change keeps text location
in the same location as the vanilla font renderer relative to
a UIText component.
*/
if (shadow) {
drawStringNow(
matrixStack,
string,
shadowColor ?: Color(
((color.rgb and 16579836).shr(2)).or((color.rgb).and(-16777216))
),
x + 1,
y,
scaledPointSize * scale
)
}
drawStringNow(
matrixStack,
string,
color,
x,
y - 1,
scaledPointSize * scale
)
}
override fun getBaseLineHeight(): Float {
return regularFont.fontInfo.atlas.baseCharHeight
}
override fun getShadowHeight(): Float {
return regularFont.fontInfo.atlas.shadowHeight
}
override fun getBelowLineHeight(): Float {
return regularFont.fontInfo.atlas.belowLineHeight
}
private fun drawStringNow(
matrixStack: UMatrixStack,
string: String,
color: Color,
x: Float,
y: Float,
originalPointSize: Float
) {
if (URenderPipeline.isRequired || ElementaVersion.atLeastV9Active) {
val bufferBuilder = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_TEXTURE_COLOR)
drawStringNow(bufferBuilder, matrixStack, string, color, x, y, originalPointSize)
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE) {
texture(0, regularFont.getTexture().gpuTextureView, UGpuSampler(
UGpuSampler.AddressMode.CLAMP_TO_EDGE,
UGpuSampler.AddressMode.CLAMP_TO_EDGE,
UGpuSampler.FilterMode.NEAREST,
UGpuSampler.FilterMode.NEAREST,
false,
))
}
} else {
UGraphics.bindTexture(0, regularFont.getTexture().dynamicGlId)
val bufferBuilder = UGraphics.getFromTessellator()
@Suppress("DEPRECATION")
bufferBuilder.beginWithDefaultShader(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_TEXTURE_COLOR)
drawStringNow(bufferBuilder.asUVertexConsumer(), matrixStack, string, color, x, y, originalPointSize)
bufferBuilder.drawDirect()
}
}
private fun drawStringNow(
vertexConsumer: UVertexConsumer,
matrixStack: UMatrixStack,
string: String,
color: Color,
x: Float,
y: Float,
originalPointSize: Float
) {
var currentX = x
var i = 0
while (i < string.length) {
val char = string[i]
// Ignore color code characters in this font renderer
if (char == '\u00a7' && i + 1 < string.length) {
i += 2
continue
}
val glyph = regularFont.fontInfo.glyphs[char.code]
if (glyph == null) {
i++
continue
}
val planeBounds = glyph.planeBounds
if (planeBounds != null) {
val width = (planeBounds.right - planeBounds.left) * originalPointSize
val height = (planeBounds.top - planeBounds.bottom) * originalPointSize
drawGlyph(
vertexConsumer,
matrixStack,
glyph,
color,
currentX,
y + planeBounds.bottom * originalPointSize,
width,
height
)
}
//The texture atlas is used here because in the context of this implementation of the font renderer
//we do not need or want the full precision the msdf font renderer exports in. Instead, we care about
//calculating width based on the texture pixels
if (glyph.atlasBounds != null) {
currentX += (((glyph.atlasBounds.right - glyph.atlasBounds.left) / regularFont.fontInfo.atlas.size) * originalPointSize)
} else {
currentX += (glyph.advance) * originalPointSize
}
i++
}
}
private fun drawGlyph(
worldRenderer: UVertexConsumer,
matrixStack: UMatrixStack,
glyph: Glyph,
color: Color,
x: Float,
y: Float,
width: Float,
height: Float
) {
val atlasBounds = glyph.atlasBounds ?: return
val atlas = regularFont.fontInfo.atlas
val textureTop = 1.0 - ((atlasBounds.top) / atlas.height)
val textureBottom = 1.0 - ((atlasBounds.bottom) / atlas.height)
val textureLeft = (atlasBounds.left / atlas.width).toDouble()
val textureRight = (atlasBounds.right / atlas.width).toDouble()
val doubleX = x.toDouble()
val doubleY = y.toDouble()
worldRenderer.pos(matrixStack, doubleX, doubleY + height, 0.0).tex(textureLeft, textureBottom).color(
color.red,
color.green,
color.blue,
255
).endVertex()
worldRenderer.pos(matrixStack, doubleX + width, doubleY + height, 0.0).tex(textureRight, textureBottom).color(
color.red,
color.green,
color.blue,
255
).endVertex()
worldRenderer.pos(matrixStack, doubleX + width, doubleY, 0.0).tex(textureRight, textureTop).color(
color.red,
color.green,
color.blue,
255
).endVertex()
worldRenderer.pos(matrixStack, doubleX, doubleY, 0.0).tex(textureLeft, textureTop).color(
color.red,
color.green,
color.blue,
255
).endVertex()
}
override fun visitImpl(visitor: ConstraintVisitor, type: ConstraintType) {
}
private companion object {
private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:basic_font", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_TEXTURE_COLOR).apply {
@Suppress("DEPRECATION")
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
}.build()
private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:basic_font", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_TEXTURE_COLOR).apply {
blendState = BlendState.ALPHA
}.build()
}
}