Truly independent web browser
<!DOCTYPE html>
<style>
@font-face {
font-family: ColorEmoji;
src: url("../../../Ref/data/fonts/NotoColorEmoji-Regular_subset.ttf");
}
@font-face {
font-family: MonoEmoji;
src: url("../../../Ref/data/fonts/NotoEmoji-Regular_subset.ttf");
}
@font-face {
font-family: Ahem;
src: url("../../../Ref/data/fonts/Ahem.ttf");
}
@font-face {
font-family: OnlyA;
src: url("../../../Ref/data/fonts/Ahem.ttf");
unicode-range: U+0041;
}
</style>
<script src="../include.js"></script>
<script>
// Both fonts have a glyph for the emoji: MonoEmoji a black outline, and ColorEmoji a color image.
const EMOJI = "\u{1FAE8}";
function draw(font, text) {
const canvas = document.createElement("canvas");
canvas.width = 200;
canvas.height = 100;
const context = canvas.getContext("2d");
context.font = font;
context.fillText(text, 20, 70);
return context.getImageData(0, 0, canvas.width, canvas.height).data;
}
function width(font, text) {
const context = document.createElement("canvas").getContext("2d");
context.font = font;
return context.measureText(text).width;
}
function hasColorInk(font, text) {
const data = draw(font, text);
for (let i = 0; i < data.length; i += 4) {
if (data[i + 3] > 128 && data[i] - data[i + 2] > 64)
return "in color";
}
return "not in color";
}
asyncTest(async done => {
await Promise.all([
document.fonts.load("40px ColorEmoji", EMOJI),
document.fonts.load("40px MonoEmoji", EMOJI),
document.fonts.load("40px Ahem"),
document.fonts.load("40px OnlyA", "A"),
]);
// A variation selector picks the font that has that presentation, wherever it is in the list.
println(`With the emoji variation selector, after the text font: ${hasColorInk("40px MonoEmoji, ColorEmoji", EMOJI + "\uFE0F")}`);
println(`With the text variation selector, after the color font: ${hasColorInk("40px ColorEmoji, MonoEmoji", EMOJI + "\uFE0E")}`);
// Without one, the first font with a glyph draws it.
println(`Without a variation selector, after the text font: ${hasColorInk("40px MonoEmoji, ColorEmoji", EMOJI)}`);
println(`Without a variation selector, after the color font: ${hasColorInk("40px ColorEmoji, MonoEmoji", EMOJI)}`);
// What follows a zero width joiner is still drawn by a font that has a glyph for it.
const font = "40px ColorEmoji, Ahem";
const letterWidth = width(font, EMOJI + "\u200DX") - width(font, EMOJI + "\u200D");
println(`A letter after an emoji and a joiner is as wide as an Ahem glyph: ${letterWidth === 40}`);
// Ahem has a glyph for the joiner too, but the joiner stays in the font of the emoji it follows.
const joined = width("40px Ahem", "\u{1F468}\u200D\u{1F469}\u200D\u{1F467}");
println(`Three emoji that joiners make one are as wide as one emoji: ${joined === width("40px Ahem", "\u{1F468}")}`);
// OnlyA is Ahem, limited to "A" by its unicode-range. So, another font draws "B", though Ahem has a glyph for
// it. That holds after a joiner too, where the joiner follows a letter rather than an emoji.
const letters = "40px OnlyA, serif";
println(`Ahem doesn't draw a "B" by itself: ${width(letters, "B") !== 40}`);
const afterJoiner = width(letters, "A\u200DB") - width(letters, "A\u200D");
println(`Nor a "B" after an "A" and a joiner: ${Math.abs(afterJoiner - width(letters, "B")) < 0.01}`);
done();
});
</script>