GNU TeXmacsUser manualReferenceAboutEnglish
Renderer implementations

1.The implementations

1.1.Overview

Class and files
Device
Used by
basic_renderer_rep (Graphics/Renderer/basic_renderer.*)
common base of screen renderers
all but X11
qt_renderer_rep (Plugins/Qt/qt_renderer.*)
QPainter
Qt, Qtwk
qt_proxy_renderer_rep, qt_shadow_renderer_rep (Plugins/Qt/qt_renderer.*)
shadows
Qt
qt_image_renderer_rep (Plugins/Qt/qt_picture.*)
QImage of a picture
Qt, Qtwk (without MuPDF)
mupdf_renderer_rep (Plugins/MuPDF/mupdf_renderer.*)
MuPDF pixmap
SDL, Vue; pictures of Qt
gpu_renderer_rep (
Plugins/Vue/vue_gpu.cpp
)
OpenGL texture or window, ThorVG
Vue
ns_renderer_rep (Plugins/NS/ns_renderer.*)
NSGraphicsContext (CoreGraphics)
Cocoa
x_drawable_rep (Plugins/X11/x_drawable.*,
x_shadow.cpp
,
x_picture.cpp
)
X11 window or pixmap
X11
cairo_renderer_rep (Plugins/Cairo/cairo_renderer.*)
Cairo context
Qt with USE_CAIRO
printer_rep (Graphics/Renderer/printer.*)
PostScript file
all
pdf_hummus_renderer_rep (Plugins/Pdf/pdf_hummus_renderer.*)
PDF file
Qt, Cocoa
mupdf_pdf_renderer_rep (Plugins/MuPDF/mupdf_pdf_renderer.*)
PDF file
with MuPDF, on demand

The GUI directory is chosen at configuration time: ./configure --with-gui=… takes Qt (or Qt6 with --enable-qt-new), Qtwk (with the renderer and the pictures of

Plugins/Qt

), X11, SDL, Vue or NS; the CMake build (TEXMACS_GUI) knows Qt, Vue, SDL and X11.

Plugins/Cairo

is always compiled, its code guarded by USE_CAIRO.

Plugins/MuPDF

is compiled with MUPDF_RENDERER for Qt, SDL and Vue when MuPDF is found (it is required by the last two), never for X11 and Cocoa, whose pictures would clash with it;

Plugins/Pdf

(PDF_RENDERER, PDFHummus) only with Qt and Cocoa (with CMake, only for Qt without MuPDF). The renderers of

Plugins/Qt

and

Plugins/Qt6

are kept identical (rounded_rectangle included); like the rest of the two directories, they are synchronized by hand.

1.2.basic_renderer_rep

basic_renderer_rep derives from renderer_rep and adds the device size w, h, a pencil pen, a foreground brush fg_brush and a background brush bg_brush, with the obvious accessors, together with begin (void* handle) and end () to bracket a drawing session on a native device, color helpers rgb and get_rgb, and no-op shadow operations. The file also defines basic_character, the key of glyph caches, and gui_interrupted. Note that

basic_renderer.cpp

is compiled for all ports but X11 (it is guarded by !defined(X11TEXMACS)): the X11 port has its own gui_interrupted.

1.3.The Qt renderer

qt_renderer_rep draws with a QPainter* painter. A single instance, returned by the_qt_renderer (double pixel_ratio), is shared by all widgets; with Qt 6 it adapts its pixel_ratio and zoom when it is requested with another ratio. begin (handle) opens the painter on a QPaintDevice (the backing pixmap, or a QImage in headless mode) and records its size.

Particularities:

1.4.The PostScript renderer

printer_rep generates DSC conforming PostScript in memory (the strings prologue and body) and writes the file in its destructor. It uses the TeX dvips prologues tex.pro, special.pro, color.pro and texps.pro from $TEXMACS_PATH/misc/convert/, and defines short PostScript procedures for its primitives (PS_LINE, PS_FILL, PS_ARC, etc.). Its pixel equals PIXEL: coordinates are output in dots of the printing resolution. Glyphs are collected per font by make_tex_char, and fonts of more than 256 glyphs are split into several PostScript fonts. At the end, generate_tex_fonts writes the font definitions into the prologue: for tt fonts whose file is a pfb Type 1 font (except on Windows) the font program itself is embedded, and otherwise the glyphs are emitted as bitmap characters in the format of the dvips prologue. Colors with an alpha component are blended with the background color in set_pencil; genuine transparency (through pdfmark) is only attempted when the preference experimental alpha is on. Pictures are embedded as EPS via picture_as_eps, and scalable images by including their PostScript code.

1.5.The PDF renderer

The whole export path, the fonts and the text layer, links, bookmarks and metadata are described in more detail in PDF export.

pdf_hummus_renderer_rep writes PDF through the PDFHummus library (a PDFWriter, the current PDFPage and PageContentContext). Unlike the screen renderers it keeps the \(y\)-axis pointing upwards: its private helpers to_x and to_y add the origin and divide by pixel without changing the sign. Particularities:

1.6.The X11 renderer

x_drawable_rep derives directly from renderer_rep, and draws on an X11 Drawable (a window or a pixmap) with a graphics context gc. It is the reference implementation of the shadow protocol: new_shadow allocates a pixmap drawable of the same size, and get_shadow, put_shadow and fetch are all XCopyArea calls. The primitives are plain Xlib calls (XDrawLine, XDrawArc, XFillPolygon, etc.), so strokes are not anti-aliased, and draw_picture ignores its alpha argument.

