computeBounds, no iterator
This commit is contained in:
parent
85bd786dd0
commit
e931bd0319
3 changed files with 37 additions and 23 deletions
|
@ -341,13 +341,13 @@ proc fillText*(
|
||||||
) =
|
) =
|
||||||
## Typesets and fills the text. Optional parameters:
|
## Typesets and fills the text. Optional parameters:
|
||||||
## transform: translation or matrix to apply
|
## 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
|
## hAlign: horizontal alignment of the text
|
||||||
## vAlign: vertical alignment of the text
|
## vAlign: vertical alignment of the text
|
||||||
## textCase: text character case
|
## textCase: text character case
|
||||||
## wrap: enable/disable text wrapping
|
## wrap: enable/disable text wrapping
|
||||||
## kerning: enable/disable kerning adjustments to letter spacing
|
## kerning: enable/disable kerning adjustments to letter spacing
|
||||||
for path in font.typeset(
|
let arrangement = font.typeset(
|
||||||
text,
|
text,
|
||||||
bounds,
|
bounds,
|
||||||
hAlign,
|
hAlign,
|
||||||
|
@ -355,8 +355,9 @@ proc fillText*(
|
||||||
textCase,
|
textCase,
|
||||||
wrap,
|
wrap,
|
||||||
kerning
|
kerning
|
||||||
).paths:
|
)
|
||||||
image.fillPath(path, color, transform)
|
for i in 0 ..< arrangement.runes.len:
|
||||||
|
image.fillPath(arrangement.getPath(i), color, transform)
|
||||||
|
|
||||||
proc fillText*(
|
proc fillText*(
|
||||||
mask: Mask,
|
mask: Mask,
|
||||||
|
@ -372,13 +373,13 @@ proc fillText*(
|
||||||
) =
|
) =
|
||||||
## Typesets and fills the text. Optional parameters:
|
## Typesets and fills the text. Optional parameters:
|
||||||
## transform: translation or matrix to apply
|
## 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
|
## hAlign: horizontal alignment of the text
|
||||||
## vAlign: vertical alignment of the text
|
## vAlign: vertical alignment of the text
|
||||||
## textCase: text character case
|
## textCase: text character case
|
||||||
## wrap: enable/disable text wrapping
|
## wrap: enable/disable text wrapping
|
||||||
## kerning: enable/disable kerning adjustments to letter spacing
|
## kerning: enable/disable kerning adjustments to letter spacing
|
||||||
for path in font.typeset(
|
let arrangement = font.typeset(
|
||||||
text,
|
text,
|
||||||
bounds,
|
bounds,
|
||||||
hAlign,
|
hAlign,
|
||||||
|
@ -386,8 +387,9 @@ proc fillText*(
|
||||||
textCase,
|
textCase,
|
||||||
wrap,
|
wrap,
|
||||||
kerning
|
kerning
|
||||||
).paths:
|
)
|
||||||
mask.fillPath(path, transform)
|
for i in 0 ..< arrangement.runes.len:
|
||||||
|
mask.fillPath(arrangement.getPath(i), transform)
|
||||||
|
|
||||||
proc strokeText*(
|
proc strokeText*(
|
||||||
image: Image,
|
image: Image,
|
||||||
|
@ -405,13 +407,13 @@ proc strokeText*(
|
||||||
) =
|
) =
|
||||||
## Typesets and strokes the text. Optional parameters:
|
## Typesets and strokes the text. Optional parameters:
|
||||||
## transform: translation or matrix to apply
|
## 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
|
## hAlign: horizontal alignment of the text
|
||||||
## vAlign: vertical alignment of the text
|
## vAlign: vertical alignment of the text
|
||||||
## textCase: text character case
|
## textCase: text character case
|
||||||
## wrap: enable/disable text wrapping
|
## wrap: enable/disable text wrapping
|
||||||
## kerning: enable/disable kerning adjustments to letter spacing
|
## kerning: enable/disable kerning adjustments to letter spacing
|
||||||
for path in font.typeset(
|
let arrangement = font.typeset(
|
||||||
text,
|
text,
|
||||||
bounds,
|
bounds,
|
||||||
hAlign,
|
hAlign,
|
||||||
|
@ -419,8 +421,9 @@ proc strokeText*(
|
||||||
textCase,
|
textCase,
|
||||||
wrap,
|
wrap,
|
||||||
kerning
|
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*(
|
proc strokeText*(
|
||||||
mask: Mask,
|
mask: Mask,
|
||||||
|
@ -437,13 +440,13 @@ proc strokeText*(
|
||||||
) =
|
) =
|
||||||
## Typesets and strokes the text. Optional parameters:
|
## Typesets and strokes the text. Optional parameters:
|
||||||
## transform: translation or matrix to apply
|
## 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
|
## hAlign: horizontal alignment of the text
|
||||||
## vAlign: vertical alignment of the text
|
## vAlign: vertical alignment of the text
|
||||||
## textCase: text character case
|
## textCase: text character case
|
||||||
## wrap: enable/disable text wrapping
|
## wrap: enable/disable text wrapping
|
||||||
## kerning: enable/disable kerning adjustments to letter spacing
|
## kerning: enable/disable kerning adjustments to letter spacing
|
||||||
for path in font.typeset(
|
let arrangement = font.typeset(
|
||||||
text,
|
text,
|
||||||
bounds,
|
bounds,
|
||||||
hAlign,
|
hAlign,
|
||||||
|
@ -451,5 +454,6 @@ proc strokeText*(
|
||||||
textCase,
|
textCase,
|
||||||
wrap,
|
wrap,
|
||||||
kerning
|
kerning
|
||||||
).paths:
|
)
|
||||||
mask.strokePath(path, transform, strokeWidth)
|
for i in 0 ..< arrangement.runes.len:
|
||||||
|
mask.strokePath(arrangement.getPath(i), transform, strokeWidth)
|
||||||
|
|
|
@ -128,7 +128,7 @@ proc typeset*(
|
||||||
): Arrangement =
|
): Arrangement =
|
||||||
## Lays out the character glyphs and returns the arrangement.
|
## Lays out the character glyphs and returns the arrangement.
|
||||||
## Optional parameters:
|
## 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
|
## hAlign: horizontal alignment of the text
|
||||||
## vAlign: vertical alignment of the text
|
## vAlign: vertical alignment of the text
|
||||||
## textCase: text character case
|
## textCase: text character case
|
||||||
|
@ -209,7 +209,7 @@ proc typeset*(
|
||||||
result.selectionRects[i] = rect(at.x, at.y - initialY, advance, lineHeight)
|
result.selectionRects[i] = rect(at.x, at.y - initialY, advance, lineHeight)
|
||||||
at.x += advance
|
at.x += advance
|
||||||
|
|
||||||
if bounds.x > 0 and hAlign != haLeft:
|
if hAlign != haLeft:
|
||||||
# Since horizontal alignment adjustments are different for each line,
|
# Since horizontal alignment adjustments are different for each line,
|
||||||
# find the start and stop of each line of text.
|
# find the start and stop of each line of text.
|
||||||
var
|
var
|
||||||
|
@ -244,7 +244,7 @@ proc typeset*(
|
||||||
result.positions[i].x += xAdjustment
|
result.positions[i].x += xAdjustment
|
||||||
result.selectionRects[i].x += xAdjustment
|
result.selectionRects[i].x += xAdjustment
|
||||||
|
|
||||||
if bounds.y > 0:
|
if vAlign != vaTop:
|
||||||
let
|
let
|
||||||
finalSelectionRect = result.selectionRects[^1]
|
finalSelectionRect = result.selectionRects[^1]
|
||||||
furthestY = finalSelectionRect.y + finalSelectionRect.h
|
furthestY = finalSelectionRect.y + finalSelectionRect.h
|
||||||
|
@ -271,10 +271,13 @@ proc getPath*(arrangement: Arrangement, index: int): Path =
|
||||||
scale(vec2(arrangement.font.scale))
|
scale(vec2(arrangement.font.scale))
|
||||||
)
|
)
|
||||||
|
|
||||||
iterator paths*(arrangement: Arrangement): Path =
|
proc computeBounds*(font: Font, text: string): Vec2 =
|
||||||
## Iterates over the paths for the arrangement.
|
let arrangement = font.typeset(text)
|
||||||
for i in 0 ..< arrangement.runes.len:
|
if arrangement.runes.len > 0:
|
||||||
yield arrangement.getPath(i)
|
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 =
|
proc parseOtf*(buf: string): Font =
|
||||||
result.typeface = Typeface()
|
result.typeface = Typeface()
|
||||||
|
|
|
@ -8,6 +8,13 @@ proc doDiff(rendered: Image, name: string) =
|
||||||
echo &"{name} score: {diffScore}"
|
echo &"{name} score: {diffScore}"
|
||||||
diffImage.writeFile(&"tests/fonts/diffs/{name}.png")
|
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:
|
block:
|
||||||
var font = readFont("tests/fonts/Roboto-Regular_1.ttf")
|
var font = readFont("tests/fonts/Roboto-Regular_1.ttf")
|
||||||
font.size = 64
|
font.size = 64
|
||||||
|
|
Loading…
Reference in a new issue