From 0876413f326839162f33d4de46aaffa33f2b72e0 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 9 Sep 2014 15:06:56 +0100 Subject: [PATCH] CPDF now uses proper metrics etc. for 14 standard fonts, as mandated by ISO standard --- cpdf.ml | 62 +++++++++++++++++++++++++++++++----------------------- empty.pdf | Bin 0 -> 1162 bytes 2 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 empty.pdf diff --git a/cpdf.ml b/cpdf.ml index 13917df..55b936a 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -1631,44 +1631,54 @@ let find_justification_offsets longest_w w position = function (* Lex an integer from the table *) let extract_num header s = match Pdfgenlex.lex_string (Hashtbl.find header s) with - [Pdfgenlex.LexInt i] -> i - | _ -> raise (Failure "extract_num") + [Pdfgenlex.LexInt i] -> Pdf.Integer i + | [Pdfgenlex.LexReal f] -> Pdf.Real f + | _ -> raise (Failure ("extract_num: " ^ s)) let extract_fontbbox header s = - match Pdfgenlex.lex_string (Hashtbl.find header s) with - [Pdfgenlex.LexInt a; - Pdfgenlex.LexInt b; - Pdfgenlex.LexInt c; - Pdfgenlex.LexInt d] -> - [Pdf.Integer a; Pdf.Integer b; Pdf.Integer c; Pdf.Integer d] - | _ -> raise (Failure "extract_fontbbox") + let num = function + Pdfgenlex.LexInt i -> Pdf.Integer i + | Pdfgenlex.LexReal f -> Pdf.Real f + | _ -> raise (Failure "extract_fontbbox") + in + match Pdfgenlex.lex_string (Hashtbl.find header s) with + [a; b; c; d] -> [num a; num b; num c; num d] + | _ -> raise (Failure "extract_fontbbox") -let extract_widths width_data = [] - -let extract_firstlast header = (0, 0) +(* Get the widths for each character from the hash table, in order, noting the +first and last *) +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 font = unopt (Pdftext.standard_font_of_name ("/" ^ fontname)) in let header, width_data, _ = Pdfstandard14.afm_data font in - let widths = extract_widths width_data - and firstchar, lastchar = extract_firstlast header - and flags = Pdfstandard14.flags_of_standard_font font - and fontbbox = extract_fontbbox header "FontBBox" - and italicangle = extract_num header "ItalicAngle" - and ascent = extract_num header "Ascender" - and descent = extract_num header "Descender" - and capheight = extract_num header "CapHeight" - and stemv = Pdfstandard14.stemv_of_standard_font font in + let firstchar, lastchar, widths = extract_widths_firstlast width_data in + let flags = Pdfstandard14.flags_of_standard_font font in + let fontbbox = extract_fontbbox header "FontBBox" in + let italicangle = extract_num header "ItalicAngle" in + let ascent = try extract_num header "Ascender" with _ -> Pdf.Integer 0 in + let descent = try extract_num header "Descender" with _ -> Pdf.Integer 0 in + let capheight = try extract_num header "CapHeight" with _ -> Pdf.Integer 0 in + let stemv = Pdfstandard14.stemv_of_standard_font font in let fontdescriptor = Pdf.Dictionary [("/Type", Pdf.Name "/FontDescriptor"); ("/FontName", Pdf.Name ("/" ^ fontname)); ("/Flags", Pdf.Integer flags); ("/FontBBox", Pdf.Array fontbbox); - ("/ItalicAngle", Pdf.Integer italicangle); - ("/Ascent", Pdf.Integer ascent); - ("/Descent", Pdf.Integer descent); - ("/CapHeight", Pdf.Integer capheight); + ("/ItalicAngle", italicangle); + ("/Ascent", ascent); + ("/Descent", descent); + ("/CapHeight", capheight); ("/StemV", Pdf.Integer stemv)] in Pdf.Dictionary @@ -1678,7 +1688,7 @@ let make_font fontname = ("/BaseFont", Pdf.Name ("/" ^ fontname)); ("/FirstChar", Pdf.Integer firstchar); ("/LastChar", Pdf.Integer lastchar); - ("/Widths", Pdf.Array widths); + ("/Widths", Pdf.Array (map (fun x -> Pdf.Integer x) widths)); ("/FontDescriptor", fontdescriptor)] let addtext diff --git a/empty.pdf b/empty.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e497ec6b129cf8af06f45fdcdfc4ca5c62a84a6e GIT binary patch literal 1162 zcmah|(TdYR6x{_MWWM6Q6f0sgGs!f`QVMIc1=(G;wiU$EhcubgNXXo|Kt?K-(C?E%=Q8|2HmA>~j~|_!{y6zgWESsG{4&W* z2J>dj?3piqP-c4jQGzuJm)rkbW*Y3WT&IIebo#)~=2@SIHUWDw;?QGb|PiVQ$#(!c4s?tfo`^s{mL7wMLo@tjjbmFr(2k z{wt+>lyVU2aHW=|)MBTx=HTrPv|ndB)hii4t=Re1&UF~U%>>>6qaDBFz^iqb>pMU- z+s174{s=SuFpAn!xzQJKda%W+t>BDP=1`yd%;uD`x0F7?U!A0yQoDw#PR(Fm(W~+q z%S;V(Fj8%*q~H z=jWrVvl=a}QnzuF%Y;y14x#%8xge$$WdoC+Quw-T`Z@lm_Ej z9Kp*hQRwaRJ3x2$WazWR