#151 Small caps, all caps, and double strikethrough; #199 Ability to use measurement unit other than twips

This commit is contained in:
Ivan Lanin 2014-05-01 23:56:55 +07:00
parent 1a1356a30b
commit 1d84c1db33
15 changed files with 197 additions and 43 deletions

View File

@ -39,7 +39,9 @@ This release marked heavy refactorings on internal code structure with the creat
- ODT Writer: Basic image writing - @ivanlanin
- ODT Writer: Link writing - @ivanlanin
- ODT Reader: Basic ODText Reader - @ivanlanin GH-71
- Section: Ability to define gutter and line numbering
- Section: Ability to define gutter and line numbering - @ivanlanin
- Font: Small caps, all caps, and double strikethrough - @ivanlanin GH-151
- Settings: Ability to use measurement unit other than twips with `setMeasurementUnit` - @ivanlanin GH-199
### Bugfixes

View File

@ -112,9 +112,12 @@ Available font styles:
- ``subScript`` Subscript, *true* or *false*
- ``underline`` Underline, *dash*, *dotted*, etc.
- ``strikethrough`` Strikethrough, *true* or *false*
- ``doubleStrikethrough`` Double strikethrough, *true* or *false*
- ``color`` Font color, e.g. *FF0000*
- ``fgColor`` Font highlight color, e.g. *yellow*, *green*, *blue*
- ``bgColor`` Font background color, e.g. *FF0000*
- ``smallCaps`` Small caps, *true* or *false*
- ``allCaps`` All caps, *true* or *false*
Paragraph style
^^^^^^^^^^^^^^^

View File

@ -515,9 +515,12 @@ Available font styles:
- `subScript` Subscript, *true* or *false*
- `underline` Underline, *dash*, *dotted*, etc.
- `strikethrough` Strikethrough, *true* or *false*
- `doubleStrikethrough` Double strikethrough, *true* or *false*
- `color` Font color, e.g. *FF0000*
- `fgColor` Font highlight color, e.g. *yellow*, *green*, *blue*
- `bgColor` Font background color, e.g. *FF0000*
- `smallCaps` Small caps, *true* or *false*
- `allCaps` All caps, *true* or *false*
#### Paragraph style

View File

@ -4,7 +4,7 @@ include_once 'Sample_Header.php';
// New Word Document
echo date('H:i:s') , " Create new PhpWord object" , EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16));
$phpWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
$phpWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100));
$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
@ -34,6 +34,7 @@ $fontStyle['strikethrough'] = true;
$fontStyle['superScript'] = true;
$fontStyle['color'] = 'FF0000';
$fontStyle['fgColor'] = 'yellow';
$fontStyle['smallCaps'] = true;
$section->addText('I am inline styled.', $fontStyle);
$section->addTextBreak();

View File

