Merge branch 'development' into fix_php7_issues

This commit is contained in:
troosan 2017-09-27 01:09:23 +02:00
commit e6f4d0276a
38 changed files with 624 additions and 61 deletions

View File

@ -14,8 +14,8 @@ This is the last version to support PHP 5.3
- Introduced the `\PhpOffice\PhpWord\SimpleType\NumberFormat` simple type. - @troosan
- Support for ContextualSpacing - @postHawk #1088
- Possiblity to hide spelling and/or grammatical errors - @troosan #542
- Possiblity to set default document language as well as changing the language for each text element - @troosan #1108
- Support for Comments - @troosan #1067
- Add support for changing the document language - @troosan #1108
### Fixed
- Loosen dependency to Zend

View File

@ -201,6 +201,25 @@ The default symbol to represent a decimal figure is the ``.`` in english. In fre
$phpWord->getSettings()->setDecimalSymbol(',');
Document Language
~~~~~~~~~~~~~~~~~
The default language of the document can be change with the following.
.. code-block:: php
$phpWord->getSettings()->setThemeFontLang(new Language(Language::FR_BE));
``Languge`` has 3 parameters, one for Latin languages, one for East Asian languages and one for Complex (Bi-Directional) languages.
A couple of language codes are provided in the ``PhpOffice\PhpWord\ComplexType\Language`` class but any valid code/ID can be used.
In case you are generating an RTF document the Language need to be set differently.
.. code-block:: php
$lang = new Language();
$lang->setLangId(Language::EN_GB_ID);
$phpWord->getSettings()->setThemeFontLang($lang);
Document information
--------------------

View File

@ -55,6 +55,8 @@ Available Font style options:
- ``subScript``. Subscript, *true* or *false*.
- ``superScript``. Superscript, *true* or *false*.
- ``underline``. Underline, *dash*, *dotted*, etc.
- ``lang``. Language, either a language code like *en-US*, *fr-BE*, etc. or an object (or as an array) if you need to set eastAsian or bidirectional languages
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.
.. _paragraph-style:
@ -106,7 +108,8 @@ Available Cell style options:
- ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'.
- ``border(Top|Right|Bottom|Left)Size``. Border size in twips.
- ``gridSpan``. Number of columns spanned.
- ``textDirection(btLr|tbRl)``. Direction of text. You can use constants ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR`` and ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_TBRL``
- ``textDirection(btLr|tbRl)``. Direction of text.
You can use constants ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR`` and ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_TBRL``
- ``valign``. Vertical alignment, *top*, *center*, *both*, *bottom*.
- ``vMerge``. *restart* or *continue*.
- ``width``. Cell width in twips.

View File

@ -1,9 +1,16 @@
<?php
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
include_once 'Sample_Header.php';
// New Word Document
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
$languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::EN_GB);
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getSettings()->setThemeFontLang($languageEnGb);
$fontStyleName = 'rStyle';
$phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
@ -20,6 +27,10 @@ $section = $phpWord->addSection();
$section->addTitle('Welcome to PhpWord', 1);
$section->addText('Hello World!');
// $pStyle = new Font();
// $pStyle->setLang()
$section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE));
// Two text break
$section->addTextBreak(2);

View File

@ -7,7 +7,7 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$header = array('size' => 16, 'bold' => true);
//1.Use EastAisa FontStyle
$section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232'));
$section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232', 'lang' => array('latin' => 'en-US', 'eastAsia' => 'zh-CN')));
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);

View File

@ -135,10 +135,10 @@ final class TrackChangesView
/**
* Set Display Formatting Revisions
*
* @param bool $formatting
* @param bool|null $formatting
* Set to true to show formatting revisions
*/
public function setFormatting($formatting)
public function setFormatting($formatting = null)
{
$this->formatting = $formatting === null ? true : $formatting;
}

View File

