2014-03-08 23:43:27 +07:00
|
|
|
<?php
|
2014-03-27 23:55:06 +07:00
|
|
|
/**
|
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-03-27 23:55:06 +07:00
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @see https://github.com/PHPOffice/PHPWord
|
2018-03-08 23:52:25 +01:00
|
|
|
* @copyright 2010-2018 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-03-27 23:55:06 +07:00
|
|
|
*/
|
|
|
|
|
|
2015-11-15 13:33:05 +04:00
|
|
|
namespace PhpOffice\PhpWord\Style;
|
2014-03-08 23:43:27 +07:00
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2015-10-10 19:22:19 +04:00
|
|
|
use PhpOffice\PhpWord\SimpleType\Jc;
|
2015-11-15 13:33:05 +04:00
|
|
|
use PhpOffice\PhpWord\TestHelperDOCX;
|
2014-03-08 23:43:27 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-03-27 23:55:06 +07:00
|
|
|
* Test class for PhpOffice\PhpWord\Style\Font
|
|
|
|
|
*
|
2014-03-08 23:43:27 +07:00
|
|
|
* @runTestsInSeparateProcesses
|
|
|
|
|
*/
|
2017-11-09 06:36:47 -02:00
|
|
|
class FontTest extends \PHPUnit\Framework\TestCase
|
2014-03-08 23:43:27 +07:00
|
|
|
{
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* Tear down after each test
|
|
|
|
|
*/
|
2014-03-09 15:03:53 -04:00
|
|
|
public function tearDown()
|
|
|
|
|
{
|
|
|
|
|
TestHelperDOCX::clear();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-08 23:43:27 +07:00
|
|
|
/**
|
|
|
|
|
* Test initiation for style type and paragraph style
|
|
|
|
|
*/
|
|
|
|
|
public function testInitiation()
|
|
|
|
|
{
|
2016-06-04 20:06:37 +04:00
|
|
|
$object = new Font('text', array('alignment' => Jc::BOTH));
|
2014-03-08 23:43:27 +07:00
|
|
|
|
2016-06-04 20:06:37 +04:00
|
|
|
$this->assertEquals('text', $object->getStyleType());
|
2014-03-19 11:04:48 +04:00
|
|
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $object->getParagraphStyle());
|
2017-11-04 22:44:12 +01:00
|
|
|
$this->assertInternalType('array', $object->getStyleValues());
|
2014-03-08 23:43:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test setting style values with null or empty value
|
|
|
|
|
*/
|
|
|
|
|
public function testSetStyleValueWithNullOrEmpty()
|
|
|
|
|
{
|
2014-03-19 11:04:48 +04:00
|
|
|
$object = new Font();
|
2014-03-08 23:43:27 +07:00
|
|
|
|
|
|
|
|
$attributes = array(
|
2015-02-06 22:28:31 +04:00
|
|
|
'name' => null,
|
|
|
|
|
'size' => null,
|
|
|
|
|
'hint' => null,
|
|
|
|
|
'color' => null,
|
|
|
|
|
'bold' => false,
|
|
|
|
|
'italic' => false,
|
|
|
|
|
'underline' => Font::UNDERLINE_NONE,
|
|
|
|
|
'superScript' => false,
|
|
|
|
|
'subScript' => false,
|
|
|
|
|
'strikethrough' => false,
|
2014-05-13 11:05:12 +07:00
|
|
|
'doubleStrikethrough' => false,
|
2015-02-06 22:28:31 +04:00
|
|
|
'smallCaps' => false,
|
|
|
|
|
'allCaps' => false,
|
2017-11-05 21:39:10 +01:00
|
|
|
'rtl' => false,
|
2015-02-06 22:28:31 +04:00
|
|
|
'fgColor' => null,
|
|
|
|
|
'bgColor' => null,
|
|
|
|
|
'scale' => null,
|
|
|
|
|
'spacing' => null,
|
|
|
|
|
'kerning' => null,
|
2017-09-27 00:40:08 +02:00
|
|
|
'lang' => null,
|
2018-12-09 01:21:59 +03:00
|
|
|
'hidden' => false,
|
2014-03-08 23:43:27 +07:00
|
|
|
);
|
|
|
|
|
foreach ($attributes as $key => $default) {
|
2014-05-13 11:05:12 +07:00
|
|
|
$get = is_bool($default) ? "is{$key}" : "get{$key}";
|
2014-05-25 22:51:14 +07:00
|
|
|
$this->assertEquals($default, $object->$get());
|
2015-02-06 22:28:31 +04:00
|
|
|
$object->setStyleValue($key, null);
|
2014-03-09 21:59:23 +07:00
|
|
|
$this->assertEquals($default, $object->$get());
|
2015-02-06 22:28:31 +04:00
|
|
|
$object->setStyleValue($key, '');
|
2014-03-09 21:59:23 +07:00
|
|
|
$this->assertEquals($default, $object->$get());
|
2014-03-08 23:43:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test setting style values with normal value
|
|
|
|
|
*/
|
|
|
|
|
public function testSetStyleValueNormal()
|
|
|
|
|
{
|
2014-03-19 11:04:48 +04:00
|
|
|
$object = new Font();
|
2014-03-08 23:43:27 +07:00
|
|
|
|
|
|
|
|
$attributes = array(
|
2015-02-06 22:28:31 +04:00
|
|
|
'name' => 'Times New Roman',
|
|
|
|
|
'size' => 9,
|
|
|
|
|
'color' => '999999',
|
|
|
|
|
'hint' => 'eastAsia',
|
|
|
|
|
'bold' => true,
|
|
|
|
|
'italic' => true,
|
|
|
|
|
'underline' => Font::UNDERLINE_HEAVY,
|
|
|
|
|
'superScript' => true,
|
|
|
|
|
'subScript' => false,
|
|
|
|
|
'strikethrough' => true,
|
2014-05-13 11:05:12 +07:00
|
|
|
'doubleStrikethrough' => false,
|
2015-02-06 22:28:31 +04:00
|
|
|
'smallCaps' => true,
|
|
|
|
|
'allCaps' => false,
|
|
|
|
|
'fgColor' => Font::FGCOLOR_YELLOW,
|
|
|
|
|
'bgColor' => 'FFFF00',
|
|
|
|
|
'lineHeight' => 2,
|
|
|
|
|
'scale' => 150,
|
|
|
|
|
'spacing' => 240,
|
|
|
|
|
'kerning' => 10,
|
2017-11-05 21:39:10 +01:00
|
|
|
'rtl' => true,
|
2018-03-11 13:27:35 +01:00
|
|
|
'noProof' => true,
|
2017-11-05 21:39:10 +01:00
|
|
|
'lang' => new Language(Language::EN_US),
|
2018-12-09 01:21:59 +03:00
|
|
|
'hidden' => true,
|
2014-03-08 23:43:27 +07:00
|
|
|
);
|
2014-05-07 09:47:02 +07:00
|
|
|
$object->setStyleByArray($attributes);
|
2014-03-08 23:43:27 +07:00
|
|
|
foreach ($attributes as $key => $value) {
|
2014-05-13 11:05:12 +07:00
|
|
|
$get = is_bool($value) ? "is{$key}" : "get{$key}";
|
2014-03-09 21:59:23 +07:00
|
|
|
$this->assertEquals($value, $object->$get());
|
2014-03-08 23:43:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
2014-03-09 15:03:53 -04:00
|
|
|
|
2014-03-28 13:50:53 +07:00
|
|
|
/**
|
|
|
|
|
* Test set line height
|
|
|
|
|
*/
|
2014-03-09 15:03:53 -04:00
|
|
|
public function testLineHeight()
|
|
|
|
|
{
|
2014-03-20 16:54:12 +04:00
|
|
|
$phpWord = new PhpWord();
|
2014-04-02 11:02:56 +07:00
|
|
|
$section = $phpWord->addSection();
|
2014-03-09 15:03:53 -04:00
|
|
|
|
|
|
|
|
// Test style array
|
2016-06-04 20:06:37 +04:00
|
|
|
$text = $section->addText('This is a test', array('line-height' => 2.0));
|
2014-03-09 15:03:53 -04:00
|
|
|
|
2014-03-20 16:54:12 +04:00
|
|
|
$doc = TestHelperDOCX::getDocument($phpWord);
|
2014-03-09 15:03:53 -04:00
|
|
|
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing');
|
|
|
|
|
|
|
|
|
|
$lineHeight = $element->getAttribute('w:line');
|
|
|
|
|
$lineRule = $element->getAttribute('w:lineRule');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(480, $lineHeight);
|
|
|
|
|
$this->assertEquals('auto', $lineRule);
|
|
|
|
|
|
|
|
|
|
// Test setter
|
|
|
|
|
$text->getFontStyle()->setLineHeight(3.0);
|
2014-03-20 16:54:12 +04:00
|
|
|
$doc = TestHelperDOCX::getDocument($phpWord);
|
2014-03-09 15:03:53 -04:00
|
|
|
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing');
|
|
|
|
|
|
|
|
|
|
$lineHeight = $element->getAttribute('w:line');
|
|
|
|
|
$lineRule = $element->getAttribute('w:lineRule');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(720, $lineHeight);
|
|
|
|
|
$this->assertEquals('auto', $lineRule);
|
|
|
|
|
}
|
2014-03-28 13:50:53 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test line height floatval
|
|
|
|
|
*/
|
|
|
|
|
public function testLineHeightFloatval()
|
|
|
|
|
{
|
2015-10-10 19:22:19 +04:00
|
|
|
$object = new Font(null, array('alignment' => Jc::CENTER));
|
2014-03-28 13:50:53 +07:00
|
|
|
$object->setLineHeight('1.5pt');
|
|
|
|
|
$this->assertEquals(1.5, $object->getLineHeight());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test line height exception by using nonnumeric value
|
|
|
|
|
*
|
2014-03-31 01:13:02 +07:00
|
|
|
* @expectedException \PhpOffice\PhpWord\Exception\InvalidStyleException
|
2014-03-28 13:50:53 +07:00
|
|
|
*/
|
|
|
|
|
public function testLineHeightException()
|
|
|
|
|
{
|
|
|
|
|
$object = new Font();
|
|
|
|
|
$object->setLineHeight('a');
|
|
|
|
|
}
|
2017-11-05 21:39:10 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test setting the language as a string
|
|
|
|
|
*/
|
|
|
|
|
public function testSetLangAsString()
|
|
|
|
|
{
|
|
|
|
|
$object = new Font();
|
|
|
|
|
$object->setLang(Language::FR_BE);
|
|
|
|
|
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Language', $object->getLang());
|
|
|
|
|
$this->assertEquals(Language::FR_BE, $object->getLang()->getLatin());
|
|
|
|
|
}
|
2014-03-11 21:44:54 +07:00
|
|
|
}
|