LCOV - code coverage report
Current view: top level - vcl/generic/glyphs - scrptrun.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 65 77 84.4 %
Date: 2014-04-11 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          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         172 : const int32_t ScriptRun::pairedCharPower = 1 << highBit(pairedCharCount);
      68         172 : const int32_t ScriptRun::pairedCharExtra = pairedCharCount - pairedCharPower;
      69             : 
      70         172 : int8_t ScriptRun::highBit(int32_t value)
      71             : {
      72         172 :     if (value <= 0) {
      73           0 :         return -32;
      74             :     }
      75             : 
      76         172 :     int8_t bit = 0;
      77             : 
      78         172 :     if (value >= 1 << 16) {
      79           0 :         value >>= 16;
      80           0 :         bit += 16;
      81             :     }
      82             : 
      83         172 :     if (value >= 1 << 8) {
      84           0 :         value >>= 8;
      85           0 :         bit += 8;
      86             :     }
      87             : 
      88         172 :     if (value >= 1 << 4) {
      89         172 :         value >>= 4;
      90         172 :         bit += 4;
      91             :     }
      92             : 
      93         172 :     if (value >= 1 << 2) {
      94           0 :         value >>= 2;
      95           0 :         bit += 2;
      96             :     }
      97             : 
      98         172 :     if (value >= 1 << 1) {
      99         172 :         value >>= 1;
     100         172 :         bit += 1;
     101             :     }
     102             : 
     103         172 :     return bit;
     104             : }
     105             : 
     106   197098823 : int32_t ScriptRun::getPairIndex(UChar32 ch)
     107             : {
     108   197098823 :     int32_t probe = pairedCharPower;
     109   197098823 :     int32_t index = 0;
     110             : 
     111   197098823 :     if (ch >= pairedChars[pairedCharExtra]) {
     112    38442414 :         index = pairedCharExtra;
     113             :     }
     114             : 
     115  1379691761 :     while (probe > (1 << 0)) {
     116   985494115 :         probe >>= 1;
     117             : 
     118   985494115 :         if (ch >= pairedChars[index + probe]) {
     119   222030832 :             index += probe;
     120             :         }
     121             :     }
     122             : 
     123   197098823 :     if (pairedChars[index] != ch) {
     124   197025471 :         index = -1;
     125             :     }
     126             : 
     127   197098823 :     return index;
     128             : }
     129             : 
     130   197098823 : UBool ScriptRun::sameScript(int32_t scriptOne, int32_t scriptTwo)
     131             : {
     132   197098823 :     return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo;
     133             : }
     134             : 
     135     1850627 : UBool ScriptRun::next()
     136             : {
     137     1850627 :     int32_t startSP  = parenSP;  // used to find the first new open character
     138     1850627 :     UErrorCode error = U_ZERO_ERROR;
     139             : 
     140             :     // if we've fallen off the end of the text, we're done
     141     1850627 :     if (scriptEnd >= charLimit) {
     142      923728 :         return false;
     143             :     }
     144             : 
     145      926899 :     scriptCode = USCRIPT_COMMON;
     146             : 
     147   198023274 :     for (scriptStart = scriptEnd; scriptEnd < charLimit; scriptEnd += 1) {
     148   197098823 :         UChar   high = charArray[scriptEnd];
     149   197098823 :         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   197098823 :         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   197098823 :         UScriptCode sc = uscript_getScript(ch, &error);
     166   197098823 :         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   197098823 :         if (pairIndex >= 0) {
     175       73352 :             if ((pairIndex & 1) == 0) {
     176       34557 :                 parenStack[++parenSP].pairIndex = pairIndex;
     177       34557 :                 parenStack[parenSP].scriptCode  = scriptCode;
     178       38795 :             } else if (parenSP >= 0) {
     179       31973 :                 int32_t pi = pairIndex & ~1;
     180             : 
     181       64225 :                 while (parenSP >= 0 && parenStack[parenSP].pairIndex != pi) {
     182         279 :                     parenSP -= 1;
     183             :                 }
     184             : 
     185       31973 :                 if (parenSP < startSP) {
     186         123 :                     startSP = parenSP;
     187             :                 }
     188             : 
     189       31973 :                 if (parenSP >= 0) {
     190       31694 :                     sc = parenStack[parenSP].scriptCode;
     191             :                 }
     192             :             }
     193             :         }
     194             : 
     195   197098823 :         if (sameScript(scriptCode, sc)) {
     196   197096375 :             if (scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
     197      608157 :                 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     1219786 :                 while (startSP < parenSP) {
     202        3472 :                     parenStack[++startSP].scriptCode = scriptCode;
     203             :                 }
     204             :             }
     205             : 
     206             :             // if this character is a close paired character,
     207             :             // pop it from the stack
     208   197096375 :             if (pairIndex >= 0 && (pairIndex & 1) != 0 && parenSP >= 0) {
     209       31690 :                 parenSP -= 1;
     210       31690 :                 startSP -= 1;
     211             :             }
     212             :         } else {
     213             :             // if the run broke on a surrogate pair,
     214             :             // end it before the high surrogate
     215        2448 :             if (ch >= 0x10000) {
     216           0 :                 scriptEnd -= 1;
     217             :             }
     218             : 
     219        2448 :             break;
     220             :         }
     221             :     }
     222             : 
     223      926899 :     return true;
     224         516 : }
     225             : 

Generated by: LCOV version 1.10