Skip to content

Commit be4b01b

Browse files
committed
PHPWord_Shared_Font::pointSizeToTwips
1 parent 545cbc6 commit be4b01b

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Classes/PHPWord/Shared/Font.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,16 @@ public static function pixelSizeToTwips($sizeInPixel = 1)
7777
{
7878
return self::centimeterSizeToTwips($sizeInPixel / 37.795275591);
7979
}
80+
81+
/**
82+
* Calculate twip based on point size, used mainly for paragraph spacing
83+
*
84+
* @param int|float $sizeInPoint Size in point
85+
* @return int|float Size (in twips)
86+
*/
87+
public static function pointSizeToTwips($sizeInPoint = 1)
88+
{
89+
return ($sizeInPoint * 20);
90+
}
91+
8092
}

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
7373
$objWriter->save('helloWorld.docx');
7474
```
7575

76+
##### Measurement units
77+
78+
The base length unit in Open Office XML is twip. Twip means "TWentieth of an Inch Point", i.e. 1 twip = 1/1440 inch.
79+
80+
You can use PHPWord helper functions to convert inches, centimeters, or points to twips.
81+
82+
```
83+
// Paragraph with 6 points space after
84+
$PHPWord->addParagraphStyle('My Style', array(
85+
'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(6))
86+
);
87+
88+
$section = $PHPWord->createSection();
89+
$sectionStyle = $section->getSettings();
90+
// half inch left margin
91+
$sectionStyle->setMarginLeft(PHPWord_Shared_Font::inchSizeToTwips(.5));
92+
// 2 cm right margin
93+
$sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2));
94+
```
95+
7696
<a name="sections"></a>
7797
#### Sections
7898

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Changes in branch for release 0.7.1 :
3838
- Feature: (ivanlanin) GH-48 GH-86 - Paragraph: Hanging paragraph
3939
- Feature: (ivanlanin) GH-48 GH-86 - Section: Multicolumn and section break
4040
- QA: (Progi1984) - UnitTests
41+
- Feature: (ivanlanin) - General: PHPWord_Shared_Font::pointSizeToTwips converter
4142

4243
Changes in branch for release 0.7.0 :
4344
- 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

0 commit comments

Comments
 (0)