@@ -57,14 +57,18 @@ function renderstring!(
57
57
58
58
bitmaps = Vector {Matrix{UInt8}} (undef, lastindex (str))
59
59
metrics = Vector {FontExtent{Int}} (undef, lastindex (str))
60
+ # ymin and ymax w.r.t the baseline
60
61
ymin = ymax = sumadvancex = 0
61
62
62
63
for (istr, char) = enumerate (str)
63
64
bitmap, metric_float = renderface (face, char, pixelsize)
64
65
metric = round .(Int, metric_float)
65
66
bitmaps[istr] = bitmap
66
67
metrics[istr] = metric
68
+
69
+ # scale of glyph (size of bitmap)
67
70
w, h = metric. scale
71
+ # offset between glyph origin and bitmap top left corner
68
72
bx, by = metric. horizontal_bearing
69
73
ymin = min (ymin, by - h)
70
74
ymax = max (ymax, by)
@@ -80,7 +84,7 @@ function renderstring!(
80
84
bitmapmax = typemax (eltype (bitmaps[1 ]))
81
85
82
86
imgh, imgw = size (img)
83
- if bcolor != nothing
87
+ if bcolor != = nothing
84
88
img[
85
89
clamp (py- ymax+ 1 , 1 , imgh) : clamp (py- ymin- 1 , 1 , imgh),
86
90
clamp (px- 1 , 1 , imgw) : clamp (px+ sumadvancex- 1 , 1 , imgw)
@@ -91,27 +95,31 @@ function renderstring!(
91
95
for (istr, char) = enumerate (str)
92
96
w, h = metrics[istr]. scale
93
97
bx, by = metrics[istr]. horizontal_bearing
98
+
94
99
if istr == 1
95
100
prev_char = char
96
101
else
97
102
kx, ky = map (x-> round (Int, x), kerning (prev_char, char, face))
98
103
px += kx
99
104
end
100
- cliprowlo, cliprowhi = max (0 , by- py), max (0 , h+ py- by- imgh)
101
- clipcollo, clipcolhi = max (0 , bx- px), max (0 , w+ px- bx- imgw)
102
- if bcolor == nothing
105
+
106
+ # trim parts of glyph images that are outside the destination
107
+ cliprowlo, cliprowhi = max (0 , - (py- by)), max (0 , py - by + h - imgh)
108
+ clipcollo, clipcolhi = max (0 , - bx- px), max (0 , px + bx + w - imgw)
109
+
110
+ if bcolor === nothing
103
111
for row = 1 + cliprowlo : h- cliprowhi, col = 1 + clipcollo : w- clipcolhi
104
112
bitmaps[istr][col,row]== 0 && continue
105
113
c1 = bitmaps[istr][col,row] / bitmapmax * fcolor
106
- img[row+ py- by, col+ px- bx] = T <: Integer ? round (T, c1) : T (c1)
114
+ img[row+ py- by, col+ px+ bx] = T <: Integer ? round (T, c1) : T (c1)
107
115
end
108
116
else
109
117
for row = 1 + cliprowlo : h- cliprowhi, col = 1 + clipcollo : w- clipcolhi
110
118
bitmaps[istr][col, row] == 0 && continue
111
119
w1 = bitmaps[istr][col, row] / bitmapmax
112
120
c1 = w1 * fcolor
113
121
c0 = (1.0 - w1) * bcolor
114
- img[row + py - by, col + px - bx] = T <: Integer ? round (T, c1 + c0) : T (c1 + c0)
122
+ img[row + py - by, col + px + bx] = T <: Integer ? round (T, c1 + c0) : T (c1 + c0)
115
123
end
116
124
end
117
125
px += metrics[istr]. advance[1 ]
0 commit comments