@ -17,6 +17,11 @@ use PhpOffice\PhpWord\Exception\InvalidStyleException;
*/
class Font extends AbstractStyle
{
/**
* Underline types
*
* @const string
*/
const UNDERLINE_NONE = 'none';
const UNDERLINE_DASH = 'dash';
const UNDERLINE_DASHHEAVY = 'dashHeavy';
@ -35,6 +40,12 @@ class Font extends AbstractStyle
const UNDERLINE_WAVYDOUBLE = 'wavyDbl';
const UNDERLINE_WAVYHEAVY = 'wavyHeavy';
const UNDERLINE_WORDS = 'words';
/**
* Foreground colors
*
* @const string
*/
const FGCOLOR_YELLOW = 'yellow';
const FGCOLOR_LIGHTGREEN = 'green';
const FGCOLOR_CYAN = 'cyan';
@ -121,6 +132,13 @@ class Font extends AbstractStyle
*/
private $strikethrough = false;
/**
* Double strikethrough
*
* @var bool
*/
private $doubleStrikethrough = false;
/**
* Font color
*
@ -161,6 +179,22 @@ class Font extends AbstractStyle
*/
private $hint = PhpWord::DEFAULT_FONT_CONTENT_TYPE;
/**
* Small caps
*
* @var bool
* @link http://www.schemacentral.com/sc/ooxml/e-w_smallCaps-1.html
*/
private $smallCaps = false;
/**
* All caps
*
* @var bool
* @link http://www.schemacentral.com/sc/ooxml/e-w_caps-1.html
*/
private $allCaps = false;
/**
* Create new font style
*
@ -387,6 +421,35 @@ class Font extends AbstractStyle
public function setStrikethrough($value = false)
{
$this->strikethrough = $this->setBoolVal($value, $this->strikethrough);
if ($this->strikethrough) {
$this->doubleStrikethrough = false;
}
return $this;
}
/**
* Get double strikethrough
*
* @return bool
*/
public function getDoubleStrikethrough()
{
return $this->doubleStrikethrough;
}
/**
* Set double strikethrough
*
* @param bool $value
* @return self
*/
public function setDoubleStrikethrough($value = false)
{
$this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough);
if ($this->doubleStrikethrough) {
$this->strikethrough = false;
}
return $this;
}
@ -534,4 +597,56 @@ class Font extends AbstractStyle
return $this;
}
/**
* Get small caps
*
* @return bool
*/
public function getSmallCaps()
{
return $this->smallCaps;
}
/**
* Set small caps
*
* @param bool $value
* @return self
*/
public function setSmallCaps($value = false)
{
$this->smallCaps = $this->setBoolVal($value, $this->smallCaps);
if ($this->smallCaps) {
$this->allCaps = false;
}
return $this;
}
/**
* Get all caps
*
* @return bool
*/
public function getAllCaps()
{
return $this->allCaps;
}
/**
* Set all caps
*
* @param bool $value
* @return self
*/
public function setAllCaps($value = false)
{
$this->allCaps = $this->setBoolVal($value, $this->allCaps);
if ($this->allCaps) {
$this->smallCaps = false;
}
return $this;
}
}

View File

