Get toc back up and running with ttf (tbc)

This commit is contained in:
John Whitington 2023-07-11 16:27:46 +01:00
parent 00acc7fe80
commit 5ec46e5140
2 changed files with 19 additions and 18 deletions

View File

@ -15,18 +15,19 @@ let rec real_newline = function
| [] -> [] | [] -> []
(* Run of Font / Text elements from a fontpack and UTF8 text *) (* Run of Font / Text elements from a fontpack and UTF8 text *)
let of_utf8 fontpack t = let of_utf8 fontpack fontsize t =
let font = ref 0 in
[]
(* Use the fontpack to find which font is required, then return runs (* Use the fontpack to find which font is required, then return runs
of [Font (font, size); Text t] in a list *) of [Font (font, size); Text t] in a list *)
(* Pdftext.codepoints_of_utf8 t let codepoints = Pdftext.codepoints_of_utf8 t in
|> option_map (Pdftext.charcode_extractor_of_font_real f) (* 1. Text to (charcode, font, fontnum) *)
|> map char_of_int*) let fonted = option_map (Cpdfembed.get_char fontpack) codepoints in
(* 2. FIXME Collate them *)
(* 3. Produce the runs, converting to char *)
flatten (map (function (charcode, fontnum, font) -> [Cpdftype.Font (font, fontsize); Cpdftype.Text [char_of_int charcode]]) fonted)
(* Cpdftype codepoints from a font and PDFDocEndoding string *) (* Cpdftype codepoints from a font and PDFDocEndoding string *)
let of_pdfdocencoding f t = let of_pdfdocencoding fontpack fontsize t =
of_utf8 f (Pdftext.utf8_of_pdfdocstring t) of_utf8 fontpack fontsize (Pdftext.utf8_of_pdfdocstring t)
(* Remove characters until it is below the length. Then remove three more and (* Remove characters until it is below the length. Then remove three more and
add dots for an ellipsis *) add dots for an ellipsis *)
@ -100,14 +101,14 @@ let typeset_table_of_contents ~font ~fontsize ~title ~bookmark pdf =
map map
(fun mark -> (fun mark ->
let indent = float mark.Pdfmarks.level *. fontsize *. 2. in let indent = float mark.Pdfmarks.level *. fontsize *. 2. in
let textruns = of_pdfdocencoding fontpack mark.Pdfmarks.text in let textruns = of_pdfdocencoding fontpack fontsize mark.Pdfmarks.text in
let labelruns = let labelruns =
if mark.Pdfmarks.target = NullDestination then of_pdfdocencoding fontpack "" else if mark.Pdfmarks.target = NullDestination then of_pdfdocencoding fontpack fontsize "" else
let pde = let pde =
let pnum = Pdfpage.pagenumber_of_target ~fastrefnums pdf mark.Pdfmarks.target in 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 try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> string_of_int pnum
in in
of_pdfdocencoding fontpack pde of_pdfdocencoding fontpack fontsize pde
in in
(*let widths = Cpdftype.font_widths f fontsize in (*let widths = Cpdftype.font_widths f fontsize in
let textgap = width -. margin *. 2. -. indent -. Cpdftype.width_of_string widths label in*) let textgap = width -. margin *. 2. -. indent -. Cpdftype.width_of_string widths label in*)
@ -123,15 +124,15 @@ let typeset_table_of_contents ~font ~fontsize ~title ~bookmark pdf =
(Pdfmarks.read_bookmarks pdf) (Pdfmarks.read_bookmarks pdf)
in in
let toc_pages = let toc_pages =
let title = (*let title =
let glue = Cpdftype.VGlue (fontsize *. 2.) in let glue = Cpdftype.VGlue (fontsize *. 2.) in
if title = "" then [] else if title = "" then [] else
flatten flatten
(map (map
(fun l -> [Cpdftype.Text l; Cpdftype.NewLine]) (fun l -> [Cpdftype.Text l; Cpdftype.NewLine])
(split_toc_title (of_utf8 fontpack title))) (split_toc_title (of_utf8 fontpack (fontsize *. 2.) title)))
@ [glue] @ [glue]
in in*)
let lm, rm, tm, bm = let lm, rm, tm, bm =
match firstpage_cropbox with match firstpage_cropbox with
| None -> (margin, margin, margin, margin) | None -> (margin, margin, margin, margin)
@ -139,10 +140,10 @@ let typeset_table_of_contents ~font ~fontsize ~title ~bookmark pdf =
(cminx +. margin, (pmaxx -. cmaxx) +. margin, cminy +. margin, (pmaxy -. cmaxy) +. margin) (cminx +. margin, (pmaxx -. cmaxx) +. margin, cminy +. margin, (pmaxy -. cmaxy) +. margin)
in in
let firstfont = let firstfont =
hd (keep (function Cpdftype.Font _ -> true | _ -> false) (title @ flatten lines)) (*FIXME when title ok *) hd (keep (function Cpdftype.Font _ -> true | _ -> false) ((*title @ *)flatten lines)) (*FIXME when title ok *)
in in
Cpdftype.typeset lm rm tm bm firstpage_papersize pdf Cpdftype.typeset lm rm tm bm firstpage_papersize pdf
([firstfont; Cpdftype.BeginDocument] @ title @ flatten lines) ([firstfont; Cpdftype.BeginDocument] @ (*title @*) flatten lines)
in in
let toc_pages = let toc_pages =
match firstpage_cropbox with match firstpage_cropbox with

View File

@ -5,8 +5,8 @@ open Pdfio
(* FIXME Base on bytes not bits - all uses of mk_b *) (* FIXME Base on bytes not bits - all uses of mk_b *)
let dbg = ref false let dbg = ref false
let _ = (*let _ =
Pdfe.logger := (fun s -> print_string s; flush stdout) Pdfe.logger := (fun s -> print_string s; flush stdout)*)
type t = type t =
{flags : int; {flags : int;