|
| 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 | +} |
0 commit comments