From 71adabd103fd862e54254e6b639e8bf9cc1aef31 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 2 Dec 2024 18:39:29 +0000 Subject: [PATCH] Scaffolding for dot leaders in TOC --- cpdfcommand.ml | 14 ++++++++++---- cpdftoc.ml | 9 +++++++-- cpdftoc.mli | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index d8e1236..c6d3c0d 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -571,7 +571,8 @@ type args = mutable rast_jpeg_quality : int; mutable rast_downsample : bool; mutable replace_stream_with : string; - mutable output_unit : Pdfunits.t} + mutable output_unit : Pdfunits.t; + mutable dot_leader : bool} let args = {op = None; @@ -720,7 +721,8 @@ let args = rast_jpeg_quality = 75; rast_downsample = false; replace_stream_with = ""; - output_unit = Pdfunits.PdfPoint} + output_unit = Pdfunits.PdfPoint; + dot_leader = false} (* Do not reset original_filename or cpdflin or was_encrypted or was_decrypted_with_owner or recrypt or producer or creator or path_to_* or @@ -855,7 +857,8 @@ let reset_arguments () = args.rast_jpeg_quality <- 75; args.rast_downsample <- false; args.replace_stream_with <- ""; - args.output_unit <- Pdfunits.PdfPoint + args.output_unit <- Pdfunits.PdfPoint; + args.dot_leader <- false (* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume installed at a system place *) @@ -2919,6 +2922,9 @@ let specs = ("-toc-no-bookmark", Arg.Unit settocnobookmark, " Don't add the table of contents to the bookmarks"); + ("-toc-dot-leader", + Arg.Unit (fun () -> args.dot_leader <- true), + " Add a dot leader to TOC entries"); ("-typeset", Arg.String settypeset, " Typeset a text file as a PDF"); @@ -4802,7 +4808,7 @@ let go () = let cpdffont = embed_font () in let pdf = Cpdftoc.typeset_table_of_contents - ~font: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 ~dotleader:args.dot_leader pdf in write_pdf false pdf | Some (Typeset filename) -> diff --git a/cpdftoc.ml b/cpdftoc.ml index 63204ca..7af48cb 100644 --- a/cpdftoc.ml +++ b/cpdftoc.ml @@ -94,11 +94,16 @@ let used pdf fastrefnums labels title marks = marks; codepoints +(* Fill the space with a small space, maybe some dots, then maybe a final tiny space *) +(* TODO Test to make sure a title shortened with dots does not gain any leader dots through this mechanism *) +let make_dots space = + [Cpdftype.HGlue space] + (* Typeset a table of contents with given font, font size and title. Mediabox (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 ~font ~fontsize ~title ~bookmark pdf = +let typeset_table_of_contents ~font ~fontsize ~title ~bookmark ~dotleader pdf = Hashtbl.clear width_table_cache; let marks = Pdfmarks.read_bookmarks pdf in if marks = [] then (Pdfe.log "No bookmarks, not making table of contents\n"; pdf) else @@ -150,7 +155,7 @@ let typeset_table_of_contents ~font ~fontsize ~title ~bookmark pdf = [Cpdftype.BeginDest mark.Pdfmarks.target; Cpdftype.HGlue indent] @ textruns @ - [Cpdftype.HGlue space] + (if dotleader then make_dots space else [Cpdftype.HGlue space]) @ labelruns @ [Cpdftype.EndDest; Cpdftype.NewLine]) diff --git a/cpdftoc.mli b/cpdftoc.mli index afe71bd..bc8509a 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 : font:Cpdfembed.cpdffont -> fontsize:float -> title:string -> bookmark:bool -> Pdf.t -> Pdf.t +val typeset_table_of_contents : font:Cpdfembed.cpdffont -> fontsize:float -> title:string -> bookmark:bool -> dotleader:bool -> Pdf.t -> Pdf.t