Exemplo n.º 1
0
 /**
  * Get font and paragraph styles
  */
 protected function getStyles()
 {
     /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */
     $parentWriter = $this->parentWriter;
     /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
     $element = $this->element;
     // Font style
     if (method_exists($element, 'getFontStyle')) {
         $this->fontStyle = $element->getFontStyle();
         if (is_string($this->fontStyle)) {
             $this->fontStyle = Style::getStyle($this->fontStyle);
         }
     }
     // Paragraph style
     if (method_exists($element, 'getParagraphStyle')) {
         $this->paragraphStyle = $element->getParagraphStyle();
         if (is_string($this->paragraphStyle)) {
             $this->paragraphStyle = Style::getStyle($this->paragraphStyle);
         }
         if ($this->paragraphStyle !== null && !$this->withoutP) {
             if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) {
                 $parentWriter->setLastParagraphStyle($element->getParagraphStyle());
             } else {
                 $parentWriter->setLastParagraphStyle();
                 $this->paragraphStyle = null;
             }
         } else {
             $parentWriter->setLastParagraphStyle();
             $this->paragraphStyle = null;
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Write element
  */
 public function write()
 {
     $rtfText = '';
     $fontStyle = $this->element->getFontStyle();
     if (is_string($fontStyle)) {
         $fontStyle = Style::getStyle($fontStyle);
     }
     $paragraphStyle = $this->element->getParagraphStyle();
     if (is_string($paragraphStyle)) {
         $paragraphStyle = Style::getStyle($paragraphStyle);
     }
     if ($paragraphStyle && !$this->withoutP) {
         if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
             $rtfText .= '\\pard\\nowidctlpar';
             if ($paragraphStyle->getSpaceAfter() != null) {
                 $rtfText .= '\\sa' . $paragraphStyle->getSpaceAfter();
             }
             if ($paragraphStyle->getAlign() != null) {
                 if ($paragraphStyle->getAlign() == 'center') {
                     $rtfText .= '\\qc';
                 }
             }
             $this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
         } else {
             $this->parentWriter->setLastParagraphStyle();
         }
     } else {
         $this->parentWriter->setLastParagraphStyle();
     }
     if ($fontStyle instanceof Font) {
         if ($fontStyle->getColor() != null) {
             $idxColor = array_search($fontStyle->getColor(), $this->parentWriter->getColorTable());
             if ($idxColor !== false) {
                 $rtfText .= '\\cf' . ($idxColor + 1);
             }
         } else {
             $rtfText .= '\\cf0';
         }
         if ($fontStyle->getName() != null) {
             $idxFont = array_search($fontStyle->getName(), $this->parentWriter->getFontTable());
             if ($idxFont !== false) {
                 $rtfText .= '\\f' . $idxFont;
             }
         } else {
             $rtfText .= '\\f0';
         }
         if ($fontStyle->getBold()) {
             $rtfText .= '\\b';
         }
         if ($fontStyle->getItalic()) {
             $rtfText .= '\\i';
         }
         if ($fontStyle->getSize()) {
             $rtfText .= '\\fs' . $fontStyle->getSize() * 2;
         }
     }
     if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) {
         $rtfText .= ' ';
     }
     $rtfText .= $this->element->getText();
     if ($fontStyle instanceof Font) {
         $rtfText .= '\\cf0';
         $rtfText .= '\\f0';
         if ($fontStyle->getBold()) {
             $rtfText .= '\\b0';
         }
         if ($fontStyle->getItalic()) {
             $rtfText .= '\\i0';
         }
         if ($fontStyle->getSize()) {
             $rtfText .= '\\fs' . PhpWord::DEFAULT_FONT_SIZE * 2;
         }
     }
     if (!$this->withoutP) {
         $rtfText .= '\\par' . PHP_EOL;
     }
     return $rtfText;
 }
Exemplo n.º 3
0
 /**
  * Test default paragraph style
  *
  * @covers ::setDefaultParagraphStyle
  * @test
  */
 public function testDefaultParagraphStyle()
 {
     $paragraph = array('alignment' => Jc::CENTER);
     Style::setDefaultParagraphStyle($paragraph);
     $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', Style::getStyle('Normal'));
 }
Exemplo n.º 4
0
 /**
  * Get all styles of each elements in container recursively
  *
  * Table style can be null or string of the style name
  *
  * @param \PhpOffice\PhpWord\Element\AbstractContainer $container
  * @param int &$paragraphStyleCount
  * @param int &$fontStyleCount
  * @return void
  * @todo Simplify the logic
  */
 private function getContainerStyle($container, &$paragraphStyleCount, &$fontStyleCount)
 {
     $elements = $container->getElements();
     foreach ($elements as $element) {
         if ($element instanceof TextRun) {
             $this->getContainerStyle($element, $paragraphStyleCount, $fontStyleCount);
         } elseif ($element instanceof Text) {
             $this->getElementStyle($element, $paragraphStyleCount, $fontStyleCount);
         } elseif ($element instanceof Image) {
             $style = $element->getStyle();
             $style->setStyleName('fr' . $element->getMediaIndex());
             $this->autoStyles['Image'][] = $style;
         } elseif ($element instanceof Table) {
             $style = $element->getStyle();
             if ($style === null) {
                 $style = new TableStyle();
             } elseif (is_string($style)) {
                 $style = Style::getStyle($style);
             }
             $style->setStyleName($element->getElementId());
             $this->autoStyles['Table'][] = $style;
         }
     }
 }
