Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / vcl / generic / glyphs / scrptrun.cxx
blob9e8eef0009178585e1850aa9f68088bafa356518
1 /*
2 *******************************************************************************
4 * Copyright (c) 1995-2013 International Business Machines Corporation and others
6 * All rights reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, and/or sell copies of the
12 * Software, and to permit persons to whom the Software is furnished to do so,
13 * provided that the above copyright notice(s) and this permission notice appear
14 * in all copies of the Software and that both the above copyright notice(s) and
15 * this permission notice appear in supporting documentation.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
20 * NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE
21 * LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY
22 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
24 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Except as contained in this notice, the name of a copyright holder shall not be
27 * used in advertising or otherwise to promote the sale, use or other dealings in
28 * this Software without prior written authorization of the copyright holder.
30 *******************************************************************************
31 * file name: scrptrun.cpp
33 * created on: 10/17/2001
34 * created by: Eric R. Mader
37 #include "unicode/utypes.h"
38 #include "unicode/uscript.h"
40 #include "scrptrun.h"
42 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
44 const char ScriptRun::fgClassID=0;
46 UChar32 ScriptRun::pairedChars[] = {
47 0x0028, 0x0029, // ascii paired punctuation
48 0x003c, 0x003e,
49 0x005b, 0x005d,
50 0x007b, 0x007d,
51 0x00ab, 0x00bb, // guillemets
52 0x2018, 0x2019, // general punctuation
53 0x201c, 0x201d,
54 0x2039, 0x203a,
55 0x3008, 0x3009, // chinese paired punctuation
56 0x300a, 0x300b,
57 0x300c, 0x300d,
58 0x300e, 0x300f,
59 0x3010, 0x3011,
60 0x3014, 0x3015,
61 0x3016, 0x3017,
62 0x3018, 0x3019,
63 0x301a, 0x301b
66 const int32_t ScriptRun::pairedCharCount = ARRAY_SIZE(pairedChars);
67 const int32_t ScriptRun::pairedCharPower = 1 << highBit(pairedCharCount);
68 const int32_t ScriptRun::pairedCharExtra = pairedCharCount - pairedCharPower;
70 int8_t ScriptRun::highBit(int32_t value)
72 if (value <= 0) {
73 return -32;
76 int8_t bit = 0;
78 if (value >= 1 << 16) {
79 value >>= 16;
80 bit += 16;
83 if (value >= 1 << 8) {
84 value >>= 8;
85 bit += 8;
88 if (value >= 1 << 4) {
89 value >>= 4;
90 bit += 4;
93 if (value >= 1 << 2) {
94 value >>= 2;
95 bit += 2;
98 if (value >= 1 << 1) {
99 value >>= 1;
100 bit += 1;
103 return bit;
106 int32_t ScriptRun::getPairIndex(UChar32 ch)
108 int32_t probe = pairedCharPower;
109 int32_t index = 0;
111 if (ch >= pairedChars[pairedCharExtra]) {
112 index = pairedCharExtra;
115 while (probe > (1 << 0)) {
116 probe >>= 1;
118 if (ch >= pairedChars[index + probe]) {
119 index += probe;
123 if (pairedChars[index] != ch) {
124 index = -1;
127 return index;
130 UBool ScriptRun::sameScript(int32_t scriptOne, int32_t scriptTwo)
132 return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo;
135 UBool ScriptRun::next()
137 int32_t startSP = parenSP; // used to find the first new open character
138 UErrorCode error = U_ZERO_ERROR;
140 // if we've fallen off the end of the text, we're done
141 if (scriptEnd >= charLimit) {
142 return false;
145 scriptCode = USCRIPT_COMMON;
147 for (scriptStart = scriptEnd; scriptEnd < charLimit; scriptEnd += 1) {
148 UChar high = charArray[scriptEnd];
149 UChar32 ch = high;
151 // if the character is a high surrogate and it's not the last one
152 // in the text, see if it's followed by a low surrogate
153 if (high >= 0xD800 && high <= 0xDBFF && scriptEnd < charLimit - 1)
155 UChar low = charArray[scriptEnd + 1];
157 // if it is followed by a low surrogate,
158 // consume it and form the full character
159 if (low >= 0xDC00 && low <= 0xDFFF) {
160 ch = (high - 0xD800) * 0x0400 + low - 0xDC00 + 0x10000;
161 scriptEnd += 1;
165 UScriptCode sc = uscript_getScript(ch, &error);
166 int32_t pairIndex = getPairIndex(ch);
168 // Paired character handling:
170 // if it's an open character, push it onto the stack.
171 // if it's a close character, find the matching open on the
172 // stack, and use that script code. Any non-matching open
173 // characters above it on the stack will be poped.
174 if (pairIndex >= 0) {
175 if ((pairIndex & 1) == 0) {
176 parenStack[++parenSP].pairIndex = pairIndex;
177 parenStack[parenSP].scriptCode = scriptCode;
178 } else if (parenSP >= 0) {
179 int32_t pi = pairIndex & ~1;
181 while (parenSP >= 0 && parenStack[parenSP].pairIndex != pi) {
182 parenSP -= 1;
185 if (parenSP < startSP) {
186 startSP = parenSP;
189 if (parenSP >= 0) {
190 sc = parenStack[parenSP].scriptCode;
195 if (sameScript(scriptCode, sc)) {
196 if (scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
197 scriptCode = sc;
199 // now that we have a final script code, fix any open
200 // characters we pushed before we knew the script code.
201 while (startSP < parenSP) {
202 parenStack[++startSP].scriptCode = scriptCode;
206 // if this character is a close paired character,
207 // pop it from the stack
208 if (pairIndex >= 0 && (pairIndex & 1) != 0 && parenSP >= 0) {
209 parenSP -= 1;
210 startSP -= 1;
212 } else {
213 // if the run broke on a surrogate pair,
214 // end it before the high surrogate
215 if (ch >= 0x10000) {
216 scriptEnd -= 1;
219 break;
223 return true;