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;
|
|
|
|
|
|
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;
|
2014-05-09 14:57:06 +07:00
|
|
|
use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter;
|
2014-04-25 18:01:17 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Text element RTF writer
|
|
|
|
|
*
|
|
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
2014-05-09 14:57:06 +07:00
|
|
|
class Text extends AbstractElement
|
2014-04-25 18:01:17 +07:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Write element
|
|
|
|
|
*/
|
|
|
|
|
public function write()
|
|
|
|
|
{
|
2014-05-09 14:57:06 +07:00
|
|
|
$fontStyle = $this->getFontStyle();
|
2014-05-06 23:37:14 +07:00
|
|
|
|
2014-05-09 14:57:06 +07:00
|
|
|
$content = '';
|
|
|
|
|
$content .= $this->writeParagraphStyle();
|
|
|
|
|
$content .= $this->writeFontStyleBegin($fontStyle);
|
|
|
|
|
if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) {
|
|
|
|
|
$content .= ' ';
|
|
|
|
|
}
|
|
|
|
|
$content .= $this->element->getText();
|
|
|
|
|
$content .= $this->writeFontStyleEnd($fontStyle);
|
2014-04-25 18:01:17 +07:00
|
|
|
|
2014-05-09 14:57:06 +07:00
|
|
|
if (!$this->withoutP) {
|
|
|
|
|
$content .= '\par' . PHP_EOL;
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
|
|
|
|
|
2014-05-09 14:57:06 +07:00
|
|
|
return $content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write paragraph style
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function writeParagraphStyle()
|
|
|
|
|
{
|
|
|
|
|
$content = '';
|
|
|
|
|
|
|
|
|
|
// Get paragraph style
|
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-09 14:57:06 +07:00
|
|
|
// Write style when applicable
|
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-09 14:57:06 +07:00
|
|
|
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
|
|
|
|
|
$content = $styleWriter->write();
|
2014-04-25 18:01:17 +07:00
|
|
|
$this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
|
|
|
|
|
} else {
|
|
|
|
|
$this->parentWriter->setLastParagraphStyle();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->parentWriter->setLastParagraphStyle();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 14:57:06 +07:00
|
|
|
return $content;
|
2014-05-06 21:22:24 +07:00
|
|
|
}
|
2014-04-25 18:01:17 +07:00
|
|
|
|
2014-05-07 01:11:56 +07:00
|
|
|
/**
|
2014-05-09 14:57:06 +07:00
|
|
|
* Write font style beginning
|
2014-05-07 01:11:56 +07:00
|
|
|
*
|
2014-05-09 14:57:06 +07:00
|
|
|
* @param \PhpOffice\PhpWord\Style\Font $style
|
2014-05-07 01:11:56 +07:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-05-09 14:57:06 +07:00
|
|
|
private function writeFontStyleBegin($style)
|
2014-05-07 01:11:56 +07:00
|
|
|
{
|
2014-05-09 14:57:06 +07:00
|
|
|
if (!$style instanceof FontStyle) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create style writer and set color/name index
|
|
|
|
|
$styleWriter = new FontStyleWriter($style);
|
|
|
|
|
if ($style->getColor() != null) {
|
|
|
|
|
$colorIndex = array_search($style->getColor(), $this->parentWriter->getColorTable());
|
|
|
|
|
if ($colorIndex !== false) {
|
|
|
|
|
$styleWriter->setColorIndex($colorIndex + 1);
|
|
|
|
|
}
|
2014-05-07 01:11:56 +07:00
|
|
|
}
|
2014-05-09 14:57:06 +07:00
|
|
|
if ($style->getName() != null) {
|
|
|
|
|
$fontIndex = array_search($style->getName(), $this->parentWriter->getFontTable());
|
|
|
|
|
if ($fontIndex !== false) {
|
|
|
|
|
$styleWriter->setNameIndex($fontIndex + 1);
|
2014-05-07 01:11:56 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 14:57:06 +07:00
|
|
|
// Write style
|
|
|
|
|
$content = $styleWriter->write();
|
|
|
|
|
|
|
|
|
|
return $content;
|
2014-05-07 01:11:56 +07:00
|
|
|
}
|
|
|
|
|
|
2014-05-06 21:22:24 +07:00
|
|
|
/**
|
2014-05-09 14:57:06 +07:00
|
|
|
* Write font style ending
|
2014-05-06 21:22:24 +07:00
|
|
|
*
|
2014-05-09 14:57:06 +07:00
|
|
|
* @param \PhpOffice\PhpWord\Style\Font $style
|
2014-05-06 21:22:24 +07:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-05-09 14:57:06 +07:00
|
|
|
private function writeFontStyleEnd($style)
|
2014-05-06 21:22:24 +07:00
|
|
|
{
|
2014-05-09 14:57:06 +07:00
|
|
|
if (!$style instanceof FontStyle) {
|
|
|
|
|
return '';
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
|
|
|
|
|
2014-05-09 14:57:06 +07:00
|
|
|
$styleWriter = new FontStyleWriter($style);
|
|
|
|
|
$content = $styleWriter->writeEnd();
|
|
|
|
|
|
|
|
|
|
return $content;
|
2014-05-06 21:22:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-09 14:57:06 +07:00
|
|
|
* Get font style
|
2014-05-06 21:22:24 +07:00
|
|
|
*
|
2014-05-09 14:57:06 +07:00
|
|
|
* @return \PhpOffice\PhpWord\Style\Font
|
2014-05-06 21:22:24 +07:00
|
|
|
*/
|
2014-05-09 14:57:06 +07:00
|
|
|
private function getFontStyle()
|
2014-05-06 21:22:24 +07:00
|
|
|
{
|
2014-05-09 14:57:06 +07:00
|
|
|
$fontStyle = $this->element->getFontStyle();
|
|
|
|
|
if (is_string($fontStyle)) {
|
|
|
|
|
$fontStyle = Style::getStyle($fontStyle);
|
2014-05-06 21:22:24 +07:00
|
|
|
}
|
|
|
|
|
|
2014-05-09 14:57:06 +07:00
|
|
|
return $fontStyle;
|
2014-04-25 18:01:17 +07:00
|
|
|
}
|
|
|
|
|
}
|