From a57b28de8f5edb1a4b79581d54d2a359b05fa704 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 30 May 2014 01:05:55 +0700 Subject: [PATCH] Some adjustments for the new `Line` element #253 --- CHANGELOG.md | 6 +- CHANGELOG.md~ | 268 ------------------ docs/elements.rst | 7 + docs/src/documentation.md | 6 + ...{Sample_28_Line.php => Sample_29_Line.php} | 0 src/PhpWord/Element/AbstractContainer.php | 2 +- src/PhpWord/Element/Field.php | 3 + src/PhpWord/Element/Line.php | 11 +- src/PhpWord/Style/Line.php | 142 ++++++---- src/PhpWord/Writer/Word2007/Element/Line.php | 14 +- src/PhpWord/Writer/Word2007/Style/Image.php | 91 +++--- src/PhpWord/Writer/Word2007/Style/Line.php | 160 +++-------- .../Tests/Writer/Word2007/ElementTest.php | 3 +- .../Writer/Word2007/Part/DocumentTest.php | 11 + .../Tests/Writer/Word2007/StyleTest.php | 3 +- 15 files changed, 219 insertions(+), 508 deletions(-) delete mode 100644 CHANGELOG.md~ rename samples/{Sample_28_Line.php => Sample_29_Line.php} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a322b2e8..2325b587 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ## 0.11.0 - Not yet released -This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Three new elements were added: TextBox, ListItemRun, Line and Field. Relative and absolute positioning for images and textboxes were added. Writer classes were refactored into parts, elements, and styles. ODT and RTF features were enhanced. Ability to add elements to PHPWord object via HTML were implemeted. RTF reader were initiated. +This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Four new elements were added: TextBox, ListItemRun, Field, and Line. Relative and absolute positioning for images and textboxes were added. Writer classes were refactored into parts, elements, and styles. ODT and RTF features were enhanced. Ability to add elements to PHPWord object via HTML were implemeted. RTF reader were initiated. ### Features @@ -30,8 +30,8 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Three - RTF Writer: Ability to write document properties - @ivanlanin - RTF Writer: Ability to write image - @ivanlanin - Element: New `Field` element - @basjan GH-251 -- Element: New `Line` element - @basjan -- RTF Reader: Basic RTF reader - @ivanlanin GH-72 +- RTF Reader: Basic RTF reader - @ivanlanin GH-72 GH-252 +- Element: New `Line` element - @basjan GH-253 ### Bugfixes diff --git a/CHANGELOG.md~ b/CHANGELOG.md~ deleted file mode 100644 index 0ccba8cc..00000000 --- a/CHANGELOG.md~ +++ /dev/null @@ -1,268 +0,0 @@ -# Changelog - -This is the changelog between releases of PHPWord. Releases are listed in reverse chronological order with the latest version listed on top, while additions/changes in each release are listed in chronological order. Changes in each release are divided into three parts: added or change features, bugfixes, and miscellaneous improvements. Each line contains short information about the change made, the person who made it, and the related issue number(s) in GitHub. - -## 0.11.0 - Not yet released - -This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Three new elements were added: TextBox, ListItemRun, and Field. Relative and absolute positioning for images and textboxes were added. Writer classes were refactored into parts, elements, and styles. ODT and RTF features were enhanced. Ability to add elements to PHPWord object via HTML were implemeted. RTF reader were initiated. - -### Features - -- Image: Ability to define relative and absolute positioning - @basjan GH-217 -- Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219 -- Element: New `TextBox` element - @basjan @ivanlanin GH-228 GH-229 GH-231 -- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231 -- Element: New `ListItemRun` element that can add a list item with inline formatting like a textrun - @basjan GH-235 -- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149 -- RTF Writer: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158 -- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237 -- RTF Writer: Ability to add links and page breaks in RTF - @ivanlanin GH-196 -- ListItemRun: Remove fontStyle parameter because ListItemRun is inherited from TextRun and TextRun doesn't have fontStyle - @ivanlanin -- Config: Ability to use a config file to store various common settings - @ivanlanin GH-200 -- ODT Writer: Enable inline font style in TextRun - @ivanlanin -- ODT Writer: Enable underline, strike/doublestrike, smallcaps/allcaps, superscript/subscript font style - @ivanlanin -- ODT Writer: Enable section and column - @ivanlanin -- PDF Writer: Add TCPDF and mPDF as optional PDF renderer library - @ivanlanin -- ODT Writer: Enable title element and custom document properties - @ivanlanin -- ODT Reader: Ability to read standard and custom document properties - @ivanlanin -- Word2007 Writer: Enable the missing custom document properties writer - @ivanlanin -- Image: Enable "image float left" - @ivanlanin GH-244 -- RTF Writer: Ability to write document properties - @ivanlanin -- RTF Writer: Ability to write image - @ivanlanin -- Element: New `Field` element - @basjan GH-251 -- RTF Reader: Basic RTF reader - @ivanlanin GH-72 - -### Bugfixes - -- Header: All images added to the second header were assigned to the first header - @basjan GH-222 -- Conversion: Fix conversion from cm to pixel, pixel to cm, and pixel to point - @basjan GH-233 GH-234 -- PageBreak: Page break adds new line in the beginning of the new page - @ivanlanin GH-150 -- Image: `marginLeft` and `marginTop` cannot accept float value - @ivanlanin GH-248 - -### Deprecated - -- Static classes `Footnotes`, `Endnotes`, and `TOC` -- `Writer\Word2007\Part`: `Numbering::writeNumbering()`, `Settings::writeSettings()`, `WebSettings::writeWebSettings()`, `ContentTypes::writeContentTypes()`, `Styles::writeStyles()`, `Document::writeDocument()` all changed into `write()` -- `Writer\Word2007\Part\DocProps`: Split into `Writer\Word2007\Part\DocPropsCore` and `Writer\Word2007\Part\DocPropsApp` -- `Element\Title::getBookmarkId()` replaced by `Element\Title::getRelationId()` -- `Writer\HTML::writeDocument`: Replaced by `Writer\HTML::getContent` - -### Miscellaneous - -- License: Change the project license from LGPL 2.1 into LGPL 3.0 - GH-211 -- Word2007 Writer: New `Style\Image` class - @ivanlanin -- Refactor: Replace static classes `Footnotes`, `Endnotes`, and `TOC` with `Collections` - @ivanlanin GH-206 -- QA: Reactivate `phpcpd` and `phpmd` on Travis - @ivanlanin -- Refactor: PHPMD recommendation: Change all `get...` method that returns `boolean` into `is...` or `has...` - @ivanlanin -- Docs: Create gh-pages branch for API documentation - @Progi1984 GH-154 -- QA: Add `.scrutinizer.yml` and include `composer.lock` for preparation to Scrutinizer - @ivanlanin GH-186 -- Writer: Refactor writer parts using composite pattern - @ivanlanin -- Docs: Show code quality and test code coverage badge on README -- Style: Change behaviour of `set...` function of boolean properties; when none is defined, assumed true - @ivanlanin -- Shared: Unify PHP ZipArchive and PCLZip features into PhpWord ZipArchive - @ivanlanin - -## 0.10.1 - 21 May 2014 - -This is a bugfix release for `php-zip` requirement in Composer. - -- Change Composer requirements for php-zip from `require` to `suggest` - @bskrtich GH-246 - -## 0.10.0 - 4 May 2014 - -This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. Word2007 reader capability is greatly enhanced. Endnote is introduced. List numbering is now customizable. Basic HTML and PDF writing support is enabled. Basic ODText reader is introduced. - -### Features - -- Image: Get image dimensions without EXIF extension - @andrew-kzoo GH-184 -- Table: Add `tblGrid` element for Libre/Open Office table sizing - @gianis6 GH-183 -- Footnote: Ability to insert textbreak in footnote `$footnote->addTextBreak()` - @ivanlanin -- Footnote: Ability to style footnote reference mark by using `FootnoteReference` style - @ivanlanin -- Font: Add `bgColor` to font style to define background using HEX color - @jcarignan GH-168 -- Table: Add `exactHeight` to row style to define whether row height should be exact or atLeast - @jcarignan GH-168 -- Element: New `CheckBox` element for sections and table cells - @ozilion GH-156 -- Settings: Ability to use PCLZip as alternative to ZipArchive - @bskrtich @ivanlanin GH-106 GH-140 GH-185 -- Template: Ability to find & replace variables in headers & footers - @dgudgeon GH-190 -- Template: Ability to clone & delete block of text using `cloneBlock` and `deleteBlock` - @diego-vieira GH-191 -- TOC: Ability to have two or more TOC in one document and to set min and max depth for TOC - @Pyreweb GH-189 -- Table: Ability to add footnote in table cell - @ivanlanin GH-187 -- Footnote: Ability to add image in footnote - @ivanlanin GH-187 -- ListItem: Ability to add list item in header/footer - @ivanlanin GH-187 -- CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187 -- Link: Ability to add link in header/footer - @ivanlanin GH-187 -- Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187 -- Media: Add `Media::resetElements()` to reset all media data - @juzi GH-19 -- General: Add `Style::resetStyles()` - @ivanlanin GH-187 -- DOCX Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table, list, image, and title - @ivanlanin -- Endnote: Ability to add endnotes - @ivanlanin -- ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198 -- ODT Writer: Basic table writing support - @ivanlanin -- Image: Keep image aspect ratio if only 1 dimension styled - @japonicus GH-194 -- HTML Writer: Basic HTML writer: text, textrun, link, title, textbreak, table, image (as Base64), footnote, endnote - @ivanlanin GH-203 GH-67 GH-147 -- PDF Writer: Basic PDF writer using DomPDF: All HTML element except image - @ivanlanin GH-68 -- DOCX Writer: Change `docProps/app.xml` `Application` to `PHPWord` - @ivanlanin -- DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin -- ODT Writer: Basic image writing - @ivanlanin -- ODT Writer: Link writing - @ivanlanin -- ODT Reader: Basic ODText Reader - @ivanlanin GH-71 -- Section: Ability to define gutter and line numbering - @ivanlanin -- Font: Small caps, all caps, and double strikethrough - @ivanlanin GH-151 -- Settings: Ability to use measurement unit other than twips with `setMeasurementUnit` - @ivanlanin GH-199 -- Style: Remove `bgColor` from `Font`, `Table`, and `Cell` and put it into the new `Shading` style - @ivanlanin -- Style: New `Indentation` and `Spacing` style - @ivanlanin -- Paragraph: Ability to define first line and right indentation - @ivanlanin - -### Bugfixes - -- Footnote: Footnote content doesn't show footnote reference number - @ivanlanin GH-170 -- Documentation: Error in a function - @theBeerNut GH-195 - -### Deprecated - -- `createTextRun` replaced by `addTextRun` -- `createFootnote` replaced by `addFootnote` -- `createHeader` replaced by `addHeader` -- `createFooter` replaced by `addFooter` -- `createSection` replaced by `addSection` -- `Element\Footnote::getReferenceId` replaced by `Element\AbstractElement::getRelationId` -- `Element\Footnote::setReferenceId` replaced by `Element\AbstractElement::setRelationId` -- `Footnote::addFootnoteLinkElement` replaced by `Media::addElement` -- `Footnote::getFootnoteLinkElements` replaced by `Media::getElements` -- All current methods on `Media` -- `Element\Link::getLinkSrc` replaced by `Element\Link::getTarget` -- `Element\Link::getLinkName` replaced by `Element\Link::getText` -- `Style\Cell::getDefaultBorderColor` - -### Miscellaneous - -- Documentation: Simplify page level docblock - @ivanlanin GH-179 -- Writer: Refactor writer classes and create a new `Write\AbstractWriter` abstract class - @ivanlanin GH-160 -- General: Refactor folders: `Element` and `Exception` - @ivanlanin GH-187 -- General: Remove legacy `HashTable` and `Shared\ZipStreamWrapper` and all related properties/methods - @ivanlanin GH-187 -- Element: New `AbstractElement` abstract class - @ivanlanin GH-187 -- Media: Refactor media class to use one method for all docPart (section, header, footer, footnote) - @ivanlanin GH-187 -- General: Remove underscore prefix from all private properties name - @ivanlanin GH-187 -- General: Move Section `Settings` to `Style\Section` - @ivanlanin GH-187 -- General: Give `Abstract` prefix and `Interface` suffix for all abstract classes and interfaces as per [PHP-FIG recommendation](https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md) - @ivanlanin GH-187 -- Style: New `Style\AbstractStyle` abstract class - @ivanlanin GH-187 -- Writer: New 'ODText\Base` class - @ivanlanin GH-187 -- General: Rename `Footnote` to `Footnotes` to reflect the nature of collection - @ivanlanin -- General: Add some unit tests for Shared & Element (100%!) - @Progi1984 -- Test: Add some samples and tests for image wrapping style - @brunocasado GH-59 -- Refactor: Remove Style\Tabs - @ivanlanin -- Refactor: Apply composite pattern for writers - @ivanlanin -- Refactor: Split `AbstractContainer` from `AbstractElement` - @ivanlanin -- Refactor: Apply composite pattern for Word2007 reader - @ivanlanin - -## 0.9.1 - 27 Mar 2014 - -This is a bugfix release for PSR-4 compatibility. - -- Fixed PSR-4 composer autoloader - @AntonTyutin - -## 0.9.0 - 26 Mar 2014 - -This release marked the transformation to namespaces (PHP 5.3+). - -### Features - -- Image: Ability to use remote or GD images using `addImage()` on sections, headers, footer, cells, and textruns - @ivanlanin -- Header: Ability to use remote or GD images using `addWatermark()` - @ivanlanin - -### Bugfixes - -- Preserve text doesn't render correctly when the text is not the first word, e.g. 'Page {PAGE}' - @ivanlanin - -### Miscellaneous - -- Move documentation to [Read The Docs](http://phpword.readthedocs.org/en/develop/) - @Progi1984 @ivanlanin GH-82 -- Reorganize and redesign samples folder - @ivanlanin GH-137 -- Use `PhpOffice\PhpWord` namespace for PSR compliance - @RomanSyroeshko @gabrielbull GH-159 GH-58 -- Restructure folders and change folder name `Classes` to `src` and `Tests` to `test` for PSR compliance - @RomanSyroeshko @gabrielbull -- Compliance to phpDocumentor - @ivanlanin -- Merge Style\TableFull into Style\Table. Style\TableFull is deprecated - @ivanlanin GH-160 -- Merge Section\MemoryImage into Section\Image. Section\Image is deprecated - @ivanlanin GH-160 - -## 0.8.1 - 17 Mar 2014 - -This is a bugfix release for image detection functionality. - -- Added fallback for computers that do not have exif_imagetype - @bskrtich, @gabrielbull - -## 0.8.0 - 15 Mar 2014 - -This release merged a lot of improvements from the community. Unit tests introduced in this release and has reached 90% code coverage. - -### Features - -- Template: Permit to save a template generated as a file (PHPWord_Template::saveAs()) - @RomanSyroeshko GH-56 GH-57 -- Word2007: Support sections page numbering - @gabrielbull -- Word2007: Added line height methods to mirror the line height settings in Word in the paragraph styling - @gabrielbull -- Word2007: Added support for page header & page footer height - @JillElaine GH-5 -- General: Add ability to manage line breaks after image insertion - @bskrtich GH-6 GH-66 GH-84 -- Template: Ability to limit number of replacements performed by setValue() method of Template class - @RomanSyroeshko GH-52 GH-53 GH-85 -- Table row: Repeat as header row & allow row to break across pages - @ivanlanin GH-48 GH-86 -- Table: Table width in percentage - @ivanlanin GH-48 GH-86 -- Font: Superscript and subscript - @ivanlanin GH-48 GH-86 -- Paragraph: Hanging paragraph - @ivanlanin GH-48 GH-86 -- Section: Multicolumn and section break - @ivanlanin GH-48 GH-86 -- Template: Ability to apply XSL style sheet to Template - @RomanSyroeshko GH-46 GH-47 GH-83 -- General: PHPWord_Shared_Font::pointSizeToTwips() converter - @ivanlanin GH-87 -- Paragraph: Ability to define normal paragraph style with PHPWord::setNormalStyle() - @ivanlanin GH-87 -- Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next) - @ivanlanin GH-87 -- Clone table rows on the fly when using a template document - @jeroenmoors GH-44 GH-88 -- Initial addition of basic footnote support - @deds GH-16 -- Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before - @ivanlanin GH-92 -- General: PHPWord_Style_Font refactoring - @ivanlanin GH-93 -- Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing. - @ivanlanin GH-93 -- Paragraph: setTabs() function - @ivanlanin GH-92 -- General: Basic support for TextRun on ODT and RTF - @ivanlanin GH-99 -- Reader: Basic Reader for Word2007 - @ivanlanin GH-104 -- TextRun: Allow Text Break in Text Run - @bskrtich GH-109 -- General: Support for East Asian fontstyle - @jhfangying GH-111 GH-118 -- Image: Use exif_imagetype to check image format instead of extension name - @gabrielbull GH-114 -- General: Setting for XMLWriter Compatibility option - @bskrtich GH-103 -- MemoryImage: Allow remote image when allow_url_open = on - @ivanlanin GH-122 -- TextBreak: Allow font and paragraph style for text break - @ivanlanin GH-18 - -### Bugfixes - -- Fixed bug with cell styling - @gabrielbull -- Fixed bug list items inside of cells - @gabrielbull -- Adding a value that contains "&" in a template breaks it - @SiebelsTim GH-51 -- Example in README.md is broken - @Progi1984 GH-89 -- General: PHPWord_Shared_Drawing::centimetersToPixels() conversion - @ivanlanin GH-94 -- Footnote: Corrupt DOCX reported by MS Word when sections > 1 and not every sections have footnote - @ivanlanin GH-125 - -### Miscellaneous - -- UnitTests - @Progi1984 - -## 0.7.0 - 28 Jan 2014 - -This is the first release after a long development hiatus in [CodePlex](https://phpword.codeplex.com/). This release initialized ODT and RTF Writer, along with some other new features for the existing Word2007 Writer, e.g. tab, multiple header, rowspan and colspan. [Composer](https://packagist.org/packages/phpoffice/phpword) and [Travis](https://travis-ci.org/PHPOffice/PHPWord) were added. - -### Features - -- Implement RTF Writer - @Progi1984 GH-1 -- Implement ODT Writer - @Progi1984 GH-2 -- Word2007: Add rowspan and colspan to cells - @kaystrobach -- Word2007: Support for tab stops - @RLovelett -- Word2007: Support Multiple headers - @RLovelett -- Word2007: Wrapping Styles to Images - @gabrielbull -- Added support for image wrapping style - @gabrielbull - -### Bugfixes - -- "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found - @RomanSyroeshko GH-32 -- PHPWord_Shared_String.IsUTF8 returns FALSE for Cyrillic UTF-8 input - @RomanSyroeshko GH-34 -- Temporary files naming logic in PHPWord_Template can lead to a collision - @RomanSyroeshko GH-38 - -### Miscellaneous - -- Add superscript/subscript styling in Excel2007 Writer - @MarkBaker -- add indentation support to paragraphs - @deds -- Support for Composer - @Progi1984 GH-27 -- Basic CI with Travis - @Progi1984 -- Added PHPWord_Exception and exception when could not copy the template - @Progi1984 -- IMPROVED: Moved examples out of Classes directory - @Progi1984 -- IMPROVED: Advanced string replace in setValue for Template - @Esmeraldo CP-49 diff --git a/docs/elements.rst b/docs/elements.rst index 901832eb..98ab042f 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -45,6 +45,8 @@ column shows the containers while the rows lists the elements. +-------+-----------------+-----------+----------+----------+---------+------------+------------+ | 18 | Field | v | v | v | v | v | v | +-------+-----------------+-----------+----------+----------+---------+------------+------------+ +| 19 | Line | v | v | v | v | v | v | ++-------+-----------------+-----------+----------+----------+---------+------------+------------+ Legend: @@ -487,3 +489,8 @@ Fields ------ To be completed + +Line +------ + +To be completed diff --git a/docs/src/documentation.md b/docs/src/documentation.md index 0259dd8b..0f96ff00 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -35,6 +35,7 @@ Don't forget to change `code::` directive to `code-block::` in the resulting rst - [Checkboxes](#checkboxes) - [Textboxes](#textboxes) - [Fields](#fields) + - [Lines](#lines) - [Templates](#templates) - [Writers & readers](#writers-readers) - [OOXML](#ooxml) @@ -463,6 +464,7 @@ Below are the matrix of element availability in each container. The column shows | 16 | CheckBox | v | v | v | v | - | - | | 17 | TextBox | v | v | v | v | - | - | | 18 | Field | v | v | v | v | v | v | +| 19 | Line | v | v | v | v | v | v | Legend: @@ -844,6 +846,10 @@ To be completed. To be completed. +## Lines + +To be completed. + # Templates You can create a docx template with included search-patterns that can be replaced by any value you wish. Only single-line values can be replaced. To load a template file, use the `loadTemplate` method. After loading the docx template, you can use the `setValue` method to change the value of a search pattern. The search-pattern model is: `${search-pattern}`. It is not possible to add new PHPWord elements to a loaded template file. diff --git a/samples/Sample_28_Line.php b/samples/Sample_29_Line.php similarity index 100% rename from samples/Sample_28_Line.php rename to samples/Sample_29_Line.php diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 6032f33f..0f065b41 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -149,11 +149,11 @@ abstract class AbstractContainer extends AbstractElement * @param string $type * @param array $properties * @param array $options + * @return \PhpOffice\PhpWord\Element\Field */ public function addField($type = null, $properties = array(), $options = array()) { return $this->addElement('Field', $type, $properties, $options); - } /** diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 7503dc9b..50f0522f 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -95,6 +95,7 @@ class Field extends AbstractElement * * @param string $type * @return string + * @throws \InvalidArgumentException */ public function setType($type = null) { @@ -123,6 +124,7 @@ class Field extends AbstractElement * * @param array $properties * @return self + * @throws \InvalidArgumentException */ public function setProperties($properties = array()) { @@ -152,6 +154,7 @@ class Field extends AbstractElement * * @param array $options * @return self + * @throws \InvalidArgumentException */ public function setOptions($options = array()) { diff --git a/src/PhpWord/Element/Line.php b/src/PhpWord/Element/Line.php index eef38914..4acca8ed 100644 --- a/src/PhpWord/Element/Line.php +++ b/src/PhpWord/Element/Line.php @@ -27,25 +27,24 @@ class Line extends AbstractElement /** * Line style * - * @var LineStyle + * @var \PhpOffice\PhpWord\Style\Line */ private $style; - + /** * Create new line element * - * @param string $source * @param mixed $style */ public function __construct($style = null) { $this->style = $this->setStyle(new LineStyle(), $style); } - + /** - * Get Image style + * Get line style * - * @return ImageStyle + * @return \PhpOffice\PhpWord\Style\Line */ public function getStyle() { diff --git a/src/PhpWord/Style/Line.php b/src/PhpWord/Style/Line.php index 94bc2ea8..44f54229 100644 --- a/src/PhpWord/Style/Line.php +++ b/src/PhpWord/Style/Line.php @@ -51,28 +51,28 @@ class Line extends Image const DASH_STYLE_LONG_DASH = 'longdash'; const DASH_STYLE_LONG_DASH_DOT = 'longdashdot'; const DASH_STYLE_LONG_DASH_DOT_DOT = 'longdashdotdot'; - + /** * flip Line * * @var boolean */ private $flip = false; - + /** * connectorType * * @var string */ private $connectorType = self::CONNECTOR_TYPE_STRAIGHT; - + /** * Line Weight * * @var int */ - private $weight = null; - + private $weight; + /** * Line color * @@ -85,33 +85,22 @@ class Line extends Image * * @var string */ - private $dash = null; - + private $dash; + /** * Begin arrow * * @var string */ - private $beginArrow = null; - + private $beginArrow; + /** * End arrow * * @var string */ - private $endArrow = null; - - - /** - * Set flip - * - * @param boolean $value - */ - public function setFlip($value = null) - { - $this->flip = $value; - } - + private $endArrow; + /** * Get flip * @@ -121,20 +110,20 @@ class Line extends Image { return $this->flip; } - + /** - * Set connectorType + * Set flip * - * @param string $value + * @param boolean $value + * @return self */ - public function setConnectorType($value = null) + public function setFlip($value = false) { - $enum = array( - self::CONNECTOR_TYPE_STRAIGHT - ); - $this->connectorType = $this->setEnumVal($value, $enum, $this->connectorType); + $this->flip = $this->setBoolVal($value, $this->flip); + + return $this; } - + /** * Get connectorType * @@ -146,15 +135,21 @@ class Line extends Image } /** - * Set weight + * Set connectorType * - * @param int $value Weight in points + * @param string $value + * @return self */ - public function setWeight($value = null) + public function setConnectorType($value = null) { - $this->weight = $value; + $enum = array( + self::CONNECTOR_TYPE_STRAIGHT + ); + $this->connectorType = $this->setEnumVal($value, $enum, $this->connectorType); + + return $this; } - + /** * Get weight * @@ -164,17 +159,20 @@ class Line extends Image { return $this->weight; } - + /** - * Set color + * Set weight * - * @param string $value + * @param int $value Weight in points + * @return self */ - public function setColor($value = null) + public function setWeight($value = null) { - $this->color = $value; + $this->weight = $this->setNumericVal($value, $this->weight); + + return $this; } - + /** * Get color * @@ -184,19 +182,18 @@ class Line extends Image { return $this->color; } - + /** - * Set beginArrow + * Set color * * @param string $value + * @return self */ - public function setBeginArrow($value = null) + public function setColor($value = null) { - $enum = array( - self::ARROW_STYLE_BLOCK, self::ARROW_STYLE_CLASSIC, self::ARROW_STYLE_DIAMOND, - self::ARROW_STYLE_OPEN, self::ARROW_STYLE_OVAL - ); - $this->beginArrow = $this->setEnumVal($value, $enum, $this->beginArrow); + $this->color = $value; + + return $this; } /** @@ -208,19 +205,24 @@ class Line extends Image { return $this->beginArrow; } + /** - * Set endArrow + * Set beginArrow * * @param string $value + * @return self */ - public function setEndArrow($value = null) + public function setBeginArrow($value = null) { $enum = array( self::ARROW_STYLE_BLOCK, self::ARROW_STYLE_CLASSIC, self::ARROW_STYLE_DIAMOND, self::ARROW_STYLE_OPEN, self::ARROW_STYLE_OVAL ); - $this->endArrow = $this->setEnumVal($value, $enum, $this->endArrow); + $this->beginArrow = $this->setEnumVal($value, $enum, $this->beginArrow); + + return $this; } + /** * Get endArrow * @@ -230,20 +232,24 @@ class Line extends Image { return $this->endArrow; } + /** - * Set Dash + * Set endArrow * * @param string $value + * @return self */ - public function setDash($value = null) + public function setEndArrow($value = null) { $enum = array( - self::DASH_STYLE_DASH, self::DASH_STYLE_DASH_DOT, self::DASH_STYLE_LONG_DASH, - self::DASH_STYLE_LONG_DASH_DOT, self::DASH_STYLE_LONG_DASH_DOT_DOT, self::DASH_STYLE_ROUND_DOT, - self::DASH_STYLE_SQUARE_DOT + self::ARROW_STYLE_BLOCK, self::ARROW_STYLE_CLASSIC, self::ARROW_STYLE_DIAMOND, + self::ARROW_STYLE_OPEN, self::ARROW_STYLE_OVAL ); - $this->dash = $this->setEnumVal($value, $enum, $this->dash); + $this->endArrow = $this->setEnumVal($value, $enum, $this->endArrow); + + return $this; } + /** * Get Dash * @@ -253,4 +259,22 @@ class Line extends Image { return $this->dash; } + + /** + * Set Dash + * + * @param string $value + * @return self + */ + public function setDash($value = null) + { + $enum = array( + self::DASH_STYLE_DASH, self::DASH_STYLE_DASH_DOT, self::DASH_STYLE_LONG_DASH, + self::DASH_STYLE_LONG_DASH_DOT, self::DASH_STYLE_LONG_DASH_DOT_DOT, self::DASH_STYLE_ROUND_DOT, + self::DASH_STYLE_SQUARE_DOT + ); + $this->dash = $this->setEnumVal($value, $enum, $this->dash); + + return $this; + } } diff --git a/src/PhpWord/Writer/Word2007/Element/Line.php b/src/PhpWord/Writer/Word2007/Element/Line.php index ea202633..1ae10694 100644 --- a/src/PhpWord/Writer/Word2007/Element/Line.php +++ b/src/PhpWord/Writer/Word2007/Element/Line.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\PhpWord\Element\Line as LineElement; -use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Line as LineStyleWriter; /** @@ -33,22 +32,23 @@ class Line extends AbstractElement public function write() { $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); + $element = $this->getElement(); if (!$element instanceof LineElement) { return; } - $style = $element->getStyle(); + $style = $element->getStyle(); $styleWriter = new LineStyleWriter($xmlWriter, $style); - - $elementId=$element->getElementIndex(); + + $elementId = $element->getElementIndex(); if (!$this->withoutP) { $xmlWriter->startElement('w:p'); $styleWriter->writeAlignment(); } $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:pict'); - if ($elementId==1) { //shapetype could be defined for each line separately, but then a unique id would be necessary + // Shapetype could be defined for each line separately, but then a unique id would be necessary + if ($elementId == 1) { $xmlWriter->startElement('v:shapetype'); $xmlWriter->writeAttribute('id', '_x0000_t32'); $xmlWriter->writeAttribute('coordsize', '21600,21600'); @@ -76,7 +76,7 @@ class Line extends AbstractElement $xmlWriter->endElement(); // v:shape $xmlWriter->endElement(); // w:pict $xmlWriter->endElement(); // w:r - + if (!$this->withoutP) { $xmlWriter->endElement(); // w:p } diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index b6b46535..280b569a 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -48,50 +48,15 @@ class Image extends AbstractStyle /** * Write style attribute + * + * @param \PhpOffice\PhpWord\Style\Image $style */ - protected function writeStyle(ImageStyle $style) + protected function writeStyle($style) { $xmlWriter = $this->getXmlWriter(); - // Default style array - $styleArray = array( - 'mso-width-percent' => '0', - 'mso-height-percent' => '0', - 'mso-width-relative' => 'margin', - 'mso-height-relative' => 'margin', - ); - $styleArray = array_merge($styleArray, $this->getElementStyle($style)); - - // Absolute/relative positioning - $positioning = $style->getPositioning(); - $styleArray['position'] = $positioning; - if ($positioning !== null) { - $styleArray['mso-position-horizontal'] = $style->getPosHorizontal(); - $styleArray['mso-position-vertical'] = $style->getPosVertical(); - $styleArray['mso-position-horizontal-relative'] = $style->getPosHorizontalRel(); - $styleArray['mso-position-vertical-relative'] = $style->getPosVerticalRel(); - } - - // Wrapping style - $wrapping = $style->getWrappingStyle(); - if ($wrapping == ImageStyle::WRAPPING_STYLE_INLINE) { - // Nothing to do when inline - } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_BEHIND) { - $styleArray['z-index'] = -251658752; - } else { - $styleArray['z-index'] = 251659264; - $styleArray['mso-position-horizontal'] = 'absolute'; - $styleArray['mso-position-vertical'] = 'absolute'; - } - - // w10 wrapping - if ($wrapping == ImageStyle::WRAPPING_STYLE_SQUARE) { - $this->w10wrap = 'square'; - } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_TIGHT) { - $this->w10wrap = 'tight'; - } - - $imageStyle = $this->assembleStyle($styleArray); + $styles = $this->getElementStyle($style); + $imageStyle = $this->assembleStyle($styles); $xmlWriter->writeAttribute('style', $imageStyle); } @@ -134,21 +99,57 @@ class Image extends AbstractStyle * @param \PhpOffice\PhpWord\Style\Image $style * @return array */ - private function getElementStyle(ImageStyle $style) + protected function getElementStyle(ImageStyle $style) { - $styles = array(); - $styleValues = array( + $styles = array( + 'mso-width-percent' => '0', + 'mso-height-percent' => '0', + 'mso-width-relative' => 'margin', + 'mso-height-relative' => 'margin', + ); + + // Dimension + $dimensions = array( 'width' => $style->getWidth(), 'height' => $style->getHeight(), 'margin-top' => $style->getMarginTop(), 'margin-left' => $style->getMarginLeft() ); - foreach ($styleValues as $key => $value) { - if (!is_null($value) && $value != '') { + foreach ($dimensions as $key => $value) { + if ($value !== null) { $styles[$key] = $value . 'px'; } } + // Absolute/relative positioning + $positioning = $style->getPositioning(); + $styles['position'] = $positioning; + if ($positioning !== null) { + $styles['mso-position-horizontal'] = $style->getPosHorizontal(); + $styles['mso-position-vertical'] = $style->getPosVertical(); + $styles['mso-position-horizontal-relative'] = $style->getPosHorizontalRel(); + $styles['mso-position-vertical-relative'] = $style->getPosVerticalRel(); + } + + // Wrapping style + $wrapping = $style->getWrappingStyle(); + if ($wrapping == ImageStyle::WRAPPING_STYLE_INLINE) { + // Nothing to do when inline + } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_BEHIND) { + $styles['z-index'] = -251658752; + } else { + $styles['z-index'] = 251659264; + $styles['mso-position-horizontal'] = 'absolute'; + $styles['mso-position-vertical'] = 'absolute'; + } + + // w10 wrapping + if ($wrapping == ImageStyle::WRAPPING_STYLE_SQUARE) { + $this->w10wrap = 'square'; + } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_TIGHT) { + $this->w10wrap = 'tight'; + } + return $styles; } diff --git a/src/PhpWord/Writer/Word2007/Style/Line.php b/src/PhpWord/Writer/Word2007/Style/Line.php index 1e4448a8..dfecb4b0 100644 --- a/src/PhpWord/Writer/Word2007/Style/Line.php +++ b/src/PhpWord/Writer/Word2007/Style/Line.php @@ -17,9 +17,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Style; -use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle; use PhpOffice\PhpWord\Style\Line as LineStyle; -use PhpOffice\PhpWord\Style\Image as ImageStyle; /** * Line style writer @@ -36,148 +34,76 @@ class Line extends Image if (!$style instanceof LineStyle) { return; } - $this->writeLineStyle($style); + $this->writeStyle($style); } - + /** * Write style attribute - * - * Copied function from Image/writeStyle in order to override getElementStyle + * + * @param \PhpOffice\PhpWord\Style\Line $style */ - protected function writeLineStyle(LineStyle $style) + protected function writeStyle($style) { $xmlWriter = $this->getXmlWriter(); - - // Default style array - $styleArray = array( - 'mso-width-percent' => '0', - 'mso-height-percent' => '0', - 'mso-width-relative' => 'margin', - 'mso-height-relative' => 'margin', - ); - $styleArray = array_merge($styleArray, $this->getElementStyle($style)); - - // Absolute/relative positioning - $positioning = $style->getPositioning(); - $styleArray['position'] = $positioning; - if ($positioning !== null) { - $styleArray['mso-position-horizontal'] = $style->getPosHorizontal(); - $styleArray['mso-position-vertical'] = $style->getPosVertical(); - $styleArray['mso-position-horizontal-relative'] = $style->getPosHorizontalRel(); - $styleArray['mso-position-vertical-relative'] = $style->getPosVerticalRel(); + + $styles = $this->getElementStyle($style); + if ($style->isFlip()) { + $styles['flip'] = 'y'; } - - // Wrapping style - $wrapping = $style->getWrappingStyle(); - if ($wrapping == LineStyle::WRAPPING_STYLE_INLINE) { - // Nothing to do when inline - } elseif ($wrapping == LineStyle::WRAPPING_STYLE_BEHIND) { - $styleArray['z-index'] = -251658752; - } else { - $styleArray['z-index'] = 251659264; - $styleArray['mso-position-horizontal'] = 'absolute'; - $styleArray['mso-position-vertical'] = 'absolute'; - } - - // w10 wrapping - if ($wrapping == LineStyle::WRAPPING_STYLE_SQUARE) { - $this->w10wrap = 'square'; - } elseif ($wrapping == LineStyle::WRAPPING_STYLE_TIGHT) { - $this->w10wrap = 'tight'; - } - - $imageStyle = $this->assembleStyle($styleArray); - + $imageStyle = $this->assembleStyle($styles); $xmlWriter->writeAttribute('style', $imageStyle); + + // Connector type $xmlWriter->writeAttribute('o:connectortype', $style->getConnectorType()); - + // Weight $weight = $style->getWeight(); - if ($weight !== null) { - $xmlWriter->writeAttribute('strokeweight', $weight . 'pt'); - } - + $xmlWriter->writeAttributeIf($weight !== null, 'strokeweight', $weight . 'pt'); + // Color $color = $style->getColor(); - if ($color !== null) { - $xmlWriter->writeAttribute('strokecolor', $color); - } + $xmlWriter->writeAttributeIf($color !== null, 'strokecolor', $color); } - - /** - * Get element style - * - * @param \PhpOffice\PhpWord\Style\Image $style - * @return array - */ - private function getElementStyle(LineStyle $style) - { - $styles = array(); - $styleValues = array( - 'width' => $style->getWidth(), - 'height' => $style->getHeight(), - 'margin-top' => $style->getMarginTop(), - 'margin-left' => $style->getMarginLeft() - ); - foreach ($styleValues as $key => $value) { - if (!is_null($value)) { - $styles[$key] = $value . 'px'; - } - } - if ($style->isFlip()) { - $styles['flip']='y'; - } - - return $styles; - } - + /** * Write Line stroke - * */ public function writeStroke() { - $style = $this->getStyle(); $xmlWriter = $this->getXmlWriter(); - + $style = $this->getStyle(); + if (!$style instanceof LineStyle) { + return; + } + $dash = $style->getDash(); $beginArrow = $style->getBeginArrow(); $endArrow = $style->getEndArrow(); - + $dashStyles = array( + LineStyle::DASH_STYLE_DASH => 'dash', + LineStyle::DASH_STYLE_ROUND_DOT => '1 1', + LineStyle::DASH_STYLE_SQUARE_DOT => '1 1', + LineStyle::DASH_STYLE_DASH_DOT => 'dashDot', + LineStyle::DASH_STYLE_LONG_DASH => 'longDash', + LineStyle::DASH_STYLE_LONG_DASH_DOT => 'longDashDot', + LineStyle::DASH_STYLE_LONG_DASH_DOT_DOT => 'longDashDotDot', + ); + if (($dash !== null) || ($beginArrow !== null) || ($endArrow !== null)) { $xmlWriter->startElement('v:stroke'); - if ($beginArrow !== null) { - $xmlWriter->writeAttribute('startarrow', $beginArrow); - } - if ($endArrow !== null) { - $xmlWriter->writeAttribute('endarrow', $endArrow); - } - if ($dash !==null) { - switch ($dash) { - case LineStyle::DASH_STYLE_DASH: - $xmlWriter->writeAttribute('dashstyle', 'dash'); - break; - case LineStyle::DASH_STYLE_ROUND_DOT: - $xmlWriter->writeAttribute('dashstyle', '1 1'); - $xmlWriter->writeAttribute('endcap', 'round'); - break; - case LineStyle::DASH_STYLE_SQUARE_DOT: - $xmlWriter->writeAttribute('dashstyle', '1 1'); - break; - case LineStyle::DASH_STYLE_DASH_DOT: - $xmlWriter->writeAttribute('dashstyle', 'dashDot'); - break; - case LineStyle::DASH_STYLE_LONG_DASH: - $xmlWriter->writeAttribute('dashstyle', 'longDash'); - break; - case LineStyle::DASH_STYLE_LONG_DASH_DOT: - $xmlWriter->writeAttribute('dashstyle', 'longDashDot'); - break; - case LineStyle::DASH_STYLE_LONG_DASH_DOT_DOT: - $xmlWriter->writeAttribute('dashstyle', 'longDashDotDot'); - break; + + $xmlWriter->writeAttributeIf($beginArrow !== null, 'startarrow', $beginArrow); + $xmlWriter->writeAttributeIf($endArrow !== null, 'endarrow', $endArrow); + + if ($dash !== null) { + if (array_key_exists($dash, $dashStyles)) { + $xmlWriter->writeAttribute('dashstyle', $dashStyles[$dash]); + } + if ($dash == LineStyle::DASH_STYLE_ROUND_DOT) { + $xmlWriter->writeAttribute('endcap', 'round'); } } + $xmlWriter->endElement(); //v:stroke } } diff --git a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php index ee8b88ae..23ba575b 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php @@ -30,7 +30,8 @@ class ElementTest extends \PHPUnit_Framework_TestCase { $elements = array( 'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun', - 'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC', 'Field' + 'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC', + 'Field', 'Line' ); foreach ($elements as $element) { $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element; diff --git a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php index 09f7d6a4..9223470e 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php @@ -87,6 +87,17 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat', 'SakaEraCalendar')); $section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat', 'LastUsedFormat')); $section->addField('PAGE', array('format'=>'ArabicDash')); + $section->addLine( + array( + 'width' => 10, + 'height' => 10, + 'positioning' => 'absolute', + 'beginArrow' => 'block', + 'endArrow' => 'open', + 'dash' => 'rounddot', + 'weight' => 10 + ) + ); $doc = TestHelperDOCX::getDocument($phpWord); diff --git a/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php b/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php index cc722efb..8303e92b 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php @@ -30,7 +30,8 @@ class StyleTest extends \PHPUnit_Framework_TestCase { $styles = array( 'Alignment', 'Cell', 'Font', 'Image', 'Indentation', 'LineNumbering', - 'Paragraph', 'Row', 'Section', 'Shading', 'Spacing', 'Tab', 'Table', 'TextBox' + 'Paragraph', 'Row', 'Section', 'Shading', 'Spacing', 'Tab', 'Table', + 'TextBox', 'Line' ); foreach ($styles as $style) { $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\' . $style;