/** * Write title element */ public function write() { $anchor = $this->element->getAnchor(); $bookmarkId = $this->element->getBookmarkId(); $style = $this->element->getStyle(); $text = htmlspecialchars($this->element->getText()); $text = String::controlCharacterPHP2OOXML($text); $this->xmlWriter->startElement('w:p'); if (!empty($style)) { $this->xmlWriter->startElement('w:pPr'); $this->xmlWriter->startElement('w:pStyle'); $this->xmlWriter->writeAttribute('w:val', $style); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); } $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:fldChar'); $this->xmlWriter->writeAttribute('w:fldCharType', 'end'); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); $this->xmlWriter->startElement('w:bookmarkStart'); $this->xmlWriter->writeAttribute('w:id', $bookmarkId); $this->xmlWriter->writeAttribute('w:name', $anchor); $this->xmlWriter->endElement(); $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:t'); $this->xmlWriter->writeRaw($text); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); $this->xmlWriter->startElement('w:bookmarkEnd'); $this->xmlWriter->writeAttribute('w:id', $bookmarkId); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); }
/** * Write text element */ public function write() { $fontStyle = $this->element->getFontStyle(); $paragraphStyle = $this->element->getParagraphStyle(); $text = htmlspecialchars($this->element->getText()); $text = String::controlCharacterPHP2OOXML($text); if (!$this->withoutP) { $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); $styleWriter->setIsInline(true); $this->xmlWriter->startElement('w:p'); $styleWriter->write(); } $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); $styleWriter->setIsInline(true); $this->xmlWriter->startElement('w:r'); $styleWriter->write(); $this->xmlWriter->startElement('w:t'); $this->xmlWriter->writeAttribute('xml:space', 'preserve'); $this->xmlWriter->writeRaw($text); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); // w:r if (!$this->withoutP) { $this->xmlWriter->endElement(); // w:p } }
/** * Create a new Link Element * * @param string $target * @param string $text * @param mixed $fontStyle * @param mixed $paragraphStyle */ public function __construct($target, $text = null, $fontStyle = null, $paragraphStyle = null) { $this->target = String::toUTF8($target); $this->text = is_null($text) ? $this->target : String::toUTF8($text); $this->fontStyle = $this->setStyle(new Font('text'), $fontStyle); $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); return $this; }
/** * Create a new Link Element * * @param string $source * @param string $text * @param mixed $fontStyle * @param mixed $paragraphStyle */ public function __construct($source, $text = null, $fontStyle = null, $paragraphStyle = null, $internal = false) { $this->source = String::toUTF8($source); $this->text = is_null($text) ? $this->source : String::toUTF8($text); $this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle); $this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle); $this->internal = $internal; return $this; }
/** * Create a new Title Element * * @param string $text * @param int $depth */ public function __construct($text, $depth = 1) { $this->text = String::toUTF8($text); $this->depth = $depth; if (array_key_exists("Heading_{$this->depth}", Style::getStyles())) { $this->style = "Heading{$this->depth}"; } return $this; }
/** * Write preserve text element */ public function write() { $fontStyle = $this->element->getFontStyle(); $paragraphStyle = $this->element->getParagraphStyle(); $texts = $this->element->getText(); if (!is_array($texts)) { $texts = array($texts); } $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); $styleWriter->setIsInline(true); $this->xmlWriter->startElement('w:p'); $styleWriter->write(); foreach ($texts as $text) { if (substr($text, 0, 1) == '{') { $text = substr($text, 1, -1); $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); $styleWriter->setIsInline(true); $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:fldChar'); $this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); $this->xmlWriter->startElement('w:r'); $styleWriter->write(); $this->xmlWriter->startElement('w:instrText'); $this->xmlWriter->writeAttribute('xml:space', 'preserve'); $this->xmlWriter->writeRaw($text); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:fldChar'); $this->xmlWriter->writeAttribute('w:fldCharType', 'separate'); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:fldChar'); $this->xmlWriter->writeAttribute('w:fldCharType', 'end'); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); } else { $text = htmlspecialchars($text); $text = String::controlCharacterPHP2OOXML($text); $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); $styleWriter->setIsInline(true); $this->xmlWriter->startElement('w:r'); $styleWriter->write(); $this->xmlWriter->startElement('w:t'); $this->xmlWriter->writeAttribute('xml:space', 'preserve'); $this->xmlWriter->writeRaw($text); $this->xmlWriter->endElement(); $this->xmlWriter->endElement(); } } $this->xmlWriter->endElement(); // p }
/** * Create a new Preserve Text Element * * @param string $text * @param mixed $fontStyle * @param mixed $paragraphStyle * @return self */ public function __construct($text = null, $fontStyle = null, $paragraphStyle = null) { $this->fontStyle = $this->setStyle(new Font('text'), $fontStyle); $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); $this->text = String::toUTF8($text); $matches = preg_split('/({.*?})/', $this->text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); if (isset($matches[0])) { $this->text = $matches; } return $this; }
/** * Create a new ListItem * * @param string $text * @param int $depth * @param mixed $fontStyle * @param array|string|null $listStyle * @param mixed $paragraphStyle */ public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) { $this->textObject = new Text(String::toUTF8($text), $fontStyle, $paragraphStyle); $this->depth = $depth; // Version >= 0.10.0 will pass numbering style name. Older version will use old method if (!is_null($listStyle) && is_string($listStyle)) { $this->style = new ListItemStyle($listStyle); } else { $this->style = $this->setNewStyle(new ListItemStyle(), $listStyle, true); } }
/** * Set style value template method * * Some child classes have their own specific overrides. * Backward compability check for versions < 0.10.0 which use underscore * prefix for their private properties. * Check if the set method is exists. Throws an exception? * * @param string $key * @param string $value * @return self */ public function setStyleValue($key, $value) { if (isset($this->aliases[$key])) { $key = $this->aliases[$key]; } $method = 'set' . String::removeUnderscorePrefix($key); if (method_exists($this, $method)) { $this->{$method}($value); } return $this; }
/** * Set text content * * @param string $text * @return self */ public function setText($text) { $this->text = String::toUTF8($text); return $this; }
/** * Convert text to valid format * * @param string $text * @return string */ protected function getText($text) { return String::controlCharacterPHP2OOXML($text); }
/** * Set name content * * @param string $name * @return self */ public function setName($name) { $this->name = String::toUTF8($name); return $this; }
/** * Test unicode conversion */ public function testToUnicode() { $this->assertEquals('a', String::toUnicode('a')); $this->assertEquals('\\uc0{\\u8364}', String::toUnicode('€')); $this->assertEquals('\\uc0{\\u233}', String::toUnicode('é')); }
/** * Add a CheckBox Element * * @param string $name * @param string $text * @param mixed $fontStyle * @param mixed $paragraphStyle * @return CheckBox */ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = null) { $this->checkValidity('checkbox'); $checkBox = new CheckBox(String::toUTF8($name), String::toUTF8($text), $fontStyle, $paragraphStyle); $checkBox->setDocPart($this->getDocPart(), $this->getDocPartId()); $this->addElement($checkBox); return $checkBox; }
/** * Convert text to valid format * * @param string $text * @return string */ protected function getText($text) { return String::controlCharacterPHP2OOXML(htmlspecialchars($text)); }
/** * @param mixed $macro * @param mixed $replace * @param integer $limit * * @return void */ public function setValue($macro, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT) { if (substr($macro, 0, 2) !== '${' && substr($macro, -1) !== '}') { $macro = '${' . $macro . '}'; } if (!String::isUTF8($replace)) { $replace = utf8_encode($replace); } foreach ($this->tempDocumentHeaders as $index => $headerXML) { $this->tempDocumentHeaders[$index] = $this->setValueForPart($this->tempDocumentHeaders[$index], $macro, $replace, $limit); } $this->tempDocumentMainPart = $this->setValueForPart($this->tempDocumentMainPart, $macro, $replace, $limit); foreach ($this->tempDocumentFooters as $index => $headerXML) { $this->tempDocumentFooters[$index] = $this->setValueForPart($this->tempDocumentFooters[$index], $macro, $replace, $limit); } }
/** * Set Style value * * @param string $key * @param mixed $value * @return self */ public function setStyleValue($key, $value) { $key = String::removeUnderscorePrefix($key); if ($key == 'indent' || $key == 'hanging') { $value = $value * 720; } elseif ($key == 'spacing') { $value += 240; // because line height of 1 matches 240 twips } return parent::setStyleValue($key, $value); }
/** * Set Style value * * @param string $key * @param mixed $value */ public function setStyleValue($key, $value) { $key = String::removeUnderscorePrefix($key); if ($key == 'indent' || $key == 'hanging') { $value = $value * 720; } elseif ($key == 'spacing') { $value += 240; // because line height of 1 matches 240 twips } elseif ($key === 'line-height') { $this->setLineHeight($value); return; } $method = 'set' . $key; if (method_exists($this, $method)) { $this->{$method}($value); } }
/** * Test remove underscore prefix */ public function testRemoveUnderscorePrefix() { $this->assertEquals('item', String::removeUnderscorePrefix('_item')); }
/** * Set text content * 修复phpword的bug,详见https://github.com/PHPOffice/PHPWord/issues/401 * @param string $text * @return self */ public function setText($text) { $this->text = String::toUTF8(htmlspecialchars($text)); return $this; }
/** * Create a new Bookmark Element * * @param string $name */ public function __construct($name) { $this->name = String::toUTF8($name); return $this; }
/** * Find and replace macros in the given XML section. * * @param string $documentPartXML * @param string $search * @param string $replace * @param integer $limit * * @return string */ protected function setValueForPart($documentPartXML, $search, $replace, $limit) { if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { $search = '${' . $search . '}'; } if (!String::isUTF8($replace)) { $replace = utf8_encode($replace); } // Note: we can't use the same function for both cases here, because of performance considerations. if (self::MAXIMUM_REPLACEMENTS_DEFAULT === $limit) { return str_replace($search, $replace, $documentPartXML); } else { $regExpDelim = '/'; $escapedSearch = preg_quote($search, $regExpDelim); return preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u", $replace, $documentPartXML, $limit); } }
/** * PHP to OOXML control character */ public function testControlCharacterPHP2OOXML() { $this->assertEquals('', String::controlCharacterPHP2OOXML('')); $this->assertEquals('_x0008_', String::controlCharacterPHP2OOXML(chr(0x8))); }
/** * Find and replace placeholders in the given XML section. * * @param string $documentPartXML * @param string $search * @param string $replace * @param integer $limit * @return string */ protected function setValueForPart($documentPartXML, $search, $replace, $limit) { if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { $search = '${' . $search . '}'; } if (!String::isUTF8($replace)) { $replace = utf8_encode($replace); } $regExpDelim = '/'; $escapedSearch = preg_quote($search, $regExpDelim); return preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u", $replace, $documentPartXML, $limit); }
/** * Write element */ public function write() { $name = htmlspecialchars($this->element->getName()); $name = String::controlCharacterPHP2OOXML($name); $text = htmlspecialchars($this->element->getText()); $text = String::controlCharacterPHP2OOXML($text); $fontStyle = $this->element->getFontStyle(); $paragraphStyle = $this->element->getParagraphStyle(); if (!$this->withoutP) { $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); $styleWriter->setIsInline(true); $this->xmlWriter->startElement('w:p'); $styleWriter->write(); } $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:fldChar'); $this->xmlWriter->writeAttribute('w:fldCharType', 'begin'); $this->xmlWriter->startElement('w:ffData'); $this->xmlWriter->startElement('w:name'); $this->xmlWriter->writeAttribute('w:val', $name); $this->xmlWriter->endElement(); //w:name $this->xmlWriter->writeAttribute('w:enabled', ''); $this->xmlWriter->startElement('w:calcOnExit'); $this->xmlWriter->writeAttribute('w:val', '0'); $this->xmlWriter->endElement(); //w:calcOnExit $this->xmlWriter->startElement('w:checkBox'); $this->xmlWriter->writeAttribute('w:sizeAuto', ''); $this->xmlWriter->startElement('w:default'); $this->xmlWriter->writeAttribute('w:val', 0); $this->xmlWriter->endElement(); //w:default $this->xmlWriter->endElement(); //w:checkBox $this->xmlWriter->endElement(); // w:ffData $this->xmlWriter->endElement(); // w:fldChar $this->xmlWriter->endElement(); // w:r $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:instrText'); $this->xmlWriter->writeAttribute('xml:space', 'preserve'); $this->xmlWriter->writeRaw(' FORMCHECKBOX '); $this->xmlWriter->endElement(); // w:instrText $this->xmlWriter->endElement(); // w:r $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:fldChar'); $this->xmlWriter->writeAttribute('w:fldCharType', 'seperate'); $this->xmlWriter->endElement(); // w:fldChar $this->xmlWriter->endElement(); // w:r $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:fldChar'); $this->xmlWriter->writeAttribute('w:fldCharType', 'end'); $this->xmlWriter->endElement(); // w:fldChar $this->xmlWriter->endElement(); // w:r $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle); $styleWriter->setIsInline(true); $this->xmlWriter->startElement('w:r'); $styleWriter->write(); $this->xmlWriter->startElement('w:t'); $this->xmlWriter->writeAttribute('xml:space', 'preserve'); $this->xmlWriter->writeRaw($text); $this->xmlWriter->endElement(); // w:t $this->xmlWriter->endElement(); // w:r if (!$this->withoutP) { $this->xmlWriter->endElement(); // w:p } }
/** * Find and replace placeholders in the given XML section. * * @param string $documentPartXML * @param string $search * @param string $replace * @param integer $limit * @return string */ protected function setValueForPart($documentPartXML, $search, $replace, $limit) { $pattern = '|\\$\\{([^\\}]+)\\}|U'; preg_match_all($pattern, $documentPartXML, $matches); foreach ($matches[0] as $value) { $valueCleaned = preg_replace('/<[^>]+>/', '', $value); $valueCleaned = preg_replace('/<\\/[^>]+>/', '', $valueCleaned); $documentPartXML = str_replace($value, $valueCleaned, $documentPartXML); } if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { $search = '${' . $search . '}'; } if (!String::isUTF8($replace)) { $replace = utf8_encode($replace); } $regExpDelim = '/'; $escapedSearch = preg_quote($search, $regExpDelim); return preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u", $replace, $documentPartXML, $limit); }
/** * Write text * * @param string $text * @return string */ protected function writeText($text) { return String::toUnicode($text); }