2014-04-25 18:01:17 +07:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
* word processing documents.
|
|
|
|
|
*
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2014-04-25 18:01:17 +07:00
|
|
|
*
|
|
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-04-25 18:01:17 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
|
|
|
|
|
|
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2014-05-05 13:06:53 +04:00
|
|
|
use PhpOffice\PhpWord\Style;
|
2014-05-07 01:11:56 +07:00
|
|
|
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
|
|
|
|
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
2014-04-25 18:01:17 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Text element RTF writer
|
|
|
|
|
*
|
|
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
|
|
|
|
class Text extends Element
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Write element
|
|
|
|
|
*/
|
|
|
|
|
public function write()
|
|
|
|
|
{
|
2014-05-06 23:37:14 +07:00
|
|
|
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 18:01:17 +07:00
|
|
|
$rtfText = '';
|
|
|
|
|
|
2014-05-01 14:37:58 +07:00
|
|
|
$fontStyle = $this->element->getFontStyle();
|
|
|
|
|
if (is_string($fontStyle)) {
|
|
|
|
|
$fontStyle = Style::getStyle($fontStyle);
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
|
|
|
|
|
2014-05-01 14:37:58 +07:00
|
|
|
$paragraphStyle = $this->element->getParagraphStyle();
|
|
|
|
|
if (is_string($paragraphStyle)) {
|
|
|
|
|
$paragraphStyle = Style::getStyle($paragraphStyle);
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
|
|
|
|
|
2014-05-01 14:37:58 +07:00
|
|
|
if ($paragraphStyle && !$this->withoutP) {
|
2014-04-25 18:01:17 +07:00
|
|
|
if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
|
2014-05-07 01:11:56 +07:00
|
|
|
$rtfText .= $this->writeParagraphStyle($paragraphStyle);
|
2014-04-25 18:01:17 +07:00
|
|
|
$this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
|
|
|
|
|
} else {
|
|
|
|
|
$this->parentWriter->setLastParagraphStyle();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->parentWriter->setLastParagraphStyle();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 21:22:24 +07:00
|
|
|
if ($fontStyle instanceof FontStyle) {
|
|
|
|
|
$rtfText .= $this->writeFontStyleBegin($fontStyle);
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
2014-05-01 14:37:58 +07:00
|
|
|
if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) {
|
2014-04-25 18:01:17 +07:00
|
|
|
$rtfText .= ' ';
|
|
|
|
|
}
|
|
|
|
|
$rtfText .= $this->element->getText();
|
2014-05-06 21:22:24 +07:00
|
|
|
if ($fontStyle instanceof FontStyle) {
|
|
|
|
|
$rtfText .= $this->writeFontStyleEnd($fontStyle);
|
|
|
|
|
}
|
|
|
|
|
if (!$this->withoutP) {
|
|
|
|
|
$rtfText .= '\par' . PHP_EOL;
|
|
|
|
|
}
|
2014-04-25 18:01:17 +07:00
|
|
|
|
2014-05-06 21:22:24 +07:00
|
|
|
return $rtfText;
|
|
|
|
|
}
|
2014-04-25 18:01:17 +07:00
|
|
|
|
2014-05-07 01:11:56 +07:00
|
|
|
/**
|
|
|
|
|
* Write paragraph style
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function writeParagraphStyle(ParagraphStyle $paragraphStyle)
|
|
|
|
|
{
|
|
|
|
|
$rtfText = '\pard\nowidctlpar';
|
|
|
|
|
if ($paragraphStyle->getSpaceAfter() != null) {
|
|
|
|
|
$rtfText .= '\sa' . $paragraphStyle->getSpaceAfter();
|
|
|
|
|
}
|
|
|
|
|
if ($paragraphStyle->getAlign() != null) {
|
|
|
|
|
if ($paragraphStyle->getAlign() == 'center') {
|
|
|
|
|
$rtfText .= '\qc';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $rtfText;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 21:22:24 +07:00
|
|
|
/**
|
|
|
|
|
* Write font style beginning
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function writeFontStyleBegin(FontStyle $style)
|
|
|
|
|
{
|
|
|
|
|
$rtfText = '';
|
|
|
|
|
if ($style->getColor() != null) {
|
|
|
|
|
$idxColor = array_search($style->getColor(), $this->parentWriter->getColorTable());
|
|
|
|
|
if ($idxColor !== false) {
|
|
|
|
|
$rtfText .= '\cf' . ($idxColor + 1);
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
2014-05-06 21:22:24 +07:00
|
|
|
} else {
|
|
|
|
|
$rtfText .= '\cf0';
|
|
|
|
|
}
|
|
|
|
|
if ($style->getName() != null) {
|
|
|
|
|
$idxFont = array_search($style->getName(), $this->parentWriter->getFontTable());
|
|
|
|
|
if ($idxFont !== false) {
|
|
|
|
|
$rtfText .= '\f' . $idxFont;
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
2014-05-06 21:22:24 +07:00
|
|
|
} else {
|
|
|
|
|
$rtfText .= '\f0';
|
|
|
|
|
}
|
|
|
|
|
if ($style->isBold()) {
|
|
|
|
|
$rtfText .= '\b';
|
|
|
|
|
}
|
|
|
|
|
if ($style->isItalic()) {
|
|
|
|
|
$rtfText .= '\i';
|
|
|
|
|
}
|
|
|
|
|
if ($style->getSize()) {
|
|
|
|
|
$rtfText .= '\fs' . ($style->getSize() * 2);
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
|
|
|
|
|
2014-05-06 21:22:24 +07:00
|
|
|
return $rtfText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write font style ending
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function writeFontStyleEnd(FontStyle $style)
|
|
|
|
|
{
|
|
|
|
|
$rtfText = '';
|
|
|
|
|
$rtfText .= '\cf0';
|
|
|
|
|
$rtfText .= '\f0';
|
|
|
|
|
|
|
|
|
|
if ($style->isBold()) {
|
|
|
|
|
$rtfText .= '\b0';
|
|
|
|
|
}
|
|
|
|
|
if ($style->isItalic()) {
|
|
|
|
|
$rtfText .= '\i0';
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
2014-05-06 21:22:24 +07:00
|
|
|
if ($style->getSize()) {
|
|
|
|
|
$rtfText .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 18:01:17 +07:00
|
|
|
return $rtfText;
|
|
|
|
|
}
|
|
|
|
|
}
|