Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(canvas): keep the text defaults' font active

Josh Corbett2460cf4f578f
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
index 19761b8a8d..a08cd3c863 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
@@ -122,6 +122,7 @@ import {
type EngineStores,
type ScalarStore,
type TextStylePatch,
+ type TextToolOptions,
} from '@workbench/canvas-engine/engineStores';
import {
exportRasterComposite as exportRasterCompositeWithDeps,
@@ -1780,11 +1781,16 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
let fontSourceStacks: CanvasDocumentContractV3['stacks'] | null = null;
let documentFontSources: CanvasTextSource[] = [];
let draftFontSource: CanvasTextSource | undefined;
+ let defaultsFontOptions: TextToolOptions | undefined;
+ let defaultsFontSource: CanvasTextSource | undefined;
syncActiveFontSources = () => {
const document = mutationPort.getCanvasState()?.document;
const stacks = document?.stacks ?? null;
const draft = document ? stores.textEditSession.get()?.source : undefined;
- if (stacks === fontSourceStacks && draft === draftFontSource) {
+ // The text defaults are active too: the pane previews them, and the next
+ // created text rasterizes with them.
+ const options = document ? stores.textOptions.get() : undefined;
+ if (stacks === fontSourceStacks && draft === draftFontSource && options === defaultsFontOptions) {
return;
}
if (stacks !== fontSourceStacks) {
@@ -1795,11 +1801,20 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
)
: [];
}
+ if (options !== defaultsFontOptions) {
+ defaultsFontOptions = options;
+ defaultsFontSource = options ? { ...options, color: '#000000', content: '', type: 'text' } : undefined;
+ }
draftFontSource = draft;
- fontLoader.setActiveSources(draft ? [...documentFontSources, draft] : documentFontSources);
+ fontLoader.setActiveSources([
+ ...documentFontSources,
+ ...(draft ? [draft] : []),
+ ...(defaultsFontSource ? [defaultsFontSource] : []),
+ ]);
};
syncActiveFontSources();
const unsubscribeTextFontSources = stores.textEditSession.subscribe(syncActiveFontSources);
+ const unsubscribeTextDefaultsFontSources = stores.textOptions.subscribe(syncActiveFontSources);
for (const layer of getDocumentLeaves(mirror.getDocument())) {
rasterController.setThumbnailKey(layer.id, getLayerThumbnailDisplayKey(layer));
@@ -2699,6 +2714,7 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
cleanup.run(unsubscribeRuleOfThirds);
cleanup.run(unsubscribeProjectPreviewLifecycle);
cleanup.run(unsubscribeTextFontSources);
+ cleanup.run(unsubscribeTextDefaultsFontSources);
cleanup.run(() => mutationContext.dispose());
cleanup.run(unsubscribeHistoryEpoch);
cleanup.run(() => historyController.dispose());
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/fontReferences.engine.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/fontReferences.engine.test.ts
index 038af8b35f..006acf767a 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/fontReferences.engine.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/fontReferences.engine.test.ts
@@ -8,6 +8,7 @@ import { describe, expect, it, vi } from 'vitest';
import type { CanvasDocumentContractV3, CanvasLayerContract, CanvasTextFontRef } from './contracts';
import type { BitmapStore } from './document/bitmapStore';
+import type { CanvasFontRuntime } from './render/fontLoader';
import { documentFrom, groupContract, layerContract } from './document-model/documentFixtures.testStub';
import { createCanvasEngine } from './engine';
@@ -53,7 +54,7 @@ const createBitmapStoreStub = (): BitmapStore => ({
suspendLayer: () => () => undefined,
});
-const createHarness = (document: CanvasDocumentContractV3) => {
+const createHarness = (document: CanvasDocumentContractV3, fonts: CanvasFontRuntime | null = null) => {
const initial = createInitialWorkbenchState().projects[0]!;
let project: Project = { ...initial, canvas: { ...createEmptyCanvasState(), document } };
const listeners = new Set<() => void>();
@@ -79,7 +80,7 @@ const createHarness = (document: CanvasDocumentContractV3) => {
const composition = createCanvasEngine({
backend: createTestStubRasterBackend(),
bitmapStore: createBitmapStoreStub(),
- fonts: null,
+ fonts,
imageResolver: () => Promise.resolve(new Blob()),
mutationPort,
projectId: project.id,
@@ -150,3 +151,43 @@ describe('Canvas font replacement capability', () => {
engine.lifecycle.dispose();
});
});
+
+describe('Canvas active font sources', () => {
+ const pendingRuntime = () => {
+ const signals: AbortSignal[] = [];
+ const runtime: CanvasFontRuntime = {
+ ensure: (_reference, signal) => {
+ signals.push(signal!);
+ return new Promise<string>(() => {
+ // Never settles: the test observes the signal, not the face.
+ });
+ },
+ ensureForOutput: () => Promise.reject(new Error('unused')),
+ resolveFamily: (reference) => reference.family ?? 'sans-serif',
+ subscribe: () => () => undefined,
+ };
+ return { runtime, signals };
+ };
+
+ it('keeps the text defaults loading through a session open, while an unrelated preview is dropped', () => {
+ const { runtime, signals } = pendingRuntime();
+ const harness = createHarness(documentFrom([]), runtime);
+ const { engine } = harness.composition;
+ engine.interaction.set('textOptions', {
+ ...engine.interaction.get('textOptions'),
+ fontFamily: from.family,
+ fontRef: from,
+ });
+ const defaults = { ...engine.interaction.get('textOptions'), color: '#000000', content: '', type: 'text' as const };
+ void engine.fonts.ensurePreview(defaults).catch(() => undefined);
+ void engine.fonts.ensurePreview({ ...defaults, fontFamily: to.family, fontRef: to }).catch(() => undefined);
+ expect(signals).toHaveLength(2);
+
+ // The session's draft diverges from the defaults; only the defaults keep the first preview alive.
+ engine.layers.openTextCreate({ x: 0, y: 0 });
+ engine.layers.updateTextEditStyle({ fontWeight: 700 });
+ expect(signals[0]!.aborted).toBe(false);
+ expect(signals[1]!.aborted).toBe(true);
+ engine.lifecycle.dispose();
+ });
+});