FLTK 1.3.3
|
This chapter explains how FLTK handles international text via Unicode and UTF-8.
Unicode support was only recently added to FLTK and is still incomplete. This chapter is Work in Progress, reflecting the current state of Unicode support.
The summary of Unicode, ISO 10646 and UTF-8 given below is deliberately brief, and provides just enough information for the rest of this chapter. For further information, please see:
The Unicode Standard was originally developed by a consortium of mainly US computer manufacturers and developers of multi-lingual software. It has now become a defacto standard for character encoding, and is supported by most of the major computing companies in the world.
Before Unicode, many different systems, on different platforms, had been developed for encoding characters for different languages, but no single encoding could satisfy all languages. Unicode provides access to over 100,000 characters used in all the major languages written today, and is independent of platform and language.
Unicode also provides higher-level concepts needed for text processing and typographic publishing systems, such as algorithms for sorting and comparing text, composite character and text rendering, right-to-left and bi-directional text handling.
There are currently no plans to add this extra functionality to FLTK.
The International Organisation for Standardization (ISO) had also been trying to develop a single unified character set. Although both ISO and the Unicode Consortium continue to publish their own standards, they have agreed to coordinate their work so that specific versions of the Unicode and ISO 10646 standards are compatible with each other.
The international standard ISO 10646 defines the Universal Character Set (UCS) which contains the characters required for almost all known languages. The standard also defines three different implementation levels specifying how these characters can be combined.
There are currently no plans for handling the different implementation levels or the combining characters in FLTK.
In UCS, characters have a unique numerical code and an official name, and are usually shown using 'U+' and the code in hexadecimal, e.g. U+0041 is the "Latin capital letter A". The UCS characters U+0000 to U+007F correspond to US-ASCII, and U+0000 to U+00FF correspond to ISO 8859-1 (Latin1).
ISO 10646 was originally designed to handle a 31-bit character set from U+00000000 to U+7FFFFFFF, but the current idea is that 21-bits will be sufficient for all future needs, giving characters up to U+10FFFF. The complete character set is sub-divided into planes. Plane 0, also known as the Basic Multilingual Plane (BMP), ranges from U+0000 to U+FFFD and consists of the most commonly used characters from previous encoding standards. Other planes contain characters for specialist applications.
The UCS also defines various methods of encoding characters as a sequence of bytes. UCS-2 encodes Unicode characters into two bytes, which is wasteful if you are only dealing with ASCII or Latin1 text, and insufficient if you need characters above U+00FFFF. UCS-4 uses four bytes, which lets it handle higher characters, but this is even more wasteful for ASCII or Latin1.
The Unicode standard defines various UCS Transformation Formats. UTF-16 and UTF-32 are based on units of two and four bytes. UCS characters requiring more than 16 bits are encoded using "surrogate pairs" in UTF-16.
UTF-8 encodes all Unicode characters into variable length sequences of bytes. Unicode characters in the 7-bit ASCII range map to the same value and are represented as a single byte, making the transformation to Unicode quick and easy.
All UCS characters above U+007F are encoded as a sequence of several bytes. The top bits of the first byte are set to show the length of the byte sequence, and subseqent bytes are always in the range 0x80 to 0x8F. This combination provides some level of synchronisation and error detection.
Unicode range | Byte sequences |
U+00000000 - U+0000007F | 0xxxxxxx |
U+00000080 - U+000007FF | 110xxxxx 10xxxxxx |
U+00000800 - U+0000FFFF | 1110xxxx 10xxxxxx 10xxxxxx |
U+00010000 - U+001FFFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
U+00200000 - U+03FFFFFF | 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
U+04000000 - U+7FFFFFFF | 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
Moving from ASCII encoding to Unicode will allow all new FLTK applications to be easily internationalized and used all over the world. By choosing UTF-8 encoding, FLTK remains largely source-code compatible to previous iterations of the library.
FLTK will be entirely converted to Unicode using UTF-8 encoding. If a different encoding is required by the underlying operating system, FLTK will convert the string as needed.
It is important to note that the initial implementation of Unicode and UTF-8 in FLTK involves three important areas:
The current implementation of Unicode / UTF-8 in FLTK will impose the following limitations:
Three pre-processor variables are defined in the source code that determine how fl_utf8decode() handles illegal UTF-8 sequences:
fl_utf8encode() is less strict, and only generates the UTF-8 sequence for 0xFFFD, the Unicode REPLACEMENT CHARACTER, if it is asked to encode a UCS value above U+10FFFF.
Many of the [fltk2] functions below use fl_utf8decode() and fl_utf8encode() in their own implementation, and are therefore somewhat protected from bad UTF-8 sequences.
The [OksiD] fl_utf8len() function assumes that the byte it is passed is the first byte in a UTF-8 sequence, and returns the length of the sequence. Trailing bytes in a UTF-8 sequence will return -1.
Please see the individual function description for further details about error handling and return values.
This section currently provides a brief overview of the functions. For more details, consult the main text for each function via its link.
int fl_utf8locale() FLTK2
fl_utf8locale
() returns true if the "locale" seems to indicate that UTF-8 encoding is used. int fl_utf8test(const char *src, unsigned len) FLTK2
fl_utf8test
() examines the first len
bytes of src
. It returns 0 if there are any illegal UTF-8 sequences; 1 if src
contains plain ASCII or if len
is zero; or 2, 3 or 4 to indicate the range of Unicode characters found.int fl_utf_nb_char(const unsigned char *buf, int len) OksiD
len
bytes of buf
.int fl_unichar_to_utf8_size(Fl_Unichar)
int fl_utf8bytes(unsigned ucs)
ucs
in UTF-8.int fl_utf8len(char c) OksiD
c
is a valid first byte of a UTF-8 encoded character sequence, fl_utf8len
() will return the number of bytes in that sequence. It returns -1 if c
is not a valid first byte.unsigned int fl_nonspacing(unsigned int ucs) OksiD
ucs
is a non-spacing character. [What are non-spacing characters?]const char* fl_utf8back(const char *p, const char *start, const char *end) FLTK2
const char* fl_utf8fwd(const char *p, const char *start, const char *end) FLTK2
p
already points to the start of a UTF-8 character sequence, these functions will return p
. Otherwise fl_utf8back
() searches backwards from p
and fl_utf8fwd
() searches forwards from p
, within the start
and end
limits, looking for the start of a UTF-8 character.unsigned int fl_utf8decode(const char *p, const char *end, int *len) FLTK2
int fl_utf8encode(unsigned ucs, char *buf) FLTK2
fl_utf8decode
() attempts to decode the UTF-8 character that starts at p
and may not extend past end
. It returns the Unicode value, and the length of the UTF-8 character sequence is returned via the len
argument. fl_utf8encode
() writes the UTF-8 encoding of ucs
into buf
and returns the number of bytes in the sequence. See the main documentation for the treatment of illegal Unicode and UTF-8 sequences.unsigned int fl_utf8froma(char *dst, unsigned dstlen, const char *src, unsigned srclen) FLTK2
unsigned int fl_utf8toa(const char *src, unsigned srclen, char *dst, unsigned dstlen) FLTK2
fl_utf8froma
() converts a character string containing single bytes per character (i.e. ASCII or ISO-8859-1) into UTF-8. If the src
string contains only ASCII characters, the return value will be the same as srclen
. fl_utf8toa
() converts a string containing UTF-8 characters into single byte characters. UTF-8 characters that do not correspond to ASCII or ISO-8859-1 characters below 0xFF are replaced with '?'.destlen
provides a means of limiting the number of bytes written, so setting destlen
to zero is a means of measuring how much storage would be needed before doing the real conversion.char* fl_utf2mbcs(const char *src) OksiD
unsigned int fl_utf8fromwc(char *dst, unsigned dstlen, const wchar_t *src, unsigned srclen) FLTK2
unsigned int fl_utf8towc(const char *src, unsigned srclen, wchar_t *dst, unsigned dstlen) FLTK2
unsigned int fl_utf8toUtf16(const char *src, unsigned srclen, unsigned short *dst, unsigned dstlen) FLTK2
wchar_t
or "wide character" strings. The difficulty lies in the fact that sizeof(wchar_t)
is 2 on Windows and 4 on Linux and most other systems. Therefore some "wide characters" on Windows may be represented as "surrogate pairs" of more than one wchar_t
.fl_utf8fromwc
() converts from a "wide character" string to UTF-8. Note that srclen
is the number of wchar_t
elements in the source string and on Windows this might be larger than the number of characters. dstlen
specifies the maximum number of bytes to copy, including the null terminator.fl_utf8towc
() converts a UTF-8 string into a "wide character" string. Note that on Windows, some "wide characters" might result in "surrogate
pairs" and therefore the return value might be more than the number of characters. dstlen
specifies the maximum number of wchar_t elements to copy, including a zero terminating element. [Is this all worded correctly?]fl_utf8toUtf16
() converts a UTF-8 string into a "wide character" string using UTF-16 encoding to handle the "surrogate pairs" on Windows. dstlen
specifies the maximum number of wchar_t elements to copy, including a zero terminating element. [Is this all worded correctly?]src
string, including the zero terminator. Therefore setting dstlen
to zero is a way of measuring how much storage would be needed before doing the real conversion.unsigned int fl_utf8from_mb(char *dst, unsigned dstlen, const char *src, unsigned srclen) FLTK2
unsigned int fl_utf8to_mb(const char *src, unsigned srclen, char *dst, unsigned dstlen) FLTK2
int fl_tolower(unsigned int ucs) OksiD
int fl_toupper(unsigned int ucs) OksiD
int fl_utf_tolower(const unsigned char *str, int len, char *buf) OksiD
int fl_utf_toupper(const unsigned char *str, int len, char *buf) OksiD
fl_tolower
() and fl_toupper
() convert a single Unicode character from upper to lower case, and vice versa. fl_utf_tolower
() and fl_utf_toupper
() convert a string of bytes, some of which may be multi-byte UTF-8 encodings of Unicode characters, from upper to lower case, and vice versa. buf
length must be at least 3*len
[for 16-bit Unicode]int fl_utf_strcasecmp(const char *s1, const char *s2) OksiD
int fl_utf_strncasecmp(const char *s1, const char *s2, int n) OksiD
fl_utf_strcasecmp
() is a UTF-8 aware string comparison function that converts the strings to lower case Unicode as part of the comparison. flt_utf_strncasecmp
() only compares the first n
characters [bytes?][Prev] Advanced FLTK | [Index] | FLTK Enumerations [Next] |