GNU TeXmacsUser manualReferenceAboutEnglish
Incremental typesetting: the typesetter and its bridges

1.The typesetter object

A typesetter is an instance of typesetter_rep, declared in

Typeset/Bridge/impl_typesetter.hpp

; the type typesetter is a plain pointer to it. Its main fields are:

class typesetter_rep {
public:
  edit_env&    env;         // the typesetting environment
  bridge       br;          // root bridge (for the document body)
  rectangles   change_log;  // regions touched since last typesetting
  array<brush> old_bgs;     // page backgrounds of previous pass
  array<page_item> l;     // current lines
  stack_border     sb;    // border properties
  array<line_item> a;     // left surroundings
  array<line_item> b;     // right surroundings
  hashmap<string,tree> old_patch;
  bool paper;
  …
};

The typesetter does not own the environment: it shares the edit_env of the editor. During a typesetting pass, the fields l and sb accumulate the page items of the document (see paragraph formatting), while a and b hold line items which have to be prepended resp. appended to the next paragraph (for instance the markers of an enclosing with, or the left and right parts of a surround). The hashmap old_patch is used to detect that the environment seen by a paragraph differs from the one it saw during the previous pass; see below.

The method typesetter_rep::typeset () (in

Typeset/Bridge/typesetter.cpp

) performs one complete pass:

  1. It resets l, sb, a, b and old_patch.

  2. It determines whether the pass will be complete, i.e. whether every bridge will be re-typeset (br->my_typeset_will_be_complete (), and no show-preamble or hide-part is present). In that case, env->complete is set and the tables used for collecting references (local_aux, missing, redefined, touched) are cleared.

  3. It calls br->typeset (PROCESSED+WANTED_PARAGRAPH) on the root bridge, which walks the bridge tree, re-typesets the invalid parts and appends all page items to l.

  4. It creates a pager_rep on l and calls make_pages, which breaks the items into pages and returns the box for the whole document.

  5. On paper, during a complete pass, it collects the page numbers of all labels (determine_page_references) so that page references can be resolved.

2.Inverse paths

Every box and every line item remembers where in the source tree it comes from, through an inverse path ip: the path from the root of the edit tree to the subtree, stored in reverse order so that common prefixes are shared. The details are explained in the boxes; here we only recall the helpers from

Typeset/boxes.hpp

which are used throughout the typesetter:

descend (ip, i)

The inverse path of the i-th child. If ip is a decoration, ip is returned unchanged: the children of an inaccessible subtree are inaccessible too.

decorate (ip), decorate_left, decorate_middle, decorate_right

Prepend the negative markers DECORATION, DECORATION_LEFT, DECORATION_MIDDLE or DECORATION_RIGHT. Boxes with such an inverse path are not editable (is_decoration); clicking on them places the cursor before, inside or after the subtree at ip.

attach_right (t, ip), attach_here, attach_middle, attach_deco

Macros expanding to two arguments t, ip'. They attach an inverse path to a tree which is not part of the edit tree (typically a macro body or the result of an evaluation) through attach_dip. Later, obtain_ip (t) recovers the inverse path of such a tree, or of any subtree of the edit tree, from the ip_observer attached to it.

Since subtrees of the edit tree carry their inverse path in their observers, the concater and the bridges start by checking is_accessible (ip); if the proposed inverse path is a decoration, they try obtain_ip (t) and use the real source location when the tree comes from the document (for instance a macro argument). This is how the text of a macro argument remains editable, although it is typeset while traversing the macro body.

3.Bridges

3.1.The bridge classes

A bridge (

Typeset/Bridge/bridge.hpp

) connects a subtree of the document to its typeset representation:

class bridge_rep: public abstract_struct {
public:
  typesetter           ttt;      // the underlying typesetter
  edit_env&            env;      // the environment
  tree                 st;       // the present subtree
  path                 ip;       // source location of the paragraph
  int                  status;   // status among above values
  hashmap<string,tree> changes;  // changes in the environment
  array<page_item>     l;        // the typesetted lines of st
  stack_border         sb;       // border properties of l
  link_repository      link_env; // loci and links declared inside bridge
  …
};

The field status combines two bits. The bit VALID_MASK is either CORRUPTED (the cached lines are out of date) or PROCESSED. The bit WANTED_MASK records whether the bridge was typeset as a paragraph (WANTED_PARAGRAPH) or as a paragraph unit (WANTED_PARUNIT). The cached lines are reused only if the status equals the desired status.

Bridges are created by make_bridge in

Typeset/Bridge/bridge.cpp

, which dispatches on the label of the subtree:

bridge_document

For document: one sub-bridge per paragraph. This is the bridge that makes typesetting incremental at the paragraph level.

bridge_with

For with: evaluates the variable assignments, then typesets the body with its own bridge. Similar bridges exist for surround (bridge_surround), locus (bridge_locus), mark (bridge_mark), expand-as (bridge_expand_as), and the decoration and formatting tags datoms, dlines, dpages and tformat (bridge_formatting).

bridge_compound

For macro applications (compound and all tags L(st) >= START_EXTENSIONS), as well as include, hlink, action and a few others. It looks up the macro, binds the arguments in env->macro_arg and env->macro_src, and typesets the macro body through a sub-bridge.

bridge_argument

For arg inside a macro body: it typesets the value of the argument through a sub-bridge whose inverse path is the source location of the argument.

bridge_eval, bridge_rewrite, bridge_auto

For constructs whose body is computed (eval, quasi, map-args, extern, animations; inactive markup and error markers go through bridge_auto).

bridge_hidden

For hidden: typesets the content (so that side effects such as labels take place) but flags the resulting items as PAGE_HIDDEN_ITEM of zero height.

bridge_ornament, bridge_art_box, bridge_canvas

For the GUI-like containers in

bridge_gui.cpp

.

bridge_default

For every other primitive: the subtree is an ordinary paragraph, and it is re-typeset as a whole when anything inside it changes.

In the preamble mode (env->preamble), all constructs are shown in source form through make_inactive_bridge.

The granularity of incremental typesetting follows from this list: bridges are only created down to the level of paragraphs (and of the bodies of the structural constructs which may contain paragraphs). Inside a paragraph, there are no bridges any more; a modification anywhere in the paragraph invalidates the whole paragraph, which is then re-typeset from scratch by the concater and the line breaker.

3.2.Notification of changes

The editor informs the typesetter about each elementary modification of the edit tree. The functions in

Edit/Modify/edit_modify.cpp

(edit_modify_rep::notify_assign etc.) convert the path to a path relative to the root of the document (p / rp) and call the global notify_assign, notify_insert, notify_remove, notify_split, notify_join, notify_assign_node, notify_insert_node and notify_remove_node from

Typeset/Bridge/typesetter.cpp

. These in turn call the virtual methods of the root bridge. An assignment at the empty path replaces the root bridge altogether; the three *_node variants are rewritten into assignments of the parent.

The abstract class only requires notify_assign, notify_macro and notify_change. Its default implementations of notify_insert, notify_remove, notify_split and notify_join compute the new value of the parent subtree and call notify_assign on it. Bridges that care about finer notifications override them:

Every notify_* method sets status= CORRUPTED on all bridges along the path from the root to the modification. Notification is cheap: it only updates st and the status flags.

3.3.Typesetting a bridge

The non-virtual method bridge_rep::typeset (int desired_status) is the heart of the incremental algorithm. Abridged, it reads:

void
bridge_rep::typeset (int desired_status) {
  if (is_accessible (ip)) st= subtree (the_et, reverse (ip));
  …
  if ((status==desired_status) && (N(ttt->old_patch)==0))
    env->monitored_patch_env (changes);   // reuse cached lines
  else {
    hashmap<string,tree> prev_back (UNINIT);
    my_clean_links ();
    …
    ttt->local_start (l, sb);
    env->local_start (prev_back);
    my_typeset (desired_status);           // really typeset
    env->local_update (ttt->old_patch, changes);
    env->local_end (prev_back);
    ttt->local_end (l, sb);
    …
    status= desired_status;
  }
  … ttt->insert_stack (l, sb);
}

Two conditions have to hold for the cached result to be reused: the bridge must not be corrupted, and the environment in which it is typeset must be the same as during the previous pass. The first condition is maintained by the notifications. The second is tracked through environment patches:

The virtual method my_typeset does the real work. For bridge_rep (and hence bridge_default) it calls ttt->insert_paragraph (st, ip), which typesets the paragraph with typeset_stack (see paragraph formatting) and merges the resulting page items into ttt->l with merge_stack. Structural bridges instead modify the environment, possibly insert markers, and recurse:

void
bridge_with_rep::my_typeset (int desired_status) {
  … evaluate the variables and new values …
  for (i=0; i<k; i++) env->write_update (vars[i], newv[i]);
  ttt->insert_marker (st, ip);
  body->typeset (desired_status);
  for (i=k-1; i>=0; i--) env->write_update (vars[i], oldv[i]);
}

insert_marker adds zero-width marker line items (produced by typeset_marker) for the positions descend (ip, 0) and descend (ip, 1) to the surroundings ttt->a and ttt->b. They will be glued to the first resp. last paragraph of the body, so that the cursor can be placed just before or after the with. Similarly, bridge_surround_rep typesets the left and right parts of a surround with typeset_concat and pushes them with insert_surround. bridge_document_rep::my_typeset hands the pending left surroundings only to its first paragraph and the right surroundings only to its last one.

When the pass is not on paper (ttt->paper is false), a bridge producing several lines without floats or multiple columns packs them into a single page item containing a stack_box. This reduces the number of page items the page breaker has to deal with in the (single page) papyrus mode. Control items for PAGE_THIS_TOP, PAGE_THIS_BOT and PAGE_THIS_BG_COLOR are kept separately.

The accelerator bridge_docrange in

Typeset/Bridge/bridge_docrange.cpp

, which would organize long documents in a binary tree of ranges, is currently disabled (bridge_document_rep::initialize_acc always sets acc to the nil bridge).

3.4.What is re-typeset after an edit

Consider typing a character inside the fifth paragraph of a document body, which is itself inside a with. The editor calls notify_insert with the path of the string. The root bridge_document forwards it to brs[k] (the with), which forwards it to its body bridge, a bridge_document, which forwards it to the bridge_default of the fifth paragraph. The latter only updates its st. All bridges along this path are now CORRUPTED.

At the next typeset, the root bridge and the with bridge are re-executed (their my_typeset is cheap: they mostly recurse), all sibling paragraphs are reused from their caches, and only the fifth paragraph goes through concatenation, line breaking and stacking. If the modified paragraph changes the environment differently than before (typically: a new numbered environment), old_patch becomes non-empty and the following paragraphs are re-typeset until the environments agree again. Finally, page breaking is done again for the whole list of page items.

The cost of these stages can be measured: with the environment variable TEXMACS_EDIT_PROFILE set, every typesetting which follows an edit prints the time spent in the bridges (with the number of bridges re-executed and reused), in the pager and in the change log, with the part of the view which the editor invalidates, and every repaint prints its area and its time (edit_profile in

typesetter.cpp

). In a document of 140 pages (1680 paragraphs, 6400 lines, measured in October 2026 on the Vue port with the software renderer), a character typed in a paragraph costs 2 ms in the bridges and 1 ms in the change log, but 50 ms in the pager on paper; on papyrus, with no pages to break, the whole typesetting takes 11 ms, of which 8 ms go to the visit of the bridges which are reused. A new section, which renumbers what follows, re-executes 2600 bridges in 120 ms.

Most of the time of the pager is the search of the page breaks, which tries every line as the start of a page (40 ms of the 50). Its result, the skeleton, is a function of the heights, spaces, penalties, columns and floating objects of the page items, not of their boxes, and most edits do not change these numbers: the skeletons of the last few such signatures are kept and returned without a search (new_break_pages in

new_breaker.cpp

; the environment variable TEXMACS_PAGE_BREAK_CACHE may be off, or check to search anyway and report a difference). A character typed in a line then costs a quarter of the time in the pager, which still formats every page with its header and footer. An edit which changes the number of lines, or the height of one, changes the signature and is searched in full. What the breaker changes in the items it gets (a footnote on two columns of an item on one column goes to the next item on two columns, and some penalties are set) is done before the signature is taken, at every pass (breaker_prepare_items): the skeleton refers to the items as they are after it, and the pager needs them so when the search is skipped.

