cpdf-source/cpdfembed.ml

94 lines
3.2 KiB
OCaml
Raw Normal View History

2022-09-12 17:06:56 +02:00
(* Embed a font *)
2022-09-11 15:52:08 +02:00
open Pdfutil
2022-09-11 21:07:55 +02:00
(* For the first stage of our embedder, we are only allowing Latin, and we don't actually subset.
2022-09-11 15:52:08 +02:00
a) Get a list of Unicode codepoints;
b) See which of them are in the glyph list;
c) See which of those are in (StdEncoding|MacRomanEncoding|WinAnsiEncoding), and get their codes;
d) Build a font to do just those;
(* FUTURE *)
2) Allow characters not in the standard encodings by builing one or more secondary subsets *)
2022-09-11 16:19:59 +02:00
(*let () =
2022-09-11 15:52:08 +02:00
iter
(fun u ->
Printf.printf "unicode %i --> " u;
let glyphname = Hashtbl.find glyphlist_table [u] in
Printf.printf "glyph name %s --> " glyphname;
let pdfcode = Hashtbl.find encoding_table glyphname in
Printf.printf "pdf code %i\n" pdfcode)
2022-09-11 21:07:55 +02:00
unicodepoints *)
let pdfcode_of_unicode_codepoint encoding_table glyphlist_table u =
2022-09-11 15:52:08 +02:00
try
Some (Hashtbl.find encoding_table (Hashtbl.find glyphlist_table [u]))
with
Not_found -> None
2022-09-11 21:07:55 +02:00
let calc_accepted_unicodepoints encoding_table glyphlist_table codepoints =
2022-09-11 15:52:08 +02:00
setify
(option_map
2022-09-12 17:06:56 +02:00
(fun u ->
match
pdfcode_of_unicode_codepoint encoding_table glyphlist_table u
with
| Some _ -> Some u
| None -> None)
2022-09-11 15:52:08 +02:00
codepoints)
let fontnum = ref 0
let basename () =
incr fontnum;
"AAAAA" ^ string_of_char (char_of_int (!fontnum + 65))
2022-09-20 15:54:45 +02:00
let embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding =
2022-09-11 21:07:55 +02:00
let glyphlist_table = Pdfglyphlist.reverse_glyph_hashes () in
let encoding_table = Pdftext.reverse_table_of_encoding encoding in
let accepted_unicodepoints =
2022-10-04 17:09:20 +02:00
calc_accepted_unicodepoints encoding_table glyphlist_table codepoints
2022-09-11 21:07:55 +02:00
in
2022-10-04 21:35:05 +02:00
let f = hd (Cpdftruetype.parse ~subset:accepted_unicodepoints fontfile ~encoding) in
2022-09-11 21:07:55 +02:00
let name_1 = basename () in
2022-09-29 15:48:34 +02:00
let module TT = Cpdftruetype in
2022-09-11 21:07:55 +02:00
let fontfile =
2022-09-29 15:48:34 +02:00
let len = Pdfio.bytes_size f.TT.subset in
2022-09-11 21:07:55 +02:00
Pdf.Stream
{contents =
2022-09-12 17:06:56 +02:00
(Pdf.Dictionary
[("/Length", Pdf.Integer len); ("/Length1", Pdf.Integer len)],
2022-09-29 15:48:34 +02:00
Pdf.Got f.TT.subset)}
2022-09-11 21:07:55 +02:00
in
let fontfile_num = Pdf.addobj pdf fontfile in
2022-09-15 19:13:42 +02:00
let open Pdftext in
2022-09-16 17:53:47 +02:00
let fontmetrics =
let a = Array.make 256 0. in
for x = f.TT.firstchar to f.TT.lastchar do
a.(x) <- float_of_int (f.TT.widths.(x - f.TT.firstchar))
done;
a
in
2022-09-15 19:13:42 +02:00
SimpleFont
{fonttype = Truetype;
basefont = Printf.sprintf "/%s+%s" name_1 fontname;
2022-09-16 17:53:47 +02:00
fontmetrics = Some fontmetrics;
2022-09-15 19:13:42 +02:00
firstchar = f.TT.firstchar;
lastchar = f.TT.lastchar;
widths = f.TT.widths;
fontdescriptor = Some
{ascent = float_of_int f.TT.ascent;
descent = float_of_int f.TT.descent;
avgwidth = float_of_int f.TT.avgwidth;
maxwidth = float_of_int f.TT.maxwidth;
flags = f.TT.flags;
italicangle = float_of_int f.TT.italicangle;
capheight = float_of_int f.TT.capheight;
xheight = float_of_int f.TT.xheight;
stemv = float_of_int f.TT.stemv;
fontbbox = (float_of_int f.TT.minx, float_of_int f.TT.miny,
float_of_int f.TT.maxx, float_of_int f.TT.maxy);
fontfile = Some (FontFile2 fontfile_num);
charset = None;
tounicode = None};
encoding}