From 3b54c94538b8f090be52dfbf4e912125d75ce911 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Thu, 18 Nov 2021 11:09:09 -0800 Subject: [PATCH] Beginning typesetter --- cpdftype.ml | 66 ++++++++++++++++++++++++++++++++++++++++++---------- cpdftype.mli | 25 +++++++++++--------- 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/cpdftype.ml b/cpdftype.ml index 6f0509e..c807caf 100644 --- a/cpdftype.ml +++ b/cpdftype.ml @@ -1,15 +1,57 @@ -(* cpdftype.ml *) -type box = - Text of string -| Glue of float -| Newline -| Font of string -| BoldOn -| BoldOff -| ItalicOn -| ItalicOff +(* A typesetter for cpdf. A list of elements is manipulated zero or more times + to lay it out, paginate it, and so on. It is then typeset to produce a list + of pages *) -type t = box list +(* Text is represented as a list of unicode code points *) +type text = int list -let typeset papersize boxes = +(* Glue *) +type glue = + {glen : float; + stretch : float} + +(* Main type *) +type code = + Text of text +| HGlue of glue +| VGlue of glue +| NewLine +| NewPage +| Font of Pdftext.font + +let indent x = HGlue {glen = x; stretch = 0.} +let newpara x = HGlue {glen = x; stretch = 0.} + +type t = code list + +let of_utf8 = Pdftext.codepoints_of_utf8 + +let example = + [Text (of_utf8 "Jackdaws love my Sphinx of Quartz. And this, this is the second sentence to provoke a line-break."); + newpara 10.; + indent 72.; + Text (of_utf8 "The second paragraph"); + NewPage] + +(* Typesetter state. Origin at top left of page *) +type state = + {mutable font : Pdftext.font; + mutable xpos : float; + mutable ypos : float} + +let initial_state () = + {font = Pdftext.StandardFont (Pdftext.TimesRoman, Pdftext.WinAnsiEncoding); + xpos = 0.; + ypos = 0.} + +(* Split text into lines, resolve all hglue stretches to 0, remove Newlines. *) +let layout lmargin rmargin papersize i = + i + +(* Resolve all hglue stretches, insert NewPage as needed. *) +let paginate tmargin bmargin papersize i = i + +(* Split on NewPages, typeset each page, add font dictionaries *) +let typeset papersize i = [Pdfpage.blankpage papersize] + diff --git a/cpdftype.mli b/cpdftype.mli index 6ff1fb9..ac5d6f3 100644 --- a/cpdftype.mli +++ b/cpdftype.mli @@ -1,14 +1,17 @@ -(* Simple typesetter *) -type box = - Text of string -| Glue of float -| Newline -| Font of string -| BoldOn -| BoldOff -| ItalicOn -| ItalicOff +type text = int list -type t = box list +type glue = + {glen : float; + stretch : float} + +type code = + Text of text +| HGlue of glue +| VGlue of glue +| NewLine +| NewPage +| Font of Pdftext.font + +type t = code list val typeset : Pdfpaper.t -> t -> Pdfpage.t list