Skip to content

Commit cee0908

Browse files
committed
Formula : Add Element (& Writer/Reader Word2007/ODText)
1 parent a836c32 commit cee0908

File tree

30 files changed

+882
-172
lines changed

30 files changed

+882
-172
lines changed

.github/workflows/php.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
name: CI
22
on:
3-
- push
4-
- pull_request
3+
pull_request:
4+
push:
5+
branches:
6+
- master
57
jobs:
68
test:
79
runs-on: ubuntu-latest

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
"ext-dom": "*",
6969
"ext-json": "*",
7070
"ext-xml": "*",
71-
"laminas/laminas-escaper": ">=2.6"
71+
"laminas/laminas-escaper": ">=2.6",
72+
"phpoffice/math": "^0.1"
7273
},
7374
"require-dev": {
7475
"ext-zip": "*",

composer.lock

Lines changed: 54 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/changes/1.x/1.2.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Word2007 Reader : Added support for Comments by [@shaedrich](https://github.com/shaedrich) in [#2161](https://github.com/PHPOffice/PHPWord/pull/2161) & [#2469](https://github.com/PHPOffice/PHPWord/pull/2469)
1515
- Word2007 Reader/Writer: Permit book-fold printing by [@potofcoffee](https://github.com/potofcoffee) in [#2225](https://github.com/PHPOffice/PHPWord/pull/2225) & [#2470](https://github.com/PHPOffice/PHPWord/pull/2470)
1616
- Word2007 Writer : Add PageNumber to TOC by [@jet-desk](https://github.com/jet-desk) in [#1652](https://github.com/PHPOffice/PHPWord/pull/1652) & [#2471](https://github.com/PHPOffice/PHPWord/pull/2471)
17+
- Word2007 Reader/Writer + ODText Reader/Writer : Add Element Formula in by [@Progi1984](https://github.com/Progi1984) in [#2477](https://github.com/PHPOffice/PHPWord/pull/2477)
1718

1819
### Bug fixes
1920

docs/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ Below are the supported features for each file formats.
6666
| **Graphs** | 2D basic graphs | :material-check: | | | | |
6767
| | 2D advanced graphs | | | | | |
6868
| | 3D graphs | :material-check: | | | | |
69-
| **Math** | OMML support | | | | | |
70-
| | MathML support | | | | | |
69+
| **Math** | OMML support | :material-check: | | | | |
70+
| | MathML support | | :material-check: | | | |
7171
| **Bonus** | Encryption | | | | | |
7272
| | Protection | | | | | |
7373

@@ -99,8 +99,8 @@ Below are the supported features for each file formats.
9999
| **Graphs** | 2D basic graphs | | | | | |
100100
| | 2D advanced graphs | | | | | |
101101
| | 3D graphs | | | | | |
102-
| **Math** | OMML support | | | | | |
103-
| | MathML support | | | | | |
102+
| **Math** | OMML support | :material-check: | | | | |
103+
| | MathML support | | :material-check: | | | |
104104
| **Bonus** | Encryption | | | | | |
105105
| | Protection | | | | | |
106106

docs/usage/elements/formula.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Formula
2+
3+
Formula can be added using
4+
5+
``` php
6+
<?php
7+
8+
use PhpOffice\Math\Element;
9+
use PhpOffice\Math\Math;
10+
11+
$fraction = new Element\Fraction();
12+
$fraction
13+
->setDenominator(new Element\Numeric(2))
14+
->setNumerator(new Element\Identifier('π'))
15+
;
16+
17+
$math = new Math();
18+
$math->add($fraction);
19+
20+
$formula = $section->addFormula($math);
21+
```

src/PhpWord/Element/AbstractContainer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Element;
1919

2020
use BadMethodCallException;
21+
use PhpOffice\Math\Math;
2122
use ReflectionClass;
2223

2324
/**
@@ -47,6 +48,7 @@
4748
* @method Chart addChart(string $type, array $categories, array $values, array $style = null, $seriesName = null)
4849
* @method FormField addFormField(string $type, mixed $fStyle = null, mixed $pStyle = null)
4950
* @method SDT addSDT(string $type)
51+
* @method Formula addFormula(Math $math)
5052
* @method \PhpOffice\PhpWord\Element\OLEObject addObject(string $source, mixed $style = null) deprecated, use addOLEObject instead
5153
*
5254
* @since 0.10.0
@@ -88,6 +90,7 @@ public function __call($function, $args)
8890
'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field',
8991
'Line', 'Shape', 'Title', 'TOC', 'PageBreak',
9092
'Chart', 'FormField', 'SDT', 'Comment',
93+
'Formula',
9194
];
9295
$functions = [];
9396
foreach ($elements as $element) {

src/PhpWord/Element/Formula.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace PhpOffice\PhpWord\Element;
21+
22+
use PhpOffice\Math\Math;
23+
24+
/**
25+
* Formula element.
26+
*/
27+
class Formula extends AbstractElement
28+
{
29+
/**
30+
* @var Math
31+
*/
32+
protected $math;
33+
34+
/**
35+
* Create a new Formula Element.
36+
*/
37+
public function __construct(Math $math)
38+
{
39+
$this->setMath($math);
40+
}
41+
42+
public function setMath(Math $math): self
43+
{
44+
$this->math = $math;
45+
46+
return $this;
47+
}
48+
49+
public function getMath(): Math
50+
{
51+
return $this->math;
52+
}
53+
}

src/PhpWord/Reader/ODText.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,8 @@ public function load($docFile)
5353

5454
/**
5555
* Read document part.
56-
*
57-
* @param array $relationships
58-
* @param string $partName
59-
* @param string $docFile
60-
* @param string $xmlFile
6156
*/
62-
private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile, $xmlFile): void
57+
private function readPart(PhpWord $phpWord, array $relationships, string $partName, string $docFile, string $xmlFile): void
6358
{
6459
$partClass = "PhpOffice\\PhpWord\\Reader\\ODText\\{$partName}";
6560
if (class_exists($partClass)) {
@@ -72,12 +67,8 @@ private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile,
7267

7368
/**
7469
* Read all relationship files.
75-
*
76-
* @param string $docFile
77-
*
78-
* @return array
7970
*/
80-
private function readRelationships($docFile)
71+
private function readRelationships(string $docFile): array
8172
{
8273
$rels = [];
8374
$xmlFile = 'META-INF/manifest.xml';

src/PhpWord/Reader/ODText/Content.php

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Reader\ODText;
1919

2020
use DateTime;
21+
use PhpOffice\Math\Reader\MathML;
2122
use PhpOffice\PhpWord\Element\TrackChange;
2223
use PhpOffice\PhpWord\PhpWord;
2324
use PhpOffice\PhpWord\Shared\XMLReader;
@@ -51,37 +52,55 @@ public function read(PhpWord $phpWord): void
5152

5253
break;
5354
case 'text:p': // Paragraph
54-
$children = $node->childNodes;
55-
foreach ($children as $child) {
56-
switch ($child->nodeName) {
57-
case 'text:change-start':
58-
$changeId = $child->getAttribute('text:change-id');
59-
if (isset($trackedChanges[$changeId])) {
60-
$changed = $trackedChanges[$changeId];
61-
}
55+
$element = $xmlReader->getElement('draw:frame/draw:object', $node);
56+
if ($element) {
57+
$mathFile = str_replace('./', '', $element->getAttribute('xlink:href')) . '/content.xml';
6258

63-
break;
64-
case 'text:change-end':
65-
unset($changed);
59+
$xmlReaderObject = new XMLReader();
60+
$mathElement = $xmlReaderObject->getDomFromZip($this->docFile, $mathFile);
61+
if ($mathElement) {
62+
$mathXML = $mathElement->saveXML($mathElement);
6663

67-
break;
68-
case 'text:change':
69-
$changeId = $child->getAttribute('text:change-id');
70-
if (isset($trackedChanges[$changeId])) {
71-
$changed = $trackedChanges[$changeId];
72-
}
64+
if (is_string($mathXML)) {
65+
$reader = new MathML();
66+
$math = $reader->read($mathXML);
7367

74-
break;
68+
$section->addFormula($math);
69+
}
7570
}
76-
}
71+
} else {
72+
$children = $node->childNodes;
73+
foreach ($children as $child) {
74+
switch ($child->nodeName) {
75+
case 'text:change-start':
76+
$changeId = $child->getAttribute('text:change-id');
77+
if (isset($trackedChanges[$changeId])) {
78+
$changed = $trackedChanges[$changeId];
79+
}
80+
81+
break;
82+
case 'text:change-end':
83+
unset($changed);
7784

78-
$element = $section->addText($node->nodeValue);
79-
if (isset($changed) && is_array($changed)) {
80-
$element->setTrackChange($changed['changed']);
81-
if (isset($changed['textNodes'])) {
82-
foreach ($changed['textNodes'] as $changedNode) {
83-
$element = $section->addText($changedNode->nodeValue);
84-
$element->setTrackChange($changed['changed']);
85+
break;
86+
case 'text:change':
87+
$changeId = $child->getAttribute('text:change-id');
88+
if (isset($trackedChanges[$changeId])) {
89+
$changed = $trackedChanges[$changeId];
90+
}
91+
92+
break;
93+
}
94+
}
95+
96+
$element = $section->addText($node->nodeValue);
97+
if (isset($changed) && is_array($changed)) {
98+
$element->setTrackChange($changed['changed']);
99+
if (isset($changed['textNodes'])) {
100+
foreach ($changed['textNodes'] as $changedNode) {
101+
$element = $section->addText($changedNode->nodeValue);
102+
$element->setTrackChange($changed['changed']);
103+
}
85104
}
86105
}
87106
}

0 commit comments

Comments
 (0)