@ -228,7 +228,7 @@ abstract class AbstractElement
/**
* Get element unique ID
*
* @return string
* @return int
*/
public function getElementId()
{

View File

@ -46,8 +46,6 @@ class Bookmark extends AbstractElement
public function __construct($name)
{
$this->name = CommonText::toUTF8($name);
return $this;
}
/**

View File

@ -40,7 +40,6 @@ class CheckBox extends Text
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($name = null, $text = null, $fontStyle = null, $paragraphStyle = null)
{

View File

@ -61,8 +61,6 @@ class Comment extends TrackChange
{
parent::__construct($author, $date);
$this->initials = $initials;
return $this;
}
/**

View File

@ -35,7 +35,7 @@ class FormField extends Text
/**
* Form field name
*
* @var string
* @var string|bool|int
*/
private $name;
@ -70,7 +70,6 @@ class FormField extends Text
* @param string $type
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
{

View File

@ -84,8 +84,6 @@ class Link extends AbstractElement
$this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
$this->internal = $internal;
return $this;
}
/**

View File

@ -53,7 +53,6 @@ class PreserveText extends AbstractElement
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
{

View File

@ -51,7 +51,6 @@ class SDT extends Text
* @param string $type
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
{

View File

@ -36,7 +36,7 @@ class TOC extends AbstractElement
/**
* Font style
*
* @var \PhpOffice\PhpWord\Style\Font|array|string
* @var \PhpOffice\PhpWord\Style\Font|string
*/
private $fontStyle;
@ -120,7 +120,7 @@ class TOC extends AbstractElement
/**
* Get Font Style
*
* @return \PhpOffice\PhpWord\Style\Font
* @return \PhpOffice\PhpWord\Style\Font|string
*/
public function getStyleFont()
{

View File

@ -45,7 +45,7 @@ class TrackChange extends AbstractContainer
* Create a new TrackChange Element
*
* @param string $author
* @param DateTime $date
* @param \DateTime $date
*/
public function __construct($author, \DateTime $date)
{

View File

@ -20,6 +20,7 @@ namespace PhpOffice\PhpWord\Metadata;
use PhpOffice\PhpWord\ComplexType\ProofState;
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\SimpleType\Zoom;
use PhpOffice\PhpWord\Style\Language;
/**
* Setting class
@ -100,6 +101,13 @@ class Settings
*/
private $evenAndOddHeaders = false;
/**
* Theme Font Languages
*
* @var Language
*/
private $themeFontLang;
/**
* Radix Point for Field Code Evaluation
*
@ -293,6 +301,26 @@ class Settings
}
}
/**
* Returns the Language
*
* @return Language
*/
public function getThemeFontLang()
{
return $this->themeFontLang;
}
/**
* sets the Language for this document
*
* @param Language $themeFontLang
*/
public function setThemeFontLang($themeFontLang)
{
$this->themeFontLang = $themeFontLang;
}
/**
* Returns the Radix Point for Field Code Evaluation
*

View File

@ -315,6 +315,8 @@ abstract class AbstractPart
'keepNext' => array(self::READ_TRUE, 'w:keepNext'),
'keepLines' => array(self::READ_TRUE, 'w:keepLines'),
'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'),
'contextualSpacing' => array(self::READ_TRUE, 'w:contextualSpacing'),
'bidi' => array(self::READ_TRUE, 'w:bidi'),
);
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
@ -358,6 +360,9 @@ abstract class AbstractPart
'subScript' => array(self::READ_EQUAL, 'w:vertAlign', 'w:val', 'subscript'),
'fgColor' => array(self::READ_VALUE, 'w:highlight'),
'rtl' => array(self::READ_TRUE, 'w:rtl'),
'font-latin' => array(self::READ_VALUE, 'w:font', 'w:val'),
'font-eastAsia' => array(self::READ_VALUE, 'w:font', 'w:eastAsia'),
'font-bidi' => array(self::READ_VALUE, 'w:font', 'w:bidi'),
);
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);

View File

@ -20,6 +20,7 @@ namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Language;
/**
* Settings reader
@ -64,6 +65,27 @@ class Settings extends AbstractPart
}
}
/**
* Sets the document Language
*
* @param XMLReader $xmlReader
* @param PhpWord $phpWord
* @param \DOMNode $node
*/
protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$val = $xmlReader->getAttribute('w:val', $node);
$eastAsia = $xmlReader->getAttribute('w:eastAsia', $node);
$bidi = $xmlReader->getAttribute('w:bidi', $node);
$themeFontLang = new Language();
$themeFontLang->setLatin($val);
$themeFontLang->setLatin($eastAsia);
$themeFontLang->setLatin($bidi);
$phpWord->getSettings()->setThemeFontLang($themeFontLang);
}
/**
* Sets the document protection
*
@ -71,7 +93,7 @@ class Settings extends AbstractPart
* @param PhpWord $phpWord
* @param \DOMNode $node
*/
protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node)
protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
@ -86,17 +108,17 @@ class Settings extends AbstractPart
* @param PhpWord $phpWord
* @param \DOMNode $node
*/
protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node)
protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$proofState = $phpWord->getSettings()->getProofState();
$spelling = $xmlReader->getAttribute('w:spelling', $node);
$grammar = $xmlReader->getAttribute('w:grammar', $node);
if ($spelling != null) {
if ($spelling !== null) {
$proofState->setSpelling($spelling);
}
if ($grammar != null) {
if ($grammar !== null) {
$proofState->setGrammar($grammar);
}
}
@ -108,13 +130,13 @@ class Settings extends AbstractPart
* @param PhpWord $phpWord
* @param \DOMNode $node
*/
protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node)
protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$percent = $xmlReader->getAttribute('w:percent', $node);
$val = $xmlReader->getAttribute('w:val', $node);
if ($percent != null || $val != null) {
$phpWord->getSettings()->setZoom($percent == null ? $val : $percent);
if ($percent !== null || $val !== null) {
$phpWord->getSettings()->setZoom($percent === null ? $val : $percent);
}
}
@ -125,7 +147,7 @@ class Settings extends AbstractPart
* @param PhpWord $phpWord
* @param \DOMNode $node
*/
protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node)
protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$revisionView = new TrackChangesView();
$revisionView->setMarkup($xmlReader->getAttribute('w:markup', $node));

