From 82d2027cf300f06a25259734c2a36f9f0a0101dd Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Sun, 9 Jun 2024 19:20:35 +0300 Subject: [PATCH] Add measure string function to Typesetter. --- Quik/Typography/Typesetter.cs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Quik/Typography/Typesetter.cs b/Quik/Typography/Typesetter.cs index 46e8a92..5437e68 100644 --- a/Quik/Typography/Typesetter.cs +++ b/Quik/Typography/Typesetter.cs @@ -2,9 +2,7 @@ using Quik.Media; using System; using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; namespace Quik.Typography { @@ -76,10 +74,38 @@ namespace Quik.Typography } } - public static void TypesetHorizontalDirect(this CommandList list, string str, QVec2 origin, float size, QFont font) + public static QVec2 MeasureHorizontal(ReadOnlySpan str, float size, QFont font) + { + var enumerator = new LineEnumerator(str); + + float width = 0.0f; + float height = 0.0f; + + while (enumerator.MoveNext()) + { + ReadOnlySpan line = enumerator.Current; + + float lineHeight = 0.0f; + + foreach (Rune r in line.EnumerateRunes()) + { + int codepoint = r.Value; + font.Get(codepoint, size, out FontGlyph glyph); + + width += glyph.Metrics.Advance.X; + lineHeight = Math.Max(lineHeight, glyph.Metrics.Size.Y); + } + + height += lineHeight; + } + + return new QVec2(width, height); + } + + public static void TypesetHorizontalDirect(this CommandList list, ReadOnlySpan str, QVec2 origin, float size, QFont font) { Dictionary drawInfo = new Dictionary(); - var enumerator = new LineEnumerator(str.AsSpan()); + var enumerator = new LineEnumerator(str); QVec2 pen = origin; while (enumerator.MoveNext())