Document.php 11 KB

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