summaryrefslogtreecommitdiff
path: root/src/cairo-unicode.c
diff options
context:
space:
mode:
authorAdrian Johnson <ajohnson@redneon.com>2008-06-29 19:32:19 +0930
committerAdrian Johnson <ajohnson@redneon.com>2008-06-29 19:32:19 +0930
commitf4d6e714a63ab44e388a06fba8d849f2ad7f0000 (patch)
tree1146009de2dd516f1eac54a7a7573b792e50cbed /src/cairo-unicode.c
parent2f99a294cd2367c4649428534b3429522448260c (diff)
Add _cairo_ucs4_to_utf8
Diffstat (limited to 'src/cairo-unicode.c')
-rw-r--r--src/cairo-unicode.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/cairo-unicode.c b/src/cairo-unicode.c
index c4873d5c..973ef576 100644
--- a/src/cairo-unicode.c
+++ b/src/cairo-unicode.c
@@ -262,6 +262,45 @@ _cairo_utf8_to_ucs4 (const char *str,
return CAIRO_STATUS_SUCCESS;
}
+/**
+ * _cairo_ucs4_to_utf8:
+ * @unicode: a UCS-4 character
+ * @utf8: buffer to write utf8 string into. Must have at least 4 bytes
+ * space available.
+ *
+ * Return value: Number of bytes in the utf8 string or 0 if an invalid
+ * unicode character
+ **/
+int
+_cairo_ucs4_to_utf8 (uint32_t unicode,
+ char *utf8)
+{
+ int bytes;
+ char *p;
+
+ if (unicode < 0x80) {
+ *utf8 = unicode;
+ return 1;
+ } else if (unicode < 0x800) {
+ bytes = 2;
+ } else if (unicode < 0x10000) {
+ bytes = 3;
+ } else if (unicode < 0x200000) {
+ bytes = 4;
+ } else {
+ return 0;
+ }
+
+ p = utf8 + bytes;
+ while (p > utf8) {
+ *--p = 0x80 | (unicode & 0x3f);
+ unicode >>= 6;
+ }
+ *p |= 0xf0 << (4 - bytes);
+
+ return bytes;
+}
+
#if CAIRO_HAS_UTF8_TO_UTF16
/**
* _cairo_utf8_to_utf16: