names possibility

This commit is contained in:
Ryan Oldenburg 2021-05-05 18:22:56 -05:00
parent 05889d51e4
commit e5a463aaa3
3 changed files with 23 additions and 38 deletions

View file

@ -339,7 +339,7 @@ proc fillText*(
wrap = true, wrap = true,
kerning = true kerning = true
) = ) =
for path in font.typesetPaths( for path in font.typeset(
text, text,
bounds, bounds,
hAlign, hAlign,
@ -347,7 +347,7 @@ proc fillText*(
textCase, textCase,
wrap, wrap,
kerning kerning
): ).paths:
image.fillPath(path, color, transform) image.fillPath(path, color, transform)
proc fillText*( proc fillText*(
@ -362,7 +362,7 @@ proc fillText*(
wrap = true, wrap = true,
kerning = true kerning = true
) = ) =
for path in font.typesetPaths( for path in font.typeset(
text, text,
bounds, bounds,
hAlign, hAlign,
@ -370,7 +370,7 @@ proc fillText*(
textCase, textCase,
wrap, wrap,
kerning kerning
): ).paths:
mask.fillPath(path, transform) mask.fillPath(path, transform)
proc strokeText*( proc strokeText*(
@ -387,7 +387,7 @@ proc strokeText*(
wrap = true, wrap = true,
kerning = true kerning = true
) = ) =
for path in font.typesetPaths( for path in font.typeset(
text, text,
bounds, bounds,
hAlign, hAlign,
@ -395,7 +395,7 @@ proc strokeText*(
textCase, textCase,
wrap, wrap,
kerning kerning
): ).paths:
image.strokePath(path, color, transform, strokeWidth) image.strokePath(path, color, transform, strokeWidth)
proc strokeText*( proc strokeText*(
@ -411,7 +411,7 @@ proc strokeText*(
wrap = true, wrap = true,
kerning = true kerning = true
) = ) =
for path in font.typesetPaths( for path in font.typeset(
text, text,
bounds, bounds,
hAlign, hAlign,
@ -419,5 +419,5 @@ proc strokeText*(
textCase, textCase,
wrap, wrap,
kerning kerning
): ).paths:
mask.strokePath(path, transform, strokeWidth) mask.strokePath(path, transform, strokeWidth)

View file

@ -1,4 +1,4 @@
import pixie/fontformats/opentype, pixie/fontformats/svgfont, pixie/paths, import bumpy, pixie/fontformats/opentype, pixie/fontformats/svgfont, pixie/paths,
unicode, vmath unicode, vmath
const AutoLineHeight* = -1.float32 ## Use default line height for the font size const AutoLineHeight* = -1.float32 ## Use default line height for the font size
@ -13,9 +13,11 @@ type
size*: float32 ## Font size in pixels. size*: float32 ## Font size in pixels.
lineHeight*: float32 ## The line height in pixels or AutoLineHeight for the font's default line height. lineHeight*: float32 ## The line height in pixels or AutoLineHeight for the font's default line height.
Typesetting* = ref object Arrangement* = ref object
font*: Font
runes*: seq[Rune] runes*: seq[Rune]
positions*: seq[Vec2] positions*: seq[Vec2]
selectionRects*: seq[Rect]
HAlignMode* = enum HAlignMode* = enum
haLeft haLeft
@ -116,8 +118,9 @@ proc typeset*(
textCase = tcNormal, textCase = tcNormal,
wrap = true, wrap = true,
kerning = true kerning = true
): Typesetting = ): Arrangement =
result = Typesetting() result = Arrangement()
result.font = font
result.runes = toRunes(text) result.runes = toRunes(text)
result.runes.convertTextCase(textCase) result.runes.convertTextCase(textCase)
result.positions.setLen(result.runes.len) result.positions.setLen(result.runes.len)
@ -128,7 +131,7 @@ proc typeset*(
else: else:
font.defaultLineHeight font.defaultLineHeight
proc glyphAdvance( proc advance(
font: Font, runes: seq[Rune], i: int, kerning: bool font: Font, runes: seq[Rune], i: int, kerning: bool
): float32 {.inline.} = ): float32 {.inline.} =
if kerning and i + 1 < runes.len: if kerning and i + 1 < runes.len:
@ -145,7 +148,7 @@ proc typeset*(
if rune.canWrap(): if rune.canWrap():
prevCanWrap = i prevCanWrap = i
let advance = glyphAdvance(font, result.runes, i, kerning) let advance = advance(font, result.runes, i, kerning)
if rune != Rune(32) and bounds.x > 0 and at.x + advance > bounds.x: if rune != Rune(32) and bounds.x > 0 and at.x + advance > bounds.x:
# Wrap to new line # Wrap to new line
at.x = 0 at.x = 0
@ -155,34 +158,16 @@ proc typeset*(
if prevCanWrap > 0 and prevCanWrap != i: if prevCanWrap > 0 and prevCanWrap != i:
for j in prevCanWrap + 1 ..< i: for j in prevCanWrap + 1 ..< i:
result.positions[j] = at result.positions[j] = at
at.x += glyphAdvance(font, result.runes, j, kerning) at.x += advance(font, result.runes, j, kerning)
result.positions[i] = at result.positions[i] = at
at.x += advance at.x += advance
iterator typesetPaths*( iterator paths*(arrangement: Arrangement): Path =
font: Font, for i in 0 ..< arrangement.runes.len:
text: string, var path = arrangement.font.typeface.getGlyphPath(arrangement.runes[i])
bounds = vec2(0, 0),
hAlign = haLeft,
vAlign = vaTop,
textCase = tcNormal,
wrap = true,
kerning = true
): Path =
let typesetText = font.typeset(
text,
bounds,
hAlign,
vAlign,
textCase,
wrap,
kerning
)
for i in 0 ..< typesetText.runes.len:
var path = font.typeface.getGlyphPath(typesetText.runes[i])
path.transform( path.transform(
translate(typesetText.positions[i]) * scale(vec2(font.scale)) translate(arrangement.positions[i]) * scale(vec2(arrangement.font.scale))
) )
yield path yield path

View file

@ -2,7 +2,7 @@ import benchy, pixie
const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in quam in nulla bibendum luctus. Integer dui lectus, ultricies commodo enim quis, laoreet lacinia erat. Vivamus ultrices maximus risus, non aliquam quam sagittis quis. Ut nec diam vitae tortor interdum ullamcorper in aliquet velit. Ut sed lobortis mi. Nulla venenatis lectus varius justo lacinia, quis sollicitudin nunc ultrices. Donec a suscipit arcu, id egestas neque. Nullam commodo pharetra est. Nullam gravida nibh eget quam venenatis lacinia. Vestibulum et libero arcu. Sed dignissim enim eros. Nullam eleifend luctus erat sed luctus. Nunc tincidunt, mi nec tincidunt tristique, ex nulla lobortis sem, sit amet finibus purus justo non massa." const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in quam in nulla bibendum luctus. Integer dui lectus, ultricies commodo enim quis, laoreet lacinia erat. Vivamus ultrices maximus risus, non aliquam quam sagittis quis. Ut nec diam vitae tortor interdum ullamcorper in aliquet velit. Ut sed lobortis mi. Nulla venenatis lectus varius justo lacinia, quis sollicitudin nunc ultrices. Donec a suscipit arcu, id egestas neque. Nullam commodo pharetra est. Nullam gravida nibh eget quam venenatis lacinia. Vestibulum et libero arcu. Sed dignissim enim eros. Nullam eleifend luctus erat sed luctus. Nunc tincidunt, mi nec tincidunt tristique, ex nulla lobortis sem, sit amet finibus purus justo non massa."
var font = readFont("tests/fonts/Roboto-Regular.ttf") var font = readFont("tests/fonts/Roboto-Regular_1.ttf")
font.size = 16 font.size = 16
timeIt "typeset": timeIt "typeset":