Ver código fonte

Merge branch 'feature/text_spacing' into feature/ui_mobile_optimization

wzl 6 meses atrás
pai
commit
180e794e0b

+ 2 - 1
.gitignore

@@ -4,4 +4,5 @@ node_modules
 dist
 lib
 components.d.ts
-fonts
+fonts*
+pso

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

@@ -63,6 +63,8 @@ class ComPDFKitViewer {
     this.color = 'color'
     this.pagesPtr = []
     this.saveAction = null
+    this.enableCustomFont = false
+    this.fontsJson = null
     this.webFontURL = 'https://www.compdf.com/fonts/'
     this.optionUrl = {
       // baseUrl: 'https://test-compdf.kdan.cn',
@@ -272,12 +274,16 @@ class ComPDFKitViewer {
     // this.webviewerServer = true
 
     if (verified) {
+      if (options.enableCustomFont) {
+        this.enableCustomFont = true
+      }
       if (options.webFontURL) {
+        this.enableCustomFont = true
         this.webFontURL = options.webFontURL
       }
-      if (!this.webviewerServer) {
+      if (!this.webviewerServer && this.enableCustomFont) {
         const webFontURL = this.webFontURL
-        
+
         const response = await fetch(webFontURL + 'font.ff')
         if (response.ok) {
           const resData = await response.arrayBuffer()

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

@@ -160,13 +160,14 @@ class CPDFWorker {
 
       if (data.fileList) {
         ComPDFKitJS.opened_Font[0] = data.fileList
-      }
-      if (data.webFontURL !== 'https://www.compdf.com/fonts/') {
-        webFontURL = data.webFontURL
-      }
-      fontsJson = data.fontsJson
 
-      Module._InitComPDFkitFont()
+        if (data.webFontURL !== 'https://www.compdf.com/fonts/') {
+          webFontURL = data.webFontURL
+        }
+        fontsJson = data.fontsJson
+
+        Module._InitComPDFkitFont()
+      }
       const doc = Module._InitDocument()
 
       return doc
@@ -405,7 +406,9 @@ class CPDFWorker {
       const signaturePtr = Module._InitSignature2(annotPtr)
 
       DataArray = []
-      Module._SaveDocumentByStream(doc, 2)
+
+      const embedded = !!fontsJson
+      Module._SaveDocumentByStream(doc, 2, embedded)
 
       const addResult = Module._AddSignSaveDocument(doc, signaturePtr, certPtr)
 
@@ -713,11 +716,9 @@ class CPDFWorker {
     messageHandler.on('GetSelectionForWordAtPos', (data) => {
       const { pagePtr, textPtr, start, end } = data
       Rect = {}
-      Module._GetSelectionForWordAtPos(pagePtr, textPtr, start.x, start.y, end.x, end.y, 5, 5)
       PDFRange = {}
-      Module._GetCharsRangeAtPos(pagePtr, textPtr, Rect.Left, Rect.Top, Rect.Right, Rect.Bottom, 5, 5)
+      const rawTextContent = Module._GetSelectionForWordAtPos(pagePtr, textPtr, start.x, start.y, 5, 5)
 
-      const rawTextContent = Module._GetTextContent(textPtr, PDFRange.Location, PDFRange.Length)
       const textContent = UTF8ToString(rawTextContent)
       TextRectArray = []
       Module._GetRects(pagePtr, textPtr, PDFRange.Location, PDFRange.Length)
@@ -3035,7 +3036,9 @@ function addToTrustedCertificates(data) {
 
 async function copyDocument(doc, password) {
   DataArray = []
-  Module._SaveDocumentByStream(doc, 2);
+
+  const embedded = !!fontsJson
+  Module._SaveDocumentByStream(doc, 2, embedded);
   let pdfData = []
   for (let i = 0; i < DataArray.length; i++) {
     let subArray = new Uint8Array(DataArray[i])
@@ -3058,7 +3061,9 @@ async function copyDocument(doc, password) {
 function saveDocument(doc, saveType) {
   DataArray = []
   saveType === 1 && DataArray.push(ComPDFKitJS.opened_files[0])
-  Module._SaveDocumentByStream(doc, saveType)
+
+  const embedded = !!fontsJson
+  Module._SaveDocumentByStream(doc, saveType, embedded)
   let pdfData = []
   for (let i = 0; i < DataArray.length; i++) {
     let subArray = new Uint8Array(DataArray[i])

+ 27 - 6
packages/core/webviewer.js

@@ -26,6 +26,7 @@ class Viewer {
   }
   constructor (options, element) {
     if (docViewer.get(element)) return
+    this.docId = 0
     docViewer.set(element, true)
     this.instance = null
     this.options = options
@@ -67,6 +68,7 @@ class Viewer {
   }
 
   _create () {
+    this.docId++
     if (!this._trigger) {
       this._trigger = function(e, t) {
         var n = this._createEvent(e, t);
@@ -84,11 +86,11 @@ class Viewer {
     } else {
       webviewerUrl += '#'
     }
+    webviewerUrl += "&docId=".concat(this.docId)
     this.options.showToolbarControl !== undefined && (webviewerUrl += "&header=".concat(this.options.showToolbarControl ? "true": "false"))
     const iframe = document.createElement("iframe")
-    iframe.id = "webviewer"
+    iframe.id = "webviewer-" + this.docId
     iframe.src = webviewerUrl
-    iframe.title = "webviewer"
     iframe.frameBorder = 0
     iframe.width = "100%"
     iframe.height = "100%"
@@ -98,14 +100,33 @@ class Viewer {
     this.iframe = iframe
     this.options?.backgroundColor && iframe.setAttribute("data-bgcolor", this.options.backgroundColor),
     this.options?.assetPath && iframe.setAttribute("data-assetpath", encodeURIComponent(this.options.assetPath)),
+    this.element.appendChild(iframe)
+
     this.loadListener = async function() {
       var $iframe = self.iframe;
       self.instance = $iframe.contentWindow.instance;
       await self.instance.initOptions(self.options)
-      self._trigger("ready");
-    },
-    iframe.addEventListener("load", this.loadListener),
-    this.element.appendChild(iframe)
+    }
+    iframe.addEventListener("load", this.loadListener)
+
+    this.messageHandler = function (event) {
+      if ("documentViewerLoaded" === event.data.type && event.data.docId === self.docId) {
+        try {
+          self.instance = self.iframe.contentWindow.instance
+        } catch(e) {} finally {
+          window.removeEventListener("message", this.messageHandler),
+          self._documentViewerLoaded(self.iframe)
+        }
+      }
+    }
+    window.addEventListener("message", this.messageHandler)
+  }
+
+  _documentViewerLoaded (iframe) {
+    this._trigger("ready")
+    if (this.options.pdfUrl) {
+      this.iframe.contentWindow.instances.UI.loadDocument(this.options.pdfUrl)
+    }
   }
 }
 

+ 4 - 2
packages/webview/src/components/App/index.vue

@@ -1,6 +1,8 @@
 <template>
   <div id="outerContainer" class="app" :class="{ dark: themeMode === 'Dark' }">
-    <Header v-if="!isHeaderDisabled" />
+    <div v-show="!isHeaderDisabled">
+      <Header />
+    </div>
     <div class="content">
       <LeftPanel :isHeaderDisabled="isHeaderDisabled" />
       <DocumentContainer :isHeaderDisabled="isHeaderDisabled" />
@@ -33,7 +35,7 @@
 <script setup>
   import initDocument from '@/helpers/initDocument'
   import getHashParameters from '@/helpers/getHashParameters'
-  import { computed, provide, getCurrentInstance } from 'vue'
+  import { onMounted, computed, provide, getCurrentInstance } from 'vue'
   import { useViewerStore } from '@/stores/modules/viewer'
   import { Sortable, MultiDrag } from 'sortablejs'
   import core from '@/core'

Diferenças do arquivo suprimidas por serem muito extensas
+ 12 - 20
packages/webview/src/components/DocumentContainer/DocumentContainer.vue