From f7cfd56001ac87084e160eaeb57536b0d6c248df Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Wed, 20 Nov 2024 21:20:04 +0100 Subject: [PATCH] kopt: implement reflow Factorized from lower layers' implementations (djvu & mupdf). --- base | 2 +- frontend/document/koptinterface.lua | 44 ++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/base b/base index 8d469790473a..a7c73299566c 160000 --- a/base +++ b/base @@ -1 +1 @@ -Subproject commit 8d469790473a5986db02a4f4535a1a49751b6ff9 +Subproject commit a7c73299566c48e3ceaf8a9bb90fc0f96375eac9 diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 5c349b03365c..aab374709701 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -16,6 +16,7 @@ local TileCacheItem = require("document/tilecacheitem") local Utf8Proc = require("ffi/utf8proc") local logger = require("logger") local util = require("util") +local ffi = require("ffi") local KoptInterface = { ocrengine = "ocrengine", @@ -263,6 +264,32 @@ function KoptInterface:getSemiAutoBBox(doc, pageno) end end +-- lazily load libpthread +local cached_pthread +local function get_pthread() + if cached_pthread then + return cached_pthread + end + local candidates, ok + if ffi.os == "Windows" then + candidates = {"libwinpthread-1.dll"} + elseif FFIUtil.isAndroid() then + -- pthread directives are in Bionic library on Android + candidates = {"libc.so"} + else + -- Kobo devices strangely have no libpthread.so in LD_LIBRARY_PATH + -- so we hardcode the libpthread.so.0 here just for Kobo. + candidates = {"pthread", "libpthread.so.0"} + end + for _, libname in ipairs(candidates) do + ok, cached_pthread = pcall(ffi.load, libname) + if ok then + require("ffi/pthread_h") + return cached_pthread + end + end +end + function KoptInterface:reflowPage(doc, pageno, bbox, background) logger.dbg("reflowing page", pageno, background and "in background" or "in foreground") local kc = self:createContext(doc, pageno, bbox) @@ -270,9 +297,24 @@ function KoptInterface:reflowPage(doc, pageno, bbox, background) kc:setPreCache() self.bg_thread = true end + -- Caculate zoom. + kc.zoom = (1.5 * kc.zoom * kc.quality * kc.dev_width) / bbox.x1 + -- Generate pixmap. local page = doc._document:openPage(pageno) - page:reflow(kc, doc.render_mode) + page:getPagePix(kc, doc.render_mode) page:close() + -- Reflow. + if background then + local pthread = get_pthread() + local rf_thread = ffi.new("pthread_t[1]") + local attr = ffi.new("pthread_attr_t[1]") + pthread.pthread_attr_init(attr) + pthread.pthread_attr_setdetachstate(attr, pthread.PTHREAD_CREATE_DETACHED) + pthread.pthread_create(rf_thread, attr, KOPTContext.k2pdfopt.k2pdfopt_reflow_bmp, ffi.cast("void*", kc)) + pthread.pthread_attr_destroy(attr) + else + KOPTContext.k2pdfopt.k2pdfopt_reflow_bmp(kc) + end return kc end