Skip to content

Commit

Permalink
Fix LaTeX bugs. Batch LaTeX rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4burns committed Jan 13, 2013
1 parent 859db58 commit b5ea016
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/QCViewer/QCLib/circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ unsigned int Circuit::depth()
shared_ptr<Subcircuit> s = dynamic_pointer_cast<Subcircuit>(gates.at(i));
if (s) {
depth += s->getCircuit()->depth()-1;
}
}
}
depth += getParallel().size() + 1;
depth += getParallel().size() + 1;
return depth;
}

Expand Down
6 changes: 6 additions & 0 deletions src/QCViewer/QCLib/circuitImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ void CircuitImage::drawbase (Circuit &c, double w, double h, double wirestart, d
vector<gateRect> CircuitImage::drawCirc (Circuit &c, double &wirestart, double &wireend, bool forreal)
{
vector <gateRect> rects;

// prep text
if(!forreal) textEngine.beginBatch();

// input labels
double xinit = 0.0;
if (op.drawLineLabels) {
Expand Down Expand Up @@ -173,6 +177,8 @@ vector<gateRect> CircuitImage::drawCirc (Circuit &c, double &wirestart, double &
addText(label,x,y);
}
}

if(!forreal) textEngine.endBatch();
return rects;
}

Expand Down
78 changes: 51 additions & 27 deletions src/QCViewer/QCLib/pdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,9 @@
#include <cstdlib>
#include "pdf.h"

PopplerContainer::~PopplerContainer()
{
g_object_unref(page);
g_object_unref(document);
}

#define LATEX_SCALEF 2.5

void PopplerContainer::draw(cairo_t* cr)
{
assert(page != NULL);
assert(cr != NULL);
cairo_scale(cr, LATEX_SCALEF, LATEX_SCALEF);
poppler_page_render(page, cr);
cairo_scale(cr, 1.0/LATEX_SCALEF, 1.0/LATEX_SCALEF);
}

PopplerContainer::PopplerContainer(const char* filename, double& width, double& height)
PDFReader::PDFReader(const char * filename)
{
GError* error = NULL;
gchar* absolute;
Expand All @@ -37,30 +22,69 @@ PopplerContainer::PopplerContainer(const char* filename, double& width, double&
uri = g_filename_to_uri(absolute, NULL, &error);
free(absolute);
if(uri == NULL) {
std::string msg = "Error loading PDF: foo";
throw msg;
throw std::string("PDFReader: Couldn't convert PDF filename to URI.");
}

document = poppler_document_new_from_file(uri, NULL, &error);

if(document == NULL) {
std::string msg = "Error loading PDF: foo";
throw msg;
throw std::string("PDFReader: Couldn't open PDF document.");
}

int num_pages = poppler_document_get_n_pages(document);
if(num_pages != 1) {
std::string msg = "Error loading PDF: more than 1 page.";
throw msg;
pages.reserve(num_pages);

for(int i=0; i<num_pages; i++) {
PopplerPage * page = poppler_document_get_page(document, i);
if(page == NULL) {
throw std::string("PDFReader: Error loading PDF: page not found.");
}
pages.push_back(std::shared_ptr<PopplerContainer>(new PopplerContainer(page)));
}
}

PDFReader::~PDFReader()
{
g_object_unref(document);
}

uint32_t PDFReader::getNumPages()
{
return pages.size();
}

page = poppler_document_get_page(document, 0);
if(page == NULL) {
std::string msg = "Error loading PDF: page not found";
throw msg;
std::shared_ptr<PopplerContainer>
PDFReader::getPage(int n)
{
std::shared_ptr<PopplerContainer> page = pages.at(n);
if(!page) {
throw std::string("PDFReader: Requested a nonexistent page.");
}
return page;
}

PopplerContainer::~PopplerContainer()
{
g_object_unref(page);
}

void PopplerContainer::getPageDimensions(double &width, double &height)
{
poppler_page_get_size(page, &width, &height);
width *= LATEX_SCALEF;
height *= LATEX_SCALEF;
}

void PopplerContainer::draw(cairo_t* cr)
{
assert(page != NULL);
assert(cr != NULL);
cairo_scale(cr, LATEX_SCALEF, LATEX_SCALEF);
poppler_page_render(page, cr);
cairo_scale(cr, 1.0/LATEX_SCALEF, 1.0/LATEX_SCALEF);
}

PopplerContainer::PopplerContainer(PopplerPage * pg)
{
page = pg;
}
22 changes: 20 additions & 2 deletions src/QCViewer/QCLib/pdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@
#include <cairo.h>
#include <poppler.h>
#include <string>
#include <vector>
#include <memory>

class PDFReader;

class PopplerContainer
{
public:
PopplerContainer(const char*, double&, double&);
~PopplerContainer();
void getPageDimensions(double&, double&);
void draw(cairo_t* cr);

private:
PopplerDocument* document;
PopplerContainer(PopplerPage * pg);
PopplerPage* page;
friend class PDFReader;
};

class PDFReader
{
public:
PDFReader(const char*);
~PDFReader();
uint32_t getNumPages();
std::shared_ptr<PopplerContainer> getPage(int);

private:
std::vector<std::shared_ptr<PopplerContainer> > pages;
PopplerDocument* document;
};

#endif
Loading

0 comments on commit b5ea016

Please sign in to comment.