Просмотр исходного кода

fix: 动态加载字体添加预先加载中文字体

liutian 7 месяцев назад
Родитель
Сommit
bc7f64e7b3

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

@@ -367,8 +367,6 @@ export class ContentContainer {
       await this.updateFrameEditor(res)
     }
 
-    // console.log('undoList: ', this.undoList)
-    // console.log('redoList: ', this.redoList)
     this.eventBus.dispatch('changeOperateList', {
       undoListLength: this.undoList.length,
       redoListLength: this.redoList.length

+ 32 - 6
packages/core/src/index.js

@@ -62,6 +62,7 @@ class ComPDFKitViewer {
     this.color = 'color'
     this.pagesPtr = []
     this.saveAction = null
+    this.webFontURL = './lib/fonts/'
     this.optionUrl = {
       // baseUrl: 'https://test-compdf.kdan.cn',
       baseUrl: 'https://wms.compdf.com',
@@ -266,12 +267,36 @@ class ComPDFKitViewer {
     // this.webviewerServer = true
 
     if (verified) {
+      if (options.webFontURL) {
+        this.webFontURL = options.webFontURL
+      }
       if (!this.webviewerServer) {
-        const response = await fetch('./lib/font.ff')
-        const resData = await response.arrayBuffer()
-        this.#fileList = new Uint8Array(resData)
-        const fileData = await fetch('./lib/fonts.json')
-        this.fontsJson = await fileData.json()
+        const webFontURL = this.webFontURL
+        
+        const response = await fetch(webFontURL + 'font.ff')
+        if (response.ok) {
+          const resData = await response.arrayBuffer()
+          this.#fileList = new Uint8Array(resData)
+        } else {
+          console.error('Invalid webFontURL')
+        }
+
+        if (this.#fileList) {
+          const fileData = await fetch(webFontURL + 'fonts.json')
+          if (fileData.ok) {
+            this.fontsJson = await fileData.json()
+          } else {
+            console.error('Invalid webFontURL')
+          }
+
+          const notoSansFontResponse = await fetch(webFontURL + 'NotoSansSC-Regular.otf.brotli')
+          if (notoSansFontResponse.ok) {
+            const notoSansFontData = await notoSansFontResponse.arrayBuffer()
+            this.notoSansFont = new Uint8Array(notoSansFontData)
+          } else {
+            console.error('Invalid webFontURL')
+          }
+        }
 
         this.messageHandler.on('LoadFont', data => {
           const fontFamily = data.fontFamily
@@ -518,8 +543,10 @@ class ComPDFKitViewer {
             doc: this.doc,
             pagesPtr: this.pagesPtr,
             buffer,
+            webFontURL: this.webFontURL,
             fileList: this.#fileList,
             fontsJson: this.fontsJson,
+            notoSansFont: this.notoSansFont,
           })
 
           this.doc = doc
@@ -3806,7 +3833,6 @@ class ComPDFKitViewer {
       doc: this.doc,
       signaturePtr: signature.signaturePtr
     })
-    console.log('deleteSignature', signature)
     this.eventBus.dispatch('updateSignatureFieldAp', signature.signaturePtr)
 
     annotationStore.signatures.splice(signature.index, 1)

+ 16 - 8
packages/core/src/worker/compdfkit_worker.js

@@ -47,6 +47,7 @@ let DiffInfoArray = []
 let AnnotArray = []
 let fontsJson = null
 let fontFile = null
+let webFontURL = './fonts/'
 
 import MessageHandler from "../message_handler"
 import { convertFileToBuffer, convertBase64ToBytes } from '../fileHandler';
@@ -67,9 +68,6 @@ function readFileAsync(url) {
   if (xhr.status == 200 || xhr.status == 206) {
     const responseArray = new Uint8Array(xhr.response)
     return responseArray
-  }
-  xhr.onreadystatechange = function () {
-    
   }
   return 0
 }
@@ -78,15 +76,22 @@ function ComPDFKitDownloadFont(index){
   if (index === 0) {
     return  ComPDFKitJS.opened_Font[index];
   }
+  if (ComPDFKitJS.opened_Font[index]) {
+    return ComPDFKitJS.opened_Font[index]
+  }
   return fontFile
 }
 function ComPDFKitDownloadFontA(index){
   if (index === 0) {
     return  ComPDFKitJS.opened_Font[index];
   }
+  if (ComPDFKitJS.opened_Font[index]) {
+    return ComPDFKitJS.opened_Font[index]
+  }
   const font = fontsJson[index - 1]
-  fontFile = readFileAsync('./fonts/' + font.filePath)
+  fontFile = readFileAsync(webFontURL + font.filePath)
   CPDFWorker.MessageHandler.send('LoadFont', { fontFamily: font.family })
+  ComPDFKitJS.opened_Font[index] = fontFile
   return fontFile
 }
 
@@ -160,9 +165,15 @@ class CPDFWorker {
       if (data.defaultFont) {
         ComPDFKitJS.opened_DefFont[0] = data.defaultFont
       }
-
+      if (data.webFontURL !== './lib/fonts/') {
+        webFontURL = data.webFontURL
+      }
       fontsJson = data.fontsJson
 
+      if (data.notoSansFont) {
+        ComPDFKitJS.opened_Font[1] = data.notoSansFont
+      }
+
       Module._InitComPDFkitFont()
       const doc = Module._InitDocument()
 
@@ -418,9 +429,6 @@ class CPDFWorker {
       const newDoc = Module._InitDocument()
       const passwordPtr = stringToNewUTF8('')
       Module._LoadDocumentByStream(newDoc, length, buffer.length, passwordPtr)
-
-      // const modifyResult = Module._AddSignModifyDocument(signaturePtr, res, 1, ComPDFKitJS.opened_files[1].length)
-      // console.log(modifyResult)
     })
 
     messageHandler.on('LoadCertificates', async (data) => {

BIN
packages/webview/public/favicon.ico