Line data Source code
1 : /*
2 : *******************************************************************************
3 : *
4 : * Copyright (c) 1995-2013 International Business Machines Corporation and others
5 : *
6 : * All rights reserved.
7 : *
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.
16 : *
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.
25 : *
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.
29 : *
30 : *******************************************************************************
31 : * file name: scrptrun.cpp
32 : *
33 : * created on: 10/17/2001
34 : * created by: Eric R. Mader
35 : */
36 :
37 : #include "unicode/utypes.h"
38 : #include "unicode/uscript.h"
39 :
40 : #include "scrptrun.h"
41 :
42 : #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
43 :
44 : const char ScriptRun::fgClassID=0;
45 :
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
64 : };
65 :
66 : const int32_t ScriptRun::pairedCharCount = ARRAY_SIZE(pairedChars);
67 1 : const int32_t ScriptRun::pairedCharPower = 1 << highBit(pairedCharCount);
68 1 : const int32_t ScriptRun::pairedCharExtra = pairedCharCount - pairedCharPower;
69 :
70 1 : int8_t ScriptRun::highBit(int32_t value)
71 : {
72 1 : if (value <= 0) {
73 0 : return -32;
74 : }
75 :
76 1 : int8_t bit = 0;
77 :
78 1 : if (value >= 1 << 16) {
79 0 : value >>= 16;
80 0 : bit += 16;
81 : }
82 :
83 1 : if (value >= 1 << 8) {
84 0 : value >>= 8;
85 0 : bit += 8;
86 : }
87 :
88 1 : if (value >= 1 << 4) {
89 1 : value >>= 4;
90 1 : bit += 4;
91 : }
92 :
93 1 : if (value >= 1 << 2) {
94 0 : value >>= 2;
95 0 : bit += 2;
96 : }
97 :
98 1 : if (value >= 1 << 1) {
99 1 : value >>= 1;
100 1 : bit += 1;
101 : }
102 :
103 1 : return bit;
104 : }
105 :
106 0 : int32_t ScriptRun::getPairIndex(UChar32 ch)
107 : {
108 0 : int32_t probe = pairedCharPower;
109 0 : int32_t index = 0;
110 :
111 0 : if (ch >= pairedChars[pairedCharExtra]) {
112 0 : index = pairedCharExtra;
113 : }
114 :
115 0 : while (probe > (1 << 0)) {
116 0 : probe >>= 1;
117 :
118 0 : if (ch >= pairedChars[index + probe]) {
119 0 : index += probe;
120 : }
121 : }
122 :
123 0 : if (pairedChars[index] != ch) {
124 0 : index = -1;
125 : }
126 :
127 0 : return index;
128 : }
129 :
130 0 : UBool ScriptRun::sameScript(int32_t scriptOne, int32_t scriptTwo)
131 : {
132 0 : return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo;
133 : }
134 :
135 0 : UBool ScriptRun::next()
136 : {
137 0 : int32_t startSP = parenSP; // used to find the first new open character
138 0 : UErrorCode error = U_ZERO_ERROR;
139 :
140 : // if we've fallen off the end of the text, we're done
141 0 : if (scriptEnd >= charLimit) {
142 0 : return false;
143 : }
144 :
145 0 : scriptCode = USCRIPT_COMMON;
146 :
147 0 : for (scriptStart = scriptEnd; scriptEnd < charLimit; scriptEnd += 1) {
148 0 : UChar high = charArray[scriptEnd];
149 0 : UChar32 ch = high;
150 :
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 0 : if (high >= 0xD800 && high <= 0xDBFF && scriptEnd < charLimit - 1)
154 : {
155 0 : UChar low = charArray[scriptEnd + 1];
156 :
157 : // if it is followed by a low surrogate,
158 : // consume it and form the full character
159 0 : if (low >= 0xDC00 && low <= 0xDFFF) {
160 0 : ch = (high - 0xD800) * 0x0400 + low - 0xDC00 + 0x10000;
161 0 : scriptEnd += 1;
162 : }
163 : }
164 :
165 0 : UScriptCode sc = uscript_getScript(ch, &error);
166 0 : int32_t pairIndex = getPairIndex(ch);
167 :
168 : // Paired character handling:
169 :
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 0 : if (pairIndex >= 0) {
175 0 : if ((pairIndex & 1) == 0) {
176 0 : parenStack[++parenSP].pairIndex = pairIndex;
177 0 : parenStack[parenSP].scriptCode = scriptCode;
178 0 : } else if (parenSP >= 0) {
179 0 : int32_t pi = pairIndex & ~1;
180 :
181 0 : while (parenSP >= 0 && parenStack[parenSP].pairIndex != pi) {
182 0 : parenSP -= 1;
183 : }
184 :
185 0 : if (parenSP < startSP) {
186 0 : startSP = parenSP;
187 : }
188 :
189 0 : if (parenSP >= 0) {
190 0 : sc = parenStack[parenSP].scriptCode;
191 : }
192 : }
193 : }
194 :
195 0 : if (sameScript(scriptCode, sc)) {
196 0 : if (scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
197 0 : scriptCode = sc;
198 :
199 : // now that we have a final script code, fix any open
200 : // characters we pushed before we knew the script code.
201 0 : while (startSP < parenSP) {
202 0 : parenStack[++startSP].scriptCode = scriptCode;
203 : }
204 : }
205 :
206 : // if this character is a close paired character,
207 : // pop it from the stack
208 0 : if (pairIndex >= 0 && (pairIndex & 1) != 0 && parenSP >= 0) {
209 0 : parenSP -= 1;
210 0 : startSP -= 1;
211 : }
212 : } else {
213 : // if the run broke on a surrogate pair,
214 : // end it before the high surrogate
215 0 : if (ch >= 0x10000) {
216 0 : scriptEnd -= 1;
217 : }
218 :
219 0 : break;
220 : }
221 : }
222 :
223 0 : return true;
224 3 : }
225 :
|