Cache text width when aligning it multiple times

This commit is contained in:
DBotThePony 2023-07-24 09:35:14 +07:00
parent 699b4e896c
commit 8407ae1328
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -12,6 +12,7 @@ import net.minecraft.network.chat.Component
import net.minecraft.util.FormattedCharSequence import net.minecraft.util.FormattedCharSequence
import org.joml.Matrix4f import org.joml.Matrix4f
import org.joml.Vector3f import org.joml.Vector3f
import ru.dbotthepony.mc.otm.core.FloatSupplier
import ru.dbotthepony.mc.otm.core.math.IAngle import ru.dbotthepony.mc.otm.core.math.IAngle
import ru.dbotthepony.mc.otm.core.math.RGBAColor import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.core.math.Vector import ru.dbotthepony.mc.otm.core.math.Vector
@ -77,12 +78,12 @@ private fun Font.drawInBatch(
text: Any, x: Float, y: Float, color: Int, drawShadow: Boolean, text: Any, x: Float, y: Float, color: Int, drawShadow: Boolean,
matrix4f: Matrix4f, buffer: MultiBufferSource, displayMode: Font.DisplayMode, matrix4f: Matrix4f, buffer: MultiBufferSource, displayMode: Font.DisplayMode,
effectColor: Int, packedLightCoords: Int, gravity: RenderGravity, effectColor: Int, packedLightCoords: Int, gravity: RenderGravity,
rounding: GravityRounding rounding: GravityRounding, width: FloatSupplier
): Int { ): Int {
if (text is String) if (text is String)
return drawInBatch( return drawInBatch(
text, text,
gravity.x(x, this, text, rounding = rounding), gravity.x(x, width, rounding = rounding),
gravity.y(y, lineHeight.toFloat(), rounding = rounding), gravity.y(y, lineHeight.toFloat(), rounding = rounding),
color, color,
drawShadow, drawShadow,
@ -94,7 +95,7 @@ private fun Font.drawInBatch(
else if (text is Component) else if (text is Component)
return drawInBatch( return drawInBatch(
text, text,
gravity.x(x, this, text, rounding = rounding), gravity.x(x, width, rounding = rounding),
gravity.y(y, lineHeight.toFloat(), rounding = rounding), gravity.y(y, lineHeight.toFloat(), rounding = rounding),
color, color,
drawShadow, drawShadow,
@ -106,7 +107,7 @@ private fun Font.drawInBatch(
else if (text is FormattedCharSequence) else if (text is FormattedCharSequence)
return drawInBatch( return drawInBatch(
text, text,
gravity.x(x, this, text, rounding = rounding), gravity.x(x, width, rounding = rounding),
gravity.y(y, lineHeight.toFloat(), rounding = rounding), gravity.y(y, lineHeight.toFloat(), rounding = rounding),
color, color,
drawShadow, drawShadow,
@ -126,6 +127,48 @@ private val outlinePositions = listOf(
0 to -1, 0 to -1,
) )
private class CachedTextWidth0(val font: Font, val text: Component) : FloatSupplier {
private var computed = false
private var width = 0f
override fun getAsFloat(): Float {
if (!computed) {
width = font.width(text).toFloat()
computed = true
}
return width
}
}
private class CachedTextWidth1(val font: Font, val text: String) : FloatSupplier {
private var computed = false
private var width = 0f
override fun getAsFloat(): Float {
if (!computed) {
width = font.width(text).toFloat()
computed = true
}
return width
}
}
private class CachedTextWidth2(val font: Font, val text: FormattedCharSequence) : FloatSupplier {
private var computed = false
private var width = 0f
override fun getAsFloat(): Float {
if (!computed) {
width = font.width(text).toFloat()
computed = true
}
return width
}
}
private fun Font.drawInternal( private fun Font.drawInternal(
poseStack: PoseStack, poseStack: PoseStack,
text: Any, text: Any,
@ -164,6 +207,15 @@ private fun Font.drawInternal(
inv = 1f inv = 1f
} }
val cache = if (text is Component)
CachedTextWidth0(this, text)
else if (text is String)
CachedTextWidth1(this, text)
else if (text is FormattedCharSequence)
CachedTextWidth2(this, text)
else
throw IllegalArgumentException(text::class.qualifiedName)
if (customShadow) { if (customShadow) {
if (shadowZ != 0f) { if (shadowZ != 0f) {
poseStack.translate(0f, 0f, shadowZ) poseStack.translate(0f, 0f, shadowZ)
@ -180,7 +232,7 @@ private fun Font.drawInternal(
displayMode, displayMode,
effectColor, effectColor,
packedLightCoords, packedLightCoords,
gravity, rounding) gravity, rounding, cache)
if (shadowZ != 0f) { if (shadowZ != 0f) {
poseStack.translate(0f, 0f, -shadowZ) poseStack.translate(0f, 0f, -shadowZ)
@ -204,7 +256,7 @@ private fun Font.drawInternal(
displayMode, displayMode,
effectColor, effectColor,
packedLightCoords, packedLightCoords,
gravity, rounding) gravity, rounding, cache)
} }
if (outlineZ != 0f) { if (outlineZ != 0f) {
@ -223,7 +275,7 @@ private fun Font.drawInternal(
displayMode, displayMode,
effectColor, effectColor,
packedLightCoords, packedLightCoords,
gravity, rounding) gravity, rounding, cache)
if (scale != 1f) { if (scale != 1f) {
poseStack.popPose() poseStack.popPose()
@ -252,7 +304,7 @@ fun Font.draw(
shadowColor: RGBAColor = RGBAColor.BLACK, shadowColor: RGBAColor = RGBAColor.BLACK,
shadowX: Float = 1f, shadowX: Float = 1f,
shadowY: Float = 1f, shadowY: Float = 1f,
shadowZ: Float = -0.1f, shadowZ: Float = 0.1f,
customShadow: Boolean = false, customShadow: Boolean = false,
rounding: GravityRounding = if (scale == 1f) GravityRounding.DEFAULT else GravityRounding.NO, rounding: GravityRounding = if (scale == 1f) GravityRounding.DEFAULT else GravityRounding.NO,
drawOutline: Boolean = false, drawOutline: Boolean = false,
@ -300,7 +352,7 @@ fun Font.draw(
shadowColor: RGBAColor = RGBAColor.BLACK, shadowColor: RGBAColor = RGBAColor.BLACK,
shadowX: Float = 1f, shadowX: Float = 1f,
shadowY: Float = 1f, shadowY: Float = 1f,
shadowZ: Float = -0.1f, shadowZ: Float = 0.1f,
customShadow: Boolean = false, customShadow: Boolean = false,
rounding: GravityRounding = if (scale == 1f) GravityRounding.DEFAULT else GravityRounding.NO, rounding: GravityRounding = if (scale == 1f) GravityRounding.DEFAULT else GravityRounding.NO,
drawOutline: Boolean = false, drawOutline: Boolean = false,