Skip to content

Commit fc76a7f

Browse files
jakejackson1claude
andcommitted
Draw small-caps in the font shorthand as small capitals and reset what it does not name
`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 3b02ff4 commit fc76a7f

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
@@ -125,6 +125,7 @@ Bugfixes
125125
* 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]
126126
* `line-height: 0` gave lines a normal height. Lines now have no height, as in a browser. A line height well below the font size, such as `0.5` or `1mm`, was stretched down to the baseline; it is now kept. A negative `line-height`, which shrank lines to odd heights, is now ignored. [#552] [#567]
127127
* 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] [#571]
128+
* `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]
128129
* 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]
129130
* Balancing columns raised a warning and drew a block's closing background past the last column. [#475] [#488]
130131
* Inside `<columns>`, a block with an `rgba()` or `cmyka()` background was painted fully opaque. [#482] [#498]
@@ -704,3 +705,4 @@ These changes do not change output.
704705
[#560]: https://github.com/GravityPDF/mpdf/pull/560
705706
[#567]: https://github.com/GravityPDF/mpdf/pull/567
706707
[#571]: https://github.com/GravityPDF/mpdf/pull/571
708+
[#581]: https://github.com/GravityPDF/mpdf/pull/581

‎src/Css/NormalizeProperties.php‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public function normalize($prop)
122122
* Process FONT shorthand property.
123123
*
124124
* Expands the CSS font shorthand into individual components:
125-
* font-family, font-size, line-height, font-style, font-weight, text-transform.
125+
* font-family, font-size, line-height, font-style, font-weight and the font-variant longhands.
126+
* A component the value does not name is reset to normal. A value with no size and family,
127+
* such as a system font keyword (caption, menu), is dropped.
126128
*
127129
* @param string $value Font property value
128130
* @return void
@@ -138,12 +140,15 @@ protected function processFontProperty($value)
138140

139141
list(, $keywords, $size, $lineHeight, $family) = $m;
140142

143+
// A negative line-height makes the whole value invalid
144+
if ((float) $lineHeight < 0) {
145+
return;
146+
}
147+
141148
$this->processFontFamilyProperty('FONT-FAMILY', $family);
142149

143150
$this->properties['FONT-SIZE'] = $size;
144-
if ($lineHeight !== '') {
145-
$this->properties['LINE-HEIGHT'] = $lineHeight;
146-
}
151+
$this->properties['LINE-HEIGHT'] = $lineHeight !== '' ? $lineHeight : 'normal';
147152

148153
// Check for font-style
149154
if (preg_match('/(italic|oblique)/i', $keywords)) {
@@ -159,9 +164,10 @@ protected function processFontProperty($value)
159164
$this->properties['FONT-WEIGHT'] = 'normal';
160165
}
161166

162-
// Check for small-caps
167+
// Check for small-caps, the only font-variant value the shorthand can name
168+
$this->processFontVariantProperty('normal');
163169
if (stripos($keywords, 'small-caps') !== false) {
164-
$this->properties['TEXT-TRANSFORM'] = 'uppercase';
170+
$this->properties['FONT-VARIANT-CAPS'] = 'small-caps';
165171
}
166172
}
167173

@@ -178,6 +184,7 @@ protected function processFontVariantProperty($value)
178184
$this->properties['FONT-VARIANT-CAPS'] = $m[1];
179185
$this->properties['FONT-VARIANT-NUMERIC'] = $m[1];
180186
$this->properties['FONT-VARIANT-ALTERNATES'] = $m[1];
187+
$this->properties['FONT-VARIANT-POSITION'] = 'normal';
181188

182189
return;
183190
}

‎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

@@ -319,4 +335,13 @@ public function testUnquotedFamilyNameWithSpaces()
319335
$this->assertSame('dejavusansmono', $result['FONT-FAMILY']);
320336
}
321337

338+
/**
339+
* font-variant: normal resets font-variant-position with the other longhands
340+
*/
341+
public function testFontVariantNormalResetsPosition()
342+
{
343+
$result = $this->normalizeProperties->normalize(['FONT-VARIANT' => 'normal']);
344+
$this->assertSame('normal', $result['FONT-VARIANT-POSITION']);
345+
}
346+
322347
}
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)