Skip to content

Commit 1e93359

Browse files
committed
Merge pull request #8 from PHPOffice/develop
Version 0.2.3
2 parents 2acb9c6 + 498937f commit 1e93359

File tree

9 files changed

+439
-19
lines changed

9 files changed

+439
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@
2323

2424
### BugFix
2525
- Fixed "Class 'PhpOffice\Common\ZipArchive' not found in /src/Common/XMLReader.php on line 54"
26+
27+
## 0.2.3
28+
29+
### Features
30+
- Added missing features for supporting PHPWord

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.0
1+
0.2.3

src/Common/Drawing.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,101 @@ public static function angleToDegrees($pValue = 0)
142142
return 0;
143143
}
144144
}
145+
146+
/**
147+
* Convert centimeters width to twips
148+
*
149+
* @param integer $pValue
150+
*/
151+
public static function centimetersToTwips($pValue = 0)
152+
{
153+
if ($pValue != 0) {
154+
return $pValue * 566.928;
155+
} else {
156+
return 0;
157+
}
158+
}
159+
160+
/**
161+
* Convert twips width to centimeters
162+
*
163+
* @param integer $pValue
164+
*/
165+
public static function twipsToCentimeters($pValue = 0)
166+
{
167+
if ($pValue != 0) {
168+
return $pValue / 566.928;
169+
} else {
170+
return 0;
171+
}
172+
}
173+
174+
/**
175+
* Convert inches width to twips
176+
*
177+
* @param integer $pValue
178+
*/
179+
public static function inchesToTwips($pValue = 0)
180+
{
181+
if ($pValue != 0) {
182+
return $pValue * 1440;
183+
} else {
184+
return 0;
185+
}
186+
}
187+
188+
/**
189+
* Convert twips width to inches
190+
*
191+
* @param integer $pValue
192+
*/
193+
public static function twipsToInches($pValue = 0)
194+
{
195+
if ($pValue != 0) {
196+
return $pValue / 1440;
197+
} else {
198+
return 0;
199+
}
200+
}
201+
202+
/**
203+
* Convert twips width to pixels
204+
*
205+
* @param integer $pValue
206+
*/
207+
public static function twipsToPixels($pValue = 0)
208+
{
209+
if ($pValue != 0) {
210+
return round($pValue / 15.873984);
211+
} else {
212+
return 0;
213+
}
214+
}
215+
216+
/**
217+
* Convert HTML hexadecimal to RGB
218+
*
219+
* @param string $pValue HTML Color in hexadecimal
220+
* @return array Value in RGB
221+
*/
222+
public static function htmlToRGB($pValue)
223+
{
224+
if ($pValue[0] == '#') {
225+
$pValue = substr($pValue, 1);
226+
}
227+
228+
if (strlen($pValue) == 6) {
229+
list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]);
230+
} elseif (strlen($pValue) == 3) {
231+
list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]);
232+
} else {
233+
return false;
234+
}
235+
236+
$colorR = hexdec($colorR);
237+
$colorG = hexdec($colorG);
238+
$colorB = hexdec($colorB);
239+
240+
return array($colorR, $colorG, $colorB);
241+
}
145242
}

src/Common/Font.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,48 @@ public static function centimeterSizeToPixels($sizeInCm = 1)
5353
{
5454
return ($sizeInCm * 37.795275591);
5555
}
56+
57+
/**
58+
* Convert centimeter to twip
59+
*
60+
* @param int $sizeInCm
61+
* @return double
62+
*/
63+
public static function centimeterSizeToTwips($sizeInCm = 1)
64+
{
65+
return $sizeInCm / 2.54 * 1440;
66+
}
67+
68+
/**
69+
* Convert inch to twip
70+
*
71+
* @param int $sizeInInch
72+
* @return double
73+
*/
74+
public static function inchSizeToTwips($sizeInInch = 1)
75+
{
76+
return $sizeInInch * 1440;
77+
}
78+
79+
/**
80+
* Convert pixel to twip
81+
*
82+
* @param int $sizeInPixel
83+
* @return double
84+
*/
85+
public static function pixelSizeToTwips($sizeInPixel = 1)
86+
{
87+
return $sizeInPixel / 96 * 1440;
88+
}
89+
90+
/**
91+
* Calculate twip based on point size, used mainly for paragraph spacing
92+
*
93+
* @param integer $sizeInPoint Size in point
94+
* @return integer Size (in twips)
95+
*/
96+
public static function pointSizeToTwips($sizeInPoint = 1)
97+
{
98+
return $sizeInPoint / 72 * 1440;
99+
}
56100
}

