This commit is contained in:
John Whitington 2022-09-13 17:59:13 +01:00
parent a653020a5f
commit 550f809896
6 changed files with 18 additions and 11 deletions

View File

@ -3985,16 +3985,22 @@ let go () =
| Some TableOfContents ->
let pdf = get_single_pdf args.op false in
let font =
match args.font with StandardFont f -> f | _ -> error "TOC requires standard font only"
match args.font with
| StandardFont f -> Pdftext.StandardFont (f, Pdftext.WinAnsiEncoding)
| _ -> error "TOC requires standard font only"
in
let pdf = Cpdftoc.typeset_table_of_contents ~font ~fontsize:args.fontsize ~title:args.toc_title ~bookmark:args.toc_bookmark pdf in
write_pdf false pdf
| Some (Typeset filename) ->
let text = Pdfio.bytes_of_input_channel (open_in filename) in
let font =
match args.font with StandardFont f -> f | _ -> error "text to PDF: not a standard font"
match args.font with
| StandardFont f -> Pdftext.StandardFont (f, Pdftext.WinAnsiEncoding)
| _ -> error "text to PDF: not a standard font"
in
let pdf =
Cpdftexttopdf.typeset ~papersize:args.createpdf_pagesize ~font ~fontsize:args.fontsize text
in
let pdf = Cpdftexttopdf.typeset ~papersize:args.createpdf_pagesize ~font ~fontsize:args.fontsize text in
write_pdf false pdf
(* Advise the user if a combination of command line flags makes little sense,

View File

@ -103,7 +103,7 @@ let embed_truetype pdf ~fontfile ~fontname ~text ~encoding =
in
Pdf.addobj pdf font
(* For now, to get a Pdftext.font, we build it with the function above using an empty (i.e. full) subset, put it in an empty PDF and then read it back. This will be fixed later. *)
(* For now, to get a Pdftext.font, put it in an empty PDF and then read it back. This will be fixed later. We just need it so that existing code which uses a charcode extractor can be reused. *)
let font_of_truetype ~fontfile ~fontname ~encoding =
let pdf = Pdf.empty () in
let fontobjnum = embed_truetype pdf ~fontfile ~fontname ~text:"" ~encoding in

View File

@ -31,13 +31,14 @@ let rec of_utf8_with_newlines t =
let typeset ~papersize ~font ~fontsize text =
let pdf = Pdf.empty () in
let margin =
Pdfunits.convert 72. (Pdfpaper.unit papersize) (Pdfunits.PdfPoint) (Pdfpaper.width papersize) /. 15.
Pdfunits.convert
72. (Pdfpaper.unit papersize) (Pdfunits.PdfPoint) (Pdfpaper.width papersize) /. 15.
in
let f = (Pdftext.StandardFont (font, Pdftext.WinAnsiEncoding), fontsize) in
let pages =
Cpdftype.typeset
margin margin margin margin papersize pdf
([Cpdftype.Font f; Cpdftype.BeginDocument] @ of_utf8_with_newlines (Pdfio.string_of_bytes text))
([Cpdftype.Font (font, fontsize); Cpdftype.BeginDocument] @
of_utf8_with_newlines (Pdfio.string_of_bytes text))
in
let pdf, pageroot = Pdfpage.add_pagetree pages pdf in
Pdfpage.add_root pageroot [] pdf

View File

@ -1,4 +1,4 @@
(** Text to PDF *)
(** Typeset a text file as a PDF. *)
val typeset : papersize:Pdfpaper.t -> font:Pdftext.standard_font -> fontsize:float -> Pdfio.bytes -> Pdf.t
val typeset : papersize:Pdfpaper.t -> font:Pdftext.font -> fontsize:float -> Pdfio.bytes -> Pdf.t

View File

@ -42,8 +42,8 @@ let shorten_text widths l t =
let typeset_table_of_contents ~font ~fontsize ~title ~bookmark pdf =
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 = (Pdftext.StandardFont (font, Pdftext.WinAnsiEncoding), fontsize) in
let big = (Pdftext.StandardFont (font, Pdftext.WinAnsiEncoding), fontsize *. 2.) in
let f, fs = (font, fontsize) in
let big = (font, fontsize *. 2.) in
let firstpage = hd (Pdfpage.pages_of_pagetree pdf) in
let width, firstpage_papersize, pmaxx, pmaxy, margin =
let width, height, xmax, ymax =

View File

@ -1,4 +1,4 @@
(** Table of contents *)
(** Typeset a table of contents and prepend to the document. *)
val typeset_table_of_contents : font:Pdftext.standard_font -> fontsize:float -> title:string -> bookmark:bool -> Pdf.t -> Pdf.t
val typeset_table_of_contents : font:Pdftext.font -> fontsize:float -> title:string -> bookmark:bool -> Pdf.t -> Pdf.t