computeBounds, no iterator

This commit is contained in:
Ryan Oldenburg 2021-05-07 19:28:49 -05:00
parent 85bd786dd0
commit e931bd0319
3 changed files with 37 additions and 23 deletions

View file

@ -341,13 +341,13 @@ proc fillText*(
) =
## Typesets and fills the text. Optional parameters:
## transform: translation or matrix to apply
## bounds: width determines wrapping and halign, height for valign
## bounds: width determines wrapping and hAlign, height for vAlign
## hAlign: horizontal alignment of the text
## vAlign: vertical alignment of the text
## textCase: text character case
## wrap: enable/disable text wrapping
## kerning: enable/disable kerning adjustments to letter spacing
for path in font.typeset(
let arrangement = font.typeset(
text,
bounds,
hAlign,
@ -355,8 +355,9 @@ proc fillText*(
textCase,
wrap,
kerning
).paths:
image.fillPath(path, color, transform)
)
for i in 0 ..< arrangement.runes.len:
image.fillPath(arrangement.getPath(i), color, transform)
proc fillText*(
mask: Mask,
@ -372,13 +373,13 @@ proc fillText*(
) =
## Typesets and fills the text. Optional parameters:
## transform: translation or matrix to apply
## bounds: width determines wrapping and halign, height for valign
## bounds: width determines wrapping and hAlign, height for vAlign
## hAlign: horizontal alignment of the text
## vAlign: vertical alignment of the text
## textCase: text character case
## wrap: enable/disable text wrapping
## kerning: enable/disable kerning adjustments to letter spacing
for path in font.typeset(
let arrangement = font.typeset(
text,
bounds,
hAlign,
@ -386,8 +387,9 @@ proc fillText*(
textCase,
wrap,
kerning
).paths:
mask.fillPath(path, transform)
)
for i in 0 ..< arrangement.runes.len:
mask.fillPath(arrangement.getPath(i), transform)
proc strokeText*(
image: Image,
@ -405,13 +407,13 @@ proc strokeText*(
) =
## Typesets and strokes the text. Optional parameters:
## transform: translation or matrix to apply
## bounds: width determines wrapping and halign, height for valign
## bounds: width determines wrapping and hAlign, height for vAlign
## hAlign: horizontal alignment of the text
## vAlign: vertical alignment of the text
## textCase: text character case
## wrap: enable/disable text wrapping
## kerning: enable/disable kerning adjustments to letter spacing
for path in font.typeset(
let arrangement = font.typeset(
text,
bounds,
hAlign,
@ -419,8 +421,9 @@ proc strokeText*(
textCase,
wrap,
kerning
).paths:
image.strokePath(path, color, transform, strokeWidth)
)
for i in 0 ..< arrangement.runes.len:
image.strokePath(arrangement.getPath(i), color, transform, strokeWidth)
proc strokeText*(
mask: Mask,
@ -437,13 +440,13 @@ proc strokeText*(
) =
## Typesets and strokes the text. Optional parameters:
## transform: translation or matrix to apply
## bounds: width determines wrapping and halign, height for valign
## bounds: width determines wrapping and hAlign, height for vAlign
## hAlign: horizontal alignment of the text
## vAlign: vertical alignment of the text
## textCase: text character case
## wrap: enable/disable text wrapping
## kerning: enable/disable kerning adjustments to letter spacing
for path in font.typeset(
let arrangement = font.typeset(
text,
bounds,
hAlign,
@ -451,5 +454,6 @@ proc strokeText*(
textCase,
wrap,
kerning
).paths:
mask.strokePath(path, transform, strokeWidth)
)
for i in 0 ..< arrangement.runes.len:
mask.strokePath(arrangement.getPath(i), transform, strokeWidth)

View file

@ -128,7 +128,7 @@ proc typeset*(
): Arrangement =
## Lays out the character glyphs and returns the arrangement.
## Optional parameters:
## bounds: width determines wrapping and halign, height for valign
## bounds: width determines wrapping and hAlign, height for vAlign
## hAlign: horizontal alignment of the text
## vAlign: vertical alignment of the text
## textCase: text character case
@ -209,7 +209,7 @@ proc typeset*(
result.selectionRects[i] = rect(at.x, at.y - initialY, advance, lineHeight)
at.x += advance
if bounds.x > 0 and hAlign != haLeft:
if hAlign != haLeft:
# Since horizontal alignment adjustments are different for each line,
# find the start and stop of each line of text.
var
@ -244,7 +244,7 @@ proc typeset*(
result.positions[i].x += xAdjustment
result.selectionRects[i].x += xAdjustment
if bounds.y > 0:
if vAlign != vaTop:
let
finalSelectionRect = result.selectionRects[^1]
furthestY = finalSelectionRect.y + finalSelectionRect.h
@ -271,10 +271,13 @@ proc getPath*(arrangement: Arrangement, index: int): Path =
scale(vec2(arrangement.font.scale))
)
iterator paths*(arrangement: Arrangement): Path =
## Iterates over the paths for the arrangement.
for i in 0 ..< arrangement.runes.len:
yield arrangement.getPath(i)
proc computeBounds*(font: Font, text: string): Vec2 =
let arrangement = font.typeset(text)
if arrangement.runes.len > 0:
for rect in arrangement.selectionRects:
result.x = max(result.x, rect.x + rect.w)
let finalRect = arrangement.selectionRects[^1]
result.y = finalRect.y + finalRect.h
proc parseOtf*(buf: string): Font =
result.typeface = Typeface()

View file

@ -8,6 +8,13 @@ proc doDiff(rendered: Image, name: string) =
echo &"{name} score: {diffScore}"
diffImage.writeFile(&"tests/fonts/diffs/{name}.png")
block:
var font = readFont("tests/fonts/Roboto-Regular_1.ttf")
font.size = 24
let bounds = font.computeBounds("Word")
doAssert bounds == vec2(56.05078125, 28)
block:
var font = readFont("tests/fonts/Roboto-Regular_1.ttf")
font.size = 64