Merge pull request #87 from ivanlanin/develop
Point to twip converter, set normal paragraph style, and parent & next style for paragraph
This commit is contained in:
commit
0ed956aaf1
|
|
@ -198,6 +198,16 @@ class PHPWord
|
|||
PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default paragraph style definition to styles.xml
|
||||
*
|
||||
* @param array $styles Paragraph style definition
|
||||
*/
|
||||
public function setDefaultParagraphStyle($styles)
|
||||
{
|
||||
PHPWord_Style::setDefaultParagraphStyle($styles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a hyperlink style to styles.xml
|
||||
*
|
||||
|
|
|
|||
|
|
@ -77,4 +77,16 @@ class PHPWord_Shared_Font
|
|||
{
|
||||
return self::centimeterSizeToTwips($sizeInPixel / 37.795275591);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate twip based on point size, used mainly for paragraph spacing
|
||||
*
|
||||
* @param int|float $sizeInPoint Size in point
|
||||
* @return int|float Size (in twips)
|
||||
*/
|
||||
public static function pointSizeToTwips($sizeInPoint = 1)
|
||||
{
|
||||
return ($sizeInPoint * 20);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,6 +140,16 @@ class PHPWord_Style
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default paragraph style
|
||||
*
|
||||
* @param array $styles Paragraph style definition
|
||||
*/
|
||||
public static function setDefaultParagraphStyle($styles)
|
||||
{
|
||||
self::addParagraphStyle('Normal', $styles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all styles
|
||||
*
|
||||
|
|
|
|||
|
|
@ -80,6 +80,20 @@ class PHPWord_Style_Paragraph
|
|||
*/
|
||||
private $_hanging;
|
||||
|
||||
/**
|
||||
* Parent style
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_basedOn;
|
||||
|
||||
/**
|
||||
* Style for next paragraph
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_next;
|
||||
|
||||
/**
|
||||
* New Paragraph Style
|
||||
*/
|
||||
|
|
@ -92,6 +106,8 @@ class PHPWord_Style_Paragraph
|
|||
$this->_tabs = null;
|
||||
$this->_indent = null;
|
||||
$this->_hanging = null;
|
||||
$this->_basedOn = 'Normal';
|
||||
$this->_next = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -231,6 +247,16 @@ class PHPWord_Style_Paragraph
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hanging
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getHanging()
|
||||
{
|
||||
return $this->_hanging;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set hanging
|
||||
*
|
||||
|
|
@ -243,16 +269,6 @@ class PHPWord_Style_Paragraph
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hanging
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getHanging()
|
||||
{
|
||||
return $this->_hanging;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tabs
|
||||
*
|
||||
|
|
@ -262,4 +278,49 @@ class PHPWord_Style_Paragraph
|
|||
{
|
||||
return $this->_tabs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parent style ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBasedOn()
|
||||
{
|
||||
return $this->_basedOn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set parent style ID
|
||||
*
|
||||
* @param string $pValue
|
||||
* @return PHPWord_Style_Paragraph
|
||||
*/
|
||||
public function setBasedOn($pValue = 'Normal')
|
||||
{
|
||||
$this->_basedOn = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get style for next paragraph
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNext()
|
||||
{
|
||||
return $this->_next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set style for next paragraph
|
||||
*
|
||||
* @param string $pValue
|
||||
* @return PHPWord_Style_Paragraph
|
||||
*/
|
||||
public function setNext($pValue = null)
|
||||
{
|
||||
$this->_next = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -59,8 +59,30 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base
|
|||
|
||||
// Write Style Definitions
|
||||
$styles = PHPWord_Style::getStyles();
|
||||
|
||||
// Write normal paragraph style
|
||||
$normalStyle = null;
|
||||
if (array_key_exists('Normal', $styles)) {
|
||||
$normalStyle = $styles['Normal'];
|
||||
}
|
||||
$objWriter->startElement('w:style');
|
||||
$objWriter->writeAttribute('w:type', 'paragraph');
|
||||
$objWriter->writeAttribute('w:default', '1');
|
||||
$objWriter->writeAttribute('w:styleId', 'Normal');
|
||||
$objWriter->startElement('w:name');
|
||||
$objWriter->writeAttribute('w:val', 'Normal');
|
||||
$objWriter->endElement();
|
||||
if (!is_null($normalStyle)) {
|
||||
$this->_writeParagraphStyle($objWriter, $normalStyle);
|
||||
}
|
||||
$objWriter->endElement();
|
||||
|
||||
// Write other styles
|
||||
if (count($styles) > 0) {
|
||||
foreach ($styles as $styleName => $style) {
|
||||
if ($styleName == 'Normal') {
|
||||
continue;
|
||||
}
|
||||
if ($style instanceof PHPWord_Style_Font) {
|
||||
|
||||
$paragraphStyle = $style->getParagraphStyle();
|
||||
|
|
@ -92,6 +114,10 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base
|
|||
$objWriter->endElement();
|
||||
|
||||
if (!is_null($paragraphStyle)) {
|
||||
// Point parent style to Normal
|
||||
$objWriter->startElement('w:basedOn');
|
||||
$objWriter->writeAttribute('w:val', 'Normal');
|
||||
$objWriter->endElement();
|
||||
$this->_writeParagraphStyle($objWriter, $paragraphStyle);
|
||||
}
|
||||
|
||||
|
|
@ -109,6 +135,22 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base
|
|||
$objWriter->writeAttribute('w:val', $styleName);
|
||||
$objWriter->endElement();
|
||||
|
||||
// Parent style
|
||||
$basedOn = $style->getBasedOn();
|
||||
if (!is_null($basedOn)) {
|
||||
$objWriter->startElement('w:basedOn');
|
||||
$objWriter->writeAttribute('w:val', $basedOn);
|
||||
$objWriter->endElement();
|
||||
}
|
||||
|
||||
// Next paragraph style
|
||||
$next = $style->getNext();
|
||||
if (!is_null($next)) {
|
||||
$objWriter->startElement('w:next');
|
||||
$objWriter->writeAttribute('w:val', $next);
|
||||
$objWriter->endElement();
|
||||
}
|
||||
|
||||
$this->_writeParagraphStyle($objWriter, $style);
|
||||
$objWriter->endElement();
|
||||
|
||||
|
|
|
|||
20
README.md
20
README.md
|
|
@ -73,6 +73,26 @@ $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
|
|||
$objWriter->save('helloWorld.docx');
|
||||
```
|
||||
|
||||
##### Measurement units
|
||||
|
||||
The base length unit in Open Office XML is twip. Twip means "TWentieth of an Inch Point", i.e. 1 twip = 1/1440 inch.
|
||||
|
||||
You can use PHPWord helper functions to convert inches, centimeters, or points to twips.
|
||||
|
||||
```php
|
||||
// Paragraph with 6 points space after
|
||||
$PHPWord->addParagraphStyle('My Style', array(
|
||||
'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(6))
|
||||
);
|
||||
|
||||
$section = $PHPWord->createSection();
|
||||
$sectionStyle = $section->getSettings();
|
||||
// half inch left margin
|
||||
$sectionStyle->setMarginLeft(PHPWord_Shared_Font::inchSizeToTwips(.5));
|
||||
// 2 cm right margin
|
||||
$sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2));
|
||||
```
|
||||
|
||||
<a name="sections"></a>
|
||||
#### Sections
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
namespace PHPWord\Tests;
|
||||
|
||||
use PHPUnit_Framework_TestCase;
|
||||
use PHPWord;
|
||||
use PHPWord_Shared_Font;
|
||||
|
||||
/**
|
||||
* Class PHPWord_Writer_Shared_FontTest
|
||||
*
|
||||
* @package PHPWord\Tests
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class PHPWord_Writer_Shared_FontTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Test various conversions
|
||||
*/
|
||||
public function testConversions()
|
||||
{
|
||||
$PHPWord = new PHPWord();
|
||||
|
||||
$original = 1;
|
||||
|
||||
$result = PHPWord_Shared_Font::fontSizeToPixels($original);
|
||||
$this->assertEquals($original * 16 / 12, $result);
|
||||
|
||||
$result = PHPWord_Shared_Font::inchSizeToPixels($original);
|
||||
$this->assertEquals($original * 96, $result);
|
||||
|
||||
$result = PHPWord_Shared_Font::centimeterSizeToPixels($original);
|
||||
$this->assertEquals($original * 37.795275591, $result);
|
||||
|
||||
$result = PHPWord_Shared_Font::centimeterSizeToTwips($original);
|
||||
$this->assertEquals($original * 565.217, $result);
|
||||
|
||||
$result = PHPWord_Shared_Font::inchSizeToTwips($original);
|
||||
$this->assertEquals($original * 565.217 * 2.54, $result);
|
||||
|
||||
$result = PHPWord_Shared_Font::pixelSizeToTwips($original);
|
||||
$this->assertEquals($original * 565.217 / 37.795275591, $result);
|
||||
|
||||
$result = PHPWord_Shared_Font::pointSizeToTwips($original);
|
||||
$this->assertEquals($original * 20, $result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
namespace PHPWord\Tests;
|
||||
|
||||
use PHPUnit_Framework_TestCase;
|
||||
use PHPWord;
|
||||
use PHPWord_Writer_Word2007_Styles;
|
||||
|
||||
/**
|
||||
* Class PHPWord_Writer_Word2007_StylesTest
|
||||
* @package PHPWord\Tests
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class PHPWord_Writer_Word2007_StylesTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Executed before each method of the class
|
||||
*/
|
||||
public function tearDown()
|
||||
{
|
||||
TestHelperDOCX::clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test write styles
|
||||
*/
|
||||
public function testWriteStyles()
|
||||
{
|
||||
$PHPWord = new PHPWord();
|
||||
|
||||
$defaultStyle = array('align' => 'both');
|
||||
$baseStyle = array('basedOn' => 'Normal');
|
||||
$newStyle = array('basedOn' => 'Base Style', 'next' => 'Normal');
|
||||
$PHPWord->setDefaultParagraphStyle($defaultStyle);
|
||||
$PHPWord->addParagraphStyle('Base Style', $baseStyle);
|
||||
$PHPWord->addParagraphStyle('New Style', $newStyle);
|
||||
$doc = TestHelperDOCX::getDocument($PHPWord);
|
||||
$file = 'word/styles.xml';
|
||||
|
||||
// Normal style generated?
|
||||
$path = '/w:styles/w:style[@w:styleId="Normal"]/w:name';
|
||||
$element = $doc->getElement($path, $file);
|
||||
$this->assertEquals('Normal', $element->getAttribute('w:val'));
|
||||
|
||||
// Parent style referenced?
|
||||
$path = '/w:styles/w:style[@w:styleId="New Style"]/w:basedOn';
|
||||
$element = $doc->getElement($path, $file);
|
||||
$this->assertEquals('Base Style', $element->getAttribute('w:val'));
|
||||
|
||||
// Next paragraph style correct?
|
||||
$path = '/w:styles/w:style[@w:styleId="New Style"]/w:next';
|
||||
$element = $doc->getElement($path, $file);
|
||||
$this->assertEquals('Normal', $element->getAttribute('w:val'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -39,6 +39,9 @@ Changes in branch for release 0.7.1 :
|
|||
- Feature: (ivanlanin) GH-48 GH-86 - Section: Multicolumn and section break
|
||||
- Feature: (RomanSyroeshko) GH-46 GH-47 GH-83 - Template : Ability to apply XSL style sheet to Template
|
||||
- QA: (Progi1984) - UnitTests
|
||||
- Feature: (ivanlanin) - General: PHPWord_Shared_Font::pointSizeToTwips() converter
|
||||
- Feature: (ivanlanin) - Paragraph: Ability to define normal paragraph style with PHPWord::setNormalStyle()
|
||||
- Feature: (ivanlanin) - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next)
|
||||
|
||||
Changes in branch for release 0.7.0 :
|
||||
- Bugfix: (RomanSyroeshko) GH-32 - "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found
|
||||
|
|
|
|||
Loading…
Reference in New Issue