From d491fe26b9c723e2763befbe2710df68e988f51d Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Tue, 18 May 2021 00:45:41 -0500 Subject: [PATCH] move font rasterize procs to fonts.nim, add join and cap param --- src/pixie.nim | 77 --------------------------------------- src/pixie/fonts.nim | 89 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 78 deletions(-) diff --git a/src/pixie.nim b/src/pixie.nim index 9b692db..baca69f 100644 --- a/src/pixie.nim +++ b/src/pixie.nim @@ -390,80 +390,3 @@ proc strokePolygon*( var path: Path path.polygon(pos, size, sides) mask.strokePath(path, transform, strokeWidth) - -proc fillText*( - target: Image | Mask, - arrangement: Arrangement, - transform: Vec2 | Mat3 = vec2(0, 0) -) = - ## Fills the text arrangement. - for spanIndex, (start, stop) in arrangement.spans: - let font = arrangement.fonts[spanIndex] - for runeIndex in start .. stop: - var path = font.typeface.getGlyphPath(arrangement.runes[runeIndex]) - path.transform( - translate(arrangement.positions[runeIndex]) * - scale(vec2(font.scale)) - ) - when type(target) is Image: - target.fillPath(path, font.paint, transform) - else: # target is Mask - target.fillPath(path, transform) - -proc fillText*( - target: Image | Mask, - font: Font, - text: string, - transform: Vec2 | Mat3 = vec2(0, 0), - bounds = vec2(0, 0), - hAlign = haLeft, - vAlign = vaTop -) {.inline.} = - ## Typesets and fills the text. Optional parameters: - ## transform: translation or matrix to apply - ## bounds: width determines wrapping and hAlign, height for vAlign - ## hAlign: horizontal alignment of the text - ## vAlign: vertical alignment of the text - fillText(target, font.typeset(text, bounds, hAlign, vAlign), transform) - -proc strokeText*( - target: Image | Mask, - arrangement: Arrangement, - transform: Vec2 | Mat3 = vec2(0, 0), - strokeWidth = 1.0 -) = - ## Strokes the text arrangement. - for spanIndex, (start, stop) in arrangement.spans: - let font = arrangement.fonts[spanIndex] - for runeIndex in start .. stop: - var path = font.typeface.getGlyphPath(arrangement.runes[runeIndex]) - path.transform( - translate(arrangement.positions[runeIndex]) * - scale(vec2(font.scale)) - ) - when type(target) is Image: - target.strokePath(path, font.paint, transform, strokeWidth) - else: # target is Mask - target.strokePath(path, transform, strokeWidth) - -proc strokeText*( - target: Image | Mask, - font: Font, - text: string, - transform: Vec2 | Mat3 = vec2(0, 0), - strokeWidth = 1.0, - bounds = vec2(0, 0), - hAlign = haLeft, - vAlign = vaTop -) {.inline.} = - ## Typesets and strokes the text. Optional parameters: - ## transform: translation or matrix to apply - ## bounds: width determines wrapping and hAlign, height for vAlign - ## hAlign: horizontal alignment of the text - ## vAlign: vertical alignment of the text - strokeText( - target, - font.typeset(text, bounds, hAlign, vAlign), - transform, - strokeWidth - ) diff --git a/src/pixie/fonts.nim b/src/pixie/fonts.nim index 64747ed..02a2254 100644 --- a/src/pixie/fonts.nim +++ b/src/pixie/fonts.nim @@ -1,5 +1,5 @@ import bumpy, chroma, pixie/fontformats/opentype, pixie/fontformats/svgfont, - pixie/paints, pixie/paths, unicode, vmath + pixie/images, pixie/masks, pixie/paints, pixie/paths, unicode, vmath const AutoLineHeight* = -1.float32 ## Use default line height for the font size @@ -400,3 +400,90 @@ proc parseSvgFont*(buf: string): Font = result.size = 12 result.lineHeight = AutoLineHeight result.paint = Paint(kind: pkSolid, color: rgbx(0, 0, 0, 255)) + +proc fillText*( + target: Image | Mask, + arrangement: Arrangement, + transform: Vec2 | Mat3 = vec2(0, 0) +) = + ## Fills the text arrangement. + for spanIndex, (start, stop) in arrangement.spans: + let font = arrangement.fonts[spanIndex] + for runeIndex in start .. stop: + var path = font.typeface.getGlyphPath(arrangement.runes[runeIndex]) + path.transform( + translate(arrangement.positions[runeIndex]) * + scale(vec2(font.scale)) + ) + when type(target) is Image: + target.fillPath(path, font.paint, transform) + else: # target is Mask + target.fillPath(path, transform) + +proc fillText*( + target: Image | Mask, + font: Font, + text: string, + transform: Vec2 | Mat3 = vec2(0, 0), + bounds = vec2(0, 0), + hAlign = haLeft, + vAlign = vaTop +) {.inline.} = + ## Typesets and fills the text. Optional parameters: + ## transform: translation or matrix to apply + ## bounds: width determines wrapping and hAlign, height for vAlign + ## hAlign: horizontal alignment of the text + ## vAlign: vertical alignment of the text + fillText(target, font.typeset(text, bounds, hAlign, vAlign), transform) + +proc strokeText*( + target: Image | Mask, + arrangement: Arrangement, + transform: Vec2 | Mat3 = vec2(0, 0), + strokeWidth = 1.0, + lineCap = lcButt, + lineJoin = ljMiter +) = + ## Strokes the text arrangement. + for spanIndex, (start, stop) in arrangement.spans: + let font = arrangement.fonts[spanIndex] + for runeIndex in start .. stop: + var path = font.typeface.getGlyphPath(arrangement.runes[runeIndex]) + path.transform( + translate(arrangement.positions[runeIndex]) * + scale(vec2(font.scale)) + ) + when type(target) is Image: + target.strokePath( + path, font.paint, transform, strokeWidth, lineCap, lineJoin + ) + else: # target is Mask + target.strokePath(path, transform, strokeWidth, lineCap, lineJoin) + +proc strokeText*( + target: Image | Mask, + font: Font, + text: string, + transform: Vec2 | Mat3 = vec2(0, 0), + strokeWidth = 1.0, + bounds = vec2(0, 0), + hAlign = haLeft, + vAlign = vaTop, + lineCap = lcButt, + lineJoin = ljMiter +) {.inline.} = + ## Typesets and strokes the text. Optional parameters: + ## transform: translation or matrix to apply + ## bounds: width determines wrapping and hAlign, height for vAlign + ## hAlign: horizontal alignment of the text + ## vAlign: vertical alignment of the text + ## lineCap: stroke line cap shape + ## lineJoin: stroke line join shape + strokeText( + target, + font.typeset(text, bounds, hAlign, vAlign), + transform, + strokeWidth, + lineCap, + lineJoin + )