| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-harf-var-cff2.lua |
| 3 |
-- DESCRIPTION: part of luaotfload / HarfBuzz / Parse and convert CFF2 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-cff2", |
| 8 |
version = "3.29", --TAGVERSION |
| 9 |
date = "2024-12-03", --TAGDATE |
| 10 |
description = "luaotfload submodule / CFF2 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 cff2 = hb.Tag.new'CFF2' |
| 19 |
local serialize = require'luaotfload-harf-var-t2-writer' |
| 20 |
|
| 21 |
local offsetfmt = ">I%i" |
| 22 |
local function parse_index(buf, i) |
| 23 |
local count, offsize |
| 24 |
count, offsize, i = string.unpack(">I4B", buf, i) |
| 25 |
if count == 0 then return {}, i-1 end |
| 26 |
local fmt = offsetfmt:format(offsize) |
| 27 |
local offsets = {} |
| 28 |
local dataoffset = i + offsize*count - 1 |
| 29 |
for j=1,count+1 do |
| 30 |
offsets[j], i = string.unpack(fmt, buf, i) |
| 31 |
end |
| 32 |
for j=1,count+1 do |
| 33 |
offsets[j] = offsets[j] + i - 1 |
| 34 |
end |
| 35 |
return offsets, offsets[#offsets] |
| 36 |
end |
| 37 |
|
| 38 |
local real_mapping = { [0] = '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 39 |
'.', 'E', 'E-', nil, '-', nil} |
| 40 |
local function parse_real(cs, offset) |
| 41 |
local c = cs:byte(offset) |
| 42 |
if not c then return offset end |
| 43 |
local c1, c2 = real_mapping[c>>4], real_mapping[c&0xF] |
| 44 |
if not c1 or not c2 then |
| 45 |
return c1 or offset, c1 and offset |
| 46 |
else |
| 47 |
return c1, c2, parse_real(cs, offset+1) --Warning: This is not a tail-call, |
| 48 |
-- so we are affected by the stack limit. On the other hand, as long as |
| 49 |
-- there are less than ~50 bytes we should be safe. |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
local function get_number(result) |
| 54 |
assert(#result == 1) |
| 55 |
local num = result[1] |
| 56 |
result[1] = nil |
| 57 |
return num |
| 58 |
end |
| 59 |
|
| 60 |
local function get_bool(result) |
| 61 |
return get_number(result) == 1 |
| 62 |
end |
| 63 |
|
| 64 |
local function get_array(result) |
| 65 |
local arr = table.move(result, 1, #result, 1, {}) |
| 66 |
for i=1,#result do result[i] = nil end |
| 67 |
return arr |
| 68 |
end |
| 69 |
|
| 70 |
local function get_delta(result) |
| 71 |
local arr = get_array(result) |
| 72 |
local last = 0 |
| 73 |
for i=1,#arr do |
| 74 |
arr[i] = arr[i]+last |
| 75 |
last = arr[i] |
| 76 |
end |
| 77 |
return arr |
| 78 |
end |
| 79 |
|
| 80 |
local function get_private(result) |
| 81 |
local arr = get_array(result) |
| 82 |
assert(#arr == 2) |
| 83 |
return arr |
| 84 |
end |
| 85 |
|
| 86 |
local function do_blend(result, vstore) |
| 87 |
if not vstore then |
| 88 |
error'blend operator only allowed in Private dictionary of variable fonts' |
| 89 |
end |
| 90 |
local vsindex = (result.vsindex or 0) + 1 |
| 91 |
local factors = vstore[vsindex] |
| 92 |
local n = result[#result] |
| 93 |
local k = #factors |
| 94 |
local before = #result - 1 - n*(k+1) |
| 95 |
for i = 1, n do |
| 96 |
local val = result[before + i] |
| 97 |
for j = 1, k do |
| 98 |
val = val + factors[j] * result[before + n + (i-1) * k + j] |
| 99 |
end |
| 100 |
result[before + i] = math.floor(val + .5) |
| 101 |
end |
| 102 |
for i = before + n + 1, #result do |
| 103 |
result[i] = nil |
| 104 |
end |
| 105 |
return arr |
| 106 |
end |
| 107 |
|
| 108 |
local function apply_matrix(m, x, y) |
| 109 |
return (m[1] * x + m[3] * y + m[5])*1000, (m[2] * x + m[4] * y + m[6])*1000 |
| 110 |
end |
| 111 |
|
| 112 |
local operators = { |
| 113 |
[6] = {'BlueValues', get_delta}, |
| 114 |
[7] = {'OtherBlues', get_delta}, |
| 115 |
[8] = {'FamilyBlues', get_delta}, |
| 116 |
[9] = {'FamilyOtherBlues', get_delta}, |
| 117 |
[10] = {'StdHW', get_number}, |
| 118 |
[11] = {'StdVW', get_number}, |
| 119 |
[17] = {'CharStrings', get_number}, |
| 120 |
[18] = {'Private', get_private}, |
| 121 |
[19] = {'Subrs', get_number}, |
| 122 |
[22] = {'vsindex', get_number}, |
| 123 |
[23] = {'blend', do_blend}, |
| 124 |
[24] = {'vstore', get_number}, |
| 125 |
[-8] = {'FontMatrix', get_array}, |
| 126 |
[-10] = {'BlueScale', get_number}, |
| 127 |
[-11] = {'BlueShift', get_number}, |
| 128 |
[-12] = {'BlueFuzz', get_number}, |
| 129 |
[-13] = {'StemSnapH', get_delta}, |
| 130 |
[-14] = {'StemSnapV', get_delta}, |
| 131 |
[-15] = {'ForceBold', get_bool}, -- ??? |
| 132 |
[-18] = {'LanguageGroup', get_number}, |
| 133 |
[-19] = {'ExpansionFactor', get_number}, |
| 134 |
[-20] = {'initialRandomSeed', get_number}, -- ??? |
| 135 |
[-37] = {'FDArray', get_number}, |
| 136 |
[-38] = {'FDSelect', get_number}, |
| 137 |
} |
| 138 |
local function parse_dict(buf, i, j, vstore) |
| 139 |
result = {} |
| 140 |
while i<=j do |
| 141 |
local cmd = buf:byte(i) |
| 142 |
if cmd == 29 then |
| 143 |
result[#result+1] = string.unpack(">i4", buf:sub(i+1, i+4)) |
| 144 |
i = i+4 |
| 145 |
elseif cmd == 28 then |
| 146 |
result[#result+1] = string.unpack(">i2", buf:sub(i+1, i+2)) |
| 147 |
i = i+2 |
| 148 |
elseif cmd >= 251 then -- Actually "and cmd ~= 255", but 255 is reserved |
| 149 |
result[#result+1] = -((cmd-251)*256)-string.byte(buf, i+1)-108 |
| 150 |
i = i+1 |
| 151 |
elseif cmd >= 247 then |
| 152 |
result[#result+1] = (cmd-247)*256+string.byte(buf, i+1)+108 |
| 153 |
i = i+1 |
| 154 |
elseif cmd >= 32 then |
| 155 |
result[#result+1] = cmd-139 |
| 156 |
elseif cmd == 30 then -- 31 is reserved again |
| 157 |
local real = {parse_real(buf, i+1)} |
| 158 |
i = real[#real] |
| 159 |
real[#real] = nil |
| 160 |
result[#result+1] = tonumber(table.concat(real)) |
| 161 |
else |
| 162 |
if cmd == 12 then |
| 163 |
i = i+1 |
| 164 |
cmd = -buf:byte(i)-1 |
| 165 |
end |
| 166 |
local op = operators[cmd] |
| 167 |
if not op then error[[Unknown CFF operator]] end |
| 168 |
result[op[1]] = op[2](result, vstore) |
| 169 |
end |
| 170 |
i = i+1 |
| 171 |
end |
| 172 |
return result |
| 173 |
end |
| 174 |
|
| 175 |
local function parse_charstring(buf, start, after, globalsubrs, subrs, result) |
| 176 |
local lastresult = result[#result] |
| 177 |
while start ~= after do |
| 178 |
local cmd = buf:byte(start) |
| 179 |
if cmd == 28 then |
| 180 |
lastresult[#lastresult+1] = string.unpack(">i2", buf:sub(start+1, start+2)) |
| 181 |
start = start+2 |
| 182 |
elseif cmd == 255 then |
| 183 |
lastresult[#lastresult+1] = string.unpack(">i4", buf:sub(start+1, start+4))/0x10000 |
| 184 |
start = start+4 |
| 185 |
elseif cmd >= 251 then |
| 186 |
lastresult[#lastresult+1] = -((cmd-251)*256)-string.byte(buf, start+1)-108 |
| 187 |
start = start+1 |
| 188 |
elseif cmd >= 247 then |
| 189 |
lastresult[#lastresult+1] = (cmd-247)*256+string.byte(buf, start+1)+108 |
| 190 |
start = start+1 |
| 191 |
elseif cmd >= 32 then |
| 192 |
lastresult[#lastresult+1] = cmd-139 |
| 193 |
elseif cmd == 10 then |
| 194 |
local idx = lastresult[#lastresult]+subrs.bias |
| 195 |
local sub_start = subrs[idx] |
| 196 |
local sub_stop = subrs[idx+1] |
| 197 |
lastresult[#lastresult] = nil |
| 198 |
parse_charstring(buf, sub_start, sub_stop, globalsubrs, subrs, result) |
| 199 |
lastresult = result[#result] |
| 200 |
elseif cmd == 29 then |
| 201 |
local idx = lastresult[#lastresult]+globalsubrs.bias |
| 202 |
local sub_start = globalsubrs[idx] |
| 203 |
local sub_stop = globalsubrs[idx+1] |
| 204 |
lastresult[#lastresult] = nil |
| 205 |
parse_charstring(buf, sub_start, sub_stop, globalsubrs, subrs, result) |
| 206 |
lastresult = result[#result] |
| 207 |
elseif cmd == 11 then |
| 208 |
break -- We do not keep subroutines, so drop returns and continue with the outer commands |
| 209 |
elseif cmd == 15 then -- vsindex |
| 210 |
assert(#lastresult == 2) |
| 211 |
result.factors = result.vstore[lastresult[2] + 1] |
| 212 |
lastresult[2] = nil |
| 213 |
elseif cmd == 16 then -- blend |
| 214 |
local factors = result.factors |
| 215 |
if not factors then |
| 216 |
error'blend operator outside of variable font or with invalid vsindex' |
| 217 |
end |
| 218 |
local n = lastresult[#lastresult] |
| 219 |
local k = #factors |
| 220 |
local before = #lastresult - 1 - n*(k+1) |
| 221 |
for i = 1, n do |
| 222 |
local val = lastresult[before + i] |
| 223 |
for j = 1, k do |
| 224 |
val = val + factors[j] * lastresult[before + n + (i-1) * k + j] |
| 225 |
end |
| 226 |
lastresult[before + i] = math.floor(val + .5) |
| 227 |
end |
| 228 |
for i = before + n + 1, #lastresult do |
| 229 |
lastresult[i] = nil |
| 230 |
end |
| 231 |
else |
| 232 |
if cmd == 12 then |
| 233 |
start = start+1 |
| 234 |
cmd = -buf:byte(start)-1 |
| 235 |
elseif cmd == 19 or cmd == 20 then |
| 236 |
if #result == 1 then |
| 237 |
lastresult = {} |
| 238 |
result[#result+1] = lastresult |
| 239 |
end |
| 240 |
local newi = start+(result.stemcount+7)//8 |
| 241 |
lastresult[2] = buf:sub(start+1, newi) |
| 242 |
start = newi |
| 243 |
elseif cmd == 21 and #result == 1 then |
| 244 |
table.insert(result, 1, {false}) |
| 245 |
if #lastresult == 4 then |
| 246 |
result[1][2] = lastresult[2] |
| 247 |
table.remove(lastresult, 2) |
| 248 |
end |
| 249 |
elseif (cmd == 4 or cmd == 22) and #result == 1 then |
| 250 |
table.insert(result, 1, {false}) |
| 251 |
if #lastresult == 3 then |
| 252 |
result[1][2] = lastresult[2] |
| 253 |
table.remove(lastresult, 2) |
| 254 |
end |
| 255 |
elseif cmd == 14 and #result == 1 then |
| 256 |
table.insert(result, 1, {false}) |
| 257 |
if #lastresult == 2 or #lastresult == 6 then |
| 258 |
result[1][2] = lastresult[2] |
| 259 |
table.remove(lastresult, 2) |
| 260 |
end |
| 261 |
elseif cmd == 1 or cmd == 3 or cmd == 18 or cmd == 23 then |
| 262 |
if #result == 1 then |
| 263 |
table.insert(result, 1, {false}) |
| 264 |
if #lastresult % 2 == 0 then |
| 265 |
result[1][2] = lastresult[2] |
| 266 |
table.remove(lastresult, 2) |
| 267 |
end |
| 268 |
end |
| 269 |
result.stemcount = result.stemcount + #lastresult//2 |
| 270 |
end |
| 271 |
lastresult[1] = cmd |
| 272 |
lastresult = {false} |
| 273 |
result[#result+1] = lastresult |
| 274 |
end |
| 275 |
start = start+1 |
| 276 |
end |
| 277 |
return result |
| 278 |
end |
| 279 |
|
| 280 |
local function parse_fdselect(buf, offset, CharStrings) |
| 281 |
local format |
| 282 |
format, offset = string.unpack(">B", buf, offset) |
| 283 |
if format == 0 then |
| 284 |
for i=0,#CharStrings-1 do |
| 285 |
local code |
| 286 |
code, offset = string.unpack(">B", buf, offset) |
| 287 |
CharStrings[i][3] = code + 1 |
| 288 |
end -- Reimplement with string.byte |
| 289 |
elseif format == 3 then |
| 290 |
local count, last |
| 291 |
count, offset = string.unpack(">I2", buf, offset) |
| 292 |
for i=1,count do |
| 293 |
local first, code, after = string.unpack(">I2BI2", buf, offset) |
| 294 |
for j=first, after-1 do |
| 295 |
CharStrings[j][3] = code + 1 |
| 296 |
end |
| 297 |
offset = offset + 3 |
| 298 |
end |
| 299 |
elseif format == 4 then |
| 300 |
local count, last |
| 301 |
count, offset = string.unpack(">I4", buf, offset) |
| 302 |
for i=1,count do |
| 303 |
local first, code, after = string.unpack(">I4I2I4", buf, offset) |
| 304 |
for j=first, after-1 do |
| 305 |
CharStrings[j][3] = code + 1 |
| 306 |
end |
| 307 |
offset = offset + 6 |
| 308 |
end |
| 309 |
else |
| 310 |
error[[Invalid FDSelect format]] |
| 311 |
end |
| 312 |
end |
| 313 |
|
| 314 |
local function parse_vstore(buf, offset, variation) |
| 315 |
local size, format, region_list_off, item_variation_count, off = string.unpack(">I2I2I4I2", buf, offset) |
| 316 |
if format ~= 1 then |
| 317 |
error'Unsupported vstore format' |
| 318 |
end |
| 319 |
offset = offset + 2 -- Skip the size |
| 320 |
region_list_off = offset + region_list_off |
| 321 |
|
| 322 |
local axis_count, region_count |
| 323 |
axis_count, region_count, region_list_off = string.unpack(">I2I2", buf, region_list_off) |
| 324 |
|
| 325 |
local variation_regions = {} |
| 326 |
for i = 1, region_count do |
| 327 |
local factor = 1 |
| 328 |
for j = 1, axis_count do |
| 329 |
local start, peak, stop |
| 330 |
start, peak, stop, region_list_off = string.unpack(">i2i2i2", buf, region_list_off) |
| 331 |
local coord = variation[j] |
| 332 |
if peak == 0 then -- Skip |
| 333 |
elseif peak == coord then |
| 334 |
-- factor = factor * 1 |
| 335 |
elseif coord <= start or coord >= stop then |
| 336 |
factor = 0 |
| 337 |
break |
| 338 |
elseif coord < peak then |
| 339 |
factor = factor * ((coord-start) / (peak-start)) |
| 340 |
else--if coord > peak then |
| 341 |
factor = factor * ((stop-coord) / (stop-peak)) |
| 342 |
end |
| 343 |
end |
| 344 |
variation_regions[i] = factor |
| 345 |
end |
| 346 |
|
| 347 |
local variation_data = {} |
| 348 |
for i = 1, item_variation_count do |
| 349 |
local item_off |
| 350 |
item_off, off = string.unpack(">I4", buf, off) |
| 351 |
local i_count, short_count, region_count |
| 352 |
i_count, short_count, region_count, item_off = string.unpack(">I2I2I2", buf, item_off + offset) |
| 353 |
if i_count ~= 0 or short_count ~= 0 then |
| 354 |
error'Unexpected variation items in CFF2 table' |
| 355 |
end |
| 356 |
local factors = {} |
| 357 |
for j = 1, region_count do |
| 358 |
local region |
| 359 |
region, item_off = string.unpack(">I2", buf, item_off) |
| 360 |
factors[j] = variation_regions[region+1] |
| 361 |
end |
| 362 |
variation_data[i] = factors |
| 363 |
end |
| 364 |
return variation_data |
| 365 |
end |
| 366 |
|
| 367 |
local function parse_cff2(buf, i0, coords) |
| 368 |
local fontid = 1 |
| 369 |
local major, minor, hdrSize, topSize = string.unpack(">BBBH", buf, i0) |
| 370 |
if major ~= 2 then error[[Unsupported CFF version]] end |
| 371 |
local i = i0 + hdrSize |
| 372 |
local top = parse_dict(buf, i, i + topSize - 1) |
| 373 |
i = i + topSize |
| 374 |
local globalsubrs |
| 375 |
globalsubrs, i = parse_index(buf, i) |
| 376 |
globalsubrs.bias = #globalsubrs-1 < 1240 and 108 or #globalsubrs-1 < 33900 and 1132 or 32769 |
| 377 |
top.GlobalSubrs = globalsubrs |
| 378 |
local CharStrings = parse_index(buf, i0+top.CharStrings) |
| 379 |
for i=1,#CharStrings-1 do |
| 380 |
CharStrings[i-1] = {CharStrings[i], CharStrings[i+1]-1} |
| 381 |
end |
| 382 |
CharStrings[#CharStrings] = nil |
| 383 |
CharStrings[#CharStrings] = nil |
| 384 |
local fonts = parse_index(buf, i0+top.FDArray) |
| 385 |
top.FDArray = nil |
| 386 |
top.vstore = parse_vstore(buf, i0 + top.vstore, coords) |
| 387 |
local privates = {} |
| 388 |
top.Privates = privates |
| 389 |
for i=1,#fonts-1 do |
| 390 |
local font = fonts[i] |
| 391 |
local fontdir = parse_dict(buf, fonts[i], fonts[i+1]-1) |
| 392 |
privates[i] = parse_dict(buf, i0+fontdir.Private[2], i0+fontdir.Private[2]+fontdir.Private[1]-1, top.vstore) |
| 393 |
local subrs = privates[i].Subrs |
| 394 |
if subrs then |
| 395 |
subrs = parse_index(buf, i0+fontdir.Private[2]+subrs) |
| 396 |
subrs.bias = #subrs-1 < 1240 and 108 or #subrs-1 < 33900 and 1132 or 32769 |
| 397 |
privates[i].Subrs = subrs |
| 398 |
end |
| 399 |
end |
| 400 |
if top.FDSelect then |
| 401 |
parse_fdselect(buf, i0+top.FDSelect, CharStrings) |
| 402 |
else |
| 403 |
for i=0,#CharStrings-1 do |
| 404 |
CharStrings[i][3] = 1 |
| 405 |
end |
| 406 |
end |
| 407 |
top.CharStrings = CharStrings |
| 408 |
local bbox |
| 409 |
if top.FontMatrix then |
| 410 |
local x0, y0 = apply_matrix(top.FontMatrix, top.FontBBox[1], top.FontBBox[2]) |
| 411 |
local x1, y1 = apply_matrix(top.FontMatrix, top.FontBBox[3], top.FontBBox[4]) |
| 412 |
bbox = {x0, y0, x1, y1} |
| 413 |
else |
| 414 |
bbox = top.FontBBox |
| 415 |
end |
| 416 |
return top, bbox |
| 417 |
end |
| 418 |
|
| 419 |
local function parse_glyph(buffer, top, gid) |
| 420 |
local cs = top.CharStrings[gid] |
| 421 |
local Private = top.Privates[cs[3]] |
| 422 |
return parse_charstring(buffer, cs[1], cs[2] + 1, |
| 423 |
top.GlobalSubrs, Private.Subrs, |
| 424 |
{{false}, stemcount = 0, vstore = top.vstore, factors = top.vstore and top.vstore[(Private.vsindex or 0) + 1]}) |
| 425 |
end |
| 426 |
|
| 427 |
return function(face, font) |
| 428 |
local data = face:get_table(cff2):get_data() |
| 429 |
local content = parse_cff2(data, 1, {font:get_var_coords_normalized()}) |
| 430 |
return function(gid) |
| 431 |
local glyph = parse_glyph(data, content, gid) |
| 432 |
glyph[1][2] = font:get_glyph_h_advance(gid) |
| 433 |
return serialize(glyph) |
| 434 |
end |
| 435 |
end |