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())