| Total Complexity | 197 |
| Total Lines | 1109 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RightToLeftSupport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RightToLeftSupport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class RightToLeftSupport |
||
| 43 | { |
||
| 44 | private const UTF8_LRM = "\xE2\x80\x8E"; // U+200E (Left to Right mark: zero-width character with LTR directionality) |
||
| 45 | private const UTF8_RLM = "\xE2\x80\x8F"; // U+200F (Right to Left mark: zero-width character with RTL directionality) |
||
| 46 | private const UTF8_LRO = "\xE2\x80\xAD"; // U+202D (Left to Right override: force everything following to LTR mode) |
||
| 47 | private const UTF8_RLO = "\xE2\x80\xAE"; // U+202E (Right to Left override: force everything following to RTL mode) |
||
| 48 | private const UTF8_LRE = "\xE2\x80\xAA"; // U+202A (Left to Right embedding: treat everything following as LTR text) |
||
| 49 | private const UTF8_RLE = "\xE2\x80\xAB"; // U+202B (Right to Left embedding: treat everything following as RTL text) |
||
| 50 | private const UTF8_PDF = "\xE2\x80\xAC"; // U+202C (Pop directional formatting: restore state prior to last LRO, RLO, LRE, RLE) |
||
| 51 | |||
| 52 | private const OPEN_PARENTHESES = '([{'; |
||
| 53 | |||
| 54 | private const CLOSE_PARENTHESES = ')]}'; |
||
| 55 | |||
| 56 | private const NUMBERS = '0123456789'; |
||
| 57 | |||
| 58 | private const NUMBER_PREFIX = '+-'; // Treat these like numbers when at beginning or end of numeric strings |
||
| 59 | |||
| 60 | private const NUMBER_PUNCTUATION = '- ,.:/'; // Treat these like numbers when inside numeric strings |
||
| 61 | |||
| 62 | private const PUNCTUATION = ',.:;?!'; |
||
| 63 | |||
| 64 | // Markup |
||
| 65 | private const START_LTR = '<LTR>'; |
||
| 66 | private const END_LTR = '</LTR>'; |
||
| 67 | private const START_RTL = '<RTL>'; |
||
| 68 | private const END_RTL = '</RTL>'; |
||
| 69 | private const LENGTH_START = 5; |
||
| 70 | private const LENGTH_END = 6; |
||
| 71 | |||
| 72 | /* Were we previously processing LTR or RTL. */ |
||
| 73 | private static string $previousState; |
||
| 74 | |||
| 75 | /* Are we currently processing LTR or RTL. */ |
||
| 76 | private static string $currentState; |
||
| 77 | |||
| 78 | /* Text waiting to be processed. */ |
||
| 79 | private static string $waitingText; |
||
| 80 | |||
| 81 | /* Offset into the text. */ |
||
| 82 | private static int $posSpanStart; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * This function strips ‎ and ‏ from the input string. It should be used for all |
||
| 86 | * text that has been passed through the PrintReady() function before that text is stored |
||
| 87 | * in the database. The database should NEVER contain these characters. |
||
| 88 | * |
||
| 89 | * @param string $inputText The string from which the ‎ and ‏ characters should be stripped |
||
| 90 | * |
||
| 91 | * @return string The input string, with ‎ and ‏ stripped |
||
| 92 | */ |
||
| 93 | private static function stripLrmRlm(string $inputText): string |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * This function encapsulates all texts in the input with <span dir='xxx'> and </span> |
||
| 112 | * according to the directionality specified. |
||
| 113 | * |
||
| 114 | * @param string $inputText Raw input |
||
| 115 | * |
||
| 116 | * @return string The string with all texts encapsulated as required |
||
| 117 | */ |
||
| 118 | public static function spanLtrRtl(string $inputText): string |
||
| 119 | { |
||
| 120 | if ($inputText === '') { |
||
| 121 | // Nothing to do |
||
| 122 | return ''; |
||
| 123 | } |
||
| 124 | |||
| 125 | $workingText = str_replace("\n", '<br>', $inputText); |
||
| 126 | $workingText = str_replace([ |
||
| 127 | '<span class="starredname"><br>', |
||
| 128 | '<span<br>class="starredname">', |
||
| 129 | ], '<br><span class="starredname">', $workingText); // Reposition some incorrectly placed line breaks |
||
| 130 | $workingText = self::stripLrmRlm($workingText); // Get rid of any existing UTF8 control codes |
||
| 131 | |||
| 132 | self::$previousState = ''; |
||
| 133 | self::$currentState = strtoupper(I18N::direction()); |
||
| 134 | $numberState = false; // Set when we're inside a numeric string |
||
| 135 | $result = ''; |
||
| 136 | self::$waitingText = ''; |
||
| 137 | $openParDirection = []; |
||
| 138 | |||
| 139 | self::beginCurrentSpan($result); |
||
| 140 | |||
| 141 | while ($workingText !== '') { |
||
| 142 | $charArray = self::getChar($workingText, 0); // Get the next ASCII or UTF-8 character |
||
| 143 | $currentLetter = $charArray['letter']; |
||
| 144 | $currentLen = $charArray['length']; |
||
| 145 | |||
| 146 | $openParIndex = strpos(self::OPEN_PARENTHESES, $currentLetter); // Which opening parenthesis is this? |
||
| 147 | $closeParIndex = strpos(self::CLOSE_PARENTHESES, $currentLetter); // Which closing parenthesis is this? |
||
| 148 | |||
| 149 | switch ($currentLetter) { |
||
| 150 | case '<': |
||
| 151 | // Assume this '<' starts an HTML element |
||
| 152 | $endPos = strpos($workingText, '>'); // look for the terminating '>' |
||
| 153 | if ($endPos === false) { |
||
| 154 | $endPos = 0; |
||
| 155 | } |
||
| 156 | $currentLen += $endPos; |
||
| 157 | $element = substr($workingText, 0, $currentLen); |
||
| 158 | $temp = strtolower(substr($element, 0, 3)); |
||
| 159 | if (strlen($element) < 7 && $temp === '<br') { |
||
| 160 | if ($numberState) { |
||
| 161 | $numberState = false; |
||
| 162 | if (self::$currentState === 'RTL') { |
||
| 163 | self::$waitingText .= self::UTF8_PDF; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | self::breakCurrentSpan($result); |
||
| 167 | } elseif (self::$waitingText === '') { |
||
| 168 | $result .= $element; |
||
| 169 | } else { |
||
| 170 | self::$waitingText .= $element; |
||
| 171 | } |
||
| 172 | $workingText = substr($workingText, $currentLen); |
||
| 173 | break; |
||
| 174 | case '&': |
||
| 175 | // Assume this '&' starts an HTML entity |
||
| 176 | $endPos = strpos($workingText, ';'); // look for the terminating ';' |
||
| 177 | if ($endPos === false) { |
||
| 178 | $endPos = 0; |
||
| 179 | } |
||
| 180 | $currentLen += $endPos; |
||
| 181 | $entity = substr($workingText, 0, $currentLen); |
||
| 182 | if (strtolower($entity) === ' ') { |
||
| 183 | $entity = ' '; // Ensure consistent case for this entity |
||
| 184 | } |
||
| 185 | if (self::$waitingText === '') { |
||
| 186 | $result .= $entity; |
||
| 187 | } else { |
||
| 188 | self::$waitingText .= $entity; |
||
| 189 | } |
||
| 190 | $workingText = substr($workingText, $currentLen); |
||
| 191 | break; |
||
| 192 | case '{': |
||
| 193 | if (substr($workingText, 1, 1) === '{') { |
||
| 194 | // Assume this '{{' starts a TCPDF directive |
||
| 195 | $endPos = strpos($workingText, '}}'); // look for the terminating '}}' |
||
| 196 | if ($endPos === false) { |
||
| 197 | $endPos = 0; |
||
| 198 | } |
||
| 199 | $currentLen = $endPos + 2; |
||
| 200 | $directive = substr($workingText, 0, $currentLen); |
||
| 201 | $workingText = substr($workingText, $currentLen); |
||
| 202 | $result .= self::$waitingText . $directive; |
||
| 203 | self::$waitingText = ''; |
||
| 204 | break; |
||
| 205 | } |
||
| 206 | // no break |
||
| 207 | default: |
||
| 208 | // Look for strings of numbers with optional leading or trailing + or - |
||
| 209 | // and with optional embedded numeric punctuation |
||
| 210 | if ($numberState) { |
||
| 211 | // If we're inside a numeric string, look for reasons to end it |
||
| 212 | $offset = 0; // Be sure to look at the current character first |
||
| 213 | $charArray = self::getChar($workingText . "\n", $offset); |
||
| 214 | if (!str_contains(self::NUMBERS, $charArray['letter'])) { |
||
| 215 | // This is not a digit. Is it numeric punctuation? |
||
| 216 | if (substr($workingText . "\n", $offset, 6) === ' ') { |
||
| 217 | $offset += 6; // This could be numeric punctuation |
||
| 218 | } elseif (str_contains(self::NUMBER_PUNCTUATION, $charArray['letter'])) { |
||
| 219 | $offset += $charArray['length']; // This could be numeric punctuation |
||
| 220 | } |
||
| 221 | // If the next character is a digit, the current character is numeric punctuation |
||
| 222 | $charArray = self::getChar($workingText . "\n", $offset); |
||
| 223 | if (!str_contains(self::NUMBERS, $charArray['letter'])) { |
||
| 224 | // This is not a digit. End the run of digits and punctuation. |
||
| 225 | $numberState = false; |
||
| 226 | if (self::$currentState === 'RTL') { |
||
| 227 | if (!str_contains(self::NUMBER_PREFIX, $currentLetter)) { |
||
| 228 | $currentLetter = self::UTF8_PDF . $currentLetter; |
||
| 229 | } else { |
||
| 230 | $currentLetter .= self::UTF8_PDF; // Include a trailing + or - in the run |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } elseif (str_contains(self::NUMBER_PREFIX, $currentLetter)) { |
||
| 236 | // If we're outside a numeric string, look for reasons to start it |
||
| 237 | // This might be a number lead-in |
||
| 238 | $offset = $currentLen; |
||
| 239 | $nextChar = substr($workingText . "\n", $offset, 1); |
||
| 240 | if (str_contains(self::NUMBERS, $nextChar)) { |
||
| 241 | $numberState = true; // We found a digit: the lead-in is therefore numeric |
||
| 242 | if (self::$currentState === 'RTL') { |
||
| 243 | $currentLetter = self::UTF8_LRE . $currentLetter; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } elseif (str_contains(self::NUMBERS, $currentLetter)) { |
||
| 247 | $numberState = true; // The current letter is a digit |
||
| 248 | if (self::$currentState === 'RTL') { |
||
| 249 | $currentLetter = self::UTF8_LRE . $currentLetter; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | // Determine the directionality of the current UTF-8 character |
||
| 254 | $newState = self::$currentState; |
||
| 255 | |||
| 256 | while (true) { |
||
| 257 | if (I18N::scriptDirection(I18N::textScript($currentLetter)) === 'rtl') { |
||
| 258 | if (self::$currentState === '') { |
||
| 259 | $newState = 'RTL'; |
||
| 260 | break; |
||
| 261 | } |
||
| 262 | |||
| 263 | if (self::$currentState === 'RTL') { |
||
| 264 | break; |
||
| 265 | } |
||
| 266 | // Switch to RTL only if this isn't a solitary RTL letter |
||
| 267 | $tempText = substr($workingText, $currentLen); |
||
| 268 | while ($tempText !== '') { |
||
| 269 | $nextCharArray = self::getChar($tempText, 0); |
||
| 270 | $nextLetter = $nextCharArray['letter']; |
||
| 271 | $nextLen = $nextCharArray['length']; |
||
| 272 | $tempText = substr($tempText, $nextLen); |
||
| 273 | |||
| 274 | if (I18N::scriptDirection(I18N::textScript($nextLetter)) === 'rtl') { |
||
| 275 | $newState = 'RTL'; |
||
| 276 | break 2; |
||
| 277 | } |
||
| 278 | |||
| 279 | if (str_contains(self::PUNCTUATION, $nextLetter) || str_contains(self::OPEN_PARENTHESES, $nextLetter)) { |
||
| 280 | $newState = 'RTL'; |
||
| 281 | break 2; |
||
| 282 | } |
||
| 283 | |||
| 284 | if ($nextLetter === ' ') { |
||
| 285 | break; |
||
| 286 | } |
||
| 287 | $nextLetter .= substr($tempText . "\n", 0, 5); |
||
| 288 | if ($nextLetter === ' ') { |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | // This is a solitary RTL letter : wrap it in UTF8 control codes to force LTR directionality |
||
| 293 | $currentLetter = self::UTF8_LRO . $currentLetter . self::UTF8_PDF; |
||
| 294 | $newState = 'LTR'; |
||
| 295 | break; |
||
| 296 | } |
||
| 297 | if ($currentLen !== 1 || $currentLetter >= 'A' && $currentLetter <= 'Z' || $currentLetter >= 'a' && $currentLetter <= 'z') { |
||
| 298 | // Since it’s neither Hebrew nor Arabic, this UTF-8 character or ASCII letter must be LTR |
||
| 299 | $newState = 'LTR'; |
||
| 300 | break; |
||
| 301 | } |
||
| 302 | if ($closeParIndex !== false) { |
||
| 303 | // This closing parenthesis has to inherit the matching opening parenthesis' directionality |
||
| 304 | if (!empty($openParDirection[$closeParIndex]) && $openParDirection[$closeParIndex] !== '?') { |
||
| 305 | $newState = $openParDirection[$closeParIndex]; |
||
| 306 | } |
||
| 307 | $openParDirection[$closeParIndex] = ''; |
||
| 308 | break; |
||
| 309 | } |
||
| 310 | self::$waitingText .= $currentLetter; |
||
| 311 | $workingText = substr($workingText, $currentLen); |
||
| 312 | if ($openParIndex !== false) { |
||
| 313 | // Opening parentheses always inherit the following directionality |
||
| 314 | while (true) { |
||
| 315 | if ($workingText === '') { |
||
| 316 | break; |
||
| 317 | } |
||
| 318 | if (str_starts_with($workingText, ' ')) { |
||
| 319 | // Spaces following this left parenthesis inherit the following directionality too |
||
| 320 | self::$waitingText .= ' '; |
||
| 321 | $workingText = substr($workingText, 1); |
||
| 322 | continue; |
||
| 323 | } |
||
| 324 | if (str_starts_with($workingText, ' ')) { |
||
| 325 | // Spaces following this left parenthesis inherit the following directionality too |
||
| 326 | self::$waitingText .= ' '; |
||
| 327 | $workingText = substr($workingText, 6); |
||
| 328 | continue; |
||
| 329 | } |
||
| 330 | break; |
||
| 331 | } |
||
| 332 | $openParDirection[$openParIndex] = '?'; |
||
| 333 | break 2; // double break because we're waiting for more information |
||
| 334 | } |
||
| 335 | |||
| 336 | // We have a digit or a "normal" special character. |
||
| 337 | // |
||
| 338 | // When this character is not at the start of the input string, it inherits the preceding directionality; |
||
| 339 | // at the start of the input string, it assumes the following directionality. |
||
| 340 | // |
||
| 341 | // Exceptions to this rule will be handled later during final clean-up. |
||
| 342 | // |
||
| 343 | if (self::$currentState !== '') { |
||
| 344 | $result .= self::$waitingText; |
||
| 345 | self::$waitingText = ''; |
||
| 346 | } |
||
| 347 | break 2; // double break because we're waiting for more information |
||
| 348 | } |
||
| 349 | if ($newState !== self::$currentState) { |
||
| 350 | // A direction change has occurred |
||
| 351 | self::finishCurrentSpan($result); |
||
| 352 | self::$previousState = self::$currentState; |
||
| 353 | self::$currentState = $newState; |
||
| 354 | self::beginCurrentSpan($result); |
||
| 355 | } |
||
| 356 | self::$waitingText .= $currentLetter; |
||
| 357 | $workingText = substr($workingText, $currentLen); |
||
| 358 | $result .= self::$waitingText; |
||
| 359 | self::$waitingText = ''; |
||
| 360 | |||
| 361 | foreach ($openParDirection as $index => $value) { |
||
| 362 | // Since we now know the proper direction, remember it for all waiting opening parentheses |
||
| 363 | if ($value === '?') { |
||
| 364 | $openParDirection[$index] = self::$currentState; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | break; |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | // We're done. Finish last <span> if necessary |
||
| 373 | if ($numberState) { |
||
| 374 | if (self::$waitingText === '') { |
||
| 375 | if (self::$currentState === 'RTL') { |
||
| 376 | $result .= self::UTF8_PDF; |
||
| 377 | } |
||
| 378 | } elseif (self::$currentState === 'RTL') { |
||
| 379 | self::$waitingText .= self::UTF8_PDF; |
||
| 380 | } |
||
| 381 | } |
||
| 382 | self::finishCurrentSpan($result, true); |
||
| 383 | |||
| 384 | // Get rid of any waiting text |
||
| 385 | if (self::$waitingText !== '') { |
||
| 386 | if (I18N::direction() === 'rtl' && self::$currentState === 'LTR') { |
||
| 387 | $result .= self::START_RTL; |
||
| 388 | $result .= self::$waitingText; |
||
| 389 | $result .= self::END_RTL; |
||
| 390 | } else { |
||
| 391 | $result .= self::START_LTR; |
||
| 392 | $result .= self::$waitingText; |
||
| 393 | $result .= self::END_LTR; |
||
| 394 | } |
||
| 395 | self::$waitingText = ''; |
||
| 396 | } |
||
| 397 | |||
| 398 | // Lastly, do some more cleanups |
||
| 399 | |||
| 400 | // Move leading RTL numeric strings to following LTR text |
||
| 401 | // (this happens when the page direction is RTL and the original text begins with a number and is followed by LTR text) |
||
| 402 | while (substr($result, 0, self::LENGTH_START + 3) === self::START_RTL . self::UTF8_LRE) { |
||
| 403 | $spanEnd = strpos($result, self::END_RTL . self::START_LTR); |
||
| 404 | if ($spanEnd === false) { |
||
| 405 | break; |
||
| 406 | } |
||
| 407 | $textSpan = self::stripLrmRlm(substr($result, self::LENGTH_START + 3, $spanEnd - self::LENGTH_START - 3)); |
||
| 408 | if (I18N::scriptDirection(I18N::textScript($textSpan)) === 'rtl') { |
||
| 409 | break; |
||
| 410 | } |
||
| 411 | $result = self::START_LTR . substr($result, self::LENGTH_START, $spanEnd - self::LENGTH_START) . substr($result, $spanEnd + self::LENGTH_START + self::LENGTH_END); |
||
| 412 | break; |
||
| 413 | } |
||
| 414 | |||
| 415 | // On RTL pages, put trailing "." in RTL numeric strings into its own RTL span |
||
| 416 | if (I18N::direction() === 'rtl') { |
||
| 417 | $result = str_replace(self::UTF8_PDF . '.' . self::END_RTL, self::UTF8_PDF . self::END_RTL . self::START_RTL . '.' . self::END_RTL, $result); |
||
| 418 | } |
||
| 419 | |||
| 420 | // Trim trailing blanks preceding <br> in LTR text |
||
| 421 | while (self::$previousState !== 'RTL') { |
||
| 422 | if (str_contains($result, ' <LTRbr>')) { |
||
| 423 | $result = str_replace(' <LTRbr>', '<LTRbr>', $result); |
||
| 424 | continue; |
||
| 425 | } |
||
| 426 | if (str_contains($result, ' <LTRbr>')) { |
||
| 427 | $result = str_replace(' <LTRbr>', '<LTRbr>', $result); |
||
| 428 | continue; |
||
| 429 | } |
||
| 430 | if (str_contains($result, ' <br>')) { |
||
| 431 | $result = str_replace(' <br>', '<br>', $result); |
||
| 432 | continue; |
||
| 433 | } |
||
| 434 | if (str_contains($result, ' <br>')) { |
||
| 435 | $result = str_replace(' <br>', '<br>', $result); |
||
| 436 | continue; |
||
| 437 | } |
||
| 438 | break; // Neither space nor : we're done |
||
| 439 | } |
||
| 440 | |||
| 441 | // Trim trailing blanks preceding <br> in RTL text |
||
| 442 | while (true) { |
||
| 443 | if (str_contains($result, ' <RTLbr>')) { |
||
| 444 | $result = str_replace(' <RTLbr>', '<RTLbr>', $result); |
||
| 445 | continue; |
||
| 446 | } |
||
| 447 | if (str_contains($result, ' <RTLbr>')) { |
||
| 448 | $result = str_replace(' <RTLbr>', '<RTLbr>', $result); |
||
| 449 | continue; |
||
| 450 | } |
||
| 451 | break; // Neither space nor : we're done |
||
| 452 | } |
||
| 453 | |||
| 454 | // Convert '<LTRbr>' and '<RTLbr' |
||
| 455 | $result = str_replace([ |
||
| 456 | '<LTRbr>', |
||
| 457 | '<RTLbr>', |
||
| 458 | ], [ |
||
| 459 | self::END_LTR . '<br>' . self::START_LTR, |
||
| 460 | self::END_RTL . '<br>' . self::START_RTL, |
||
| 461 | ], $result); |
||
| 462 | |||
| 463 | // Include leading indeterminate directional text in whatever follows |
||
| 464 | if (substr($result . "\n", 0, self::LENGTH_START) !== self::START_LTR && substr($result . "\n", 0, self::LENGTH_START) !== self::START_RTL && !str_starts_with($result . "\n", '<br>')) { |
||
| 465 | $leadingText = ''; |
||
| 466 | while (true) { |
||
| 467 | if ($result === '') { |
||
| 468 | $result = $leadingText; |
||
| 469 | break; |
||
| 470 | } |
||
| 471 | if (substr($result . "\n", 0, self::LENGTH_START) !== self::START_LTR && substr($result . "\n", 0, self::LENGTH_START) !== self::START_RTL) { |
||
| 472 | $leadingText .= substr($result, 0, 1); |
||
| 473 | $result = substr($result, 1); |
||
| 474 | continue; |
||
| 475 | } |
||
| 476 | $result = substr($result, 0, self::LENGTH_START) . $leadingText . substr($result, self::LENGTH_START); |
||
| 477 | break; |
||
| 478 | } |
||
| 479 | } |
||
| 480 | |||
| 481 | // Include solitary "-" and "+" in surrounding RTL text |
||
| 482 | $result = str_replace([ |
||
| 483 | self::END_RTL . self::START_LTR . '-' . self::END_LTR . self::START_RTL, |
||
| 484 | self::END_RTL . self::START_LTR . '+' . self::END_LTR . self::START_RTL, |
||
| 485 | ], [ |
||
| 486 | '-', |
||
| 487 | '+', |
||
| 488 | ], $result); |
||
| 489 | |||
| 490 | //$result = strtr($result, [ |
||
| 491 | // self::END_RTL . self::START_LTR . '-' . self::END_LTR . self::START_RTL => '-', |
||
| 492 | // self::END_RTL . self::START_LTR . '+' . self::END_LTR . self::START_RTL => '+', |
||
| 493 | //]); |
||
| 494 | |||
| 495 | // Remove empty spans |
||
| 496 | $result = str_replace([ |
||
| 497 | self::START_LTR . self::END_LTR, |
||
| 498 | self::START_RTL . self::END_RTL, |
||
| 499 | ], '', $result); |
||
| 500 | |||
| 501 | // Finally, correct '<LTR>', '</LTR>', '<RTL>', and '</RTL>' |
||
| 502 | // LTR text: <span dir="ltr"> text </span> |
||
| 503 | // RTL text: <span dir="rtl"> text </span> |
||
| 504 | |||
| 505 | $result = str_replace([ |
||
| 506 | self::START_LTR, |
||
| 507 | self::END_LTR, |
||
| 508 | self::START_RTL, |
||
| 509 | self::END_RTL, |
||
| 510 | ], [ |
||
| 511 | '<span dir="ltr">', |
||
| 512 | '</span>', |
||
| 513 | '<span dir="rtl">', |
||
| 514 | '</span>', |
||
| 515 | ], $result); |
||
| 516 | |||
| 517 | return $result; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Wrap words that have an asterisk suffix in <u> and </u> tags. |
||
| 522 | * This should underline starred names to show the preferred name. |
||
| 523 | * |
||
| 524 | * @param string $textSpan |
||
| 525 | * @param string $direction |
||
| 526 | * |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | private static function starredName(string $textSpan, string $direction): string |
||
| 530 | { |
||
| 531 | // To avoid a TCPDF bug that mixes up the word order, insert those <u> and </u> tags |
||
| 532 | // only when page and span directions are identical. |
||
| 533 | if ($direction === strtoupper(I18N::direction())) { |
||
| 534 | while (true) { |
||
| 535 | $starPos = strpos($textSpan, '*'); |
||
| 536 | if ($starPos === false) { |
||
| 537 | break; |
||
| 538 | } |
||
| 539 | $trailingText = substr($textSpan, $starPos + 1); |
||
| 540 | $textSpan = substr($textSpan, 0, $starPos); |
||
| 541 | $wordStart = strrpos($textSpan, ' '); // Find the start of the word |
||
| 542 | if ($wordStart !== false) { |
||
| 543 | $leadingText = substr($textSpan, 0, $wordStart + 1); |
||
| 544 | $wordText = substr($textSpan, $wordStart + 1); |
||
| 545 | } else { |
||
| 546 | $leadingText = ''; |
||
| 547 | $wordText = $textSpan; |
||
| 548 | } |
||
| 549 | $textSpan = $leadingText . '<u>' . $wordText . '</u>' . $trailingText; |
||
| 550 | } |
||
| 551 | $textSpan = preg_replace('~<span class="starredname">(.*)</span>~', '<u>\1</u>', $textSpan); |
||
| 552 | // The is a work-around for a TCPDF bug eating blanks. |
||
| 553 | $textSpan = str_replace([ |
||
| 554 | ' <u>', |
||
| 555 | '</u> ', |
||
| 556 | ], [ |
||
| 557 | ' <u>', |
||
| 558 | '</u> ', |
||
| 559 | ], $textSpan); |
||
| 560 | } else { |
||
| 561 | // Text and page directions differ: remove the <span> and </span> |
||
| 562 | $textSpan = preg_replace('~(.*)\*~', '\1', $textSpan); |
||
| 563 | $textSpan = preg_replace('~<span class="starredname">(.*)</span>~', '\1', $textSpan); |
||
| 564 | } |
||
| 565 | |||
| 566 | return $textSpan; |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Get the next character from an input string |
||
| 571 | * |
||
| 572 | * @param string $text |
||
| 573 | * @param int $offset |
||
| 574 | * |
||
| 575 | * @return array{letter:string,length:int} |
||
| 576 | */ |
||
| 577 | private static function getChar(string $text, int $offset): array |
||
| 578 | { |
||
| 579 | if ($text === '') { |
||
| 580 | return [ |
||
| 581 | 'letter' => '', |
||
| 582 | 'length' => 0, |
||
| 583 | ]; |
||
| 584 | } |
||
| 585 | |||
| 586 | $char = substr($text, $offset, 1); |
||
| 587 | $length = 1; |
||
| 588 | if ((ord($char) & 0xE0) === 0xC0) { |
||
| 589 | $length = 2; |
||
| 590 | } |
||
| 591 | if ((ord($char) & 0xF0) === 0xE0) { |
||
| 592 | $length = 3; |
||
| 593 | } |
||
| 594 | if ((ord($char) & 0xF8) === 0xF0) { |
||
| 595 | $length = 4; |
||
| 596 | } |
||
| 597 | $letter = substr($text, $offset, $length); |
||
| 598 | |||
| 599 | return [ |
||
| 600 | 'letter' => $letter, |
||
| 601 | 'length' => $length, |
||
| 602 | ]; |
||
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Insert <br> into current span |
||
| 607 | * |
||
| 608 | * @param string $result |
||
| 609 | * |
||
| 610 | * @return void |
||
| 611 | */ |
||
| 612 | private static function breakCurrentSpan(string &$result): void |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Begin current span |
||
| 624 | * |
||
| 625 | * @param string $result |
||
| 626 | * |
||
| 627 | * @return void |
||
| 628 | */ |
||
| 629 | private static function beginCurrentSpan(string &$result): void |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Finish current span |
||
| 643 | * |
||
| 644 | * @param string $result |
||
| 645 | * @param bool $theEnd |
||
| 646 | * |
||
| 647 | * @return void |
||
| 648 | */ |
||
| 649 | private static function finishCurrentSpan(string &$result, bool $theEnd = false): void |
||
| 1151 | } |
||
| 1152 | } |
||
| 1153 |