/* 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)
/* 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) {
	if (utf8)
	    *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;
    }
    if (!utf8)
	return bytes;
    p = utf8 + bytes;
    while (p > utf8) {
	*--p = 0x80 | (unicode & 0x3f);
	unicode >>= 6;
    }
    *p |= 0xf0 << (4 - bytes);
    return bytes;
}