summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_unicode_range_loading.html
blob: 43622e2ae5864aee8703d48a1955f85a23721c2c (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!DOCTYPE HTML>
<html>
<head>
  <meta charset=utf-8>
  <title>unicode-range load tests using font loading api</title>
  <link rel="author" title="John Daggett" href="mailto:jdaggett@mozilla.com">
  <link rel="help" href="http://www.w3.org/TR/css-fonts-3/#unicode-range-desc" />
  <link rel="help" href="http://dev.w3.org/csswg/css-font-loading/" />
  <meta name="assert" content="unicode-range descriptor defines precisely which fonts should be loaded" />
  <script type="text/javascript" src="/resources/testharness.js"></script>
  <script type="text/javascript" src="/resources/testharnessreport.js"></script>
  <style type="text/css">
  </style>
</head>
<body>
<div id="log"></div>
<pre id="display"></pre>
<style id="testfonts"></style>
<style id="teststyle"></style>
<div id="testcontent"></div>

<script>

const kSheetFonts = 1;
const kSheetStyles = 2;

const redSquDataURL = "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10' width='100%' height='100%'><rect fill='red' x='0' y='0' width='10' height='10'/></svg>";

var unicodeRangeTests = [
  { test: "simple load sanity check, unused fonts not loaded",
    fonts: [{ family: "a", src: "markA", descriptors: { }, loaded: false}],
    content: "AAA", style: { "font-family": "unused" } },
  { test: "simple load sanity check, font for a used character loaded",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: true}],
    content: "AAA" },
  { test: "simple load sanity check, font for an unused character not loaded",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: false}],
    content: "BBB" },
  { test: "simple load sanity check, with two fonts only font for used character loaded A",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: true},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: false}],
    content: "AAA" },
  { test: "simple load sanity check, with two fonts only font for used character loaded B",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: false},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: true}],
    content: "BBB" },
  { test: "simple load sanity check, two fonts but neither supports characters used",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: false},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: false}],
    content: "CCC" },
  { test: "simple load sanity check, two fonts and both are used",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: true},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: true}],
    content: "ABC" },
  { test: "simple load sanity check, one with Han ranges",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+3???,u+5???,u+7???,u+8???" }, loaded: true},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: false}],
    content: "納豆嫌い" },
  { test: "simple load sanity check, two fonts with different styles A",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: true},
            { family: "a", src: "markB", descriptors: { weight: "bold", unicodeRange: "u+42" }, loaded: false}],
    content: "ABC" },
  { test: "simple load sanity check, two fonts with different styles B",
    fonts: [{ family: "a", src: "markA", descriptors: { weight: "bold", unicodeRange: "u+41" }, loaded: false},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: true}],
    content: "ABC" },
  { test: "multiple fonts with overlapping ranges, all with default ranges, only last one supports character used",
    fonts: [{ family: "a", src: "markC", descriptors: { }, loaded: true},
            { family: "a", src: "markA", descriptors: { }, loaded: true},
            { family: "a", src: "markB", descriptors: { }, loaded: true}],
    content: "CCC" },
  { test: "multiple fonts with overlapping ranges, all with default ranges, first one supports character used",
    fonts: [{ family: "a", src: "markB", descriptors: { }, loaded: false},
            { family: "a", src: "markA", descriptors: { }, loaded: false},
            { family: "a", src: "markC", descriptors: { }, loaded: true}],
    content: "CCC" },
  { test: "multiple fonts with overlapping ranges, one with default value in the fallback position",
    fonts: [{ family: "a", src: "markC", descriptors: { }, loaded: true},
            { family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: true},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: true}],
    content: "ABC" },
  { test: "multiple fonts with overlapping ranges, one with default value in the primary use position, fallback to one",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: true},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: false},
            { family: "a", src: "markC", descriptors: { }, loaded: true}],
    content: "AAA" },
  { test: "multiple fonts with overlapping ranges, one with default value in the primary use position, fallback to two",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: true},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: true},
            { family: "a", src: "markC", descriptors: { }, loaded: true}],
    content: "ABC" },
  { test: "multiple fonts with overlapping ranges, one with default value in the primary use position, no fallback",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+41" }, loaded: false},
            { family: "a", src: "markB", descriptors: { unicodeRange: "u+42" }, loaded: false},
            { family: "a", src: "markC", descriptors: { }, loaded: true}],
    content: "CCC" },
  { test: "metrics only case, ex-sized image, single font with space in range",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+0??" }, loaded: true}],
    content: "<img style='width: 2ex' src=\"" + redSquDataURL + "\">" },
  { test: "metrics only case, ex-sized image, single font with space outside range",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+1??" }, loaded: false}],
    content: "<img style='width: 2ex' src=\"" + redSquDataURL + "\">" },
  { test: "metrics only case, ch-sized image, single font with space in range",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+0??" }, loaded: true}],
    content: "<img style='width: 2ch' src=\"" + redSquDataURL + "\">" },
  { test: "metrics only case, ch-sized image, single font with space outside range",
    fonts: [{ family: "a", src: "markA", descriptors: { unicodeRange: "u+1??" }, loaded: false}],
    content: "<img style='width: 2ch' src=\"" + redSquDataURL + "\">" },
];

