summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-fonts/support/js/variation-sequences.js
blob: 84c5a1a9c77932b9b4aedc2be24a38e148bae00d (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
var baseChars = {
    "emoji": "\u{1fae8}",
    "cjk": "\u{8279}",
    "math": "\u{2205}"
};

var variationSelectors = {
    "emoji": ["\u{fe0e}", "\u{fe0f}"],
    "cjk": ["", "\u{FE00}", "\u{FE01}", "\u{e0100}", "\u{e0101}",
        "\u{e0102}"
    ],
    "math": ["", "\u{FE00}"]
};

var families = {
    "emoji": ["ColorEmojiFont", "MonoEmojiFont",
        "EmojiFontWithBaseCharOnly",
        "sans-serif"
    ],
    "cjk": ["CJKFontWithVS", "CJKFontWithBaseCharOnly",
        "sans-serif"
    ],
    "math": ["MathFontWithVS", "MathFontWithBaseCharOnly",
        "sans-serif"
    ]
};

var variationSequenceFamilies = new Map([
    ["\u{1fae8}\u{fe0e}", "MonoEmojiFont"],
    ["\u{1fae8}\u{fe0f}", "ColorEmojiFont"],
    ["\u{8279}\u{fe00}", "CJKFontWithVS"],
    ["\u{8279}\u{fe01}", "CJKFontWithVS"],
    ["\u{8279}\u{e0100}", "CJKFontWithVS"],
    ["\u{8279}\u{e0101}", "CJKFontWithVS"],
    ["\u{8279}\u{e0102}", "CJKFontWithVS"],
    ["\u{2205}\u{FE00}", "MathFontWithVS"]
]);

var baseCharFamilies = new Map([
    ["\u{1fae8}", new Set(["MonoEmojiFont", "ColorEmojiFont",
        "EmojiFontWithBaseCharOnly"
    ])],
    ["\u{8279}", new Set(["CJKFontWithVS",
        "CJKFontWithBaseCharOnly"
    ])],
    ["\u{2205}", new Set(["MathFontWithVS",
        "MathFontWithBaseCharOnly"
    ])]
]);

const range = function*(l) {
    for (let i = 0; i < l; i += 1) yield i;
}
const isEmpty = arr =>
    arr.length === 0;

const permutations =
    function*(a) {
  const r = arguments[1] || [];
  if (isEmpty(a))
    yield r;
  for (let i of range(a.length)) {
    const aa = [...a];
    const rr = [...r, ...aa.splice(i, 1)];
    yield* permutations(aa, rr);
  }
}

function getMatchedFamilyForVariationSequence(
    familyList, baseCharacter, variationSelector) {
  const variationSequence = baseCharacter + variationSelector;
  // First try to find a match for the whole variation sequence.
  if (variationSequenceFamilies.has(variationSequence)) {
    const matchedFamily = variationSequenceFamilies.get(variationSequence);
    if (familyList.includes(matchedFamily)) {
      return matchedFamily;
    }
  }
  // If failed, try to match only the base character from the
  // variation sequence.
  if (baseCharFamilies.has(baseCharacter)) {
    const eligibleFamilies = baseCharFamilies.get(baseCharacter);
    const matchedFamilies =
        familyList.filter(value => eligibleFamilies.has(value));
    if (matchedFamilies.length) {
      return matchedFamilies[0];
    }
  }
  // We should not reach here, we should always match one of the
  // specified web fonts in the tests.
  return "";
}

function generateContent(
    families, baseChar, variationSelectors, getFontFamilyValue) {
  var rootElem = document.createElement('div');
  // We want to test all possible combinations of variation
  // selectors and font-family list values. For the refs,
  // we explicitly specify the font that we expect to be
  // matched from the maps at the beginning of the files.
  const allFamiliesLists = permutations(families);
  for (const familyList of allFamiliesLists) {
    for (const variationSelector of variationSelectors) {
      const contentSpan = document.createElement("span");
      contentSpan.textContent = baseChar + variationSelector;
      contentSpan.style.fontFamily =
          getFontFamilyValue(familyList, baseChar, variationSelector);
      rootElem.appendChild(contentSpan);
    }
  }
  document.body.appendChild(rootElem);
}

function generateVariationSequenceTests(type) {
  var getFontFamilyValue = (familyList, baseChar, variationSelector) => {
    return familyList.join(', ');
  }
  generateContent(families[type], baseChars[type], variationSelectors[type], getFontFamilyValue);
}

function generateVariationSequenceRefs(type) {
  generateContent(
      families[type], baseChars[type], variationSelectors[type],
      getMatchedFamilyForVariationSequence);
}