Writer: Style writers

This commit is contained in:
Ivan Lanin 2014-04-26 11:14:27 +07:00
parent ec9d90017f
commit 7863e397c4
32 changed files with 994 additions and 625 deletions

View File

@ -9,9 +9,6 @@
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\TOC as TOCStyle;
/**
* Table of contents
*/

View File

@ -17,7 +17,9 @@ use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Writer\HTML\Element\Element as ElementWriter;
use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
/**
* HTML writer
@ -212,26 +214,26 @@ class HTML extends AbstractWriter implements WriterInterface
),
);
foreach ($defaultStyles as $selector => $style) {
$styleWriter = new StyleWriter();
$css .= $selector . ' ';
$css .= $styleWriter->assembleCss($style, true) . PHP_EOL;
$styleWriter = new GenericStyleWriter($style);
$css .= $selector . ' {' . $styleWriter->write() . '}' . PHP_EOL;
}
// Custom styles
$customStyles = Style::getStyles();
if (is_array($customStyles)) {
foreach ($customStyles as $name => $style) {
$styleWriter = new StyleWriter($this, $style, true);
if ($style instanceof Font) {
$styleWriter = new FontStyleWriter($style);
if ($style->getStyleType() == 'title') {
$name = str_replace('Heading_', 'h', $name);
} else {
$name = '.' . $name;
}
$css .= "{$name} " . $styleWriter->write() . PHP_EOL;
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
} elseif ($style instanceof Paragraph) {
$styleWriter = new ParagraphStyleWriter($style);
$name = '.' . $name;
$css .= "{$name} " . $styleWriter->write() . PHP_EOL;
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
}
}
}

View File

@ -10,7 +10,7 @@
namespace PhpOffice\PhpWord\Writer\HTML\Element;
use PhpOffice\PhpWord\Element\Image as ImageElement;
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Image as ImageStyleWriter;
/**
* Image element HTML writer
@ -33,11 +33,9 @@ class Image extends Element
if (!$this->parentWriter->isPdf()) {
$imageData = $this->getBase64ImageData($this->element);
if (!is_null($imageData)) {
$styleWriter = new StyleWriter();
$style = $styleWriter->assembleCss(array(
'width' => $this->element->getStyle()->getWidth() . 'px',
'height' => $this->element->getStyle()->getHeight() . 'px',
));
$styleWriter = new ImageStyleWriter($this->element->getStyle());
$style = $styleWriter->write();
$html = "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
if (!$this->withoutP) {
$html = "<p>{$html}</p>" . PHP_EOL;

View File

@ -24,7 +24,7 @@ class ListItem extends Element
public function write()
{
$text = htmlspecialchars($this->element->getTextObject()->getText());
$html = '<p>' . $text . '</{$p}>' . PHP_EOL;
$html = '<p>' . $text . '</p>' . PHP_EOL;
return $html;
}

View File

@ -11,7 +11,8 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
/**
* Text element HTML writer
@ -32,14 +33,14 @@ class Text extends Element
$pStyle = $this->element->getParagraphStyle();
$pStyleIsObject = ($pStyle instanceof Paragraph);
if ($pStyleIsObject) {
$styleWriter = new StyleWriter($this->parentWriter, $pStyle);
$styleWriter = new ParagraphStyleWriter($pStyle);
$pStyle = $styleWriter->write();
}
// Font style
$fStyle = $this->element->getFontStyle();
$fStyleIsObject = ($fStyle instanceof Font);
if ($fStyleIsObject) {
$styleWriter = new StyleWriter($this->parentWriter, $fStyle);
$styleWriter = new FontStyleWriter($fStyle);
$fStyle = $styleWriter->write();
}

View File

@ -10,7 +10,7 @@
namespace PhpOffice\PhpWord\Writer\HTML\Element;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
/**
* TextRun element HTML writer
@ -33,7 +33,7 @@ class TextRun extends Element
$pStyle = $this->element->getParagraphStyle();
$pStyleIsObject = ($pStyle instanceof Paragraph);
if ($pStyleIsObject) {
$styleWriter = new StyleWriter($this->parentWriter, $pStyle);
$styleWriter = new ParagraphStyleWriter($pStyle);
$pStyle = $styleWriter->write();
}
$tag = $this->withoutP ? 'span' : 'p';

View File

@ -0,0 +1,62 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\HTML\Style;
/**
* Style writer
*
* @since 0.10.0
*/
abstract class AbstractStyle
{
/**
* Style
*
* @var array|\PhpOffice\PhpWord\Style\AbstractStyle
*/
protected $style;
/**
* Write style
*/
abstract public function write();
/**
* Create new instance
*
* @param array|\PhpOffice\PhpWord\Style\AbstractStyle $style
*/
public function __construct($style = null)
{
$this->style = $style;
}
/**
* Takes array where of CSS properties / values and converts to CSS string
*
* @param array $css
* @return string
*/
protected function assembleCss($css)
{
$pairs = array();
$string = '';
foreach ($css as $key => $value) {
if ($value != '') {
$pairs[] = $key . ': ' . $value;
}
}
if (!empty($pairs)) {
$string = implode('; ', $pairs) . ';';
}
return $string;
}
}

View File

