Font: New `scale`, `spacing`, and `kerning` property of font style

This commit is contained in:
Ivan Lanin 2014-06-04 22:57:59 +07:00
parent 9c8620ecd7
commit a2294b4b1e
9 changed files with 244 additions and 87 deletions

View File

@ -7,6 +7,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
### Features
- Element: Ability to add drawing shapes (arc, curve, line, polyline, rect, oval) using new `Shape` element - @ivanlanin GH-123
- Font: New `scale`, `spacing`, and `kerning` property of font style - @ivanlanin
### Bugfixes

View File

@ -23,22 +23,43 @@ $section->addText('I am styled by a font style definition.', 'rStyle');
$section->addText('I am styled by a paragraph style definition.', null, 'pStyle');
$section->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle');
$section->addPageBreak();
$section->addTextBreak();
// Inline font style
$fontStyle['name'] = 'Times New Roman';
$fontStyle['size'] = 20;
$fontStyle['bold'] = true;
$fontStyle['italic'] = true;
$fontStyle['underline'] = 'dash';
$fontStyle['strikethrough'] = true;
$fontStyle['superScript'] = true;
$fontStyle['color'] = 'FF0000';
$fontStyle['fgColor'] = 'yellow';
$fontStyle['smallCaps'] = true;
$section->addText('I am inline styled.', $fontStyle);
$section->addTextBreak();
$textrun = $section->addTextRun();
$textrun->addText('I am inline styled ', $fontStyle);
$textrun->addText('with ');
$textrun->addText('color', array('color' => '996699'));
$textrun->addText(', ');
$textrun->addText('bold', array('bold' => true));
$textrun->addText(', ');
$textrun->addText('italic', array('italic' => true));
$textrun->addText(', ');
$textrun->addText('underline', array('underline' => 'dash'));
$textrun->addText(', ');
$textrun->addText('strikethrough', array('strikethrough' => true));
$textrun->addText(', ');
$textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true));
$textrun->addText(', ');
$textrun->addText('superScript', array('superScript' => true));
$textrun->addText(', ');
$textrun->addText('subScript', array('subScript' => true));
$textrun->addText(', ');
$textrun->addText('smallCaps', array('smallCaps' => true));
$textrun->addText(', ');
$textrun->addText('allCaps', array('allCaps' => true));
$textrun->addText(', ');
$textrun->addText('fgColor', array('fgColor' => 'yellow'));
$textrun->addText(', ');
$textrun->addText('scale', array('scale' => 200));
$textrun->addText(', ');
$textrun->addText('spacing', array('spacing' => 120));
$textrun->addText(', ');
$textrun->addText('kerning', array('kerning' => 10));
$textrun->addText('. ');
// Link
$section->addLink('http://www.google.com', 'Google');

View File

