Skip to content

Commit

Permalink
fix: add fontRenderExtension to fontCache
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Feb 5, 2025
1 parent f08fe80 commit 303a96b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 69 deletions.
34 changes: 17 additions & 17 deletions packages/engine-render/src/components/sheets/extensions/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import type { Documents } from '../../docs/document';
import type { IDrawInfo } from '../../extension';
import type { IFontCacheItem } from '../interfaces';
import type { SheetComponent } from '../sheet-component';
import type { SpreadsheetSkeleton } from '../sheet.render-skeleton';
import { HorizontalAlign, Range, VerticalAlign, WrapStrategy } from '@univerjs/core';
import { FIX_ONE_PIXEL_BLUR_OFFSET } from '../../../basics';
import { VERTICAL_ROTATE_ANGLE } from '../../../basics/text-rotation';
import { clampRange, inViewRanges } from '../../../basics/tools';
import { SpreadsheetExtensionRegistry } from '../../extension';
import { EXPAND_SIZE_FOR_RENDER_OVERFLOW, FONT_EXTENSION_Z_INDEX } from '../constants';
import { getDocsSkeletonPageSize, type SpreadsheetSkeleton } from '../sheet.render-skeleton';
import { getDocsSkeletonPageSize } from '../sheet.render-skeleton';
import { SheetExtension } from './sheet-extension';

