michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1999-2001, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: scrptrun.cpp michael@0: * michael@0: * created on: 10/17/2001 michael@0: * created by: Eric R. Mader michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/uscript.h" michael@0: michael@0: #include "scrptrun.h" michael@0: michael@0: #define ARRAY_SIZE(array) (sizeof array / sizeof array[0]) michael@0: michael@0: const char ScriptRun::fgClassID=0; michael@0: michael@0: UChar32 ScriptRun::pairedChars[] = { michael@0: 0x0028, 0x0029, // ascii paired punctuation michael@0: 0x003c, 0x003e, michael@0: 0x005b, 0x005d, michael@0: 0x007b, 0x007d, michael@0: 0x00ab, 0x00bb, // guillemets michael@0: 0x2018, 0x2019, // general punctuation michael@0: 0x201c, 0x201d, michael@0: 0x2039, 0x203a, michael@0: 0x3008, 0x3009, // chinese paired punctuation michael@0: 0x300a, 0x300b, michael@0: 0x300c, 0x300d, michael@0: 0x300e, 0x300f, michael@0: 0x3010, 0x3011, michael@0: 0x3014, 0x3015, michael@0: 0x3016, 0x3017, michael@0: 0x3018, 0x3019, michael@0: 0x301a, 0x301b michael@0: }; michael@0: michael@0: const int32_t ScriptRun::pairedCharCount = ARRAY_SIZE(pairedChars); michael@0: const int32_t ScriptRun::pairedCharPower = 1 << highBit(pairedCharCount); michael@0: const int32_t ScriptRun::pairedCharExtra = pairedCharCount - pairedCharPower; michael@0: michael@0: int8_t ScriptRun::highBit(int32_t value) michael@0: { michael@0: if (value <= 0) { michael@0: return -32; michael@0: } michael@0: michael@0: int8_t bit = 0; michael@0: michael@0: if (value >= 1 << 16) { michael@0: value >>= 16; michael@0: bit += 16; michael@0: } michael@0: michael@0: if (value >= 1 << 8) { michael@0: value >>= 8; michael@0: bit += 8; michael@0: } michael@0: michael@0: if (value >= 1 << 4) { michael@0: value >>= 4; michael@0: bit += 4; michael@0: } michael@0: michael@0: if (value >= 1 << 2) { michael@0: value >>= 2; michael@0: bit += 2; michael@0: } michael@0: michael@0: if (value >= 1 << 1) { michael@0: value >>= 1; michael@0: bit += 1; michael@0: } michael@0: michael@0: return bit; michael@0: } michael@0: michael@0: int32_t ScriptRun::getPairIndex(UChar32 ch) michael@0: { michael@0: int32_t probe = pairedCharPower; michael@0: int32_t index = 0; michael@0: michael@0: if (ch >= pairedChars[pairedCharExtra]) { michael@0: index = pairedCharExtra; michael@0: } michael@0: michael@0: while (probe > (1 << 0)) { michael@0: probe >>= 1; michael@0: michael@0: if (ch >= pairedChars[index + probe]) { michael@0: index += probe; michael@0: } michael@0: } michael@0: michael@0: if (pairedChars[index] != ch) { michael@0: index = -1; michael@0: } michael@0: michael@0: return index; michael@0: } michael@0: michael@0: UBool ScriptRun::sameScript(int32_t scriptOne, int32_t scriptTwo) michael@0: { michael@0: return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo; michael@0: } michael@0: michael@0: UBool ScriptRun::next() michael@0: { michael@0: int32_t startSP = parenSP; // used to find the first new open character michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: michael@0: // if we've fallen off the end of the text, we're done michael@0: if (scriptEnd >= charLimit) { michael@0: return false; michael@0: } michael@0: michael@0: scriptCode = USCRIPT_COMMON; michael@0: michael@0: for (scriptStart = scriptEnd; scriptEnd < charLimit; scriptEnd += 1) { michael@0: UChar high = charArray[scriptEnd]; michael@0: UChar32 ch = high; michael@0: michael@0: // if the character is a high surrogate and it's not the last one michael@0: // in the text, see if it's followed by a low surrogate michael@0: if (high >= 0xD800 && high <= 0xDBFF && scriptEnd < charLimit - 1) michael@0: { michael@0: UChar low = charArray[scriptEnd + 1]; michael@0: michael@0: // if it is followed by a low surrogate, michael@0: // consume it and form the full character michael@0: if (low >= 0xDC00 && low <= 0xDFFF) { michael@0: ch = (high - 0xD800) * 0x0400 + low - 0xDC00 + 0x10000; michael@0: scriptEnd += 1; michael@0: } michael@0: } michael@0: michael@0: UScriptCode sc = uscript_getScript(ch, &error); michael@0: int32_t pairIndex = getPairIndex(ch); michael@0: michael@0: // Paired character handling: michael@0: // michael@0: // if it's an open character, push it onto the stack. michael@0: // if it's a close character, find the matching open on the michael@0: // stack, and use that script code. Any non-matching open michael@0: // characters above it on the stack will be poped. michael@0: if (pairIndex >= 0) { michael@0: if ((pairIndex & 1) == 0) { michael@0: parenStack[++parenSP].pairIndex = pairIndex; michael@0: parenStack[parenSP].scriptCode = scriptCode; michael@0: } else if (parenSP >= 0) { michael@0: int32_t pi = pairIndex & ~1; michael@0: michael@0: while (parenSP >= 0 && parenStack[parenSP].pairIndex != pi) { michael@0: parenSP -= 1; michael@0: } michael@0: michael@0: if (parenSP < startSP) { michael@0: startSP = parenSP; michael@0: } michael@0: michael@0: if (parenSP >= 0) { michael@0: sc = parenStack[parenSP].scriptCode; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (sameScript(scriptCode, sc)) { michael@0: if (scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) { michael@0: scriptCode = sc; michael@0: michael@0: // now that we have a final script code, fix any open michael@0: // characters we pushed before we knew the script code. michael@0: while (startSP < parenSP) { michael@0: parenStack[++startSP].scriptCode = scriptCode; michael@0: } michael@0: } michael@0: michael@0: // if this character is a close paired character, michael@0: // pop it from the stack michael@0: if (pairIndex >= 0 && (pairIndex & 1) != 0 && parenSP >= 0) { michael@0: parenSP -= 1; michael@0: startSP -= 1; michael@0: } michael@0: } else { michael@0: // if the run broke on a surrogate pair, michael@0: // end it before the high surrogate michael@0: if (ch >= 0x10000) { michael@0: scriptEnd -= 1; michael@0: } michael@0: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: