Skip to content

Commit 43d19c8

Browse files
committed
Merge pull request #1 from PHPOffice/develop
Version 0.1
2 parents 5b4482f + ac9e1b1 commit 43d19c8

File tree

18 files changed

+1517
-0
lines changed

18 files changed

+1517
-0
lines changed

src/Common/Autoloader.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* This file is part of PHPOffice Common
4+
*
5+
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
6+
* General Public License version 3 as published by the Free Software Foundation.
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code. For the full list of
10+
* contributors, visit https://github.com/PHPOffice/Common/contributors.
11+
*
12+
* @link https://github.com/PHPOffice/Common
13+
* @copyright 2009-2014 PHPOffice Common contributors
14+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15+
*/
16+
17+
namespace PhpOffice\Common;
18+
19+
/**
20+
* Autoloader
21+
*/
22+
class Autoloader
23+
{
24+
/** @const string */
25+
const NAMESPACE_PREFIX = 'PhpOffice\\Common\\';
26+
27+
/**
28+
* Register
29+
*
30+
* @return void
31+
*/
32+
public static function register()
33+
{
34+
spl_autoload_register(array(new self, 'autoload'));
35+
}
36+
37+
/**
38+
* Autoload
39+
*
40+
* @param string $class
41+
*/
42+
public static function autoload($class)
43+
{
44+
$prefixLength = strlen(self::NAMESPACE_PREFIX);
45+
if (0 === strncmp(self::NAMESPACE_PREFIX, $class, $prefixLength)) {
46+
$file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
47+
$file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
48+
if (file_exists($file)) {
49+
/** @noinspection PhpIncludeInspection Dynamic includes */
50+
require_once $file;
51+
}
52+
}
53+
}
54+
}

src/Common/Drawing.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* This file is part of PHPOffice Common
4+
*
5+
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
6+
* General Public License version 3 as published by the Free Software Foundation.
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code. For the full list of
10+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
11+
*
12+
* @link https://github.com/PHPOffice/Common
13+
* @copyright 2009-2014 PHPOffice Common contributors
14+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15+
*/
16+
17+
namespace PhpOffice\Common;
18+
19+
/**
20+
* \PhpOffice\Common\Drawing
21+
*/
22+
class Drawing
23+
{
24+
const DPI_96 = 96;
25+
26+
/**
27+
* Convert pixels to EMU
28+
*
29+
* @param int $pValue Value in pixels
30+
* @return int Value in EMU
31+
*/
32+
public static function pixelsToEmu($pValue = 0)
33+
{
34+
return round($pValue * 9525);
35+
}
36+
37+
/**
38+
* Convert EMU to pixels
39+
*
40+
* @param int $pValue Value in EMU
41+
* @return int Value in pixels
42+
*/
43+
public static function emuToPixels($pValue = 0)
44+
{
45+
if ($pValue != 0) {
46+
return round($pValue / 9525);
47+
} else {
48+
return 0;
49+
}
50+
}
51+
52+
/**
53+
* Convert pixels to points
54+
*
55+
* @param int $pValue Value in pixels
56+
* @return int Value in points
57+
*/
58+
public static function pixelsToPoints($pValue = 0)
59+
{
60+
return $pValue * 0.67777777;
61+
}
62+
63+
/**
64+
* Convert points width to centimeters
65+
*
66+
* @param int $pValue Value in points
67+
* @return int Value in centimeters
68+
*/
69+
public static function pointsToCentimeters($pValue = 0)
70+
{
71+
if ($pValue != 0) {
72+
return ((($pValue * 1.333333333) / self::DPI_96) * 2.54);
73+
} else {
74+
return 0;
75+
}
76+
}
77+
78+
/**
79+
* Convert points width to pixels
80+
*
81+
* @param int $pValue Value in points
82+
* @return int Value in pixels
83+
*/
84+
public static function pointsToPixels($pValue = 0)
85+
{
86+
if ($pValue != 0) {
87+
return $pValue * 1.333333333;
88+
} else {
89+
return 0;
90+
}
91+
}
92+
93+
/**
94+
* Convert pixels to centimeters
95+
*
96+
* @param int $pValue Value in pixels
97+
* @return int Value in centimeters
98+
*/
99+
public static function pixelsToCentimeters($pValue = 0)
100+
{
101+
//return $pValue * 0.028;
102+
return (($pValue / self::DPI_96) * 2.54);
103+
}
104+
105+
/**
106+
* Convert centimeters width to pixels
107+
*
108+
* @param int $pValue Value in centimeters
109+
* @return int Value in pixels
110+
*/
111+
public static function centimetersToPixels($pValue = 0)
112+
{
113+
if ($pValue != 0) {
114+
return ($pValue / 2.54) * self::DPI_96;
115+
} else {
116+
return 0;
117+
}
118+
}
119+
120+
/**
121+
* Convert degrees to angle
122+
*
123+
* @param int $pValue Degrees
124+
* @return int Angle
125+
*/
126+
public static function degreesToAngle($pValue = 0)
127+
{
128+
return (int) round($pValue * 60000);
129+
}
130+
131+
/**
132+
* Convert angle to degrees
133+
*
134+
* @param int $pValue Angle
135+
* @return int Degrees
136+
*/
137+
public static function angleToDegrees($pValue = 0)
138+
{
139+
if ($pValue != 0) {
140+
return round($pValue / 60000);
141+
} else {
142+
return 0;
143+
}
144+
}
145+
}

src/Common/File.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* This file is part of PHPOffice Common
4+
*
5+
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
6+
* General Public License version 3 as published by the Free Software Foundation.
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code. For the full list of
10+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
11+
*
12+
* @link https://github.com/PHPOffice/Common
13+
* @copyright 2009-2014 PHPOffice Common contributors
14+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15+
*/
16+
17+
namespace PhpOffice\Common;
18+
19+
/**
20+
* Drawing
21+
*/
22+
class File
23+
{
24+
/**
25+
* Verify if a file exists
26+
*
27+
* @param string $pFilename Filename
28+
* @return bool
29+
*/
30+
public static function fileExists($pFilename)
31+
{
32+
// Sick construction, but it seems that
33+
// file_exists returns strange values when
34+
// doing the original file_exists on ZIP archives...
35+
if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
36+
// Open ZIP file and verify if the file exists
37+
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
38+
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
39+
40+
$zip = new \ZipArchive();
41+
if ($zip->open($zipFile) === true) {
42+
$returnValue = ($zip->getFromName($archiveFile) !== false);
43+
$zip->close();
44+
45+
return $returnValue;
46+
} else {
47+
return false;
48+
}
49+
} else {
50+
// Regular file_exists
51+
return file_exists($pFilename);
52+
}
53+
}
54+
55+
/**
56+
* Returns canonicalized absolute pathname, also for ZIP archives
57+
*
58+
* @param string $pFilename
59+
* @return string
60+
*/
61+
public static function realpath($pFilename)
62+
{
63+
// Try using realpath()
64+
$returnValue = realpath($pFilename);
65+
66+
// Found something?
67+
if ($returnValue == '' || is_null($returnValue)) {
68+
$pathArray = explode('/', $pFilename);
69+
while (in_array('..', $pathArray) && $pathArray[0] != '..') {
70+
$numPathArray = count($pathArray);
71+
for ($i = 0; $i < $numPathArray; ++$i) {
72+
if ($pathArray[$i] == '..' && $i > 0) {
73+
unset($pathArray[$i]);
74+
unset($pathArray[$i - 1]);
75+
break;
76+
}
77+
}
78+
}
79+
$returnValue = implode('/', $pathArray);
80+
}
81+
82+
// Return
83+
return $returnValue;
84+
}
85+
}

src/Common/Font.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* This file is part of PHPOffice Common
4+
*
5+
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
6+
* General Public License version 3 as published by the Free Software Foundation.
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code. For the full list of
10+
* contributors, visit https://github.com/PHPOffice/Common/contributors.
11+
*
12+
* @link https://github.com/PHPOffice/Common
13+
* @copyright 2009-2014 PHPOffice Common contributors
14+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15+
*/
16+
17+
namespace PhpOffice\Common;
18+
19+
/**
20+
* Font
21+
*/
22+
class Font
23+
{
24+
/**
25+
* Calculate an (approximate) pixel size, based on a font points size
26+
*
27+
* @param int $fontSizeInPoints Font size (in points)
28+
* @return int Font size (in pixels)
29+
*/
30+
public static function fontSizeToPixels($fontSizeInPoints = 12)
31+
{
32+
return ((16 / 12) * $fontSizeInPoints);
33+
}
34+
35+
/**
36+
* Calculate an (approximate) pixel size, based on inch size
37+
*
38+
* @param int $sizeInInch Font size (in inch)
39+
* @return int Size (in pixels)
40+
*/
41+
public static function inchSizeToPixels($sizeInInch = 1)
42+
{
43+
return ($sizeInInch * 96);
44+
}
45+
46+
/**
47+
* Calculate an (approximate) pixel size, based on centimeter size
48+
*
49+
* @param int $sizeInCm Font size (in centimeters)
50+
* @return int Size (in pixels)
51+
*/
52+
public static function centimeterSizeToPixels($sizeInCm = 1)
53+
{
54+
return ($sizeInCm * 37.795275591);
55+
}
56+
}

0 commit comments

Comments
 (0)