cpdf-source/cpdftoc.ml

217 lines
8.5 KiB
OCaml
Raw Normal View History

2021-12-07 00:46:52 +01:00
open Pdfutil
2021-12-11 06:51:05 +01:00
(* We allow \n in titles. Split for typesetter. *)
2021-12-07 04:45:10 +01:00
let rec split_toc_title_inner a = function
| '\\'::'n'::r -> rev a :: split_toc_title_inner [] r
| x::xs -> split_toc_title_inner (x::a) xs
2021-12-07 00:46:52 +01:00
| [] -> [rev a]
2021-12-07 04:45:10 +01:00
let split_toc_title = split_toc_title_inner []
2021-12-11 06:51:05 +01:00
(* And for new bookmark for TOC, change \\n to \n *)
let rec real_newline = function
| '\\'::'n'::r -> '\n'::real_newline r
| x::r -> x::real_newline r
| [] -> []
2023-07-19 17:09:25 +02:00
let width_table_cache = null_hash ()
2023-07-12 14:23:57 +02:00
let rec width_of_runs runs =
match runs with
2023-07-20 13:56:20 +02:00
| Cpdftype.Font (id, f, fontsize)::Cpdftype.Text t::more ->
2023-07-19 17:09:25 +02:00
let width_table =
2023-07-20 13:56:20 +02:00
match Hashtbl.find width_table_cache (id, fontsize) with
2023-07-19 17:09:25 +02:00
| w -> w
2023-07-20 13:56:20 +02:00
| exception Not_found ->
let ws = Cpdftype.font_widths id f fontsize in Hashtbl.add width_table_cache (id, fontsize) ws; ws
2023-07-19 17:09:25 +02:00
in
Cpdftype.width_of_string width_table t +. width_of_runs more
2023-07-12 14:23:57 +02:00
| [] -> 0.
| _ -> failwith "width_of_runs"
(* Run of Font / Text elements from a fontpack and UTF8 text *)
let of_utf8 fontpack fontsize t =
let codepoints = Pdftext.codepoints_of_utf8 t in
let fonted = option_map (Cpdfembed.get_char fontpack) codepoints in
let collated = Cpdfembed.collate_runs fonted in
2023-07-11 17:53:21 +02:00
flatten
(map
(function
| [] -> []
| (_, _, font) as h::t ->
let charcodes = map (fun (c, _, _) -> char_of_int c) (h::t) in
2023-07-20 13:56:20 +02:00
(*FIXME id *)
[Cpdftype.Font ("", font, fontsize); Cpdftype.Text charcodes])
2023-07-11 17:53:21 +02:00
collated)
2021-12-07 00:46:52 +01:00
2021-12-07 04:45:10 +01:00
(* Cpdftype codepoints from a font and PDFDocEndoding string *)
let of_pdfdocencoding fontpack fontsize t =
of_utf8 fontpack fontsize (Pdftext.utf8_of_pdfdocstring t)
2021-12-07 00:46:52 +01:00
2022-01-09 14:46:41 +01:00
(* Remove characters until it is below the length. Then remove three more and
add dots for an ellipsis *)
2023-07-12 15:38:36 +02:00
let rec shorten_text_inner l t =
match rev t with
2023-07-20 13:56:20 +02:00
| Cpdftype.Text text::Cpdftype.Font (id, f, fs)::more ->
2023-07-19 17:09:25 +02:00
let width_table =
2023-07-20 13:56:20 +02:00
match Hashtbl.find width_table_cache (id, fs) with
2023-07-19 17:09:25 +02:00
| w -> w
2023-07-20 13:56:20 +02:00
| exception Not_found ->
let ws = Cpdftype.font_widths id f fs in Hashtbl.add width_table_cache (id, fs) ws; ws
2023-07-19 17:09:25 +02:00
in
if Cpdftype.width_of_string width_table text > l then
2023-07-20 13:56:20 +02:00
shorten_text_inner l (rev (Cpdftype.Text (all_but_last text)::Cpdftype.Font (id, f, fs)::more))
2023-07-12 15:38:36 +02:00
else
t
| _ -> t
2022-01-09 14:46:41 +01:00
2023-07-12 15:38:36 +02:00
let shorten_text fontpack fontsize l t =
let short = shorten_text_inner l t in
if short = t then t else
let charcode, _, dotfont =
unopt (Cpdfembed.get_char fontpack (int_of_char '.'))
in
let charcode = char_of_int charcode in
2023-07-20 13:56:20 +02:00
(* FIXME ID *)
short @ [Cpdftype.Font ("", dotfont, fontsize); Cpdftype.Text [charcode; charcode; charcode]]
2022-01-09 14:46:41 +01:00
(* Calculate the used codepoints *)
let used pdf fastrefnums labels title marks =
let codepoints = null_hash () in
Hashtbl.add codepoints (int_of_char '.') ();
let addtext t =
iter
(fun c -> Hashtbl.replace codepoints c ())
(Pdftext.codepoints_of_utf8 (Pdftext.utf8_of_pdfdocstring t))
in
iter (fun c -> Hashtbl.replace codepoints c ()) (Pdftext.codepoints_of_utf8 title);
iter
(fun m ->
addtext m.Pdfmarks.text;
let pnum = Pdfpage.pagenumber_of_target ~fastrefnums pdf m.Pdfmarks.target in
let labeltext =
try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> string_of_int pnum
in
addtext labeltext)
marks;
codepoints
2021-12-07 04:45:10 +01:00
(* Typeset a table of contents with given font, font size and title. Mediabox
2021-12-14 13:57:27 +01:00
(and CropBox) copied from first page of existing PDF. Margin of 10% inside
2021-12-07 04:45:10 +01:00
CropBox. Font size of title twice body font size. Null page labels added for
TOC, others bumped up and so preserved. *)
2022-10-19 16:34:19 +02:00
let typeset_table_of_contents ~font ~fontsize ~title ~bookmark pdf =
let marks = Pdfmarks.read_bookmarks pdf in
if marks = [] then (Pdfe.log "No bookmarks, not making table of contents\n"; pdf) else
let labels = Pdfpagelabels.read pdf in
let refnums = Pdf.page_reference_numbers pdf in
let fastrefnums = hashtable_of_dictionary (combine refnums (indx refnums)) in
let codepoints = map fst (list_of_hashtbl (used pdf fastrefnums labels title marks)) in
let fontpack =
match font with
| Cpdfembed.PreMadeFontPack t -> t
| Cpdfembed.EmbedInfo {fontfile; fontname; encoding} ->
Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding
| Cpdfembed.ExistingNamedFont -> raise (Pdf.PDFError "Cannot use existing font with -table-of-contents")
in
2021-12-07 00:46:52 +01:00
let firstpage = hd (Pdfpage.pages_of_pagetree pdf) in
2021-12-14 13:57:27 +01:00
let width, firstpage_papersize, pmaxx, pmaxy, margin =
2021-12-07 00:46:52 +01:00
let width, height, xmax, ymax =
2022-07-14 15:06:25 +02:00
match Pdf.parse_rectangle pdf firstpage.Pdfpage.mediabox with
2021-12-07 00:46:52 +01:00
xmin, ymin, xmax, ymax -> xmax -. xmin, ymax -. ymin, xmax, ymax
in
2021-12-14 13:57:27 +01:00
width, Pdfpaper.make Pdfunits.PdfPoint width height, xmax, ymax, fmin width height *. 0.1
2021-12-07 00:46:52 +01:00
in
let firstpage_cropbox =
match Pdf.lookup_direct pdf "/CropBox" firstpage.Pdfpage.rest with
2022-07-14 15:06:25 +02:00
| Some r -> Some (Pdf.parse_rectangle pdf r)
2021-12-07 00:46:52 +01:00
| None -> None
in
2023-07-12 13:49:03 +02:00
let width =
2021-12-14 13:57:27 +01:00
match firstpage_cropbox with
| Some (xmin, _, xmax, _) -> xmax -. xmin
| None -> width
2023-07-12 13:49:03 +02:00
in
2021-12-07 00:46:52 +01:00
let lines =
map
(fun mark ->
2021-12-14 13:57:27 +01:00
let indent = float mark.Pdfmarks.level *. fontsize *. 2. in
let textruns = of_pdfdocencoding fontpack fontsize mark.Pdfmarks.text in
let labelruns =
if mark.Pdfmarks.target = NullDestination then of_pdfdocencoding fontpack fontsize "" else
2021-12-14 13:57:27 +01:00
let pde =
let pnum = Pdfpage.pagenumber_of_target ~fastrefnums pdf mark.Pdfmarks.target in
try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> string_of_int pnum
in
of_pdfdocencoding fontpack fontsize pde
2021-12-14 13:57:27 +01:00
in
2023-07-12 14:23:57 +02:00
let textgap = width -. margin *. 2. -. indent -. width_of_runs labelruns in
2023-07-12 15:38:36 +02:00
let textruns = shorten_text fontpack fontsize (textgap -. fontsize *. 3.) textruns in
2023-07-12 14:23:57 +02:00
let space = textgap -. width_of_runs textruns in
2021-12-07 00:46:52 +01:00
[Cpdftype.BeginDest mark.Pdfmarks.target;
Cpdftype.HGlue indent]
@ textruns @
[Cpdftype.HGlue space]
@ labelruns @
[Cpdftype.EndDest;
2021-12-07 00:46:52 +01:00
Cpdftype.NewLine])
(Pdfmarks.read_bookmarks pdf)
in
let toc_pages =
2023-07-12 13:49:03 +02:00
let title =
2023-07-10 15:36:34 +02:00
let glue = Cpdftype.VGlue (fontsize *. 2.) in
2021-12-28 13:03:46 +01:00
if title = "" then [] else
flatten
(map
2023-07-12 13:49:03 +02:00
(fun l -> l @ [Cpdftype.NewLine])
(map (of_utf8 fontpack (fontsize *. 2.)) (map implode (split_toc_title (explode title)))))
2021-12-28 13:03:46 +01:00
@ [glue]
2023-07-12 13:49:03 +02:00
in
2021-12-07 00:46:52 +01:00
let lm, rm, tm, bm =
match firstpage_cropbox with
2021-12-14 12:36:21 +01:00
| None -> (margin, margin, margin, margin)
2021-12-07 00:46:52 +01:00
| Some (cminx, cminy, cmaxx, cmaxy) ->
2021-12-14 12:36:21 +01:00
(cminx +. margin, (pmaxx -. cmaxx) +. margin, cminy +. margin, (pmaxy -. cmaxy) +. margin)
2022-09-23 20:06:07 +02:00
in
let firstfont =
2023-07-12 13:49:03 +02:00
hd (keep (function Cpdftype.Font _ -> true | _ -> false) (title @ flatten lines))
in
Cpdftype.typeset lm rm tm bm firstpage_papersize pdf
2023-07-12 13:49:03 +02:00
([firstfont; Cpdftype.BeginDocument] @ title @ flatten lines)
2021-12-07 00:46:52 +01:00
in
let toc_pages =
match firstpage_cropbox with
| Some (a, b, c, d) ->
let rect =
Pdf.Array [Pdf.Real a; Pdf.Real b; Pdf.Real c; Pdf.Real d]
in
map
(fun p -> {p with Pdfpage.rest = Pdf.add_dict_entry p.Pdfpage.rest "/CropBox" rect})
toc_pages
| None -> toc_pages
in
let original_pages = Pdfpage.pages_of_pagetree pdf in
let toc_pages_len = length toc_pages in
let changes = map (fun n -> (n, n + toc_pages_len)) (indx original_pages) in
let pdf = Pdfpage.change_pages ~changes true pdf (toc_pages @ original_pages) in
let label =
{Pdfpagelabels.labelstyle = NoLabelPrefixOnly;
Pdfpagelabels.labelprefix = None;
Pdfpagelabels.startpage = 1;
Pdfpagelabels.startvalue = 1}
in
let labels' = label::map (fun l -> {l with Pdfpagelabels.startpage = l.Pdfpagelabels.startpage + toc_pages_len}) labels in
Pdfpagelabels.write pdf labels';
2021-12-10 13:58:30 +01:00
if bookmark then
let marks = Pdfmarks.read_bookmarks pdf in
let refnums = Pdf.page_reference_numbers pdf in
let newmark =
{Pdfmarks.level = 0;
2021-12-11 06:51:05 +01:00
Pdfmarks.text = Pdftext.pdfdocstring_of_utf8 (implode (real_newline (explode title)));
2021-12-10 13:58:30 +01:00
Pdfmarks.target = Pdfdest.XYZ (Pdfdest.PageObject (hd refnums), None, None, None);
Pdfmarks.isopen = false}
in
Pdfmarks.add_bookmarks (newmark::marks) pdf
else
pdf