@ -10,13 +10,14 @@
namespace PhpOffice\PhpWord\Writer\HTML\Style;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Font as FontStyle;
/**
* Font style HTML writer
*
* @since 0.10.0
*/
class Font extends Style
class Font extends AbstractStyle
{
/**
* Write style
@ -48,13 +49,13 @@ class Font extends Style
$css['vertical-align'] = 'sub';
}
$css['text-decoration'] = '';
if ($this->style->getUnderline() != \PhpOffice\PhpWord\Style\Font::UNDERLINE_NONE) {
if ($this->style->getUnderline() != FontStyle::UNDERLINE_NONE) {
$css['text-decoration'] .= 'underline ';
}
if ($this->style->getStrikethrough()) {
$css['text-decoration'] .= 'line-through ';
}
return $this->assembleCss($css, $this->curlyBracket);
return $this->assembleCss($css);
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\HTML\Style;
/**
* Generic style writer
*
* @since 0.10.0
*/
class Generic extends AbstractStyle
{
/**
* Write style
*
* @return string
*/
public function write()
{
$css = array();
if (is_array($this->style) && !empty($this->style)) {
$css = $this->style;
}
return $this->assembleCss($css);
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\HTML\Style;
/**
* Paragraph style HTML writer
*
* @since 0.10.0
*/
class Image extends AbstractStyle
{
/**
* Write style
*
* @return string
*/
public function write()
{
$css = array();
if ($this->style->getWidth()) {
$css['width'] = $this->style->getWidth() . 'px';
}
if ($this->style->getWidth()) {
$css['height'] = $this->style->getHeight() . 'px';
}
return $this->assembleCss($css);
}
}

View File

@ -14,7 +14,7 @@ namespace PhpOffice\PhpWord\Writer\HTML\Style;
*
* @since 0.10.0
*/
class Paragraph extends Style
class Paragraph extends AbstractStyle
{
/**
* Write style
@ -28,6 +28,6 @@ class Paragraph extends Style
$css['text-align'] = $this->style->getAlign();
}
return $this->assembleCss($css, $this->curlyBracket);
return $this->assembleCss($css);
}
}

View File

@ -1,95 +0,0 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\HTML\Style;
use PhpOffice\PhpWord\Style\AbstractStyle;
use PhpOffice\PhpWord\Writer\HTML;
/**
* Style HTML writer
*
* @since 0.10.0
*/
class Style
{
/**
* Parent writer
*
* @var \PhpOffice\PhpWord\Writer\HTML
*/
protected $parentWriter;
/**
* Style
*
* @var \PhpOffice\PhpWord\Style\AbstractStyle
*/
protected $style;
/**
* Curly bracket
*
* @var bool
*/
protected $curlyBracket = false;
/**
* Create new instance
*
* @param bool $curlyBracket
*/
public function __construct(HTML $parentWriter = null, AbstractStyle $style = null, $curlyBracket = false)
{
$this->parentWriter = $parentWriter;
$this->style = $style;
$this->curlyBracket = $curlyBracket;
}
/**
* Write element
*
* @return string
*/
public function write()
{
$css = '';
$styleType = str_replace('PhpOffice\\PhpWord\\Style\\', '', get_class($this->style));
$styleWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Style\\' . $styleType;
if (class_exists($styleWriterClass) === true) {
$styleWriter = new $styleWriterClass($this->parentWriter, $this->style, $this->curlyBracket);
$css = $styleWriter->write();
}
return $css;
}
/**
* Takes array where of CSS properties / values and converts to CSS string
*
* @param array $css
* @param boolean $curlyBracket
* @return string
*/
public function assembleCss($css, $curlyBracket = false)
{
$pairs = array();
foreach ($css as $key => $value) {
if ($value != '') {
$pairs[] = $key . ': ' . $value;
}
}
$string = implode('; ', $pairs);
if ($curlyBracket) {
$string = '{ ' . $string . ' }';
}
return $string;
}
}

View File

@ -14,10 +14,8 @@ use PhpOffice\PhpWord\Element\AbstractElement;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Cell;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style\Table as TableStyle;
use PhpOffice\PhpWord\Writer\Word2007\Element\Element as ElementWriter;
/**
@ -27,439 +25,6 @@ use PhpOffice\PhpWord\Writer\Word2007\Element\Element as ElementWriter;
*/
class Base extends AbstractWriterPart
{
/**
* Write paragraph style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Style\Paragraph $style
* @param bool $withoutPPR
*/
public function writeParagraphStyle(XMLWriter $xmlWriter, Paragraph $style, $withoutPPR = false)
{
$align = $style->getAlign();
$spacing = $style->getSpacing();
$spaceBefore = $style->getSpaceBefore();
$spaceAfter = $style->getSpaceAfter();
$indent = $style->getIndent();
$hanging = $style->getHanging();
$tabs = $style->getTabs();
$widowControl = $style->getWidowControl();
$keepNext = $style->getKeepNext();
$keepLines = $style->getKeepLines();
$pageBreakBefore = $style->getPageBreakBefore();
if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) ||
!is_null($spaceAfter) || !is_null($indent) || !is_null($hanging) ||
!is_null($tabs) || !is_null($widowControl) || !is_null($keepNext) ||
!is_null($keepLines) || !is_null($pageBreakBefore)) {
if (!$withoutPPR) {
$xmlWriter->startElement('w:pPr');
}
// Alignment
if (!is_null($align)) {
$xmlWriter->startElement('w:jc');
$xmlWriter->writeAttribute('w:val', $align);
$xmlWriter->endElement();
}
// Indentation
if (!is_null($indent) || !is_null($hanging)) {
$xmlWriter->startElement('w:ind');
$xmlWriter->writeAttribute('w:firstLine', 0);
if (!is_null($indent)) {
$xmlWriter->writeAttribute('w:left', $indent);
}
if (!is_null($hanging)) {
$xmlWriter->writeAttribute('w:hanging', $hanging);
}
$xmlWriter->endElement();
}
// Spacing
if (!is_null($spaceBefore) || !is_null($spaceAfter) ||
!is_null($spacing)) {
$xmlWriter->startElement('w:spacing');
if (!is_null($spaceBefore)) {
$xmlWriter->writeAttribute('w:before', $spaceBefore);
}
if (!is_null($spaceAfter)) {
$xmlWriter->writeAttribute('w:after', $spaceAfter);
}
if (!is_null($spacing)) {
$xmlWriter->writeAttribute('w:line', $spacing);
$xmlWriter->writeAttribute('w:lineRule', 'auto');
}
$xmlWriter->endElement();
}
// Pagination
if (!$widowControl) {
$xmlWriter->startElement('w:widowControl');
$xmlWriter->writeAttribute('w:val', '0');
$xmlWriter->endElement();
}
if ($keepNext) {
$xmlWriter->startElement('w:keepNext');
$xmlWriter->writeAttribute('w:val', '1');
$xmlWriter->endElement();
}
if ($keepLines) {
$xmlWriter->startElement('w:keepLines');
$xmlWriter->writeAttribute('w:val', '1');
$xmlWriter->endElement();
}
if ($pageBreakBefore) {
$xmlWriter->startElement('w:pageBreakBefore');
$xmlWriter->writeAttribute('w:val', '1');
$xmlWriter->endElement();
}
// Tabs
if (!empty($tabs)) {
$xmlWriter->startElement("w:tabs");
foreach ($tabs as $tab) {
$xmlWriter->startElement("w:tab");
$xmlWriter->writeAttribute("w:val", $tab->getStopType());
if (!is_null($tab->getLeader())) {
$xmlWriter->writeAttribute("w:leader", $tab->getLeader());
}
$xmlWriter->writeAttribute("w:pos", $tab->getPosition());
$xmlWriter->endElement();
}
$xmlWriter->endElement();
}
if (!$withoutPPR) {
$xmlWriter->endElement(); // w:pPr
}
}
}
/**
* Write font style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Style\Font $style
*/
public function writeFontStyle(XMLWriter $xmlWriter, Font $style)
{
$font = $style->getName();
$bold = $style->getBold();
$italic = $style->getItalic();
$color = $style->getColor();
$size = $style->getSize();
$fgColor = $style->getFgColor();
$bgColor = $style->getBgColor();
$strikethrough = $style->getStrikethrough();
$underline = $style->getUnderline();
$superscript = $style->getSuperScript();
$subscript = $style->getSubScript();
$hint = $style->getHint();
$xmlWriter->startElement('w:rPr');
// Font
if ($font != PhpWord::DEFAULT_FONT_NAME) {
$xmlWriter->startElement('w:rFonts');
$xmlWriter->writeAttribute('w:ascii', $font);
$xmlWriter->writeAttribute('w:hAnsi', $font);
$xmlWriter->writeAttribute('w:eastAsia', $font);
$xmlWriter->writeAttribute('w:cs', $font);
//Font Content Type
if ($hint != PhpWord::DEFAULT_FONT_CONTENT_TYPE) {
$xmlWriter->writeAttribute('w:hint', $hint);
}
$xmlWriter->endElement();
}
// Color
if ($color != PhpWord::DEFAULT_FONT_COLOR) {
$xmlWriter->startElement('w:color');
$xmlWriter->writeAttribute('w:val', $color);
$xmlWriter->endElement();
}
// Size
if ($size != PhpWord::DEFAULT_FONT_SIZE) {
$xmlWriter->startElement('w:sz');
$xmlWriter->writeAttribute('w:val', $size * 2);
$xmlWriter->endElement();
$xmlWriter->startElement('w:szCs');
$xmlWriter->writeAttribute('w:val', $size * 2);
$xmlWriter->endElement();
}
// Bold
if ($bold) {
$xmlWriter->writeElement('w:b', null);
}
// Italic
if ($italic) {
$xmlWriter->writeElement('w:i', null);
$xmlWriter->writeElement('w:iCs', null);
}
// Underline
if (!is_null($underline) && $underline != 'none') {
$xmlWriter->startElement('w:u');
$xmlWriter->writeAttribute('w:val', $underline);
$xmlWriter->endElement();
}
// Strikethrough
if ($strikethrough) {
$xmlWriter->writeElement('w:strike', null);
}
// Foreground-Color
if (!is_null($fgColor)) {
$xmlWriter->startElement('w:highlight');
$xmlWriter->writeAttribute('w:val', $fgColor);
$xmlWriter->endElement();
}
// Background-Color
if (!is_null($bgColor)) {
$xmlWriter->startElement('w:shd');
$xmlWriter->writeAttribute('w:val', "clear");
$xmlWriter->writeAttribute('w:color', "auto");
$xmlWriter->writeAttribute('w:fill', $bgColor);
$xmlWriter->endElement();
}
// Superscript/subscript
if ($superscript || $subscript) {
$xmlWriter->startElement('w:vertAlign');
$xmlWriter->writeAttribute('w:val', $superscript ? 'superscript' : 'subscript');
$xmlWriter->endElement();
}
$xmlWriter->endElement();
}
/**
* Write table style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Style\Table $style
* @param boolean $isFullStyle
*/
public function writeTableStyle(XMLWriter $xmlWriter, TableStyle $style, $isFullStyle = true)
{
$bgColor = $style->getBgColor();
$brdCol = $style->getBorderColor();
$brdSz = $style->getBorderSize();
$cellMargin = $style->getCellMargin();
// If any of the borders/margins is set, process them
$hasBorders = false;
for ($i = 0; $i < 6; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
$hasMargins = false;
for ($i = 0; $i < 4; $i++) {
if (!is_null($cellMargin[$i])) {
$hasMargins = true;
break;
}
}
if ($hasMargins || $hasBorders) {
$xmlWriter->startElement('w:tblPr');
if ($hasMargins) {
$xmlWriter->startElement('w:tblCellMar');
$this->writeMarginBorder($xmlWriter, $cellMargin);
$xmlWriter->endElement(); // w:tblCellMar
}
if ($hasBorders) {
$xmlWriter->startElement('w:tblBorders');
$this->writeMarginBorder($xmlWriter, $brdSz, $brdCol);
$xmlWriter->endElement(); // w:tblBorders
}
$xmlWriter->endElement(); // w:tblPr
}
// Only write background color and first row for full style
if ($isFullStyle) {
// Background color
if (!is_null($bgColor)) {
$xmlWriter->startElement('w:tcPr');
$xmlWriter->startElement('w:shd');
$xmlWriter->writeAttribute('w:val', 'clear');
$xmlWriter->writeAttribute('w:color', 'auto');
$xmlWriter->writeAttribute('w:fill', $bgColor);
$xmlWriter->endElement();
$xmlWriter->endElement();
}
// First Row
$firstRow = $style->getFirstRow();
if (!is_null($firstRow)) {
$this->writeRowStyle($xmlWriter, 'firstRow', $firstRow);
}
}
}
/**
* Write row style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param string $type
* @param \PhpOffice\PhpWord\Style\Table $style
*/
public function writeRowStyle(XMLWriter $xmlWriter, $type, TableStyle $style)
{
$bgColor = $style->getBgColor();
$xmlWriter->startElement('w:tblStylePr');
$xmlWriter->writeAttribute('w:type', $type);
$xmlWriter->startElement('w:tcPr');
if (!is_null($bgColor)) {
$xmlWriter->startElement('w:shd');
$xmlWriter->writeAttribute('w:val', 'clear');
$xmlWriter->writeAttribute('w:color', 'auto');
$xmlWriter->writeAttribute('w:fill', $bgColor);
$xmlWriter->endElement(); // w:shd
}
// Borders
$brdSz = $style->getBorderSize();
$brdCol = $style->getBorderColor();
$hasBorders = false;
for ($i = 0; $i < 6; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
if ($hasBorders) {
$xmlWriter->startElement('w:tcBorders');
$this->writeMarginBorder($xmlWriter, $brdSz, $brdCol);
$xmlWriter->endElement(); // w:tcBorders
}
$xmlWriter->endElement(); // w:tcPr
$xmlWriter->endElement(); // w:tblStylePr
}
/**
* Write footnote reference element
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Style\Cell $style
*/
public function writeCellStyle(XMLWriter $xmlWriter, Cell $style)
{
$bgColor = $style->getBgColor();
$valign = $style->getVAlign();
$textDir = $style->getTextDirection();
$brdSz = $style->getBorderSize();
$brdCol = $style->getBorderColor();
$hasBorders = false;
for ($i = 0; $i < 4; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
$styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $hasBorders) ? true : false;
if ($styles) {
if (!is_null($textDir)) {
$xmlWriter->startElement('w:textDirection');
$xmlWriter->writeAttribute('w:val', $textDir);
$xmlWriter->endElement();
}
if (!is_null($bgColor)) {
$xmlWriter->startElement('w:shd');
$xmlWriter->writeAttribute('w:val', 'clear');
$xmlWriter->writeAttribute('w:color', 'auto');
$xmlWriter->writeAttribute('w:fill', $bgColor);
$xmlWriter->endElement();
}
if (!is_null($valign)) {
$xmlWriter->startElement('w:vAlign');
$xmlWriter->writeAttribute('w:val', $valign);
$xmlWriter->endElement();
}
if ($hasBorders) {
$defaultColor = $style->getDefaultBorderColor();
$xmlWriter->startElement('w:tcBorders');
$this->writeMarginBorder($xmlWriter, $brdSz, $brdCol, array('defaultColor' => $defaultColor));
$xmlWriter->endElement();
}
}
$gridSpan = $style->getGridSpan();
if (!is_null($gridSpan)) {
$xmlWriter->startElement('w:gridSpan');
$xmlWriter->writeAttribute('w:val', $gridSpan);
$xmlWriter->endElement();
}
$vMerge = $style->getVMerge();
if (!is_null($vMerge)) {
$xmlWriter->startElement('w:vMerge');
$xmlWriter->writeAttribute('w:val', $vMerge);
$xmlWriter->endElement();
}
}
/**
* Write inline paragraph style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Style\Paragraph|string $styleParagraph
* @param boolean $withoutPPR
*/
public function writeInlineParagraphStyle(XMLWriter $xmlWriter, $styleParagraph = null, $withoutPPR = false)
{
if ($styleParagraph instanceof Paragraph) {
$this->writeParagraphStyle($xmlWriter, $styleParagraph, $withoutPPR);
} else {
if (!is_null($styleParagraph)) {
if (!$withoutPPR) {
$xmlWriter->startElement('w:pPr');
}
$xmlWriter->startElement('w:pStyle');
$xmlWriter->writeAttribute('w:val', $styleParagraph);
$xmlWriter->endElement();
if (!$withoutPPR) {
$xmlWriter->endElement();
}
}
}
}
/**
* Write inline font style
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Style\Font|string $styleFont
*/
public function writeInlineFontStyle(XMLWriter $xmlWriter, $styleFont = null)
{
if ($styleFont instanceof Font) {
$this->writeFontStyle($xmlWriter, $styleFont);
} else {
if (!is_null($styleFont)) {
$xmlWriter->startElement('w:rPr');
$xmlWriter->startElement('w:rStyle');
$xmlWriter->writeAttribute('w:val', $styleFont);
$xmlWriter->endElement();
$xmlWriter->endElement();
}
}
}
/**
* Write container elements
*
@ -503,42 +68,4 @@ class Base extends AbstractWriterPart
}
}
}
/**
* Write margin or border
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param array $sizes
* @param array $colors
* @param array $attributes
*/
protected function writeMarginBorder(XMLWriter $xmlWriter, $sizes, $colors = array(), $attributes = array())
{
$sides = array('top', 'left', 'right', 'bottom', 'insideH', 'insideV');
$sizeCount = count($sizes) - 1;
for ($i = 0; $i < $sizeCount; $i++) {
if (!is_null($sizes[$i])) {
$xmlWriter->startElement('w:' . $sides[$i]);
if (!empty($colors)) {
if (is_null($colors[$i]) && !empty($attributes)) {
if (array_key_exists('defaultColor', $attributes)) {
$colors[$i] = $attributes['defaultColor'];
}
}
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $sizes[$i]);
$xmlWriter->writeAttribute('w:color', $colors[$i]);
if (!empty($attributes)) {
if (array_key_exists('space', $attributes)) {
$xmlWriter->writeAttribute('w:space', '24');
}
}
} else {
$xmlWriter->writeAttribute('w:w', $sizes[$i]);
$xmlWriter->writeAttribute('w:type', 'dxa');
}
$xmlWriter->endElement();
}
}
}
}

View File

@ -178,9 +178,14 @@ class Document extends Base
}
if ($hasBorders) {
$borderColor = $settings->getBorderColor();
$mbWriter = new \PhpOffice\PhpWord\Writer\Word2007\Style\MarginBorder($xmlWriter);
$mbWriter->setSizes($borders);
$mbWriter->setColors($borderColor);
$mbWriter->setAttributes(array('space' => '24'));
$xmlWriter->startElement('w:pgBorders');
$xmlWriter->writeAttribute('w:offsetFrom', 'page');
$this->writeMarginBorder($xmlWriter, $borders, $borderColor, array('space' => '24'));
$mbWriter->write();
$xmlWriter->endElement();
}

View File

@ -10,6 +10,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* CheckBox element writer
@ -31,8 +33,11 @@ class CheckBox extends Element
$pStyle = $this->element->getParagraphStyle();
if (!$this->withoutP) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$this->parentWriter->writeInlineParagraphStyle($this->xmlWriter, $pStyle);
$styleWriter->write();
}
$this->xmlWriter->startElement('w:r');
@ -73,8 +78,11 @@ class CheckBox extends Element
$this->xmlWriter->endElement();// w:fldChar
$this->xmlWriter->endElement(); // w:r
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r');
$this->parentWriter->writeInlineFontStyle($this->xmlWriter, $fStyle);
$styleWriter->write();
$this->xmlWriter->startElement('w:t');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($text);

View File

@ -9,7 +9,6 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Element\Image as ImageElement;
use PhpOffice\PhpWord\Style\Image as ImageStyle;
/**

View File

@ -9,6 +9,9 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* Link element writer
*
@ -30,14 +33,21 @@ class Link extends Element
$pStyle = $this->element->getParagraphStyle();
if (!$this->withoutP) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$this->parentWriter->writeInlineParagraphStyle($this->xmlWriter, $pStyle);
$styleWriter->write();
}
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:hyperlink');
$this->xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$this->xmlWriter->writeAttribute('w:history', '1');
$this->xmlWriter->startElement('w:r');
$this->parentWriter->writeInlineFontStyle($this->xmlWriter, $fStyle);
$styleWriter->write();
$this->xmlWriter->startElement('w:t');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($linkName);

View File

@ -10,6 +10,7 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Writer\Word2007\Element\Element as ElementWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* ListItem element writer
@ -27,11 +28,14 @@ class ListItem extends Element
$depth = $this->element->getDepth();
$numId = $this->element->getStyle()->getNumId();
$pStyle = $textObject->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter->setWithoutPPR(true);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$this->xmlWriter->startElement('w:pPr');
$this->parentWriter->writeInlineParagraphStyle($this->xmlWriter, $pStyle, true);
$styleWriter->write();
$this->xmlWriter->startElement('w:numPr');
$this->xmlWriter->startElement('w:ilvl');
$this->xmlWriter->writeAttribute('w:val', $depth);

View File

@ -10,6 +10,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* PreserveText element writer
@ -30,11 +32,17 @@ class PreserveText extends Element
$texts = array($texts);
}
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$this->parentWriter->writeInlineParagraphStyle($this->xmlWriter, $pStyle);
$styleWriter->write();
foreach ($texts as $text) {
if (substr($text, 0, 1) == '{') {
$text = substr($text, 1, -1);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:fldChar');
@ -43,7 +51,7 @@ class PreserveText extends Element
$this->xmlWriter->endElement();
$this->xmlWriter->startElement('w:r');
$this->parentWriter->writeInlineFontStyle($this->xmlWriter, $fStyle);
$styleWriter->write();
$this->xmlWriter->startElement('w:instrText');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($text);
@ -64,9 +72,11 @@ class PreserveText extends Element
} else {
$text = htmlspecialchars($text);
$text = String::controlCharacterPHP2OOXML($text);
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r');
$this->parentWriter->writeInlineFontStyle($this->xmlWriter, $fStyle);
$styleWriter->write();
$this->xmlWriter->startElement('w:t');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($text);

View File

@ -10,6 +10,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* TOC element writer
@ -45,7 +47,8 @@ class TOC extends Element
$this->xmlWriter->startElement('w:pPr');
if ($isObject && !is_null($styleFont->getParagraphStyle())) {
$this->parentWriter->writeParagraphStyle($this->xmlWriter, $styleFont->getParagraphStyle());
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $styleFont->getParagraphStyle());
$styleWriter->write();
}
if ($indent > 0) {
@ -103,7 +106,8 @@ class TOC extends Element
$this->xmlWriter->startElement('w:r');
if ($isObject) {
$this->parentWriter->writeFontStyle($this->xmlWriter, $styleFont);
$styleWriter = new FontStyleWriter($this->xmlWriter, $styleFont);
$styleWriter->write();
}
$this->xmlWriter->startElement('w:t');

View File

@ -11,6 +11,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Style\Cell;
use PhpOffice\PhpWord\Style\Table as TableStyle;
use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter;
/**
* Table element writer
@ -58,7 +60,9 @@ class Table extends Element
$tblStyle = $this->element->getStyle();
$tblWidth = $this->element->getWidth();
if ($tblStyle instanceof TableStyle) {
$this->parentWriter->writeTableStyle($this->xmlWriter, $tblStyle, false);
$styleWriter = new TableStyleWriter($this->xmlWriter, $tblStyle);
$styleWriter->setIsFullStyle(false);
$styleWriter->write();
} else {
if (!empty($tblStyle)) {
$this->xmlWriter->startElement('w:tblPr');
@ -115,7 +119,8 @@ class Table extends Element
$this->xmlWriter->writeAttribute('w:type', 'dxa');
$this->xmlWriter->endElement(); // w:tcW
if ($cellStyle instanceof Cell) {
$this->parentWriter->writeCellStyle($this->xmlWriter, $cellStyle);
$styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle);
$styleWriter->write();
}
$this->xmlWriter->endElement(); // w:tcPr
$this->parentWriter->writeContainerElements($this->xmlWriter, $cell);

View File

@ -10,8 +10,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* Text element writer
@ -31,11 +31,17 @@ class Text extends Element
$text = String::controlCharacterPHP2OOXML($text);
if (!$this->withoutP) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$this->parentWriter->writeInlineParagraphStyle($this->xmlWriter, $pStyle);
$styleWriter->write();
}
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:r');
$this->parentWriter->writeInlineFontStyle($this->xmlWriter, $fStyle);
$styleWriter->write();
$this->xmlWriter->startElement('w:t');
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
$this->xmlWriter->writeRaw($text);

View File

@ -9,6 +9,9 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* TextBreak element writer
*
@ -31,11 +34,17 @@ class TextBreak extends Element
$hasStyle = !is_null($fStyle) || !is_null($pStyle);
}
if ($hasStyle) {
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$this->parentWriter->writeInlineParagraphStyle($this->xmlWriter, $pStyle);
$styleWriter->write();
if (!is_null($fStyle)) {
$styleWriter = new FontStyleWriter($this->xmlWriter, $fStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:pPr');
$this->parentWriter->writeInlineFontStyle($this->xmlWriter, $fStyle);
$styleWriter->write();
$this->xmlWriter->endElement(); // w:pPr
}
$this->xmlWriter->endElement(); // w:p

View File

@ -9,8 +9,7 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Writer\Word2007\Style\Style as StyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* TextRun element writer
@ -25,8 +24,11 @@ class TextRun extends Element
public function write()
{
$pStyle = $this->element->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $pStyle);
$styleWriter->setIsInline(true);
$this->xmlWriter->startElement('w:p');
$this->parentWriter->writeInlineParagraphStyle($this->xmlWriter, $pStyle);
$styleWriter->write();
$this->parentWriter->writeContainerElements($this->xmlWriter, $this->element);
$this->xmlWriter->endElement(); // w:p
}

View File

@ -12,6 +12,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Element\Footnote;
use PhpOffice\PhpWord\Element\Endnote;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
/**
* Word2007 footnotes part writer
@ -96,8 +97,9 @@ class Notes extends Base
$xmlWriter->startElement('w:p');
// Paragraph style
$styleParagraph = $element->getParagraphStyle();
$this->writeInlineParagraphStyle($xmlWriter, $styleParagraph);
$styleWriter = new ParagraphStyleWriter($xmlWriter, $element->getParagraphStyle());
$styleWriter->setIsInline(true);
$styleWriter->write();
// Reference symbol
$xmlWriter->startElement('w:r');

View File

@ -0,0 +1,50 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Style writer
*
* @since 0.10.0
*/
abstract class AbstractStyle
{
/**
* XML writer
*
* @var \PhpOffice\PhpWord\Shared\XMLWriter
*/
protected $xmlWriter;
/**
* Style
*
* @var string|\PhpOffice\PhpWord\Style\AbstractStyle
*/
protected $style;
/**
* Write style
*/
abstract public function write();
/**
* Create new instance
*
* @param string|\PhpOffice\PhpWord\Style\AbstractStyle $style
*/
public function __construct(XMLWriter $xmlWriter, $style = null)
{
$this->xmlWriter = $xmlWriter;
$this->style = $style;
}
}

View File

@ -0,0 +1,90 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
/**
* Cell style writer
*
* @since 0.10.0
*/
class Cell extends AbstractStyle
{
/**
* Write style
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Cell)) {
return;
}
$bgColor = $this->style->getBgColor();
$valign = $this->style->getVAlign();
$textDir = $this->style->getTextDirection();
$brdSz = $this->style->getBorderSize();
$brdCol = $this->style->getBorderColor();
$hasBorders = false;
for ($i = 0; $i < 4; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
$styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $hasBorders) ? true : false;
if ($styles) {
if (!is_null($textDir)) {
$this->xmlWriter->startElement('w:textDirection');
$this->xmlWriter->writeAttribute('w:val', $textDir);
$this->xmlWriter->endElement();
}
if (!is_null($bgColor)) {
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', 'clear');
$this->xmlWriter->writeAttribute('w:color', 'auto');
$this->xmlWriter->writeAttribute('w:fill', $bgColor);
$this->xmlWriter->endElement();
}
if (!is_null($valign)) {
$this->xmlWriter->startElement('w:vAlign');
$this->xmlWriter->writeAttribute('w:val', $valign);
$this->xmlWriter->endElement();
}
if ($hasBorders) {
$defaultColor = $this->style->getDefaultBorderColor();
$mbWriter = new MarginBorder($this->xmlWriter);
$mbWriter->setSizes($brdSz);
$mbWriter->setColors($brdCol);
$mbWriter->setAttributes(array('defaultColor' => $defaultColor));
$this->xmlWriter->startElement('w:tcBorders');
$mbWriter->write();
$this->xmlWriter->endElement();
}
}
$gridSpan = $this->style->getGridSpan();
if (!is_null($gridSpan)) {
$this->xmlWriter->startElement('w:gridSpan');
$this->xmlWriter->writeAttribute('w:val', $gridSpan);
$this->xmlWriter->endElement();
}
$vMerge = $this->style->getVMerge();
if (!is_null($vMerge)) {
$this->xmlWriter->startElement('w:vMerge');
$this->xmlWriter->writeAttribute('w:val', $vMerge);
$this->xmlWriter->endElement();
}
}
}

View File

@ -0,0 +1,159 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\PhpWord;
/**
* Font style writer
*
* @since 0.10.0
*/
class Font extends AbstractStyle
{
/**
* Is inline in element
*
* @var bool
*/
private $isInline = false;
/**
* Write style
*/
public function write()
{
$isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style);
if ($isStyleName) {
$this->xmlWriter->startElement('w:rPr');
$this->xmlWriter->startElement('w:rStyle');
$this->xmlWriter->writeAttribute('w:val', $this->style);
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
} else {
$this->writeStyle();
}
}
/**
* Write full style
*/
private function writeStyle()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) {
return;
}
$font = $this->style->getName();
$bold = $this->style->getBold();
$italic = $this->style->getItalic();
$color = $this->style->getColor();
$size = $this->style->getSize();
$fgColor = $this->style->getFgColor();
$bgColor = $this->style->getBgColor();
$strikethrough = $this->style->getStrikethrough();
$underline = $this->style->getUnderline();
$superscript = $this->style->getSuperScript();
$subscript = $this->style->getSubScript();
$hint = $this->style->getHint();
$this->xmlWriter->startElement('w:rPr');
// Font
if ($font != PhpWord::DEFAULT_FONT_NAME) {
$this->xmlWriter->startElement('w:rFonts');
$this->xmlWriter->writeAttribute('w:ascii', $font);
$this->xmlWriter->writeAttribute('w:hAnsi', $font);
$this->xmlWriter->writeAttribute('w:eastAsia', $font);
$this->xmlWriter->writeAttribute('w:cs', $font);
//Font Content Type
if ($hint != PhpWord::DEFAULT_FONT_CONTENT_TYPE) {
$this->xmlWriter->writeAttribute('w:hint', $hint);
}
$this->xmlWriter->endElement();
}
// Color
if ($color != PhpWord::DEFAULT_FONT_COLOR) {
$this->xmlWriter->startElement('w:color');
$this->xmlWriter->writeAttribute('w:val', $color);
$this->xmlWriter->endElement();
}
// Size
if ($size != PhpWord::DEFAULT_FONT_SIZE) {
$this->xmlWriter->startElement('w:sz');
$this->xmlWriter->writeAttribute('w:val', $size * 2);
$this->xmlWriter->endElement();
$this->xmlWriter->startElement('w:szCs');
$this->xmlWriter->writeAttribute('w:val', $size * 2);
$this->xmlWriter->endElement();
}
// Bold
if ($bold) {
$this->xmlWriter->writeElement('w:b', null);
}
// Italic
if ($italic) {
$this->xmlWriter->writeElement('w:i', null);
$this->xmlWriter->writeElement('w:iCs', null);
}
// Underline
if (!is_null($underline) && $underline != 'none') {
$this->xmlWriter->startElement('w:u');
$this->xmlWriter->writeAttribute('w:val', $underline);
$this->xmlWriter->endElement();
}
// Strikethrough
if ($strikethrough) {
$this->xmlWriter->writeElement('w:strike', null);
}
// Foreground-Color
if (!is_null($fgColor)) {
$this->xmlWriter->startElement('w:highlight');
$this->xmlWriter->writeAttribute('w:val', $fgColor);
$this->xmlWriter->endElement();
}
// Background-Color
if (!is_null($bgColor)) {
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', "clear");
$this->xmlWriter->writeAttribute('w:color', "auto");
$this->xmlWriter->writeAttribute('w:fill', $bgColor);
$this->xmlWriter->endElement();
}
// Superscript/subscript
if ($superscript || $subscript) {
$this->xmlWriter->startElement('w:vertAlign');
$this->xmlWriter->writeAttribute('w:val', $superscript ? 'superscript' : 'subscript');
$this->xmlWriter->endElement();
}
$this->xmlWriter->endElement();
}
/**
* Set is inline
*
* @param bool $value
*/
public function setIsInline($value)
{
$this->isInline = $value;
}
}

