Merge `Shared\Drawing` and `Shared\Font` into `Shared\Converter`

This commit is contained in:
Ivan Lanin 2014-06-14 07:19:00 +07:00
parent e9f8e889f8
commit 8c2b099dbc
18 changed files with 424 additions and 246 deletions

View File

@ -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

View File

@ -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));

View File

@ -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

View File

@ -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,
));

View File

@ -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,

View File

@ -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,

View File

@ -0,0 +1,245 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Shared;
/**
* Common converter functions
*/
class Converter
{
const INCH_TO_CM = 2.54;
const INCH_TO_TWIP = 1440;
const INCH_TO_PIXEL = 96;
const INCH_TO_POINT = 72;
const PIXEL_TO_EMU = 9525;
const DEGREE_TO_ANGLE = 60000;
/**
* Convert centimeter to twip
*
* @param int $centimeter
* @return float
*/
public static function cmToTwip($centimeter = 1)
{
return $centimeter / self::INCH_TO_CM * self::INCH_TO_TWIP;
}
/**
* Convert centimeter to inch
*
* @param int $centimeter
* @return float
*/
public static function cmToInch($centimeter = 1)
{
return $centimeter / self::INCH_TO_CM;
}
/**
* Convert centimeter to pixel
*
* @param int $centimeter
* @return float
*/
public static function cmToPixel($centimeter = 1)
{
return $centimeter / self::INCH_TO_CM * self::INCH_TO_PIXEL;
}
/**
* Convert inch to twip
*
* @param int $inch
* @return int
*/
public static function inchToTwip($inch = 1)
{
return $inch * self::INCH_TO_TWIP;
}
/**
* Convert inch to centimeter
*
* @param int $inch
* @return float
*/
public static function inchToCm($inch = 1)
{
return $inch * self::INCH_TO_CM;
}
/**
* Convert inch to pixel
*
* @param int $inch
* @return int
*/
public static function inchToPixel($inch = 1)
{
return $inch * self::INCH_TO_PIXEL;
}
/**
* Convert inch to point
*
* @param int $inch
* @return int
*/
public static function inchToPoint($inch = 1)
{
return $inch * self::INCH_TO_POINT;
}
/**
* Convert pixel to twip
*
* @param int $pixel
* @return int
*/
public static function pixelToTwip($pixel = 1)
{
return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_TWIP;
}
/**
* Convert pixel to centimeter
*
* @param int $pixel
* @return float
*/
public static function pixelToCm($pixel = 1)
{
return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_CM;
}
/**
* Convert pixel to point
*
* @param int $pixel
* @return float
*/
public static function pixelToPoint($pixel = 1)
{
return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_POINT;
}
/**
* Convert pixel to EMU
*
* @param int $pixel
* @return int
*/
public static function pixelToEmu($pixel = 1)
{
return round($pixel * self::PIXEL_TO_EMU);
}
/**
* Convert point to twip unit
*
* @param int $point
* @return int
*/
public static function pointToTwip($point = 1)
{
return $point / self::INCH_TO_POINT * self::INCH_TO_TWIP;
}
/**
* Convert point to pixel
*
* @param int $point
* @return float
*/
public static function pointToPixel($point = 1)
{
return $point / self::INCH_TO_POINT * self::INCH_TO_PIXEL;
}
/**
* Convert point to EMU
*
* @param int $point
* @return int
*/
public static function pointToEmu($point = 1)
{
return round($point / self::INCH_TO_POINT * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
}
/**
* Convert EMU to pixel
*
* @param int $emu
* @return int
*/
public static function emuToPixel($emu = 1)
{
return round($emu / self::PIXEL_TO_EMU);
}
/**
* Convert degree to angle
*
* @param int $degree
* @return int
*/
public static function degreeToAngle($degree = 1)
{
return (int)round($degree * self::DEGREE_TO_ANGLE);
}
/**
* Convert angle to degrees
*
* @param int $angle
* @return int
*/
public static function angleToDegree($angle = 1)
{
return round($angle / self::DEGREE_TO_ANGLE);
}
/**
* Convert HTML hexadecimal to RGB
*
* @param string $value HTML Color in hexadecimal
* @return array Value in RGB
*/
public static function htmlToRgb($value)
{
if ($value[0] == '#') {
$value = substr($value, 1);
}
if (strlen($value) == 6) {
list($red, $green, $blue) = array($value[0] . $value[1], $value[2] . $value[3], $value[4] . $value[5]);
} elseif (strlen($value) == 3) {
list($red, $green, $blue) = array($value[0] . $value[0], $value[1] . $value[1], $value[2] . $value[2]);
} else {
return false;
}
$red = hexdec($red);
$green = hexdec($green);
$blue = hexdec($blue);
return array($red, $green, $blue);
}
}

View File

@ -18,9 +18,12 @@
namespace PhpOffice\PhpWord\Shared;
/**
* Common drawing functions
* Common drawing functions; replaced by `Converter`
*
* @deprecated 0.12.0
* @codeCoverageIgnore
*/
class Drawing
class Drawing extends Converter
{
/**
* Convert pixels to EMU
@ -30,7 +33,7 @@ class Drawing
*/
public static function pixelsToEMU($value = 0)
{
return round($value * 9525);
return self::pixelToEmu($value);
}
/**
@ -41,11 +44,7 @@ class Drawing
*/
public static function emuToPixels($value = 0)
{
if ($value != 0) {
return round($value / 9525);
} else {
return 0;
}
return self::emuToPixel($value);
}
/**
@ -56,7 +55,7 @@ class Drawing
*/
public static function pixelsToPoints($value = 0)
{
return $value * 0.75;
return self::pixelToPoint($value);
}
/**
@ -67,11 +66,7 @@ class Drawing
*/
public static function pointsToPixels($value = 0)
{
if ($value != 0) {
return $value * 1.333333333;
} else {
return 0;
}
return self::pointToPixel($value);
}
/**
@ -82,7 +77,7 @@ class Drawing
*/
public static function degreesToAngle($value = 0)
{
return (integer)round($value * 60000);
return self::degreeToAngle($value);
}
/**
@ -93,11 +88,7 @@ class Drawing
*/
public static function angleToDegrees($value = 0)
{
if ($value != 0) {
return round($value / 60000);
} else {
return 0;
}
return self::angleToDegree($value);
}
/**
@ -108,7 +99,7 @@ class Drawing
*/
public static function pixelsToCentimeters($value = 0)
{
return $value * 0.026458333;
return self::pixelToCm($value);
}
/**
@ -119,37 +110,6 @@ class Drawing
*/
public static function centimetersToPixels($value = 0)
{
if ($value != 0) {
return $value / 0.026458333;
} else {
return 0;
}
}
/**
* Convert HTML hexadecimal to RGB
*
* @param string $value HTML Color in hexadecimal
* @return array Value in RGB
*/
public static function htmlToRGB($value)
{
if ($value[0] == '#') {
$value = substr($value, 1);
}
if (strlen($value) == 6) {
list($red, $green, $blue) = array($value[0] . $value[1], $value[2] . $value[3], $value[4] . $value[5]);
} elseif (strlen($value) == 3) {
list($red, $green, $blue) = array($value[0] . $value[0], $value[1] . $value[1], $value[2] . $value[2]);
} else {
return false;
}
$red = hexdec($red);
$green = hexdec($green);
$blue = hexdec($blue);
return array($red, $green, $blue);
return self::cmToPixel($value);
}
}

View File

@ -18,9 +18,12 @@
namespace PhpOffice\PhpWord\Shared;
/**
* Common font functions
* Common font functions; replaced by `Converter`
*
* @deprecated 0.12.0
* @codeCoverageIgnore
*/
class Font
class Font extends Converter
{
/**
* Calculate an (approximate) pixel size, based on a font points size
@ -30,7 +33,7 @@ class Font
*/
public static function fontSizeToPixels($fontSizeInPoints = 12)
{
return ((16 / 12) * $fontSizeInPoints);
return self::pointToPixel($fontSizeInPoints);
}
/**
@ -41,7 +44,7 @@ class Font
*/
public static function inchSizeToPixels($sizeInInch = 1)
{
return ($sizeInInch * 96);
return self::inchToPixel($sizeInInch);
}
/**
@ -52,7 +55,7 @@ class Font
*/
public static function centimeterSizeToPixels($sizeInCm = 1)
{
return ($sizeInCm * 37.795275591);
return self::cmToPixel($sizeInCm);
}
/**
@ -63,7 +66,7 @@ class Font
*/
public static function centimeterSizeToTwips($sizeInCm = 1)
{
return ($sizeInCm * 565.217);
return self::cmToTwip($sizeInCm);
}
/**
@ -74,7 +77,7 @@ class Font
*/
public static function inchSizeToTwips($sizeInInch = 1)
{
return self::centimeterSizeToTwips($sizeInInch * 2.54);
return self::inchToTwip($sizeInInch);
}
/**
@ -85,7 +88,7 @@ class Font
*/
public static function pixelSizeToTwips($sizeInPixel = 1)
{
return self::centimeterSizeToTwips($sizeInPixel / 37.795275591);
return self::pixelToTwip($sizeInPixel);
}
/**
@ -96,6 +99,6 @@ class Font
*/
public static function pointSizeToTwips($sizeInPoint = 1)
{
return ($sizeInPoint * 20);
return self::pointToTwip($sizeInPoint);
}
}

View File

@ -17,7 +17,7 @@
namespace PhpOffice\PhpWord\Writer\ODText\Element;
use PhpOffice\PhpWord\Shared\Drawing;
use PhpOffice\PhpWord\Shared\Converter;
/**
* Image element writer
@ -40,8 +40,8 @@ class Image extends AbstractElement
$mediaIndex = $element->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');

View File

@ -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 .= '}}';

View File

@ -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 .= '}';

View File

@ -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;

View File

@ -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());
}
}

View File

@ -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,

View File

@ -0,0 +1,108 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Shared;
use PhpOffice\PhpWord\Shared\Converter;
/**
* Test class for PhpOffice\PhpWord\Shared\Converter
*
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Converter
*/
class ConverterTest extends \PHPUnit_Framework_TestCase
{
/**
* Test unit conversion functions with various numbers
*/
public function testUnitConversions()
{
$values[] = 0; // zero value
$values[] = rand(1, 100) / 100; // fraction number
$values[] = rand(1, 100); // integer
foreach ($values as $value) {
$result = Converter::cmToTwip($value);
$this->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);
}
}
}

View File

@ -1,82 +0,0 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Shared;
use PhpOffice\PhpWord\Shared\Drawing;
/**
* Test class for PhpOffice\PhpWord\Shared\Drawing
*
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Drawing
* @runTestsInSeparateProcesses
*/
class DrawingTest extends \PHPUnit_Framework_TestCase
{
/**
* Test unit conversion functions with various numbers
*/
public function testUnitConversions()
{
$values[] = 0; // zero value
$values[] = rand(1, 100) / 100; // fraction number
$values[] = rand(1, 100); // integer
foreach ($values as $value) {
$result = Drawing::pixelsToEMU($value);
$this->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);
}
}
}

View File

@ -1,58 +0,0 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Shared;
use PhpOffice\PhpWord\Shared\Font;
/**
* Test class for PhpOffice\PhpWord\Shared\Font
*
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Font
* @runTestsInSeparateProcesses
*/
class FontTest extends \PHPUnit_Framework_TestCase
{
/**
* Test various conversions
*/
public function testConversions()
{
$original = 1;
$result = Font::fontSizeToPixels($original);
$this->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);
}
}