%Bookmark<n> finished

This commit is contained in:
John Whitington 2024-11-11 18:49:11 +00:00
parent 1491f30094
commit a7e4a29de3
2 changed files with 22 additions and 14 deletions

View File

@ -177,22 +177,29 @@ let pagelabel pdf num =
num num
(Pdfpagelabels.complete (Pdfpagelabels.read pdf)) (Pdfpagelabels.complete (Pdfpagelabels.read pdf))
(*let debug pdf fastrefnums =
iter
(fun m -> Printf.printf "%i %s (%i)\n" m.Pdfmarks.level (Pdftext.utf8_of_pdfdocstring m.Pdfmarks.text) (Pdfpage.pagenumber_of_target ~fastrefnums pdf m.Pdfmarks.target))*)
(* Return UTF8 of current bookmark at given level at start of page. No bookmark (* Return UTF8 of current bookmark at given level at start of page. No bookmark
available = empty string. *) available = empty string.
Method: Remove from the list anything from end up to the last mark which is
at higher level. This prevents sections in an earlier chapter showing up as
bookmarks in a later chapter if no section has yet been introduced in that
chapter. Do this by reversing, then keeping everything up to any higher
level. Then re-reverse and filter to only the level required. Then, We want
the first which is on the target page or, if none, the last available. *)
let bookmark marks fastrefnums level pdf num = let bookmark marks fastrefnums level pdf num =
let before, _ = let before = takewhile (fun mark -> Pdfpage.pagenumber_of_target ~fastrefnums pdf mark.Pdfmarks.target <= num) marks in
(* 1. Pick all marks up to and including those on the needed page. *) let pickfrom = keep (fun mark -> mark.Pdfmarks.level = level) (rev (fst (cleavewhile (fun mark -> mark.Pdfmarks.level >= level) (rev before)))) in
cleavewhile (fun mark -> Pdfpage.pagenumber_of_target ~fastrefnums pdf mark.Pdfmarks.target <= num) marks let on_target_page, before_target_page = List.partition (fun mark -> Pdfpage.pagenumber_of_target ~fastrefnums pdf mark.Pdfmarks.target = num) pickfrom in
in match on_target_page with
match | h::_ -> Pdftext.utf8_of_pdfdocstring h.Pdfmarks.text
(* 2. Remove from the list anything up to the last mark which is at higher | [] ->
level. This prevents sections in an earlier chapter showing up as match before_target_page with
bookmarks in a later chapter if no section has yet been introduced in | _::_ -> Pdftext.utf8_of_pdfdocstring (last before_target_page).Pdfmarks.text
that chapter. Do this by reversing, then keeping everything up to any higher level. Then re-reverse. *) | [] -> ""
rev (fst (cleavewhile (fun mark -> mark.Pdfmarks.level = level) (rev before)))
with
| h::_ -> Pdftext.utf8_of_pdfdocstring h.Pdfmarks.text
| [] -> ""
let replace_pairs marks fastrefnums pdf endpage extract_text_font_size filename bates batespad num page = let replace_pairs marks fastrefnums pdf endpage extract_text_font_size filename bates batespad num page =
[ [

View File

@ -1,3 +1,4 @@
%Document %Bookmark<n>
%Fix up typesetting of Cpdf program name %Fix up typesetting of Cpdf program name
\documentclass{book} \documentclass{book}
% Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf, % Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf,