2021-12-07 00:55:46 +01:00
|
|
|
open Pdfutil
|
|
|
|
|
2022-10-19 15:36:26 +02:00
|
|
|
(* Return set of unicode characters in this text *)
|
|
|
|
let used_characters t =
|
|
|
|
let codepoints = Pdftext.codepoints_of_utf8 t in
|
|
|
|
setify codepoints
|
|
|
|
|
2022-10-20 15:28:14 +02:00
|
|
|
let rec of_utf8_with_newlines fontpack fontsize t =
|
2021-12-07 00:55:46 +01:00
|
|
|
let items = ref [] in
|
2022-10-20 15:28:14 +02:00
|
|
|
let currfont = ref 0 in
|
2022-09-21 16:21:57 +02:00
|
|
|
let codepoints = Pdftext.codepoints_of_utf8 t in
|
2022-10-20 15:28:14 +02:00
|
|
|
let currtext = ref [] in
|
|
|
|
let process_codepoints cs =
|
|
|
|
iter
|
2022-09-21 16:21:57 +02:00
|
|
|
(fun u ->
|
2022-10-19 15:47:20 +02:00
|
|
|
match Cpdfembed.get_char fontpack u with
|
2022-10-20 15:28:14 +02:00
|
|
|
| Some (c, n, f) ->
|
2022-10-20 16:06:49 +02:00
|
|
|
(*Printf.printf "Charcode %i, font number %i\n" c n;*)
|
2022-10-20 15:28:14 +02:00
|
|
|
begin if n <> !currfont then
|
|
|
|
begin
|
|
|
|
if !currtext <> [] then items := Cpdftype.Text (rev !currtext)::!items;
|
|
|
|
currtext := [];
|
|
|
|
currfont := n;
|
|
|
|
items := Cpdftype.Font (f, fontsize)::!items;
|
|
|
|
currtext := char_of_int c::!currtext;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
currtext := char_of_int c::!currtext
|
|
|
|
end
|
|
|
|
| None -> Printf.printf "No glyph for unicode U+%04X in this font\n" u)
|
|
|
|
cs;
|
|
|
|
items := Cpdftype.Text (rev !currtext)::!items
|
2022-09-21 16:21:57 +02:00
|
|
|
in
|
2022-10-20 15:28:14 +02:00
|
|
|
let buf = ref [] in
|
2022-09-21 16:21:57 +02:00
|
|
|
List.iter
|
2021-12-07 00:55:46 +01:00
|
|
|
(function
|
2022-09-21 16:21:57 +02:00
|
|
|
| 10 (*'\n'*) ->
|
|
|
|
let c = rev !buf in
|
2022-10-20 15:28:14 +02:00
|
|
|
if c <> [] then process_codepoints c;
|
2021-12-07 00:55:46 +01:00
|
|
|
items := Cpdftype.NewLine::!items;
|
2022-09-21 16:21:57 +02:00
|
|
|
buf := []
|
|
|
|
| 13 (*'\r'*) -> ()
|
2021-12-07 00:55:46 +01:00
|
|
|
| x ->
|
2022-09-21 16:21:57 +02:00
|
|
|
buf := x::!buf)
|
|
|
|
codepoints;
|
2021-12-07 00:55:46 +01:00
|
|
|
(* Do last one *)
|
2022-09-21 16:21:57 +02:00
|
|
|
let c = rev !buf in
|
2022-10-20 15:28:14 +02:00
|
|
|
if c <> [] then process_codepoints c;
|
2022-09-21 16:21:57 +02:00
|
|
|
rev !items
|
2021-12-07 00:55:46 +01:00
|
|
|
|
2022-10-19 14:48:13 +02:00
|
|
|
let typeset ~papersize ~font ~fontsize text =
|
|
|
|
let pdf = Pdf.empty () in
|
2022-10-19 15:36:26 +02:00
|
|
|
let codepoints = used_characters (Pdfio.string_of_bytes text) in
|
|
|
|
let font, fontpack =
|
2022-10-19 14:48:13 +02:00
|
|
|
match font with
|
2022-10-19 15:36:26 +02:00
|
|
|
| Cpdfembed.PreMadeFontPack t -> (hd (fst t), t)
|
2022-10-19 14:48:13 +02:00
|
|
|
| Cpdfembed.EmbedInfo {fontfile; fontname; encoding} ->
|
2022-10-19 15:36:26 +02:00
|
|
|
let embedded = Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints ~encoding in
|
|
|
|
(hd (fst embedded), embedded)
|
2022-10-19 17:45:52 +02:00
|
|
|
| Cpdfembed.ExistingNamedFont -> raise (Pdf.PDFError "Can't use existing named font for text-to-PDF")
|
2022-10-19 14:48:13 +02:00
|
|
|
in
|
2022-10-20 15:28:14 +02:00
|
|
|
let instrs = of_utf8_with_newlines fontpack fontsize (Pdfio.string_of_bytes text) in
|
2021-12-29 16:58:03 +01:00
|
|
|
let margin =
|
2023-04-11 14:50:17 +02:00
|
|
|
Pdfunits.points (Pdfpaper.width papersize) (Pdfpaper.unit papersize) /. 15.
|
2021-12-29 16:58:03 +01:00
|
|
|
in
|
2022-10-20 15:28:14 +02:00
|
|
|
let instrs = [Cpdftype.Font (font, fontsize); Cpdftype.BeginDocument] @ instrs in
|
2022-10-20 16:06:49 +02:00
|
|
|
(*Printf.printf "to_string: %s\n" (Cpdftype.to_string instrs);*)
|
2022-10-20 15:28:14 +02:00
|
|
|
let pages = Cpdftype.typeset margin margin margin margin papersize pdf instrs in
|
2021-12-07 00:55:46 +01:00
|
|
|
let pdf, pageroot = Pdfpage.add_pagetree pages pdf in
|
2022-10-20 16:06:49 +02:00
|
|
|
let pdf = Pdfpage.add_root pageroot [] pdf in
|
|
|
|
(*Pdfwrite.debug_whole_pdf pdf;*)
|
|
|
|
pdf
|