That search finds the same breaks as before, in the same order and with the same arithmetic, but faster for the positions with no pending float (find_page_breaks_plain): their best previous breaks and penalties are in arrays instead of tables indexed by paths, no path is made for each candidate page, and the height and the penalty of a candidate are computed with integers. TEXMACS_PAGE_BREAK_FAST may be 0 for the search as it was, 1 to 3 for the first changes only, or check to run both searches and report a difference. The tables which hold the starts to try are kept as they were: the starts are tried in the order of their iteration, on which the result may depend.

Measurements

The times below were measured in October 2026 on the Vue port with the software renderer, in a view of 800 by 446 points, on two documents made for the purpose: 140 pages of plain paragraphs, sections and numbered equations (1680 paragraphs, 6400 lines; the search has 6200 starts and 345000 candidate pages), and 70 pages with footnotes, floats and forced page breaks. They are in milliseconds, and vary by a factor of up to two from one run to the next on the same machine: the figures compared with each other come from the same runs.

The search of the page breaks, with the changes up to each one (the skeletons not kept, so that every edit searches):

as it was arrays and no paths and integers and candidates kept
lines of code 70 85 45 105
140 pages 43 to 44 30 to 37 17 to 25 7 4 to 5
70 pages with floats 21.5 19 14.5 3 3.5

Table 1. The search of the page breaks, in milliseconds.

The last column is a fourth change: the candidates of a start depend on the items from the start to the candidate only, and those of a previous search are used again for the starts whose items did not change. It gains little on these two documents, for the most code and four megabytes of candidates kept for the first one, and was taken out for a while. It is what counts in a book with an index, see below.

The whole of an edit in the first document, on paper, before and after these changes and the others of October 2026 (the invalid regions cut to the view in the Vue widget, the skeletons kept, the faster search):

bridges pager before pager after repaint before repaint after
a character in a line 2 49 9 to 16 0.4 0.4
a new line 3 51 18 to 22 67 0.7
a new section 120 52 20 61 0.8

Table 2. One edit in a document of 140 pages, in milliseconds.

Of the 18 to 22 ms of the pager for a new line, the search is 6; the rest is the setup of the breaker (3 to 5), the signature (2) and the pages with their headers and footers (7 to 9), which are made again for every page at every pass. On papyrus there is no page to break: the whole typesetting of a character takes 11 ms, of which 8 are the visit of the bridges which are reused.

A new section, or a new numbered equation, changes counters for the rest of the document, and old_patch stays non-empty down to its end: every bridge after the edit was typeset again, 2600 of them in 120 ms, whether it used these counters or not. A bridge now records the variables which its typesetting reads or writes (env_table in

env.hpp

, and bridge_rep::typeset), and one whose subtree did not change is used again when none of the variables of old_patch is among them. The conditions are in the comment before bridge_rep::typeset: the bridge must have got no line items from the bridges around it (the number of an equation ends up in the lines of its body); its record must be complete (the reads of the bridges below it are added to it, up to a limit; a Scheme routine makes it unknown); the references and attachments which it looked up, recorded with the values found, must still have these values; and the variables which changed must be plain ones, from which the environment derives no state when they are written. The variables which a bridge writes count as read: its changes only hold those whose value it changed. In the same way a paragraph which is removed no longer has every bridge after it typeset again: the bridge which follows keeps the changes of the removed one, which the next pass compares with the environment. A bridge which binds variables for the bridges below it (a with, an ornament, a locus, a formatting tag) writes them without a record of the change, since they have their values back when it is done: it keeps what it bound, and the variables whose binding differs from the previous pass are in old_patch while the bridges below it are typeset (bridge_rep::bind_start and bind_end). TEXMACS_TYPESET_READS may be off, or check to typeset again all the same and report a bridge whose lines, contents or changes differ.

bridges typeset again before after
a new section, 140 pages 2625, then 676 121 25
a new equation, 140 pages 2642, then 1890 128 39
a new section, 70 pages with floats 1566, then 327 88 50
a new equation, 70 pages with floats 1583, then 1067 82 46

