![]() |
Converters between encodings | ![]() |
Declared in
Data/String/converter.hpp
. A converter is a RESOURCE: it is created once per pair of encodings by load_converter (from, to) and cached under the name from-to for the rest of the session. Its fields are the prefix tree ht (a hashtree<char,string> mapping byte sequences to their translation), an output buffer, the names of the two encodings and the flag copy_unmatched, which is always true.
apply (c, s) walks through s; at each position it follows the prefix tree as far as possible and replaces the longest matching key by its value. A byte which starts no key is copied unchanged. c << s and flush (c) do the same incrementally.
The tables live in $TEXMACS_PATH/langs/encoding/. Each file is a list of pairs ("key" "value"), read by hashtree_from_dictionary (dic, file, key_escape, val_escape, reverse). The two escape_type arguments say how to read keys and values:
#hh denotes the byte with that hexadecimal value (used for Cork and T2A bytes and for the universal symbols, which are written literally).
#hhhh denotes the UTF-8 encoding of that code point.
The string contains numeric character entities &#nnn; or &#xhh;, which are decoded to UTF-8.
The string is an entity name, which is wrapped into &name;.
With reverse set, the second column is used as key. Several files are usually loaded into the same tree, and a later entry for the same key overwrites an earlier one; this is how, for instance,
tmuniversaltounicode.scm
overrides the treatment of <less> and <gtr>. Files with oneway in their name contain mappings which are only valid in one direction (for instance several Unicode spaces which all become a plain space in Cork).
converter_rep::load knows the following pairs. For each, the table files are listed in loading order.
corktounicode, cork-unicode-oneway, tmuniversaltounicode, symbol-unicode-oneway, symbol-unicode-fallback, symbol-unicode-math. Strict-Cork to UTF-8 is the same without the fallback table, and T2A to UTF-8 adds t2atounicode.
The reverse of corktounicode, tmuniversaltounicode and unicode-symbol-oneway, plus unicode-cork-oneway; UTF-8 to T2A adds t2atounicode.
Like Cork to and from UTF-8 (but without cork-unicode-oneway in the direction of UTF-8), with cork-to-real-ascii on top, which keeps the backquote and … as they are instead of turning them into typographic characters.
Named entities from HTMLlat1, HTMLspecial and HTMLsymbol, in both directions.
utf8tolatex, utf8tolatex-onedir and utf8tolatex-back.
cork-escaped-to-ascii, which replaces every byte outside printable ASCII by an escape \xhh; it is used to embed TeXmacs code in SVG images (
convert/images/tmimage.scm
).
Conversions between the Cyrillic part of T2A and Unicode escapes, used when upgrading old documents (
Data/Convert/Texmacs/upgradetm.cpp
) and by smart fonts (
Graphics/Fonts/smart_font.cpp
).
On top of the converters,
converter.cpp
defines the functions used by the rest of the program:
Decodes s one code point at a time and converts each separately. A code point of at least 256 without a table entry becomes <#hex>; one below 256 without a table entry is copied unchanged. var_utf8_to_cork in addition turns the typographic quotes U+2018–U+201D into escapes; sourcecode_to_cork and utf8_to_t2a are the analogues for the other targets. Scheme: utf8->cork, sourcecode->cork, utf8->t2a.
Handles <#hex> escapes directly (by encode_as_utf8) and applies the converter to the text in between. strict_cork_to_utf8, cork_to_sourcecode and t2a_to_utf8 are the analogues. Scheme: cork->utf8, cork->sourcecode, t2a->utf8.
Named entities, plus hexadecimal entities: utf8_to_html turns every non-ASCII character into &#xhhhh;, but html_to_utf8 only decodes hexadecimal entities between 0x80 and 0xFF (utf8->html, html->utf8). The XML and HTML parser decodes numeric entities itself, with convert_char_entity (
Data/Convert/Xml/parsexml.cpp
; see the LaTeX and HTML converters).
Character level conversions between UTF-8 and LaTeX commands. convert_utf8_to_LaTeX prints a warning for characters it cannot translate.
See above (escape-to-ascii).
The general entry points. Conversions involving Cork, UTF-8, SourceCode and LaTeX use the tables; anything else goes through UTF-8 and iconv (convert_using_iconv, check_using_iconv). In builds without USE_ICONV, unknown conversions return their input unchanged and check_encoding returns true.
The iconv wrapper converts in one pass with a growing output buffer. On an invalid or incomplete input sequence it prints an error and returns the whole input unchanged, so callers cannot tell a failed conversion from an identity conversion except by the message.
The UTF-8 bytes of a code point (up to 0x1FFFFF; larger values give the empty string).
Decodes the code point at position i and advances i. A byte which does not start a valid sequence, or a sequence with a bad continuation byte, is returned as a single byte value; this “failsafe” is what lets utf8_to_cork pass bytes of other encodings through unchanged.
See utf8_to_html above.
Converts a Cork string to the UTF-16BE hexadecimal form <FEFF…> used for text strings in PDF files (outline entries and document metadata, in
Plugins/Pdf/pdf_hummus_renderer.cpp
and
Graphics/Renderer/printer.cpp
).
Helpers for reading the tables.
Data/String/wencoding.cpp
decides how to interpret text of unknown origin (plain text files, clipboard contents, output of external programs):
Returns "ASCII" if all bytes are printable ASCII or common control characters, "UTF-8-BOM" or "UTF-8" if the text decodes as UTF-8 (with or without a byte order mark), "ISO-8859" if it only contains bytes allowed in the ISO-8859 family, and "other" otherwise (guess-wencoding).
Converts according to the guess. ISO-8859 text is converted from the character set associated with the language of the current locale (language_to_local_ISO_charset). If no conversion applies, the string is kept as it is if it already looks like a universal string (looks_universal), and otherwise passed through tm_encode.
The analogue with UTF-8 as target.
base64.cpp
encode_base64 (with a line break every 80 output characters) and decode_base64, which ignores characters outside the base 64 alphabet and stops at the first =; Scheme: encode-base64, decode-base64. In C++ they are used by the conservative LaTeX converters to store attributes in comments.
as_hexadecimal (i), as_hexadecimal (i, len) (zero padded) and from_hexadecimal in
analyze.cpp
(integer->hexadecimal, integer->padded-hexadecimal, hexadecimal->integer). from_hexadecimal silently ignores characters which are not hexadecimal digits (but still shifts the result).
koi8_to_iso, iso_to_koi8 (and the Ukrainian variants), il2_to_cork, cork_to_il2 in
analyze.cpp
. il2_to_cork and cork_to_il2 are used for Czech and other ISO-8859-2 selections (
Edit/Replace/edit_select.cpp
) and dates (
System/Language/locale.cpp
).