|
| 1 | +/*! |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { getBuilder } from '@nextcloud/browser-storage' |
| 7 | + |
| 8 | +/** |
| 9 | + * Cool down time after the last attempt to clear the Flatpak font config cache preventing infinite loop in case of failed fix |
| 10 | + */ |
| 11 | +const FLATPAK_FONT_CONFIG_CACHE_CLEAR_COOL_DOWN = 24 * 60 * 60 * 1000 // 24h |
| 12 | + |
| 13 | +const LAST_FLATPAK_FONT_CONFIG_CACHE_CLEAR_KEY = 'lastFlatpakFontConfigCacheClear' |
| 14 | + |
| 15 | +/** |
| 16 | + * Ensure there is no emoji rendering issue in Flatpak installation resulting in emojis being rendered as text instead of colored. |
| 17 | + * If there is, clear the Flatpak font config cache and relaunch the app. |
| 18 | + */ |
| 19 | +export async function ensureFlatpakEmojiFontRendering() { |
| 20 | + // Flatpak specific issue only |
| 21 | + if (!window.systemInfo.isFlatpak) { |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + // No issues - nothing to fix |
| 26 | + if (!hasEmojiRenderingIssue()) { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + // Prevent the relaunch loop when there is an issue but it is not solved by clearing the cache and relaunch |
| 31 | + const browserStorage = getBuilder('talk-desktop').persist().build() |
| 32 | + const lastFontconfigCacheClear = browserStorage.getItem(LAST_FLATPAK_FONT_CONFIG_CACHE_CLEAR_KEY) |
| 33 | + |
| 34 | + if (lastFontconfigCacheClear && (Date.now() - parseInt(lastFontconfigCacheClear)) < FLATPAK_FONT_CONFIG_CACHE_CLEAR_COOL_DOWN) { |
| 35 | + console.warn('Emoji rendering issue detected, but font config cache was cleared recently. Probably the issue is not solvable by clearing the cache...') |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + browserStorage.setItem(LAST_FLATPAK_FONT_CONFIG_CACHE_CLEAR_KEY, Date.now().toString()) |
| 40 | + |
| 41 | + await window.TALK_DESKTOP.clearFlatpakFontConfigCache() |
| 42 | + await window.TALK_DESKTOP.relaunch() |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Check whether there is an emoji rendering issue resulting in emojis being rendered as text instead of colored images |
| 47 | + * by rendering an emoji on a canvas and checking how colorful it is. |
| 48 | + * The check cost is around 40ms. |
| 49 | + */ |
| 50 | +export function hasEmojiRenderingIssue(): boolean { |
| 51 | + const EMOJI = '😅' |
| 52 | + // Uncomment for testing of forced text emoji rendering |
| 53 | + // const EMOJI = '😅\uFE0E' |
| 54 | + |
| 55 | + // Same as in EmojiPicker |
| 56 | + const FONT_FAMILY = '"Segoe UI Emoji","Segoe UI Symbol","Segoe UI","Apple Color Emoji","Twemoji Mozilla","Noto Color Emoji","EmojiOne Color","Android Emoji"' |
| 57 | + const FONT_SIZE = 15 |
| 58 | + const WIDTH = 20 |
| 59 | + const HEIGHT = 20 |
| 60 | + |
| 61 | + /** |
| 62 | + * How much colored an emoji must be to consider it successfully colored. |
| 63 | + * On testing, monochrome text emoji is always 0.0 and colored emoji is usually 0.4..0.6 with bright yellow Noto Color Emojis |
| 64 | + * Only very gray emojis like 😶🌫️ has low chroma and it is still >0.11 |
| 65 | + */ |
| 66 | + const CHROMA_THRESHOLD = 0.1 |
| 67 | + |
| 68 | + const canvas = document.createElement('canvas') |
| 69 | + canvas.width = WIDTH |
| 70 | + canvas.height = HEIGHT |
| 71 | + // Uncomment for debugging |
| 72 | + // document.body.appendChild(canvas) |
| 73 | + |
| 74 | + const ctx = canvas.getContext('2d')! |
| 75 | + ctx.fillStyle = '#000000' |
| 76 | + ctx.font = `${FONT_SIZE}px ${FONT_FAMILY}` |
| 77 | + ctx.textAlign = 'center' |
| 78 | + ctx.fillText(EMOJI, WIDTH / 2, FONT_SIZE, WIDTH) |
| 79 | + |
| 80 | + const { data } = ctx.getImageData(0, 0, WIDTH, HEIGHT) |
| 81 | + |
| 82 | + const chroma = imageChroma(data) |
| 83 | + console.debug('Flatpak emoji rendering test chroma:', chroma) |
| 84 | + |
| 85 | + return chroma < CHROMA_THRESHOLD |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Calculates the average chroma of the given pixel data, ignoring transparent parts |
| 90 | + * |
| 91 | + * @param pixels - RGBA pixel image data |
| 92 | + */ |
| 93 | +function imageChroma(pixels: Uint8ClampedArray): number { |
| 94 | + let totalChroma = 0 |
| 95 | + let nonTransparentPixels = 0 |
| 96 | + for (let i = 0; i < pixels.length; i += 4) { |
| 97 | + const [r, g, b, a] = [pixels[i]!, pixels[i + 1]!, pixels[i + 2]!, pixels[i + 3]!] |
| 98 | + if (a === 0) { |
| 99 | + continue |
| 100 | + } |
| 101 | + nonTransparentPixels += 1 |
| 102 | + totalChroma += Math.max(r, g, b) - Math.min(r, g, b) |
| 103 | + } |
| 104 | + return totalChroma / nonTransparentPixels / 255 |
| 105 | +} |
0 commit comments