summaryrefslogtreecommitdiff
path: root/src/cairo-unicode.c
diff options
context:
space:
mode:
authorAndrea Canciani <ranma42@gmail.com>2017-03-18 08:55:11 +0100
committerAndrea Canciani <ranma42@gmail.com>2017-04-25 18:05:40 +0200
commit5584bf755c98703653eef06670abaeb4873f9ee5 (patch)
tree56124e93311ee1a27b92071720142f07282d4fb2 /src/cairo-unicode.c
parentcb9f6273780bb2ffc710d2efdd4224d9096972cb (diff)
downloadcairo-5584bf755c98703653eef06670abaeb4873f9ee5.tar.gz
unicode: Extract the UCS4 to UTF-16 conversion to a separate function
Reuse the function for the UTF-8 to UTF-16 conversion, but also make it available for internal use by cairo.
Diffstat (limited to 'src/cairo-unicode.c')
-rw-r--r--src/cairo-unicode.c39
1 files changed, 33 insertions, 6 deletions
diff --git a/src/cairo-unicode.c b/src/cairo-unicode.c
index 88de39516..7933d12af 100644
--- a/src/cairo-unicode.c
+++ b/src/cairo-unicode.c
@@ -342,6 +342,38 @@ _cairo_ucs4_to_utf8 (uint32_t unicode,
return bytes;
}
+/**
+ * _cairo_ucs4_to_utf16:
+ * @unicode: a UCS-4 character
+ * @utf16: buffer to write utf16 string into. Must have at least 2
+ * elements. Or %NULL.
+ *
+ * This space left intentionally blank.
+ *
+ * Return value: Number of elements in the utf16 string or 0 if an
+ * invalid unicode character
+ **/
+int
+_cairo_ucs4_to_utf16 (uint32_t unicode,
+ uint16_t *utf16)
+{
+ int len;
+
+ if (unicode < 0x10000) {
+ if (utf16)
+ utf16[0] = unicode;
+ return 1;
+ } else if (unicode < 0x110000) {
+ if (utf16) {
+ utf16[0] = (unicode - 0x10000) / 0x400 + 0xd800;
+ utf16[1] = (unicode - 0x10000) % 0x400 + 0xdc00;
+ }
+ return 2;
+ } else {
+ return 0;
+ }
+}
+
#if CAIRO_HAS_UTF8_TO_UTF16
/**
* _cairo_utf8_to_utf16:
@@ -401,12 +433,7 @@ _cairo_utf8_to_utf16 (const char *str,
for (i = 0; i < n16;) {
uint32_t wc = _utf8_get_char (in);
- if (wc < 0x10000) {
- str16[i++] = wc;
- } else {
- str16[i++] = (wc - 0x10000) / 0x400 + 0xd800;
- str16[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
- }
+ i += _cairo_ucs4_to_utf16 (wc, str16 + i);
in = UTF8_NEXT_CHAR (in);
}