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.

fix: text is cropped when rendered in italic style with background (#1862)

Ran Luo80f43b877559
diff --git a/examples/src/data/docs/default-document-data-cn.ts b/examples/src/data/docs/default-document-data-cn.ts
index e1a45e7a7d..721b335d37 100644
--- a/examples/src/data/docs/default-document-data-cn.ts
+++ b/examples/src/data/docs/default-document-data-cn.ts
@@ -37,6 +37,7 @@ export const DEFAULT_DOCUMENT_DATA_CN: IDocumentData = {
bg: {
rgb: '#FF6670',
},
+ it: BooleanNumber.TRUE,
},
},
{
diff --git a/packages/engine-render/src/basics/i-document-skeleton-cached.ts b/packages/engine-render/src/basics/i-document-skeleton-cached.ts
index 49a628b174..6d5d800a49 100644
--- a/packages/engine-render/src/basics/i-document-skeleton-cached.ts
+++ b/packages/engine-render/src/basics/i-document-skeleton-cached.ts
@@ -216,7 +216,9 @@ export interface IDocumentSkeletonDrawing {
export interface IDocumentSkeletonFontStyle {
fontString: string;
fontSize: number;
+ originFontSize: number;
fontFamily: string;
+ fontCache: string;
}
export interface IDocumentSkeletonBoundingBox {
diff --git a/packages/engine-render/src/basics/interfaces.ts b/packages/engine-render/src/basics/interfaces.ts
index 9214adcfdf..70e609004e 100644
--- a/packages/engine-render/src/basics/interfaces.ts
+++ b/packages/engine-render/src/basics/interfaces.ts
@@ -132,8 +132,8 @@ export interface IFontCreateConfig {
fontStyle: IDocumentSkeletonFontStyle;
textStyle: ITextStyle;
charSpace: number;
- gridType?: GridType;
snapToGrid: BooleanNumber;
+ gridType?: GridType;
pageWidth?: number;
}
diff --git a/packages/engine-render/src/basics/tools.ts b/packages/engine-render/src/basics/tools.ts
index 381782fc2f..dcf6e9a63f 100644
--- a/packages/engine-render/src/basics/tools.ts
+++ b/packages/engine-render/src/basics/tools.ts
@@ -30,6 +30,7 @@ import * as cjk from 'cjk-regex';
import { FontCache } from '../components/docs/layout/shaping-engine/font-cache';
import { DEFAULT_FONTFACE_PLANE } from './const';
import type { IBoundRectNoAngle } from './vector2';
+import type { IDocumentSkeletonFontStyle } from './i-document-skeleton-cached';
const DEG180 = 180;
@@ -248,10 +249,11 @@ export const UNIVER_GLOBAL_DEFAULT_FONT_SIZE = 11;
export const UNIVER_GLOBAL_DEFAULT_FONT_FAMILY = 'Arial';
-export function getFontStyleString(textStyle?: IStyleBase, localeService?: LocaleService) {
- // 获取字体配置
-
- // TODO: @jikkai @DR-Univer should read default font from configuration, not from locale service
+// eslint-disable-next-line max-lines-per-function
+export function getFontStyleString(
+ textStyle?: IStyleBase,
+ _localeService?: LocaleService
+): IDocumentSkeletonFontStyle {
const defaultFont = UNIVER_GLOBAL_DEFAULT_FONT_FAMILY;
const defaultFontSize = UNIVER_GLOBAL_DEFAULT_FONT_SIZE;
@@ -263,6 +265,7 @@ export function getFontStyleString(textStyle?: IStyleBase, localeService?: Local
fontCache: fontString,
fontString,
fontSize: defaultFontSize,
+ originFontSize: defaultFontSize,
fontFamily: defaultFont,
};
}
@@ -283,10 +286,11 @@ export function getFontStyleString(textStyle?: IStyleBase, localeService?: Local
}
// font-size/line-height
- let fontSize = defaultFontSize;
+ let originFontSize = defaultFontSize;
if (textStyle.fs) {
- fontSize = Math.ceil(textStyle.fs);
+ originFontSize = Math.ceil(textStyle.fs);
}
+ let fontSize = originFontSize;
let fontFamilyResult = defaultFont;
if (textStyle.ff) {
@@ -329,6 +333,7 @@ export function getFontStyleString(textStyle?: IStyleBase, localeService?: Local
fontCache: fontStringPure,
fontString,
fontSize,
+ originFontSize,
fontFamily: fontFamilyResult,
};
}
diff --git a/packages/engine-render/src/components/docs/document.ts b/packages/engine-render/src/components/docs/document.ts
index 59c047727d..41d5227a51 100644
--- a/packages/engine-render/src/components/docs/document.ts
+++ b/packages/engine-render/src/components/docs/document.ts
@@ -221,6 +221,9 @@ export class Documents extends DocComponent {
extension.clearCache();
}
+ const backgroundExtension = extensions.find((e) => e.uKey === 'DefaultDocsBackgroundExtension');
+ const glyphExtensionsExcludeBackground = extensions.filter((e) => e.type === DOCS_EXTENSION_TYPE.SPAN && e.uKey !== 'DefaultDocsBackgroundExtension');
+
// broadcasting the pageTop and pageLeft for each page in the document with multiple pages.
let pageTop = 0;
let pageLeft = 0;
@@ -392,6 +395,39 @@ export class Documents extends DocComponent {
this._drawLiquid.translateSave();
this._drawLiquid.translateDivide(divide);
+ // Draw text background.
+ for (const glyph of glyphGroup) {
+ if (!glyph.content || glyph.content.length === 0) {
+ continue;
+ }
+
+ const { width: spanWidth, left: spanLeft } = glyph;
+
+ const { x: translateX, y: translateY } = this._drawLiquid;
+
+ const originTranslate = Vector2.create(translateX, translateY);
+
+ const centerPoint = Vector2.create(spanWidth / 2, lineHeight / 2);
+
+ const spanStartPoint = calculateRectRotate(
+ originTranslate.addByPoint(spanLeft, 0),
+ centerPoint,
+ centerAngle,
+ vertexAngle,
+ alignOffset
+ );
+
+ const extensionOffset: IExtensionConfig = {
+ spanStartPoint,
+ };
+
+ if (backgroundExtension) {
+ backgroundExtension.extensionOffset = extensionOffset;
+ backgroundExtension.draw(ctx, parentScale, glyph);
+ }
+ }
+
+ // Draw text\border\lines etc.
for (const glyph of glyphGroup) {
if (!glyph.content || glyph.content.length === 0) {
continue;
@@ -433,13 +469,12 @@ export class Documents extends DocComponent {
renderConfig,
};
- for (const extension of extensions) {
- if (extension.type === DOCS_EXTENSION_TYPE.SPAN) {
- extension.extensionOffset = extensionOffset;
- extension.draw(ctx, parentScale, glyph);
- }
+ for (const extension of glyphExtensionsExcludeBackground) {
+ extension.extensionOffset = extensionOffset;
+ extension.draw(ctx, parentScale, glyph);
}
}
+
this._drawLiquid.translateRestore();
}
this._drawLiquid.translateRestore();
diff --git a/packages/engine-render/src/components/docs/extensions/background.ts b/packages/engine-render/src/components/docs/extensions/background.ts
index 51da3b74e0..90c0d31a83 100644
--- a/packages/engine-render/src/components/docs/extensions/background.ts
+++ b/packages/engine-render/src/components/docs/extensions/background.ts
@@ -54,13 +54,13 @@ export class Background extends docExtension {
const background = getColorStyle(bg);
- // console.log('background', content, background, textStyle);
-
const { spanStartPoint = Vector2.create(0, 0) } = this.extensionOffset;
+ const DELTA = 1.5;
+
if (background) {
ctx.fillStyle = background;
- ctx.fillRectByPrecision(spanStartPoint.x - 0.5, spanStartPoint.y, width + 1, contentHeight + 1);
+ ctx.fillRectByPrecision(spanStartPoint.x - 0.5, spanStartPoint.y - 1, width + 1, contentHeight + 2 * DELTA);
}
}
diff --git a/packages/engine-render/src/components/docs/layout/model/glyph.ts b/packages/engine-render/src/components/docs/layout/model/glyph.ts
index bc837ff656..ce6e719380 100644
--- a/packages/engine-render/src/components/docs/layout/model/glyph.ts
+++ b/packages/engine-render/src/components/docs/layout/model/glyph.ts
@@ -158,7 +158,7 @@ export function _createSkeletonWordOrLetter(
let xOffset = 0;
if (glyphInfo && glyphInfo.boundingBox && glyphInfo.font) {
- bBox = FontCache.getBBoxFromGlyphInfo(glyphInfo, fontStyle.fontSize);
+ bBox = FontCache.getBBoxFromGlyphInfo(glyphInfo, fontStyle);
} else {
bBox = FontCache.getTextSize(content, fontStyle);
}
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.ts
index 6275f79d16..3785b8fcb9 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
@@ -195,17 +195,17 @@ export class FontCache {
// content = '0';
// }
const measureText = this.getMeasureText(content, fontString);
- bBox = this._calculateBoundingBoxByMeasureText(measureText, fontSize);
+ bBox = this._calculateBoundingBoxByMeasureText(measureText, fontStyle);
}
return bBox;
}
- static getBBoxFromGlyphInfo(glyphInfo: IOpenTypeGlyphInfo, fontSize: number) {
+ static getBBoxFromGlyphInfo(glyphInfo: IOpenTypeGlyphInfo, fontStyle: IDocumentSkeletonFontStyle) {
const glyph = glyphInfo.glyph!;
const font = glyphInfo.font!;
const { y1, y2 } = glyphInfo.boundingBox!;
- const scale = ptToPixel(fontSize) / font.unitsPerEm;
+ const scale = ptToPixel(fontStyle.fontSize) / font.unitsPerEm;
const { ascender, descender } = font;
@@ -215,7 +215,7 @@ export class FontCache {
fontBoundingBoxDescent: Math.abs(descender * scale),
actualBoundingBoxAscent: y2 * scale,
actualBoundingBoxDescent: Math.abs(y1 * scale),
- }, fontSize);
+ }, fontStyle);
}
// 获取有值单元格文本大小
@@ -349,27 +349,32 @@ export class FontCache {
};
}
- private static _calculateBoundingBoxByMeasureText(textCache: IMeasureTextCache, fontSize: number) {
+ private static _calculateBoundingBoxByMeasureText(textCache: IMeasureTextCache, fontStyle: IDocumentSkeletonFontStyle) {
const {
width,
fontBoundingBoxAscent,
fontBoundingBoxDescent,
- actualBoundingBoxAscent,
- actualBoundingBoxDescent,
+ actualBoundingBoxAscent: aba,
+ actualBoundingBoxDescent: abd,
} = textCache;
+ const { fontSize, originFontSize } = fontStyle;
+ const scale = originFontSize / fontSize;
+ const ba = fontBoundingBoxAscent * scale;
+ const bd = fontBoundingBoxDescent * scale;
+
return {
width,
- ba: fontBoundingBoxAscent,
- bd: fontBoundingBoxDescent,
- aba: actualBoundingBoxAscent,
- abd: actualBoundingBoxDescent,
- sp: (fontBoundingBoxAscent + fontBoundingBoxDescent) / 2,
+ ba,
+ bd,
+ aba,
+ abd,
+ sp: (ba + bd) / 2,
sbr: 0.6,
spr: 0.6,
// https://en.wikipedia.org/wiki/Subscript_and_superscript Microsoft Word 2015
- sbo: (fontBoundingBoxAscent + fontBoundingBoxDescent) * 0.141,
- spo: (fontBoundingBoxAscent + fontBoundingBoxDescent) * 0.4,
+ sbo: (ba + bd) * 0.141,
+ spo: (ba + bd) * 0.4,
};
}
}
diff --git a/packages/engine-render/src/components/docs/layout/tools.ts b/packages/engine-render/src/components/docs/layout/tools.ts
index 0cfcd192e3..1b462e5c40 100644
--- a/packages/engine-render/src/components/docs/layout/tools.ts
+++ b/packages/engine-render/src/components/docs/layout/tools.ts
@@ -38,6 +38,7 @@ import { DEFAULT_DOCUMENT_FONTSIZE } from '../../../basics/const';
import type {
IDocumentSkeletonColumn,
IDocumentSkeletonDivide,
+ IDocumentSkeletonFontStyle,
IDocumentSkeletonGlyph,
IDocumentSkeletonLine,
IDocumentSkeletonPage,
@@ -662,11 +663,7 @@ export function getGlyphGroupWidth(divide: IDocumentSkeletonDivide) {
}
interface IFontCreateConfig {
- fontStyle: {
- fontString: string;
- fontSize: number;
- fontFamily: string;
- };
+ fontStyle: IDocumentSkeletonFontStyle;
textStyle: ITextStyle;
charSpace: number;
gridType: GridType;