@ -52,6 +52,8 @@ class LineNumbering extends AbstractStyle
/**
* Create a new instance
*
* @param array $style
*/
public function __construct($style = null)
{

View File

@ -162,6 +162,9 @@ class Section extends Border
/**
* Set orientation
*
* @param string $value
* @return self
*/
public function setOrientation($value = null)
{
@ -177,6 +180,8 @@ class Section extends Border
$this->pageSizeW = $longSize;
$this->pageSizeH = $shortSize;
}
return $this;
}
/**
@ -191,18 +196,22 @@ class Section extends Border
/**
* Set Portrait Orientation
*
* @return self
*/
public function setPortrait()
{
$this->setOrientation(self::ORIENTATION_PORTRAIT);
return $this->setOrientation(self::ORIENTATION_PORTRAIT);
}
/**
* Set Landscape Orientation
*
* @return self
*/
public function setLandscape()
{
$this->setOrientation(self::ORIENTATION_LANDSCAPE);
return $this->setOrientation(self::ORIENTATION_LANDSCAPE);
}
/**

View File

@ -31,8 +31,8 @@ class Text extends Element
$html = '';
// Paragraph style
$paragraphStyle = $this->element->getParagraphStyle();
$paragraphStyleIsObject = ($paragraphStyle instanceof Paragraph);
if ($paragraphStyleIsObject) {
$pStyleIsObject = ($paragraphStyle instanceof Paragraph);
if ($pStyleIsObject) {
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
$paragraphStyle = $styleWriter->write();
}
@ -45,7 +45,7 @@ class Text extends Element
}
if ($paragraphStyle && !$this->withoutP) {
$attribute = $paragraphStyleIsObject ? 'style' : 'class';
$attribute = $pStyleIsObject ? 'style' : 'class';
$html .= "<p {$attribute}=\"{$paragraphStyle}\">";
}
if ($fontStyle) {

View File

@ -31,13 +31,13 @@ class TextRun extends Element
if (count($elements) > 0) {
// Paragraph style
$paragraphStyle = $this->element->getParagraphStyle();
$paragraphStyleIsObject = ($paragraphStyle instanceof Paragraph);
if ($paragraphStyleIsObject) {
$pStyleIsObject = ($paragraphStyle instanceof Paragraph);
if ($pStyleIsObject) {
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
$paragraphStyle = $styleWriter->write();
}
$tag = $this->withoutP ? 'span' : 'p';
$attribute = $paragraphStyleIsObject ? 'style' : 'class';
$attribute = $pStyleIsObject ? 'style' : 'class';
$html .= "<{$tag} {$attribute}=\"{$paragraphStyle}\">";
foreach ($elements as $element) {
$elementWriter = new Element($this->parentWriter, $element, true);

View File

@ -52,21 +52,13 @@ class Font extends AbstractStyle
}
$font = $this->style->getName();
$bold = $this->style->getBold();
$italic = $this->style->getItalic();
$color = $this->style->getColor();
$size = $this->style->getSize();
$fgColor = $this->style->getFgColor();
$bgColor = $this->style->getBgColor();
$strikethrough = $this->style->getStrikethrough();
$underline = $this->style->getUnderline();
$superscript = $this->style->getSuperScript();
$subscript = $this->style->getSubScript();
$hint = $this->style->getHint();
$this->xmlWriter->startElement('w:rPr');
// Font
// Font name/family
if ($font != PhpWord::DEFAULT_FONT_NAME) {
$this->xmlWriter->startElement('w:rFonts');
$this->xmlWriter->writeAttribute('w:ascii', $font);
@ -74,13 +66,12 @@ class Font extends AbstractStyle
$this->xmlWriter->writeAttribute('w:eastAsia', $font);
$this->xmlWriter->writeAttribute('w:cs', $font);
//Font Content Type
if ($hint != PhpWord::DEFAULT_FONT_CONTENT_TYPE) {
$this->xmlWriter->writeAttribute('w:hint', $hint);
if ($this->style->getHint() != PhpWord::DEFAULT_FONT_CONTENT_TYPE) {
$this->xmlWriter->writeAttribute('w:hint', $this->style->getHint());
}
$this->xmlWriter->endElement();
}
// Color
if ($color != PhpWord::DEFAULT_FONT_COLOR) {
$this->xmlWriter->startElement('w:color');
@ -99,32 +90,37 @@ class Font extends AbstractStyle
}
// Bold
if ($bold) {
if ($this->style->getBold()) {
$this->xmlWriter->writeElement('w:b', null);
}
// Italic
if ($italic) {
if ($this->style->getItalic()) {
$this->xmlWriter->writeElement('w:i', null);
$this->xmlWriter->writeElement('w:iCs', null);
}
// Underline
if (!is_null($underline) && $underline != 'none') {
if ($this->style->getUnderline() != 'none') {
$this->xmlWriter->startElement('w:u');
$this->xmlWriter->writeAttribute('w:val', $underline);
$this->xmlWriter->writeAttribute('w:val', $this->style->getUnderline());
$this->xmlWriter->endElement();
}
// Strikethrough
if ($strikethrough) {
if ($this->style->getStrikethrough()) {
$this->xmlWriter->writeElement('w:strike', null);
}
// Double strikethrough
if ($this->style->getDoubleStrikethrough()) {
$this->xmlWriter->writeElement('w:dstrike', null);
}
// Foreground-Color
if (!is_null($fgColor)) {
if (!is_null($this->style->getFgColor())) {
$this->xmlWriter->startElement('w:highlight');
$this->xmlWriter->writeAttribute('w:val', $fgColor);
$this->xmlWriter->writeAttribute('w:val', $this->style->getFgColor());
$this->xmlWriter->endElement();
}
@ -138,12 +134,22 @@ class Font extends AbstractStyle
}
// Superscript/subscript
if ($superscript || $subscript) {
if ($this->style->getSuperScript() || $this->style->getSubScript()) {
$this->xmlWriter->startElement('w:vertAlign');
$this->xmlWriter->writeAttribute('w:val', $superscript ? 'superscript' : 'subscript');
$this->xmlWriter->writeAttribute('w:val', $this->style->getSuperScript() ? 'superscript' : 'subscript');
$this->xmlWriter->endElement();
}
// Small caps
if ($this->style->getSmallCaps()) {
$this->xmlWriter->writeElement('w:smallCaps', null);
}
// All caps
if ($this->style->getAllCaps()) {
$this->xmlWriter->writeElement('w:caps', null);
}
$this->xmlWriter->endElement();
}

View File

@ -9,8 +9,6 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Settings;
/**
* Margin border style writer
*
@ -46,7 +44,6 @@ class MarginBorder extends AbstractStyle
{
$sides = array('top', 'left', 'right', 'bottom', 'insideH', 'insideV');
$sizeCount = count($this->sizes) - 1;
$unit = Settings::getMeasurementUnit();
for ($i = 0; $i < $sizeCount; $i++) {
if (!is_null($this->sizes[$i])) {

View File

@ -130,7 +130,6 @@ class Table extends AbstractStyle
for ($i = 0; $i < 6; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
if ($hasBorders) {

View File

@ -52,4 +52,14 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($domPdfPath, Settings::getPdfRendererPath());
$this->assertFalse(Settings::setPdfRendererPath('dummy/path'));
}
/**
* Test set/get measurement unit
*/
public function testSetGetMeasurementUnit()
{
$this->assertEquals(Settings::UNIT_TWIP, Settings::getMeasurementUnit());
$this->assertTrue(Settings::setMeasurementUnit(Settings::UNIT_INCH));
$this->assertFalse(Settings::setMeasurementUnit('foo'));
}
}