// map font loading descriptor names to @font-face rule descriptor names
var mapDescriptorNames = {
  style: "font-style",
  weight: "font-weight",
  stretch: "font-stretch",
  unicodeRange: "unicode-range",
  variant: "font-variant",
  featureSettings: "font-feature-settings"
};

var kBaseFontURL;
if ("SpecialPowers" in window) {
  kBaseFontURL = "";
} else {
  kBaseFontURL = "fonts/";
}

var mapFontURLs = {
  markA: "url(" + kBaseFontURL + "markA.woff" + ")",
  markB: "url(" + kBaseFontURL + "markB.woff" + ")",
  markC: "url(" + kBaseFontURL + "markC.woff" + ")",
  markD: "url(" + kBaseFontURL + "markD.woff" + ")",

  /* twourl versions include a bogus url followed by a valid url */
  markAtwourl: "url(" + kBaseFontURL + "bogus-markA.woff" + "), url(" + kBaseFontURL + "markA.woff" + ")",
  markBtwourl: "url(" + kBaseFontURL + "bogus-markB.woff" + "), url(" + kBaseFontURL + "markB.woff" + ")",
  markCtwourl: "url(" + kBaseFontURL + "bogus-markC.woff" + "), url(" + kBaseFontURL + "markC.woff" + ")",
  markDtwourl: "url(" + kBaseFontURL + "bogus-markD.woff" + "), url(" + kBaseFontURL + "markD.woff" + ")",

  /* localfont versions include a bogus local ref followed by a valid url */
  markAlocalfirst: "local(bogus-markA), url(" + kBaseFontURL + "markA.woff" + ")",
  markBlocalfirst: "local(bogus-markB), url(" + kBaseFontURL + "markB.woff" + ")",
  markClocalfirst: "local(bogus-markC), url(" + kBaseFontURL + "markC.woff" + ")",
  markDlocalfirst: "local(bogus-markD), url(" + kBaseFontURL + "markD.woff" + ")",
};

function familyName(name, i) {
  return "test" + i + "-" + name;
}

function fontFaceRule(name, fontdata, ft) {
  var desc = [];
  desc.push("font-family: " + name);
  var srckey = fontdata.src + ft;
  desc.push("src: " + mapFontURLs[srckey]);
  for (var d in fontdata.descriptors) {
    desc.push(mapDescriptorNames[d] + ": " + fontdata.descriptors[d]);
  }
  return "@font-face { " + desc.join(";") + " }";
}

function clearRules(sheetIndex) {
  var sheet = document.styleSheets[sheetIndex];
  while(sheet.cssRules.length > 0) {
    sheet.deleteRule(0);
  }
}

function clearAllRulesAndFonts() {
  clearRules(kSheetFonts);
  clearRules(kSheetStyles);
  document.fonts.clear();
}

function addStyleRulesAndText(testdata, i) {
  // add style rules for testcontent
  var sheet = document.styleSheets[kSheetStyles];
  while(sheet.cssRules.length > 0) {
    sheet.deleteRule(0);
  }
  var rule = [];
  var family = familyName(testdata.fonts[0].family, i);
  rule.push("#testcontent { font-family: " + family);
  if ("style" in testdata) {
    for (s in testdata.style) {
      rule.push(s + ": " + testdata.style[s]);
    }
  }
  rule.push("}");
  sheet.insertRule(rule.join("; "), 0);

  var content = document.getElementById("testcontent");
  content.innerHTML = testdata.content;
  content.offsetHeight;
}

// work arounds
function getFonts() {
  if ("forEach" in document.fonts) {
    return document.fonts;
  }
  return Array.from(document.fonts);
}

function getSize() {
  if ("size" in document.fonts) {
    return document.fonts.size;
  }
  return getFonts().length;
}

function getReady() {
  if (typeof(document.fonts.ready) == "function") {
    return document.fonts.ready();
  }
  return document.fonts.ready;
}

function setTimeoutPromise(aDelay) {
  return new Promise(function(aResolve, aReject) {
    setTimeout(aResolve, aDelay);
  });
}

