GNU TeXmacsUser manualReferenceAboutEnglish
Converters between encodings

1.Table driven converters

struct converter_rep
(a dictionary applied by longest match)

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:

BIT2BIT

#hh denotes the byte with that hexadecimal value (used for Cork and T2A bytes and for the universal symbols, which are written literally).

UTF8

#hhhh denotes the UTF-8 encoding of that code point.

CHAR_ENTITY

The string contains numeric character entities &#nnn; or &#xhh;, which are decoded to UTF-8.

ENTITY_NAME

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).

2.The available conversions

converter_rep::load knows the following pairs. For each, the table files are listed in loading order.

Cork to UTF-8

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.

UTF-8 to Cork

The reverse of corktounicode, tmuniversaltounicode and unicode-symbol-oneway, plus unicode-cork-oneway; UTF-8 to T2A adds t2atounicode.

SourceCode

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.

HTML

Named entities from HTMLlat1, HTMLspecial and HTMLsymbol, in both directions.

LaTeX

utf8tolatex, utf8tolatex-onedir and utf8tolatex-back.

Cork to ASCII

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

).

T2A.CY and CODEPOINT

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:

utf8_to_cork (s)

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.

cork_to_utf8 (s)

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.

utf8_to_html (s), html_to_utf8 (s)

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).

convert_utf8_to_LaTeX, convert_LaTeX_to_utf8

Character level conversions between UTF-8 and LaTeX commands. convert_utf8_to_LaTeX prints a warning for characters it cannot translate.

cork_to_ascii (s)

See above (escape-to-ascii).

convert (s, from, to), convert_to_cork, convert_from_cork, check_encoding

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.

3.Low level UTF-8 routines

encode_as_utf8 (code)

The UTF-8 bytes of a code point (up to 0x1FFFFF; larger values give the empty string).

decode_from_utf8 (s, i)

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.

utf8_to_hex_entities, hex_entities_to_utf8

See utf8_to_html above.

utf8_to_pdf_hex_string (s)

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

).

convert_escapes, convert_char_entities, convert_char_entity, hex_digit_to_int

Helpers for reading the tables.

4.Guessing the encoding of western text

Data/String/wencoding.cpp

decides how to interpret text of unknown origin (plain text files, clipboard contents, output of external programs):

guess_wencoding (s)

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).

western_to_cork (s)

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.

western_to_utf8 (s)

The analogue with UTF-8 as target.

5.Other encodings of bytes

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.

Hexadecimal numbers

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).

Legacy 8-bit encodings

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

).

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".