Skip to content

Commit

Permalink
Can read pdfs now
Browse files Browse the repository at this point in the history
  • Loading branch information
tstruk committed Feb 26, 2012
1 parent c1aea58 commit 22e7bf0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 50 deletions.
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ env.Program(
'moth_book.cpp',
'moth_reader.cpp',
'moth_reader_pdf.cpp' ] )

3 changes: 2 additions & 1 deletion moth_book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ moth_book::moth_book(const std::string& path)
if(type == moth_format_pdf)
{
reader = new moth_reader_pdf(file_name);
current_page = 0;
}
if(type == moth_format_mobi)
{
Expand All @@ -68,7 +69,7 @@ int moth_book::get_pages()
return reader->get_pages();
}

int moth_book::get_page(int number, GdkPixbuf *pixbuff)
int moth_book::get_page(int number, GdkPixbuf *&pixbuff)
{
return reader->get_page(number, pixbuff);
}
Expand Down
14 changes: 13 additions & 1 deletion moth_book.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "moth_reader.h"

class moth_book {
unsigned int current_page;
moth_reader *reader;
std::string file_name;
moth_format_type get_type();
Expand All @@ -31,9 +32,20 @@ class moth_book {
moth_book& operator=(moth_book&);
public:
moth_book(const std::string&);
void set_page(unsigned int const page)
{
if(page >= reader->get_pages())
current_page = 0;
else
current_page = page;
}
unsigned int get_page(void)
{
return current_page;
}
virtual ~moth_book();
int get_pages();
int get_page(int, GdkPixbuf *pixbuff);
int get_page(int, GdkPixbuf *&pixbuff);
int get_page_size(int, double*, double*);
};
#endif
62 changes: 31 additions & 31 deletions moth_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ moth_gui::moth_gui()
running = 1;
}

static int pn = 0;

void moth_gui::handle_resize(SDL_ResizeEvent *resize)
{
width = resize->w;
Expand All @@ -61,10 +59,9 @@ void moth_gui::handle_resize(SDL_ResizeEvent *resize)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, ratio, 1, 1024);
gluLookAt(0.0, 0.0, width / 2.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, width / 2.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
std::cout << "resized to " << width << "x" << height << std::endl;
}

void moth_gui::handle_mouse_motion(SDL_MouseMotionEvent* motion)
Expand All @@ -84,10 +81,10 @@ void moth_gui::handle_key(SDL_keysym *key)
running = 0;
break;
case SDLK_UP:
pn++;
book->set_page(book->get_page() + 1);
break;
case SDLK_DOWN:
pn--;
book->set_page(book->get_page() - 1);
break;
default:
break;
Expand Down Expand Up @@ -130,10 +127,16 @@ int moth_gui::read_book(moth_book *book)
void moth_gui::create_textures()
{
double w, h, wp, hp;
GError *error = NULL;
num_pages = book->get_pages();
book->get_page_size(0, &w, &h);
GdkPixbuf *pixbuff = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
true, 8, w, h);
true, 8, w * 2, h * 2);
if(!pixbuff)
{
std::cerr << "Could not allocate buffer for texture" << std::endl;
throw moth_bad_gui();
}
const char *str0 = "Please wait";
const char *str[] = {"Mapping textures %d%%",
"Mapping textures %d%% .",
Expand All @@ -152,26 +155,26 @@ void moth_gui::create_textures()
{
std::cerr << "Page "<< i <<" has different size " << hp << "x"
<< wp << std::endl;
g_object_unref(pixbuff);
throw moth_bad_pdf();
}
if(SUCCESS == book->get_page(i, pixbuff)){
/* Enhance the picture */
gdk_pixbuf_saturate_and_pixelate(pixbuff, pixbuff, 2.0, 0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w * 2, h * 2, 0, GL_RGBA,
GL_UNSIGNED_BYTE, gdk_pixbuf_get_pixels(pixbuff));
}
else
{
std::cerr << "Could not get texture for page "<< i << std::endl;
throw moth_bad_gui();
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
Expand All @@ -193,40 +196,35 @@ void moth_gui::create_textures()
glPopMatrix();
SDL_GL_SwapBuffers();
}
g_object_unref (pixbuff);
g_object_unref(pixbuff);
}

