Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
84.44% |
38 / 45 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
Ruby | |
84.44% |
38 / 45 |
|
50.00% |
2 / 4 |
14.74 | |
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 | |
83.33% |
15 / 18 |
|
0.00% |
0 / 1 |
8.30 |
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 | if (!method_exists($textRun, 'getParagraphStyle')) { |
104 | return $style; |
105 | } |
106 | |
107 | $paragraphStyle = $textRun->getParagraphStyle(); |
108 | $pStyleIsObject = ($paragraphStyle instanceof Paragraph); |
109 | if ($pStyleIsObject) { |
110 | $styleWriter = new ParagraphStyleWriter($paragraphStyle); |
111 | $styleWriter->setParentWriter($this->parentWriter); |
112 | $style = $styleWriter->write(); |
113 | } elseif (is_string($paragraphStyle)) { |
114 | $style = $paragraphStyle; |
115 | } |
116 | if ($style !== null && $style !== '') { |
117 | if ($pStyleIsObject) { |
118 | // CSS pairs (style="...") |
119 | $style = " style=\"{$style}{$extraCSS}\""; |
120 | } else { |
121 | // class name; need to append extra styles manually |
122 | $style = " class=\"{$style}\" style=\"{$extraCSS}\""; |
123 | } |
124 | } elseif ($extraCSS !== '') { |
125 | $style = " style=\"{$extraCSS}\""; |
126 | } |
127 | |
128 | return $style; |
129 | } |
130 | } |