Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
73 / 73 |
|
100.00% |
29 / 29 |
CRAP | |
100.00% |
1 / 1 |
Converter | |
100.00% |
73 / 73 |
|
100.00% |
29 / 29 |
58 | |
100.00% |
1 / 1 |
cmToTwip | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cmToInch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cmToPixel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cmToPoint | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cmToEmu | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
inchToTwip | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
inchToCm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
inchToPixel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
inchToPoint | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
inchToEmu | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pixelToTwip | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pixelToCm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pixelToPoint | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pixelToEmu | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pointToTwip | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pointToPixel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pointToEmu | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pointToCm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
emuToPixel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
picaToPoint | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
degreeToAngle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
angleToDegree | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
stringToRgb | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
16 | |||
htmlToRgb | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
7 | |||
cssToPoint | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
9 | |||
cssToTwip | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cssToPixel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cssToCm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cssToEmu | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord\Shared; |
19 | |
20 | /** |
21 | * Common converter functions. |
22 | */ |
23 | class Converter |
24 | { |
25 | const INCH_TO_CM = 2.54; |
26 | const INCH_TO_TWIP = 1440; |
27 | const INCH_TO_PIXEL = 96; |
28 | const INCH_TO_POINT = 72; |
29 | const INCH_TO_PICA = 6; |
30 | const PIXEL_TO_EMU = 9525; |
31 | const DEGREE_TO_ANGLE = 60000; |
32 | |
33 | /** |
34 | * Convert centimeter to twip. |
35 | * |
36 | * @param float $centimeter |
37 | * |
38 | * @return float |
39 | */ |
40 | public static function cmToTwip($centimeter = 1) |
41 | { |
42 | return $centimeter / self::INCH_TO_CM * self::INCH_TO_TWIP; |
43 | } |
44 | |
45 | /** |
46 | * Convert centimeter to inch. |
47 | * |
48 | * @param float $centimeter |
49 | * |
50 | * @return float |
51 | */ |
52 | public static function cmToInch($centimeter = 1) |
53 | { |
54 | return $centimeter / self::INCH_TO_CM; |
55 | } |
56 | |
57 | /** |
58 | * Convert centimeter to pixel. |
59 | * |
60 | * @param float $centimeter |
61 | * |
62 | * @return float |
63 | */ |
64 | public static function cmToPixel($centimeter = 1) |
65 | { |
66 | return $centimeter / self::INCH_TO_CM * self::INCH_TO_PIXEL; |
67 | } |
68 | |
69 | /** |
70 | * Convert centimeter to point. |
71 | * |
72 | * @param float $centimeter |
73 | * |
74 | * @return float |
75 | */ |
76 | public static function cmToPoint($centimeter = 1) |
77 | { |
78 | return $centimeter / self::INCH_TO_CM * self::INCH_TO_POINT; |
79 | } |
80 | |
81 | /** |
82 | * Convert centimeter to EMU. |
83 | * |
84 | * @param float $centimeter |
85 | * |
86 | * @return float |
87 | */ |
88 | public static function cmToEmu($centimeter = 1) |
89 | { |
90 | return round($centimeter / self::INCH_TO_CM * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU); |
91 | } |
92 | |
93 | /** |
94 | * Convert inch to twip. |
95 | * |
96 | * @param float $inch |
97 | * |
98 | * @return float |
99 | */ |
100 | public static function inchToTwip($inch = 1) |
101 | { |
102 | return $inch * self::INCH_TO_TWIP; |
103 | } |
104 | |
105 | /** |
106 | * Convert inch to centimeter. |
107 | * |
108 | * @param float $inch |
109 | * |
110 | * @return float |
111 | */ |
112 | public static function inchToCm($inch = 1) |
113 | { |
114 | return $inch * self::INCH_TO_CM; |
115 | } |
116 | |
117 | /** |
118 | * Convert inch to pixel. |
119 | * |
120 | * @param float $inch |
121 | * |
122 | * @return float |
123 | */ |
124 | public static function inchToPixel($inch = 1) |
125 | { |
126 | return $inch * self::INCH_TO_PIXEL; |
127 | } |
128 | |
129 | /** |
130 | * Convert inch to point. |
131 | * |
132 | * @param float $inch |
133 | * |
134 | * @return float |
135 | */ |
136 | public static function inchToPoint($inch = 1) |
137 | { |
138 | return $inch * self::INCH_TO_POINT; |
139 | } |
140 | |
141 | /** |
142 | * Convert inch to EMU. |
143 | * |
144 | * @param float $inch |
145 | * |
146 | * @return int |
147 | */ |
148 | public static function inchToEmu($inch = 1) |
149 | { |
150 | return round($inch * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU); |
151 | } |
152 | |
153 | /** |
154 | * Convert pixel to twip. |
155 | * |
156 | * @param float $pixel |
157 | * |
158 | * @return float |
159 | */ |
160 | public static function pixelToTwip($pixel = 1) |
161 | { |
162 | return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_TWIP; |
163 | } |
164 | |
165 | /** |
166 | * Convert pixel to centimeter. |
167 | * |
168 | * @param float $pixel |
169 | * |
170 | * @return float |
171 | */ |
172 | public static function pixelToCm($pixel = 1) |
173 | { |
174 | return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_CM; |
175 | } |
176 | |
177 | /** |
178 | * Convert pixel to point. |
179 | * |
180 | * @param float $pixel |
181 | * |
182 | * @return float |
183 | */ |
184 | public static function pixelToPoint($pixel = 1) |
185 | { |
186 | return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_POINT; |
187 | } |
188 | |
189 | /** |
190 | * Convert pixel to EMU. |
191 | * |
192 | * @param float $pixel |
193 | * |
194 | * @return int |
195 | */ |
196 | public static function pixelToEmu($pixel = 1) |
197 | { |
198 | return round($pixel * self::PIXEL_TO_EMU); |
199 | } |
200 | |
201 | /** |
202 | * Convert point to twip unit. |
203 | * |
204 | * @param float $point |
205 | * |
206 | * @return float |
207 | */ |
208 | public static function pointToTwip($point = 1) |
209 | { |
210 | return $point / self::INCH_TO_POINT * self::INCH_TO_TWIP; |
211 | } |
212 | |
213 | /** |
214 | * Convert point to pixel. |
215 | * |
216 | * @param float $point |
217 | * |
218 | * @return float |
219 | */ |
220 | public static function pointToPixel($point = 1) |
221 | { |
222 | return $point / self::INCH_TO_POINT * self::INCH_TO_PIXEL; |
223 | } |
224 | |
225 | /** |
226 | * Convert point to EMU. |
227 | * |
228 | * @param float $point |
229 | * |
230 | * @return float |
231 | */ |
232 | public static function pointToEmu($point = 1) |
233 | { |
234 | return round($point / self::INCH_TO_POINT * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU); |
235 | } |
236 | |
237 | /** |
238 | * Convert point to cm. |
239 | * |
240 | * @param float $point |
241 | * |
242 | * @return float |
243 | */ |
244 | public static function pointToCm($point = 1) |
245 | { |
246 | return $point / self::INCH_TO_POINT * self::INCH_TO_CM; |
247 | } |
248 | |
249 | /** |
250 | * Convert EMU to pixel. |
251 | * |
252 | * @param float $emu |
253 | * |
254 | * @return float |
255 | */ |
256 | public static function emuToPixel($emu = 1) |
257 | { |
258 | return round($emu / self::PIXEL_TO_EMU); |
259 | } |
260 | |
261 | /** |
262 | * Convert pica to point. |
263 | * |
264 | * @param float $pica |
265 | * |
266 | * @return float |
267 | */ |
268 | public static function picaToPoint($pica = 1) |
269 | { |
270 | return $pica / self::INCH_TO_PICA * self::INCH_TO_POINT; |
271 | } |
272 | |
273 | /** |
274 | * Convert degree to angle. |
275 | * |
276 | * @param float $degree |
277 | * |
278 | * @return int |
279 | */ |
280 | public static function degreeToAngle($degree = 1) |
281 | { |
282 | return (int) round($degree * self::DEGREE_TO_ANGLE); |
283 | } |
284 | |
285 | /** |
286 | * Convert angle to degrees. |
287 | * |
288 | * @param float $angle |
289 | * |
290 | * @return int |
291 | */ |
292 | public static function angleToDegree($angle = 1) |
293 | { |
294 | return round($angle / self::DEGREE_TO_ANGLE); |
295 | } |
296 | |
297 | /** |
298 | * Convert colorname as string to RGB. |
299 | * |
300 | * @param string $value color name |
301 | * |
302 | * @return string color as hex RGB string, or original value if unknown |
303 | */ |
304 | public static function stringToRgb($value) |
305 | { |
306 | switch ($value) { |
307 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW: |
308 | return 'FFFF00'; |
309 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGREEN: |
310 | return '90EE90'; |
311 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_CYAN: |
312 | return '00FFFF'; |
313 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_MAGENTA: |
314 | return 'FF00FF'; |
315 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLUE: |
316 | return '0000FF'; |
317 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_RED: |
318 | return 'FF0000'; |
319 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKBLUE: |
320 | return '00008B'; |
321 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKCYAN: |
322 | return '008B8B'; |
323 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGREEN: |
324 | return '006400'; |
325 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKMAGENTA: |
326 | return '8B008B'; |
327 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKRED: |
328 | return '8B0000'; |
329 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKYELLOW: |
330 | return '8B8B00'; |
331 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGRAY: |
332 | return 'A9A9A9'; |
333 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGRAY: |
334 | return 'D3D3D3'; |
335 | case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLACK: |
336 | return '000000'; |
337 | } |
338 | |
339 | return $value; |
340 | } |
341 | |
342 | /** |
343 | * Convert HTML hexadecimal to RGB. |
344 | * |
345 | * @param string $value HTML Color in hexadecimal |
346 | * |
347 | * @return array Value in RGB |
348 | */ |
349 | public static function htmlToRgb($value) |
350 | { |
351 | if ($value[0] == '#') { |
352 | $value = substr($value, 1); |
353 | } else { |
354 | $value = self::stringToRgb($value); |
355 | } |
356 | |
357 | if (strlen($value) == 6) { |
358 | [$red, $green, $blue] = [$value[0] . $value[1], $value[2] . $value[3], $value[4] . $value[5]]; |
359 | } elseif (strlen($value) == 3) { |
360 | [$red, $green, $blue] = [$value[0] . $value[0], $value[1] . $value[1], $value[2] . $value[2]]; |
361 | } else { |
362 | return false; |
363 | } |
364 | |
365 | $red = ctype_xdigit($red) ? hexdec($red) : 0; |
366 | $green = ctype_xdigit($green) ? hexdec($green) : 0; |
367 | $blue = ctype_xdigit($blue) ? hexdec($blue) : 0; |
368 | |
369 | return [$red, $green, $blue]; |
370 | } |
371 | |
372 | /** |
373 | * Transforms a size in CSS format (eg. 10px, 10px, ...) to points. |
374 | * |
375 | * @param string $value |
376 | * |
377 | * @return ?float |
378 | */ |
379 | public static function cssToPoint($value) |
380 | { |
381 | if ($value == '0') { |
382 | return 0; |
383 | } |
384 | $matches = []; |
385 | if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches)) { |
386 | $size = $matches[1]; |
387 | $unit = $matches[2]; |
388 | |
389 | switch ($unit) { |
390 | case 'pt': |
391 | return $size; |
392 | case 'px': |
393 | return self::pixelToPoint($size); |
394 | case 'cm': |
395 | return self::cmToPoint($size); |
396 | case 'mm': |
397 | return self::cmToPoint($size / 10); |
398 | case 'in': |
399 | return self::inchToPoint($size); |
400 | case 'pc': |
401 | return self::picaToPoint($size); |
402 | } |
403 | } |
404 | |
405 | return null; |
406 | } |
407 | |
408 | /** |
409 | * Transforms a size in CSS format (eg. 10px, 10px, ...) to twips. |
410 | * |
411 | * @param string $value |
412 | * |
413 | * @return float |
414 | */ |
415 | public static function cssToTwip($value) |
416 | { |
417 | return self::pointToTwip(self::cssToPoint($value)); |
418 | } |
419 | |
420 | /** |
421 | * Transforms a size in CSS format (eg. 10px, 10px, ...) to pixel. |
422 | * |
423 | * @param string $value |
424 | * |
425 | * @return float |
426 | */ |
427 | public static function cssToPixel($value) |
428 | { |
429 | return self::pointToPixel(self::cssToPoint($value)); |
430 | } |
431 | |
432 | /** |
433 | * Transforms a size in CSS format (eg. 10px, 10px, ...) to cm. |
434 | * |
435 | * @param string $value |
436 | * |
437 | * @return float |
438 | */ |
439 | public static function cssToCm($value) |
440 | { |
441 | return self::pointToCm(self::cssToPoint($value)); |
442 | } |
443 | |
444 | /** |
445 | * Transforms a size in CSS format (eg. 10px, 10px, ...) to emu. |
446 | * |
447 | * @param string $value |
448 | * |
449 | * @return float |
450 | */ |
451 | public static function cssToEmu($value) |
452 | { |
453 | return self::pointToEmu(self::cssToPoint($value)); |
454 | } |
455 | } |