summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-fonts/generic-family-keywords-003.html
blob: c787b59fb285dab2551c98026641b7f9dfdb84cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<title>Test quotes vs. no-quotes matchings of generic font family keywords in a CanvasRenderingContext2D</title>
<link rel="help" href="https://drafts.csswg.org/css-fonts-4/#family-name-syntax">
<link rel="author" title="Frédéric Wang" href="mailto:fwang@igalia.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/css/css-fonts/support/font-family-keywords.js"></script>
<link rel="stylesheet" type="text/css" href="/fonts/ahem.css"/>
<body>
<canvas id="canvas" width="400" height="150"></canvas>
<script>
  setup({ explicit_done: true });
  window.addEventListener("load", () => { document.fonts.ready.then(runTests); });
  function runTests() {
    const measured_text = "|||||";
    const canvas = document.getElementById("canvas");
    const ctx = canvas.getContext("2d");
    ctx.font = `25px Ahem`;
    let ahem_expected_width = ctx.measureText(measured_text).width;

    kGenericFontFamilyKeywords.forEach(keyword => {
      test(() => {
         ctx.font = `25px ${keyword}`;
         let expected_width = ctx.measureText(measured_text).width;

        // Insert the @font-face rules for quoted and unquoted keywords.
        document.documentElement.insertAdjacentHTML('beforeend', `
<style>
@font-face {
  font-family: ${keyword};
  src: local(Ahem), url('/fonts/Ahem.ttf');
}
</style>
<style>
@font-face {
  font-family: "${keyword}";
  src: local(Ahem), url('/fonts/Ahem.ttf');
}
</style>`);

        ctx.font = `25px ${keyword}`;
        let unquoted_width = ctx.measureText(measured_text).width;
        assert_equals(unquoted_width, expected_width, `unquoted ${keyword} does not match @font-face rule`);

        ctx.font = `25px "${keyword}"`;
        let quoted_width = ctx.measureText(measured_text).width;
        assert_equals(quoted_width, ahem_expected_width, `quoted ${keyword} matches  @font-face rule`);
      }, `@font-face matching for quoted and unquoted ${keyword} (drawing text in a canvas)`);
    });
    done();
  }
</script>
</body>