Merge pull request #191 from guzba/master

move font rasterize procs to fonts.nim, add join and cap param
This commit is contained in:
treeform 2021-05-18 09:31:12 -07:00 committed by GitHub
commit e672b83a8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 78 deletions

View file

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

View file

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