diff --git a/packages/pdfx/lib/src/viewer/pinch/pdf_view_pinch.dart b/packages/pdfx/lib/src/viewer/pinch/pdf_view_pinch.dart index 99488684f3e77f3bf18e8bb55faf387c720437d6..89f9968c17abb15674e0e805c7ac03deafe002ba 100644 --- a/packages/pdfx/lib/src/viewer/pinch/pdf_view_pinch.dart +++ b/packages/pdfx/lib/src/viewer/pinch/pdf_view_pinch.dart @@ -410,7 +410,7 @@ class _PdfViewPinchState extends State return; } - const fullPurgeDistThreshold = 33; + const fullPurgeDistThreshold = 10; const partialRemovalDistThreshold = 8; final dpr = View.of(context).devicePixelRatio; @@ -444,33 +444,6 @@ class _PdfViewPinchState extends State fh <= page.preview!.textureHeight!) { // no real-size overlay needed; use preview page.realSizeOverlayRect = null; - } else { - // render real-size overlay - final offset = part.topLeft - pageRectZoomed.topLeft; - page - ..realSizeOverlayRect = Rect.fromLTWH( - offset.dx / r, - offset.dy / r, - part.width / r, - part.height / r, - ) - ..realSize ??= await page.pdfPage.createTexture(); - final w = (part.width * dpr).toInt(); - final h = (part.height * dpr).toInt(); - await page.realSize!.updateRect( - documentId: _controller._document!.id, - width: w, - height: h, - sourceX: (offset.dx * dpr).toInt(), - sourceY: (offset.dy * dpr).toInt(), - textureWidth: w, - textureHeight: h, - fullWidth: fw, - fullHeight: fh, - allowAntiAliasing: true, - backgroundColor: '#ffffff', - ); - page._updateRealSizeOverlay(); } } } diff --git a/packages/pdfx/ohos/src/main/ets/dev/flutter/pigeon/Pigeon.ets b/packages/pdfx/ohos/src/main/ets/dev/flutter/pigeon/Pigeon.ets deleted file mode 100644 index 6bfbe6746124e40475d227b873c81729d8fd8734..0000000000000000000000000000000000000000 --- a/packages/pdfx/ohos/src/main/ets/dev/flutter/pigeon/Pigeon.ets +++ /dev/null @@ -1,1007 +0,0 @@ -/** - * Copyright (c) 2024 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 AbilityAware from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/ability/AbilityAware'; -import { AbilityPluginBinding } from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/ability/AbilityPluginBinding'; -import { FlutterPlugin, FlutterPluginBinding } from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/FlutterPlugin'; -import { MethodCallHandler, MethodResult } from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel'; -import MethodChannel from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel'; -import MethodCall from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodCall'; -import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; -import HashMap from '@ohos.util.HashMap'; -import StandardMessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/StandardMessageCodec'; -import { ByteBuffer } from '@ohos/flutter_ohos/src/main/ets/util/ByteBuffer'; -import MessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/MessageCodec'; -import { BinaryMessenger } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BinaryMessenger'; -import BasicMessageChannel, { Reply } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BasicMessageChannel'; - - - -export class OpenDataMessage{ - - private data: Uint8Array; - - constructor(data: Uint8Array) { - this.data = data; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("data", this.data); - return toMapResult; - } - - static fromMap(map: Map): OpenDataMessage{ - const data = map.get("data") as Uint8Array; - return new OpenDataMessage(data); - } - -} - -class OpenPathMessage { - - private path : string; - -/* setPath(path : string){ - this.path = path; - } - - getPath(): string { - return this.path; - }*/ - - private password : string; - -/* setPassword(password : string){ - this.password = password; - } - - getPassword(): string { - return this.password; - }*/ - - constructor(path : string, password : string) { - this.path = path; - this.password = password; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("path", this.path); - toMapResult.set("password", this.password); - return toMapResult; - } - - static fromMap(map: Map): OpenPathMessage{ - const path = map.get("path") as string; - const password = map.get("password") as string; - return new OpenPathMessage(path,password); - } - -} - -class OpenReply { - - private id : string; - - private pagesCount : number; - - constructor(id : string, pagesCount : number) { - this.id = id; - this.pagesCount = pagesCount; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("id", this.id); - toMapResult.set("pagesCount", this.pagesCount); - return toMapResult; - } - - static fromMap(map: Map): OpenReply{ - const path = map.get("id") as string; - const pagesCount = map.get("pagesCount") as number; - return new OpenReply(path,pagesCount); - } -} - -class IdMessage { - - private id : string; - - - constructor(id : string) { - this.id = id; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("id", this.id); - return toMapResult; - } - - static fromMap(map: Map): IdMessage{ - const path = map.get("id") as string; - return new IdMessage(path); - } -} - - -class GetPageMessage { - - private documentId : string; - - private pageNumber : number; - - private autoCloseOhOs : boolean; - - constructor(documentId : string, pageNumber : number, autoCloseOhOs : boolean) { - this.documentId = documentId; - this.pageNumber = pageNumber; - this.autoCloseOhOs = autoCloseOhOs; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("documentId", this.documentId); - toMapResult.set("pageNumber", this.pageNumber); - toMapResult.set("autoCloseOhOs", this.autoCloseOhOs); - return toMapResult; - } - - static fromMap(map: Map): GetPageMessage{ - const documentId = map.get("documentId") as string; - const pageNumber = map.get("pageNumber") as number; - const autoCloseOhOs = map.get("autoCloseOhOs") as boolean; - return new GetPageMessage(documentId,pageNumber,autoCloseOhOs); - } -} - -class GetPageReply { - - private id : string; - - private width : number; - - private height : number; - - constructor(id : string, width : number, height : number) { - this.id = id; - this.width = width; - this.height = height; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("id", this.id); - toMapResult.set("width", this.width); - toMapResult.set("height", this.height); - return toMapResult; - } - - static fromMap(map: Map): GetPageReply{ - const id = map.get("id") as string; - const width = map.get("width") as number; - const height = map.get("height") as number; - return new GetPageReply(id,width,height); - } -} - -class RenderPageMessage { - - private pageId : string; - - private width : number; - - private height : number; - - private format : number; - - private backgroundColor : string; - - private crop : boolean; - - private cropX : number; - - private cropY : number; - - private cropHeight : number; - - private cropWidth : number; - - private quality : number; - - private forPrint : boolean; - - - constructor(pageId : string, width : number, height : number, format : number, backgroundColor : string, crop : boolean, - cropX : number, cropY : number, cropHeight : number, cropWidth : number, quality : number,forPrint : boolean ) { - this.pageId = pageId; - this.width = width; - this.height = height; - this.format = format; - this.backgroundColor = backgroundColor; - this.crop = crop; - this.cropX = cropX; - this.cropY = cropY; - this.cropHeight = cropHeight; - this.cropWidth = cropWidth; - this.quality = quality; - this.forPrint = forPrint; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("pageId", this.pageId); - toMapResult.set("width", this.width); - toMapResult.set("height", this.height); - toMapResult.set("format", this.format); - toMapResult.set("backgroundColor", this.backgroundColor); - toMapResult.set("crop", this.crop); - toMapResult.set("cropX", this.cropX); - toMapResult.set("cropY", this.cropY); - toMapResult.set("cropHeight", this.cropHeight); - toMapResult.set("cropWidth", this.cropWidth); - toMapResult.set("quality", this.quality); - toMapResult.set("forPrint", this.forPrint); - - return toMapResult; - } - - static fromMap(map: Map): RenderPageMessage{ - const pageId = map.get("pageId") as string; - const width = map.get("width") as number; - const height = map.get("height") as number; - const format = map.get("format") as number; - const backgroundColor = map.get("backgroundColor") as string; - const crop = map.get("crop") as boolean; - const cropX = map.get("cropX") as number; - const cropY = map.get("cropY") as number; - const cropHeight = map.get("cropHeight") as number; - const cropWidth = map.get("cropWidth") as number; - const quality = map.get("quality") as number; - const forPrint = map.get("forPrint") as boolean; - return new RenderPageMessage(pageId,width,height,format,backgroundColor,crop,cropX,cropY,cropHeight,cropWidth,quality,forPrint); - } - -} - -class RenderPageReply { - - private width : number; - - private height : number; - - private path : string; - - private data : Uint8Array; - - - constructor(width : number, height : number, path : string, data : Uint8Array) { - this.width = width; - this.height = height; - this.path = path; - this.data = data; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("width", this.width); - toMapResult.set("height", this.height); - toMapResult.set("path", this.path); - toMapResult.set("data", this.data); - return toMapResult; - } - - static fromMap(map: Map): RenderPageReply{ - const width = map.get("width") as number; - const height = map.get("height") as number; - const path = map.get("path") as string; - const data = map.get("data") as Uint8Array; - return new RenderPageReply(width,height,path,data); - } - -} - -class RegisterTextureReply { - - private id : number; - - constructor(id : number) { - this.id = id; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("id", this.id); - return toMapResult; - } - - static fromMap(map: Map): RegisterTextureReply{ - const id = map.get("id") as number; - return new RegisterTextureReply(id); - } -} - -class UpdateTextureMessage { - - private documentId : string; - - private pageNumber : number; - - private pageId : string; - - private textureId : number; - - private width : number; - - private height : number; - - private backgroundColor : string; - - private sourceX : number; - - private sourceY : number; - - private destinationX : number; - - private destinationY : number; - - private fullWidth : number; - - private fullHeight : number; - - private textureWidth : number; - - private textureHeight : number; - - private allowAntiAliasing : boolean; - - constructor(documentId : string, pageNumber : number, pageId : string, textureId : number, width : number, height : number, - backgroundColor : string, sourceX : number, sourceY : number, destinationX : number, destinationY : number, - fullWidth : number, fullHeight : number, textureWidth : number, textureHeight : number, allowAntiAliasing : boolean) { - this.documentId = documentId; - this.pageNumber = pageNumber; - this.pageId = pageId; - this.textureId = textureId; - this.width = width; - this.height = height; - this.backgroundColor = backgroundColor; - this.sourceX = sourceX; - this.sourceY = sourceY; - this.destinationX = destinationX; - this.destinationY = destinationY; - this.fullWidth = fullWidth; - this.fullHeight = fullHeight; - this.textureWidth = textureWidth; - this.textureHeight = textureHeight; - this.allowAntiAliasing = allowAntiAliasing; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("documentId", this.documentId); - toMapResult.set("pageNumber", this.pageNumber); - toMapResult.set("pageId", this.pageId); - toMapResult.set("textureId", this.textureId); - toMapResult.set("width", this.width); - toMapResult.set("height", this.height); - toMapResult.set("backgroundColor", this.backgroundColor); - toMapResult.set("sourceX", this.sourceX); - toMapResult.set("sourceY", this.sourceY); - toMapResult.set("destinationX", this.destinationX); - toMapResult.set("destinationY", this.destinationY); - toMapResult.set("fullWidth", this.fullWidth); - toMapResult.set("fullHeight", this.fullHeight); - toMapResult.set("textureWidth", this.textureWidth); - toMapResult.set("textureHeight", this.textureHeight); - toMapResult.set("allowAntiAliasing", this.allowAntiAliasing); - return toMapResult; - } - - static fromMap(map: Map): UpdateTextureMessage{ - const documentId = map.get("documentId") as string; - const pageNumber = map.get("pageNumber") as number; - const pageId = map.get("pageId") as string; - const textureId = map.get("textureId") as number; - const width = map.get("width") as number; - const height = map.get("height") as number; - const backgroundColor = map.get("backgroundColor") as string; - const sourceX = map.get("sourceX") as number; - const sourceY = map.get("sourceY") as number; - const destinationX = map.get("destinationX") as number; - const destinationY = map.get("destinationY") as number; - const fullWidth = map.get("fullWidth") as number; - const fullHeight = map.get("fullHeight") as number; - const textureWidth = map.get("textureWidth") as number; - const textureHeight = map.get("textureHeight") as number; - const allowAntiAliasing = map.get("allowAntiAliasing") as boolean; - return new UpdateTextureMessage(documentId,pageNumber,pageId,textureId,width,height,backgroundColor,sourceX,sourceY, - destinationX,destinationY,fullWidth,fullHeight,textureWidth,textureHeight,allowAntiAliasing); - } -} - -class ResizeTextureMessage { - - private textureId : number; - - private width : number; - - private height : number; - - constructor(textureId : number, width : number, height : number) { - this.textureId = textureId; - this.width = width; - this.height = height; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("textureId", this.textureId); - toMapResult.set("width", this.width); - toMapResult.set("height", this.height); - return toMapResult; - } - - static fromMap(map: Map): ResizeTextureMessage{ - const textureId = map.get("textureId") as number; - const width = map.get("width") as number; - const height = map.get("height") as number; - return new ResizeTextureMessage(textureId,width,height); - } -} - -class UnregisterTextureMessage { - - private id : number; - - constructor(id : number) { - this.id = id; - } - - toMap(): Map{ - const toMapResult = new Map(); - toMapResult.set("id", this.id); - return toMapResult; - } - - static fromMap(map: Map): UnregisterTextureMessage{ - const id = map.get("id") as number; - return new UnregisterTextureMessage(id); - } -} - -export interface Result { - success: (result: T) => void; - - error: (error: Error) => void; -} - - class PdfxApiCodec extends StandardMessageCodec { - - public static INSTANCE :PdfxApiCodec = new PdfxApiCodec(); - - readValueOfType(type: number, buffer: ByteBuffer): ESObject { - switch (type) { - case 128: - return GetPageMessage.fromMap(super.readValue(buffer)); - - case 129: - return GetPageReply.fromMap(super.readValue(buffer)); - - case 130: - return IdMessage.fromMap(super.readValue(buffer)); - - case 131: - return OpenDataMessage.fromMap(super. readValue(buffer)); - - case 132: - return OpenPathMessage.fromMap(super.readValue(buffer)); - - case 133: - return OpenReply.fromMap(super.readValue(buffer)); - - case 134: - return RegisterTextureReply.fromMap(super.readValue(buffer)); - - case 135: - return RenderPageMessage.fromMap(super.readValue(buffer)); - - case 136: - return RenderPageReply.fromMap(super.readValue(buffer)); - - case 137: - return ResizeTextureMessage.fromMap(super.readValue(buffer)); - - case 138: - return UnregisterTextureMessage.fromMap(super.readValue(buffer)); - - case 139: - return UpdateTextureMessage.fromMap(super.readValue(buffer)); - - default: - return super.readValueOfType(type, buffer); - - } - - } - - writeValue(stream: ByteBuffer, value: ESObject): ESObject { - - if (value instanceof GetPageMessage) { - stream.writeInt8(128); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof GetPageReply) { - stream.writeInt8(129); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof IdMessage) { - stream.writeInt8(130); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof OpenDataMessage) { - stream.writeInt8(131); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof OpenPathMessage) { - stream.writeInt8(132); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof OpenReply) { - stream.writeInt8(133); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof RegisterTextureReply) { - stream.writeInt8(134); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof RenderPageMessage) { - stream.writeInt8(135); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof RenderPageReply) { - stream.writeInt8(136); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof ResizeTextureMessage) { - stream.writeInt8(137); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof UnregisterTextureMessage) { - stream.writeInt8(138); - super.writeValue(stream, value.toMap()); - } else - if (value instanceof UpdateTextureMessage) { - stream.writeInt8(139); - super.writeValue(stream, value.toMap()); - } else - { - super.writeValue(stream, value); - } - - } -} - -export abstract class PdfxApi { - - abstract openDocumentData(message :OpenDataMessage , result:Result) : void; - - abstract openDocumentFile(message: OpenPathMessage, result: Result) : void; - - abstract openDocumentAsset(message: OpenPathMessage, result: Result) : void; - - abstract closeDocument(message: IdMessage) : void; - - abstract getPage(message: GetPageMessage, result: Result) : void; - - abstract renderPage(message: RenderPageMessage, result: Result ) : void; - - abstract closePage(message: IdMessage) : void; - - abstract registerTexture(): RegisterTextureReply; - - abstract updateTexture(message: UpdateTextureMessage, result: Result ) : void; - - abstract resizeTexture(message: ResizeTextureMessage, result: Result) : void; - - abstract unregisterTexture(message: UnregisterTextureMessage ) : void; - - static getCodec() : MessageCodec{ - return PdfxApiCodec.INSTANCE; - } - - setup(binaryMessenger: BinaryMessenger,api: PdfxApi):void{ - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.openDocumentData",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { - onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: OpenDataMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - - const resultCallback: Result = - { - success: (result: OpenReply): void => { - wrapped.set("result", result); - reply.reply(wrapped); - }, - - error: (error: Error): void => { - wrapped.set("error", PdfxApi.wrapError(error)); - reply.reply(wrapped); - } - }; - api.openDocumentData(messageArg,resultCallback); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - reply.reply(wrapped); - } - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.openDocumentFile",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: OpenPathMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - - const resultCallback: Result = - { - success: (result: OpenReply): void => { - wrapped.set("result", result); - reply.reply(wrapped); - }, - - error: (error: Error): void => { - wrapped.set("error", PdfxApi.wrapError(error)); - reply.reply(wrapped); - } - }; - api.openDocumentFile(messageArg,resultCallback); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - reply.reply(wrapped); - } - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.openDocumentAsset",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: OpenPathMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - - const resultCallback: Result = - { - success: (result: OpenReply): void => { - wrapped.set("result", result); - reply.reply(wrapped); - }, - - error: (error: Error): void => { - wrapped.set("error", PdfxApi.wrapError(error)); - reply.reply(wrapped); - } - }; - api.openDocumentAsset(messageArg,resultCallback); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - reply.reply(wrapped); - } - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.closeDocument",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: IdMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - api.closeDocument(messageArg); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - } - reply.reply(wrapped); - } - }); - } else { - channel.setMessageHandler(null); - } - } - - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.getPage",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: GetPageMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - - const resultCallback: Result = - { - success: (result: GetPageReply): void => { - wrapped.set("result", result); - reply.reply(wrapped); - }, - - error: (error: Error): void => { - wrapped.set("error", PdfxApi.wrapError(error)); - reply.reply(wrapped); - } - }; - api.getPage(messageArg,resultCallback); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - reply.reply(wrapped); - } - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.renderPage",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: RenderPageMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - - const resultCallback: Result = - { - success: (result: RenderPageReply): void => { - wrapped.set("result", result); - reply.reply(wrapped); - }, - - error: (error: Error): void => { - wrapped.set("error", PdfxApi.wrapError(error)); - reply.reply(wrapped); - } - }; - api.renderPage(messageArg,resultCallback); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - reply.reply(wrapped); - } - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.closePage",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: IdMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - api.closePage(messageArg); - wrapped.set("result", null); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - } - reply.reply(wrapped); - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.registerTexture",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const output = api.registerTexture(); - wrapped.set("result", output); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - } - reply.reply(wrapped); - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.updateTexture",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: UpdateTextureMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - - const resultCallback: Result = - { - success: (result: void): void => { - wrapped.set("result", null); - reply.reply(wrapped); - }, - - error: (error: Error): void => { - wrapped.set("error", PdfxApi.wrapError(error)); - reply.reply(wrapped); - } - }; - api.updateTexture(messageArg,resultCallback); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - reply.reply(wrapped); - } - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.resizeTexture",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: ResizeTextureMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - - const resultCallback: Result = - { - success: (result: void): void => { - wrapped.set("result", null); - reply.reply(wrapped); - }, - - error: (error: Error): void => { - wrapped.set("error", PdfxApi.wrapError(error)); - reply.reply(wrapped); - } - }; - api.resizeTexture(messageArg,resultCallback); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - reply.reply(wrapped); - } - } - }); - } else { - channel.setMessageHandler(null); - } - } - - { - const channel: BasicMessageChannel = - new BasicMessageChannel(binaryMessenger,"dev.flutter.pigeon.PdfxApi.unregisterTexture",PdfxApi.getCodec()); - if (api != null) { - channel.setMessageHandler( - { onMessage: (message: ESObject, reply: Reply) => { - let wrapped: Map = new Map(); - try { - const args: Array = message; - const messageArg: UnregisterTextureMessage = args[0]; - if (messageArg == null) { - throw new Error("messageArg unexpectedly null.") - } - - api.unregisterTexture(messageArg); - wrapped.set("result", null); - } catch (err) { - wrapped.set("error", PdfxApi.wrapError(err)); - reply.reply(wrapped); - } - } - }); - } else { - channel.setMessageHandler(null); - } - } - - } - - - static wrapError(exception: Error): Map { - const errorMap: Map = new Map(); - errorMap.set("message",exception.message); - errorMap.set("code", exception.name); - errorMap.set("details", exception.stack); - return errorMap; - } - -} - diff --git a/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/Messages.ets b/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/Messages.ets index 4c0473b22d50b1c131ca86a437fbca5999669e26..01b698c646a690d6ad474931449d2251ee23ddd9 100644 --- a/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/Messages.ets +++ b/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/Messages.ets @@ -15,7 +15,6 @@ import { PlatformViewsController } from '@ohos/flutter_ohos'; import { FlutterPluginBinding } from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/FlutterPlugin'; -import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; import { GetPageMessage, GetPageReply, @@ -34,16 +33,14 @@ import { } from './Pigeon'; import { PdfViewPlatformView } from './PdfxView' -import { Params } from '@ohos/flutter_ohos/src/main/ets/plugin/platform/PlatformView'; import { common } from '@kit.AbilityKit'; import fs from '@ohos.file.fs'; -import { randomID, randomFilename } from './utils/Random'; +import { randomFilename } from './utils/Random'; import { pdfService } from '@kit.PDFKit'; import { PageRepository } from './resources/PageRepository'; import { DocumentRepository } from './resources/DocumentRepository'; import { image } from '@kit.ImageKit'; -import { SurfaceTextureEntry } from '@ohos/flutter_ohos/src/main/ets/view/TextureRegistry'; - +import { TextureRegistry } from '@ohos/flutter_ohos'; const TAG = "Messages"; @@ -52,8 +49,10 @@ export class Messages extends PdfxApi { private documents: DocumentRepository; private pages: PageRepository; private platformViewController: PlatformViewsController | null; - private textures: Map = new Map(); + private textures: Map = new Map(); private platformViews: Map = new Map(); + private pixelMaps: Map = new Map(); + private textureRegistry: TextureRegistry; constructor(binding: FlutterPluginBinding, documents: DocumentRepository, @@ -63,9 +62,11 @@ export class Messages extends PdfxApi { this.documents = documents; this.pages = pages; this.platformViewController = this.binding.getFlutterEngine().getPlatformViewsController(); + this.textureRegistry = this.binding.getTextureRegistry(); } openDocumentData(message: OpenDataMessage, result: Result): void { + this.cleanAll(); const resultResponse = new OpenReply(); try { let filePath = getContext(this).tempDir + '/' + randomFilename(); @@ -86,6 +87,7 @@ export class Messages extends PdfxApi { } openDocumentFile(message: OpenPathMessage, result: Result): void { + this.cleanAll(); const resultResponse = new OpenReply(); try { const pdfDocument = new pdfService.PdfDocument(); @@ -101,6 +103,7 @@ export class Messages extends PdfxApi { } openDocumentAsset(message: OpenPathMessage, result: Result): void { + this.cleanAll(); const resultResponse: OpenReply = new OpenReply(); try { const pdfDocument = new pdfService.PdfDocument(); @@ -127,6 +130,18 @@ export class Messages extends PdfxApi { } } + renderPage(message: RenderPageMessage, result: Result): void { + } + + closePage(message: IdMessage): void { + try { + const id = message.getId(); + this.pages.close(id); + } catch (e) { + throw new Error(); + } + } + getPage(message: GetPageMessage, result: Result): void { const resultResponse = new GetPageReply() try { @@ -151,78 +166,71 @@ export class Messages extends PdfxApi { } } - renderPage(message: RenderPageMessage, result: Result): void { - const resultResponse = new RenderPageReply(); - try { - const pageId = message.getPage(); - - const page = this.pages.get(pageId) + registerTexture(): RegisterTextureReply { + const textureId = this.textureRegistry.getTextureId(); + const surfaceTexture = this.textureRegistry.registerTexture(textureId); - const pageImage: image.PixelMap = page.render(); + this.textures.set(textureId, surfaceTexture.getSurfaceId()); - // 渲染 + const platformView: PdfViewPlatformView = new PdfViewPlatformView(); + this.platformViews.set(textureId, platformView); - resultResponse.setPath(""); - resultResponse.setWidth(1.1); - resultResponse.setHeight(1.1) - result.success(resultResponse) - } catch (e) { - result.error(e); - } + return new RegisterTextureReply(textureId); } - - closePage(message: IdMessage): void { + updateTexture(message: UpdateTextureMessage, result: Result) { try { - const id = message.getId(); - this.pages.close(id); + const textureId = message.getTextureId(); + const pageId = message.getPageId(); + const surfaceId = this.textures.get(textureId); + + let pixelMap = this.pixelMaps.get(textureId) + if (pixelMap === undefined) { + pixelMap = this.pages.get(pageId).render(); + this.pixelMaps.set(textureId, pixelMap); + } + + // 找到textureId对应的platformView + let platformView = this.platformViews.get(textureId); + platformView?.setPixelMap(pixelMap); + this.platformViewController?.render(surfaceId, platformView, + message.getWidth(), + message.getHeight(), + message.getSourceX(), + message.getSourceY()); + result.success(undefined); } catch (e) { - throw new Error(); + result.error(e); } } - private flutterTexture?: SurfaceTextureEntry; - - registerTexture(): RegisterTextureReply { - const textureRegistry = this.binding.getTextureRegistry(); - const textureId = textureRegistry.getTextureId(); - const surfaceTexture = textureRegistry.registerTexture(textureId); - - this.textures.set(textureId, surfaceTexture); - const platformView: PdfViewPlatformView = new PdfViewPlatformView(); - this.platformViews.set(textureId, platformView); + unregisterTexture(message: UnregisterTextureMessage): void { + try { + const textureId = message.getId(); - return new RegisterTextureReply(textureId); - } + let surfaceId = this.textures.get(textureId); + this.textures.delete(textureId); + this.platformViews.delete(textureId); - updateTexture(message: UpdateTextureMessage, result: Result): void { - try { - const textureId = message.getTextureId(); - const pageNumber = message.getPageNumber(); - const surfaceTexture = this.textures.get(textureId); - const document = this.documents.get(message.getDocumentId()); + this.pixelMaps.get(textureId)?.release(); + this.pixelMaps.delete(textureId); - const pdfPage: pdfService.PdfPage = document.openPage(pageNumber); - const pixelMap: image.PixelMap = pdfPage.getPagePixelMap(); - // todo: 刷新渲染 - // 找到textureId对应的platformView - const platformView = this.platformViews.get(textureId); - platformView!.setPixelMap(pixelMap); - platformView!.setWidth(message.getWidth()); - platformView!.setHeight(message.getHeight()); - platformView!.setLeft(message.getSourceX()); - platformView!.setTop(message.getSourceY()); + this.platformViewController?.dispose(surfaceId); - this.platformViewController?.render(surfaceTexture?.getSurfaceId(), platformView!, - message.getWidth(), message.getHeight(), message.getSourceX(), message.getSourceY()); + this.textureRegistry.unregisterTexture(textureId); - result.success(undefined); } catch (e) { - result.error(e); + console.error(e) } } + cleanAll() { + this.textures.forEach((value, key) => { + this.unregisterTexture((new UnregisterTextureMessage(key))) + }) + } + resizeTexture(message: ResizeTextureMessage, result: Result): void { const texId = message.getTextureId(); const width = message.getWidth(); @@ -232,14 +240,6 @@ export class Messages extends PdfxApi { result.success() } - unregisterTexture(message: UnregisterTextureMessage): void { - const id = message.getId(); - const tex = this.textures.get(id); - tex?.release(); - this.textures.delete(id); - this.binding.getTextureRegistry().unregisterTexture(id); - } - openDataDocument() { } @@ -262,4 +262,4 @@ export class Messages extends PdfxApi { } return filePath; } -} \ No newline at end of file +} diff --git a/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/OhosPdfView.ets b/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/OhosPdfView.ets index 99ba62edb9fd89ef8dbb323e028602c50cd78516..39c8f882f775acb7d09eca64ac204e9b70d6de3b 100644 --- a/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/OhosPdfView.ets +++ b/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/OhosPdfView.ets @@ -12,11 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -import {pdfService} from '@kit.PDFKit'; + +import { pdfService } from '@kit.PDFKit'; import { image } from '@kit.ImageKit'; import { Params } from '@ohos/flutter_ohos/src/main/ets/plugin/platform/PlatformView'; -import {PdfViewPlatformView}from './PdfxView' +import { PdfViewPlatformView } from './PdfxView' const TAG = '[PDFKit_LOG_TAG]'; @Entry @@ -25,14 +25,20 @@ struct PDFPreview { @Prop params: Params; _pixelMap: image.PixelMap | undefined = undefined; + aboutToDisappear(): void { + this._pixelMap?.release(() => { + this._pixelMap = undefined; + }) + } + build() { - Column(){ + Column() { Image(this._pixelMap) } } } @Builder -export function PdfBuilder(params: Params){ - PDFPreview({params: params,_pixelMap: (params.platformView as PdfViewPlatformView).getPixelMap()}); -} \ No newline at end of file +export function PdfBuilder(params: Params) { + PDFPreview({ params: params, _pixelMap: (params.platformView as PdfViewPlatformView).getPixelMap() }); +} diff --git a/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/PdfxView.ets b/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/PdfxView.ets index 235b8b03df0cae4fe6352bd924d9b7c526ee10a8..15e7a3b2ebcd35f2416399f1edc5f7c6a319b63e 100644 --- a/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/PdfxView.ets +++ b/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/PdfxView.ets @@ -21,55 +21,23 @@ import { image } from '@kit.ImageKit'; export class PdfViewPlatformView extends PlatformView { private _pixelMap: image.PixelMap | undefined = undefined; - private _width: number = 0; - private _height: number = 0; - private _left: number = 0; - private _top: number = 0; getView(): WrappedBuilder<[Params]> { return new WrappedBuilder(PdfBuilder) } dispose(): void { + this._pixelMap?.release(() => { + this._pixelMap = undefined; + }) } getPixelMap(): image.PixelMap | undefined { return this._pixelMap; + } setPixelMap(_pixelMap: image.PixelMap | undefined): void { this._pixelMap = _pixelMap; } - - getWidth(): number { - return this._width; - } - - setWidth(setterArg: number) { - this._width = setterArg; - } - - getHeight(): number { - return this._height; - } - - setHeight(setterArg: number) { - this._height = setterArg; - } - - getLeft(): number { - return this._left; - } - - setLeft(setterArg: number) { - this._left = setterArg; - } - - getTop(): number { - return this._top; - } - - setTop(setterArg: number) { - this._top = setterArg; - } -} \ No newline at end of file +} diff --git a/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/Pigeon.ets b/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/Pigeon.ets index 6c65e51cbbccd33b60f4bb35b04eecd4e59c47f8..5ca1af897d68608c42d3aad851e5cbda55fb6cd7 100644 --- a/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/Pigeon.ets +++ b/packages/pdfx/ohos/src/main/ets/io/scer/pdfx/Pigeon.ets @@ -579,6 +579,15 @@ export class UpdateTextureMessage { } private pageId: string; + + public getPageId() { + return this.pageId; + } + + public setPageId(setterArg: string) { + this.pageId = setterArg; + } + private textureId: number; public getTextureId(): number {