Skip to content

Commit f59b722

Browse files
authored
Merge pull request #977 from troosan/paper_size_computation
fix paper size and add tests for Paper class
2 parents 8c3efa4 + fc3bc29 commit f59b722

File tree

4 files changed

+99
-21
lines changed

4 files changed

+99
-21
lines changed

src/PhpWord/Style/Paper.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PhpOffice\PhpWord\Shared\Converter;
21+
2022
/**
2123
* Paper size from ISO/IEC 29500-1:2012 pg. 1656-1657
2224
*
@@ -100,6 +102,7 @@ class Paper extends AbstractStyle
100102
'A3' => array(297, 420, 'mm'),
101103
'A4' => array(210, 297, 'mm'),
102104
'A5' => array(148, 210, 'mm'),
105+
'B5' => array(176, 250, 'mm'),
103106
'Folio' => array(8.5, 13, 'in'),
104107
'Legal' => array(8.5, 14, 'in'),
105108
'Letter' => array(8.5, 11, 'in'),
@@ -157,11 +160,14 @@ public function setSize($size)
157160
$this->size = $this->setEnumVal($size, array_keys($this->sizes), $this->size);
158161

159162
list($width, $height, $unit) = $this->sizes[$this->size];
160-
$multipliers = array('mm' => 56.5217, 'in' => 1440);
161-
$multiplier = $multipliers[$unit];
162163

163-
$this->width = (int)round($width * $multiplier);
164-
$this->height = (int)round($height * $multiplier);
164+
if ($unit == 'mm') {
165+
$this->width = Converter::cmToTwip($width / 10);
166+
$this->height = Converter::cmToTwip($height / 10);
167+
} else {
168+
$this->width = Converter::inchToTwip($width);
169+
$this->height = Converter::inchToTwip($height);
170+
}
165171

166172
return $this;
167173
}

src/PhpWord/Style/Section.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class Section extends Border
3535
*
3636
* @const int|float
3737
*/
38-
const DEFAULT_WIDTH = 11870; // In twips.
39-
const DEFAULT_HEIGHT = 16787; // In twips.
40-
const DEFAULT_MARGIN = 1440; // In twips.
41-
const DEFAULT_GUTTER = 0; // In twips.
42-
const DEFAULT_HEADER_HEIGHT = 720; // In twips.
43-
const DEFAULT_FOOTER_HEIGHT = 720; // In twips.
38+
const DEFAULT_WIDTH = 11905.511811024; // In twips.
39+
const DEFAULT_HEIGHT = 16837.79527559; // In twips.
40+
const DEFAULT_MARGIN = 1440; // In twips.
41+
const DEFAULT_GUTTER = 0; // In twips.
42+
const DEFAULT_HEADER_HEIGHT = 720; // In twips.
43+
const DEFAULT_FOOTER_HEIGHT = 720; // In twips.
4444
const DEFAULT_COLUMN_COUNT = 1;
45-
const DEFAULT_COLUMN_SPACING = 720; // In twips.
45+
const DEFAULT_COLUMN_SPACING = 720; // In twips.
4646

4747
/**
4848
* Page Orientation

tests/PhpWord/Style/PaperTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2016 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Style;
19+
20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\TestHelperDOCX;
22+
23+
/**
24+
* Test class for PhpOffice\PhpWord\Style\Paper
25+
*
26+
* @runTestsInSeparateProcesses
27+
*/
28+
class PaperTest extends \PHPUnit_Framework_TestCase
29+
{
30+
/**
31+
* Tear down after each test
32+
*/
33+
public function tearDown()
34+
{
35+
TestHelperDOCX::clear();
36+
}
37+
38+
/**
39+
* Test initiation for paper
40+
*/
41+
public function testInitiation()
42+
{
43+
$object = new Paper();
44+
45+
$this->assertEquals('A4', $object->getSize());
46+
}
47+
48+
/**
49+
* Test paper size for B5 format
50+
*/
51+
public function testB5Size()
52+
{
53+
$object = new Paper('B5');
54+
55+
$this->assertEquals('B5', $object->getSize());
56+
$this->assertEquals(9977.9527559055, $object->getWidth(), '', 0.000000001);
57+
$this->assertEquals(14173.228346457, $object->getHeight(), '', 0.000000001);
58+
}
59+
60+
/**
61+
* Test paper size for Folio format
62+
*/
63+
public function testFolioSize()
64+
{
65+
$object = new Paper();
66+
$object->setSize('Folio');
67+
68+
$this->assertEquals('Folio', $object->getSize());
69+
$this->assertEquals(12240, $object->getWidth(), '', 0.1);
70+
$this->assertEquals(18720, $object->getHeight(), '', 0.1);
71+
}
72+
}

tests/PhpWord/Style/SectionTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function testSettingValue()
3333
$oSettings = new Section();
3434

3535
$this->assertEquals('portrait', $oSettings->getOrientation());
36-
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeW());
37-
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeH());
36+
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeW(), '', 0.000000001);
37+
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeH(), '', 0.000000001);
3838
$this->assertEquals('A4', $oSettings->getPaperSize());
3939

4040
$oSettings->setSettingValue('orientation', 'landscape');
4141
$this->assertEquals('landscape', $oSettings->getOrientation());
42-
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeW());
43-
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeH());
42+
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeW(), '', 0.000000001);
43+
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeH(), '', 0.000000001);
4444

4545
$iVal = rand(1, 1000);
4646
$oSettings->setSettingValue('borderSize', $iVal);
@@ -110,7 +110,7 @@ public function testPageWidth()
110110
// Section Settings
111111
$oSettings = new Section();
112112

113-
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeW());
113+
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeW(), '', 0.000000001);
114114
$iVal = rand(1, 1000);
115115
$oSettings->setSettingValue('pageSizeW', $iVal);
116116
$this->assertEquals($iVal, $oSettings->getPageSizeW());
@@ -124,7 +124,7 @@ public function testPageHeight()
124124
// Section Settings
125125
$oSettings = new Section();
126126

127-
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeH());
127+
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeH(), '', 0.000000001);
128128
$iVal = rand(1, 1000);
129129
$oSettings->setSettingValue('pageSizeH', $iVal);
130130
$this->assertEquals($iVal, $oSettings->getPageSizeH());
@@ -140,8 +140,8 @@ public function testOrientationLandscape()
140140

141141
$oSettings->setLandscape();
142142
$this->assertEquals('landscape', $oSettings->getOrientation());
143-
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeW());
144-
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeH());
143+
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeW(), '', 0.000000001);
144+
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeH(), '', 0.000000001);
145145
}
146146

147147
/**
@@ -154,8 +154,8 @@ public function testOrientationPortrait()
154154

155155
$oSettings->setPortrait();
156156
$this->assertEquals('portrait', $oSettings->getOrientation());
157-
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeW());
158-
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeH());
157+
$this->assertEquals(Section::DEFAULT_WIDTH, $oSettings->getPageSizeW(), '', 0.000000001);
158+
$this->assertEquals(Section::DEFAULT_HEIGHT, $oSettings->getPageSizeH(), '', 0.000000001);
159159
}
160160

161161
/**

0 commit comments

Comments
 (0)