diff --git a/src/PhpWord/Shared/Converter.php b/src/PhpWord/Shared/Converter.php index c53f0030..a0cde503 100644 --- a/src/PhpWord/Shared/Converter.php +++ b/src/PhpWord/Shared/Converter.php @@ -217,6 +217,17 @@ class Converter return round($point / self::INCH_TO_POINT * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU); } + /** + * Convert point to cm + * + * @param int $point + * @return float + */ + public static function pointToCm($point = 1) + { + return $point / self::INCH_TO_POINT * self::INCH_TO_CM; + } + /** * Convert EMU to pixel * @@ -299,6 +310,7 @@ class Converter if ($value == '0') { return 0; } + $matches = array(); if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches)) { $size = $matches[1]; $unit = $matches[2]; @@ -332,4 +344,37 @@ class Converter { return self::pointToTwip(self::cssToPoint($value)); } + + /** + * Transforms a size in CSS format (eg. 10px, 10px, ...) to pixel + * + * @param string $value + * @return float + */ + public static function cssToPixel($value) + { + return self::pointToPixel(self::cssToPoint($value)); + } + + /** + * Transforms a size in CSS format (eg. 10px, 10px, ...) to cm + * + * @param string $value + * @return float + */ + public static function cssToCm($value) + { + return self::pointToCm(self::cssToPoint($value)); + } + + /** + * Transforms a size in CSS format (eg. 10px, 10px, ...) to emu + * + * @param string $value + * @return float + */ + public static function cssToEmu($value) + { + return self::pointToEmu(self::cssToPoint($value)); + } } diff --git a/tests/PhpWord/Shared/ConverterTest.php b/tests/PhpWord/Shared/ConverterTest.php index fbe92c25..15be8ec1 100644 --- a/tests/PhpWord/Shared/ConverterTest.php +++ b/tests/PhpWord/Shared/ConverterTest.php @@ -29,6 +29,7 @@ class ConverterTest extends \PHPUnit\Framework\TestCase */ public function testUnitConversions() { + $values = array(); $values[] = 0; // zero value $values[] = rand(1, 100) / 100; // fraction number $values[] = rand(1, 100); // integer @@ -79,6 +80,9 @@ class ConverterTest extends \PHPUnit\Framework\TestCase $result = Converter::pointToTwip($value); $this->assertEquals($value * 20, $result); + $result = Converter::pointToCm($value); + $this->assertEquals($value * 0.035277778, $result, '', 0.00001); + $result = Converter::pointToPixel($value); $this->assertEquals($value / 72 * 96, $result); @@ -105,6 +109,7 @@ class ConverterTest extends \PHPUnit\Framework\TestCase public function testHtmlToRGB() { // Prepare test values [ original, expected ] + $values = array(); $values[] = array('#FF99DD', array(255, 153, 221)); // With # $values[] = array('FF99DD', array(255, 153, 221)); // 6 characters $values[] = array('F9D', array(255, 153, 221)); // 3 characters