Skip to content

AI-native design editor. Open-source Figma alternative.

fix: draw emoji in text and lay them out 1 em wide, as Figma does (#1277)

Danila Poyarkov55ece1ce3de7
diff --git a/bun.lock b/bun.lock
index 868e38196..fbd72eeb3 100644
--- a/bun.lock
+++ b/bun.lock
@@ -719,6 +719,12 @@
"typescript": "~5.8.3",
},
},
+ "tools/generate/emoji-metrics": {
+ "name": "@open-pencil/emoji-metrics-tools",
+ "devDependencies": {
+ "@open-pencil/package-artifacts-tools": "workspace:*",
+ },
+ },
"tools/generate/landing-posters": {
"name": "@open-pencil/landing-posters-tools",
"dependencies": {
@@ -1587,6 +1593,8 @@
"@open-pencil/emit": ["@open-pencil/emit@workspace:packages/emit"],
+ "@open-pencil/emoji-metrics-tools": ["@open-pencil/emoji-metrics-tools@workspace:tools/generate/emoji-metrics"],
+
"@open-pencil/feedback": ["@open-pencil/feedback@workspace:packages/feedback"],
"@open-pencil/fig": ["@open-pencil/fig@workspace:packages/fig"],
diff --git a/changes/1277/fixed.md b/changes/1277/fixed.md
new file mode 100644
index 000000000..ec52f8e42
--- /dev/null
+++ b/changes/1277/fixed.md
@@ -0,0 +1,4 @@
+- Emoji in text from Figma draw as emoji instead of squares in the text's colour, and keep drawing in Figma after a save; the browser and desktop app use the system's emoji font, and the CLI uses Noto Color Emoji.
+- The text caret, arrow keys, Backspace, and Delete treat an emoji as one character, and emoji in edited or typed text take the same width as in Figma and draw as emoji instead of missing-glyph boxes.
+- Gradient-filled text from Figma spreads its gradient across the layer as Figma does, instead of showing little more than its first colour.
+- The text caret takes the colour of the character before it when that character has one solid fill, as in Figma, so it shows on light text over dark backgrounds; with a gradient, several fills, or none it stays black.
diff --git a/package.json b/package.json
index 5c85dec9c..81ba02307 100644
--- a/package.json
+++ b/package.json
@@ -40,6 +40,7 @@
"tools/generate/authoring-reference",
"tools/generate/brand",
"tools/generate/demo",
+ "tools/generate/emoji-metrics",
"tools/generate/landing-posters",
"tools/generate/tauri-menu",
"tools/generate/visual-oracles",
@@ -109,6 +110,7 @@
"docs:build:production": "bun --filter @open-pencil/docs build:production",
"docs:preview": "bun --filter @open-pencil/docs preview",
"generate:demo": "bun tools/generate/demo/src/cli.ts --force",
+ "generate:emoji-metrics": "bun tools/generate/emoji-metrics/src/generate.ts",
"generate:landing-posters": "bun tools/generate/landing-posters/src/generate.ts --force",
"generate:icons": "bun tools/generate/brand/src/cli.ts generate",
"check:icons": "bun tools/generate/brand/src/cli.ts check",
diff --git a/packages/core/AGENTS.md b/packages/core/AGENTS.md
index 42627d738..b7d5fe58d 100644
--- a/packages/core/AGENTS.md
+++ b/packages/core/AGENTS.md
@@ -83,6 +83,7 @@ Canvas is CanvasKit (Skia WASM) on a WebGL surface, not DOM.
- Set fill types on the detached `Path`, never on a `PathBuilder`: in CanvasKit 0.41 `PathBuilder.setFillType` returns an undeleted copy of the builder (`packages/core/src/vector/index.ts`).
- Paragraph construction is typed against `packages/core/src/canvas/text/paragraph/inputs.ts`; the same inputs drive preparation-cache invalidation. Add a mutation case when extending that contract. Drawing borrows native paragraphs; the renderer owns their bounded cache and destruction.
- Text lays out through `buildParagraph` into a `TextLayout` (`packages/core/src/canvas/text/layout/`): one native paragraph for plain text, or one per paragraph with its list indent, marker, and spacing, since Skia has no hanging indent. Drawing, measurement, the text editor, saved glyphs (`shapeText`), and text outlines all read that layout rather than laying text out again (`packages/core/tests/canvas/text/lists.test.ts`).
+- Emoji are 1 em squares with their image drawn over, once per text and apart from the fills, as Figma does: `drawSavedEmoji` over a saved glyph's placeholder square (`emojiCodePoints`), `drawLaidOutEmoji` over the blank square the layout gives a character it falls back to `OpenPencil Emoji Metrics` for, the last fallback family, generated by `bun run generate:emoji-metrics`. The image comes from the browser's emoji font through a 2D canvas, or from Noto Color Emoji that `prepareGraphFonts` loads where nothing draws platform emoji (`tests/e2e/canvas/saved-glyph-visual.spec.ts`).
### Text decorations
diff --git a/packages/core/assets/OpenPencilEmojiMetrics-OFL.txt b/packages/core/assets/OpenPencilEmojiMetrics-OFL.txt
new file mode 100644
index 000000000..973095528
--- /dev/null
+++ b/packages/core/assets/OpenPencilEmojiMetrics-OFL.txt
@@ -0,0 +1,87 @@
+Copyright 2021 Google Inc. All Rights Reserved.
+
+This Font Software is licensed under the SIL Open Font License, Version 1.1.
+This license is copied below, and is also available with a FAQ at:
+http://scripts.sil.org/OFL
+
+---------------------------------------------------------------------------
+SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
+---------------------------------------------------------------------------
+
+PREAMBLE
+
+The goals of the Open Font License (OFL) are to stimulate worldwide development
+of collaborative font projects, to support the font creation efforts of academic
+and linguistic communities, and to provide a free and open framework in which
+fonts may be shared and improved in partnership with others.
+
+The OFL allows the licensed fonts to be used, studied, modified and redistributed
+freely as long as they are not sold by themselves. The fonts, including any
+derivative works, can be bundled, embedded, redistributed and/or sold with any
+software provided that any reserved names are not used by derivative works. The
+fonts and derivatives, however, cannot be released under any other type of license.
+The requirement for fonts to remain under this license does not apply to any
+document created using the fonts or their derivatives.
+
+DEFINITIONS
+
+"Font Software" refers to the set of files released by the Copyright Holder(s) under
+this license and clearly marked as such. This may include source files, build
+scripts and documentation.
+
+"Reserved Font Name" refers to any names specified as such after the copyright
+statement(s).
+
+"Original Version" refers to the collection of Font Software components as
+distributed by the Copyright Holder(s).
+
+"Modified Version" refers to any derivative made by adding to, deleting, or
+substituting -- in part or in whole -- any of the components of the Original Version,
+by changing formats or by porting the Font Software to a new environment.
+
+"Author" refers to any designer, engineer, programmer, technical writer or other
+person who contributed to the Font Software.
+
+PERMISSION & CONDITIONS
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of the
+Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell
+modified and unmodified copies of the Font Software, subject to the following
+conditions:
+
+1) Neither the Font Software nor any of its individual components, in Original or
+Modified Versions, may be sold by itself.
+
+2) Original or Modified Versions of the Font Software may be bundled, redistributed
+and/or sold with any software, provided that each copy contains the above copyright
+notice and this license. These can be included either as stand-alone text files,
+human-readable headers or in the appropriate machine-readable metadata fields within
+text or binary files as long as those fields can be easily viewed by the user.
+
+3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless
+explicit written permission is granted by the corresponding Copyright Holder. This
+restriction only applies to the primary font name as presented to the users.
+
+4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall
+not be used to promote, endorse or advertise any Modified Version, except to
+acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with
+their explicit written permission.
+
+5) The Font Software, modified or unmodified, in part or in whole, must be distributed
+entirely under this license, and must not be distributed under any other license. The
+requirement for fonts to remain under this license does not apply to any document
+created using the Font Software.
+
+TERMINATION
+
+This license becomes null and void if any of the above conditions are not met.
+
+DISCLAIMER
+
+THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER
+RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR
+INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
diff --git a/packages/core/assets/OpenPencilEmojiMetrics-Regular.ttf b/packages/core/assets/OpenPencilEmojiMetrics-Regular.ttf
new file mode 100644
index 000000000..7175fa35c
Binary files /dev/null and b/packages/core/assets/OpenPencilEmojiMetrics-Regular.ttf differ
diff --git a/packages/core/src/canvas/overlays/text-edit.ts b/packages/core/src/canvas/overlays/text-edit.ts
index 3e108bcba..f740a127d 100644
--- a/packages/core/src/canvas/overlays/text-edit.ts
+++ b/packages/core/src/canvas/overlays/text-edit.ts
@@ -1,13 +1,31 @@
import type { Canvas } from 'canvaskit-wasm'
-import type { SceneNode } from '@open-pencil/scene-graph'
+import type { Color, SceneNode } from '@open-pencil/scene-graph'
import type { SkiaRenderer } from '#core/canvas/renderer'
import { TEXT_CARET_COLOR, TEXT_CARET_WIDTH, TEXT_SELECTION_ALPHA } from '#core/constants'
import type { TextEditor } from '#core/text/editor'
+import { previousGraphemeBoundary } from '#core/text/graphemes'
import { withScreenStroke } from './outline'
+/**
+ * The caret's colour at `index`, as Figma draws it: the fill of the character before the caret,
+ * or of the first at the text's start, when that is one visible solid fill, with its opacity;
+ * black for a gradient, several fills, or none. The layer's opacity leaves it be. Measured in
+ * Figma desktop.
+ */
+export function textCaretColor(node: SceneNode, index: number): Color {
+ const character = index > 0 ? previousGraphemeBoundary(node.text, index) : 0
+ const run = node.styleRuns.find(
+ (item) => character >= item.start && character < item.start + item.length
+ )
+ const visible = (run?.style.fills ?? node.fills).filter((fill) => fill.visible)
+ const [fill] = visible
+ if (visible.length !== 1 || fill.type !== 'SOLID') return TEXT_CARET_COLOR
+ return { ...fill.color, a: fill.color.a * fill.opacity }
+}
+
export function drawTextEditOverlay(
r: SkiaRenderer,
canvas: Canvas,
@@ -39,9 +57,8 @@ export function drawTextEditOverlay(
if (editor.caretVisible && !editor.hasSelection()) {
const caret = editor.getCaretRect()
if (caret) {
- r.auxFill.setColor(
- r.ck.Color4f(TEXT_CARET_COLOR.r, TEXT_CARET_COLOR.g, TEXT_CARET_COLOR.b, TEXT_CARET_COLOR.a)
- )
+ const color = textCaretColor(node, editor.caretIndex ?? 0)
+ r.auxFill.setColor(r.ck.Color4f(color.r, color.g, color.b, color.a))
const w = TEXT_CARET_WIDTH / r.zoom
canvas.drawRect(
r.ck.LTRBRect(caret.x - w / 2, caret.y0, caret.x + w / 2, caret.y1),
diff --git a/packages/core/src/canvas/renderer.ts b/packages/core/src/canvas/renderer.ts
index 749ec1a0d..491166e40 100644
--- a/packages/core/src/canvas/renderer.ts
+++ b/packages/core/src/canvas/renderer.ts
@@ -42,6 +42,7 @@ import * as RendererState from './renderer/state'
import * as RenderText from './text'
import type { SavedDecorationCache } from './text/decorations'
import { createDerivedGlyphPathCache, createGlyphSilhouetteCache } from './text/derived'
+import { createEmojiImageCache } from './text/emoji'
import { TextPreparationCache } from './text/preparation/cache'
export type {
MeasurementMode,
@@ -149,6 +150,8 @@ export class SkiaRenderer {
glyphSilhouetteCache = createGlyphSilhouetteCache()
derivedGlyphPathCache = createDerivedGlyphPathCache()
derivedDecorationCache: SavedDecorationCache = new WeakMap()
+ /** Images of the emoji saved text draws, by what drew them and the emoji. */
+ emojiImageCache = createEmojiImageCache()
renderingSceneBacking = false
scenePicture: SkPicture | null = null
scenePictureVersion = -1
diff --git a/packages/core/src/canvas/renderer/fonts.ts b/packages/core/src/canvas/renderer/fonts.ts
index 28cf01638..3764865e1 100644
--- a/packages/core/src/canvas/renderer/fonts.ts
+++ b/packages/core/src/canvas/renderer/fonts.ts
@@ -13,6 +13,7 @@ import {
VALUE_FONT_SIZE
} from '#core/constants'
import { withTextMeasurer } from '#core/layout/text-measurement'
+import { EMOJI_METRICS_FAMILY } from '#core/text/emoji'
import { fontManager } from '#core/text/fonts'
import { prepareGraphFonts } from '#core/text/prepare'
import {
@@ -125,7 +126,9 @@ export async function loadFonts(
const [fontData, titleData] = await Promise.all([
fontManager.loadFont(DEFAULT_FONT_FAMILY, 'Regular'),
- fontManager.fetchBundledFont(SECTION_TITLE_FONT_URL).catch(() => null)
+ fontManager.fetchBundledFont(SECTION_TITLE_FONT_URL).catch(() => null),
+ // Without it emoji only take the text font's missing-glyph width; text still draws.
+ fontManager.loadFont(EMOJI_METRICS_FAMILY, 'Regular').catch(() => null)
])
if (r.isDestroyed()) return
if (titleData) registerSectionTitleFont(r, titleData)
diff --git a/packages/core/src/canvas/renderer/lifecycle.ts b/packages/core/src/canvas/renderer/lifecycle.ts
index 04f579819..f635bbbc8 100644
--- a/packages/core/src/canvas/renderer/lifecycle.ts
+++ b/packages/core/src/canvas/renderer/lifecycle.ts
@@ -14,6 +14,7 @@ function disposePathCaches(r: SkiaRenderer): void {
r.nodePaths.clear()
r.glyphSilhouetteCache.clear()
r.derivedGlyphPathCache.clear()
+ r.emojiImageCache.clear()
}
/** Frees every live image `r`'s surface holds and forgets those an earlier surface held. */
diff --git a/packages/core/src/canvas/scene.ts b/packages/core/src/canvas/scene.ts
index d3f72ccf4..b0e1496d3 100644
--- a/packages/core/src/canvas/scene.ts
+++ b/packages/core/src/canvas/scene.ts
@@ -16,6 +16,7 @@ import type { Color } from '@open-pencil/scene-graph/primitives'
import { createSceneGeometry, nodeOrientationMatrix, projectedNode } from '#core/geometry'
import { shownText } from '#core/text/case'
+import { mayHoldEmoji } from '#core/text/emoji'
import { fontManager } from '#core/text/fonts'
import {
addStrokeRunToPath,
@@ -53,8 +54,10 @@ import {
canDrawSavedText,
drawDerivedText,
drawReflowedPathTextSilhouettes,
+ drawsSavedText,
isReflowedPathText
} from './text/derived'
+import { drawLaidOutEmoji, drawSavedEmoji } from './text/emoji'
import { stickyPlaceholderNode } from './text/sticky-placeholder'
function drawVisibleFills(
@@ -921,6 +924,7 @@ export function renderShapeUncached(
if (!drawVectorMultiStyleFills(r, canvas, node, graph)) {
drawVisibleFills(r, node, graph, (fill) => r.drawNodeFill(canvas, node, rect, hasRadius, fill))
}
+ if (node.type === 'TEXT') drawTextEmoji(r, canvas, node)
if (!pathTextStrokeFirst && !reflowedPathText) {
paintNodeStrokes(r, canvas, node, graph, rect, hasRadius, sg, vectorPaths, vectorStroke)
@@ -962,16 +966,46 @@ function drawResolvedPathText(
r: SkiaRenderer,
canvas: Canvas,
node: SceneNode,
- fontReadiness: ReturnType<SkiaRenderer['nodeFontReadiness']>
+ fontReadiness: ReturnType<SkiaRenderer['nodeFontReadiness']>,
+ fill?: Fill
): boolean {
return (
// Resolving the exact face must not replace curved glyph placement with a straight paragraph.
- fontReadiness !== 'exhausted' && node.textPathData !== null && drawDerivedText(r, canvas, node)
+ fontReadiness !== 'exhausted' &&
+ node.textPathData !== null &&
+ drawDerivedText(r, canvas, node, fill)
)
}
+/** Whether the text is drawn from the editor's own layout, which leaves its emoji to be drawn. */
+function drawsLaidOutText(r: SkiaRenderer, node: SceneNode): boolean {
+ if (!r.fontsLoaded || !r.fontProvider || node.textPathData !== null) return false
+ const readiness = r.nodeFontReadiness(node)
+ return readiness !== 'pending' && readiness !== 'exhausted' && mayHoldEmoji(shownText(node))
+}
+
+/**
+ * The emoji of a text, once whatever fills it has, clipped as its glyphs are: over its saved
+ * glyphs' placeholder squares, or over the blank squares the editor's layout gives them.
+ */
+function drawTextEmoji(r: SkiaRenderer, canvas: Canvas, node: SceneNode): void {
+ const saved = drawsSavedText(node)
+ if (!saved && !drawsLaidOutText(r, node)) return
+ canvas.save()
+ if (shouldClipTextToLayoutBox(node)) {
+ canvas.clipRect(r.ck.LTRBRect(0, 0, node.width, node.height), r.ck.ClipOp.Intersect, false)
+ }
+ if (saved) drawSavedEmoji(r, canvas, node)
+ else {
+ withTextParagraph(r, node, r.ck.BLACK, { halfLeading: true }, (paragraph) =>
+ drawLaidOutEmoji(r, canvas, node, paragraph, textVerticalOffset(node, paragraph.getHeight()))
+ )
+ }
+ canvas.restore()
+}
+
function drawSavedText(r: SkiaRenderer, canvas: Canvas, node: SceneNode, fill?: Fill): boolean {
- return canDrawSavedText(node, fill) && drawDerivedText(r, canvas, node)
+ return canDrawSavedText(node, fill) && drawDerivedText(r, canvas, node, fill)
}
export function renderText(r: SkiaRenderer, canvas: Canvas, node: SceneNode, fill?: Fill): void {
@@ -992,7 +1026,7 @@ export function renderText(r: SkiaRenderer, canvas: Canvas, node: SceneNode, fil
canvas.restore()
return
}
- if (drawResolvedPathText(r, canvas, node, fontReadiness)) {
+ if (drawResolvedPathText(r, canvas, node, fontReadiness, fill)) {
canvas.restore()
return
}
@@ -1006,7 +1040,7 @@ export function renderText(r: SkiaRenderer, canvas: Canvas, node: SceneNode, fil
return
}
}
- if (drawDerivedText(r, canvas, node)) {
+ if (drawDerivedText(r, canvas, node, fill)) {
canvas.restore()
return
}
diff --git a/packages/core/src/canvas/text/derived.ts b/packages/core/src/canvas/text/derived.ts
index 5920cab9a..eb6512f24 100644
--- a/packages/core/src/canvas/text/derived.ts
+++ b/packages/core/src/canvas/text/derived.ts
@@ -12,6 +12,7 @@ import {
type Stroke,
type TextParagraphRange
} from '@open-pencil/scene-graph'
+import { isGradientFill } from '@open-pencil/scene-graph/gradient'
import type { ResourceCache } from '#core/cache/resource'
import { createPathCache } from '#core/canvas/paths/cache'
@@ -138,16 +139,42 @@ function getGlyphSilhouette(
* font-units→px (Y-flipped). The fill and silhouette passes share it so they
* stay registered; see drawDerivedText for the order rationale.
*/
-function applyGlyphEmTransform(canvas: Canvas, glyph: DerivedTextGlyph, glyphY: number): void {
+/** Moves to a saved glyph's pen position, in pixels: its baseline, resize scale, and rotation. */
+export function applyGlyphTransform(canvas: Canvas, glyph: DerivedTextGlyph, glyphY: number): void {
canvas.translate(glyph.x, glyphY)
const scaleX = glyph.scaleX ?? 1
const scaleY = glyph.scaleY ?? 1
if (scaleX !== 1 || scaleY !== 1) canvas.scale(scaleX, scaleY)
const rotation = glyph.rotation ?? 0
if (rotation !== 0) canvas.rotate((-rotation * 180) / Math.PI, 0, 0)
+}
+
+function applyGlyphEmTransform(canvas: Canvas, glyph: DerivedTextGlyph, glyphY: number): void {
+ applyGlyphTransform(canvas, glyph, glyphY)
canvas.scale(glyph.fontSize, -glyph.fontSize)
}
+/** The outline moved by the transform `applyGlyphEmTransform` applies to the canvas. */
+function layerSpaceGlyphPath(
+ r: SkiaRenderer,
+ path: Path,
+ glyph: DerivedTextGlyph,
+ glyphY: number
+): Path {
+ const { Matrix } = r.ck
+ const builder = new r.ck.PathBuilder()
+ builder.addPath(
+ path,
+ Matrix.multiply(
+ Matrix.translated(glyph.x, glyphY),
+ Matrix.scaled(glyph.scaleX ?? 1, glyph.scaleY ?? 1),
+ Matrix.rotated(-(glyph.rotation ?? 0)),
+ Matrix.scaled(glyph.fontSize, -glyph.fontSize)
+ )
+ )
+ return builder.detachAndDelete()
+}
+
/**
* Paint per-glyph stroke silhouettes under a reflowed path-text node.
* Transform chain mirrors drawDerivedText exactly so silhouettes and
@@ -171,7 +198,7 @@ export function drawReflowedPathTextSilhouettes(
paint.setAlphaf(stroke.opacity)
try {
for (const glyph of glyphs) {
- if (glyph.fontSize <= 0) continue
+ if (glyph.fontSize <= 0 || glyph.emojiCodePoints) continue
const silhouette = getGlyphSilhouette(r, glyph, stroke)
canvas.save()
applyGlyphEmTransform(
@@ -260,12 +287,53 @@ function savedTextEligibility(node: SceneNode): boolean {
)
}
+/** Solid and gradient fills paint saved glyph outlines as they paint shapes; images do not. */
+function paintsSavedGlyphs(fill: Fill): boolean {
+ return fill.type === 'SOLID' || isGradientFill(fill.type)
+}
+
export function canDrawSavedText(node: SceneNode, fill?: Fill): boolean {
- if (fill && fill.type !== 'SOLID') return false
+ if (fill && !paintsSavedGlyphs(fill)) return false
return savedTextEligibility(node)
}
-export function drawDerivedText(r: SkiaRenderer, canvas: Canvas, node: SceneNode): boolean {
+/**
+ * Whether the text is drawn from its saved glyphs whatever fills it has: every visible fill
+ * draws them, or it has none. The emoji among them are then drawn once, apart from the fills.
+ */
+export function drawsSavedText(node: SceneNode): boolean {
+ return (
+ (node.derivedTextGlyphs?.length ?? 0) > 0 &&
+ node.fills.every((fill) => !fill.visible || paintsSavedGlyphs(fill)) &&
+ savedTextEligibility(node)
+ )
+}
+
+/** One glyph's outline in its style run's solid fills, or in the layer's fill paint. */
+function drawGlyphOutline(r: SkiaRenderer, canvas: Canvas, path: Path, runFills?: Fill[]): void {
+ if (!runFills) {
+ canvas.drawPath(path, r.fillPaint)
+ return
+ }
+ const paint = r.fillPaint.copy()
+ try {
+ for (const fill of runFills) {
+ if (!fill.visible || fill.type !== 'SOLID') continue
+ const color = fill.color
+ paint.setColor(r.ck.Color4f(color.r, color.g, color.b, color.a * fill.opacity))
+ canvas.drawPath(path, paint)
+ }
+ } finally {
+ paint.delete()
+ }
+}
+
+export function drawDerivedText(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ node: SceneNode,
+ fill?: Fill
+): boolean {
if (!node.derivedTextGlyphs?.length) return false
// Pixel-snap is for horizontal Figma baselines only — on a curve it stair-steps
@@ -273,11 +341,17 @@ export function drawDerivedText(r: SkiaRenderer, canvas: Canvas, node: SceneNode
const snapBaselines = !hasRotatedDerivedGlyphs(node)
const savedTextEligible = savedTextEligibility(node)
const markerCharacters = markerStyleCharacters(node)
+ // A gradient spans the layer, so its glyphs are moved into layer space rather than drawn
+ // under an em-scaled canvas that would stretch the gradient by the font size.
+ const paintsLayerSpace = fill !== undefined && fill.type !== 'SOLID'
for (const [index, glyph] of node.derivedTextGlyphs.entries()) {
+ // An emoji's outline is a placeholder square; its image is drawn apart from the fills.
+ if (glyph.emojiCodePoints) continue
const glyphY = snapBaselines ? snapDerivedGlyphBaseline(glyph.y) : glyph.y
- const path = derivedGlyphPath(r, glyph.commandsBlob)
+ const emPath = derivedGlyphPath(r, glyph.commandsBlob)
+ const path = paintsLayerSpace ? layerSpaceGlyphPath(r, emPath, glyph, glyphY) : emPath
canvas.save()
- applyGlyphEmTransform(canvas, glyph, glyphY)
+ if (!paintsLayerSpace) applyGlyphEmTransform(canvas, glyph, glyphY)
const shouldUseHardCoverage = shouldUseHardDerivedGlyphCoverage(node)
if (shouldUseHardCoverage) r.fillPaint.setAntiAlias(false)
const character = glyph.firstCharacter ?? markerCharacters.get(index)
@@ -285,21 +359,9 @@ export function drawDerivedText(r: SkiaRenderer, canvas: Canvas, node: SceneNode
character === undefined
? undefined
: node.styleRuns.find((run) => character >= run.start && character < run.start + run.length)
- const fills = run?.style.fills
- if (fills && savedTextEligible) {
- const paint = r.fillPaint.copy()
- try {
- for (const fill of fills) {
- if (!fill.visible || fill.type !== 'SOLID') continue
- const color = fill.color
- paint.setColor(r.ck.Color4f(color.r, color.g, color.b, color.a * fill.opacity))
- canvas.drawPath(path, paint)
- }
- } finally {
- paint.delete()
- }
- } else canvas.drawPath(path, r.fillPaint)
+ drawGlyphOutline(r, canvas, path, savedTextEligible ? run?.style.fills : undefined)
if (shouldUseHardCoverage) r.fillPaint.setAntiAlias(true)
+ if (path !== emPath) path.delete()
canvas.restore()
}
diff --git a/packages/core/src/canvas/text/emoji/index.ts b/packages/core/src/canvas/text/emoji/index.ts
new file mode 100644
index 000000000..8e9d15d6d
--- /dev/null
+++ b/packages/core/src/canvas/text/emoji/index.ts
@@ -0,0 +1,142 @@
+import type { Canvas, Image, Paint } from 'canvaskit-wasm'
+
+import type { SceneNode } from '@open-pencil/scene-graph'
+
+import { ResourceCache } from '#core/cache/resource'
+import type { SkiaRenderer } from '#core/canvas/renderer'
+import {
+ applyGlyphTransform,
+ hasRotatedDerivedGlyphs,
+ snapDerivedGlyphBaseline
+} from '#core/canvas/text/derived'
+import type { TextLayout } from '#core/canvas/text/layout'
+import { characterFamily, laidOutGlyphs } from '#core/canvas/text/placed'
+import { shownText } from '#core/text/case'
+import {
+ canDrawPlatformEmoji,
+ EMOJI_METRICS_FAMILY,
+ emojiText,
+ mayHoldEmoji
+} from '#core/text/emoji'
+import { nextGraphemeBoundary } from '#core/text/graphemes'
+
+import { rasterizeNotoEmoji, rasterizePlatformEmoji } from './raster'
+
+/** Distinct emoji a renderer keeps drawn: one image each, at most a few hundred on a page. */
+const MAX_EMOJI_IMAGES = 512
+
+/**
+ * Emoji images by what draws them and the emoji they show. An emoji the source cannot draw is
+ * kept as `null`, so it is not drawn again each frame.
+ */
+export function createEmojiImageCache(): ResourceCache<string, Image | null> {
+ return new ResourceCache({ maxEntries: MAX_EMOJI_IMAGES, dispose: (image) => image?.delete() })
+}
+
+function rasterizeEmoji(r: SkiaRenderer, platform: boolean, text: string): Image | null {
+ if (platform) return rasterizePlatformEmoji(r.ck, text)
+ return r.fontProvider ? rasterizeNotoEmoji(r.ck, r.fontProvider, text) : null
+}
+
+function emojiImage(r: SkiaRenderer, text: string): Image | null {
+ const platform = canDrawPlatformEmoji()
+ // Noto draws only once its font is loaded, so a later font generation tries again.
+ const key = platform ? `platform:${text}` : `noto:${r.fontGeneration}:${text}`
+ const cached = r.emojiImageCache.get(key)
+ if (cached !== undefined) return cached
+ const image = rasterizeEmoji(r, platform, text)
+ r.emojiImageCache.set(key, image)
+ return image
+}
+
+/**
+ * Draws `image` over an emoji's square, 1 em wide from 0.07 em below the baseline at the
+ * canvas origin, as Figma places it.
+ */
+function drawEmojiSquare(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ image: Image,
+ size: number,
+ paint: Paint
+): void {
+ canvas.drawImageRectOptions(
+ image,
+ r.ck.LTRBRect(0, 0, image.width(), image.height()),
+ r.ck.LTRBRect(0, -0.93 * size, size, 0.07 * size),
+ r.ck.FilterMode.Linear,
+ r.ck.MipmapMode.Linear,
+ paint
+ )
+}
+
+function withEmojiPaint(r: SkiaRenderer, draw: (paint: Paint) => void): void {
+ const paint = new r.ck.Paint()
+ paint.setAntiAlias(true)
+ try {
+ draw(paint)
+ } finally {
+ paint.delete()
+ }
+}
+
+/**
+ * Draws the emoji among a text's saved glyphs. Figma saves an emoji as a placeholder square
+ * 1 em wide, from 0.07 em below the baseline, and draws the emoji's image over that square in
+ * its own colours: the text's fills never reach it, while the layer's opacity does.
+ */
+export function drawSavedEmoji(r: SkiaRenderer, canvas: Canvas, node: SceneNode): void {
+ const glyphs = node.derivedTextGlyphs
+ if (!glyphs?.some((glyph) => glyph.emojiCodePoints)) return
+ // Baselines snap as the saved glyphs around the emoji do.
+ const snapBaselines = !hasRotatedDerivedGlyphs(node)
+ withEmojiPaint(r, (paint) => {
+ for (const glyph of glyphs) {
+ if (!glyph.emojiCodePoints || glyph.fontSize <= 0) continue
+ const image = emojiImage(r, emojiText(glyph))
+ if (!image) continue
+ canvas.save()
+ applyGlyphTransform(
+ canvas,
+ glyph,
+ snapBaselines ? snapDerivedGlyphBaseline(glyph.y) : glyph.y
+ )
+ drawEmojiSquare(r, canvas, image, glyph.fontSize, paint)
+ canvas.restore()
+ }
+ })
+}
+
+/**
+ * Draws the emoji of text the editor laid out, `offsetY` below its top. The layout falls back
+ * to the emoji metrics font for a character no text font has, which gives it Figma's 1 em
+ * square and draws nothing in it; its image is drawn there as a saved emoji's is.
+ */
+export function drawLaidOutEmoji(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ node: SceneNode,
+ layout: TextLayout,
+ offsetY: number
+): void {
+ const text = shownText(node)
+ if (!mayHoldEmoji(text)) return
+ const glyphs = laidOutGlyphs(node, layout, text, offsetY)
+ if (!glyphs) return
+ withEmojiPaint(r, (paint) => {
+ // A sequence the font does not join is several glyphs; its image is drawn once, at its first.
+ let drawnUntil = 0
+ for (const glyph of glyphs) {
+ const start = glyph.firstCharacter
+ if (start === undefined || start < drawnUntil || glyph.fontSize <= 0) continue
+ if (characterFamily(node, text, start) !== EMOJI_METRICS_FAMILY) continue
+ drawnUntil = nextGraphemeBoundary(text, start)
+ const image = emojiImage(r, text.slice(start, drawnUntil))
+ if (!image) continue
+ canvas.save()
+ canvas.translate(glyph.x, glyph.y)
+ drawEmojiSquare(r, canvas, image, glyph.fontSize, paint)
+ canvas.restore()
+ }
+ })
+}
diff --git a/packages/core/src/canvas/text/emoji/raster.ts b/packages/core/src/canvas/text/emoji/raster.ts
new file mode 100644
index 000000000..9d688e5f1
--- /dev/null
+++ b/packages/core/src/canvas/text/emoji/raster.ts
@@ -0,0 +1,82 @@
+import type { CanvasKit, Image, TypefaceFontProvider } from 'canvaskit-wasm'
+
+import { NOTO_COLOR_EMOJI_FAMILY } from '#core/text/emoji'
+
+/** Emoji are drawn from square images this many pixels wide, as Figma draws its own. */
+export const EMOJI_IMAGE_SIZE = 160
+
+/**
+ * Where the platform's emoji font puts its pen in an image `EMOJI_IMAGE_SIZE` wide at that font
+ * size: the baseline this far down. Measured against Figma's Apple images, which Apple Color
+ * Emoji draws to within a pixel at this offset.
+ */
+const PLATFORM_BASELINE = 0.8775
+
+const PLATFORM_EMOJI_FONTS = '"Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji"'
+
+function imageFromPixels(ck: CanvasKit, pixels: Uint8Array | Uint8ClampedArray): Image | null {
+ return ck.MakeImage(
+ {
+ width: EMOJI_IMAGE_SIZE,
+ height: EMOJI_IMAGE_SIZE,
+ colorType: ck.ColorType.RGBA_8888,
+ alphaType: ck.AlphaType.Unpremul,
+ colorSpace: ck.ColorSpace.SRGB
+ },
+ pixels,
+ EMOJI_IMAGE_SIZE * 4
+ )
+}
+
+/** The emoji as the platform's own emoji font draws it, through the browser's 2D canvas. */
+export function rasterizePlatformEmoji(ck: CanvasKit, text: string): Image | null {
+ const canvas = new OffscreenCanvas(EMOJI_IMAGE_SIZE, EMOJI_IMAGE_SIZE)
+ const context = canvas.getContext('2d')
+ if (!context) return null
+ context.font = `${EMOJI_IMAGE_SIZE}px ${PLATFORM_EMOJI_FONTS}`
+ context.textBaseline = 'alphabetic'
+ context.fillText(text, 0, EMOJI_IMAGE_SIZE * PLATFORM_BASELINE)
+ return imageFromPixels(ck, context.getImageData(0, 0, EMOJI_IMAGE_SIZE, EMOJI_IMAGE_SIZE).data)
+}
+
+/**
+ * The emoji drawn with Noto Color Emoji, once loaded into `provider`. Noto's glyphs are wider
+ * than they are tall; the glyph's advance fills the image's width, centred on its height.
+ */
+export function rasterizeNotoEmoji(
+ ck: CanvasKit,
+ provider: TypefaceFontProvider,
+ text: string
+): Image | null {
+ const surface = ck.MakeSurface(EMOJI_IMAGE_SIZE, EMOJI_IMAGE_SIZE)
+ if (!surface) return null
+ const style = new ck.ParagraphStyle({
+ textStyle: {
+ fontFamilies: [NOTO_COLOR_EMOJI_FAMILY],
+ fontSize: EMOJI_IMAGE_SIZE,
+ color: ck.BLACK
+ }
+ })
+ const builder = ck.ParagraphBuilder.MakeFromFontProvider(style, provider)
+ try {
+ builder.addText(text)
+ const paragraph = builder.build()
+ try {
+ paragraph.layout(EMOJI_IMAGE_SIZE * 4)
+ const advance = paragraph.getMaxIntrinsicWidth()
+ if (advance <= 0) return null
+ const scale = EMOJI_IMAGE_SIZE / advance
+ const canvas = surface.getCanvas()
+ canvas.clear(ck.TRANSPARENT)
+ canvas.translate(0, (EMOJI_IMAGE_SIZE - paragraph.getHeight() * scale) / 2)
+ canvas.scale(scale, scale)
+ canvas.drawParagraph(paragraph, 0, 0)
+ return surface.makeImageSnapshot()
+ } finally {
+ paragraph.delete()
+ }
+ } finally {
+ builder.delete()
+ surface.delete()
+ }
+}
diff --git a/packages/core/src/canvas/text/font-families.ts b/packages/core/src/canvas/text/font-families.ts
index e483047f5..f0e5632f7 100644
--- a/packages/core/src/canvas/text/font-families.ts
+++ b/packages/core/src/canvas/text/font-families.ts
@@ -2,6 +2,7 @@ import { uniq } from 'es-toolkit/array'
import { ResourceCache } from '#core/cache/resource'
import { DEFAULT_FONT_FAMILY } from '#core/constants'
+import { EMOJI_METRICS_FAMILY } from '#core/text/emoji'
import { fontManager } from '#core/text/fonts'
const familiesCache = new ResourceCache<string, string[]>({ maxEntries: 256 })
@@ -24,7 +25,8 @@ export function resolveParagraphFontFamilies(
if (cached) return cached
const families = [renderPrimary]
if (primary !== DEFAULT_FONT_FAMILY) families.push(DEFAULT_FONT_FAMILY)
- families.push(...renderArabicFallbacks, ...renderCJKFallbacks)
+ // Emoji come last, so a character a script's font has stays in that font.
+ families.push(...renderArabicFallbacks, ...renderCJKFallbacks, EMOJI_METRICS_FAMILY)
const resolved = uniq(families)
familiesCache.set(key, resolved)
return resolved
diff --git a/packages/core/src/canvas/text/layout/index.ts b/packages/core/src/canvas/text/layout/index.ts
index 3c4dd0f2b..6dbc0061d 100644
--- a/packages/core/src/canvas/text/layout/index.ts
+++ b/packages/core/src/canvas/text/layout/index.ts
@@ -399,9 +399,11 @@ export class TextLayout {
hi = block.length
useRight = !rtl
}
+ // The box of the whole character beside the caret: half an emoji or a surrogate pair has none.
+ const cluster = block.paragraph.getGlyphInfoAt(lo + block.lead)?.graphemeClusterTextRange
const rects = block.paragraph.getRectsForRange(
- lo + block.lead,
- hi + block.lead,
+ cluster?.start ?? lo + block.lead,
+ cluster?.end ?? hi + block.lead,
this.ck.RectHeightStyle.Max,
this.ck.RectWidthStyle.Tight
)
diff --git a/packages/core/src/canvas/text/placed.ts b/packages/core/src/canvas/text/placed.ts
index 7487a1d8e..7e978f6fc 100644
--- a/packages/core/src/canvas/text/placed.ts
+++ b/packages/core/src/canvas/text/placed.ts
@@ -66,6 +66,23 @@ function* candidateSources(font: ReturnType<typeof fontAt>) {
}
}
+/**
+ * The family the paragraph draws the character at `index` of `text` in, as it falls back: the
+ * first of the fonts it lists, the style's own then its fallbacks, that maps the character.
+ */
+export function characterFamily(node: SceneNode, text: string, index: number): string | null {
+ const codePoint = text.codePointAt(index)
+ if (codePoint === undefined) return null
+ const character = String.fromCodePoint(codePoint)
+ const font = fontAt(node, index)
+ for (const family of resolveParagraphFontFamilies(font.family, font.style)) {
+ const style = loadedStyle(family, font.weight, font.italic)
+ if (style && glyphOutlineSourceSync(family, style, font.variations)?.covers(character))
+ return family
+ }
+ return null
+}
+
/** Whether a glyph the shaper chose is the one the font maps its character to. */
function drawsOwnGlyph(
source: GlyphOutlineSource,
diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts
index e1d3180a8..a4a325d3a 100644
--- a/packages/core/src/constants.ts
+++ b/packages/core/src/constants.ts
@@ -166,6 +166,7 @@ export const AI_DONE_DURATION_MS = 800
/** Opacity of the selection color behind selected text while editing. */
export const TEXT_SELECTION_ALPHA = 0.3
+/** The caret's colour where the text's fill gives it none, as Figma's. */
export const TEXT_CARET_COLOR = BLACK
export const TEXT_CARET_WIDTH = 1
diff --git a/packages/core/src/text/editor.ts b/packages/core/src/text/editor.ts
index 72064d360..63ae5d05c 100644
--- a/packages/core/src/text/editor.ts
+++ b/packages/core/src/text/editor.ts
@@ -6,6 +6,7 @@ import { resolveNodeTextDirection } from '@open-pencil/scene-graph/text-directio
import type { SkiaRenderer } from '#core/canvas'
import type { TextLayout } from '#core/canvas/text/layout'
+import { nextGraphemeBoundary, previousGraphemeBoundary } from '#core/text/graphemes'
export interface TextCaret {
x: number
@@ -259,7 +260,9 @@ export class TextEditor {
backspace(node: SceneNode): void {
const s = this.current()
if (!s) return
- const range = this.getSelectionRange() ?? (s.cursor > 0 ? [s.cursor - 1, s.cursor] : null)
+ const range =
+ this.getSelectionRange() ??
+ (s.cursor > 0 ? [previousGraphemeBoundary(s.text, s.cursor), s.cursor] : null)
if (range) this.replaceRange(range[0], range[1], '')
this.rebuildParagraph(node)
}
@@ -268,7 +271,8 @@ export class TextEditor {
const s = this.current()
if (!s) return
const range =
- this.getSelectionRange() ?? (s.cursor < s.text.length ? [s.cursor, s.cursor + 1] : null)
+ this.getSelectionRange() ??
+ (s.cursor < s.text.length ? [s.cursor, nextGraphemeBoundary(s.text, s.cursor)] : null)
if (range) this.replaceRange(range[0], range[1], '')
this.rebuildParagraph(node)
}
@@ -279,9 +283,9 @@ export class TextEditor {
if (!extend && this.collapseSelectionTo(visualDirection === 'left' ? 0 : 1)) return
this.prepareMove(extend)
const movesForward = (visualDirection === 'left') === (s.textDirection === 'RTL')
- const step = movesForward ? 1 : -1
- const next = s.cursor + step
- if (next >= 0 && next <= s.text.length) s.cursor = next
+ s.cursor = movesForward
+ ? nextGraphemeBoundary(s.text, s.cursor)
+ : previousGraphemeBoundary(s.text, s.cursor)
}
moveLeft(extend = false): void {
diff --git a/packages/core/src/text/emoji.ts b/packages/core/src/text/emoji.ts
new file mode 100644
index 000000000..b41697d93
--- /dev/null
+++ b/packages/core/src/text/emoji.ts
@@ -0,0 +1,35 @@
+import type { DerivedTextGlyph, SceneNode } from '@open-pencil/scene-graph'
+
+/**
+ * The font the editor's text layout falls back to for emoji: Noto Color Emoji's characters and
+ * sequences with every emoji 1 em wide, as Figma lays them out, and no outlines. Built by
+ * `tools/generate/emoji-metrics`; the emoji themselves are drawn over it.
+ */
+export const EMOJI_METRICS_FAMILY = 'OpenPencil Emoji Metrics'
+
+/** The colour emoji font that draws emoji where the platform cannot. */
+export const NOTO_COLOR_EMOJI_FAMILY = 'Noto Color Emoji'
+
+/** The characters a saved emoji glyph stands for. */
+export function emojiText(glyph: Pick<DerivedTextGlyph, 'emojiCodePoints'>): string {
+ return glyph.emojiCodePoints ? String.fromCodePoint(...glyph.emojiCodePoints) : ''
+}
+
+/** Whether the platform draws emoji itself: a browser does, through its 2D canvas. */
+export function canDrawPlatformEmoji(): boolean {
+ return typeof OffscreenCanvas !== 'undefined'
+}
+
+const POSSIBLE_EMOJI = /[\p{Extended_Pictographic}\p{Regional_Indicator}]/u
+
+/** Whether text has a character that may be drawn as an emoji, to skip looking for them. */
+export function mayHoldEmoji(text: string): boolean {
+ return POSSIBLE_EMOJI.test(text)
+}
+
+/** The emoji a text's saved glyphs draw, as characters, for loading a font that has them. */
+export function savedEmojiCharacters(node: SceneNode): string {
+ let characters = ''
+ for (const glyph of node.derivedTextGlyphs ?? []) characters += emojiText(glyph)
+ return characters
+}
diff --git a/packages/core/src/text/fonts.ts b/packages/core/src/text/fonts.ts
index 690f46140..418cef7e8 100644
--- a/packages/core/src/text/fonts.ts
+++ b/packages/core/src/text/fonts.ts
@@ -41,7 +41,8 @@ const BUNDLED_FONTS: Record<string, string> = {
'Inter|SemiBold': '/Inter-SemiBold.ttf',
'Inter|Bold': '/Inter-Bold.ttf',
'Inter|ExtraBold': '/Inter-ExtraBold.ttf',
- 'Noto Naskh Arabic|Regular': '/NotoNaskhArabic-Regular.ttf'
+ 'Noto Naskh Arabic|Regular': '/NotoNaskhArabic-Regular.ttf',
+ 'OpenPencil Emoji Metrics|Regular': '/OpenPencilEmojiMetrics-Regular.ttf'
}
/**
diff --git a/packages/core/src/text/graphemes.ts b/packages/core/src/text/graphemes.ts
new file mode 100644
index 000000000..41f34f0c9
--- /dev/null
+++ b/packages/core/src/text/graphemes.ts
@@ -0,0 +1,18 @@
+/**
+ * Boundaries of user-perceived characters, which the caret moves over and deletion removes
+ * whole: an emoji sequence, a flag, or a surrogate pair is one, never split.
+ */
+const graphemes = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
+
+/** The boundary after the character at `index`, or the text's end. */
+export function nextGraphemeBoundary(text: string, index: number): number {
+ if (index >= text.length) return text.length
+ const segment = graphemes.segment(text).containing(index)
+ return segment ? segment.index + segment.segment.length : text.length
+}
+
+/** The boundary before the character that ends at `index`, or the text's start. */
+export function previousGraphemeBoundary(text: string, index: number): number {
+ if (index <= 0) return 0
+ return graphemes.segment(text).containing(Math.min(index, text.length) - 1)?.index ?? 0
+}
diff --git a/packages/core/src/text/prepare.ts b/packages/core/src/text/prepare.ts
index 6d8cd4797..d0e23d1a8 100644
--- a/packages/core/src/text/prepare.ts
+++ b/packages/core/src/text/prepare.ts
@@ -1,11 +1,44 @@
-import type { SceneGraph } from '@open-pencil/scene-graph'
+import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
+import { DEFAULT_FONT_FAMILY } from '#core/constants'
+import {
+ canDrawPlatformEmoji,
+ mayHoldEmoji,
+ NOTO_COLOR_EMOJI_FAMILY,
+ savedEmojiCharacters
+} from '#core/text/emoji'
import { fontManager } from '#core/text/fonts'
+import { fontGlyphCoverageSync } from '#core/text/opentype'
import { collectGraphFontRequirements, collectNodeFontFaces } from '#core/text/requirements'
import { missingGraphFontScripts } from '#core/text/resolved-requirements'
import { documentFontStatus, type DocumentFontStatus } from './font/status'
+/**
+ * The characters of a text its layout falls back to the emoji metrics font for: ones that may be
+ * emoji and that none of the faces it draws in, nor the fallbacks after them, has. A face not
+ * loaded cannot tell, and leaves the character to it.
+ */
+function laidOutEmojiCharacters(node: SceneNode): string {
+ if (node.type !== 'TEXT' || !mayHoldEmoji(node.text)) return ''
+ const faces = [
+ ...collectNodeFontFaces(node),
+ { family: DEFAULT_FONT_FAMILY, style: 'Regular' },
+ ...[...fontManager.getArabicFallbackFamilies(), ...fontManager.getCJKFallbackFamilies()].map(
+ (family) => ({ family, style: 'Regular' })
+ )
+ ]
+ let characters = ''
+ for (const character of node.text) {
+ if (!mayHoldEmoji(character)) continue
+ const drawnByText = faces.some(
+ ({ family, style }) => fontGlyphCoverageSync(family, style, character) !== 'missing'
+ )
+ if (!drawnByText) characters += character
+ }
+ return characters
+}
+
export async function prepareGraphFonts(
graph: SceneGraph,
nodeIds: readonly string[]
@@ -19,5 +52,12 @@ export async function prepareGraphFonts(
missingGraphFontScripts(requirements),
requirements.characters
)
+ // Without a platform that draws emoji, emoji are drawn with Noto, loaded for just these: the
+ // saved ones, and the ones the layout leaves to the emoji metrics font.
+ const emoji =
+ requirements.nodes.map(savedEmojiCharacters).join('') +
+ requirements.nodes.map(laidOutEmojiCharacters).join('')
+ if (emoji && !canDrawPlatformEmoji())
+ await fontManager.loadRemoteFont(NOTO_COLOR_EMOJI_FAMILY, 'Regular', emoji)
return documentFontStatus(graph, nodeIds)
}
diff --git a/packages/core/tests/canvas/overlays/text-caret.test.ts b/packages/core/tests/canvas/overlays/text-caret.test.ts
new file mode 100644
index 000000000..bb2c74596
--- /dev/null
+++ b/packages/core/tests/canvas/overlays/text-caret.test.ts
@@ -0,0 +1,48 @@
+import { describe, expect, test } from 'bun:test'
+
+import type { Fill } from '@open-pencil/scene-graph'
+import { createDefaultNode } from '@open-pencil/scene-graph/node-defaults'
+
+import { textCaretColor } from '#core/canvas/overlays/text-edit'
+
+const solid = (r: number, g: number, b: number, opacity = 1): Fill => ({
+ type: 'SOLID',
+ color: { r, g, b, a: 1 },
+ opacity,
+ visible: true
+})
+
+function textNode(fills: Fill[], runs: Array<[start: number, length: number, fills: Fill[]]> = []) {
+ return createDefaultNode(() => 'caret-text', 'TEXT', {
+ text: 'Text',
+ fills,
+ styleRuns: runs.map(([start, length, runFills]) => ({ start, length, style: { fills: runFills } }))
+ })
+}
+
+/** Measured in Figma desktop: the caret takes the text's colour, not the background's. */
+describe('text caret colour', () => {
+ const red = solid(1, 0, 0)
+ const blue = solid(0, 0, 1)
+ const runs = textNode([solid(0, 0, 0)], [
+ [0, 2, [red]],
+ [2, 2, [blue]]
+ ])
+
+ test("takes the fill of the character before the caret, or the first's at the start", () => {
+ expect(textCaretColor(runs, 4)).toEqual(blue.color)
+ expect(textCaretColor(runs, 2)).toEqual(red.color)
+ expect(textCaretColor(runs, 0)).toEqual(red.color)
+ })
+
+ test('keeps the fill opacity', () => {
+ expect(textCaretColor(textNode([solid(1, 0, 0, 0.5)]), 4)).toEqual({ r: 1, g: 0, b: 0, a: 0.5 })
+ })
+
+ test('is black for a gradient, a hidden fill, several fills, or none', () => {
+ const gradient: Fill = { ...red, type: 'GRADIENT_LINEAR' }
+ for (const fills of [[gradient], [{ ...red, visible: false }], [red, blue], []]) {
+ expect(textCaretColor(textNode(fills), 4)).toEqual({ r: 0, g: 0, b: 0, a: 1 })
+ }
+ })
+})
diff --git a/packages/core/tests/canvas/text/shape.test.ts b/packages/core/tests/canvas/text/shape.test.ts
index 5bb811fa4..1297402e1 100644
--- a/packages/core/tests/canvas/text/shape.test.ts
+++ b/packages/core/tests/canvas/text/shape.test.ts
@@ -11,6 +11,7 @@ import type { SceneNode } from '@open-pencil/scene-graph'
import { withFigExportRuntime } from '#core/canvas/text/shape'
import { getCanvasKit } from '#core/canvaskit'
+import { EMOJI_METRICS_FAMILY } from '#core/text/emoji'
import { fontManager } from '#core/text/fonts'
import { getGlyphOutlineMetricsSync } from '#core/text/opentype'
@@ -43,6 +44,26 @@ describe('text shaping for saved glyphs', () => {
fontManager.markLoaded('Inter', 'Regular', inter)
})
+ /**
+ * Figma saves every emoji as a 1 em square, a joined sequence or a flag as one: the layout
+ * falls back to the emoji metrics font for them, which gives them that width.
+ */
+ test('lays an emoji out 1 em wide, a joined sequence and a flag as one', async () => {
+ const metrics = expectDefined(
+ await fontManager.fetchBundledFont('/OpenPencilEmojiMetrics-Regular.ttf'),
+ 'bundled emoji metrics font'
+ )
+ fontManager.markLoaded(EMOJI_METRICS_FAMILY, 'Regular', metrics)
+ const family = '👨‍👩‍👧‍👦'
+ const shaped = expectDefined(
+ await shape({ text: `a😀${family}🇺🇸b`, fontSize: 40, width: 400, textAutoResize: 'NONE' }),
+ 'shaped text'
+ )
+ const advance = (character: number) =>
+ shaped.glyphs.find((glyph) => glyph.firstCharacter === character)?.advance
+ expect([advance(1), advance(3), advance(3 + family.length)]).toEqual([40, 40, 40])
+ })
+
test('breaks lines at the layer width (#914)', async () => {
const shaped = expectDefined(await shape({ text: SENTENCE }), 'shaped text')
const [first, second] = shaped.baselines
diff --git a/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts b/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
index e4f973df5..2de24a968 100644
--- a/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
+++ b/packages/core/tests/io/formats/fig/roundtrip/source-metadata.test.ts
@@ -224,6 +224,7 @@ describe('fig roundtrip source metadata', () => {
text.source.fig.rawNodeFields.toggledOffOTFeatures = ['LIGA']
text.source.fig.rawNodeFields.semanticWeight = 'BOLD'
text.source.fig.rawNodeFields.semanticItalic = 'ITALIC'
+ text.source.fig.rawNodeFields.emojiImageSet = 'NOTO'
text.source.fig.rawNodeFields.derivedTextData = {
layoutSize: { x: 80, y: 20 },
derivedLines: [{ directionality: 'LTR' }]
@@ -245,6 +246,7 @@ describe('fig roundtrip source metadata', () => {
expect(exported?.toggledOffOTFeatures).toEqual(['LIGA'])
expect(exported?.semanticWeight).toBe('BOLD')
expect(exported?.semanticItalic).toBe('ITALIC')
+ expect(exported?.emojiImageSet).toBe('NOTO')
expect(exported?.derivedTextData?.layoutSize).toEqual({ x: 80, y: 20 })
})
diff --git a/packages/core/tests/text/graphemes.test.ts b/packages/core/tests/text/graphemes.test.ts
new file mode 100644
index 000000000..ce8d1c63a
--- /dev/null
+++ b/packages/core/tests/text/graphemes.test.ts
@@ -0,0 +1,27 @@
+import { describe, expect, test } from 'bun:test'
+
+import { nextGraphemeBoundary, previousGraphemeBoundary } from '#core/text/graphemes'
+
+describe('grapheme boundaries', () => {
+ const family = '👨‍👩‍👧‍👦'
+ const flag = '🇺🇸'
+ const text = `a😀${family}${flag}👍🏽b`
+ const boundaries = [0, 1, 3, 3 + family.length, 3 + family.length + 4, text.length - 1, text.length]
+
+ test('step forward over each user-perceived character whole', () => {
+ const steps = [0]
+ while (steps.at(-1) !== text.length) steps.push(nextGraphemeBoundary(text, steps.at(-1) ?? 0))
+ expect(steps).toEqual(boundaries)
+ })
+
+ test('step back over each user-perceived character whole', () => {
+ const steps = [text.length]
+ while (steps.at(-1) !== 0) steps.push(previousGraphemeBoundary(text, steps.at(-1) ?? 0))
+ expect(steps).toEqual([...boundaries].reverse())
+ })
+
+ test('stay at the ends of the text', () => {
+ expect(previousGraphemeBoundary(text, 0)).toBe(0)
+ expect(nextGraphemeBoundary(text, text.length)).toBe(text.length)
+ })
+})
diff --git a/packages/core/tests/text/prepare.test.ts b/packages/core/tests/text/prepare.test.ts
new file mode 100644
index 000000000..5fad953e8
--- /dev/null
+++ b/packages/core/tests/text/prepare.test.ts
@@ -0,0 +1,63 @@
+import { afterEach, beforeAll, describe, expect, spyOn, test } from 'bun:test'
+
+import { expectDefined } from '#core-tests/helpers/assert'
+
+import { SceneGraph } from '@open-pencil/scene-graph'
+
+import { NOTO_COLOR_EMOJI_FAMILY } from '#core/text/emoji'
+import { fontManager } from '#core/text/fonts'
+import { prepareGraphFonts } from '#core/text/prepare'
+
+function graphWithText(text: string) {
+ const graph = new SceneGraph()
+ const node = graph.createNode('TEXT', graph.getPages()[0].id, { text, fontFamily: 'Inter' })
+ return { graph, nodeIds: [node.id] }
+}
+
+/**
+ * Where nothing draws platform emoji, Noto Color Emoji is fetched for the emoji a text's layout
+ * leaves to the emoji metrics font, and only those: a symbol the text font has stays text and
+ * must not cost the whole emoji font's download.
+ */
+describe('emoji font preparation', () => {
+ const requested: string[] = []
+ let spy: ReturnType<typeof spyOn> | null = null
+
+ beforeAll(async () => {
+ const inter = expectDefined(
+ await fontManager.fetchBundledFont('/Inter-Regular.ttf'),
+ 'bundled Inter font'
+ )
+ fontManager.markLoaded('Inter', 'Regular', inter)
+ })
+
+ afterEach(() => {
+ spy?.mockRestore()
+ requested.length = 0
+ })
+
+ function recordNotoRequests() {
+ const load = fontManager.loadRemoteFont.bind(fontManager)
+ spy = spyOn(fontManager, 'loadRemoteFont').mockImplementation(async (family, style, characters) => {
+ if (family === NOTO_COLOR_EMOJI_FAMILY) {
+ requested.push(characters ?? '')
+ return null
+ }
+ return load(family, style, characters)
+ })
+ }
+
+ test('fetches nothing for symbols the text font has', async () => {
+ recordNotoRequests()
+ const { graph, nodeIds } = graphWithText('© ™ ↔ text')
+ await prepareGraphFonts(graph, nodeIds)
+ expect(requested).toEqual([])
+ })
+
+ test('fetches the emoji the text font lacks', async () => {
+ recordNotoRequests()
+ const { graph, nodeIds } = graphWithText('© 😀')
+ await prepareGraphFonts(graph, nodeIds)
+ expect(requested).toEqual(['😀'])
+ })
+})
diff --git a/packages/fig/src/node-change/convert.ts b/packages/fig/src/node-change/convert.ts
index 1c8844e3a..f1e664304 100644
--- a/packages/fig/src/node-change/convert.ts
+++ b/packages/fig/src/node-change/convert.ts
@@ -1178,6 +1178,7 @@ export const FIGMA_RAW_NODE_FIELD_KEYS = [
'letterSpacing',
'textTracking',
'fontVersion',
+ 'emojiImageSet',
'textUserLayoutVersion',
'textExplicitLayoutVersion',
'fontVariations',
diff --git a/packages/fig/src/node-change/derived-text/build.ts b/packages/fig/src/node-change/derived-text/build.ts
index 1c0482e3f..c6210cfdc 100644
--- a/packages/fig/src/node-change/derived-text/build.ts
+++ b/packages/fig/src/node-change/derived-text/build.ts
@@ -16,6 +16,7 @@ import { weightToFigmaStyle } from '../font/style'
import { bakeGlyphScale, encodePathCommandsBlob, type OutlineCommand } from '../path/commands'
import { characterStyleIDs } from '../text/data-export'
import { buildDerivedTextData } from './data'
+import { emojiGlyphFields } from './glyphs'
type DerivedTextData = NonNullable<NodeChange['derivedTextData']>
type DerivedTextBaseline = NonNullable<DerivedTextData['baselines']>[number]
@@ -240,7 +241,8 @@ function savedGlyphRecords(
firstCharacter: characters[index],
advance: savedAdvance(glyph, sameLine ? next : undefined),
// Preserve path-text radians; hardcoding 0 used to flatten circular text on re-export.
- rotation: glyph.rotation ?? 0
+ rotation: glyph.rotation ?? 0,
+ ...emojiGlyphFields(glyph)
}
})
}
diff --git a/packages/fig/src/node-change/derived-text/glyphs.ts b/packages/fig/src/node-change/derived-text/glyphs.ts
index b0e5a1e05..09f8583f8 100644
--- a/packages/fig/src/node-change/derived-text/glyphs.ts
+++ b/packages/fig/src/node-change/derived-text/glyphs.ts
@@ -26,6 +26,22 @@ function isUnshapedOpenPencilText(derivedTextData: DerivedTextData, characters:
* Dropping `rotation` used to flatten path text to axis-aligned scribbles;
* units are radians (schema `float`, DomeSticker values ≈ -1.75…-0.5).
*/
+type EmojiGlyphFields = Pick<DerivedTextGlyph, 'emojiCodePoints' | 'emojiImageSet'>
+
+/**
+ * An emoji glyph's code points and image set, copied, for a glyph read or saved; nothing for any
+ * other glyph. Figma draws an emoji glyph's placeholder square without them.
+ */
+export function emojiGlyphFields(glyph: {
+ emojiCodePoints?: readonly number[]
+ emojiImageSet?: string
+}): EmojiGlyphFields {
+ if (!glyph.emojiCodePoints?.length) return {}
+ const fields: EmojiGlyphFields = { emojiCodePoints: [...glyph.emojiCodePoints] }
+ if (glyph.emojiImageSet === 'NOTO') fields.emojiImageSet = 'NOTO'
+ return fields
+}
+
export function convertFigmaDerivedTextGlyphs(
derivedTextData: NodeChange['derivedTextData'],
blobs: Uint8Array[],
@@ -50,7 +66,8 @@ export function convertFigmaDerivedTextGlyphs(
fontSize: glyph.fontSize,
firstCharacter: glyph.firstCharacter,
rotation: glyph.rotation,
- advance: Number.isFinite(advance) ? advance : undefined
+ advance: Number.isFinite(advance) ? advance : undefined,
+ ...emojiGlyphFields(glyph)
})
}
return glyphs
diff --git a/packages/fig/src/node-change/serialize.ts b/packages/fig/src/node-change/serialize.ts
index 51d3f71b2..17886172e 100644
--- a/packages/fig/src/node-change/serialize.ts
+++ b/packages/fig/src/node-change/serialize.ts
@@ -127,7 +127,8 @@ function serializeTextProps(
nc.fontVariantContextualLigatures = true
applyFontFeaturesToKiwi(nc, node.fontFeatures)
nc.fontVersion = ''
- nc.emojiImageSet = 'APPLE'
+ // FigJam text draws Noto emoji; a file's own set is replayed from its raw fields.
+ if (!('emojiImageSet' in rawNodeFields)) nc.emojiImageSet = 'APPLE'
if (node.textCase !== 'ORIGINAL') nc.textCase = node.textCase
if (node.textTruncation === 'ENDING') nc.textTruncation = 'ENDING'
if (node.maxLines != null) nc.maxLines = node.maxLines
diff --git a/packages/fig/tests/node-change/derived-text.test.ts b/packages/fig/tests/node-change/derived-text.test.ts
index d8865dc27..aeb3fb533 100644
--- a/packages/fig/tests/node-change/derived-text.test.ts
+++ b/packages/fig/tests/node-change/derived-text.test.ts
@@ -322,4 +322,66 @@ describe('derived text reader', () => {
expect(convertFigmaDerivedTextGlyphs(data, blobs, characters)).toEqual([])
})
+ /**
+ * Figma saves an emoji as a placeholder square with the emoji's code points, measured in
+ * Figma desktop, and draws the emoji over the square; without the code points it draws the
+ * square, so they survive reading and saving.
+ */
+ test("keeps an emoji glyph's code points and image set through reading and saving", () => {
+ const characters = 'H🖥️👍🏽'
+ const data: DerivedTextData = {
+ glyphs: [
+ {
+ commandsBlob: 0,
+ position: { x: 0, y: 97 },
+ fontSize: 100,
+ firstCharacter: 0,
+ advance: 0.74
+ },
+ {
+ commandsBlob: 0,
+ position: { x: 74, y: 97 },
+ fontSize: 100,
+ firstCharacter: 1,
+ advance: 1,
+ emojiCodePoints: [0x1f5a5, 0xfe0f]
+ },
+ {
+ commandsBlob: 0,
+ position: { x: 174, y: 97 },
+ fontSize: 100,
+ firstCharacter: 4,
+ advance: 1,
+ emojiCodePoints: [0x1f44d, 0x1f3fd],
+ emojiImageSet: 'NOTO'
+ }
+ ],
+ logicalIndexToCharacterOffsetMap: [0, 74, 74, 74, 174, 174, 174, 174]
+ }
+
+ const glyphs = convertFigmaDerivedTextGlyphs(data, blobs, characters)
+ expect(glyphs.map((glyph) => glyph.emojiCodePoints)).toEqual([
+ undefined,
+ [0x1f5a5, 0xfe0f],
+ [0x1f44d, 0x1f3fd]
+ ])
+ expect(glyphs.map((glyph) => glyph.emojiImageSet)).toEqual([undefined, undefined, 'NOTO'])
+ const saved = expectDefined(
+ buildNodeDerivedTextData(
+ textNode({ text: characters, derivedTextGlyphs: glyphs }),
+ context(null)
+ ),
+ 'derived text'
+ )
+ expect(saved.glyphs?.map((glyph) => glyph.emojiCodePoints)).toEqual([
+ undefined,
+ [0x1f5a5, 0xfe0f],
+ [0x1f44d, 0x1f3fd]
+ ])
+ expect(saved.glyphs?.map((glyph) => glyph.emojiImageSet)).toEqual([
+ undefined,
+ undefined,
+ 'NOTO'
+ ])
+ })
})
diff --git a/packages/kiwi/src/fig/codec.ts b/packages/kiwi/src/fig/codec.ts
index 82ab12108..8f568896c 100644
--- a/packages/kiwi/src/fig/codec.ts
+++ b/packages/kiwi/src/fig/codec.ts
@@ -323,6 +323,10 @@ export interface NodeChange {
advance: number
/** Radians along the text path; kiwi omits the field for axis-aligned text. */
rotation?: number
+ /** An emoji's code points; Figma draws its image in place of the glyph's placeholder. */
+ emojiCodePoints?: number[]
+ /** The image set the emoji is drawn from; left out for the default, APPLE. */
+ emojiImageSet?: 'APPLE' | 'NOTO'
}>
/** Underlines and strikethroughs, which Figma draws saved text's decorations from. */
decorations?: Array<{
diff --git a/packages/scene-graph/src/copy.ts b/packages/scene-graph/src/copy.ts
index af14290c2..1a5c75a07 100644
--- a/packages/scene-graph/src/copy.ts
+++ b/packages/scene-graph/src/copy.ts
@@ -213,7 +213,13 @@ function copyPropertyDefs(
/** Deep-copy path-text glyphs (fresh commandsBlob buffers). */
export function copyDerivedGlyphs(glyphs: DerivedTextGlyph[] | null): DerivedTextGlyph[] | null {
- return glyphs ? glyphs.map((g) => ({ ...g, commandsBlob: new Uint8Array(g.commandsBlob) })) : null
+ return glyphs
+ ? glyphs.map((g) => ({
+ ...g,
+ commandsBlob: new Uint8Array(g.commandsBlob),
+ ...(g.emojiCodePoints ? { emojiCodePoints: [...g.emojiCodePoints] } : {})
+ }))
+ : null
}
// --- Complex structure copy functions ---
diff --git a/packages/scene-graph/src/types.ts b/packages/scene-graph/src/types.ts
index 55fb2404c..e986b4311 100644
--- a/packages/scene-graph/src/types.ts
+++ b/packages/scene-graph/src/types.ts
@@ -446,6 +446,13 @@ export interface DerivedTextGlyph {
*/
scaleX?: number
scaleY?: number
+ /**
+ * The code points of the emoji this glyph stands for, sequence and all. Its outline is only a
+ * placeholder square a 1 em wide; the emoji's image is drawn there instead, as Figma does.
+ */
+ emojiCodePoints?: number[]
+ /** The image set Figma draws the emoji from, when the file names one other than Apple's. */
+ emojiImageSet?: 'NOTO'
}
export interface SymbolLink {
diff --git a/src/app/editor/fonts/bundled.ts b/src/app/editor/fonts/bundled.ts
index bb047702f..85d6ea9a6 100644
--- a/src/app/editor/fonts/bundled.ts
+++ b/src/app/editor/fonts/bundled.ts
@@ -4,6 +4,7 @@ import interMedium from '@open-pencil/core/assets/Inter-Medium.ttf?url'
import interRegular from '@open-pencil/core/assets/Inter-Regular.ttf?url'
import interSemiBold from '@open-pencil/core/assets/Inter-SemiBold.ttf?url'
import notoNaskhArabic from '@open-pencil/core/assets/NotoNaskhArabic-Regular.ttf?url'
+import emojiMetrics from '@open-pencil/core/assets/OpenPencilEmojiMetrics-Regular.ttf?url'
import { fontManager } from '@open-pencil/core/text'
/**
@@ -18,7 +19,8 @@ const BUNDLED_FONT_URLS: Record<string, string> = {
'Inter-SemiBold.ttf': interSemiBold,
'Inter-Bold.ttf': interBold,
'Inter-ExtraBold.ttf': interExtraBold,
- 'NotoNaskhArabic-Regular.ttf': notoNaskhArabic
+ 'NotoNaskhArabic-Regular.ttf': notoNaskhArabic,
+ 'OpenPencilEmojiMetrics-Regular.ttf': emojiMetrics
}
/** Points core at the emitted font files. Call before any font loads. */
diff --git a/tests/e2e/canvas/saved-glyph-visual.spec.ts b/tests/e2e/canvas/saved-glyph-visual.spec.ts
index 64d07f4df..b4c8235d0 100644
--- a/tests/e2e/canvas/saved-glyph-visual.spec.ts
+++ b/tests/e2e/canvas/saved-glyph-visual.spec.ts
@@ -101,3 +101,164 @@ test('saved glyphs remain authoritative with an available font and invalidate on
)
expect(await editor.canvas.screenshotCanvasRegion()).toMatchSnapshot('saved-glyph-authority.png')
})
+
+/**
+ * Figma saves an emoji as a placeholder square 1 em wide, from 0.07 em below the baseline, with
+ * the emoji's code points, and draws the emoji's image over it in its own colours: the text's
+ * fill, here a half-transparent red or a gradient, colours the letters and never the emoji. A
+ * gradient spans the layer as on a shape. Measured in Figma desktop; the square was drawn in the
+ * fill colour before, and gradient text lost its emoji and stretched its gradient by the font size.
+ */
+test('saved emoji glyphs draw the emoji, not their placeholder, in their own colours', async () => {
+ await editor.page.evaluate(() => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('Missing editor')
+ const outline = (points: [number, number, number][]) => {
+ const bytes = new Uint8Array(points.length * 9 + 1)
+ const view = new DataView(bytes.buffer)
+ for (const [index, [command, x, y]] of points.entries()) {
+ bytes[index * 9] = command
+ view.setFloat32(index * 9 + 1, x, true)
+ view.setFloat32(index * 9 + 5, y, true)
+ }
+ return bytes
+ }
+ const letter = outline([
+ [1, 0.1, 0],
+ [2, 0.6, 0],
+ [2, 0.6, 0.7],
+ [2, 0.1, 0.7]
+ ])
+ const placeholder = outline([
+ [1, 0, -0.07],
+ [2, 1, -0.07],
+ [2, 1, 0.93],
+ [2, 0, 0.93]
+ ])
+ store.graph.createNode('TEXT', store.state.currentPageId, {
+ x: 40,
+ y: 100,
+ width: 300,
+ height: 80,
+ text: 'H🖥️H👍🏽',
+ fontFamily: 'Inter',
+ fontSize: 64,
+ fills: [{ type: 'SOLID', color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 0.5, visible: true }],
+ derivedTextGlyphs: [
+ { commandsBlob: letter.slice(), x: 0, y: 62, fontSize: 64, firstCharacter: 0, advance: 47 },
+ {
+ commandsBlob: placeholder.slice(),
+ x: 47,
+ y: 62,
+ fontSize: 64,
+ firstCharacter: 1,
+ advance: 64,
+ emojiCodePoints: [0x1f5a5, 0xfe0f]
+ },
+ {
+ commandsBlob: letter.slice(),
+ x: 111,
+ y: 62,
+ fontSize: 64,
+ firstCharacter: 4,
+ advance: 47
+ },
+ {
+ commandsBlob: placeholder.slice(),
+ x: 158,
+ y: 62,
+ fontSize: 64,
+ firstCharacter: 5,
+ advance: 64,
+ emojiCodePoints: [0x1f44d, 0x1f3fd]
+ }
+ ]
+ })
+ store.graph.createNode('TEXT', store.state.currentPageId, {
+ x: 40,
+ y: 200,
+ width: 240,
+ height: 80,
+ text: 'HH🖥️',
+ fontFamily: 'Inter',
+ fontSize: 64,
+ fills: [
+ {
+ type: 'GRADIENT_LINEAR',
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ opacity: 1,
+ visible: true,
+ gradientStops: [
+ { position: 0, color: { r: 0.49, g: 0.23, b: 0.93, a: 1 } },
+ { position: 1, color: { r: 0.98, g: 0.45, b: 0.09, a: 1 } }
+ ],
+ gradientTransform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 0 }
+ }
+ ],
+ derivedTextGlyphs: [
+ { commandsBlob: letter.slice(), x: 0, y: 62, fontSize: 64, firstCharacter: 0, advance: 47 },
+ {
+ commandsBlob: letter.slice(),
+ x: 64,
+ y: 62,
+ fontSize: 64,
+ firstCharacter: 1,
+ advance: 47
+ },
+ {
+ commandsBlob: placeholder.slice(),
+ x: 128,
+ y: 62,
+ fontSize: 64,
+ firstCharacter: 2,
+ advance: 64,
+ emojiCodePoints: [0x1f5a5, 0xfe0f]
+ }
+ ]
+ })
+ store.requestRender()
+ })
+ await editor.page.evaluate(
+ () =>
+ new Promise<void>((resolve) => {
+ requestAnimationFrame(() => requestAnimationFrame(() => resolve()))
+ })
+ )
+ // A missing emoji covers less than the default 1% of the region, so allow only edge noise.
+ expect(await editor.canvas.screenshotCanvasRegion()).toMatchSnapshot('saved-emoji-glyphs.png', {
+ maxDiffPixels: 500
+ })
+})
+
+/**
+ * Emoji typed in OpenPencil have no saved glyphs: the layout gives each Figma's 1 em square
+ * through the emoji metrics font, and the emoji's image is drawn there in its own colours.
+ * A character a text font has, such as ©, stays text.
+ */
+test('typed emoji take a 1 em square and draw their image', async () => {
+ await editor.page.evaluate(() => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('Missing editor')
+ store.graph.createNode('TEXT', store.state.currentPageId, {
+ x: 40,
+ y: 100,
+ width: 600,
+ height: 80,
+ text: 'Hi 😀👨‍👩‍👧‍👦🇺🇸 © ok',
+ fontFamily: 'Inter',
+ fontSize: 48,
+ fills: [{ type: 'SOLID', color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 0.5, visible: true }]
+ })
+ store.requestRender()
+ })
+ await editor.page.evaluate(
+ () =>
+ new Promise<void>((resolve) => {
+ requestAnimationFrame(() => requestAnimationFrame(() => resolve()))
+ })
+ )
+ // A missing emoji covers less than the default 1% of the region, so allow only edge noise.
+ expect(await editor.canvas.screenshotCanvasRegion()).toMatchSnapshot('typed-emoji.png', {
+ maxDiffPixels: 500
+ })
+})
diff --git a/tests/e2e/canvas/saved-glyph-visual.spec.ts-snapshots/saved-emoji-glyphs-openpencil-darwin.png b/tests/e2e/canvas/saved-glyph-visual.spec.ts-snapshots/saved-emoji-glyphs-openpencil-darwin.png
new file mode 100644
index 000000000..9434b65d2
Binary files /dev/null and b/tests/e2e/canvas/saved-glyph-visual.spec.ts-snapshots/saved-emoji-glyphs-openpencil-darwin.png differ
diff --git a/tests/e2e/canvas/saved-glyph-visual.spec.ts-snapshots/typed-emoji-openpencil-darwin.png b/tests/e2e/canvas/saved-glyph-visual.spec.ts-snapshots/typed-emoji-openpencil-darwin.png
new file mode 100644
index 000000000..e481276a5
Binary files /dev/null and b/tests/e2e/canvas/saved-glyph-visual.spec.ts-snapshots/typed-emoji-openpencil-darwin.png differ
diff --git a/tests/engine/io/fig/import/raw-field-coverage.test.ts b/tests/engine/io/fig/import/raw-field-coverage.test.ts
index 299d27453..37aea6bef 100644
--- a/tests/engine/io/fig/import/raw-field-coverage.test.ts
+++ b/tests/engine/io/fig/import/raw-field-coverage.test.ts
@@ -85,6 +85,7 @@ const RAW_FIELD_COVERAGE = {
'detachedSymbolId',
'documentColorProfile',
'editInfo',
+ 'emojiImageSet',
'fontVersion',
'gridChildHorizontalAlign',
'gridChildVerticalAlign',
diff --git a/tests/engine/render/canvas/renderer-lifecycle.test.ts b/tests/engine/render/canvas/renderer-lifecycle.test.ts
index 2d4fb93ba..cc5690cdb 100644
--- a/tests/engine/render/canvas/renderer-lifecycle.test.ts
+++ b/tests/engine/render/canvas/renderer-lifecycle.test.ts
@@ -9,6 +9,7 @@ import type { SkiaRenderer } from '#core/canvas/renderer'
import { EffectRasterCache } from '#core/canvas/renderer/effect-raster-cache'
import { destroyRenderer } from '#core/canvas/renderer/lifecycle'
import { createDerivedGlyphPathCache, createGlyphSilhouetteCache } from '#core/canvas/text/derived'
+import { createEmojiImageCache } from '#core/canvas/text/emoji'
import { TextPreparationCache } from '#core/canvas/text/preparation/cache'
import { asDouble, asRenderer } from './helpers'
@@ -29,6 +30,7 @@ function createRenderer() {
nodePaths: new NodePathCaches(),
glyphSilhouetteCache: createGlyphSilhouetteCache(),
derivedGlyphPathCache: createDerivedGlyphPathCache(),
+ emojiImageCache: createEmojiImageCache(),
fillPaint: deletable<Paint>(),
strokePaint: deletable<Paint>(),
selectionPaint: deletable<Paint>(),
diff --git a/tests/engine/text/editor.test.ts b/tests/engine/text/editor.test.ts
index 78b9e64e1..0c5ff5906 100644
--- a/tests/engine/text/editor.test.ts
+++ b/tests/engine/text/editor.test.ts
@@ -59,6 +59,7 @@ function createParagraphEditor(textAlignVertical: SceneNode['textAlignVertical']
},
getHeight: () => 20,
getLineMetrics: () => [asDouble(lineMetrics)],
+ getGlyphInfoAt: () => null,
getLineNumberAt: () => 0,
getRectsForRange: () => rects
},
@@ -281,6 +282,51 @@ describe('TextEditor', () => {
expect(editor.state?.cursor).toBe(10)
})
+ /** An emoji is one character to the caret, whether a surrogate pair or a joined sequence. */
+ test('moves over, and deletes, an emoji whole', () => {
+ const family = '👨‍👩‍👧'
+ const { editor, node } = createEditor(`a😀${family}b`)
+ const state = editorState(editor)
+ state.cursor = 1
+ editor.moveRight()
+ expect(state.cursor).toBe(3)
+ editor.moveRight()
+ expect(state.cursor).toBe(3 + family.length)
+ editor.moveLeft()
+ expect(state.cursor).toBe(3)
+ editor.backspace(node)
+ expect(state.text).toBe(`a${family}b`)
+ expect(state.cursor).toBe(1)
+ editor.delete(node)
+ expect(state.text).toBe('ab')
+ })
+
+ /** CanvasKit gives no box for half a character, so the caret beside one takes its cluster's. */
+ test('places the caret beside an emoji at the edge of its whole cluster', () => {
+ const ranges: Array<[number, number]> = []
+ const layout = layoutOf(
+ {
+ getGlyphInfoAt: (index: number) =>
+ index >= 1 && index < 3
+ ? asDouble({ graphemeClusterTextRange: { start: 1, end: 3 } })
+ : null,
+ getHeight: () => 12,
+ getLineMetrics: () => [
+ asDouble<LineMetrics>({ baseline: 10, ascent: 8, height: 12, left: 0 })
+ ],
+ getRectsForRange: (start: number, end: number) => {
+ ranges.push([start, end])
+ return start === 1 && end === 3
+ ? [asDouble<RectWithDirection>({ rect: [10, 0, 34, 12] })]
+ : []
+ }
+ },
+ 4
+ )
+ expect(layout.getCaretRect(1, false)?.x).toBe(10)
+ expect(ranges).toEqual([[1, 3]])
+ })
+
test('moveRight at end stays', () => {
const { editor } = createEditor()
editor.moveRight()
diff --git a/tools/AGENTS.md b/tools/AGENTS.md
index 0786ff7ec..d146934e5 100644
--- a/tools/AGENTS.md
+++ b/tools/AGENTS.md
@@ -2,13 +2,13 @@
Private tooling lives under `tools/<role>/<domain>/{src,tests}`; Steiger enforces the layout and kebab-case domain names. Every tool is a workspace named `@open-pencil/<domain>-tools`, listed literally in the root `workspaces` (the manifest schema rejects globs on purpose), so it can declare dependencies and run through `bun --filter`. `bun run check:tools` type-checks every tool through `tools/tsconfig.json`; `bun run test:tools` runs every tool with a `test` script. A tool imports its own files through its package.json `imports` alias (`#ci/*`, `#release/*`), other tools through their package name, and the docs site config through the shared `#docs-config/*` path.
-| Role | Contract | Domains |
-| ----------- | ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
-| `checks/` | Read the tree and exit non-zero; never write inside the repository except an own baseline behind an explicit `--write` | `architecture`, `lint`, `i18n`, `secret-scan`, `type-shapes`, `docs`, `test-homes`, `package-quality` |
-| `generate/` | Write generated files or artifacts, idempotently | `brand`, `demo`, `tauri-menu`, `authoring-reference`, `visual-oracles`, `landing-posters` |
-| `release/` | Build, verify, and publish packages and native artifacts | `package-artifacts`, `release-packages` |
-| `ci/` | Consumed by workflows: path classification and gate policy, container images, the review-guidance bot | `policy`, `images`, `pr-review-guidance` |
-| `dev/` | Test running, benchmarks, and local services for humans | `unit-tests`, `navigation-benchmark`, `dev-server`, `cloud` |
+| Role | Contract | Domains |
+| ----------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
+| `checks/` | Read the tree and exit non-zero; never write inside the repository except an own baseline behind an explicit `--write` | `architecture`, `lint`, `i18n`, `secret-scan`, `type-shapes`, `docs`, `test-homes`, `package-quality` |
+| `generate/` | Write generated files or artifacts, idempotently | `brand`, `demo`, `emoji-metrics`, `tauri-menu`, `authoring-reference`, `visual-oracles`, `landing-posters` |
+| `release/` | Build, verify, and publish packages and native artifacts | `package-artifacts`, `release-packages` |
+| `ci/` | Consumed by workflows: path classification and gate policy, container images, the review-guidance bot | `policy`, `images`, `pr-review-guidance` |
+| `dev/` | Test running, benchmarks, and local services for humans | `unit-tests`, `navigation-benchmark`, `dev-server`, `cloud` |
- Nothing under `generate/` or `release/` runs from `bun run check` except through a check entrypoint; `generate/authoring-reference` keeps its `check.ts` beside `generate.ts` until it splits.
- Resolve the workspace with `resolveWorkspaceRoot` from `@open-pencil/package-artifacts-tools`, not parent-directory traversal. Keep sibling imports relative.
diff --git a/tools/generate/emoji-metrics/package.json b/tools/generate/emoji-metrics/package.json
new file mode 100644
index 000000000..c5a09a6fd
--- /dev/null
+++ b/tools/generate/emoji-metrics/package.json
@@ -0,0 +1,14 @@
+{
+ "name": "@open-pencil/emoji-metrics-tools",
+ "private": true,
+ "type": "module",
+ "imports": {
+ "#emoji-metrics/*": "./src/*.ts"
+ },
+ "scripts": {
+ "test": "bun test tests"
+ },
+ "devDependencies": {
+ "@open-pencil/package-artifacts-tools": "workspace:*"
+ }
+}
diff --git a/tools/generate/emoji-metrics/src/cmap.ts b/tools/generate/emoji-metrics/src/cmap.ts
new file mode 100644
index 000000000..dfcb43b32
--- /dev/null
+++ b/tools/generate/emoji-metrics/src/cmap.ts
@@ -0,0 +1,106 @@
+/** Reading a font's character map and writing one back, as format 4 and format 12 subtables. */
+
+export type CharacterMap = Map<number, number>
+
+/** The code points of the font's full Unicode subtable (format 12) and the glyphs they map to. */
+export function readCharacterMap(cmap: Uint8Array): CharacterMap {
+ const view = new DataView(cmap.buffer, cmap.byteOffset, cmap.byteLength)
+ const map: CharacterMap = new Map()
+ const count = view.getUint16(2)
+ for (let index = 0; index < count; index++) {
+ const offset = view.getUint32(4 + index * 8 + 4)
+ if (view.getUint16(offset) !== 12) continue
+ const groups = view.getUint32(offset + 12)
+ for (let group = 0; group < groups; group++) {
+ const record = offset + 16 + group * 12
+ const start = view.getUint32(record)
+ const end = view.getUint32(record + 4)
+ const glyph = view.getUint32(record + 8)
+ for (let codePoint = start; codePoint <= end; codePoint++)
+ map.set(codePoint, glyph + codePoint - start)
+ }
+ return map
+ }
+ throw new Error('The source font has no format 12 character map')
+}
+
+/** Runs of consecutive code points mapping to consecutive glyphs. */
+function groupsOf(map: CharacterMap): Array<[start: number, end: number, glyph: number]> {
+ const groups: Array<[number, number, number]> = []
+ for (const codePoint of [...map.keys()].sort((a, b) => a - b)) {
+ const glyph = map.get(codePoint) ?? 0
+ const last = groups.at(-1)
+ if (last && codePoint === last[1] + 1 && glyph === last[2] + codePoint - last[0])
+ last[1] = codePoint
+ else groups.push([codePoint, codePoint, glyph])
+ }
+ return groups
+}
+
+function format12(map: CharacterMap): Uint8Array {
+ const groups = groupsOf(map)
+ const bytes = new Uint8Array(16 + groups.length * 12)
+ const view = new DataView(bytes.buffer)
+ view.setUint16(0, 12)
+ view.setUint32(4, bytes.length)
+ view.setUint32(12, groups.length)
+ for (const [index, [start, end, glyph]] of groups.entries()) {
+ view.setUint32(16 + index * 12, start)
+ view.setUint32(20 + index * 12, end)
+ view.setUint32(24 + index * 12, glyph)
+ }
+ return bytes
+}
+
+/** The Basic Multilingual Plane part, as segments with explicit glyph offsets. */
+function format4(map: CharacterMap): Uint8Array {
+ const segments = groupsOf(new Map([...map].filter(([codePoint]) => codePoint <= 0xfffe)))
+ segments.push([0xffff, 0xffff, 0])
+ const count = segments.length
+ const bytes = new Uint8Array(16 + count * 8)
+ const view = new DataView(bytes.buffer)
+ const searchPower = 2 ** Math.floor(Math.log2(count))
+ view.setUint16(0, 4)
+ view.setUint16(2, bytes.length)
+ view.setUint16(6, count * 2)
+ view.setUint16(8, searchPower * 2)
+ view.setUint16(10, Math.log2(searchPower))
+ view.setUint16(12, count * 2 - searchPower * 2)
+ const ends = 14
+ const starts = ends + count * 2 + 2
+ const deltas = starts + count * 2
+ const rangeOffsets = deltas + count * 2
+ for (const [index, [start, end, glyph]] of segments.entries()) {
+ view.setUint16(ends + index * 2, end)
+ view.setUint16(starts + index * 2, start)
+ // The final segment maps U+FFFF to the missing glyph.
+ view.setUint16(deltas + index * 2, end === 0xffff ? 1 : (glyph - start) & 0xffff)
+ view.setUint16(rangeOffsets + index * 2, 0)
+ }
+ return bytes
+}
+
+/** A character map with `map` as Unicode BMP (format 4) and full (format 12) subtables. */
+export function writeCharacterMap(map: CharacterMap): Uint8Array {
+ const subtables: Array<[platform: number, encoding: number, data: Uint8Array]> = [
+ [0, 3, format4(map)],
+ [0, 4, format12(map)],
+ [3, 1, format4(map)],
+ [3, 10, format12(map)]
+ ]
+ const header = 4 + subtables.length * 8
+ const bytes = new Uint8Array(
+ header + subtables.reduce((total, [, , data]) => total + data.length, 0)
+ )
+ const view = new DataView(bytes.buffer)
+ view.setUint16(2, subtables.length)
+ let offset = header
+ for (const [index, [platform, encoding, data]] of subtables.entries()) {
+ view.setUint16(4 + index * 8, platform)
+ view.setUint16(6 + index * 8, encoding)
+ view.setUint32(8 + index * 8, offset)
+ bytes.set(data, offset)
+ offset += data.length
+ }
+ return bytes
+}
diff --git a/tools/generate/emoji-metrics/src/generate.ts b/tools/generate/emoji-metrics/src/generate.ts
new file mode 100644
index 000000000..393ae17f4
--- /dev/null
+++ b/tools/generate/emoji-metrics/src/generate.ts
@@ -0,0 +1,31 @@
+/**
+ * Writes the emoji metrics font into `packages/core/assets` from Noto Color Emoji at a
+ * pinned google/fonts commit, with Noto's licence beside it. usage: bun generate.ts
+ */
+import { join } from 'node:path'
+
+import { resolveWorkspaceRoot } from '@open-pencil/package-artifacts-tools'
+
+import { EMOJI_METRICS_ASSET, emojiMetricsFont } from './metrics'
+import { readFontTables, writeFontTables } from './sfnt'
+
+const SOURCE_COMMIT = '142c8963e7606b510c93a644c82a4c4cdeae6ef9'
+const SOURCE = `https://raw.githubusercontent.com/google/fonts/${SOURCE_COMMIT}/ofl/notocoloremoji`
+const COPYRIGHT =
+ 'Copyright 2021 Google Inc. All Rights Reserved. Derived from Noto Color Emoji: metrics only.'
+
+async function download(file: string): Promise<Uint8Array> {
+ const response = await fetch(`${SOURCE}/${file}`)
+ if (!response.ok) throw new Error(`${file}: HTTP ${response.status}`)
+ return new Uint8Array(await response.arrayBuffer())
+}
+
+const [font, licence] = await Promise.all([
+ download('NotoColorEmoji-Regular.ttf'),
+ download('OFL.txt')
+])
+const output = writeFontTables(emojiMetricsFont(readFontTables(font), COPYRIGHT))
+const assets = join(await resolveWorkspaceRoot(import.meta.dir), 'packages/core/assets')
+await Bun.write(join(assets, EMOJI_METRICS_ASSET), output)
+await Bun.write(join(assets, 'OpenPencilEmojiMetrics-OFL.txt'), licence)
+console.log(`${EMOJI_METRICS_ASSET}: ${output.length} bytes`)
diff --git a/tools/generate/emoji-metrics/src/metrics.ts b/tools/generate/emoji-metrics/src/metrics.ts
new file mode 100644
index 000000000..5c43ab834
--- /dev/null
+++ b/tools/generate/emoji-metrics/src/metrics.ts
@@ -0,0 +1,133 @@
+import { readCharacterMap, writeCharacterMap } from './cmap'
+import type { FontTables } from './sfnt'
+
+/** The family the metrics font registers under, which the editor's text layout falls back to. */
+export const EMOJI_METRICS_FAMILY = 'OpenPencil Emoji Metrics'
+
+/** Its file among Core's bundled fonts. */
+export const EMOJI_METRICS_ASSET = 'OpenPencilEmojiMetrics-Regular.ttf'
+
+/** Tables carried over unchanged: how emoji sequences join, and the font's basic metadata. */
+const KEPT_TABLES = ['GSUB', 'OS/2', 'post'] as const
+
+/**
+ * Noto maps ASCII digits, `#`, `*`, and the space for its keycap sequences. Figma draws a keycap
+ * as the text font's character with the combining keycap after it, so those stay with the text
+ * font and the keycap mark takes no room.
+ */
+const TEXT_CHARACTER_LIMIT = 0x80
+const COMBINING_KEYCAP = 0x20e3
+
+/**
+ * The missing glyph's outline: a glyph header with no contours, all zero. Browsers' font
+ * sanitizer rejects an empty `glyf` table, so the font keeps this one valid, empty outline.
+ */
+const NOTDEF_OUTLINE = 10
+
+function required(tables: FontTables, tag: string): Uint8Array {
+ const table = tables.get(tag)
+ if (!table) throw new Error(`The source font has no ${tag} table`)
+ return table
+}
+
+function copy(table: Uint8Array): Uint8Array {
+ return new Uint8Array(table)
+}
+
+function nameTable(family: string, copyright: string): Uint8Array {
+ const records: Array<[number, string]> = [
+ [0, copyright],
+ [1, family],
+ [2, 'Regular'],
+ [3, `${family} Regular`],
+ [4, `${family} Regular`],
+ [5, 'Version 1.000'],
+ [6, family.replaceAll(' ', '') + '-Regular']
+ ]
+ const strings = records.map(([, text]) => {
+ const encoded = new Uint8Array(text.length * 2)
+ const view = new DataView(encoded.buffer)
+ for (let index = 0; index < text.length; index++)
+ view.setUint16(index * 2, text.charCodeAt(index))
+ return encoded
+ })
+ const storageOffset = 6 + records.length * 12
+ const bytes = new Uint8Array(
+ storageOffset + strings.reduce((total, data) => total + data.length, 0)
+ )
+ const view = new DataView(bytes.buffer)
+ view.setUint16(2, records.length)
+ view.setUint16(4, storageOffset)
+ let offset = 0
+ for (const [index, [nameID]] of records.entries()) {
+ const record = 6 + index * 12
+ view.setUint16(record, 3)
+ view.setUint16(record + 2, 1)
+ view.setUint16(record + 4, 0x409)
+ view.setUint16(record + 6, nameID)
+ view.setUint16(record + 8, strings[index].length)
+ view.setUint16(record + 10, offset)
+ bytes.set(strings[index], storageOffset + offset)
+ offset += strings[index].length
+ }
+ return bytes
+}
+
+/**
+ * A font that lays emoji out as Figma does and draws nothing: the source's characters and
+ * sequences, each emoji 1 em wide and every joiner, tag, or selector it gives no width still
+ * without one, and empty outlines. Figma saves every emoji as a 1 em square, in design files and
+ * FigJam alike, so the editor's text layout uses this for emoji and their images are drawn over.
+ */
+export function emojiMetricsFont(source: FontTables, copyright: string): FontTables {
+ const head = copy(required(source, 'head'))
+ const hhea = copy(required(source, 'hhea'))
+ const maxp = copy(required(source, 'maxp'))
+ const sourceMetrics = required(source, 'hmtx')
+ const headView = new DataView(head.buffer)
+ const hheaView = new DataView(hhea.buffer)
+ const unitsPerEm = headView.getUint16(18)
+ const glyphCount = new DataView(maxp.buffer).getUint16(4)
+ const sourceMetricCount = hheaView.getUint16(34)
+ const sourceView = new DataView(
+ sourceMetrics.buffer,
+ sourceMetrics.byteOffset,
+ sourceMetrics.byteLength
+ )
+
+ const characters = new Map(
+ [...readCharacterMap(required(source, 'cmap'))].filter(
+ ([codePoint]) => codePoint >= TEXT_CHARACTER_LIMIT
+ )
+ )
+ const zeroWidth = new Set([characters.get(COMBINING_KEYCAP)])
+
+ const hmtx = new Uint8Array(glyphCount * 4)
+ const hmtxView = new DataView(hmtx.buffer)
+ for (let glyph = 0; glyph < glyphCount; glyph++) {
+ const advance = sourceView.getUint16(Math.min(glyph, sourceMetricCount - 1) * 4)
+ hmtxView.setUint16(glyph * 4, advance > 0 && !zeroWidth.has(glyph) ? unitsPerEm : 0)
+ }
+ // Short offsets, in words: the missing glyph holds the only outline, and every other is empty.
+ headView.setInt16(50, 0)
+ const loca = new Uint8Array((glyphCount + 1) * 2)
+ const locaView = new DataView(loca.buffer)
+ for (let glyph = 1; glyph <= glyphCount; glyph++)
+ locaView.setUint16(glyph * 2, NOTDEF_OUTLINE / 2)
+ hheaView.setUint16(10, unitsPerEm)
+ for (const field of [12, 14, 16]) hheaView.setInt16(field, 0)
+ hheaView.setUint16(34, glyphCount)
+
+ const tables: FontTables = new Map([
+ ['head', head],
+ ['hhea', hhea],
+ ['maxp', maxp],
+ ['hmtx', hmtx],
+ ['loca', loca],
+ ['glyf', new Uint8Array(NOTDEF_OUTLINE)],
+ ['name', nameTable(EMOJI_METRICS_FAMILY, copyright)],
+ ['cmap', writeCharacterMap(characters)]
+ ])
+ for (const tag of KEPT_TABLES) tables.set(tag, copy(required(source, tag)))
+ return tables
+}
diff --git a/tools/generate/emoji-metrics/src/sfnt.ts b/tools/generate/emoji-metrics/src/sfnt.ts
new file mode 100644
index 000000000..8174ae462
--- /dev/null
+++ b/tools/generate/emoji-metrics/src/sfnt.ts
@@ -0,0 +1,61 @@
+/** Reading and writing the table directory of a TrueType font (`sfnt`), which is all it needs. */
+
+export type FontTables = Map<string, Uint8Array>
+
+export function readFontTables(bytes: Uint8Array): FontTables {
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
+ const tables: FontTables = new Map()
+ const count = view.getUint16(4)
+ for (let index = 0; index < count; index++) {
+ const record = 12 + index * 16
+ const tag = String.fromCharCode(...bytes.subarray(record, record + 4))
+ const offset = view.getUint32(record + 8)
+ const length = view.getUint32(record + 12)
+ tables.set(tag, bytes.subarray(offset, offset + length))
+ }
+ return tables
+}
+
+function checksum(data: Uint8Array): number {
+ const padded = new Uint8Array(Math.ceil(data.length / 4) * 4)
+ padded.set(data)
+ const view = new DataView(padded.buffer)
+ let sum = 0
+ for (let offset = 0; offset < padded.length; offset += 4) {
+ sum = (sum + view.getUint32(offset)) >>> 0
+ }
+ return sum
+}
+
+/** A TrueType font holding `tables`, with its checksums and `head` adjustment worked out. */
+export function writeFontTables(tables: FontTables): Uint8Array {
+ const tags = [...tables.keys()].sort()
+ const headerLength = 12 + tags.length * 16
+ let length = headerLength
+ for (const tag of tags) length += Math.ceil((tables.get(tag)?.length ?? 0) / 4) * 4
+ const bytes = new Uint8Array(length)
+ const view = new DataView(bytes.buffer)
+ const searchPower = 2 ** Math.floor(Math.log2(tags.length))
+ view.setUint32(0, 0x00010000)
+ view.setUint16(4, tags.length)
+ view.setUint16(6, searchPower * 16)
+ view.setUint16(8, Math.log2(searchPower))
+ view.setUint16(10, tags.length * 16 - searchPower * 16)
+ let offset = headerLength
+ let headOffset = -1
+ for (const [index, tag] of tags.entries()) {
+ const data = new Uint8Array(tables.get(tag) ?? new Uint8Array())
+ // The checksums count `head`'s adjustment as zero; a source font's own value would skew both.
+ if (tag === 'head') data.fill(0, 8, 12)
+ const record = 12 + index * 16
+ for (let char = 0; char < 4; char++) bytes[record + char] = tag.charCodeAt(char)
+ view.setUint32(record + 4, checksum(data))
+ view.setUint32(record + 8, offset)
+ view.setUint32(record + 12, data.length)
+ bytes.set(data, offset)
+ if (tag === 'head') headOffset = offset
+ offset += Math.ceil(data.length / 4) * 4
+ }
+ if (headOffset >= 0) view.setUint32(headOffset + 8, (0xb1b0afba - checksum(bytes)) >>> 0)
+ return bytes
+}
diff --git a/tools/generate/emoji-metrics/tests/metrics.test.ts b/tools/generate/emoji-metrics/tests/metrics.test.ts
new file mode 100644
index 000000000..3259dd46c
--- /dev/null
+++ b/tools/generate/emoji-metrics/tests/metrics.test.ts
@@ -0,0 +1,47 @@
+import { expect, test } from 'bun:test'
+import { readFile } from 'node:fs/promises'
+import { join } from 'node:path'
+
+import { readCharacterMap } from '#emoji-metrics/cmap'
+import { EMOJI_METRICS_ASSET, EMOJI_METRICS_FAMILY } from '#emoji-metrics/metrics'
+import { readFontTables } from '#emoji-metrics/sfnt'
+
+import { resolveWorkspaceRoot } from '@open-pencil/package-artifacts-tools'
+
+async function committedFont() {
+ const root = await resolveWorkspaceRoot(import.meta.dir)
+ return readFontTables(
+ new Uint8Array(await readFile(join(root, 'packages/core/assets', EMOJI_METRICS_ASSET)))
+ )
+}
+
+function required(tables: Map<string, Uint8Array>, tag: string): DataView {
+ const table = tables.get(tag)
+ if (!table) throw new Error(`missing ${tag}`)
+ return new DataView(table.buffer, table.byteOffset, table.byteLength)
+}
+
+test('the committed metrics font gives each emoji 1 em and its joiners none', async () => {
+ const tables = await committedFont()
+ const unitsPerEm = required(tables, 'head').getUint16(18)
+ const hmtx = required(tables, 'hmtx')
+ const characters = readCharacterMap(tables.get('cmap') ?? new Uint8Array())
+ const advance = (codePoint: number) => {
+ const glyph = characters.get(codePoint)
+ return glyph === undefined ? undefined : hmtx.getUint16(glyph * 4)
+ }
+
+ expect(advance(0x1f600)).toBe(unitsPerEm)
+ expect(advance(0x1f1fa)).toBe(unitsPerEm)
+ expect(advance(0x200d)).toBe(0)
+ expect(advance(0x20e3)).toBe(0)
+ // Keycaps keep the text font's character, as Figma draws them.
+ expect([...characters.keys()].some((codePoint) => codePoint < 0x80)).toBe(false)
+ expect(tables.has('CBDT') || tables.has('COLR') || tables.has('SVG ')).toBe(false)
+})
+
+test('the committed metrics font registers under the family Core falls back to', async () => {
+ const name = (await committedFont()).get('name') ?? new Uint8Array()
+ const text = new TextDecoder('utf-16be').decode(name)
+ expect(text).toContain(EMOJI_METRICS_FAMILY)
+})