diff options
Diffstat (limited to 'VP-fontwork.lua')
-rw-r--r-- | VP-fontwork.lua | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/VP-fontwork.lua b/VP-fontwork.lua new file mode 100644 index 0000000..4029227 --- /dev/null +++ b/VP-fontwork.lua @@ -0,0 +1,93 @@ +function put_extra_glyph(width, data) + local idx = width .. "-" .. data + if not extra_glyphs_file then extra_glyphs_file = Output "extra-glyphs.xml" end + if extra_glyphs[idx] then return end + extra_glyphs_file:write(' <Symbol Width="' .. width .. '" Data="' .. data .. '" Text="Unknown" />\n') + extra_glyphs[idx] = true +end + +function resolve_font(font) + local r = {} + + for k, v in pairs(font) do + local idx = v.Width .. "-" .. v.Data + local g = glyphes[idx] + if not g then + put_extra_glyph(v.Width, v.Data) + r[k] = "<??>" + else + r[k] = g.attr.Text + end + end + + return r +end + +function extract_font(fname, h) + local n_glyphes = h:readU32() + local height = h:readU32() + local widths = {} + + for i = 1, n_glyphes do + widths[i] = h:readU8() + end + + local o + local r = {} + + if dump_glyph then + dump_mkdir(fname) + end + + for i = 1, n_glyphes do + if dump_mode and dump_glyph then + o = Output(fname .. string.format("/%03i-%02i.glyph", i, widths[i])) + o:copyfrom(h, height * 2) + o:destroy() + h:seek(-height * 2, SEEK_CUR) + o = Output(fname .. string.format("/%03i-%02i.raw", i, widths[i])) + local bits + for j = 1, height do + bits = h:readU16() + for k = 1, height do -- should be widths[i] + bits = shl(bits, 1) + local bit = andB(bits, 65536) ~= 0 + o:writeU8(bit and 0xff or 0) + end + end + o:destroy() + h:seek(-height * 2, SEEK_CUR) + end + local b = Buffer(true) + b:copyfrom(h, height * 2) + if widths[i] ~= 0 then + r[i] = { Data = Base64Encode(b), Width = widths[i] } + else + -- Verify this is only padding + local Data = Base64Encode(b) + if Data ~= "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" then + error("Font not consistant.") + end + end + b:destroy() + end + + return r +end + +function add_glyph(font, char) + local idx = font.idx + font.ilookup[idx] = char + font.clookup[char] = idx + font.idx = font.idx + 1 + return idx +end + +function load_glyphes() + local tglyphes = xml.LoadHandle(Input "VP-database.xml")[1] + glyphes = {} + for k, v in ipairs(tglyphes) do + glyphes[v.attr.Width .. "-" .. v.attr.Data] = v + glyphes[v.attr.Text] = v + end +end |