View File

@ -0,0 +1,102 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
/**
* Margin border style writer
*
* @since 0.10.0
*/
class MarginBorder extends AbstractStyle
{
/**
* Sizes
*
* @var int[]
*/
private $sizes;
/**
* Colors
*
* @var string[]
*/
private $colors;
/**
* Other attributes
*
* @var array
*/
private $attributes = array();
/**
* Write style
*/
public function write()
{
$sides = array('top', 'left', 'right', 'bottom', 'insideH', 'insideV');
$sizeCount = count($this->sizes) - 1;
for ($i = 0; $i < $sizeCount; $i++) {
if (!is_null($this->sizes[$i])) {
$this->xmlWriter->startElement('w:' . $sides[$i]);
if (!empty($this->colors)) {
if (is_null($this->colors[$i]) && !empty($this->attributes)) {
if (array_key_exists('defaultColor', $this->attributes)) {
$this->colors[$i] = $this->attributes['defaultColor'];
}
}
$this->xmlWriter->writeAttribute('w:val', 'single');
$this->xmlWriter->writeAttribute('w:sz', $this->sizes[$i]);
$this->xmlWriter->writeAttribute('w:color', $this->colors[$i]);
if (!empty($this->attributes)) {
if (array_key_exists('space', $this->attributes)) {
$this->xmlWriter->writeAttribute('w:space', '24');
}
}
} else {
$this->xmlWriter->writeAttribute('w:w', $this->sizes[$i]);
$this->xmlWriter->writeAttribute('w:type', 'dxa');
}
$this->xmlWriter->endElement();
}
}
}
/**
* Set sizes
*
* @param int[] $value
*/
public function setSizes($value)
{
$this->sizes = $value;
}
/**
* Set colors
*
* @param string[] $value
*/
public function setColors($value)
{
$this->colors = $value;
}
/**
* Set attributes
*
* @param array $value
*/
public function setAttributes($value)
{
$this->attributes = $value;
}
}

