/** * Write style */ public function write() { if (!$this->style instanceof \PhpOffice\PhpWord\Style\Cell) { return; } $brdSz = $this->style->getBorderSize(); $brdCol = $this->style->getBorderColor(); $hasBorders = false; for ($i = 0; $i < 4; $i++) { if (!is_null($brdSz[$i])) { $hasBorders = true; break; } } // Border if ($hasBorders) { $mbWriter = new MarginBorder($this->xmlWriter); $mbWriter->setSizes($brdSz); $mbWriter->setColors($brdCol); $mbWriter->setAttributes(array('defaultColor' => CellStyle::DEFAULT_BORDER_COLOR)); $this->xmlWriter->startElement('w:tcBorders'); $mbWriter->write(); $this->xmlWriter->endElement(); } // Text direction if (!is_null($this->style->getTextDirection())) { $this->xmlWriter->startElement('w:textDirection'); $this->xmlWriter->writeAttribute('w:val', $this->style->getTextDirection()); $this->xmlWriter->endElement(); } // Shading if (!is_null($this->style->getShading())) { $styleWriter = new Shading($this->xmlWriter, $this->style->getShading()); $styleWriter->write(); } // Alignment if (!is_null($this->style->getVAlign())) { $this->xmlWriter->startElement('w:vAlign'); $this->xmlWriter->writeAttribute('w:val', $this->style->getVAlign()); $this->xmlWriter->endElement(); } // Colspan if (!is_null($this->style->getGridSpan())) { $this->xmlWriter->startElement('w:gridSpan'); $this->xmlWriter->writeAttribute('w:val', $this->style->getGridSpan()); $this->xmlWriter->endElement(); } // Row span if (!is_null($this->style->getVMerge())) { $this->xmlWriter->startElement('w:vMerge'); $this->xmlWriter->writeAttribute('w:val', $this->style->getVMerge()); $this->xmlWriter->endElement(); } }
/** * Write style */ public function write() { $style = $this->getStyle(); if (!$style instanceof CellStyle) { return; } $xmlWriter = $this->getXmlWriter(); $xmlWriter->startElement('w:tcPr'); // Width $xmlWriter->startElement('w:tcW'); $xmlWriter->writeAttribute('w:w', $this->width); $xmlWriter->writeAttribute('w:type', 'dxa'); $xmlWriter->endElement(); // w:tcW // Text direction $textDir = $style->getTextDirection(); $xmlWriter->writeElementIf(!is_null($textDir), 'w:textDirection', 'w:val', $textDir); // Vertical alignment $vAlign = $style->getVAlign(); $xmlWriter->writeElementIf(!is_null($vAlign), 'w:vAlign', 'w:val', $vAlign); // Border if ($style->hasBorder()) { $xmlWriter->startElement('w:tcBorders'); $styleWriter = new MarginBorder($xmlWriter); $styleWriter->setSizes($style->getBorderSize()); $styleWriter->setColors($style->getBorderColor()); $styleWriter->setAttributes(array('defaultColor' => CellStyle::DEFAULT_BORDER_COLOR)); $styleWriter->write(); $xmlWriter->endElement(); } // Shading $shading = $style->getShading(); if (!is_null($shading)) { $styleWriter = new Shading($xmlWriter, $shading); $styleWriter->write(); } // Colspan & rowspan $gridSpan = $style->getGridSpan(); $vMerge = $style->getVMerge(); $xmlWriter->writeElementIf(!is_null($gridSpan), 'w:gridSpan', 'w:val', $gridSpan); $xmlWriter->writeElementIf(!is_null($vMerge), 'w:vMerge', 'w:val', $vMerge); $xmlWriter->endElement(); // w:tcPr }
/** * Write shading. * * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param \PhpOffice\PhpWord\Style\Table $style * @return void */ private function writeShading(XMLWriter $xmlWriter, TableStyle $style) { if (null !== $style->getShading()) { $xmlWriter->startElement('w:tcPr'); $styleWriter = new Shading($xmlWriter, $style->getShading()); $styleWriter->write(); $xmlWriter->endElement(); } }
/** * Write full style */ private function writeStyle() { if (!$this->style instanceof \PhpOffice\PhpWord\Style\Font) { return; } $font = $this->style->getName(); $color = $this->style->getColor(); $size = $this->style->getSize(); $this->xmlWriter->startElement('w:rPr'); // Font name/family 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 ($this->style->getHint() != PhpWord::DEFAULT_FONT_CONTENT_TYPE) { $this->xmlWriter->writeAttribute('w:hint', $this->style->getHint()); } $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 ($this->style->getBold()) { $this->xmlWriter->writeElement('w:b', null); } // Italic if ($this->style->getItalic()) { $this->xmlWriter->writeElement('w:i', null); $this->xmlWriter->writeElement('w:iCs', null); } // Underline if ($this->style->getUnderline() != 'none') { $this->xmlWriter->startElement('w:u'); $this->xmlWriter->writeAttribute('w:val', $this->style->getUnderline()); $this->xmlWriter->endElement(); } // Strikethrough if ($this->style->getStrikethrough()) { $this->xmlWriter->writeElement('w:strike', null); } // Double strikethrough if ($this->style->getDoubleStrikethrough()) { $this->xmlWriter->writeElement('w:dstrike', null); } // Foreground-Color if (!is_null($this->style->getFgColor())) { $this->xmlWriter->startElement('w:highlight'); $this->xmlWriter->writeAttribute('w:val', $this->style->getFgColor()); $this->xmlWriter->endElement(); } // Background-Color if (!is_null($this->style->getShading())) { $styleWriter = new Shading($this->xmlWriter, $this->style->getShading()); $styleWriter->write(); } // Superscript/subscript if ($this->style->getSuperScript() || $this->style->getSubScript()) { $this->xmlWriter->startElement('w:vertAlign'); $this->xmlWriter->writeAttribute('w:val', $this->style->getSuperScript() ? 'superscript' : 'subscript'); $this->xmlWriter->endElement(); } // Small caps if ($this->style->getSmallCaps()) { $this->xmlWriter->writeElement('w:smallCaps', null); } // All caps if ($this->style->getAllCaps()) { $this->xmlWriter->writeElement('w:caps', null); } $this->xmlWriter->endElement(); }
/** * Write row style * * @param string $type */ private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style, $type) { $this->xmlWriter->startElement('w:tblStylePr'); $this->xmlWriter->writeAttribute('w:type', $type); $this->xmlWriter->startElement('w:tcPr'); if (!is_null($style->getShading())) { $styleWriter = new Shading($this->xmlWriter, $style->getShading()); $styleWriter->write(); } // Borders $brdSz = $style->getBorderSize(); $brdCol = $style->getBorderColor(); $hasBorders = false; for ($i = 0; $i < 6; $i++) { if (!is_null($brdSz[$i])) { $hasBorders = true; } } 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 }