Browse Source

update: 新增自定义字体

liutian 11 months ago
parent
commit
b261d27693

+ 0 - 6
packages/core/src/editor/content_container.js

@@ -31,12 +31,6 @@ export class ContentContainer {
       pagePtr: this.pagePtr
     })
     this.editPagePtr = editPagePtr
-
-    await this.messageHandler.sendWithPromise('SetFontCallBackForEditPage', {
-      editPagePtr: this.editPagePtr,
-      fontFile: this.pageViewer.fontFile,
-      fontName: 'DroidSansFallbackFull'
-    })
   }
 
   async render () {

+ 5 - 3
packages/core/src/index.js

@@ -248,11 +248,13 @@ class ComPDFKitViewer {
 
     if (verified) {
       if (!this.webviewerServer) {
-        const response = await fetch('./lib/DroidSansFallbackFull.ttf')
+        const response = await fetch('./lib/font.ff')
         const blob = await response.blob()
-        this.#fonFile = new File([blob], 'DroidSansFallbackFull.ttf', { type: blob.type })
+        this.#fonFile = new File([blob], 'font.ff', { type: blob.type })
       }
-      const doc = await this.messageHandler.sendWithPromise('InitDocument')
+      const doc = await this.messageHandler.sendWithPromise('InitDocument', {
+        fontJson: this.#fonFile
+      })
 
       this.doc = doc
     }

+ 23 - 38
packages/core/src/worker/compdfkit_worker.js

@@ -4,10 +4,13 @@ import { parseAdobePDFTimestamp, convertToPDFTimestamp, roundToDecimalPlaces, co
 import { AnnotationType, WidgetType, LineTypeString, StampType, StampTypeString, TextStampShapeString, ActionTypeString, WidgetTypeString, AnnotationFlags, BorderStyleInt, BorderStyleString, ALIGN, ALIGNMAP } from '../../constants'
 
 let ComPDFKitJS = {
-  opened_Font: [],
-  opened_FontNames: [],
   opened_DefFont: [],
-  opened_image: []
+  opened_Font: [],
+  opened_image: [],
+  ComPDFKitDownloadFont,
+  ComPDFKitDownloadFontA,
+  FontNameList: [],
+  FontCount: 3000
 }
 
 let DataArray = []
@@ -52,6 +55,13 @@ function hexToRGB(hexColor) {
   }
 }
 
+function ComPDFKitDownloadFont(index){
+  return  ComPDFKitJS.opened_Font[index];
+}
+function ComPDFKitDownloadFontA(index){
+  return  ComPDFKitJS.opened_Font[index];
+}
+
 class CPDFWorker {
   static async setUp() {
     const messageHandler = new MessageHandler('worker', 'main', self)
@@ -88,40 +98,32 @@ class CPDFWorker {
       return error
     })
 
-    messageHandler.on('InitDocument', () => {
-      const doc = Module._InitDocument()
-
-      return doc
-    })
-
-    messageHandler.on('LoadFile', async (data) => {
-      ComPDFKitJS.opened_files = []
-      ComPDFKitJS.opened_files[0] = data.buffer
-
-      if (!data.fontFile) return true
-
+    messageHandler.on('InitDocument', async (data) => {
+      const { fontJson } = data
       await new Promise((resolve, reject) => {
         const fileReader = new FileReader()
         fileReader.onload = () => {
           const buf = new Uint8Array(fileReader.result)
-
           ComPDFKitJS.opened_Font[0] = buf
-          ComPDFKitJS.opened_FontNames[0] = data.fontName
-
-          ComPDFKitJS.opened_DefFont[0] = buf
-
           resolve(true)
         }
         fileReader.onerror = () => {
           reject(fileReader.error)
         }
-        fileReader.readAsArrayBuffer(data.fontFile)
+        fileReader.readAsArrayBuffer(fontJson)
       })
+
+      Module._InitComPDFkitFont()
       const doc = Module._InitDocument()
 
       return doc
     })
 
+    messageHandler.on('LoadFile', (data) => {
+      ComPDFKitJS.opened_files = []
+      ComPDFKitJS.opened_files[0] = data.buffer
+    })
+
     messageHandler.on('LoadDocumentByStream', loadDocumentByStream)
 
     messageHandler.on('GetPageCount', (data) => {
@@ -1092,23 +1094,6 @@ class CPDFWorker {
       return U8StringData
     })
 
-    messageHandler.on('SetFontCallBackForEditPage',async (data) => {
-      const { editPagePtr } = data
-
-      const fileReader = new FileReader()
-      fileReader.onload = () => {
-        const buf = new Uint8Array(fileReader.result)
-
-        ComPDFKitJS.opened_Font[0] = buf
-        ComPDFKitJS.opened_FontNames[0] = data.fontName
-        Module._SetFontCallBackForEditPage(editPagePtr)
-      }
-      fileReader.onerror = () => {
-        reject(fileReader.error)
-      }
-      fileReader.readAsArrayBuffer(data.fontFile)
-    })
-
     messageHandler.on('createEditorDoc', async (data) => {
       const { doc, password } = data
       return await copyDocument(doc, password)