\PhpOffice\Common\Drawing -> \PhpOffice\PhpWord\Shared\Drawing

This commit is contained in:
Libor M 2021-01-01 16:15:24 +01:00
parent c52686c243
commit b656720619
3 changed files with 363 additions and 1 deletions

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader; namespace PhpOffice\PhpWord\Reader;
use PhpOffice\Common\Drawing;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Drawing;
use PhpOffice\PhpWord\Shared\OLERead; use PhpOffice\PhpWord\Shared\OLERead;
use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style;

View File

@ -0,0 +1,238 @@
<?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.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Shared;
/**
* Drawing
*/
class Drawing
{
const DPI_96 = 96;
/**
* Convert pixels to EMU
*
* @param int $pValue Value in pixels
* @return int
*/
public static function pixelsToEmu($pValue = 0)
{
return round($pValue * 9525);
}
/**
* Convert EMU to pixels
*
* @param int $pValue Value in EMU
* @return int
*/
public static function emuToPixels($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return round($pValue / 9525);
}
/**
* Convert pixels to points
*
* @param int $pValue Value in pixels
* @return float
*/
public static function pixelsToPoints($pValue = 0)
{
return $pValue * 0.67777777;
}
/**
* Convert points width to centimeters
*
* @param int $pValue Value in points
* @return float
*/
public static function pointsToCentimeters($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return ((($pValue * 1.333333333) / self::DPI_96) * 2.54);
}
/**
* Convert points width to pixels
*
* @param int $pValue Value in points
* @return float
*/
public static function pointsToPixels($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return $pValue * 1.333333333;
}
/**
* Convert pixels to centimeters
*
* @param int $pValue Value in pixels
* @return float
*/
public static function pixelsToCentimeters($pValue = 0)
{
//return $pValue * 0.028;
return (($pValue / self::DPI_96) * 2.54);
}
/**
* Convert centimeters width to pixels
*
* @param int $pValue Value in centimeters
* @return float
*/
public static function centimetersToPixels($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return ($pValue / 2.54) * self::DPI_96;
}
/**
* Convert degrees to angle
*
* @param int $pValue Degrees
* @return int
*/
public static function degreesToAngle($pValue = 0)
{
return (int) round($pValue * 60000);
}
/**
* Convert angle to degrees
*
* @param int $pValue Angle
* @return int
*/
public static function angleToDegrees($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return round($pValue / 60000);
}
/**
* Convert centimeters width to twips
*
* @param integer $pValue
* @return float
*/
public static function centimetersToTwips($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return $pValue * 566.928;
}
/**
* Convert twips width to centimeters
*
* @param integer $pValue
* @return float
*/
public static function twipsToCentimeters($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return $pValue / 566.928;
}
/**
* Convert inches width to twips
*
* @param integer $pValue
* @return float
*/
public static function inchesToTwips($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return $pValue * 1440;
}
/**
* Convert twips width to inches
*
* @param integer $pValue
* @return float
*/
public static function twipsToInches($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return $pValue / 1440;
}
/**
* Convert twips width to pixels
*
* @param integer $pValue
* @return float
*/
public static function twipsToPixels($pValue = 0)
{
if ($pValue == 0) {
return 0;
}
return round($pValue / 15.873984);
}
/**
* Convert HTML hexadecimal to RGB
*
* @param string $pValue HTML Color in hexadecimal
* @return array|false Value in RGB
*/
public static function htmlToRGB($pValue)
{
if ($pValue[0] == '#') {
$pValue = substr($pValue, 1);
}
if (strlen($pValue) == 6) {
list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]);
} elseif (strlen($pValue) == 3) {
list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]);
} else {
return false;
}
$colorR = hexdec($colorR);
$colorG = hexdec($colorG);
$colorB = hexdec($colorB);
return array($colorR, $colorG, $colorB);
}
}

View File

@ -0,0 +1,124 @@
<?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.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Shared;
/**
* Test class for PhpOffice\PhpWord\Shared\Drawing
*
* @coversDefaultClass PhpOffice\PhpWord\Shared\Drawing
*/
class DrawingTest extends \PHPUnit\Framework\TestCase
{
/**
*/
public function testDegreesAngle()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::degreesToAngle());
$this->assertEquals((int) round($value * 60000), Drawing::degreesToAngle($value));
$this->assertEquals(0, Drawing::angleToDegrees());
$this->assertEquals(round($value / 60000), Drawing::angleToDegrees($value));
}
/**
*/
public function testPixelsCentimeters()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::pixelsToCentimeters());
$this->assertEquals($value / Drawing::DPI_96 * 2.54, Drawing::pixelsToCentimeters($value));
$this->assertEquals(0, Drawing::centimetersToPixels());
$this->assertEquals($value / 2.54 * Drawing::DPI_96, Drawing::centimetersToPixels($value));
}
/**
*/
public function testPixelsEMU()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::pixelsToEmu());
$this->assertEquals(round($value*9525), Drawing::pixelsToEmu($value));
$this->assertEquals(0, Drawing::emuToPixels());
$this->assertEquals(round($value/9525), Drawing::emuToPixels($value));
}
/**
*/
public function testPixelsPoints()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::pixelsToPoints());
$this->assertEquals($value*0.67777777, Drawing::pixelsToPoints($value));
$this->assertEquals(0, Drawing::pointsToPixels());
$this->assertEquals($value* 1.333333333, Drawing::pointsToPixels($value));
}
/**
*/
public function testPointsCentimeters()
{
$value = rand(1, 100);
$this->assertEquals(0, Drawing::pointsToCentimeters());
$this->assertEquals($value * 1.333333333 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value));
}
/**
*/
public function testTwips()
{
$value = rand(1, 100);
// Centimeters
$this->assertEquals(0, Drawing::centimetersToTwips());
$this->assertEquals($value * 566.928, Drawing::centimetersToTwips($value));
$this->assertEquals(0, Drawing::twipsToCentimeters());
$this->assertEquals($value / 566.928, Drawing::twipsToCentimeters($value));
// Inches
$this->assertEquals(0, Drawing::inchesToTwips());
$this->assertEquals($value * 1440, Drawing::inchesToTwips($value));
$this->assertEquals(0, Drawing::twipsToInches());
$this->assertEquals($value / 1440, Drawing::twipsToInches($value));
// Pixels
$this->assertEquals(0, Drawing::twipsToPixels());
$this->assertEquals(round($value / 15.873984), Drawing::twipsToPixels($value));
}
public function testHTML()
{
$this->assertFalse(Drawing::htmlToRGB('0'));
$this->assertFalse(Drawing::htmlToRGB('00'));
$this->assertFalse(Drawing::htmlToRGB('0000'));
$this->assertFalse(Drawing::htmlToRGB('00000'));
$this->assertInternalType('array', Drawing::htmlToRGB('ABCDEF'));
$this->assertCount(3, Drawing::htmlToRGB('ABCDEF'));
$this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('ABCDEF'));
$this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('#ABCDEF'));
$this->assertEquals(array(0xAA, 0xBB, 0xCC), Drawing::htmlToRGB('ABC'));
$this->assertEquals(array(0xAA, 0xBB, 0xCC), Drawing::htmlToRGB('#ABC'));
}
}