Document.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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\Reader\RTF;
  18. use PhpOffice\PhpWord\PhpWord;
  19. use PhpOffice\PhpWord\SimpleType\Jc;
  20. /**
  21. * RTF document reader
  22. *
  23. * References:
  24. * - How to Write an RTF Reader http://latex2rtf.sourceforge.net/rtfspec_45.html
  25. * - PHP rtfclass by Markus Fischer https://github.com/mfn/rtfclass
  26. * - JavaScript RTF-parser by LazyGyu https://github.com/lazygyu/RTF-parser
  27. *
  28. * @since 0.11.0
  29. * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
  30. */
  31. class Document
  32. {
  33. /** @const int */
  34. const PARA = 'readParagraph';
  35. const STYL = 'readStyle';
  36. const SKIP = 'readSkip';
  37. /**
  38. * PhpWord object
  39. *
  40. * @var \PhpOffice\PhpWord\PhpWord
  41. */
  42. private $phpWord;
  43. /**
  44. * Section object
  45. *
  46. * @var \PhpOffice\PhpWord\Element\Section
  47. */
  48. private $section;
  49. /**
  50. * Textrun object
  51. *
  52. * @var \PhpOffice\PhpWord\Element\TextRun
  53. */
  54. private $textrun;
  55. /**
  56. * RTF content
  57. *
  58. * @var string
  59. */
  60. public $rtf;
  61. /**
  62. * Content length
  63. *
  64. * @var int
  65. */
  66. private $length = 0;
  67. /**
  68. * Character index
  69. *
  70. * @var int
  71. */
  72. private $offset = 0;
  73. /**
  74. * Current control word
  75. *
  76. * @var string
  77. */
  78. private $control = '';
  79. /**
  80. * Text content
  81. *
  82. * @var string
  83. */
  84. private $text = '';
  85. /**
  86. * Parsing a control word flag
  87. *
  88. * @var bool
  89. */
  90. private $isControl = false;
  91. /**
  92. * First character flag: watch out for control symbols
  93. *
  94. * @var bool
  95. */
  96. private $isFirst = false;
  97. /**
  98. * Group groups
  99. *
  100. * @var array
  101. */
  102. private $groups = array();
  103. /**
  104. * Parser flags; not used
  105. *
  106. * @var array
  107. */
  108. private $flags = array();
  109. /**
  110. * Parse RTF content
  111. *
  112. * - Marks controlling characters `{`, `}`, and `\`
  113. * - Removes line endings
  114. * - Builds control words and control symbols
  115. * - Pushes every other character into the text queue
  116. *
  117. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  118. * @todo Use `fread` stream for scalability
  119. */
  120. public function read(PhpWord $phpWord)
  121. {
  122. $markers = array(
  123. 123 => 'markOpening', // {
  124. 125 => 'markClosing', // }
  125. 92 => 'markBackslash', // \
  126. 10 => 'markNewline', // LF
  127. 13 => 'markNewline', // CR
  128. );
  129. $this->phpWord = $phpWord;
  130. $this->section = $phpWord->addSection();
  131. $this->textrun = $this->section->addTextRun();
  132. $this->length = strlen($this->rtf);
  133. $this->flags['paragraph'] = true; // Set paragraph flag from the beginning
  134. // Walk each characters
  135. while ($this->offset < $this->length) {
  136. $char = $this->rtf[$this->offset];
  137. $ascii = ord($char);
  138. if (isset($markers[$ascii])) { // Marker found: {, }, \, LF, or CR
  139. $markerFunction = $markers[$ascii];
  140. $this->$markerFunction();
  141. } else {
  142. if (false === $this->isControl) { // Non control word: Push character
  143. $this->pushText($char);
  144. } else {
  145. if (preg_match('/^[a-zA-Z0-9-]?$/', $char)) { // No delimiter: Buffer control
  146. $this->control .= $char;
  147. $this->isFirst = false;
  148. } else { // Delimiter found: Parse buffered control
  149. if ($this->isFirst) {
  150. $this->isFirst = false;
  151. } else {
  152. if (' ' == $char) { // Discard space as a control word delimiter
  153. $this->flushControl(true);
  154. }
  155. }
  156. }
  157. }
  158. }
  159. $this->offset++;
  160. }
  161. $this->flushText();
  162. }
  163. /**
  164. * Mark opening braket `{` character.
  165. */
  166. private function markOpening()
  167. {
  168. $this->flush(true);
  169. array_push($this->groups, $this->flags);
  170. }
  171. /**
  172. * Mark closing braket `}` character.
  173. */
  174. private function markClosing()
  175. {
  176. $this->flush(true);
  177. $this->flags = array_pop($this->groups);
  178. }
  179. /**
  180. * Mark backslash `\` character.
  181. */
  182. private function markBackslash()
  183. {
  184. if ($this->isFirst) {
  185. $this->setControl(false);
  186. $this->text .= '\\';
  187. } else {
  188. $this->flush();
  189. $this->setControl(true);
  190. $this->control = '';
  191. }
  192. }
  193. /**
  194. * Mark newline character: Flush control word because it's not possible to span multiline.
  195. */
  196. private function markNewline()
  197. {
  198. if ($this->isControl) {
  199. $this->flushControl(true);
  200. }
  201. }
  202. /**
  203. * Flush control word or text.
  204. *
  205. * @param bool $isControl
  206. */
  207. private function flush($isControl = false)
  208. {
  209. if ($this->isControl) {
  210. $this->flushControl($isControl);
  211. } else {
  212. $this->flushText();
  213. }
  214. }
  215. /**
  216. * Flush control word.
  217. *
  218. * @param bool $isControl
  219. */
  220. private function flushControl($isControl = false)
  221. {
  222. if (1 === preg_match('/^([A-Za-z]+)(-?[0-9]*) ?$/', $this->control, $match)) {
  223. list(, $control, $parameter) = $match;
  224. $this->parseControl($control, $parameter);
  225. }
  226. if (true === $isControl) {
  227. $this->setControl(false);
  228. }
  229. }
  230. /**
  231. * Flush text in queue.
  232. */
  233. private function flushText()
  234. {
  235. if ($this->text != '') {
  236. if (isset($this->flags['property'])) { // Set property
  237. $this->flags['value'] = $this->text;
  238. } else { // Set text
  239. if (true === $this->flags['paragraph']) {
  240. $this->flags['paragraph'] = false;
  241. $this->flags['text'] = $this->text;
  242. }
  243. }
  244. // Add text if it's not flagged as skipped
  245. if (!isset($this->flags['skipped'])) {
  246. $this->readText();
  247. }
  248. $this->text = '';
  249. }
  250. }
  251. /**
  252. * Reset control word and first char state.
  253. *
  254. * @param bool $value
  255. */
  256. private function setControl($value)
  257. {
  258. $this->isControl = $value;
  259. $this->isFirst = $value;
  260. }
  261. /**
  262. * Push text into queue.
  263. *
  264. * @param string $char
  265. */
  266. private function pushText($char)
  267. {
  268. if ('<' == $char) {
  269. $this->text .= '&lt;';
  270. } elseif ('>' == $char) {
  271. $this->text .= '&gt;';
  272. } else {
  273. $this->text .= $char;
  274. }
  275. }
  276. /**
  277. * Parse control.
  278. *
  279. * @param string $control
  280. * @param string $parameter
  281. */
  282. private function parseControl($control, $parameter)
  283. {
  284. $controls = array(
  285. 'par' => array(self::PARA, 'paragraph', true),
  286. 'b' => array(self::STYL, 'font', 'bold', true),
  287. 'i' => array(self::STYL, 'font', 'italic', true),
  288. 'u' => array(self::STYL, 'font', 'underline', true),
  289. 'strike' => array(self::STYL, 'font', 'strikethrough', true),
  290. 'fs' => array(self::STYL, 'font', 'size', $parameter),
  291. 'qc' => array(self::STYL, 'paragraph', 'alignment', Jc::CENTER),
  292. 'sa' => array(self::STYL, 'paragraph', 'spaceAfter', $parameter),
  293. 'fonttbl' => array(self::SKIP, 'fonttbl', null),
  294. 'colortbl' => array(self::SKIP, 'colortbl', null),
  295. 'info' => array(self::SKIP, 'info', null),
  296. 'generator' => array(self::SKIP, 'generator', null),
  297. 'title' => array(self::SKIP, 'title', null),
  298. 'subject' => array(self::SKIP, 'subject', null),
  299. 'category' => array(self::SKIP, 'category', null),
  300. 'keywords' => array(self::SKIP, 'keywords', null),
  301. 'comment' => array(self::SKIP, 'comment', null),
  302. 'shppict' => array(self::SKIP, 'pic', null),
  303. 'fldinst' => array(self::SKIP, 'link', null),
  304. );
  305. if (isset($controls[$control])) {
  306. list($function) = $controls[$control];
  307. if (method_exists($this, $function)) {
  308. $directives = $controls[$control];
  309. array_shift($directives); // remove the function variable; we won't need it
  310. $this->$function($directives);
  311. }
  312. }
  313. }
  314. /**
  315. * Read paragraph.
  316. *
  317. * @param array $directives
  318. */
  319. private function readParagraph($directives)
  320. {
  321. list($property, $value) = $directives;
  322. $this->textrun = $this->section->addTextRun();
  323. $this->flags[$property] = $value;
  324. }
  325. /**
  326. * Read style.
  327. *
  328. * @param array $directives
  329. */
  330. private function readStyle($directives)
  331. {
  332. list($style, $property, $value) = $directives;
  333. $this->flags['styles'][$style][$property] = $value;
  334. }
  335. /**
  336. * Read skip.
  337. *
  338. * @param array $directives
  339. */
  340. private function readSkip($directives)
  341. {
  342. list($property) = $directives;
  343. $this->flags['property'] = $property;
  344. $this->flags['skipped'] = true;
  345. }
  346. /**
  347. * Read text.
  348. */
  349. private function readText()
  350. {
  351. $text = $this->textrun->addText($this->text);
  352. if (isset($this->flags['styles']['font'])) {
  353. $text->getFontStyle()->setStyleByArray($this->flags['styles']['font']);
  354. }
  355. }
  356. }