Skip to content

Commit 1a1eb0a

Browse files
committed
Fixed typing & Added conversion from points to EMU
1 parent da4c7ad commit 1a1eb0a

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/Common/Drawing.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class Drawing
2424
/**
2525
* Convert pixels to EMU
2626
*
27-
* @param int $pValue Value in pixels
27+
* @param float $pValue Value in pixels
2828
*
2929
* @return float
3030
*/
31-
public static function pixelsToEmu(int $pValue = 0): float
31+
public static function pixelsToEmu(float $pValue = 0): float
3232
{
3333
return round($pValue * 9525);
3434
}
@@ -38,15 +38,15 @@ public static function pixelsToEmu(int $pValue = 0): float
3838
*
3939
* @param int $pValue Value in EMU
4040
*
41-
* @return float
41+
* @return int
4242
*/
43-
public static function emuToPixels(int $pValue = 0): float
43+
public static function emuToPixels(int $pValue = 0): int
4444
{
4545
if ($pValue == 0) {
4646
return 0;
4747
}
4848

49-
return round($pValue / 9525);
49+
return (int) round($pValue / 9525);
5050
}
5151

5252
/**
@@ -96,11 +96,11 @@ public static function centimetersToPoints(float $pValue = 0): float
9696
/**
9797
* Convert points width to pixels
9898
*
99-
* @param int $pValue Value in points
99+
* @param float $pValue Value in points
100100
*
101101
* @return float
102102
*/
103-
public static function pointsToPixels(int $pValue = 0): float
103+
public static function pointsToPixels(float $pValue = 0): float
104104
{
105105
if ($pValue == 0) {
106106
return 0;
@@ -125,17 +125,17 @@ public static function pixelsToCentimeters(int $pValue = 0): float
125125
/**
126126
* Convert centimeters width to pixels
127127
*
128-
* @param int $pValue Value in centimeters
128+
* @param float $pValue Value in centimeters
129129
*
130-
* @return float
130+
* @return int
131131
*/
132-
public static function centimetersToPixels(int $pValue = 0): float
132+
public static function centimetersToPixels(float $pValue = 0): int
133133
{
134134
if ($pValue == 0) {
135135
return 0;
136136
}
137137

138-
return ($pValue / 2.54) * self::DPI_96;
138+
return (int) round((($pValue / 2.54) * self::DPI_96));
139139
}
140140

141141
/**
@@ -246,6 +246,22 @@ public static function twipsToPixels(int $pValue = 0): float
246246
return round($pValue / 15);
247247
}
248248

249+
/**
250+
* Convert points to emu
251+
*
252+
* @param float $pValue
253+
*
254+
* @return int
255+
*/
256+
public static function pointsToEmu(float $pValue = 0): int
257+
{
258+
if ($pValue == 0) {
259+
return 0;
260+
}
261+
262+
return (int) round(($pValue / 0.75) / 9525);
263+
}
264+
249265
/**
250266
* Convert HTML hexadecimal to RGB
251267
*

tests/Common/Tests/DrawingTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testPixelsCentimeters(): void
4343
$this->assertEquals(0, Drawing::pixelsToCentimeters());
4444
$this->assertEquals($value / Drawing::DPI_96 * 2.54, Drawing::pixelsToCentimeters($value));
4545
$this->assertEquals(0, Drawing::centimetersToPixels());
46-
$this->assertEquals($value / 2.54 * Drawing::DPI_96, Drawing::centimetersToPixels($value));
46+
$this->assertEquals(round($value / 2.54 * Drawing::DPI_96), Drawing::centimetersToPixels($value));
4747
}
4848

4949
public function testPixelsEMU(): void
@@ -74,6 +74,14 @@ public function testPointsCentimeters(): void
7474
$this->assertEquals($value / 0.75 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value));
7575
}
7676

77+
public function testPointsEmu(): void
78+
{
79+
$value = rand(1, 100);
80+
81+
$this->assertEquals(0, Drawing::pointsToEmu());
82+
$this->assertEquals(round($value / 0.75 / 9525), Drawing::pointsToEmu($value));
83+
}
84+
7785
public function testCentimetersPoints(): void
7886
{
7987
$this->assertEquals(0, Drawing::centimetersToPoints());

0 commit comments

Comments
 (0)