View File

@ -0,0 +1,182 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
/**
* Paragraph style writer
*
* @since 0.10.0
*/
class Paragraph extends AbstractStyle
{
/**
* Without w:pPr
*
* @var bool
*/
private $withoutPPR = false;
/**
* Is inline in element
*
* @var bool
*/
private $isInline = false;
/**
* Write style
*/
public function write()
{
$isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style);
if ($isStyleName) {
if (!$this->withoutPPR) {
$this->xmlWriter->startElement('w:pPr');
}
$this->xmlWriter->startElement('w:pStyle');
$this->xmlWriter->writeAttribute('w:val', $this->style);
$this->xmlWriter->endElement();
if (!$this->withoutPPR) {
$this->xmlWriter->endElement();
}
} else {
$this->writeStyle();
}
}
/**
* Write full style
*/
private function writeStyle()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Paragraph)) {
return;
}
$align = $this->style->getAlign();
$spacing = $this->style->getSpacing();
$spaceBefore = $this->style->getSpaceBefore();
$spaceAfter = $this->style->getSpaceAfter();
$indent = $this->style->getIndent();
$hanging = $this->style->getHanging();
$tabs = $this->style->getTabs();
$widowControl = $this->style->getWidowControl();
$keepNext = $this->style->getKeepNext();
$keepLines = $this->style->getKeepLines();
$pageBreakBefore = $this->style->getPageBreakBefore();
if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) ||
!is_null($spaceAfter) || !is_null($indent) || !is_null($hanging) ||
!is_null($tabs) || !is_null($widowControl) || !is_null($keepNext) ||
!is_null($keepLines) || !is_null($pageBreakBefore)) {
if (!$this->withoutPPR) {
$this->xmlWriter->startElement('w:pPr');
}
// Alignment
if (!is_null($align)) {
$this->xmlWriter->startElement('w:jc');
$this->xmlWriter->writeAttribute('w:val', $align);
$this->xmlWriter->endElement();
}
// Indentation
if (!is_null($indent) || !is_null($hanging)) {
$this->xmlWriter->startElement('w:ind');
$this->xmlWriter->writeAttribute('w:firstLine', 0);
if (!is_null($indent)) {
$this->xmlWriter->writeAttribute('w:left', $indent);
}
if (!is_null($hanging)) {
$this->xmlWriter->writeAttribute('w:hanging', $hanging);
}
$this->xmlWriter->endElement();
}
// Spacing
if (!is_null($spaceBefore) || !is_null($spaceAfter) ||
!is_null($spacing)) {
$this->xmlWriter->startElement('w:spacing');
if (!is_null($spaceBefore)) {
$this->xmlWriter->writeAttribute('w:before', $spaceBefore);
}
if (!is_null($spaceAfter)) {
$this->xmlWriter->writeAttribute('w:after', $spaceAfter);
}
if (!is_null($spacing)) {
$this->xmlWriter->writeAttribute('w:line', $spacing);
$this->xmlWriter->writeAttribute('w:lineRule', 'auto');
}
$this->xmlWriter->endElement();
}
// Pagination
if (!$widowControl) {
$this->xmlWriter->startElement('w:widowControl');
$this->xmlWriter->writeAttribute('w:val', '0');
$this->xmlWriter->endElement();
}
if ($keepNext) {
$this->xmlWriter->startElement('w:keepNext');
$this->xmlWriter->writeAttribute('w:val', '1');
$this->xmlWriter->endElement();
}
if ($keepLines) {
$this->xmlWriter->startElement('w:keepLines');
$this->xmlWriter->writeAttribute('w:val', '1');
$this->xmlWriter->endElement();
}
if ($pageBreakBefore) {
$this->xmlWriter->startElement('w:pageBreakBefore');
$this->xmlWriter->writeAttribute('w:val', '1');
$this->xmlWriter->endElement();
}
// Tabs
if (!empty($tabs)) {
$this->xmlWriter->startElement("w:tabs");
foreach ($tabs as $tab) {
$this->xmlWriter->startElement("w:tab");
$this->xmlWriter->writeAttribute("w:val", $tab->getStopType());
if (!is_null($tab->getLeader())) {
$this->xmlWriter->writeAttribute("w:leader", $tab->getLeader());
}
$this->xmlWriter->writeAttribute("w:pos", $tab->getPosition());
$this->xmlWriter->endElement();
}
$this->xmlWriter->endElement();
}
if (!$this->withoutPPR) {
$this->xmlWriter->endElement(); // w:pPr
}
}
}
/**
* Set without w:pPr
*
* @param bool $value
*/
public function setWithoutPPR($value)
{
$this->withoutPPR = $value;
}
/**
* Set is inline
*
* @param bool $value
*/
public function setIsInline($value)
{
$this->isInline = $value;
}
}

