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.

feat(image): feat img clip by shape in OSS (#6765)

VicKun96ec7dec6c1a
diff --git a/packages/drawing-ui/src/index.ts b/packages/drawing-ui/src/index.ts
index 6d0d0eba05..eefc8ef55e 100644
--- a/packages/drawing-ui/src/index.ts
+++ b/packages/drawing-ui/src/index.ts
@@ -33,6 +33,8 @@ export { ImageResetSizeOperation } from './commands/operations/image-reset-size.
export type { IUniverDrawingUIConfig } from './config/config';
export { getCurrentUnitInfo, insertGroupObject } from './controllers/utils';
export { UniverDrawingUIPlugin } from './plugin';
+export { DrawingImageClipService, IMAGE_CLIP_SHAPE_PICKER_COMPONENT } from './services/drawing-image-clip.service';
+export type { ImageShapeClipDelegate } from './services/drawing-image-clip.service';
export { DrawingRenderService } from './services/drawing-render.service';
export { getUpdateParams } from './utils/get-update-params';
export { ImageCropperObject } from './views/crop/image-cropper-object';
diff --git a/packages/drawing-ui/src/locale/ca-ES.ts b/packages/drawing-ui/src/locale/ca-ES.ts
index a5a5e4d8b3..1bb158bd03 100644
--- a/packages/drawing-ui/src/locale/ca-ES.ts
+++ b/packages/drawing-ui/src/locale/ca-ES.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: 'Retalla',
start: 'Inicia retallada',
mode: 'Lliure',
+ shape: 'Retalla a forma',
},
group: {
title: 'Agrupa',
diff --git a/packages/drawing-ui/src/locale/en-US.ts b/packages/drawing-ui/src/locale/en-US.ts
index 6929868650..aea47f9e3b 100644
--- a/packages/drawing-ui/src/locale/en-US.ts
+++ b/packages/drawing-ui/src/locale/en-US.ts
@@ -46,6 +46,7 @@ const locale = {
title: 'Crop',
start: 'Start Crop',
mode: 'Free',
+ shape: 'Crop to Shape',
},
group: {
title: 'Group',
diff --git a/packages/drawing-ui/src/locale/es-ES.ts b/packages/drawing-ui/src/locale/es-ES.ts
index 0aeb7305ef..27671efa11 100644
--- a/packages/drawing-ui/src/locale/es-ES.ts
+++ b/packages/drawing-ui/src/locale/es-ES.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: 'Recortar',
start: 'Iniciar recorte',
mode: 'Libre',
+ shape: 'Recortar a forma',
},
group: {
title: 'Agrupar',
diff --git a/packages/drawing-ui/src/locale/fa-IR.ts b/packages/drawing-ui/src/locale/fa-IR.ts
index 3312f34dd3..2ada66fc67 100644
--- a/packages/drawing-ui/src/locale/fa-IR.ts
+++ b/packages/drawing-ui/src/locale/fa-IR.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: 'کراپ',
start: 'شروع کراپ',
mode: 'آزاد',
+ shape: 'کراپ به شکل',
},
group: {
title: 'گروه',
diff --git a/packages/drawing-ui/src/locale/fr-FR.ts b/packages/drawing-ui/src/locale/fr-FR.ts
index 6a01b1e3c4..1c54e414c9 100644
--- a/packages/drawing-ui/src/locale/fr-FR.ts
+++ b/packages/drawing-ui/src/locale/fr-FR.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: 'Rogner',
start: 'Commencer à rogner',
mode: 'Libre',
+ shape: 'Rogner à la forme',
},
group: {
title: 'Grouper',
diff --git a/packages/drawing-ui/src/locale/ja-JP.ts b/packages/drawing-ui/src/locale/ja-JP.ts
index 4c2ed72ff2..6e70233f16 100644
--- a/packages/drawing-ui/src/locale/ja-JP.ts
+++ b/packages/drawing-ui/src/locale/ja-JP.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: 'トリミング',
start: 'トリミングを開始',
mode: '自由モード',
+ shape: '形状にトリミング',
},
group: {
title: 'グループ',
diff --git a/packages/drawing-ui/src/locale/ko-KR.ts b/packages/drawing-ui/src/locale/ko-KR.ts
index 40e095a0f6..7dd931a758 100644
--- a/packages/drawing-ui/src/locale/ko-KR.ts
+++ b/packages/drawing-ui/src/locale/ko-KR.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: '자르기',
start: '자르기 시작',
mode: '자유 모드',
+ shape: '모양에 맞게 자르기',
},
group: {
title: '그룹',
diff --git a/packages/drawing-ui/src/locale/ru-RU.ts b/packages/drawing-ui/src/locale/ru-RU.ts
index 70f5d76422..7992ab129b 100644
--- a/packages/drawing-ui/src/locale/ru-RU.ts
+++ b/packages/drawing-ui/src/locale/ru-RU.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: 'Обрезка',
start: 'Начать обрезку',
mode: 'Свободный',
+ shape: 'Обрезать по форме',
},
group: {
title: 'Группировка',
diff --git a/packages/drawing-ui/src/locale/sk-SK.ts b/packages/drawing-ui/src/locale/sk-SK.ts
index ccacbf885e..e973f7d1ac 100644
--- a/packages/drawing-ui/src/locale/sk-SK.ts
+++ b/packages/drawing-ui/src/locale/sk-SK.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: 'Orezanie',
start: 'Začať orezanie',
mode: 'Voľne',
+ shape: 'Oříznout podle tvaru',
},
group: {
title: 'Zoskupiť',
diff --git a/packages/drawing-ui/src/locale/vi-VN.ts b/packages/drawing-ui/src/locale/vi-VN.ts
index a8767905b2..2d8593e36f 100644
--- a/packages/drawing-ui/src/locale/vi-VN.ts
+++ b/packages/drawing-ui/src/locale/vi-VN.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: 'Cắt',
start: 'Bắt đầu cắt',
mode: 'Cắt tự do tỷ lệ',
+ shape: 'Cắt theo hình dạng',
},
group: {
title: 'Nhóm',
diff --git a/packages/drawing-ui/src/locale/zh-CN.ts b/packages/drawing-ui/src/locale/zh-CN.ts
index 58edba19a1..5b295adad6 100644
--- a/packages/drawing-ui/src/locale/zh-CN.ts
+++ b/packages/drawing-ui/src/locale/zh-CN.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: '裁剪',
start: '开始裁剪',
mode: '自由比例裁剪',
+ shape: '裁剪为形状',
},
group: {
title: '组合',
diff --git a/packages/drawing-ui/src/locale/zh-TW.ts b/packages/drawing-ui/src/locale/zh-TW.ts
index 93da3e24e7..b8561e9927 100644
--- a/packages/drawing-ui/src/locale/zh-TW.ts
+++ b/packages/drawing-ui/src/locale/zh-TW.ts
@@ -48,6 +48,7 @@ const locale: typeof enUS = {
title: '裁切',
start: '開始裁切',
mode: '自由比例裁切',
+ shape: '裁切為形狀',
},
group: {
title: '組合',
diff --git a/packages/drawing-ui/src/plugin.ts b/packages/drawing-ui/src/plugin.ts
index d7e3a1063c..1472cff3b0 100644
--- a/packages/drawing-ui/src/plugin.ts
+++ b/packages/drawing-ui/src/plugin.ts
@@ -23,6 +23,7 @@ import { DrawingUIController } from './controllers/drawing-ui.controller';
import { DrawingUpdateController } from './controllers/drawing-update.controller';
import { ImageCropperController } from './controllers/image-cropper.controller';
import { ImageUpdateController } from './controllers/image-update.controller';
+import { DrawingImageClipService } from './services/drawing-image-clip.service';
import { DrawingRenderService } from './services/drawing-render.service';
export class UniverDrawingUIPlugin extends Plugin {
@@ -62,6 +63,7 @@ export class UniverDrawingUIPlugin extends Plugin {
private _initDependencies(): void {
const dependencies: Dependency[] = [
+ [DrawingImageClipService],
[DrawingRenderService],
[DrawingUpdateController],
[DrawingUIController],
diff --git a/packages/drawing-ui/src/services/__tests__/drawing-render.service.spec.ts b/packages/drawing-ui/src/services/__tests__/drawing-render.service.spec.ts
index 8ba48946e7..dd6047b6df 100644
--- a/packages/drawing-ui/src/services/__tests__/drawing-render.service.spec.ts
+++ b/packages/drawing-ui/src/services/__tests__/drawing-render.service.spec.ts
@@ -53,6 +53,10 @@ function createService(options?: { unitType?: UniverInstanceType; visible?: bool
attachTransformerTo: vi.fn(),
};
+ const drawingImageClipService = {
+ applyShapeClip: vi.fn(),
+ };
+
return {
drawingManagerService,
imageIoService,
@@ -62,7 +66,8 @@ function createService(options?: { unitType?: UniverInstanceType; visible?: bool
imageIoService as never,
{} as never,
urlImageService as never,
- univerInstanceService as never
+ univerInstanceService as never,
+ drawingImageClipService as never
),
univerInstanceService,
};
diff --git a/packages/drawing-ui/src/services/drawing-image-clip.service.ts b/packages/drawing-ui/src/services/drawing-image-clip.service.ts
new file mode 100644
index 0000000000..d743e0ddb3
--- /dev/null
+++ b/packages/drawing-ui/src/services/drawing-image-clip.service.ts
@@ -0,0 +1,78 @@
+/**
+ * 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 { IDisposable, Nullable } from '@univerjs/core';
+import type { IImageShapeClipService, IShapeClipBounds, UniverRenderingContext } from '@univerjs/engine-render';
+import { Disposable, toDisposable } from '@univerjs/core';
+import { BehaviorSubject } from 'rxjs';
+
+export const IMAGE_CLIP_SHAPE_PICKER_COMPONENT = 'sheet.image-clip.shape.picker.component';
+
+/**
+ * Delegate function type for building a shape clip path.
+ * The function should call ctx.beginPath(), build the shape outline path, and call ctx.clip().
+ * Coordinate system: (0,0) is at the top-left of the shape area.
+ * @returns The actual bounding rect of the clip region, or false if no clip was built
+ */
+export type ImageShapeClipDelegate = (ctx: UniverRenderingContext, prstGeom: string, width: number, height: number, adjustValues?: Nullable<Record<string, number>>) => IShapeClipBounds | false;
+
+/**
+ * Bridge service that enables shape-based image clipping.
+ * This service lives in the open-source drawing-ui package and delegates
+ * to a registered clip implementation (provided by pro engine-shape package).
+ *
+ * When no delegate is registered, applyShapeClip returns false and images render normally without shape clipping.
+ */
+export class DrawingImageClipService extends Disposable implements IImageShapeClipService {
+ private _clipDelegate: Nullable<ImageShapeClipDelegate> = null;
+ private readonly _canUseShapeClip$ = new BehaviorSubject<boolean>(false);
+ readonly canUseShapeClip$ = this._canUseShapeClip$.asObservable();
+
+ constructor() {
+ super();
+ }
+
+ setCanUseShapeClip(canUse: boolean) {
+ this._canUseShapeClip$.next(canUse);
+ }
+
+ /**
+ * Register a clip delegate that knows how to build shape clip paths.
+ * Typically called by the pro-side plugin with a ShapeModel-based implementation.
+ * @returns IDisposable to unregister the delegate
+ */
+ registerClipDelegate(delegate: ImageShapeClipDelegate): IDisposable {
+ this._clipDelegate = delegate;
+ return toDisposable(() => {
+ if (this._clipDelegate === delegate) {
+ this._clipDelegate = null;
+ }
+ });
+ }
+
+ applyShapeClip(ctx: UniverRenderingContext, prstGeom: string, width: number, height: number, adjustValues?: Nullable<Record<string, number>>): IShapeClipBounds | false {
+ if (this._clipDelegate) {
+ return this._clipDelegate(ctx, prstGeom, width, height, adjustValues);
+ }
+ return false;
+ }
+
+ override dispose(): void {
+ this._clipDelegate = null;
+ this._canUseShapeClip$.complete();
+ super.dispose();
+ }
+}
diff --git a/packages/drawing-ui/src/services/drawing-render.service.ts b/packages/drawing-ui/src/services/drawing-render.service.ts
index 0ec4e6fdae..bb8055a76e 100644
--- a/packages/drawing-ui/src/services/drawing-render.service.ts
+++ b/packages/drawing-ui/src/services/drawing-render.service.ts
@@ -17,11 +17,12 @@
import type { IDrawingSearch, Workbook } from '@univerjs/core';
import type { IDocFloatDomData, IImageData } from '@univerjs/drawing';
import type { IImageProps, IRectProps, Scene } from '@univerjs/engine-render';
-import { DrawingTypeEnum, IUniverInstanceService, IURLImageService, UniverInstanceType } from '@univerjs/core';
+import { DrawingTypeEnum, Inject, IUniverInstanceService, IURLImageService, UniverInstanceType } from '@univerjs/core';
import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService, IImageIoService, ImageSourceType } from '@univerjs/drawing';
import { DRAWING_OBJECT_LAYER_INDEX, Image, Rect } from '@univerjs/engine-render';
import { IGalleryService } from '@univerjs/ui';
import { insertGroupObject } from '../controllers/utils';
+import { DrawingImageClipService } from './drawing-image-clip.service';
// const IMAGE_VIEWER_DROPDOWN_PADDING = 50;
@@ -31,7 +32,8 @@ export class DrawingRenderService {
@IImageIoService private readonly _imageIoService: IImageIoService,
@IGalleryService private readonly _galleryService: IGalleryService,
@IURLImageService private readonly _urlImageService: IURLImageService,
- @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService
+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
+ @Inject(DrawingImageClipService) private readonly _drawingImageClipService: DrawingImageClipService
) { }
// eslint-disable-next-line max-lines-per-function, complexity
@@ -49,6 +51,7 @@ export class DrawingRenderService {
drawingId,
isMultiTransform,
transforms: multiTransforms,
+ adjustValues,
} = imageParam;
if (drawingType !== DrawingTypeEnum.DRAWING_IMAGE) {
@@ -118,6 +121,7 @@ export class DrawingRenderService {
imageConfig.printable = true;
const image = new Image(imageShapeKey, imageConfig);
+ image.setClipService(this._drawingImageClipService);
if (shouldBeCache) {
this._imageIoService.addImageSourceCache(source, imageSourceType, image.getNative());
}
@@ -132,6 +136,9 @@ export class DrawingRenderService {
if (prstGeom != null) {
image.setPrstGeom(prstGeom);
}
+ if (adjustValues != null) {
+ image.setPrstGeomAdjValues(adjustValues);
+ }
if (srcRect != null) {
image.setSrcRect(srcRect);
}
diff --git a/packages/drawing-ui/src/views/crop/image-cropper-object.ts b/packages/drawing-ui/src/views/crop/image-cropper-object.ts
index f2e1a6132c..4f71c10b20 100644
--- a/packages/drawing-ui/src/views/crop/image-cropper-object.ts
+++ b/packages/drawing-ui/src/views/crop/image-cropper-object.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ISrcRect, ITransformState, Nullable, PresetGeometryType } from '@univerjs/core';
+import type { ISrcRect, ITransformState, Nullable } from '@univerjs/core';
import type { Engine, IShapeProps, IViewportInfo, Scene, UniverRenderingContext, Vector2 } from '@univerjs/engine-render';
import { Canvas, Rect, Shape } from '@univerjs/engine-render';
@@ -27,7 +27,7 @@ export interface IImageCropperObjectProps extends IShapeProps {
/**
* 20.1.9.18 prstGeom (Preset geometry)
*/
- prstGeom?: Nullable<PresetGeometryType>;
+ prstGeom?: Nullable<string>;
applyTransform?: ITransformState;
dragPadding?: number;
}
@@ -39,7 +39,7 @@ enum ImageCropperObjectType {
export class ImageCropperObject<T extends IImageCropperObjectProps = IImageCropperObjectProps> extends Shape<T> {
private _srcRect: Nullable<ISrcRect>;
- private _prstGeom: Nullable<PresetGeometryType>;
+ private _prstGeom: Nullable<string>;
private _applyTransform: Nullable<ITransformState>;
private _dragPadding = 8;
diff --git a/packages/drawing-ui/src/views/panel/ImageCropper.tsx b/packages/drawing-ui/src/views/panel/ImageCropper.tsx
index 7cdade00a0..baaedf467e 100644
--- a/packages/drawing-ui/src/views/panel/ImageCropper.tsx
+++ b/packages/drawing-ui/src/views/panel/ImageCropper.tsx
@@ -18,9 +18,11 @@ import type { IDrawingParam } from '@univerjs/core';
import { ICommandService, LocaleService } from '@univerjs/core';
import { Button, clsx, Select } from '@univerjs/design';
import { CreateCopyIcon } from '@univerjs/icons';
-import { useDependency } from '@univerjs/ui';
+import { ComponentManager, useDependency, useObservable } from '@univerjs/ui';
import { useEffect, useRef, useState } from 'react';
+
import { AutoImageCropOperation, CloseImageCropOperation, CropType } from '../../commands/operations/image-crop.operation';
+import { DrawingImageClipService, IMAGE_CLIP_SHAPE_PICKER_COMPONENT } from '../../services/drawing-image-clip.service';
export interface IImageCropperProps {
drawings: IDrawingParam[];
@@ -30,6 +32,9 @@ export interface IImageCropperProps {
export const ImageCropper = (props: IImageCropperProps) => {
const commandService = useDependency(ICommandService);
const localeService = useDependency(LocaleService);
+ const clipService = useDependency(DrawingImageClipService);
+ const componentManager = useDependency(ComponentManager);
+ const canUseShapeClip = useObservable(clipService.canUseShapeClip$, false);
const { drawings, cropperShow } = props;
@@ -140,6 +145,13 @@ export const ImageCropper = (props: IImageCropperProps) => {
<Select value={cropValue} options={cropOptions} onChange={handleCropChange} />
</div>
+
+ {canUseShapeClip && (() => {
+ const ShapeClipPicker = componentManager.get(IMAGE_CLIP_SHAPE_PICKER_COMPONENT);
+ return ShapeClipPicker
+ ? <ShapeClipPicker />
+ : null;
+ })()}
</div>
);
};
diff --git a/packages/drawing/src/models/image-model-interface.ts b/packages/drawing/src/models/image-model-interface.ts
index a1dd34b2f3..00d08969b3 100644
--- a/packages/drawing/src/models/image-model-interface.ts
+++ b/packages/drawing/src/models/image-model-interface.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IDrawingParam, ISrcRect, Nullable, PresetGeometryType, Serializable } from '@univerjs/core';
+import type { IDrawingParam, ISrcRect, Nullable, Serializable } from '@univerjs/core';
import type { ImageSourceType } from '../services/image-io.service';
export interface IImageData extends IDrawingParam {
@@ -27,7 +27,13 @@ export interface IImageData extends IDrawingParam {
/**
* 20.1.9.18 prstGeom (Preset geometry)
*/
- prstGeom?: Nullable<PresetGeometryType>;
+ prstGeom?: Nullable<string>;
+
+ /**
+ * Adjust values for the preset geometry (e.g. corner radius for roundRect).
+ * Keys are adjust handle names, values are numeric values.
+ */
+ adjustValues?: Nullable<Record<string, number>>;
}
export interface IDocFloatDomDataBase {
diff --git a/packages/engine-render/src/shape/image.ts b/packages/engine-render/src/shape/image.ts
index 50ec2e541f..62b4520399 100644
--- a/packages/engine-render/src/shape/image.ts
+++ b/packages/engine-render/src/shape/image.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ISrcRect, Nullable, PresetGeometryType } from '@univerjs/core';
+import type { ISrcRect, Nullable } from '@univerjs/core';
import type { IObjectFullState, ITransformChangeState, IViewportInfo } from '../basics';
import type { UniverRenderingContext } from '../context';
@@ -25,6 +25,24 @@ import { RENDER_CLASS_TYPE, Transform, Vector2 } from '../basics';
import { offsetRotationAxis } from '../basics/offset-rotation-axis';
import { Shape } from './shape';
+export interface IShapeClipBounds {
+ left: number;
+ top: number;
+ width: number;
+ height: number;
+}
+
+export interface IImageShapeClipService {
+ /**
+ * Build the shape outline path and clip the canvas context.
+ * Assumes the coordinate system has (0,0) at the top-left of the shape area.
+ * The method calls ctx.beginPath(), builds the shape path, and calls ctx.clip().
+ * @returns The actual bounding rect of the clip region, or false if no clip was built.
+ * For multi-path shapes the bounds may extend beyond (0, 0, width, height).
+ */
+ applyShapeClip(ctx: UniverRenderingContext, prstGeom: string, width: number, height: number, adjustValues?: Nullable<Record<string, number>>): IShapeClipBounds | false;
+}
+
export interface IImageProps extends IShapeProps {
image?: HTMLImageElement;
url?: string;
@@ -38,7 +56,13 @@ export interface IImageProps extends IShapeProps {
/**
* 20.1.9.18 prstGeom (Preset geometry)
*/
- prstGeom?: Nullable<PresetGeometryType>;
+ prstGeom?: Nullable<string>;
+
+ /**
+ * Adjust values for the preset geometry (e.g. corner radius for roundRect).
+ * Keys are adjust handle names, values are numeric values.
+ */
+ adjustValues?: Nullable<Record<string, number>>;
opacity?: number;
}
@@ -52,6 +76,8 @@ export class Image extends Shape<IImageProps> {
private _transformCalculateSrcRect: boolean = true;
+ private _clipService: Nullable<IImageShapeClipService> = null;
+
override objectType = ObjectType.IMAGE;
override isDrawingObject: boolean = true;
@@ -106,6 +132,14 @@ export class Image extends Shape<IImageProps> {
this.makeDirty(true);
}
+ setClipService(clipService: Nullable<IImageShapeClipService>) {
+ this._clipService = clipService;
+ }
+
+ getClipService(): Nullable<IImageShapeClipService> {
+ return this._clipService;
+ }
+
override get classType(): RENDER_CLASS_TYPE {
return RENDER_CLASS_TYPE.IMAGE;
}
@@ -137,10 +171,18 @@ export class Image extends Shape<IImageProps> {
this.setSrcRect(null);
}
- setPrstGeom(prstGeom?: Nullable<PresetGeometryType>) {
+ setPrstGeom(prstGeom?: Nullable<string>) {
this._props.prstGeom = prstGeom;
}
+ setPrstGeomAdjValues(adjValues?: Nullable<Record<string, number>>) {
+ this._props.adjustValues = adjValues;
+ }
+
+ get prstGeomAdjValues() {
+ return this._props.adjustValues;
+ }
+
setSrcRect(srcRect?: Nullable<ISrcRect>) {
this._props.srcRect = srcRect;
@@ -325,6 +367,46 @@ export class Image extends Shape<IImageProps> {
}
const w = renderWidth ?? this.width;
const h = renderHeight ?? this.height;
+
+ // Shape clip: when prstGeom is set and a clip service is available,
+ // clip the image to the shape outline (e.g. ellipse, roundRect, etc.)
+ if (this.prstGeom && this._clipService) {
+ ctx.save();
+ ctx.translate(-w / 2, -h / 2); // move origin to top-left for clip path
+ // Clip to bounding rect first so that any shape path overshooting the
+ // bounding box (e.g. due to control-point curves) is safely contained.
+ ctx.beginPath();
+ // ctx.rect(0, 0, w, h);
+ // ctx.clip();
+ const clipBounds = this._clipService.applyShapeClip(ctx, this.prstGeom, w, h, this.prstGeomAdjValues);
+ if (clipBounds) {
+ // Use the actual clip bounds for image drawing — for multi-path shapes
+ // (e.g. cloudCallout) the clip region may extend beyond (0, 0, w, h).
+ const drawLeft = clipBounds.left;
+ const drawTop = clipBounds.top;
+ const drawWidth = clipBounds.width;
+ const drawHeight = clipBounds.height;
+ if (!this._renderByCropper && this.srcRect) {
+ const { left = 0, top = 0, right = 0, bottom = 0 } = this.srcRect;
+ // Scale srcRect offsets proportionally to the actual clip bounds
+ const scaleW = drawWidth / w;
+ const scaleH = drawHeight / h;
+ ctx.drawImage(
+ this._native,
+ drawLeft - left * scaleW,
+ drawTop - top * scaleH,
+ drawWidth + (right + left) * scaleW,
+ drawHeight + (bottom + top) * scaleH
+ );
+ } else {
+ ctx.drawImage(this._native, drawLeft, drawTop, drawWidth, drawHeight);
+ }
+ ctx.restore();
+ return;
+ }
+ ctx.restore();
+ }
+
if (!this._renderByCropper && this.srcRect) {
const { left = 0, top = 0, right = 0, bottom = 0 } = this.srcRect;
ctx.beginPath();