Пример #1
0
int BKPDF::setCurrentPage(int position) {
	int oldPage = ctx->pageno;
	ctx->pageno = position;
	if (ctx->pageno < 1)
		ctx->pageno = 1;
	if (ctx->pageno > pdf_getpagecount(ctx->pages))
		ctx->pageno = pdf_getpagecount(ctx->pages);
	if (ctx->pageno != oldPage) {
		loadNewPage = true;
		char t[256];
		snprintf(t, 256, "Loading page %d", ctx->pageno);
		setBanner(t);
		panY = 0;
		return BK_CMD_MARK_DIRTY;
	}
	return 0;
}
Пример #2
0
void
drawpages(char *pagelist)
{
    int page, spage, epage;
    char *spec, *dash;

    if (!src)
        drawusage();

    spec = strsep(&pagelist, ",");
    while (spec)
    {
        dash = strchr(spec, '-');

        if (dash == spec)
            spage = epage = 1;
        else
            spage = epage = atoi(spec);

        if (dash)
        {
            if (strlen(dash) > 1)
                epage = atoi(dash + 1);
            else
                epage = pdf_getpagecount(srcpages);
        }

        if (spage > epage)
            page = spage, spage = epage, epage = page;

        for (page = spage; page <= epage; page++)
        {
            if (page < 1 || page > pdf_getpagecount(srcpages))
                continue;
            switch (drawmode)
            {
            case DRAWPNM: drawpnm(page); break;
            case DRAWTXT: drawtxt(page); break;
            case DRAWXML: drawxml(page); break;
            }
        }

        spec = strsep(&pagelist, ",");
    }
}
Пример #3
0
void
editpages(char *pagelist)
{
	int page, spage, epage;
	char *spec, *dash;

	if (!src)
		editusage();

	spec = strsep(&pagelist, ",");
	while (spec)
	{
		dash = strchr(spec, '-');

		if (dash == spec)
			spage = epage = 1;
		else
			spage = epage = atoi(spec);

		if (dash)
		{
			if (strlen(dash) > 1)
				epage = atoi(dash + 1);
			else
				epage = pdf_getpagecount(srcpages);
		}

		if (spage > epage)
			page = spage, spage = epage, epage = page;

		for (page = spage; page <= epage; page++)
		{
			if (page < 1 || page > pdf_getpagecount(srcpages))
				continue;
			editcopy(page);
		}

		spec = strsep(&pagelist, ",");
	}
}
Пример #4
0
JNIEXPORT int JNICALL Java_com_artifex_mupdf_MuPDFCore_openFile(JNIEnv * env, jobject thiz, jstring jfilename)
{
    const char *filename;
    char *password = "";
    int accelerate = 1;
    fz_error error;

    filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
    if (filename == NULL)
    {
        LOGE("Failed to get filename");
        return 0;
    }

    if (accelerate)
        fz_accelerate();
    glyphcache = fz_newglyphcache();
    colorspace = fz_devicergb;

    LOGE("Opening document...");
    error = pdf_openxref(&xref, filename, password);
    if (error)
    {
        LOGE("Cannot open document: '%s'\n", filename);
        return 0;
    }

    LOGE("Loading page tree...");
    error = pdf_loadpagetree(xref);
    if (error)
    {
        LOGE("Cannot load page tree: '%s'\n", filename);
        return 0;
    }
    LOGE("Done! %d pages", pdf_getpagecount(xref));

    return pdf_getpagecount(xref);
}
Пример #5
0
int BKPDF::updateContent() {
	if (lastScrollFlag != BKUser::options.pdfFastScroll)
		return BK_CMD_RELOAD;

	if (loadNewPage) {
		pageError = pdfLoadPage(ctx) != 0;
		redrawBuffer();
		loadNewPage = false;
		char t[256];
		snprintf(t, 256, "Page %d of %d", ctx->pageno, pdf_getpagecount(ctx->pages));
		setBanner(t);
		return BK_CMD_MARK_DIRTY;
	}

	/*bannerFrames--;
	if (bannerFrames < 0)
		bannerFrames = 0;*/

	/*
	if (loadNewPage) {
		if (FZScreen::wasSuspended()) {
			FZScreen::clearSuspended();
			PDFContext* oldctx = ctx;
			ctx = pdfOpen((char*)path.c_str());
			if (ctx == 0) {
				// f****d... just implement *proper* error handling some day ok???
				return 0;
			}
			ctx->pageno = oldctx->pageno;
			ctx->zoom = oldctx->zoom;
			ctx->zoomLevel = oldctx->zoomLevel;
			ctx->rotate = oldctx->rotate;
			// remove this if it crashes on resume, but it will leak a ton of ram
			// need*pooled*malloc*NOW
			//pdfClose(oldctx);
			delete oldctx;
		}
		pageError = pdfLoadPage(ctx) != 0;
		redrawBuffer();
		loadNewPage = false;
		char t[256];
		snprintf(t, 256, "Page %d of %d", ctx->pageno, pdf_getpagecount(ctx->pages));
		banner = t;
		bannerFrames = 60;
		return BK_CMD_MARK_DIRTY;
	}
	*/
	return 0;
}
Пример #6
0
void openxref(char *filename, char *password, int dieonbadpass)
{
	fz_error error;
	int okay;

	basename = strrchr(filename, '/');
	if (!basename)
		basename = filename;
	else
		basename++;

	xref = pdf_newxref();
	error = pdf_loadxref(xref, filename);
	if (error)
	{
		fz_catch(error, "trying to repair");
		error = pdf_repairxref(xref, filename);
		if (error)
			die(error);
	}

	error = pdf_decryptxref(xref);
	if (error)
		die(error);

	if (pdf_needspassword(xref))
	{
		okay = pdf_authenticatepassword(xref, password);
		if (!okay && !dieonbadpass)
			fz_warn("invalid password, attempting to continue.");
		else if (!okay && dieonbadpass)
			die(fz_throw("invalid password"));
	}

	xref->root = fz_dictgets(xref->trailer, "Root");
	if (xref->root)
		fz_keepobj(xref->root);

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

	pagecount = pdf_getpagecount(xref);
}
Пример #7
0
void pdfapp_gotopage(pdfapp_t *app, fz_obj *obj)
{
	int oid = fz_tonum(obj);
	int i;

	for (i = 0; i < pdf_getpagecount(app->pages); i++)
	{
		if (fz_tonum(app->pages->pref[i]) == oid)
		{
			if (app->histlen + 1 == 256)
			{
				memmove(app->hist, app->hist + 1, sizeof(int) * 255);
				app->histlen --;
			}
			app->hist[app->histlen++] = app->pageno;
			app->pageno = i + 1;
			pdfapp_showpage(app, 1, 1);
			return;
		}
	}
}
Пример #8
0
int
copyPdfFile(
    soPdfFile* inFile,
    soPdfFile* outFile
    )
{
    fz_error    *error;
    int         pageTreeNum, pageTreeGen;

    assert(inFile != NULL);
    assert(outFile != NULL);

    //
    // Process every page in the source file
    //
    {
        printf("\nProcessing input page : ");
        for (int pageNo = 0; pageNo < pdf_getpagecount(inFile->pageTree); pageNo++)
        {
            displayPageNumber(pageNo + 1, !pageNo);

            // Get the page object from the source
            fz_obj  *pageRef = inFile->pageTree->pref[pageNo];
            fz_obj  *pageObj = pdf_getpageobject(inFile->pageTree, pageNo);

            //
            // Process the page. Each page can be split into up-to 3 pages
            //
            fz_rect    bbRect[3];
            error = processPage(inFile, pageNo, bbRect, 3);
            if (error)
                return soPdfError(error);


            for (int ctr = 0; ctr < 3; ctr++)
            {
                // Check if this was a blank page
                if (fz_isemptyrect(bbRect[ctr]))
                    break;

                //
                // copy the source page dictionary entry. The way this is done is basically
                // by making a copy of the page dict object in the source file, and adding
                // the copy in the source file. Then the copied page dict object is 
                // referenced and added to the destination file.
                //
                // This convoluted procedure is done because the copy is done by pdf_transplant
                // function that accepts a source and destination. Whatever is referenced by
                // destination object is deep copied
                //
                

                // allocate an object id and generation id in source file
                //
                // There is a bug in mupdf where the object allocation returns
                // 0 oid and 0 gid when the input pdf file has iref stream
                // so to work around the issue, we wrap the pdf_allocojbect
                // in a for loop 10 times to get the number
                //
                int sNum, sGen, tries;

                for (tries = 0; tries < 10; tries++)
                {
                    error = pdf_allocobject(inFile->xref, &sNum, &sGen);
                    if (error)
                        return soPdfError(error);

                    // If sNum is non zero then the allocation was successful
                    if (sNum != 0)
                        break;  
                    pdf_updateobject(inFile->xref, sNum, sGen, pageObj);
                }

                // If we didn't succeed even after 10 tries then this file 
                // is not going to work.
                if (tries >= 10)
                    return soPdfError(fz_throw("cannot allocate object because of mupdf bug"));

                // make a deep copy of the original page dict
                fz_obj  *pageObj2;
                error = fz_deepcopydict(&pageObj2, pageObj);
                if (error)
                    return soPdfError(error);

                // update the source file with the duplicate page object
                pdf_updateobject(inFile->xref, sNum, sGen, pageObj2);

                fz_dropobj(pageObj2);

                // create an indirect reference to the page object
                fz_obj  *pageRef2;
                error = fz_newindirect(&pageRef2, sNum, sGen);
                if (error)
                    return soPdfError(error);

                // delete the parent dictionary entry
                // Do we need to delete any other dictionary entry 
                // like annot, tabs, metadata, etc
                fz_dictdels(pageObj2, "Parent");

                // Set the media box
                setPageMediaBox(inFile->xref, pageObj2, bbRect[ctr]);

                // Set the rotation based on input
                switch(p_mode)
                {
                    // no rotation if fit height
                case FitHeight:
                case Fit2xHeight:
                    break;

                    // rotate -90 deg if fit width
                case Fit2xWidth:
                case FitWidth:
                    setPageRotate(pageObj2, p_reverseLandscape ? 90 : -90);
                    break;

                case SmartFitHeight:
                case SmartFitWidth:
                default:
                    return soPdfError(fz_throw("Mode(%d) not yet implemented.", p_mode));
                    break;
                }


                // push the indirect reference to the destination list for copy by pdf_transplant
                error = fz_arraypush(outFile->editobjs, pageRef2);
                if (error)
                    return soPdfError(error);
            }
        }
    }

    // flush the objects into destination from source
    {
        fz_obj      *results;
        int         outPages;

        printf("\nCopying output page : ");
        error = pdf_transplant(outFile->xref, inFile->xref, &results, outFile->editobjs);
        if (error)
            return soPdfError(error);

        outPages = fz_arraylen(results);
        for (int ctr = 0; ctr < outPages; ctr++)
        {
            displayPageNumber(ctr + 1, !ctr);
            error = fz_arraypush(outFile->pagelist, fz_arrayget(results, 
                p_reverseLandscape ? outPages - 1 - ctr : ctr));
            if (error)
                return soPdfError(error);
        }

        fz_dropobj(results);
    }

    // flush page tree

    // Create page tree and add back-links
    {
        fz_obj  *pageTreeObj;
        fz_obj  *pageTreeRef;

        // allocate a new object in out file for pageTree object
        error = pdf_allocobject(outFile->xref, &pageTreeNum, &pageTreeGen);
        if (error)
            return soPdfError(error);

        // Create a page tree object
        error = fz_packobj(&pageTreeObj, "<</Type/Pages/Count %i/Kids %o>>",
            fz_arraylen(outFile->pagelist), outFile->pagelist);
        if (error)
            return soPdfError(error);

        // Update the xref entry with the pageTree object
        pdf_updateobject(outFile->xref, pageTreeNum, pageTreeGen, pageTreeObj);

        fz_dropobj(pageTreeObj);

        // Create a reference to the pageTree object
        error = fz_newindirect(&pageTreeRef, pageTreeNum, pageTreeGen);
        if (error)
            return soPdfError(error);

        //
        // For every page in the output file, update the parent entry
        //
        for (int ctr = 0; ctr < fz_arraylen(outFile->pagelist); ctr++)
        {
            fz_obj  *pageObj;

            int num = fz_tonum(fz_arrayget(outFile->pagelist, ctr));
            int gen = fz_togen(fz_arrayget(outFile->pagelist, ctr));

            // Get the page object from xreft
            error = pdf_loadobject(&pageObj, outFile->xref, num, gen);
            if (error)
                return soPdfError(error);

            // Update the parent entry in the page dictionary
            error = fz_dictputs(pageObj, "Parent", pageTreeRef);
            if (error)
                return soPdfError(error);

            // Update the entry with the updated page object
            pdf_updateobject(outFile->xref, num, gen, pageObj);

            fz_dropobj(pageObj);
        }
    }

    // Create catalog and root entries
    {
        fz_obj  *catObj, *infoObj;
        int     rootNum, rootGen;
        int     infoNum, infoGen;

        //
        // Copy the info catalog to the destination

        // alloc an object id and gen id in destination file
        error = pdf_allocobject(outFile->xref, &infoNum, &infoGen);
        if (error)
            return soPdfError(error);

        // make a deep copy of the original page dict
        error = fz_deepcopydict(&infoObj, inFile->xref->info);
        if (error)
            return soPdfError(error);

        // update the dest file with object
        pdf_updateobject(outFile->xref, infoNum, infoGen, infoObj);
        outFile->xref->info = infoObj;

        fz_dropobj(infoObj);

        //
        // root/catalog object creation
        error = pdf_allocobject(outFile->xref, &rootNum, &rootGen);
        if (error)
            return soPdfError(error);

        error = fz_packobj(&catObj, "<</Type/Catalog /Pages %r>>", pageTreeNum, pageTreeGen);
        if (error)
            return soPdfError(error);

        pdf_updateobject(outFile->xref, rootNum, rootGen, catObj);

        fz_dropobj(catObj);

        // Create trailer
        error = fz_packobj(&outFile->xref->trailer, "<</Root %r /Info %r>>", 
            rootNum, rootGen, infoNum, infoGen);
        if (error)
            return soPdfError(error);

    }

    // Update the info in the target file and save the xref
    printf("\nSaving.\n");
    error = setPageInfo(inFile, outFile);
    if (error)
        return soPdfError(error);

    error = pdf_savexref(outFile->xref, outFile->fileName, NULL);
    if (error)
        return soPdfError(error);

    if (g_errorCount != 0)
    {
        printf("\nFollowing issues encounted were ignored.\n\n");
        for (int ctr = g_errorCount - 1; ctr >= 0; ctr--)
            soPdfError(g_errorList[ctr]);
    }
    printf("\nSaved.\n");

    return 0;
}
Пример #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);
}
Пример #10
0
void
drawpages(char *pagelist)
{
	int page, spage, epage;
	char *spec, *dash;
	struct benchmark loadtimes, drawtimes;

	if (!src)
		drawusage();

	if (benchmark)
	{
		memset(&loadtimes, 0x00, sizeof (loadtimes));
		loadtimes.min = LONG_MAX;
		memset(&drawtimes, 0x00, sizeof (drawtimes));
		drawtimes.min = LONG_MAX;
	}

	spec = strsep(&pagelist, ",");
	while (spec)
	{
		dash = strchr(spec, '-');

		if (dash == spec)
			spage = epage = 1;
		else
			spage = epage = atoi(spec);

		if (dash)
		{
			if (strlen(dash) > 1)
				epage = atoi(dash + 1);
			else
				epage = pdf_getpagecount(srcpages);
		}

		if (spage > epage)
			page = spage, spage = epage, epage = page;

		printf("Drawing pages %d-%d...\n", spage, epage);
		for (page = spage; page <= epage; page++)
		{
			if (page < 1 || page > pdf_getpagecount(srcpages))
				continue;

			switch (drawmode)
			{
			case DRAWPNM: drawpnm(page, &loadtimes, &drawtimes); break;
			case DRAWTXT: drawtxt(page); break;
			case DRAWXML: drawxml(page); break;
			}
		}

		spec = strsep(&pagelist, ",");
	}

	if (benchmark)
	{
		if (loadtimes.pages > 0)
		{
			loadtimes.avg /= loadtimes.pages;
			drawtimes.avg /= drawtimes.pages;

			printf("benchmark[load]: min: %6.3fs (page % 4d), avg: %6.3fs, max: %6.3fs (page % 4d)\n",
				loadtimes.min / 1000000.0, loadtimes.minpage,
				loadtimes.avg / 1000000.0,
				loadtimes.max / 1000000.0, loadtimes.maxpage);
			printf("benchmark[draw]: min: %6.3fs (page % 4d), avg: %6.3fs, max: %6.3fs (page % 4d)\n",
				drawtimes.min / 1000000.0, drawtimes.minpage,
				drawtimes.avg / 1000000.0,
				drawtimes.max / 1000000.0, drawtimes.maxpage);
		}
	}
}
Пример #11
0
void pdfapp_onkey(pdfapp_t *app, int c)
{
	int oldpage = app->pageno;
	int panto = 0; /* 0 = top, 1 = bottom, 2 = leave alone */

	/*
	 * Save numbers typed for later
	 */

	if (c >= '0' && c <= '9')
		app->number[app->numberlen++] = c;
	else
		if (c != 'g' && c != 'G')
			app->numberlen = 0;

	switch (c)
	{

	/*
	 * Zoom and rotate
	 */

	case '+': case '=':
		app->zoom += 0.1;
		if (app->zoom > 3.0)
			app->zoom = 3.0;
		pdfapp_showpage(app, 0, 1);
		break;
	case '-':
		app->zoom -= 0.1;
		if (app->zoom < 0.1)
			app->zoom = 0.1;
		pdfapp_showpage(app, 0, 1);
		break;
	case 'l': case '<':
		app->rotate -= 90;
		pdfapp_showpage(app, 0, 1);
		break;
	case 'r': case '>':
		app->rotate += 90;
		pdfapp_showpage(app, 0, 1);
		break;

	case 'a':
		app->rotate -= 15;
		pdfapp_showpage(app, 0, 1);
		break;
	case 's':
		app->rotate += 15;
		pdfapp_showpage(app, 0, 1);
		break;

	/*
	 * Pan view, but dont need to repaint image
	 */

	case 'w':
		app->shrinkwrap = 1;
		app->panx = app->pany = 0;
		pdfapp_showpage(app, 0, 0);
		break;

	case 'd':
		app->pany -= app->image->h / 10;
		pdfapp_showpage(app, 0, 0);
		break;

	case 'u':
		app->pany += app->image->h / 10;
		pdfapp_showpage(app, 0, 0);
		break;

	case ',':
		app->panx += app->image->w / 10;
		pdfapp_showpage(app, 0, 0);
		break;

	case '.':
		app->panx -= app->image->w / 10;
		pdfapp_showpage(app, 0, 0);
		break;

	/*
	 * Page navigation
	 */

	case 'g':
	case '\n':
	case '\r':
		if (app->numberlen > 0)
		{
			app->number[app->numberlen] = '\0';
			app->pageno = atoi(app->number);
			app->numberlen = 0;
		}
		break;

	case 'G':
		app->pageno = pdf_getpagecount(app->pages);
		break;

	case 'm':
		if (app->histlen + 1 == 256)
		{
			memmove(app->hist, app->hist + 1, sizeof(int) * 255);
			app->histlen --;
		}
		app->hist[app->histlen++] = app->pageno;
		break;

	case 't':
		if (app->histlen > 0)
			app->pageno = app->hist[--app->histlen];
		break;

	/*
	 * Back and forth ...
	 */

	case 'p':
		panto = 2;
		app->pageno--;
		break;

	case 'b': case '\b':
		panto = 1;
		app->pageno--;
		break;

	case 'n':
		panto = 2;
	case 'f': case ' ':
		app->pageno++;
		break;

	case 'B':
		app->pageno -= 10;
		break;

	case 'F':
		app->pageno += 10;
		break;
	}

	if (app->pageno < 1)
		app->pageno = 1;
	if (app->pageno > pdf_getpagecount(app->pages))
		app->pageno = pdf_getpagecount(app->pages);

	if (app->pageno != oldpage)
	{
		switch (panto)
		{
		case 0: app->pany = 0; break;
		case 1: app->pany = -2000; break;
		case 2: break;
		}
		pdfapp_showpage(app, 1, 1);
	}
}
Пример #12
0
int BKPDF::getTotalPages() {
	return pdf_getpagecount(ctx->pages);
}
Пример #13
0
void pdfapp_open(pdfapp_t *app, char *filename)
{
#if 0
	fz_error error;
	fz_obj *obj;
	char *password = "";
#else
	fz_error error;
	fz_stream *file;
	char *password = "";
	fz_obj *obj;
	fz_obj *info;

	int fd;
	fd = open(filename, O_BINARY | O_RDONLY, 0666);
	if (fd < 0)
		fprintf(stderr, "error, file %s does not exist\n", filename);
#endif

	/*
	 * Open PDF and load xref table
	 */

	app->filename = filename;

#if 0
	app->xref = pdf_newxref();
	error = pdf_loadxref(app->xref, filename);
	if (error)
	{
		fz_catch(error, "trying to air");
		error = pdf_repairxref(app->xref, filename);
		if (error)
			pdfapp_error(app, error);
	}

	error = pdf_decryptxref(app->xref);
	if (error)
		pdfapp_error(app, error);
#else
	file = fz_open_fd(fd);
	error = pdf_open_xref_with_stream(&app->xref, file, NULL);
	if (error)
		pdfapp_error(app, fz_rethrow(error, "cannot open document '%s'", filename));
	fz_close(file);
#endif

	/*
	 * Handle encrypted PDF files
	 */

	if (pdf_needs_password(app->xref))
	{
		int okay = pdf_authenticate_password(app->xref, password);
		while (!okay)
		{	
		        //password = winpassword(app, filename);
			if (!password)
				exit(1);
			okay = pdf_authenticate_password(app->xref, password);
			if (!okay)
				pdfapp_warn(app, "Invalid password.");
		}
	}

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

#if 0
	obj = fz_dictgets(app->xref->trailer, "Root");
	app->xref->root = fz_resolveindirect(obj);
	if (!app->xref->root)
		pdfapp_error(app, fz_throw("syntaxerror: missing Root object"));
	fz_keepobj(app->xref->root);

	obj = fz_dictgets(app->xref->trailer, "Info");
	app->xref->info = fz_resolveindirect(obj);
	if (!app->xref->info)
		pdfapp_warn(app, "Could not load PDF meta information.");
	if (app->xref->info)
		fz_keepobj(app->xref->info);

	/*app->outline = pdf_loadoutline(app->xref);*/

	app->doctitle = filename;
	if (strrchr(app->doctitle, '\\'))
		app->doctitle = 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)
		{
			app->doctitle = pdf_toutf8(obj);
		}
	}
