source-class-Com.Tecnick.Unicode.Bidi.StepX

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 
<?php
/**
 * StepX.php
 *
 * @since       2011-05-23
 * @category    Library
 * @package     Unicode
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 * @link        https://github.com/tecnickcom/tc-lib-unicode
 *
 * This file is part of tc-lib-unicode software library.
 */

namespace Com\Tecnick\Unicode\Bidi;

use \Com\Tecnick\Unicode\Bidi\StepP;
use \Com\Tecnick\Unicode\Data\Type as UniType;
use \Com\Tecnick\Unicode\Data\Constant as UniConstant;

/**
 * Com\Tecnick\Unicode\Bidi\StepX
 *
 * @since       2015-07-13
 * @category    Library
 * @package     Unicode
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 * @link        https://github.com/tecnickcom/tc-lib-unicode
 */
class StepX
{
    /**
     * Maximum embedding level
     */
    const MAX_DEPTH = 125;

    /**
     * Directional Status Stack
     *
     * @var array
     */
    protected $dss = array();
    
    /**
     * Overflow Isolate Count
     *
     * @var int
     */
    protected $oic = 0;
    
    /**
     * Overflow Embedding Count
     *
     * @var int
     */
    protected $oec = 0;
    
    /**
     * Valid Isolate Count
     *
     * @var int
     */
    protected $vic = 0;

    /**
     * Array of characters data to return
     *
     * @var array
     */
    protected $chardata = array();

    /**
     * Array of UTF-8 codepoints
     *
     * @var array
     */
    protected $ordarr = array();

    /**
     * X Steps for Bidirectional algorithm
     * Explicit Levels and Directions
     *
     * @param array  $ordarr   Array of UTF-8 codepoints
     * @param int    $pel      Paragraph embedding level
     */
    public function __construct($ordarr, $pel)
    {
        $this->ordarr = $ordarr;
        $this->dss = array();
        $this->chardata = array();
        // X1. At the beginning of a paragraph, perform the following steps:
        //     - Set the stack to empty.
        $this->dss = array();
        //     - Push onto the stack an entry consisting of the paragraph embedding level,
        //       a neutral directional override status, and a false directional isolate status.
        $this->dss[] = array(
            'cel' => $pel,
            'dos' => 'NI',
            'dis' => false
        );
        //     - Set the overflow isolate count to zero.
        $this->oic = 0;
        //     - Set the overflow embedding count to zero.
        $this->oec = 0;
        //     - Set the valid isolate count to zero.
        $this->vic = 0;
        //     - Process each character iteratively, applying rules X2 through X8.
        //       Only embedding levels from 0 through max_depth are valid in this phase.
        //       (Note that in the resolution of levels in rules I1 and I2,
        //       the maximum embedding level of max_depth+1 can be reached.)
        $this->processX();
    }

    /**
     * Returns the processed array
     *
     * @return array
     */
    public function getChrData()
    {
        return $this->chardata;
    }

    /**
     * Calculate the Least Even
     *
     * @param int $num Number to process
     *
     * @return int
     */
    protected function getLEven($num)
    {
        return (2 + $num - ($num % 2));
    }

    /**
     * Calculate the Least Odd
     *
     * @param int $num Number to process
     *
     * @return int
     */
    protected function getLOdd($num)
    {
        return (1 + $num + ($num % 2));
    }

    /**
     * Process X1
     */
    protected function processX()
    {
        foreach ($this->ordarr as $key => $ord) {
            $this->processXcase($key, $ord);
        }
    }

    /**
     * Process X1 case
     *
     * @param int    $pos  Original character position in the input string
     * @param int $ord Char code
     *
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    protected function processXcase($pos, $ord)
    {
        $edss = end($this->dss);
        switch ($ord) {
            case UniConstant::RLE:
                // X2
                $this->setDss($this->getLOdd($edss['cel']), UniConstant::RLE, 'NI');
                break;
            case UniConstant::LRE:
                // X3
                $this->setDss($this->getLEven($edss['cel']), UniConstant::LRE, 'NI');
                break;
            case UniConstant::RLO:
                // X4
                $this->setDss($this->getLOdd($edss['cel']), UniConstant::RLO, 'R');
                break;
            case UniConstant::LRO:
                // X5
                $this->setDss($this->getLEven($edss['cel']), UniConstant::LRO, 'L');
                break;
            case UniConstant::RLI:
                // X5a
                $this->processChar($pos, $ord, $edss);
                $this->setDss($this->getLOdd($edss['cel']), UniConstant::RLI, 'NI', true, true, 1);
                break;
            case UniConstant::LRI:
                // X5b
                $this->processChar($pos, $ord, $edss);
                $this->setDss($this->getLEven($edss['cel']), UniConstant::LRI, 'NI', true, true, 1);
                break;
            case UniConstant::FSI:
                // X5c
                $this->processChar($pos, $ord, $edss);
                $this->processFsiCase($pos, $edss);
                break;
            case UniConstant::PDI:
                // X6a
                $this->processPdiCase($pos, $ord, $edss);
                break;
            case UniConstant::PDF:
                // X7
                $this->processPdfCase($edss);
                break;
            default:
                // X6
                $this->processChar($pos, $ord, $edss);
                break;
        }
    }

    /**
     * Set temporary data (X2 to X5)
     *
     * @param int    $cel     Embedding Level
     * @param int    $ord     Char code
     * @param string $dos     Directional override status
     * @param bool   $dis     Directional isolate status
     * @param string $isolate True if Isolate initiator
     * @param int    $ivic    increment for the valid isolate count
     */
    protected function setDss($cel, $ord, $dos, $dis = false, $isolate = false, $ivic = 0)
    {
        // X2 to X5
        //     - Compute the least odd|even embedding level greater than the embedding level of the last entry
        //       on the directional status stack.
        //     - If this new level would be valid, and the overflow isolate count and overflow embedding
        //       count are both zero, then this RLE is valid. Push an entry consisting of the new embedding
        //       level, neutral|left|right directional override status, and false directional isolate status onto the
        //       directional status stack.
        //     - Otherwise, this is an overflow RLE. If the overflow isolate count is zero, increment the
        //       overflow embedding|isolate count by one. Leave all other variables unchanged.
        if (($cel >= self::MAX_DEPTH) || ($this->oic != 0) || ($this->oec != 0)) {
            if ($isolate) {
                ++$this->oic;
            } elseif ($this->oic == 0) {
                ++$this->oec;
            }
            return;
        }
        $this->vic += $ivic;
        $this->dss[] = array(
            'ord' => $ord,
            'cel' => $cel,
            'dos' => $dos,
            'dis' => $dis
        );
    }

    /**
     * Push a char on the stack
     *
     * @param int    $pos  Original character position in the input string
     * @param int    $ord  Char code
     * @param array  $edss Last entry in the Directional Status Stack
     */
    protected function pushChar($pos, $ord, $edss)
    {
        $unitype = (isset(UniType::$uni[$ord]) ? UniType::$uni[$ord] : $edss['dos']);
        $this->chardata[] = array(
            'pos'   => $pos,
            'char'  => $ord,
            'level' => $edss['cel'],
            'type'  => (($edss['dos'] !== 'NI') ? $edss['dos'] : $unitype),
            'otype' => $unitype // original type
        );
    }

    /**
     * Process normal char (X6)
     *
     * @param int    $pos  Original character position in the input string
     * @param int    $ord  Char code
     * @param array  $edss Last entry in the Directional Status Stack
     */
    protected function processChar($pos, $ord, $edss)
    {
        // X6. For all types besides B, BN, RLE, LRE, RLO, LRO, PDF, RLI, LRI, FSI, and PDI:
        //     - Set the current character’s embedding level to the embedding level
        //       of the last entry on the directional status stack.
        //     - Whenever the directional override status of the last entry on the directional status stack
        //       is not neutral, reset the current character type according to the directional override
        //       status of the last entry on the directional status stack.
        if (isset(UniType::$uni[$ord]) && ((UniType::$uni[$ord] == 'B') || (UniType::$uni[$ord] == 'BN'))) {
            return;
        }
        $this->pushChar($pos, $ord, $edss);
    }

