Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2014 16:52
Show Gist options
  • Save anonymous/10291372 to your computer and use it in GitHub Desktop.
Save anonymous/10291372 to your computer and use it in GitHub Desktop.
mupdf
#include "ofMain.h"
#include "testApp.h"
//========================================================================
int main( ){
ofSetupOpenGL(1024,768, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new testApp());
}
#pragma once
#include "ofMain.h"
extern "C" {
#include "mupdf/fitz.h"
#include "mupdf/pdf.h"
}
class testApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofTexture texture;
};
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup()
{
ofSetBackgroundColor( ofColor::gray );
fz_context *context = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
if( context != NULL )
{
fz_try( context )
{
fz_document *fzDocument = fz_open_document( context, ofToDataPath("examplePDFData.pdf").c_str() );
if( fzDocument != NULL )
{
fz_page *page = NULL;
int pageCount = 0;
fz_try( context )
{
pageCount = fz_count_pages( fzDocument );
} fz_catch( context )
{
ofLogError() << fz_caught( context ) << " Error Counting Pages.";
}
if( pageCount > 0 )
{
fz_try( context )
{
page = fz_load_page( fzDocument, 0 );
} fz_catch( context )
{
ofLogError() << fz_caught( context ) << " Error Loading Page.";
}
if( page )
{
fz_matrix transform;
fz_rotate(&transform, 0.0f );
fz_pre_scale(&transform, 1.0f, 1.0f );
fz_rect bounds;
fz_bound_page( fzDocument, page, &bounds);
fz_transform_rect(&bounds, &transform);
fz_irect bbox;
fz_round_rect(&bbox, &bounds);
fz_pixmap *pix = fz_new_pixmap_with_bbox( context, fz_device_rgb(context), &bbox);
fz_clear_pixmap( context, pix );
fz_device *dev = fz_new_draw_device( context, pix);
fz_run_page( fzDocument, page, dev, &transform, NULL);
texture.allocate( pix->w, pix->h, GL_RGBA );
texture.loadData( pix->samples, pix->w, pix->h, GL_RGBA );
fz_free_device(dev);
fz_drop_pixmap( context, pix);
fz_free_page( fzDocument, page);
}
}
fz_close_document( fzDocument );
}
}fz_catch( context )
{
ofLogError() << fz_caught( context ) << " Error.";
}
fz_free_context( context );
}
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
texture.draw( ofPoint() );
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment