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

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: <?php
  2: /**
  3:  * StepL.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     Unicode
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-unicode
 12:  *
 13:  * This file is part of tc-lib-unicode software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Unicode\Bidi;
 17: 
 18: use \Com\Tecnick\Unicode\Data\Mirror as UniMirror;
 19: use \Com\Tecnick\Unicode\Data\Constant as UniConstant;
 20: 
 21: /**
 22:  * Com\Tecnick\Unicode\Bidi\StepL
 23:  *
 24:  * @since       2015-07-13
 25:  * @category    Library
 26:  * @package     Unicode
 27:  * @author      Nicola Asuni <info@tecnick.com>
 28:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 29:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 30:  * @link        https://github.com/tecnickcom/tc-lib-unicode
 31:  */
 32: class StepL
 33: {
 34:     /**
 35:      * Array of characters data to return
 36:      *
 37:      * @var array
 38:      */
 39:     protected $chardata = array();
 40: 
 41:     /**
 42:      * Number of characters in $this->chardata
 43:      *
 44:      * @var int
 45:      */
 46:     protected $numchars = 0;
 47: 
 48:     /**
 49:      * Paragraph embedding level
 50:      *
 51:      * @var int
 52:      */
 53:     protected $pel = 0;
 54: 
 55:     /**
 56:      * Maximul level
 57:      *
 58:      * @var int
 59:      */
 60:     protected $maxlevel = 0;
 61: 
 62:     /**
 63:      * L steps
 64:      *
 65:      * @param array $chardata Array of characters data
 66:      * @param int   $pel      Paragraph embedding level
 67:      */
 68:     public function __construct($chardata, $pel, $maxlevel)
 69:     {
 70:         // reorder chars by their original position
 71:         usort($chardata, function ($apos, $bpos) {
 72:             return ($apos['pos'] - $bpos['pos']);
 73:         });
 74:         $this->chardata = $chardata;
 75:         $this->numchars = count($this->chardata);
 76:         $this->pel = $pel;
 77:         $this->maxlevel = $maxlevel;
 78:         $this->processL1();
 79:         $this->processL2();
 80:     }
 81: 
 82:     /**
 83:      * Returns the processed array
 84:      *
 85:      * @return array
 86:      */
 87:     public function getChrData()
 88:     {
 89:         return $this->chardata;
 90:     }
 91: 
 92:     /**
 93:      * L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
 94:      *     1. Segment separators,
 95:      *     2. Paragraph separators,
 96:      *     3. Any sequence of whitespace characters and/or isolate formatting characters (FSI, LRI, RLI, and PDI)
 97:      *        preceding a segment separator or paragraph separator, and
 98:      *     4. Any sequence of whitespace characters and/or isolate formatting characters (FSI, LRI, RLI, and PDI)
 99:      *        at the end of the line.
100:      */
101:     protected function processL1()
102:     {
103:         for ($idx = 0; $idx < $this->numchars; ++$idx) {
104:             $this->processL1b($idx, $idx);
105:         }
106:     }
107: 
108:     /**
109:      * Internal L1 step
110:      *
111:      * @param int $idx Main character index
112:      * @param int $jdx Current index
113:      */
114:     protected function processL1b($idx, $jdx)
115:     {
116:         if ($jdx >= $this->numchars) {
117:             return;
118:         }
119:         if ((($this->chardata[$jdx]['otype'] == 'S') || ($this->chardata[$jdx]['otype'] == 'B'))
120:             || (($jdx == ($this->numchars - 1)) && ($this->chardata[$jdx]['otype'] == 'WS'))
121:         ) {
122:             $this->chardata[$idx]['level'] = $this->pel;
123:             return;
124:         } elseif (($this->chardata[$jdx]['otype'] != 'WS')
125:             && (($this->chardata[$idx]['char'] < UniConstant::LRI)
126:             || ($this->chardata[$idx]['char'] > UniConstant::PDI))
127:         ) {
128:             return $this->processL1b($idx, ($jdx + 1));
129:         }
130:     }
131: 
132:     /**
133:      * L2. From the highest level found in the text to the lowest odd level on each line,
134:      *     including intermediate levels not actually present in the text,
135:      *     reverse any contiguous sequence of characters that are at that level or higher.
136:      *     This rule reverses a progressively larger series of substrings.
137:      */
138:     protected function processL2()
139:     {
140:         for ($level = $this->maxlevel; $level > 0; --$level) {
141:             $ordered = array();
142:             $reversed = array();
143:             foreach ($this->chardata as $char) {
144:                 if ($char['level'] >= $level) {
145:                     if (($char['type'] == 'R') && (isset(UniMirror::$uni[$char['char']]))) {
146:                         // L4. A character is depicted by a mirrored glyph if and only if
147:                         //     (a) the resolved directionality of that character is R, and
148:                         //     (b) the Bidi_Mirrored property value of that character is true.
149:                         $char['char'] = UniMirror::$uni[$char['char']];
150:                     }
151:                     $reversed[] = $char;
152:                 } else {
153:                     if (!empty($reversed)) {
154:                         $ordered = array_merge($ordered, array_reverse($reversed));
155:                         $reversed = array();
156:                     }
157:                     $ordered[] = $char;
158:                 }
159:             }
160:             if (!empty($reversed)) {
161:                 $ordered = array_merge($ordered, array_reverse($reversed));
162:                 $reversed = array();
163:             }
164:             $this->chardata = $ordered;
165:         }
166:     }
167: }
168: 
 

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