Skip to content

Commit 3efc30e

Browse files
committed
Rasamassen PR's 2836, 2839, and 2850
I made RTF usable some time ago. @rasamassen has recently done a lot of useful work to cover many missing areas. Since who knows when those changes will be merged, I intend to incorporate much of that work. I will do this with several pushes, each based on one or more of those changes. This one is based on PR PHPOffice#2836, PR PHPOffice#2839, and PR PHPOffice#2850.
1 parent 598f6ae commit 3efc30e

File tree

15 files changed

+463
-101
lines changed

15 files changed

+463
-101
lines changed

samples/Sample_20_BGColor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
$section->addText(
1313
'This is some text highlighted using fgColor (limited to 15 colors)',
14-
['fgColor' => PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW]
14+
['fgColor' => PhpOffice\PhpWord\SimpleType\Color::YELLOW]
1515
);
1616
$section->addText('This one uses bgColor and is using hex value (0xfbbb10)', ['bgColor' => 'fbbb10']);
1717
$section->addText('Compatible with font colors', ['color' => '0000ff', 'bgColor' => 'fbbb10']);

src/PhpWord/Shared/Converter.php

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

1919
namespace PhpOffice\PhpWord\Shared;
2020

21+
use PhpOffice\PhpWord\SimpleType\Color;
22+
2123
/**
2224
* Common converter functions.
2325
*/
@@ -295,6 +297,49 @@ public static function angleToDegree($angle = 1)
295297
return round($angle / self::DEGREE_TO_ANGLE);
296298
}
297299

300+
private const STRING_TO_RGB = [
301+
Color::AQUA => '00FFFF',
302+
Color::BLACK => '000000',
303+
Color::BLUE => '0000FF',
304+
Color::BROWN => 'A52A2A',
305+
Color::CYAN => '00FFFF',
306+
Color::DARKBLUE => '00008B',
307+
Color::DARKCYAN => '008B8B',
308+
Color::DARKGRAY => 'A9A9A9',
309+
Color::DARKGREEN => '006400',
310+
Color::DARKMAGENTA => '8B008B',
311+
Color::DARKORANGE => 'FF8C00',
312+
Color::DARKRED => '8B0000',
313+
Color::DARKVIOLET => '9400D3',
314+
Color::DARKYELLOW => '808000',
315+
Color::FUCHSIA => 'FF00FF',
316+
Color::GOLD => 'FFD700',
317+
Color::GRAY => '808080',
318+
Color::GREEN => '008000',
319+
Color::LIGHTBLUE => 'ADD8E6',
320+
Color::LIGHTCYAN => 'E0FFFF',
321+
Color::LIGHTGRAY => 'D3D3D3',
322+
Color::LIGHTGREEN => '90EE90',
323+
Color::LIGHTPINK => 'FFB6C1',
324+
Color::LIGHTYELLOW => 'FFFFE0',
325+
Color::LIME => '00FF00',
326+
Color::MAGENTA => 'FF00FF',
327+
Color::MAROON => '800000',
328+
Color::NAVY => '000080',
329+
Color::OLIVE => '808000',
330+
Color::ORANGE => 'FFA500',
331+
Color::PINK => 'FFC0CB',
332+
Color::PURPLE => '800080',
333+
Color::RED => 'FF0000',
334+
Color::SILVER => 'C0C0C0',
335+
Color::TAN => 'D2B48C',
336+
Color::TEAL => '008080',
337+
Color::TURQUOISE => '40E0D0',
338+
Color::VIOLET => 'EE82EE',
339+
Color::WHITE => 'FFFFFF',
340+
Color::YELLOW => 'FFFF00',
341+
];
342+
298343
/**
299344
* Convert colorname as string to RGB.
300345
*
@@ -304,40 +349,7 @@ public static function angleToDegree($angle = 1)
304349
*/
305350
public static function stringToRgb($value)
306351
{
307-
switch ($value) {
308-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW:
309-
return 'FFFF00';
310-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGREEN:
311-
return '90EE90';
312-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_CYAN:
313-
return '00FFFF';
314-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_MAGENTA:
315-
return 'FF00FF';
316-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLUE:
317-
return '0000FF';
318-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_RED:
319-
return 'FF0000';
320-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKBLUE:
321-
return '00008B';
322-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKCYAN:
323-
return '008B8B';
324-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGREEN:
325-
return '006400';
326-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKMAGENTA:
327-
return '8B008B';
328-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKRED:
329-
return '8B0000';
330-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKYELLOW:
331-
return '8B8B00';
332-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGRAY:
333-
return 'A9A9A9';
334-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGRAY:
335-
return 'D3D3D3';
336-
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLACK:
337-
return '000000';
338-
}
339-
340-
return $value;
352+
return self::STRING_TO_RGB[$value] ?? $value;
341353
}
342354

