This commit is contained in:
wangjinlei
2024-07-17 09:23:45 +08:00
parent edf9deeb1a
commit 881ac3e056
1001 changed files with 41032 additions and 5452 deletions

View File

@@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Element;
use BadMethodCallException;
use PhpOffice\Math\Math;
use ReflectionClass;
/**
@@ -34,7 +35,7 @@ use ReflectionClass;
* @method Footnote addFootnote(mixed $pStyle = null)
* @method Endnote addEndnote(mixed $pStyle = null)
* @method CheckBox addCheckBox(string $name, $text, mixed $fStyle = null, mixed $pStyle = null)
* @method Title addTitle(mixed $text, int $depth = 1)
* @method Title addTitle(mixed $text, int $depth = 1, int $pageNumber = null)
* @method TOC addTOC(mixed $fontStyle = null, mixed $tocStyle = null, int $minDepth = 1, int $maxDepth = 9)
* @method PageBreak addPageBreak()
* @method Table addTable(mixed $style = null)
@@ -47,6 +48,7 @@ use ReflectionClass;
* @method Chart addChart(string $type, array $categories, array $values, array $style = null, $seriesName = null)
* @method FormField addFormField(string $type, mixed $fStyle = null, mixed $pStyle = null)
* @method SDT addSDT(string $type)
* @method Formula addFormula(Math $math)
* @method \PhpOffice\PhpWord\Element\OLEObject addObject(string $source, mixed $style = null) deprecated, use addOLEObject instead
*
* @since 0.10.0
@@ -88,6 +90,7 @@ abstract class AbstractContainer extends AbstractElement
'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field',
'Line', 'Shape', 'Title', 'TOC', 'PageBreak',
'Chart', 'FormField', 'SDT', 'Comment',
'Formula',
];
$functions = [];
foreach ($elements as $element) {

View File

@@ -32,7 +32,7 @@ abstract class AbstractElement
/**
* PhpWord object.
*
* @var \PhpOffice\PhpWord\PhpWord
* @var ?\PhpOffice\PhpWord\PhpWord
*/
protected $phpWord;
@@ -147,7 +147,7 @@ abstract class AbstractElement
/**
* Get PhpWord.
*
* @return \PhpOffice\PhpWord\PhpWord
* @return ?\PhpOffice\PhpWord\PhpWord
*/
public function getPhpWord()
{
@@ -156,8 +156,6 @@ abstract class AbstractElement
/**
* Set PhpWord as reference.
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function setPhpWord(?PhpWord $phpWord = null): void
{
@@ -347,8 +345,6 @@ abstract class AbstractElement
* Set parent container.
*
* Passed parameter should be a container, except for Table (contain Row) and Row (contain Cell)
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $container
*/
public function setParentContainer(self $container): void
{
@@ -432,7 +428,7 @@ abstract class AbstractElement
* Set new style value.
*
* @param mixed $styleObject Style object
* @param mixed $styleValue Style value
* @param null|array|\PhpOffice\PhpWord\Style|string $styleValue Style value
* @param bool $returnObject Always return object
*
* @return mixed

View File

@@ -32,21 +32,21 @@ class Cell extends AbstractContainer
/**
* Cell width.
*
* @var int
* @var ?int
*/
private $width;
/**
* Cell style.
*
* @var \PhpOffice\PhpWord\Style\Cell
* @var ?\PhpOffice\PhpWord\Style\Cell
*/
private $style;
/**
* Create new instance.
*
* @param int $width
* @param null|int $width
* @param array|\PhpOffice\PhpWord\Style\Cell $style
*/
public function __construct($width = null, $style = null)
@@ -58,7 +58,7 @@ class Cell extends AbstractContainer
/**
* Get cell style.
*
* @return \PhpOffice\PhpWord\Style\Cell
* @return ?\PhpOffice\PhpWord\Style\Cell
*/
public function getStyle()
{
@@ -68,7 +68,7 @@ class Cell extends AbstractContainer
/**
* Get cell width.
*
* @return int
* @return ?int
*/
public function getWidth()
{

View File

@@ -50,7 +50,7 @@ class Chart extends AbstractElement
/**
* Chart style.
*
* @var \PhpOffice\PhpWord\Style\Chart
* @var ?\PhpOffice\PhpWord\Style\Chart
*/
private $style;
@@ -120,7 +120,7 @@ class Chart extends AbstractElement
/**
* Get chart style.
*
* @return \PhpOffice\PhpWord\Style\Chart
* @return ?\PhpOffice\PhpWord\Style\Chart
*/
public function getStyle()
{

View File

@@ -79,8 +79,6 @@ class Comment extends TrackChange
/**
* Sets the element where this comment starts.
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $value
*/
public function setStartElement(AbstractElement $value): void
{
@@ -102,8 +100,6 @@ class Comment extends TrackChange
/**
* Sets the element where this comment ends.
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $value
*/
public function setEndElement(AbstractElement $value): void
{

View File

@@ -85,6 +85,12 @@ class Field extends AbstractElement
'properties' => ['StyleIdentifier' => ''],
'options' => ['PreserveFormat'],
],
'FILENAME' => [
'properties' => [
'format' => ['Upper', 'Lower', 'FirstCap', 'Caps'],
],
'options' => ['Path', 'PreserveFormat'],
],
];
/**
@@ -268,7 +274,7 @@ class Field extends AbstractElement
/**
* Set Field text.
*
* @param string|TextRun $text
* @param null|string|TextRun $text
*
* @return null|string|TextRun
*/

View File

@@ -29,7 +29,7 @@ class Footnote extends AbstractContainer
/**
* Paragraph style.
*
* @var \PhpOffice\PhpWord\Style\Paragraph|string
* @var null|\PhpOffice\PhpWord\Style\Paragraph|string
*/
protected $paragraphStyle;
@@ -54,7 +54,7 @@ class Footnote extends AbstractContainer
/**
* Get paragraph style.
*
* @return \PhpOffice\PhpWord\Style\Paragraph|string
* @return null|\PhpOffice\PhpWord\Style\Paragraph|string
*/
public function getParagraphStyle()
{

View File

@@ -35,7 +35,7 @@ class FormField extends Text
/**
* Form field name.
*
* @var bool|int|string
* @var ?string
*/
private $name;
@@ -53,7 +53,7 @@ class FormField extends Text
/**
* Value.
*
* @var bool|int|string
* @var null|bool|int|string
*/
private $value;
@@ -105,7 +105,7 @@ class FormField extends Text
/**
* Get name.
*
* @return string
* @return ?string
*/
public function getName()
{
@@ -115,7 +115,7 @@ class FormField extends Text
/**
* Set name.
*
* @param bool|int|string $value
* @param ?string $value
*
* @return self
*/
@@ -153,7 +153,7 @@ class FormField extends Text
/**
* Get value.
*
* @return bool|int|string
* @return null|bool|int|string
*/
public function getValue()
{
@@ -163,7 +163,7 @@ class FormField extends Text
/**
* Set value.
*
* @param bool|int|string $value
* @param null|bool|int|string $value
*
* @return self
*/

View File

@@ -0,0 +1,53 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
declare(strict_types=1);
namespace PhpOffice\PhpWord\Element;
use PhpOffice\Math\Math;
/**
* Formula element.
*/
class Formula extends AbstractElement
{
/**
* @var Math
*/
protected $math;
/**
* Create a new Formula Element.
*/
public function __construct(Math $math)
{
$this->setMath($math);
}
public function setMath(Math $math): self
{
$this->math = $math;
return $this;
}
public function getMath(): Math
{
return $this->math;
}
}

View File

@@ -54,7 +54,7 @@ class Image extends AbstractElement
/**
* Image style.
*
* @var ImageStyle
* @var ?ImageStyle
*/
private $style;
@@ -89,7 +89,7 @@ class Image extends AbstractElement
/**
* Image function.
*
* @var string
* @var null|callable(resource): void
*/
private $imageFunc;
@@ -100,6 +100,16 @@ class Image extends AbstractElement
*/
private $imageExtension;
/**
* Image quality.
*
* Functions imagepng() and imagejpeg() have an optional parameter for
* quality.
*
* @var null|int
*/
private $imageQuality;
/**
* Is memory image.
*
@@ -149,7 +159,7 @@ class Image extends AbstractElement
/**
* Get Image style.
*
* @return ImageStyle
* @return ?ImageStyle
*/
public function getStyle()
{
@@ -249,13 +259,21 @@ class Image extends AbstractElement
/**
* Get image function.
*
* @return string
* @return null|callable(resource): void
*/
public function getImageFunction()
public function getImageFunction(): ?callable
{
return $this->imageFunc;
}
/**
* Get image quality.
*/
public function getImageQuality(): ?int
{
return $this->imageQuality;
}
/**
* Get image extension.
*
@@ -317,20 +335,13 @@ class Image extends AbstractElement
}
/**
* Get image string data.
*
* @param bool $base64
*
* @return null|string
*
* @since 0.11.0
* Get image string.
*/
public function getImageStringData($base64 = false)
public function getImageString(): ?string
{
$source = $this->source;
$actualSource = null;
$imageBinary = null;
$imageData = null;
$isTemp = false;
// Get actual source from archive image or other source
@@ -367,7 +378,8 @@ class Image extends AbstractElement
imagesavealpha($imageResource, true);
}
ob_start();
call_user_func($this->imageFunc, $imageResource);
$callback = $this->imageFunc;
$callback($imageResource);
$imageBinary = ob_get_contents();
ob_end_clean();
} elseif ($this->sourceType == self::SOURCE_STRING) {
@@ -379,20 +391,36 @@ class Image extends AbstractElement
fclose($fileHandle);
}
}
if ($imageBinary !== null) {
if ($base64) {
$imageData = chunk_split(base64_encode($imageBinary));
} else {
$imageData = chunk_split(bin2hex($imageBinary));
}
}
// Delete temporary file if necessary
if ($isTemp === true) {
@unlink($actualSource);
}
return $imageData;
return $imageBinary;
}
/**
* Get image string data.
*
* @param bool $base64
*
* @return null|string
*
* @since 0.11.0
*/
public function getImageStringData($base64 = false)
{
$imageBinary = $this->getImageString();
if ($imageBinary === null) {
return null;
}
if ($base64) {
return base64_encode($imageBinary);
}
return bin2hex($imageBinary);
}
/**
@@ -503,31 +531,45 @@ class Image extends AbstractElement
switch ($this->imageType) {
case 'image/png':
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefrompng';
$this->imageFunc = 'imagepng';
$this->imageFunc = function ($resource): void {
imagepng($resource, null, $this->imageQuality);
};
$this->imageExtension = 'png';
$this->imageQuality = -1;
break;
case 'image/gif':
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromgif';
$this->imageFunc = 'imagegif';
$this->imageFunc = function ($resource): void {
imagegif($resource);
};
$this->imageExtension = 'gif';
$this->imageQuality = null;
break;
case 'image/jpeg':
case 'image/jpg':
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromjpeg';
$this->imageFunc = 'imagejpeg';
$this->imageFunc = function ($resource): void {
imagejpeg($resource, null, $this->imageQuality);
};
$this->imageExtension = 'jpg';
$this->imageQuality = 100;
break;
case 'image/bmp':
case 'image/x-ms-bmp':
$this->imageType = 'image/bmp';
$this->imageFunc = null;
$this->imageExtension = 'bmp';
$this->imageQuality = null;
break;
case 'image/tiff':
$this->imageType = 'image/tiff';
$this->imageFunc = null;
$this->imageExtension = 'tif';
$this->imageQuality = null;
break;
}

View File

@@ -27,7 +27,7 @@ class Line extends AbstractElement
/**
* Line style.
*
* @var \PhpOffice\PhpWord\Style\Line
* @var ?\PhpOffice\PhpWord\Style\Line
*/
private $style;
@@ -44,7 +44,7 @@ class Line extends AbstractElement
/**
* Get line style.
*
* @return \PhpOffice\PhpWord\Style\Line
* @return ?\PhpOffice\PhpWord\Style\Line
*/
public function getStyle()
{

View File

@@ -43,14 +43,14 @@ class Link extends AbstractElement
/**
* Font style.
*
* @var \PhpOffice\PhpWord\Style\Font|string
* @var null|\PhpOffice\PhpWord\Style\Font|string
*/
private $fontStyle;
/**
* Paragraph style.
*
* @var \PhpOffice\PhpWord\Style\Paragraph|string
* @var null|\PhpOffice\PhpWord\Style\Paragraph|string
*/
private $paragraphStyle;
@@ -109,7 +109,7 @@ class Link extends AbstractElement
/**
* Get Text style.
*
* @return \PhpOffice\PhpWord\Style\Font|string
* @return null|\PhpOffice\PhpWord\Style\Font|string
*/
public function getFontStyle()
{
@@ -119,7 +119,7 @@ class Link extends AbstractElement
/**
* Get Paragraph style.
*
* @return \PhpOffice\PhpWord\Style\Paragraph|string
* @return null|\PhpOffice\PhpWord\Style\Paragraph|string
*/
public function getParagraphStyle()
{

View File

@@ -28,7 +28,7 @@ class ListItem extends AbstractElement
/**
* Element style.
*
* @var \PhpOffice\PhpWord\Style\ListItem
* @var ?\PhpOffice\PhpWord\Style\ListItem
*/
private $style;
@@ -71,7 +71,7 @@ class ListItem extends AbstractElement
/**
* Get style.
*
* @return \PhpOffice\PhpWord\Style\ListItem
* @return ?\PhpOffice\PhpWord\Style\ListItem
*/
public function getStyle()
{

View File

@@ -32,7 +32,7 @@ class ListItemRun extends TextRun
/**
* ListItem Style.
*
* @var \PhpOffice\PhpWord\Style\ListItem
* @var ?\PhpOffice\PhpWord\Style\ListItem
*/
private $style;
@@ -66,7 +66,7 @@ class ListItemRun extends TextRun
/**
* Get ListItem style.
*
* @return \PhpOffice\PhpWord\Style\ListItem
* @return ?\PhpOffice\PhpWord\Style\ListItem
*/
public function getStyle()
{

View File

@@ -35,7 +35,7 @@ class OLEObject extends AbstractElement
/**
* Image Style.
*
* @var \PhpOffice\PhpWord\Style\Image
* @var ?\PhpOffice\PhpWord\Style\Image
*/
private $style;
@@ -100,7 +100,7 @@ class OLEObject extends AbstractElement
/**
* Get object style.
*
* @return \PhpOffice\PhpWord\Style\Image
* @return ?\PhpOffice\PhpWord\Style\Image
*/
public function getStyle()
{

View File

@@ -29,21 +29,21 @@ class PreserveText extends AbstractElement
/**
* Text content.
*
* @var array|string
* @var null|array|string
*/
private $text;
/**
* Text style.
*
* @var \PhpOffice\PhpWord\Style\Font|string
* @var null|\PhpOffice\PhpWord\Style\Font|string
*/
private $fontStyle;
/**
* Paragraph style.
*
* @var \PhpOffice\PhpWord\Style\Paragraph|string
* @var null|\PhpOffice\PhpWord\Style\Paragraph|string
*/
private $paragraphStyle;
@@ -69,7 +69,7 @@ class PreserveText extends AbstractElement
/**
* Get Text style.
*
* @return \PhpOffice\PhpWord\Style\Font|string
* @return null|\PhpOffice\PhpWord\Style\Font|string
*/
public function getFontStyle()
{
@@ -79,7 +79,7 @@ class PreserveText extends AbstractElement
/**
* Get Paragraph style.
*
* @return \PhpOffice\PhpWord\Style\Paragraph|string
* @return null|\PhpOffice\PhpWord\Style\Paragraph|string
*/
public function getParagraphStyle()
{
@@ -89,7 +89,7 @@ class PreserveText extends AbstractElement
/**
* Get Text content.
*
* @return array|string
* @return null|array|string
*/
public function getText()
{

View File

@@ -29,14 +29,14 @@ class Row extends AbstractElement
/**
* Row height.
*
* @var int
* @var ?int
*/
private $height;
/**
* Row style.
*
* @var \PhpOffice\PhpWord\Style\Row
* @var ?\PhpOffice\PhpWord\Style\Row
*/
private $style;
@@ -89,7 +89,7 @@ class Row extends AbstractElement
/**
* Get row style.
*
* @return \PhpOffice\PhpWord\Style\Row
* @return ?\PhpOffice\PhpWord\Style\Row
*/
public function getStyle()
{
@@ -99,7 +99,7 @@ class Row extends AbstractElement
/**
* Get row height.
*
* @return int
* @return ?int
*/
public function getHeight()
{

View File

@@ -34,7 +34,7 @@ class SDT extends Text
/**
* Value.
*
* @var bool|int|string
* @var null|bool|int|string
*/
private $value;
@@ -100,7 +100,7 @@ class SDT extends Text
/**
* Get value.
*
* @return bool|int|string
* @return null|bool|int|string
*/
public function getValue()
{
@@ -110,7 +110,7 @@ class SDT extends Text
/**
* Set value.
*
* @param bool|int|string $value
* @param null|bool|int|string $value
*
* @return self
*/

View File

@@ -31,7 +31,7 @@ class Section extends AbstractContainer
/**
* Section style.
*
* @var \PhpOffice\PhpWord\Style\Section
* @var ?\PhpOffice\PhpWord\Style\Section
*/
private $style;
@@ -60,7 +60,7 @@ class Section extends AbstractContainer
* Create new instance.
*
* @param int $sectionCount
* @param null|array|\PhpOffice\PhpWord\Style $style
* @param null|array|\PhpOffice\PhpWord\Style|string $style
*/
public function __construct($sectionCount, $style = null)
{
@@ -87,7 +87,7 @@ class Section extends AbstractContainer
/**
* Get section style.
*
* @return \PhpOffice\PhpWord\Style\Section
* @return ?\PhpOffice\PhpWord\Style\Section
*/
public function getStyle()
{
@@ -154,8 +154,6 @@ class Section extends AbstractContainer
/**
* Set the footnote properties.
*
* @param FootnoteProperties $footnoteProperties
*/
public function setFootnoteProperties(?FootnoteProperties $footnoteProperties = null): void
{

View File

@@ -36,7 +36,7 @@ class Shape extends AbstractElement
/**
* Shape style.
*
* @var \PhpOffice\PhpWord\Style\Shape
* @var ?\PhpOffice\PhpWord\Style\Shape
*/
private $style;
@@ -80,7 +80,7 @@ class Shape extends AbstractElement
/**
* Get shape style.
*
* @return \PhpOffice\PhpWord\Style\Shape
* @return ?\PhpOffice\PhpWord\Style\Shape
*/
public function getStyle()
{

View File

@@ -27,7 +27,7 @@ class Table extends AbstractElement
/**
* Table style.
*
* @var \PhpOffice\PhpWord\Style\Table
* @var ?\PhpOffice\PhpWord\Style\Table
*/
private $style;
@@ -41,7 +41,7 @@ class Table extends AbstractElement
/**
* Table width.
*
* @var int
* @var ?int
*/
private $width;
@@ -102,7 +102,7 @@ class Table extends AbstractElement
/**
* Get table style.
*
* @return \PhpOffice\PhpWord\Style\Table
* @return null|\PhpOffice\PhpWord\Style\Table|string
*/
public function getStyle()
{
@@ -112,7 +112,7 @@ class Table extends AbstractElement
/**
* Get table width.
*
* @return int
* @return ?int
*/
public function getWidth()
{

View File

@@ -29,7 +29,7 @@ class Text extends AbstractElement
/**
* Text content.
*
* @var string
* @var ?string
*/
protected $text;
@@ -147,7 +147,7 @@ class Text extends AbstractElement
/**
* Get Text content.
*
* @return string
* @return ?string
*/
public function getText()
{

View File

@@ -34,7 +34,7 @@ class TextBox extends AbstractContainer
/**
* TextBox style.
*
* @var \PhpOffice\PhpWord\Style\TextBox
* @var ?\PhpOffice\PhpWord\Style\TextBox
*/
private $style;
@@ -51,7 +51,7 @@ class TextBox extends AbstractContainer
/**
* Get textbox style.
*
* @return \PhpOffice\PhpWord\Style\TextBox
* @return ?\PhpOffice\PhpWord\Style\TextBox
*/
public function getStyle()
{

View File

@@ -28,14 +28,14 @@ class TextBreak extends AbstractElement
/**
* Paragraph style.
*
* @var \PhpOffice\PhpWord\Style\Paragraph|string
* @var null|\PhpOffice\PhpWord\Style\Paragraph|string
*/
private $paragraphStyle;
/**
* Text style.
*
* @var \PhpOffice\PhpWord\Style\Font|string
* @var null|\PhpOffice\PhpWord\Style\Font|string
*/
private $fontStyle;
@@ -82,7 +82,7 @@ class TextBreak extends AbstractElement
/**
* Get Text style.
*
* @return \PhpOffice\PhpWord\Style\Font|string
* @return null|\PhpOffice\PhpWord\Style\Font|string
*/
public function getFontStyle()
{
@@ -113,7 +113,7 @@ class TextBreak extends AbstractElement
/**
* Get Paragraph style.
*
* @return \PhpOffice\PhpWord\Style\Paragraph|string
* @return null|\PhpOffice\PhpWord\Style\Paragraph|string
*/
public function getParagraphStyle()
{

View File

@@ -78,4 +78,16 @@ class TextRun extends AbstractContainer
return $this->paragraphStyle;
}
public function getText(): string
{
$outstr = '';
foreach ($this->getElements() as $element) {
if ($element instanceof Text) {
$outstr .= $element->getText();
}
}
return $outstr;
}
}

View File

@@ -43,7 +43,7 @@ class Title extends AbstractElement
/**
* Name of the heading style, e.g. 'Heading1'.
*
* @var string
* @var ?string
*/
private $style;
@@ -54,13 +54,20 @@ class Title extends AbstractElement
*/
protected $collectionRelation = true;
/**
* Page number.
*
* @var int
*/
private $pageNumber;
/**
* Create a new Title Element.
*
* @param string|TextRun $text
* @param int $depth
*/
public function __construct($text, $depth = 1)
public function __construct($text, $depth = 1, ?int $pageNumber = null)
{
if (is_string($text)) {
$this->text = SharedText::toUTF8($text);
@@ -75,12 +82,16 @@ class Title extends AbstractElement
if (array_key_exists($styleName, Style::getStyles())) {
$this->style = str_replace('_', '', $styleName);
}
if ($pageNumber !== null) {
$this->pageNumber = $pageNumber;
}
}
/**
* Get Title Text content.
*
* @return string
* @return string|TextRun
*/
public function getText()
{
@@ -100,10 +111,18 @@ class Title extends AbstractElement
/**
* Get Title style.
*
* @return string
* @return ?string
*/
public function getStyle()
{
return $this->style;
}
/**
* Get page number.
*/
public function getPageNumber(): ?int
{
return $this->pageNumber;
}
}