From b4c07ea798f9fa87025473f3a08f5bb20d5302cd Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 19 Oct 2022 15:34:19 +0100 Subject: [PATCH] more --- cpdfcommand.ml | 7 +++---- cpdfembed.mli | 6 ++++++ cpdftoc.ml | 18 ++++++++++-------- cpdftoc.mli | 2 +- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 603c4af..77f95cf 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -4007,14 +4007,13 @@ let go () = let pdf = get_single_pdf args.op true in Cpdffont.print_font_table pdf fontname args.copyfontpage | Some TableOfContents -> - (*let pdf = get_single_pdf args.op false in + let pdf = get_single_pdf args.op false in let cpdffont = embed_font () in let pdf = Cpdftoc.typeset_table_of_contents - cpdffont ~fontsize:args.fontsize ~title:args.toc_title ~bookmark:args.toc_bookmark pdf + ~font:cpdffont ~fontsize:args.fontsize ~title:args.toc_title ~bookmark:args.toc_bookmark pdf in - write_pdf false pdf*) - () + write_pdf false pdf | Some (Typeset filename) -> let text = Pdfio.bytes_of_input_channel (open_in filename) in let cpdffont = embed_font () in diff --git a/cpdfembed.mli b/cpdfembed.mli index 3b3fdcb..1897371 100644 --- a/cpdfembed.mli +++ b/cpdfembed.mli @@ -2,6 +2,12 @@ encoding, adding the fontfiles to the PDF and returning a list of font objects, together with a unicode codepoint --> (font number in list, charcode) table *) +(* FIXME: Really we want to create an interactive fontpack which creates fonts + when needed, but delays the actual production of the subset truetype data + until later. This will mean we don't need to pre-calculate the USED set. For + now, we just hack Cpdftoc, cpdfaddtext and cpdftextofpdf to pre-calculate + the subset. *) + type t = Pdftext.font list * (int, int * int) Hashtbl.t type cpdffont = diff --git a/cpdftoc.ml b/cpdftoc.ml index 76bac91..303a97a 100644 --- a/cpdftoc.ml +++ b/cpdftoc.ml @@ -41,7 +41,15 @@ let shorten_text widths l t = (and CropBox) copied from first page of existing PDF. Margin of 10% inside CropBox. Font size of title twice body font size. Null page labels added for TOC, others bumped up and so preserved. *) -let typeset_table_of_contents ?embedinfo ~font ~fontsize ~title ~bookmark pdf = +let typeset_table_of_contents ~font ~fontsize ~title ~bookmark pdf = + let codepoints = [] (* FIXME *) in + let font = + match font with + | Cpdfembed.PreMadeFontPack t -> hd (fst t) + | Cpdfembed.EmbedInfo {fontfile; fontname; encoding} -> + hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding)) + | Cpdfembed.ExistingNamedFont _ -> raise (Pdf.PDFError "Cannot use existing font with -table-of-contents") + in let marks = Pdfmarks.read_bookmarks pdf in if marks = [] then (Printf.eprintf "No bookmarks, not making table of contents\n%!"; pdf) else let f, fs = (font, fontsize) in @@ -110,13 +118,7 @@ let typeset_table_of_contents ?embedinfo ~font ~fontsize ~title ~bookmark pdf = | Some (cminx, cminy, cmaxx, cmaxy) -> (cminx +. margin, (pmaxx -. cmaxx) +. margin, cminy +. margin, (pmaxy -. cmaxy) +. margin) in - let codepoints = map fst (list_of_hashtbl used) in - let font = - match embedinfo with - | None -> font - | Some (pdf, fontfile, fontname, encoding) -> - hd (fst (Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding)) - in + Cpdftype.typeset lm rm tm bm firstpage_papersize pdf ([Cpdftype.Font (font, bfs); Cpdftype.BeginDocument] @ title @ [Cpdftype.Font (font, fs)] @ flatten lines) diff --git a/cpdftoc.mli b/cpdftoc.mli index 49b449e..afe71bd 100644 --- a/cpdftoc.mli +++ b/cpdftoc.mli @@ -1,4 +1,4 @@ (** Table of contents *) (** Typeset a table of contents and prepend to the document. *) -val typeset_table_of_contents : ?embedinfo:(Pdf.t * Pdfio.bytes * string * Pdftext.encoding) -> font:Pdftext.font -> fontsize:float -> title:string -> bookmark:bool -> Pdf.t -> Pdf.t +val typeset_table_of_contents : font:Cpdfembed.cpdffont -> fontsize:float -> title:string -> bookmark:bool -> Pdf.t -> Pdf.t