343355
/**

src/PhpWord/SimpleType/Color.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWord\SimpleType;
20+
21+
use PhpOffice\PhpWord\Shared\AbstractEnum;
22+
23+
/**
24+
* Colors.
25+
* See https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.drawing.presetcolor
26+
* See https://www.datypic.com/sc/ooxml/t-a_ST_PresetColorVal.html.
27+
* See https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_ST_PresetColorVal_topic_ID0ELA5NB.html.
28+
*
29+
* Highlight colors limited to certain select colors.
30+
* See https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_ST_HighlightColor_topic_ID0E4PY2.html.
31+
*
32+
* Highlight but not foreground: GREEN, WHITE
33+
* Foreground but not highlight: DARKGRAY, LIGHTGREEN
34+
*/
35+
final class Color extends AbstractEnum
36+
{
37+
const AQUA = 'aqua';
38+
const BLACK = 'black'; // highlight color, foreground
39+
const BLUE = 'blue'; // highlight color, foreground
40+
const BROWN = 'brown';
41+
const CYAN = 'cyan'; // highlight color, foreground
42+
const DARKBLUE = 'darkBlue'; // highlight color, foreground
43+
const DARKCYAN = 'darkCyan'; // highlight color, foreground
44+
const DARKGRAY = 'darkGray'; // foreground
45+
const DARKGREEN = 'darkGreen'; // highlight color, foreground
46+
const DARKMAGENTA = 'darkMagenta'; // highlight color, foreground
47+
const DARKORANGE = 'darkOrange';
48+
const DARKRED = 'darkRed'; // highlight color, foreground
49+
const DARKVIOLET = 'darkViolet';
50+
const DARKYELLOW = 'darkYellow'; // highlight color, foreground
51+
const FUCHSIA = 'fuchsia';
52+
const GOLD = 'gold';
53+
const GRAY = 'gray';
54+
const GREEN = 'green'; // highlight color
55+
const LIGHTBLUE = 'lightBlue';
56+
const LIGHTCYAN = 'lightCyan';
57+
const LIGHTGRAY = 'lightGray'; // highlight color, foreground
58+
const LIGHTGREEN = 'lightGreen'; // foreground
59+
const LIGHTPINK = 'lightPink';
60+
const LIGHTYELLOW = 'lightYellow';
61+
const LIME = 'lime';
62+
const MAGENTA = 'magenta'; // highlight color, foreground
63+
const MAROON = 'maroon';
64+
const NAVY = 'navy';
65+
const OLIVE = 'olive';
66+
const ORANGE = 'orange';
67+
const PINK = 'pink';
68+
const PURPLE = 'purple';
69+
const RED = 'red'; // highlight color, foreground
70+
const SILVER = 'silver';
71+
const TAN = 'tan';
72+
const TEAL = 'teal';
73+
const TURQUOISE = 'turqoise';
74+
const VIOLET = 'violet';
75+
const WHITE = 'white'; // highlight color
76+
const YELLOW = 'yellow'; // highlight color, foreground
77+
}