#else
	app->outline = pdf_load_outline(app->xref);

	app->doctitle = filename;
	if (strrchr(app->doctitle, '\\'))
		app->doctitle = strrchr(app->doctitle, '\\') + 1;
	if (strrchr(app->doctitle, '/'))
		app->doctitle = strrchr(app->doctitle, '/') + 1;
	info = fz_dict_gets(app->xref->trailer, "Info");
	if (info)
	{
		obj = fz_dict_gets(info, "Title");
		if (obj)
			app->doctitle = pdf_to_utf8(obj);
	}
#endif

	/*
	 * Start at first page
	 */

#if 0
	app->pagecount = pdf_getpagecount(app->xref);
#else
	error = pdf_load_page_tree(app->xref);
	if (error)
		pdfapp_error(app, fz_rethrow(error, "cannot load page tree"));

	app->pagecount = pdf_count_pages(app->xref);
#endif
	
	app->rotate = 0;
	
}
Пример #14
0
static void retainpages(int argc, char **argv)
{
	fz_error error;
	fz_obj *oldroot, *root, *pages, *kids, *countobj, *parent;

	/* Load the old page tree */
	error = pdf_loadpagetree(xref);
	if (error)
		die(fz_rethrow(error, "cannot load page tree"));

	/* Keep only pages/type entry to avoid references to unretained pages */
	oldroot = fz_dictgets(xref->trailer, "Root");
	pages = fz_dictgets(oldroot, "Pages");

	root = fz_newdict(2);
	fz_dictputs(root, "Type", fz_dictgets(oldroot, "Type"));
	fz_dictputs(root, "Pages", fz_dictgets(oldroot, "Pages"));

	pdf_updateobject(xref, fz_tonum(oldroot), fz_togen(oldroot), root);

	fz_dropobj(root);

	/* Create a new kids array with only the pages we want to keep */
	parent = fz_newindirect(fz_tonum(pages), fz_togen(pages), xref);
	kids = fz_newarray(1);

	/* Retain pages specified */
	while (argc - fz_optind)
	{
		int page, spage, epage;
		char *spec, *dash;
		char *pagelist = argv[fz_optind];

		spec = fz_strsep(&pagelist, ",");
		while (spec)
		{
			dash = strchr(spec, '-');

			if (dash == spec)
				spage = epage = pdf_getpagecount(xref);
			else
				spage = epage = atoi(spec);

			if (dash)
			{
				if (strlen(dash) > 1)
					epage = atoi(dash + 1);
				else
					epage = pdf_getpagecount(xref);
			}

			if (spage > epage)
				page = spage, spage = epage, epage = page;

			if (spage < 1)
				spage = 1;
			if (epage > pdf_getpagecount(xref))
				epage = pdf_getpagecount(xref);

			for (page = spage; page <= epage; page++)
			{
				fz_obj *pageobj = pdf_getpageobject(xref, page);
				fz_obj *pageref = pdf_getpageref(xref, page);

				fz_dictputs(pageobj, "Parent", parent);

				/* Store page object in new kids array */
				fz_arraypush(kids, pageref);
			}

			spec = fz_strsep(&pagelist, ",");
		}

		fz_optind++;
	}

	fz_dropobj(parent);

	/* Update page count and kids array */
	countobj = fz_newint(fz_arraylen(kids));
	fz_dictputs(pages, "Count", countobj);
	fz_dropobj(countobj);
	fz_dictputs(pages, "Kids", kids);
	fz_dropobj(kids);
}
Пример #15
0
void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage)
{
	char buf[256];
	fz_error *error;
	fz_matrix ctm;
	fz_rect bbox;
	fz_obj *obj;

	if (loadpage)
	{
		wincursor(app, WAIT);

		if (app->page)
			pdf_droppage(app->page);
		app->page = nil;

		obj = pdf_getpageobject(app->pages, app->pageno - 1);

		error = pdf_loadpage(&app->page, app->xref, obj);
		if (error)
			pdfapp_error(app, error);

		sprintf(buf, "%s - %d/%d", app->doctitle,
				app->pageno, pdf_getpagecount(app->pages));
		wintitle(app, buf);
	}

	if (drawpage)
	{
		wincursor(app, WAIT);

		if (app->image)
			fz_droppixmap(app->image);
		app->image = nil;

		ctm = pdfapp_viewctm(app);
		bbox = fz_transformaabb(ctm, app->page->mediabox);

		error = fz_rendertree(&app->image, app->rast, app->page->tree,
				ctm, fz_roundrect(bbox), 1);
		if (error)
			pdfapp_error(app, error);

		winconvert(app, app->image);
	}

	pdfapp_panview(app, app->panx, app->pany);

	if (app->shrinkwrap)
	{
		int w = app->image->w;
		int h = app->image->h;
		if (app->winw == w)
			app->panx = 0;
		if (app->winh == h)
			app->pany = 0;
		if (w > app->scrw * 90 / 100)
			w = app->scrw * 90 / 100;
		if (h > app->scrh * 90 / 100)
			h = app->scrh * 90 / 100;
		if (w != app->winw || h != app->winh)
			winresize(app, w, h);
	}

	winrepaint(app);

	wincursor(app, ARROW);
}
Пример #16
0
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;
}
Пример #17
0
void pdfapp_open(pdfapp_t *app, char *filename)
{
    fz_obj *obj;
    fz_obj *info;
    char *password = "";

    /*
     * Open PDF and load xref table
     */

    app->filename = filename;

    app->xref = pdf_openxref(filename);
    if (!app->xref)
        pdfapp_error(app, -1);

    /*
     * Handle encrypted PDF files
     */

    if (pdf_needspassword(app->xref))
    {
        int okay = pdf_authenticatepassword(app->xref, password);
        while (!okay)
        {
            password = winpassword(app, filename);
            if (!password)
                exit(1);
            okay = pdf_authenticatepassword(app->xref, password);
            if (!okay)
                pdfapp_warn(app, "Invalid password.");
        }
    }

    /*
     * Load meta information
     */

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

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

    /*
     * Start at first page
     */

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

    app->shrinkwrap = 1;
    if (app->pageno < 1)
        app->pageno = 1;
    if (app->zoom < 0.1)
        app->zoom = 0.1;
    if (app->zoom > 3.0)
        app->zoom = 3.0;
    app->rotate = 0;
    app->panx = 0;
    app->pany = 0;

    //code change by kakai
    //highlight color settings
    kno_setHighlightColor(app, 0x00ffff00);
    //code change by kakai

    pdfapp_showpage(app, 1, 1);
}