    /**
     * Process the PDF type character
     *
     * @param array  $edss Last entry in the Directional Status Stack
     */
    protected function processPdfCase($edss)
    {
        // X7. With each PDF, perform the following steps:
        //     - If the overflow isolate count is greater than zero, do nothing. (This PDF is within the
        //       scope of an overflow isolate initiator. It either matches and terminates the scope of an
        //       overflow embedding initiator within that overflow isolate, or does not match any
        //       embedding initiator.)
        if ($this->oic > 0) {
            return;
        }
        //     - Otherwise, if the overflow embedding count is greater than zero, decrement it by one.
        //       (This PDF matches and terminates the scope of an overflow embedding initiator that is not
        //       within the scope of an overflow isolate initiator.)
        if ($this->oec > 0) {
            --$this->oec;
            return;
        }
        //     - Otherwise, if the directional isolate status of the last entry on the directional status
        //       stack is false, and the directional status stack contains at least two entries, pop the
        //       last entry from the directional status stack. (This PDF matches and terminates the scope
        //       of a valid embedding initiator. Since the stack has at least two entries, this pop does
        //       not leave the stack empty.)
        if (($edss['dis'] === false) && (count($this->dss) > 1)) {
            array_pop($this->dss);
        }
        //     - Otherwise, do nothing. (This PDF does not match any embedding initiator.)
    }

    /**
     * Process the PDI type character
     *
     * @param int    $pos  Original character position in the input string
     * @param int    $ord  Char code
     * @param array  $edss Last entry in the Directional Status Stack
     */
    protected function processPdiCase($pos, $ord, $edss)
    {
        // X6a. With each PDI, perform the following steps:
        //      - If the overflow isolate count is greater than zero, this PDI matches an overflow isolate
        //        initiator. Decrement the overflow isolate count by one.
        if ($this->oic > 0) {
            --$this->oic;
            return;
        }
        //      - Otherwise, if the valid isolate count is zero, this PDI does not match any isolate
        //        initiator, valid or overflow. Do nothing.
        if ($this->vic == 0) {
            return;
        }
        //      - Otherwise, this PDI matches a valid isolate initiator. Perform the following steps:
        //        - Reset the overflow embedding count to zero. (This terminates the scope of those overflow
        //          embedding initiators within the scope of the matched isolate initiator whose scopes have
        //          not been terminated by a matching PDF, and which thus lack a matching PDF.)
        $this->oec = 0;
        //        - While the directional isolate status of the last entry on the stack is false, pop the
        //          last entry from the directional status stack. (This terminates the scope of those valid
        //          embedding initiators within the scope of the matched isolate initiator whose scopes have
        //          not been terminated by a matching PDF, and which thus lack a matching PDF. Given that the
        //          valid isolate count is non-zero, the directional status stack before this step is
        //          executed must contain an entry with directional isolate status true, and thus after this
        //          step is executed the last entry on the stack will indeed have a true directional isolate
        //          status, i.e. represent the scope of the matched isolate initiator. This cannot be the
        //          stack's first entry, which always belongs to the paragraph level and has a false
        //          directional status, so there is at least one more entry below it on the stack.)
        while (($edss['dis'] === false) && (count($this->dss) > 1)) {
            array_pop($this->dss);
            $edss = end($this->dss);
        }
        //        - Pop the last entry from the directional status stack and decrement the valid isolate
        //          count by one. (This terminates the scope of the matched isolate initiator. Since the
        //          preceding step left the stack with at least two entries, this pop does not leave the
        //          stack empty.)
        array_pop($this->dss);
        $edss = end($this->dss);
        --$this->vic;
        //      - In all cases, look up the last entry on the directional status stack left after the
        //        steps above and:
        //        - Set the PDI’s level to the entry's embedding level.
        //        - If the entry's directional override status is not neutral, reset the current character type
        //          from PDI to L if the override status is left-to-right, and to R if the override status is
        //          right-to-left.
        $this->pushChar($pos, $ord, $edss);
    }

    /**
     * Process the PDF type character
     *
     * @param int    $pos  Original character position in the input string
     * @param array  $edss Last entry in the Directional Status Stack
     */
    protected function processFsiCase($pos, $edss)
    {
        // X5c. With each FSI, apply rules P2 and P3 to the sequence of characters between the FSI and its
        //      matching PDI, or if there is no matching PDI, the end of the paragraph, as if this sequence
        //      of characters were a paragraph. If these rules decide on paragraph embedding level 1, treat
        //      the FSI as an RLI in rule X5a. Otherwise, treat it as an LRI in rule X5b.
        $stepp = new StepP(array_slice($this->ordarr, $pos));
        if ($stepp->getPel() == 0) {
            $this->setDss($this->getLEven($edss['cel']), UniConstant::LRI, 'NI', true, true, 1);
        } else {
            $this->setDss($this->getLOdd($edss['cel']), UniConstant::RLI, 'NI', true, true, 1);
        }
    }
}
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy