From 98851e0ab8813a55094d3442e500883e2d9b2b12 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 27 Sep 2024 12:30:59 +0200 Subject: [PATCH] [orx-shapes] Add font scaler to shapesFromText --- .../src/commonMain/kotlin/text/TextShapes.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/orx-shapes/src/commonMain/kotlin/text/TextShapes.kt b/orx-shapes/src/commonMain/kotlin/text/TextShapes.kt index 25aa77ea..0bb6dc2f 100644 --- a/orx-shapes/src/commonMain/kotlin/text/TextShapes.kt +++ b/orx-shapes/src/commonMain/kotlin/text/TextShapes.kt @@ -1,27 +1,35 @@ package org.openrndr.extra.shapes.text import org.openrndr.draw.font.Face +import org.openrndr.draw.font.fontHeightScaler import org.openrndr.math.Vector2 import org.openrndr.math.transforms.buildTransform import org.openrndr.shape.Shape -fun shapesFromText(face: Face, text: String, size: Double, position: Vector2 = Vector2.ZERO): List { +fun shapesFromText( + face: Face, + text: String, + size: Double, + position: Vector2 = Vector2.ZERO, + scaler: (Face) -> Double = ::fontHeightScaler +): List { var cursor = position + val scale = size * scaler(face) return text.windowed(2, 1, partialWindows = true) { if (it[0] == '\n') { - cursor = Vector2(position.x, cursor.y + face.lineSpace(size)) + cursor = Vector2(position.x, cursor.y + face.lineSpace(scale)) Shape.EMPTY } else { val glyph = face.glyphForCharacter(it.first()) - val shape = glyph.shape(size).transform(buildTransform { + val shape = glyph.shape(scale).transform(buildTransform { translate(cursor) }) 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 } }.filter { !it.empty }