Skip to content

Commit b7ab8be

Browse files
jakejackson1claude
andauthored
Draw small-caps in the font shorthand as small capitals and reset what it does not name (#581)
`font: small-caps 14pt serif` set `text-transform: uppercase`, so the text came out in full-size capitals. It now sets `font-variant-caps: small-caps`, which mPDF draws with the font's smcp feature or synthesises. The shorthand also left an inherited `line-height` and `font-variant` in place. As in CSS, it now resets every part it does not name: the line height, style, weight and each `font-variant` longhand go back to `normal`. A negative line height makes the whole value invalid, so the declaration is dropped. A system font keyword such as `caption` was already dropped, and a test now covers it. `font-variant: normal` resets `font-variant-position` with the other longhands, which the shorthand relies on. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent edfcf9b commit b7ab8be

8 files changed

Lines changed: 259 additions & 7 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Bugfixes
154154
* A channel outside its range wrapped instead of being clamped: `rgb(255.5, 0, 0)` drew black and `rgb(300, 0, 0)` dark red. Channels, alphas, and `hsl()` saturation and lightness are now clamped, as CSS does.
155155
* `rebeccapurple` was missing. mPDF's `violetred`, which is not a CSS colour, is kept.
156156
* A `list-style-image` whose URL had capitals, such as `url(img/List-Bullet.png)`, drew no marker on a case-sensitive disk or server, because the URL was lowercased before the image was fetched. The URL now keeps its case. [#552] [#623]
157+
* `font: small-caps 14pt serif` drew the text in full-size capitals, as `text-transform: uppercase` does. It is now drawn in small capitals, as `font-variant: small-caps` draws it. The `font` shorthand also kept a `line-height` and a `font-variant` it inherited; like the style and weight, they are now reset to `normal` when the shorthand does not name them, as in a browser. To keep a line height, give it in the shorthand, as in `font: 14pt/1.5 serif`, or declare `line-height` after it. A `font` shorthand with a negative line height, such as `font: 12pt/-1 serif`, is dropped whole. `font-variant: normal` now resets `font-variant-position` as well. [#552] [#581]
157158
* A percentage `width`, `min-width` or `max-width` on an image in a table cell was resolved against the block around the table, not the cell. In a 60mm cell, `max-width: 20%` came out as 36mm. [#223] [#505]
158159
* An `@page :left` or `@page :right` rule was ignored unless the style sheet also had a plain `@page` rule. With one, the margins of a `:right` rule were applied to left pages too. [#555] [#556]
159160
* When a later `WriteHTML()` call turned the document right to left, the text on the page already started was set between the swapped side margins. That page now keeps its margins, and the swap starts with the next page. [#554] [#558]
@@ -779,6 +780,7 @@ These changes do not change output.
779780
[#570]: https://github.com/GravityPDF/mpdf/pull/570
780781
[#578]: https://github.com/GravityPDF/mpdf/pull/578
781782
[#580]: https://github.com/GravityPDF/mpdf/pull/580
783+
[#581]: https://github.com/GravityPDF/mpdf/pull/581
782784
[#615]: https://github.com/GravityPDF/mpdf/pull/615
783785
[#622]: https://github.com/GravityPDF/mpdf/pull/622
784786
[#623]: https://github.com/GravityPDF/mpdf/pull/623

‎src/Css/NormalizeProperties.php‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ public function canParse($property, $value)
237237
* Process FONT shorthand property.
238238
*
239239
* Expands the CSS font shorthand into individual components:
240-
* font-family, font-size, line-height, font-style, font-weight, text-transform.
240+
* font-family, font-size, line-height, font-style, font-weight and the font-variant longhands.
241+
* A component the value does not name is reset to normal. A value with no size and family,
242+
* such as a system font keyword (caption, menu), is dropped.
241243
*
242244
* @param string $value Font property value
243245
* @return void
@@ -253,12 +255,15 @@ protected function processFontProperty($value)
253255

254256
list(, $keywords, $size, $lineHeight, $family) = $m;
255257

258+
// A negative line-height makes the whole value invalid
259+
if ((float) $lineHeight < 0) {
260+
return;
261+
}
262+
256263
$this->processFontFamilyProperty('FONT-FAMILY', $family);
257264

258265
$this->properties['FONT-SIZE'] = $size;
259-
if ($lineHeight !== '') {
260-
$this->properties['LINE-HEIGHT'] = $lineHeight;
261-
}
266+
$this->properties['LINE-HEIGHT'] = $lineHeight !== '' ? $lineHeight : 'normal';
262267

263268
// Check for font-style
264269
if (preg_match('/(italic|oblique)/i', $keywords)) {
@@ -274,9 +279,10 @@ protected function processFontProperty($value)
274279
$this->properties['FONT-WEIGHT'] = 'normal';
275280
}
276281

277-
// Check for small-caps
282+
// Check for small-caps, the only font-variant value the shorthand can name
283+
$this->processFontVariantProperty('normal');
278284
if (stripos($keywords, 'small-caps') !== false) {
279-
$this->properties['TEXT-TRANSFORM'] = 'uppercase';
285+
$this->properties['FONT-VARIANT-CAPS'] = 'small-caps';
280286
}
281287
}
282288

@@ -293,6 +299,7 @@ protected function processFontVariantProperty($value)
293299
$this->properties['FONT-VARIANT-CAPS'] = $m[1];
294300
$this->properties['FONT-VARIANT-NUMERIC'] = $m[1];
295301
$this->properties['FONT-VARIANT-ALTERNATES'] = $m[1];
302+
$this->properties['FONT-VARIANT-POSITION'] = 'normal';
296303

297304
return;
298305
}

‎tests/Mpdf/Css/NormalizePropertiesTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,22 @@ public function providerFontShorthand()
307307
'spaces around the slash' => ['12pt / 2 serif', ['FONT-FAMILY' => 'serif', 'FONT-SIZE' => '12pt', 'LINE-HEIGHT' => '2']],
308308
'numeric weight before the size' => ['italic 700 12pt serif', ['FONT-FAMILY' => 'serif', 'FONT-SIZE' => '12pt', 'FONT-STYLE' => 'italic']],
309309
'keyword inside a family name' => ['12pt "Bold Italic Sans", serif', ['FONT-FAMILY' => 'serif', 'FONT-STYLE' => 'normal', 'FONT-WEIGHT' => 'normal']],
310+
'small-caps' => ['small-caps 12pt serif', ['FONT-VARIANT-CAPS' => 'small-caps', 'TEXT-TRANSFORM' => null]],
311+
'parts not named are reset' => [
312+
'12pt serif',
313+
[
314+
'LINE-HEIGHT' => 'normal',
315+
'FONT-STYLE' => 'normal',
316+
'FONT-WEIGHT' => 'normal',
317+
'FONT-VARIANT-CAPS' => 'normal',
318+
'FONT-VARIANT-LIGATURES' => 'normal',
319+
'FONT-VARIANT-NUMERIC' => 'normal',
320+
'FONT-VARIANT-ALTERNATES' => 'normal',
321+
'FONT-VARIANT-POSITION' => 'normal',
322+
],
323+
],
324+
'negative line-height' => ['12pt/-1 serif', ['FONT-FAMILY' => null, 'FONT-SIZE' => null, 'LINE-HEIGHT' => null, 'FONT-STYLE' => null]],
325+
'system font' => ['caption', ['FONT-FAMILY' => null, 'FONT-SIZE' => null, 'LINE-HEIGHT' => null, 'FONT-STYLE' => null]],
310326
];
311327
}
312328

@@ -504,4 +520,13 @@ public function testNormalizeDropsThePlusSignOfALength()
504520
$this->assertSame('1.5', $result['LINE-HEIGHT']);
505521
}
506522

523+
/**
524+
* font-variant: normal resets font-variant-position with the other longhands
525+
*/
526+
public function testFontVariantNormalResetsPosition()
527+
{
528+
$result = $this->normalizeProperties->normalize(['FONT-VARIANT' => 'normal']);
529+
$this->assertSame('normal', $result['FONT-VARIANT-POSITION']);
530+
}
531+
507532
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
use Mpdf\Css\TextVars;
6+
7+
/**
8+
* The font shorthand draws small-caps as small capitals, resets the line-height, style, weight and variant it does not
9+
* name, and is dropped when it names a system font.
10+
*/
11+
class FontShorthandResetsTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
12+
{
13+
14+
/**
15+
* small-caps in the shorthand draws small capitals, not the text in full-size capitals
16+
*/
17+
public function testSmallCapsDrawsSmallCapitals()
18+
{
19+
$mpdf = $this->render('<style>p { font: small-caps 14pt dejavuserif }</style><p>qq</p>');
20+
21+
$this->assertSame(['qq'], $mpdf->drawnText);
22+
$this->assertSame(TextVars::FC_SMALLCAPS, $mpdf->drawnTextvar[0] & (TextVars::FC_SMALLCAPS | TextVars::FT_UPPERCASE));
23+
}
24+
25+
/**
26+
* A shorthand with no line-height gives lines their normal height, not the one inherited
27+
*/
28+
public function testLineHeightIsResetToNormal()
29+
{
30+
$normal = $this->render('<style>p { font: 14pt dejavuserif }</style><p>qq</p>');
31+
$reset = $this->render('<style>div { line-height: 3 } p { font: 14pt dejavuserif }</style><div><p>qq</p></div>');
32+
33+
$this->assertSame($normal->drawnLineHeight, $reset->drawnLineHeight);
34+
$this->assertLessThan(3 * 14 * 25.4 / 72, $reset->drawnLineHeight[0]);
35+
}
36+
37+
/**
38+
* A line-height given in the shorthand replaces the one inherited
39+
*/
40+
public function testLineHeightGivenIsKept()
41+
{
42+
$mpdf = $this->render('<style>div { line-height: 3 } p { font: 14pt/2 dejavuserif }</style><div><p>qq</p></div>');
43+
44+
$this->assertEqualsWithDelta(28 * 25.4 / 72, $mpdf->drawnLineHeight[0], 0.001);
45+
}
46+
47+
/**
48+
* @dataProvider providerStyleWeightAndVariant
49+
*
50+
* @param string $html A document whose shorthand names no style, weight or variant, inside text that has all three
51+
*/
52+
public function testStyleWeightAndVariantAreResetToNormal($html)
53+
{
54+
$mpdf = $this->render($html);
55+
56+
$this->assertSame(['qq'], $mpdf->drawnText);
57+
$this->assertSame([''], $mpdf->drawnFontStyle);
58+
$this->assertSame(0, $mpdf->drawnTextvar[0] & TextVars::FC_SMALLCAPS);
59+
}
60+
61+
/**
62+
* @return array[] Documents whose shorthand should reset the style, weight and variant it inherits
63+
*/
64+
public function providerStyleWeightAndVariant()
65+
{
66+
return [
67+
'block' => ['<style>div { font-style: italic; font-weight: bold; font-variant: small-caps } p { font: 14pt dejavuserif }</style><div><p>qq</p></div>'],
68+
'inline' => ['<style>p { font-style: italic; font-weight: bold; font-variant: small-caps } span { font: 14pt dejavuserif }</style><p><span>qq</span></p>'],
69+
];
70+
}
71+
72+
/**
73+
* A system font keyword, which mPDF has no font for, drops the declaration and leaves what the earlier rule set
74+
*/
75+
public function testSystemFontIsDropped()
76+
{
77+
$mpdf = $this->render('<style>p { font: italic 18pt dejavuserif } p { font: caption }</style><p>qq</p>');
78+
79+
$this->assertSame(['dejavuserif'], $mpdf->drawnFontFamily);
80+
$this->assertSame([18.0], array_map('floatval', $mpdf->drawnFontSize));
81+
$this->assertSame(['I'], $mpdf->drawnFontStyle);
82+
}
83+
84+
/**
85+
* @param string $html The document
86+
*
87+
* @return FontStateRecordingMpdf The document written, with the state each line was drawn with
88+
*/
89+
private function render($html)
90+
{
91+
$mpdf = new FontStateRecordingMpdf();
92+
$mpdf->WriteHTML($html);
93+
94+
return $mpdf;
95+
}
96+
97+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* Records, beside the text TextRecordingMpdf records, the style, text variants and line height each line is drawn with.
7+
*/
8+
class FontStateRecordingMpdf extends TextRecordingMpdf
9+
{
10+
11+
/** The font style ('', 'B', 'I' or 'BI') each line was drawn in, in the same order. */
12+
public $drawnFontStyle = [];
13+
14+
/** The TextVars flags each line was drawn with, in the same order. */
15+
public $drawnTextvar = [];
16+
17+
/** The height of each line, in mm, in the same order. */
18+
public $drawnLineHeight = [];
19+
20+
/**
21+
* Records the state a line is drawn with before drawing it
22+
*
23+
* @see TextRecordingMpdf::Cell() for why the signature is restated in full
24+
*/
25+
function Cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '', $currentx = 0, $lcpaddingL = 0, $lcpaddingR = 0, $valign = 'M', $spanfill = 0, $exactWidth = false, $OTLdata = false, $textvar = 0, $lineBox = false)
26+
{
27+
if (is_string($txt) && trim($txt) !== '') {
28+
$this->drawnFontStyle[] = $this->FontStyle;
29+
$this->drawnTextvar[] = $textvar;
30+
$this->drawnLineHeight[] = $h;
31+
}
32+
33+
return parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link, $currentx, $lcpaddingL, $lcpaddingR, $valign, $spanfill, $exactWidth, $OTLdata, $textvar, $lineBox);
34+
}
35+
36+
}

‎tests/Mpdf/LineHeightZeroTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function providerNegative()
112112
],
113113
'font shorthand' => [
114114
'<div style="line-height: 2"><p style="font: 10pt/-1 serif">' . $lines . '</p></div>',
115-
'<div style="line-height: 2"><p style="font: 10pt serif">' . $lines . '</p></div>',
115+
'<div style="line-height: 2"><p>' . $lines . '</p></div>',
116116
],
117117
'table cell' => [
118118
'<table><tr><td style="line-height: -1">' . $lines . '</td></tr></table>',
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* The font shorthand drawing small-caps as small capitals, resetting the line-height, style, weight and variant it does
7+
* not name, and being dropped when it names a system font.
8+
*
9+
* @group snapshot
10+
*/
11+
class FontShorthandResetsSnapshotTest extends Snapshot
12+
{
13+
14+
/**
15+
* The fixture's name
16+
*
17+
* @return string
18+
*/
19+
public function getId()
20+
{
21+
return 'font-shorthand-resets';
22+
}
23+
24+
/**
25+
* Boxes each styled with the font shorthand, under a caption saying how the text should look
26+
*/
27+
public function generatePdf()
28+
{
29+
ob_start();
30+
?>
31+
<style>
32+
body { font-size: 9pt; }
33+
h1 { font-size: 15pt; margin: 0 0 2mm 0; }
34+
p.caption { margin: 4mm 0 1mm 0; font-size: 8pt; color: #606060; }
35+
div.case { border: 0.2mm solid #999999; padding: 1mm 2mm; font-family: dejavuserif; }
36+
div.case p { margin: 1mm 0; }
37+
38+
p.small-caps { font: small-caps 14pt dejavuserif; }
39+
40+
div.tall { line-height: 3; }
41+
p.shorthand { font: 12pt dejavuserif; }
42+
p.size-only { font-size: 12pt; }
43+
p.given { font: 12pt/2 dejavuserif; }
44+
45+
div.styled { font-style: italic; font-weight: bold; font-variant: small-caps; }
46+
span.shorthand { font: 12pt dejavuserif; }
47+
48+
p.system { font: italic 14pt dejavuserif; }
49+
p.system { font: caption; }
50+
</style>
51+
52+
<h1>What the font shorthand sets</h1>
53+
54+
<p class="caption">small-caps: the lowercase letters drawn as small capitals, the first letter of each word a full-size capital.</p>
55+
<div class="case"><p class="small-caps">Small capitals, not full capitals</p></div>
56+
57+
<p class="caption">The box sets line-height: 3. The shorthand names no line height, so these lines are closely spaced.</p>
58+
<div class="case tall"><p class="shorthand">This paragraph is long enough to wrap onto more than one line, which shows how far apart the lines are drawn once the shorthand has reset the line height.</p></div>
59+
60+
<p class="caption">For comparison, the same box with font-size: 12pt in place of the shorthand: widely spaced lines.</p>
61+
<div class="case tall"><p class="size-only">This paragraph is long enough to wrap onto more than one line, which shows how far apart the lines are drawn with the line height the box sets.</p></div>
62+
63+
<p class="caption">The box sets line-height: 3. The shorthand gives 12pt/2, so these lines are twice the font size apart.</p>
64+
<div class="case tall"><p class="given">This paragraph is long enough to wrap onto more than one line, which shows how far apart the lines are drawn with the line height the shorthand gives.</p></div>
65+
66+
<p class="caption">The box sets italic, bold and small-caps. The first line keeps them; the second, set with the shorthand, is upright, regular-weight lowercase.</p>
67+
<div class="case styled">
68+
<p>Italic bold small caps from the box</p>
69+
<p class="shorthand">upright regular lowercase from the shorthand</p>
70+
</div>
71+
72+
<p class="caption">The same on an inline element: only the middle words are upright, regular-weight lowercase.</p>
73+
<div class="case styled"><p>Italic bold small caps, <span class="shorthand">upright regular lowercase,</span> italic bold small caps</p></div>
74+
75+
<p class="caption">font: caption names a system font, which mPDF has none of, so it is dropped: italic 14pt serif from the rule before it.</p>
76+
<div class="case"><p class="system">Italic 14pt serif</p></div>
77+
<?php
78+
$html = ob_get_clean();
79+
80+
$this->mpdf = $this->createMpdf();
81+
82+
$this->mpdf->WriteHTML($html);
83+
}
84+
85+
}
86.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)