Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
Rtf | n/a |
0 / 0 |
n/a |
0 / 0 |
20 | n/a |
0 / 0 |
|||
escapeAsciiCharacter | n/a |
0 / 0 |
n/a |
0 / 0 |
7 | |||||
escapeMultibyteCharacter | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
escapeSingleValue | n/a |
0 / 0 |
n/a |
0 / 0 |
12 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord\Escaper; |
19 | |
20 | /** |
21 | * @since 0.13.0 |
22 | * |
23 | * @codeCoverageIgnore |
24 | */ |
25 | class Rtf extends AbstractEscaper |
26 | { |
27 | protected function escapeAsciiCharacter($code) |
28 | { |
29 | if ($code == 9) { |
30 | return '{\\tab}'; |
31 | } |
32 | if (0x20 > $code || $code >= 0x80) { |
33 | return '{\\u' . $code . '}'; |
34 | } |
35 | if ($code == 123 || $code == 125 || $code == 92) { // open or close brace or backslash |
36 | return '\\' . chr($code); |
37 | } |
38 | |
39 | return chr($code); |
40 | } |
41 | |
42 | protected function escapeMultibyteCharacter($code) |
43 | { |
44 | return '\\uc0{\\u' . $code . '}'; |
45 | } |
46 | |
47 | /** |
48 | * @see http://www.randomchaos.com/documents/?source=php_and_unicode |
49 | * |
50 | * @param ?string $input |
51 | */ |
52 | protected function escapeSingleValue($input) |
53 | { |
54 | $escapedValue = ''; |
55 | |
56 | $numberOfBytes = 1; |
57 | $bytes = []; |
58 | for ($i = 0; $i < strlen($input); ++$i) { |
59 | $character = $input[$i]; |
60 | $asciiCode = ord($character); |
61 | |
62 | if ($asciiCode < 128) { |
63 | $escapedValue .= $this->escapeAsciiCharacter($asciiCode); |
64 | } else { |
65 | if (0 == count($bytes)) { |
66 | if ($asciiCode < 224) { |
67 | $numberOfBytes = 2; |
68 | } elseif ($asciiCode < 240) { |
69 | $numberOfBytes = 3; |
70 | } elseif ($asciiCode < 248) { |
71 | $numberOfBytes = 4; |
72 | } |
73 | } |
74 | |
75 | $bytes[] = $asciiCode; |
76 | |
77 | if ($numberOfBytes == count($bytes)) { |
78 | if (4 == $numberOfBytes) { |
79 | $multibyteCode = ($bytes[0] % 8) * 262144 + ($bytes[1] % 64) * 4096 + ($bytes[2] % 64) * 64 + ($bytes[3] % 64); |
80 | } elseif (3 == $numberOfBytes) { |
81 | $multibyteCode = ($bytes[0] % 16) * 4096 + ($bytes[1] % 64) * 64 + ($bytes[2] % 64); |
82 | } else { |
83 | $multibyteCode = ($bytes[0] % 32) * 64 + ($bytes[1] % 64); |
84 | } |
85 | |
86 | if (65279 != $multibyteCode) { |
87 | $escapedValue .= $multibyteCode < 128 ? $this->escapeAsciiCharacter($multibyteCode) : $this->escapeMultibyteCharacter($multibyteCode); |
88 | } |
89 | |
90 | $numberOfBytes = 1; |
91 | $bytes = []; |
92 | } |
93 | } |
94 | } |
95 | |
96 | return $escapedValue; |
97 | } |
98 | } |