Table 3. The bridges after an edit which renumbers, in milliseconds.

What is still typeset again are the titles and the equations which follow, with the bridges inside them (they read or write the counters and the current label), and in the second document the paragraphs with footnotes. The first typesetting of a document, where every read is recorded, takes the same time within the precision of the measure (143 and 149 ms).

The user manual

The same edits were made in the middle of the user manual, opened as one book (Help\(\rightarrow\)Manual\(\rightarrow\)User manual): 3000 paragraphs and structures at its top level, 18000 bridges, 23000 starts for the pages. The whole typesetting of an edit, in milliseconds, with the three mechanisms off (TEXMACS_TYPESET_READS=off, TEXMACS_PAGE_BREAK_FAST=0, TEXMACS_PAGE_BREAK_CACHE=off) and on:

before after
a character in a line 416 72
a new line 419 93 to 117
two paragraphs joined 1134 78
a new section 1811 155
a new numbered equation 1142 127

Table 4. One edit in the middle of the user manual, in milliseconds.

A real book gains less than the documents made for the measures. The manual has an index in two columns, and the height of a candidate page with several columns means balancing them: 137000 such candidates took 210 of the 250 ms of the search, at every change of the number of lines anywhere in the book. These candidates are those of the previous search as long as the index does not change, which is why the candidates of each start are kept: the items of the new search are matched with those of a previous one (lines added, removed or changed in several places, since a pass after another one also changes the page numbers of the table of contents and of the index; three searches are kept, the documents which are open being searched in turn), and a start whose items follow each other as before takes its candidates from it. The search then takes 10 to 16 ms, and a new line in the manual 93 to 117 ms instead of 359; the first change of the number of lines after the manual is opened still takes 370, the index itself having changed with its page numbers. The pages are made again at every pass, 50 ms for 260 of them.

The automatic labels

A new section in the manual still had 10600 bridges typeset again, in 700 ms, for a reason which is not in the typesetter. The entries of the table of contents, of the index, of the glossary and of the lists of figures and tables each put a label where they stand, which gives their page (auto-label in std-automatic.ts). Its name was made of a counter: the label after \(n\) others was auto-\(n\), so that a new section renamed the labels of all the index entries after it, of which the manual has several in most of its paragraphs on the macros. These bridges did make something else, the same boxes with other names.

The names are now made of numbers which stay: auto-id with the argument new takes a number for a new label, and with any other one gives the last one taken (edit_env_rep::exec_auto_id). The style file also defines a macro of that name, which a version of TeXmacs without the primitive calls instead, and which numbers the labels with the counter as before: the same package works in both.

The labels of a buffer are numbered from one sequence, kept with the tree of the buffer in an addendum (ADDENDUM_AUTO_ID, an observer like the one which holds the player of an animation): an epoch and the next number. A label made by a tag of the document (the section, the index entry, the figure) keeps its number: the tag holds the numbers of its labels in an addendum of its own, its \(k\)-th label has the \(k\)-th of them, and a label which it did not make before takes the next number of the sequence. The tag is found from the arguments of the macros which are being expanded (edit_env_rep::auto_label_tag): the innermost argument which has a place in the document is one of its arguments. A label which no tag of the buffer makes, in an included file for instance, takes the next number each time it is made. A tag which is typeset again makes the same labels, in every window on the document, since the windows share the tree: the numbers do not belong to a typesetter. The variable auto-last holds the last name which was given: the next tag which makes a label after a new one reads another value and is typeset again, once. The variable auto-open holds, for the tag and the tags around it, which of their labels was made last, from which a tag knows how many labels it made when a tag inside it made one in between; it is a variable and not a count kept with the tag, since the labels of one tag may be made by several bridges, of which some are used again.

The name of a label is auto-\(n\), as with the counter. When the labels were numbered in the order in which they are made, the names are exactly those of the counter, which is what a document gets when it is loaded, and again when it is updated (renumber-auto-labels, which update-document calls before it makes the tables, since these hold the names): the epoch of the buffer changes, the numbers of another epoch are not used, and the document is typeset again as a whole, which is one more pass at each update. The function tells whether a name changed, from the numbers of the labels in the order of the document before and after: an update of one table makes all of them when one did. Outside of a typesetting pass (a converter which expands the macros of a copy of the document, whose tags are not in the document) the counter names the labels, which gives the same names in a document which was updated.

