selection rects, better initial y

This commit is contained in:
Ryan Oldenburg 2021-05-06 13:40:31 -05:00
parent 4a670e19df
commit 20eb1af118
5 changed files with 32 additions and 2 deletions

View file

@ -157,16 +157,25 @@ proc typeset*(
result += font.typeface.getAdvance(runes[i]) result += font.typeface.getAdvance(runes[i])
result *= font.scale result *= font.scale
var fontUnitInitialY = font.typeface.ascent + font.typeface.lineGap / 2
if lineHeight != font.defaultLineHeight:
fontUnitInitialY += (
(lineHeight / font.scale) -
(font.typeface.ascent - font.typeface.descent + font.typeface.lineGap)
) / 2
let initialY = round(fontUnitInitialY * font.scale)
var var
at: Vec2 at: Vec2
prevCanWrap: int prevCanWrap: int
at.y = round((font.typeface.ascent + font.typeface.lineGap / 2) * font.scale) at.y = initialY
at.y += (lineheight - font.defaultLineHeight) / 2
for i, rune in result.runes: for i, rune in result.runes:
if rune == LF: if rune == LF:
let advance = font.typeface.getAdvance(SP) * font.scale
result.positions[i] = at result.positions[i] = at
at.x = 0 at.x = 0
at.y += lineHeight at.y += lineHeight
result.selectionRects[i] = rect(at.x, at.y - initialY, advance, lineHeight)
prevCanWrap = 0 prevCanWrap = 0
else: else:
if rune.canWrap(): if rune.canWrap():
@ -185,6 +194,7 @@ proc typeset*(
at.x += advance(font, result.runes, j, kerning) at.x += advance(font, result.runes, j, kerning)
result.positions[i] = at result.positions[i] = at
result.selectionRects[i] = rect(at.x, at.y - initialY, advance, lineHeight)
at.x += advance at.x += advance
iterator paths*(arrangement: Arrangement): Path = iterator paths*(arrangement: Arrangement): Path =

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

View file

@ -533,3 +533,23 @@ Seventh line""",
) )
doDiff(image, "lines1") doDiff(image, "lines1")
block:
var font = readFont("tests/fonts/Roboto-Regular_1.ttf")
font.size = 18
font.lineHeight = 30
let image = newImage(200, 150)
image.fill(rgba(255, 255, 255, 255))
image.fillText(
font,
"""First line
Second line
Third line
Fourth line
Fifth line""",
rgba(0, 0, 0, 255),
bounds = image.wh
)
doDiff(image, "lines2")