beta-0.89.2
[luatex.git] / source / libs / poppler / poppler-src / poppler / UTF8.h
blob34a07d4082fbcaf3260628e993ca4a1b9a4e9457
1 //========================================================================
2 //
3 // UTF8.h
4 //
5 // Copyright 2001-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 //========================================================================
11 // Modified under the Poppler project - http://poppler.freedesktop.org
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
16 // Copyright (C) 2008 Koji Otani <sho@bbr.jp>
18 // To see a description of the changes please see the Changelog file that
19 // came with your tarball or type make ChangeLog if you are building from git
21 //========================================================================
23 static int mapUTF8(Unicode u, char *buf, int bufSize) {
24 if (u <= 0x0000007f) {
25 if (bufSize < 1) {
26 return 0;
28 buf[0] = (char)u;
29 return 1;
30 } else if (u <= 0x000007ff) {
31 if (bufSize < 2) {
32 return 0;
34 buf[0] = (char)(0xc0 + (u >> 6));
35 buf[1] = (char)(0x80 + (u & 0x3f));
36 return 2;
37 } else if (u <= 0x0000ffff) {
38 if (bufSize < 3) {
39 return 0;
41 buf[0] = (char)(0xe0 + (u >> 12));
42 buf[1] = (char)(0x80 + ((u >> 6) & 0x3f));
43 buf[2] = (char)(0x80 + (u & 0x3f));
44 return 3;
45 } else if (u <= 0x0010ffff) {
46 if (bufSize < 4) {
47 return 0;
49 buf[0] = (char)(0xf0 + (u >> 18));
50 buf[1] = (char)(0x80 + ((u >> 12) & 0x3f));
51 buf[2] = (char)(0x80 + ((u >> 6) & 0x3f));
52 buf[3] = (char)(0x80 + (u & 0x3f));
53 return 4;
54 } else {
55 return 0;
59 static int mapUCS2(Unicode u, char *buf, int bufSize) {
60 if (u <= 0xffff) {
61 if (bufSize < 2) {
62 return 0;
64 buf[0] = (char)((u >> 8) & 0xff);
65 buf[1] = (char)(u & 0xff);
66 return 2;
67 } else if (u < 0x110000) {
68 Unicode uu;
70 /* using surrogate pair */
71 if (bufSize < 4) {
72 return 0;
74 uu = ((u - 0x10000) >> 10) + 0xd800;
75 buf[0] = (char)((uu >> 8) & 0xff);
76 buf[1] = (char)(uu & 0xff);
77 uu = (u & 0x3ff)+0xdc00;
78 buf[2] = (char)((uu >> 8) & 0xff);
79 buf[3] = (char)(uu & 0xff);
80 return 4;
81 } else {
82 return 0;