diff --git a/src/pixie/fonts.nim b/src/pixie/fonts.nim index 22e8860..e92f921 100644 --- a/src/pixie/fonts.nim +++ b/src/pixie/fonts.nim @@ -500,6 +500,60 @@ proc parseSvgFont*(buf: string): Typeface {.raises: [PixieError].} = result = Typeface() result.svgFont = svgfont.parseSvgFont(buf) + +proc computePaths( + arrangement: Arrangement +): seq[Path] = + ## Takes an Arrangement and computes Paths for drawing. + ## Returns a seq of paths that match the Spans in the arrangement. + ## If you only have one Span you should only get one Path. + var line: int + for spanIndex, (start, stop) in arrangement.spans: + var spanPath = Path() + let + font = arrangement.fonts[spanIndex] + underlineThickness = font.typeface.underlineThickness * font.scale + underlinePosition = font.typeface.underlinePosition * font.scale + strikeoutThickness = font.typeface.strikeoutThickness * font.scale + strikeoutPosition = font.typeface.strikeoutPosition * font.scale + for runeIndex in start .. stop: + let position = arrangement.positions[runeIndex] + + let path = font.typeface.getGlyphPath(arrangement.runes[runeIndex]) + path.transform( + translate(position) * + scale(vec2(font.scale)) + ) + + var applyDecoration = true + if runeIndex == arrangement.lines[line][1]: + inc line + if arrangement.runes[runeIndex] == SP: + # Do not apply decoration to the space at end of lines + applyDecoration = false + + if applyDecoration: + if font.underline: + path.rect( + arrangement.selectionRects[runeIndex].x, + position.y - underlinePosition + underlineThickness / 2, + arrangement.selectionRects[runeIndex].w, + underlineThickness, + font.typeface.isCCW() + ) + if font.strikethrough: + path.rect( + arrangement.selectionRects[runeIndex].x, + position.y - strikeoutPosition, + arrangement.selectionRects[runeIndex].w, + strikeoutThickness, + font.typeface.isCCW() + ) + + spanPath.addPath(path) + + result.add(spanPath) + proc textUber( target: Image | Mask, arrangement: Arrangement, @@ -512,6 +566,7 @@ proc textUber( stroke: static[bool] = false ) = var line: int + let spanPaths = arrangement.computePaths() for spanIndex, (start, stop) in arrangement.spans: let font = arrangement.fonts[spanIndex] @@ -519,56 +574,15 @@ proc textUber( underlinePosition = font.typeface.underlinePosition * font.scale strikeoutThickness = font.typeface.strikeoutThickness * font.scale strikeoutPosition = font.typeface.strikeoutPosition * font.scale - for runeIndex in start .. stop: - let position = arrangement.positions[runeIndex] - let path = font.typeface.getGlyphPath(arrangement.runes[runeIndex]) - path.transform( - translate(position) * - scale(vec2(font.scale)) - ) + let path = spanPaths[spanIndex] - var applyDecoration = true - if runeIndex == arrangement.lines[line][1]: - inc line - if arrangement.runes[runeIndex] == SP: - # Do not apply decoration to the space at end of lines - applyDecoration = false - - if applyDecoration: - if font.underline: - path.rect( - arrangement.selectionRects[runeIndex].x, - position.y - underlinePosition + underlineThickness / 2, - arrangement.selectionRects[runeIndex].w, - underlineThickness, - font.typeface.isCCW() - ) - if font.strikethrough: - path.rect( - arrangement.selectionRects[runeIndex].x, - position.y - strikeoutPosition, - arrangement.selectionRects[runeIndex].w, - strikeoutThickness, - font.typeface.isCCW() - ) - - when stroke: - when type(target) is Image: - for paint in font.paints: - target.strokePath( - path, - paint, - transform, - strokeWidth, - lineCap, - lineJoin, - miterLimit, - dashes - ) - else: # target is Mask + when stroke: + when type(target) is Image: + for paint in font.paints: target.strokePath( path, + paint, transform, strokeWidth, lineCap, @@ -576,63 +590,31 @@ proc textUber( miterLimit, dashes ) - else: - when type(target) is Image: - for paint in font.paints: - target.fillPath(path, paint, transform) - else: # target is Mask - target.fillPath(path, transform) + else: # target is Mask + target.strokePath( + path, + transform, + strokeWidth, + lineCap, + lineJoin, + miterLimit, + dashes + ) + else: + when type(target) is Image: + for paint in font.paints: + #echo transform + target.fillPath(path, paint, transform) + else: # target is Mask + target.fillPath(path, transform) proc computeBounds*( arrangement: Arrangement, transform = mat3() ): Rect = - var - fullPath = newPath() - line: int - for spanIndex, (start, stop) in arrangement.spans: - let - font = arrangement.fonts[spanIndex] - underlineThickness = font.typeface.underlineThickness * font.scale - underlinePosition = font.typeface.underlinePosition * font.scale - strikeoutThickness = font.typeface.strikeoutThickness * font.scale - strikeoutPosition = font.typeface.strikeoutPosition * font.scale - for runeIndex in start .. stop: - let position = arrangement.positions[runeIndex] - - let path = font.typeface.getGlyphPath(arrangement.runes[runeIndex]) - path.transform( - translate(position) * - scale(vec2(font.scale)) - ) - - var applyDecoration = true - if runeIndex == arrangement.lines[line][1]: - inc line - if arrangement.runes[runeIndex] == SP: - # Do not apply decoration to the space at end of lines - applyDecoration = false - - if applyDecoration: - if font.underline: - path.rect( - arrangement.selectionRects[runeIndex].x, - position.y - underlinePosition + underlineThickness / 2, - arrangement.selectionRects[runeIndex].w, - underlineThickness, - font.typeface.isCCW() - ) - if font.strikethrough: - path.rect( - arrangement.selectionRects[runeIndex].x, - position.y - strikeoutPosition, - arrangement.selectionRects[runeIndex].w, - strikeoutThickness, - font.typeface.isCCW() - ) - - fullPath.addPath(path) - + var fullPath = newPath() + for path in arrangement.computePaths(): + fullPath.addPath(path) fullPath.transform(transform) fullPath.computeBounds() diff --git a/tests/fonts/diffs/customlineheight.png b/tests/fonts/diffs/customlineheight.png index ccce547..4b1283a 100644 Binary files a/tests/fonts/diffs/customlineheight.png and b/tests/fonts/diffs/customlineheight.png differ diff --git a/tests/fonts/diffs/paragraph1.png b/tests/fonts/diffs/paragraph1.png index fe7fc52..55ddc06 100644 Binary files a/tests/fonts/diffs/paragraph1.png and b/tests/fonts/diffs/paragraph1.png differ diff --git a/tests/fonts/diffs/paragraph1_nokern.png b/tests/fonts/diffs/paragraph1_nokern.png index 9222a66..234d52b 100644 Binary files a/tests/fonts/diffs/paragraph1_nokern.png and b/tests/fonts/diffs/paragraph1_nokern.png differ diff --git a/tests/fonts/diffs/paragraph3_nokern.png b/tests/fonts/diffs/paragraph3_nokern.png index c1941e3..ba40833 100644 Binary files a/tests/fonts/diffs/paragraph3_nokern.png and b/tests/fonts/diffs/paragraph3_nokern.png differ diff --git a/tests/fonts/diffs/paragraph3_nokern_2.png b/tests/fonts/diffs/paragraph3_nokern_2.png index e00dae2..6f15308 100644 Binary files a/tests/fonts/diffs/paragraph3_nokern_2.png and b/tests/fonts/diffs/paragraph3_nokern_2.png differ diff --git a/tests/fonts/diffs/paragraph3_nokern_3.png b/tests/fonts/diffs/paragraph3_nokern_3.png index 2ff2c7e..acce806 100644 Binary files a/tests/fonts/diffs/paragraph3_nokern_3.png and b/tests/fonts/diffs/paragraph3_nokern_3.png differ diff --git a/tests/fonts/diffs/paragraph4.png b/tests/fonts/diffs/paragraph4.png index 2d3c3e9..2835c3f 100644 Binary files a/tests/fonts/diffs/paragraph4.png and b/tests/fonts/diffs/paragraph4.png differ diff --git a/tests/fonts/diffs/paragraph4_2.png b/tests/fonts/diffs/paragraph4_2.png index 4799708..abd0af9 100644 Binary files a/tests/fonts/diffs/paragraph4_2.png and b/tests/fonts/diffs/paragraph4_2.png differ diff --git a/tests/fonts/diffs/paragraph4_3.png b/tests/fonts/diffs/paragraph4_3.png index 997f544..4e222ec 100644 Binary files a/tests/fonts/diffs/paragraph4_3.png and b/tests/fonts/diffs/paragraph4_3.png differ diff --git a/tests/fonts/diffs/paragraph4_nokern.png b/tests/fonts/diffs/paragraph4_nokern.png index 1c91a99..5761068 100644 Binary files a/tests/fonts/diffs/paragraph4_nokern.png and b/tests/fonts/diffs/paragraph4_nokern.png differ diff --git a/tests/fonts/diffs/paragraph4_nokern_3.png b/tests/fonts/diffs/paragraph4_nokern_3.png index a2c420a..e2b27a5 100644 Binary files a/tests/fonts/diffs/paragraph4_nokern_3.png and b/tests/fonts/diffs/paragraph4_nokern_3.png differ diff --git a/tests/fonts/diffs/paragraph5.png b/tests/fonts/diffs/paragraph5.png index 651c63c..198c715 100644 Binary files a/tests/fonts/diffs/paragraph5.png and b/tests/fonts/diffs/paragraph5.png differ diff --git a/tests/fonts/diffs/paragraph5_2.png b/tests/fonts/diffs/paragraph5_2.png index c95b103..cbc1d30 100644 Binary files a/tests/fonts/diffs/paragraph5_2.png and b/tests/fonts/diffs/paragraph5_2.png differ diff --git a/tests/fonts/diffs/paragraph5_3.png b/tests/fonts/diffs/paragraph5_3.png index 119d697..9700156 100644 Binary files a/tests/fonts/diffs/paragraph5_3.png and b/tests/fonts/diffs/paragraph5_3.png differ diff --git a/tests/fonts/diffs/paragraph5_nokern.png b/tests/fonts/diffs/paragraph5_nokern.png index 148a080..175abc2 100644 Binary files a/tests/fonts/diffs/paragraph5_nokern.png and b/tests/fonts/diffs/paragraph5_nokern.png differ diff --git a/tests/fonts/diffs/paragraph5_nokern_2.png b/tests/fonts/diffs/paragraph5_nokern_2.png index cddcab0..71ffc83 100644 Binary files a/tests/fonts/diffs/paragraph5_nokern_2.png and b/tests/fonts/diffs/paragraph5_nokern_2.png differ diff --git a/tests/fonts/diffs/paragraph5_nokern_3.png b/tests/fonts/diffs/paragraph5_nokern_3.png index 5c0580c..35ea556 100644 Binary files a/tests/fonts/diffs/paragraph5_nokern_3.png and b/tests/fonts/diffs/paragraph5_nokern_3.png differ diff --git a/tests/fonts/diffs/spans7.png b/tests/fonts/diffs/spans7.png index 127555f..53340f5 100644 Binary files a/tests/fonts/diffs/spans7.png and b/tests/fonts/diffs/spans7.png differ diff --git a/tests/fonts/diffs/strikethrough3.png b/tests/fonts/diffs/strikethrough3.png index ebbbcf1..fb8ebe3 100644 Binary files a/tests/fonts/diffs/strikethrough3.png and b/tests/fonts/diffs/strikethrough3.png differ diff --git a/tests/fonts/diffs/underline3.png b/tests/fonts/diffs/underline3.png index 9b4929d..aab00ef 100644 Binary files a/tests/fonts/diffs/underline3.png and b/tests/fonts/diffs/underline3.png differ diff --git a/tests/fonts/rendered/customlineheight.png b/tests/fonts/rendered/customlineheight.png index 03bc16a..62aeba9 100644 Binary files a/tests/fonts/rendered/customlineheight.png and b/tests/fonts/rendered/customlineheight.png differ diff --git a/tests/fonts/rendered/paragraph1.png b/tests/fonts/rendered/paragraph1.png index b32dc7d..f54e123 100644 Binary files a/tests/fonts/rendered/paragraph1.png and b/tests/fonts/rendered/paragraph1.png differ diff --git a/tests/fonts/rendered/paragraph1_nokern.png b/tests/fonts/rendered/paragraph1_nokern.png index f6150f2..c5507f7 100644 Binary files a/tests/fonts/rendered/paragraph1_nokern.png and b/tests/fonts/rendered/paragraph1_nokern.png differ diff --git a/tests/fonts/rendered/paragraph3_nokern.png b/tests/fonts/rendered/paragraph3_nokern.png index 161f1fd..69df16f 100644 Binary files a/tests/fonts/rendered/paragraph3_nokern.png and b/tests/fonts/rendered/paragraph3_nokern.png differ diff --git a/tests/fonts/rendered/paragraph3_nokern_2.png b/tests/fonts/rendered/paragraph3_nokern_2.png index 16f6cdc..a4276be 100644 Binary files a/tests/fonts/rendered/paragraph3_nokern_2.png and b/tests/fonts/rendered/paragraph3_nokern_2.png differ diff --git a/tests/fonts/rendered/paragraph3_nokern_3.png b/tests/fonts/rendered/paragraph3_nokern_3.png index 3a17f4f..5e68386 100644 Binary files a/tests/fonts/rendered/paragraph3_nokern_3.png and b/tests/fonts/rendered/paragraph3_nokern_3.png differ diff --git a/tests/fonts/rendered/paragraph4.png b/tests/fonts/rendered/paragraph4.png index 04079eb..35322f2 100644 Binary files a/tests/fonts/rendered/paragraph4.png and b/tests/fonts/rendered/paragraph4.png differ diff --git a/tests/fonts/rendered/paragraph4_2.png b/tests/fonts/rendered/paragraph4_2.png index 954b2bd..de45d23 100644 Binary files a/tests/fonts/rendered/paragraph4_2.png and b/tests/fonts/rendered/paragraph4_2.png differ diff --git a/tests/fonts/rendered/paragraph4_3.png b/tests/fonts/rendered/paragraph4_3.png index 2e8f592..e4d3b1f 100644 Binary files a/tests/fonts/rendered/paragraph4_3.png and b/tests/fonts/rendered/paragraph4_3.png differ diff --git a/tests/fonts/rendered/paragraph4_nokern.png b/tests/fonts/rendered/paragraph4_nokern.png index 29bdbbe..948cb58 100644 Binary files a/tests/fonts/rendered/paragraph4_nokern.png and b/tests/fonts/rendered/paragraph4_nokern.png differ diff --git a/tests/fonts/rendered/paragraph4_nokern_3.png b/tests/fonts/rendered/paragraph4_nokern_3.png index 7e142d2..307f9e2 100644 Binary files a/tests/fonts/rendered/paragraph4_nokern_3.png and b/tests/fonts/rendered/paragraph4_nokern_3.png differ diff --git a/tests/fonts/rendered/paragraph5.png b/tests/fonts/rendered/paragraph5.png index 8ad257e..3a3f5a0 100644 Binary files a/tests/fonts/rendered/paragraph5.png and b/tests/fonts/rendered/paragraph5.png differ diff --git a/tests/fonts/rendered/paragraph5_2.png b/tests/fonts/rendered/paragraph5_2.png index 480f7d5..f94a250 100644 Binary files a/tests/fonts/rendered/paragraph5_2.png and b/tests/fonts/rendered/paragraph5_2.png differ diff --git a/tests/fonts/rendered/paragraph5_3.png b/tests/fonts/rendered/paragraph5_3.png index 055229a..8cc3ed5 100644 Binary files a/tests/fonts/rendered/paragraph5_3.png and b/tests/fonts/rendered/paragraph5_3.png differ diff --git a/tests/fonts/rendered/paragraph5_nokern.png b/tests/fonts/rendered/paragraph5_nokern.png index 2c85d5d..810ae2d 100644 Binary files a/tests/fonts/rendered/paragraph5_nokern.png and b/tests/fonts/rendered/paragraph5_nokern.png differ diff --git a/tests/fonts/rendered/paragraph5_nokern_2.png b/tests/fonts/rendered/paragraph5_nokern_2.png index 98f980b..81d4c42 100644 Binary files a/tests/fonts/rendered/paragraph5_nokern_2.png and b/tests/fonts/rendered/paragraph5_nokern_2.png differ diff --git a/tests/fonts/rendered/paragraph5_nokern_3.png b/tests/fonts/rendered/paragraph5_nokern_3.png index 583423b..05a776b 100644 Binary files a/tests/fonts/rendered/paragraph5_nokern_3.png and b/tests/fonts/rendered/paragraph5_nokern_3.png differ diff --git a/tests/fonts/rendered/spans7.png b/tests/fonts/rendered/spans7.png index 33c74f5..f65f238 100644 Binary files a/tests/fonts/rendered/spans7.png and b/tests/fonts/rendered/spans7.png differ diff --git a/tests/fonts/rendered/strikethrough3.png b/tests/fonts/rendered/strikethrough3.png index b045282..dccaa29 100644 Binary files a/tests/fonts/rendered/strikethrough3.png and b/tests/fonts/rendered/strikethrough3.png differ diff --git a/tests/fonts/rendered/underline3.png b/tests/fonts/rendered/underline3.png index a76ade5..efee3c1 100644 Binary files a/tests/fonts/rendered/underline3.png and b/tests/fonts/rendered/underline3.png differ