|
| 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 | + * @link https://github.com/PHPOffice/PHPWord |
| 14 | + * @copyright 2010-2016 PHPWord contributors |
| 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 | + if (20 > $code || $code >= 80) { |
| 29 | + return '{\u' . $code . '}'; |
| 30 | + } else { |
| 31 | + return chr($code); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + protected function escapeMultibyteCharacter($code) { |
| 36 | + return '\uc0{\u' . $code . '}'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @see http://www.randomchaos.com/documents/?source=php_and_unicode |
| 41 | + */ |
| 42 | + protected function escapeSingleValue($input) |
| 43 | + { |
| 44 | + $escapedValue = ''; |
| 45 | + |
| 46 | + $numberOfBytes = 1; |
| 47 | + $bytes = array(); |
| 48 | + for ($i = 0; $i < strlen($input); ++$i) { |
| 49 | + $character = $input[$i]; |
| 50 | + $asciiCode = ord($character); |
| 51 | + |
| 52 | + if ($asciiCode < 128) { |
| 53 | + $escapedValue .= $this->escapeAsciiCharacter($asciiCode); |
| 54 | + } else { |
| 55 | + if (0 == count($bytes)) { |
| 56 | + if ($asciiCode < 224) { |
| 57 | + $numberOfBytes = 2; |
| 58 | + } else if ($asciiCode < 240) { |
| 59 | + $numberOfBytes = 3; |
| 60 | + } else if ($asciiCode < 248) { |
| 61 | + $numberOfBytes = 4; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $bytes[] = $asciiCode; |
| 66 | + |
| 67 | + if ($numberOfBytes == count($bytes)) { |
| 68 | + if (4 == $numberOfBytes) { |
| 69 | + $multibyteCode = ($bytes[0] % 8) * 262144 + ($bytes[1] % 64) * 4096 + ($bytes[2] % 64) * 64 + ($bytes[3] % 64); |
| 70 | + } elseif (3 == $numberOfBytes) { |
| 71 | + $multibyteCode = ($bytes[0] % 16) * 4096 + ($bytes[1] % 64) * 64 + ($bytes[2] % 64); |
| 72 | + } else { |
| 73 | + $multibyteCode = ($bytes[0] % 32) * 64 + ($bytes[1] % 64); |
| 74 | + } |
| 75 | + |
| 76 | + if (65279 != $multibyteCode) { |
| 77 | + $escapedValue .= $multibyteCode < 128 ? $this->escapeAsciiCharacter($multibyteCode) : $this->escapeMultibyteCharacter($multibyteCode); |
| 78 | + } |
| 79 | + |
| 80 | + $numberOfBytes = 1; |
| 81 | + $bytes = array(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return $escapedValue; |
| 87 | + } |
| 88 | +} |
0 commit comments