function addFontFaceRules(testdata, i, ft) {
  var sheet = document.styleSheets[kSheetFonts];
  var createdFonts = [];
  testdata.fonts.forEach(function(f) {
    var n = sheet.cssRules.length;
    var fn = familyName(f.family, i);
    sheet.insertRule(fontFaceRule(fn, f, ft), n);
    var newfont;
    var fonts = getFonts();
    try {
      fonts.forEach(function(font) { newfont = font; });
      createdFonts.push({family: fn, data: f, font: newfont});
    } catch (e) {
      console.log(e);
    }
  });
  return createdFonts;
}

function addDocumentFonts(testdata, i, ft) {
  var createdFonts = [];
  testdata.fonts.forEach(function(fd) {
    var fn = familyName(fd.family, i);
    var srckey = fd.src + ft;
    var f = new FontFace(fn, mapFontURLs[srckey], fd.descriptors);
    document.fonts.add(f);
    createdFonts.push({family: fn, data: fd, font: f});
  });
  return createdFonts;
}

var q = Promise.resolve();

function runTests() {
  function setupTests() {
    setup({explicit_done: true});
  }

  function checkFontsBeforeLoad(name, testdata, fd) {
    test(function() {
      assert_equals(document.fonts.status, "loaded", "before initializing test, no fonts should be loading - found: " + document.fonts.status);
      var size = getSize();
      assert_equals(size, testdata.fonts.length,
                    "fonts where not added to the font set object");
      var i = 0;
      fonts = getFonts();
      fonts.forEach(function(ff) {
        assert_equals(ff.status, "unloaded", "added fonts should be in unloaded state");
      });
    }, name + " before load");
  }

  function checkFontsAfterLoad(name, testdata, fd, afterTimeout) {
    test(function() {
      assert_equals(document.fonts.status, "loaded", "after ready promise resolved, no fonts should be loading");
      var i = 0;
      fd.forEach(function(f) {
        assert_true(f.font instanceof FontFace, "font needs to be an instance of FontFace object");
        if (f.data.loaded) {
          assert_equals(f.font.status, "loaded", "font not loaded - font " + i + " " + f.data.src + " "
                        + JSON.stringify(f.data.descriptors) + " for content " + testdata.content);
        } else {
          assert_equals(f.font.status, "unloaded", "font loaded - font " + i + " " + f.data.src + " "
                        + JSON.stringify(f.data.descriptors) + " for content " + testdata.content);
        }
        i++;
      });
    }, name + " after load" + (afterTimeout ? " and timeout" : ""));
  }

  function testFontLoads(testdata, i, name, fd) {
    checkFontsBeforeLoad(name, testdata, fd);
    addStyleRulesAndText(testdata, i);

    var ready = getReady();
    return ready.then(function() {
      checkFontsAfterLoad(name, testdata, fd, false);
    }).then(function() {
      return setTimeoutPromise(0).then(function() {
        checkFontsAfterLoad(name, testdata, fd, true);
      });
    }).then(function() {
      var ar = getReady();
      return ar.then(function() {
        test(function() {
          assert_equals(document.fonts.status, "loaded", "after ready promise fulfilled once, fontset should not be loading");
          var fonts = getFonts();
          fonts.forEach(function(f) {
            assert_not_equals(f.status, "loading", "after ready promise fulfilled once, no font should be loading");
          });
        }, name + " test done check");
      });
    }).then(function() {
      clearAllRulesAndFonts();
    });
  }

  function testUnicodeRangeFontFace(testdata, i, ft) {
    var name = "TEST " + i + " " + testdata.test + " (@font-face rules)" + (ft != "" ? " " + ft : ft);

    var fd = addFontFaceRules(testdata, i, ft);
    return testFontLoads(testdata, i, name, fd);
  }

  function testUnicodeRangeDocumentFonts(testdata, i, ft) {
    var name = "TEST " + i + " " + testdata.test + " (document.fonts)" + (ft != "" ? " " + ft : ft);

    var fd = addDocumentFonts(testdata, i, ft);
    return testFontLoads(testdata, i, name, fd);
  }

  q = q.then(function() {
    setupTests();
  });

  var fontTypes = ["", "twourl", "localfirst"];

  unicodeRangeTests.forEach(function(testdata, i) {
    fontTypes.forEach(function(ft) {
      q = q.then(function() {
        return testUnicodeRangeFontFace(testdata, i, ft);
      }).then(function() {
        return testUnicodeRangeDocumentFonts(testdata, i, ft);
      });
    });
  });

  q = q.then(function() {
    done();
  });
}

if ("fonts" in document) {
  runTests();
} else {
  test(function() {
    assert_true(true, "CSS Font Loading API is not enabled.");
  }, "CSS Font Loading API is not enabled");
}
</script>
</body>
</html>