2019-05-22 01:31:24 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Based on...
|
|
|
|
* ChromaCheck 1.16
|
|
|
|
* author Roel Nieskens, https://pixelambacht.nl
|
|
|
|
* MIT license
|
|
|
|
*/
|
|
|
|
|
2019-05-29 09:59:50 +00:00
|
|
|
function safariVersionCheck(ua) {
|
2019-05-29 10:03:38 +00:00
|
|
|
try {
|
|
|
|
const safariVersionMatch = ua.match(/Mac OS X ([\d|_]+).*Version\/([\d|\.]+) Safari/);
|
|
|
|
if (safariVersionMatch) {
|
|
|
|
const macOSVersionStr = safariVersionMatch[1];
|
|
|
|
const safariVersionStr = safariVersionMatch[2];
|
|
|
|
const macOSVersion = macOSVersionStr.split("_").map(n => parseInt(n, 10));
|
|
|
|
const safariVersion = safariVersionStr.split(".").map(n => parseInt(n, 10));
|
|
|
|
const colrFontSupported = macOSVersion[0] >= 10 && macOSVersion[1] >= 14 && safariVersion[0] >= 12;
|
|
|
|
// https://www.colorfonts.wtf/ states safari supports COLR fonts from this version on
|
|
|
|
console.log(`Browser is Safari - requiring macOS 10.14 and Safari 12,` +
|
|
|
|
`detected Safari ${safariVersionStr} on macOS ${macOSVersionStr},` +
|
2019-05-29 10:08:59 +00:00
|
|
|
`COLR supported: ${colrFontSupported}`);
|
2019-05-29 10:03:38 +00:00
|
|
|
return colrFontSupported;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Couldn't determine Safari version to check COLR font support, assuming no.", err);
|
2019-05-29 09:59:50 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-22 01:31:24 +00:00
|
|
|
async function isColrFontSupported() {
|
2019-05-24 16:36:07 +00:00
|
|
|
console.log("Checking for COLR support");
|
|
|
|
|
2019-05-22 09:08:11 +00:00
|
|
|
// Firefox has supported COLR fonts since version 26
|
2019-05-29 08:11:14 +00:00
|
|
|
// but doesn't support the check below without
|
|
|
|
// "Extract canvas data" permissions
|
|
|
|
// when content blocking is enabled.
|
2019-05-22 09:08:11 +00:00
|
|
|
if (navigator.userAgent.includes("Firefox")) {
|
2019-05-24 16:36:07 +00:00
|
|
|
console.log("Browser is Firefox - assuming COLR is supported");
|
2019-05-29 08:11:14 +00:00
|
|
|
return true;
|
2019-05-22 09:08:11 +00:00
|
|
|
}
|
2019-05-29 09:59:50 +00:00
|
|
|
// Safari doesn't wait for the font to load (if it doesn't have it in cache)
|
|
|
|
// to emit the load event on the image, so there is no way to not make the check
|
|
|
|
// reliable. Instead sniff the version.
|
|
|
|
if (navigator.userAgent.includes("Safari")) {
|
|
|
|
return safariVersionCheck(navigator.userAgent);
|
|
|
|
}
|
2019-05-22 09:08:11 +00:00
|
|
|
|
2019-05-22 01:31:24 +00:00
|
|
|
try {
|
2019-05-22 01:41:07 +00:00
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
const img = new Image();
|
|
|
|
// eslint-disable-next-line
|
|
|
|
const fontCOLR = 'd09GRgABAAAAAAKAAAwAAAAAAowAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABDT0xSAAACVAAAABYAAAAYAAIAJUNQQUwAAAJsAAAAEgAAABLJAAAQT1MvMgAAAYAAAAA6AAAAYBfxJ0pjbWFwAAABxAAAACcAAAAsAAzpM2dseWYAAAH0AAAAGgAAABoNIh0kaGVhZAAAARwAAAAvAAAANgxLumdoaGVhAAABTAAAABUAAAAkCAEEAmhtdHgAAAG8AAAABgAAAAYEAAAAbG9jYQAAAewAAAAGAAAABgANAABtYXhwAAABZAAAABsAAAAgAg4AHW5hbWUAAAIQAAAAOAAAAD4C5wsecG9zdAAAAkgAAAAMAAAAIAADAAB4AWNgZGAAYQ5+qdB4fpuvDNIsDCBwaQGTAIi+VlscBaJZGMDiHAxMIAoAtjIF/QB4AWNgZGBgYQACOAkUQQWMAAGRABAAAAB4AWNgZGBgYGJgAdMMUJILJMQgAWICAAH3AC4AeAFjYGFhYJzAwMrAwDST6QwDA0M/hGZ8zWDMyMmAChgFkDgKQMBw4CXDSwYWEBdIYgAFBgYA/8sIdAAABAAAAAAAAAB4AWNgYGBkYAZiBgYeBhYGBSDNAoRA/kuG//8hpDgjWJ4BAFVMBiYAAAAAAAANAAAAAQAAAAAEAAQAAAMAABEhESEEAPwABAD8AAAAeAEtxgUNgAAAAMHHIQTShTlOAty9/4bf7AARCwlBNhBw4L/43qXjYGUmf19TMuLcj/BJL3XfBg54AWNgZsALAAB9AAR4AWNgYGAEYj4gFgGygGwICQACOwAoAAAAAAABAAEAAQAAAA4AAAAAyP8AAA==';
|
|
|
|
const svg = `
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="100" style="background:#fff;fill:#000;">
|
|
|
|
<style type="text/css">
|
|
|
|
@font-face {
|
|
|
|
font-family: "chromacheck-colr";
|
|
|
|
src: url(data:application/x-font-woff;base64,${fontCOLR}) format("woff");
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<text x="0" y="0" font-size="20">
|
|
|
|
<tspan font-family="chromacheck-colr" x="0" dy="20"></tspan>
|
|
|
|
</text>
|
|
|
|
</svg>`;
|
|
|
|
canvas.width = 20;
|
2019-05-22 01:31:24 +00:00
|
|
|
canvas.height = 100;
|
|
|
|
|
|
|
|
img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
|
|
|
|
|
2019-05-29 08:11:14 +00:00
|
|
|
console.log("Waiting for COLR SVG to load");
|
|
|
|
await new Promise(resolve => img.onload = resolve);
|
2019-05-24 16:36:07 +00:00
|
|
|
console.log("Drawing canvas to detect COLR support");
|
2019-05-22 01:31:24 +00:00
|
|
|
context.drawImage(img, 0, 0);
|
2019-05-29 08:11:14 +00:00
|
|
|
const colrFontSupported = (context.getImageData(10, 10, 1, 1).data[0] === 200);
|
2019-05-24 16:36:07 +00:00
|
|
|
console.log("Canvas check revealed COLR is supported? " + colrFontSupported);
|
2019-05-29 08:11:14 +00:00
|
|
|
return colrFontSupported;
|
2019-05-22 01:41:07 +00:00
|
|
|
} catch (e) {
|
2019-05-24 16:36:07 +00:00
|
|
|
console.error("Couldn't load COLR font", e);
|
2019-05-29 08:11:14 +00:00
|
|
|
return false;
|
2019-05-22 01:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:11:14 +00:00
|
|
|
let colrFontCheckStarted = false;
|
2019-05-22 01:31:24 +00:00
|
|
|
export async function fixupColorFonts() {
|
2019-05-29 08:11:14 +00:00
|
|
|
if (colrFontCheckStarted) {
|
2019-05-22 01:31:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-05-29 08:11:14 +00:00
|
|
|
colrFontCheckStarted = true;
|
2019-05-22 01:31:24 +00:00
|
|
|
|
|
|
|
if (await isColrFontSupported()) {
|
2019-05-22 15:22:18 +00:00
|
|
|
const path = `url('${require("../../res/fonts/Twemoji_Mozilla/TwemojiMozilla-colr.woff2")}')`;
|
|
|
|
document.fonts.add(new FontFace("Twemoji", path, {}));
|
2019-05-22 15:37:52 +00:00
|
|
|
// For at least Chrome on Windows 10, we have to explictly add extra
|
|
|
|
// weights for the emoji to appear in bold messages, etc.
|
2019-05-22 15:22:18 +00:00
|
|
|
document.fonts.add(new FontFace("Twemoji", path, { weight: 600 }));
|
|
|
|
document.fonts.add(new FontFace("Twemoji", path, { weight: 700 }));
|
2019-05-22 01:31:24 +00:00
|
|
|
}
|
2019-05-22 11:06:04 +00:00
|
|
|
// if not supported, the browser will fall back to one of the native fonts specified.
|
2019-05-22 01:31:24 +00:00
|
|
|
}
|
|
|
|
|