Skip to content

Commit

Permalink
Merge pull request #8 from PHPOffice/develop
Browse files Browse the repository at this point in the history
Version 0.2.3
  • Loading branch information
Progi1984 committed Nov 8, 2015
2 parents 2acb9c6 + 498937f commit 1e93359
Show file tree
Hide file tree
Showing 9 changed files with 439 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@

### BugFix
- Fixed "Class 'PhpOffice\Common\ZipArchive' not found in /src/Common/XMLReader.php on line 54"

## 0.2.3

### Features
- Added missing features for supporting PHPWord
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.2.3
97 changes: 97 additions & 0 deletions src/Common/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,101 @@ public static function angleToDegrees($pValue = 0)
return 0;
}
}

/**
* Convert centimeters width to twips
*
* @param integer $pValue
*/
public static function centimetersToTwips($pValue = 0)
{
if ($pValue != 0) {
return $pValue * 566.928;
} else {
return 0;
}
}

/**
* Convert twips width to centimeters
*
* @param integer $pValue
*/
public static function twipsToCentimeters($pValue = 0)
{
if ($pValue != 0) {
return $pValue / 566.928;
} else {
return 0;
}
}

/**
* Convert inches width to twips
*
* @param integer $pValue
*/
public static function inchesToTwips($pValue = 0)
{
if ($pValue != 0) {
return $pValue * 1440;
} else {
return 0;
}
}

/**
* Convert twips width to inches
*
* @param integer $pValue
*/
public static function twipsToInches($pValue = 0)
{
if ($pValue != 0) {
return $pValue / 1440;
} else {
return 0;
}
}

/**
* Convert twips width to pixels
*
* @param integer $pValue
*/
public static function twipsToPixels($pValue = 0)
{
if ($pValue != 0) {
return round($pValue / 15.873984);
} else {
return 0;
}
}

/**
* Convert HTML hexadecimal to RGB
*
* @param string $pValue HTML Color in hexadecimal
* @return array 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);
}
}
44 changes: 44 additions & 0 deletions src/Common/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,48 @@ public static function centimeterSizeToPixels($sizeInCm = 1)
{
return ($sizeInCm * 37.795275591);
}

/**
* Convert centimeter to twip
*
* @param int $sizeInCm
* @return double
*/
public static function centimeterSizeToTwips($sizeInCm = 1)
{
return $sizeInCm / 2.54 * 1440;
}

/**
* Convert inch to twip
*
* @param int $sizeInInch
* @return double
*/
public static function inchSizeToTwips($sizeInInch = 1)
{
return $sizeInInch * 1440;
}

/**
* Convert pixel to twip
*
* @param int $sizeInPixel
* @return double
*/
public static function pixelSizeToTwips($sizeInPixel = 1)
{
return $sizeInPixel / 96 * 1440;
}

/**
* Calculate twip based on point size, used mainly for paragraph spacing
*
* @param integer $sizeInPoint Size in point
* @return integer Size (in twips)
*/
public static function pointSizeToTwips($sizeInPoint = 1)
{
return $sizeInPoint / 72 * 1440;
}
}
133 changes: 133 additions & 0 deletions src/Common/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,137 @@ public static function chr($dec)
}
return '';
}

/**
* Convert from OpenXML escaped control character to PHP control character
*
* @param string $value Value to unescape
* @return string
*/
public static function controlCharacterOOXML2PHP($value = '')
{
if (empty(self::$controlCharacters)) {
self::buildControlCharacters();
}

return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
}

/**
* Check if a string contains UTF-8 data
*
* @param string $value
* @return boolean
*/
public static function isUTF8($value = '')
{
return $value === '' || preg_match('/^./su', $value) === 1;
}

/**
* Return UTF8 encoded value
*
* @param string $value
* @return string
*/
public static function toUTF8($value = '')
{
if (!is_null($value) && !self::isUTF8($value)) {
$value = utf8_encode($value);
}

return $value;
}

/**
* Returns unicode from UTF8 text
*
* The function is splitted to reduce cyclomatic complexity
*
* @param string $text UTF8 text
* @return string Unicode text
* @since 0.11.0
*/
public static function toUnicode($text)
{
return self::unicodeToEntities(self::utf8ToUnicode($text));
}

/**
* Returns unicode array from UTF8 text
*
* @param string $text UTF8 text
* @return array
* @since 0.11.0
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
*/
private static function utf8ToUnicode($text)
{
$unicode = array();
$values = array();
$lookingFor = 1;

// Gets unicode for each character
for ($i = 0; $i < strlen($text); $i++) {
$thisValue = ord($text[$i]);
if ($thisValue < 128) {
$unicode[] = $thisValue;
} else {
if (count($values) == 0) {
$lookingFor = $thisValue < 224 ? 2 : 3;
}
$values[] = $thisValue;
if (count($values) == $lookingFor) {
if ($lookingFor == 3) {
$number = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
} else {
$number = (($values[0] % 32) * 64) + ($values[1] % 64);
}
$unicode[] = $number;
$values = array();
$lookingFor = 1;
}
}
}

return $unicode;
}

/**
* Returns entites from unicode array
*
* @param array $unicode
* @return string
* @since 0.11.0
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
*/
private static function unicodeToEntities($unicode)
{
$entities = '';

foreach ($unicode as $value) {
if ($value != 65279) {
$entities .= $value > 127 ? '\uc0{\u' . $value . '}' : chr($value);
}
}

return $entities;
}

/**
* Return name without underscore for < 0.10.0 variable name compatibility
*
* @param string $value
* @return string
*/
public static function removeUnderscorePrefix($value)
{
if (!is_null($value)) {
if (substr($value, 0, 1) == '_') {
$value = substr($value, 1);
}
}

return $value;
}
}
73 changes: 70 additions & 3 deletions src/Common/XMLWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class XMLWriter
* @param int $pTemporaryStorage Temporary storage location
* @param string $pTemporaryStorageDir Temporary storage folder
*/
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = './')
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = './', $compatibility = false)
{
// Create internal XMLWriter
$this->xmlWriter = new \XMLWriter();
Expand All @@ -75,8 +75,13 @@ public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTempora
$this->xmlWriter->openUri($this->tempFileName);
}

// Set default values
$this->xmlWriter->setIndent(true);
if ($compatibility) {
$this->xmlWriter->setIndent(false);
$this->xmlWriter->setIndentString('');
} else {
$this->xmlWriter->setIndent(true);
$this->xmlWriter->setIndentString(' ');
}
}

/**
Expand Down Expand Up @@ -126,4 +131,66 @@ public function getData()
return file_get_contents($this->tempFileName);
}
}


/**
* Write simple element and attribute(s) block
*
* There are two options:
* 1. If the `$attributes` is an array, then it's an associative array of attributes
* 2. If not, then it's a simple attribute-value pair
*
* @param string $element
* @param string|array $attributes
* @param string $value
* @return void
*/
public function writeElementBlock($element, $attributes, $value = null)
{
$this->xmlWriter->startElement($element);
if (!is_array($attributes)) {
$attributes = array($attributes => $value);
}
foreach ($attributes as $attribute => $value) {
$this->xmlWriter->writeAttribute($attribute, $value);
}
$this->xmlWriter->endElement();
}

/**
* Write element if ...
*
* @param bool $condition
* @param string $element
* @param string $attribute
* @param mixed $value
* @return void
*/
public function writeElementIf($condition, $element, $attribute = null, $value = null)
{
if ($condition == true) {
if (is_null($attribute)) {
$this->xmlWriter->writeElement($element, $value);
} else {
$this->xmlWriter->startElement($element);
$this->xmlWriter->writeAttribute($attribute, $value);
$this->xmlWriter->endElement();
}
}
}

/**
* Write attribute if ...
*
* @param bool $condition
* @param string $attribute
* @param mixed $value
* @return void
*/
public function writeAttributeIf($condition, $attribute, $value)
{
if ($condition == true) {
$this->xmlWriter->writeAttribute($attribute, $value);
}
}
}
Loading

0 comments on commit 1e93359

Please sign in to comment.