![]() |
Renderers at work | ![]() |
The box side is documented in the boxes document; the interaction with the renderer is concentrated in a few methods of box_rep (
Typeset/boxes.hpp
,
Typeset/Boxes/Basic/boxes.cpp
):
Paint the box itself, not its children, in its local coordinates.
Change the renderer state before the box and its children are painted, and restore it afterwards. Examples: clipping (clip_box_rep), transformations (transformed_box_rep), page numbers (page_box_rep), background ornaments (art_box_rep, highlight_box_rep).
The driver of the traversal.
Repaint only the backgrounds of a region, used by the editor to clear parts of the page with the correct background.
The default box_rep::redraw reads, in abridged form:
void
box_rep::redraw (renderer ren, path p, rectangles& l) {
if ((nr_painted&15) == 15 && ren->is_screen && gui_interrupted (true)) return;
ren->move_origin (x0, y0);
SI delta= ren->retina_pixel;
if (ren->is_visible (x3- delta, y3- delta, x4+ delta, y4+ delta)) {
…
pre_display (ren);
redraw the children, starting near the path p
if (interrupted) clear_incomplete (…);
else {
l= rectangle (x3+ ren->ox, y3+ ren->oy, x4+ ren->ox, y4+ ren->oy);
display (ren);
if (!ren->is_screen) display_links (ren);
if (nr_painted < 15) ren->apply_shadow (x1, y1, x2, y2);
nr_painted++;
}
post_display (ren);
}
ren->move_origin (-x0, -y0);
} |
Notice the following points:
The visibility test uses the ink extents (x3, y3, x4, y4), enlarged by one retina_pixel to compensate for truncation.
Children are drawn before the parent's own display, so that display of a composite box draws on top of its children; backgrounds are painted in pre_display or through redraw_background.
The rectangles returned in l are in absolute coordinates (the origin is added); they describe what has actually been repainted and are used by the editor to copy only the completed parts of the shadow to the screen when drawing was interrupted.
On screen, every sixteenth box checks for pending user events with gui_interrupted; if drawing is interrupted, the remaining rectangles are invalidated again and drawn later.
Graphics boxes (
Typeset/Boxes/Graphics/
) and decorations use line, lines, arc, polygon and the fill routines; effect_box_rep uses shadow (picture&, …) and draw_picture (see the renderer API); image boxes use draw_scalable.
When the document changes, the editor calls edit_interface_rep::invalidate (SI x1, SI y1, SI x2, SI y2) with a rectangle in document coordinates. It multiplies the rectangle by magf and sends it to the widget with send_invalidate. The Qt widget qt_simple_widget_rep receives it as SLOT_INVALIDATE, converts it to device pixels with the main renderer (set_origin, outer_round, decode) and records it with invalidate_rect.
Periodically, the GUI calls qt_simple_widget_rep::repaint_invalid_regions. This routine updates the backing pixmap (scrolling and resizing it, and invalidating the uncovered parts), obtains the renderer with get_renderer () (the shared the_qt_renderer (device_pixel_ratio ()) on which begin has been called with the backing pixmap), and then, for each invalid rectangle:
ren->set_origin (ox, oy);
ren->encode (r->x1, r->y1);
ren->encode (r->x2, r->y2);
ren->set_clipping (r->x1, r->y2, r->x2, r->y1);
handle_repaint (ren, r->x1, r->y2, r->x2, r->y1);
if (gui_interrupted ()) invalidate_rect (r0->x1, r0->y1, r0->x2, r0->y2); |
Finally it calls ren->end () and schedules the repainted region for display on the screen. At this level the renderer is at its reset zoom, so that the SI coordinates are those of the window, i.e. document coordinates multiplied by magf.
The other ports follow the same scheme with their own backing store: ns_simple_widget_rep::repaint_invalid_regions in Cocoa (
ns_simple_widget.mm
), vue_simple_widget_rep::repaint_invalid_regions in Vue (
vue_widget.cpp
, on an MuPDF pixmap or a GPU texture), and, for the Widkit ports (X11, SDL, Qtwk), the window (x_window_rep::repaint_invalid_regions and its counterparts), which sends repaint events to the widgets; simple_widget_rep::handle_repaint (
simple_wk_widget.cpp
) then reaches the editor.
edit_interface_rep::handle_repaint (in
Edit/Interface/edit_repaint.cpp
) divides the rectangle by magf and calls draw_with_stored. The main steps are:
If the requested region is entirely contained in stored_rects, the region is restored from the stored shadow instead of being retypeset and redrawn. This backing store is only filled while editing graphics (inside_active_graphics ()), so that the graphical cursor and the object being edited can be redrawn quickly over an unchanged document. Otherwise it calls draw_with_shadow; unless drawing was interrupted, it then calls draw_post on the shadow and copies the result to the window with win->put_shadow.
Prepare the shadow with win->new_shadow (shadow) and win->get_shadow (shadow, …), set the zoom factor of both renderers to the editor's zoomf, call draw_pre and draw_text, and reset the zoom factors. If drawing was interrupted, only the rectangles which were completely drawn are copied to the window.
Paint the background (draw_background, which uses clear_device, box_rep::clear or clear_pattern) and the area around the page (draw_surround).
Set the background and call eb->redraw (ren, …) on the root box eb, starting from the path of the cursor so that the neighbourhood of the cursor is drawn first.
Draw the overlays which are not part of the box tree: context and focus rectangles (draw_env), the selections (draw_selection), active graphics (draw_graphics), the cursor (draw_cursor) and the keyboard help (draw_keys).
Other widgets follow the same protocol at a smaller scale: for instance box_widget_rep::handle_repaint in
Texmacs/Window/tm_button.cpp
redraws a single box, and QTMImpressIconEngine repaints a widget into an icon using a temporary qt_renderer_rep.
Printing uses the same box traversal with a printer renderer, created by the factory function declared in
Graphics/Renderer/printer.hpp
:
Return a PDF renderer (pdf_hummus_renderer) if PDF_RENDERER is defined, use_pdf () holds, and either the file has the suffix pdf or use_ps () does not hold. Otherwise return a PostScript printer_rep. The paper size is in centimeters; page_type is normalized through the Scheme routine standard-paper-size. The file is written when the renderer is deleted.
edit_main_rep::print_doc (in
Edit/Editor/edit_main.cpp
) typesets the document for paper at the printing resolution, creates the printer, sets the metadata, and then for each page:
tree bg= env->read (BG_COLOR);
ren->set_background (bg);
if (bg != "white" && bg != "#ffffff")
ren->clear_pattern (0, (SI) -h, (SI) w, 0);
rectangles rs;
the_box[0]->sx(i)= 0;
the_box[0]->sy(i)= 0;
the_box[0][i]->redraw (ren, path (0), rs);
if (i<end-1) ren->next_page (); |
and finally tm_delete (ren). When Ghostscript support is compiled in (USE_GS), print_doc may print to a temporary file in the other format and convert it with gs_to_pdf or gs_to_ps. print_to_file, print_buffer and export_ps are thin wrappers around print_doc.
Snippets (e.g. for exporting a selection as an image) go through edit_main_rep::print_snippet, which calls either make_eps (url name, box b, int dpi) (a one page printer renderer of page type user fitted to the ink extents of the box) or, for bitmap formats with Qt, Vue or Cocoa (not with X11 and SDL), make_raster_image (url name, box b, double zoomf), which creates a native_picture, draws on it with a picture_renderer and saves it with save_picture. Both are defined in
Typeset/Boxes/Basic/boxes.cpp
.