Skip to content

Commit 6769df2

Browse files
committed
Sample 11 Fixed for Odt and Docx, Partial for RTF and HTML
Still not out of the woods. Write to Docx and Odt are fine. Write to RTF and HTML are fine only if they follow write to ODT.
1 parent 6445176 commit 6769df2

File tree

7 files changed

+76
-11
lines changed

7 files changed

+76
-11
lines changed

src/PhpWord/Style.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,24 @@ public static function getStyles()
178178
/**
179179
* Get style by name.
180180
*
181-
* @param string $styleName
181+
* @param ?string $styleName
182182
*
183183
* @return ?AbstractStyle Paragraph|Font|Table|Numbering
184184
*/
185185
public static function getStyle($styleName)
186186
{
187+
if ($styleName === null) {
188+
return null;
189+
}
187190
if (isset(self::$styles[$styleName])) {
188191
return self::$styles[$styleName];
189192
}
193+
foreach (self::$styles as $key => $value) {
194+
$styleId = self::alternateName($key);
195+
if ($styleId === $styleName) {
196+
return $value;
197+
}
198+
}
190199

191200
return null;
192201
}
@@ -221,4 +230,24 @@ private static function setStyleValues($name, $style, $value = null)
221230

222231
return self::getStyle($name);
223232
}
233+
234+
/**
235+
* Get alternate name for style names with embedded spaces.
236+
*
237+
* @param string $name
238+
*
239+
* @return string
240+
*/
241+
public static function alternateName($name)
242+
{
243+
$explode = explode(' ', $name);
244+
if (count($explode) > 1) {
245+
$name = '';
246+
foreach ($explode as $explodeItem) {
247+
$name .= ucfirst($explodeItem);
248+
}
249+
}
250+
251+
return $name;
252+
}
224253
}

src/PhpWord/Writer/HTML/Part/Head.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ private function writeStyles(): string
130130
// Custom styles
131131
$customStyles = Style::getStyles();
132132
if (is_array($customStyles)) {
133-
foreach ($customStyles as $name => $style) {
133+
foreach ($customStyles as $namex => $style) {
134+
$name = Style::alternateName($namex);
134135
$styleParagraph = null;
135136
if ($style instanceof Font) {
136137
$styleWriter = new FontStyleWriter($style);

src/PhpWord/Writer/ODText/Style/Font.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\ODText\Style;
2020

21+
use PhpOffice\PhpWord\Style;
22+
2123
/**
2224
* Font style writer.
2325
*
@@ -31,21 +33,21 @@ class Font extends AbstractStyle
3133
public function write(): void
3234
{
3335
$style = $this->getStyle();
34-
if (!$style instanceof \PhpOffice\PhpWord\Style\Font) {
36+
if (!$style instanceof Style\Font) {
3537
return;
3638
}
3739
$xmlWriter = $this->getXmlWriter();
3840

3941
$stylep = $style->getParagraph();
40-
if ($stylep instanceof \PhpOffice\PhpWord\Style\Paragraph) {
42+
if ($stylep instanceof Style\Paragraph) {
4143
$temp1 = clone $stylep;
4244
$temp1->setStyleName($style->getStyleName());
4345
$temp2 = new Paragraph($xmlWriter, $temp1);
4446
$temp2->write();
4547
}
4648

4749
$xmlWriter->startElement('style:style');
48-
$xmlWriter->writeAttribute('style:name', $style->getStyleName());
50+
$xmlWriter->writeAttribute('style:name', Style::alternateName($style->getStyleName()));
4951
$xmlWriter->writeAttribute('style:family', 'text');
5052
$xmlWriter->startElement('style:text-properties');
5153

@@ -100,9 +102,9 @@ public function write(): void
100102
$xmlWriter->writeAttribute('style:country-complex', 'none');
101103
}
102104

103-
// @todo Foreground-Color
104-
105-
// @todo Background color
105+
// Foreground-Color (which is really background color)
106+
$fgColor = $style->getFgColor();
107+
$xmlWriter->writeAttributeIf($fgColor != '', 'fo:background-color', '#' . \PhpOffice\PhpWord\Shared\Converter::stringToRgb($fgColor));
106108

107109
$xmlWriter->endElement(); // style:text-properties
108110
$xmlWriter->endElement(); // style:style

src/PhpWord/Writer/RTF/Element/Link.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,21 @@ public function write()
4141
$content = '';
4242
$content .= $this->writeOpening();
4343
$content .= '{\field {\*\fldinst {HYPERLINK "' . $this->element->getSource() . '"}}{\\fldrslt {';
44-
$content .= $this->writeFontStyle();
44+
$temp = $this->writeFontStyle();
45+
$content .= $temp;
46+
if ($temp === '\cf0\f0 ') {
47+
$content .= '\ul ';
48+
$colors = $this->parentWriter->getColorTable();
49+
$count = count($colors);
50+
for ($i = 0; $i < $count; ++$i) {
51+
if ($colors[$i] === '0000FF') {
52+
$j = $i + 1;
53+
$content .= '\cf' . $j . ' ';
54+
55+
break;
56+
}
57+
}
58+
}
4559
$content .= $this->writeText($this->element->getText());
4660
$content .= '}}}';
4761
$content .= $this->writeClosing();

src/PhpWord/Writer/RTF/Element/Text.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ public function write()
4545
$content .= $this->writeOpening();
4646
$content .= '{';
4747
$content .= $this->writeFontStyle();
48+
$change = $element->getTrackChange();
49+
if ($change !== null) {
50+
if ($change->getChangeType() === 'DELETED') {
51+
$content .= '\strike ';
52+
}
53+
}
54+
if ($this->fontStyle !== null) {
55+
$fgColor = $this->fontStyle->getFgColor();
56+
if ($fgColor !== null) {
57+
$fgColorIndex = array_search($fgColor, $this->parentWriter->getColorTable());
58+
if ($fgColorIndex !== false) {
59+
$content .= '\highlight' . ++$fgColorIndex . ' ';
60+
}
61+
}
62+
}
4863
$content .= $this->writeText($element->getText());
4964
$content .= '}';
5065
$content .= $this->writeClosing();

src/PhpWord/Writer/Word2007/Part/Styles.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ private function writeFontStyle(XMLWriter $xmlWriter, $styleName, FontStyle $sty
202202
} elseif (null !== $paragraphStyle) {
203203
// if type is 'paragraph' it should have a styleId
204204
$xmlWriter->writeAttribute('w:styleId', $styleName);
205+
} else {
206+
$styleId = Style::alternateName($styleName);
207+
if ($styleName !== $styleId) {
208+
$xmlWriter->writeAttribute('w:styleId', $styleId);
209+
}
205210
}
206211

207212
// Style name

src/PhpWord/Writer/Word2007/Style/Font.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public function write(): void
3939
{
4040
$xmlWriter = $this->getXmlWriter();
4141

42-
$isStyleName = $this->isInline && null !== $this->style && is_string($this->style);
43-
if ($isStyleName) {
42+
if ($this->isInline && null !== $this->style && is_string($this->style)) {
4443
$xmlWriter->startElement('w:rPr');
4544
$xmlWriter->startElement('w:rStyle');
4645
$xmlWriter->writeAttribute('w:val', $this->style);

0 commit comments

Comments
 (0)