CPDF now uses proper metrics etc. for 14 standard fonts, as mandated by

ISO standard
This commit is contained in:
John Whitington 2014-09-09 15:06:56 +01:00
parent 5e09b88ed7
commit 0876413f32
2 changed files with 36 additions and 26 deletions

62
cpdf.ml
View File

@ -1631,44 +1631,54 @@ let find_justification_offsets longest_w w position = function
(* Lex an integer from the table *) (* Lex an integer from the table *)
let extract_num header s = let extract_num header s =
match Pdfgenlex.lex_string (Hashtbl.find header s) with match Pdfgenlex.lex_string (Hashtbl.find header s) with
[Pdfgenlex.LexInt i] -> i [Pdfgenlex.LexInt i] -> Pdf.Integer i
| _ -> raise (Failure "extract_num") | [Pdfgenlex.LexReal f] -> Pdf.Real f
| _ -> raise (Failure ("extract_num: " ^ s))
let extract_fontbbox header s = let extract_fontbbox header s =
match Pdfgenlex.lex_string (Hashtbl.find header s) with let num = function
[Pdfgenlex.LexInt a; Pdfgenlex.LexInt i -> Pdf.Integer i
Pdfgenlex.LexInt b; | Pdfgenlex.LexReal f -> Pdf.Real f
Pdfgenlex.LexInt c; | _ -> raise (Failure "extract_fontbbox")
Pdfgenlex.LexInt d] -> in
[Pdf.Integer a; Pdf.Integer b; Pdf.Integer c; Pdf.Integer d] match Pdfgenlex.lex_string (Hashtbl.find header s) with
| _ -> raise (Failure "extract_fontbbox") [a; b; c; d] -> [num a; num b; num c; num d]
| _ -> raise (Failure "extract_fontbbox")
let extract_widths width_data = [] (* Get the widths for each character from the hash table, in order, noting the
first and last *)
let extract_firstlast header = (0, 0) let extract_widths_firstlast width_data =
let sorted = List.sort compare (list_of_hashtbl width_data) in
match sorted, rev sorted with
(first, _)::_, (last, _)::_ ->
for x = first to last do
if not (Hashtbl.mem width_data x) then Hashtbl.add width_data x 0
done;
(first, last,
map snd (List.sort compare (list_of_hashtbl width_data)))
| _ -> raise (Failure "extract_widths_firstlast")
let make_font fontname = let make_font fontname =
let font = unopt (Pdftext.standard_font_of_name ("/" ^ fontname)) in let font = unopt (Pdftext.standard_font_of_name ("/" ^ fontname)) in
let header, width_data, _ = Pdfstandard14.afm_data font in let header, width_data, _ = Pdfstandard14.afm_data font in
let widths = extract_widths width_data let firstchar, lastchar, widths = extract_widths_firstlast width_data in
and firstchar, lastchar = extract_firstlast header let flags = Pdfstandard14.flags_of_standard_font font in
and flags = Pdfstandard14.flags_of_standard_font font let fontbbox = extract_fontbbox header "FontBBox" in
and fontbbox = extract_fontbbox header "FontBBox" let italicangle = extract_num header "ItalicAngle" in
and italicangle = extract_num header "ItalicAngle" let ascent = try extract_num header "Ascender" with _ -> Pdf.Integer 0 in
and ascent = extract_num header "Ascender" let descent = try extract_num header "Descender" with _ -> Pdf.Integer 0 in
and descent = extract_num header "Descender" let capheight = try extract_num header "CapHeight" with _ -> Pdf.Integer 0 in
and capheight = extract_num header "CapHeight" let stemv = Pdfstandard14.stemv_of_standard_font font in
and stemv = Pdfstandard14.stemv_of_standard_font font in
let fontdescriptor = let fontdescriptor =
Pdf.Dictionary Pdf.Dictionary
[("/Type", Pdf.Name "/FontDescriptor"); [("/Type", Pdf.Name "/FontDescriptor");
("/FontName", Pdf.Name ("/" ^ fontname)); ("/FontName", Pdf.Name ("/" ^ fontname));
("/Flags", Pdf.Integer flags); ("/Flags", Pdf.Integer flags);
("/FontBBox", Pdf.Array fontbbox); ("/FontBBox", Pdf.Array fontbbox);
("/ItalicAngle", Pdf.Integer italicangle); ("/ItalicAngle", italicangle);
("/Ascent", Pdf.Integer ascent); ("/Ascent", ascent);
("/Descent", Pdf.Integer descent); ("/Descent", descent);
("/CapHeight", Pdf.Integer capheight); ("/CapHeight", capheight);
("/StemV", Pdf.Integer stemv)] ("/StemV", Pdf.Integer stemv)]
in in
Pdf.Dictionary Pdf.Dictionary
@ -1678,7 +1688,7 @@ let make_font fontname =
("/BaseFont", Pdf.Name ("/" ^ fontname)); ("/BaseFont", Pdf.Name ("/" ^ fontname));
("/FirstChar", Pdf.Integer firstchar); ("/FirstChar", Pdf.Integer firstchar);
("/LastChar", Pdf.Integer lastchar); ("/LastChar", Pdf.Integer lastchar);
("/Widths", Pdf.Array widths); ("/Widths", Pdf.Array (map (fun x -> Pdf.Integer x) widths));
("/FontDescriptor", fontdescriptor)] ("/FontDescriptor", fontdescriptor)]
let addtext let addtext

BIN
empty.pdf Normal file

Binary file not shown.