Exemplo n.º 5
0
 /**
  * Set default paragraph style
  */
 public function testDefaultParagraphStyle()
 {
     $paragraph = array('align' => 'center');
     Style::setDefaultParagraphStyle($paragraph);
     $this->assertInstanceOf("PhpOffice\\PhpWord\\Style\\Paragraph", Style::getStyle('Normal'));
 }
Exemplo n.º 6
0
    /**
     * Write numbering.
     *
     * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
     * @param array $numbering
     * @return void
     */
    private function writeNumbering(XMLWriter $xmlWriter, $numbering)
    {
        $numStyle = $numbering['style'];
        $numLevel = $numbering['level'];

        /** @var \PhpOffice\PhpWord\Style\Numbering $numbering */
        $numbering = Style::getStyle($numStyle);
        if ($numStyle !== null && $numbering !== null) {
            $xmlWriter->startElement('w:numPr');
            $xmlWriter->startElement('w:numId');
            $xmlWriter->writeAttribute('w:val', $numbering->getIndex());
            $xmlWriter->endElement(); // w:numId
            $xmlWriter->startElement('w:ilvl');
            $xmlWriter->writeAttribute('w:val', $numLevel);
            $xmlWriter->endElement(); // w:ilvl
            $xmlWriter->endElement(); // w:numPr

            $xmlWriter->startElement('w:outlineLvl');
            $xmlWriter->writeAttribute('w:val', $numLevel);
            $xmlWriter->endElement(); // w:outlineLvl
        }
    }
Exemplo n.º 7
0
 /**
  * Test add title style
  */
 public function testAddTitleStyle()
 {
     $phpWord = new PhpWord();
     $titleLevel = 1;
     $titleName = "Heading_{$titleLevel}";
     $phpWord->addTitleStyle($titleLevel, array());
     $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($titleName));
 }
Exemplo n.º 8
0
 /**
  * Get legacy numbering definition
  *
  * @return array
  * @since 0.10.0
  */
 private function getListTypeStyle()
 {
     // Check if legacy style already registered in global Style collection
     $numStyle = "PHPWordList{$this->listType}";
     if (Style::getStyle($numStyle) !== null) {
         $this->setNumStyle($numStyle);
         return;
     }
     // Property mapping for numbering level information
     $properties = array('start', 'format', 'text', 'alignment', 'tabPos', 'left', 'hanging', 'font', 'hint');
     // Legacy level information
     $listTypeStyles = array(self::TYPE_SQUARE_FILLED => array('type' => 'hybridMultilevel', 'levels' => array(0 => '1, bullet, , left, 720, 720, 360, Wingdings, default', 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default', 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default', 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default', 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default', 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default', 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default', 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default', 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default')), self::TYPE_BULLET_FILLED => array('type' => 'hybridMultilevel', 'levels' => array(0 => '1, bullet, , left, 720, 720, 360, Symbol, default', 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default', 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default', 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default', 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default', 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default', 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default', 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default', 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default')), self::TYPE_BULLET_EMPTY => array('type' => 'hybridMultilevel', 'levels' => array(0 => '1, bullet, o, left, 720, 720, 360, Courier New, default', 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default', 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default', 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default', 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default', 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default', 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default', 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default', 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default')), self::TYPE_NUMBER => array('type' => 'hybridMultilevel', 'levels' => array(0 => '1, decimal, %1., left, 720, 720, 360, , default', 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default', 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default', 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default', 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default', 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default', 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default', 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default', 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default')), self::TYPE_NUMBER_NESTED => array('type' => 'multilevel', 'levels' => array(0 => '1, decimal, %1., left, 360, 360, 360, , ', 1 => '1, decimal, %1.%2., left, 792, 792, 432, , ', 2 => '1, decimal, %1.%2.%3., left, 1224, 1224, 504, , ', 3 => '1, decimal, %1.%2.%3.%4., left, 1800, 1728, 648, , ', 4 => '1, decimal, %1.%2.%3.%4.%5., left, 2520, 2232, 792, , ', 5 => '1, decimal, %1.%2.%3.%4.%5.%6., left, 2880, 2736, 936, , ', 6 => '1, decimal, %1.%2.%3.%4.%5.%6.%7., left, 3600, 3240, 1080, , ', 7 => '1, decimal, %1.%2.%3.%4.%5.%6.%7.%8., left, 3960, 3744, 1224, , ', 8 => '1, decimal, %1.%2.%3.%4.%5.%6.%7.%8.%9., left, 4680, 4320, 1440, , ')), self::TYPE_ALPHANUM => array('type' => 'multilevel', 'levels' => array(0 => '1, decimal, %1., left, 720, 720, 360, , ', 1 => '1, lowerLetter, %2., left, 1440, 1440, 360, , ', 2 => '1, lowerRoman, %3., right, 2160, 2160, 180, , ', 3 => '1, decimal, %4., left, 2880, 2880, 360, , ', 4 => '1, lowerLetter, %5., left, 3600, 3600, 360, , ', 5 => '1, lowerRoman, %6., right, 4320, 4320, 180, , ', 6 => '1, decimal, %7., left, 5040, 5040, 360, , ', 7 => '1, lowerLetter, %8., left, 5760, 5760, 360, , ', 8 => '1, lowerRoman, %9., right, 6480, 6480, 180, , ')));
     // Populate style and register to global Style register
     $style = $listTypeStyles[$this->listType];
     foreach ($style['levels'] as $key => $value) {
         $level = array();
         $levelProperties = explode(', ', $value);
         $level['level'] = $key;
         for ($i = 0; $i < count($properties); $i++) {
             $property = $properties[$i];
             $level[$property] = $levelProperties[$i];
         }
         $style['levels'][$key] = $level;
     }
     Style::addNumberingStyle($numStyle, $style);
     $this->setNumStyle($numStyle);
 }