tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

variation-sequences.js (3893B)


      1 var baseChars = {
      2    "emoji": "\u{1fae8}",
      3    "cjk": "\u{8279}",
      4    "math": "\u{2205}"
      5 };
      6 
      7 var variationSelectors = {
      8    "emoji": ["\u{fe0e}", "\u{fe0f}"],
      9    "cjk": ["", "\u{FE00}", "\u{FE01}", "\u{e0100}", "\u{e0101}",
     10        "\u{e0102}"
     11    ],
     12    "math": ["", "\u{FE00}"]
     13 };
     14 
     15 var families = {
     16    "emoji": ["ColorEmojiFont", "MonoEmojiFont",
     17        "EmojiFontWithBaseCharOnly",
     18        "sans-serif"
     19    ],
     20    "cjk": ["CJKFontWithVS", "CJKFontWithBaseCharOnly",
     21        "sans-serif"
     22    ],
     23    "math": ["MathFontWithVS", "MathFontWithBaseCharOnly",
     24        "sans-serif"
     25    ]
     26 };
     27 
     28 var variationSequenceFamilies = new Map([
     29    ["\u{1fae8}\u{fe0e}", "MonoEmojiFont"],
     30    ["\u{1fae8}\u{fe0f}", "ColorEmojiFont"],
     31    ["\u{8279}\u{fe00}", "CJKFontWithVS"],
     32    ["\u{8279}\u{fe01}", "CJKFontWithVS"],
     33    ["\u{8279}\u{e0100}", "CJKFontWithVS"],
     34    ["\u{8279}\u{e0101}", "CJKFontWithVS"],
     35    ["\u{8279}\u{e0102}", "CJKFontWithVS"],
     36    ["\u{2205}\u{FE00}", "MathFontWithVS"]
     37 ]);
     38 
     39 var baseCharFamilies = new Map([
     40    ["\u{1fae8}", new Set(["MonoEmojiFont", "ColorEmojiFont",
     41        "EmojiFontWithBaseCharOnly"
     42    ])],
     43    ["\u{8279}", new Set(["CJKFontWithVS",
     44        "CJKFontWithBaseCharOnly"
     45    ])],
     46    ["\u{2205}", new Set(["MathFontWithVS",
     47        "MathFontWithBaseCharOnly"
     48    ])]
     49 ]);
     50 
     51 const range = function*(l) {
     52    for (let i = 0; i < l; i += 1) yield i;
     53 }
     54 const isEmpty = arr =>
     55    arr.length === 0;
     56 
     57 const permutations =
     58    function*(a) {
     59  const r = arguments[1] || [];
     60  if (isEmpty(a))
     61    yield r;
     62  for (let i of range(a.length)) {
     63    const aa = [...a];
     64    const rr = [...r, ...aa.splice(i, 1)];
     65    yield* permutations(aa, rr);
     66  }
     67 }
     68 
     69 function getMatchedFamilyForVariationSequence(
     70    familyList, baseCharacter, variationSelector) {
     71  const variationSequence = baseCharacter + variationSelector;
     72  // First try to find a match for the whole variation sequence.
     73  if (variationSequenceFamilies.has(variationSequence)) {
     74    const matchedFamily = variationSequenceFamilies.get(variationSequence);
     75    if (familyList.includes(matchedFamily)) {
     76      return matchedFamily;
     77    }
     78  }
     79  // If failed, try to match only the base character from the
     80  // variation sequence.
     81  if (baseCharFamilies.has(baseCharacter)) {
     82    const eligibleFamilies = baseCharFamilies.get(baseCharacter);
     83    const matchedFamilies =
     84        familyList.filter(value => eligibleFamilies.has(value));
     85    if (matchedFamilies.length) {
     86      return matchedFamilies[0];
     87    }
     88  }
     89  // We should not reach here, we should always match one of the
     90  // specified web fonts in the tests.
     91  return "";
     92 }
     93 
     94 function generateContent(
     95    families, baseChar, variationSelectors, getFontFamilyValue) {
     96  var rootElem = document.createElement('div');
     97  // We want to test all possible combinations of variation
     98  // selectors and font-family list values. For the refs,
     99  // we explicitly specify the font that we expect to be
    100  // matched from the maps at the beginning of the files.
    101  const allFamiliesLists = permutations(families);
    102  for (const familyList of allFamiliesLists) {
    103    for (const variationSelector of variationSelectors) {
    104      const contentSpan = document.createElement("span");
    105      contentSpan.textContent = baseChar + variationSelector;
    106      contentSpan.style.fontFamily =
    107          getFontFamilyValue(familyList, baseChar, variationSelector);
    108      rootElem.appendChild(contentSpan);
    109    }
    110  }
    111  document.body.appendChild(rootElem);
    112 }
    113 
    114 function generateVariationSequenceTests(type) {
    115  var getFontFamilyValue = (familyList, baseChar, variationSelector) => {
    116    return familyList.join(', ');
    117  }
    118  generateContent(families[type], baseChars[type], variationSelectors[type], getFontFamilyValue);
    119 }
    120 
    121 function generateVariationSequenceRefs(type) {
    122  generateContent(
    123      families[type], baseChars[type], variationSelectors[type],
    124      getMatchedFamilyForVariationSequence);
    125 }