Skip to content

Commit ef67364

Browse files
committed
ODPresentation Reader : Read differents units for margin
1 parent 9b0921a commit ef67364

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

docs/changes/1.2.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- `Gd::setImageResource()` : Fixed when imagecreatetruecolor returns false by [@jaapdh](https://github.com/jaapdh) in [#843](https://github.com/PHPOffice/PHPPresentation/pull/843)
1919
- Word2007 Writer: LineChart supports LabelPosition for Series by [@pal-software](https://github.com/pal-software) fixing [#606](https://github.com/PHPOffice/PHPPresentation/pull/606) in [#8434](https://github.com/PHPOffice/PHPPresentation/pull/844)
2020
- `createDrawingShape` has no container defined by [@Progi1984](https://github.com/Progi1984) fixing [#820](https://github.com/PHPOffice/PHPPresentation/pull/820) in [#845](https://github.com/PHPOffice/PHPPresentation/pull/845)
21+
- ODPresentation Reader : Read differents units for margin by [@Progi1984](https://github.com/Progi1984) fixing [#830](https://github.com/PHPOffice/PHPPresentation/pull/830) in [#847](https://github.com/PHPOffice/PHPPresentation/pull/847)
2122

2223
## Miscellaneous
2324

samples/Sample_12_Reader_ODPresentation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
set_time_limit(10);
44

5-
include_once 'Sample_Header.php';
5+
include_once __DIR__ . '/Sample_Header.php';
66

77
use PhpOffice\PhpPresentation\IOFactory;
88

99
$pptReader = IOFactory::createReader('ODPresentation');
10-
$oPHPPresentation = $pptReader->load('resources/Sample_12.odp');
10+
$oPHPPresentation = $pptReader->load(__DIR__ . '/resources/Sample_12.odp');
1111

1212
$oTree = new PhpPptTree($oPHPPresentation);
1313
echo $oTree->display();

src/PhpPresentation/Reader/ODPresentation.php

+34-4
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,10 @@ protected function loadStyle(DOMElement $nodeStyle): bool
424424
$lineSpacing = $this->getExpressionValue($nodeParagraphProps->getAttribute('fo:margin-bottom'));
425425
}
426426
if ($nodeParagraphProps->hasAttribute('fo:margin-bottom')) {
427-
$spacingAfter = (float) substr($nodeParagraphProps->getAttribute('fo:margin-bottom'), 0, -2);
428-
$spacingAfter = CommonDrawing::centimetersToPoints($spacingAfter);
427+
$spacingAfter = self::sizeToPoint($nodeParagraphProps->getAttribute('fo:margin-bottom'));
429428
}
430429
if ($nodeParagraphProps->hasAttribute('fo:margin-top')) {
431-
$spacingBefore = (float) substr($nodeParagraphProps->getAttribute('fo:margin-top'), 0, -2);
432-
$spacingBefore = CommonDrawing::centimetersToPoints($spacingBefore);
430+
$spacingBefore = self::sizeToPoint($nodeParagraphProps->getAttribute('fo:margin-top'));
433431
}
434432
$oAlignment = new Alignment();
435433
if ($nodeParagraphProps->hasAttribute('fo:text-align')) {
@@ -774,4 +772,36 @@ private function getExpressionValue(string $expr): string
774772

775773
return substr($expr, 0, -2);
776774
}
775+
776+
/**
777+
* Transforms a size in CSS format (eg. 10px, 10px, ...) to points.
778+
*/
779+
protected static function sizeToPoint(string $value): ?float
780+
{
781+
if ($value == '0') {
782+
return 0;
783+
}
784+
$matches = [];
785+
if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches)) {
786+
$size = (float) $matches[1];
787+
$unit = $matches[2];
788+
789+
switch ($unit) {
790+
case 'pt':
791+
return $size;
792+
case 'px':
793+
return CommonDrawing::pixelsToPoints((int) $size);
794+
case 'cm':
795+
return CommonDrawing::centimetersToPoints($size);
796+
case 'mm':
797+
return CommonDrawing::centimetersToPoints($size / 10);
798+
case 'in':
799+
return CommonDrawing::inchesToPoints($size);
800+
case 'pc':
801+
return CommonDrawing::picasToPoints($size);
802+
}
803+
}
804+
805+
return null;
806+
}
777807
}

src/PhpPresentation/Shape/RichText/Paragraph.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ class Paragraph implements ComparableInterface
7272
private $lineSpacingMode = self::LINE_SPACING_MODE_PERCENT;
7373

7474
/**
75-
* @var int
75+
* @var float
7676
*/
7777
private $spacingBefore = 0;
7878

7979
/**
80-
* @var int
80+
* @var float
8181
*/
8282
private $spacingAfter = 0;
8383

@@ -346,15 +346,15 @@ public function setLineSpacingMode(string $lineSpacingMode): self
346346
/**
347347
* Value in points.
348348
*/
349-
public function getSpacingBefore(): int
349+
public function getSpacingBefore(): float
350350
{
351351
return $this->spacingBefore;
352352
}
353353

354354
/**
355355
* Value in points.
356356
*/
357-
public function setSpacingBefore(int $spacingBefore): self
357+
public function setSpacingBefore(float $spacingBefore): self
358358
{
359359
$this->spacingBefore = $spacingBefore;
360360

@@ -364,15 +364,15 @@ public function setSpacingBefore(int $spacingBefore): self
364364
/**
365365
* Value in points.
366366
*/
367-
public function getSpacingAfter(): int
367+
public function getSpacingAfter(): float
368368
{
369369
return $this->spacingAfter;
370370
}
371371

372372
/**
373373
* Value in points.
374374
*/
375-
public function setSpacingAfter(int $spacingAfter): self
375+
public function setSpacingAfter(float $spacingAfter): self
376376
{
377377
$this->spacingAfter = $spacingAfter;
378378

tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ public function testAxisVisibility(): void
751751

752752
public function testAxisXUnit(): void
753753
{
754-
$value = mt_rand(0, 100);
754+
$value = mt_rand(1, 100);
755755

756756
$oSeries = new Series('Downloads', $this->seriesData);
757757
$oLine = new Line();

0 commit comments

Comments
 (0)