![]() |
Renderer implementations | ![]() |
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.
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.
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:
Lines, polylines, arcs and polygons are anti-aliased (QPainter::Antialiasing), whereas fill, clear, draw_triangle and bitmaps are not. Rectangles are therefore pixel-exact, while strokes are drawn at sub-pixel positions using the floating point decode.
The pen width is pen->get_width () / pixel device pixels, as a floating point number. Joins are always round, caps are round or square.
Pattern pencils and brushes are implemented as QBrushes textured with the pattern image, loaded with get_image (url u, int w, int h, tree eff, SI pixel) and translated to the device position of the logical origin.
Glyphs are rasterized once per color and font, and cached in character_image; images loaded from files are cached by get_image. del_obj_qt_renderer empties the static caches before Qt is shut down.
draw_clipped draws a bitmap at a logical point after subtracting one pixel in \(y\) (top-left origin to bottom-left origin conversion).
With Qt 6, clear_device paints a neutral checkerboard.
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.
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:
Fonts are embedded natively whenever possible: make_pdf_font looks for the font file with tt_font_find and loads it with PDFWriter::GetFontForFile. Fonts which cannot be embedded (for instance Metafont generated pk fonts) are converted into bitmap Type 3 fonts (class t3font), in chunks of glyphs.
Text is written inside BT/ET blocks (begin_text, end_text); every change of clipping or transformation closes the current text block.
Transparency is supported through extended graphics states (select_alpha), and pattern brushes through PDF patterns.
Pictures are saved as temporary png files (with QImage under Qt, save_picture under Cocoa) and embedded, cached by get_unique_id; image files are embedded directly by draw_scalable.
Hyperlinks, destinations, outlines and metadata are written when the document is finished.
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.
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.
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):
Without the GPU, every window has an MuPDF pixmap as backing store, drawn by mupdf_renderer_rep (vue_sdl_mupdf_window_rep in
vue_gui.cpp
).
With the GPU (./configure --with-thorvg=…, or THORVG_DIR with CMake, which define USE_THORVG; on unless TEXMACS_VUE_GPU=0, and abandoned when no OpenGL context can be made), gpu_renderer_rep (
vue_gpu.cpp
) draws into OpenGL textures (WebGL 2 in the browser): glyphs from an atlas of the bitmaps of TeXmacs, plain fills from the same atlas, pictures as textures, and lines, polygons, arcs and rounded rectangles with the OpenGL engine of ThorVG. The backing store of an editor is a texture (gpu_picture_rep), repainted incrementally as the pixmap was.
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.
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.
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.
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:
State: get_pencil, set_pencil, get_background, set_background (provided by basic_renderer_rep).
Drawing: clear_device, draw, line, lines, clear, fill, arc, fill_arc, polygon.
Shadows: fetch, new_shadow, delete_shadow, get_shadow, put_shadow, apply_shadow (no-ops in basic_renderer_rep; a printer can leave them empty).
In practice, the following ones must also be overridden:
set_clipping, to clip the device as well; call the base implementation first so that cx1, …, cy2 are kept up to date, and honor the restore flag if the device clipping is a stack.
draw_picture, whose default aborts.
set_transformation and reset_transformation, if rotated or scaled content should be rendered correctly.
set_brush, for pattern fills, and set_zoom_factor, if the device has its own scaling.
For printers: is_printer, next_page, shadow, draw_scalable, and the hyperlink and metadata methods; the destructor must finish the output.
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.
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.
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.
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.
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).
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.
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.
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.
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.