diff --git a/src/pixie.nim b/src/pixie.nim index 26358dc..d9b066a 100644 --- a/src/pixie.nim +++ b/src/pixie.nim @@ -339,7 +339,7 @@ proc fillText*( wrap = true, kerning = true ) = - for path in font.typesetPaths( + for path in font.typeset( text, bounds, hAlign, @@ -347,7 +347,7 @@ proc fillText*( textCase, wrap, kerning - ): + ).paths: image.fillPath(path, color, transform) proc fillText*( @@ -362,7 +362,7 @@ proc fillText*( wrap = true, kerning = true ) = - for path in font.typesetPaths( + for path in font.typeset( text, bounds, hAlign, @@ -370,7 +370,7 @@ proc fillText*( textCase, wrap, kerning - ): + ).paths: mask.fillPath(path, transform) proc strokeText*( @@ -387,7 +387,7 @@ proc strokeText*( wrap = true, kerning = true ) = - for path in font.typesetPaths( + for path in font.typeset( text, bounds, hAlign, @@ -395,7 +395,7 @@ proc strokeText*( textCase, wrap, kerning - ): + ).paths: image.strokePath(path, color, transform, strokeWidth) proc strokeText*( @@ -411,7 +411,7 @@ proc strokeText*( wrap = true, kerning = true ) = - for path in font.typesetPaths( + for path in font.typeset( text, bounds, hAlign, @@ -419,5 +419,5 @@ proc strokeText*( textCase, wrap, kerning - ): + ).paths: mask.strokePath(path, transform, strokeWidth) diff --git a/src/pixie/fonts.nim b/src/pixie/fonts.nim index e07b1c9..5925cf5 100644 --- a/src/pixie/fonts.nim +++ b/src/pixie/fonts.nim @@ -1,4 +1,4 @@ -import pixie/fontformats/opentype, pixie/fontformats/svgfont, pixie/paths, +import bumpy, pixie/fontformats/opentype, pixie/fontformats/svgfont, pixie/paths, unicode, vmath const AutoLineHeight* = -1.float32 ## Use default line height for the font size @@ -13,9 +13,11 @@ type size*: float32 ## Font size in pixels. lineHeight*: float32 ## The line height in pixels or AutoLineHeight for the font's default line height. - Typesetting* = ref object + Arrangement* = ref object + font*: Font runes*: seq[Rune] positions*: seq[Vec2] + selectionRects*: seq[Rect] HAlignMode* = enum haLeft @@ -116,8 +118,9 @@ proc typeset*( textCase = tcNormal, wrap = true, kerning = true -): Typesetting = - result = Typesetting() +): Arrangement = + result = Arrangement() + result.font = font result.runes = toRunes(text) result.runes.convertTextCase(textCase) result.positions.setLen(result.runes.len) @@ -128,7 +131,7 @@ proc typeset*( else: font.defaultLineHeight - proc glyphAdvance( + proc advance( font: Font, runes: seq[Rune], i: int, kerning: bool ): float32 {.inline.} = if kerning and i + 1 < runes.len: @@ -145,7 +148,7 @@ proc typeset*( if rune.canWrap(): prevCanWrap = i - let advance = glyphAdvance(font, result.runes, i, kerning) + let advance = advance(font, result.runes, i, kerning) if rune != Rune(32) and bounds.x > 0 and at.x + advance > bounds.x: # Wrap to new line at.x = 0 @@ -155,34 +158,16 @@ proc typeset*( if prevCanWrap > 0 and prevCanWrap != i: for j in prevCanWrap + 1 ..< i: result.positions[j] = at - at.x += glyphAdvance(font, result.runes, j, kerning) + at.x += advance(font, result.runes, j, kerning) result.positions[i] = at at.x += advance -iterator typesetPaths*( - font: Font, - text: string, - bounds = vec2(0, 0), - hAlign = haLeft, - vAlign = vaTop, - textCase = tcNormal, - wrap = true, - kerning = true -): Path = - let typesetText = font.typeset( - text, - bounds, - hAlign, - vAlign, - textCase, - wrap, - kerning - ) - for i in 0 ..< typesetText.runes.len: - var path = font.typeface.getGlyphPath(typesetText.runes[i]) +iterator paths*(arrangement: Arrangement): Path = + for i in 0 ..< arrangement.runes.len: + var path = arrangement.font.typeface.getGlyphPath(arrangement.runes[i]) path.transform( - translate(typesetText.positions[i]) * scale(vec2(font.scale)) + translate(arrangement.positions[i]) * scale(vec2(arrangement.font.scale)) ) yield path diff --git a/tests/benchmark_fonts.nim b/tests/benchmark_fonts.nim index 796acf7..eb3f2d5 100644 --- a/tests/benchmark_fonts.nim +++ b/tests/benchmark_fonts.nim @@ -2,7 +2,7 @@ import benchy, pixie const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in quam in nulla bibendum luctus. Integer dui lectus, ultricies commodo enim quis, laoreet lacinia erat. Vivamus ultrices maximus risus, non aliquam quam sagittis quis. Ut nec diam vitae tortor interdum ullamcorper in aliquet velit. Ut sed lobortis mi. Nulla venenatis lectus varius justo lacinia, quis sollicitudin nunc ultrices. Donec a suscipit arcu, id egestas neque. Nullam commodo pharetra est. Nullam gravida nibh eget quam venenatis lacinia. Vestibulum et libero arcu. Sed dignissim enim eros. Nullam eleifend luctus erat sed luctus. Nunc tincidunt, mi nec tincidunt tristique, ex nulla lobortis sem, sit amet finibus purus justo non massa." -var font = readFont("tests/fonts/Roboto-Regular.ttf") +var font = readFont("tests/fonts/Roboto-Regular_1.ttf") font.size = 16 timeIt "typeset":