소스 검색

fix: 设置默认中文字体

liutian 1 개월 전
부모
커밋
e6a03327ab
2개의 변경된 파일13개의 추가작업 그리고 3개의 파일을 삭제
  1. 5 0
      packages/core/src/index.js
  2. 8 3
      packages/core/src/worker/compdfkit_worker.js

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

@@ -63,6 +63,7 @@ class ComPDFKitViewer {
     this.color = 'color'
     this.pagesPtr = []
     this.saveAction = null
+    this.enableDefaultFont = false
     this.enableCustomFont = false
     this.fontsJson = null
     this.webFontURL = 'https://www.compdf.com/fonts/'
@@ -275,6 +276,9 @@ class ComPDFKitViewer {
     // this.webviewerServer = true
 
     if (verified) {
+      if ('enableDefaultFont' in options) {
+        this.enableDefaultFont = options.enableDefaultFont
+      }
       if (options.enableCustomFont) {
         this.enableCustomFont = true
       }
@@ -554,6 +558,7 @@ class ComPDFKitViewer {
             webFontURL: this.webFontURL,
             fileList: this.#fileList,
             fontsJson: this.fontsJson,
+            enableDefaultFont: this.enableDefaultFont
           })
 
           this.doc = doc

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

@@ -168,6 +168,11 @@ class CPDFWorker {
 
         Module._InitComPDFkitFont()
       }
+
+      if (data.enableDefaultFont && !ComPDFKitJS.opened_DefFont[0]) {
+        const fontFile = readFileAsync('./DroidSansFallbackFull.ttf')
+        ComPDFKitJS.opened_DefFont[0] = fontFile
+      }
       const doc = Module._InitDocument()
 
       return doc
@@ -401,7 +406,7 @@ class CPDFWorker {
 
       DataArray = []
 
-      const embedded = !!fontsJson
+      const embedded = !!fontsJson || ComPDFKitJS.opened_DefFont[0]
       Module._SaveDocumentByStream(doc, 2, embedded)
 
       const addResult = Module._AddSignSaveDocument(doc, signaturePtr, certPtr)
@@ -3067,7 +3072,7 @@ function addToTrustedCertificates(data) {
 async function copyDocument(doc, password) {
   DataArray = []
 
-  const embedded = !!fontsJson
+  const embedded = !!fontsJson || ComPDFKitJS.opened_DefFont[0]
   Module._SaveDocumentByStream(doc, 2, embedded);
   let pdfData = []
   for (let i = 0; i < DataArray.length; i++) {
@@ -3092,7 +3097,7 @@ function saveDocument(doc, saveType) {
   DataArray = []
   saveType === 1 && DataArray.push(ComPDFKitJS.opened_files[0])
 
-  const embedded = !!fontsJson
+  const embedded = !!fontsJson || ComPDFKitJS.opened_DefFont[0]
   Module._SaveDocumentByStream(doc, saveType, embedded)
   let pdfData = []
   for (let i = 0; i < DataArray.length; i++) {