UNPKG

@leafer-draw/miniapp

Version:
1 lines 214 kB
{"version":3,"file":"miniapp.min.cjs","sources":["../../../../../../../src/leafer/packages/interface/src/path/IPathCommand.ts","../../../../../../../src/leafer/packages/canvas/canvas-miniapp/src/LeaferCanvas.ts","../../../../../../../src/leafer/packages/core/miniapp-core/src/index.ts","../../../../../../../src/leafer/packages/partner/watcher/src/Watcher.ts","../../../../../../../src/leafer/packages/partner/layouter/src/LayouterHelper.ts","../../../../../../../src/leafer/packages/partner/layouter/src/LayoutBlockData.ts","../../../../../../../src/leafer/packages/partner/layouter/src/Layouter.ts","../../../../../../../src/leafer/packages/partner/renderer/src/Renderer.ts","../../../../../../../src/ui/packages/partner/paint/src/Fill.ts","../../../../../../../src/ui/packages/partner/paint/src/Stroke.ts","../../../../../../../src/ui/packages/partner/paint/src/StrokeText.ts","../../../../../../../src/draw/packages/partner/src/index.ts","../../../../../../../src/ui/packages/partner/paint/src/Shape.ts","../../../../../../../src/ui/packages/partner/paint/src/Compute.ts","../../../../../../../src/ui/packages/partner/paint/src/index.ts","../../../../../../../src/ui/packages/partner/paint/src/FillText.ts","../../../../../../../src/ui/packages/partner/image/src/image.ts","../../../../../../../src/ui/packages/partner/image/src/data.ts","../../../../../../../src/ui/packages/partner/image/src/mode.ts","../../../../../../node_modules/.pnpm/@rollup+plugin-typescript@11.1.6_rollup@4.44.2_tslib@2.8.1_typescript@5.8.3/node_modules/tslib/tslib.es6.js","../../../../../../../src/ui/packages/partner/image/src/pattern.ts","../../../../../../../src/ui/packages/partner/image/src/index.ts","../../../../../../../src/ui/packages/partner/image/src/check.ts","../../../../../../../src/ui/packages/partner/image/src/recycle.ts","../../../../../../../src/ui/packages/partner/gradient/src/linear.ts","../../../../../../../src/ui/packages/partner/gradient/src/radial.ts","../../../../../../../src/ui/packages/partner/gradient/src/conic.ts","../../../../../../../src/ui/packages/partner/gradient/src/index.ts","../../../../../../../src/ui/packages/partner/effect/src/Shadow.ts","../../../../../../../src/ui/packages/partner/effect/src/InnerShadow.ts","../../../../../../../src/ui/packages/partner/effect/src/index.ts","../../../../../../../src/ui/packages/partner/effect/src/Blur.ts","../../../../../../../src/ui/packages/partner/effect/src/BackgroundBlur.ts","../../../../../../../src/ui/packages/partner/mask/src/index.ts","../../../../../../../src/ui/packages/partner/text/src/CharType.ts","../../../../../../../src/ui/packages/partner/text/src/TextRowHelper.ts","../../../../../../../src/ui/packages/partner/text/src/TextCase.ts","../../../../../../../src/ui/packages/partner/text/src/TextRows.ts","../../../../../../../src/ui/packages/partner/text/src/TextConvert.ts","../../../../../../../src/ui/packages/partner/text/src/index.ts","../../../../../../../src/ui/packages/partner/text/src/TextLayout.ts","../../../../../../../src/ui/packages/partner/text/src/CharLayout.ts","../../../../../../../src/ui/packages/partner/text/src/TextClip.ts","../../../../../../../src/ui/packages/partner/text/src/TextDecoration.ts","../../../../../../../src/ui/packages/partner/color/src/index.ts","../../../../../../../src/ui/packages/partner/color/src/color.ts","../../../../../../../src/ui/packages/partner/partner/src/index.ts","../../../../../../../src/draw/packages/platform/miniapp/src/index.ts"],"sourcesContent":["import { IPointData } from '../math/IMath'\nimport { IWindingRule } from '../canvas/ICanvas'\n\ntype Command = number\ntype x = number\ntype y = number\ntype x1 = number\ntype y1 = number\ntype x2 = number\ntype y2 = number\ntype radiusX = number\ntype radiusY = number\ntype xAxisRotation = number\ntype largeArcFlag = number\ntype sweepFlag = number\n\n\nexport type MCommandData = [Command, x, y]\nexport type HCommandData = [Command, x]\nexport type VCommandData = [Command, y]\nexport type LCommandData = MCommandData\n\nexport type CCommandData = [Command, x1, y1, x2, y2, x, y]\nexport type SCommandData = [Command, x2, y2, x, y]\n\nexport type QCommandData = [Command, x1, y1, x, y]\nexport type TCommandData = [Command, x, y]\n\nexport type ZCommandData = [Command]\n\nexport type ACommandData = [Command, radiusX, radiusY, xAxisRotation, largeArcFlag, sweepFlag, x, y]\n\n\n// 非svg标准的canvas绘图命令\ntype width = number\ntype height = number\ntype rotation = number\ntype startAngle = number\ntype endAngle = number\ntype anticlockwise = boolean\ntype cornerRadius = number | number[]\ntype radius = number\n\nexport type RectCommandData = [Command, x, y, width, height]\nexport type RoundRectCommandData = [Command, x, y, width, height, cornerRadius]\nexport type EllipseCommandData = [Command, x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise]\nexport type ArcCommandData = [Command, x, y, radius, startAngle, endAngle, anticlockwise]\nexport type ArcToCommandData = [Command, x1, y1, x2, y2, radius]\n\n\nexport type CanvasPathCommand = 1 | 2 | 5 | 7 | 11 // M | L | C | Q | Z canvas可以绘制的命令\n\nexport type IPathCommandData = number[] // ...(MCommandData | LCommandData | CCommandData | QCommandData | ZCommandData)\n\nexport interface IPathCommandDataWithRadius {\n data: IPathCommandData\n radius: number[]\n}\n\nexport interface IPathCommandDataWithWindingRule {\n data: IPathCommandData\n windingRule?: IWindingRule\n}\n\n// 路径命令对象\nexport interface MoveToCommandObject {\n name: 'M'\n x: number\n y: number\n pressure?: number // 压感\n}\nexport interface LineToCommandObject {\n name: 'L'\n x: number\n y: number\n pressure?: number\n}\n\nexport interface BezierCurveToCommandObject {\n name: 'C'\n x1: number\n y1: number\n x2: number\n y2: number\n x: number\n y: number\n pressure?: number\n}\n\nexport interface QuadraticCurveToCommandObject {\n name: 'Q'\n x1: number\n y1: number\n x: number\n y: number\n pressure?: number\n}\n\nexport interface ClosePathCommandObject {\n name: 'Z'\n}\n\nexport type IPathCommandObject = MoveToCommandObject | LineToCommandObject | BezierCurveToCommandObject | QuadraticCurveToCommandObject | ClosePathCommandObject // M | L | C | Q | Z canvas可以绘制的命令\n\n\n// 可视化路径节点\n\nexport interface IPathCommandNodeBase {\n name: 'M^' | 'L^' | 'C^' | 'Z^'\n x: number\n y: number\n r?: number // 圆角半径\n a?: IPointData // 第一个手柄,连接上一个节点\n b?: IPointData // 第二个手柄,连接下一个节点\n ab?: PathNodeHandleType // 手柄类型\n pressure?: number // 压感\n}\n\nexport interface MoveToCommandNode extends IPathCommandNodeBase {\n name: 'M^'\n}\nexport interface LineToCommandNode extends IPathCommandNodeBase {\n name: 'L^'\n}\n\nexport interface BezierCurveToCommandNode extends IPathCommandNodeBase {\n name: 'C^'\n}\n\nexport interface ClosePathCommandNode {\n name: 'Z^'\n x?: number\n y?: number\n r?: number // 圆角半径\n a?: IPointData\n b?: IPointData\n ab?: PathNodeHandleType\n}\n\nexport type IPathCommandNode = MoveToCommandNode | LineToCommandNode | BezierCurveToCommandNode | ClosePathCommandNode // M | L | C | Z 路径节点命令(适合可视化编辑)\n\nexport enum PathNodeHandleType { // 手柄类型\n none = 1, // 无手柄\n free = 2, // 每个手柄自由控制\n mirrorAngle = 3, // 仅镜像角度\n mirror = 4, // 镜像角度和长度\n}\n\nexport type PathNodeHandleName = 'a' | 'b' // 手柄名称\n\nexport interface IPathNodeBase {\n pathNode: IPathCommandNode\n}","import { IResizeEventListener, IAutoBounds, IScreenSizeData, IFunction, IMiniappSelect, IObject, ICanvasContext2D } from '@leafer/interface'\nimport { LeaferCanvasBase, canvasPatch, canvasSizeAttrs, Platform, DataHelper, ResizeEvent, isString, isNumber, isUndefined } from '@leafer/core'\n\n\nexport class LeaferCanvas extends LeaferCanvasBase {\n\n public get allowBackgroundColor(): boolean { return false }\n\n public viewSelect: IMiniappSelect\n public resizeListener: IResizeEventListener\n\n public testView: any\n public testContext: ICanvasContext2D\n\n public init(): void {\n const { config } = this\n let view = config.view || config.canvas\n\n if (view) {\n if (isString(view)) {\n if (view[0] !== '#') view = '#' + view\n this.viewSelect = Platform.miniapp.select(view)\n } else if (view.fields) {\n this.viewSelect = view\n } else {\n this.initView(view)\n }\n\n if (this.viewSelect) Platform.miniapp.getSizeView(this.viewSelect).then(sizeView => {\n this.initView(sizeView)\n })\n } else {\n this.initView()\n }\n }\n\n protected initView(view?: IObject): void {\n if (!view) {\n view = {}\n this.__createView()\n } else {\n this.view = view.view || view\n }\n\n this.view.getContext ? this.__createContext() : this.unrealCanvas()\n\n const { width, height, pixelRatio } = this.config\n const size = { width: width || view.width, height: height || view.height, pixelRatio }\n this.resize(size)\n\n if (this.context) {\n\n if (this.viewSelect) Platform.renderCanvas = this\n\n // fix roundRect\n if (this.context.roundRect) {\n this.roundRect = function (x: number, y: number, width: number, height: number, radius?: number | number[]): void {\n this.context.roundRect(x, y, width, height, isNumber(radius) ? [radius] : radius)\n }\n }\n canvasPatch((this.context as IObject).__proto__)\n }\n }\n\n protected __createView(): void {\n this.view = Platform.origin.createCanvas(1, 1)\n }\n\n public updateViewSize(): void {\n if (this.unreal) return\n\n const { width, height, pixelRatio } = this\n\n this.view.width = Math.ceil(width * pixelRatio)\n this.view.height = Math.ceil(height * pixelRatio)\n }\n\n public updateClientBounds(callback?: IFunction): void {\n if (this.viewSelect) Platform.miniapp.getBounds(this.viewSelect).then(bounds => {\n this.clientBounds = bounds\n\n // 抖音小程序 boundingClientRect() 会重置 Canvas 尺寸\n this.updateViewSize()\n\n if (callback) callback()\n })\n }\n\n\n public startAutoLayout(autoBounds: IAutoBounds, listener: IResizeEventListener): void {\n if (this.resizeListener) return\n\n this.resizeListener = listener\n if (autoBounds) {\n this.checkSize = this.checkSize.bind(this)\n Platform.miniapp.onWindowResize(this.checkSize)\n }\n }\n\n public checkSize(): void {\n if (this.viewSelect) {\n setTimeout(() => {\n this.updateClientBounds(() => {\n const { width, height } = this.clientBounds\n const { pixelRatio } = this\n const size = { width, height, pixelRatio }\n if (!this.isSameSize(size)) this.emitResize(size)\n })\n }, 500)\n }\n }\n\n public stopAutoLayout(): void {\n this.autoLayout = false\n this.resizeListener = null\n Platform.miniapp.offWindowResize(this.checkSize)\n }\n\n public unrealCanvas(): void { // App needs to use\n this.unreal = true\n }\n\n protected emitResize(size: IScreenSizeData): void {\n const oldSize = {} as IScreenSizeData\n DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs)\n this.resize(size)\n if (!isUndefined(this.width)) this.resizeListener(new ResizeEvent(size, oldSize))\n }\n\n}","export * from '@leafer/core'\n\nexport * from '@leafer/canvas-miniapp'\nexport * from '@leafer/image-miniapp'\n\nimport { ICanvasType, ICreator, IExportFileType, ILeaferCanvas, IExportImageType, IResponseType, IFunction, IObject, IMiniappSelect, IMiniappSizeView, IBoundsData, IImageCrossOrigin, ILeaferImage } from '@leafer/interface'\nimport { Platform, Creator, FileHelper, defineKey } from '@leafer/core'\n\nimport { LeaferCanvas } from '@leafer/canvas-miniapp'\nimport { LeaferImage } from '@leafer/image-miniapp'\n\n\nObject.assign(Creator, {\n canvas: (options?, manager?) => new LeaferCanvas(options, manager),\n image: (options) => new LeaferImage(options)\n} as ICreator)\n\n\nexport function useCanvas(_canvasType: ICanvasType, app?: IObject): void {\n Platform.origin = {\n createCanvas: (width: number, height: number, _format?: string) => {\n const data = { type: '2d', width, height }\n return app.createOffscreenCanvas ? app.createOffscreenCanvas(data) : app.createOffScreenCanvas(data) // fix: 微信小游戏居然使用的是Screen大写的 wx.createOffScreenCanvas() [踩坑]\n },\n canvasToDataURL: (canvas: IObject, type?: IExportImageType, quality?: number) => canvas.toDataURL(FileHelper.mimeType(type), quality),\n canvasToBolb: (canvas: IObject, type?: IExportFileType, quality?: number) => canvas.toBuffer(type, { quality }),\n canvasSaveAs: (canvas: IObject, filePath: string, quality?: any) => {\n let data: string = canvas.toDataURL(FileHelper.mimeType(FileHelper.fileType(filePath)), quality)\n data = data.substring(data.indexOf('64,') + 3)\n return Platform.origin.download(data, filePath)\n },\n download(data: string, filePath: string): Promise<void> {\n return new Promise((resolve, reject) => {\n let toAlbum: boolean\n if (!filePath.includes('/')) {\n filePath = `${app.env.USER_DATA_PATH}/` + filePath\n toAlbum = true\n }\n const fs = app.getFileSystemManager()\n fs.writeFile({\n filePath,\n data,\n encoding: 'base64',\n success() {\n if (toAlbum) {\n Platform.miniapp.saveToAlbum(filePath).then(() => {\n fs.unlink({ filePath })\n resolve()\n })\n } else {\n resolve()\n }\n\n },\n fail(error: any) {\n reject(error)\n }\n })\n })\n },\n loadImage(src: string, _crossOrigin?: IImageCrossOrigin, _leaferImage?: ILeaferImage): Promise<HTMLImageElement> {\n return new Promise((resolve, reject) => {\n const img = Platform.getCanvas().view.createImage() // 抖音小程序 Platform.canvas 图片无法加载,需使用 Platform.renderCanvas\n img.onload = () => { resolve(img) }\n img.onerror = (error: any) => { reject(error) }\n img.src = Platform.image.getRealURL(src)\n })\n },\n loadContent(url: string, responseType: IResponseType = 'text'): Promise<any> {\n return new Promise((resolve, reject) =>\n app.request({\n url,\n responseType: responseType === 'arrayBuffer' ? 'arraybuffer' : 'text',\n success: (res: any) => resolve(responseType === 'json' && typeof res.data === 'string' ? JSON.parse(res.data) : res.data),\n fail: reject\n })\n )\n },\n noRepeat: 'repeat-x' // fix: 微信小程序 createPattern 直接使用 no-repeat 有bug,导致无法显示\n }\n\n Platform.miniapp = {\n select(name: string): IMiniappSelect {\n return app.createSelectorQuery().select(name)\n },\n getBounds(select: IMiniappSelect): Promise<IBoundsData> {\n return new Promise((resolve) => {\n select.boundingClientRect().exec((res: any) => {\n const rect = res[1]\n resolve({ x: rect.top, y: rect.left, width: rect.width, height: rect.height })\n })\n })\n },\n getSizeView(select: IMiniappSelect): Promise<IMiniappSizeView> {\n return new Promise((resolve) => {\n select.fields({ node: true, size: true }).exec((res: any) => {\n const data = res[0]\n resolve({ view: data.node, width: data.width, height: data.height })\n })\n })\n },\n saveToAlbum(path: string): Promise<any> {\n return new Promise((resolve) => {\n app.getSetting({\n success: (res: any) => {\n if (res.authSetting['scope.writePhotosAlbum']) {\n app.saveImageToPhotosAlbum({\n filePath: path,\n success() { resolve(true) }\n })\n } else {\n app.authorize({\n scope: 'scope.writePhotosAlbum',\n success: () => {\n app.saveImageToPhotosAlbum({\n filePath: path,\n success() { resolve(true) }\n })\n },\n fail: () => { }\n })\n }\n }\n })\n })\n },\n onWindowResize(fun: IFunction): void {\n app.onWindowResize(fun)\n },\n offWindowResize(fun: IFunction): void {\n app.offWindowResize(fun)\n }\n }\n\n Platform.event = {\n stopDefault(_origin: IObject): void { },\n stopNow(_origin: IObject): void { },\n stop(_origin: IObject): void { }\n }\n\n Platform.canvas = Creator.canvas()\n Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient\n\n defineKey(Platform, 'devicePixelRatio', { get() { return Math.max(1, app.getWindowInfo ? app.getWindowInfo().pixelRatio : app.getSystemInfoSync().pixelRatio) } })\n}\n\n\nPlatform.name = 'miniapp'\n\nPlatform.getCanvas = function (): ILeaferCanvas {\n const { renderCanvas } = Platform\n return (renderCanvas && renderCanvas.view) ? renderCanvas : Platform.canvas\n}\n\nPlatform.requestRender = function (render: IFunction): void {\n const { view } = Platform.getCanvas()\n view.requestAnimationFrame ? view.requestAnimationFrame(render) : setTimeout(render, 16) // fix 抖音小程序\n}\n\n","import { ILeaf, IWatcher, IEventListenerId, ILeafList, IWatcherConfig } from '@leafer/interface'\nimport { PropertyEvent, ChildEvent, RenderEvent, WatchEvent, LeafList, DataHelper } from '@leafer/core'\n\n\nexport class Watcher implements IWatcher {\n\n public target: ILeaf\n\n public totalTimes: number = 0\n\n public disabled: boolean\n public running: boolean\n public changed: boolean\n\n public hasVisible: boolean\n public hasAdd: boolean\n public hasRemove: boolean\n public get childrenChanged() { return this.hasAdd || this.hasRemove || this.hasVisible }\n\n public config: IWatcherConfig = {}\n\n public get updatedList(): ILeafList {\n if (this.hasRemove && this.config.usePartLayout) {\n const updatedList = new LeafList()\n this.__updatedList.list.forEach(item => { if (item.leafer) updatedList.add(item) })\n return updatedList\n } else {\n return this.__updatedList\n }\n }\n\n protected __eventIds: IEventListenerId[]\n protected __updatedList: ILeafList = new LeafList()\n\n constructor(target: ILeaf, userConfig?: IWatcherConfig) {\n this.target = target\n if (userConfig) this.config = DataHelper.default(userConfig, this.config)\n this.__listenEvents()\n }\n\n public start(): void {\n if (this.disabled) return\n this.running = true\n }\n\n public stop(): void {\n this.running = false\n }\n\n public disable(): void {\n this.stop()\n this.__removeListenEvents()\n this.disabled = true\n }\n\n public update(): void {\n this.changed = true\n if (this.running) this.target.emit(RenderEvent.REQUEST)\n }\n\n protected __onAttrChange(event: PropertyEvent): void {\n if (this.config.usePartLayout) this.__updatedList.add(event.target as ILeaf)\n this.update()\n }\n\n protected __onChildEvent(event: ChildEvent): void {\n if (this.config.usePartLayout) {\n if (event.type === ChildEvent.ADD) {\n this.hasAdd = true\n this.__pushChild(event.child)\n } else {\n this.hasRemove = true\n this.__updatedList.add(event.parent)\n }\n }\n this.update()\n }\n\n protected __pushChild(child: ILeaf): void {\n this.__updatedList.add(child)\n if (child.isBranch) this.__loopChildren(child)\n }\n\n protected __loopChildren(parent: ILeaf): void {\n const { children } = parent\n for (let i = 0, len = children.length; i < len; i++) this.__pushChild(children[i])\n }\n\n public __onRquestData(): void {\n this.target.emitEvent(new WatchEvent(WatchEvent.DATA, { updatedList: this.updatedList }))\n this.__updatedList = new LeafList()\n this.totalTimes++\n this.changed = this.hasVisible = this.hasRemove = this.hasAdd = false\n }\n\n protected __listenEvents(): void {\n this.__eventIds = [\n this.target.on_([\n [PropertyEvent.CHANGE, this.__onAttrChange, this],\n [[ChildEvent.ADD, ChildEvent.REMOVE], this.__onChildEvent, this],\n [WatchEvent.REQUEST, this.__onRquestData, this]\n ])\n ]\n }\n\n protected __removeListenEvents(): void {\n this.target.off_(this.__eventIds)\n }\n\n public destroy(): void {\n if (this.target) {\n this.stop()\n this.__removeListenEvents()\n this.target = this.__updatedList = null\n }\n }\n\n}","import { ILeafLayout, ILeafLevelList, ILeafList, ILeaf } from '@leafer/interface'\nimport { BranchHelper, LeafHelper } from '@leafer/core'\n\n\nconst { updateAllMatrix, updateBounds: updateOneBounds, updateChange: updateOneChange } = LeafHelper\nconst { pushAllChildBranch, pushAllParent } = BranchHelper\n\n\nexport function updateMatrix(updateList: ILeafList, levelList: ILeafLevelList): void {\n\n let layout: ILeafLayout\n updateList.list.forEach(leaf => { // 更新矩阵, 所有子元素,和父元素都需要更新bounds\n layout = leaf.__layout\n if (levelList.without(leaf) && !layout.proxyZoom) { // 防止重复, 子元素可能已经被父元素更新过\n\n if (layout.matrixChanged) {\n\n updateAllMatrix(leaf, true)\n\n levelList.add(leaf)\n if (leaf.isBranch) pushAllChildBranch(leaf, levelList)\n pushAllParent(leaf, levelList)\n\n } else if (layout.boundsChanged) {\n\n levelList.add(leaf)\n if (leaf.isBranch) leaf.__tempNumber = 0 // 标识需要更新子Leaf元素的WorldBounds分支, 0表示不需要更新\n pushAllParent(leaf, levelList)\n }\n }\n })\n\n}\n\nexport function updateBounds(boundsList: ILeafLevelList): void {\n let list: ILeaf[], branch: ILeaf, children: ILeaf[]\n boundsList.sort(true)\n boundsList.levels.forEach(level => {\n list = boundsList.levelMap[level]\n for (let i = 0, len = list.length; i < len; i++) {\n branch = list[i]\n\n // 标识了需要更新子元素\n if (branch.isBranch && branch.__tempNumber) {\n children = branch.children\n for (let j = 0, jLen = children.length; j < jLen; j++) {\n if (!children[j].isBranch) {\n updateOneBounds(children[j])\n }\n }\n }\n updateOneBounds(branch)\n }\n })\n}\n\n\nexport function updateChange(updateList: ILeafList): void {\n updateList.list.forEach(updateOneChange)\n}","import { IBounds, ILayoutBlockData, ILeafList, ILeaf } from '@leafer/interface'\nimport { Bounds, LeafBoundsHelper, LeafList, isArray } from '@leafer/core'\n\n\nconst { worldBounds } = LeafBoundsHelper\n\nexport class LayoutBlockData implements ILayoutBlockData {\n\n public updatedList: ILeafList\n public updatedBounds: IBounds = new Bounds()\n\n public beforeBounds: IBounds = new Bounds()\n public afterBounds: IBounds = new Bounds()\n\n constructor(list: ILeafList | ILeaf[]) {\n if (isArray(list)) list = new LeafList(list)\n this.updatedList = list\n }\n\n public setBefore(): void {\n this.beforeBounds.setListWithFn(this.updatedList.list, worldBounds)\n }\n\n public setAfter(): void {\n this.afterBounds.setListWithFn(this.updatedList.list, worldBounds)\n this.updatedBounds.setList([this.beforeBounds, this.afterBounds])\n }\n\n public merge(data: ILayoutBlockData): void {\n this.updatedList.addList(data.updatedList.list)\n this.beforeBounds.add(data.beforeBounds)\n this.afterBounds.add(data.afterBounds)\n this.updatedBounds.add(data.updatedBounds)\n }\n\n public destroy(): void {\n this.updatedList = null\n }\n\n}","import { ILayouter, ILeaf, ILayoutBlockData, IEventListenerId, ILayouterConfig, ILeafList } from '@leafer/interface'\nimport { LayoutEvent, WatchEvent, LeafLevelList, LeafList, BranchHelper, LeafHelper, DataHelper, Run, Debug } from '@leafer/core'\n\nimport { updateMatrix, updateBounds, updateChange } from './LayouterHelper'\nimport { LayoutBlockData } from './LayoutBlockData'\n\n\nconst { updateAllMatrix, updateAllChange } = LeafHelper\n\nconst debug = Debug.get('Layouter')\n\nexport class Layouter implements ILayouter {\n\n public target: ILeaf\n public layoutedBlocks: ILayoutBlockData[]\n public extraBlock: ILayoutBlockData // around / autoLayout\n\n public totalTimes: number = 0\n public times: number\n\n public disabled: boolean\n public running: boolean\n public layouting: boolean\n\n public waitAgain: boolean\n\n public config: ILayouterConfig = { usePartLayout: true }\n\n public __updatedList: ILeafList\n protected __levelList: LeafLevelList = new LeafLevelList()\n protected __eventIds: IEventListenerId[]\n\n constructor(target: ILeaf, userConfig?: ILayouterConfig) {\n this.target = target\n if (userConfig) this.config = DataHelper.default(userConfig, this.config)\n this.__listenEvents()\n }\n\n public start(): void {\n if (this.disabled) return\n this.running = true\n }\n\n public stop(): void {\n this.running = false\n }\n\n public disable(): void {\n this.stop()\n this.__removeListenEvents()\n this.disabled = true\n }\n\n public layout(): void {\n if (this.layouting || !this.running) return\n const { target } = this\n this.times = 0\n\n try {\n target.emit(LayoutEvent.START)\n this.layoutOnce()\n target.emitEvent(new LayoutEvent(LayoutEvent.END, this.layoutedBlocks, this.times))\n } catch (e) {\n debug.error(e)\n }\n\n this.layoutedBlocks = null\n }\n\n public layoutAgain(): void {\n if (this.layouting) {\n this.waitAgain = true\n } else {\n this.layoutOnce()\n }\n }\n\n public layoutOnce(): void {\n if (this.layouting) return debug.warn('layouting')\n if (this.times > 3) return debug.warn('layout max times')\n\n this.times++\n this.totalTimes++\n\n this.layouting = true\n\n this.target.emit(WatchEvent.REQUEST)\n\n if (this.totalTimes > 1 && this.config.usePartLayout) {\n this.partLayout()\n } else {\n this.fullLayout()\n }\n\n this.layouting = false\n\n if (this.waitAgain) {\n this.waitAgain = false\n this.layoutOnce()\n }\n }\n\n public partLayout(): void {\n if (!this.__updatedList?.length) return\n\n const t = Run.start('PartLayout')\n const { target, __updatedList: updateList } = this\n const { BEFORE, LAYOUT, AFTER } = LayoutEvent\n\n const blocks = this.getBlocks(updateList)\n blocks.forEach(item => item.setBefore())\n target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times))\n\n this.extraBlock = null\n updateList.sort()\n updateMatrix(updateList, this.__levelList)\n updateBounds(this.__levelList)\n updateChange(updateList)\n\n if (this.extraBlock) blocks.push(this.extraBlock)\n blocks.forEach(item => item.setAfter())\n\n target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times))\n target.emitEvent(new LayoutEvent(AFTER, blocks, this.times))\n\n this.addBlocks(blocks)\n\n this.__levelList.reset()\n this.__updatedList = null\n Run.end(t)\n }\n\n public fullLayout(): void {\n const t = Run.start('FullLayout')\n\n const { target } = this\n const { BEFORE, LAYOUT, AFTER } = LayoutEvent\n\n const blocks = this.getBlocks(new LeafList(target))\n target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times))\n\n Layouter.fullLayout(target)\n\n blocks.forEach(item => { item.setAfter() })\n target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times))\n target.emitEvent(new LayoutEvent(AFTER, blocks, this.times))\n\n this.addBlocks(blocks)\n\n Run.end(t)\n }\n\n static fullLayout(target: ILeaf): void {\n updateAllMatrix(target, true)\n\n if (target.isBranch) BranchHelper.updateBounds(target)\n else LeafHelper.updateBounds(target)\n\n updateAllChange(target)\n }\n\n public addExtra(leaf: ILeaf): void {\n if (!this.__updatedList.has(leaf)) {\n const { updatedList, beforeBounds } = this.extraBlock || (this.extraBlock = new LayoutBlockData([]))\n updatedList.length ? beforeBounds.add(leaf.__world) : beforeBounds.set(leaf.__world)\n updatedList.add(leaf)\n }\n }\n\n public createBlock(data: ILeafList | ILeaf[]): ILayoutBlockData {\n return new LayoutBlockData(data)\n }\n\n public getBlocks(list: ILeafList): ILayoutBlockData[] {\n return [this.createBlock(list)]\n }\n\n public addBlocks(current: ILayoutBlockData[]) {\n this.layoutedBlocks ? this.layoutedBlocks.push(...current) : this.layoutedBlocks = current\n }\n\n protected __onReceiveWatchData(event: WatchEvent): void {\n this.__updatedList = event.data.updatedList\n }\n\n protected __listenEvents(): void {\n this.__eventIds = [\n this.target.on_([\n [LayoutEvent.REQUEST, this.layout, this],\n [LayoutEvent.AGAIN, this.layoutAgain, this],\n [WatchEvent.DATA, this.__onReceiveWatchData, this]\n ])\n ]\n }\n\n protected __removeListenEvents(): void {\n this.target.off_(this.__eventIds)\n }\n\n public destroy(): void {\n if (this.target) {\n this.stop()\n this.__removeListenEvents()\n this.target = this.config = null\n }\n }\n\n}\n\n\n","import { ILeaf, ILeaferBase, ILeaferCanvas, IRenderer, IRendererConfig, IEventListenerId, IBounds, IFunction, IRenderOptions, ILeafList } from '@leafer/interface'\nimport { LayoutEvent, RenderEvent, ResizeEvent, ImageManager, Bounds, DataHelper, Platform, Debug, Run } from '@leafer/core'\n\n\nconst debug = Debug.get('Renderer')\n\nexport class Renderer implements IRenderer {\n\n public target: ILeaf\n public canvas: ILeaferCanvas\n public updateBlocks: IBounds[]\n\n public FPS = 60\n public totalTimes = 0\n public times: number = 0\n\n public running: boolean\n public rendering: boolean\n\n public waitAgain: boolean\n public changed: boolean\n public ignore: boolean\n\n public config: IRendererConfig = {\n usePartRender: true,\n ceilPartPixel: true,\n maxFPS: 120\n }\n\n static clipSpread = 10\n\n protected renderBounds: IBounds\n protected renderOptions: IRenderOptions\n protected totalBounds: IBounds\n\n protected requestTime: number\n protected frameTime: number\n protected frames: number[] = []\n protected __eventIds: IEventListenerId[]\n\n protected get needFill(): boolean { return !!(!this.canvas.allowBackgroundColor && this.config.fill) }\n\n constructor(target: ILeaf, canvas: ILeaferCanvas, userConfig?: IRendererConfig) {\n this.target = target\n this.canvas = canvas\n if (userConfig) this.config = DataHelper.default(userConfig, this.config)\n this.__listenEvents()\n }\n\n public start(): void {\n this.running = true\n this.update(false)\n }\n\n public stop(): void {\n this.running = false\n }\n\n public update(change = true): void {\n if (!this.changed) this.changed = change\n if (!this.requestTime) this.__requestRender()\n }\n\n public requestLayout(): void {\n this.target.emit(LayoutEvent.REQUEST)\n }\n\n public checkRender(): void {\n if (this.running) {\n const { target } = this\n if (target.isApp) {\n target.emit(RenderEvent.CHILD_START, target);\n (target.children as ILeaferBase[]).forEach(leafer => {\n leafer.renderer.FPS = this.FPS\n leafer.renderer.checkRender()\n })\n target.emit(RenderEvent.CHILD_END, target)\n }\n\n if (this.changed && this.canvas.view) this.render()\n this.target.emit(RenderEvent.NEXT)\n }\n }\n\n public render(callback?: IFunction): void {\n if (!(this.running && this.canvas.view)) return this.update()\n\n const { target } = this\n this.times = 0\n this.totalBounds = new Bounds()\n\n debug.log(target.innerName, '--->')\n\n try {\n this.emitRender(RenderEvent.START)\n this.renderOnce(callback)\n this.emitRender(RenderEvent.END, this.totalBounds)\n\n ImageManager.clearRecycled()\n } catch (e) {\n this.rendering = false\n debug.error(e)\n }\n\n debug.log('-------------|')\n }\n\n public renderAgain(): void {\n if (this.rendering) {\n this.waitAgain = true\n } else {\n this.renderOnce()\n }\n }\n\n public renderOnce(callback?: IFunction): void {\n if (this.rendering) return debug.warn('rendering')\n if (this.times > 3) return debug.warn('render max times')\n\n this.times++\n this.totalTimes++\n\n this.rendering = true\n this.changed = false\n this.renderBounds = new Bounds()\n this.renderOptions = {}\n\n if (callback) {\n this.emitRender(RenderEvent.BEFORE)\n callback()\n } else {\n this.requestLayout()\n\n if (this.ignore) {\n this.ignore = this.rendering = false // 仍保留 updateBlocks 用于下次渲染\n return\n }\n\n this.emitRender(RenderEvent.BEFORE)\n\n if (this.config.usePartRender && this.totalTimes > 1) {\n this.partRender()\n } else {\n this.fullRender()\n }\n }\n\n this.emitRender(RenderEvent.RENDER, this.renderBounds, this.renderOptions)\n this.emitRender(RenderEvent.AFTER, this.renderBounds, this.renderOptions)\n\n this.updateBlocks = null\n this.rendering = false\n\n if (this.waitAgain) {\n this.waitAgain = false\n this.renderOnce()\n }\n }\n\n public partRender(): void {\n const { canvas, updateBlocks: list } = this\n if (!list) return // debug.warn('PartRender: need update attr')\n\n this.mergeBlocks()\n list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty()) this.clipRender(block) })\n }\n\n public clipRender(block: IBounds): void {\n const t = Run.start('PartRender')\n const { canvas } = this, bounds = block.getIntersect(canvas.bounds), realBounds = new Bounds(bounds)\n\n canvas.save()\n\n bounds.spread(Renderer.clipSpread).ceil() // 局部渲染区域需扩大一些,避免出现残影\n\n const { ceilPartPixel } = this.config\n canvas.clipWorld(bounds, ceilPartPixel)\n canvas.clearWorld(bounds, ceilPartPixel)\n\n this.__render(bounds, realBounds)\n canvas.restore()\n\n Run.end(t)\n }\n\n public fullRender(): void {\n const t = Run.start('FullRender')\n const { canvas } = this\n\n canvas.save()\n canvas.clear()\n this.__render(canvas.bounds)\n canvas.restore()\n\n Run.end(t)\n }\n\n protected __render(bounds: IBounds, realBounds?: IBounds): void {\n const { canvas, target } = this, includes = bounds.includes(target.__world), options: IRenderOptions = includes ? { includes } : { bounds, includes }\n\n if (this.needFill) canvas.fillWorld(bounds, this.config.fill)\n if (Debug.showRepaint) Debug.drawRepaint(canvas, bounds)\n\n if (this.config.useCellRender) options.cellList = this.getCellList()\n\n Platform.render(target, canvas, options)\n\n this.renderBounds = realBounds = realBounds || bounds\n this.renderOptions = options\n this.totalBounds.isEmpty() ? this.totalBounds = realBounds : this.totalBounds.add(realBounds)\n\n canvas.updateRender(realBounds)\n }\n\n // need rewrite\n getCellList(): ILeafList {\n return undefined\n }\n\n public addBlock(block: IBounds, _leafList?: ILeafList): void {\n if (!this.updateBlocks) this.updateBlocks = []\n this.updateBlocks.push(block)\n }\n\n public mergeBlocks(): void {\n const { updateBlocks: list } = this\n if (list) {\n const bounds = new Bounds()\n bounds.setList(list)\n list.length = 0\n list.push(bounds)\n }\n }\n\n protected __requestRender(): void {\n const target = this.target as ILeaferBase\n if (this.requestTime || !target) return\n if (target.parentApp) return target.parentApp.requestRender(false) // App 模式下统一走 app 控制渲染帧\n\n this.requestTime = this.frameTime || Date.now()\n\n const render = () => {\n\n const nowFPS = 1000 / ((this.frameTime = Date.now()) - this.requestTime)\n\n const { maxFPS } = this.config\n if (maxFPS && nowFPS > maxFPS) return Platform.requestRender(render)\n\n const { frames } = this\n if (frames.length > 30) frames.shift()\n frames.push(nowFPS)\n this.FPS = Math.round(frames.reduce((a, b) => a + b, 0) / frames.length) // 帧率采样\n this.requestTime = 0\n\n this.checkRender()\n\n }\n\n Platform.requestRender(render)\n }\n\n protected __onResize(e: ResizeEvent): void {\n if (this.canvas.unreal) return\n if (e.bigger || !e.samePixelRatio) {\n const { width, height } = e.old\n const bounds = new Bounds(0, 0, width, height)\n if (!bounds.includes(this.target.__world) || this.needFill || !e.samePixelRatio) {\n this.addBlock(this.canvas.bounds)\n this.target.forceUpdate('surface')\n return\n }\n }\n\n // 需要象征性派发一下渲染事件\n this.addBlock(new Bounds(0, 0, 1, 1))\n this.update()\n }\n\n protected __onLayoutEnd(event: LayoutEvent): void {\n if (event.data) event.data.map(item => {\n let empty: boolean\n const { updatedList } = item\n if (updatedList) updatedList.list.some(leaf => {\n empty = (!leaf.__world.width || !leaf.__world.height)\n if (empty) {\n if (!leaf.isLeafer) debug.tip(leaf.innerName, ': empty')\n empty = (!leaf.isBranch || leaf.isBranchLeaf) // render object\n }\n return empty\n })\n this.addBlock(empty ? this.canvas.bounds : item.updatedBounds, updatedList)\n })\n }\n\n protected emitRender(type: string, bounds?: IBounds, options?: IRenderOptions): void {\n this.target.emitEvent(new RenderEvent(type, this.times, bounds, options))\n }\n\n protected __listenEvents(): void {\n this.__eventIds = [\n this.target.on_([\n [RenderEvent.REQUEST, this.update, this],\n [LayoutEvent.END, this.__onLayoutEnd, this],\n [RenderEvent.AGAIN, this.renderAgain, this],\n [ResizeEvent.RESIZE, this.__onResize, this]\n ])\n ]\n }\n\n protected __removeListenEvents(): void {\n this.target.off_(this.__eventIds)\n }\n\n public destroy(): void {\n if (this.target) {\n this.stop()\n this.__removeListenEvents()\n this.config = {}\n this.target = this.canvas = null\n }\n }\n}","import { ILeaferCanvas, IRenderOptions } from '@leafer/interface'\n\nimport { IImagePaint, ILeafPaint, IUI } from '@leafer-ui/interface'\nimport { PaintImage, Paint } from \"@leafer-ui/draw\"\n\n\nexport function fill(fill: string, ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n canvas.fillStyle = fill\n fillPathOrText(ui, canvas, renderOptions)\n}\n\n\nexport function fills(fills: ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n let item: ILeafPaint, originPaint: IImagePaint, countImage: number\n for (let i = 0, len = fills.length; i < len; i++) {\n item = fills[i], originPaint = item.originPaint as IImagePaint\n\n if (item.image) {\n\n countImage ? countImage++ : countImage = 1\n\n if (PaintImage.checkImage(item, !ui.__.__font, ui, canvas, renderOptions)) continue\n\n if (!item.style) {\n if (countImage === 1 && item.image.isPlacehold) ui.drawImagePlaceholder(item, canvas, renderOptions) // 图片加载中或加载失败\n continue\n }\n\n }\n\n canvas.fillStyle = item.style\n\n if (item.transform || originPaint.scaleFixed) {\n\n canvas.save()\n if (item.transform) canvas.transform(item.transform)\n if (originPaint.scaleFixed) {\n const { scaleX, scaleY } = ui.getRenderScaleData(true, originPaint.scaleFixed, false)\n if (scaleX !== 1) canvas.scale(scaleX, scaleY)\n }\n if (originPaint.blendMode) canvas.blendMode = originPaint.blendMode\n fillPathOrText(ui, canvas, renderOptions)\n canvas.restore()\n\n } else {\n\n if (originPaint.blendMode) {\n\n canvas.saveBlendMode(originPaint.blendMode)\n fillPathOrText(ui, canvas, renderOptions)\n canvas.restoreBlendMode()\n\n } else fillPathOrText(ui, canvas, renderOptions)\n\n }\n\n }\n}\n\n\nexport function fillPathOrText(ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n ui.__.__font ? Paint.fillText(ui, canvas, renderOptions) : (ui.__.windingRule ? canvas.fill(ui.__.windingRule) : canvas.fill())\n}","import { ILeaferCanvas, IRenderOptions } from '@leafer/interface'\nimport { LeafHelper, isObject } from \"@leafer/core\"\n\nimport { IUI, ILeafPaint } from '@leafer-ui/interface'\nimport { Paint } from '@leafer-ui/draw'\n\n\nexport function stroke(stroke: string | ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n const data = ui.__\n if (!data.__strokeWidth) return\n\n if (data.__font) {\n\n Paint.strokeText(stroke, ui, canvas, renderOptions)\n\n } else if (data.__pathForStroke) {\n\n Paint.fillStroke(stroke, ui, canvas, renderOptions)\n\n } else {\n\n switch (data.strokeAlign) {\n case 'center':\n drawCenter(stroke, 1, ui, canvas, renderOptions)\n break\n case 'inside':\n drawInside(stroke, ui, canvas, renderOptions)\n break\n case 'outside':\n drawOutside(stroke, ui, canvas, renderOptions)\n break\n }\n\n }\n}\n\n\nexport function strokes(strokes: ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n Paint.stroke(strokes, ui, canvas, renderOptions)\n}\n\n\nfunction drawCenter(stroke: string | ILeafPaint[], strokeWidthScale: number, ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions) {\n const data = ui.__\n if (isObject(stroke)) {\n Paint.drawStrokesStyle(stroke, strokeWidthScale, false, ui, canvas, renderOptions)\n } else {\n canvas.setStroke(stroke, data.__strokeWidth * strokeWidthScale, data)\n canvas.stroke()\n }\n\n if (data.__useArrow) Paint.strokeArrow(stroke, ui, canvas, renderOptions)\n}\n\nfunction drawInside(stroke: string | ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions) {\n canvas.save()\n canvas.clipUI(ui)\n\n drawCenter(stroke, 2, ui, canvas, renderOptions)\n canvas.restore()\n}\n\nfunction drawOutside(stroke: string | ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions) {\n const data = ui.__\n if (data.__fillAfterStroke) {\n\n drawCenter(stroke, 2, ui, canvas, renderOptions)\n\n } else {\n const { renderBounds } = ui.__layout\n const out = canvas.getSameCanvas(true, true)\n ui.__drawRenderPath(out)\n\n drawCenter(stroke, 2, ui, out, renderOptions)\n\n out.clipUI(data)\n out.clearWorld(renderBounds)\n\n LeafHelper.copyCanvasByWorld(ui, canvas, out)\n\n out.recycle(ui.__nowWorld)\n }\n}\n","import { ILeaferCanvas, IRenderOptions } from '@leafer/interface'\nimport { LeafHelper, isObject } from \"@leafer/core\"\n\nimport { IUI, ITextRowData, ILeafPaint, IStrokeAlign, ILeafStrokePaint } from '@leafer-ui/interface'\nimport { PaintImage, Paint } from \"@leafer-ui/draw\"\n\n\nexport function strokeText(stroke: string | ILeafPaint[], ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n switch (ui.__.strokeAlign) {\n case 'center':\n drawCenter(stroke, 1, ui, canvas, renderOptions)\n break\n case 'inside':\n drawAlign(stroke, 'inside', ui, canvas, renderOptions)\n break\n case 'outside':\n ui.__.__fillAfterStroke ? drawCenter(stroke, 2, ui, canvas, renderOptions) : drawAlign(stroke, 'outside', ui, canvas, renderOptions)\n break\n }\n}\n\nfunction drawCenter(stroke: string | ILeafPaint[], strokeWidthScale: number, ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n const data = ui.__\n if (isObject(stroke)) {\n Paint.drawStrokesStyle(stroke, strokeWidthScale, true, ui, canvas, renderOptions)\n } else {\n canvas.setStroke(stroke, data.__strokeWidth * strokeWidthScale, data)\n Paint.drawTextStroke(ui, canvas, renderOptions)\n }\n}\n\nfunction drawAlign(stroke: string | ILeafPaint[], align: IStrokeAlign, ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n const out = canvas.getSameCanvas(true, true)\n out.font = ui.__.__font\n drawCenter(stroke, 2, ui, out, renderOptions)\n\n out.blendMode = align === 'outside' ? 'destination-out' : 'destination-in'\n Paint.fillText(ui, out, renderOptions)\n out.blendMode = 'normal'\n\n LeafHelper.copyCanvasByWorld(ui, canvas, out)\n\n out.recycle(ui.__nowWorld)\n}\n\n\nexport function drawTextStroke(ui: IUI, canvas: ILeaferCanvas, _renderOptions: IRenderOptions): void {\n\n let row: ITextRowData, data = ui.__.__textDrawData\n const { rows, decorationY } = data\n\n for (let i = 0, len = rows.length; i < len; i++) {\n row = rows[i]\n\n if (row.text) canvas.strokeText(row.text, row.x, row.y)\n else if (row.data) row.data.forEach(charData => { canvas.strokeText(charData.char, charData.x, row.y) })\n }\n\n if (decorationY) {\n const { decorationHeight } = data\n rows.forEach(row => decorationY.forEach(value => canvas.strokeRect(row.x, row.y + value, row.width, decorationHeight)))\n }\n\n}\n\nexport function drawStrokesStyle(strokes: ILeafStrokePaint[], strokeWidthScale: number, isText: boolean, ui: IUI, canvas: ILeaferCanvas, renderOptions: IRenderOptions): void {\n let item: ILeafStrokePaint\n const data = ui.__, { __hasMultiStrokeStyle } = data\n __hasMultiStrokeStyle || canvas.setStroke(undefined, data.__strokeWidth * strokeWidthScale, data)\n\n for (let i = 0, len = strokes.length; i < len; i++) {\n item = strokes[i]\n\n if (item.image && PaintImage.checkImage(item, false, ui, canvas, renderOptions)) continue\n\n if (item.style) {\n if (__hasMultiStrokeStyle) {\n const { strokeStyle } = item\n strokeStyle ? canvas.setStroke(item.style, data.__getRealStrokeWidth(strokeStyle) * strokeWidthScale, data, strokeStyle) : canvas.s