1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16interface BaseContext { 17 readonly area: int; 18 readonly filesDir: string; 19 readonly tempDir: string; 20} 21 22abstract class Context implements BaseContext { 23 area: int = 0; 24 filesDir: string = ""; 25 tempDir: string = ""; 26} 27 28type Callback = ()=> void; 29 30class Result<T, E>{ 31 result:T; 32 error:E; 33 constructor(result: T , error: E) { 34 this.result = result; 35 this.error = error; 36 } 37} 38 39type AsyncCallback<T> = (err: BusinessError<T>, data: T)=> void; 40 41class BusinessError<T = void> { 42 code: number; 43 data: T; 44 constructor(code: number = 0, data?: T) { 45 this.code = code; 46 this.data = data!; 47 } 48} 49 50class printAni { 51 native static printAsyncCallback(files: Array<string>): int; 52 native static on(type: string) : int; 53 native static off(type: string) : int; 54} 55 56function syncFunction(files: Array<string>):number{ 57 let ret : number = printAni.printAsyncCallback(files) 58 return ret; 59} 60 61function syncOn(type: string):number{ 62 let ret : number = printAni.on(type) 63 return ret; 64} 65 66function syncOff(type: string):number{ 67 let ret : number = printAni.off(type) 68 return ret; 69} 70 71export default namespace print { 72 loadLibrary("print_ani"); 73 74 export interface PrintTask { 75 on(type: 'block' | 'succeed' | 'fail' | 'cancel', callback: Callback): void; 76 off(type: 'block' | 'succeed' | 'fail' | 'cancel', callback?: Callback): void; 77 } 78 79 class PrintTaskImpl implements PrintTask { 80 on(type: 'block' | 'succeed' | 'fail' | 'cancel', callback: Callback): void { 81 let taskAsyn = taskpool.execute(syncOn, type); 82 taskAsyn.then((e : NullishType) => { 83 let r = e as number; 84 callback(); 85 }); 86 } 87 off(type: 'block' | 'succeed' | 'fail' | 'cancel', callback?: Callback): void { 88 let taskAsyn = taskpool.execute(syncOff, type); 89 taskAsyn.then((e : NullishType) => { 90 let r = e as number; 91 if (callback) { 92 callback(); 93 } 94 }); 95 } 96 } 97 98 export function print(files: Array<string>, callback: AsyncCallback<PrintTask>): void { 99 let taskAsyn = taskpool.execute(syncFunction, files); 100 taskAsyn.then((e : NullishType) => { 101 let r = e as number; 102 console.println(r); 103 let data = new PrintTaskImpl(); 104 let err = new BusinessError<PrintTask>(0, data); 105 callback(err, data); 106 }); 107 } 108 109 export interface PrintPageRange { 110 startPage?: number; 111 endPage?: number; 112 pages?: Array<number>; 113 } 114 115 export interface PrintAttributes { 116 copyNumber?: number; 117 pageRange?: PrintPageRange; 118 pageSize?: PrintPageSize | PrintPageType; 119 directionMode?: PrintDirectionMode; 120 colorMode?: PrintColorMode; 121 duplexMode?: PrintDuplexMode; 122 } 123 124 export interface PrintDocumentAdapter { 125 onStartLayoutWrite(jobId: string, oldAttrs: PrintAttributes, newAttrs: PrintAttributes, fd: number, 126 writeResultCallback: (jobId: string, writeResult: PrintFileCreationState) => void): void; 127 onJobStateChanged(jobId: string, state: PrintDocumentAdapterState): void; 128 } 129 130 export enum PrintDuplexMode { 131 DUPLEX_MODE_NONE = 0, 132 DUPLEX_MODE_LONG_EDGE = 1, 133 DUPLEX_MODE_SHORT_EDGE = 2, 134 } 135 136 export enum PrintColorMode { 137 COLOR_MODE_MONOCHROME = 0, 138 COLOR_MODE_COLOR = 1, 139 } 140 141 export enum PrintDirectionMode { 142 DIRECTION_MODE_AUTO = 0, 143 DIRECTION_MODE_PORTRAIT = 1, 144 DIRECTION_MODE_LANDSCAPE = 2, 145 } 146 147 export interface PrintPageSize { 148 id: string; 149 name: string; 150 width: number; 151 height: number; 152 } 153 154 export enum PrintPageType { 155 PAGE_ISO_A3 = 0, 156 PAGE_ISO_A4 = 1, 157 PAGE_ISO_A5 = 2, 158 PAGE_JIS_B5 = 3, 159 PAGE_ISO_C5 = 4, 160 PAGE_ISO_DL = 5, 161 PAGE_LETTER = 6, 162 PAGE_LEGAL = 7, 163 PAGE_PHOTO_4X6 = 8, 164 PAGE_PHOTO_5X7 = 9, 165 PAGE_INT_DL_ENVELOPE = 10, 166 PAGE_B_TABLOID = 11, 167 } 168 169 export enum PrintDocumentAdapterState { 170 PREVIEW_DESTROY = 0, 171 PRINT_TASK_SUCCEED = 1, 172 PRINT_TASK_FAIL = 2, 173 PRINT_TASK_CANCEL = 3, 174 PRINT_TASK_BLOCK = 4, 175 } 176 177 export enum PrintFileCreationState { 178 PRINT_FILE_CREATED = 0, 179 PRINT_FILE_CREATION_FAILED = 1, 180 PRINT_FILE_CREATED_UNRENDERED = 2, 181 } 182} 183 184