xtk_mathtext.h
Lays out and draws one LaTeX math formula.
XtkMathText turns the source of one LaTeX math formula (what goes between $...$ or $$...$$) into a laid-out formula that a program draws into any drawable. It is not a widget: it has no window and takes no events. XtkRichText uses it for the math in a document; a program can use it to put a formula in its own drawing.
Use. xtk_mathtext_create() parses and lays out the formula at once, in the fonts it is given. xtk_mathtext_get_size() gives its width, height and baseline, so the program can place it, and xtk_mathtext_draw() paints it at a top-left corner, as often as needed. xtk_mathtext_destroy() frees it. To change the formula or its fonts, make a new one.
What it understands.
- Letters (set in italic), digits and other characters;
^and_for superscripts and subscripts, alone or together. - Groups in braces,
\frac,\dfrac,\tfrac(one size smaller),\binom,\sqrtand\sqrt[n],\text{...}(upright text). \leftand\rightwith(,[,\{,|,.and the other usual delimiters, sized to what they hold.- The environments
matrix,pmatrix,bmatrix,vmatrix,cases,align(andalign*),gatherandeqnarray, with&between cells and\\between rows; also\cases{...}. An unknown environment is set asmatrix. - Accents:
\hat,\widehat,\bar,\vec,\ddot(two dots) and its one-dot form,\tilde,\widetilde,\acute,\grave,\overline,\underline,\overrightarrow,\overbraceand\underbrace. - Big operators:
\sum,\prod,\coprod,\int,\oint,\bigcup,\bigcap,\bigoplus,\bigotimes,\biguplus, and\lim. - Function names set upright:
\sin,\cos,\tan,\sec,\csc,\cot,\log,\ln,\exp,\min,\max,\det,\gcd,\arg,\sinh,\cosh,\tanh,\deg,\dim,\ker,\hom. - The Greek letters, and the usual relations, operators, arrows, dots and symbols (
\times,\leq,\in,\infty,\rightarrow,\ldots,\langle, ...). - Spacing:
\quadand\qquad(two spaces), and\,,\;,\:and\!, which all give one space. \{,\},\$,\%,\&,\_,\#and other escaped characters, drawn as themselves.
Bad input. The formula is never refused. An unknown command is drawn as its name in upright text, and a missing brace or \right is closed where the source ends. Only when the formula is too large or too deeply nested to lay out is the whole source drawn as plain text.
Fonts. The formula is set in the fonts the caller passes (they are the caller's and must outlive the XtkMathText). Smaller fonts for scripts (0.71 and 0.5 of the size), a larger one for big operators in display style (1.4 times), and fallback fonts for symbols the font lacks (DejaVu Sans, Noto Sans Symbols, Noto Sans Symbols 2, STIX Two Math) are opened by the formula itself and closed by xtk_mathtext_destroy(). With an OpenType math font (for example "Latin Modern Math" or "STIX Two Math"), tall delimiters use the font's own size variants and assemblies, and italic letters use the Unicode mathematical italic characters.
Example.
XftFont *font = xtk_config_open_font(display, screen, "DejaVu Serif", 18);
XftFont *italic = xtk_config_open_font(display, screen, "DejaVu Serif:slant=100", 18);
XtkMathText *formula = xtk_mathtext_create(display, screen,
"x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}", 1, font, italic, NULL, NULL);
int width, height, baseline;
xtk_mathtext_get_size(formula, &width, &height, &baseline);
// On each Expose of the window, with an XftDraw for it:
xtk_mathtext_draw(formula, window, gc, xft_draw, 20, 20, text_pixel);
xtk_mathtext_destroy(formula);
XftFontClose(display, italic);
XftFontClose(display, font);
*
Limitations.
- It handles only the subset listed above; there are no macros (
\newcommand), no\color, no\mathbbor other font commands, and all four small spaces have one width. - A text run (a number or
\text{...}) is drawn to at most 95 bytes; the plain-text fallback is drawn whole. - A matrix or environment has at most 16 rows and 12 columns; more are dropped. A row with fewer cells than the widest row is padded with empty cells at its end.
- Use it from one thread only, the one that draws, as with every xtk widget: it keeps one process-wide cache of a math font's tables (see
xtk_mathtext_invalidate_font_cache()), and Xft is not called from two threads at once either.
#include "xtk_mathtext.h"
Types: XtkMathText
Functions: xtk_mathtext_create, xtk_mathtext_destroy, xtk_mathtext_invalidate_font_cache, xtk_mathtext_get_size, xtk_mathtext_draw
Types
XtkMathText
typedef struct XtkMathText XtkMathText;
A laid-out formula; opaque, made by xtk_mathtext_create() and freed by xtk_mathtext_destroy().
Functions
xtk_mathtext_create
XtkMathText *xtk_mathtext_create(
Display *display,
int screen,
const char *latex_source,
int display_mode,
XftFont *base_font,
XftFont *base_font_italic,
XftFont *math_font,
XftFont *math_font_italic);
Parse a LaTeX math formula and lay it out.
| Parameter | Description |
|---|---|
display |
The X display |
screen |
The screen number |
latex_source |
The formula's LaTeX source, without the $ or $$ around it; NULL is taken as an empty formula |
display_mode |
0 for inline style ($...$): limits of big operators go to the side as scripts. 1 for display style ($$...$$): big operators are drawn larger, with their limits stacked above and below. |
base_font |
The upright font to set the formula in; owned by the caller and must outlive the formula. Must not be NULL. |
base_font_italic |
The italic of the same family and size, for letters; owned by the caller. NULL uses base_font. |
math_font |
NULL to set the formula in base_font and base_font_italic. Otherwise an OpenType math font (such as "Latin Modern Math") that sets the whole formula instead, at its own size; letters then use the mathematical italic characters. It is owned by the caller, like base_font: open it once and reuse it for every formula, since opening a font is slow. Before closing it, call xtk_mathtext_invalidate_font_cache(). |
math_font_italic |
The italic of math_font, or NULL to use math_font (the usual case, since math fonts have no italic face). Not used when math_font is NULL. |
Returns: The formula, or NULL if display or base_font is NULL or memory runs out. Malformed LaTeX does not return NULL (see "Bad input").
xtk_mathtext_destroy
void xtk_mathtext_destroy(XtkMathText *mathtext);
Free a formula and the fonts it opened for itself.
The fonts given to xtk_mathtext_create() are not closed.
| Parameter | Description |
|---|---|
mathtext |
The formula (NULL is ignored) |
xtk_mathtext_invalidate_font_cache
void xtk_mathtext_invalidate_font_cache(const XftFont *font);
Drop the cached math tables of a font that is about to be closed.
The tables of the last math font used for tall delimiters are kept in one process-wide cache, found by the font's pointer. Call this just before XftFontClose() of any font that was passed as math_font: a font opened later can get the same pointer and would otherwise be given the old font's tables. It does nothing when the cache holds another font.
| Parameter | Description |
|---|---|
font |
The font about to be closed |
xtk_mathtext_get_size
void xtk_mathtext_get_size(
const XtkMathText *mathtext,
int *width,
int *height,
int *baseline_from_top);
The size of the laid-out formula.
| Parameter | Description |
|---|---|
mathtext |
The formula; if NULL all three values are 0 |
width |
Receives the width in pixels (may be NULL) |
height |
Receives the height in pixels, above and below the baseline together (may be NULL) |
baseline_from_top |
Receives the distance in pixels from the top of the formula to its baseline (may be NULL). Use it to line the formula up with the text around it: draw it at the text's baseline minus this value. |
xtk_mathtext_draw
void xtk_mathtext_draw(
const XtkMathText *mathtext,
Drawable target,
GC gc,
XftDraw *xft_draw,
int x,
int y,
unsigned long foreground_pixel);
Draw the formula.
Glyphs are drawn through xft_draw, and rules, radicals and hand-drawn delimiters through gc. The gc's foreground is set to foreground_pixel and left so. Nothing behind the formula is filled.
| Parameter | Description |
|---|---|
mathtext |
The formula (NULL is ignored) |
target |
The drawable to draw into |
gc |
A graphics context for target, used for lines and shapes |
xft_draw |
An XftDraw for the same drawable, used for glyphs; NULL draws the lines only |
x |
The left edge of the formula in target |
y |
The top edge of the formula in target (not its baseline; add baseline_from_top from xtk_mathtext_get_size() to get that) |
foreground_pixel |
The pixel value of the color for both glyphs and lines |
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats