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

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: 
<?php
/**
 * StepXten.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\Data\Constant as UniConstant;

/**
 * Com\Tecnick\Unicode\Bidi\StepXten
 *
 * @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 StepXten
{
    /**
     * Array of characters data to return
     *
     * @var array
     */
    protected $chardata = array();

    /**
     * Paragraph Embedding Level
     *
     * @var int
     */
    protected $pel = 0;

    /**
     * Number of characters
     *
     * @var int
     */
    protected $numchars = 0;

    /**
     * Array of Level Run sequences
     *
     * @var array
     */
    protected $runseq = array();

    /**
     * Number of Level Run sequences
     *
     * @var int
     */
    protected $numrunseq = 0;

    /**
     * Array of Isolated Level Run sequences
     *
     * @var array
     */
    protected $ilrs = array();

    /**
     * X Steps for Bidirectional algorithm
     *
     * @param array  $chardata  Array of UTF-8 codepoints
     * @param int    $pel       Paragraph Embedding Level
     */
    public function __construct($chardata, $pel)
    {
        $this->chardata = $chardata;
        $this->numchars = count($chardata);
        $this->pel = $pel;
        $this->setIsolatedLevelRunSequences();
    }

    /**
     * Get the Isolated Run Sequences
     *
     * @return array
     */
    public function getIsolatedLevelRunSequences()
    {
        return $this->ilrs;
    }

    /**
     * Get the embedded direction (L or R)
     *
     * @param int $level
     *
     * @return string
     */
    protected function getEmbeddedDirection($level)
    {
        return ((($level % 2) == 0) ? 'L' : 'R');
    }

    /**
     * Set Level Run Sequences
     */
    protected function setLevelRunSequences()
    {
        $start = 0;
        while ($start < $this->numchars) {
            $end = ($start + 1);
            while (($end < $this->numchars) && ($this->chardata[$end]['level'] == $this->chardata[$start]['level'])) {
                ++$end;
            }
            --$end;
            $this->runseq[] = array(
                'start' => $start,
                'end'   => $end,
                'e'     => $this->chardata[$start]['level']
            );
            ++$this->numrunseq;
            $start = ($end + 1);
        }
    }

    /**
     * returns true if the input char is an Isolate Initiator
     *
     * @return bool
     */
    protected function isIsolateInitiator($ord)
    {
        return (($ord == UniConstant::RLI) || ($ord == UniConstant::LRI) || ($ord == UniConstant::FSI));
    }

    /**
     * Set level Isolated Level Run Sequences
     *
     * @return array
     */
    protected function setIsolatedLevelRunSequences()
    {
        $this->setLevelRunSequences();
        $numiso = 0;
        foreach ($this->runseq as $idx => $seq) {
            // Create a new level run sequence, and initialize it to contain just that level run
            $isorun = array(
                'e'      => $seq['e'],
                'edir'   => $this->getEmbeddedDirection($seq['e']), // embedded direction
                'start'  => $seq['start'], // position of the first char
                'end'    => $seq['end'],   // position of the last char
                'length' => ($seq['end'] - $seq['start'] + 1),
                'sos'    => '', // start-of-sequence
                'eos'    => '', // end-of-sequence
                'item'   => array()
            );
            for ($jdx = 0; $jdx < $isorun['length']; ++$jdx) {
                $isorun['item'][$jdx] = $this->chardata[($seq['start'] + $jdx)];
            }
            $endchar = $isorun['item'][($jdx - 1)]['char'];

            // While the level run currently last in the sequence ends with an isolate initiator that has a
            // matching PDI, append the level run containing the matching PDI to the sequence.
            // (Note that this matching PDI must be the first character of its level run.)
            $pdimatch = -1;
            if ($this->isIsolateInitiator($endchar)) {
                // find the next sequence with the same level that starts with a PDI
                for ($kdx = ($idx + 1); $kdx < $this->numrunseq; ++$kdx) {
                    if (($this->runseq[$kdx]['e'] == $isorun['e'])
                        && ($this->chardata[$this->runseq[$kdx]['start']]['char'] == UniConstant::PDI)
                    ) {
                        $pdimatch = $this->runseq[$kdx]['start'];
                        $this->chardata[$pdimatch]['pdimatch'] = $numiso;
                        break;
                    }
                }
            }

            // For each level run in the paragraph whose first character is not a PDI,
            // or is a PDI that does not match any isolate initiator
            if (isset($this->chardata[$seq['start']]['pdimatch'])) {
                $parent = $this->chardata[$seq['start']]['pdimatch'];
                $this->ilrs[$parent]['item'] = array_merge($this->ilrs[$parent]['item'], $isorun['item']);
                $this->ilrs[$parent]['length'] += $isorun['length'];
                $this->ilrs[$parent]['end'] += $isorun['end'];
                if ($pdimatch >= 0) {
                    $this->chardata[$pdimatch]['pdimatch'] = $parent;
                }
            } else {
                $this->ilrs[$numiso] = $isorun;
                ++$numiso;
            }
        }
        $this->setStartEndOfSequence();
    }

    /**
     * Determine the start-of-sequence (sos) and end-of-sequence (eos) types, either L or R,
     * for each isolating run sequence.
     */
    protected function setStartEndOfSequence()
    {
        foreach ($this->ilrs as $key => $seq) {
            // For sos, compare the level of the first character in the sequence with the level of the character
            // preceding it in the paragraph (not counting characters removed by X9), and if there is none,
            // with the paragraph embedding level.
            $lev = $seq['item'][0]['level'];
            if ($seq['start'] == 0) {
                $prev = $this->pel;
            } else {
                $lastchr = $this->chardata[($seq['start'] - 1)];
                $prev = $lastchr['level'];
            }
            $this->ilrs[$key]['sos'] = $this->getEmbeddedDirection(($prev > $lev) ? $prev : $lev);

            // For eos, compare the level of the last character in the sequence with the level of the character
            // following it in the paragraph (not counting characters removed by X9), and if there is none or the
            // last character of the sequence is an isolate initiator (lacking a matching PDI), with the paragraph
            // embedding level.
            $lastchr = end($seq['item']);
            $lev = $lastchr['level'];
            if (!isset($this->chardata[($seq['end'] + 1)]['level']) || $this->isIsolateInitiator($lastchr['char'])) {
                $next = $this->pel;
            } else {
                $next = $this->chardata[($seq['end'] + 1)]['level'];
            }
            $this->ilrs[$key]['eos'] = $this->getEmbeddedDirection(($next > $lev) ? $next : $lev);
            
            // If the higher level is odd, the sos or eos is R; otherwise, it is L.
        }
    }
}
 

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