2 *******************************************************************************
4 * Copyright (c) 1995-2013 International Business Machines Corporation and others
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 * This file is largely copied from the ICU project,
38 * under folder source/extra/scrptrun/scrptrun.cpp
41 #include <sal/config.h>
43 #include <rtl/character.hxx>
44 #include <unicode/uchar.h>
45 #include <unicode/utypes.h>
46 #include <unicode/uscript.h>
61 std::fill_n(ma00
, 0xff, -1);
62 std::fill_n(ma20
, 0x7f, -1);
63 std::fill_n(ma30
, 0x7f, -1);
65 // characters in the range 0x0000 - 0x007e (inclusive)
66 // ascii paired punctuation
79 // characters in the range 0x2000 - 0x207e (inclusive)
80 // general punctuation
88 // characters in the range 0x3000 - 0x307e (inclusive)
89 // chinese paired punctuation
110 int32_t getPairIndex(UChar32 ch
) const
114 if (ch
>= 0x2000 && ch
< 0x207f)
115 return ma20
[ch
- 0x2000];
116 if (ch
>= 0x3000 && ch
< 0x307f)
117 return ma30
[ch
- 0x3000];
123 UScriptCode
getScript(UChar32 ch
, UErrorCode
* status
)
126 // Make combining marks inherit the script of their bases, regardless of
128 if (u_getIntPropertyValue(ch
, UCHAR_GENERAL_CATEGORY
) == U_NON_SPACING_MARK
)
129 return USCRIPT_INHERITED
;
131 UScriptCode script
= uscript_getScript(ch
, status
);
132 if (U_FAILURE(*status
))
135 // There are three Unicode script codes for Japanese text, but only one
136 // OpenType script tag, so we want to keep them in one run as splitting is
137 // pointless for the purpose of OpenType shaping.
138 if (script
== USCRIPT_KATAKANA
|| script
== USCRIPT_KATAKANA_OR_HIRAGANA
)
139 return USCRIPT_HIRAGANA
;
145 const PairIndices gPairIndices
;
150 const char ScriptRun::fgClassID
=0;
152 static bool sameScript(int32_t scriptOne
, int32_t scriptTwo
)
154 return scriptOne
<= USCRIPT_INHERITED
|| scriptTwo
<= USCRIPT_INHERITED
|| scriptOne
== scriptTwo
;
157 UBool
ScriptRun::next()
159 int32_t startSP
= parenSP
; // used to find the first new open character
160 UErrorCode error
= U_ZERO_ERROR
;
162 // if we've fallen off the end of the text, we're done
163 if (scriptEnd
>= charLimit
) {
167 scriptCode
= USCRIPT_COMMON
;
169 for (scriptStart
= scriptEnd
; scriptEnd
< charLimit
; scriptEnd
+= 1) {
170 UChar high
= charArray
[scriptEnd
];
173 // if the character is a high surrogate and it's not the last one
174 // in the text, see if it's followed by a low surrogate
175 if (rtl::isHighSurrogate(high
) && scriptEnd
< charLimit
- 1)
177 UChar low
= charArray
[scriptEnd
+ 1];
179 // if it is followed by a low surrogate,
180 // consume it and form the full character
181 if (rtl::isLowSurrogate(low
)) {
182 ch
= rtl::combineSurrogates(high
, low
);
187 UScriptCode sc
= getScript(ch
, &error
);
188 int32_t pairIndex
= gPairIndices
.getPairIndex(ch
);
190 // Paired character handling:
192 // if it's an open character, push it onto the stack.
193 // if it's a close character, find the matching open on the
194 // stack, and use that script code. Any non-matching open
195 // characters above it on the stack will be popped.
196 if (pairIndex
>= 0) {
197 if ((pairIndex
& 1) == 0) {
199 int32_t nVecSize
= parenStack
.size();
200 if (parenSP
== nVecSize
)
201 parenStack
.resize(nVecSize
+ 128);
202 parenStack
[parenSP
].pairIndex
= pairIndex
;
203 parenStack
[parenSP
].scriptCode
= scriptCode
;
204 } else if (parenSP
>= 0) {
205 int32_t pi
= pairIndex
& ~1;
207 while (parenSP
>= 0 && parenStack
[parenSP
].pairIndex
!= pi
) {
211 if (parenSP
< startSP
) {
216 sc
= parenStack
[parenSP
].scriptCode
;
221 if (sameScript(scriptCode
, sc
)) {
222 if (scriptCode
<= USCRIPT_INHERITED
&& sc
> USCRIPT_INHERITED
) {
225 // now that we have a final script code, fix any open
226 // characters we pushed before we knew the script code.
227 while (startSP
< parenSP
) {
228 parenStack
[++startSP
].scriptCode
= scriptCode
;
232 // if this character is a close paired character,
233 // pop it from the stack
234 if (pairIndex
>= 0 && (pairIndex
& 1) != 0 && parenSP
>= 0) {
236 /* decrement startSP only if it is >= 0,
237 decrementing it unnecessarily will lead to memory corruption
238 while processing the above while block.
239 e.g. startSP = -4 , parenSP = -1
246 // if the run broke on a surrogate pair,
247 // end it before the high surrogate