@ -75,6 +75,7 @@ abstract class IOFactory
*/
public static function load($filename, $readerName = 'Word2007')
{
/** @var \PhpOffice\PhpWord\Reader\ReaderInterface $reader */
$reader = self::createReader($readerName);
return $reader->load($filename);

View File

@ -126,6 +126,24 @@ abstract class AbstractStyle
return $this;
}
/**
* Return style value of child style object, e.g. `left` from `Indentation` child style of `Paragraph`
*
* @param \PhpOffice\PhpWord\Style\AbstractStyle $substyleObject
* @param string $substyleProperty
* @return mixed
* @since 0.12.0
*/
public function getChildStyleValue($substyleObject, $substyleProperty)
{
if ($substyleObject !== null) {
$method = "get{$substyleProperty}";
return $substyleObject->$method();
} else {
return null;
}
}
/**
* Set style value template method
*
@ -296,6 +314,24 @@ abstract class AbstractStyle
return $style;
}
/**
* Set $property value and set $pairProperty = false when $value = true
*
* @param bool $property
* @param bool $pairProperty
* @param bool $value
* @return self
*/
protected function setPairedVal(&$property, &$pairProperty, $value)
{
$property = $this->setBoolVal($value, $property);
if ($value == true) {
$pairProperty = false;
}
return $this;
}
/**
* Set style using associative array
*

View File

@ -182,10 +182,31 @@ class Font extends AbstractStyle
private $fgColor;
/**
* Text line height
* Expanded/compressed text: 0-600 (percent)
*
* @var int
* @since 0.12.0
* @link http://www.schemacentral.com/sc/ooxml/e-w_w-1.html
*/
private $scale;
/**
* Character spacing adjustment: twip
*
* @var int|float
* @since 0.12.0
* @link http://www.schemacentral.com/sc/ooxml/e-w_spacing-2.html
*/
private $spacing;
/**
* Font kerning: halfpoint
*
* @var int|float
* @since 0.12.0
* @link http://www.schemacentral.com/sc/ooxml/e-w_kern-1.html
*/
private $kerning;
/**
* Paragraph style
@ -213,6 +234,46 @@ class Font extends AbstractStyle
$this->setParagraph($paragraph);
}
/**
* Get style values
*
* @return array
* @since 0.12.0
*/
public function getStyleValues()
{
$styles = array(
'name' => $this->getStyleName(),
'basic' => array(
'name' => $this->getName(),
'size' => $this->getSize(),
'color' => $this->getColor(),
'hint' => $this->getHint(),
),
'style' => array(
'bold' => $this->isBold(),
'italic' => $this->isItalic(),
'underline' => $this->getUnderline(),
'strike' => $this->isStrikethrough(),
'dStrike' => $this->isDoubleStrikethrough(),
'super' => $this->isSuperScript(),
'sub' => $this->isSubScript(),
'smallCaps' => $this->isSmallCaps(),
'allCaps' => $this->isAllCaps(),
'fgColor' => $this->getFgColor(),
),
'spacing' => array(
'scale' => $this->getScale(),
'spacing' => $this->getSpacing(),
'kerning' => $this->getKerning(),
),
'paragraph' => $this->getParagraph(),
'shading' => $this->getShading(),
);
return $styles;
}
/**
* Get style type
*
@ -236,7 +297,7 @@ class Font extends AbstractStyle
/**
* Set font name
*
* @param string $value
* @param string $value
* @return self
*/
public function setName($value = null)
@ -259,7 +320,7 @@ class Font extends AbstractStyle
/**
* Set Font Content Type
*
* @param string $value
* @param string $value
* @return self
*/
public function setHint($value = null)
@ -272,7 +333,7 @@ class Font extends AbstractStyle
/**
* Get font size
*
* @return int|float
* @return int|float
*/
public function getSize()
{
@ -282,7 +343,7 @@ class Font extends AbstractStyle
/**
* Set font size
*
* @param int|float $value
* @param int|float $value
* @return self
*/
public function setSize($value = null)
@ -305,7 +366,7 @@ class Font extends AbstractStyle
/**
* Set font color
*
* @param string $value
* @param string $value
* @return self
*/
public function setColor($value = null)
@ -328,7 +389,7 @@ class Font extends AbstractStyle
/**
* Set bold
*
* @param bool $value
* @param bool $value
* @return self
*/
public function setBold($value = true)
@ -351,7 +412,7 @@ class Font extends AbstractStyle
/**
* Set italic
*
* @param bool $value
* @param bool $value
* @return self
*/
public function setItalic($value = true)
@ -374,7 +435,7 @@ class Font extends AbstractStyle
/**
* Set underline
*
* @param string $value
* @param string $value
* @return self
*/
public function setUnderline($value = self::UNDERLINE_NONE)
@ -397,12 +458,12 @@ class Font extends AbstractStyle
/**
* Set superscript
*
* @param bool $value
* @param bool $value
* @return self
*/
public function setSuperScript($value = true)
{
return $this->setPairedProperty($this->superScript, $this->subScript, $value);
return $this->setPairedVal($this->superScript, $this->subScript, $value);
}
/**
@ -418,12 +479,12 @@ class Font extends AbstractStyle
/**
* Set subscript
*
* @param bool $value
* @param bool $value
* @return self
*/
public function setSubScript($value = true)
{
return $this->setPairedProperty($this->subScript, $this->superScript, $value);
return $this->setPairedVal($this->subScript, $this->superScript, $value);
}
/**
@ -439,12 +500,12 @@ class Font extends AbstractStyle
/**
* Set strikethrough
*
* @param bool $value
* @param bool $value
* @return self
*/
public function setStrikethrough($value = true)
{
return $this->setPairedProperty($this->strikethrough, $this->doubleStrikethrough, $value);
return $this->setPairedVal($this->strikethrough, $this->doubleStrikethrough, $value);
}
/**
@ -460,12 +521,12 @@ class Font extends AbstractStyle
/**
* Set double strikethrough
*
* @param bool $value
* @param bool $value
* @return self
*/
public function setDoubleStrikethrough($value = true)
{
return $this->setPairedProperty($this->doubleStrikethrough, $this->strikethrough, $value);
return $this->setPairedVal($this->doubleStrikethrough, $this->strikethrough, $value);
}
/**
@ -481,12 +542,12 @@ class Font extends AbstractStyle
/**
* Set small caps
*
* @param bool $value
* @param bool $value
* @return self
*/
public function setSmallCaps($value = true)
{
return $this->setPairedProperty($this->smallCaps, $this->allCaps, $value);
return $this->setPairedVal($this->smallCaps, $this->allCaps, $value);
}
/**
@ -502,12 +563,12 @@ class Font extends AbstractStyle
/**
* Set all caps
*
* @param bool $value
* @param bool $value
* @return self
*/
public function setAllCaps($value = true)
{
return $this->setPairedProperty($this->allCaps, $this->smallCaps, $value);
return $this->setPairedVal($this->allCaps, $this->smallCaps, $value);
}
/**
@ -523,7 +584,7 @@ class Font extends AbstractStyle
/**
* Set foreground/highlight color
*
* @param string $value
* @param string $value
* @return self
*/
public function setFgColor($value = null)
@ -540,11 +601,7 @@ class Font extends AbstractStyle
*/
public function getBgColor()
{
if ($this->shading !== null) {
return $this->shading->getFill();
} else {
return null;
}
return $this->getChildStyleValue($this->shading, 'fill');
}
/**
@ -558,6 +615,75 @@ class Font extends AbstractStyle
$this->setShading(array('fill' => $value));
}
/**
* Get scale
*
* @return int
*/
public function getScale()
{
return $this->scale;
}
/**
* Set scale
*
* @param int $value
* @return self
*/
public function setScale($value = null)
{
$this->scale = $this->setIntVal($value, null);
return $this;
}
/**
* Get font spacing
*
* @return int|float
*/
public function getSpacing()
{
return $this->spacing;
}
/**
* Set font spacing
*
* @param int|float $value
* @return self
*/
public function setSpacing($value = null)
{
$this->spacing = $this->setNumericVal($value, null);
return $this;
}
/**
* Get font kerning
*
* @return int|float
*/
public function getKerning()
{
return $this->kerning;
}
/**
* Set font kerning
*
* @param int|float $value
* @return self
*/
public function setKerning($value = null)
{
$this->kerning = $this->setNumericVal($value, null);
return $this;
}
/**
* Get line height
*
@ -571,7 +697,7 @@ class Font extends AbstractStyle
/**
* Set lineheight
*
* @param int|float|string $value
* @param int|float|string $value
* @return self
*/
public function setLineHeight($value)
@ -627,24 +753,6 @@ class Font extends AbstractStyle
return $this;
}
/**
* Set $property value and set $pairProperty = false when $value = true
*
* @param bool $property
* @param bool $pairProperty
* @param bool $value
* @return self
*/
private function setPairedProperty(&$property, &$pairProperty, $value)
{
$property = $this->setBoolVal($value, $property);
if ($value == true) {
$pairProperty = false;
}
return $this;
}
/**
* Get bold
*

View File

@ -313,11 +313,7 @@ class Paragraph extends AbstractStyle
*/
public function getIndent()
{
if ($this->indentation !== null) {
return $this->indentation->getLeft();
} else {
return null;
}
return $this->getChildStyleValue($this->indentation, 'left');
}
/**
@ -338,11 +334,7 @@ class Paragraph extends AbstractStyle
*/
public function getHanging()
{
if ($this->indentation !== null) {
return $this->indentation->getHanging();
} else {
return null;
}
return $this->getChildStyleValue($this->indentation, 'hanging');
}
/**
@ -388,11 +380,7 @@ class Paragraph extends AbstractStyle
*/
public function getSpaceBefore()
{
if ($this->spacing !== null) {
return $this->spacing->getBefore();
} else {
return null;
}
return $this->getChildStyleValue($this->spacing, 'before');
}
/**
@ -413,11 +401,7 @@ class Paragraph extends AbstractStyle
*/
public function getSpaceAfter()
{
if ($this->spacing !== null) {
return $this->spacing->getAfter();
} else {
return null;
}
return $this->getChildStyleValue($this->spacing, 'after');
}
/**
@ -438,11 +422,7 @@ class Paragraph extends AbstractStyle
*/
public function getSpacing()
{
if ($this->spacing !== null) {
return $this->spacing->getLine();
} else {
return null;
}
return $this->getChildStyleValue($this->spacing, 'line');
}
/**

View File

@ -103,17 +103,20 @@ class Font extends AbstractStyle
$xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps');
// Underline
$underline = $style->getUnderline();
$xmlWriter->writeElementIf($underline != 'none', 'w:u', 'w:val', $underline);
$xmlWriter->writeElementIf($style->getUnderline() != 'none', 'w:u', 'w:val', $style->getUnderline());
// Foreground-Color
$fgColor = $style->getFgColor();
$xmlWriter->writeElementIf(!is_null($fgColor), 'w:highlight', 'w:val', $fgColor);
$xmlWriter->writeElementIf($style->getFgColor() !== null, 'w:highlight', 'w:val', $style->getFgColor());
// Superscript/subscript
$xmlWriter->writeElementIf($style->isSuperScript(), 'w:vertAlign', 'w:val', 'superscript');
$xmlWriter->writeElementIf($style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript');
// Spacing
$xmlWriter->writeElementIf($style->getScale() !== null, 'w:w', 'w:val', $style->getScale());
$xmlWriter->writeElementIf($style->getSpacing() !== null, 'w:spacing', 'w:val', $style->getSpacing());
$xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2);
// Background-Color
$shading = $style->getShading();
if (!is_null($shading)) {

View File

@ -71,6 +71,9 @@ class FontTest extends \PHPUnit_Framework_TestCase
'allCaps' => false,
'fgColor' => null,
'bgColor' => null,
'scale' => null,
'spacing' => null,
'kerning' => null,
);
foreach ($attributes as $key => $default) {
$get = is_bool($default) ? "is{$key}" : "get{$key}";
@ -106,6 +109,9 @@ class FontTest extends \PHPUnit_Framework_TestCase
'fgColor' => Font::FGCOLOR_YELLOW,
'bgColor' => 'FFFF00',
'lineHeight' => 2,
'scale' => 150,
'spacing' => 240,
'kerning' => 10,
);
$object->setStyleByArray($attributes);
foreach ($attributes as $key => $value) {

View File

@ -136,7 +136,8 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
$tabs = array(new \PhpOffice\PhpWord\Style\Tab('right', 9090));
$phpWord = new PhpWord();
$phpWord->addParagraphStyle('pStyle', array('align' => 'center', 'tabs' => $tabs)); // Style #1
$phpWord->addFontStyle('fStyle', array('size' => '20', 'bold' => true, 'allCaps' => true)); // Style #2
$phpWord->addFontStyle('fStyle', array('size' => '20', 'bold' => true, 'allCaps' => true,
'scale' => 200, 'spacing' => 240, 'kerning' => 10)); // Style #2
$phpWord->addTitleStyle(1, array('color' => '333333', 'doubleStrikethrough' => true)); // Style #3
$phpWord->addTableStyle('tStyle', array('borderSize' => 1));
$fontStyle = new Font('text', array('align' => 'center'));