src/PhpWord/Style/Font.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,59 @@ class Font extends AbstractStyle
3333
*/
3434
const UNDERLINE_NONE = 'none';
3535
const UNDERLINE_DASH = 'dash';
36-
const UNDERLINE_DASHHEAVY = 'dashHeavy';
36+
const UNDERLINE_DASHDOTHEAVY = 'dashDotHeavy';
37+
const UNDERLINE_DASHDOTDOTHEAVY = 'dashDotDotHeavy';
38+
const UNDERLINE_DASHEDHEAVY = 'dashedHeavy';
3739
const UNDERLINE_DASHLONG = 'dashLong';
3840
const UNDERLINE_DASHLONGHEAVY = 'dashLongHeavy';
39-
const UNDERLINE_DOUBLE = 'dbl';
41+
const UNDERLINE_DOUBLE = 'double';
4042
const UNDERLINE_DOTDASH = 'dotDash';
41-
const UNDERLINE_DOTDASHHEAVY = 'dotDashHeavy';
4243
const UNDERLINE_DOTDOTDASH = 'dotDotDash';
43-
const UNDERLINE_DOTDOTDASHHEAVY = 'dotDotDashHeavy';
4444
const UNDERLINE_DOTTED = 'dotted';
4545
const UNDERLINE_DOTTEDHEAVY = 'dottedHeavy';
46-
const UNDERLINE_HEAVY = 'heavy';
46+
const UNDERLINE_HEAVY = 'thick';
4747
const UNDERLINE_SINGLE = 'single';
48-
const UNDERLINE_WAVY = 'wavy';
49-
const UNDERLINE_WAVYDOUBLE = 'wavyDbl';
48+
const UNDERLINE_WAVY = 'wave';
49+
const UNDERLINE_WAVYDOUBLE = 'wavyDouble';
5050
const UNDERLINE_WAVYHEAVY = 'wavyHeavy';
5151
const UNDERLINE_WORDS = 'words';
52-
53-
/**
54-
* Foreground colors.
55-
*
56-
* @const string
57-
*/
52+
/** @deprecated 2.0 use UNDERLINE_DASHEDHEAVY */
53+
const UNDERLINE_DASHHEAVY = self::UNDERLINE_DASHEDHEAVY;
54+
/** @deprecated 2.0 use UNDERLINE_DASHDOTHEAVY */
55+
const UNDERLINE_DOTDASHHEAVY = self::UNDERLINE_DASHDOTHEAVY;
56+
/** @deprecated 2.0 use UNDERLINE_DASHDOTHEAVY */
57+
const UNDERLINE_DOTDOTDASHHEAVY = self::UNDERLINE_DASHDOTDOTHEAVY;
58+
59+
// Foreground colors.
60+
/** @deprecated 2.0 use SimpleType\Color::YELLOW */
5861
const FGCOLOR_YELLOW = 'yellow';
59-
const FGCOLOR_LIGHTGREEN = 'green';
62+
/** @deprecated 2.0 use SimpleType\Color::LIGHTGREEN */
63+
const FGCOLOR_LIGHTGREEN = 'lightGreen';
64+
/** @deprecated 2.0 use SimpleType\Color::CYAN */
6065
const FGCOLOR_CYAN = 'cyan';
66+
/** @deprecated 2.0 use SimpleType\Color::MAGENTA */
6167
const FGCOLOR_MAGENTA = 'magenta';
68+
/** @deprecated 2.0 use SimpleType\Color::BLUE */
6269
const FGCOLOR_BLUE = 'blue';
70+
/** @deprecated 2.0 use SimpleType\Color::RED */
6371
const FGCOLOR_RED = 'red';
72+
/** @deprecated 2.0 use SimpleType\Color::DARKBLUE */
6473
const FGCOLOR_DARKBLUE = 'darkBlue';
74+
/** @deprecated 2.0 use SimpleType\Color::DARKCYAN */
6575
const FGCOLOR_DARKCYAN = 'darkCyan';
76+
/** @deprecated 2.0 use SimpleType\Color::DARKGREEN */
6677
const FGCOLOR_DARKGREEN = 'darkGreen';
78+
/** @deprecated 2.0 use SimpleType\Color::DARKMAGENTA */
6779
const FGCOLOR_DARKMAGENTA = 'darkMagenta';
80+
/** @deprecated 2.0 use SimpleType\Color::DARKRED */
6881
const FGCOLOR_DARKRED = 'darkRed';
82+
/** @deprecated 2.0 use SimpleType\Color::DARKYELLOW */
6983
const FGCOLOR_DARKYELLOW = 'darkYellow';
84+
/** @deprecated 2.0 use SimpleType\Color::DARKGRAY */
7085
const FGCOLOR_DARKGRAY = 'darkGray';
86+
/** @deprecated 2.0 use SimpleType\Color::LIGHTGRAY */
7187
const FGCOLOR_LIGHTGRAY = 'lightGray';
88+
/** @deprecated 2.0 use SimpleType\Color::BLACK */
7289
const FGCOLOR_BLACK = 'black';
7390

