Merge pull request #240 from ivanlanin/config

Close #200
This commit is contained in:
Ivan Lanin 2014-05-17 01:46:44 +07:00
commit 710e147668
28 changed files with 293 additions and 161 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ Thumbs.db
Desktop.ini
composer.phar
phpunit.xml
phpword.ini
/.buildpath
/.idea
/.project

View File

@ -52,7 +52,7 @@ script:
## PHPUnit
- phpunit -c ./ --coverage-text --coverage-html ./build/coverage
## PHPDocumentor
- vendor/bin/phpdoc.php -d ./src -t ./build/docs --ignore "*/src/PhpWord/Shared/PCLZip/*" --template="responsive-twig"
- vendor/bin/phpdoc.php -d ./src -t ./build/docs --ignore "*/src/PhpWord/Shared/*/*" --template="responsive-twig"
after_script:
## PHPDocumentor

View File

@ -19,6 +19,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237
- RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196
- ListItemRun: Remove fontStyle parameter because ListItemRun is inherited from TextRun and TextRun doesn't have fontStyle - @ivanlanin
- Config: Ability to use a config file to store various common settings - @ivanlanin GH-200
### Bugfixes

View File

@ -10,7 +10,7 @@
syntaxCheck="false">
<testsuites>
<testsuite name="PhpWord Test Suite">
<directory>./tests/PhpWord/</directory>
<directory>./tests/PhpWord</directory>
</testsuite>
</testsuites>
<filter>
@ -18,13 +18,11 @@
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix=".php">./src/PhpWord/Shared/PCLZip</directory>
<directory suffix=".php">./src/PhpWord/Shared/Spyc</directory>
</exclude>
</whitelist>
</filter>
<logging>
<!--
For http://phpoffice.github.io/PHPWord/coverage/ and Scrutinizer
-->
<log type="coverage-html" target="./build/coverage" charset="UTF-8" highlight="true" />
<log type="coverage-clover" target="./build/logs/clover.xml" />
</logging>

14
phpword.ini.dist Normal file
View File

@ -0,0 +1,14 @@
; Default config file for PHPWord
; Copy this file into phpword.ini and use Settings::loadConfig to load
[General]
compatibility = true
zipClass = ZipArchive
pdfRendererName = DomPDF
pdfRendererPath =
[Font]
defaultFontName = Arial
defaultFontSize = 10

View File

