mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 13:56:32 +08:00
This commit is contained in:
1 parent
eac5a659e6
commit
6b0332224f
10 files changed
+201
-20
No files matched your search
+22
-8
@@ -4304,12 +4304,7 @@ async function boot() {
|
||||
}).catch((err) => showError(err, !state.doc));
|
||||
});
|
||||
}
|
||||
if (nativeHost) await nativeHost.start(files => {
|
||||
if (document.body.hasAttribute("data-busy")) { setStatus("请等待当前操作完成"); return; }
|
||||
if (batchElements.Panel.open) addBatchFiles(files);
|
||||
else if (files.length === 1) openOFD(files[0]);
|
||||
else if (files.length) setStatus("请选择一个文档");
|
||||
}, error => showError(error, !state.doc));
|
||||
if (nativeHost) await nativeHost.start(openNativeFiles, error => showError(error, !state.doc));
|
||||
try {
|
||||
const remote = remoteDocumentOptions(window.location.hash);
|
||||
if (remote && openSeq === state.openSeq) await openOFD({ name: remote.name }, remote);
|
||||
@@ -4326,6 +4321,21 @@ async function boot() {
|
||||
}
|
||||
}
|
||||
|
||||
async function openNativeFiles(files) {
|
||||
let retained = [];
|
||||
try {
|
||||
if (document.body.hasAttribute("data-busy") || batchElements.Panel.open && batch.running) { setStatus("请等待当前操作完成"); return; }
|
||||
if (batchElements.Panel.open) {
|
||||
retained = files.filter(isDocumentFile);
|
||||
addBatchFiles(files);
|
||||
} else if (files.length === 1) {
|
||||
if (await openOFD(files[0])) retained = files;
|
||||
} else if (files.length) setStatus("请选择一个文档");
|
||||
} finally {
|
||||
await nativeHost.discardFiles(files.filter(file => !retained.includes(file)));
|
||||
}
|
||||
}
|
||||
|
||||
async function registerOffline() {
|
||||
const scope = new URL("./", location.href).href;
|
||||
let registration = await navigator.serviceWorker.getRegistration(scope);
|
||||
@@ -4804,6 +4814,7 @@ async function openOFD(file, remote = null, handle = file?.handle) {
|
||||
state.wasmRecoveries = 0;
|
||||
const openSeq = ++state.openSeq;
|
||||
setBusy(true, "正在读取文档", 10, STATUS.opening);
|
||||
let accepted = false;
|
||||
try {
|
||||
const downloaded = remote ? await downloadRemoteDocument(remote) : null;
|
||||
if (openSeq !== state.openSeq) return;
|
||||
@@ -4843,6 +4854,7 @@ async function openOFD(file, remote = null, handle = file?.handle) {
|
||||
if (openSeq !== state.openSeq) return;
|
||||
state.ofdBytes = bytes;
|
||||
state.saveHandle = format === "ofd" && !remote ? handle || null : null;
|
||||
accepted = true;
|
||||
state.saveStamp = state.saveHandle && !nativeHost ? {size: file.size, modified: file.lastModified} : null;
|
||||
state.conversionWarnings = warnings;
|
||||
state.fileName = (name || "ofdgo.ofd").replace(/\.pdf$/i, ".ofd");
|
||||
@@ -4893,6 +4905,7 @@ async function openOFD(file, remote = null, handle = file?.handle) {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
return accepted;
|
||||
}
|
||||
|
||||
function isDocumentFile(file) {
|
||||
@@ -12186,10 +12199,11 @@ function fitPageScale(page, mode) {
|
||||
}
|
||||
const layoutWidth = state.pageLayout > 1 ? Math.max(width, contentWidth) : width;
|
||||
const scrollbar = scrollbarWidth();
|
||||
const viewport = el.viewerPanel.getBoundingClientRect();
|
||||
let vertical = false, horizontal = false, scale = 1;
|
||||
for (let pass = 0; pass < 3; pass++) {
|
||||
const availableWidth = Math.max(1, el.viewerPanel.offsetWidth - (vertical ? scrollbar : 0));
|
||||
const availableHeight = Math.max(1, el.viewerPanel.offsetHeight - (horizontal ? scrollbar : 0));
|
||||
const availableWidth = Math.max(1, viewport.width - (vertical ? scrollbar : 0));
|
||||
const availableHeight = Math.max(1, viewport.height - (horizontal ? scrollbar : 0));
|
||||
scale = Math.min(4, Math.max(1, availableWidth - space * 2 - gap) / layoutWidth);
|
||||
if (mode === "height") scale = Math.min(scale, Math.max(1, availableHeight - space * 2) / height);
|
||||
const blockSpace = state.continuous ? 0 : Math.max(space, (availableHeight - height * scale) / 2);
|
||||
|
||||
@@ -529,13 +529,20 @@ class NativeHost {
|
||||
}
|
||||
|
||||
async start(onFiles, onError) {
|
||||
this.bridge.on("ofdgo.files", values => onFiles(values.map(value => new NativeFile(value))));
|
||||
this.bridge.on("ofdgo.files", async values => {
|
||||
try { await onFiles(values.map(value => new NativeFile(value))); }
|
||||
catch (error) { onError(error); }
|
||||
});
|
||||
this.bridge.on("ofdgo.error", message => onError(new Error(message)));
|
||||
const title = document.querySelector("title");
|
||||
if (title) new MutationObserver(() => this.call("title", {title:document.title}).catch(onError)).observe(title, {childList:true});
|
||||
await this.call("ready", {});
|
||||
}
|
||||
|
||||
async discardFiles(files) {
|
||||
await Promise.all(files.filter(file => file.drop && file.nativeFile).map(file => this.call("drop.release", {file:file.nativeFile})));
|
||||
}
|
||||
|
||||
installFileInputs(onError) {
|
||||
document.addEventListener("click", async event => {
|
||||
const input = event.target;
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2025-2026 肖其顿 (XIAO QI DUN)
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build linux && amd64
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/xiaoqidun/ofdgo/internal/webview"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dataDir := flag.String("data-dir", "", "browser data directory")
|
||||
flag.Parse()
|
||||
if err := webview.Run(context.Background(), flag.Args(), *dataDir); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !windows || !amd64
|
||||
//go:build (!windows && !linux) || !amd64
|
||||
|
||||
package main
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ require (
|
||||
github.com/go-text/typesetting v0.3.5
|
||||
github.com/tdewolff/canvas v0.0.0-20260923214215-09804640d00c
|
||||
github.com/tdewolff/font v0.0.0-20261010204837-88a7f6b56918
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261019033038-6971953317bb
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261020023410-769328036c38
|
||||
github.com/xiaoqidun/j2kgo v1.0.2
|
||||
github.com/xiaoqidun/jbig2 v1.1.8
|
||||
github.com/xiaoqidun/pdfgo v0.0.0-20261018110224-069b0fe96ea8
|
||||
@@ -26,6 +26,7 @@ require (
|
||||
github.com/ByteArena/poly2tri-go v0.0.0-20170716161910-d102ad91854f // indirect
|
||||
github.com/benoitkugler/textlayout v0.3.2 // indirect
|
||||
github.com/benoitkugler/textprocessing v0.0.6 // indirect
|
||||
github.com/ebitengine/purego v0.11.2 // indirect
|
||||
github.com/go-fonts/latin-modern v0.3.3 // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect
|
||||
|
||||
@@ -28,6 +28,8 @@ github.com/benoitkugler/textlayout v0.3.2/go.mod h1:o+1hFV+JSHBC9qNLIuwVoLedERU7
|
||||
github.com/benoitkugler/textlayout-testdata v0.1.1/go.mod h1:i/qZl09BbUOtd7Bu/W1CAubRwTWrEXWq6JwMkw8wYxo=
|
||||
github.com/benoitkugler/textprocessing v0.0.6 h1:obkMyj62GEPg3xUVYqROlCN22z1OleuZm6ULqX9Om1g=
|
||||
github.com/benoitkugler/textprocessing v0.0.6/go.mod h1:Io0gN08/PXEzrSOWFa88xHx2Xv3VjvLMY7H76YoI23A=
|
||||
github.com/ebitengine/purego v0.11.2 h1:WWiTx8rD8zYdOwSj8O/NiNbsOvSAgJMTlyV1XlbUSeU=
|
||||
github.com/ebitengine/purego v0.11.2/go.mod h1:DCHPP08djqhNSoTfImcnHYQRZmd0qhakvrozqaEYhGQ=
|
||||
github.com/emmansun/gmsm v0.45.0 h1:PU/6ObyR3g05YN3jn1L5GZDowzyAi8Xhkq4svzYNyiA=
|
||||
github.com/emmansun/gmsm v0.45.0/go.mod h1:HgLSvmdePGLo+Fo2PNgdTQU6lqAepSyriSQJRpECY6w=
|
||||
github.com/go-fonts/latin-modern v0.3.3 h1:g2xNgI8yzdNzIVm+qvbMryB6yGPe0pSMss8QT3QwlJ0=
|
||||
@@ -58,8 +60,8 @@ github.com/tdewolff/parse/v2 v2.8.16 h1:bLk5svUOQRkW/Y2SJ+DeENSIkZBcTIkq+Atyv5D8
|
||||
github.com/tdewolff/parse/v2 v2.8.16/go.mod h1:XdsoSFThlVIRIajAuqz1evNY7bagZS8LBOPA3aVopwQ=
|
||||
github.com/tdewolff/test v1.0.12 h1:7F21DqIajswxuche0geHdrUZRCWE4oko4b7bcmkkrxk=
|
||||
github.com/tdewolff/test v1.0.12/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261019033038-6971953317bb h1:MvkeDfZFJvEM5SG+3KM/gOak2sO7WE541ZoKghhcCEY=
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261019033038-6971953317bb/go.mod h1:QAHK2Sa/KvY6dO+6dtvVUeOXVQO4Zh8R5oSSU+av7kE=
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261020023410-769328036c38 h1:8DQijibcXKHB8EZvGlQ1AfscgH5hThCeIprmPjH6QKc=
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261020023410-769328036c38/go.mod h1:xJF8xE6Bjw+NcOZNvWJEVX/XXIkngs3ltc9F6vNcLls=
|
||||
github.com/xiaoqidun/j2kgo v1.0.2 h1:OAjjEOfaBaQzBNcj9wx+eM68UI5pewR6SzNZa1R1DIw=
|
||||
github.com/xiaoqidun/j2kgo v1.0.2/go.mod h1:DCwAfuDzCDYqUiAlZ7BDwigTjaXw7o9L1FtEma/D1aA=
|
||||
github.com/xiaoqidun/jbig2 v1.1.8 h1:dERPW7F8gs18a/DOD9gXHR3fP1Vl6KhCNsLnq5l3BIo=
|
||||
|
||||
@@ -92,7 +92,7 @@ func Run(ctx context.Context, paths []string, dataDir string) error {
|
||||
h.conversionPool = h.resources.ConversionPool()
|
||||
defer h.close()
|
||||
bindings := make(map[string]guigo.HandlerFunc)
|
||||
for _, name := range []string{"engine.open", "engine.close", "engine.call", "engine.start", "engine.cancel", "engine.finish", "batch", "open", "save", "directory", "file", "release", "child", "remove", "write.begin", "write.data", "write.commit", "write.abort", "fonts", "ready", "title"} {
|
||||
for _, name := range []string{"engine.open", "engine.close", "engine.call", "engine.start", "engine.cancel", "engine.finish", "batch", "open", "save", "directory", "file", "release", "drop.release", "child", "remove", "write.begin", "write.data", "write.commit", "write.abort", "fonts", "ready", "title"} {
|
||||
bindings["ofdgo.host."+name] = func(ctx context.Context, args any) (any, error) { return h.call(ctx, name, args) }
|
||||
}
|
||||
static := http.FileServerFS(webuiassets.NativeFS)
|
||||
@@ -248,17 +248,22 @@ func (h *Host) emit(name string, value any) {
|
||||
func (h *Host) openPaths(names []string) {
|
||||
files := make([]any, 0, len(names))
|
||||
for _, name := range names {
|
||||
handle, err := h.files.add(name, false)
|
||||
file, err := h.files.addDrop(name)
|
||||
if err != nil {
|
||||
h.emit("ofdgo.error", err.Error())
|
||||
continue
|
||||
}
|
||||
file, err := h.files.file(handle["nativeHandle"].(string))
|
||||
if err == nil {
|
||||
files = append(files, file)
|
||||
}
|
||||
h.mu.Lock()
|
||||
w := h.window
|
||||
h.mu.Unlock()
|
||||
if w != nil && w.Emit("ofdgo.files", files) == nil {
|
||||
return
|
||||
}
|
||||
for _, file := range files {
|
||||
h.files.discardDrop(file.(map[string]any)["nativeFile"].(string))
|
||||
}
|
||||
h.emit("ofdgo.files", files)
|
||||
}
|
||||
|
||||
// call 执行宿主协议并保留业务结果结构
|
||||
@@ -455,6 +460,9 @@ func (h *Host) call(ctx context.Context, name string, value any) (any, error) {
|
||||
case "release":
|
||||
h.files.release(text("file"))
|
||||
return nil, nil
|
||||
case "drop.release":
|
||||
h.files.discardDrop(text("file"))
|
||||
return nil, nil
|
||||
case "child":
|
||||
value, err := h.files.child(text("handle"), text("name"), flag("directory"), flag("create"))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
@@ -819,7 +827,11 @@ func windowOptions(dataDir string) (guigo.Options, error) {
|
||||
if err != nil {
|
||||
return guigo.Options{}, err
|
||||
}
|
||||
dataDir = filepath.Join(cache, "OFDGo")
|
||||
name := "ofdgo"
|
||||
if runtime.GOOS == "windows" {
|
||||
name = "OFDGo"
|
||||
}
|
||||
dataDir = filepath.Join(cache, name)
|
||||
}
|
||||
dataDir, err := filepath.Abs(dataDir)
|
||||
if err != nil {
|
||||
|
||||
@@ -47,6 +47,7 @@ type fileEntry struct {
|
||||
saveTarget ofdgo.Output
|
||||
readers int
|
||||
retired bool
|
||||
dropHandle string
|
||||
}
|
||||
|
||||
// fileStore 管理窗口范围内的文件授权和临时数据
|
||||
@@ -137,6 +138,59 @@ func (s *fileStore) add(name string, directory bool) (map[string]any, error) {
|
||||
return s.descriptor(id), nil
|
||||
}
|
||||
|
||||
// addDrop 登记拖放文件及读取快照,失败时撤销本次授权
|
||||
// 入参: name 拖放文件路径
|
||||
// 返回: map[string]any 文件描述, error 登记错误
|
||||
func (s *fileStore) addDrop(name string) (map[string]any, error) {
|
||||
handle, err := s.add(name, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id := handle["nativeHandle"].(string)
|
||||
file, err := s.file(id)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if err == nil && !s.closed {
|
||||
if entry := s.entries[file["nativeFile"].(string)]; entry != nil && s.entries[id] != nil {
|
||||
entry.dropHandle = id
|
||||
file["drop"] = true
|
||||
return file, nil
|
||||
}
|
||||
err = os.ErrNotExist
|
||||
}
|
||||
delete(s.entries, id)
|
||||
if file != nil {
|
||||
key := file["nativeFile"].(string)
|
||||
if entry := s.entries[key]; entry != nil {
|
||||
delete(s.entries, key)
|
||||
if entry.file != nil {
|
||||
entry.file.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
err = os.ErrClosed
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// discardDrop 撤销未接收的拖放文件及其授权,不删除源文件
|
||||
// 入参: id 拖放读取快照标识
|
||||
func (s *fileStore) discardDrop(id string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
entry := s.entries[id]
|
||||
if entry == nil || entry.dropHandle == "" {
|
||||
return
|
||||
}
|
||||
delete(s.entries, entry.dropHandle)
|
||||
delete(s.entries, id)
|
||||
entry.retired = true
|
||||
if entry.file != nil && entry.readers == 0 {
|
||||
entry.file.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// save 登记用户选择的输出路径
|
||||
// 入参: name 输出文件路径
|
||||
// 返回: map[string]any 句柄描述
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2025-2026 肖其顿 (XIAO QI DUN)
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package webview
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// conversionMemory 未提供系统内存探测时仅按并行数调度
|
||||
var conversionMemory func() int64
|
||||
|
||||
// fontNameLanguage 获取Linux区域设置对应的OpenType名称表标识
|
||||
// 返回: uint16 名称表平台标识, uint16 显示语言标识
|
||||
func fontNameLanguage() (uint16, uint16) {
|
||||
locale := ""
|
||||
for _, name := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
|
||||
if locale = os.Getenv(name); locale != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
locale = strings.ToLower(strings.ReplaceAll(locale, "-", "_"))
|
||||
locale, _, _ = strings.Cut(locale, ".")
|
||||
locale, _, _ = strings.Cut(locale, "@")
|
||||
switch locale {
|
||||
case "zh", "zh_cn", "zh_hans":
|
||||
return 3, 0x0804
|
||||
case "zh_hk":
|
||||
return 3, 0x0c04
|
||||
case "zh_mo":
|
||||
return 3, 0x1404
|
||||
case "zh_tw", "zh_hant":
|
||||
return 3, 0x0404
|
||||
case "zh_sg":
|
||||
return 3, 0x1004
|
||||
case "ja", "ja_jp":
|
||||
return 3, 0x0411
|
||||
case "ko", "ko_kr":
|
||||
return 3, 0x0412
|
||||
case "c", "posix", "en", "en_us":
|
||||
return 3, 0x0409
|
||||
}
|
||||
return 0xffff, 0xffff
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !windows
|
||||
//go:build !windows && !linux
|
||||
|
||||
package webview
|
||||
|
||||
|
||||
Reference in new issue
Block a user