This commit is contained in:
John Whitington 2021-12-06 15:55:46 -08:00
parent 9f776cb6c6
commit 9516784ece
5 changed files with 45 additions and 44 deletions

View File

@ -1,7 +1,7 @@
# Build the cpdf command line tools and top level # Build the cpdf command line tools and top level
MODS = cpdfyojson cpdfxmlm \ MODS = cpdfyojson cpdfxmlm \
cpdfunicodedata cpdferror cpdfjson cpdfstrftime cpdfcoord cpdfattach \ cpdfunicodedata cpdferror cpdfjson cpdfstrftime cpdfcoord cpdfattach \
cpdfpagespec cpdfposition cpdf cpdffont cpdftype cpdftoc cpdfcommand cpdfpagespec cpdfposition cpdf cpdffont cpdftype cpdftexttopdf cpdftoc cpdfcommand
SOURCES = $(foreach x,$(MODS),$(x).ml $(x).mli) cpdfcommandrun.ml SOURCES = $(foreach x,$(MODS),$(x).ml $(x).mli) cpdfcommandrun.ml

View File

@ -2918,47 +2918,6 @@ let collate (names, pdfs, ranges) =
done; done;
split3 (rev !nis) split3 (rev !nis)
let of_utf8 (f, fontsize) t =
Pdftext.codepoints_of_utf8 t
|> option_map (Pdftext.charcode_extractor_of_font_real f)
|> map char_of_int
|> implode
let of_pdfdocencoding (f, fontsize) t =
of_utf8 (f, fontsize) (Pdftext.utf8_of_pdfdocstring t)
let rec of_utf8_with_newlines t =
let items = ref [] in
let buf = Buffer.create 256 in
String.iter
(function
| '\n' ->
let c = Buffer.contents buf in
if c <> "" then items := Cpdftype.Text (explode c)::!items;
items := Cpdftype.NewLine::!items;
Buffer.clear buf
| x ->
Buffer.add_char buf x)
t;
(* Do last one *)
let c = Buffer.contents buf in
if c <> "" then items := Text (explode c)::!items;
rev !items
(* FIXME margins, hyphenation of too-long words, efficiency *)
let typeset text =
let pdf = Pdf.empty () in
let f =
(begin match args.font with StandardFont sf -> Pdftext.StandardFont (sf, Pdftext.WinAnsiEncoding) | _ -> failwith "typeset bad font" end,
args.fontsize)
in
let pages =
Cpdftype.typeset
20. 20. 20. 20. Pdfpaper.a4 pdf ([Cpdftype.Font f] @ of_utf8_with_newlines (string_of_bytes text))
in
let pdf, pageroot = Pdfpage.add_pagetree pages pdf in
Pdfpage.add_root pageroot [] pdf
(* Main function *) (* Main function *)
let go () = let go () =
match args.op with match args.op with
@ -3888,7 +3847,10 @@ let go () =
write_pdf false pdf write_pdf false pdf
| Some (Typeset filename) -> | Some (Typeset filename) ->
let text = Pdfio.bytes_of_input_channel (open_in filename) in let text = Pdfio.bytes_of_input_channel (open_in filename) in
let pdf = typeset text in let font =
match args.font with StandardFont f -> f | _ -> error "text to PDF: not a standard font"
in
let pdf = Cpdftexttopdf.typeset ~font ~fontsize:args.fontsize text in
write_pdf false pdf write_pdf false pdf
(* Advise the user if a combination of command line flags makes little sense, (* Advise the user if a combination of command line flags makes little sense,

39
cpdftexttopdf.ml Normal file
View File

@ -0,0 +1,39 @@
open Pdfutil
let of_utf8 (f, fontsize) t =
Pdftext.codepoints_of_utf8 t
|> option_map (Pdftext.charcode_extractor_of_font_real f)
|> map char_of_int
|> implode
let of_pdfdocencoding (f, fontsize) t =
of_utf8 (f, fontsize) (Pdftext.utf8_of_pdfdocstring t)
let rec of_utf8_with_newlines t =
let items = ref [] in
let buf = Buffer.create 256 in
String.iter
(function
| '\n' ->
let c = Buffer.contents buf in
if c <> "" then items := Cpdftype.Text (explode c)::!items;
items := Cpdftype.NewLine::!items;
Buffer.clear buf
| x ->
Buffer.add_char buf x)
t;
(* Do last one *)
let c = Buffer.contents buf in
if c <> "" then items := Text (explode c)::!items;
rev !items
(* FIXME margins, hyphenation of too-long words, efficiency *)
let typeset ~font ~fontsize text =
let pdf = Pdf.empty () in
let f = (Pdftext.StandardFont (font, Pdftext.WinAnsiEncoding), fontsize) in
let pages =
Cpdftype.typeset
20. 20. 20. 20. Pdfpaper.a4 pdf ([Cpdftype.Font f] @ of_utf8_with_newlines (Pdfio.string_of_bytes text))
in
let pdf, pageroot = Pdfpage.add_pagetree pages pdf in
Pdfpage.add_root pageroot [] pdf

1
cpdftexttopdf.mli Normal file
View File

@ -0,0 +1 @@
val typeset : font:Pdftext.standard_font -> fontsize:float -> Pdfio.bytes -> Pdf.t

View File

@ -93,4 +93,3 @@ let typeset_table_of_contents ~font ~fontsize ~title pdf =
let labels' = label::map (fun l -> {l with Pdfpagelabels.startpage = l.Pdfpagelabels.startpage + toc_pages_len}) labels in let labels' = label::map (fun l -> {l with Pdfpagelabels.startpage = l.Pdfpagelabels.startpage + toc_pages_len}) labels in
Pdfpagelabels.write pdf labels'; Pdfpagelabels.write pdf labels';
pdf pdf