![]() |
Strings, characters and encodings | ![]() |
Inside TeXmacs, text is a sequence of bytes held in the C++ class string (see basic data types). The bytes are not interpreted as UTF-8: the labels of atomic trees, the strings manipulated by the editor and most strings passed to Scheme are in the TeXmacs universal encoding, which combines the 8-bit Cork font encoding of TeX with symbolic names such as <alpha> and numeric escapes such as <#20AC>. Conversions to and from UTF-8, HTML entities, LaTeX, the T2A Cyrillic encoding or the local character set of the system happen at the boundaries of the program: keyboard input, the clipboard, file import and export, widgets, fonts and external programs.
This chapter describes the encoding itself, the routines which analyze and convert strings, and where the conversions take place. The serialization of documents in .tm files is described in default serialization; how fonts render universal symbols is explained in TeXmacs fonts and smart fonts.
A character is either a single byte (an ASCII character or a Cork glyph such as \351 for an e with acute accent) or a symbol between angular brackets: a name (<alpha>, <leq>, <less> for the less-than sign itself) or a Unicode code point in hexadecimal (<#3B1>). Routines whose name starts with tm_ or uni_ work on such characters rather than on bytes.
Conversions between encodings are table driven: the tables in $TEXMACS_PATH/langs/encoding/ are loaded into prefix trees and applied by longest match. Encodings that TeXmacs does not know itself are handled by iconv, when it is available.
Each subsystem that talks to the outside world is responsible for its own conversion: the Qt layer converts key presses and widget strings, the converters convert imported and exported documents, and fonts map universal characters to glyphs. File names and system commands stay in the encoding of the operating system (in practice UTF-8).
Kernel/Types/string.hpp,
string.cpp
The byte string class; see basic data types.
Data/String/analyze.hpp,
analyze.cpp
Character tests and case changes on Cork bytes, the tm_* routines on universal characters, quoting and escaping, parsing helpers, search and replace, Roman numbers and hexadecimal numbers, completions, and a few special purpose conversions (KOI8, ISO-8859-2, ispanish, igerman).
Data/String/universal.hpp,
universal.cpp
Case changes, transliteration, removal of accents, letter tests and sorting for universal strings (uni_*).
Data/String/converter.hpp,
converter.cpp
The table driven converter class, the conversion functions between Cork, UTF-8, HTML, LaTeX, T2A and SourceCode, the iconv wrapper and the UTF-8 encoding and decoding primitives.
Data/String/wencoding.hpp,
wencoding.cpp
Heuristics which guess the encoding of western text (guess_wencoding, western_to_cork).
Data/String/base64.hpp,
base64.cpp
Base 64 encoding and decoding.
Data/String/fast_search.hpp,
fast_search.cpp
Indexed substring search and longest common substrings, used by the conservative LaTeX converters.
Data/String/merge_sort.hpp
A generic merge sort on arrays.
The conversion tables.
Plugins/Qt/qt_utilities.cpp,
QTMKeyboardEvent.cpp,
qt_gui.cpp
Conversions for widgets, key presses and the clipboard.
Data/Convert/Verbatim/verbatim.cpp
Encodings of plain text import and export.
Scheme/Glue/build-glue-basic.scm
The Scheme glue for the routines above (utf8->cork, tmstring-length, tmstring-upcase-all, …).