View File

@ -329,7 +329,7 @@ abstract class AbstractStyle
protected function setPairedVal(&$property, &$pairProperty, $value)
{
$property = $this->setBoolVal($value, $property);
if ($value == true) {
if ($value === true) {
$pairProperty = false;
}

View File

@ -228,6 +228,12 @@ class Font extends AbstractStyle
*/
private $rtl = false;
/**
* Languages
* @var \PhpOffice\PhpWord\Style\Language
*/
private $lang;
/**
* Create new font style
*
@ -276,6 +282,7 @@ class Font extends AbstractStyle
'paragraph' => $this->getParagraph(),
'rtl' => $this->isRTL(),
'shading' => $this->getShading(),
'lang' => $this->getLang(),
);
return $styles;
@ -783,6 +790,32 @@ class Font extends AbstractStyle
return $this;
}
/**
* Get language
*
* @return \PhpOffice\PhpWord\Style\Language
*/
public function getLang()
{
return $this->lang;
}
/**
* Set language
*
* @param mixed $value
* @return self
*/
public function setLang($value = null)
{
if (is_string($value) && $value != '') {
$value = new Language($value);
}
$this->setObjectVal($value, 'Language', $this->lang);
return $this;
}
/**
* Get bold
*

View File

@ -0,0 +1,223 @@
<?php
/**
* 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.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2017 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Style;
/**
* Language
* A couple of predefined values are defined here, see the websites below for more values
*
* @see http://www.datypic.com/sc/ooxml/t-w_CT_Language.html
* @see https://technet.microsoft.com/en-us/library/cc287874(v=office.12).aspx
*/
final class Language extends AbstractStyle
{
const EN_US = 'en-US';
const EN_US_ID = 1033;
const EN_GB = 'en-GB';
const EN_GB_ID = 2057;
const FR_FR = 'fr-FR';
const FR_FR_ID = 1036;
const FR_BE = 'fr-BE';
const FR_BE_ID = 2060;
const ES_ES = 'es-ES';
const ES_ES_ID = 3082;
const DE_DE = 'de-DE';
const DE_DE_ID = 1031;
const HE_IL = 'he-IL';
const HE_IL_ID = 1037;
const JA_JP = 'ja-JP';
const JA_JP_ID = 1041;
const KO_KR = 'ko-KR';
const KO_KR_ID = 1042;
const ZH_CN = 'zh-CN';
const ZH_CN_ID = 2052;
const HI_IN = 'hi-IN';
const HI_IN_ID = 1081;
/**
* Language ID, used for RTF document generation
*
* @var int
* @see https://technet.microsoft.com/en-us/library/cc179219.aspx
*/
private $langId;
/**
* Latin Language
*
* @var string
*/
private $latin;
/**
* East Asian Language
*
* @var string
*/
private $eastAsia;
/**
* Complex Script Language
*
* @var string
*/
private $bidirectional;
/**
* Constructor
*
* @param string|null $latin
* @param string|null $eastAsia
* @param string|null $bidirectional
*/
public function __construct($latin = null, $eastAsia = null, $bidirectional = null)
{
if (!empty($latin)) {
$this->setLatin($latin);
}
if (!empty($eastAsia)) {
$this->setEastAsia($eastAsia);
}
if (!empty($bidirectional)) {
$this->setBidirectional($bidirectional);
}
}
/**
* Set the Latin Language
*
* @param string $latin
* The value for the latin language
* @return self
*/
public function setLatin($latin)
{
$this->validateLocale($latin);
$this->latin = $latin;
return $this;
}
/**
* Get the Latin Language
*
* @return string|null
*/
public function getLatin()
{
return $this->latin;
}
/**
* Set the Language ID
*
* @param int $langId
* The value for the language ID
* @return self
* @see https://technet.microsoft.com/en-us/library/cc287874(v=office.12).aspx
*/
public function setLangId($langId)
{
$this->langId = $langId;
return $this;
}
/**
* Get the Language ID
*
* @return int
*/
public function getLangId()
{
return $this->langId;
}
/**
* Set the East Asian Language
*
* @param string $eastAsia
* The value for the east asian language
* @return self
*/
public function setEastAsia($eastAsia)
{
$this->validateLocale($eastAsia);
$this->eastAsia = $eastAsia;
return $this;
}
/**
* Get the East Asian Language
*
* @return string|null
*/
public function getEastAsia()
{
return $this->eastAsia;
}
/**
* Set the Complex Script Language
*
* @param string $bidirectional
* The value for the complex script language
* @return self
*/
public function setBidirectional($bidirectional)
{
$this->validateLocale($bidirectional);
$this->bidirectional = $bidirectional;
return $this;
}
/**
* Get the Complex Script Language
*
* @return string|null
*/
public function getBidirectional()
{
return $this->bidirectional;
}
/**
* Validates that the language passed is in the format xx-xx
*
* @param string $locale
* @return bool
*/
private function validateLocale($locale)
{
if ($locale !== null && strstr($locale, '-') === false) {
throw new \InvalidArgumentException($locale . ' is not a valid language code');
}
}
}

View File

@ -248,11 +248,12 @@ class ListItem extends AbstractStyle
// Populate style and register to global Style register
$style = $listTypeStyles[$this->listType];
$numProperties = count($properties);
foreach ($style['levels'] as $key => $value) {
$level = array();
$levelProperties = explode(', ', $value);
$level['level'] = $key;
for ($i = 0; $i < count($properties); $i++) {
for ($i = 0; $i < $numProperties; $i++) {
$property = $properties[$i];
$level[$property] = $levelProperties[$i];
}

View File

@ -165,6 +165,13 @@ class Paragraph extends Border
*/
private $contextualSpacing = false;
/**
* Right to Left Paragraph Layout
*
* @var bool
*/
private $bidi = false;
/**
* Set Style value
*
@ -216,6 +223,7 @@ class Paragraph extends Border
'tabs' => $this->getTabs(),
'shading' => $this->getShading(),
'contextualSpacing' => $this->hasContextualSpacing(),
'bidi' => $this->isBidi(),
);
return $styles;
@ -762,4 +770,28 @@ class Paragraph extends Border
return $this;
}
/**
* Get bidirectional
*
* @return bool
*/
public function isBidi()
{
return $this->bidi;
}
/**
* Set bidi
*
* @param bool $bidi
* Set to true to write from right to left
* @return self
*/
public function setBidi($bidi)
{
$this->bidi = $bidi;
return $this;
}
}

View File

@ -178,7 +178,8 @@ class TextBox extends Image
{
$hasInnerMargins = false;
$margins = $this->getInnerMargin();
for ($i = 0; $i < count($margins); $i++) {
$numMargins = count($margins);
for ($i = 0; $i < $numMargins; $i++) {
if ($margins[$i] !== null) {
$hasInnerMargins = true;
}

View File

@ -80,22 +80,27 @@ class Styles extends AbstractPart
$xmlWriter->writeAttribute('style:writing-mode', 'page');
$xmlWriter->endElement(); // style:paragraph-properties
$language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang();
$latinLang = $language != null && is_string($language->getLatin()) ? explode('-', $language->getLatin()) : array('fr', 'FR');
$asianLang = $language != null && is_string($language->getEastAsia()) ? explode('-', $language->getEastAsia()) : array('zh', 'CN');
$complexLang = $language != null && is_string($language->getBidirectional()) ? explode('-', $language->getBidirectional()) : array('hi', 'IN');
// Font
$xmlWriter->startElement('style:text-properties');
$xmlWriter->writeAttribute('style:use-window-font-color', 'true');
$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('fo:language', $latinLang[0]);
$xmlWriter->writeAttribute('fo:country', $latinLang[1]);
$xmlWriter->writeAttribute('style:letter-kerning', 'true');
$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:language-asian', $asianLang[0]);
$xmlWriter->writeAttribute('style:country-asian', $asianLang[1]);
$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('style:language-complex', $complexLang[0]);
$xmlWriter->writeAttribute('style:country-complex', $complexLang[1]);
$xmlWriter->writeAttribute('fo:hyphenate', 'false');
$xmlWriter->writeAttribute('fo:hyphenation-remain-char-count', '2');
$xmlWriter->writeAttribute('fo:hyphenation-push-char-count', '2');

View File

@ -90,6 +90,10 @@ class Document extends AbstractPart
*/
private function writeFormatting()
{
$docSettings = $this->getParentWriter()->getPhpWord()->getSettings();
// Applies a language to a text run (defaults to 1036 : French (France))
$langId = $docSettings->getThemeFontLang() != null && $docSettings->getThemeFontLang()->getLangId() != null ? $docSettings->getThemeFontLang()->getLangId() : 1036;
$content = '';
$content .= '\deftab720'; // Set the default tab size (720 twips)
@ -98,7 +102,7 @@ class Document extends AbstractPart
$content .= '\uc1'; // Set the numberof bytes that follows a unicode character
$content .= '\pard'; // Resets to default paragraph properties.
$content .= '\nowidctlpar'; // No widow/orphan control
$content .= '\lang1036'; // Applies a language to a text run (1036 : French (France))
$content .= '\lang' . $langId;
$content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs
$content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points
$content .= PHP_EOL;

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\ComplexType\ProofState;
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\Style\Language;
/**
* Word2007 settings part writer: word/settings.xml
@ -105,7 +106,6 @@ class Settings extends AbstractPart
'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')),
'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')),
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')),
'w:decimalSymbol' => array('@attributes' => array('w:val' => $documentSettings->getDecimalSymbol())),
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
'w:compat' => array(),
@ -147,6 +147,7 @@ class Settings extends AbstractPart
$this->setOnOffValue('w:doNotTrackFormatting', $documentSettings->hasDoNotTrackFormatting());
$this->setOnOffValue('w:evenAndOddHeaders', $documentSettings->hasEvenAndOddHeaders());
$this->setThemeFontLang($documentSettings->getThemeFontLang());
$this->setRevisionView($documentSettings->getRevisionView());
$this->setDocumentProtection($documentSettings->getDocumentProtection());
$this->setProofState($documentSettings->getProofState());
@ -174,7 +175,7 @@ class Settings extends AbstractPart
/**
* Get protection settings.
*
* @param \PhpOffice\PhpWord\Metadata\Settings $documentProtection
* @param \PhpOffice\PhpWord\Metadata\Protection $documentProtection
*/
private function setDocumentProtection($documentProtection)
{
@ -206,9 +207,9 @@ class Settings extends AbstractPart
}
/**
* Set the Proof state
* Set the Revision View
*
* @param ProofState $proofState
* @param TrackChangesView $trackChangesView
*/
private function setRevisionView(TrackChangesView $trackChangesView = null)
{
@ -223,6 +224,23 @@ class Settings extends AbstractPart
}
}
/**
* Sets the language
*
* @param Language $language
*/
private function setThemeFontLang(Language $language = null)
{
$latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin();
$lang = array();
$lang['w:val'] = $latinLanguage;
if ($language != null) {
$lang['w:eastAsia'] = $language->getEastAsia() === null ? 'x-none' : $language->getEastAsia();
$lang['w:bidi'] = $language->getBidirectional() === null ? 'x-none' : $language->getBidirectional();
}
$this->settings['w:themeFontLang'] = array('@attributes' => $lang);
}
/**
* Set the magnification
*

View File

@ -84,6 +84,8 @@ class Styles extends AbstractPart
{
$fontName = PhpWordSettings::getDefaultFontName();
$fontSize = PhpWordSettings::getDefaultFontSize();
$language = $this->getParentWriter()->getPhpWord()->getSettings()->getThemeFontLang();
$latinLanguage = ($language == null || $language->getLatin() === null) ? 'en-US' : $language->getLatin();
// Default font
$xmlWriter->startElement('w:docDefaults');
@ -101,6 +103,13 @@ class Styles extends AbstractPart
$xmlWriter->startElement('w:szCs');
$xmlWriter->writeAttribute('w:val', $fontSize * 2);
$xmlWriter->endElement(); // w:szCs
$xmlWriter->startElement('w:lang');
$xmlWriter->writeAttribute('w:val', $latinLanguage);
if ($language != null) {
$xmlWriter->writeAttributeIf($language->getEastAsia() !== null, 'w:eastAsia', $language->getEastAsia());
$xmlWriter->writeAttributeIf($language->getBidirectional() !== null, 'w:bidi', $language->getBidirectional());
}
$xmlWriter->endElement(); // w:lang
$xmlWriter->endElement(); // w:rPr
$xmlWriter->endElement(); // w:rPrDefault
$xmlWriter->endElement(); // w:docDefaults

View File

@ -71,7 +71,7 @@ abstract class AbstractStyle
/**
* Get Style
*
* @return \PhpOffice\PhpWord\Style\AbstractStyle
* @return string|\PhpOffice\PhpWord\Style\AbstractStyle
*/
protected function getStyle()
{

View File

@ -82,6 +82,16 @@ class Font extends AbstractStyle
$xmlWriter->endElement();
}
//Language
$language = $style->getLang();
if ($language != null && ($language->getLatin() !== null || $language->getEastAsia() !== null || $language->getBidirectional() !== null)) {
$xmlWriter->startElement('w:lang');
$xmlWriter->writeAttributeIf($language->getLatin() !== null, 'w:val', $language->getLatin());
$xmlWriter->writeAttributeIf($language->getEastAsia() !== null, 'w:eastAsia', $language->getEastAsia());
$xmlWriter->writeAttributeIf($language->getBidirectional() !== null, 'w:bidi', $language->getBidirectional());
$xmlWriter->endElement();
}
// Color
$color = $style->getColor();
$xmlWriter->writeElementIf($color !== null, 'w:color', 'w:val', $color);

View File

@ -103,6 +103,9 @@ class Paragraph extends AbstractStyle
$xmlWriter->endElement();
}
//Right to left
$xmlWriter->writeElementIf($styles['bidi'] === true, 'w:bidi');
//Paragraph contextualSpacing
$xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing');

View File

@ -20,9 +20,9 @@ namespace PhpOffice\PhpWord\ComplexType;
use PhpOffice\PhpWord\SimpleType\NumberFormat;
/**
* Test class for PhpOffice\PhpWord\SimpleType\FootnoteProperties
* Test class for PhpOffice\PhpWord\ComplexType\FootnoteProperties
*
* @coversDefaultClass \PhpOffice\PhpWord\SimpleType\FootnoteProperties
* @coversDefaultClass \PhpOffice\PhpWord\ComplexType\FootnoteProperties
* @runTestsInSeparateProcesses
*/
class FootnotePropertiesTest extends \PHPUnit_Framework_TestCase

View File

@ -0,0 +1,61 @@
<?php
/**
* 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.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2017 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\ComplexType;
/**
* Test class for PhpOffice\PhpWord\ComplexType\ProofState
*
* @coversDefaultClass \PhpOffice\PhpWord\ComplexType\ProofState
*/
class ProofStateTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests the getters and setters
*/
public function testGetSet()
{
$pState = new ProofState();
$pState->setGrammar(ProofState::CLEAN);
$pState->setSpelling(ProofState::DIRTY);
$this->assertEquals(ProofState::CLEAN, $pState->getGrammar());
$this->assertEquals(ProofState::DIRTY, $pState->getSpelling());
}
/**
* Test throws exception if wrong grammar proof state value given
*
* @expectedException \InvalidArgumentException
*/
public function testWrongGrammar()
{
$pState = new ProofState();
$pState->setGrammar('Wrong');
}
/**
* Test throws exception if wrong spelling proof state value given
*
* @expectedException \InvalidArgumentException
*/
public function testWrongSpelling()
{
$pState = new ProofState();
$pState->setSpelling('Wrong');
}
}

View File

@ -74,6 +74,7 @@ class FontTest extends \PHPUnit_Framework_TestCase
'scale' => null,
'spacing' => null,
'kerning' => null,
'lang' => null,
);
foreach ($attributes as $key => $default) {
$get = is_bool($default) ? "is{$key}" : "get{$key}";

View File

@ -0,0 +1,62 @@
<?php
/**
* 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.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2017 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Style;
/**
* Test class for PhpOffice\PhpWord\Style\Language
*
* @coversDefaultClass \PhpOffice\PhpWord\Style\Language
*/
class LanguageTest extends \PHPUnit_Framework_TestCase
{
/**
* Test get/set
*/
public function testGetSetProperties()
{
$object = new Language();
$properties = array(
'latin' => array(null, 'fr-BE'),
'eastAsia' => array(null, 'ja-JP'),
'bidirectional' => array(null, 'ar-SA'),
'langId' => array(null, 1036),
);
foreach ($properties as $property => $value) {
list($default, $expected) = $value;
$get = "get{$property}";
$set = "set{$property}";
$this->assertEquals($default, $object->$get()); // Default value
$object->$set($expected);
$this->assertEquals($expected, $object->$get()); // New value
}
}
/**
* Test throws exception if wrong locale is given
*
* @expectedException \InvalidArgumentException
*/
public function testWrongLanguage()
{
$language = new Language();
$language->setLatin('fr');
}
}

View File

@ -80,6 +80,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
'keepLines' => true,
'pageBreakBefore' => true,
'contextualSpacing' => true,
'bidi' => true,
);
foreach ($attributes as $key => $value) {
$get = $this->findGetter($key, $value, $object);

View File

@ -21,6 +21,7 @@ use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Zoom;
use PhpOffice\PhpWord\Style\Language;
use PhpOffice\PhpWord\TestHelperDOCX;
/**
@ -74,7 +75,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
/**
* Test language
*/
public function testLanguage()
public function testDefaultLanguage()
{
$phpWord = new PhpWord();
@ -89,6 +90,26 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('en-US', $element->getAttribute('w:val'));
}
/**
* Test language
*/
public function testLanguage()
{
$phpWord = new PhpWord();
$phpWord->getSettings()->setThemeFontLang(new Language(Language::DE_DE, Language::KO_KR, Language::HE_IL));
$doc = TestHelperDOCX::getDocument($phpWord);
$file = 'word/settings.xml';
$path = '/w:settings/w:themeFontLang';
$this->assertTrue($doc->elementExists($path, $file));
$element = $doc->getElement($path, $file);
$this->assertEquals(Language::DE_DE, $element->getAttribute('w:val'));
$this->assertEquals(Language::KO_KR, $element->getAttribute('w:eastAsia'));
$this->assertEquals(Language::HE_IL, $element->getAttribute('w:bidi'));
}
/**
* Test spelling
*/