[orx-shapes] Add font scaler to shapesFromText

This commit is contained in:
Edwin Jakobs
2024-09-27 12:30:59 +02:00
parent bdcb6af551
commit 98851e0ab8

View File

@@ -1,27 +1,35 @@
package org.openrndr.extra.shapes.text package org.openrndr.extra.shapes.text
import org.openrndr.draw.font.Face import org.openrndr.draw.font.Face
import org.openrndr.draw.font.fontHeightScaler
import org.openrndr.math.Vector2 import org.openrndr.math.Vector2
import org.openrndr.math.transforms.buildTransform import org.openrndr.math.transforms.buildTransform
import org.openrndr.shape.Shape import org.openrndr.shape.Shape
fun shapesFromText(face: Face, text: String, size: Double, position: Vector2 = Vector2.ZERO): List<Shape> { fun shapesFromText(
face: Face,
text: String,
size: Double,
position: Vector2 = Vector2.ZERO,
scaler: (Face) -> Double = ::fontHeightScaler
): List<Shape> {
var cursor = position var cursor = position
val scale = size * scaler(face)
return text.windowed(2, 1, partialWindows = true) { return text.windowed(2, 1, partialWindows = true) {
if (it[0] == '\n') { if (it[0] == '\n') {
cursor = Vector2(position.x, cursor.y + face.lineSpace(size)) cursor = Vector2(position.x, cursor.y + face.lineSpace(scale))
Shape.EMPTY Shape.EMPTY
} else { } else {
val glyph = face.glyphForCharacter(it.first()) val glyph = face.glyphForCharacter(it.first())
val shape = glyph.shape(size).transform(buildTransform { val shape = glyph.shape(scale).transform(buildTransform {
translate(cursor) translate(cursor)
}) })
if (it.length == 2) { if (it.length == 2) {
cursor += Vector2(face.kernAdvance(size, it[0], it[1]), 0.0) cursor += Vector2(face.kernAdvance(scale, it[0], it[1]), 0.0)
} }
cursor += Vector2(glyph.advanceWidth(size), 0.0) cursor += Vector2(glyph.advanceWidth(scale), 0.0)
shape shape
} }
}.filter { !it.empty } }.filter { !it.empty }