Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

3.2 KB
Raw Blame
import { FLAGS_GROUP, type EmojiData, type EmojiEntry } from "./emojiPicker";

// Mirrors .font-emoji in index.css: the support probe must draw with the same
// fonts the grid renders with.
export const EMOJI_FONT_FAMILY =
  '"Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif';

type Loader = () => Promise<{ default: EmojiData }>;

// Generated by scripts/sync-emoji-data.js. Each locale is its own chunk so only
// the active language is loaded; locales without a dataset (ar) fall back to en.
const LOADERS: Record<string, Loader> = {
  de: () => import("../data/emoji/de.json"),
  en: () => import("../data/emoji/en.json"),
  es: () => import("../data/emoji/es.json"),
  fr: () => import("../data/emoji/fr.json"),
  it: () => import("../data/emoji/it.json"),
  ja: () => import("../data/emoji/ja.json"),
  pt: () => import("../data/emoji/pt.json"),
  ru: () => import("../data/emoji/ru.json"),
  "zh-CN": () => import("../data/emoji/zh-CN.json"),
  "zh-TW": () => import("../data/emoji/zh-TW.json"),
};

export async function loadEmojiData(language: string): Promise<EmojiData> {
  const load = LOADERS[language] ?? LOADERS[language.split("-")[0]] ?? LOADERS.en;
  return (await load()).default;
}

const PROBE_SIZE = 32;
let supportedVersion: number | undefined;

// Highest Emoji version the platform's colour emoji font can draw. One probe per
// version is rendered onto a canvas: unsupported emoji come out as monochrome
// tofu or split into several glyphs, so a single coloured glyph means support.
export function getSupportedEmojiVersion(entries: EmojiEntry[]): number {
  if (supportedVersion === undefined) {
    const canvas = document.createElement("canvas");
    canvas.width = canvas.height = PROBE_SIZE;
    const context = canvas.getContext("2d", { willReadFrequently: true });
    supportedVersion = context ? detectSupportedVersion(context, entries) : Infinity;
  }
  return supportedVersion;
}

function detectSupportedVersion(context: CanvasRenderingContext2D, entries: EmojiEntry[]): number {
  context.font = `${PROBE_SIZE * 0.75}px ${EMOJI_FONT_FAMILY}`;
  context.textBaseline = "top";
  const glyphWidth = context.measureText("😀").width;
  const versions = [...new Set(entries.map((entry) => entry.version))].sort((a, b) => b - a);
  for (const version of versions) {
    // Flags render as letter pairs on Windows, which would fail an otherwise fine version.
    const probe = entries.find((entry) => entry.version === version && entry.group !== FLAGS_GROUP);
    if (probe && rendersAsColorGlyph(context, probe.emoji, glyphWidth)) return version;
  }
  // No colour emoji font at all (a bare Linux install): hide nothing.
  return Infinity;
}

function rendersAsColorGlyph(
  context: CanvasRenderingContext2D,
  emoji: string,
  glyphWidth: number
): boolean {
  if (context.measureText(emoji).width > glyphWidth * 1.2) return false;
  context.clearRect(0, 0, PROBE_SIZE, PROBE_SIZE);
  context.fillText(emoji, 0, 0);
  const { data } = context.getImageData(0, 0, PROBE_SIZE, PROBE_SIZE);
  for (let i = 0; i < data.length; i += 4) {
    const saturation =
      Math.max(data[i], data[i + 1], data[i + 2]) - Math.min(data[i], data[i + 1], data[i + 2]);
    if (data[i + 3] > 0 && saturation > 40) return true;
  }
  return false;
}