|(require quad)||package: quad|
|#lang quadwriter||package: quad|
Quad is in progress. It works, but it is unstable — I am still changing things, small and large — and thus I make no commitment to maintain the API in its current state.
raco pkg install quad
raco pkg update quad
Much of the font-parsing and PDF-rendering code in Quad is adapted from FolioJS by Devon Govett. I thank Mr. Govett for figuring out a lot of details that would’ve made me squeal in agony.
A document processor, which means that it:
Computes the layout of your document from a series of formatting codes (not unlike a web browser)
Renders to PDF (not unlike a word processor).
For instance, LaTeX is a document processor. So are web browsers. Quad borrows from both traditions — it’s an attempt to modernize the good ideas in LaTeX, and generalize the good ideas in web browsers, while bypassing some of the limitations of LaTeX (e.g., no Unicode) and of web browsers (e.g., performance and error recovery are valued above all).
Document processors sit opposite WYSIWYG tools like Microsoft Word and Adobe InDesign. There, the user controls the layout by manipulating a representation of the page on the screen. This is fine as far as it goes. But changes to the layout — for instance, a new page size — often require a new round of manual adjustments.
A document processor, by contrast, relies on markup codes within the text to determine the layout programmatically. Compared to WYSIWYG, this approach offers less granular control. But it also creates a more flexible relationship between the source and its possible layouts.
Another benefit of document processors is that it permits every document to have a high-level, text-based source file that’s independent of any particular output format.
Quad produces PDFs using three ingredients:
A font engine that handles glyph shaping and positioning using standard TTF or OTF font files.
A layout engine that converts typesetting instructions into an output-independent layout — e.g., putting characters into lines, and lines into pages.
A PDF engine that takes this layout and renders it as a finished PDF file.
For the most part, neither Quad nor Quadwriter rely much on racket/draw, and completely avoid its PDF-drawing functions. These facilities are provided by Pango, which has some major shortcomings in the kind of PDFs it produces (for instance, it doesn’t support hyperlinks).
A demo app built with Quad. It takes a text-based source file as input, calculates the typesetting and layout, and then outputs a PDF.
You can fiddle with it & then submit issues and feature requests at the Quad repo.
Save the document. Any place, any name is fine.
Run the document. You’ll get REPL output like this:
hyphenate: cpu time: 0 real time: 0 gc time: 0
line-wrap: cpu time: 27 real time: 30 gc time: 0
page-wrap: cpu time: 0 real time: 1 gc time: 0
position: cpu time: 1 real time: 0 gc time: 0
draw: cpu time: 77 real time: 76 gc time: 23
wrote PDF to /Desktop/test.pdf
Congratulations — you just made your first PDF. If you want to have a look, either open the file manually, or enter this command on the REPL, which will open the PDF in your default viewer:
Next, on the REPL enter this:
You will see the actual input to Quadwriter, which is called a Q-expression:
'(q () (q ((page-margin-left "120") (page-margin-top "80") (page-margin-bottom "120") (font-family "default-serif") (line-height "17")) (q ((keep-first-lines "2") (keep-last-lines "3") (font-size-adjust "100%") (character-tracking "0") (hyphenate "true") (display "g49598")) "Brennan and Dale like fancy sauce.")))
In the demos that follow, the input language will change slightly. But the PDF will be rendered the same way (by running the source file) and you can always look at doc or use view-result.
I don’t recommend that writers adopt Markdown for serious projects. But for goofing around, why not.
Our first version of "test.rkt" had one line of plain text:
Behind the scenes, quadwriter/markdown is doing more heavy lifting than this sample suggests. We can type our source in Markdown notation, and it will automatically be converted to the appropriate Quad formatting commands to make things look right.
For instance, try this sample, which combines a Markdown heading, bullet list, code block, and bold and italic formatting:
You’re welcome to paste in bigger Markdown files that you have laying around and see what happens. As a demo language, I’m sure there are tortured agglomerations of Markdown notation that will confuse quadwriter/markdown. But vanilla files should be fine.
Back to the demo. Curious characters can do this:
To see this:
((page-margin-left "120") (page-margin-top "80") (page-margin-bottom "120") (font-family "default-serif") (line-height "17"))
(q ((break "para")))
(q ((font-family "default-heading") (first-line-indent "0") (display "block") (font-size "20") (line-height "24.0") (border-width-top "0.5") (border-inset-top "9") (inset-bottom "-3") (inset-top "6") (keep-with-next "true") (id "did-you-know")) "Did you know?")
This is the first part of the Q-expression that the source file produces when it runs and exports via doc. This Q-expression is passed to Quadwriter for layout and rendering.
Suppose Markdown is just not your thing. You prefer to enter your markup the old-fashioned way — by hand. I hear you. So let’s switch to the quadwriter/markup dialect. First we try our simple test:
We get the same PDF result as before, again because a short line of plain text is the same in this dialect as the last.
But if we want to reproduce the result of the Markdown notation, this time we use the equivalent HTML-ish markup tags:
The special ◊ character is called a lozenge. It introduces markup tags. Instructions for typing it, but for now it suffices to copy & paste, or use the Insert Command Char button in the DrRacket toolbar.
Under the hood, the quadwriter/markdown dialect is converting the Markdown surface notation into markup tags that look like this. So the quadwriter/markup dialect just lets us start with those tags.
Curious characters can prove that this is so by again typing at the REPL:
This Q-expression is exactly the same as the one that resulted with the quadwriter/markdown source file.
quadwriter/markdown showed high-level notation (= a generous way of describing Markdown) that generated a Q-expression. Then quadwriter/markup showed a mid-level notation that generated another (identical) Q-expression.
If we wish, we can also skip the notational foofaraw and just write Q-expressions directly in our source file. We do this with the basic quadwriter language.
Recall our very first example:
In the REPL, the doc was this Q-expression:
'(q () (q ((page-margin-left "120") (page-margin-top "80") (page-margin-bottom "120") (font-family "default-serif") (line-height "17")) "Brennan and Dale like fancy sauce."))
This produces the same one-line PDF as before.
Likewise, we can pick up the doc from our more complex example:
#lang quadwriter/markdown # Did you know? __Brennan__ and **Dale** like: * *Fancy* sauce * _Chicken_ fingers ``` And they love to code ```
And again, use the resulting Q-expression in doc as the source for a new quadwriter program, which will result in the same PDF.
Even if you’re using a quadwriter dialect, you can still set top-level formatting attributes for the document. For instance, suppose we wanted to make our original quadwriter/markdown example 24 points and red, and put the PDF on wide tabloid (17in × 11in) paper. We can add these top-level attributes to the beginning of our source file as keyword arguments:
Any of the Markup attributes documented below can be used as keyword arguments. The syntax follows the pattern above: one attribute + value pair per line, with the attribute prefixed with #: to make it a keyword, and the value unquoted.
This keyword syntax works in the quadwriter, quadwriter/markdown, and quadwriter/markup languages. The idea is to make it easy to adjust the default layout behavior without going outside the source file.
Let’s see how this works by doing document layout and rendering from within good old racket/base:
Here, we create a little Q-expression, which we pass to render-pdf with a pdf-path argument.
Fans of pollen might be glad to hear that quadwriter can be used to handle layout and PDF rendering for Pollen source files. As usual we start with a Pollen source file, this time with the pdf.pm extension to indicate that it’s a Pollen markup file that will produce a PDF:
Then we add a simple "pollen.rkt" that converts the output of our source file into a Q-expression:
All we’re doing here is wrapping our paragraphs in q tags (rather than the default p tags) and then adding explicit Quadwriter paragraph breaks between them (see para-break).
In this case, we pass #false as the path argument to render-pdf so that it returns the actual bytes, which the Pollen renderer will put in the right place.
You can fire up the Pollen project server and see how this works. As usual with Pollen sources, when you make changes to the source file, the rendered PDF will be dynamically updated.
Though a quadwriter source file and a pollen source file both export something called doc, these exports don’t share any deeper connection. (The name was chosen to be consistent with Scribble, which also exports a doc.)
In the usual Racket tradition, quadwriter and its dialects are just compiling a document from a higher-level representation to a lower-level representation.
If you’re a writer, you might prefer to use the high-level representation (like quadwriter/markdown) so that your experience is optimized for ease of use.
If you’re a developer, you might prefer to use the lower-level representation for precision. For instance, a pollen author who wanted to generate a PDF could design tag functions that emit Q-expressions, and then pass the result to render-pdf.
Or, you can aim somewhere in between. Like everything else in Racket, you can design functions & macros to emit the pieces of a Q-expression using whatever interface you prefer.
doc : qexpr?
A Q-expression is an X-expression, but more restricted:
||||(list q (list (list attr-name attr-val) ...) qexpr ...)|
||||(list q (list qexpr ...))|
This grammar means that a Q-expression is either a) a string, b) an X-expression whose tag is q and whose elements are themselves Q-expressions.
> (qexpr? "Hello world")
> (qexpr? '(q "Hello world"))
> (qexpr? '(q () "Hello world"))
> (qexpr? '(q ((font-color "pink")) "Hello world"))
> (qexpr? '(q ((font-color "pink")) (q "Hello world")))
; malformed Q-expressions > (qexpr? 42)
> (qexpr? '(div "Hello world"))
> (qexpr? '(q (("pink" font-color)) "Hello world"))
Because Q-expressions are a subset of X-expressions, you can apply any tools that work with X-expressions (for instance, the txexpr library).
Unlike X-expressions, Q-expressions do not support character entities or CDATA, because those are inherent to XML-ish markup.
para-break : qexpr?
These are the attributes that can be used inside a Q-expression passed to quadwriter. Inside a Q-expression, every attribute is a symbol, and every attribute value is a string.
A dimension string represents a distance in the plane. If unitless, it is treated as points (where 1 point = 1/72 of an inch). If the number has in, cm, or mm as a suffix, it is treated as inches, centimeters, or millimeters respectively.
A block is a paragraph or other rectangular item (say, a blockquote or code block) with paragraph breaks around it.
background-color : symbol?
keep-all-lines keeps all the lines of a quad on the same page. Activated only when value is "true". Be careful with this option — it’s possible to make a single quad that is longer than one page, in which case quadwriter will ignore the setting to prevent an impossible situation.
keep-with-next : symbol?
first-line-indent : symbol?
line-wrap : symbol?
hyphenate : symbol?
font-family : symbol?
font-color : symbol?
font-bold : symbol?
font-italic : symbol?
line-height : symbol?
TK: OT feature attributes, bullet attributes
qx : qexpr? pdf-path : (or/c path? path-string? #false) replace? : any/c = #true
The optional replace? argument controls whether an existing file is automatically overwritten. The default is #true.
A design goal of Quadwriter is to treat document layout as the result of a program. Along those lines, fonts are handled differently than usual. When you use a word processor, you choose from whatever fonts might be installed on your system.
Quadwriter, by contrast, relies only on fonts that are in the same directory as your other project source files. This is a feature: it means that everything necessary to render the document travels together in the same directory. You can re-render it anywhere with identical results. You never have the problem — still with us after 35 years of desktop word processing — that “oh, you need to install such-and-such font in your system before it will work.” Bah!
Quadwriter supports the usual TrueType (.ttf) and OpenType (.otf) font files. To add fonts to your Quadwriter experience:
Within your project directory, create a subdirectory called "fonts".
Within "fonts", create a subdirectory for each font family you want to use in your Quadwriter document. The names of these subdirectories will become the acceptable values for the font-family attribute in your documents.
If there is only one font file in the family subdirectory, then it is used every time the font family is requested.
Alternatively, you can specify styled variants by creating within the family directory style subdirectories called "regular", "bold", "italic", and "bold-italic".
Though this system may seem like a lot of housekeeping, it’s nice for two reasons. First, we use the filesystem to map font names to font files, and avoid having another configuration file floating around our project. Second, we create a layer of abstraction between font names and files. This makes it easy to change the fonts in the document: you just put new fonts in the appropriate font-family directory, and you don’t need to faff about with the source file itself.
TK: example of font setup
(view-result) → void?
As mentioned above, The quad library itself knows as little as it can about typography and fonts and pictures. Nor does it even assert a document model like Scribble. Rather, it offers a generic geometric represntation of layout elements. In turn, these elements can be combined into more useful pieces (e.g., quadwriter).
The eponymous quad is a structure type that represents a rectangular layout area. This rectangle is used for layout purposes only. It is not enforced during the rendering phase. Meaning, once positioned, a quad’s drawing function can access this rectangle, but does not need to stay within it.
Quads can be freely nested. There are no rules about what kind of quad can be nested in another.
Wrapping is a optional phase where lists of quads are broken into sublists of a certain size. In quadwriter, the list of words is wrapped to produce a list of lines of a certain horizontal width. In turn, the list of lines is wrapped to produce a list of pages of a certain vertical height.
Each quad has a set of 11 anchor points on its perimeter.
Eight points are named for the compass directions: 'n (= top center) 'e (= right center) 's (= bottom center) 'w (= left ceter) 'ne (= upper right) 'se (= lower right) 'sw (= lower left) 'nw (= upper left).
The center of the quad is 'c.
The other two anchor points are 'baseline-in and 'baseline-out or just 'bi and 'bo. These points are also on the quad perimieter. They allow quads containing type to be aligned according to adjacent baselines. The exact location of these points depends on the direction of the script. For instance, in left-to-right languages, 'baseline-in is on the left edge, and 'baseline-out is on the right. The vertical position of these points depends on the font associated with the quad. If no font is specified, the 'bi and 'bo points are vertically positioned at the southern edge.
By default, each subquad will ultimately be positioned relative to the immediately preceding subquad (or, if it’s the first subquad, the parent). Optionally, a subquad can attach to the parent.
How does a quad know which anchor points to use? Each quad specifies a to anchor on its own perimeter, and a from anchor on the previous quad’s perimeter. The quad is positioned by moving it until its to anchor matches the position of the (already positioned) from anchor. Think of it like two tiny magnets clicking together.
A key benefit of the anchor-point system is that it gets rid of notions of “horizontal”, “vertical”, “up”, “down”, etc. Quads flow in whatever direction is implied by their anchor points.
> (define q1 (make-quad #:size '(25 25))) > (define q2 (make-quad #:size '(15 15))) > (quad->pict (position (attach-to q1 'e q2 'w))) > (quad->pict (position (attach-to q1 'nw q2 'se))) > (quad->pict (position (attach-to q1 'w q2 'e))) > (quad->pict (position (attach-to q1 's q2 'n))) > (quad->pict (position (attach-to q1 'e q2 'n)))
“Wait a minute — why is the new quad specifying both anchor points? Shouldn’t the from anchor be specified by the previous quad?” It could, but it would make the layout system less flexible, because all the subquads hanging onto a certain quad would have to emanate from a single point. This way, every subquad can attach to its neighbor (or the parent) in whatever way it prefers.
Once the quads have been positioned, they are passed to the renderer, which recursively visits each quad and calls its drawing function.
Though every quad has a size field, this is just the size used during layout and positioning. Quad doesn’t know (or care) about whether the drawing stays within those bounds.
Some things I personally plan to use Quad for:
A simple word processor. Quadwriter is the demo of this.
Font sample documents. In my work as a type designer, I have to put together PDFs of fonts. To date, I have done them by hand, but I would like to just write programs to generate them.
Racket documentation. The PDFs of Racket documentation are currently generated by LaTeX. I would like to make Quad good enough to handle them.
Book publishing. My wife is a lawyer and wants to publish a book about a certain area of the law that involves a zillion fiddly charts. If I had to do it by hand, it would take months. But with a Quad program, it could be easy.
In letterpress printing, a quad was a piece of metal used as spacing material within a line.
“A way of doing something original is by trying something so painstaking that nobody else has ever bothered with it.” — Brian Eno