Rtf.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Escaper;
  18. /**
  19. * @since 0.13.0
  20. *
  21. * @codeCoverageIgnore
  22. */
  23. class Rtf extends AbstractEscaper
  24. {
  25. protected function escapeAsciiCharacter($code)
  26. {
  27. if ($code == 9) {
  28. return '{\\tab}';
  29. }
  30. if (0x20 > $code || $code >= 0x80) {
  31. return '{\\u' . $code . '}';
  32. }
  33. if ($code == 123 || $code == 125 || $code == 92) { // open or close brace or backslash
  34. return '\\' . chr($code);
  35. }
  36. return chr($code);
  37. }
  38. protected function escapeMultibyteCharacter($code)
  39. {
  40. return '\\uc0{\\u' . $code . '}';
  41. }
  42. /**
  43. * @see http://www.randomchaos.com/documents/?source=php_and_unicode
  44. * @param string $input
  45. */
  46. protected function escapeSingleValue($input)
  47. {
  48. $escapedValue = '';
  49. $numberOfBytes = 1;
  50. $bytes = array();
  51. for ($i = 0; $i < strlen($input); ++$i) {
  52. $character = $input[$i];
  53. $asciiCode = ord($character);
  54. if ($asciiCode < 128) {
  55. $escapedValue .= $this->escapeAsciiCharacter($asciiCode);
  56. } else {
  57. if (0 == count($bytes)) {
  58. if ($asciiCode < 224) {
  59. $numberOfBytes = 2;
  60. } elseif ($asciiCode < 240) {
  61. $numberOfBytes = 3;
  62. } elseif ($asciiCode < 248) {
  63. $numberOfBytes = 4;
  64. }
  65. }
  66. $bytes[] = $asciiCode;
  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. if (65279 != $multibyteCode) {
  76. $escapedValue .= $multibyteCode < 128 ? $this->escapeAsciiCharacter($multibyteCode) : $this->escapeMultibyteCharacter($multibyteCode);
  77. }
  78. $numberOfBytes = 1;
  79. $bytes = array();
  80. }
  81. }
  82. }
  83. return $escapedValue;
  84. }
  85. }