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(sheets): align background image DPI rendering (#7720)

wpxp12345667cba762837e
diff --git a/packages/core/src/sheets/__tests__/clone-worksheet-data.spec.ts b/packages/core/src/sheets/__tests__/clone-worksheet-data.spec.ts
index 67d285a279..38153e44e8 100644
--- a/packages/core/src/sheets/__tests__/clone-worksheet-data.spec.ts
+++ b/packages/core/src/sheets/__tests__/clone-worksheet-data.spec.ts
@@ -90,6 +90,8 @@ function createTestWorksheetData(rowCount: number, colCount: number): IWorksheet
backgroundImage: {
source: 'data:image/png;base64,background',
imageSourceType: ImageSourceType.BASE64,
+ scaleX: 0.8,
+ scaleY: 0.8,
},
rightToLeft: BooleanNumber.FALSE,
};
diff --git a/packages/core/src/sheets/clone.ts b/packages/core/src/sheets/clone.ts
index b47de89b80..e9ee26ff60 100644
--- a/packages/core/src/sheets/clone.ts
+++ b/packages/core/src/sheets/clone.ts
@@ -297,6 +297,8 @@ export function cloneWorksheetData(worksheet: IWorksheetData): IWorksheetData {
? {
source: worksheet.backgroundImage.source,
imageSourceType: worksheet.backgroundImage.imageSourceType,
+ scaleX: worksheet.backgroundImage.scaleX,
+ scaleY: worksheet.backgroundImage.scaleY,
}
: undefined,
diff --git a/packages/core/src/sheets/typedef.ts b/packages/core/src/sheets/typedef.ts
index 871c10a540..cd88349fac 100644
--- a/packages/core/src/sheets/typedef.ts
+++ b/packages/core/src/sheets/typedef.ts
@@ -104,6 +104,10 @@ export enum WorksheetHiddenState {
export interface IWorksheetBackgroundImage {
source: string;
imageSourceType: ImageSourceType;
+ /** Horizontal scale from source pixels to 96-DPI worksheet layout pixels. */
+ scaleX?: number;
+ /** Vertical scale from source pixels to 96-DPI worksheet layout pixels. */
+ scaleY?: number;
}
/**
diff --git a/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-update.controller.spec.ts b/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-update.controller.spec.ts
index 6139e028ec..5b67b2a9ae 100644
--- a/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-update.controller.spec.ts
+++ b/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-update.controller.spec.ts
@@ -136,8 +136,9 @@ describe('SheetDrawingUpdateController', () => {
it('uploads and sets a worksheet background image', async () => {
const file = { name: 'background.png' };
const imageParam = {
- source: 'data:image/png;base64,background',
- imageSourceType: ImageSourceType.BASE64,
+ source: 'uploaded-file-id',
+ imageSourceType: ImageSourceType.UUID,
+ base64Cache: 'data:image/png;base64,background',
};
const { controller, commandService, imageIoService } = createController({
openFiles: [file],
@@ -150,7 +151,10 @@ describe('SheetDrawingUpdateController', () => {
expect(commandService.executeCommand).toHaveBeenCalledWith(SetWorksheetBackgroundImageCommand.id, {
unitId: 'unit-1',
subUnitId: 'sheet-1',
- backgroundImage: imageParam,
+ backgroundImage: {
+ source: imageParam.base64Cache,
+ imageSourceType: ImageSourceType.BASE64,
+ },
});
controller.dispose();
diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts
index 84ce01ba35..9a97d86b44 100644
--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts
+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-update.controller.ts
@@ -80,6 +80,7 @@ import { ISheetSelectionRenderService, SheetSkeletonManagerService } from '@univ
import { ILocalFileService, IMessageService } from '@univerjs/ui';
import { GroupSheetDrawingCommand } from '../commands/commands/group-sheet-drawing.command';
import { UngroupSheetDrawingCommand } from '../commands/commands/ungroup-sheet-drawing.command';
+import { getWorksheetBackgroundImageScale } from '../utils/worksheet-background-image';
/**
* Calculate the bounding box after rotation
@@ -239,12 +240,15 @@ export class SheetDrawingUpdateController extends Disposable implements IRenderM
}
const { unitId, subUnitId } = this._getUnitInfo();
+ const source = imageParam.base64Cache || imageParam.source;
+ const scale = getWorksheetBackgroundImageScale(source);
return this._commandService.executeCommand(SetWorksheetBackgroundImageCommand.id, {
unitId,
subUnitId,
backgroundImage: {
- source: imageParam.source,
- imageSourceType: imageParam.imageSourceType,
+ source,
+ imageSourceType: imageParam.base64Cache ? ImageSourceType.BASE64 : imageParam.imageSourceType,
+ ...scale,
},
});
}
diff --git a/packages/sheets-drawing-ui/src/utils/__tests__/worksheet-background-image.spec.ts b/packages/sheets-drawing-ui/src/utils/__tests__/worksheet-background-image.spec.ts
new file mode 100644
index 0000000000..10686f18c7
--- /dev/null
+++ b/packages/sheets-drawing-ui/src/utils/__tests__/worksheet-background-image.spec.ts
@@ -0,0 +1,67 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { describe, expect, it } from 'vitest';
+import { getWorksheetBackgroundImageScale } from '../worksheet-background-image';
+
+function base64(bytes: number[]): string {
+ return `data:image/png;base64,${btoa(String.fromCharCode(...bytes))}`;
+}
+
+describe('getWorksheetBackgroundImageScale', () => {
+ it('converts PNG pixels-per-meter metadata to a 96-DPI worksheet scale', () => {
+ const bytes = [
+ 0x89,
+ 0x50,
+ 0x4E,
+ 0x47,
+ 0x0D,
+ 0x0A,
+ 0x1A,
+ 0x0A,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x09,
+ 0x70,
+ 0x48,
+ 0x59,
+ 0x73,
+ 0x00,
+ 0x00,
+ 0x17,
+ 0x12,
+ 0x00,
+ 0x00,
+ 0x17,
+ 0x12,
+ 0x01,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x00,
+ ];
+
+ const scale = getWorksheetBackgroundImageScale(base64(bytes));
+ expect(scale).toBeDefined();
+ expect(scale?.scaleX).toBeCloseTo(0.6399471, 6);
+ expect(scale?.scaleY).toBeCloseTo(0.6399471, 6);
+ });
+
+ it('leaves images without usable resolution metadata unchanged', () => {
+ expect(getWorksheetBackgroundImageScale('data:image/png;base64,invalid')).toBeUndefined();
+ });
+});
diff --git a/packages/sheets-drawing-ui/src/utils/worksheet-background-image.ts b/packages/sheets-drawing-ui/src/utils/worksheet-background-image.ts
new file mode 100644
index 0000000000..b6a953f030
--- /dev/null
+++ b/packages/sheets-drawing-ui/src/utils/worksheet-background-image.ts
@@ -0,0 +1,171 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+const WORKSHEET_LAYOUT_DPI = 96;
+const METERS_PER_INCH = 0.0254;
+
+export interface IWorksheetBackgroundImageScale {
+ scaleX: number;
+ scaleY: number;
+}
+
+/**
+ * Excel tiles worksheet background images using their physical resolution,
+ * while Canvas tiles them using source pixels. Convert the source resolution
+ * to Univer's 96-DPI worksheet layout pixels.
+ */
+export function getWorksheetBackgroundImageScale(source: string): IWorksheetBackgroundImageScale | undefined {
+ const bytes = decodeBase64DataUri(source);
+ if (!bytes) {
+ return undefined;
+ }
+
+ const pixelsPerMeter = readImagePixelsPerMeter(bytes);
+ if (!pixelsPerMeter) {
+ return undefined;
+ }
+
+ const scaleX = WORKSHEET_LAYOUT_DPI / (pixelsPerMeter.x * METERS_PER_INCH);
+ const scaleY = WORKSHEET_LAYOUT_DPI / (pixelsPerMeter.y * METERS_PER_INCH);
+ if (!Number.isFinite(scaleX) || !Number.isFinite(scaleY)) {
+ return undefined;
+ }
+ if (Math.abs(scaleX - 1) <= 0.001 && Math.abs(scaleY - 1) <= 0.001) {
+ return undefined;
+ }
+
+ return { scaleX, scaleY };
+}
+
+function decodeBase64DataUri(source: string): Uint8Array | undefined {
+ const separator = source.indexOf(',');
+ if (separator < 0 || !source.slice(0, separator).toLowerCase().includes(';base64')) {
+ return undefined;
+ }
+
+ try {
+ const binary = globalThis.atob(source.slice(separator + 1));
+ const bytes = new Uint8Array(binary.length);
+ for (let index = 0; index < binary.length; index++) {
+ bytes[index] = binary.charCodeAt(index);
+ }
+ return bytes;
+ } catch {
+ return undefined;
+ }
+}
+
+function readImagePixelsPerMeter(bytes: Uint8Array): { x: number; y: number } | undefined {
+ return readPngPixelsPerMeter(bytes) ?? readJpegPixelsPerMeter(bytes);
+}
+
+function readPngPixelsPerMeter(bytes: Uint8Array): { x: number; y: number } | undefined {
+ if (!hasBytes(bytes, 0, [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
+ return undefined;
+ }
+
+ let offset = 8;
+ while (offset + 12 <= bytes.length) {
+ const length = readUint32Be(bytes, offset);
+ const typeOffset = offset + 4;
+ const dataOffset = offset + 8;
+ const dataEnd = dataOffset + length;
+ const nextOffset = dataEnd + 4;
+ if (nextOffset > bytes.length) {
+ return undefined;
+ }
+ if (hasBytes(bytes, typeOffset, [0x70, 0x48, 0x59, 0x73])) {
+ if (length !== 9 || bytes[dataEnd - 1] !== 1) {
+ return undefined;
+ }
+ const x = readUint32Be(bytes, dataOffset);
+ const y = readUint32Be(bytes, dataOffset + 4);
+ return x > 0 && y > 0 ? { x, y } : undefined;
+ }
+ offset = nextOffset;
+ }
+ return undefined;
+}
+
+function readJpegPixelsPerMeter(bytes: Uint8Array): { x: number; y: number } | undefined {
+ if (!hasBytes(bytes, 0, [0xFF, 0xD8])) {
+ return undefined;
+ }
+
+ let offset = 2;
+ while (offset + 4 <= bytes.length) {
+ if (bytes[offset] !== 0xFF) {
+ offset++;
+ continue;
+ }
+ while (offset < bytes.length && bytes[offset] === 0xFF) {
+ offset++;
+ }
+ const marker = bytes[offset++];
+ if (marker == null) {
+ return undefined;
+ }
+ if (marker === 0xD8 || marker === 0xD9 || marker === 0x01 || (marker >= 0xD0 && marker <= 0xD7)) {
+ continue;
+ }
+ const length = readUint16Be(bytes, offset);
+ if (length < 2) {
+ return undefined;
+ }
+ const dataOffset = offset + 2;
+ const dataEnd = offset + length;
+ if (dataEnd > bytes.length) {
+ return undefined;
+ }
+ if (marker === 0xE0 && length >= 14 && hasBytes(bytes, dataOffset, [0x4A, 0x46, 0x49, 0x46, 0x00])) {
+ const units = bytes[dataOffset + 7];
+ const xDensity = readUint16Be(bytes, dataOffset + 8);
+ const yDensity = readUint16Be(bytes, dataOffset + 10);
+ if (!xDensity || !yDensity) {
+ return undefined;
+ }
+ if (units === 1) {
+ return {
+ x: Math.round(xDensity / METERS_PER_INCH),
+ y: Math.round(yDensity / METERS_PER_INCH),
+ };
+ }
+ if (units === 2) {
+ return { x: xDensity * 100, y: yDensity * 100 };
+ }
+ return undefined;
+ }
+ offset = dataEnd;
+ }
+ return undefined;
+}
+
+function hasBytes(bytes: Uint8Array, offset: number, expected: number[]): boolean {
+ return expected.every((value, index) => bytes[offset + index] === value);
+}
+
+function readUint16Be(bytes: Uint8Array, offset: number): number {
+ return ((bytes[offset] ?? 0) << 8) | (bytes[offset + 1] ?? 0);
+}
+
+function readUint32Be(bytes: Uint8Array, offset: number): number {
+ return (
+ (((bytes[offset] ?? 0) << 24) >>> 0)
+ | ((bytes[offset + 1] ?? 0) << 16)
+ | ((bytes[offset + 2] ?? 0) << 8)
+ | (bytes[offset + 3] ?? 0)
+ ) >>> 0;
+}
diff --git a/packages/sheets-drawing-ui/src/views/background/__tests__/worksheet-background-image.extension.spec.ts b/packages/sheets-drawing-ui/src/views/background/__tests__/worksheet-background-image.extension.spec.ts
new file mode 100644
index 0000000000..0be3089c1d
--- /dev/null
+++ b/packages/sheets-drawing-ui/src/views/background/__tests__/worksheet-background-image.extension.spec.ts
@@ -0,0 +1,64 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { ImageSourceType } from '@univerjs/core';
+import { describe, expect, it, vi } from 'vitest';
+import { WorksheetBackgroundImageExtension } from '../worksheet-background-image.extension';
+
+describe('WorksheetBackgroundImageExtension', () => {
+ it('applies the imported Excel DPI scale to the repeated pattern', () => {
+ const setTransform = vi.fn();
+ const pattern = { setTransform };
+ const context = {
+ createPattern: vi.fn(() => pattern),
+ save: vi.fn(),
+ restore: vi.fn(),
+ fillRect: vi.fn(),
+ fillStyle: undefined,
+ };
+ const image = {
+ complete: true,
+ naturalWidth: 100,
+ getAttribute: vi.fn(() => null),
+ };
+ const extension = new WorksheetBackgroundImageExtension({} as never, vi.fn());
+ (extension as any)._imageCache = { getImage: vi.fn(() => image) };
+
+ extension.draw(
+ context as never,
+ {} as never,
+ {
+ worksheet: {
+ getConfig: () => ({
+ backgroundImage: {
+ source: 'data:image/png;base64,background',
+ imageSourceType: ImageSourceType.BASE64,
+ scaleX: 0.8,
+ scaleY: 0.75,
+ },
+ }),
+ },
+ columnWidthAccumulation: [100],
+ rowHeightAccumulation: [20],
+ } as never,
+ [{ startColumn: 0, endColumn: 0, startRow: 0, endRow: 0 }],
+ { viewRanges: [] } as never
+ );
+
+ expect(setTransform).toHaveBeenCalledWith({ a: 0.8, d: 0.75 });
+ expect(context.fillRect).toHaveBeenCalledWith(0, 0, 100, 20);
+ });
+});
diff --git a/packages/sheets-drawing-ui/src/views/background/worksheet-background-image.extension.ts b/packages/sheets-drawing-ui/src/views/background/worksheet-background-image.extension.ts
index 304a570f98..4ea006c452 100644
--- a/packages/sheets-drawing-ui/src/views/background/worksheet-background-image.extension.ts
+++ b/packages/sheets-drawing-ui/src/views/background/worksheet-background-image.extension.ts
@@ -58,6 +58,11 @@ export class WorksheetBackgroundImageExtension extends SheetExtension {
if (!pattern) {
return;
}
+ const scaleX = backgroundImage.scaleX ?? 1;
+ const scaleY = backgroundImage.scaleY ?? 1;
+ if (scaleX !== 1 || scaleY !== 1) {
+ pattern.setTransform({ a: scaleX, d: scaleY });
+ }
const ranges = diffRanges.length > 0 ? diffRanges : drawInfo.viewRanges;
const { columnWidthAccumulation, rowHeightAccumulation } = spreadsheetSkeleton;