Skip to content

Commit

Permalink
drawImage / update podofo
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonov548 committed Jun 1, 2024
1 parent 3d3f065 commit 4c8f18e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ set(ALL_SOURCES
src/bind.cpp
)

set(PODOFO_BUILD_TEST OFF)
set(PODOFO_BUILD_LIB_ONLY ON)
set(PODOFO_BUILD_STATIC ON)
add_subdirectory(podofo)

Expand Down
2 changes: 1 addition & 1 deletion podofo
Submodule podofo updated 41 files
+4 −4 SOURCE-LAYOUT.md
+0 −2 TODO.md
+1 −1 src/podofo/CMakeLists.txt
+155 −0 src/podofo/auxiliary/Convert.h
+1 −1 src/podofo/main/PdfAnnotation.cpp
+3 −3 src/podofo/main/PdfCanvas.cpp
+3 −3 src/podofo/main/PdfCanvas.h
+82 −532 src/podofo/main/PdfColor.cpp
+14 −135 src/podofo/main/PdfColor.h
+5 −371 src/podofo/main/PdfColorSpace.cpp
+18 −165 src/podofo/main/PdfColorSpace.h
+733 −0 src/podofo/main/PdfColorSpaceFilter.cpp
+268 −0 src/podofo/main/PdfColorSpaceFilter.h
+1 −1 src/podofo/main/PdfContentStreamReader.cpp
+8 −0 src/podofo/main/PdfDocument.cpp
+3 −0 src/podofo/main/PdfDocument.h
+1 −1 src/podofo/main/PdfFontManager.cpp
+5 −3 src/podofo/main/PdfGraphicsState.h
+18 −19 src/podofo/main/PdfImage.cpp
+4 −4 src/podofo/main/PdfImage.h
+2 −2 src/podofo/main/PdfIndirectObjectList.h
+7 −0 src/podofo/main/PdfName.cpp
+7 −0 src/podofo/main/PdfName.h
+147 −83 src/podofo/main/PdfPainter.cpp
+20 −34 src/podofo/main/PdfPainter.h
+110 −55 src/podofo/main/PdfResources.cpp
+22 −12 src/podofo/main/PdfResources.h
+2 −7 src/podofo/podofo.h
+1 −1 src/podofo/private/ImageUtils.cpp
+2 −2 src/podofo/private/ImageUtils.h
+1 −0 src/podofo/private/PdfDeclarationsPrivate.h
+0 −213 src/podofo/staging/PdfPainterExtensions.cpp
+0 −115 src/podofo/staging/PdfPainterExtensions.h
+0 −0 staging/PdfFunction.cpp
+0 −0 staging/PdfFunction.h
+0 −0 staging/PdfShadingPattern.cpp
+0 −0 staging/PdfShadingPattern.h
+0 −0 staging/PdfTilingPattern.cpp
+0 −0 staging/PdfTilingPattern.h
+22 −633 test/unit/ColorTest.cpp
+4 −4 test/unit/ImageTest.cpp
64 changes: 59 additions & 5 deletions src/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,34 @@ void drawText(PoDoFo::PdfPainter& painter, const std::string& text, double x,

namespace Image
{

std::unique_ptr<PoDoFo::PdfImage> makeFromObject([[maybe_unused]] const std::string& key, PoDoFo::PdfObject& object)
{
std::unique_ptr<PoDoFo::PdfImage> image;
PoDoFo::PdfXObject::TryCreateFromObject<PoDoFo::PdfImage>(object, image);

return image;
}

std::unique_ptr<PoDoFo::PdfImage> makeImage(Document::DocumentWrapper& wrapper)
{
return wrapper.document.CreateImage();
}

void loadFromBuffer(PoDoFo::PdfImage& image, const std::vector<char>& buffer)
void loadFromBuffer(PoDoFo::PdfImage& image, em::val jbuffer)
{
const auto buffer{vecFromJSArray<char>(jbuffer)};
image.LoadFromBuffer({buffer.data(), buffer.size()});
}
} // namespace Image

namespace Page
{
PoDoFo::PdfResources* getResources(PoDoFo::PdfPage& page)
{
return page.GetResources();
}

em::val extractText(const PoDoFo::PdfPage& page)
{
std::vector<PoDoFo::PdfTextEntry> es;
Expand Down Expand Up @@ -173,18 +188,39 @@ em::val getPageSize(const PoDoFo::PdfPageSize& page_size,
}
} // namespace Page

namespace Resources
{
em::val getArray(PoDoFo::PdfResources& resources, PoDoFo::PdfResourceType type)
{
em::val objects{em::val::object()};
for (auto& res : resources.GetResourceIterator(type))
objects.set(res.first.GetString(), res.second);

return objects;
}
}

namespace
{
std::vector<char> makeBuffer(em::val jbuffer)
{
return vecFromJSArray<char>(jbuffer);
}

std::string error(std::uintptr_t p)
{
return reinterpret_cast<std::exception*>(p)->what();
}

} // namespace

// clang-format off

EMSCRIPTEN_BINDINGS(PODOFO)
{
em::class_<PoDoFo::PdfObject>("PdfObject")
;

em::class_<PoDoFo::PdfPageCollection>("PageCollection")
.function("getCount", &PoDoFo::PdfPageCollection::GetCount)
.function("getPage", &PageCollection::getPage, em::allow_raw_pointers())
Expand All @@ -201,11 +237,27 @@ EMSCRIPTEN_BINDINGS(PODOFO)
em::class_<PoDoFo::PdfPage, em::base<PoDoFo::PdfCanvas>>("Page")
.function("getRect", &Page::getRect)
.function("extractText", &Page::extractText)
.function("getResources", &Page::getResources, em::allow_raw_pointers())
;

em::class_<PoDoFo::PdfCanvas>("Canvas")
;

em::class_<PoDoFo::PdfResources>("Resources")
.function("getArray", &Resources::getArray)
;

em::enum_<PoDoFo::PdfResourceType>("ResourceType")
.value("Unknown", PoDoFo::PdfResourceType::Unknown)
.value("ExtGState", PoDoFo::PdfResourceType::ExtGState)
.value("ColorSpace", PoDoFo::PdfResourceType::ColorSpace)
.value("Pattern", PoDoFo::PdfResourceType::Pattern)
.value("Shading", PoDoFo::PdfResourceType::Shading)
.value("XObject", PoDoFo::PdfResourceType::XObject)
.value("Font", PoDoFo::PdfResourceType::Font)
.value("Properties", PoDoFo::PdfResourceType::Properties)
;

em::class_<Document::DocumentWrapper>("Document")
.constructor()
.function("loadFromBuffer", &Document::loadFromBuffer)
Expand All @@ -226,6 +278,7 @@ EMSCRIPTEN_BINDINGS(PODOFO)
.function("setCanvas", &PoDoFo::PdfPainter::SetCanvas)
.function("setFont", &Painter::setFont, em::allow_raw_pointers())
.function("drawText", &Painter::drawText)
.function("drawImage", &PoDoFo::PdfPainter::DrawImage)
.function("drawCircle", &PoDoFo::PdfPainter::DrawCircle)
.function("drawLine", &PoDoFo::PdfPainter::DrawLine)
.function("drawCubicBezier", &PoDoFo::PdfPainter::DrawCubicBezier)
Expand All @@ -236,7 +289,10 @@ EMSCRIPTEN_BINDINGS(PODOFO)

em::class_<PoDoFo::PdfImage>("Image")
.constructor(&Image::makeImage)
.constructor(&Image::makeFromObject)
.function("loadFromBuffer", &Image::loadFromBuffer)
.property("width", &PoDoFo::PdfImage::GetWidth)
.property("height", &PoDoFo::PdfImage::GetHeight)
;

em::enum_<PoDoFo::PdfPathDrawMode>("PathDrawMode")
Expand All @@ -249,10 +305,6 @@ EMSCRIPTEN_BINDINGS(PODOFO)

em::function("getPageSize", &Page::getPageSize);

em::class_<std::vector<char>>("Buffer")
.constructor(&makeBuffer)
;

em::enum_<PoDoFo::PdfPageSize>("PageSize")
.value("A0", PoDoFo::PdfPageSize::A0)
.value("A1", PoDoFo::PdfPageSize::A1)
Expand All @@ -265,4 +317,6 @@ EMSCRIPTEN_BINDINGS(PODOFO)
.value("Legal", PoDoFo::PdfPageSize::Legal)
.value("Tabloid", PoDoFo::PdfPageSize::Tabloid)
;

em::function("error", &error);
}
Binary file added tests/test_data/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 25 additions & 1 deletion tests/unit_tests/image.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { readFile } = require('./utils')

describe('podofo.js', () => {
it('jpeg image', async () => {
const Podofo = global.Podofo;
Expand All @@ -7,13 +9,35 @@ describe('podofo.js', () => {
const page = document.getPages()
.createPage(size);

const buffer = readFile('image.jpg');

const image = new Podofo.Image(document);
image.loadFromBuffer(buffer);

const painter = new Podofo.Painter();
painter.setCanvas(page);
painter.drawCircle(100, 500, 20, Podofo.PathDrawMode.Fill);
painter.drawImage(image, 0, 0, 1, 1);
painter.finishDrawing();

const pdf = document.save();

const parsedDocument = new Podofo.Document();
parsedDocument.loadFromBuffer(pdf);

const parsedPage = parsedDocument.getPages().getPage(0);
const resources = parsedPage.getResources();

const objects = resources.getArray(Podofo.ResourceType.XObject);
expect(Object.keys(objects).length).toEqual(1);

for (const key in objects) {
const parsedImage = new Podofo.Image(key, objects[key]);
expect(parsedImage.width).toEqual(image.width);
expect(parsedImage.height).toEqual(image.height);

parsedImage.delete();
}

painter.delete();
image.delete();
document.delete();
Expand Down
9 changes: 7 additions & 2 deletions tests/unit_tests/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const fs = require('fs');
const path = require('path');

const writeToFile = (buffer, name) => {
const writeFile = (buffer, name) => {
const filePath = path.join(__dirname, name);
fs.writeFileSync(filePath, buffer);
}

module.exports = { writeToFile };
const readFile = (name) => {
const filePath = path.join(__dirname, `../test_data/${name}`);
return fs.readFileSync(filePath);
}

module.exports = { writeFile, readFile };

0 comments on commit 4c8f18e

Please sign in to comment.