Skip to content

Commit 0026354

Browse files
jakejackson1claude
andauthored
Compute font-weight numbers, bolder and lighter, and font-size larger and smaller from the parent's values (#647)
* Compute font-weight numbers, bolder and lighter, and font-size larger and smaller from the parent's values In the standard CSS mode, font-weight takes a number from 1 to 1000, and bolder and lighter step from the parent's weight by the table in CSS Fonts. The weight is kept as a number and carried through each channel a value is inherited through, so bolder inside bolder gives 900 and lighter inside 700 gives 400. A weight above 500 is drawn with the bold face. larger and smaller multiply and divide the parent's size by 1.2. The font shorthand keeps the weight it names. The legacy mode reads only normal and bold, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Ignore font-size larger and smaller on form fields in legacy mode rather than drawing them at 0pt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Caption the legacy report with what cssMode legacy draws since #633 #633 drops a descendant rule through an inline ancestor under legacy, so .lead em b no longer applies there and "steady" keeps the bold of b. The legacy fixture draws it bold; the caption says so, and the standard fixture changes only in that caption and the line it adds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Read larger and smaller on html and body against their parents #642 reads html's and body's font sizes through SizeConverter::convertFontSize(), which gives points and takes the size medium stands for. It now reads larger and smaller too, in the standard CSS mode. The form fields and tables keep the millimetre conversion under the name convertFontSizeToMm(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep the font-weight helpers out of Mpdf, and read a text circle's larger and smaller as a form field's The parent's weight, the table's weight and the weight that agrees with the B style move to RelativeFontValues. The weight is now squared with the B style where it is read rather than in every style setter, so SetStyle() and its siblings are as before. Form fields and text circles set their font size through one Tag helper, which gives the text circle the legacy fix the form fields had: larger and smaller are ignored rather than drawn at 0pt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5be2e92 commit 0026354

25 files changed

