Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/luaotfload-harf-var-ttf.lua

raw
1 -----------------------------------------------------------------------
2 -- FILE: luaotfload-harf-var-ttf.lua
3 -- DESCRIPTION: part of luaotfload / HarfBuzz / Parse and apply gvar tables
4 -----------------------------------------------------------------------
5 do
6 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
7 name = "luaotfload-harf-var-ttf",
8 version = "3.29", --TAGVERSION
9 date = "2024-12-03", --TAGDATE
10 description = "luaotfload submodule / gvar table processing",
11 license = "GPL v2.0",
12 author = "Marcel Krüger",
13 copyright = "Luaotfload Development Team",
14 }
15 end
16
17 local hb = assert(luaotfload.harfbuzz)
18 local gvar_tag = hb.Tag.new'gvar'
19 local glyf_tag = hb.Tag.new'glyf'
20 local loca_tag = hb.Tag.new'loca'
21
22 local function read_tuple(data, offset, axes_count)
23 local tuple = lua.newtable(axes_count, 0)
24 for i=1, axes_count do
25 tuple[i] = sio.readinteger2(data, offset + 2*i-2)
26 end
27 return tuple, offset + 2*axes_count
28 end
29
30 local function read_gvar(data)
31 if 1 ~= sio.readcardinal2(data, 1) then
32 return nil, 'Unknown gvar version'
33 end
34 local axes_count = sio.readcardinal2(data, 5)
35 local shared_count = sio.readcardinal2(data, 7)
36 local shared_offset = sio.readcardinal4(data, 9) + 1
37 local glyph_count = sio.readcardinal2(data, 13)
38 local flags = sio.readcardinal2(data, 15)
39 local variation_offset = sio.readcardinal4(data, 17) + 1
40 local variation_offsets = sio.readcardinaltable(data, 21, glyph_count+1, flags & 1 == 1 and 4 or 2)
41 if flags & 1 == 1 then
42 for i = 1, glyph_count+1 do
43 variation_offsets[i] = variation_offset + variation_offsets[i]
44 end
45 else
46 for i = 1, glyph_count+1 do
47 variation_offsets[i] = variation_offset + 2*variation_offsets[i]
48 end
49 end
50
51 local shared = lua.newtable(shared_count, 0)
52 for i=1, shared_count do
53 shared[i], shared_offset = read_tuple(data, shared_offset, axes_count)
54 end
55
56 local glyph_variation = lua.newtable(glyph_count, 0)
57 for i = 1, glyph_count do
58 local offset = variation_offsets[i]
59 if variation_offsets[i+1] ~= offset then
60 local tuple_variations = sio.readcardinal2(data, offset)
61 local flags = tuple_variations >> 12
62 tuple_variations = tuple_variations & 0xFFF
63 local data_offset = offset + sio.readcardinal2(data, offset + 2)
64
65 local headers = lua.newtable(tuple_variations, 0)
66 offset = offset + 4
67 for j=1, tuple_variations do
68 local data_size = sio.readcardinal2(data, offset)
69 local tuple_index = sio.readcardinal2(data, offset + 2)
70 local peak, start_inter, end_inter
71 offset = offset + 4
72 if tuple_index & 0x8000 == 0x8000 then
73 peak, offset = read_tuple(data, offset, axes_count)
74 else
75 peak = shared[(tuple_index & 0xFFF) + 1]
76 end
77 if tuple_index & 0x4000 == 0x4000 then
78 start_inter, offset = read_tuple(data, offset, axes_count)
79 end_inter, offset = read_tuple(data, offset, axes_count)
80 end
81 headers[j] = {
82 size = data_size,
83 peak = peak,
84 start_inter = start_inter,
85 end_inter = end_inter,
86 private_points = tuple_index & 0x2000 == 0x2000 or nil,
87 }
88 end
89 glyph_variation[i] = {
90 offset = data_offset,
91 shared_points = flags & 0x8 == 0x8 or nil,
92 headers = headers,
93 }
94 end
95 end
96 return glyph_variation
97 end
98
99 local function read_loca(face)
100 local data = face:get_table(loca_tag):get_data()
101 local count = face:get_glyph_count() + 1
102 if #data == count * 2 then
103 local result = sio.readcardinaltable(data, 1, count, 2)
104 for i=1, count do
105 result[i] = 2 * result[i]
106 end
107 return result
108 elseif #data == count * 4 then
109 return sio.readcardinaltable(data, 1, count, 4)
110 else
111 return nil, 'Invalid loca format'
112 end
113 end
114
115 local function parse_glyf(loca, glyf, gid)
116 local offset = loca[gid + 1] + 1
117 if loca[gid + 2] + 1 == offset then return end
118 local num_contours = sio.readinteger2(glyf, offset)
119 -- local xmin = sio.readinteger2(glyf, offset + 2)
120 -- local ymin = sio.readinteger2(glyf, offset + 4)
121 -- local xmax = sio.readinteger2(glyf, offset + 6)
122 -- local ymax = sio.readinteger2(glyf, offset + 8)
123 if num_contours < 0 then
124 -- composite
125 local xmin = sio.readinteger2(glyf, offset + 2)
126 local ymin = sio.readinteger2(glyf, offset + 4)
127 local xmax = sio.readinteger2(glyf, offset + 6)
128 local ymax = sio.readinteger2(glyf, offset + 8)
129 local components = { -- FIXME: These are likely incorrect
130 xmin = xmin,
131 ymin = ymin,
132 xmax = xmax,
133 ymax = ymax,
134 }
135 local flags
136 offset = offset + 10
137 repeat
138 local component = {}
139 flags = sio.readcardinal2(glyf, offset)
140 component.glyph = sio.readcardinal2(glyf, offset + 2)
141 local payload_length
142 if flags & 0x2 == 0x2 then
143 if flags & 0x1 == 0x1 then
144 component.x = sio.readinteger2(glyf, offset + 4)
145 component.y = sio.readinteger2(glyf, offset + 6)
146 offset = offset + 8
147 else
148 component.x = sio.readinteger1(glyf, offset + 4)
149 component.y = sio.readinteger1(glyf, offset + 5)
150 offset = offset + 6
151 end
152 payload_length = 0
153 else
154 offset = offset + 4
155 if flags & 0x1 == 0x1 then
156 payload_length = 4
157 else
158 payload_length = 2
159 end
160 end
161 if flags & 0x8 == 0x8 then
162 payload_length = payload_length + 2
163 elseif flags & 0x40 == 0x40 then
164 payload_length = payload_length + 4
165 elseif flags & 0x80 == 0x80 then
166 payload_length = payload_length + 8
167 end
168 if flags & 0x120 == 0x100 then -- Only applies to the last character
169 payload_length = payload_length + 2 + sio.readcardinal2(glyf, offset + payload_length)
170 end
171 component.flags = flags
172 component.payload = glyf:sub(offset, offset + payload_length - 1)
173 components[#components+1] = component
174 offset = offset + payload_length
175 until flags & 0x20 == 0
176 return components
177 else
178 local end_contours = sio.readcardinaltable(glyf, offset+10, num_contours, 2)
179 offset = offset + 10 + num_contours * 2
180 local instruction_length = sio.readcardinal2(glyf, offset)
181 local instructions = glyf:sub(offset+2, offset+1+instruction_length)
182 offset = offset+2+instruction_length
183 local total_points = end_contours[num_contours] + 1
184 local points = lua.newtable(total_points, 2)
185 do
186 local i = 1
187 while i <= total_points do
188 local flags = sio.readcardinal1(glyf, offset)
189 offset = offset + 1
190 local count = 0
191 if flags & 0x8 == 0x8 then
192 count = sio.readcardinal1(glyf, offset)
193 offset = offset + 1
194 end
195 for j=i, i + count do
196 points[j] = {flags = flags}
197 end
198 i = i + 1 + count
199 end
200 end
201 do
202 local last = 0
203 for i=1, total_points do
204 local point = points[i]
205 local flags = point.flags
206 local value
207 if flags & 0x2 == 0x2 then -- short
208 value = sio.readcardinal1(glyf, offset)
209 offset = offset + 1
210 if flags & 0x10 == 0 then
211 value = -value
212 end
213 elseif flags & 0x10 == 0 then
214 value = sio.readinteger2(glyf, offset)
215 offset = offset + 2
216 else
217 value = 0
218 end
219 last = last + value
220 point.x = last
221 end
222 last = 0
223 for i=1, total_points do
224 local point = points[i]
225 local flags = point.flags
226 local value
227 if flags & 0x4 == 0x4 then -- short
228 value = sio.readcardinal1(glyf, offset)
229 offset = offset + 1
230 if flags & 0x20 == 0 then
231 value = -value
232 end
233 elseif flags & 0x20 == 0 then
234 value = sio.readinteger2(glyf, offset)
235 offset = offset + 2
236 else
237 value = 0
238 end
239 last = last + value
240 point.y = last
241 point.flags = flags & 0xC1 -- Discard all flags we already used
242 end
243 -- assert (i == total_points)
244 end
245 points.contours = end_contours
246 points.instructions = instructions
247 return points
248 end
249 end
250
251 local function serialize_glyf(points, map)
252 local contours = points.contours
253 if points.contours then
254 local flagdata, xdata, ydata = {}, {}, {}
255 local last_flags, last_x, last_y = 0x100 -- Impossible flag to avoid triggering a repeated flag in the first step
256 local xmin, ymin, xmax, ymax
257 for i=1, #points do
258 local point = points[i]
259 local flags, x, y = point.flags, math.floor(point.x + .5), math.floor(point.y + .5)
260 xmin = xmin and xmin < x and xmin or x
261 xmax = xmax and xmax > x and xmax or x
262 ymin = ymin and ymin < y and ymin or y
263 ymax = ymax and ymax > y and ymax or y
264 x, last_x = x - (last_x or 0), x
265 if x == 0 then
266 flags = flags | 0x10
267 elseif x < 0x100 and x > -0x100 then
268 if x < 0 then
269 x = -x
270 flags = flags | 0x2
271 else
272 flags = flags | 0x12
273 end
274 xdata[#xdata+1] = x
275 else
276 if x > 0x7FFF then
277 x = 0x7FFF
278 elseif x < -0x8000 then
279 x = -0x8000
280 end
281 if x < 0 then
282 x = x + 0x10000
283 end
284 xdata[#xdata+1] = x >> 8
285 xdata[#xdata+1] = x & 0xFF
286 end
287 y, last_y = y - (last_y or 0), y
288 if y == 0 then
289 flags = flags | 0x20
290 elseif y < 0x100 and y > -0x100 then
291 if y < 0 then
292 y = -y
293 flags = flags | 0x4
294 else
295 flags = flags | 0x24
296 end
297 ydata[#ydata+1] = y
298 else
299 if y > 0x7FFF then
300 y = 0x7FFF
301 elseif y < -0x8000 then
302 y = -0x8000
303 end
304 if y < 0 then
305 y = y + 0x10000
306 end
307 ydata[#ydata+1] = y >> 8
308 ydata[#ydata+1] = y & 0xFF
309 end
310 if flags == last_flags & 0x1F7 then
311 if last_flags & 0x8 == 0x8 then
312 flagdata[#flagdata] = flagdata[#flagdata] + 1
313 else
314 last_flags = last_flags | 0x8
315 flagdata[#flagdata] = last_flags
316 flagdata[#flagdata+1] = 1
317 end
318 else
319 last_flags = flags
320 flagdata[#flagdata+1] = last_flags
321 end
322 end
323 local header = string.pack(">i2i2i2i2i2" .. string.rep('I2', #contours), #contours, xmin, ymin, xmax, ymax, table.unpack(contours))
324 return header .. string.pack(">s2", points.instructions) .. string.char(table.unpack(flagdata)) .. string.char(table.unpack(xdata)) .. string.char(table.unpack(ydata))
325 else
326 local result = string.pack(">i2i2i2i2i2", -1, points.xmin, points.ymin, points.xmax, points.ymax)
327 for i = 1, #points do
328 local component = points[i]
329 local x, y = component.x, component.y
330 x = x and math.floor(x + .5)
331 y = y and math.floor(y + .5)
332 if component.flags & 0x3 == 0x2 and (x >= 0x100 or x < -0x100 or y >= 0x100 or y < 0x100) then
333 component.flags = component.flags | 0x1
334 end
335 result = result
336 .. string.pack(component.flags & 0x2 == 0 and '>I2I2'
337 or component.flags & 0x1 == 0x1 and '>I2I2i2i2'
338 or '>I2I2i1i1',
339 component.flags, map[component.glyph], x, y)
340 .. component.payload
341 end
342 return result
343 end
344 end
345
346 local function read_points(gvar_data, offset)
347 local point_count = sio.readcardinal1(gvar_data, offset)
348 offset = offset + 1
349 if point_count == 0 then
350 return true, offset
351 end
352 if point_count & 0x80 ~= 0 then
353 point_count = ((point_count & 0x7F) << 8) | sio.readcardinal1(gvar_data, offset)
354 offset = offset + 1
355 end
356 local points = lua.newtable(point_count, 0)
357 local i, last = 1, 1
358 while i <= point_count do
359 local control = sio.readcardinal1(gvar_data, offset)
360 offset = offset + 1
361 local count = (control & 0x7F) + 1
362 local size = control & 0x80 == 0x80 and 2 or 1
363 local read = size == 2 and sio.readcardinal2 or sio.readcardinal1
364 for j=0, count - 1 do
365 last = last + read(gvar_data, offset + j * size)
366 points[i + j] = last
367 end
368 i, offset = i + count, offset + size * count
369 end
370 return points, offset
371 end
372
373 local function read_deltas(gvar_data, offset, count)
374 local deltas = lua.newtable(count, 0)
375 local i = 1
376 while i <= count do
377 local control = sio.readcardinal1(gvar_data, offset)
378 offset = offset + 1
379 local length = (control & 0x3F) + 1
380 if control & 0x80 == 0x80 then
381 for j=0, length - 1 do
382 deltas[i + j] = 0
383 end
384 else
385 local size = control & 0x40 == 0x40 and 2 or 1
386 local read = size == 2 and sio.readinteger2 or sio.readinteger1
387 for j=0, length - 1 do
388 deltas[i + j] = read(gvar_data, offset + j * size)
389 end
390 offset = offset + size * length
391 end
392 i = i + length
393 end
394 return deltas, offset
395 end
396
397 local function interpolate_glyf(loca, gvar_index, gvar, glyf, gid, coords, map)
398 local var = gvar_index[gid+1]
399 if not var then
400 local start = loca[gid+1] + 1
401 local stop = loca[gid+2]
402 -- If the glyph uses components then we can never just copy it but have to parse
403 -- it to rewrite the components if necessary.
404 if stop >= start + 2 and sio.readinteger2(glyf, start) < 0 then
405 return serialize_glyf(parse_glyf(loca, glyf, gid), map)
406 else
407 return glyf:sub(start, stop)
408 end
409 end
410 local points = parse_glyf(loca, glyf, gid)
411 if not points then return '' end
412 local offset = var.offset
413 local shared_points
414 if var.shared_points then
415 shared_points, offset = read_points(gvar, offset)
416 end
417 for i=1, #var.headers do
418 local header = var.headers[i]
419 local size = header.size
420 local factor = 1
421 local start, peak, stop = header.start_inter, header.peak, header.end_inter
422 for j = 1, #coords do
423 local coord = coords[j]
424 local peak = peak[j]
425 local start = start and start[j] or peak < 0 and peak or 0
426 local stop = stop and stop[j] or peak > 0 and peak or 0
427 if peak == 0 then -- Skip
428 elseif peak == coord then
429 -- factor = factor * 1
430 elseif coord <= start or coord >= stop then
431 factor = 0
432 break
433 elseif coord < peak then
434 factor = factor * ((coord-start) / (peak-start))
435 else--if coord > peak then
436 factor = factor * ((stop-coord) / (stop-peak))
437 end
438 end
439 if factor ~= 0 then
440 local base_points, suboffset
441 if header.private_points then
442 base_points, suboffset = read_points(gvar, offset)
443 else
444 base_points, suboffset = shared_points, offset
445 end
446 local count = base_points == true and #points + 4 or #base_points
447 local x_deltas, y_deltas
448 x_deltas, suboffset = read_deltas(gvar, suboffset, count)
449 y_deltas, suboffset = read_deltas(gvar, suboffset, count)
450 local contours = points.contours
451 if contours then
452 if base_points == true then
453 for i=1, #points do
454 local point = points[i]
455 point.x = point.x + factor * x_deltas[i]
456 point.y = point.y + factor * y_deltas[i]
457 end
458 else
459 local contour = 1
460 local last, last_x, last_y, last_dx, last_dy
461 local first, first_x, first_y, first_dx, first_dy
462 local cur = 1
463 local skip = base_points[1] > contours[1] + 1
464 local n_points = #points
465 for i = 1, n_points do
466 if i == contours[contour] + 2 then
467 contour = contour + 1
468 last_x, last_y, last_dx, last_dy = nil
469 first_x, first_y, first_dx, first_dy = nil
470 skip = (base_points[cur] or n_points + 1) > contours[contour] + 1
471 end
472 if not skip then
473 local point = points[i]
474 local this_x, this_y = point.x, point.y
475 if base_points[cur] == i then
476 repeat
477 last_x, last_y = this_x, this_y
478 last_dx, last_dy = x_deltas[cur], y_deltas[cur]
479 if not first_x then
480 first_x, first_y, first_dx, first_dy = last_x, last_y, last_dx, last_dy
481 end
482 point.x = last_x + factor * last_dx
483 point.y = last_y + factor * last_dy
484 cur = cur + 1
485 until base_points[cur] ~= i
486 else
487 if not last_x then -- We started a new contour and haven't seen a real point yet. Look for the last one instead.
488 local idx = cur
489 while (base_points[idx + 1] or n_points + 1) <= contours[contour] + 1 do
490 idx = idx + 1
491 end
492 local idx_point = points[base_points[idx]]
493 last_x, last_y = idx_point.x, idx_point.y
494 last_dx, last_dy = x_deltas[idx], y_deltas[idx]
495 end
496 local after_x, after_y, after_dx, after_dy
497 if (base_points[cur] or n_points + 1) <= contours[contour] + 1 then -- If the next point is part of the contour, use it. Otherwise use the first point in our contour)
498 local next_point = points[base_points[cur]]
499 after_x, after_y = next_point.x, next_point.y
500 after_dx, after_dy = x_deltas[cur], y_deltas[cur]
501 else
502 after_x, after_y = first_x, first_y
503 after_dx, after_dy = first_dx, first_dy
504 end
505
506 -- The first case is a bit weird, but afterwards we just interpolate while clipping to the boundaries.
507 -- See the gvar spec for details.
508 if last_x == after_x then
509 if last_dx == after_dx then
510 point.x = this_x + factor * last_dx
511 end
512 elseif this_x <= last_x and this_x <= after_x then
513 point.x = this_x + factor * (last_x < after_x and last_dx or after_dx)
514 elseif this_x >= last_x and this_x >= after_x then
515 point.x = this_x + factor * (last_x > after_x and last_dx or after_dx)
516 else -- We have to interpolate
517 local t = (this_x - last_x) / (after_x - last_x)
518 point.x = this_x + factor * ((1-t) * last_dx + t * after_dx)
519 end
520
521 -- And again for y
522 if last_y == after_y then
523 if last_dy == after_dy then
524 point.y = this_y + factor * last_dy
525 end
526 elseif this_y <= last_y and this_y <= after_y then
527 point.y = this_y + factor * (last_y < after_y and last_dy or after_dy)
528 elseif this_y >= last_y and this_y >= after_y then
529 point.y = this_y + factor * (last_y > after_y and last_dy or after_dy)
530 else -- We have to interpolate
531 local t = (this_y - last_y) / (after_y - last_y)
532 point.y = this_y + factor * ((1-t) * last_dy + t * after_dy)
533 end
534 end
535 end
536 end
537 end
538 else
539 -- Composite glyph
540 if base_points == true then
541 for i=1, #points do
542 local point = points[i]
543 if point.x then
544 point.x = point.x + factor * x_deltas[i]
545 point.y = point.y + factor * y_deltas[i]
546 end
547 end
548 else
549 for i=1, #base_points do
550 local point = points[base_points[i]]
551 if point and point.x then
552 point.x = point.x + factor * x_deltas[i]
553 point.y = point.y + factor * y_deltas[i]
554 end
555 end
556 end
557 end
558 assert(suboffset == offset + size)
559 end
560 offset = offset + size
561 end
562 return serialize_glyf(points, map)
563 end
564
565 return function(face, font, map, map_inv)
566 local gvar = assert(face:get_table(gvar_tag):get_data())
567 local gvar_index = assert(read_gvar(gvar))
568 local loca = assert(read_loca(face))
569 local glyf = assert(face:get_table(glyf_tag):get_data())
570 local normalized = {font:get_var_coords_normalized()}
571 return function(gid)
572 return interpolate_glyf(loca, gvar_index, gvar, glyf, map_inv[gid], normalized, map)
573 end
574 end