Beispiel #1
0
int main(int argc, char **argv)
{
	fz_error error;
	char *password = "";
	int c;
	enum { NO_FILE_OPENED, NO_PAGES_DRAWN, DREW_PAGES } state;

	while ((c = fz_getopt(argc, argv, "b:d:o:r:txm")) != -1)
	{
		switch (c)
		{
		case 'b': drawbands = atoi(fz_optarg); break;
		case 'd': password = fz_optarg; break;
		case 'o': drawpattern = fz_optarg; break;
		case 'r': drawzoom = atof(fz_optarg) / 72.0; break;
		case 't': drawmode = DRAWTXT; break;
		case 'x': drawmode = DRAWXML; break;
		case 'm': benchmark = 1; break;
		default:
			drawusage();
			break;
		}
	}

	if (fz_optind == argc)
		drawusage();

	setcleanup(local_cleanup);

	state = NO_FILE_OPENED;
	while (fz_optind < argc)
	{
		if (strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF"))
		{
			if (state == NO_PAGES_DRAWN)
				drawpages("1-");

			closexref();

			error = fz_newrenderer(&drawgc, pdf_devicergb, 0, 1024 * 512);
			if (error)
				die(error);

			openxref(argv[fz_optind], password, 0);
			state = NO_PAGES_DRAWN;
		}
		else
		{
			drawpages(argv[fz_optind]);
			state = DREW_PAGES;
		}
		fz_optind++;
	}

	if (state == NO_PAGES_DRAWN)
		drawpages("1-");

	closexref();
}
Beispiel #2
0
void
drawmain(int argc, char **argv)
{
	fz_error *error;
	char *password = "";
	int c;
	enum { NO_FILE_OPENED, NO_PAGES_DRAWN, DREW_PAGES } state;

	while ((c = getopt(argc, argv, "b:d:o:r:txm")) != -1)
	{
		switch (c)
		{
		case 'b': drawbands = atoi(optarg); break;
		case 'd': password = optarg; break;
		case 'o': drawpattern = optarg; break;
		case 'r': drawzoom = (float)(atof(optarg) / 72.0); break;
		case 't': drawmode = DRAWTXT; break;
		case 'x': drawmode = DRAWXML; break;
		case 'm': benchmark = 1; break;
		default:
			  drawusage();
			  break;
		}
	}

	if (optind == argc)
		drawusage();

	error = fz_newrenderer(&drawgc, pdf_devicergb, 0, 1024 * 512);
	if (error)
		die(error);

	state = NO_FILE_OPENED;
	while (optind < argc)
	{
		if (strstr(argv[optind], ".pdf"))
		{
			if (state == NO_PAGES_DRAWN)
				drawpages("1-");

			opensrc(argv[optind], password, 1);
			state = NO_PAGES_DRAWN;
		}
		else
		{
			drawpages(argv[optind]);
			state = DREW_PAGES;
		}
		optind++;
	}

	if (state == NO_PAGES_DRAWN)
		drawpages("1-");

	closesrc();

	fz_droprenderer(drawgc);
}
Beispiel #3
0
void pdfapp_init(pdfapp_t *app)
{
#if 0
	fz_error error;
	memset(app, 0, sizeof(pdfapp_t));
	error = fz_newrenderer(&app->rast, pdf_devicergb, 0, 1024 * 512);
	if (error)
		pdfapp_error(app, error);
#else
	memset(app, 0, sizeof(pdfapp_t));
#endif
}
Beispiel #4
0
void pdfapp_init(pdfapp_t *app)
{
	fz_error *error;

	memset(app, 0, sizeof(pdfapp_t));

	error = fz_newrenderer(&app->rast, pdf_devicergb, 0, 1024 * 512);
	if (error)
		pdfapp_error(app, error);

	app->scrw = 640;
	app->scrh = 480;
}
Beispiel #5
0
fz_error
fz_rendert3glyph(fz_glyph *glyph, fz_font *font, int gid, fz_matrix trm)
{
	fz_error error;
	fz_renderer *gc;
	fz_tree *tree;
	fz_matrix ctm;
	fz_irect bbox;

	/* TODO: make it reentrant */
	static fz_pixmap *pixmap = nil;
	if (pixmap)
	{
		fz_droppixmap(pixmap);
		pixmap = nil;
	}

	if (gid < 0 || gid > 255)
		return fz_throw("assert: glyph out of range");

	tree = font->t3procs[gid];
	if (!tree)
	{
		glyph->w = 0;
		glyph->h = 0;
		return fz_okay;
	}

	ctm = fz_concat(font->t3matrix, trm);
	bbox = fz_roundrect(fz_boundtree(tree, ctm));

	error = fz_newrenderer(&gc, pdf_devicegray, 1, 4096);
	if (error)
		return fz_rethrow(error, "cannot create renderer");
	error = fz_rendertree(&pixmap, gc, tree, ctm, bbox, 0);
	fz_droprenderer(gc);
	if (error)
		return fz_rethrow(error, "cannot render glyph");

	assert(pixmap->n == 1);

	glyph->x = pixmap->x;
	glyph->y = pixmap->y;
	glyph->w = pixmap->w;
	glyph->h = pixmap->h;
	glyph->samples = pixmap->samples;

	return fz_okay;
}
Beispiel #6
0
static fz_error *
t3render(fz_glyph *glyph, fz_font *fzfont, int cid, fz_matrix trm)
{
    pdf_font *font = (pdf_font*)fzfont;
    fz_error *error;
    fz_renderer *gc;
    fz_tree *tree;
    fz_pixmap *pixmap;
    fz_matrix ctm;
    fz_irect bbox;

    if (cid < 0 || cid > 255)
        return fz_throw("assert: glyph out of range");

    tree = font->charprocs[cid];
    if (!tree)
    {
        glyph->w = 0;
        glyph->h = 0;
        return fz_okay;
    }

    ctm = fz_concat(font->matrix, trm);
    bbox = fz_roundrect(fz_boundtree(tree, ctm));

    error = fz_newrenderer(&gc, pdf_devicegray, 1, GCMEM);
    if (error)
        return fz_rethrow(error, "cannot create renderer");
    error = fz_rendertree(&pixmap, gc, tree, ctm, bbox, 0);
    fz_droprenderer(gc);
    if (error)
        return fz_rethrow(error, "cannot render glyph");

    assert(pixmap->n == 1);

    glyph->x = pixmap->x;
    glyph->y = pixmap->y;
    glyph->w = pixmap->w;
    glyph->h = pixmap->h;
    glyph->samples = pixmap->samples;

    return fz_okay;
}
void
drawmain(int argc, char **argv)
{
    fz_error *error;
    char *password = "";
    int c;

    while ((c = getopt(argc, argv, "b:d:o:r:tx")) != -1)
    {
        switch (c)
        {
        case 'b': drawbands = atoi(optarg); break;
        case 'd': password = optarg; break;
        case 'o': drawpattern = optarg; break;
        case 'r': drawzoom = atof(optarg) / 72.0; break;
        case 't': drawmode = DRAWTXT; break;
        case 'x': drawmode = DRAWXML; break;
        default:
                  drawusage();
                  break;
        }
    }

    if (optind == argc)
        drawusage();

    error = fz_newrenderer(&drawgc, pdf_devicergb, 0, 1024 * 512);
    if (error)
        die(error);

    while (optind < argc)
    {
        if (strstr(argv[optind], ".pdf"))
            opensrc(argv[optind], password, 1);
        else
            drawpages(argv[optind]);
        optind++;
    }

    closesrc();

    fz_droprenderer(drawgc);
}
Epdf_Document* epdf_document_new(const char* filename)
{
    Epdf_Document* doc;
    fz_error error;

    doc = (Epdf_Document*)malloc(sizeof(Epdf_Document));
    if(!doc)
        return NULL;

    doc->xref = NULL;
    doc->outline = NULL;
    doc->rast = NULL;
    doc->pagecount = 0;
    doc->zoom = 1.0;
    doc->rotate = 0;
    doc->locked = false;

    error = fz_newrenderer(&doc->rast, pdf_devicergb, 0, 1024 * 512);
    if(error)
        return pdfdoc_error(doc);

    fz_obj* obj;

    // Open PDF and load xref table

    doc->xref = pdf_newxref();
    if(!doc->xref)
        return pdfdoc_error(doc);

    error = pdf_loadxref(doc->xref, (char*)filename);
    if(error)
    {
        fz_catch(error, "trying to repair");
        fprintf(stderr, "There was a problem with file \"%s\".\nIt may be corrupted or generated by faulty software.\nTrying to repair the file.\n", filename);
        error = pdf_repairxref(doc->xref, (char*)filename);
        if(error)
            return pdfdoc_error(doc);
    }

    error = pdf_decryptxref(doc->xref);
    if(error)
        pdfdoc_error(doc);

    // Handle encrypted PDF files

    if(pdf_needspassword(doc->xref))
        doc->locked = true;

    /*
     * Load meta information
     * TODO: move this into mupdf library
     */

    obj = fz_dictgets(doc->xref->trailer, (char*)"Root");
    doc->xref->root = fz_resolveindirect(obj);
    if(!doc->xref->root)
    {
        fz_throw("syntaxerror: missing Root object");
        pdfdoc_error(doc);
    }
    fz_keepobj(doc->xref->root);

    obj = fz_dictgets(doc->xref->trailer, "Info");
    doc->xref->info = fz_resolveindirect(obj);
    if(!doc->xref->info)
        fprintf(stderr, "Could not load PDF meta information.\n");
    if(doc->xref->info)
        fz_keepobj(doc->xref->info);

    doc->outline = pdf_loadoutline(doc->xref);

    doc->filename = strdup(filename);
    if(doc->xref->info)
    {
        obj = fz_dictgets(doc->xref->info, "Title");
        if(obj)
            doc->doctitle = pdf_toutf8(obj);
    }

    /*
     * Start at first page
     */

    doc->pagecount = pdf_getpagecount(doc->xref);

    if(doc->zoom < 0.1)
        doc->zoom = 0.1;
    if(doc->zoom > 3.0)
        doc->zoom = 3.0;

    return doc;
}
Beispiel #9
0
void pdfmoz_open(pdfmoz_t *moz, char *filename)
{
    SCROLLINFO si;
    fz_error error;
    fz_obj *obj;
    fz_irect bbox;
    int rot;
    int i;

    strcpy(moz->error, "");

    error = fz_newrenderer(&moz->rast, pdf_devicergb, 0, 1024 * 512);
    if (error)
	pdfmoz_error(moz, error);

    /*
     * Open PDF and load xref table
     */

    moz->filename = filename;

    moz->xref = pdf_newxref();
    error = pdf_loadxref(moz->xref, filename);
    if (error)
    {
	error = pdf_repairxref(moz->xref, filename);
	if (error)
	    pdfmoz_error(moz, error);
    }

    /*
     * Handle encrypted PDF files
     */

    error = pdf_decryptxref(moz->xref);
    if (error)
	pdfmoz_error(moz, error);

    if (pdf_needspassword(moz->xref))
    {
	pdfmoz_warn(moz, "PDF file is encrypted and needs a password.");
    }

    moz->pagecount = pdf_getpagecount(moz->xref);
    moz->pages = fz_malloc(sizeof(page_t) * moz->pagecount);

    for (i = 0; i < moz->pagecount; i++)
    {
	fz_obj *pageobj;
	pageobj = pdf_getpageobject(moz->xref, i);
	moz->pages[i].obj = fz_keepobj(pageobj);
	moz->pages[i].page = nil;
	moz->pages[i].image = nil;

	obj = fz_dictgets(moz->pages[i].obj, "CropBox");
	if (!obj)
	    obj = fz_dictgets(moz->pages[i].obj, "MediaBox");
	bbox = fz_roundrect(pdf_torect(obj));
	moz->pages[i].w = bbox.x1 - bbox.x0;
	moz->pages[i].h = bbox.y1 - bbox.y0;

	rot = fz_toint(fz_dictgets(moz->pages[i].obj, "Rotate"));
	if ((rot / 90) % 2)
	{
	    int t = moz->pages[i].w;
	    moz->pages[i].w = moz->pages[i].h;
	    moz->pages[i].h = t;
	}

	moz->pages[i].px = 1 + PAD;
    }

    /*
     * Load meta information
     * TODO: move this into mupdf library
     */

    obj = fz_dictgets(moz->xref->trailer, "Root");
    moz->xref->root = fz_resolveindirect(obj);
    if (!moz->xref->root)
	pdfmoz_error(moz, fz_throw("syntaxerror: missing Root object"));
    if (moz->xref->root)
	fz_keepobj(moz->xref->root);

    obj = fz_dictgets(moz->xref->trailer, "Info");
    moz->xref->info = fz_resolveindirect(obj);
    if (moz->xref->info)
	fz_keepobj(moz->xref->info);

    moz->doctitle = filename;
    if (strrchr(moz->doctitle, '\\'))
	moz->doctitle = strrchr(moz->doctitle, '\\') + 1;
    if (strrchr(moz->doctitle, '/'))
	moz->doctitle = strrchr(moz->doctitle, '/') + 1;
    if (moz->xref->info)
    {
	obj = fz_dictgets(moz->xref->info, "Title");
	if (obj)
	    moz->doctitle = pdf_toutf8(obj);
    }

    /*
     * Start at first page
     */

    si.cbSize = sizeof(si);
    si.fMask = SIF_POS | SIF_RANGE; // XXX | SIF_PAGE;
    si.nPos = 0;
    si.nMin = 0;
    si.nMax = 1;
    si.nPage = 1;
    SetScrollInfo(moz->hwnd, SB_VERT, &si, TRUE);

    moz->scrollpage = 0;
    moz->scrollyofs = 0;

    InvalidateRect(moz->hwnd, NULL, FALSE);
}
Beispiel #10
0
static PDFContext* pdfOpen(char *filename) {
	PDFContext* ctx = new PDFContext();
	memset(ctx, 0, sizeof(PDFContext));
	fz_error *error;
	fz_obj *obj;

	error = fz_newrenderer(&ctx->rast, pdf_devicergb, 0, 1024 * 512);
	if (error) {
		printf("err0: %s\n", error->msg);
		delete ctx;
		return 0;
	}

	/*
	* Open PDF and load xref table
	*/
	error = pdf_newxref(&ctx->xref);
	if (error) {
		printf("err1: %s\n", error->msg);
		delete ctx;
		return 0;
	}

	error = pdf_loadxref(ctx->xref, filename);
	if (error) {
		printf("err2: %s\n", error->msg);
		if (!strncmp(error->msg, "ioerror", 7)) {
			delete ctx;
			return 0;
		}
		printf("There was a problem with file \"%s\".\n"
			"It may be corrupted, or generated by broken software.\n\n"
			"%s\n\nTrying to continue anyway...\n", filename, error->msg);
		error = pdf_repairxref(ctx->xref, filename);
		if (error) {
			printf("err3: %s\n", error->msg);
			delete ctx;
			return 0;
		}
	}

	/*
	* Handle encrypted PDF files
	*/

	error = pdf_decryptxref(ctx->xref);
	if (error) {
		printf("err4: %s\n", error->msg);
		delete ctx;
		return 0;
	}

	/*if (ctx->xref->crypt) {
		//printf("err5: %s\n", error->msg);
		printf("err5\n", error->msg);
		delete ctx;
		return 0;
	}*/

	if (ctx->xref->crypt) {
		error = pdf_setpassword(ctx->xref->crypt, "");
		if (error) {
			printf("err5: encrypted file (tried empty password): %s\n", error->msg);
			delete ctx;
			return 0;
			/*
			fz_droperror(error);
			password = winpassword(app, filename);
			if (!password)
				exit(1);
			error = pdf_setpassword(app->xref->crypt, password);
			if (error)
				pdfapp_warn(app, "Invalid password.");
			*/
		}
	}

	/*
	* Load page tree
	*/
	error = pdf_loadpagetree(&ctx->pages, ctx->xref);
	if (error) {
		printf("err6: %s\n", error->msg);
		delete ctx;
		return 0;
	}

	/*
	* Load meta information
	* TODO: move this into mupdf library
	*/
	obj = fz_dictgets(ctx->xref->trailer, "Root");
	if (!obj) {
		printf("err7: %s\n", error->msg);
		//pdfapp_error(app, fz_throw("syntaxerror: missing Root object"));
		delete ctx;
		return 0;
	}

	error = pdf_loadindirect(&ctx->xref->root, ctx->xref, obj);
	if (error) {
		printf("err8: %s\n", error->msg);
		delete ctx;
		return 0;
	}

	obj = fz_dictgets(ctx->xref->trailer, "Info");
	if (obj) {

		error = pdf_loadindirect(&ctx->xref->info, ctx->xref, obj);

		if (error) {
			printf("err10: %s\n", error->msg);
			delete ctx;
			return 0;
		}
	}

	error = pdf_loadnametrees(ctx->xref);
	// non-critical error, we can live without the outline
	if (error) {
		printf("warn11 - no outline: %s\n", error->msg);
	} else {
		error = pdf_loadoutline(&ctx->outline, ctx->xref);
		if (error) {
			printf("warn12 - no outline: %s\n", error->msg);
		}
	}
/*
	char* ptitle = filename;
	if (strrchr(app->doctitle, '\\'))
		pt = strrchr(app->doctitle, '\\') + 1;
	if (strrchr(app->doctitle, '/'))
		app->doctitle = strrchr(app->doctitle, '/') + 1;
	if (app->xref->info) {
		obj = fz_dictgets(app->xref->info, "Title");
		if (obj) {
			error = pdf_toutf8(&app->doctitle, obj);
			if (error) {
				delete ctx;
				return 0;
			}
		}
	}
*/
	/*
	* Start at first page
	*/
/*
	app->shrinkwrap = 1;
	if (app->pageno < 1)
		app->pageno = 1;
	if (app->zoom <= 0.0)
		app->zoom = 1.0;
	app->rotate = 0;
	app->panx = 0;
	app->pany = 0;

	pdfapp_showpage(app, 1, 1);
*/

	ctx->pageno = 1;
	ctx->zoom = 1.0f;
	ctx->zoomLevel = 4;
	ctx->rotate = 0.0f;
	ctx->rotateLevel = 0;
	return ctx;
}