From 8c2b099dbc428ce6dfb473023f91b472ce5659ab Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 14 Jun 2014 07:19:00 +0700 Subject: [PATCH] Merge `Shared\Drawing` and `Shared\Font` into `Shared\Converter` --- CHANGELOG.md | 3 + docs/general.rst | 6 +- docs/src/documentation.md | 6 +- samples/Sample_08_ParagraphPagination.php | 2 +- samples/Sample_13_Images.php | 12 +- samples/Sample_29_Line.php | 20 +- src/PhpWord/Shared/Converter.php | 245 ++++++++++++++++++ src/PhpWord/Shared/Drawing.php | 66 +---- src/PhpWord/Shared/Font.php | 21 +- src/PhpWord/Writer/ODText/Element/Image.php | 6 +- src/PhpWord/Writer/RTF/Element/Image.php | 6 +- src/PhpWord/Writer/RTF/Part/Header.php | 4 +- .../Word2007/Element/AbstractElement.php | 1 - tests/PhpWord/Tests/Element/LineTest.php | 16 +- tests/PhpWord/Tests/Element/TextBoxTest.php | 8 +- tests/PhpWord/Tests/Shared/ConverterTest.php | 108 ++++++++ tests/PhpWord/Tests/Shared/DrawingTest.php | 82 ------ tests/PhpWord/Tests/Shared/FontTest.php | 58 ----- 18 files changed, 424 insertions(+), 246 deletions(-) create mode 100644 src/PhpWord/Shared/Converter.php create mode 100644 tests/PhpWord/Tests/Shared/ConverterTest.php delete mode 100644 tests/PhpWord/Tests/Shared/DrawingTest.php delete mode 100644 tests/PhpWord/Tests/Shared/FontTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ebfd3263..5290ccb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This release added drawing shapes (arc, curve, line, polyline, rect, oval) eleme - RTF Writer: Support for sections, margins, and borders - @ivanlanin GH-249 - Section: Ability to set paper size, e.g. A4, A3, and Legal - @ivanlanin GH-249 - General: New `PhpWord::save()` method to encapsulate `IOFactory` - @ivanlanin +- General: New `Shared\Converter` static class - @ivanlanin ### Bugfixes @@ -25,6 +26,8 @@ This release added drawing shapes (arc, curve, line, polyline, rect, oval) eleme - `Element\Link::getTarget()` replaced by `Element\Link::getSource()` - `Element\Section::getSettings()` and `Element\Section::setSettings()` replaced by `Element\Section::getStyle()` and `Element\Section::setStyle()` +- `Shared\Drawing` and `Shared\Font` merged into `Shared\Converter` + ### Miscellaneous diff --git a/docs/general.rst b/docs/general.rst index 60d9b609..a2498aff 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -130,13 +130,13 @@ points to twips. // Paragraph with 6 points space after $phpWord->addParagraphStyle('My Style', array( - 'spaceAfter' => \PhpOffice\PhpWord\Shared\Font::pointSizeToTwips(6)) + 'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6)) ); $section = $phpWord->addSection(); $sectionStyle = $section->getStyle(); // half inch left margin - $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Font::inchSizeToTwips(.5)); + $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); // 2 cm right margin - $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Font::centimeterSizeToTwips(2)); + $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); diff --git a/docs/src/documentation.md b/docs/src/documentation.md index 49505c7f..6ad2f67a 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -301,15 +301,15 @@ You can use PHPWord helper functions to convert inches, centimeters, or points t ```php // Paragraph with 6 points space after $phpWord->addParagraphStyle('My Style', array( - 'spaceAfter' => \PhpOffice\PhpWord\Shared\Font::pointSizeToTwips(6)) + 'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6)) ); $section = $phpWord->addSection(); $sectionStyle = $section->getStyle(); // half inch left margin -$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Font::inchSizeToTwips(.5)); +$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); // 2 cm right margin -$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Font::centimeterSizeToTwips(2)); +$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); ``` # Containers diff --git a/samples/Sample_08_ParagraphPagination.php b/samples/Sample_08_ParagraphPagination.php index d3f0c1fb..f3914758 100644 --- a/samples/Sample_08_ParagraphPagination.php +++ b/samples/Sample_08_ParagraphPagination.php @@ -6,7 +6,7 @@ echo date('H:i:s') , " Create new PhpWord object" , EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord->setDefaultParagraphStyle(array( 'align' => 'both', - 'spaceAfter' => \PhpOffice\PhpWord\Shared\Font::pointSizeToTwips(12), + 'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(12), 'spacing' => 120, )); diff --git a/samples/Sample_13_Images.php b/samples/Sample_13_Images.php index dd0c8801..29b3e733 100644 --- a/samples/Sample_13_Images.php +++ b/samples/Sample_13_Images.php @@ -37,14 +37,14 @@ $section->addText('Absolute positioning: see top right corner of page'); $section->addImage( 'resources/_mars.jpg', array( - 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(3), - 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(3), + 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3), + 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3), 'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE, 'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_RIGHT, 'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE, 'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE, - 'marginLeft' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(15.5), - 'marginTop' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(1.55) + 'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(15.5), + 'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.55) ) ); @@ -55,8 +55,8 @@ $section->addText('Vertical position top relative to line'); $section->addImage( 'resources/_mars.jpg', array( - 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(3), - 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(3), + 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3), + 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3), 'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE, 'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER, 'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN, diff --git a/samples/Sample_29_Line.php b/samples/Sample_29_Line.php index 5a955702..f9162c2a 100644 --- a/samples/Sample_29_Line.php +++ b/samples/Sample_29_Line.php @@ -13,16 +13,16 @@ $section = $phpWord->addSection(); $section->addText('Horizontal Line (Inline style):'); $section->addLine( array( - 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(4), - 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(0), + 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(4), + 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0), 'positioning' => 'absolute' ) ); $section->addText('Vertical Line (Inline style):'); $section->addLine( array( - 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(0), - 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(1), + 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0), + 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1), 'positioning' => 'absolute' ) ); @@ -32,13 +32,13 @@ $section->addTextBreak(1); $section->addText('Positioned Line (red):'); $section->addLine( array( - 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(4), - 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(1), + 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(4), + 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1), 'positioning' => 'absolute', 'posHorizontalRel' => 'page', 'posVerticalRel' => 'page', - 'marginLeft' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(10), - 'marginTop' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(8), + 'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(10), + 'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(8), 'wrappingStyle' => \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_SQUARE, 'color' => 'red' ) @@ -47,8 +47,8 @@ $section->addLine( $section->addText('Horizontal Formatted Line'); $section->addLine( array( - 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(15), - 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(0), + 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(15), + 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0), 'positioning' => 'absolute', 'beginArrow' => \PhpOffice\PhpWord\Style\Line::ARROW_STYLE_BLOCK, 'endArrow' => \PhpOffice\PhpWord\Style\Line::ARROW_STYLE_OVAL, diff --git a/src/PhpWord/Shared/Converter.php b/src/PhpWord/Shared/Converter.php new file mode 100644 index 00000000..8bc1cecf --- /dev/null +++ b/src/PhpWord/Shared/Converter.php @@ -0,0 +1,245 @@ +getMediaIndex(); $target = 'Pictures/' . $element->getTarget(); $style = $element->getStyle(); - $width = Drawing::pixelsToCentimeters($style->getWidth()); - $height = Drawing::pixelsToCentimeters($style->getHeight()); + $width = Converter::pixelToCm($style->getWidth()); + $height = Converter::pixelToCm($style->getHeight()); $xmlWriter->startElement('text:p'); $xmlWriter->writeAttribute('text:style-name', 'Standard'); diff --git a/src/PhpWord/Writer/RTF/Element/Image.php b/src/PhpWord/Writer/RTF/Element/Image.php index 1daae2a0..52e705e9 100644 --- a/src/PhpWord/Writer/RTF/Element/Image.php +++ b/src/PhpWord/Writer/RTF/Element/Image.php @@ -18,7 +18,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Element; use PhpOffice\PhpWord\Element\Image as ImageElement; -use PhpOffice\PhpWord\Shared\Font; +use PhpOffice\PhpWord\Shared\Converter; /** * Image element RTF writer @@ -45,8 +45,8 @@ class Image extends AbstractElement $content .= $this->writeOpening(); $content .= '{\*\shppict {\pict'; $content .= '\pngblip\picscalex100\picscaley100'; - $content .= '\picwgoal' . round(Font::pixelSizeToTwips($style->getWidth())); - $content .= '\pichgoal' . round(Font::pixelSizeToTwips($style->getHeight())); + $content .= '\picwgoal' . round(Converter::pixelToTwip($style->getWidth())); + $content .= '\pichgoal' . round(Converter::pixelToTwip($style->getHeight())); $content .= PHP_EOL; $content .= $this->element->getImageStringData(); $content .= '}}'; diff --git a/src/PhpWord/Writer/RTF/Part/Header.php b/src/PhpWord/Writer/RTF/Part/Header.php index 4d8d3a27..68cf1803 100644 --- a/src/PhpWord/Writer/RTF/Part/Header.php +++ b/src/PhpWord/Writer/RTF/Part/Header.php @@ -18,7 +18,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Part; use PhpOffice\PhpWord\Settings; -use PhpOffice\PhpWord\Shared\Drawing; +use PhpOffice\PhpWord\Shared\Converter; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; @@ -151,7 +151,7 @@ class Header extends AbstractPart $content .= '{'; $content .= '\colortbl;'; foreach ($this->colorTable as $color) { - list($red, $green, $blue) = Drawing::htmlToRGB($color); + list($red, $green, $blue) = Converter::htmlToRgb($color); $content .= "\\red{$red}\\green{$green}\\blue{$blue};"; } $content .= '}'; diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index 1cbf213b..b98b983d 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\PhpWord\Element\AbstractElement as Element; -use PhpOffice\PhpWord\Element\PageBreak as PageBreakElement; use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\XMLWriter; diff --git a/tests/PhpWord/Tests/Element/LineTest.php b/tests/PhpWord/Tests/Element/LineTest.php index 429f9df6..5add9a6b 100644 --- a/tests/PhpWord/Tests/Element/LineTest.php +++ b/tests/PhpWord/Tests/Element/LineTest.php @@ -37,17 +37,17 @@ class LineTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Line', $oLine); $this->assertEquals($oLine->getStyle(), null); } - + /** * Get style name */ public function testStyleText() { $oLine = new Line('lineStyle'); - + $this->assertEquals($oLine->getStyle(), 'lineStyle'); } - + /** * Get style array */ @@ -55,14 +55,14 @@ class LineTest extends \PHPUnit_Framework_TestCase { $oLine = new Line( array( - 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(14), - 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(4), + 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(14), + 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(4), 'positioning' => 'absolute', 'posHorizontalRel' => 'page', 'posVerticalRel' => 'page', 'flip' => true, - 'marginLeft' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(5), - 'marginTop' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(3), + 'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(5), + 'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3), 'wrappingStyle' => \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_SQUARE, 'beginArrow' => \PhpOffice\PhpWord\Style\Line::ARROW_STYLE_BLOCK, 'endArrow' => \PhpOffice\PhpWord\Style\Line::ARROW_STYLE_OVAL, @@ -70,7 +70,7 @@ class LineTest extends \PHPUnit_Framework_TestCase 'weight' => 10 ) ); - + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Line', $oLine->getStyle()); } } diff --git a/tests/PhpWord/Tests/Element/TextBoxTest.php b/tests/PhpWord/Tests/Element/TextBoxTest.php index c3c89ed4..2c6da465 100644 --- a/tests/PhpWord/Tests/Element/TextBoxTest.php +++ b/tests/PhpWord/Tests/Element/TextBoxTest.php @@ -55,11 +55,11 @@ class TextBoxTest extends \PHPUnit_Framework_TestCase { $oTextBox = new TextBox( array( - 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(4.5), - 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(17.5), + 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.5), + 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(17.5), 'positioning' => 'absolute', - 'marginLeft' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(15.4), - 'marginTop' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(9.9), + 'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(15.4), + 'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(9.9), 'stroke' => 0, 'innerMargin' => 0, 'borderSize' => 1, diff --git a/tests/PhpWord/Tests/Shared/ConverterTest.php b/tests/PhpWord/Tests/Shared/ConverterTest.php new file mode 100644 index 00000000..9525148c --- /dev/null +++ b/tests/PhpWord/Tests/Shared/ConverterTest.php @@ -0,0 +1,108 @@ +assertEquals($value / 2.54 * 1440, $result); + + $result = Converter::cmToInch($value); + $this->assertEquals($value / 2.54, $result); + + $result = Converter::cmToPixel($value); + $this->assertEquals($value / 2.54 * 96, $result); + + $result = Converter::inchToTwip($value); + $this->assertEquals($value * 1440, $result); + + $result = Converter::inchToCm($value); + $this->assertEquals($value * 2.54, $result); + + $result = Converter::inchToPixel($value); + $this->assertEquals($value * 96, $result); + + $result = Converter::inchToPoint($value); + $this->assertEquals($value * 72, $result); + + $result = Converter::pixelToTwip($value); + $this->assertEquals($value / 96 * 1440, $result); + + $result = Converter::pixelToCm($value); + $this->assertEquals($value / 96 * 2.54, $result); + + $result = Converter::pixelToPoint($value); + $this->assertEquals($value / 96 * 72, $result); + + $result = Converter::pixelToEMU($value); + $this->assertEquals(round($value * 9525), $result); + + $result = Converter::pointToTwip($value); + $this->assertEquals($value * 20, $result); + + $result = Converter::pointToPixel($value); + $this->assertEquals($value / 72 * 96, $result); + + $result = Converter::pointToEMU($value); + $this->assertEquals(round($value / 72 * 96 * 9525), $result); + + $result = Converter::emuToPixel($value); + $this->assertEquals(round($value / 9525), $result); + + $result = Converter::degreeToAngle($value); + $this->assertEquals((int)round($value * 60000), $result); + + $result = Converter::angleToDegree($value); + $this->assertEquals(round($value / 60000), $result); + } + } + + /** + * Test htmlToRGB() + */ + public function testHtmlToRGB() + { + // Prepare test values [ original, expected ] + $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 + $values[] = array('0F9D', false); // 4 characters + // Conduct test + foreach ($values as $value) { + $result = Converter::htmlToRGB($value[0]); + $this->assertEquals($value[1], $result); + } + } +} diff --git a/tests/PhpWord/Tests/Shared/DrawingTest.php b/tests/PhpWord/Tests/Shared/DrawingTest.php deleted file mode 100644 index 25fd3a38..00000000 --- a/tests/PhpWord/Tests/Shared/DrawingTest.php +++ /dev/null @@ -1,82 +0,0 @@ -assertEquals(round($value * 9525), $result); - - $result = Drawing::emuToPixels($value); - $this->assertEquals(round($value / 9525), $result); - - $result = Drawing::pixelsToPoints($value); - $this->assertEquals($value * 0.75, $result); - - $result = Drawing::pointsToPixels($value); - $this->assertEquals($value * 1.333333333, $result); - - $result = Drawing::degreesToAngle($value); - $this->assertEquals((int)round($value * 60000), $result); - - $result = Drawing::angleToDegrees($value); - $this->assertEquals(round($value / 60000), $result); - - $result = Drawing::pixelsToCentimeters($value); - $this->assertEquals($value * 0.026458333, $result); - - $result = Drawing::centimetersToPixels($value); - $this->assertEquals($value / 0.026458333, $result); - } - } - - /** - * Test htmlToRGB() - */ - public function testHtmlToRGB() - { - // Prepare test values [ original, expected ] - $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 - $values[] = array('0F9D', false); // 4 characters - // Conduct test - foreach ($values as $value) { - $result = Drawing::htmlToRGB($value[0]); - $this->assertEquals($value[1], $result); - } - } -} diff --git a/tests/PhpWord/Tests/Shared/FontTest.php b/tests/PhpWord/Tests/Shared/FontTest.php deleted file mode 100644 index c10f7739..00000000 --- a/tests/PhpWord/Tests/Shared/FontTest.php +++ /dev/null @@ -1,58 +0,0 @@ -assertEquals($original * 16 / 12, $result); - - $result = Font::inchSizeToPixels($original); - $this->assertEquals($original * 96, $result); - - $result = Font::centimeterSizeToPixels($original); - $this->assertEquals($original * 37.795275591, $result); - - $result = Font::centimeterSizeToTwips($original); - $this->assertEquals($original * 565.217, $result); - - $result = Font::inchSizeToTwips($original); - $this->assertEquals($original * 565.217 * 2.54, $result); - - $result = Font::pixelSizeToTwips($original); - $this->assertEquals($original * 565.217 / 37.795275591, $result); - - $result = Font::pointSizeToTwips($original); - $this->assertEquals($original * 20, $result); - } -}