Skip to content

Commit b3045ea

Browse files
authored
Merge pull request #648 from GravityPDF/fix/539-inherited-channels
Carry every inherited text property through every channel that hands inherited values on
1 parent 0026354 commit b3045ea

19 files changed

Lines changed: 1102 additions & 99 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ Bugfixes
224224
* `linear-gradient(to bottom, …)` and `to top` were drawn upside down, and so was the vertical half of `to bottom right` and the other corners. Angles ran counter-clockwise from pointing right, so `90deg` drew bottom to top. A gradient now runs as CSS Images gives: `0deg` points up and angles turn clockwise, `turn` is read, and a corner keyword leaves the other two corners halfway along. `-moz-`, `-webkit-` and `-o-` gradients keep their legacy angles, and their side keyword, such as `left`, names the side the gradient starts from. Set `'cssMode' => \Mpdf\CssMode::LEGACY` to keep the directions mPDF v7 drew, prefixed or not. [#577] [#639]
225225
* In a table with collapsed borders, a row with a `border-color` and no border style, or a border of no width, took the borders of its cells away. A reset such as `* { border-color: #dee2e6 }` or `* { border: 0 solid }` gives every row a border like that. A row border that draws nothing now leaves the cells' borders; `hidden` still takes them away. A row's `border-top` or `border-bottom` was drawn only when the row also set `border-left`; each side is now drawn on its own. [#644]
226226
* Characters mPDF moved into a substitute font (`useSubstitutions`, and the fonts for supplementary planes) were wrapped in a `<span>` that stylesheet rules reached, so `span { border: 1px solid }` drew a box around them, and the span counted among its siblings for `:nth-child()`. In the standard CSS mode no rule reaches that span and it is not counted, as a browser has no such element. The legacy mode styles it as before. [#634]
227+
* Some inherited properties stopped short of where a browser takes them. A `text-shadow` did not reach a block's child blocks or list items. `word-spacing`, `hyphens` and `text-outline` did not reach the content of a positioned block. `text-transform`, `text-shadow`, `font-variant` and the other font features did not reach a table's cells. A table nested in a cell took none of the cell's colour, font, size, weight, style, transform or shadow. Each of these channels now carries every inherited property mPDF supports, from the one list in `Mpdf\Css\InheritedProperties`. Under the standard `cssMode` only; set `'cssMode' => \Mpdf\CssMode::LEGACY` to keep the old output. [#539] [#648]
228+
* `text-transform: none` on an inline element, such as a `<span>` in an uppercase paragraph, did not undo the transform it inherits. Under the standard `cssMode` only; set `'cssMode' => \Mpdf\CssMode::LEGACY` to keep the transform. [#552] [#539] [#648]
227229

228230
### Images and SVG
229231

@@ -827,6 +829,7 @@ These changes do not change output.
827829
[#537]: https://github.com/GravityPDF/mpdf/issues/537
828830
[#538]: https://github.com/GravityPDF/mpdf/issues/538
829831
[#545]: https://github.com/GravityPDF/mpdf/issues/545
832+
[#539]: https://github.com/GravityPDF/mpdf/issues/539
830833
[#548]: https://github.com/GravityPDF/mpdf/issues/548
831834
[#549]: https://github.com/GravityPDF/mpdf/pull/549
832835
[#552]: https://github.com/GravityPDF/mpdf/issues/552
@@ -885,3 +888,4 @@ These changes do not change output.
885888
[#643]: https://github.com/GravityPDF/mpdf/pull/643
886889
[#644]: https://github.com/GravityPDF/mpdf/pull/644
887890
[#647]: https://github.com/GravityPDF/mpdf/pull/647
891+
[#648]: https://github.com/GravityPDF/mpdf/pull/648

‎src/Css/CssMerger.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,18 @@ protected function mergeInheritedBlockProperties($inherit, $tag)
510510
}
511511

512512
if (isset($previousBlock['InlineProperties'])) {
513-
$converted = $this->inlinePropertyConverter->convert($previousBlock['InlineProperties']);
513+
$saved = $previousBlock['InlineProperties'];
514+
if ($this->mpdf->cssMode === CssMode::LEGACY) {
515+
// mPDF v7 did not hand a block's text shadow on to its child blocks
516+
unset($saved['textshadow']);
517+
$converted = $this->inlinePropertyConverter->convert($saved);
518+
} else {
519+
// Text decorations and vertical-align are not inherited, but child blocks still take them (#544)
520+
$converted = InheritedProperties::of(
521+
$this->inlinePropertyConverter->convert($saved),
522+
array_merge(InheritedProperties::TEXT, ['TEXT-DECORATION', 'VERTICAL-ALIGN'])
523+
);
524+
}
514525
$this->cssProperties = array_merge($this->cssProperties, $converted); // mPDF 5.7.1
515526
}
516527
}

‎src/Css/InheritedProperties.php‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Mpdf\Css;
4+
5+
/**
6+
* The inherited CSS properties mPDF supports, by the names the merged CSS uses for them.
7+
*
8+
* An element takes these from its parent unless it sets them itself. Under CssMode::STANDARD each channel that hands
9+
* them on takes them from here:
10+
*
11+
* - a block to its child blocks: CssMerger::mergeInheritedBlockProperties() reads TEXT back from the block's saved
12+
* text state through InlinePropertyConverter, and BLOCK from the block's level of the block stack;
13+
* - a positioned block to its content: Mpdf::WriteFixedPosHTML() puts TEXT and BLOCK from the block's merged CSS on
14+
* the <div> that stands in for the block;
15+
* - a table to its cells: Table::open() puts TEXT from the table's merged CSS in base_table_properties, which each
16+
* cell merges under its own CSS. The table hands on text-align, line-height, the line stacking and direction
17+
* through its own fields;
18+
* - a table cell to a table nested in it: Table::open() starts the nested table's base_table_properties and default
19+
* font from the cell's text state, read back through InlinePropertyConverter.
20+
*
21+
* To carry another property, add it to TEXT when it lives in the text state that Mpdf::saveInlineProperties() saves,
22+
* and make InlinePropertyConverter::convert() read it back. Otherwise add it to BLOCK, and carry it on the block stack
23+
* in mergeInheritedBlockProperties().
24+
* InheritedPropertiesTest checks that each channel carries each TEXT property.
25+
*
26+
* Under CssMode::LEGACY the channels carry the lists mPDF v7 did.
27+
*/
28+
final class InheritedProperties
29+
{
30+
31+
/**
32+
* Held in the text state, and set by Mpdf::setCSS() for inline elements, blocks and table cells alike
33+
*/
34+
const TEXT = [
35+
'COLOR',
36+
'FONT-FAMILY',
37+
'FONT-SIZE',
38+
'FONT-STYLE',
39+
'FONT-WEIGHT',
40+
'FONT-KERNING',
41+
'FONT-VARIANT-POSITION',
42+
'FONT-VARIANT-CAPS',
43+
'FONT-VARIANT-LIGATURES',
44+
'FONT-VARIANT-NUMERIC',
45+
'FONT-VARIANT-ALTERNATES',
46+
'FONT-FEATURE-SETTINGS',
47+
'FONT-LANGUAGE-OVERRIDE',
48+
'LETTER-SPACING',
49+
'WORD-SPACING',
50+
'TEXT-TRANSFORM',
51+
'TEXT-SHADOW',
52+
'HYPHENS',
53+
'TEXT-OUTLINE',
54+
'TEXT-OUTLINE-COLOR',
55+
'TEXT-OUTLINE-WIDTH',
56+
];
57+
58+
/**
59+
* Held on the block, and read when its lines are laid out
60+
*/
61+
const BLOCK = [
62+
'TEXT-ALIGN',
63+
'TEXT-INDENT',
64+
'LINE-HEIGHT',
65+
'LINE-STACKING-STRATEGY',
66+
'LINE-STACKING-SHIFT',
67+
'DIRECTION',
68+
'LIST-STYLE-TYPE',
69+
'LIST-STYLE-IMAGE',
70+
'LIST-STYLE-POSITION',
71+
];
72+
73+
/**
74+
* What a positioned block handed its content under CssMode::LEGACY, in the order it was written. Text decorations
75+
* and the stacking order are not inherited, but the <div> that stands in for the block takes them too
76+
*/
77+
const LEGACY_POSITIONED = [
78+
'TEXT-ALIGN',
79+
'TEXT-TRANSFORM',
80+
'TEXT-INDENT',
81+
'TEXT-DECORATION',
82+
'FONT-FAMILY',
83+
'FONT-STYLE',
84+
'FONT-WEIGHT',
85+
'FONT-SIZE',
86+
'LINE-HEIGHT',
87+
'TEXT-SHADOW',
88+
'LETTER-SPACING',
89+
'FONT-VARIANT-POSITION',
90+
'FONT-VARIANT-CAPS',
91+
'FONT-VARIANT-LIGATURES',
92+
'FONT-VARIANT-NUMERIC',
93+
'FONT-VARIANT-ALTERNATES',
94+
'FONT-FEATURE-SETTINGS',
95+
'FONT-LANGUAGE-OVERRIDE',
96+
'FONT-KERNING',
97+
'COLOR',
98+
'Z-INDEX',
99+
];
100+
101+
/**
102+
* @return string[] Every inherited property mPDF supports: TEXT, then BLOCK
103+
*/
104+
public static function names()
105+
{
106+
return array_merge(self::TEXT, self::BLOCK);
107+
}
108+
109+
/**
110+
* The entries of a set of merged CSS properties that are inherited
111+
*
112+
* @param array $properties Merged CSS, keyed by uppercased property name
113+
* @param string[] $names The properties to take, such as TEXT
114+
*
115+
* @return array The entries of $properties named in $names, in the order of $names
116+
*/
117+
public static function of(array $properties, array $names)
118+
{
119+
$inherited = [];
120+
foreach ($names as $name) {
121+
if (isset($properties[$name])) {
122+
$inherited[$name] = $properties[$name];
123+
}
124+
}
125+
126+
return $inherited;
127+
}
128+
}

‎src/Css/InlinePropertyConverter.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function convert($properties)
6262
$css['WORD-SPACING'] = $properties['wSpacingCSS'];
6363
}
6464

65+
if (!empty($properties['textshadow'])) {
66+
$css['TEXT-SHADOW'] = $this->textShadow($properties['textshadow']);
67+
}
68+
6569
if (!empty($properties['textparam'])) {
6670
if (isset($properties['textparam']['hyphens'])) {
6771
$hyphens = (int) $properties['textparam']['hyphens'];
@@ -191,4 +195,27 @@ public function convert($properties)
191195

192196
return $css;
193197
}
198+
199+
/**
200+
* Writes parsed text shadows back as a text-shadow value
201+
*
202+
* @param array[] $shadows As ShadowParser::parseTextShadow() gives them: the last shadow written first, with the
203+
* offsets and blur in millimetres
204+
* @return string
205+
*/
206+
private function textShadow(array $shadows)
207+
{
208+
$values = [];
209+
foreach (array_reverse($shadows) as $shadow) {
210+
$values[] = sprintf(
211+
'%.10Fmm %.10Fmm %.10Fmm %s',
212+
$shadow['x'],
213+
$shadow['y'],
214+
$shadow['blur'],
215+
$this->colorConverter->colAtoString($shadow['col'])
216+
);
217+
}
218+
219+
return implode(', ', $values);
220+
}
194221
}

‎src/Mpdf.php‎

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Mpdf\Conversion;
88
use Mpdf\Css\Border;
99
use Mpdf\Css\CommentParser;
10+
use Mpdf\Css\InheritedProperties;
1011
use Mpdf\Css\RelativeFontValues;
1112
use Mpdf\Color\IccProfile;
1213
use Mpdf\Css\TextVars;
@@ -15428,71 +15429,15 @@ function WriteFixedPosHTML($html, $x, $y, $w, $h, $overflow = 'visible', $boundi
1542815429
}
1542915430

1543015431
// Pass on in-line properties to the innerhtml
15431-
$css = '';
15432-
if (isset($p['TEXT-ALIGN'])) {
15433-
$css .= 'text-align: ' . strtolower($p['TEXT-ALIGN']) . '; ';
15434-
}
15435-
if (isset($p['TEXT-TRANSFORM'])) {
15436-
$css .= 'text-transform: ' . strtolower($p['TEXT-TRANSFORM']) . '; ';
15437-
}
15438-
if (isset($p['TEXT-INDENT'])) {
15439-
$css .= 'text-indent: ' . strtolower($p['TEXT-INDENT']) . '; ';
15440-
}
15441-
if (isset($p['TEXT-DECORATION'])) {
15442-
$css .= 'text-decoration: ' . strtolower($p['TEXT-DECORATION']) . '; ';
15443-
}
15444-
if (isset($p['FONT-FAMILY'])) {
15445-
$css .= 'font-family: ' . strtolower($p['FONT-FAMILY']) . '; ';
15446-
}
15447-
if (isset($p['FONT-STYLE'])) {
15448-
$css .= 'font-style: ' . strtolower($p['FONT-STYLE']) . '; ';
15449-
}
15450-
if (isset($p['FONT-WEIGHT'])) {
15451-
$css .= 'font-weight: ' . strtolower($p['FONT-WEIGHT']) . '; ';
15452-
}
15453-
if (isset($p['FONT-SIZE'])) {
15454-
$css .= 'font-size: ' . strtolower($p['FONT-SIZE']) . '; ';
15455-
}
15456-
if (isset($p['LINE-HEIGHT'])) {
15457-
$css .= 'line-height: ' . strtolower($p['LINE-HEIGHT']) . '; ';
15458-
}
15459-
if (isset($p['TEXT-SHADOW'])) {
15460-
$css .= 'text-shadow: ' . strtolower($p['TEXT-SHADOW']) . '; ';
15461-
}
15462-
if (isset($p['LETTER-SPACING'])) {
15463-
$css .= 'letter-spacing: ' . strtolower($p['LETTER-SPACING']) . '; ';
15464-
}
15465-
// mPDF 6
15466-
if (isset($p['FONT-VARIANT-POSITION'])) {
15467-
$css .= 'font-variant-position: ' . strtolower($p['FONT-VARIANT-POSITION']) . '; ';
15468-
}
15469-
if (isset($p['FONT-VARIANT-CAPS'])) {
15470-
$css .= 'font-variant-caps: ' . strtolower($p['FONT-VARIANT-CAPS']) . '; ';
15471-
}
15472-
if (isset($p['FONT-VARIANT-LIGATURES'])) {
15473-
$css .= 'font-variant-ligatures: ' . strtolower($p['FONT-VARIANT-LIGATURES']) . '; ';
15474-
}
15475-
if (isset($p['FONT-VARIANT-NUMERIC'])) {
15476-
$css .= 'font-variant-numeric: ' . strtolower($p['FONT-VARIANT-NUMERIC']) . '; ';
15477-
}
15478-
if (isset($p['FONT-VARIANT-ALTERNATES'])) {
15479-
$css .= 'font-variant-alternates: ' . strtolower($p['FONT-VARIANT-ALTERNATES']) . '; ';
15480-
}
15481-
if (isset($p['FONT-FEATURE-SETTINGS'])) {
15482-
$css .= 'font-feature-settings: ' . strtolower($p['FONT-FEATURE-SETTINGS']) . '; ';
15483-
}
15484-
if (isset($p['FONT-LANGUAGE-OVERRIDE'])) {
15485-
$css .= 'font-language-override: ' . strtolower($p['FONT-LANGUAGE-OVERRIDE']) . '; ';
15486-
}
15487-
if (isset($p['FONT-KERNING'])) {
15488-
$css .= 'font-kerning: ' . strtolower($p['FONT-KERNING']) . '; ';
15489-
}
15490-
15491-
if (isset($p['COLOR'])) {
15492-
$css .= 'color: ' . strtolower($p['COLOR']) . '; ';
15432+
if ($this->cssMode === CssMode::LEGACY) {
15433+
$carried = InheritedProperties::LEGACY_POSITIONED;
15434+
} else {
15435+
// Text decorations and the stacking order are not inherited, but the <div> stands in for the block
15436+
$carried = array_merge(InheritedProperties::names(), ['TEXT-DECORATION', 'Z-INDEX']);
1549315437
}
15494-
if (isset($p['Z-INDEX'])) {
15495-
$css .= 'z-index: ' . $p['Z-INDEX'] . '; ';
15438+
$css = '';
15439+
foreach (InheritedProperties::of($p, $carried) as $property => $value) {
15440+
$css .= strtolower($property) . ': ' . strtolower($value) . '; ';
1549615441
}
1549715442
// The inner HTML is written as a document of its own, in which this <div> stands in for the positioned block
1549815443
$html = '<div style="' . $css . '">' . $html . '</div>';
@@ -20011,10 +19956,14 @@ function setCSS($arrayaux, $type = '', $tag = '')
2001119956
$this->textvar = ($this->textvar & ~TextVars::FT_CAPITALIZE); // mPDF 5.7.1
2001219957
break;
2001319958
case 'NONE':
20014-
break;
19959+
// mPDF v7 left the transform an inline element inherits in place
19960+
if ($this->cssMode === CssMode::LEGACY) {
19961+
break;
19962+
}
2001519963
$this->textvar = ($this->textvar & ~TextVars::FT_UPPERCASE); // mPDF 5.7.1
2001619964
$this->textvar = ($this->textvar & ~TextVars::FT_LOWERCASE); // mPDF 5.7.1
2001719965
$this->textvar = ($this->textvar & ~TextVars::FT_CAPITALIZE); // mPDF 5.7.1
19966+
break;
2001819967
}
2001919968
break;
2002019969

0 commit comments

Comments
 (0)