Skip to content

Commit d8d697c

Browse files
committed
added convertors with test
1 parent a4b5320 commit d8d697c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/PhpWord/Shared/Converter.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ public static function pointToEmu($point = 1)
217217
return round($point / self::INCH_TO_POINT * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
218218
}
219219

220+
/**
221+
* Convert point to cm
222+
*
223+
* @param int $point
224+
* @return float
225+
*/
226+
public static function pointToCm($point = 1)
227+
{
228+
return $point / self::INCH_TO_POINT * self::INCH_TO_CM;
229+
}
230+
220231
/**
221232
* Convert EMU to pixel
222233
*
@@ -299,6 +310,7 @@ public static function cssToPoint($value)
299310
if ($value == '0') {
300311
return 0;
301312
}
313+
$matches = array();
302314
if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches)) {
303315
$size = $matches[1];
304316
$unit = $matches[2];
@@ -332,4 +344,37 @@ public static function cssToTwip($value)
332344
{
333345
return self::pointToTwip(self::cssToPoint($value));
334346
}
347+
348+
/**
349+
* Transforms a size in CSS format (eg. 10px, 10px, ...) to pixel
350+
*
351+
* @param string $value
352+
* @return float
353+
*/
354+
public static function cssToPixel($value)
355+
{
356+
return self::pointToPixel(self::cssToPoint($value));
357+
}
358+
359+
/**
360+
* Transforms a size in CSS format (eg. 10px, 10px, ...) to cm
361+
*
362+
* @param string $value
363+
* @return float
364+
*/
365+
public static function cssToCm($value)
366+
{
367+
return self::pointToCm(self::cssToPoint($value));
368+
}
369+
370+
/**
371+
* Transforms a size in CSS format (eg. 10px, 10px, ...) to emu
372+
*
373+
* @param string $value
374+
* @return float
375+
*/
376+
public static function cssToEmu($value)
377+
{
378+
return self::pointToEmu(self::cssToPoint($value));
379+
}
335380
}

tests/PhpWord/Shared/ConverterTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ConverterTest extends \PHPUnit\Framework\TestCase
2929
*/
3030
public function testUnitConversions()
3131
{
32+
$values = array();
3233
$values[] = 0; // zero value
3334
$values[] = rand(1, 100) / 100; // fraction number
3435
$values[] = rand(1, 100); // integer
@@ -79,6 +80,9 @@ public function testUnitConversions()
7980
$result = Converter::pointToTwip($value);
8081
$this->assertEquals($value * 20, $result);
8182

83+
$result = Converter::pointToCm($value);
84+
$this->assertEquals($value * 0.035277778, $result, '', 0.00001);
85+
8286
$result = Converter::pointToPixel($value);
8387
$this->assertEquals($value / 72 * 96, $result);
8488

@@ -105,6 +109,7 @@ public function testUnitConversions()
105109
public function testHtmlToRGB()
106110
{
107111
// Prepare test values [ original, expected ]
112+
$values = array();
108113
$values[] = array('#FF99DD', array(255, 153, 221)); // With #
109114
$values[] = array('FF99DD', array(255, 153, 221)); // 6 characters
110115
$values[] = array('F9D', array(255, 153, 221)); // 3 characters

0 commit comments

Comments
 (0)