Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(engine-render): invalidate cached document font metrics (#7635)
Univer9986fd548901
diff --git a/packages/engine-render/src/components/docs/layout/shaping-engine/__tests__/font-cache.spec.ts b/packages/engine-render/src/components/docs/layout/shaping-engine/__tests__/font-cache.spec.tsindex 3b2afc5590..e462e0a3ce 100644--- a/packages/engine-render/src/components/docs/layout/shaping-engine/__tests__/font-cache.spec.ts+++ b/packages/engine-render/src/components/docs/layout/shaping-engine/__tests__/font-cache.spec.ts@@ -15,7 +15,7 @@*/import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';-import { FontCache } from '../font-cache';+import { FontCache, invalidateDocumentFontMetrics } from '../font-cache';describe('font cache', () => {beforeEach(() => {@@ -27,6 +27,56 @@ describe('font cache', () => {afterEach(() => {vi.unstubAllGlobals();+ vi.restoreAllMocks();+ });++ it('remeasures glyph widths and DOM fallback heights after font metrics are invalidated', () => {+ let width = 12;+ let height = 20;+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({+ font: '',+ textBaseline: 'alphabetic',+ measureText: () => ({+ width,+ actualBoundingBoxAscent: height,+ actualBoundingBoxDescent: 0,+ }),+ } as unknown as CanvasRenderingContext2D);+ vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(() => ({+ left: 0,+ top: 0,+ right: width,+ bottom: height,+ x: 0,+ y: 0,+ width,+ height,+ toJSON: () => ({}),+ }));+ const first = FontCache.getMeasureText('Agent', '14px FontChange');+ const unrelated = FontCache.getMeasureText('Agent', '14px Unrelated');+ const unrelatedHeight = FontCache.getTextSizeByDom('Agent', '14px Unrelated');+ const domOnly = FontCache.getTextSizeByDom('Agent', '14px DOMOnly');+ expect(first).toMatchObject({ width: 12, fontBoundingBoxAscent: 20 });+ width = 24;+ height = 40;+ expect(FontCache.getMeasureText('Agent', '14px FontChange')).toBe(first);+ expect(FontCache.getTextSizeByDom('Agent', '14px FontChange').height).toBe(20);++ expect(invalidateDocumentFontMetrics((font) => font === '14px FontChange')).toBe(true);+ const next = FontCache.getMeasureText('Agent', '14px FontChange');+ expect(next).toMatchObject({ width: 24, fontBoundingBoxAscent: 40 });+ expect(next).not.toBe(first);+ expect(FontCache.getTextSizeByDom('Agent', '14px FontChange').height).toBe(40);+ expect(FontCache.getMeasureText('Agent', '14px Unrelated')).toBe(unrelated);+ expect(FontCache.getTextSizeByDom('Agent', '14px Unrelated')).toBe(unrelatedHeight);+ expect(invalidateDocumentFontMetrics((font) => font === '14px DOMOnly')).toBe(true);+ expect(FontCache.getTextSizeByDom('Agent', '14px DOMOnly')).not.toBe(domOnly);+ expect(FontCache.getTextSizeByDom('Agent', '14px DOMOnly').height).toBe(40);+ expect(invalidateDocumentFontMetrics(() => false)).toBe(false);+ expect(invalidateDocumentFontMetrics((font) => font === '14px FontChange')).toBe(true);+ expect(invalidateDocumentFontMetrics((font) => font === '14px FontChange')).toBe(false);+ expect(FontCache.getMeasureText('Agent', '14px FontChange')).toEqual(next);});it('measures text with OffscreenCanvas when the DOM is unavailable', () => {@@ -194,6 +244,7 @@ describe('font cache', () => {}],]);+ invalidateDocumentFontMetrics((font) => font === '12px Local Font');const byFont = FontCache.getTextSize('A', {fontString: '12px Local Font',fontSize: 12,diff --git a/packages/engine-render/src/components/docs/layout/shaping-engine/font-cache.ts b/packages/engine-render/src/components/docs/layout/shaping-engine/font-cache.tsindex cae46559e3..f6fbe8f2dd 100644--- a/packages/engine-render/src/components/docs/layout/shaping-engine/font-cache.ts+++ b/packages/engine-render/src/components/docs/layout/shaping-engine/font-cache.ts@@ -58,6 +58,11 @@ interface IGlyphHorizonData {pixelsPerEm?: number[];}+/** Invalidates selected CSS font keys in both measurement caches; registered font data is retained. */+export function invalidateDocumentFontMetrics(matches: (fontStyle: string) => boolean): boolean {+ return FontCache.invalidateMetrics(matches);+}+export class FontCache {private static _getTextHeightCache: { [key: string]: { width: number; height: number } } = {};@@ -71,6 +76,19 @@ export class FontCache {return this._globalFontMeasureCache;}+ static invalidateMetrics(matches: (fontStyle: string) => boolean): boolean {+ let changed = false;+ const keys = new Set([...this._globalFontMeasureCache.keys(), ...Object.keys(this._getTextHeightCache)]);+ for (const key of keys) {+ if (matches(key)) {+ this._globalFontMeasureCache.delete(key);+ delete this._getTextHeightCache[key];+ changed = true;+ }+ }+ return changed;+ }+static setFontMeasureCache(fontStyle: string, content: string, tm: IMeasureTextCache) {if (!this._globalFontMeasureCache.has(fontStyle)) {this._globalFontMeasureCache.set(fontStyle, new Map());diff --git a/packages/engine-render/src/index.ts b/packages/engine-render/src/index.tsindex c08abbd58d..3402aff49d 100644--- a/packages/engine-render/src/index.ts+++ b/packages/engine-render/src/index.ts@@ -72,6 +72,7 @@ export type {IDocumentLayoutProtectedPageRange,IDocumentLayoutProtectedRange,} from './components/docs/layout/document-layout-types';+export { invalidateDocumentFontMetrics } from './components/docs/layout/shaping-engine/font-cache';export {compareDocumentSkeletonNestedPagePathOrder,documentSkeletonLineIterator,