Skip to content

Commit

Permalink
Move and refactor functions for PDF rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
kosude committed Jun 10, 2024
1 parent 9aac48b commit f63406c
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 42 deletions.
4 changes: 3 additions & 1 deletion texedit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ set(SRCS
"gui/layout/panes/preview_pane.cpp"
"gui/layout/layout_manager.cpp"
"gui/pdf_viewer/pdf_canvas.cpp"
"gui/pdf_viewer/pdf_document.cpp"
"gui/main_frame.cpp"
"gui/prog_info.cpp"

"pdf_render/pdf_document.cpp"
"pdf_render/poppler_log.cpp"

"process/services/compiler_process.cpp"
"process/process_manager.cpp"
"process/process.cpp"
Expand Down
4 changes: 3 additions & 1 deletion texedit/gui/layout/panes/preview_pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "except.hpp"

#include "pdf_render/pdf_document.hpp"

namespace te::gui {
PreviewPane::PreviewPane(wxWindow *parent) : PaneBase(parent) {
_canvas = new PDFCanvas(this);
Expand All @@ -31,7 +33,7 @@ namespace te::gui {
if (_document) {
delete _document;
}
_document = new PDFDocument(path);
_document = new pdfr::PDFDocument(path);

// RenderDocument() stores the new document's rendered images into the canvas object to be drawn later
_canvas->RenderDocument(_document);
Expand Down
6 changes: 5 additions & 1 deletion texedit/gui/layout/panes/preview_pane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "pane_base.hpp"
#include "gui/pdf_viewer/pdf_canvas.hpp"

namespace te::pdfr {
class PDFDocument;
}

namespace te::gui {
class PreviewPane : public PaneBase {
public:
Expand All @@ -26,7 +30,7 @@ namespace te::gui {
private:
wxBoxSizer *_sizer;

PDFDocument *_document{nullptr};
pdfr::PDFDocument *_document{nullptr};
PDFCanvas *_canvas;
};
}
Expand Down
6 changes: 3 additions & 3 deletions texedit/gui/main_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#ifndef __texedit__root_frame_hpp__
#define __texedit__root_frame_hpp__

#include <wx/wx.h>
#include <wx/treebase.h>

#include "process/process_manager.hpp"
#include "process/services/compiler_process.hpp"
#include "layout/layout_manager.hpp"
#include "util/logger.hpp"

#include <wx/wx.h>
#include <wx/treebase.h>

namespace te::gui {
class MainFrame : public wxFrame {
public:
Expand Down
18 changes: 3 additions & 15 deletions texedit/gui/pdf_viewer/pdf_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

#include "pdf_canvas.hpp"

#include <wx/dcclient.h>
#include <wx/graphics.h>
#include <poppler-page.h>
#include "pdf_render/pdf_document.hpp"

#include <wx/rawbmp.h>
#include <wx/dcclient.h>

namespace te::gui {
PDFCanvas::PDFCanvas(wxWindow *parent) : wxScrolledWindow{parent, wxID_ANY} {
Expand All @@ -20,7 +18,7 @@ namespace te::gui {
SetScrollbars(5, 50, 0, 100);
}

void PDFCanvas::RenderDocument(const PDFDocument *doc) {
void PDFCanvas::RenderDocument(const pdfr::PDFDocument *doc) {
for (wxImage *img : _page_images) {
delete img;
}
Expand All @@ -41,16 +39,6 @@ namespace te::gui {

yi++;
}

// wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
// if (gc) {
// gc->SetPen(*wxGREY_PEN);
// gc->SetBrush(*wxLIGHT_GREY_BRUSH);
// gc->DrawRectangle(10, 10, 50, 500);
// gc->DrawEllipse(70, 10, 50, 700);

// delete gc;
// }
}

wxBEGIN_EVENT_TABLE(PDFCanvas, wxScrolledWindow)
Expand Down
8 changes: 5 additions & 3 deletions texedit/gui/pdf_viewer/pdf_canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
#ifndef __texedit__pdf_canvas_hpp__
#define __texedit__pdf_canvas_hpp__

#include "pdf_document.hpp"

#include <wx/scrolwin.h>
#include <wx/image.h>
#include <vector>

namespace te::pdfr {
class PDFDocument;
}

namespace te::gui {
class PDFCanvas : public wxScrolledWindow {
public:
PDFCanvas(wxWindow *parent);

void RenderDocument(const PDFDocument *doc);
void RenderDocument(const pdfr::PDFDocument *doc);

void OnPaint(wxPaintEvent &event);

Expand Down
4 changes: 2 additions & 2 deletions texedit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "main.hpp"

#include "gui/main_frame.hpp"
#include "gui/pdf_viewer/pdf_document.hpp"
#include "pdf_render/poppler_log.hpp"

wxIMPLEMENT_APP(te::Application);

Expand Down Expand Up @@ -59,7 +59,7 @@ namespace te {
// TODO: command-line parsing here

// initialise logging for PDF document processing
gui::PDFDocument::CaptureRuntimeLogging();
pdfr::CaptureRuntimeLogging();

// show root (editor) window
gui::MainFrame *main_frame = new gui::MainFrame();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <wx/log.h>

namespace te::gui {
namespace te::pdfr {
PDFDocument::PDFDocument(const wxString &path) : _path{path} {
_renderer.set_render_hints(
poppler::page_renderer::antialiasing |
Expand Down Expand Up @@ -72,12 +72,4 @@ namespace te::gui {

return ret;
}

void PDFDocument::CaptureRuntimeLogging() {
poppler::set_debug_error_function(PDFDocument::PopplerDebugFunc, nullptr);
}

void PDFDocument::PopplerDebugFunc(const std::string &msg, void *closure) {
wxLogError("poppler: " + wxString{msg});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

#pragma once
#ifndef __pdf_viewer__pdf_document_hpp__
#define __pdf_viewer__pdf_document_hpp__
#ifndef __texedit__pdf_document_hpp__
#define __texedit__pdf_document_hpp__

#include <vector>

Expand All @@ -16,7 +16,7 @@
#include <poppler-document.h>
#include <poppler-page-renderer.h>

namespace te::gui {
namespace te::pdfr {
/**
* An abstraction over a `libpoppler` PDF document and associated functionality.
*/
Expand All @@ -27,17 +27,13 @@ namespace te::gui {
// Render all pages in the document into a vector of wxImages (note they must be freed after this).
std::vector<wxImage *> RenderAll() const;

static void CaptureRuntimeLogging();

private:
const wxString &_path;

poppler::page_renderer _renderer;

poppler::document *_doc;
std::vector<poppler::page *> _pages;

static void PopplerDebugFunc(const std::string &msg, void *closure);
};
}

Expand Down
22 changes: 22 additions & 0 deletions texedit/pdf_render/poppler_log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024 Jack Bennett.
* All Rights Reserved.
*
* See the LICENCE file for more information.
*/

#include "poppler_log.hpp"

#include <wx/string.h>
#include <wx/log.h>
#include <poppler-global.h>

namespace te::pdfr {
static void PopplerDebugFunc(const std::string &msg, void *closure) {
wxLogError("poppler: " + wxString{msg});
}

void CaptureRuntimeLogging() {
poppler::set_debug_error_function(PopplerDebugFunc, nullptr);
}
}
16 changes: 16 additions & 0 deletions texedit/pdf_render/poppler_log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2024 Jack Bennett.
* All Rights Reserved.
*
* See the LICENCE file for more information.
*/

#pragma once
#ifndef __texedit__poppler_log_hpp__
#define __texedit__poppler_log_hpp__

namespace te::pdfr {
void CaptureRuntimeLogging();
}

#endif

0 comments on commit f63406c

Please sign in to comment.