Skip to content

ODPresentation Reader : Read differents units for margin #847

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

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- `Gd::setImageResource()` : Fixed when imagecreatetruecolor returns false by [@jaapdh](https://github.com/jaapdh) in [#843](https://github.com/PHPOffice/PHPPresentation/pull/843)
- 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)
- `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)
- 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)

## Miscellaneous

Expand Down
4 changes: 2 additions & 2 deletions samples/Sample_12_Reader_ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

set_time_limit(10);

include_once 'Sample_Header.php';
include_once __DIR__ . '/Sample_Header.php';

use PhpOffice\PhpPresentation\IOFactory;

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

$oTree = new PhpPptTree($oPHPPresentation);
echo $oTree->display();
Expand Down
38 changes: 34 additions & 4 deletions src/PhpPresentation/Reader/ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,10 @@ protected function loadStyle(DOMElement $nodeStyle): bool
$lineSpacing = $this->getExpressionValue($nodeParagraphProps->getAttribute('fo:margin-bottom'));
}
if ($nodeParagraphProps->hasAttribute('fo:margin-bottom')) {
$spacingAfter = (float) substr($nodeParagraphProps->getAttribute('fo:margin-bottom'), 0, -2);
$spacingAfter = CommonDrawing::centimetersToPoints($spacingAfter);
$spacingAfter = self::sizeToPoint($nodeParagraphProps->getAttribute('fo:margin-bottom'));
}
if ($nodeParagraphProps->hasAttribute('fo:margin-top')) {
$spacingBefore = (float) substr($nodeParagraphProps->getAttribute('fo:margin-top'), 0, -2);
$spacingBefore = CommonDrawing::centimetersToPoints($spacingBefore);
$spacingBefore = self::sizeToPoint($nodeParagraphProps->getAttribute('fo:margin-top'));
}
$oAlignment = new Alignment();
if ($nodeParagraphProps->hasAttribute('fo:text-align')) {
Expand Down Expand Up @@ -774,4 +772,36 @@ private function getExpressionValue(string $expr): string

return substr($expr, 0, -2);
}

/**
* Transforms a size in CSS format (eg. 10px, 10px, ...) to points.
*/
protected static function sizeToPoint(string $value): ?float
{
if ($value == '0') {
return 0;
}
$matches = [];
if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches)) {
$size = (float) $matches[1];
$unit = $matches[2];

switch ($unit) {
case 'pt':
return $size;
case 'px':
return CommonDrawing::pixelsToPoints((int) $size);
case 'cm':
return CommonDrawing::centimetersToPoints($size);
case 'mm':
return CommonDrawing::centimetersToPoints($size / 10);
case 'in':
return CommonDrawing::inchesToPoints($size);
case 'pc':
return CommonDrawing::picasToPoints($size);
}
}

return null;
}
}
12 changes: 6 additions & 6 deletions src/PhpPresentation/Shape/RichText/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ class Paragraph implements ComparableInterface
private $lineSpacingMode = self::LINE_SPACING_MODE_PERCENT;

/**
* @var int
* @var float
*/
private $spacingBefore = 0;

/**
* @var int
* @var float
*/
private $spacingAfter = 0;

Expand Down Expand Up @@ -346,15 +346,15 @@ public function setLineSpacingMode(string $lineSpacingMode): self
/**
* Value in points.
*/
public function getSpacingBefore(): int
public function getSpacingBefore(): float
{
return $this->spacingBefore;
}

/**
* Value in points.
*/
public function setSpacingBefore(int $spacingBefore): self
public function setSpacingBefore(float $spacingBefore): self
{
$this->spacingBefore = $spacingBefore;

Expand All @@ -364,15 +364,15 @@ public function setSpacingBefore(int $spacingBefore): self
/**
* Value in points.
*/
public function getSpacingAfter(): int
public function getSpacingAfter(): float
{
return $this->spacingAfter;
}

/**
* Value in points.
*/
public function setSpacingAfter(int $spacingAfter): self
public function setSpacingAfter(float $spacingAfter): self
{
$this->spacingAfter = $spacingAfter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ public function testAxisVisibility(): void

public function testAxisXUnit(): void
{
$value = mt_rand(0, 100);
$value = mt_rand(1, 100);

$oSeries = new Series('Downloads', $this->seriesData);
$oLine = new Line();
Expand Down
Loading