Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement character spacing #301

Merged
merged 10 commits into from
Jan 15, 2017
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Bugfix
- PowerPoint2007 Writer : The presentation need repairs on Mac @jrking4 GH-266 GH-276

### Features
- PowerPoint2007 Writer : Implement character spacing - @jvanoostrom GH-301

## 0.7.0 - 2016-09-12

### Bugfix
Expand Down
52 changes: 43 additions & 9 deletions src/PhpPresentation/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ class Font implements ComparableInterface
*/
private $color;

/**
* Character Spacing
*
* @var int
*/
private $characterSpacing;

/**
* Hash index
*
Expand All @@ -120,15 +127,16 @@ class Font implements ComparableInterface
public function __construct()
{
// Initialise values
$this->name = 'Calibri';
$this->size = 10;
$this->bold = false;
$this->italic = false;
$this->superScript = false;
$this->subScript = false;
$this->underline = self::UNDERLINE_NONE;
$this->strikethrough = false;
$this->color = new Color(Color::COLOR_BLACK);
$this->name = 'Calibri';
$this->size = 10;
$this->characterSpacing = 0;
$this->bold = false;
$this->italic = false;
$this->superScript = false;
$this->subScript = false;
$this->underline = self::UNDERLINE_NONE;
$this->strikethrough = false;
$this->color = new Color(Color::COLOR_BLACK);
}

/**
Expand Down Expand Up @@ -156,6 +164,32 @@ public function setName($pValue = 'Calibri')

return $this;
}

/**
* Get Character Spacing
*
* @return double
*/
public function getCharacterSpacing()
{
return $this->characterSpacing;
}

/**
* Set Character Spacing
* Value in pt
* @param float|int $pValue
* @return \PhpOffice\PhpPresentation\Style\Font
*/
public function setCharacterSpacing($pValue = 0)
{
if ($pValue == '') {
$pValue = 0;
}
$this->characterSpacing = $pValue * 100;

return $this;
}

/**
* Get Size
Expand Down
3 changes: 3 additions & 0 deletions src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,9 @@ protected function writeParagraphs(XMLWriter $objWriter, $paragraphs, $bIsPlaceh

// Size
$objWriter->writeAttribute('sz', ($element->getFont()->getSize() * 100));

// Character spacing
$objWriter->writeAttribute('spc', $element->getFont()->getCharacterSpacing());

// Underline
$objWriter->writeAttribute('u', $element->getFont()->getUnderline());
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpPresentation/Tests/Style/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function testConstruct()
$this->assertFalse($object->isSubScript());
$this->assertFalse($object->isStrikethrough());
$this->assertEquals(Font::UNDERLINE_NONE, $object->getUnderline());
$this->assertEquals(0, $object->getCharacterSpacing());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Color', $object->getColor());
$this->assertEquals(Color::COLOR_BLACK, $object->getColor()->getARGB());
}
Expand All @@ -56,6 +57,21 @@ public function testSetGetColorException()
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->setColor());
}

/**
* Test get/set Character Spacing
*/
public function testSetGetCharacterSpacing()
{
$object = new Font();
$this->assertEquals(0, $object->getCharacterSpacing());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->setCharacterSpacing(0));
$this->assertEquals(0, $object->getCharacterSpacing());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->setCharacterSpacing(10));
$this->assertEquals(1000, $object->getCharacterSpacing());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->setCharacterSpacing());
$this->assertEquals(0, $object->getCharacterSpacing());
}

/**
* Test get/set color
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,27 @@ public function testSlideLayoutExists()
$this->assertTrue($pres->fileExists('ppt/slideLayouts/slideLayout1.xml'));
}

public function testStyleCharacterSpacing()
{
$element = '/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:rPr';

$oPhpPresentation = new PhpPresentation();
$oSlide = $oPhpPresentation->getActiveSlide();
$oRichText = $oSlide->createRichTextShape();
$oRun = $oRichText->createTextRun('pText');
// Default : $oRun->getFont()->setCharacterSpacing(0);

$pres = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007');
$this->assertTrue($pres->elementExists($element, 'ppt/slides/slide1.xml'));
$this->assertEquals('0', $pres->getElementAttribute($element, 'spc', 'ppt/slides/slide1.xml'));

$oRun->getFont()->setCharacterSpacing(42);

$pres = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007');
$this->assertTrue($pres->elementExists($element, 'ppt/slides/slide1.xml'));
$this->assertEquals('4200', $pres->getElementAttribute($element, 'spc', 'ppt/slides/slide1.xml'));
}

public function testStyleSubScript()
{
$oPhpPresentation = new PhpPresentation();
Expand Down