src/Common/Text.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,137 @@ public static function chr($dec)
9696
}
9797
return '';
9898
}
99+
100+
/**
101+
* Convert from OpenXML escaped control character to PHP control character
102+
*
103+
* @param string $value Value to unescape
104+
* @return string
105+
*/
106+
public static function controlCharacterOOXML2PHP($value = '')
107+
{
108+
if (empty(self::$controlCharacters)) {
109+
self::buildControlCharacters();
110+
}
111+
112+
return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
113+
}
114+
115+
/**
116+
* Check if a string contains UTF-8 data
117+
*
118+
* @param string $value
119+
* @return boolean
120+
*/
121+
public static function isUTF8($value = '')
122+
{
123+
return $value === '' || preg_match('/^./su', $value) === 1;
124+
}
125+
126+
/**
127+
* Return UTF8 encoded value
128+
*
129+
* @param string $value
130+
* @return string
131+
*/
132+
public static function toUTF8($value = '')
133+
{
134+
if (!is_null($value) && !self::isUTF8($value)) {
135+
$value = utf8_encode($value);
136+
}
137+
138+
return $value;
139+
}
140+
141+
/**
142+
* Returns unicode from UTF8 text
143+
*
144+
* The function is splitted to reduce cyclomatic complexity
145+
*
146+
* @param string $text UTF8 text
147+
* @return string Unicode text
148+
* @since 0.11.0
149+
*/
150+
public static function toUnicode($text)
151+
{
152+
return self::unicodeToEntities(self::utf8ToUnicode($text));
153+
}
154+
155+
/**
156+
* Returns unicode array from UTF8 text
157+
*
158+
* @param string $text UTF8 text
159+
* @return array
160+
* @since 0.11.0
161+
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
162+
*/
163+
private static function utf8ToUnicode($text)
164+
{
165+
$unicode = array();
166+
$values = array();
167+
$lookingFor = 1;
168+
169+
// Gets unicode for each character
170+
for ($i = 0; $i < strlen($text); $i++) {
171+
$thisValue = ord($text[$i]);
172+
if ($thisValue < 128) {
173+
$unicode[] = $thisValue;
174+
} else {
175+
if (count($values) == 0) {
176+
$lookingFor = $thisValue < 224 ? 2 : 3;
177+
}
178+
$values[] = $thisValue;
179+
if (count($values) == $lookingFor) {
180+
if ($lookingFor == 3) {
181+
$number = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
182+
} else {
183+
$number = (($values[0] % 32) * 64) + ($values[1] % 64);
184+
}
185+
$unicode[] = $number;
186+
$values = array();
187+
$lookingFor = 1;
188+
}
189+
}
190+
}
191+
192+
return $unicode;
193+
}
194+
195+
/**
196+
* Returns entites from unicode array
197+
*
198+
* @param array $unicode
199+
* @return string
200+
* @since 0.11.0
201+
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
202+
*/
203+
private static function unicodeToEntities($unicode)
204+
{
205+
$entities = '';
206+
207+
foreach ($unicode as $value) {
208+
if ($value != 65279) {
209+
$entities .= $value > 127 ? '\uc0{\u' . $value . '}' : chr($value);
210+
}
211+
}
212+
213+
return $entities;
214+
}
215+
216+
/**
217+
* Return name without underscore for < 0.10.0 variable name compatibility
218+
*
219+
* @param string $value
220+
* @return string
221+
*/
222+
public static function removeUnderscorePrefix($value)
223+
{
224+
if (!is_null($value)) {
225+
if (substr($value, 0, 1) == '_') {
226+
$value = substr($value, 1);
227+
}
228+
}
229+
230+
return $value;
231+
}
99232
}

src/Common/XMLWriter.php

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class XMLWriter
5959
* @param int $pTemporaryStorage Temporary storage location
6060
* @param string $pTemporaryStorageDir Temporary storage folder
6161
*/
62-
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = './')
62+
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = './', $compatibility = false)
6363
{
6464
// Create internal XMLWriter
6565
$this->xmlWriter = new \XMLWriter();
@@ -75,8 +75,13 @@ public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTempora
7575
$this->xmlWriter->openUri($this->tempFileName);
7676
}
7777

78-
// Set default values
79-
$this->xmlWriter->setIndent(true);
78+
if ($compatibility) {
79+
$this->xmlWriter->setIndent(false);
80+
$this->xmlWriter->setIndentString('');
81+
} else {
82+
$this->xmlWriter->setIndent(true);
83+
$this->xmlWriter->setIndentString(' ');
84+
}
8085
}
8186

8287
/**
@@ -126,4 +131,66 @@ public function getData()
126131
return file_get_contents($this->tempFileName);
127132
}
128133
}
134+
135+
136+
/**
137+
* Write simple element and attribute(s) block
138+
*
139+
* There are two options:
140+
* 1. If the `$attributes` is an array, then it's an associative array of attributes
141+
* 2. If not, then it's a simple attribute-value pair
142+
*
143+
* @param string $element
144+
* @param string|array $attributes
145+
* @param string $value
146+
* @return void
147+
*/
148+
public function writeElementBlock($element, $attributes, $value = null)
149+
{
150+
$this->xmlWriter->startElement($element);
151+
if (!is_array($attributes)) {
152+
$attributes = array($attributes => $value);
153+
}
154+
foreach ($attributes as $attribute => $value) {
155+
$this->xmlWriter->writeAttribute($attribute, $value);
156+
}
157+
$this->xmlWriter->endElement();
158+
}
159+
160+
/**
161+
* Write element if ...
162+
*
163+
* @param bool $condition
164+
* @param string $element
165+
* @param string $attribute
166+
* @param mixed $value
167+
* @return void
168+
*/
169+
public function writeElementIf($condition, $element, $attribute = null, $value = null)
170+
{
171+
if ($condition == true) {
172+
if (is_null($attribute)) {
173+
$this->xmlWriter->writeElement($element, $value);
174+
} else {
175+
$this->xmlWriter->startElement($element);
176+
$this->xmlWriter->writeAttribute($attribute, $value);
177+
$this->xmlWriter->endElement();
178+
}
179+
}
180+
}
181+
182+
/**
183+
* Write attribute if ...
184+
*
185+
* @param bool $condition
186+
* @param string $attribute
187+
* @param mixed $value
188+
* @return void
189+
*/
190+
public function writeAttributeIf($condition, $attribute, $value)
191+
{
192+
if ($condition == true) {
193+
$this->xmlWriter->writeAttribute($attribute, $value);
194+
}
195+
}
129196
}

0 commit comments

Comments
 (0)