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