Example #1
0
 /**
  * Set Text style
  *
  * @param mixed $style
  * @param mixed $paragraphStyle
  * @return string|\PhpOffice\PhpWord\Style\Font
  */
 public function setFontStyle($style = null, $paragraphStyle = null)
 {
     if ($style instanceof Font) {
         $this->fontStyle = $style;
         $this->setParagraphStyle($paragraphStyle);
     } elseif (is_array($style)) {
         $this->fontStyle = new Font('text', $paragraphStyle);
         $this->fontStyle->setStyleByArray($style);
     } else {
         $this->fontStyle = $style;
         $this->setParagraphStyle($paragraphStyle);
     }
     return $this->fontStyle;
 }
 /**
  * Create a new Table-of-Contents Element
  *
  * @param mixed $fontStyle
  * @param array $tocStyle
  * @param integer $minDepth
  * @param integer $maxDepth
  */
 public function __construct($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
 {
     $this->TOCStyle = new TOCStyle();
     if (!is_null($tocStyle) && is_array($tocStyle)) {
         $this->TOCStyle->setStyleByArray($tocStyle);
     }
     if (!is_null($fontStyle) && is_array($fontStyle)) {
         $this->fontStyle = new Font();
         $this->fontStyle->setStyleByArray($fontStyle);
     } else {
         $this->fontStyle = $fontStyle;
     }
     $this->minDepth = $minDepth;
     $this->maxDepth = $maxDepth;
 }
 /**
  * Write font style
  *
  * @return string
  */
 protected function writeFontStyle()
 {
     if (!$this->fontStyle instanceof FontStyle) {
         return '';
     }
     /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */
     $parentWriter = $this->parentWriter;
     // Create style writer and set color/name index
     $styleWriter = new FontStyleWriter($this->fontStyle);
     if ($this->fontStyle->getColor() != null) {
         $colorIndex = array_search($this->fontStyle->getColor(), $parentWriter->getColorTable());
         if ($colorIndex !== false) {
             $styleWriter->setColorIndex($colorIndex + 1);
         }
     }
     if ($this->fontStyle->getName() != null) {
         $fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable());
         if ($fontIndex !== false) {
             $styleWriter->setNameIndex($fontIndex);
         }
     }
     // Write style
     $content = $styleWriter->write();
     return $content;
 }
Example #4
0
 /**
  * Create a new Table-of-Contents Element
  *
  * @param mixed $fontStyle
  * @param array $tocStyle
  * @param integer $minDepth
  * @param integer $maxDepth
  */
 public function __construct($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
 {
     $this->TOCStyle = new TOCStyle();
     if (!is_null($tocStyle) && is_array($tocStyle)) {
         foreach ($tocStyle as $key => $value) {
             $this->TOCStyle->setStyleValue($key, $value);
         }
     }
     if (!is_null($fontStyle)) {
         if (is_array($fontStyle)) {
             $this->fontStyle = new Font();
             foreach ($fontStyle as $key => $value) {
                 $this->fontStyle->setStyleValue($key, $value);
             }
         } else {
             $this->fontStyle = $fontStyle;
         }
     }
     $this->minDepth = $minDepth;
     $this->maxDepth = $maxDepth;
 }
Example #5
0
 /**
  * Test line height exception by using nonnumeric value
  *
  * @expectedException \PhpOffice\PhpWord\Exception\InvalidStyleException
  */
 public function testLineHeightException()
 {
     $object = new Font();
     $object->setLineHeight('a');
 }
Example #6
0
 /**
  * Write font style
  *
  * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  * @param string $styleName
  * @param \PhpOffice\PhpWord\Style\Font $style
  */
 private function writeFontStyle(XMLWriter $xmlWriter, $styleName, FontStyle $style)
 {
     $paragraphStyle = $style->getParagraph();
     $styleType = $style->getStyleType();
     $type = $styleType == 'title' ? 'paragraph' : 'character';
     if (!is_null($paragraphStyle)) {
         $type = 'paragraph';
     }
     $xmlWriter->startElement('w:style');
     $xmlWriter->writeAttribute('w:type', $type);
     // Heading style
     if ($styleType == 'title') {
         $arrStyle = explode('_', $styleName);
         $styleId = 'Heading' . $arrStyle[1];
         $styleName = 'heading ' . $arrStyle[1];
         $styleLink = 'Heading' . $arrStyle[1] . 'Char';
         $xmlWriter->writeAttribute('w:styleId', $styleId);
         $xmlWriter->startElement('w:link');
         $xmlWriter->writeAttribute('w:val', $styleLink);
         $xmlWriter->endElement();
     }
     // Style name
     $xmlWriter->startElement('w:name');
     $xmlWriter->writeAttribute('w:val', $styleName);
     $xmlWriter->endElement();
     // Parent style
     $xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal');
     // w:pPr
     if (!is_null($paragraphStyle)) {
         $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
         $styleWriter->write();
     }
     // w:rPr
     $styleWriter = new FontStyleWriter($xmlWriter, $style);
     $styleWriter->write();
     $xmlWriter->endElement();
 }
Example #7
0
 /**
  * Set style values and put it to static style collection
  *
  * @param string $styleName
  * @param Paragraph|Font|Table|Numbering $styleObject
  * @param array|null $styleValues
  */
 private static function setStyleValues($styleName, $styleObject, $styleValues = null)
 {
     if (!array_key_exists($styleName, self::$styles)) {
         if (!is_null($styleValues) && is_array($styleValues)) {
             foreach ($styleValues as $key => $value) {
                 $styleObject->setStyleValue($key, $value);
             }
         }
         $styleObject->setStyleName($styleName);
         $styleObject->setIndex(self::countStyles() + 1);
         // One based index
         self::$styles[$styleName] = $styleObject;
     }
 }