![]() |
TeXmacs fonts | ![]() |
This chapter describes the font classes of the program. How a user or a style file selects fonts, which files describe them and how virtual fonts are written is explained in the reference chapter fonts, from selection to glyph, and the mathematical fonts shipped with TeXmacs are presented in the section mathematical fonts of the user manual. The support of OpenType fonts, and in particular of the MATH table, is the subject of the chapter OpenType fonts.
The way TeXmacs handles fonts is quite different from classical text editors and even from TeX. Let us first analyze some classical ways of conceiving fonts.
Physical fonts are just given by the name of a file, which contains a character set, i.e. a list of bitmaps. Usually the size of a character set is limited by 256 (or 65536).
True type fonts essentially work in the same way, except that the bitmaps can now be computed for any desired size.
In the X-window system, the name of the font is replaced by a more systematic name, which explicitly contains a certain number of font parameters, such as its size, series and shape. This makes it easier for applications to select an appropriate font. However, character sets are still limited in size.
In TeX, symbols are seen as commands, which select an appropriate physical font (which corresponds to a .tfm and a .pk file), based on symbol font declarations and environment variables (such as size, series and shape).
Clearly, among all these methods, TeX provides the largest flexibility. However, philosophically speaking, we think that it also has some drawbacks:
There is no distinction between usual commands and commands to make symbols: the current time might be considered as a symbol.
The encoding of the font is fixed by the names of the commands. For instance, for mathematical symbols, no clean general encoding scheme is provided, except the default naming of symbols by commands.
For beginners, it remains extremely hard to use non standard fonts.
Actually, in TeX, the notion of “the current font” is ill-defined: it is merely the superposition of all character generating commands.
Philosophically speaking, we think that a font should be characterized by the following two essential properties:
A font associates graphical meanings to words. The words can always be represented by strings.
The way this association takes place is coherent as a function of the word.
By a word, we either mean a word in a natural language, or a sequence of mathematical, technical or artistic symbols.
This way of viewing fonts has several advantages:
A font may take care of kerning and ligatures.
A font may consist of several “physical fonts”, which are somehow merged together.
A font might in principle automatically build very complicated glyphs like hieroglyphs or large delimiters from words in a well chosen encoding.
A font is an irreducible and persistent entity, not a bunch of commands whose actions may depend on some environment.
Notice finally that the “graphical meaning” of a word might be more than just a bitmap: it might also contain some information about a logical bounding box, appropriate places for scripts, etc. Similarly, the “coherence of the association” should be interpreted in its broadest sense: the font might contain additional information for the global typesetting of the words on a page, like the recommended distance between lines, the height of a fraction bar, etc.
All text strings in TeXmacs consist of sequences of either specific or universal symbols. A specific symbol is a character, different from '\0', '<' and '>', which is interpreted in the Cork encoding. A universal symbol is a string starting with '<', followed by an arbitrary sequence of characters different from '\0', '<' and '>', and ending with '>'. Examples are named symbols like <alpha> or <leqslant>, and arbitrary Unicode characters like <#2212>. The meaning of symbols does not depend on the particular font which is used, but different fonts may render them in a different way. It is the responsibility of each font to translate symbols into its own internal encoding.
Universal symbols can also be used to represent mathematical symbols of variable sizes like large brackets. The point here is that the shapes of such symbols depend on certain size parameters, which can not conveniently be thought of as font parameters. This problem is solved by letting the extra parameters be part of the symbol. For instance, "<left-(-0>" is the usual opening bracket and "<left-(-1>" a slightly larger one. Similarly, big operators are represented by symbols like "<big-sum-1>" (small version) and "<big-sum-2>" (display version). The typesetter determines the appropriate size of a delimiter as a function of the height of the delimited expression (see get_delimiter in
Typeset/Boxes/Basic/text_boxes.cpp
). A font which knows the size variants of its glyphs, as an OpenType math font does, is first asked directly with get_rubber_variant (for heights) and get_wide_variant (for the widths of wide accents and arrows, get_wide), and the numbered names are only searched when it cannot tell; see stretchable glyphs.
The main abstract font class is defined in
Graphics/Fonts/font.hpp
. Here follows an abridged version of its representation class:
struct font_rep: rep<font> {
int type; // font type
int math_type; // normal, Stix, TeX Gyre or OpenType
SI size; // requested size
SI design_size; // design size in points/256
SI display_size; // display size in points/PIXEL
double slope; // italic slope
space spc; // usual space between words
space extra; // extra space at end of words
SI sep; // separation space between close components
SI y1, y2; // bottom and top y positions
SI yx; // height of the x character
SI yfrac; // vertical position fraction bar
SI ysub_lo_base, …; // positioning of scripts
SI wpt, hpt; // width and height of one point in font
SI wfn; // wpt * design size in points
SI wline; // width of fraction bars and so
SI wquad; // quad space
…
bool ot_math; // parameters read from a MATH table
SI frac_rule_thickness, …; // about forty MATH constants
virtual bool supports (string c) = 0;
virtual void get_extents (string s, metric& ex) = 0;
virtual void get_xpositions (string s, SI* xpos);
virtual void draw_fixed (renderer ren, string s, SI x, SI y) = 0;
virtual font magnify (double zoomx, double zoomy) = 0;
virtual void draw (renderer ren, string s, SI x, SI y);
virtual double get_left_slope (string s);
virtual double get_right_slope (string s);
virtual SI get_left_correction (string s);
virtual SI get_right_correction (string s);
virtual SI get_lsub_correction (string s);
virtual SI get_lsup_correction (string s);
virtual SI get_rsub_correction (string s);
virtual SI get_rsup_correction (string s);
virtual SI get_rsup_correction_at (string s, SI h);
virtual bool get_rubber_variant (string s, SI height, string& r);
virtual bool get_wide_variant (string s, SI width, string& r);
virtual bool get_top_accent (string s, SI& x);
virtual bool get_feature_variant (string, string, int, string&);
…
virtual glyph get_glyph (string s);
virtual int index_glyph (string s, font_metric& fnm, font_glyphs& fng);
}; |
The main abstract routines are get_extents and draw_fixed. The first routine determines the logical and ink bounding boxes of the graphical representation of a word (in a structure of type metric), the second one draws the string on a renderer (which may be the screen, a printer or a PDF file). The routine supports tells whether a given symbol is available in the font and magnify constructs a magnified version of the font. Some fonts can also export their glyphs as bitmaps (get_glyph) or as indexes into tables of glyphs and metrics (index_glyph), which are used by the renderers. Fonts are cached by name, so that each font is only constructed once.
The additional data are used for global typesetting using the font. The other virtual routines are used for determining additional properties of typeset strings, such as slopes and italic corrections, which are needed for the mathematical typesetting. More recent fields (not shown above) contain microtypographic adjustments for scripts and wide accents, tables for character protrusion and tables for the spacing between mathematical symbols.
A font with an OpenType MATH table sets ot_math and, unless a hand-tuned branch keeps its own math type, the math type MATH_TYPE_OPENTYPE (see activation), and fills about forty further fields with the constants of the table (fraction, radical, limit, stretch stack, script and accent geometry); they stay zero for the other fonts, and copy_math_pars copies them like the older fields. The hooks added for such fonts all have defaults in font_rep which answer “unknown”: the script corrections get_lsub_correction_at, …, get_rsup_correction_at take the height of the script into account (and fall back on the corrections without height), get_rubber_variant, get_wide_variant and is_extended_shape serve the stretchable glyphs, get_top_accent the placement of accents, and get_feature_variant the glyph substitutions of the OpenType features. make_rubber_font, which builds the font of the large delimiters (rubber_font), is a virtual method as well, so that a smart font can hand this job to its OpenType main font. These fields and hooks are described in mathematics from the MATH table.
Several types of concrete fonts have been implemented in TeXmacs:
See
Plugins/Metafont/tex_font.cpp
. These fonts use .tfm metrics and .pk or Type 1 glyphs (see also
load_tex.cpp
,
load_tfm.cpp
and
load_pk.cpp
in the same directory).
See
Plugins/Metafont/tex_rubber_font.cpp
. These fonts provide large delimiters and big operators of variable size.
See Plugins/Freetype:
tt_font.cpp
implements fonts with a fixed encoding,
unicode_font.cpp
Unicode fonts, and
unicode_math_font.cpp
,
rubber_unicode_font.cpp
,
rubber_stix_font.cpp
and
rubber_assemble_font.cpp
mathematical fonts and their rubber (extensible) variants. The files adjust_*.cpp contain font specific adjustments, which take precedence over the data of an OpenType MATH table.
tt_tools.cpp
reads the MATH, GSUB and GPOS tables of such fonts and
tt_face.cpp
caches them per face; see the OpenType layout tables.
See
Plugins/Qt/qt_font.cpp
and
Plugins/X11/x_font.cpp
.
See
Graphics/Fonts/math_font.cpp
. These fonts combine several TeX fonts according to an encoding.
See
Graphics/Fonts/virtual_font.cpp
. Virtual fonts build new symbols out of existing ones, using the definitions in $TEXMACS_PATH/fonts/virtual/*.vfn.
See
Graphics/Fonts/compound_font.cpp
.
See
Graphics/Fonts/smart_font.cpp
. A smart font merges several fonts: symbols which are not supported by the main font are looked up in other fonts, depending on their Unicode range and on the font database, and symbols which are not available at all are rendered using an error font. The profiles of the named OpenType math fonts, which give their text, sans serif and typewriter companions, are in
math_font_profiles.cpp
; see math font profiles.
The files poor_*.cpp in Graphics/Fonts implement “poor man's” fonts, which synthesize bold, italic, small capitals, blackboard bold, extended, monospaced or distorted variants of existing fonts. Similarly,
superposed_font.cpp
and
recolored_font.cpp
implement superposed and recolored fonts, and
feature_font.cpp
shows a font through one of its OpenType substitution features (feature_font, apply_features; see OpenType features).
In most cases, the lowest layer of the implementation consists of a collection of glyphs (see
Graphics/Bitmap_fonts
) or of outline fonts, together with some font metric information. The font is responsible for putting these glyphs together using some appropriate spacing. The renderers take care of displaying glyphs in a nice, anti-aliased way, or of embedding the fonts in PDF or PostScript files.
After having implemented fonts themselves, an important remaining issue is the selection of the appropriate font as a function of a certain number of parameters, such as its name, variant, series, shape and size. These parameters are given by the environment variables font, font-family, font-series, font-shape, font-base-size and font-size (and similar variables for mathematics and programs); the font is recomputed by edit_env_rep::update_font in
Typeset/Env/env_semantics.cpp
whenever one of them changes. TeXmacs currently provides two font selection mechanisms.
By default (when the preference "new style fonts" is set to "on"), fonts are created by the function smart_font. Font names are resolved using a font database, which associates to each font family and style the corresponding font files and a list of characteristics (such as the supported scripts, whether the font is monospaced, sans serif or italic, its slant, its x-height, etc.). The global database consists of the files
font-database.scm
,
font-features.scm
,
font-characteristics.scm
and
font-substitutions.scm
in $TEXMACS_PATH/fonts; a local database of the fonts which are installed on the user's system is maintained in $TEXMACS_HOME_PATH/fonts. It can be rebuilt using the Scheme command scan-disk-for-fonts, and the entries of a newer shipped database are merged into it automatically (see the font database). The implementation can be found in
Graphics/Fonts/font_database.cpp
and
Graphics/Fonts/font_select.cpp
: requested fonts are translated into lists of “logical” features, which are matched against the characteristics of the available fonts in order to find the closest match. This makes it possible to use arbitrary fonts installed on the system and to find reasonable substitutes for missing fonts.
TeXmacs also comes with a macro-based font-selection scheme (using the Scheme syntax), which is still used for TeX fonts, and for all fonts when the new style fonts are disabled. At the lowest level, we provide a fixed number of macros which directly correspond to the above types of concrete fonts (see find_font in
Graphics/Fonts/find_font.cpp
). For instance, the macro
(tex $name $size $dpi) |
corresponds to the constructor
font tex_font (string fam, int size, int dpi, int dsize=10); |
of a TeX text font. Other macros are ec, cm, la, adobe, tex-rubber, truetype, unicode, unimath, math, compound, x and qt.
At the middle level, it is possible to specify some rewriting rules like
((roman rm medium right $s $d) (ec ecrm $s $d))
((avant-garde rm medium right $s $d) (ec avant-garde-rm $s $d))
((x-times rm medium right $s $d) (x adobe-times-medium-r-normal $s $d)) |
When a left hand pattern is matched, it is recursively substituted by the right hand side. The files in the directory
progs/fonts
contain a large number of rewriting rules, which are declared using set-font-rules.
At the top level, TeXmacs calls a macro of the form
($name $variant $series $shape $size $dpi) |
as a function of the current environment in the text. If no rules were declared for the font family, the font database is searched instead. If no rule matches, TeXmacs successively tries macros with fewer parameters, and finally falls back on the Computer Modern font cmr.