@ -2,6 +2,10 @@
/**
* Header file
*/
use PhpOffice\PhpWord\Autoloader;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\IOFactory;
error_reporting(E_ALL);
define('CLI', (PHP_SAPI == 'cli') ? true : false);
define('EOL', CLI ? PHP_EOL : '<br />');
@ -9,16 +13,14 @@ define('SCRIPT_FILENAME', basename($_SERVER['SCRIPT_FILENAME'], '.php'));
define('IS_INDEX', SCRIPT_FILENAME == 'index');
require_once '../src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();
Autoloader::register();
Settings::loadConfig();
// Set writers
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf');
// Set PDF renderer
$rendererName = \PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF;
$rendererLibraryPath = ''; // DomPDF library path
if (!\PhpOffice\PhpWord\Settings::setPdfRenderer($rendererName, $rendererLibraryPath)) {
if (Settings::getPdfRendererPath() === null) {
$writers['PDF'] = null;
}
@ -60,7 +62,7 @@ function write($phpWord, $filename, $writers)
foreach ($writers as $writer => $extension) {
$result .= date('H:i:s') . " Write to {$writer} format";
if (!is_null($extension)) {
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer);
$xmlWriter = IOFactory::createWriter($phpWord, $writer);
$xmlWriter->save("{$filename}.{$extension}");
rename("{$filename}.{$extension}", "results/{$filename}.{$extension}");
} else {

View File

@ -22,24 +22,22 @@ use PhpOffice\PhpWord\Collection\Footnotes;
use PhpOffice\PhpWord\Collection\Titles;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Style;
/**
* PHPWord main class
*/
class PhpWord
{
const DEFAULT_FONT_COLOR = '000000'; // HEX
const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
const DEFAULT_FONT_NAME = 'Arial';
/**
* Default font size, in points.
* Default font settings
*
* OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
* use, and the conversion will be conducted during XML writing.
* @const string|int
* @deprecated 0.11.0 Use Settings constants
*/
const DEFAULT_FONT_SIZE = 10;
const DEFAULT_FONT_NAME = Settings::DEFAULT_FONT_NAME;
const DEFAULT_FONT_SIZE = Settings::DEFAULT_FONT_SIZE;
const DEFAULT_FONT_COLOR = Settings::DEFAULT_FONT_COLOR;
const DEFAULT_FONT_CONTENT_TYPE = Settings::DEFAULT_FONT_CONTENT_TYPE;
/**
* Document properties object
@ -76,19 +74,6 @@ class PhpWord
*/
private $endnotes;
/**
* Default font name
*
* @var string
*/
private $defaultFontName;
/**
* Default font size
* @var int
*/
private $defaultFontSize;
/**
* Create new
*/
@ -98,8 +83,6 @@ class PhpWord
$this->titles = new Titles();
$this->footnotes = new Footnotes();
$this->endnotes = new Endnotes();
$this->defaultFontName = self::DEFAULT_FONT_NAME;
$this->defaultFontSize = self::DEFAULT_FONT_SIZE;
}
/**
@ -220,7 +203,7 @@ class PhpWord
*/
public function getDefaultFontName()
{
return $this->defaultFontName;
return Settings::getDefaultFontName();
}
/**
@ -230,7 +213,7 @@ class PhpWord
*/
public function setDefaultFontName($fontName)
{
$this->defaultFontName = $fontName;
Settings::setDefaultFontName($fontName);
}
/**
@ -240,7 +223,7 @@ class PhpWord
*/
public function getDefaultFontSize()
{
return $this->defaultFontSize;
return Settings::getDefaultFontSize();
}
/**
@ -250,7 +233,7 @@ class PhpWord
*/
public function setDefaultFontSize($fontSize)
{
$this->defaultFontSize = $fontSize;
Settings::setDefaultFontSize($fontSize);
}
/**

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader\ODText;
use PhpOffice\PhpWord\Shared\XMLReader;
use PhpOffice\PhpWord\Reader\Word2007\AbstractPart as Word2007AbstractPart;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Abstract part reader

View File

@ -48,14 +48,25 @@ class Settings
* - Indentation: left, right, firstLine, hanging
* - Spacing: before, after
*
* @const int|float
* @const string
*/
const UNIT_TWIP = 1; // = 1/20 point
const UNIT_CM = 567;
const UNIT_MM = 56.7;
const UNIT_INCH = 1440;
const UNIT_POINT = 20; // = 1/72 inch
const UNIT_PICA = 240; // = 1/6 inch = 12 points
const UNIT_TWIP = 'twip'; // = 1/20 point
const UNIT_CM = 'cm';
const UNIT_MM = 'mm';
const UNIT_INCH = 'inch';
const UNIT_POINT = 'point'; // = 1/72 inch
const UNIT_PICA = 'pica'; // = 1/6 inch = 12 points
/**
* Default font settings
*
* OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
* use, and the conversion will be conducted during XML writing.
*/
const DEFAULT_FONT_NAME = 'Arial';
const DEFAULT_FONT_SIZE = 10;
const DEFAULT_FONT_COLOR = '000000';
const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
/**
* Compatibility option for XMLWriter
@ -71,13 +82,6 @@ class Settings
*/
private static $zipClass = self::ZIPARCHIVE;
/**
* Name of the classes used for PDF renderer
*
* @var array
*/
private static $pdfRenderers = array(self::PDF_RENDERER_DOMPDF);
/**
* Name of the external Library used for rendering PDF files
*
@ -99,6 +103,19 @@ class Settings
*/
private static $measurementUnit = self::UNIT_TWIP;
/**
* Default font name
*
* @var string
*/
private static $defaultFontName = self::DEFAULT_FONT_NAME;
/**
* Default font size
* @var int
*/
private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
/**
* Return the compatibility option used by the XMLWriter
*
@ -119,12 +136,10 @@ class Settings
*/
public static function setCompatibility($compatibility)
{
if (is_bool($compatibility)) {
self::$xmlWriterCompatibility = $compatibility;
return true;
}
$compatibility = (bool)$compatibility;
self::$xmlWriterCompatibility = $compatibility;
return false;
return true;
}
/**
@ -145,8 +160,7 @@ class Settings
*/
public static function setZipClass($zipClass)
{
if (($zipClass === self::PCLZIP) ||
($zipClass === self::ZIPARCHIVE)) {
if (in_array($zipClass, array(self::PCLZIP, self::ZIPARCHIVE))) {
self::$zipClass = $zipClass;
return true;
}
@ -186,7 +200,8 @@ class Settings
*/
public static function setPdfRendererName($libraryName)
{
if (!in_array($libraryName, self::$pdfRenderers)) {
$pdfRenderers = array(self::PDF_RENDERER_DOMPDF);
if (!in_array($libraryName, $pdfRenderers)) {
return false;
}
self::$pdfRendererName = $libraryName;
@ -222,7 +237,7 @@ class Settings
/**
* Get measurement unit
*
* @return int|float
* @return string
*/
public static function getMeasurementUnit()
{
@ -232,7 +247,7 @@ class Settings
/**
* Set measurement unit
*
* @param int|float $value
* @param string $value
* @return bool
*/
public static function setMeasurementUnit($value)
@ -247,6 +262,102 @@ class Settings
return true;
}
/**
* Get default font name
*
* @return string
*/
public static function getDefaultFontName()
{
return self::$defaultFontName;
}
/**
* Set default font name
*
* @param string $value
* @return bool
*/
public static function setDefaultFontName($value)
{
if (is_string($value) && trim($value) !== '') {
self::$defaultFontName = $value;
return true;
}
return false;
}
/**
* Get default font size
*
* @return integer
*/
public static function getDefaultFontSize()
{
return self::$defaultFontSize;
}
/**
* Set default font size
*
* @param int $value
* @return bool
*/
public static function setDefaultFontSize($value)
{
$value = intval($value);
if ($value > 0) {
self::$defaultFontSize = $value;
return true;
}
return false;
}
/**
* Load setting from phpword.yml or phpword.yml.dist
*
* @param string $filename
* @return array
*/
public static function loadConfig($filename = null)
{
// Get config file
$configFile = null;
$configPath = __DIR__ . '/../../';
if ($filename !== null) {
$files = array($filename);
} else {
$files = array("{$configPath}phpword.ini", "{$configPath}phpword.ini.dist");
}
foreach ($files as $file) {
if (file_exists($file)) {
$configFile = realpath($file);
break;
}
}
// Parse config file
$config = array();
if ($configFile !== null) {
$config = parse_ini_file($configFile);
if ($config === false) {
return $config;
}
}
// Set config value
foreach ($config as $key => $value) {
$method = "set{$key}";
if (method_exists(__CLASS__, $method)) {
self::$method($value);
}
}
return $config;
}
/**
* Return the compatibility option used by the XMLWriter
*

View File

@ -28,12 +28,13 @@ if (!defined('DATE_W3C')) {
/**
* XMLWriter wrapper
*
* @method bool writeElement(string $name, string $content = null)
* @method bool startElement(string $name)
* @method bool writeAttribute(string $name, string $value)
* @method bool endElement()
* @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
* @method bool startElement(string $name)
* @method bool text(string $content)
* @method bool writeAttribute(string $name, string $value)
* @method bool writeElement(string $name, string $content = null)
* @method bool writeRaw(string $content)
*/
class XMLWriter
{
@ -135,21 +136,6 @@ class XMLWriter
}
}
/**
* Fallback method for writeRaw, introduced in PHP 5.2
*
* @param string $text
* @return bool
*/
public function writeRaw($text)
{
if (isset($this->xmlWriter) && is_object($this->xmlWriter) && (method_exists($this->xmlWriter, 'writeRaw'))) {
return $this->xmlWriter->writeRaw($text);
}
return $this->text($text);
}
/**
* Write element if ...
*

View File

@ -17,8 +17,6 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\PhpWord;
/**
* Font style
*/
@ -86,30 +84,30 @@ class Font extends AbstractStyle
/**
* Font name
*
* @var int|float
* @var string
*/
private $name = PhpWord::DEFAULT_FONT_NAME;
private $name;
/**
* Font Content Type
*
* @var string
*/
private $hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE;
private $hint;
/**
* Font size
*
* @var int|float
*/
private $size = PhpWord::DEFAULT_FONT_SIZE;
private $size;
/**
* Font color
*
* @var string
*/
private $color = PhpWord::DEFAULT_FONT_COLOR;
private $color;
/**
* Bold
@ -241,9 +239,9 @@ class Font extends AbstractStyle
* @param string $value
* @return self
*/
public function setName($value = PhpWord::DEFAULT_FONT_NAME)
public function setName($value = null)
{
$this->name = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_NAME);
$this->name = $value;
return $this;
}
@ -264,9 +262,9 @@ class Font extends AbstractStyle
* @param string $value
* @return self
*/
public function setHint($value = PhpWord::DEFAULT_FONT_CONTENT_TYPE)
public function setHint($value = null)
{
$this->hint = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_CONTENT_TYPE);
$this->hint = $value;
return $this;
}
@ -287,9 +285,9 @@ class Font extends AbstractStyle
* @param int|float $value
* @return self
*/
public function setSize($value = PhpWord::DEFAULT_FONT_SIZE)
public function setSize($value = null)
{
$this->size = $this->setNumericVal($value, PhpWord::DEFAULT_FONT_SIZE);
$this->size = $this->setNumericVal($value, $this->size);
return $this;
}
@ -310,9 +308,9 @@ class Font extends AbstractStyle
* @param string $value
* @return self
*/
public function setColor($value = PhpWord::DEFAULT_FONT_COLOR)
public function setColor($value = null)
{
$this->color = $this->setNonEmptyVal($value, PhpWord::DEFAULT_FONT_COLOR);
$this->color = $value;
return $this;
}

View File

@ -430,7 +430,7 @@ class Table extends Border
/**
* Get cell margin
*
* @return int[]
* @return integer[]
*/
public function getCellMargin()
{

View File

@ -19,9 +19,10 @@ namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Writer\HTML\Element\Container;
use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
@ -168,14 +169,13 @@ class HTML extends AbstractWriter implements WriterInterface
*/
private function writeStyles()
{
$phpWord = $this->getPhpWord();
$css = '<style>' . PHP_EOL;
// Default styles
$defaultStyles = array(
'*' => array(
'font-family' => $phpWord->getDefaultFontName(),
'font-size' => $phpWord->getDefaultFontSize() . 'pt',
'font-family' => Settings::getDefaultFontName(),
'font-size' => Settings::getDefaultFontSize() . 'pt',
),
'a.NoteRef' => array(
'text-decoration' => 'none',

View File

@ -17,7 +17,7 @@
namespace PhpOffice\PhpWord\Writer\HTML\Style;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style\Font as FontStyle;
/**
@ -47,9 +47,9 @@ class Font extends AbstractStyle
$underline = $style->getUnderline() != FontStyle::UNDERLINE_NONE;
$lineThrough = $style->isStrikethrough() || $style->isDoubleStrikethrough();
$css['font-family'] = $this->getValueIf($font != PhpWord::DEFAULT_FONT_NAME, "'{$font}'");
$css['font-size'] = $this->getValueIf($size != PhpWord::DEFAULT_FONT_SIZE, "{$size}pt");
$css['color'] = $this->getValueIf($color != PhpWord::DEFAULT_FONT_COLOR, "#{$color}");
$css['font-family'] = $this->getValueIf($font !== null, "'{$font}'");
$css['font-size'] = $this->getValueIf($size !== null, "{$size}pt");
$css['color'] = $this->getValueIf($color != Settings::DEFAULT_FONT_COLOR, "#{$color}");
$css['background'] = $this->getValueIf($fgColor != '', $fgColor);
$css['font-weight'] = $this->getValueIf($style->isBold(), 'bold');
$css['font-style'] = $this->getValueIf($style->isItalic(), 'italic');

View File

@ -17,8 +17,6 @@
namespace PhpOffice\PhpWord\Writer\HTML\Style;
use PhpOffice\PhpWord\Settings;
/**
* Paragraph style HTML writer
*
@ -48,8 +46,8 @@ class Paragraph extends AbstractStyle
if (!is_null($spacing)) {
$before = $spacing->getBefore();
$after = $spacing->getAfter();
$css['margin-top'] = $this->getValueIf(!is_null($before), ($before / Settings::UNIT_POINT) . 'pt');
$css['margin-bottom'] = $this->getValueIf(!is_null($after), ($after / Settings::UNIT_POINT) . 'pt');
$css['margin-top'] = $this->getValueIf(!is_null($before), ($before / 20) . 'pt');
$css['margin-bottom'] = $this->getValueIf(!is_null($after), ($after / 20) . 'pt');
}
return $this->assembleCss($css);

View File

@ -17,7 +17,7 @@
namespace PhpOffice\PhpWord\Writer\ODText\Part;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font;
@ -68,7 +68,7 @@ abstract class AbstractPart extends Word2007AbstractPart
protected function writeFontFaces(XMLWriter $xmlWriter)
{
$xmlWriter->startElement('office:font-face-decls');
$arrFonts = array();
$fontTable = array();
$styles = Style::getStyles();
$numFonts = 0;
if (count($styles) > 0) {
@ -77,8 +77,8 @@ abstract class AbstractPart extends Word2007AbstractPart
if ($style instanceof Font) {
$numFonts++;
$name = $style->getName();
if (!in_array($name, $arrFonts)) {
$arrFonts[] = $name;
if (!in_array($name, $fontTable)) {
$fontTable[] = $name;
// style:font-face
$xmlWriter->startElement('style:font-face');
@ -89,10 +89,10 @@ abstract class AbstractPart extends Word2007AbstractPart
}
}
}
if (!in_array(PhpWord::DEFAULT_FONT_NAME, $arrFonts)) {
if (!in_array(Settings::getDefaultFontName(), $fontTable)) {
$xmlWriter->startElement('style:font-face');
$xmlWriter->writeAttribute('style:name', PhpWord::DEFAULT_FONT_NAME);
$xmlWriter->writeAttribute('svg:font-family', PhpWord::DEFAULT_FONT_NAME);
$xmlWriter->writeAttribute('style:name', Settings::getDefaultFontName());
$xmlWriter->writeAttribute('svg:font-family', Settings::getDefaultFontName());
$xmlWriter->endElement();
}
$xmlWriter->endElement();

View File

@ -23,8 +23,8 @@ use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Writer\ODText\Element\Container;
/**

View File

@ -17,7 +17,7 @@
namespace PhpOffice\PhpWord\Writer\ODText\Part;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style;
/**
@ -64,17 +64,17 @@ class Styles extends AbstractPart
// style:text-properties
$xmlWriter->startElement('style:text-properties');
$xmlWriter->writeAttribute('style:use-window-font-color', 'true');
$xmlWriter->writeAttribute('style:font-name', PhpWord::DEFAULT_FONT_NAME);
$xmlWriter->writeAttribute('fo:font-size', PhpWord::DEFAULT_FONT_SIZE . 'pt');
$xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName());
$xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt');
$xmlWriter->writeAttribute('fo:language', 'fr');
$xmlWriter->writeAttribute('fo:country', 'FR');
$xmlWriter->writeAttribute('style:letter-kerning', 'true');
$xmlWriter->writeAttribute('style:font-name-asian', PhpWord::DEFAULT_FONT_NAME . '2');
$xmlWriter->writeAttribute('style:font-size-asian', PhpWord::DEFAULT_FONT_SIZE . 'pt');
$xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2');
$xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt');
$xmlWriter->writeAttribute('style:language-asian', 'zh');
$xmlWriter->writeAttribute('style:country-asian', 'CN');
$xmlWriter->writeAttribute('style:font-name-complex', PhpWord::DEFAULT_FONT_NAME . '2');
$xmlWriter->writeAttribute('style:font-size-complex', PhpWord::DEFAULT_FONT_SIZE . 'pt');
$xmlWriter->writeAttribute('style:font-name-complex', Settings::getDefaultFontName() . '2');
$xmlWriter->writeAttribute('style:font-size-complex', Settings::getDefaultFontSize() . 'pt');
$xmlWriter->writeAttribute('style:language-complex', 'hi');
$xmlWriter->writeAttribute('style:country-complex', 'IN');
$xmlWriter->writeAttribute('fo:hyphenate', 'false');

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\Drawing;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font;
@ -153,7 +154,7 @@ class RTF extends AbstractWriter implements WriterInterface
$content .= '\nowidctlpar'; // No widow/orphan control
$content .= '\lang1036'; // Applies a language to a text run (1036 : French (France))
$content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs
$content .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2); // Set the font size in half-points
$content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points
$content .= PHP_EOL;
// Body
@ -191,7 +192,7 @@ class RTF extends AbstractWriter implements WriterInterface
{
$phpWord = $this->getPhpWord();
$fontTable = array();
$fontTable[] = PhpWord::DEFAULT_FONT_NAME;
$fontTable[] = Settings::getDefaultFontName();
// Browse styles
$styles = Style::getStyles();
@ -236,7 +237,7 @@ class RTF extends AbstractWriter implements WriterInterface
private function populateColorTable()
{
$phpWord = $this->getPhpWord();
$defaultFontColor = PhpWord::DEFAULT_FONT_COLOR;
$defaultFontColor = Settings::DEFAULT_FONT_COLOR;
$colorTable = array();
// Browse styles

View File

@ -21,9 +21,9 @@ use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\Font as FontStyle;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
use PhpOffice\PhpWord\Writer\HTML\Element\AbstractElement as HTMLAbstractElement;
use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Element\AbstractElement as HTMLAbstractElement;
/**
* Abstract RTF element writer

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Element\TextBreak as TextBreakElement;
use PhpOffice\PhpWord\Element\AbstractContainer as ContainerElement;
use PhpOffice\PhpWord\Element\TextBreak as TextBreakElement;
/**
* Container element writer (section, textrun, header, footnote, cell, etc.)

View File

@ -18,9 +18,9 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Numbering as NumberingStyle;
use PhpOffice\PhpWord\Style\NumberingLevel;
use PhpOffice\PhpWord\Style;
/**
* Word2007 numbering part writer: word/numbering.xml

View File

@ -17,11 +17,11 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings as PhpWordSettings;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
@ -41,7 +41,6 @@ class Styles extends AbstractPart
*/
public function write()
{
$phpWord = $this->getParentWriter()->getPhpWord();
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
@ -51,7 +50,7 @@ class Styles extends AbstractPart
// Write default styles
$styles = Style::getStyles();
$this->writeDefaultStyles($xmlWriter, $phpWord, $styles);
$this->writeDefaultStyles($xmlWriter, $styles);
// Write styles
if (count($styles) > 0) {
@ -155,13 +154,12 @@ class Styles extends AbstractPart
* Write default font and other default styles
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @param array $styles
*/
private function writeDefaultStyles(XMLWriter $xmlWriter, PhpWord $phpWord, $styles)
private function writeDefaultStyles(XMLWriter $xmlWriter, $styles)
{
$fontName = $phpWord->getDefaultFontName();
$fontSize = $phpWord->getDefaultFontSize();
$fontName = PhpWordSettings::getDefaultFontName();
$fontSize = PhpWordSettings::getDefaultFontSize();
// Default font
$xmlWriter->startElement('w:docDefaults');

View File

@ -87,11 +87,18 @@ abstract class AbstractStyle
*/
protected function convertTwip($value, $default = 0)
{
$conversions = array(
Settings::UNIT_CM => 567,
Settings::UNIT_MM => 56.7,
Settings::UNIT_INCH => 1440,
Settings::UNIT_POINT => 20,
Settings::UNIT_PICA => 240,
);
$unit = Settings::getMeasurementUnit();
if ($unit == Settings::UNIT_TWIP || $value == $default) {
return $value;
if (in_array($unit, $conversions) && $value != $default) {
return $value * $conversions[$unit];
} else {
return $value * $unit;
return $value;
}
}
}

View File

@ -17,8 +17,6 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\PhpWord;
/**
* Font style writer
*
@ -72,24 +70,24 @@ class Font extends AbstractStyle
// Font name/family
$font = $style->getName();
$hint = $style->getHint();
if ($font != PhpWord::DEFAULT_FONT_NAME) {
if ($font !== null) {
$xmlWriter->startElement('w:rFonts');
$xmlWriter->writeAttribute('w:ascii', $font);
$xmlWriter->writeAttribute('w:hAnsi', $font);
$xmlWriter->writeAttribute('w:eastAsia', $font);
$xmlWriter->writeAttribute('w:cs', $font);
$xmlWriter->writeAttributeIf($hint != PhpWord::DEFAULT_FONT_CONTENT_TYPE, 'w:hint', $hint);
$xmlWriter->writeAttributeIf($hint !== null, 'w:hint', $hint);
$xmlWriter->endElement();
}
// Color
$color = $style->getColor();
$xmlWriter->writeElementIf($color != PhpWord::DEFAULT_FONT_COLOR, 'w:color', 'w:val', $color);
$xmlWriter->writeElementIf($color !== null, 'w:color', 'w:val', $color);
// Size
$size = $style->getSize();
$xmlWriter->writeElementIf($size != PhpWord::DEFAULT_FONT_SIZE, 'w:sz', 'w:val', $size * 2);
$xmlWriter->writeElementIf($size != PhpWord::DEFAULT_FONT_SIZE, 'w:szCs', 'w:val', $size * 2);
$xmlWriter->writeElementIf($size !== null, 'w:sz', 'w:val', $size * 2);
$xmlWriter->writeElementIf($size !== null, 'w:szCs', 'w:val', $size * 2);
// Bold, italic
$xmlWriter->writeElementIf($style->isBold(), 'w:b');

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Tests;
use PhpOffice\PhpWord\DocumentProperties;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style;
/**
@ -35,8 +36,8 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
{
$phpWord = new PhpWord();
$this->assertEquals(new DocumentProperties(), $phpWord->getDocumentProperties());
$this->assertEquals(PhpWord::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
$this->assertEquals(PhpWord::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
$this->assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
$this->assertEquals(Settings::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
}
/**
@ -69,7 +70,7 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
{
$phpWord = new PhpWord();
$fontName = 'Times New Roman';
$this->assertEquals(PhpWord::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
$this->assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
$phpWord->setDefaultFontName($fontName);
$this->assertEquals($fontName, $phpWord->getDefaultFontName());
}
@ -81,7 +82,7 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
{
$phpWord = new PhpWord();
$fontSize = 16;
$this->assertEquals(PhpWord::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
$this->assertEquals(Settings::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
$phpWord->setDefaultFontSize($fontSize);
$this->assertEquals($fontSize, $phpWord->getDefaultFontSize());
}

View File

@ -34,7 +34,6 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(Settings::hasCompatibility());
$this->assertTrue(Settings::setCompatibility(false));
$this->assertFalse(Settings::hasCompatibility());
$this->assertFalse(Settings::setCompatibility('Non boolean'));
}
/**
@ -70,4 +69,40 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(Settings::setMeasurementUnit(Settings::UNIT_INCH));
$this->assertFalse(Settings::setMeasurementUnit('foo'));
}
/**
* Test set/get default font name
*/
public function testSetGetDefaultFontName()
{
$this->assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultFontName());
$this->assertTrue(Settings::setDefaultFontName('Times New Roman'));
$this->assertFalse(Settings::setDefaultFontName(' '));
}
/**
* Test set/get default font size
*/
public function testSetGetDefaultFontSize()
{
$this->assertEquals(Settings::DEFAULT_FONT_SIZE, Settings::getDefaultFontSize());
$this->assertTrue(Settings::setDefaultFontSize(12));
$this->assertFalse(Settings::setDefaultFontSize(null));
}
/**
* Test load config
*/
public function testLoadConfig()
{
$expected = array(
'compatibility' => true,
'zipClass' => 'ZipArchive',
'pdfRendererName' => 'DomPDF',
'pdfRendererPath' => '',
'defaultFontName' => 'Arial',
'defaultFontSize' => 10,
);
$this->assertEquals($expected, Settings::loadConfig(__DIR__ . '/../../../phpword.ini.dist'));
}
}

View File

@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Tests\Style;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Tests\TestHelperDOCX;
@ -55,8 +56,10 @@ class FontTest extends \PHPUnit_Framework_TestCase
$object = new Font();
$attributes = array(
'name' => PhpWord::DEFAULT_FONT_NAME,
'size' => PhpWord::DEFAULT_FONT_SIZE,
'name' => null,
'size' => null,
'hint' => null,
'color' => null,
'bold' => false,
'italic' => false,
'underline' => Font::UNDERLINE_NONE,
@ -66,11 +69,8 @@ class FontTest extends \PHPUnit_Framework_TestCase
'doubleStrikethrough' => false,
'smallCaps' => false,
'allCaps' => false,
'doubleStrikethrough' => false,
'color' => PhpWord::DEFAULT_FONT_COLOR,
'fgColor' => null,
'bgColor' => null,
'hint' => PhpWord::DEFAULT_FONT_CONTENT_TYPE,
);
foreach ($attributes as $key => $default) {
$get = is_bool($default) ? "is{$key}" : "get{$key}";
@ -91,6 +91,8 @@ class FontTest extends \PHPUnit_Framework_TestCase
$attributes = array(
'name' => 'Times New Roman',
'size' => 9,
'color' => '999999',
'hint' => 'eastAsia',
'bold' => true,
'italic' => true,
'underline' => Font::UNDERLINE_HEAVY,
@ -100,10 +102,8 @@ class FontTest extends \PHPUnit_Framework_TestCase
'doubleStrikethrough' => false,
'smallCaps' => true,
'allCaps' => false,
'color' => '999999',
'fgColor' => Font::FGCOLOR_YELLOW,
'bgColor' => 'FFFF00',
'hint' => 'eastAsia',
'lineHeight' => 2,
);
$object->setStyleByArray($attributes);