View File

@ -0,0 +1,149 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
/**
* Table style writer
*
* @since 0.10.0
*/
class Table extends AbstractStyle
{
/**
* Is full style
*
* @var bool
*/
private $isFullStyle = true;
/**
* Write style
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Table)) {
return;
}
$bgColor = $this->style->getBgColor();
$brdCol = $this->style->getBorderColor();
$brdSz = $this->style->getBorderSize();
$cellMargin = $this->style->getCellMargin();
// If any of the borders/margins is set, process them
$hasBorders = false;
for ($i = 0; $i < 6; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
$hasMargins = false;
for ($i = 0; $i < 4; $i++) {
if (!is_null($cellMargin[$i])) {
$hasMargins = true;
break;
}
}
if ($hasMargins || $hasBorders) {
$this->xmlWriter->startElement('w:tblPr');
if ($hasMargins) {
$mbWriter = new MarginBorder($this->xmlWriter);
$mbWriter->setSizes($cellMargin);
$this->xmlWriter->startElement('w:tblCellMar');
$mbWriter->write();
$this->xmlWriter->endElement(); // w:tblCellMar
}
if ($hasBorders) {
$mbWriter = new MarginBorder($this->xmlWriter);
$mbWriter->setSizes($brdSz);
$mbWriter->setColors($brdCol);
$this->xmlWriter->startElement('w:tblBorders');
$mbWriter->write();
$this->xmlWriter->endElement(); // w:tblBorders
}
$this->xmlWriter->endElement(); // w:tblPr
}
// Only write background color and first row for full style
if ($this->isFullStyle) {
// Background color
if (!is_null($bgColor)) {
$this->xmlWriter->startElement('w:tcPr');
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', 'clear');
$this->xmlWriter->writeAttribute('w:color', 'auto');
$this->xmlWriter->writeAttribute('w:fill', $bgColor);
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
}
// First Row
$firstRow = $this->style->getFirstRow();
if ($firstRow instanceof \PhpOffice\PhpWord\Style\Table) {
$this->writeFirstRow($firstRow, 'firstRow');
}
}
}
/**
* Set is full style
*
* @param bool $value
*/
public function setIsFullStyle($value)
{
$this->isFullStyle = $value;
}
/**
* Write row style
*
* @param string $type
*/
private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style, $type)
{
$bgColor = $style->getBgColor();
$this->xmlWriter->startElement('w:tblStylePr');
$this->xmlWriter->writeAttribute('w:type', $type);
$this->xmlWriter->startElement('w:tcPr');
if (!is_null($bgColor)) {
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', 'clear');
$this->xmlWriter->writeAttribute('w:color', 'auto');
$this->xmlWriter->writeAttribute('w:fill', $bgColor);
$this->xmlWriter->endElement(); // w:shd
}
// Borders
$brdSz = $style->getBorderSize();
$brdCol = $style->getBorderColor();
$hasBorders = false;
for ($i = 0; $i < 6; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
if ($hasBorders) {
$mbWriter = new MarginBorder($this->xmlWriter);
$mbWriter->setSizes($brdSz);
$mbWriter->setColors($brdCol);
$this->xmlWriter->startElement('w:tcBorders');
$mbWriter->write();
$this->xmlWriter->endElement(); // w:tcBorders
}
$this->xmlWriter->endElement(); // w:tcPr
$this->xmlWriter->endElement(); // w:tblStylePr
}
}

