Fix padding of truetype tables

This commit is contained in:
John Whitington 2023-07-04 16:36:32 +01:00
parent 300a2c504b
commit 5f6e5d7ad0
1 changed files with 11 additions and 5 deletions

View File

@ -208,10 +208,11 @@ let write_loca_table subset cmap indexToLocFormat bs loca =
if !dbg then Printf.printf "write_loca_table: Unicode U+%04X is at location number %i\n" u locnum; if !dbg then Printf.printf "write_loca_table: Unicode U+%04X is at location number %i\n" u locnum;
Hashtbl.add locnums locnum ()) Hashtbl.add locnums locnum ())
subset; subset;
let len = ref 0 in
let write_entry loc position = let write_entry loc position =
match indexToLocFormat with match indexToLocFormat with
| 0 -> putval bs 16 (i32div position 2l) | 0 -> len += 2; putval bs 16 (i32div position 2l)
| 1 -> putval bs 32 position | 1 -> len += 4; putval bs 32 position
| _ -> raise (Pdf.PDFError "Unknown indexToLocFormat in write_loca_table") | _ -> raise (Pdf.PDFError "Unknown indexToLocFormat in write_loca_table")
in in
let pos = ref 0l in let pos = ref 0l in
@ -235,7 +236,10 @@ let write_loca_table subset cmap indexToLocFormat bs loca =
end end
else else
write_entry loc off) write_entry loc off)
pairs pairs;
let padding = if !len mod 4 = 0 then 0 else 4 - !len mod 4 in
Printf.printf "***loca table - adding %i bytes of padding\n" padding;
for x = 1 to padding do putval bs 8 0l done
(* Write the notdef glyf, and any others in the subset *) (* Write the notdef glyf, and any others in the subset *)
let write_glyf_table subset cmap bs mk_b glyfoffset loca = let write_glyf_table subset cmap bs mk_b glyfoffset loca =
@ -259,7 +263,8 @@ let write_glyf_table subset cmap bs mk_b glyfoffset loca =
for x = 1 to i32toi l do putval bs 8 (getval_32 b 8) done for x = 1 to i32toi l do putval bs 8 (getval_32 b 8) done
in in
iter (fun (a, b) -> write_bytes bs a (i32sub b a)) byteranges; iter (fun (a, b) -> write_bytes bs a (i32sub b a)) byteranges;
let padding = 4 - i32toi len mod 4 in let padding = if i32toi len mod 4 = 0 then 0 else 4 - i32toi len mod 4 in
Printf.printf "***glyf table - adding %i bytes of padding\n" padding;
for x = 1 to padding do putval bs 8 0l done; for x = 1 to padding do putval bs 8 0l done;
len len
@ -280,7 +285,8 @@ let write_cmap_table subset cmap bs =
putval bs 16 (i32ofi (length glyphindexes)); (* number of character codes *) putval bs 16 (i32ofi (length glyphindexes)); (* number of character codes *)
iter (fun gi -> putval bs 16 (i32ofi gi)) glyphindexes; (* glyph indexes *) iter (fun gi -> putval bs 16 (i32ofi gi)) glyphindexes; (* glyph indexes *)
let len = i32ofi (22 + 2 * length glyphindexes) in let len = i32ofi (22 + 2 * length glyphindexes) in
let padding = 4 - i32toi len mod 4 in let padding = if i32toi len mod 4 = 0 then 0 else 4 - i32toi len mod 4 in
Printf.printf "***cmap table - adding %i bytes of padding\n" padding;
for x = 1 to padding do putval bs 8 0l done; for x = 1 to padding do putval bs 8 0l done;
len len