Skip to content

Commit e9a6a2c

Browse files
committed
IMPROVED : Writer/ODText for just generating a simple file with some formatting texts and paragraphs
1 parent f5da76e commit e9a6a2c

File tree

6 files changed

+601
-39
lines changed

6 files changed

+601
-39
lines changed

src/PHPWord/Section/Text.php

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,10 @@ class PHPWord_Section_Text {
6565
*/
6666
public function __construct($text = null, $styleFont = null, $styleParagraph = null) {
6767
// Set font style
68-
if(is_array($styleFont)) {
69-
$this->_styleFont = new PHPWord_Style_Font('text');
70-
71-
foreach($styleFont as $key => $value) {
72-
if(substr($key, 0, 1) != '_') {
73-
$key = '_'.$key;
74-
}
75-
$this->_styleFont->setStyleValue($key, $value);
76-
}
77-
} else {
78-
$this->_styleFont = $styleFont;
79-
}
68+
$this->setFontStyle($styleFont);
8069

8170
// Set paragraph style
82-
if(is_array($styleParagraph)) {
83-
$this->_styleParagraph = new PHPWord_Style_Paragraph();
84-
85-
foreach($styleParagraph as $key => $value) {
86-
if(substr($key, 0, 1) != '_') {
87-
$key = '_'.$key;
88-
}
89-
$this->_styleParagraph->setStyleValue($key, $value);
90-
}
91-
} else {
92-
$this->_styleParagraph = $styleParagraph;
93-
}
71+
$this->setParagraphStyle($styleParagraph);
9472

9573
$this->_text = $text;
9674

@@ -106,6 +84,26 @@ public function getFontStyle() {
10684
return $this->_styleFont;
10785
}
10886

87+
/**
88+
* Set Text style
89+
*
90+
* @return PHPWord_Style_Font
91+
*/
92+
public function setFontStyle($styleFont) {
93+
if(is_array($styleFont)) {
94+
$this->_styleFont = new PHPWord_Style_Font('text');
95+
96+
foreach($styleFont as $key => $value) {
97+
if(substr($key, 0, 1) != '_') {
98+
$key = '_'.$key;
99+
}
100+
$this->_styleFont->setStyleValue($key, $value);
101+
}
102+
} else {
103+
$this->_styleFont = $styleFont;
104+
}
105+
}
106+
109107
/**
110108
* Get Paragraph style
111109
*
@@ -115,6 +113,26 @@ public function getParagraphStyle() {
115113
return $this->_styleParagraph;
116114
}
117115

116+
/**
117+
* Set Paragraph style
118+
*
119+
* @return PHPWord_Style_Paragraph
120+
*/
121+
public function setParagraphStyle($styleParagraph) {
122+
if(is_array($styleParagraph)) {
123+
$this->_styleParagraph = new PHPWord_Style_Paragraph();
124+
125+
foreach($styleParagraph as $key => $value) {
126+
if(substr($key, 0, 1) != '_') {
127+
$key = '_'.$key;
128+
}
129+
$this->_styleParagraph->setStyleValue($key, $value);
130+
}
131+
} else {
132+
$this->_styleParagraph = $styleParagraph;
133+
}
134+
}
135+
118136
/**
119137
* Get Text content
120138
*

src/PHPWord/Shared/Drawing.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,28 @@ public static function angleToDegrees($pValue = 0) {
9999
return 0;
100100
}
101101
}
102+
103+
/**
104+
* Convert pixels to centimeters
105+
*
106+
* @param int $pValue Value in pixels
107+
* @return int Value in centimeters
108+
*/
109+
public static function pixelsToCentimeters($pValue = 0) {
110+
return $pValue * 0.028;
111+
}
112+
113+
/**
114+
* Convert centimeters width to pixels
115+
*
116+
* @param int $pValue Value in centimeters
117+
* @return int Value in pixels
118+
*/
119+
public static function centimetersToPixels($pValue = 0) {
120+
if ($pValue != 0) {
121+
return $pValue * 0.028;
122+
} else {
123+
return 0;
124+
}
125+
}
102126
}

src/PHPWord/Writer/ODText.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PHPWord_Writer_ODText implements PHPWord_Writer_IWriter
3232
*
3333
* @var PHPWord
3434
*/
35-
private $_presentation;
35+
private $_document;
3636

3737
/**
3838
* Private writer parts
@@ -80,6 +80,7 @@ public function __construct(PHPWord $pPHPWord = null)
8080
$this->_writerParts['manifest'] = new PHPWord_Writer_ODText_Manifest();
8181
$this->_writerParts['meta'] = new PHPWord_Writer_ODText_Meta();
8282
$this->_writerParts['mimetype'] = new PHPWord_Writer_ODText_Mimetype();
83+
$this->_writerParts['styles'] = new PHPWord_Writer_ODText_Styles();
8384

8485

8586
// Assign parent IWriter
@@ -99,7 +100,7 @@ public function __construct(PHPWord $pPHPWord = null)
99100
*/
100101
public function save($pFilename = null)
101102
{
102-
if (!is_null($this->_presentation)) {
103+
if (!is_null($this->_document)) {
103104
// If $pFilename is php://output or php://stdout, make it a temporary file...
104105
$originalFilename = $pFilename;
105106
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
@@ -123,16 +124,19 @@ public function save($pFilename = null)
123124

124125
// Add mimetype to ZIP file
125126
//@todo Not in ZIPARCHIVE::CM_STORE mode
126-
$objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->_presentation));
127+
$objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->_document));
127128

128129
// Add content.xml to ZIP file
129-
$objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->_presentation));
130+
$objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->_document));
130131

131132
// Add meta.xml to ZIP file
132-
$objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->_presentation));
133+
$objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->_document));
134+
135+
// Add styles.xml to ZIP file
136+
$objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document));
133137

134138
// Add META-INF/manifest.xml
135-
$objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->_presentation));
139+
$objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->_document));
136140

137141
// Add media
138142
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
@@ -192,8 +196,8 @@ public function save($pFilename = null)
192196
* @throws Exception
193197
*/
194198
public function getPHPWord() {
195-
if (!is_null($this->_presentation)) {
196-
return $this->_presentation;
199+
if (!is_null($this->_document)) {
200+
return $this->_document;
197201
} else {
198202
throw new Exception("No PHPWord assigned.");
199203
}
@@ -207,7 +211,7 @@ public function getPHPWord() {
207211
* @return PHPWord_Writer_PowerPoint2007
208212
*/
209213
public function setPHPWord(PHPWord $pPHPWord = null) {
210-
$this->_presentation = $pPHPWord;
214+
$this->_document = $pPHPWord;
211215
return $this;
212216
}
213217

0 commit comments

Comments
 (0)