Skip to content

Commit

Permalink
use mupdf FFI API instead of mupdfbmp API
Browse files Browse the repository at this point in the history
  • Loading branch information
hwhw committed Oct 20, 2014
1 parent 5b3cd63 commit bb2f598
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions spec/unit/koptcontext_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ffi = require("ffi")
local KOPTContext = require("ffi/koptcontext")
local bmpmupdf = require("ffi/mupdfbmp")
local mupdf = require("ffi/mupdf")
local k2pdfopt = ffi.load("libs/libk2pdfopt.so.2")

local sample_pdf = "spec/base/unit/data/Alice.pdf"
Expand Down Expand Up @@ -50,30 +50,30 @@ describe("KOPTContext module", function()
local kc1 = KOPTContext.new()
assert.are.same({kc1.dst.width, kc1.dst.height}, {0, 0})
local kc2 = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc2.dst, ffi.cast("char*", sample_pdf), 1, 300, 8)
mupdf.openDocument(sample_pdf):openPage(1):toBmp(kc2.dst, 300)
kc1:copyDestBMP(kc2)
assert.are_not.same({kc1.dst.width, kc1.dst.height}, {0, 0})
assert.are.same({kc1.dst.width, kc1.dst.height}, {kc2.dst.width, kc2.dst.height})
end)
it("should be used as reflowing context", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", sample_pdf), 2, 300, 8)
mupdf.openDocument(sample_pdf):openPage(2):toBmp(kc.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc)
assert(kc.dst.size_allocated ~= 0)
assert.are_not.same({kc.dst.width, kc.dst.height}, {0, 0})
end)
it("should get larger reflowed page with larger original page", function()
local kc1 = KOPTContext.new()
local kc2 = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc1.src, ffi.cast("char*", sample_pdf), 2, 167, 8)
bmpmupdf:pdffile_to_bmp(kc2.src, ffi.cast("char*", sample_pdf), 2, 300, 8)
mupdf.openDocument(sample_pdf):openPage(2):toBmp(kc1.src, 167)
mupdf.openDocument(sample_pdf):openPage(2):toBmp(kc2.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc1)
k2pdfopt.k2pdfopt_reflow_bmp(kc2)
assert(kc1.dst.height < kc2.dst.height)
end)
it("should get reflowed word boxes", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", sample_pdf), 3, 300, 8)
mupdf.openDocument(sample_pdf):openPage(3):toBmp(kc.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc)
local boxes = kc:getReflowedWordBoxes("dst", 0, 0, kc.dst.width, kc.dst.height)
for i = 1, #boxes do
Expand All @@ -85,7 +85,7 @@ describe("KOPTContext module", function()
end)
it("should get native word boxes", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", sample_pdf), 4, 300, 8)
mupdf.openDocument(sample_pdf):openPage(4):toBmp(kc.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc)
local boxes = kc:getNativeWordBoxes("dst", 0, 0, kc.dst.width, kc.dst.height)
for i = 1, #boxes do
Expand All @@ -97,7 +97,7 @@ describe("KOPTContext module", function()
end)
it("should transform native postion to reflowed position", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", sample_pdf), 5, 300, 8)
mupdf.openDocument(sample_pdf):openPage(5):toBmp(kc.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc)
for j = 0, 800, 100 do
for i = 0, 600, 100 do
Expand All @@ -108,7 +108,7 @@ describe("KOPTContext module", function()
end)
it("should transform reflow postion to native position", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", sample_pdf), 5, 300, 8)
mupdf.openDocument(sample_pdf):openPage(5):toBmp(kc.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc)
for j = 0, 800, 100 do
for i = 0, 600, 100 do
Expand All @@ -119,23 +119,23 @@ describe("KOPTContext module", function()
end)
it("should get OCR word from tesseract OCR engine", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", sample_pdf), 5, 300, 8)
mupdf.openDocument(sample_pdf):openPage(5):toBmp(kc.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc)
local word = kc:getTOCRWord("dst", 280, 40, 100, 40, "data", "eng", 3, 0, 0)
assert.are_same(word, "Alice")
kc:freeOCR()
end)
it("should free dst bitmap after reflowing", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", sample_pdf), 6, 300, 8)
mupdf.openDocument(sample_pdf):openPage(6):toBmp(kc.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc)
assert(kc.dst.size_allocated ~= 0)
kc:free()
assert(kc.dst.size_allocated == 0)
end)
it("should get list of page regions", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", paper_pdf), 1, 300, 8)
mupdf.openDocument(sample_pdf):openPage(1):toBmp(kc.src, 300)
kc.page_width, kc.page_height = kc.src.width, kc.src.height
local regions = kc:getPageRegions()
for i = 1, #regions do
Expand All @@ -146,7 +146,7 @@ describe("KOPTContext module", function()
it("should convert koptcontext to/from table", function()
local kc = KOPTContext.new()
kc:setLanguage("eng")
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", sample_pdf), 6, 300, 8)
mupdf.openDocument(sample_pdf):openPage(6):toBmp(kc.src, 300)
k2pdfopt.k2pdfopt_reflow_bmp(kc)
local kc_table = KOPTContext.totable(kc)
local new_kc = KOPTContext.fromtable(kc_table)
Expand All @@ -165,12 +165,12 @@ describe("KOPTContext module", function()
end)
it("should export src bitmap to PNG file", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", paper_pdf), 1, 300, 8)
mupdf.openDocument(sample_pdf):openPage(1):toBmp(kc.src, 300)
kc:exportSrcPNGFile("/tmp/src.png")
end)
it("should export src bitmap to PNG string", function()
local kc = KOPTContext.new()
bmpmupdf:pdffile_to_bmp(kc.src, ffi.cast("char*", paper_pdf), 1, 300, 8)
mupdf.openDocument(sample_pdf):openPage(1):toBmp(kc.src, 300)
local png = kc:exportSrcPNGString()
assert.truthy(png)
end)
Expand Down

0 comments on commit bb2f598

Please sign in to comment.