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