2014-04-26 11:14:27 +07:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* 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.
|
2014-04-26 11:14:27 +07:00
|
|
|
*
|
|
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-04-26 11:14:27 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
|
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
use PhpOffice\PhpWord\Settings;
|
2014-04-26 11:14:27 +07:00
|
|
|
use PhpOffice\PhpWord\Shared\XMLWriter;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Style writer
|
|
|
|
|
*
|
|
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
|
|
|
|
abstract class AbstractStyle
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* XML writer
|
|
|
|
|
*
|
|
|
|
|
* @var \PhpOffice\PhpWord\Shared\XMLWriter
|
|
|
|
|
*/
|
2014-05-07 09:47:02 +07:00
|
|
|
private $xmlWriter;
|
2014-04-26 11:14:27 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-05-07 09:47:02 +07:00
|
|
|
* Style; set protected for a while
|
2014-04-26 11:14:27 +07:00
|
|
|
*
|
|
|
|
|
* @var string|\PhpOffice\PhpWord\Style\AbstractStyle
|
|
|
|
|
*/
|
|
|
|
|
protected $style;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write style
|
|
|
|
|
*/
|
|
|
|
|
abstract public function write();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create new instance
|
|
|
|
|
*
|
2014-05-14 19:41:44 +07:00
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
2014-04-26 11:14:27 +07:00
|
|
|
* @param string|\PhpOffice\PhpWord\Style\AbstractStyle $style
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(XMLWriter $xmlWriter, $style = null)
|
|
|
|
|
{
|
|
|
|
|
$this->xmlWriter = $xmlWriter;
|
|
|
|
|
$this->style = $style;
|
|
|
|
|
}
|
2014-05-01 18:37:34 +07:00
|
|
|
|
2014-05-07 09:47:02 +07:00
|
|
|
/**
|
|
|
|
|
* Get XML Writer
|
|
|
|
|
*
|
|
|
|
|
* @return \PhpOffice\PhpWord\Shared\XMLWriter
|
|
|
|
|
*/
|
|
|
|
|
protected function getXmlWriter()
|
|
|
|
|
{
|
|
|
|
|
return $this->xmlWriter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Style
|
|
|
|
|
*
|
|
|
|
|
* @return \PhpOffice\PhpWord\Style\AbstractStyle
|
|
|
|
|
*/
|
|
|
|
|
protected function getStyle()
|
|
|
|
|
{
|
|
|
|
|
return $this->style;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
/**
|
|
|
|
|
* Convert twip value
|
|
|
|
|
*
|
|
|
|
|
* @param int|float $value
|
2014-05-19 00:14:14 +07:00
|
|
|
* @param int $default (int|float)
|
2014-05-01 18:37:34 +07:00
|
|
|
* @return int|float
|
|
|
|
|
*/
|
2014-05-02 13:39:56 +07:00
|
|
|
protected function convertTwip($value, $default = 0)
|
2014-05-01 18:37:34 +07:00
|
|
|
{
|
2014-05-19 00:14:14 +07:00
|
|
|
$factors = array(
|
2014-05-15 22:13:03 +07:00
|
|
|
Settings::UNIT_CM => 567,
|
|
|
|
|
Settings::UNIT_MM => 56.7,
|
|
|
|
|
Settings::UNIT_INCH => 1440,
|
|
|
|
|
Settings::UNIT_POINT => 20,
|
|
|
|
|
Settings::UNIT_PICA => 240,
|
|
|
|
|
);
|
2014-05-01 18:37:34 +07:00
|
|
|
$unit = Settings::getMeasurementUnit();
|
2014-05-19 00:14:14 +07:00
|
|
|
$factor = 1;
|
|
|
|
|
if (in_array($unit, $factors) && $value != $default) {
|
|
|
|
|
$factor = $factors[$unit];
|
2014-05-01 18:37:34 +07:00
|
|
|
}
|
2014-05-19 00:14:14 +07:00
|
|
|
|
|
|
|
|
return $value * $factor;
|
2014-05-01 18:37:34 +07:00
|
|
|
}
|
2014-06-02 21:21:34 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write child style
|
|
|
|
|
*
|
|
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
*/
|
|
|
|
|
protected function writeChildStyle(XMLWriter $xmlWriter, $name, $value)
|
|
|
|
|
{
|
|
|
|
|
if ($value !== null) {
|
|
|
|
|
$class = "PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\" . $name;
|
|
|
|
|
|
|
|
|
|
/** @var \PhpOffice\PhpWord\Writer\Word2007\Style\AbstractStyle $writer */
|
|
|
|
|
$writer = new $class($xmlWriter, $value);
|
|
|
|
|
$writer->write();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assemble style array into style string
|
|
|
|
|
*
|
|
|
|
|
* @param array $styles
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function assembleStyle($styles = array())
|
|
|
|
|
{
|
|
|
|
|
$style = '';
|
|
|
|
|
foreach ($styles as $key => $value) {
|
|
|
|
|
if (!is_null($value) && $value != '') {
|
|
|
|
|
$style .= "{$key}:{$value}; ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return trim($style);
|
|
|
|
|
}
|
2014-04-26 11:14:27 +07:00
|
|
|
}
|