void moth_gui::draw_screen()
{
/* Assume modelview */
/* Clear the color and depth buffers. */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* We don't want to modify the projection matrix. */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/* Move down the z-axis. */
glPushMatrix();
glTranslatef(0.0, 0.0, -5.0);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, textures[pn]);
glBindTexture(GL_TEXTURE_2D, textures[book->get_page()]);
glBegin(GL_QUADS);

glTexCoord2d(1.0,0.0); glVertex2d(-4.0, -4.0);
glTexCoord2d(0.0,0.0); glVertex2d(-4.0, 4.0);
glTexCoord2d(0.0,1.0); glVertex2d(4.0, 4.0);
glTexCoord2d(1.0,1.0); glVertex2d(4.0, -4.0);
glTexCoord2d(0.0,0.0); glVertex2d(-800.0, 1000.0);
glTexCoord2d(1.0,0.0); glVertex2d(800.0, 1000.0);
glTexCoord2d(1.0,1.0); glVertex2d(800.0, -1000.0);
glTexCoord2d(0.0,1.0); glVertex2d(-800.0, -1000.0);

glEnd();
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glPopMatrix();
SDL_GL_SwapBuffers();
}

void moth_gui::init_opengl()
{
float ratio = (float) width / (float) height;
GLenum err = glewInit();
if(GLEW_OK != err)
{
Expand All @@ -240,12 +238,14 @@ void moth_gui::init_opengl()
std::cerr<< "OpenGL 2.0 or greater is required" << std::endl;
throw moth_bad_ogl();
}
float ratio = (float) width / (float) height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum();
gluPerspective(90, ratio, 1, 1024);
gluLookAt(0.0, 0.0, width / 2.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, width / 2.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void moth_gui::init_video()
Expand Down
2 changes: 2 additions & 0 deletions moth_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class moth_gui {
int flags;
int width;
int height;
double page_width;
double page_height;
int running;
int num_pages;
GLuint *textures;
Expand Down
2 changes: 1 addition & 1 deletion moth_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class moth_reader {
public:
moth_reader();
virtual int get_pages() = 0;
virtual int get_page(int, GdkPixbuf*) = 0;
virtual int get_page(int, GdkPixbuf*&) = 0;
virtual int get_page_size(int, double*, double*) = 0;
};

Expand Down
17 changes: 2 additions & 15 deletions moth_reader_pdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
#include "moth.h"
#include "moth_reader.h"
#include "moth_reader_pdf.h"
//#include <cairo.h>
//#include <gdk/gdk.h>
//extern "C" {
//#include <evince-document.h>
//};

moth_reader_pdf::moth_reader_pdf(const std::string &file)
{
Expand Down Expand Up @@ -77,17 +72,9 @@ int moth_reader_pdf::get_pages()
int moth_reader_pdf::get_page(int num, GdkPixbuf *&pixbuff)
{
double w, h;
static cairo_surface_t *cario;
get_page_size(num, &w, &h);
// cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
// cairo_t *cairo = cairo_create(surface);
// poppler_page_render(pages[num], cairo);
// cairo_set_operator(cairo, CAIRO_OPERATOR_DEST_OVER);
// cairo_set_source_rgb(cairo, 1.0, 1.0, 1.0);
// cairo_paint(cairo);
// cairo_destroy(cairo);
// pixbuff = ev_document_misc_pixbuf_from_surface(surface);
// cairo_surface_destroy(surface);
poppler_page_render_to_pixbuf(pages[num], 0, 0, w, h, 1, 0, pixbuff);
poppler_page_render_to_pixbuf(pages[num], 0, 0, w, h, 2, 0, pixbuff);
return SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion moth_reader_pdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class moth_reader_pdf : public moth_reader {
moth_reader_pdf(const std::string&);
virtual ~moth_reader_pdf();
virtual int get_pages();
virtual int get_page(int, GdkPixbuf*);
virtual int get_page(int, GdkPixbuf*&);
virtual int get_page_size(int page, double*, double*);
};
#endif
Expand Down

0 comments on commit 22e7bf0

Please sign in to comment.