Browse Source

add: 文本编辑支持中文字体

liutian 1 year ago
parent
commit
5cae2f4df3

+ 4 - 4
packages/core/src/editor/content_container.js

@@ -29,10 +29,10 @@ export class ContentContainer {
     })
     this.editPagePtr = editPagePtr
 
-    // await this.messageHandler.sendWithPromise('SetFontCallBackForEditPage', {
-    //   editPagePtr: this.editPagePtr,
-    //   file: this.pageViewer.fontFile
-    // })
+    await this.messageHandler.sendWithPromise('SetFontCallBackForEditPage', {
+      editPagePtr: this.editPagePtr,
+      file: this.pageViewer.fontFile
+    })
   }
 
   async render () {

+ 14 - 11
packages/core/src/index.js

@@ -27,6 +27,7 @@ class ComPDFKitViewer {
   #oldPwd = ''
   #textSearch = null
   #activeSearchIndex = 0
+  #fonFile = null
   constructor(options) {
     this.config = options
     this.viewerContainer = null
@@ -155,6 +156,11 @@ class ComPDFKitViewer {
       });
 
     if (verified) {
+      if (!this.webviewerServer) {
+        const response = await fetch('/lib/DroidSansFallbackFull.ttf')
+        const blob = await response.blob()
+        this.#fonFile = new File([blob], 'DroidSansFallbackFull.ttf', { type: blob.type })
+      }
       await this.init()
     }
     return verified
@@ -704,7 +710,7 @@ class ComPDFKitViewer {
         annotation.isBold = rawAnnotation.textStyle.blod === 'true'
         annotation.isItalic = rawAnnotation.textStyle.italic === 'true'
         annotation.isMultiLine = rawAnnotation.textStyle.multiLine === 'true' ? 1 : 0
-        annotation.isHidden = (!rawAnnotation.flag || rawAnnotation.flag === 'invisible') ? 1 : 0
+        annotation.isHidden = (!rawAnnotation.flags || rawAnnotation.flags === 'invisible') ? 1 : 0
         break
       case 'checkbox':
       case 'radiobutton':
@@ -713,7 +719,7 @@ class ComPDFKitViewer {
         annotation.borderWidth = (rawAnnotation.width && round(rawAnnotation.width, 2)) || 1
         rawAnnotation['border-color'] && (annotation.borderColor = rawAnnotation['border-color'])
         annotation.fieldName = rawAnnotation.fieldname
-        annotation.isHidden = (!rawAnnotation.flag || rawAnnotation.flag === 'invisible') ? 1 : 0
+        annotation.isHidden = (!rawAnnotation.flags || rawAnnotation.flags === 'invisible') ? 1 : 0
         annotation.isChecked = rawAnnotation.value === 'Yes' ? 1 : 0
         annotation.checkStyle = Number(rawAnnotation.styleCA)
         break
@@ -732,7 +738,7 @@ class ComPDFKitViewer {
         annotation.isBold = rawAnnotation.textStyle.blod === 'true'
         annotation.isItalic = rawAnnotation.textStyle.italic === 'true'
         annotation.isMultiLine = rawAnnotation.textStyle.multiLine === 'true' ? 1 : 0
-        annotation.isHidden = !rawAnnotation.flag && rawAnnotation.flag === 'invisible' ? 1 : 0
+        annotation.isHidden = !rawAnnotation.flags && rawAnnotation.flags === 'invisible' ? 1 : 0
         annotation.items = this.#getItems(rawAnnotation)
         annotation.selected = round(rawAnnotation.select, 0)
         break
@@ -764,7 +770,7 @@ class ComPDFKitViewer {
         annotation.isBold = rawAnnotation.textStyle.blod === 'true'
         annotation.isItalic = rawAnnotation.textStyle.italic === 'true'
         annotation.title = rawAnnotation.styleCA
-        annotation.isHidden = !rawAnnotation.flag && rawAnnotation.flag === 'invisible' ? 1 : 0
+        annotation.isHidden = !rawAnnotation.flags && rawAnnotation.flags === 'invisible' ? 1 : 0
         break
     }
 
@@ -2194,9 +2200,9 @@ class ComPDFKitViewer {
   async setToolMode(mode) {
     const oldMode = this.toolMode;
     this.toolMode = mode;
-    // if (mode === 'editor' && !this.fontFileInited) {
-    //   await this.initFontFile()
-    // }
+    if (mode === 'editor' && !this.fontFileInited) {
+      await this.initFontFile()
+    }
     this.eventBus.dispatch("toolModeChanged", mode);
 
     if (oldMode === 'editor' && this.toolMode !== 'editor') {
@@ -2496,10 +2502,7 @@ class ComPDFKitViewer {
 
   async initFontFile() {
     try {
-      const response = await fetch('/lib/DroidSansFallbackFull.ttf')
-      const blob = await response.blob()
-      const file = new File([blob], 'DroidSansFallbackFull.ttf', { type: blob.type })
-      this.pdfViewer.setFontFile(file)
+      this.pdfViewer.setFontFile(this.#fonFile)
       this.fontFileInited = true
     } catch (error) {
       console.log(error)

+ 2 - 2
packages/webview/src/components/EditTextPanel/EditTextPanel.vue

@@ -98,7 +98,7 @@
       class: 'edit-select'
     }
   })
-  const fontFamilyArr = ['Helvetica', 'Courier', 'Times-Roman']
+  const fontFamilyArr = ['Helvetica', 'Courier', 'Times-Roman', 'DroidSansFallbackFull']
   const fontFamilyOptions = fontFamilyArr.map(fontFamily => {
     return {
       label: fontFamily,
@@ -437,4 +437,4 @@
       color: var(--c-text);
     }
   }
-</style>
+</style>