7491
/**

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@
2727
*/
2828
class Font extends AbstractStyle
2929
{
30+
private const UNDERLINES = [
31+
FontStyle::UNDERLINE_DASH => 'underline dashed ',
32+
FontStyle::UNDERLINE_DASHDOTDOTHEAVY => 'underline dotted 2px ',
33+
FontStyle::UNDERLINE_DASHDOTHEAVY => 'underline dotted 2px ',
34+
FontStyle::UNDERLINE_DASHEDHEAVY => 'underline dashed 2px ',
35+
FontStyle::UNDERLINE_DASHLONG => 'underline dashed ',
36+
FontStyle::UNDERLINE_DASHLONGHEAVY => 'underline dashed 2px ',
37+
FontStyle::UNDERLINE_DOTDASH => 'underline dotted ',
38+
FontStyle::UNDERLINE_DOTDOTDASH => 'underline dotted ',
39+
FontStyle::UNDERLINE_DOTTED => 'underline dotted ',
40+
FontStyle::UNDERLINE_DOTTEDHEAVY => 'underline dotted 2px ',
41+
FontStyle::UNDERLINE_DOUBLE => 'underline double ',
42+
FontStyle::UNDERLINE_HEAVY => 'underline solid 2px ',
43+
FontStyle::UNDERLINE_SINGLE => 'underline solid ',
44+
FontStyle::UNDERLINE_WAVY => 'underline wavy ',
45+
FontStyle::UNDERLINE_WAVYDOUBLE => 'underline wavy ',
46+
FontStyle::UNDERLINE_WAVYHEAVY => 'underline wavy 2px ',
47+
FontStyle::UNDERLINE_WORDS => 'underline solid ',
48+
];
49+
3050
/**
3151
* Write style.
3252
*
@@ -57,7 +77,9 @@ public function write()
5777
$css['vertical-align'] .= $this->getValueIf($style->isSuperScript(), 'super');
5878
$css['vertical-align'] .= $this->getValueIf($style->isSubScript(), 'sub');
5979
$css['text-decoration'] = '';
60-
$css['text-decoration'] .= $this->getValueIf($underline, 'underline ');
80+
if (isset(self::UNDERLINES[$style->getUnderline()])) {
81+
$css['text-decoration'] .= $this->getValueIf($underline, self::UNDERLINES[$style->getUnderline()]);
82+
}
6183
$css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through ');
6284
$css['text-transform'] = $this->getValueIf($style->isAllCaps(), 'uppercase');
6385
$css['font-variant'] = $this->getValueIf($style->isSmallCaps(), 'small-caps');

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
namespace PhpOffice\PhpWord\Writer\ODText\Style;
2020

2121
use PhpOffice\PhpWord\Style;
22+
use PhpOffice\PhpWord\Style\Font as FontStyle;
2223

2324
/**
2425
* Font style writer.
@@ -27,15 +28,39 @@
2728
*/
2829
class Font extends AbstractStyle
2930
{
31+
private const UNDERLINES = [
32+
FontStyle::UNDERLINE_DASH => 'dash',
33+
FontStyle::UNDERLINE_DASHDOTDOTHEAVY => 'dot-dot-dash',
34+
FontStyle::UNDERLINE_DASHDOTHEAVY => 'dot-dash',
35+
FontStyle::UNDERLINE_DASHEDHEAVY => 'dash',
36+
FontStyle::UNDERLINE_DASHLONG => 'long-dash',
37+
FontStyle::UNDERLINE_DASHLONGHEAVY => 'long-dash',
38+
FontStyle::UNDERLINE_DOTDASH => 'dot-dash',
39+
FontStyle::UNDERLINE_DOTDOTDASH => 'dot-dot-dash',
40+
FontStyle::UNDERLINE_DOTTED => 'dotted',
41+
FontStyle::UNDERLINE_DOTTEDHEAVY => 'dotted',
42+
FontStyle::UNDERLINE_DOUBLE => 'solid',
43+
FontStyle::UNDERLINE_HEAVY => 'solid',
44+
FontStyle::UNDERLINE_SINGLE => 'solid',
45+
FontStyle::UNDERLINE_WAVY => 'wave',
46+
FontStyle::UNDERLINE_WAVYDOUBLE => 'wave',
47+
FontStyle::UNDERLINE_WAVYHEAVY => 'wave',
48+
FontStyle::UNDERLINE_WORDS => 'solid',
49+
];
50+
3051
/**
3152
* Write style.
3253
*/
3354
public function write(): void
3455
{
3556
$style = $this->getStyle();
36-
if (!$style instanceof Style\Font) {
37-
return;
57+
if ($style instanceof FontStyle) {
58+
$this->writeStyle($style);
3859
}
60+
}
61+
62+
private function writeStyle(FontStyle $style): void
63+
{
3964
$xmlWriter = $this->getXmlWriter();
4065

4166
$stylep = $style->getParagraph();
@@ -74,9 +99,14 @@ public function write(): void
7499
$xmlWriter->writeAttributeIf($style->isItalic(), 'style:font-style-complex', 'italic');
75100

76101
// Underline
77-
// @todo Various mode of underline
78102
$underline = $style->getUnderline();
79-
$xmlWriter->writeAttributeIf($underline != 'none', 'style:text-underline-style', 'solid');
103+
if (isset(self::UNDERLINES[$underline])) {
104+
$xmlWriter->writeAttribute('style:text-underline-style', self::UNDERLINES[$underline]);
105+
$xmlWriter->writeAttributeIf(strpos(strtolower($underline), 'heavy') !== false, 'style:text-underline-width', 'bold');
106+
$xmlWriter->writeAttributeIf(strpos(strtolower($underline), 'thick') !== false, 'style:text-underline-width', 'bold');
107+
$xmlWriter->writeAttributeIf(strpos(strtolower($underline), 'double') !== false, 'style:text-underline-type', 'double');
108+
$xmlWriter->writeAttributeIf(strpos(strtolower($underline), 'words') !== false, 'style:text-underline-mode', 'skip-white-space');
109+
}
80110

81111
// Strikethrough, double strikethrough
82112
$xmlWriter->writeAttributeIf($style->isStrikethrough(), 'style:text-line-through-type', 'single');

0 commit comments

Comments
 (0)