Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.05% |
37 / 43 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Ruby | |
86.05% |
37 / 43 |
|
50.00% |
2 / 4 |
13.46 | |
0.00% |
0 / 1 |
| write | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| getPropertyCssForRubyTag | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
4.77 | |||
| getPropertyCssForRtTag | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParagraphStyleForTextRun | |
87.50% |
14 / 16 |
|
0.00% |
0 / 1 |
7.10 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of PHPWord - A pure PHP library for reading and writing |
| 5 | * word processing documents. |
| 6 | * |
| 7 | * PHPWord is free software distributed under the terms of the GNU Lesser |
| 8 | * General Public License version 3 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * For the full copyright and license information, please read the LICENSE |
| 11 | * file that was distributed with this source code. For the full list of |
| 12 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 13 | * |
| 14 | * @see https://github.com/PHPOffice/PHPWord |
| 15 | * |
| 16 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 17 | */ |
| 18 | |
| 19 | namespace PhpOffice\PhpWord\Writer\HTML\Element; |
| 20 | |
| 21 | use PhpOffice\PhpWord\ComplexType\RubyProperties; |
| 22 | use PhpOffice\PhpWord\Element\TextRun; |
| 23 | use PhpOffice\PhpWord\Style\Paragraph; |
| 24 | use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter; |
| 25 | |
| 26 | /** |
| 27 | * Ruby element HTML writer. |
| 28 | */ |
| 29 | class Ruby extends AbstractElement |
| 30 | { |
| 31 | /** |
| 32 | * Write text. |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | public function write() |
| 37 | { |
| 38 | // $this->processFontStyle(); |
| 39 | |
| 40 | /** @var \PhpOffice\PhpWord\Element\Ruby $element Type hint */ |
| 41 | $element = $this->element; |
| 42 | |
| 43 | $baseText = $this->parentWriter->escapeHTML($element->getBaseTextRun()->getText()); |
| 44 | $rubyText = $this->parentWriter->escapeHTML($element->getRubyTextRun()->getText()); |
| 45 | |
| 46 | $rubyTagPropertyCSS = $this->getPropertyCssForRubyTag($element->getProperties()); |
| 47 | $lang = $element->getProperties()->getLanguageId(); |
| 48 | $content = "<ruby {$this->getParagraphStyleForTextRun($element->getBaseTextRun(), $rubyTagPropertyCSS)} lang=\"{$lang}\">"; |
| 49 | $content .= $baseText; |
| 50 | $content .= ' <rp>(</rp>'; |
| 51 | $rtTagPropertyCSS = $this->getPropertyCssForRtTag($element->getProperties()); |
| 52 | $content .= "<rt {$this->getParagraphStyleForTextRun($element->getRubyTextRun(), $rtTagPropertyCSS)}>"; |
| 53 | $content .= $rubyText; |
| 54 | $content .= '</rt>'; |
| 55 | $content .= '<rp>)</rp>'; |
| 56 | $content .= '</ruby>'; |
| 57 | |
| 58 | return $content; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get property CSS for the <ruby> tag. |
| 63 | */ |
| 64 | private function getPropertyCssForRubyTag(RubyProperties $properties): string |
| 65 | { |
| 66 | // alignment CSS: https://developer.mozilla.org/en-US/docs/Web/CSS/ruby-align |
| 67 | $alignment = 'space-between'; |
| 68 | switch ($properties->getAlignment()) { |
| 69 | case RubyProperties::ALIGNMENT_CENTER: |
| 70 | $alignment = 'center'; |
| 71 | |
| 72 | break; |
| 73 | case RubyProperties::ALIGNMENT_LEFT: |
| 74 | $alignment = 'start'; |
| 75 | |
| 76 | break; |
| 77 | default: |
| 78 | $alignment = 'space-between'; |
| 79 | |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | return |
| 84 | 'font-size:' . $properties->getFontSizeForBaseText() . 'pt' . ';' . |
| 85 | 'ruby-align:' . $alignment . ';'; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get property CSS for the <rt> tag. |
| 90 | */ |
| 91 | private function getPropertyCssForRtTag(RubyProperties $properties): string |
| 92 | { |
| 93 | // alignment CSS: https://developer.mozilla.org/en-US/docs/Web/CSS/ruby-align |
| 94 | return 'font-size:' . $properties->getFontFaceSize() . 'pt' . ';'; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Write paragraph style for a given TextRun. |
| 99 | */ |
| 100 | private function getParagraphStyleForTextRun(TextRun $textRun, string $extraCSS): string |
| 101 | { |
| 102 | $style = ''; |
| 103 | $paragraphStyle = $textRun->getParagraphStyle(); |
| 104 | $pStyleIsObject = ($paragraphStyle instanceof Paragraph); |
| 105 | if ($pStyleIsObject) { |
| 106 | $styleWriter = new ParagraphStyleWriter($paragraphStyle); |
| 107 | $styleWriter->setParentWriter($this->parentWriter); |
| 108 | $style = $styleWriter->write(); |
| 109 | } elseif (is_string($paragraphStyle)) { |
| 110 | $style = $paragraphStyle; |
| 111 | } |
| 112 | if ($style !== null && $style !== '') { |
| 113 | if ($pStyleIsObject) { |
| 114 | // CSS pairs (style="...") |
| 115 | $style = " style=\"{$style}{$extraCSS}\""; |
| 116 | } else { |
| 117 | // class name; need to append extra styles manually |
| 118 | $style = " class=\"{$style}\" style=\"{$extraCSS}\""; |
| 119 | } |
| 120 | } elseif ($extraCSS !== '') { |
| 121 | $style = " style=\"{$extraCSS}\""; |
| 122 | } |
| 123 | |
| 124 | return $style; |
| 125 | } |
| 126 | } |