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