Skip to content

Commit

Permalink
feat(sheet): add font method (#1506)
Browse files Browse the repository at this point in the history
* feat(sheet): add font method

* fix(sheet): type error
  • Loading branch information
DR-Univer authored Mar 7, 2024
1 parent 7c28761 commit 9cf028f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/shared/__test__/object-matrix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('test ObjectMatrix', () => {
2: { 1: '111', 2: '121', 3: '313' },
});
});
it('test moveMatrixArray', () => {
it('test moveMatrixArray2', () => {
const primitiveObject = getPrimitiveObj();
moveMatrixArray(2, 1, 0, primitiveObject);
expect(primitiveObject).toStrictEqual({
Expand Down
48 changes: 37 additions & 11 deletions packages/engine-render/src/components/sheets/extensions/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { IColorStyle, IRange, IScale } from '@univerjs/core';
import type { IRange, IScale } from '@univerjs/core';
import { HorizontalAlign, ObjectMatrix, WrapStrategy } from '@univerjs/core';

import type { UniverRenderingContext } from '../../../context';
Expand All @@ -23,31 +23,48 @@ import { SpreadsheetExtensionRegistry } from '../../extension';
import type { IFontCacheItem } from '../interfaces';
import type { SheetComponent } from '../sheet-component';
import { getDocsSkeletonPageSize, type SpreadsheetSkeleton } from '../sheet-skeleton';
import type { Spreadsheet } from '../spreadsheet';
import { SheetExtension } from './sheet-extension';

const UNIQUE_KEY = 'DefaultFontExtension';

const EXTENSION_Z_INDEX = 30;

export class Font extends SheetExtension {
override uKey = UNIQUE_KEY;
private _fontOffset = new ObjectMatrix<number>();

override Z_INDEX = EXTENSION_Z_INDEX;
private _fontHidden = new ObjectMatrix<boolean>();

changeFontColor: ObjectMatrix<IColorStyle> = new ObjectMatrix();
override uKey = UNIQUE_KEY;

get spreadsheet() {
return this.parent as Spreadsheet;
}
override Z_INDEX = EXTENSION_Z_INDEX;

getDocuments() {
const parent = this.parent as SheetComponent;
return parent?.getDocuments();
}

setChangeFontColor(r: number, c: number, color: IColorStyle) {
this.changeFontColor.setValue(r, c, color);
clearFontOffset() {
this._fontOffset.reset();
}

setFontOffset(r: number, c: number, offset: number) {
this._fontOffset.setValue(r, c, offset);
}

getFontOffset(r: number, c: number) {
return this._fontOffset.getValue(r, c);
}

clearFontHidden() {
this._fontHidden.reset();
}

setFontHidden(r: number, c: number, state: boolean) {
this._fontHidden.setValue(r, c, state);
}

getFontHidden(r: number, c: number) {
return this._fontHidden.getValue(r, c);
}

override draw(
Expand Down Expand Up @@ -92,6 +109,10 @@ export class Font extends SheetExtension {
let { startY, endY, startX, endX } = cellInfo;
const { isMerged, isMergedMainCell, mergeInfo } = cellInfo;

if (this.getFontHidden(rowIndex, columnIndex) === true) {
return true;
}

if (isMerged) {
return true;
}
Expand Down Expand Up @@ -124,7 +145,7 @@ export class Font extends SheetExtension {
const cellHeight = endY - startY;

const overflowRectangle = overflowCache.getValue(rowIndex, columnIndex);
const { horizontalAlign } = docsConfig;
const { horizontalAlign, angle } = docsConfig;

ctx.save();
ctx.beginPath();
Expand Down Expand Up @@ -191,6 +212,11 @@ export class Font extends SheetExtension {
);
}

if (angle === 0 && this.getFontOffset(rowIndex, columnIndex) != null) {
const offset = this.getFontOffset(rowIndex, columnIndex);
startX = startX + offset;
}

ctx.translate(startX, startY);
this._renderDocuments(ctx, docsConfig, startX, startY, endX, endY, rowIndex, columnIndex);
ctx.restore();
Expand Down

0 comments on commit 9cf028f

Please sign in to comment.