Skip to content

Commit 84bf47e

Browse files
authored
Merge pull request #679 from GravityPDF/fix/640-important-page-rules
Let an important @page declaration beat a plain one in a :first, :left, :right or named page rule
2 parents 971bd4d + 69592cb commit 84bf47e

12 files changed

Lines changed: 420 additions & 112 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Bugfixes
199199
* A descendant rule with a part mPDF cannot parse was cut short at that part and applied to the element before it. Such rules are now dropped. [#519]
200200
* An inline `style` with `!important` drew no border for a `border` shorthand, dropped the bottom padding of a two-value `padding`, drew text at 0pt for a `font-size`, and ignored an image's `height` with a warning. The flag is now removed, as it is from a stylesheet. [mpdf/mpdf#1010] [mpdf/mpdf#1707] [#523] [#561]
201201
* A declaration whose flag had a space after the bang, such as `margin: 5mm ! important`, kept the flag in its value: that margin set the right and left margins to `!` and the bottom one to `important`, a `font-size` was lost, and a `border` drew nothing. The flag is now found whatever its spacing and case, in stylesheets and inline styles, in either `cssMode`. [#532] [#638]
202+
* With `cssMode` standard, an `!important` declaration in a plain `@page` rule lost to a plain one in a `@page :first`, `:left`, `:right` or named page rule, so `@page { margin-top: 30mm !important } @page :first { margin-top: 10mm }` gave the first page a 10mm top margin. The important declaration now wins, as a named page's does over its own `:first`, `:left` and `:right` pages. Of two important declarations, the one in the rule applied later wins, as before, and the default stylesheet's (`defaultCssFile`) beats the document's. A plain `@page` side margin is still mirrored, so on a left page an important `margin-left` is the right margin, and beats the `margin-right` of a `:left` rule. Legacy mode is unchanged. [#532] [#640] [#679]
202203
* Text styled with the `font` shorthand or `<font face>` came out as empty boxes when the family was not installed or had a space in its name, such as `font: 12pt Roboto` or `font: 16px "DejaVu Sans Mono"`. It was drawn in the first registered font, which is the emoji font in a full install. The family is now read as `font-family` reads it: a name is kept whole, the list is tried in order, and when mPDF knows none of it the text keeps the family it inherits. `font-family` itself also reads an unquoted name with spaces whole. [#552] [#560]
203204
* A descendant rule ending in `:lang()` or `[lang]`, such as `div :lang(fr)`, `div p:lang(fr)` or `table [lang=fr]`, never applied. It now applies in block content and in tables, and a regional language such as `fr-CA` falls back to the rule for `fr`, as the simple lang rules do. [#525] [#559]
204205
* A rule such as `td:nth-child(2):not(.x)` or `td:nth-child(2 of .x)` was applied to the second cell of every row, as if it were `td:nth-child(2)`. A rule with anything after an nth-child argument, or with an argument that is not a formula, is now dropped. [mpdf/mpdf#83] [#522] [#564]
@@ -944,3 +945,5 @@ These changes do not change output.
944945
[#678]: https://github.com/GravityPDF/mpdf/pull/678
945946
[#547]: https://github.com/GravityPDF/mpdf/issues/547
946947
[#683]: https://github.com/GravityPDF/mpdf/pull/683
948+
[#640]: https://github.com/GravityPDF/mpdf/issues/640
949+
[#679]: https://github.com/GravityPDF/mpdf/pull/679

‎src/CssManager.php‎

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ public function readDefaultCss($css)
157157
* Parse the CSS in HTML content into the stores readCss() describes, and file its compiled rules in a rule set
158158
*
159159
* The !important declarations of the rules stored by key are kept apart as well, and laid again over $CSS after
160-
* each read, those of the default stylesheet last, so that what reads $CSS directly (@page, BODY, SVG's classes)
161-
* finds them over any declaration that is not important.
160+
* each read, those of the default stylesheet last, so that what reads one key of $CSS directly, such as SVG's
161+
* classes, finds them over any declaration that is not important. What merges several keys lays the stores over
162+
* the merged rules itself: pageProperties() for the @page rules, the merger for BODY.
162163
*
163164
* @param string $html
164165
* @param RuleSet $rules
@@ -381,14 +382,9 @@ public function getBorderDominance($side)
381382
*/
382383
public function pseudoPagesSetSideMargins($name)
383384
{
384-
$prefixes = ['@PAGE>>'];
385-
if ($name) {
386-
$prefixes[] = '@PAGE>>NAMED>>' . strtoupper($name) . '>>';
387-
}
388-
389-
foreach ($prefixes as $prefix) {
385+
foreach ($this->pageRuleKeys($name) as $key) {
390386
foreach (['FIRST', 'LEFT', 'RIGHT'] as $pseudo) {
391-
$rule = $prefix . 'PSEUDO>>' . $pseudo;
387+
$rule = $key . '>>PSEUDO>>' . $pseudo;
392388
if (isset($this->CSS[$rule]['MARGIN-LEFT']) || isset($this->CSS[$rule]['MARGIN-RIGHT'])) {
393389
return true;
394390
}
@@ -398,6 +394,64 @@ public function pseudoPagesSetSideMargins($name)
398394
return false;
399395
}
400396

397+
/**
398+
* The properties the @page rules set on a page: the plain rule's, then the named page's, each followed by those
399+
* of its :right, :left and :first pages that apply
400+
*
401+
* The rules are merged one over the other whatever their importance, so the !important declarations of each are
402+
* then laid over them in the same order, and the default stylesheet's last. Legacy mode reads a declaration marked
403+
* !important as any other, so there those stores are empty.
404+
*
405+
* A rule's odd-header-name or even-header-name, whichever the page's parity takes, becomes its header before the
406+
* next rule is merged, so that a later rule's header beats it, and the footer names likewise.
407+
*
408+
* @param string $name The named page, or '' for none
409+
* @param string $side 'R' or 'L', or '' for the defaults of every page
410+
* @param bool $first Whether this is the first page
411+
* @param string $oddEven 'E' for an even page when margins are mirrored, 'O' for any other page, or '' for the
412+
* defaults of every page
413+
* @return array
414+
*/
415+
public function pageProperties($name, $side, $first, $oddEven)
416+
{
417+
$parity = $oddEven == 'E' ? 'EVEN-' : 'ODD-';
418+
$properties = [];
419+
foreach ([$this->CSS, $this->importantCss, $this->defaultImportantCss] as $css) {
420+
foreach ($this->pageRuleKeys($name) as $key) {
421+
if (!empty($css[$key])) {
422+
$properties = array_merge($properties, $css[$key]);
423+
}
424+
425+
foreach (['HEADER', 'FOOTER'] as $part) {
426+
if (isset($properties[$parity . $part . '-NAME'])) {
427+
$properties[$part] = $properties[$parity . $part . '-NAME'];
428+
unset($properties[$parity . $part . '-NAME']);
429+
}
430+
}
431+
432+
$properties = array_merge($properties, $this->pseudoPagePropertiesIn($css, $key . '>>', $side, $first, $oddEven));
433+
}
434+
}
435+
436+
return $properties;
437+
}
438+
439+
/**
440+
* The keys of the plain @page rule and, when a page is named, of the named page's rule
441+
*
442+
* @param string|null $name The named page, or '' or null for none
443+
* @return string[]
444+
*/
445+
private function pageRuleKeys($name)
446+
{
447+
$keys = ['@PAGE'];
448+
if ($name) {
449+
$keys[] = '@PAGE>>NAMED>>' . strtoupper($name);
450+
}
451+
452+
return $keys;
453+
}
454+
401455
/**
402456
* Whether the style sheet has a plain @page rule, or a :first, :left or :right one, that sets anything
403457
*
@@ -428,14 +482,29 @@ public function hasUnnamedPageRules()
428482
* @return array
429483
*/
430484
public function pseudoPageProperties($prefix, $side, $first, $oddEven)
485+
{
486+
return $this->pseudoPagePropertiesIn($this->CSS, $prefix, $side, $first, $oddEven);
487+
}
488+
489+
/**
490+
* The properties pseudoPageProperties() reads, from the rules given rather than $CSS
491+
*
492+
* @param array[] $css Rules under the keys of $CSS, such as those of getImportantCss()
493+
* @param string $prefix
494+
* @param string $side
495+
* @param bool $first
496+
* @param string $oddEven
497+
* @return array
498+
*/
499+
private function pseudoPagePropertiesIn(array $css, $prefix, $side, $first, $oddEven)
431500
{
432501
$properties = [];
433502
foreach (['RIGHT' => $side == 'R', 'LEFT' => $side == 'L', 'FIRST' => $first] as $pseudo => $applies) {
434-
if (!$applies || !isset($this->CSS[$prefix . 'PSEUDO>>' . $pseudo])) {
503+
if (!$applies || !isset($css[$prefix . 'PSEUDO>>' . $pseudo])) {
435504
continue;
436505
}
437506

438-
$rule = $this->CSS[$prefix . 'PSEUDO>>' . $pseudo];
507+
$rule = $css[$prefix . 'PSEUDO>>' . $pseudo];
439508
unset($rule['SIZE'], $rule['SHEET-SIZE']);
440509

441510
if ($oddEven === 'E') {

‎src/Mpdf.php‎

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16333,65 +16333,7 @@ function SetPagedMediaCSS($name, $first, $oddEven)
1633316333
$p['MARGIN-HEADER'] = strval($this->orig_hMargin - $outerTB) . 'mm';
1633416334
$p['MARGIN-FOOTER'] = strval($this->orig_fMargin - $outerTB) . 'mm';
1633516335

16336-
// Basic page + selector
16337-
if (isset($this->cssManager->CSS['@PAGE'])) {
16338-
$zp = $this->cssManager->CSS['@PAGE'];
16339-
} else {
16340-
$zp = [];
16341-
}
16342-
if (is_array($zp) && !empty($zp)) {
16343-
$p = array_merge($p, $zp);
16344-
}
16345-
16346-
if (isset($p['EVEN-HEADER-NAME']) && $oddEven == 'E') {
16347-
$p['HEADER'] = $p['EVEN-HEADER-NAME'];
16348-
unset($p['EVEN-HEADER-NAME']);
16349-
}
16350-
if (isset($p['ODD-HEADER-NAME']) && $oddEven != 'E') {
16351-
$p['HEADER'] = $p['ODD-HEADER-NAME'];
16352-
unset($p['ODD-HEADER-NAME']);
16353-
}
16354-
if (isset($p['EVEN-FOOTER-NAME']) && $oddEven == 'E') {
16355-
$p['FOOTER'] = $p['EVEN-FOOTER-NAME'];
16356-
unset($p['EVEN-FOOTER-NAME']);
16357-
}
16358-
if (isset($p['ODD-FOOTER-NAME']) && $oddEven != 'E') {
16359-
$p['FOOTER'] = $p['ODD-FOOTER-NAME'];
16360-
unset($p['ODD-FOOTER-NAME']);
16361-
}
16362-
16363-
$p = array_merge($p, $this->cssManager->pseudoPageProperties('@PAGE>>', $side, $first, $oddEven));
16364-
16365-
// If named page
16366-
if ($name) {
16367-
if (isset($this->cssManager->CSS['@PAGE>>NAMED>>' . $name])) {
16368-
$zp = $this->cssManager->CSS['@PAGE>>NAMED>>' . $name];
16369-
} else {
16370-
$zp = [];
16371-
}
16372-
if (is_array($zp) && !empty($zp)) {
16373-
$p = array_merge($p, $zp);
16374-
}
16375-
16376-
if (isset($p['EVEN-HEADER-NAME']) && $oddEven == 'E') {
16377-
$p['HEADER'] = $p['EVEN-HEADER-NAME'];
16378-
unset($p['EVEN-HEADER-NAME']);
16379-
}
16380-
if (isset($p['ODD-HEADER-NAME']) && $oddEven != 'E') {
16381-
$p['HEADER'] = $p['ODD-HEADER-NAME'];
16382-
unset($p['ODD-HEADER-NAME']);
16383-
}
16384-
if (isset($p['EVEN-FOOTER-NAME']) && $oddEven == 'E') {
16385-
$p['FOOTER'] = $p['EVEN-FOOTER-NAME'];
16386-
unset($p['EVEN-FOOTER-NAME']);
16387-
}
16388-
if (isset($p['ODD-FOOTER-NAME']) && $oddEven != 'E') {
16389-
$p['FOOTER'] = $p['ODD-FOOTER-NAME'];
16390-
unset($p['ODD-FOOTER-NAME']);
16391-
}
16392-
16393-
$p = array_merge($p, $this->cssManager->pseudoPageProperties('@PAGE>>NAMED>>' . $name . '>>', $side, $first, $oddEven));
16394-
}
16336+
$p = array_merge($p, $this->cssManager->pageProperties($name, $side, $first, $oddEven));
1639516337

1639616338
$orientation = $mgl = $mgr = $mgt = $mgb = $mgh = $mgf = '';
1639716339
$header = $footer = '';

‎tests/Mpdf/DrawnStyles.php‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Mpdf;
44

55
/**
6-
* Writes a document through TextRecordingMpdf, to read the colour and style each piece of text is drawn in, or
7-
* matches a selector against the elements a document leaves open
6+
* Writes a document through TextRecordingMpdf, to read the colour and style each piece of text is drawn in, or where
7+
* on the page it lies, or matches a selector against the elements a document leaves open
88
*/
99
trait DrawnStyles
1010
{
@@ -210,4 +210,55 @@ private function matchesLastOpenElement($html, $selector)
210210

211211
return $matcher->matches($compiler->compile($selector), $mpdf->getOpenElements());
212212
}
213+
214+
/**
215+
* Justified paragraphs of text
216+
*
217+
* @param int $paragraphs
218+
*
219+
* @return string
220+
*/
221+
private function story($paragraphs)
222+
{
223+
return str_repeat('<p>' . str_repeat('Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', 8) . '</p>', $paragraphs);
224+
}
225+
226+
/**
227+
* The narrowest left edge and widest right edge of the boxes under each key, rounded to a tenth of a millimetre
228+
*
229+
* @param array[] $boxes Each a key, such as the page, then a left and right edge, as TextRecordingMpdf records them
230+
*
231+
* @return array[] By key, in the order the keys first appear
232+
*/
233+
private function spans(array $boxes)
234+
{
235+
$spans = [];
236+
foreach ($boxes as $box) {
237+
list($key, $left, $right) = $box;
238+
$spans[$key] = isset($spans[$key]) ? [min($spans[$key][0], $left), max($spans[$key][1], $right)] : [$left, $right];
239+
}
240+
241+
return array_map(static function ($span) {
242+
return [round($span[0], 1), round($span[1], 1)];
243+
}, $spans);
244+
}
245+
246+
/**
247+
* The top of the first line drawn on each page, rounded to a tenth of a millimetre
248+
*
249+
* @param array[] $boxes As TextRecordingMpdf records them
250+
*
251+
* @return float[] By page number, in the order the pages were drawn
252+
*/
253+
private function firstLineTops(array $boxes)
254+
{
255+
$tops = [];
256+
foreach ($boxes as $box) {
257+
if (!isset($tops[$box[0]])) {
258+
$tops[$box[0]] = round($box[3], 1);
259+
}
260+
}
261+
262+
return $tops;
263+
}
213264
}

0 commit comments

Comments
 (0)