const UNIQUE_KEY = 'DefaultFontExtension';
Expand All @@ -53,10 +54,10 @@ interface IRenderFontContext {
diffRanges: IRange[];
spreadsheetSkeleton: SpreadsheetSkeleton;
overflowRectangle: Nullable<IRange>;
cellData: {
fontCache?: Nullable<IFontCacheItem>;
cellDataInterceptor?: Nullable<ICellDataForSheetInterceptor>;
};
/**
* includes documentSkeleton & cellData
*/
fontCache?: Nullable<IFontCacheItem>;
startY: number;
endY: number;
startX: number;
Expand Down Expand Up @@ -109,7 +110,6 @@ export class Font extends SheetExtension {
checkOutOfViewBound: checkOutOfViewBound || true,
diffRanges,
spreadsheetSkeleton,
cellData: {},
} as IRenderFontContext;
ctx.save();

Expand Down Expand Up @@ -138,7 +138,6 @@ export class Font extends SheetExtension {

Range.foreach(range, (row, col) => {
const index = spreadsheetSkeleton.worksheet.getSpanModel().getMergeDataIndex(row, col);
// put all merged cells to another pass to render. -1 means not merged.
if (index !== -1) {
return;
}
Expand Down Expand Up @@ -187,6 +186,7 @@ export class Font extends SheetExtension {

const fontCache = fontMatrix.getValue(row, col);
if (!fontCache) return true;
renderFontCtx.fontCache = fontCache;

//#region overflow
// If the cell is overflowing, but the overflowRectangle has not been set,
Expand All @@ -208,9 +208,12 @@ export class Font extends SheetExtension {
const visibleCol = spreadsheetSkeleton.worksheet.getColVisible(col);
if (!visibleRow || !visibleCol) return true;

const cellData = spreadsheetSkeleton.worksheet.getCell(row, col) as ICellDataForSheetInterceptor || {};
// const cellData = spreadsheetSkeleton.worksheet.getCell(row, col) as ICellDataForSheetInterceptor || {};
// if (row == 12 && col == 4) {
// console.log('renderFontCtx.fontCache?.cellData?.fontRenderExtension?', renderFontCtx.fontCache?.cellData?.fontRenderExtension, 'get', cellData.fontRenderExtension);
// }
// renderFontCtx.cellData.cellDataInterceptor = cellData;
if (cellData.fontRenderExtension?.isSkip) {
if (renderFontCtx.fontCache?.cellData?.fontRenderExtension?.isSkip) {
return true;
}

Expand All @@ -219,7 +222,6 @@ export class Font extends SheetExtension {

//#region text overflow
renderFontCtx.overflowRectangle = overflowRange;
renderFontCtx.cellData = { fontCache };
this._setFontRenderBounds(renderFontCtx, row, col);
//#endregion

Expand All @@ -243,7 +245,6 @@ export class Font extends SheetExtension {
renderFontCtx.endX = 0;
renderFontCtx.endY = 0;
renderFontCtx.overflowRectangle = null;
renderFontCtx.cellData = { fontCache: null };
return false;
};

Expand Down Expand Up @@ -326,12 +327,12 @@ export class Font extends SheetExtension {
* @param fontCache
*/
private _setFontRenderBounds(renderFontContext: IRenderFontContext, row: number, col: number, padding = 0) {
const { ctx, scale, overflowRectangle, rowHeightAccumulation, columnWidthAccumulation, cellData } = renderFontContext;
const { ctx, scale, overflowRectangle, rowHeightAccumulation, columnWidthAccumulation, fontCache } = renderFontContext;
let { startX, endX, startY, endY } = renderFontContext;

// https://github.com/dream-num/univer-pro/issues/334
// When horizontal alignment is not set, the default alignment for rotation angles varies to accommodate overflow scenarios.
const { horizontalAlign, vertexAngle = 0, centerAngle = 0 } = cellData?.fontCache ?? {};
const { horizontalAlign = 0, vertexAngle = 0, centerAngle = 0 } = fontCache as IFontCacheItem;
let horizontalAlignOverFlow = horizontalAlign;
if (horizontalAlign === HorizontalAlign.UNSPECIFIED) {
if (centerAngle === VERTICAL_ROTATE_ANGLE && vertexAngle === VERTICAL_ROTATE_ANGLE) {
Expand All @@ -340,10 +341,9 @@ export class Font extends SheetExtension {
horizontalAlignOverFlow = HorizontalAlign.RIGHT;
}
}
const cellDataInterceptor = renderFontContext.spreadsheetSkeleton.worksheet.getCell(row, col) as ICellDataForSheetInterceptor || {};
// const cellDataInterceptor = cellData.cellDataInterceptor;
const rightOffset = cellDataInterceptor?.fontRenderExtension?.rightOffset ?? 0;
const leftOffset = cellDataInterceptor?.fontRenderExtension?.leftOffset ?? 0;
// const cellDataInterceptor = renderFontContext.spreadsheetSkeleton.worksheet.getCell(row, col) as ICellDataForSheetInterceptor || {};
const rightOffset = fontCache?.cellData?.fontRenderExtension?.rightOffset ?? 0;
const leftOffset = fontCache?.cellData?.fontRenderExtension?.leftOffset ?? 0;
let isOverflow = true;

if (vertexAngle === 0) {
Expand Down
12 changes: 6 additions & 6 deletions packages/engine-render/src/components/sheets/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import type {
BorderStyleTypes,
HorizontalAlign,
ICellDataForSheetInterceptor,
ICellWithCoord,
IFontRenderExtension,

Check failure on line 22 in packages/engine-render/src/components/sheets/interfaces.ts

View workflow job for this annotation

GitHub Actions / eslint

'IFontRenderExtension' is defined but never used
ImageCacheMap,
Nullable,
ObjectMatrix,
VerticalAlign,
WrapStrategy,
Expand All @@ -42,25 +44,23 @@ export interface BorderCacheItem {

export interface IFontCacheItem {
documentSkeleton: DocumentSkeleton;
// marginTop?: number;
// marginBottom?: number;
// marginRight?: number;
// marginLeft?: number;
vertexAngle?: number; // Text rotation offset based on the top-left corner.
centerAngle?: number; // Text rotation based on the center point.
verticalAlign: VerticalAlign;
horizontalAlign: HorizontalAlign;
wrapStrategy: WrapStrategy;
// content?: string;
imageCacheMap: ImageCacheMap;
fontRenderExtension?: IFontRenderExtension;
cellData: Nullable<ICellDataForSheetInterceptor>;
}

type colorString = string;
export interface IStylesCache {
background?: Record<colorString, ObjectMatrix<string>>;
backgroundPositions?: ObjectMatrix<ICellWithCoord>;
font?: Record<string, ObjectMatrix<IFontCacheItem>>;
/**
* Get value from getCell in skeleton and this value is used in font extension
*/
fontMatrix: ObjectMatrix<IFontCacheItem>;
border?: ObjectMatrix<BorderCache>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ import type { IDocumentSkeletonColumn } from '../../basics/i-document-skeleton-c
import type { ITransformChangeState } from '../../basics/interfaces';
import type { IBoundRectNoAngle, IViewportInfo } from '../../basics/vector2';
import type { Scene } from '../../scene';
import type { BorderCache, IFontCacheItem, IStylesCache } from './interfaces';
import {
addLinkToDocumentModel,
AUTO_HEIGHT_FOR_MERGED_CELLS,
BooleanNumber,
CellValueType,
composeStyles,
Expand Down Expand Up @@ -77,7 +77,7 @@ import { DocumentSkeleton } from '../docs/layout/doc-skeleton';
import { columnIterator } from '../docs/layout/tools';
import { DocumentViewModel } from '../docs/view-model/document-view-model';
import { EXPAND_SIZE_FOR_RENDER_OVERFLOW, MEASURE_EXTENT, MEASURE_EXTENT_FOR_PARAGRAPH } from './constants';
import { type BorderCache, type IFontCacheItem, type IStylesCache, SHEET_VIEWPORT_KEY } from './interfaces';
import { SHEET_VIEWPORT_KEY } from './interfaces';
import { createDocumentModelWithStyle, extractOtherStyle, getFontFormat } from './util';

interface ICellDocumentModelOption {
Expand Down Expand Up @@ -1210,39 +1210,59 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
}
}

_setFontStylesCache(row: number, col: number, cell: Nullable<ICellDataForSheetInterceptor>) {
if (isNullCell(cell)) return;
_setFontStylesCache(row: number, col: number, cellData: Nullable<ICellDataForSheetInterceptor>) {
if (isNullCell(cellData)) return;

this._handleFontMatrix.setValue(row, col, true);
if (this._stylesCache.fontMatrix.getValue(row, col)) return;
let config: Partial<IFontCacheItem> = {
cellData,
imageCacheMap: this._imageCacheMap,
};

const cacheValue = this._stylesCache.fontMatrix.getValue(row, col);
if (!cacheValue) {
this._stylesCache.fontMatrix.setValue(row, col, config as IFontCacheItem);
} else {
const cacheItem = cacheValue as IFontCacheItem;
cacheItem.cellData = cellData;
this._stylesCache.fontMatrix.setValue(row, col, cacheValue as IFontCacheItem);
return;
}

const modelObject = this._getCellDocumentModel(cell, {
const modelObject = this._getCellDocumentModel(cellData, {
displayRawFormula: this._renderRawFormula,
});
if (modelObject == null) return;
const { documentModel } = modelObject;
if (documentModel == null) return;

const { fontString: _fontString, textRotation, wrapStrategy, verticalAlign, horizontalAlign } = modelObject;
if (modelObject) {
const { documentModel } = modelObject;
if (documentModel) {
const { fontString: _fontString, textRotation, wrapStrategy, verticalAlign, horizontalAlign } = modelObject;
const documentViewModel = new DocumentViewModel(documentModel);
if (documentViewModel) {
const { vertexAngle, centerAngle } = convertTextRotation(textRotation);
const documentSkeleton = DocumentSkeleton.create(documentViewModel, this._localeService);
documentSkeleton.calculate();

config = {
documentSkeleton,
vertexAngle,
centerAngle,
verticalAlign,
horizontalAlign,
wrapStrategy,
imageCacheMap: this._imageCacheMap,
cellData,
};
this._calculateOverflowCell(row, col, config as IFontCacheItem);
this._handleFontMatrix.setValue(row, col, true);
}
}
}

const documentViewModel = new DocumentViewModel(documentModel);
if (documentViewModel) {
const { vertexAngle, centerAngle } = convertTextRotation(textRotation);
const documentSkeleton = DocumentSkeleton.create(documentViewModel, this._localeService);
documentSkeleton.calculate();
// if (row == 12 && col == 4) {
// console.log('fontMatrix.setValue_______', config.cellData?.fontRenderExtension);
// }

const config: IFontCacheItem = {
documentSkeleton,
vertexAngle,
centerAngle,
verticalAlign,
horizontalAlign,
wrapStrategy,
imageCacheMap: this._imageCacheMap,
};
this._stylesCache.fontMatrix.setValue(row, col, config);
this._calculateOverflowCell(row, col, config);
}
this._stylesCache.fontMatrix.setValue(row, col, config as IFontCacheItem);
this._handleFontMatrix.setValue(row, col, true);
}

/**
Expand All @@ -1260,7 +1280,7 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
// const handledBgCell = Tools.isDefine(this._handleBgMatrix.getValue(row, col));
// const handledBorderCell = Tools.isDefine(this._handleBorderMatrix.getValue(row, col));

// worksheet.getCell has significant performance overhead, if we had handled this cell then return first.
// // worksheet.getCell has significant performance overhead, if we had handled this cell then return first.
// if (handledBgCell && handledBorderCell) {
// return;
// }
Expand All @@ -1286,7 +1306,12 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
}

const cell = this.worksheet.getCell(row, col) || this.worksheet.getCellRaw(row, col);

if (row == 12 && col == 4) {

Check failure on line 1309 in packages/engine-render/src/components/sheets/sheet.render-skeleton.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected '===' and instead saw '=='

Check failure on line 1309 in packages/engine-render/src/components/sheets/sheet.render-skeleton.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected '===' and instead saw '=='
console.log('sksksksk', (cell as ICellDataForSheetInterceptor).fontRenderExtension);

Check failure on line 1310 in packages/engine-render/src/components/sheets/sheet.render-skeleton.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement. Only these console methods are allowed: warn, error
if ((cell as ICellDataForSheetInterceptor).fontRenderExtension) {
// debugger;
}
}
const cellStyle = this._styles.getStyleByCell(cell);
const columnStyle = this.worksheet.getColumnStyle(col) as IStyleData;
const rowStyle = this.worksheet.getRowStyle(row) as IStyleData;
Expand All @@ -1298,7 +1323,6 @@ export class SpreadsheetSkeleton extends SheetSkeleton {

this._setBgStylesCache(row, col, style, options);
this._setBorderStylesCache(row, col, style, options);

this._setFontStylesCache(row, col, { ...cell, ...{ s: style } });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class HeaderFreezeRenderController extends Disposable implements IRenderM
this._zoomRefresh();
}

// eslint-disable-next-line max-lines-per-function
// eslint-disable-next-line max-lines-per-function, complexity
private _createFreeze(
freezeDirectionType: FREEZE_DIRECTION_TYPE = FREEZE_DIRECTION_TYPE.ROW,
freezeConfig?: IFreeze
Expand Down Expand Up @@ -238,7 +238,9 @@ export class HeaderFreezeRenderController extends Disposable implements IRenderM

this._rowFreezeHeaderRect = new Rect(FREEZE_ROW_HEADER_NAME, {
fill: this._freezeNormalHeaderColor, width: rowHeaderWidthAndMarginLeft,
height: FREEZE_SIZE, left: 0, top: startY - FREEZE_OFFSET, zIndex: 3,
height: FREEZE_SIZE, left: 0,
top: startY - FREEZE_OFFSET,
zIndex: 3,
});

let fill = this._freezeNormalHeaderColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,18 +361,19 @@ export class SheetRenderController extends RxDisposable implements IRenderModule
height: columnHeaderHeight,
});

const rowFreezeHeaderRect = this._context.scene.getObject(FREEZE_ROW_HEADER_NAME);
if (rowFreezeHeaderRect) {
rowFreezeHeaderRect.transformByState({
top: columnHeaderHeight - rowFreezeHeaderRect.height,
});
}
const colFreezeHeaderRect = this._context.scene.getObject(FREEZE_COLUMN_HEADER_NAME);
if (colFreezeHeaderRect) {
colFreezeHeaderRect.transformByState({
height: columnHeaderHeight,
});
}
// no need to update freezelineRect, freeze render controller has handled.
// const rowFreezeHeaderRect = this._context.scene.getObject(FREEZE_ROW_HEADER_NAME);
// if (rowFreezeHeaderRect) {
// rowFreezeHeaderRect.transformByState({
// top: columnHeaderHeight - rowFreezeHeaderRect.height,
// });
// }
// const colFreezeHeaderRect = this._context.scene.getObject(FREEZE_COLUMN_HEADER_NAME);
// if (colFreezeHeaderRect) {
// colFreezeHeaderRect.transformByState({
// height: columnHeaderHeight,
// });
// }
}));
}

Expand Down

0 comments on commit 303a96b

Please sign in to comment.