Lines changed: 1030 additions & 23 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ New features
130130
* **The universal selector.** `*` matches every element: alone, with a class, an id, an attribute or a pseudo-class, on either side of a combinator, and inside `:is()`, `:where()` and `:not()`. It weighs nothing, so any rule naming a tag, class, id, attribute or pseudo-class beats it. It reaches `html` and `body` too, so `* { color: … }` colours text written straight into the body, and `* { font-size: … }` sets the size `rem` is read against. Rules using it were dropped before, so a document that has them changes. Set `'cssMode' => \Mpdf\CssMode::LEGACY` to keep dropping them. [mpdf/mpdf#312] [mpdf/mpdf#1168] [mpdf/mpdf#1838] [#530] [#644]
131131
* A reset such as `* { margin: 0; padding: 0 }` now takes away the default margins of paragraphs, headings and lists, a list's indent and a cell's padding, as in a browser. It reaches the lines of the table of contents and the index as well, which are the document's elements, so their levels are no longer indented. Style `div.mpdf_toc_level_1` and the like to indent them again.
132132
* mPDF's own `<barcode>`, `<dottab>` and `<textcircle>` are not HTML elements. A rule reaches them only by naming their tag, or an id or class they carry, so `*`, and `:empty`, `[attr]` or `:not()` alone, leave them as they are: `:empty { display: none }` does not hide a barcode.
133+
* **Font weights as numbers, `bolder` and `lighter`, and `font-size: larger` and `smaller`.** In the standard CSS mode, `font-weight` takes a number from 1 to 1000, and `bolder` and `lighter` step from the parent's weight by the table in CSS Fonts. The weight is inherited as a number, so `bolder` inside `bolder` gives 900 and `lighter` inside 700 gives 400, and `h1 { font-weight: lighter }` is regular, one step down from its parent's 400. A weight above 500 is drawn with the family's bold face and any other with its regular face. `larger` and `smaller` multiply and divide the parent's size by 1.2. A form field reads them against the document's size, as it reads its other font sizes. The `font` shorthand's weight is read the same way. All of these were ignored before, so a document that has them changes. Set `'cssMode' => \Mpdf\CssMode::LEGACY` to read only `normal` and `bold`, as before. [#545] [#647]
133134

134135
Performance
135136
-----------
@@ -244,6 +245,7 @@ Bugfixes
244245
### Forms
245246

246247
* A `<select>` with a bare `disabled` attribute was not disabled. [#429]
248+
* `font-size: larger` or `smaller` on an `<input>`, `<select>`, `<textarea>` or `<textcircle>` drew its text at 0pt, so it could not be seen. In the legacy CSS mode the value is now ignored and the element keeps the size around it; in the standard mode it is 1.2 times the document's size, or that divided by 1.2. [#545] [#647]
247249
* A button with no `name` raised warnings under `useActiveForms`. Unnamed buttons are now named `Submit_<n>`, `Reset_<n>` or `Button_<n>`. [#438]
248250
* Text wider than its field was cut off. It is now drawn in a smaller font, both on the page and in an active field's appearance. [#433] [#441]
249251
* A shaped value too long for its field is cut to the longest start that fits, and only between whole clusters. [#460] [#492]
@@ -824,6 +826,7 @@ These changes do not change output.
824826
[#536]: https://github.com/GravityPDF/mpdf/issues/536
825827
[#537]: https://github.com/GravityPDF/mpdf/issues/537
826828
[#538]: https://github.com/GravityPDF/mpdf/issues/538
829+
[#545]: https://github.com/GravityPDF/mpdf/issues/545
827830
[#548]: https://github.com/GravityPDF/mpdf/issues/548
828831
[#549]: https://github.com/GravityPDF/mpdf/pull/549
829832
[#552]: https://github.com/GravityPDF/mpdf/issues/552
@@ -881,3 +884,4 @@ These changes do not change output.
881884
[#642]: https://github.com/GravityPDF/mpdf/pull/642
882885
[#643]: https://github.com/GravityPDF/mpdf/pull/643
883886
[#644]: https://github.com/GravityPDF/mpdf/pull/644
887+
[#647]: https://github.com/GravityPDF/mpdf/pull/647

‎src/Css/InlinePropertyConverter.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ public function convert($properties)
4141
$css['FONT-SIZE'] = $properties['sizePt'] . 'pt';
4242
}
4343

44-
if (!empty($properties['B'])) {
44+
// The computed weight is saved in the standard CSS mode only. A block starts from the normal weight
45+
if (isset($properties['weight'])) {
46+
if ($properties['weight'] != RelativeFontValues::NORMAL_WEIGHT) {
47+
$css['FONT-WEIGHT'] = (string) $properties['weight'];
48+
}
49+
} elseif (!empty($properties['B'])) {
4550
$css['FONT-WEIGHT'] = 'bold';
4651
}
4752

‎src/Css/NormalizeProperties.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,10 @@ protected function processFontProperty($value)
290290
$this->properties['FONT-STYLE'] = 'normal';
291291
}
292292

293-
// Check for font-weight
294-
if (stripos($keywords, 'bold') !== false) {
293+
// Check for font-weight. Legacy mode reads bolder as bold, and lighter and the numbers as normal
294+
if ($this->mpdf->cssMode === CssMode::STANDARD && preg_match('/\b(bold|bolder|lighter|\d+)\b/i', $keywords, $weight)) {
295+
$this->properties['FONT-WEIGHT'] = strtolower($weight[1]);
296+
} elseif (stripos($keywords, 'bold') !== false) {
295297
$this->properties['FONT-WEIGHT'] = 'bold';
296298
} else {
297299
$this->properties['FONT-WEIGHT'] = 'normal';

‎src/Css/RelativeFontValues.php‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace Mpdf\Css;
4+
5+
use Mpdf\Mpdf;
6+
7+
/**
8+
* The font values that are computed from the parent element's: font-weight, whose bolder and lighter step from the
9+
* parent's weight, and font-size: larger and smaller. Used in the standard CSS mode
10+
*/
11+
final class RelativeFontValues
12+
{
13+
14+
const NORMAL_WEIGHT = 400;
15+
16+
const BOLD_WEIGHT = 700;
17+
18+
/**
19+
* What bolder and lighter give, from CSS Fonts Level 4: each row is the lowest parent weight it applies to, then
20+
* bolder and lighter. Null keeps the parent's weight
21+
*/
22+
private static $relativeWeights = [
23+
[900, null, 700],
24+
[750, 900, 700],
25+
[550, 900, 400],
26+
[350, 700, 100],
27+
[100, 400, 100],
28+
[0, 400, null],
29+
];
30+
31+
/**
32+
* The weight an element's font-weight computes to
33+
*
34+
* @param string $value normal, bold, bolder, lighter, or a number from 1 to 1000
35+
* @param int|float $parentWeight The parent element's computed weight
36+
*
37+
* @return int|float|null Null for a value that is not a font weight
38+
*/
39+
public static function weight($value, $parentWeight)
40+
{
41+
$value = strtolower(trim($value));
42+
43+
switch ($value) {
44+
case 'normal':
45+
return self::NORMAL_WEIGHT;
46+
47+
case 'bold':
48+
return self::BOLD_WEIGHT;
49+
50+
case 'bolder':
51+
case 'lighter':
52+
foreach (self::$relativeWeights as $row) {
53+
if ($parentWeight >= $row[0]) {
54+
$weight = $value === 'bolder' ? $row[1] : $row[2];
55+
56+
return $weight === null ? $parentWeight : $weight;
57+
}
58+
}
59+
60+
return null;
61+
}
62+
63+
if (preg_match('/^\d+(\.\d+)?$/', $value, $m) && $value >= 1 && $value <= 1000) {
64+
return isset($m[1]) ? (float) $value : (int) $value;
65+
}
66+
67+
return null;
68+
}
69+
70+
/**
71+
* Whether a weight is drawn with the bold face of a family that has a regular face and a bold one. Those are
72+
* weights 400 and 700, and CSS Fonts' matching gives the bold face to any weight above 500: of the hundreds, 600 and up.
73+
*
74+
* A family registered with more weights would instead be given the face nearest the weight by the same matching,
75+
* which would need its faces keyed by weight rather than by the B style.
76+
*
77+
* @param int|float $weight
78+
*
79+
* @return bool
80+
*/
81+
public static function isBold($weight)
82+
{
83+
return $weight > 500;
84+
}
85+
86+
/**
87+
* The computed weight of text drawn in the B style given. Some tags and a replayed text buffer set the style on its
88+
* own, so the weight is taken while it agrees with the style, and otherwise is bold's or normal's
89+
*
90+
* @param int|float $weight
91+
* @param bool $bold Whether the B style is set
92+
*
93+
* @return int|float
94+
*/
95+
public static function weightForStyle($weight, $bold)
96+
{
97+
if (self::isBold($weight) === (bool) $bold) {
98+
return $weight;
99+
}
100+
101+
return $bold ? self::BOLD_WEIGHT : self::NORMAL_WEIGHT;
102+
}
103+
104+
/**
105+
* The computed font-weight of the parent of the element setCSS() is styling, which bolder and lighter step from. A
106+
* block starts from the state its parent block saved, and a table cell from its table's weight; an inline element
107+
* is styled in its parent's state
108+
*
109+
* @param Mpdf $mpdf
110+
* @param string $type As setCSS() takes it
111+
*
112+
* @return int|float
113+
*/
114+
public static function parentWeight(Mpdf $mpdf, $type)
115+
{
116+
if ($type === 'BLOCK' && $mpdf->blklvl > 0 && isset($mpdf->blk[$mpdf->blklvl - 1]['InlineProperties']['weight'])) {
117+
return $mpdf->blk[$mpdf->blklvl - 1]['InlineProperties']['weight'];
118+
}
119+
120+
if ($type === 'TABLECELL') {
121+
return self::tableWeight($mpdf->base_table_properties);
122+
}
123+
124+
return self::weightForStyle($mpdf->fontWeight, $mpdf->B);
125+
}
126+
127+
/**
128+
* @param array $tableProperties The properties the table being written hands its cells, as Mpdf::$base_table_properties
129+
*
130+
* @return int|float The computed font-weight of the table, which its cells inherit
131+
*/
132+
public static function tableWeight(array $tableProperties)
133+
{
134+
return isset($tableProperties['FONT-WEIGHT']) ? (float) $tableProperties['FONT-WEIGHT'] : self::NORMAL_WEIGHT;
135+
}
136+
137+
/**
138+
* @param string $value A font-size value
139+
*
140+
* @return float|null What larger or smaller multiply the parent's size by, or null for any other value
141+
*/
142+
public static function sizeRatio($value)
143+
{
144+
switch (strtolower(trim($value))) {
145+
case 'larger':
146+
return 1.2;
147+
148+
case 'smaller':
149+
return 1 / 1.2;
150+
}
151+
152+
return null;
153+
}
154+
}

‎src/Mpdf.php‎

Lines changed: 24 additions & 1 deletion
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\RelativeFontValues;
1011
use Mpdf\Color\IccProfile;
1112
use Mpdf\Css\TextVars;
1213
use Mpdf\Fonts\Color\ColorFormats;
@@ -875,6 +876,13 @@ class Mpdf implements \Psr\Log\LoggerAwareInterface
875876
var $links; // array of internal links
876877
var $FontFamily; // current font family
877878
var $FontStyle; // current font style
879+
880+
/**
881+
* @var int|float The computed font-weight of the text being written, in the standard CSS mode, which sets the B style.
882+
* Tags that set the B style on their own leave it behind, so it is read through RelativeFontValues::weightForStyle()
883+
*/
884+
var $fontWeight = RelativeFontValues::NORMAL_WEIGHT;
885+
878886
var $CurrentFont; // current font info
879887
var $FontSizePt; // current font size in points
880888
var $FontSize; // current font size in user unit
@@ -7157,6 +7165,9 @@ function saveInlineProperties()
71577165
$saved['lang'] = $this->currentLang;
71587166
$saved['fontLanguageOverride'] = $this->fontLanguageOverride; // mPDF 5.7.1
71597167
$saved['display_off'] = $this->inlineDisplayOff;
7168+
if ($this->cssMode === CssMode::STANDARD) {
7169+
$saved['weight'] = RelativeFontValues::weightForStyle($this->fontWeight, $this->B);
7170+
}
71607171

71617172
return $saved;
71627173
}
@@ -7200,6 +7211,9 @@ function restoreInlineProperties(&$saved)
72007211
$this->currentfontstyle = $saved['style'];
72017212
$this->currentfontsize = $saved['sizePt'];
72027213
$this->SetStylesArray(['B' => $saved['B'], 'I' => $saved['I']]); // mPDF 5.7.1
7214+
if (isset($saved['weight'])) {
7215+
$this->fontWeight = $saved['weight'];
7216+
}
72037217

72047218
$this->TextColor = $saved['color'];
72057219
$this->FillColor = $saved['bgcolor'];
@@ -19758,7 +19772,15 @@ function setCSS($arrayaux, $type = '', $tag = '')
1975819772
}
1975919773
break;
1976019774

19761-
case 'FONT-WEIGHT': // normal bold // Does not support: bolder, lighter, 100..900(step value=100)
19775+
case 'FONT-WEIGHT':
19776+
if ($this->cssMode === CssMode::STANDARD) {
19777+
$parentWeight = RelativeFontValues::parentWeight($this, $type);
19778+
$weight = RelativeFontValues::weight($v, $parentWeight);
19779+
$this->fontWeight = $weight === null ? $parentWeight : $weight;
19780+
$this->SetStyle('B', RelativeFontValues::isBold($this->fontWeight));
19781+
break;
19782+
}
19783+
// Legacy mode reads only normal and bold
1976219784
switch (strtoupper($v)) {
1976319785
case 'BOLD':
1976419786
$this->SetStyle('B', true);
@@ -20198,6 +20220,7 @@ function ResetStyles()
2019820220
foreach (['B', 'I'] as $s) {
2019920221
$this->$s = false;
2020020222
}
20223+
$this->fontWeight = RelativeFontValues::NORMAL_WEIGHT;
2020120224
$this->currentfontstyle = '';
2020220225
$this->SetFont('', '', 0, false);
2020320226
}

‎src/SizeConverter.php‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mpdf;
44

55
use Psr\Log\LoggerInterface;
6+
use Mpdf\Css\RelativeFontValues;
67
use Mpdf\Log\Context as LogContext;
78
use Mpdf\PsrLogAwareTrait\PsrLogAwareTrait;
89

@@ -177,11 +178,11 @@ public function convert($size = 5, $maxsize = 0, $fontsize = false, $usefontsize
177178
}
178179

179180
/**
180-
* Reads a font size: a length against the parent's font size, or a keyword such as small against the size medium
181-
* stands for
181+
* Reads a font size: a length against the parent's font size, larger or smaller as the parent's multiplied or
182+
* divided by 1.2 in the standard CSS mode, or a keyword such as small against the size medium stands for
182183
*
183184
* @param string $value
184-
* @param float $parent The parent's font size in mm, which em and % are read against
185+
* @param float $parent The parent's font size in mm, which em, %, larger and smaller are read against
185186
* @param float $medium The size in points medium stands for
186187
*
187188
* @return float|null In points, or null for a value it does not read
@@ -193,11 +194,34 @@ public function convertFontSize($value, $parent, $medium)
193194
return $this->convert($value, $parent) * Mpdf::SCALE;
194195
}
195196

197+
$ratio = RelativeFontValues::sizeRatio($value);
198+
if ($ratio !== null) {
199+
return $this->mpdf->cssMode === CssMode::STANDARD ? $parent * $ratio * Mpdf::SCALE : null;
200+
}
201+
196202
$keyword = strtoupper($value);
197203

198204
return isset($this->mpdf->fontsizes[$keyword]) ? $this->mpdf->fontsizes[$keyword] * $medium : null;
199205
}
200206

207+
/**
208+
* A font-size, in mm, against the parent element's size
209+
*
210+
* @param string $size
211+
* @param float $parentSize In mm
212+
*
213+
* @return float|null Null for larger or smaller in the legacy CSS mode, which ignores them
214+
*/
215+
public function convertFontSizeToMm($size, $parentSize)
216+
{
217+
$ratio = RelativeFontValues::sizeRatio($size);
218+
if ($ratio === null) {
219+
return $this->convert($size, $parentSize);
220+
}
221+
222+
return $this->mpdf->cssMode === CssMode::STANDARD ? $parentSize * $ratio : null;
223+
}
224+
201225
/**
202226
* Whether convert() reads the value as a number in a unit it knows, or as a number with no unit, which it reads
203227
* as pixels. The number may have a sign and an exponent. Keywords such as "auto" or "thin" are not lengths.

‎src/Tag/Input.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public function open($attr, &$ahtml, &$ihtml)
7171
$this->mpdf->SetFont($properties['FONT-FAMILY'], $this->mpdf->FontStyle, 0, false);
7272
}
7373
if (isset($properties['FONT-SIZE']) && $properties['FONT-SIZE'] !== 'auto') {
74-
$mmsize = $this->sizeConverter->convert($properties['FONT-SIZE'], $this->mpdf->default_font_size / Mpdf::SCALE);
75-
$this->mpdf->SetFontSize($mmsize * Mpdf::SCALE, false);
74+
$this->setFontSizeAgainstDocument($properties['FONT-SIZE']);
7675
}
7776
if (isset($properties['COLOR'])) {
7877
$objattr['color'] = $this->colorConverter->convert($properties['COLOR'], $this->mpdf->PDFAXwarnings);

‎src/Tag/Select.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public function open($attr, &$ahtml, &$ihtml)
1616
$this->mpdf->SetFont($properties['FONT-FAMILY'], $this->mpdf->FontStyle, 0, false);
1717
}
1818
if (isset($properties['FONT-SIZE'])) {
19-
$mmsize = $this->sizeConverter->convert($properties['FONT-SIZE'], $this->mpdf->default_font_size / Mpdf::SCALE);
20-
$this->mpdf->SetFontSize($mmsize * Mpdf::SCALE, false);
19+
$this->setFontSizeAgainstDocument($properties['FONT-SIZE']);
2120
}
2221
if (isset($attr['SPELLCHECK']) && strtolower($attr['SPELLCHECK']) === 'true') {
2322
$this->mpdf->selectoption['SPELLCHECK'] = true;

‎src/Tag/Table.php‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Mpdf\Css\Border;
66
use Mpdf\Css\PresentationalHints;
7+
use Mpdf\Css\RelativeFontValues;
78
use Mpdf\CssMode;
89
use Mpdf\Mpdf;
910
use Mpdf\Utils\Rotation;
@@ -300,12 +301,10 @@ public function open($attr, &$ahtml, &$ihtml)
300301
$this->mpdf->base_table_properties['FONT-FAMILY'] = $this->mpdf->FontFamily;
301302

302303
if (isset($properties['FONT-SIZE'])) {
303-
if ($this->mpdf->tableLevel > 1) {
304-
$tableFontSize = $this->sizeConverter->convert($this->mpdf->base_table_properties['FONT-SIZE']);
305-
$mmsize = $this->sizeConverter->convert($properties['FONT-SIZE'], $tableFontSize);
306-
} else {
307-
$mmsize = $this->sizeConverter->convert($properties['FONT-SIZE'], $this->mpdf->default_font_size / Mpdf::SCALE);
308-
}
304+
$parentSize = $this->mpdf->tableLevel > 1
305+
? $this->sizeConverter->convert($this->mpdf->base_table_properties['FONT-SIZE'])
306+
: $this->mpdf->default_font_size / Mpdf::SCALE;
307+
$mmsize = $this->sizeConverter->convertFontSizeToMm($properties['FONT-SIZE'], $parentSize);
309308
if ($mmsize) {
310309
$this->mpdf->default_font_size = $mmsize * Mpdf::SCALE;
311310
$this->mpdf->SetFontSize($this->mpdf->default_font_size, false);
@@ -314,7 +313,12 @@ public function open($attr, &$ahtml, &$ihtml)
314313
$this->mpdf->base_table_properties['FONT-SIZE'] = $this->mpdf->FontSize . 'mm';
315314

316315
if (isset($properties['FONT-WEIGHT'])) {
317-
if (strtoupper($properties['FONT-WEIGHT']) === 'BOLD') {
316+
if ($this->mpdf->cssMode === CssMode::STANDARD) {
317+
$weight = RelativeFontValues::weight($properties['FONT-WEIGHT'], RelativeFontValues::tableWeight($this->mpdf->base_table_properties));
318+
if ($weight !== null) {
319+
$this->mpdf->base_table_properties['FONT-WEIGHT'] = (string) $weight;
320+
}
321+
} elseif (strtoupper($properties['FONT-WEIGHT']) === 'BOLD') {
318322
$this->mpdf->base_table_properties['FONT-WEIGHT'] = 'BOLD';
319323
}
320324
}

0 commit comments

Comments
 (0)