An earlier version numbered the tags rather than the labels, named the other labels of a tag auto-\(n\).2…, gave the labels without a tag names of their own, and numbered again only when a flag said that a number had been given out of order: a section removed was not seen, and the names were not those of the counter for these labels. Between two updates the names differ from those of the counter, for the better: after a new section, a line of the table of contents which was not made again still refers to the label of its own section, where with the counter it referred to the label of whatever had taken its number. The new section in the manual takes 175 ms instead of 815, with 600 bridges typeset again instead of 10650. TEXMACS_STABLE_LABELS=off uses the counter as before.

The entries of the index itself, 1350 of them with their page numbers, and the paragraphs with references are used again since the references are recorded. The check mode of the recorded reads found one more thing a bridge may depend on, in a paragraph of the manual which shows the current time: the date and the animations make a record unknown.

The pages

With the bridges and the breaks used again, most of what an edit in a book costs is the making of the pages: for each page the pager stacks the lines and stretches them to the height of the page, and typesets the header and the footer. In a view of the manual with 379 pages this took 60 to 70 ms at every pass, of which 40 to 55 for the headers and footers and 10 to 13 for the stacks.

The typesetter now keeps what the pager made in the last pass (page_memo, filled by pager_rep::pages_make_page and pager_rep::make_part in

Typeset/Page/make_pages.cpp

), page by page. The text of a page is used again when it is made of the very same boxes, which is what a bridge which is used again gives, with the same spaces, in insertions of the same kinds, heights and stretches (pager_rep::text_parts lists all that pages_format uses). A header or a footer is used again when the tree to typeset is the same and the variables which its typesetting read have the values they had, with the same record as the one of the bridges: the number of the page is one of them, and so is every macro of the style which the header calls; the references it looks up are recorded with their values. One which reads something which is not a variable is typeset again, and in a complete pass they all are, since such a pass collects the references which are used. Nothing is kept when a parameter of the pager changed, or one of the variables from which the environment derives a state which is not read as a variable, such as the font and the color (edit_env_rep::derived_state), and when the whole document is to be typeset again (notify_assign of the root): the reason may be one which the pager does not see, such as the fonts which a browser receives late. The pages without text are not kept: they cost nothing.

The pager then takes 13 to 21 ms for a character typed in the manual, and the whole typesetting 35 to 45 ms instead of 85 to 100; two paragraphs joined take 35 instead of 90, a new line 60 to 75 instead of 120 and a new section 135 instead of 200. What remains of the pager is the signature for the breaks and the list of the boxes of each page; the visit of the bridges which are used again takes 11 to 13 ms. TEXMACS_PAGE_REUSE=off makes every page again, and =check makes them again too and compares them with what is kept. On papyrus there is one page and nothing of this applies.

Checking what is used again

Each of these mechanisms has a mode in which it does the work anyway and compares (TEXMACS_TYPESET_READS, TEXMACS_PAGE_BREAK_CACHE, TEXMACS_PAGE_BREAK_FAST and TEXMACS_PAGE_REUSE set to check). For the bridges this covers both ways of using one again: because none of the variables which changed was read, and as in the classic cache, when nothing changed around it as far as the bookkeeping of the changes says (the paragraphs removed before it, the bindings above it): a mistake in that bookkeeping shows as a bridge which is typeset otherwise. The differences found are counted, and (typeset-check-mismatches) gives their number. The group incremental against scratch of the typeset suite makes random edits of a document which holds what the reuse has to see (counters, references, index entries, footnotes, hidden paragraphs, a unit made by a macro), compares the places of all the paragraphs after each one with a typesetting from scratch, and fails when the check modes counted a difference; with the derived inputs left out of plain_variable it finds 36 bad edits out of 40. The record of what a bridge or a header reads is held by a scoped object (edit_env_rep::read_record), so that an exception during the typesetting leaves no record pointing to a table which is gone.

3.5.Complete typesetting and references

