Debian Codegraph @

src/doc/utf8-notes.rst

raw
1 ********************************************
2 UTF-8 and Wide-character handling in Angband
3 ********************************************
4
5 Background
6 ==========
7
8 As of the sequence of commits from 589d1d3 to c91ae22, (plus a few bug-fixes and cleanups since) the Angband source is able to handle UTF-8 characters in its edit files, and has dropped the previous hacky mechanism of generating accented characters (with sequences like ["e] for ë).
9
10 There are a number of changes that had to happen for this to work, and this page aims to document them.
11
12 Locale
13 ======
14
15 Angband now needs to be run within a UTF-8 capable locale, and this is checked in **main.c**:*main()*, as::
16
17 if (setlocale(LC_CTYPE, "")) {
18 /* Require UTF-8 */
19 if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0)
20 quit("Angband requires UTF-8 support");
21 }
22
23 Files
24 =====
25
26 All the edit files are now expected to be in the UTF-8 encoding, and can have accented characters directly inserted in them.
27 Output files such as spoilers, character dumps and other text output is now in UTF-8.
28
29 User Editing of Strings with Multi-byte Characters
30 ==================================================
31
32 Provisions have been made in **ui-input.c**:*askfor_aux_keypress()* and
33 **ui-birth.c**:*edit_text()* to allow the player to input and edit strings
34 with multi-byte characters. If multi-line editing is needed in more places
35 than just the history editing in **ui-birth.c**, then it would be necessary
36 to extend **ui-input.c** to provide that.
37
38 Internals
39 =========
40
41 Canvas
42 ------
43
44 The internal representation of the main (and other terminal) screen(s) is as two arrays, one of "attributes" (``byte attr``) and one of "characters" (``wchar_t char``). The characters to be displayed are stored as unicode, in the native wchar_t representation of a unicode character on the platform, whatever that is. When strings are printed to the screen, they are converted from UTF-8 (as ``char *``) to wide characters (``wchar_t *``) using **z-util.c**:*text_mbstowcs()*. This allows the conversion function to be overloaded if a particular platform needs it.
45 For the few cases where the reverse conversion, wide characters to UTF-8, is
46 necessary, there is **z-util.c**:*text_wctomb()* and some support utilities,
47 **z-util.c**:*text_wcsz()* and **z-util.c**:*text_iswprint()*. Like
48 *text_mbstowcs()*, all of those allow overloading for a particular platform.
49
50 When they are displayed, the wide chars are put on the screen in different ways, depending on the port (see below). In the case of graphics tiles, things are slightly different. The "character" is still stored as a ``wchar_t``, but only the bottom 7 bits are used, as an index into a large 2-D bitmap, containing the tiles along the x axis, and the attributes ("colour") on the y axis. (Check this bit). A tile used to be indicated by the top bit set in both the attribute and the character, but it is now indicated only by the top bit of the attribute. In the original design, I had hoped to treat tiles as a special case of a font, and allow all unicode character support, but the tiles are multi-coloured, so this cannot work.
51
52 Textblock
53 ---------
54
55 Textblocks (in **z-textblock.c**) also have ``wchar_t`` as the internal representation of the displayed characters. These are then copied directly onto the canvas when the textblock is displayed.
56
57 Parsers
58 -------
59
60 In reading the edit files, all strings are maintained in UTF-8 until needed.
61
62 Glyphs are read in directly to a ``wchar_t`` type, using the "char" parse type, and the *parser_getchar()* function. This does not apply to preference files, where the symbols are all treated as ``int``.
63 One exception to that is the lists of symbols used in **ui_entry_renderer.txt**:
64 those are expected to be encoded as UTF-8 and will be written to preference
65 files in that encoding as well.
66
67 Ports
68 =====
69
70 This section lists port-specific changes and what the individual ports do with the wide-char representation of the display characters to get them onto the display.
71
72 SDL
73 ---
74
75 Wide chars on the canvas are converted to a UTF-8 string using *wcstombs()* and then rendered to the screen using a pre-computed ``TTF_Font`` using *TTF_RenderUTF8_Shaded()* in the function *sdl_mapFontDraw()*.
76
77 X11
78 ---
79
80 Wide chars on the canvas are drawn directly to the window using *XwcDrawImageString()* in *Infofnt_test_std()*. Fonts are now rendered using ``XFontSet``, rather than the previous ``XFontStruct``.
81
82 GCU
83 ---
84
85 Now requires the "wide" version of ncurses (i.e. ncursesw), and will fail to build if this is not present.
86
87 Wide characters from the canvas are written directly to the screen using *mvwaddnwstr()* in *Term_text_gcu()*.
88
89 Some of the default symbols have changed as follows (note that only the change
90 to the floor representation remains present in 4.2):
91
92 ============ =================== =========================
93 Feature From To
94 ============ =================== =========================
95 Floor Period '.' (U+002E) MIDDLE DOT '·' (U+00B7)
96 Magma ?? (0x03) MEDIUM SHADE '▒' (U+2592)
97 Quartz Vein ?? (0x03) LIGHT SHADE '░' (U+2591)
98 Granite Wall ?? (0x02) DARK SHADE '▓' (U+2593)
99 ============ =================== =========================
100
101 This has the added advantage that standard fonts can be used, and it is not necessary to resort to hacking fonts to get "solid walls".
102
103 Windows
104 -------
105
106 Windows does not properly support UTF-8 using the standard C library routines for locale, so the *text_mbcs_hook* function is defined to use the Windows-native *MultiByteToWideChar()* function, and the external files are assumed to be in UTF-8. Wide chars from the canvas are written directly to the screen using *ExtTextOutW()* in *Term_text_win()*.
107 To match up with the UTF-8 to wide character conversion, the Windows front end
108 also customizes *text_wctomb_hook*, *text_wcsz_hook*, and *text_iswprint_hook*
109 for handling the reverse conversion.
110
111 OSX
112 ---
113
114 Commit 80341e8 to master implemented UTF-8 support for Cocoa. Like the
115 Windows front end, it uses custom functions for *text_mbcs_hook*,
116 *text_wctomb_hook*, *text_wcsz_hook*, and *text_iswprint_hook*.
117
118 GTK
119 ---
120
121 Wide chars on the canvas are converted to UTF-8 using *wcstombs()* and then rendered to the screen with *pango_layout_set_text()* in **cairo-utils.c**:*draw_text()*. Tiles are almost certainly broken.
122
123 Android
124 -------
125
126 There were significant problems in adapting an Android port to this change, as the support for wide chars in Android is lacking. ``wchar_t`` is implemented as an 8-bit quantity, and some of the support functions such as *mbstowcs()* are missing or broken. We worked around this problem by storing wide characters in 8-bit char types and using ISO-8859-1 (Latin-1) for the encoding. We implemented the missing functions by using the Java String class to convert between UTF-8 and ISO-8859-1.
127
128 To Do
129 =====
130
131 Things still remaining to do:
132
133 * Change display editor to allow arbitrary input of unicode chars / avoid
134 scrolling display of chars?
135 * Check preference files, and how they store chars. Check that display changes
136 work.
137 * Improve way that input key is matched to display char. Locale-aware input?
138 * Change tile-handling, so that the tile index is the look up into the bitmap,
139 and not (tile index & 0x7f)
140
141 Issues
142 ======
143
144 * What's seen by the player will depend on whether the font has glyphs for
145 the characters used. If the input files start using code points outside
146 of the 1 - 255 range, may need to consider changing the default fonts used
147 in the front ends so that they'll properly render those characters.
148 * On Windows, wchar_t is 16-bit and represents one piece encoded in UTF-16.
149 Because of that and the way the canvas is declared for terminals, only
150 code points in the basic multi-lingual plane (which, is still alot) can be
151 stored. Code points which require a surrogate pair when encoded as UTF-16
152 can't be passed through for rendering.
153 * On most platforms, fairly low-level key input is used, and that's necessary
154 for being able to respond to single keystrokes. In the context of
155 *askfor_aux_keypress()* or history editing in the birth process, it could be
156 useful to have better support for the plaform's method of choice for entering
157 characters that aren't represented by a single keystroke and modifiers on the
158 current keyboard layout.