1.7.The MuPDF renderers

mupdf_renderer_rep (

mupdf_renderer.cpp

) draws with the Fitz library of MuPDF into an fz_pixmap, which is the picture mupdf_picture_rep (

mupdf_picture.cpp

). Strokes and fills are anti-aliased; glyphs are drawn from the bitmaps of TeXmacs; shadows are pixmaps. It is the screen renderer of the SDL port (one backing pixmap per window, see

sdl_window.cpp

) and of the Vue port without the GPU, and it draws the pictures (icons, images) of Qt and Qtwk builds with MuPDF, in place of the QImages of

qt_picture.cpp

.

mupdf_pdf_renderer_rep (

mupdf_pdf_renderer.cpp

) is a printer which writes PDF with MuPDF, a prototype alternative to PDFHummus: printer in

printer.cpp

takes it when use_mupdf_pdf () holds, that is with the preference native pdf renderer set to mupdf, with TEXMACS_PDF_MUPDF=1, or always in the browser.

1.8.The renderers of the Vue port

The Vue port draws its widgets in immediate mode with Clay and its documents with one of two renderers, chosen once at start-up (vue_gpu_enabled):

A third path, through SDL's own renderer and the example renderer of Clay (vue_sdl_window_rep), is unused. A CMake build without THORVG_DIR has no ThorVG, hence no GPU path.

1.9.The Cocoa renderer

ns_renderer_rep (

ns_renderer.mm

) derives from basic_renderer_rep and draws with CoreGraphics in the NSGraphicsContext given to begin, that of the view (TMView) or that of a bitmap for the pictures (

ns_picture.mm

). It keeps a stack of clippings per context (clip_pushed) and reapplies its state after a context is saved and restored.

1.10.The Cairo renderer

cairo_renderer_rep (used by qt_simple_widget_rep::get_renderer when USE_CAIRO is defined) derives from basic_renderer_rep. It has not followed the recent evolution of the interface (shadows, transformations, pictures); treat it as a starting point rather than as working code. The former aqua_renderer_rep and cg_renderer_rep are gone: the Cocoa port now draws with ns_renderer_rep.

2.Writing a new renderer

A new renderer is a subclass of renderer_rep, or, for a screen device, of basic_renderer_rep. The following methods are pure virtual and must be implemented:

In practice, the following ones must also be overridden:

If the renderer comes with a new GUI back-end, that back-end must also provide the free functions native_picture, picture_renderer, load_picture, as_native_picture and save_picture, and adapt the conditional stubs at the end of

renderer.cpp

. A printer that should be selectable for export must be hooked into the factory printer in

printer.cpp

.

A useful way to proceed is to start from the Qt renderer, which shows how to map each primitive to a modern 2D graphics API (the MuPDF and Cocoa renderers do the same with Fitz and CoreGraphics), and from the PDF renderer for the printer-specific parts.

3.Pitfalls

The \(y\)-axis

Logical \(y\) grows upwards and device \(y\) downwards. A rectangle (x1, y1, x2, y2) decodes to a device rectangle with top left corner (x1, y2). Forgetting this is the most common source of off-by-one-rectangle bugs. The PDF renderer is an exception, as its device axis also points upwards.

Relative and absolute coordinates

Drawing routines and get_clipping/set_clipping use coordinates relative to the origin, whereas the fields cx1, …, cy2 and the rectangles returned by box_rep::redraw are absolute. Code which reads the fields directly (as the Qt clear and fill do) must subtract ox and oy.

Rounding

Integer decode rounds towards \(- \infty\), and rectangles should be passed through outer_round (as set_clipping and the shadow routines do) before being decoded; otherwise one-pixel seams or stale lines remain after partial repaints. The visibility test in box_rep::redraw enlarges boxes by retina_pixel for the same reason. Note that the rounding grid is that of retina_pixel, not pixel.

Pixel ratio

On high density screens pixel is the size of a device pixel and retina_pixel the size of a logical pixel. Use pixel for the finest detail (hairlines) and retina_pixel for anything related to the layout on screen. With Qt older than 6 the scaling is controlled by the global retina_factor; with Qt 6 by the pixel_ratio of each renderer, and a shadow must be recreated when the ratio changes (which new_shadow does).

Zoom and state

The zoom factor changes the meaning of ox, oy, cx1, …: always change it with set_zoom_factor and reset_zoom_factor, which rescale these fields, and restore the previous zoom when you are done. Remember that font_rep::draw temporarily overwrites zoomf, pixel, ox, etc. while glyphs are drawn.

Clipping balance

Every narrowing set_clipping or extra_clipping must be matched by a set_clipping (…, true) restoring the previous region (or use clip/unclip). On screen an unbalanced call just leaves a wrong clip, but printers maintain a graphics state stack and produce corrupted output.

Interruptions

On screen, redrawing may stop at any time when the user types. Code in pre_display and post_display must be symmetric, and a box overriding redraw should respect gui_interrupted as effect_box_rep::redraw does.

Lifetime

Renderers are raw pointers. Shadows are owned by whoever calls new_shadow, picture renderers by the caller of shadow (picture&, …) or picture_renderer, and printer renderers must be deleted to produce their file.

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