References, tables of contents and page numbers need several passes. The loop in edit_typeset_rep::typeset (in

Edit/Editor/edit_typeset.cpp

) calls typeset_sub, which calls the typesetter, as long as the pass was complete and there remain undefined (env->missing) or redefined (env->redefined) labels whose number decreases. Each new pass is forced by ::notify_assign (ttt, path(), ttt->br->st), which invalidates the whole document. After the loop, unused references are removed with clean_unused, and remaining problems are reported as warnings.

3.6.The environment at a given position

The editor frequently needs the environment at the cursor (to know the current font, mode, language, etc.). This is done by edit_typeset_rep::typeset_exec_until (path p), which caches the result per path in the hashmap cur. Unless the experimental option enable_fastenv is set, it calls the global exec_until (ttt, p / rp), and hence bridge_rep::exec_until. A bridge which is up to date does not re-execute its subtree: if p is at its right border, it just applies its cached changes; otherwise my_exec_until descends (for documents, the preceding paragraphs are replayed through their cached changes). Corrupted bridges fall back on env->exec_until (st, p), which evaluates the tree (see macro expansion).

4.From the typeset box to the screen

4.1.Tracking the modified regions

After a pass, only the parts of the screen which actually changed need to be repainted. This is achieved without comparing box trees. The lines of the paragraphs (phrase_box,

concat_boxes.cpp

) and the remember boxes log where they are placed: each has a placed_box (

boxes.hpp

), the log, the pass and the origin at which it was placed last.

The least upper bound of the remaining rectangles is returned in the four coordinates, enlarged by the rectangles of pages whose background color changed (collect_page_colors compared with old_bgs). Since cached paragraphs keep their boxes, typing in a paragraph usually only invalidates that paragraph, plus the following ones if its height changed.

Placing every line at every pass costs as many lines as the document has (6.6 ms for 50000 lines). The chunks of lines of a long document (chunk_lines in

bridge.cpp

) are therefore group boxes (group_box,

change_boxes.cpp

): a group which is placed where it was in the previous pass does not place its lines again, since a box does not change and a chunk which is kept is the same box. A box which logs is therefore placed until it is unplaced (not only when it was placed in the previous pass). The lines which a group places are kept by the group, not by the log: the log would else unplace them at the next pass, in which the group does not place them. A group which is gone unplaces the lines in it which were not placed again (box_rep::unplace recurses). With TEXMACS_TYPESET_CACHES=check, check_repaint compares the rectangles to repaint with all the boxes placed, before and after.

4.2.The editor side

The editor class edit_typeset_rep (

Edit/Editor/edit_typeset.cpp

) owns the typesetter ttt, created in its constructor on the subtree subtree (et, rp) of the edit tree. The relevant methods are:

void edit_typeset_rep::typeset_preamble ()
(compute the initial environment)

Resets the environment to the default values, applies the style files (typeset_style_use_cache) and the document's initial environment init, and stores the result in pre. It is called when the style or the initial environment changes.

void edit_typeset_rep::typeset_prepare ()
(start a pass)

Restores the environment pre before a typesetting pass or a call to exec_until.

void edit_typeset_rep::typeset (SI& x1, SI& y1, SI& x2, SI& y2)
(typeset and report the changed region)

Runs typeset_sub (which calls ::typeset (ttt, …) and stores the box in eb) as many times as needed for the references to stabilize, and returns the union of the changed regions. If typesetting throws an exception, typeset_sub replaces the document by an empty one.

void edit_typeset_rep::typeset_invalidate (path p)
(force re-typesetting of a subtree)

Notifies an assignment of the subtree at p to itself, so that its bridges become corrupted. typeset_invalidate_all does the same for the whole document after recomputing the preamble; it is triggered by changes of the environment (THE_ENVIRONMENT). typeset_invalidate_players invalidates animations.

The actual update of the screen happens in edit_interface_rep::apply_changes (

Edit/Interface/edit_interface.cpp

). When the change flags contain THE_TREE or THE_ENVIRONMENT, it clears the cache of cursor environments (typeset_invalidate_env), calls typeset (x1, y1, x2, y2), and invalidates the returned rectangle (enlarged by two pixels) with invalidate. The widget then repaints that region of eb at the next redraw.

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".