Skip to content

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.

feat(engine-render): support custom range glyph width (#7199)

白熱7e1307195866
diff --git a/packages/core/src/types/interfaces/i-document-data-interceptor.ts b/packages/core/src/types/interfaces/i-document-data-interceptor.ts
index be3f47599d..d21c94e177 100644
--- a/packages/core/src/types/interfaces/i-document-data-interceptor.ts
+++ b/packages/core/src/types/interfaces/i-document-data-interceptor.ts
@@ -18,6 +18,7 @@ import type { ICustomDecoration, ICustomRange, IParagraph } from './i-document-d
export interface ICustomRangeForInterceptor extends ICustomRange {
active?: boolean;
+ glyphWidthEm?: number;
show?: boolean;
}
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/language-ruler.spec.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/language-ruler.spec.ts
index cceb019fec..e45e4ab42b 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/language-ruler.spec.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/language-ruler.spec.ts
@@ -16,7 +16,7 @@
import type { ISectionBreakConfig } from '../../../../../../basics/interfaces';
import { DocumentDataModel } from '@univerjs/core';
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
import { DocumentViewModel } from '../../../../view-model/document-view-model';
import { ArabicHandler, emojiHandler, otherHandler, ThaiHandler, TibetanHandler } from '../language-ruler';
@@ -100,6 +100,24 @@ describe('language-ruler', () => {
expect(result.step).toBe(2);
expect(result.glyphGroup.length).toBe(2);
});
+
+ it('uses runtime custom range width for placeholder glyphs', () => {
+ const { viewModel } = createViewModel('i');
+ const paragraphNode = getParagraphNode(viewModel);
+ const paragraph = getParagraph(viewModel);
+ const sectionBreakConfig = createSectionBreakConfig();
+ vi.spyOn(viewModel, 'getCustomRange').mockReturnValue({
+ endIndex: 0,
+ glyphWidthEm: 3,
+ rangeId: 'formula-1',
+ startIndex: 0,
+ } as never);
+
+ const result = otherHandler(0, 'i', viewModel, paragraphNode, sectionBreakConfig, paragraph);
+ const glyph = result.glyphGroup[0];
+
+ expect(glyph.width).toBeCloseTo(glyph.fontStyle!.originFontSize * 3);
+ });
});
describe('ArabicHandler', () => {
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/language-ruler.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/language-ruler.ts
index 00d3cb57cd..4ab9da2fc2 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/language-ruler.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/language-ruler.ts
@@ -27,7 +27,7 @@ import {
startWithEmoji,
} from '../../../../../basics/tools';
import { createSkeletonLetterGlyph, createSkeletonWordGlyph } from '../../model/glyph';
-import { getFontCreateConfig } from '../../tools';
+import { getCustomRangeGlyphWidth, getFontCreateConfig } from '../../tools';
// Handle English word, English punctuation, number characters.
// https://en.wikipedia.org/wiki/CJK_characters
@@ -55,7 +55,7 @@ export function otherHandler(
}
const config = getFontCreateConfig(index + step, viewModel, paragraphNode, sectionBreakConfig, paragraph);
- const glyph = createSkeletonLetterGlyph(char, config);
+ const glyph = createSkeletonLetterGlyph(char, config, getCustomRangeGlyphWidth(index + step, viewModel, paragraphNode, config));
glyphGroup.push(glyph);
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/shaping.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/shaping.ts
index 9941ed719e..880c4c24d7 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/shaping.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/shaping.ts
@@ -49,7 +49,7 @@ import { getBoundingBox } from '../../model/line';
import { fontLibrary } from '../../shaping-engine/font-library';
import { textShape } from '../../shaping-engine/text-shaping';
import { prepareParagraphBody } from '../../shaping-engine/utils';
-import { getCharSpaceApply, getFontCreateConfig } from '../../tools';
+import { getCharSpaceApply, getCustomRangeGlyphWidth, getFontCreateConfig } from '../../tools';
import { ArabicHandler, emojiHandler, otherHandler, ThaiHandler, TibetanHandler } from './language-ruler';
// Now we apply consecutive punctuation adjustment, specified in Chinese Layout
@@ -210,7 +210,12 @@ export function shaping(
const newSpan = createSkeletonLetterGlyph(char, config);
shapedGlyphs.push(newSpan);
} else {
- const newSpan = createSkeletonLetterGlyph(char, config, undefined, glyphInfo);
+ const newSpan = createSkeletonLetterGlyph(
+ char,
+ config,
+ getCustomRangeGlyphWidth(start, viewModel, paragraphNode, config),
+ glyphInfo
+ );
shapedGlyphs.push(newSpan);
}
}
@@ -265,7 +270,11 @@ export function shaping(
if (zeroWidthParagraphBreak === BooleanNumber.TRUE) {
newGlyph = createSkeletonLetterGlyph(char, config, 0);
} else {
- newGlyph = createSkeletonLetterGlyph(char, config);
+ newGlyph = createSkeletonLetterGlyph(
+ char,
+ config,
+ getCustomRangeGlyphWidth(i, viewModel, paragraphNode, config)
+ );
}
} else {
newGlyph = createSkeletonLetterGlyph(char, config);
diff --git a/packages/engine-render/src/components/docs/layout/tools.ts b/packages/engine-render/src/components/docs/layout/tools.ts
index 206849373d..36da95a77a 100644
--- a/packages/engine-render/src/components/docs/layout/tools.ts
+++ b/packages/engine-render/src/components/docs/layout/tools.ts
@@ -1509,6 +1509,21 @@ export function getFontCreateConfig(
return result;
}
+export function getCustomRangeGlyphWidth(
+ index: number,
+ viewModel: DocumentViewModel,
+ paragraphNode: DataStreamTreeNode,
+ config: IFontCreateConfig
+): number | undefined {
+ const customRange = viewModel.getCustomRange(index + paragraphNode.startIndex);
+ const glyphWidthEm = customRange?.glyphWidthEm;
+ if (typeof glyphWidthEm !== 'number' || !Number.isFinite(glyphWidthEm) || glyphWidthEm < 0) {
+ return undefined;
+ }
+
+ return glyphWidthEm * config.fontStyle.originFontSize;
+}
+
// Generate an empty doc skeleton with the initial states.
export function getNullSkeleton(): IDocumentSkeletonCached {
return {