| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-harf-var-t2-writer.lua |
| 3 |
-- DESCRIPTION: part of luaotfload / HarfBuzz / Serialize Type 2 charstrings |
| 4 |
----------------------------------------------------------------------- |
| 5 |
do |
| 6 |
assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { |
| 7 |
name = "luaotfload-harf-var-t2-writer", |
| 8 |
version = "3.29", --TAGVERSION |
| 9 |
date = "2024-12-03", --TAGDATE |
| 10 |
description = "luaotfload submodule / Type 2 charstring writer", |
| 11 |
license = "GPL v2.0", |
| 12 |
author = "Marcel Krüger", |
| 13 |
copyright = "Luaotfload Development Team", |
| 14 |
} |
| 15 |
end |
| 16 |
|
| 17 |
local pack = string.pack |
| 18 |
local function numbertot2(n) |
| 19 |
if math.abs(n) > 2^15 then |
| 20 |
error[[Number too big]] |
| 21 |
end |
| 22 |
local num = math.floor(n + .5) |
| 23 |
if n ~= 0 and math.abs((num-n)/n) > 0.001 then |
| 24 |
num = math.floor(n * 2^16 + 0.5) |
| 25 |
return pack(">Bi4", 255, math.floor(n * 2^16 + 0.5)) |
| 26 |
elseif num >= -107 and num <= 107 then |
| 27 |
return string.char(num + 139) |
| 28 |
elseif num >= 108 and num <= 1131 then |
| 29 |
return pack(">I2", num+0xF694) -- -108+(247*0x100) |
| 30 |
elseif num >= -1131 and num <= -108 then |
| 31 |
return pack(">I2", -num+0xFA94) -- -108+(251*0x100) |
| 32 |
else |
| 33 |
return pack(">Bi2", 28, num) |
| 34 |
end |
| 35 |
end |
| 36 |
local function convert_cs(cs, upem) |
| 37 |
local cs_parts = {} |
| 38 |
local function add(cmd, first, ...) |
| 39 |
if cmd == 19 or cmd == 20 then |
| 40 |
cs_parts[#cs_parts+1] = string.char(cmd) |
| 41 |
cs_parts[#cs_parts+1] = first |
| 42 |
return |
| 43 |
end |
| 44 |
if first then |
| 45 |
cs_parts[#cs_parts+1] = numbertot2(first*upem/1000) |
| 46 |
return add(cmd, ...) |
| 47 |
end |
| 48 |
if cmd then |
| 49 |
if cmd < 0 then |
| 50 |
cs_parts[#cs_parts+1] = string.char(12, -cmd-1) |
| 51 |
else |
| 52 |
cs_parts[#cs_parts+1] = string.char(cmd) |
| 53 |
end |
| 54 |
end |
| 55 |
end |
| 56 |
for _, args in ipairs(cs) do |
| 57 |
if args then |
| 58 |
local cmd = args[1] |
| 59 |
local height = #args - 1 |
| 60 |
local i = 2 |
| 61 |
while height > 48 do |
| 62 |
-- Special cases: hhcurveto, vvcurveto, rcurveline, rlinecurve |
| 63 |
-- hhvurveto/vvcurveto: In odd argument case, the first argument must be |
| 64 |
-- followed by a multiple of four arguments. |
| 65 |
if cmd == 27 and i == 2 and height % 4 == 1 then -- hhcurveto |
| 66 |
add(cmd, table.unpack(args, i, i + 44)) |
| 67 |
height = height - 45 |
| 68 |
i = i + 45 |
| 69 |
elseif cmd == 26 and i == 2 and height % 4 == 1 then -- vvcurveto |
| 70 |
add(cmd, table.unpack(args, i, i + 44)) |
| 71 |
height = height - 45 |
| 72 |
i = i + 45 |
| 73 |
-- rcurveline/rlinecurve: At least 8 arguments must be preserved, all previous instances |
| 74 |
-- need different commands |
| 75 |
elseif cmd == 24 then -- rcurveline |
| 76 |
height = height - 48 |
| 77 |
local count |
| 78 |
if height >= 8 then |
| 79 |
count = 48 |
| 80 |
else |
| 81 |
count = 48 - 8 + height |
| 82 |
height = 8 |
| 83 |
end |
| 84 |
add(8, table.unpack(args, i, i + count - 1)) -- 8 = rrcurveto |
| 85 |
i = i + count |
| 86 |
elseif cmd == 25 then -- rlinecurve |
| 87 |
height = height - 48 |
| 88 |
local count |
| 89 |
if height >= 8 then |
| 90 |
count = 48 |
| 91 |
else |
| 92 |
count = 48 - 8 + height |
| 93 |
height = 8 |
| 94 |
end |
| 95 |
add(5, table.unpack(args, i, i + count - 1)) -- 5 = rlineto |
| 96 |
i = i + count |
| 97 |
else -- Some commands have an optional last argument which can appear after multiple of 48 arguments, |
| 98 |
-- so it's safer to never leave a element alone in a line. |
| 99 |
height = height - 48 |
| 100 |
local count |
| 101 |
if height > 1 then |
| 102 |
count = 48 |
| 103 |
else |
| 104 |
-- assert(height == 1) |
| 105 |
count = 24--48 - 25 + height |
| 106 |
height = 25 |
| 107 |
end |
| 108 |
add(cmd, table.unpack(args, i, i + count - 1)) |
| 109 |
i = i + count |
| 110 |
end |
| 111 |
end |
| 112 |
add(cmd, table.unpack(args, i)) |
| 113 |
end |
| 114 |
end |
| 115 |
return table.concat(cs_parts) |
| 116 |
end |
| 117 |
|
| 118 |
return function(cs, upem) |
| 119 |
return convert_cs(cs, upem or 1000) |
| 120 |
end |