Explorar o código

fix: 字体使用更新区分压缩和未压缩

liutian hai 1 mes
pai
achega
3a14a81995
Modificáronse 2 ficheiros con 25 adicións e 22 borrados
  1. 8 8
      packages/core/src/index.js
  2. 17 14
      packages/core/src/worker/compdfkit_worker.js

+ 8 - 8
packages/core/src/index.js

@@ -293,18 +293,18 @@ class ComPDFKitViewer {
       if (!this.webviewerServer && this.enableCustomFont) {
         const webFontURL = this.webFontURL
 
-        const response = await fetch(webFontURL + 'font.ff')
-        if (response.ok) {
-          const resData = await response.arrayBuffer()
-          this.#fileList = new Uint8Array(resData)
+        const fileData = await fetch(webFontURL + 'fonts.json')
+        if (fileData.ok) {
+          this.fontsJson = await fileData.json()
         } else {
           console.error('Invalid webFontURL')
         }
 
-        if (this.#fileList) {
-          const fileData = await fetch(webFontURL + 'fonts.json')
-          if (fileData.ok) {
-            this.fontsJson = await fileData.json()
+        if (this.fontsJson && this.fontsJson[0].filePath && this.fontsJson[0].filePath.endsWith('brotli')) {
+          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')
           }

+ 17 - 14
packages/core/src/worker/compdfkit_worker.js

@@ -10,7 +10,7 @@ let ComPDFKitJS = {
   ComPDFKitDownloadFont,
   ComPDFKitDownloadFontA,
   FontNameList: [],
-  FontCount: 120,
+  FontCount: 1,
   opened_image: [],
   opened_files: [],
   opened_cert: [],
@@ -73,18 +73,12 @@ function readFileAsync(url) {
 }
 
 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]
   }
@@ -160,13 +154,22 @@ class CPDFWorker {
 
       if (data.fileList) {
         ComPDFKitJS.opened_Font[0] = data.fileList
+      }
 
-        if (data.webFontURL !== 'https://www.compdf.com/fonts/') {
-          webFontURL = data.webFontURL
-        }
+      if (data.webFontURL !== 'https://www.compdf.com/fonts/') {
+        webFontURL = data.webFontURL
+      }
+
+      if (data.fontsJson) {
         fontsJson = data.fontsJson
 
-        Module._InitComPDFkitFont()
+        const compress = !!ComPDFKitJS.opened_Font[0]
+        ComPDFKitJS.FontCount = (compress ? (data.fontsJson.length + 1) : data.fontsJson.length)
+        if (!compress && data.fontsJson.length === 1) {
+          const fontFile = readFileAsync(webFontURL + data.fontsJson[0].filePath)
+          ComPDFKitJS.opened_Font[0] = fontFile
+        }
+        Module._InitComPDFkitFont(compress)
       }
 
       if (data.enableDefaultFont && !ComPDFKitJS.opened_DefFont[0]) {
@@ -406,7 +409,7 @@ class CPDFWorker {
 
       DataArray = []
 
-      const embedded = !!fontsJson || ComPDFKitJS.opened_DefFont[0]
+      const embedded = !!fontsJson || !!ComPDFKitJS.opened_DefFont[0]
       Module._SaveDocumentByStream(doc, 2, embedded)
 
       const addResult = Module._AddSignSaveDocument(doc, signaturePtr, certPtr)
@@ -3072,7 +3075,7 @@ function addToTrustedCertificates(data) {
 async function copyDocument(doc, password) {
   DataArray = []
 
-  const embedded = !!fontsJson || ComPDFKitJS.opened_DefFont[0]
+  const embedded = !!fontsJson || !!ComPDFKitJS.opened_DefFont[0]
   Module._SaveDocumentByStream(doc, 2, embedded);
   let pdfData = []
   for (let i = 0; i < DataArray.length; i++) {
@@ -3097,7 +3100,7 @@ function saveDocument(doc, saveType) {
   DataArray = []
   saveType === 1 && DataArray.push(ComPDFKitJS.opened_files[0])
 
-  const embedded = !!fontsJson || ComPDFKitJS.opened_DefFont[0]
+  const embedded = !!fontsJson || !!ComPDFKitJS.opened_DefFont[0]
   Module._SaveDocumentByStream(doc, saveType, embedded)
   let pdfData = []
   for (let i = 0; i < DataArray.length; i++) {