diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnife.ets b/imageknife/src/main/ets/components/imageknife/ImageKnife.ets index adcf174..a725a71 100644 --- a/imageknife/src/main/ets/components/imageknife/ImageKnife.ets +++ b/imageknife/src/main/ets/components/imageknife/ImageKnife.ets @@ -59,6 +59,8 @@ export class ImageKnife { private defaultLifeCycle: IDrawLifeCycle; + private debugLog: boolean = false; + private constructor(imgCtx) { this.imageKnifeContext = imgCtx; @@ -175,6 +177,20 @@ export class ImageKnife { } } + /** + * 是否开启日志打印 true开启 false关闭 默认不开启 + * @param openLog + */ + public setDebugLog(openLog:boolean){ + this.debugLog = openLog + } + + public log(info:string){ + if(this.debugLog){ + globalThis.ImageKnife.log(info) + } + } + // 预加载 resource资源一级缓存,string资源实现二级缓存 preload(request: RequestOption) { // 每个request 公共信息补充 @@ -326,7 +342,7 @@ export class ImageKnife { } } else { - console.log("key没有生成无法进入存取!") + globalThis.ImageKnife.log("key没有生成无法进入存取!") } @@ -370,7 +386,7 @@ export class ImageKnife { request.diskCacheStrategy(none); this.loadResources(request); } else { - console.error("输入参数有问题!") + globalThis.ImageKnife.error("输入参数有问题!") } } } diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets b/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets index 936f8cb..4a14977 100644 --- a/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets +++ b/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets @@ -97,7 +97,7 @@ export struct ImageKnifeComponent { // 前提:宽高值均有效,值>0. 条件1:当前宽高与上一次宽高不同 条件2:当前是第一次绘制 if( (this.currentHeight != this.lastHeight || this.currentWidth != this.lastWidth) || this.firstDrawFlag){ this.firstDrawFlag = false; - console.log('ImageKnifeComponent onAreaChange isValid Canvas currentWidth =' + this.currentWidth + ' currentHeight=' + this.currentHeight) + globalThis.ImageKnife.log('ImageKnifeComponent onAreaChange isValid Canvas currentWidth =' + this.currentWidth + ' currentHeight=' + this.currentHeight) this.lastWidth = this.currentWidth this.lastHeight = this.currentHeight this.imageKnifeExecute() @@ -107,7 +107,7 @@ export struct ImageKnifeComponent { .onReady(() => { this.canvasHasReady = true; if(this.onReadyNext){ - console.log('ImageKnifeComponent onReadyNext is running!') + globalThis.ImageKnife.log('ImageKnifeComponent onReadyNext is running!') this.onReadyNext() this.onReadyNext = undefined; } @@ -124,7 +124,7 @@ export struct ImageKnifeComponent { } watchImageKnifeOption() { - console.log('ImageKnifeComponent watchImageKnifeOption is happened!') + globalThis.ImageKnife.log('ImageKnifeComponent watchImageKnifeOption is happened!') this.whetherWaitSize(); } @@ -140,7 +140,7 @@ export struct ImageKnifeComponent { this.firstDrawFlag = true; } }else{ - console.log('ImageKnifeComponent whetherWaitSize 宽高有效 直接发送请求') + globalThis.ImageKnife.log('ImageKnifeComponent whetherWaitSize 宽高有效 直接发送请求') this.imageKnifeExecute() } } @@ -168,7 +168,7 @@ export struct ImageKnifeComponent { configNecessary(request: RequestOption) { request.load(this.imageKnifeOption.loadSrc) .addListener((err, data) => { - console.log('ImageKnifeComponent request.load callback') + globalThis.ImageKnife.log('ImageKnifeComponent request.load callback') this.runNextFunction(this.displayMainSource.bind(this,data)); return false; }) @@ -200,20 +200,20 @@ export struct ImageKnifeComponent { configDisplay(request: RequestOption) { if (this.imageKnifeOption.placeholderSrc) { request.placeholder(this.imageKnifeOption.placeholderSrc, (data) => { - console.log('ImageKnifeComponent request.placeholder callback') + globalThis.ImageKnife.log('ImageKnifeComponent request.placeholder callback') this.runNextFunction(this.displayPlaceholder.bind(this,data)) }) } if (this.imageKnifeOption.thumbSizeMultiplier) { request.thumbnail(this.imageKnifeOption.thumbSizeMultiplier, (data) => { - console.log('ImageKnifeComponent request.thumbnail callback') + globalThis.ImageKnife.log('ImageKnifeComponent request.thumbnail callback') this.runNextFunction(this.displayThumbSizeMultiplier.bind(this,data)) }, this.imageKnifeOption.thumbSizeDelay) } if (this.imageKnifeOption.errorholderSrc) { request.errorholder(this.imageKnifeOption.errorholderSrc, (data) => { - console.log('ImageKnifeComponent request.errorholder callback') + globalThis.ImageKnife.log('ImageKnifeComponent request.errorholder callback') this.runNextFunction(this.displayErrorholder.bind(this, data)) }) } @@ -235,14 +235,14 @@ export struct ImageKnifeComponent { if (this.imageKnifeOption.displayProgress) { request.addProgressListener((percentValue: number) => { // 如果进度条百分比 未展示大小,展示其动画 - console.log('ImageKnifeComponent request.addProgressListener callback') + globalThis.ImageKnife.log('ImageKnifeComponent request.addProgressListener callback') this.runNextFunction(this.displayProgress.bind(this,percentValue)) }) } if (this.imageKnifeOption.retryholderSrc) { request.retryholder(this.imageKnifeOption.retryholderSrc, (data) => { - console.log('ImageKnifeComponent request.retryholder callback') + globalThis.ImageKnife.log('ImageKnifeComponent request.retryholder callback') this.hasDisplayRetryholder = true this.runNextFunction(this.displayRetryholder.bind(this,data)) }) @@ -366,16 +366,16 @@ export struct ImageKnifeComponent { } drawPlaceholder(context: CanvasRenderingContext2D, data: ImageKnifeData, imageKnifeOption: ImageKnifeOption, compWidth: number, compHeight: number, setGifTimeId?: (timeId: number) => void) { - console.log('ImageKnifeComponent default drawPlaceholder start!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawPlaceholder start!') // @ts-ignore data.drawPixelMap.imagePixelMap.getImageInfo().then((imageInfo) => { - console.log('ImageKnifeComponent imageinfo widht =' + imageInfo.size.width + ' height=' + imageInfo.size.height) + globalThis.ImageKnife.log('ImageKnifeComponent imageinfo widht =' + imageInfo.size.width + ' height=' + imageInfo.size.height) let scaleType = (typeof imageKnifeOption.placeholderScaleType == 'number') ? imageKnifeOption.placeholderScaleType : ScaleType.FIT_CENTER context.save(); context.clearRect(0, 0, compWidth, compHeight) ScaleTypeHelper.drawImageWithScaleType(context, scaleType, data.drawPixelMap.imagePixelMap, px2vp(imageInfo.size.width), px2vp(imageInfo.size.height), compWidth, compHeight, 0, 0) context.restore(); - console.log('ImageKnifeComponent default drawPlaceholder end!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawPlaceholder end!') }) } @@ -421,31 +421,31 @@ export struct ImageKnifeComponent { } drawThumbSizeMultiplier(context: CanvasRenderingContext2D, data: ImageKnifeData, imageKnifeOption: ImageKnifeOption, compWidth: number, compHeight: number, setGifTimeId?: (timeId: number) => void) { - console.log('ImageKnifeComponent default drawThumbSizeMultiplier start!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawThumbSizeMultiplier start!') // @ts-ignore data.drawPixelMap.imagePixelMap.getImageInfo().then((imageInfo) => { - console.log('ImageKnifeComponent imageinfo widht =' + imageInfo.size.width + ' height=' + imageInfo.size.height) + globalThis.ImageKnife.log('ImageKnifeComponent imageinfo widht =' + imageInfo.size.width + ' height=' + imageInfo.size.height) let scaleType = (typeof imageKnifeOption.thumbSizeMultiplierScaleType == 'number') ? imageKnifeOption.thumbSizeMultiplierScaleType : ScaleType.FIT_CENTER context.save(); context.clearRect(0, 0, compWidth, compHeight) ScaleTypeHelper.drawImageWithScaleType(context, scaleType, data.drawPixelMap.imagePixelMap, px2vp(imageInfo.size.width), px2vp(imageInfo.size.height), compWidth, compHeight, 0, 0) context.restore(); - console.log('ImageKnifeComponent default drawThumbSizeMultiplier end!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawThumbSizeMultiplier end!') }) } drawMainSource(context: CanvasRenderingContext2D, data: ImageKnifeData, imageKnifeOption: ImageKnifeOption, compWidth: number, compHeight: number, setGifTimeId?: (timeId: number) => void) { - console.log('ImageKnifeComponent default drawMainSource start!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawMainSource start!') if (data.isPixelMap()) { // @ts-ignore data.drawPixelMap.imagePixelMap.getImageInfo().then((imageInfo) => { let scaleType = (typeof imageKnifeOption.mainScaleType == 'number') ? imageKnifeOption.mainScaleType : ScaleType.FIT_CENTER - console.log('ImageKnifeComponent imageinfo width =' + imageInfo.size.width + ' height=' + imageInfo.size.height + 'scaleType=' + scaleType) + globalThis.ImageKnife.log('ImageKnifeComponent imageinfo width =' + imageInfo.size.width + ' height=' + imageInfo.size.height + 'scaleType=' + scaleType) context.save(); context.clearRect(0, 0, compWidth, compHeight) ScaleTypeHelper.drawImageWithScaleType(context, scaleType, data.drawPixelMap.imagePixelMap, px2vp(imageInfo.size.width), px2vp(imageInfo.size.height), compWidth, compHeight, 0, 0) context.restore(); - console.log('ImageKnifeComponent default drawMainSource end!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawMainSource end!') }) } else if (data.isGIFFrame()) { this.drawGIFFrame(context, data, imageKnifeOption, compWidth, compHeight, setGifTimeId) @@ -453,30 +453,30 @@ export struct ImageKnifeComponent { } drawRetryholder(context: CanvasRenderingContext2D, data: ImageKnifeData, imageKnifeOption: ImageKnifeOption, compWidth: number, compHeight: number, setGifTimeId?: (timeId: number) => void) { - console.log('ImageKnifeComponent default drawRetryholder start!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawRetryholder start!') // @ts-ignore data.drawPixelMap.imagePixelMap.getImageInfo().then((imageInfo) => { - console.log('ImageKnifeComponent imageinfo width =' + imageInfo.size.width + ' height=' + imageInfo.size.height) + globalThis.ImageKnife.log('ImageKnifeComponent imageinfo width =' + imageInfo.size.width + ' height=' + imageInfo.size.height) let scaleType = (typeof imageKnifeOption.retryholderScaleType == 'number') ? imageKnifeOption.retryholderScaleType : ScaleType.FIT_CENTER context.save(); context.clearRect(0, 0, compWidth, compHeight) ScaleTypeHelper.drawImageWithScaleType(context, scaleType, data.drawPixelMap.imagePixelMap, px2vp(imageInfo.size.width), px2vp(imageInfo.size.height), compWidth, compHeight, 0, 0) context.restore(); - console.log('ImageKnifeComponent default drawRetryholder end!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawRetryholder end!') }) } drawErrorholder(context: CanvasRenderingContext2D, data: ImageKnifeData, imageKnifeOption: ImageKnifeOption, compWidth: number, compHeight: number, setGifTimeId?: (timeId: number) => void) { - console.log('ImageKnifeComponent default drawErrorholder start!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawErrorholder start!') // @ts-ignore data.drawPixelMap.imagePixelMap.getImageInfo().then((imageInfo) => { - console.log('ImageKnifeComponent imageinfo widht =' + imageInfo.size.width + ' height=' + imageInfo.size.height) + globalThis.ImageKnife.log('ImageKnifeComponent imageinfo widht =' + imageInfo.size.width + ' height=' + imageInfo.size.height) let scaleType = (typeof imageKnifeOption.errorholderSrcScaleType == 'number') ? imageKnifeOption.errorholderSrcScaleType : ScaleType.FIT_CENTER context.save(); context.clearRect(0, 0, compWidth, compHeight) ScaleTypeHelper.drawImageWithScaleType(context, scaleType, data.drawPixelMap.imagePixelMap, px2vp(imageInfo.size.width), px2vp(imageInfo.size.height), compWidth, compHeight, 0, 0) context.restore(); - console.log('ImageKnifeComponent default drawErrorholder end!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawErrorholder end!') }) } @@ -523,13 +523,13 @@ export struct ImageKnifeComponent { } aboutToAppear() { - console.log('ImageKnifeComponent aboutToAppear happened!') + globalThis.ImageKnife.log('ImageKnifeComponent aboutToAppear happened!') this.canvasHasReady = false; this.whetherWaitSize(true); } aboutToDisappear() { - console.log('ImageKnifeComponent aboutToDisappear happened!') + globalThis.ImageKnife.log('ImageKnifeComponent aboutToDisappear happened!') this.resetGifData(); } @@ -557,7 +557,7 @@ export struct ImageKnifeComponent { private drawGIFFrame(context: CanvasRenderingContext2D, data: ImageKnifeData, imageKnifeOption: ImageKnifeOption, compWidth: number, compHeight: number, setGifTimeId?: (timeId: number) => void) { let frames = data.drawGIFFrame.imageGIFFrames as GIFFrame[] - console.log('ImageKnifeComponent gifFrameLength =' + frames.length); + globalThis.ImageKnife.log('ImageKnifeComponent gifFrameLength =' + frames.length); if (imageKnifeOption.gif && (typeof (imageKnifeOption.gif.seekTo) == 'number') && imageKnifeOption.gif.seekTo >= 0) { this.autoPlay = false; context.clearRect(0, 0, compWidth, compHeight) @@ -586,7 +586,7 @@ export struct ImageKnifeComponent { } private renderFrames(frames: GIFFrame[], index: number, context: CanvasRenderingContext2D, compWidth: number, compHeight: number) { - console.log('ImageKnifeComponent renderFrames frames length =' + frames.length) + globalThis.ImageKnife.log('ImageKnifeComponent renderFrames frames length =' + frames.length) let start = new Date().getTime(); if (index === 0) { // 如果是第一帧,我们只从开始渲染前记录时间 @@ -647,7 +647,7 @@ export struct ImageKnifeComponent { // 具体绘制过程 private canvasDrawPixelMap(frames: GIFFrame[], index: number, context: CanvasRenderingContext2D, compWidth: number, compHeight: number) { - console.log('ImageKnifeComponent canvasDrawPixelMap index=' + index) + globalThis.ImageKnife.log('ImageKnifeComponent canvasDrawPixelMap index=' + index) let frame = frames[index]; let pixelmap = frame['drawPixelMap'] let disposal = 0 @@ -666,7 +666,7 @@ export struct ImageKnifeComponent { ScaleTypeHelper.drawImageWithScaleType(context, scaleType, pixelmap, px2vp(frameW), px2vp(frameH), compWidth, compHeight, px2vp(frame.dims.left), px2vp(frame.dims.top)) // tips:worker如果不是在展示页面中创建,使用子线程回来的数据创建的图片,会导致canvas绘制不出来 context.restore(); - console.log('ImageKnifeComponent default drawMainSource end!') + globalThis.ImageKnife.log('ImageKnifeComponent default drawMainSource end!') } } diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnifeDrawFactory.ets b/imageknife/src/main/ets/components/imageknife/ImageKnifeDrawFactory.ets index 8f10f73..e68a0d3 100644 --- a/imageknife/src/main/ets/components/imageknife/ImageKnifeDrawFactory.ets +++ b/imageknife/src/main/ets/components/imageknife/ImageKnifeDrawFactory.ets @@ -237,7 +237,6 @@ export class ImageKnifeDrawFactory{ context.restore(); } context.restore(); - console.log('TestImageKnifeOptionChangedPage4 drawMainSource end!') }) return true; } diff --git a/imageknife/src/main/ets/components/imageknife/RequestOption.ets b/imageknife/src/main/ets/components/imageknife/RequestOption.ets index acbdf82..b16d173 100644 --- a/imageknife/src/main/ets/components/imageknife/RequestOption.ets +++ b/imageknife/src/main/ets/components/imageknife/RequestOption.ets @@ -327,8 +327,8 @@ export class RequestOption { // 占位图解析成功 placeholderOnComplete(imageKnifeData: ImageKnifeData) { - console.log("placeholderOnComplete has called!"); - console.log("Main Image is Ready:" + this.loadMainReady); + globalThis.ImageKnife.log("placeholderOnComplete has called!"); + globalThis.ImageKnife.log("Main Image is Ready:" + this.loadMainReady); if (!this.loadMainReady && !(this.loadErrorReady || this.loadRetryReady) && !this.loadThumbnailReady) { // 主图未加载成功,并且未加载失败 显示占位图 主图加载成功或者加载失败后=>不展示占位图 this.placeholderFunc(imageKnifeData) @@ -337,7 +337,7 @@ export class RequestOption { // 占位图解析失败 placeholderOnError(error) { - console.log("占位图解析失败 error =" + error) + globalThis.ImageKnife.log("占位图解析失败 error =" + error) } @@ -353,7 +353,7 @@ export class RequestOption { // 缩略图解析失败 thumbholderOnError(error) { - console.log("缩略图解析失败 error =" + error) + globalThis.ImageKnife.log("缩略图解析失败 error =" + error) } // 加载失败 占位图解析成功 @@ -367,7 +367,7 @@ export class RequestOption { //加载失败 占位图解析失败 errorholderOnError(error) { - console.log("失败占位图解析失败 error =" + error) + globalThis.ImageKnife.log("失败占位图解析失败 error =" + error) } retryholderOnComplete(imageKnifeData: ImageKnifeData){ @@ -378,7 +378,7 @@ export class RequestOption { } retryholderOnError(error){ - console.log("重试占位图解析失败 error ="+ error) + globalThis.ImageKnife.log("重试占位图解析失败 error ="+ error) } loadComplete(imageKnifeData: ImageKnifeData) { @@ -395,8 +395,8 @@ export class RequestOption { } loadError(err) { - console.log("loadError:"+err); - console.log("loadError stack=:"+JSON.stringify(err.stack)); + globalThis.ImageKnife.log("loadError:"+err); + globalThis.ImageKnife.log("loadError stack=:"+JSON.stringify(err.stack)); //失败占位图展示规则 if (this.retryholderFunc) { // 重试图层优先于加载失败展示 diff --git a/imageknife/src/main/ets/components/imageknife/holder/ErrorHolderManager.ets b/imageknife/src/main/ets/components/imageknife/holder/ErrorHolderManager.ets index a829038..139f18d 100644 --- a/imageknife/src/main/ets/components/imageknife/holder/ErrorHolderManager.ets +++ b/imageknife/src/main/ets/components/imageknife/holder/ErrorHolderManager.ets @@ -40,7 +40,7 @@ export class ErrorHolderManager { } private displayErrorholder(onComplete, onError) { - console.log("displayErrorholder") + globalThis.ImageKnife.log("displayErrorholder") if ((typeof (this.options.errorholderSrc as image.PixelMap).isEditable) == 'boolean') { let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, this.options.errorholderSrc as PixelMap) onComplete(imageKnifeData); diff --git a/imageknife/src/main/ets/components/imageknife/holder/PlaceHolderManager.ets b/imageknife/src/main/ets/components/imageknife/holder/PlaceHolderManager.ets index a3c23f7..d5636dc 100644 --- a/imageknife/src/main/ets/components/imageknife/holder/PlaceHolderManager.ets +++ b/imageknife/src/main/ets/components/imageknife/holder/PlaceHolderManager.ets @@ -42,7 +42,7 @@ export class PlaceHolderManager { } private displayPlaceholder(onComplete, onError) { - console.log("displayPlaceholder") + globalThis.ImageKnife.log("displayPlaceholder") if ((typeof (this.options.placeholderSrc as image.PixelMap).isEditable) == 'boolean') { let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, this.options.placeholderSrc as PixelMap) onComplete(imageKnifeData); diff --git a/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager.ets b/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager.ets index ff186a0..9271f75 100644 --- a/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager.ets +++ b/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager.ets @@ -42,7 +42,7 @@ export class RetryHolderManager { } private displayRetryholder(onComplete, onError) { - console.log("displayRetryholder") + globalThis.ImageKnife.log("displayRetryholder") if ((typeof (this.options.retryholderSrc as image.PixelMap).isEditable) == 'boolean') { let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, this.options.placeholderSrc as PixelMap) onComplete(imageKnifeData); diff --git a/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets b/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets index 43a9ec3..627b3ad 100644 --- a/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets +++ b/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets @@ -88,7 +88,7 @@ export class RequestManager { } static execute(option: RequestOption, memoryCache1: LruCache, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) { - console.log("RequestManager execute") + globalThis.ImageKnife.log("RequestManager execute") let manager = new RequestManager(option, memoryCache1, diskMemoryCache1, dataFetch, resourceFetch); return new Promise(manager.process.bind(manager)) .then(option.loadComplete.bind(option)) @@ -98,9 +98,9 @@ export class RequestManager { loadCompleteAfter() { try { // 内部消化问题 - console.log("loadCompleteAfter!") + globalThis.ImageKnife.log("loadCompleteAfter!") if (this.options.allCacheInfoCallback) { - console.log("RequestOption =" + JSON.stringify(this.options)); + globalThis.ImageKnife.log("RequestOption =" + JSON.stringify(this.options)); // 内存缓存 let allCacheInfo = new AllCacheInfo(); @@ -124,7 +124,7 @@ export class RequestManager { this.options.allCacheInfoCallback(allCacheInfo) } } catch (err) { - console.log("after err =" + err) + globalThis.ImageKnife.log("after err =" + err) } } @@ -134,12 +134,12 @@ export class RequestManager { private mRunReason: RunReason = RunReason.INITIALIZE; process(onComplete, onError) { - console.log("RequestManager process !"); + globalThis.ImageKnife.log("RequestManager process !"); this.loadLeve1MemoryCache(onComplete, onError) } private runWrapped(request: RequestOption, runReason: RunReason, onComplete, onError) { - console.log("RequestManager runWrapped") + globalThis.ImageKnife.log("RequestManager runWrapped") if (runReason == RunReason.INITIALIZE) { this.mStage = this.getNextStage(request, this.mStage); this.searchLoadFrom(this.options, this.mStage, onComplete, onError); @@ -170,7 +170,7 @@ export class RequestManager { //究竟从哪里加载数据 private searchLoadFrom(request: RequestOption, current: Stage, onComplete, onError) { - console.log("RequestManager searchLoadFrom") + globalThis.ImageKnife.log("RequestManager searchLoadFrom") if (current == Stage.RESOURCE_CACHE) { this.loadDiskFromTransform(request, onComplete, onError); } else if (current == Stage.DATA_CACHE) { @@ -194,13 +194,13 @@ export class RequestManager { // 加载本地资源 private loadSourceFormNative(request: RequestOption, onComplete, onError) { - console.log("RequestManager loadSourceFormNative") + globalThis.ImageKnife.log("RequestManager loadSourceFormNative") // 本地解析后进行一级缓存 let success = (arrayBuffer) => { // 使用媒体子系统 ImageSource解析文件 获取PixelMap let fileTypeUtil = new FileTypeUtil(); let typeValue = fileTypeUtil.getFileType(arrayBuffer) - console.log("RequestManager - 文件类型为= " + typeValue) + globalThis.ImageKnife.log("RequestManager - 文件类型为= " + typeValue) // gif处理 if(ImageKnifeData.GIF == typeValue && !request.dontAnimateFlag){ // 处理gif @@ -240,7 +240,7 @@ export class RequestManager { } // 加载磁盘缓存 原图 private loadDiskFromSource(request: RequestOption, onComplete, onError) { - console.log("RequestManager loadDiskFromSource") + globalThis.ImageKnife.log("RequestManager loadDiskFromSource") let cached = this.mDiskCacheProxy.getValue(request.generateDataKey) if (cached != null) { this.parseDiskFile2PixelMap(request, cached, onComplete, onError) @@ -252,7 +252,7 @@ export class RequestManager { // 加载磁盘缓存 变换后图片 private loadDiskFromTransform(request: RequestOption, onComplete, onError) { - console.log("RequestManager loadDiskFromTransform") + globalThis.ImageKnife.log("RequestManager loadDiskFromTransform") let cached = this.mDiskCacheProxy.getValue(request.generateResourceKey) if (cached != null) { this.parseDiskTransformFile2PixelMap(request, cached, onComplete, onError) @@ -263,7 +263,7 @@ export class RequestManager { } parseSource(request: RequestOption, onComplete, onError) { - console.log("RequestManager parseSource") + globalThis.ImageKnife.log("RequestManager parseSource") if ((typeof (request.loadSrc as image.PixelMap).isEditable) == 'boolean') { // PixelMap 外层捕获效率更高,不会进入这里 } else if (typeof request.loadSrc == 'string') { @@ -273,14 +273,14 @@ export class RequestManager { if (typeof res.id != 'undefined' && typeof res.id != 'undefined') { this.loadSourceFormNative(request, onComplete, onError) } else { - console.log("输入参数有问题!") + globalThis.ImageKnife.log("输入参数有问题!") } } } private loadLeve1MemoryCache(onComplete, onError) { - console.log("RequestManager loadLeve1MemoryCache") + globalThis.ImageKnife.log("RequestManager loadLeve1MemoryCache") // 一级缓存 内存获取 let cache = this.mMemoryCacheProxy.loadMemoryCache(this.options.generateCacheKey, this.options.isCacheable); if (cache == null || typeof cache == 'undefined') { @@ -414,7 +414,7 @@ export class RequestManager { } private downloadSuccess(source: ArrayBuffer, onComplete, onError) { - console.info('Download task completed.'); + globalThis.ImageKnife.info('Download task completed.'); if(source == null || source == undefined || source.byteLength <= 0){ onError('Download task completed. but download file is empty!') @@ -445,7 +445,7 @@ export class RequestManager { await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer) }) .catch(err=>{ - console.log('download file is ='+ImageKnifeData.GIF+'and save diskLruCache error ='+ err) + globalThis.ImageKnife.log('download file is ='+ImageKnifeData.GIF+'and save diskLruCache error ='+ err) }) }else if(ImageKnifeData.SVG == filetype){ // 处理svg @@ -459,7 +459,7 @@ export class RequestManager { await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer) }) .catch(err=>{ - console.log('download file is ='+ImageKnifeData.SVG+'and save diskLruCache error ='+ err) + globalThis.ImageKnife.log('download file is ='+ImageKnifeData.SVG+'and save diskLruCache error ='+ err) }) } else { // 进行变换 @@ -572,12 +572,12 @@ export class RequestManager { if(err){ onError(err) } - console.log("gifProcess data is null:"+(data == null)); + globalThis.ImageKnife.log("gifProcess data is null:"+(data == null)); if(!!data){ let imageKnifeData = this.createImageGIFFrame(ImageKnifeType.GIFFRAME,data) - console.log('gifProcess 生成gif 返回数据类型') + globalThis.ImageKnife.log('gifProcess 生成gif 返回数据类型') if(cacheStrategy){ - console.log('gifProcess 生成gif并且存入了缓存策略') + globalThis.ImageKnife.log('gifProcess 生成gif并且存入了缓存策略') cacheStrategy(imageKnifeData) } onComplete(imageKnifeData) diff --git a/imageknife/src/main/ets/components/imageknife/transform/BlurTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/BlurTransformation.ets index 5a84076..b445208 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/BlurTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/BlurTransformation.ets @@ -35,7 +35,7 @@ export class BlurTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";BlurTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";BlurTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";BlurTransformation buf is empty", null); } @@ -73,7 +73,7 @@ export class BlurTransformation implements BaseTransform { fastBlur.blur(data, this._mRadius, true, func); }) .catch((e) => { - console.log(Constants.PROJECT_TAG + ";error:" + e); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";error:" + e); func(e, null); }) }) diff --git a/imageknife/src/main/ets/components/imageknife/transform/BrightnessFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/BrightnessFilterTransformation.ets index 6afc66b..fe245c8 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/BrightnessFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/BrightnessFilterTransformation.ets @@ -36,7 +36,7 @@ export class BrightnessFilterTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/ContrastFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/ContrastFilterTransformation.ets index 33956ce..9ecbf2d 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/ContrastFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/ContrastFilterTransformation.ets @@ -48,7 +48,7 @@ export class ContrastFilterTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";ContrastFilterTransformation buf is empty", null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/CropCircleTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/CropCircleTransformation.ets index 6d64065..c883282 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/CropCircleTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/CropCircleTransformation.ets @@ -34,7 +34,7 @@ export class CropCircleTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.error(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty"); if (func) { func(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " buf is empty", null); } @@ -77,7 +77,7 @@ export class CropCircleTransformation implements BaseTransform { this.transformCircle(p, func); }) .catch(e => { - console.error(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " transform e:" + e); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + CropCircleTransformation.TAG + " transform e:" + e); if (func) { func(Constants.PROJECT_TAG + CropCircleTransformation.TAG + "e" + e, null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/CropCircleWithBorderTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/CropCircleWithBorderTransformation.ets index 86495f1..5ae07ae 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/CropCircleWithBorderTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/CropCircleWithBorderTransformation.ets @@ -53,7 +53,7 @@ export class CropCircleWithBorderTransformation implements BaseTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation buf is empty", null); } @@ -96,7 +96,7 @@ export class CropCircleWithBorderTransformation implements BaseTransform { - console.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e); if (func) { func(Constants.PROJECT_TAG + ";CropCircleWithBorderTransformation e:" + e, null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/CropSquareTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/CropSquareTransformation.ets index 63b7b21..428fad4 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/CropSquareTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/CropSquareTransformation.ets @@ -29,7 +29,7 @@ export class CropSquareTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";CropSquareTransformation buf is empty", null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/CropTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/CropTransformation.ets index f06cc09..a088c98 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/CropTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/CropTransformation.ets @@ -40,7 +40,7 @@ export class CropTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";CropTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";CropTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";CropTransformation buf is empty", null); } @@ -85,7 +85,7 @@ export class CropTransformation implements BaseTransform { func("", data); }) .catch((e) => { - console.log(Constants.PROJECT_TAG + ";error:" + e); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";error:" + e); func(e, null); }) }) diff --git a/imageknife/src/main/ets/components/imageknife/transform/GrayscaleTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/GrayscaleTransformation.ets index d146073..5c52270 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/GrayscaleTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/GrayscaleTransformation.ets @@ -27,7 +27,7 @@ export class GrayscaleTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";GrayscaleTransformation buf is empty", null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/InvertFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/InvertFilterTransformation.ets index ccaeaca..35d1e9b 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/InvertFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/InvertFilterTransformation.ets @@ -34,7 +34,7 @@ export class InvertFilterTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";InvertFilterTransformation buf is empty", null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets index 27130e6..c7fcdcb 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets @@ -34,7 +34,7 @@ export class MaskTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";MaskTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";MaskTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";MaskTransformation buf is empty", null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/PixelationFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/PixelationFilterTransformation.ets index 6ea204f..b878c79 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/PixelationFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/PixelationFilterTransformation.ets @@ -40,7 +40,7 @@ export class PixelationFilterTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";PixelationFilterTransformation buf is empty", null); } @@ -78,7 +78,7 @@ export class PixelationFilterTransformation implements BaseTransform { pixelUtils.pixel(data, this._mPixel, func); }) .catch((e) => { - console.log(Constants.PROJECT_TAG + ";error:" + e); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";error:" + e); func(e, null); }) }) diff --git a/imageknife/src/main/ets/components/imageknife/transform/RotateImageTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/RotateImageTransformation.ets index 3034c55..8266c94 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/RotateImageTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/RotateImageTransformation.ets @@ -33,7 +33,7 @@ export class RotateImageTransformation implements BaseTransform { transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";RotateImageTransformation buf is empty", null); } @@ -72,7 +72,7 @@ export class RotateImageTransformation implements BaseTransform { func("", data); }) .catch((e) => { - console.log(Constants.PROJECT_TAG + ";error:" + e); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";error:" + e); func(e, null); }) }) diff --git a/imageknife/src/main/ets/components/imageknife/transform/RoundedCornersTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/RoundedCornersTransformation.ets index da5fcb3..6610742 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/RoundedCornersTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/RoundedCornersTransformation.ets @@ -49,10 +49,10 @@ export class RoundedCornersTransformation implements BaseTransform { } transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { - console.log('RoundedCornersTransformation = '+ this.getName() + globalThis.ImageKnife.log('RoundedCornersTransformation = '+ this.getName() + 'buf is null ='+ (buf == null)); if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";RoundedCornersTransformation buf is empty", null); } @@ -114,11 +114,11 @@ export class RoundedCornersTransformation implements BaseTransform { } }) .catch((error) => { - console.log(Constants.PROJECT_TAG + "RoundedCornersTransformation error:" + error); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + "RoundedCornersTransformation error:" + error); }); }) .catch((e) => { - console.log(Constants.PROJECT_TAG + ";error:" + e); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";error:" + e); if (func) { func(e, null); } diff --git a/imageknife/src/main/ets/components/imageknife/transform/SepiaFilterTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/SepiaFilterTransformation.ets index bca60c4..2199918 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/SepiaFilterTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/SepiaFilterTransformation.ets @@ -31,7 +31,7 @@ export class SepiaFilterTransformation implements BaseTransform { async transform(buf: ArrayBuffer, request: RequestOption, func?: AsyncTransform) { if (!buf || buf.byteLength <= 0) { - console.log(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty"); + globalThis.ImageKnife.log(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty"); if (func) { func(Constants.PROJECT_TAG + ";SepiaFilterTransformation buf is empty", null); } diff --git a/imageknife/src/main/ets/components/imageknife/utils/FileTypeUtil.ets b/imageknife/src/main/ets/components/imageknife/utils/FileTypeUtil.ets index cb9a367..71289ca 100644 --- a/imageknife/src/main/ets/components/imageknife/utils/FileTypeUtil.ets +++ b/imageknife/src/main/ets/components/imageknife/utils/FileTypeUtil.ets @@ -44,7 +44,7 @@ export class FileTypeUtil { } let dataView = new DataView(arraybuffer); - console.log('dataView +'+this.getDataViewAt(dataView,0)+this.getDataViewAt(dataView,1)) + globalThis.ImageKnife.log('dataView +'+this.getDataViewAt(dataView,0)+this.getDataViewAt(dataView,1)) for(var [key,value] of this.map){ let keySplit = key.split(',') @@ -58,10 +58,10 @@ export class FileTypeUtil { fileMagic+=this.getDataViewAt(dataView,offset+start) start++; } -// console.log('magic='+fileMagic+' keySplit[1]='+keySplit[1]+' fileMagic == keySplit[1] ='+(fileMagic == keySplit[1])+ +// globalThis.ImageKnife.log('magic='+fileMagic+' keySplit[1]='+keySplit[1]+' fileMagic == keySplit[1] ='+(fileMagic == keySplit[1])+ // ' fileMagic type ='+typeof(fileMagic) + ' keySplit[1]='+typeof(keySplit[1])) if(fileMagic == keySplit[1]){ - console.log('匹配到了 fileType='+value) + globalThis.ImageKnife.log('匹配到了 fileType='+value) fileType = value break; } @@ -105,7 +105,7 @@ export class FileTypeUtil { private printMapContent(){ for(var [key,value] of this.map){ - console.log('key='+key+'---value='+value) + globalThis.ImageKnife.log('key='+key+'---value='+value) } } diff --git a/imageknife/src/main/ets/components/imageknife/utils/gif/GIFParseImpl.ets b/imageknife/src/main/ets/components/imageknife/utils/gif/GIFParseImpl.ets index aa82948..29552dc 100644 --- a/imageknife/src/main/ets/components/imageknife/utils/gif/GIFParseImpl.ets +++ b/imageknife/src/main/ets/components/imageknife/utils/gif/GIFParseImpl.ets @@ -21,14 +21,14 @@ import image from '@ohos.multimedia.image' export class GIFParseImpl implements IParseGif { parseGifs(imageinfo: ArrayBuffer, callback: (data?, err?) => void, worker?,runMainThread?:boolean) { let resolveWorker = worker; - console.log('parseGifs resolveWorker1 is null =' + (resolveWorker == null)) + globalThis.ImageKnife.log('parseGifs resolveWorker1 is null =' + (resolveWorker == null)) if (!resolveWorker) { resolveWorker = globalThis.ImageKnife.getGifWorker(); } - console.log('parseGifs resolveWorker2 is null =' + (resolveWorker == null)) + globalThis.ImageKnife.log('parseGifs resolveWorker2 is null =' + (resolveWorker == null)) if (!!resolveWorker && !runMainThread) { - console.log('parseGifs in worker thread!') + globalThis.ImageKnife.log('parseGifs in worker thread!') this.useWorkerParse(resolveWorker, imageinfo, (data, err) => { if (err) { callback(undefined, err) @@ -48,9 +48,9 @@ export class GIFParseImpl implements IParseGif { } }) } else { - console.log('parseGifs in main thread!') + globalThis.ImageKnife.log('parseGifs in main thread!') let frames = parseBufferToFrame(imageinfo) - console.log('frames length =' + frames.length) + globalThis.ImageKnife.log('frames length =' + frames.length) this.createPixelMapAll(frames).then((pixelmaps) => { if (pixelmaps.length == frames.length) { for (let i = 0;i < frames.length; i++) { @@ -58,11 +58,11 @@ export class GIFParseImpl implements IParseGif { frame['drawPixelMap'] = pixelmaps[i]; frame['patch'] = null; } - console.log('parseGifs in main thread! callback is done!') + globalThis.ImageKnife.log('parseGifs in main thread! callback is done!') callback(frames, undefined) } }).catch(err => { - console.log('parseGifs in main thread! err =' + err) + globalThis.ImageKnife.log('parseGifs in main thread! err =' + err) callback(undefined, err) }) } @@ -97,7 +97,7 @@ export class GIFParseImpl implements IParseGif { } worker.onexit = function () { - console.log('gifWork worker.onexit!') + globalThis.ImageKnife.log('gifWork worker.onexit!') } worker.onmessage = (e) => { diff --git a/imageknife/src/main/ets/components/imageknife/utils/svg/SVGImageViewModel.ets b/imageknife/src/main/ets/components/imageknife/utils/svg/SVGImageViewModel.ets deleted file mode 100644 index c7480da..0000000 --- a/imageknife/src/main/ets/components/imageknife/utils/svg/SVGImageViewModel.ets +++ /dev/null @@ -1,1107 +0,0 @@ -/* - * Copyright (C) 2022 Huawei Device 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 image from '@ohos.multimedia.image' -import util from '@ohos.util' -import display from '@ohos.display'; -import configuration from '@system.configuration'; -import { - SVG, - PathView, - ImageSvg, - CircleView, - EllipseView, - LineView, - PolyLineView, - PolygonView, - RectView, - Group, - Switch, - TextView, - Use, - SVGXMLConstants, - SVGAttrConstants, - SVGManager, - SVGXMLChecker -} from '@ohos/svg'; - -export default class SVGImageViewModel { - private svgString: string = ""; - private rawfileName: string = ""; - private media: Resource; - svgs: SVG[] = [] - svg: SVG = new SVG(); - isPx: boolean = false; - private context: OffscreenCanvasRenderingContext2D - private svgManager: SVGManager = SVGManager.getInstance(); - - deviceW: number = display.getDefaultDisplaySync().width; - deviceH: number = display.getDefaultDisplaySync().height; - - /** - * 设置svg的字符串 - * @param svgString svg字符串 - */ - public setSvgString(svgString: string) { - this.svgString = svgString; - this.parseSvgFromString(); - } - - /** - * 设置rawfile下的svg - * @param svgRawfile rawfile下的svg文件名 - */ - public setSvgRawfile(svgRawfile: string) { - this.rawfileName = svgRawfile; - this.parseSvgFromRawfile(); - } - - /** - * 设置media下的svg - * @param media media下的svg文件 - */ - public setSvgMedia(media: Resource) { - this.media = media; - this.parseSvgFromMedia(); - } - - public setSvgUint8Array(uint8:Uint8Array){ - this.svgString = this.unit8ArrayToString(uint8) - this.parseSvgFromString(); - } - - private parseSvgFromMedia() { - globalThis.resourceManager.getMedia(this.media.id, (error, value) => { - if (error != null) { - console.error("media get error:" + error) - throw new Error("未找到media下的svg") - } else { - this.svgString = this.unit8ArrayToString(value); - this.parseSvgFromString(); - } - }) - } - - private parseSvgFromRawfile() { - globalThis.resourceManager.getRawFile(this.rawfileName, (error, value) => { - if (error != null) { - console.error("rawfile get error:" + error) - throw new Error("未找到rawfile下的svg") - } else { - this.svgString = this.unit8ArrayToString(value); - this.parseSvgFromString(); - } - }) - } - - private unit8ArrayToString(fileData) { -// var dataString = ""; -// for (var i = 0; i < fileData.length; i++) { -// dataString += String.fromCharCode(fileData[i]); -// } -// return dataString - let textDecoder = new util.TextDecoder("utf-8", {ignoreBOM:true}); - let retStr = textDecoder.decode(fileData, {stream: false}); - return retStr; - } - - private parseSvgFromString() { - this.svgs.length = 0; - this.svgManager.parseString(this.svgString, (jsonString) => { - var reg = new RegExp("stroke-", "g") - jsonString = String(jsonString).replace(reg, "stroke_"); - reg = new RegExp("font-", "g") - jsonString = String(jsonString).replace(reg, "font_"); - reg = new RegExp("fill-", "g") - jsonString = String(jsonString).replace(reg, "fill_"); - var svgElements: any[] = JSON.parse(jsonString).elements; - if (!SVGXMLChecker.checkArrayEmpty(svgElements)) { - this.getSvgData(svgElements); - } - }) - } - - private getSvgData(data: any[], g?: SVG) { - for (let i = 0;i < data.length; i++) { - if (data[i].type == SVGAttrConstants.ATTR_VALUE_ELEMENT) { - switch (data[i].name) { - case SVGXMLConstants.SVG_SVG_TAG: - if (!SVGXMLChecker.checkObjectEmpty(data[i].attributes)) { - this.parseAttributesSVG(data[i].attributes); - } - if (!SVGXMLChecker.checkArrayEmpty(data[i].elements)) { - this.getSvgData(data[i].elements); - } - break; - } - this.setSvgArrayChild(data[i], this.svgs, g) - } - } - } - - private setSvgArrayChild(elements: any, svgs: SVG[], g?: SVG) { - switch (elements.name) { - case SVGXMLConstants.SVG_G_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - g = this.parseAttributesGroup(elements.attributes, g); - } - if (!SVGXMLChecker.checkArrayEmpty(elements.elements)) { - this.getSvgData(elements.elements, g); - } - break; - case SVGXMLConstants.SVG_CIRCLE_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesCircle(elements.attributes, g)) - } - break; - case SVGXMLConstants.SVG_ELLIPSE_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesEllipse(elements.attributes, g)) - } - break; - case SVGXMLConstants.SVG_LINE_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesLine(elements.attributes, g)) - } - break; - case SVGXMLConstants.SVG_POLYLINE_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesPolyLine(elements.attributes, g)) - } - break; - case SVGXMLConstants.SVG_POLYGON_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesPolygon(elements.attributes, g)); - } - break; - case SVGXMLConstants.SVG_PATH_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesPath(elements.attributes, g)); - } - break; - case SVGXMLConstants.SVG_RECT_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesRect(elements.attributes, g)); - } - break; - case SVGXMLConstants.SVG_IMAGE_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesImage(elements.attributes, g)); - } - break; - case SVGXMLConstants.SVG_STYLE_TAG: - if (!SVGXMLChecker.checkArrayEmpty(elements.elements)) { - } - break; - case SVGXMLConstants.SVG_USE_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesUse(elements.attributes, g)); - } - break; - case SVGXMLConstants.SVG_TEXT_TAG: - if (!SVGXMLChecker.checkObjectEmpty(elements.attributes)) { - svgs.push(this.parseAttributesText(elements.attributes, g, elements.elements[0].text)); - } - break; - case SVGXMLConstants.SVG_SWITCH_TAG: - if (!SVGXMLChecker.checkArrayEmpty(elements.elements)) { - svgs.push(this.parseAttributesSwitch(elements.attributes, elements.elements)); - } - break; - } - } - - private parseAttributesSVG(svg: any) { - if (!SVGXMLChecker.checkObjectEmpty(svg.id)) { - this.svg.setId(svg.id); - } - if (!SVGXMLChecker.checkObjectEmpty(svg.width)) { - this.svg.setWidth(svg.width); - } - if (!SVGXMLChecker.checkObjectEmpty(svg.height)) { - this.svg.setHeight(svg.height); - } - if (!SVGXMLChecker.checkObjectEmpty(svg.viewBox)) { - this.svg.setViewBox(svg.viewBox); - } - if (!SVGXMLChecker.checkObjectEmpty(svg.fill)) { - this.svg.setFill(svg.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(svg.stroke)) { - this.svg.setStroke(svg.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(svg.stroke_width)) { - this.svg.setStrokeWidth(svg.stroke_width); - } - if (this.svg.width == '100%') { - this.svg.setWidth(this.svg.viewBoxWidth) - } - if (this.svg.height == '100%') { - this.svg.setHeight(this.svg.viewBoxHeight) - } - } - - private parseAttributesGroup(g: any, lastGroup?: SVG): SVG { - var newGroup: SVG = SVGXMLChecker.checkObjectEmpty(lastGroup) ? new SVG() : lastGroup - if (!SVGXMLChecker.checkObjectEmpty(g.id)) { - newGroup.setId(g.id); - } - if (!SVGXMLChecker.checkObjectEmpty(g.class)) { - newGroup.setClass(g.class); - } - if (!SVGXMLChecker.checkObjectEmpty(g.fill)) { - newGroup.setFill(g.fill); - } else { - newGroup.setFill(newGroup.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(g.fill_opacity)) { - newGroup.setFillOpacity(g.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(g.stroke)) { - newGroup.setStroke(g.stroke); - } else { - newGroup.setStroke(newGroup.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(g.stroke_width)) { - newGroup.setStrokeWidth(g.stroke_width); - } else { - newGroup.setStrokeWidth(newGroup.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(g.stroke_miterlimit)) { - newGroup.setStrokeMiterLimit(g.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(g.style)) { - newGroup.setStyle(g.style); - } - if (!SVGXMLChecker.checkObjectEmpty(g.transform)) { - newGroup.setTransform(g.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, true); - } - if (!SVGXMLChecker.checkObjectEmpty(g.systemLanguage)) { - newGroup.setSystemLanguage(g.systemLanguage); - } - return newGroup; - } - - private parseAttributesCircle(circle: any, g: SVG): CircleView { - var newCircle: CircleView = new CircleView(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.id)) { - newCircle.setId(circle.id); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.class)) { - newCircle.setClass(circle.class); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.r)) { - newCircle.setWidth(circle.r); - newCircle.setHeight(circle.r); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.cx)) { - newCircle.setX(circle.cx); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.cy)) { - newCircle.setY(circle.cy); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.transform)) { - newCircle.copyTransform(g); - newCircle.setTransform(circle.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.fill)) { - newCircle.setFill(circle.fill); - } else { - newCircle.setFill(g.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.fill_opacity)) { - newCircle.setFillOpacity(circle.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.stroke)) { - newCircle.setStroke(circle.stroke); - } else { - newCircle.setStroke(g.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.stroke_width)) { - newCircle.setStrokeWidth(circle.stroke_width); - } else { - newCircle.setStrokeWidth(g.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.stroke_miterlimit)) { - newCircle.setStrokeMiterLimit(circle.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(circle.style)) { - newCircle.setStyle(circle.style); - } - return newCircle; - } - - private parseAttributesEllipse(ellipse: any, g: SVG): EllipseView { - var newEllipse: EllipseView = new EllipseView(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.id)) { - newEllipse.setId(ellipse.id); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.class)) { - newEllipse.setClass(ellipse.class); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.cx)) { - newEllipse.setX(ellipse.cx) - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.cy)) { - newEllipse.setY(ellipse.cy) - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.rx)) { - newEllipse.setWidth(Number(ellipse.rx) * 2); - // newEllipse.setX(-Number(ellipse.rx)) - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.ry)) { - newEllipse.setHeight(Number(ellipse.ry) * 2); - // newEllipse.setY(-Number(ellipse.ry)) - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.x)) { - newEllipse.setX(ellipse.x); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.y)) { - newEllipse.setY(ellipse.y); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.transform)) { - newEllipse.copyTransform(g); - newEllipse.setTransform(ellipse.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.fill)) { - newEllipse.setFill(ellipse.fill); - } else { - newEllipse.setFill(g.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.fill_opacity)) { - newEllipse.setFillOpacity(ellipse.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.stroke)) { - newEllipse.setStroke(ellipse.stroke); - } else { - newEllipse.setStroke(g.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.stroke_width)) { - newEllipse.setStrokeWidth(ellipse.stroke_width); - } else { - newEllipse.setStrokeWidth(g.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.stroke_miterlimit)) { - newEllipse.setStrokeMiterLimit(ellipse.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.style)) { - newEllipse.setStyle(ellipse.style); - } - - if (newEllipse.cx > 0 && newEllipse.cy > 0) { - newEllipse.setX(Number(newEllipse.x) + Number(newEllipse.cx)) - newEllipse.setY(Number(newEllipse.y) + Number(newEllipse.cy)) - } - if (!SVGXMLChecker.checkObjectEmpty(ellipse.systemLanguage)) { - newEllipse.setSystemLanguage(ellipse.systemLanguage); - } - return newEllipse; - } - - private parseAttributesLine(line: any, g: SVG): LineView { - var newLine: LineView = new LineView(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(line.id)) { - newLine.setId(line.id); - } - if (!SVGXMLChecker.checkObjectEmpty(line.class)) { - newLine.setClass(line.class); - } - if (!SVGXMLChecker.checkObjectEmpty(line.x1)) { - newLine.setStartX(line.x1); - } - if (!SVGXMLChecker.checkObjectEmpty(line.y1)) { - newLine.setStartY(line.y1); - } - if (!SVGXMLChecker.checkObjectEmpty(line.x2)) { - newLine.setEndX(line.x2); - } - if (!SVGXMLChecker.checkObjectEmpty(line.y2)) { - newLine.setEndY(line.y2); - } - if (!SVGXMLChecker.checkObjectEmpty(line.x)) { - newLine.setX(line.x); - } - if (!SVGXMLChecker.checkObjectEmpty(line.y)) { - newLine.setY(line.y); - } - if (!SVGXMLChecker.checkObjectEmpty(line.transform)) { - newLine.copyTransform(g); - newLine.setTransform(line.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(line.fill)) { - newLine.setFill(line.fill); - } else { - newLine.setFill(g.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(line.fill_opacity)) { - newLine.setFillOpacity(line.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(line.stroke)) { - newLine.setStroke(line.stroke); - } else { - newLine.setStroke(g.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(line.stroke_width)) { - newLine.setStrokeWidth(line.stroke_width); - } else { - newLine.setStrokeWidth(g.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(line.stroke_miterlimit)) { - newLine.setStrokeMiterLimit(line.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(line.style)) { - newLine.setStyle(line.style); - } - if (!SVGXMLChecker.checkObjectEmpty(line.systemLanguage)) { - newLine.setSystemLanguage(line.systemLanguage); - } - - return newLine; - } - - private parseAttributesPolyLine(polyLine: any, g: SVG): PolyLineView { - var newPolyLine = new PolyLineView(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.id)) { - newPolyLine.setId(polyLine.id); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.class)) { - newPolyLine.setClass(polyLine.class); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.points)) { - let pointsArray: string[] = String(polyLine.points).split(" "); - let pointsResult: Array<[number, number]> = new Array<[number, number]>(); - for (let i = 0;i < pointsArray.length; i++) { - let pointsItem = pointsArray[i].split(",") - pointsResult.push([Number(pointsItem[0]), Number(pointsItem[1])]) - } - newPolyLine.setPointsResult(pointsResult); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.x)) { - newPolyLine.setX(polyLine.x); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.y)) { - newPolyLine.setY(polyLine.y); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.transform)) { - newPolyLine.copyTransform(g); - newPolyLine.setTransform(polyLine.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.fill)) { - newPolyLine.setFill(polyLine.fill); - } else { - newPolyLine.setFill(g.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.fill_opacity)) { - newPolyLine.setFillOpacity(polyLine.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.stroke)) { - newPolyLine.setStroke(polyLine.stroke); - } else { - newPolyLine.setStroke(g.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.stroke_width)) { - newPolyLine.setStrokeWidth(polyLine.stroke_width); - } else { - newPolyLine.setStrokeWidth(g.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.stroke_miterlimit)) { - newPolyLine.setStrokeMiterLimit(polyLine.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.style)) { - newPolyLine.setStyle(polyLine.style); - } - if (!SVGXMLChecker.checkObjectEmpty(polyLine.systemLanguage)) { - newPolyLine.setSystemLanguage(polyLine.systemLanguage); - } - - return newPolyLine; - } - - private parseAttributesPolygon(polygon: any, g: SVG): PolygonView { - var newPolygon = new PolygonView(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.id)) { - newPolygon.setId(polygon.id); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.class)) { - newPolygon.setClass(polygon.class); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.points)) { - let pointsArray: string[] = String(polygon.points).split(" "); - let pointsResult: Array<[number, number]> = new Array<[number, number]>(); - for (let i = 0;i < pointsArray.length; i++) { - let pointsItem = pointsArray[i].split(",") - pointsResult.push([Number(pointsItem[0]), Number(pointsItem[1])]) - } - newPolygon.setPointsResult(pointsResult); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.x)) { - newPolygon.setX(polygon.x); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.y)) { - newPolygon.setY(polygon.y); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.transform)) { - newPolygon.copyTransform(g); - newPolygon.setTransform(polygon.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.fill)) { - newPolygon.setFill(polygon.fill); - } else { - newPolygon.setFill(g.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.fill_opacity)) { - newPolygon.setFillOpacity(polygon.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.stroke)) { - newPolygon.setStroke(polygon.stroke); - } else { - newPolygon.setStroke(g.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.stroke_width)) { - newPolygon.setStrokeWidth(polygon.stroke_width); - } else { - newPolygon.setStrokeWidth(g.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.stroke_miterlimit)) { - newPolygon.setStrokeMiterLimit(polygon.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.style)) { - newPolygon.setStyle(polygon.style); - } - if (!SVGXMLChecker.checkObjectEmpty(polygon.systemLanguage)) { - newPolygon.setSystemLanguage(polygon.systemLanguage); - } - - return newPolygon; - } - - private parseAttributesPath(path: any, g: SVG): PathView { - var newPath = new PathView(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(path.id)) { - newPath.setId(path.id); - } - if (!SVGXMLChecker.checkObjectEmpty(path.class)) { - newPath.setClass(path.class); - } - if (!SVGXMLChecker.checkObjectEmpty(path.d)) { - newPath.setCommands(path.d); - } - if (!SVGXMLChecker.checkObjectEmpty(path.x)) { - newPath.setX(path.x); - } - if (!SVGXMLChecker.checkObjectEmpty(path.y)) { - newPath.setY(path.y); - } - if (!SVGXMLChecker.checkObjectEmpty(path.transform)) { - newPath.copyTransform(g); - newPath.setTransform(path.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(path.fill)) { - newPath.setFill(path.fill); - } else { - newPath.setFill(g.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(path.fill_opacity)) { - newPath.setFillOpacity(path.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(path.stroke)) { - newPath.setStroke(path.stroke); - } else { - newPath.setStroke(g.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(path.stroke_width)) { - newPath.setStrokeWidth(path.stroke_width); - } else { - newPath.setStrokeWidth(g.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(path.stroke_miterlimit)) { - newPath.setStrokeMiterLimit(path.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(path.style)) { - newPath.setStyle(path.style); - } - if (!SVGXMLChecker.checkObjectEmpty(path.systemLanguage)) { - newPath.setSystemLanguage(path.systemLanguage); - } - - return newPath; - } - - private parseAttributesRect(rect: any, g: SVG): RectView { - var newRect = new RectView(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.id)) { - newRect.setId(rect.id); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.class)) { - newRect.setClass(g._class + " " + rect.class); - } else { - newRect.setClass(g._class); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.width)) { - newRect.setWidth(rect.width); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.height)) { - newRect.setHeight(rect.height); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.rx)) { - newRect.setRx(rect.rx); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.ry)) { - newRect.setRy(rect.ry); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.x)) { - newRect.setX(rect.x); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.y)) { - newRect.setY(rect.y); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.transform)) { - newRect.copyTransform(g); - newRect.setTransform(rect.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.fill)) { - newRect.setFill(!SVGXMLChecker.checkObjectEmpty(rect.fill) ? rect.fill : g.fill); - } else { - newRect.setFill(g.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.fill_opacity)) { - newRect.setFillOpacity(rect.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.stroke)) { - newRect.setStroke(rect.stroke); - } else { - newRect.setStroke(g.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.stroke_width)) { - newRect.setStrokeWidth(rect.stroke_width); - } else { - newRect.setStrokeWidth(g.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.stroke_miterlimit)) { - newRect.setStrokeMiterLimit(rect.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.style)) { - newRect.setStyle(rect.style); - } - if (!SVGXMLChecker.checkObjectEmpty(rect.systemLanguage)) { - newRect.setSystemLanguage(rect.systemLanguage); - } - - return newRect; - } - - private parseAttributesImage(image: any, g: SVG): ImageSvg { - var newImage = new ImageSvg(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(image.id)) { - newImage.setId(image.id); - } - if (!SVGXMLChecker.checkObjectEmpty(image.class)) { - newImage.setClass(image.class); - } - if (!SVGXMLChecker.checkObjectEmpty(image.width)) { - newImage.setWidth(image.width); - } - if (!SVGXMLChecker.checkObjectEmpty(image.height)) { - newImage.setHeight(image.height); - } - if (!SVGXMLChecker.checkObjectEmpty(image.opacity)) { - newImage.setOpacity(Number(image.opacity)); - } - if (!SVGXMLChecker.checkObjectEmpty(image.href)) { - newImage.setHref(image.href); - } - if (!SVGXMLChecker.checkObjectEmpty(image.transform)) { - newImage.copyTransform(g); - newImage.setTransform(image.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(image.systemLanguage)) { - newImage.setSystemLanguage(image.systemLanguage); - } - - return newImage; - } - - private parseAttributesUse(use: any, g: SVG): Use { - var newUse = new Use(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(use.id)) { - newUse.setId(use.id); - } - if (!SVGXMLChecker.checkObjectEmpty(use.x)) { - newUse.setX(use.x); - } - if (!SVGXMLChecker.checkObjectEmpty(use.y)) { - newUse.setY(use.y); - } - if (!SVGXMLChecker.checkObjectEmpty(use.width)) { - newUse.setWidth(use.width); - } - if (!SVGXMLChecker.checkObjectEmpty(use.height)) { - newUse.setHeight(use.height); - } - if (!SVGXMLChecker.checkObjectEmpty(use.href)) { - newUse.setHref(use.href); - } - if (!SVGXMLChecker.checkObjectEmpty(use.transform)) { - newUse.copyTransform(g); - newUse.setTransform(use.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(use.systemLanguage)) { - newUse.setSystemLanguage(use.systemLanguage); - } - - return newUse; - } - - private parseAttributesText(text: any, g: SVG, str: string): TextView { - var newText = new TextView(); - if (SVGXMLChecker.checkObjectEmpty(g)) { - g = new Group(); - } - if (!SVGXMLChecker.checkObjectEmpty(text.id)) { - newText.setId(text.id); - } - if (!SVGXMLChecker.checkObjectEmpty(text.x)) { - newText.setX(text.x); - } - if (!SVGXMLChecker.checkObjectEmpty(text.y)) { - newText.setY(text.y); - } - if (!SVGXMLChecker.checkObjectEmpty(text.dx)) { - newText.setDx(text.dx); - } - if (!SVGXMLChecker.checkObjectEmpty(text.dy)) { - newText.setDy(text.dy); - } - if (!SVGXMLChecker.checkObjectEmpty(str)) { - newText.setText(str); - } - if (!SVGXMLChecker.checkObjectEmpty(text.font_size)) { - newText.setFontSize(text.font_size); - } - if (!SVGXMLChecker.checkObjectEmpty(text.transform)) { - newText.copyTransform(g); - newText.setTransform(text.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, false); - } - if (!SVGXMLChecker.checkObjectEmpty(text.fill)) { - newText.setFill(!SVGXMLChecker.checkObjectEmpty(text.fill) ? text.fill : g.fill); - } else { - newText.setFill(g.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(text.fill_opacity)) { - newText.setFillOpacity(text.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(text.stroke)) { - newText.setStroke(text.stroke); - } else { - newText.setStroke(g.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(text.stroke_width)) { - newText.setStrokeWidth(text.stroke_width); - } else { - newText.setStrokeWidth(g.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(text.stroke_miterlimit)) { - newText.setStrokeMiterLimit(text.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(text.style)) { - newText.setStyle(text.style); - } - if (!SVGXMLChecker.checkObjectEmpty(text.systemLanguage)) { - newText.setSystemLanguage(text.systemLanguage); - } - - return newText; - } - - private parseAttributesSwitch(g: any, elements: any): Switch { - var newSwitch: Switch = new Switch(); - if (g != undefined) { - if (!SVGXMLChecker.checkObjectEmpty(g.id)) { - newSwitch.setId(g.id); - } - if (!SVGXMLChecker.checkObjectEmpty(g.class)) { - newSwitch.setClass(g.class); - } - if (!SVGXMLChecker.checkObjectEmpty(g.fill)) { - newSwitch.setFill(g.fill); - } else { - newSwitch.setFill(newSwitch.fill); - } - if (!SVGXMLChecker.checkObjectEmpty(g.fill_opacity)) { - newSwitch.setFillOpacity(g.fill_opacity); - } - if (!SVGXMLChecker.checkObjectEmpty(g.stroke)) { - newSwitch.setStroke(g.stroke); - } else { - newSwitch.setStroke(newSwitch.stroke); - } - if (!SVGXMLChecker.checkObjectEmpty(g.stroke_width)) { - newSwitch.setStrokeWidth(g.stroke_width); - } else { - newSwitch.setStrokeWidth(newSwitch.strokeWidth); - } - if (!SVGXMLChecker.checkObjectEmpty(g.stroke_miterlimit)) { - newSwitch.setStrokeMiterLimit(g.stroke_miterlimit); - } - if (!SVGXMLChecker.checkObjectEmpty(g.style)) { - newSwitch.setStyle(g.style); - } - if (!SVGXMLChecker.checkObjectEmpty(g.transform)) { - newSwitch.setTransform(g.transform, this.svg.viewBoxWidth, this.svg.viewBoxHeight, true); - } - if (!SVGXMLChecker.checkObjectEmpty(g.systemLanguage)) { - newSwitch.setSystemLanguage(g.systemLanguage); - } - } - for (let i = 0;i < elements.length; i++) { - this.setSvgArrayChild(elements[i], newSwitch.child, newSwitch); - } - return newSwitch; - } - - private addSvgToArray(svg: SVG) { - this.svgs.push(svg) - } - - public copy(val: SVG): SVG{ - let copySvg = null; - if (val instanceof ImageSvg) { - copySvg = new ImageSvg(); - copySvg.href = val.href; - } else if (val instanceof CircleView) { - copySvg = new CircleView(); - } else if (val instanceof EllipseView) { - copySvg = new EllipseView(); - } else if (val instanceof LineView) { - copySvg = new LineView(); - copySvg.startX = val.startX; - copySvg.startY = val.startY; - copySvg.endX = val.endX; - copySvg.endY = val.endY; - } else if (val instanceof PolyLineView) { - copySvg = new PolyLineView(); - copySvg.pointsResult = val.pointsResult; - } else if (val instanceof PolygonView) { - copySvg = new PolygonView(); - copySvg.pointsResult = val.pointsResult; - } else if (val instanceof RectView) { - copySvg = new RectView(); - copySvg.rx = val.rx; - copySvg.ry = val.ry; - } else if (val instanceof Use) { - copySvg = new Use(); - copySvg.href = val.href; - } - copySvg.id = val.id; - copySvg._class = val._class; - copySvg.x = val.x; - copySvg.y = val.y; - copySvg.width = val.width; - copySvg.height = val.height; - copySvg.fill = val.fill; - copySvg.opacity = val.opacity; - copySvg.fillOpacity = val.fillOpacity; - copySvg.stroke = val.stroke; - copySvg.strokeDashArray = val.strokeDashArray; - copySvg.strokeDashOffset = val.strokeDashOffset; - copySvg.strokeLineCap = val.strokeLineCap; - copySvg.strokeLineJoin = val.strokeLineJoin; - copySvg.strokeMiterLimit = val.strokeMiterLimit; - copySvg.strokeOpacity = val.strokeOpacity; - copySvg.strokeWidth = val.strokeWidth; - copySvg.translateX = val.translateX; - copySvg.translateY = val.translateY; - copySvg.rotateX = val.rotateX; - copySvg.rotateY = val.rotateY; - copySvg.rotateZ = val.rotateZ; - copySvg.rotateAngle = val.rotateAngle; - copySvg.scaleX = val.scaleX; - copySvg.scaleY = val.scaleY; - return copySvg; - } - - private drawSvg(val: any) { - if (val instanceof ImageSvg) { - let img: ImageBitmap = new ImageBitmap("../../../resources/rawfile/" + val.href); - this.context.drawImage(img, px2vp(val.x), px2vp(val.y), px2vp(Number(val.width)), px2vp(Number(val.height))) - } else if (val instanceof CircleView) { - this.context.beginPath() - this.context.ellipse(px2vp(val.x), px2vp(val.y), px2vp(Number(val.width)), px2vp(Number(val.height)), 0, 0, Math.PI * 2) - } else if (val instanceof EllipseView) { - this.context.beginPath() - this.context.ellipse(px2vp(val.x), px2vp(val.y), px2vp(Number(val.width) / 2), px2vp(Number(val.height) / 2), 0, 0, Math.PI * 2) - } else if (val instanceof LineView) { - this.context.beginPath(); - this.context.moveTo(px2vp(Number(val.startX)), px2vp(Number(val.startY))); - this.context.lineTo(px2vp(Number(val.endX)), px2vp(Number(val.endY))) - } else if (val instanceof PolyLineView) { - this.context.beginPath(); - this.context.moveTo(px2vp(val.pointsResult[0][0]), px2vp(val.pointsResult[0][1])) - val.pointsResult.forEach((val, idx) => { - if (idx > 0) { - this.context.lineTo(px2vp(val[0]), px2vp(val[1])) - } - }) - } else if (val instanceof PolygonView) { - this.context.beginPath(); - this.context.moveTo(px2vp(val.pointsResult[0][0]), px2vp(val.pointsResult[0][1])) - val.pointsResult.forEach((val, idx) => { - if (idx > 0) { - this.context.lineTo(px2vp(val[0]), px2vp(val[1])) - } - }) - } else if (val instanceof RectView) { - this.context.beginPath() - this.context.rect(px2vp(val.x), px2vp(val.y), px2vp(Number(val.width)), px2vp(Number(val.height))) - } else if (val instanceof Use) { - for (let i = 0;i < this.svgs.length; i++) { - if (this.svgs[i].id.trim().indexOf(val.href.replace("#", "").trim()) > -1) { - let svg = this.copy(this.svgs[i]) - if (val.x) { - svg.x = Number(this.svgs[i].x) + Number(val.x) - } - if (val.y) { - svg.y = Number(this.svgs[i].y) + Number(val.y) - } - this.drawSvg(svg) - break - } - } - } else if (val instanceof TextView) { - this.context.font = val.fontSize + " sans-serif"; - } else if (val instanceof Switch) { - let language = configuration.getLocale().language; - for (let i = 0;i < val.child.length; i++) { - if (val.child[i].systemLanguage.trim() == language.trim()) { - this.drawSvg(val.child[i]) - break - } - } - } - - this.context.save(); - if (val.strokeMiterLimit != null) { - this.context.miterLimit = val.strokeMiterLimit; - } - if (val.strokeDashOffset != 0) { - this.context.lineDashOffset = Number(val.strokeDashOffset); - } - if (val.translateX != 0 || val.translateY != 0) { - this.context.translate(px2vp(val.translateX), px2vp(val.translateY)) - } - if (val.scaleX != 1 || val.scaleY != 1) { - this.context.scale(val.scaleX, val.scaleY); - } - if (val.rotateAngle != 0) { - this.context.rotate(val.rotateAngle) - } - if (val.strokeWidth != null && Number(val.strokeWidth.toString().replace("px", "")) > 0) { - this.context.strokeStyle = val.stroke; - this.context.lineWidth = Number(val.strokeWidth.toString().replace("px", "")); - if (val instanceof PathView) { - let path2D: Path2D = new Path2D(val.commands); - this.context.stroke(path2D) - } else if (val instanceof TextView) { - this.context.strokeText(val.text, Number(val.x) + Number(val.dx), Number(val.y) + Number(val.dy)) - } else { - this.context.stroke(); - } - } - if (val.fill != null) { - this.context.fillStyle = val.fill; - if (val instanceof PathView) { - let path2D: Path2D = new Path2D(val.commands); - this.context.fill(path2D); - } else if (val instanceof TextView) { - this.context.fillText(val.text, Number(val.x) + Number(val.dx), Number(val.y) + Number(val.dy)) - } else { - this.context.fill(); - } - } - this.context.restore(); - } - - public getSVGPixelMap(size?:{width:number,height:number}): PixelMap{ - - let setttings: RenderingContextSettings = new RenderingContextSettings(true) - let offContext: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(px2vp(this.svg.viewBoxWidth), px2vp(this.svg.viewBoxHeight),setttings) - - image.createImageSource(null) - this.context = offContext - - // 根据目标大小适配 - let targetCompWidth: number; - let targetCompHeight: number; - if(size){ - targetCompWidth = size.width - targetCompHeight = size.height - }else{ - targetCompWidth = this.deviceW*px2vp(1) - targetCompHeight = this.deviceH*px2vp(1) - } - let minScale = 1; - if(this.svg.viewBoxWidth * px2vp(1) > targetCompWidth || this.svg.viewBoxHeight*px2vp(1)> targetCompHeight){ - let scaleW = targetCompWidth / (this.svg.viewBoxWidth * px2vp(1) *1.0) - let scaleH = targetCompHeight / (this.svg.viewBoxHeight * px2vp(1) *1.0) - minScale = scaleW >= scaleH ? scaleH:scaleW; - this.context.setTransform(minScale,0,0,minScale,0,0) - } - // 缩放之后再绘制 - this.svgs.forEach((val, idx, array) => { - this.drawSvg(val) - }) - let minShowWidth = this.svg.viewBoxWidth * px2vp(1) *minScale; - let minShowHeight = this.svg.viewBoxHeight * px2vp(1) *minScale; - console.log('getPixelMap width ='+minShowWidth+'height='+minShowHeight) - let pix = this.context.getPixelMap(0,0,minShowWidth,minShowHeight) - this.context.clearRect(0, 0, this.svg.viewBoxWidth, this.svg.viewBoxHeight) - return pix - } - -// getMinShowWidth(): number{ -// return px2vp(this.svg.viewBoxWidth < globalThis.deviceW ? this.svg.viewBoxWidth : globalThis.deviceW) -// } -// -// getMinShowHeight(): number{ -// return px2vp(this.svg.viewBoxHeight < globalThis.deviceH ? this.svg.viewBoxHeight : globalThis.deviceH) -// } -} \ No newline at end of file