fix emojiMask not working correctly.

its index was being reset for each wrapped line thus not corresponding to the actual position in the full paragraph content anymore.

basically if the emojiMask was [false, false, false, true, false] for "abc🔛d" but that text was split in two lines like:
"abc
🔛d"
then the emojiMask would be read at 0, 1, 2, then 0, 1.

now that is fixed.
This commit is contained in:
fiatjaf
2023-12-28 06:48:13 -03:00
parent a500e7bb79
commit e17b3e2b45
2 changed files with 20 additions and 6 deletions

View File

@@ -518,10 +518,11 @@ func drawShapedRunAt(
clr color.Color, clr color.Color,
out shaping.Output, out shaping.Output,
emojiMask []bool, emojiMask []bool,
maskBaseIndex int,
img draw.Image, img draw.Image,
startX, startX,
startY int, startY int,
) int { ) (charsWritten int, endingX int) {
scale := float32(fontSize) / float32(out.Face.Upem()) scale := float32(fontSize) / float32(out.Face.Upem())
b := img.Bounds() b := img.Bounds()
@@ -536,7 +537,7 @@ func drawShapedRunAt(
face := out.Face face := out.Face
currentScale := scale currentScale := scale
if emojiMask[i] { if emojiMask[maskBaseIndex+i] {
face = emojiFace face = emojiFace
currentScale = float32(fontSize) / float32(face.Upem()) currentScale = float32(fontSize) / float32(face.Upem())
} }
@@ -551,10 +552,12 @@ func drawShapedRunAt(
panic("format not supported for glyph") panic("format not supported for glyph")
} }
charsWritten++
x += fixed266ToFloat(g.XAdvance) x += fixed266ToFloat(g.XAdvance)
} }
f.Draw() f.Draw()
return int(math.Ceil(float64(x)))
return charsWritten, int(math.Ceil(float64(x)))
} }
// this draws a font glyph (i.e. a letter) according to instructions and scale and whatever // this draws a font glyph (i.e. a letter) according to instructions and scale and whatever

View File

@@ -173,7 +173,7 @@ func drawText(paragraphs []string, width, height int) image.Image {
color := color.RGBA{R: 255, G: 230, B: 238, A: 255} color := color.RGBA{R: 255, G: 230, B: 238, A: 255}
img := image.NewNRGBA(image.Rect(0, 0, width, height)) img := image.NewNRGBA(image.Rect(0, 0, width, height))
i := 1 lineNumber := 1
for _, paragraph := range paragraphs { for _, paragraph := range paragraphs {
rawText := []rune(paragraph) rawText := []rune(paragraph)
@@ -183,10 +183,21 @@ func drawText(paragraphs []string, width, height int) image.Image {
it := shaping.NewSliceIterator([]shaping.Output{shapedRunes}) it := shaping.NewSliceIterator([]shaping.Output{shapedRunes})
lines, _ := wrapper.WrapParagraph(shaping.WrapConfig{}, width, rawText, it) lines, _ := wrapper.WrapParagraph(shaping.WrapConfig{}, width, rawText, it)
totalCharsWritten := 0
for _, line := range lines { for _, line := range lines {
for _, out := range line { for _, out := range line {
drawShapedRunAt(FONT_SIZE, color, out, emojiMask, img, 0, FONT_SIZE*i*12/10) charsWritten, _ := drawShapedRunAt(
i++ FONT_SIZE,
color,
out,
emojiMask,
totalCharsWritten,
img,
0,
FONT_SIZE*lineNumber*12/10,
)
totalCharsWritten += charsWritten
lineNumber++
} }
} }
} }