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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(engine-render): clip cache blit source rect to canvas bounds for Safari < 18 (#7331)
fxwanfe2d2052f906
diff --git a/packages/engine-render/src/components/sheets/__tests__/spreadsheet.apply-cache.spec.ts b/packages/engine-render/src/components/sheets/__tests__/spreadsheet.apply-cache.spec.tsnew file mode 100644index 0000000000..31c99eb0e2--- /dev/null+++ b/packages/engine-render/src/components/sheets/__tests__/spreadsheet.apply-cache.spec.ts@@ -0,0 +1,184 @@+/**+ * 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 type { Canvas } from '../../../canvas';+import type { UniverRenderingContext2D } from '../../../context';+import { describe, expect, it, vi } from 'vitest';+import { Spreadsheet } from '../spreadsheet';++interface ISpreadsheetApplyCache {+ _applyCache: (+ cacheCanvas: Canvas,+ ctx: UniverRenderingContext2D,+ sx?: number,+ sy?: number,+ sw?: number,+ sh?: number,+ dx?: number,+ dy?: number,+ dw?: number,+ dh?: number+ ) => void;+}++function createMockCtx() {+ return {+ save: vi.fn(),+ restore: vi.fn(),+ setTransform: vi.fn(),+ drawImage: vi.fn(),+ imageSmoothingEnabled: true,+ };+}++function createMockCacheCanvas(width: number, height: number, pixelRatio = 2) {+ const cacheCtx = createMockCtx();+ const canvasEle = {};+ return {+ canvasEle,+ getPixelRatio: () => pixelRatio,+ getWidth: () => width,+ getHeight: () => height,+ getContext: () => cacheCtx,+ getCanvasEle: () => canvasEle,+ };+}++function callApplyCache(+ cacheCanvas: ReturnType<typeof createMockCacheCanvas>,+ ctx: ReturnType<typeof createMockCtx>,+ ...rect: [number, number, number, number, number, number, number, number]+) {+ const spreadsheet = new Spreadsheet('sheet-apply-cache-test');+ (spreadsheet as unknown as ISpreadsheetApplyCache)._applyCache(+ cacheCanvas as unknown as Canvas,+ ctx as unknown as UniverRenderingContext2D,+ ...rect+ );+}++describe('spreadsheet _applyCache blit clipping', () => {+ it('clips the source rect to the cache canvas and shrinks the destination proportionally', () => {+ // Frozen column viewport (viewMainLeft): bufferEdgeX is 0, so the cache canvas is+ // exactly the viewport width (80), while renderByViewports requests a blit wider+ // by the row header margin (126 = 80 + 46).+ const cacheCanvas = createMockCacheCanvas(80, 480);+ const ctx = createMockCtx();++ callApplyCache(cacheCanvas, ctx, 0, 100, 126, 280, 46, 26, 126, 280);++ expect(ctx.drawImage).toHaveBeenCalledWith(+ cacheCanvas.canvasEle,+ 0, // sx+ 100 * 2, // sy+ 80 * 2, // sw clipped from 126 to the canvas width+ 280 * 2, // sh+ 46 * 2, // dx+ 26 * 2, // dy+ 80 * 2, // dw shrunk by the same ratio as sw+ 280 * 2 // dh+ );+ });++ it('passes the rect through unchanged when it already fits the cache canvas', () => {+ // Main viewport (viewMain): bufferEdge 100 on both axes leaves enough room for the+ // header margins, so nothing needs clipping.+ const cacheCanvas = createMockCacheCanvas(1000, 900);+ const ctx = createMockCtx();++ callApplyCache(cacheCanvas, ctx, 100, 100, 846, 726, 46, 26, 846, 726);++ expect(ctx.drawImage).toHaveBeenCalledWith(+ cacheCanvas.canvasEle,+ 100 * 2,+ 100 * 2,+ 846 * 2,+ 726 * 2,+ 46 * 2,+ 26 * 2,+ 846 * 2,+ 726 * 2+ );+ });++ it('clips the bottom overflow and keeps the vertical ratio', () => {+ // Frozen row viewport (viewMainTop): bufferEdgeY is 0, cache canvas is exactly the+ // viewport height (480), requested blit is taller by the column header margin (506).+ const cacheCanvas = createMockCacheCanvas(1200, 480);+ const ctx = createMockCtx();++ callApplyCache(cacheCanvas, ctx, 100, 0, 1100, 506, 0, 26, 1100, 506);++ expect(ctx.drawImage).toHaveBeenCalledWith(+ cacheCanvas.canvasEle,+ 100 * 2,+ 0,+ 1100 * 2,+ 480 * 2, // sh clipped from 506 to 480+ 0,+ 26 * 2,+ 1100 * 2,+ 480 * 2 // dh shrunk by the same ratio+ );+ });++ it('draws nothing when the source rect lies entirely outside the cache canvas', () => {+ const cacheCanvas = createMockCacheCanvas(80, 480);+ const ctx = createMockCtx();++ callApplyCache(cacheCanvas, ctx, 200, 0, 50, 800, 0, 0, 50, 800);++ expect(ctx.drawImage).not.toHaveBeenCalled();+ });++ it('clips a negative source offset and shifts the destination accordingly', () => {+ const cacheCanvas = createMockCacheCanvas(500, 500);+ const ctx = createMockCtx();++ callApplyCache(cacheCanvas, ctx, -20, 0, 100, 100, 0, 0, 100, 100);++ expect(ctx.drawImage).toHaveBeenCalledWith(+ cacheCanvas.canvasEle,+ 0, // sx clipped to 0+ 0,+ 80 * 2, // sw reduced by the clipped 20px+ 100 * 2,+ 20 * 2, // dx shifted by the same proportion+ 0,+ 80 * 2, // dw reduced by the same proportion+ 100 * 2+ );+ });++ it('keeps the previous behavior for zero-sized rects', () => {+ const cacheCanvas = createMockCacheCanvas(80, 480);+ const ctx = createMockCtx();++ callApplyCache(cacheCanvas, ctx, 0, 0, 0, 100, 0, 0, 0, 100);++ expect(ctx.drawImage).toHaveBeenCalledWith(+ cacheCanvas.canvasEle,+ 0,+ 0,+ 0,+ 200,+ 0,+ 0,+ 0,+ 200+ );+ });+});diff --git a/packages/engine-render/src/components/sheets/spreadsheet.ts b/packages/engine-render/src/components/sheets/spreadsheet.tsindex 6f3a485159..b1be077d69 100644--- a/packages/engine-render/src/components/sheets/spreadsheet.ts+++ b/packages/engine-render/src/components/sheets/spreadsheet.ts@@ -170,6 +170,71 @@ function getSparseExtensionDiffRanges(uKey: string, flags: ISparseExtensionFeatu}}+interface IBlitRect {+ sx: number;+ sy: number;+ sw: number;+ sh: number;+ dx: number;+ dy: number;+ dw: number;+ dh: number;+}++/**+ * Clip a blit source rectangle to the source canvas bounds and shrink the destination+ * rectangle proportionally, as required by the HTML spec:+ * https://html.spec.whatwg.org/multipage/canvas.html#drawing-images+ * ("the source rectangle must be clipped to the source image and the destination+ * rectangle must be clipped in the same proportion").+ *+ * renderByViewports() blits a region larger than the cache canvas (viewport size plus+ * the row/column header margins, while frozen viewports use bufferEdge=0 on their long+ * axis) and relies on this behavior: compliant browsers treat the overflow as+ * transparent padding. WebKit only implements it since Safari 18 ("Fixed drawImage to+ * not alter the input source or the destination rectangles"), so on Safari < 18 the+ * overflowed blit is dropped or distorted and frozen viewports lose their content.+ *+ * Returns null when the source rectangle lies entirely outside the canvas, meaning+ * nothing visible remains to be drawn.+ */+function clipBlitRectToBounds(rect: IBlitRect, sourceWidth: number, sourceHeight: number): Nullable<IBlitRect> {+ let { sx, sy, sw, sh, dx, dy, dw, dh } = rect;++ if (sx < 0) {+ const cut = Math.min(-sx, sw);+ const ratio = cut / sw;+ dx += dw * ratio;+ dw -= dw * ratio;+ sw -= cut;+ sx = 0;+ }+ if (sy < 0) {+ const cut = Math.min(-sy, sh);+ const ratio = cut / sh;+ dy += dh * ratio;+ dh -= dh * ratio;+ sh -= cut;+ sy = 0;+ }+ if (sx + sw > sourceWidth) {+ const clamped = Math.max(sourceWidth - sx, 0);+ dw *= clamped / sw;+ sw = clamped;+ }+ if (sy + sh > sourceHeight) {+ const clamped = Math.max(sourceHeight - sy, 0);+ dh *= clamped / sh;+ sh = clamped;+ }++ if (sw <= 0 || sh <= 0) {+ return null;+ }++ return { sx, sy, sw, sh, dx, dy, dw, dh };+}+export class Spreadsheet extends SheetComponent {private _backgroundExtension!: Background;@@ -620,6 +685,18 @@ export class Spreadsheet extends SheetComponent {return;}+ let blitRect: IBlitRect = { sx, sy, sw, sh, dx, dy, dw, dh };+ if (sw > 0 && sh > 0 && dw > 0 && dh > 0) {+ // The blit may request a source rect larger than the cache canvas (see+ // clipBlitRectToBounds). Clip it to the canvas bounds so the drawImage+ // call below behaves the same on every browser, including Safari < 18.+ const clipped = clipBlitRectToBounds(blitRect, cacheCanvas.getWidth(), cacheCanvas.getHeight());+ if (!clipped) {+ return;+ }+ blitRect = clipped;+ }+const pixelRatio = cacheCanvas.getPixelRatio();const cacheCtx = cacheCanvas.getContext();cacheCtx.save();@@ -630,14 +707,14 @@ export class Spreadsheet extends SheetComponent {ctx.imageSmoothingEnabled = false;ctx.drawImage(cacheCanvas.getCanvasEle(),- sx * pixelRatio,- sy * pixelRatio,- sw * pixelRatio,- sh * pixelRatio,- dx * pixelRatio,- dy * pixelRatio,- dw * pixelRatio,- dh * pixelRatio+ blitRect.sx * pixelRatio,+ blitRect.sy * pixelRatio,+ blitRect.sw * pixelRatio,+ blitRect.sh * pixelRatio,+ blitRect.dx * pixelRatio,+ blitRect.dy * pixelRatio,+ blitRect.dw * pixelRatio,+ blitRect.dh * pixelRatio);ctx.restore();cacheCtx.restore();