View File

@ -16,6 +16,9 @@ use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
/**
* Word2007 styles part writer
@ -82,10 +85,13 @@ class Styles extends Base
$xmlWriter->startElement('w:basedOn');
$xmlWriter->writeAttribute('w:val', 'Normal');
$xmlWriter->endElement();
$this->writeParagraphStyle($xmlWriter, $paragraphStyle);
$styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
$styleWriter->write();
}
$this->writeFontStyle($xmlWriter, $style);
$styleWriter = new FontStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement();
// Paragraph style
@ -112,7 +118,8 @@ class Styles extends Base
$xmlWriter->endElement();
}
$this->writeParagraphStyle($xmlWriter, $style);
$styleWriter = new ParagraphStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement();
// Table style
@ -128,7 +135,9 @@ class Styles extends Base
$xmlWriter->writeAttribute('w:val', '99');
$xmlWriter->endElement();
$this->writeTableStyle($xmlWriter, $style);
$styleWriter = new TableStyleWriter($xmlWriter, $style);
$styleWriter->write();
$xmlWriter->endElement(); // w:style
}
}
@ -179,7 +188,8 @@ class Styles extends Base
$xmlWriter->writeAttribute('w:val', 'Normal');
$xmlWriter->endElement(); // w:name
if (array_key_exists('Normal', $styles)) {
$this->writeParagraphStyle($xmlWriter, $styles['Normal']);
$styleWriter = new ParagraphStyleWriter($xmlWriter, $styles['Normal']);
$styleWriter->write();
}
$xmlWriter->endElement(); // w:style