GNU TeXmacsUser manualReferenceAboutEnglish
The typesetting algorithm

1.Introduction

The TeXmacs typesetter converts the edit tree of a document into a tree of graphical boxes. The boxes themselves, the way they record their source location and the routines for cursor positioning and selection are described in the boxes produced by the typesetter; the evaluation of macros, with constructs and other environment primitives is described in the chapter on macro expansion. The present chapter explains what lies in between: how the typesetter walks the tree, how it produces lines, paragraphs and pages, and how it manages to redo only a small amount of work after each keystroke.

Contrary to a batch system like LaTeX, the TeXmacs typesetter is incremental: the complete document is kept in typeset form, and every modification of the edit tree is notified to the typesetter, which marks the corresponding parts as invalid. When the screen has to be updated, only the invalid paragraphs are re-typeset; the cached results for all other paragraphs are reused. Page breaking, on the other hand, is redone for the whole document at each update, but it works on precomputed page items and is therefore relatively cheap.

All C++ file names in this chapter are relative to

src/src/

. The typesetter lives in Typeset/, with the public interface in

Typeset/typesetter.hpp

. It is driven by the editor from

Edit/Editor/edit_typeset.cpp

.

2.Overview of the pipeline

Typesetting a document proceeds through the following stages. Each stage has its own data structure, and the output of one stage is the input of the next one.

  1. Bridges (

    Typeset/Bridge/

    ). The top-level document and the structural constructs that may contain whole paragraphs (with, surround, macro applications, arg, locus, etc.) are mirrored by a tree of bridge objects. Each bridge remembers the subtree it is responsible for, whether it is still valid, the lines it produced last time and the changes it made to the environment. Bridges receive the modification notifications and decide what has to be re-typeset.

  2. Concatenation (

    Typeset/Concat/

    ). Each paragraph is handed to a concater_rep, which traverses the paragraph's tree (text, mathematics, inline macros, tables, graphics, …) and produces a flat array of line_items: boxes decorated with the space that follows them, a line-breaking penalty, a type and, for control items, the tree of the control command.

  3. Line breaking and paragraph formatting (

    Typeset/Line/

    ). A lazy_paragraph_rep splits the line items into paragraph units, calls the line breaker (

    Typeset/Line/line_breaker.cpp

    ) to find optimal break points, justifies each line and turns it into a phrase_box.

  4. Vertical stacking (

    Typeset/Stack/

    ). The lines are passed to a stacker_rep, which computes the vertical distance between successive lines (shoving lines into each other when possible), paragraph separations and page-breaking penalties. The result is an array of page_items.

  5. Page breaking (

    Typeset/Page/

    ). The page items of the whole document are handed to a pager_rep, which calls the page breaker to cut the item list into pages, places floats and footnotes, and finally builds page boxes with headers and footers. On screen, in papyrus mode, a single tall page is produced.

  6. Screen update (

    Edit/Editor/edit_typeset.cpp

    ,

    Edit/Interface/edit_interface.cpp

    ). The editor stores the resulting box as eb, determines the rectangle of the screen that really changed and invalidates it.

Tables (

Typeset/Table/

) are typeset by a separate module, called by the concater for inline tables, and by the paragraph formatter for tables that may be broken across pages. The environment (Typeset/Env/) is used by all stages to read style parameters and to evaluate macros.

3.Source map

Typeset/typesetter.hpp

The public interface: new_typesetter, the notify_* functions, typeset, and the helpers typeset_as_concat, typeset_as_box, typeset_as_atomic, typeset_as_stack, typeset_as_table, typeset_as_var_table, typeset_as_paragraph and typeset_as_document.

Typeset/Bridge/

The typesetter_rep class (

impl_typesetter.hpp

,

typesetter.cpp

), the abstract bridge_rep (

bridge.hpp

,

bridge.cpp

) and one file per kind of bridge.

Typeset/Format/
,
Typeset/formatter.hpp

The formatter data structures: line_item, page_item, stack_border, and the format and lazy classes.

Typeset/Concat/

The concater: strings and spacing (

concat_text.cpp

), mathematics (

concat_math.cpp

), macros (

concat_macro.cpp

), other active and inactive markup, graphics, animations, and post-processing of brackets and scripts (

concat_post.cpp

).

Typeset/Line/

Paragraph formatting (

lazy_paragraph.cpp

), line breaking (

line_breaker.cpp

), lazy structures for documents and other vertical material (

lazy_typeset.cpp

,

lazy_vstream.cpp

,

lazy_gui.cpp

).

Typeset/Stack/

The stacker.

Typeset/Table/

Tables and cells.

Typeset/Page/

The pager (

pager.cpp

,

make_pages.cpp

), the page breakers (

new_breaker.cpp

,

columns_breaker.cpp

and the older

page_breaker.cpp

), and the auxiliary types pagelet, insertion (

skeleton.hpp

) and vpenalty (

vpenalty.hpp

).

Typeset/Boxes/

The box classes; see the boxes.

Typeset/Env/,
Typeset/env.hpp

The typesetting environment edit_env; see macro expansion.

4.Main entry points

typesetter new_typesetter (edit_env& env, tree et, path ip)
(create a typesetter)

Creates a typesetter for the document body et, whose inverse path is ip. The typesetter builds the root bridge immediately, but does not typeset anything yet. Each editor owns one typesetter (the member ttt of edit_typeset_rep).

void notify_assign (typesetter ttt, path p, tree u)

void notify_insert (typesetter ttt, path p, tree u)

void notify_remove (typesetter ttt, path p, int nr)
(inform the typesetter about modifications)

These functions (together with notify_split, notify_join, notify_assign_node, notify_insert_node and notify_remove_node) mirror the elementary modifications of the edit tree. The path p is relative to the root of the typeset document. They only invalidate bridges; no typesetting takes place.

box typeset (typesetter ttt, SI& x1, SI& y1, SI& x2, SI& y2)
(typeset the document)

Re-typesets the invalid parts of the document, reassembles the pages and returns the box of the whole document. On return, the four coordinates delimit the region whose appearance changed since the previous call.

void exec_until (typesetter ttt, path p)
(compute the environment at a path)

Brings the environment of the typesetter into the state it has just before the position p, reusing the environment changes cached in the bridges. This is used to determine the environment at the cursor.

box typeset_as_document (edit_env e, tree t, path ip)
(one-shot typesetting)

Creates a temporary typesetter, typesets the tree t as a complete document and deletes the typesetter again. This is used for printing (edit_main_rep::print_doc) and for counting pages. The other typeset_as_* helpers typeset fragments inside a given environment and are used recursively by the typesetter itself.

5.Contents of this chapter

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".