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