View File

@ -24,19 +24,17 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
*/
public function testSettingValue()
{
// Section Settings
$oSettings = new Section();
$this->assertEquals('portrait', $oSettings->getOrientation());
$this->assertEquals(11906, $oSettings->getPageSizeW());
$this->assertEquals(16838, $oSettings->getPageSizeH());
$oSettings->setSettingValue('orientation', 'landscape');
$this->assertEquals('landscape', $oSettings->getOrientation());
$this->assertEquals(16838, $oSettings->getPageSizeW());
$this->assertEquals(11906, $oSettings->getPageSizeH());
$oSettings->setSettingValue('orientation', null);
$this->assertEquals('portrait', $oSettings->getOrientation());
$this->assertEquals(11906, $oSettings->getPageSizeW());
$this->assertEquals(16838, $oSettings->getPageSizeH());
$iVal = rand(1, 1000);
$oSettings->setSettingValue('borderSize', $iVal);
$this->assertEquals(array($iVal, $iVal, $iVal, $iVal), $oSettings->getBorderSize());
@ -55,6 +53,13 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$iVal = rand(1, 1000);
$oSettings->setSettingValue('headerHeight', $iVal);
$this->assertEquals($iVal, $oSettings->getHeaderHeight());
$oSettings->setSettingValue('lineNumbering', array());
$oSettings->setSettingValue('lineNumbering', array('start' => 1, 'increment' => 1, 'distance' => 240, 'restart' => 'newPage'));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\LineNumbering', $oSettings->getLineNumbering());
$oSettings->removeLineNumbering();
$this->assertNull($oSettings->getLineNumbering());
}
/**
@ -105,7 +110,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$oSettings = new Section();
$oSettings->setPortrait();
$this->assertNull($oSettings->getOrientation());
$this->assertEquals('portrait', $oSettings->getOrientation());
$this->assertEquals(11906, $oSettings->getPageSizeW());
$this->assertEquals(16838, $oSettings->getPageSizeH());
}

View File

@ -113,7 +113,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
$phpWord = new PhpWord();
$phpWord->addParagraphStyle('pStyle', array('align' => 'center')); // Style #1
$phpWord->addFontStyle('fStyle', array('size' => '20')); // Style #2
$phpWord->addFontStyle('fStyle', array('size' => '20', 'doubleStrikethrough' => true, 'allCaps' => true)); // Style #2
$phpWord->addTitleStyle(1, array('color' => '333333', 'bold' => true)); // Style #3
$fontStyle = new Font('text', array('align' => 'center'));
$section = $phpWord->addSection();
@ -391,6 +391,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
$styles['fgColor'] = 'yellow';
$styles['bgColor'] = 'FFFF00';
$styles['hint'] = 'eastAsia';
$styles['smallCaps'] = true;
$section = $phpWord->addSection();
$section->addText('Test', $styles);
@ -406,6 +407,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('superscript', $doc->getElementAttribute("{$parent}/w:vertAlign", 'w:val'));
$this->assertEquals($styles['color'], $doc->getElementAttribute("{$parent}/w:color", 'w:val'));
$this->assertEquals($styles['fgColor'], $doc->getElementAttribute("{$parent}/w:highlight", 'w:val'));
$this->assertTrue($doc->elementExists("{$parent}/w:smallCaps"));
}
/**