示例#1
0
文件: pdf_font.c 项目: iroot/sopdf
fz_error *
pdf_loadfont(pdf_font **fontp, pdf_xref *xref, fz_obj *dict, fz_obj *ref)
{
	fz_error *error;
	char *subtype;

	if ((*fontp = pdf_finditem(xref->store, PDF_KFONT, ref)))
	{
		fz_keepfont((fz_font*)*fontp);
		return fz_okay;
	}

	subtype = fz_toname(fz_dictgets(dict, "Subtype"));
	if (!strcmp(subtype, "Type0"))
		error = loadtype0(fontp, xref, dict, ref);
	else if (!strcmp(subtype, "Type1") || !strcmp(subtype, "MMType1"))
		error = loadsimplefont(fontp, xref, dict, ref);
	else if (!strcmp(subtype, "TrueType"))
		error = loadsimplefont(fontp, xref, dict, ref);
	else if (!strcmp(subtype, "Type3"))
		error = pdf_loadtype3font(fontp, xref, dict, ref);
	else
		return fz_throw("cannot recognize font format %s", subtype);

	if (error)
		return fz_rethrow(error, "cannot load font");

	error = pdf_storeitem(xref->store, PDF_KFONT, ref, *fontp);
	if (error)
		return fz_rethrow(error, "cannot store font resource");

	return fz_okay;
}
static fz_obj *
resolvedest(pdf_xref *xref, fz_obj *dest)
{
	if (fz_isname(dest) || fz_isstring(dest))
	{
		dest = pdf_lookupdest(xref, dest);
		return resolvedest(xref, dest);
	}

	else if (fz_isarray(dest))
	{
		return fz_arrayget(dest, 0);
	}

	else if (fz_isdict(dest))
	{
		dest = fz_dictgets(dest, "D");
		return resolvedest(xref, dest);
	}

	else if (fz_isindirect(dest))
		return dest;

	return nil;
}
示例#3
0
static fz_obj *
resolvedest(pdf_xref *xref, fz_obj *dest)
{
	if (fz_isname(dest))
	{
		dest = fz_dictget(xref->dests, dest);
		if (dest)
			pdf_resolve(&dest, xref); /* XXX */
		return resolvedest(xref, dest);
	}

	else if (fz_isstring(dest))
	{
		dest = fz_dictget(xref->dests, dest);
		if (dest)
			pdf_resolve(&dest, xref); /* XXX */
		return resolvedest(xref, dest);
	}

	else if (fz_isarray(dest))
	{
		return fz_arrayget(dest, 0);
	}

	else if (fz_isdict(dest))
	{
		dest = fz_dictgets(dest, "D");
		return resolvedest(xref, dest);
	}

	else if (fz_isindirect(dest))
		return dest;

	return nil;
}
示例#4
0
fz_error
fz_newlzwe(fz_filter **fp, fz_obj *params)
{
	FZ_NEWFILTER(fz_lzwe, lzw, lzwe);

	lzw->earlychange = 0;

	if (params)
	{
		fz_obj *obj;
		obj = fz_dictgets(params, "EarlyChange");
		if (obj) lzw->earlychange = fz_toint(obj) != 0;
	}

	lzw->bidx = 0;
	lzw->bsave = 0;

	lzw->resume = 0;
	lzw->code = -1;
	lzw->hcode = -1;
	lzw->fcode = -1;

	lzw->codebits = MINBITS;
	lzw->nextcode = LZW_FIRST;
	lzw->oldcode = -1;	/* generates LZW_CLEAR */

	clearhash(lzw);

	return fz_okay;
}
示例#5
0
static void addhexfilter(fz_obj *dict)
{
	fz_obj *f, *dp, *newf, *newdp;
	fz_obj *ahx, *nullobj;

	ahx = fz_newname("ASCIIHexDecode");
	nullobj = fz_newnull();
	newf = newdp = nil;

	f = fz_dictgets(dict, "Filter");
	dp = fz_dictgets(dict, "DecodeParms");

	if (fz_isname(f))
	{
		newf = fz_newarray(2);
		fz_arraypush(newf, ahx);
		fz_arraypush(newf, f);
		f = newf;
		if (fz_isdict(dp))
		{
			newdp = fz_newarray(2);
			fz_arraypush(newdp, nullobj);
			fz_arraypush(newdp, dp);
			dp = newdp;
		}
	}
	else if (fz_isarray(f))
	{
		fz_arrayinsert(f, ahx);
		if (fz_isarray(dp))
			fz_arrayinsert(dp, nullobj);
	}
	else
		f = ahx;

	fz_dictputs(dict, "Filter", f);
	if (dp)
		fz_dictputs(dict, "DecodeParms", dp);

	fz_dropobj(ahx);
	fz_dropobj(nullobj);
	if (newf)
		fz_dropobj(newf);
	if (newdp)
		fz_dropobj(newdp);
}
示例#6
0
static void writexref(void)
{
	fz_obj *trailer;
	fz_obj *obj;
	int startxref;
	int num;

	startxref = ftell(out);

	fprintf(out, "xref\n0 %d\n", xref->len);
	for (num = 0; num < xref->len; num++)
	{
		if (uselist[num])
			fprintf(out, "%010d %05d n \n", ofslist[num], genlist[num]);
		else
			fprintf(out, "%010d %05d f \n", ofslist[num], genlist[num]);
	}
	fprintf(out, "\n");

	trailer = fz_newdict(5);

	obj = fz_newint(xref->len);
	fz_dictputs(trailer, "Size", obj);
	fz_dropobj(obj);

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

	obj = fz_dictgets(xref->trailer, "Root");
	if (obj)
		fz_dictputs(trailer, "Root", obj);

	obj = fz_dictgets(xref->trailer, "ID");
	if (obj)
		fz_dictputs(trailer, "ID", obj);

	fprintf(out, "trailer\n");
	fz_fprintobj(out, trailer, !doexpand);
	fprintf(out, "\n");

	fz_dropobj(trailer);

	fprintf(out, "startxref\n%d\n%%%%EOF\n", startxref);
}
示例#7
0
BKPDF* BKPDF::create(string& file) {
	if (singleton != 0) {
		printf("cannot open more than 1 pdf at the same time\n");
		return singleton;
	}

	reset_allocs();

	BKPDF* b = new BKPDF(file);
	singleton = b;

	fz_setmemorycontext(&bkmem);
	fz_cpudetect();
	fz_accelerate();

	pdfInit();

	PDFContext* ctx = pdfOpen((char*)file.c_str());
	if (ctx == 0) {
		delete b;
		return 0;
	}
	b->ctx = ctx;
	int lastSlash = 0;
	int n = file.size();
	for (int i = 0; i < n; ++i) {
		if (file[i] == '\\')
			lastSlash == i++;
		else if (file[i] == '/')
			lastSlash == i++;
	}
	b->title.assign(file, lastSlash, n - lastSlash);
	if (ctx->xref->info) {
		fz_error *error;
		fz_obj *obj;
		obj = fz_dictgets(ctx->xref->info, "Title");
		if (obj) {
			char *p = NULL;
			error = pdf_toutf8(&p, obj);
			if (error == NULL) {
				b->title = p;
			}
		}
	}

	// Add bookmark support
	//int position = BKBookmark::getLastView(b->filePath);
	//b->setPage(position);

	b->pageError = pdfLoadPage(ctx) != 0;

	FZScreen::resetReps();
	b->redrawBuffer();
	print_allocs();
	lastScrollFlag = BKUser::options.pdfFastScroll;
	return b;
}
示例#8
0
fz_error *
pdf_loadannots(pdf_comment **cp, pdf_link **lp, pdf_xref *xref, fz_obj *annots)
{
	fz_error *error;
	pdf_comment *comment;
	pdf_link *link;
	fz_obj *subtype;
	fz_obj *obj;
	int i;

	comment = nil;
	link = nil;

	pdf_logpage("load annotations {\n");

	for (i = 0; i < fz_arraylen(annots); i++)
	{
		obj = fz_arrayget(annots, i);
		error = pdf_resolve(&obj, xref);
		if (error)
			goto cleanup;

		subtype = fz_dictgets(obj, "Subtype");
		if (!strcmp(fz_toname(subtype), "Link"))
		{
			pdf_link *temp = nil;

			error = pdf_loadlink(&temp, xref, obj);
			fz_dropobj(obj);
			if (error)
				goto cleanup;

			if (temp)
			{
				temp->next = link;
				link = temp;
			}
		}
		else
		{
			error = loadcomment(&comment, xref, obj);
			fz_dropobj(obj);
			if (error)
				goto cleanup;
		}
	}

	pdf_logpage("}\n");

	*cp = comment;
	*lp = link;
	return nil;

cleanup:
	pdf_droplink(link);
	return error;
}
示例#9
0
fz_error*
setPageMediaBox(
    pdf_xref*   pdfXRef,
    fz_obj*     pageObj,
    fz_rect     mediaBox
    )
{
    fz_error    *error;
    fz_obj      *objMedia;
    fz_irect    mRect;
    fz_obj      *objInt;

    // Delete the CropBox. This is done because we are reducing
    // the size of the media box and CropBox is of no use to us
    fz_dictdels(pageObj, "CropBox");
    //objMedia = fz_dictgets(pageObj, "CropBox");
    //if (objMedia == NULL) return fz_throw("no CropBox entry");
    //error = pdf_resolve(&objMedia, pdfXRef);
    //if (error) return fz_rethrow(error, "cannot resolve page bounds");
    //if (! fz_isarray(objMedia)) return fz_throw("cannot find page bounds");
    //fz_rect cRect = pdf_torect(objMedia);

    // Get the media box
    objMedia = fz_dictgets(pageObj, "MediaBox");
    if (objMedia == NULL) return fz_throw("no MediaBox entry");

    error = pdf_resolve(&objMedia, pdfXRef);
    if (error) return fz_rethrow(error, "cannot resolve page bounds");

    if (! fz_isarray(objMedia)) return fz_throw("cannot find page bounds");


    // We have the MediaBox array here
    mRect = fz_roundrect(mediaBox);

    error = fz_newint(&objInt, mRect.x0);
    if (error) return fz_rethrow(error, "cannot allocate int"); 
    fz_arrayput(objMedia, 0, objInt);
    fz_dropobj(objInt);

    error = fz_newint(&objInt, mRect.y0);
    if (error) return fz_rethrow(error, "cannot allocate int"); 
    fz_arrayput(objMedia, 1, objInt);
    fz_dropobj(objInt);

    error = fz_newint(&objInt, mRect.x1);
    if (error) return fz_rethrow(error, "cannot allocate int"); 
    fz_arrayput(objMedia, 2, objInt);
    fz_dropobj(objInt);

    error = fz_newint(&objInt, mRect.y1);
    if (error) return fz_rethrow(error, "cannot allocate int"); 
    fz_arrayput(objMedia, 3, objInt);
    fz_dropobj(objInt);

    return NULL;
}
示例#10
0
文件: pdftool.c 项目: Limsik/e17
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);
}
示例#11
0
static fz_colorspace *
loadcalgray(pdf_xref *xref, fz_obj *dict)
{
	struct calgray *cs;
	fz_obj *tmp;

	cs = fz_malloc(sizeof(struct calgray));

	pdf_logrsrc("load CalGray\n");

	initcs((fz_colorspace*)cs, "CalGray", 1, graytoxyz, xyztogray, nil);

	cs->white[0] = 1.0;
	cs->white[1] = 1.0;
	cs->white[2] = 1.0;

	cs->black[0] = 0.0;
	cs->black[1] = 0.0;
	cs->black[2] = 0.0;

	cs->gamma = 1.0;

	tmp = fz_dictgets(dict, "WhitePoint");
	if (fz_isarray(tmp))
	{
		cs->white[0] = fz_toreal(fz_arrayget(tmp, 0));
		cs->white[1] = fz_toreal(fz_arrayget(tmp, 1));
		cs->white[2] = fz_toreal(fz_arrayget(tmp, 2));
	}

	tmp = fz_dictgets(dict, "BlackPoint");
	if (fz_isarray(tmp))
	{
		cs->black[0] = fz_toreal(fz_arrayget(tmp, 0));
		cs->black[1] = fz_toreal(fz_arrayget(tmp, 1));
		cs->black[2] = fz_toreal(fz_arrayget(tmp, 2));
	}

	tmp = fz_dictgets(dict, "Gamma");
	if (fz_isreal(tmp))
		cs->gamma = fz_toreal(tmp);

	return (fz_colorspace*) cs;
}
fz_error
pdf_loadannots(pdf_comment **cp, pdf_link **lp, pdf_xref *xref, fz_obj *annots)
{
	fz_error error;
	pdf_comment *comment;
	pdf_link *link;
	fz_obj *subtype;
	fz_obj *obj;
	int i;

	comment = nil;
	link = nil;

	pdf_logpage("load annotations {\n");

	for (i = 0; i < fz_arraylen(annots); i++)
	{
		obj = fz_arrayget(annots, i);

		subtype = fz_dictgets(obj, "Subtype");
		if (fz_isname(subtype) && !strcmp(fz_toname(subtype), "Link"))
		{
			pdf_link *temp = nil;

			error = pdf_loadlink(&temp, xref, obj);
			if (error)
			{
				if (link)
					pdf_droplink(link);
				return fz_rethrow(error, "cannot load annotation link");
			}

			if (temp)
			{
				temp->next = link;
				link = temp;
			}
		}
		else
		{
			error = loadcomment(&comment, xref, obj);
			if (error)
			{
				if (link)
					pdf_droplink(link);
				return fz_rethrow(error, "cannot load annotation comment");
			}
		}
	}

	pdf_logpage("}\n");

	*cp = comment;
	*lp = link;
	return fz_okay;
}
示例#13
0
文件: pdf_font.c 项目: iroot/sopdf
static fz_error *
loadtype0(pdf_font **fontp, pdf_xref *xref, fz_obj *dict, fz_obj *ref)
{
	fz_error *error;
	fz_obj *dfonts;
	fz_obj *dfont;
	fz_obj *subtype;
	fz_obj *encoding;
	fz_obj *tounicode;

	dfonts = fz_dictgets(dict, "DescendantFonts");
	error = pdf_resolve(&dfonts, xref);
	if (error)
		return fz_rethrow(error, "cannot find DescendantFonts");

	dfont = fz_arrayget(dfonts, 0);
	error = pdf_resolve(&dfont, xref);
	if (error)
	{
		fz_dropobj(dfonts);
		return fz_rethrow(error, "cannot find descendant font");
	}

	subtype = fz_dictgets(dfont, "Subtype");
	encoding = fz_dictgets(dict, "Encoding");
	tounicode = fz_dictgets(dict, "ToUnicode");

	if (!strcmp(fz_toname(subtype), "CIDFontType0"))
		error = loadcidfont(fontp, xref, dfont, ref, encoding, tounicode);
	else if (!strcmp(fz_toname(subtype), "CIDFontType2"))
		error = loadcidfont(fontp, xref, dfont, ref, encoding, tounicode);
	else
		error = fz_throw("syntaxerror: unknown cid font type");

	fz_dropobj(dfont);
	fz_dropobj(dfonts);

	if (error)
		return fz_rethrow(error, "cannot load descendant font");

	return fz_okay;
}
示例#14
0
static int
pdf_xobjectusesblending(fz_obj *dict)
{
	fz_obj *obj;

	obj = fz_dictgets(dict, "Resources");
	if (fz_isdict(obj) && pdf_resourcesuseblending(obj))
		return 1;

	return 0;
}
示例#15
0
static int
pdf_extgstateusesblending(fz_obj *dict)
{
	fz_obj *obj;

	obj = fz_dictgets(dict, "BM");
	if (fz_isname(obj) && strcmp(fz_toname(obj), "Normal"))
		return 1;

	return 0;
}
static fz_error
pdf_loadxref(pdf_xref *xref, char *buf, int bufsize)
{
    fz_error error;
    fz_obj *size;
    int i;

    error = pdf_loadversion(xref);
    if (error)
        return fz_rethrow(error, "cannot read version marker");

    error = pdf_readstartxref(xref);
    if (error)
        return fz_rethrow(error, "cannot read startxref");

    error = pdf_readtrailer(xref, buf, bufsize);
    if (error)
        return fz_rethrow(error, "cannot read trailer");

    size = fz_dictgets(xref->trailer, "Size");
    if (!size)
        return fz_throw("trailer missing Size entry");

    pdf_logxref("\tsize %d at 0x%x\n", fz_toint(size), xref->startxref);

    xref->len = fz_toint(size);
    xref->cap = xref->len + 1; /* for hack to allow broken pdf generators with off-by-one errors */
    xref->table = fz_malloc(xref->cap * sizeof(pdf_xrefentry));
    for (i = 0; i < xref->cap; i++)
    {
        xref->table[i].ofs = 0;
        xref->table[i].gen = 0;
        xref->table[i].stmofs = 0;
        xref->table[i].obj = nil;
        xref->table[i].type = 0;
    }

    error = pdf_readxrefsections(xref, xref->startxref, buf, bufsize);
    if (error)
        return fz_rethrow(error, "cannot read xref");

    /* broken pdfs where first object is not free */
    if (xref->table[0].type != 'f')
        return fz_throw("first object in xref is not free");

    /* broken pdfs where object offsets are out of range */
    for (i = 0; i < xref->len; i++)
        if (xref->table[i].type == 'n')
            if (xref->table[i].ofs <= 0 || xref->table[i].ofs >= xref->filesize)
                return fz_throw("object offset out of range: %d", xref->table[i].ofs);

    return fz_okay;
}
fz_error *
fz_newpredict(fz_filter **fp, fz_obj *params, int encode)
{
    fz_obj *obj;

    FZ_NEWFILTER(fz_predict, p, predict);

    p->encode = encode;

    p->predictor = 1;
    p->columns = 1;
    p->colors = 1;
    p->bpc = 8;

    obj = fz_dictgets(params, "Predictor");
    if (obj) p->predictor = fz_toint(obj);

    obj = fz_dictgets(params, "Columns");
    if (obj) p->columns = fz_toint(obj);

    obj = fz_dictgets(params, "Colors");
    if (obj) p->colors = fz_toint(obj);

    obj = fz_dictgets(params, "BitsPerComponent");
    if (obj) p->bpc = fz_toint(obj);

    p->stride = (p->bpc * p->colors * p->columns + 7) / 8;
    p->bpp = (p->bpc * p->colors + 7) / 8;

    if (p->predictor >= 10) {
        p->ref = fz_malloc(p->stride);
        if (!p->ref) { fz_free(p); return fz_throw("outofmem: predictor reference buffer"); }
        memset(p->ref, 0, p->stride);
    }
    else {
        p->ref = nil;
    }

    return fz_okay;
}
示例#18
0
fz_error *
pdf_loadnametrees(pdf_xref *xref)
{
	fz_error *error;
	fz_obj *names;
	fz_obj *dests;

	/* PDF 1.1 */
	dests = fz_dictgets(xref->root, "Dests");
	if (dests)
	{
		error = pdf_resolve(&dests, xref);
		if (error)
			return error;
		xref->dests = dests;
		return nil;
	}

	/* PDF 1.2 */
	names = fz_dictgets(xref->root, "Names");
	if (names)
	{
		error = pdf_resolve(&names, xref);
		if (error)
			return error;
		dests = fz_dictgets(names, "Dests");
		if (dests)
		{
			error = pdf_loadnametree(&xref->dests, xref, dests);
			if (error)
			{
				fz_dropobj(names);
				return error;
			}
		}
		fz_dropobj(names);
	}

	return nil;
}
示例#19
0
fz_error *
pdf_loadoutline(pdf_outline **nodep, pdf_xref *xref)
{
	fz_error *error;
	pdf_outline *node;
	fz_obj *obj;
	fz_obj *first;

	pdf_logpage("load outlines {\n");

	node = nil;

	obj = fz_dictgets(xref->root, "Outlines");
	if (obj)
	{
		error = pdf_resolve(&obj, xref);
		if (error)
			return error;

		first = fz_dictgets(obj, "First");
		if (first)
		{
			error = pdf_resolve(&first, xref);
			fz_dropobj(obj);
			if (error)
				return error;
			error = loadoutline(&node, xref, first);
			fz_dropobj(first);
			if (error)
				return error;
		}
		else
			fz_dropobj(obj);
	}

	pdf_logpage("}\n");

	*nodep = node;
	return nil;
}
static fz_error
pdf_readxrefsections(pdf_xref *xref, int ofs, char *buf, int cap)
{
    fz_error error;
    fz_obj *trailer;
    fz_obj *prev;
    fz_obj *xrefstm;

    error = pdf_readxref(&trailer, xref, ofs, buf, cap);
    if (error)
        return fz_rethrow(error, "cannot read xref section");

    /* FIXME: do we overwrite free entries properly? */
    xrefstm = fz_dictgets(trailer, "XRefStm");
    if (xrefstm)
    {
        pdf_logxref("load xrefstm\n");
        error = pdf_readxrefsections(xref, fz_toint(xrefstm), buf, cap);
        if (error)
        {
            fz_dropobj(trailer);
            return fz_rethrow(error, "cannot read /XRefStm xref section");
        }
    }

    prev = fz_dictgets(trailer, "Prev");
    if (prev)
    {
        pdf_logxref("load prev at 0x%x\n", fz_toint(prev));
        error = pdf_readxrefsections(xref, fz_toint(prev), buf, cap);
        if (error)
        {
            fz_dropobj(trailer);
            return fz_rethrow(error, "cannot read /Prev xref section");
        }
    }

    fz_dropobj(trailer);
    return fz_okay;
}
示例#21
0
static fz_error
pdf_repairobjstm(pdf_xref *xref, int num, int gen)
{
	fz_error error;
	fz_obj *obj;
	fz_stream *stm;
	int tok;
	int i, n, count;
	char buf[256];

	error = pdf_loadobject(&obj, xref, num, gen);
	if (error)
		return fz_rethrow(error, "cannot load object stream object (%d %d R)", num, gen);

	count = fz_toint(fz_dictgets(obj, "N"));

	fz_dropobj(obj);

	error = pdf_openstream(&stm, xref, num, gen);
	if (error)
		return fz_rethrow(error, "cannot open object stream object (%d %d R)", num, gen);

	for (i = 0; i < count; i++)
	{
		error = pdf_lex(&tok, stm, buf, sizeof buf, &n);
		if (error || tok != PDF_TINT)
		{
			fz_close(stm);
			return fz_rethrow(error, "corrupt object stream (%d %d R)", num, gen);
		}

		n = atoi(buf);
		if (n >= xref->len)
			pdf_resizexref(xref, n + 1);

		xref->table[n].ofs = num;
		xref->table[n].gen = i;
		xref->table[n].stmofs = 0;
		xref->table[n].obj = nil;
		xref->table[n].type = 'o';

		error = pdf_lex(&tok, stm, buf, sizeof buf, &n);
		if (error || tok != PDF_TINT)
		{
			fz_close(stm);
			return fz_rethrow(error, "corrupt object stream (%d %d R)", num, gen);
		}
	}

	fz_close(stm);
	return fz_okay;
}
示例#22
0
int
epdf_index_item_page_get(const Epdf_Document *doc, const Epdf_Index_Item *item)
{
   fz_obj *dest;
   int p;
   int n;
   int g;

   if (!item || !item->link)
     return -1;

   if (PDF_LGOTO != item->link->kind)
     return -1;

   dest = item->link->dest;
   p = 0;
   if (fz_isint(dest))
     {
        p = fz_toint(dest);
        return p;
     }
   if (fz_isdict(dest))
     {
        /* The destination is linked from a Go-To action's D array */
       fz_obj *D;

       D = fz_dictgets(dest, "D");
       if (D && fz_isarray(D))
         dest = fz_arrayget(D, 0);
     }

   n = fz_tonum(dest);
   g = fz_togen(dest);

   for (p = 1; p <= epdf_document_page_count_get(doc); p++)
     {
        fz_obj *page;
        int np;
        int gp;

        page = pdf_getpageobject(doc->xref, p);
        if (!page)
          continue;

        np = fz_tonum(page);
        gp = fz_togen(page);
        if (n == np && g == gp)
          return p-1;
    }

   return 0;
}
示例#23
0
static int
pdf_resourcesuseblending(fz_obj *rdb)
{
	fz_obj *dict;
	fz_obj *tmp;
	int i;

	/* stop on cyclic resource dependencies */
	if (fz_dictgets(rdb, ".useBM"))
		return fz_tobool(fz_dictgets(rdb, ".useBM"));

	tmp = fz_newbool(0);
	fz_dictputs(rdb, ".useBM", tmp);
	fz_dropobj(tmp);

	dict = fz_dictgets(rdb, "ExtGState");
	for (i = 0; i < fz_dictlen(dict); i++)
		if (pdf_extgstateusesblending(fz_dictgetval(dict, i)))
			goto found;

	dict = fz_dictgets(rdb, "Pattern");
	for (i = 0; i < fz_dictlen(dict); i++)
		if (pdf_patternusesblending(fz_dictgetval(dict, i)))
			goto found;

	dict = fz_dictgets(rdb, "XObject");
	for (i = 0; i < fz_dictlen(dict); i++)
		if (pdf_xobjectusesblending(fz_dictgetval(dict, i)))
			goto found;

	return 0;

found:
	tmp = fz_newbool(1);
	fz_dictputs(rdb, ".useBM", tmp);
	fz_dropobj(tmp);
	return 1;
}
示例#24
0
int
pdf_isjpximage(fz_obj *dict)
{
	fz_obj *filter;
	int i;

	filter = fz_dictgets(dict, "Filter");
	if (!strcmp(fz_toname(filter), "JPXDecode"))
		return 1;
	for (i = 0; i < fz_arraylen(filter); i++)
		if (!strcmp(fz_toname(fz_arrayget(filter, i)), "JPXDecode"))
			return 1;
	return 0;
}
示例#25
0
pdf_outline *
pdf_loadoutline(pdf_xref *xref)
{
	pdf_outline *node;
	fz_obj *root, *obj, *first;

	pdf_logpage("load outlines {\n");

	node = nil;

	root = fz_dictgets(xref->trailer, "Root");
	obj = fz_dictgets(root, "Outlines");
	if (obj)
	{
		first = fz_dictgets(obj, "First");
		if (first)
			node = pdf_loadoutlineimp(xref, first);
	}

	pdf_logpage("}\n");

	return node;
}
示例#26
0
static fz_error
gathershadings(int page, fz_obj *pageobj, fz_obj *dict)
{
	int i;

	for (i = 0; i < fz_dictlen(dict); i++)
	{
		fz_obj *ref;
		fz_obj *shade;
		fz_obj *type;
		int k;

		shade = ref = fz_dictgetval(dict, i);
		if (!fz_isdict(shade))
			return fz_throw("not a shading dict (%d %d R)", fz_tonum(ref), fz_togen(ref));

		type = fz_dictgets(shade, "ShadingType");
		if (!fz_isint(type) || fz_toint(type) < 1 || fz_toint(type) > 7)
		{
			fz_warn("not a shading type (%d %d R)", fz_tonum(ref), fz_togen(ref));
			type = nil;
		}

		for (k = 0; k < shadings; k++)
			if (fz_tonum(shading[k]->ref) == fz_tonum(ref) &&
				fz_togen(shading[k]->ref) == fz_togen(ref))
				break;

		if (k < shadings)
			continue;

		shadings++;

		shading = fz_realloc(shading, shadings * sizeof (struct info *));
		if (!shading)
			return fz_throw("out of memory");

		shading[shadings - 1] = fz_malloc(sizeof (struct info));
		if (!shading[shadings - 1])
			return fz_throw("out of memory");

		shading[shadings - 1]->page = page;
		shading[shadings - 1]->pageobj = pageobj;
		shading[shadings - 1]->ref = ref;
		shading[shadings - 1]->u.shading.type = type;
	}

	return fz_okay;
}
示例#27
0
fz_filter *
fz_newdctd(fz_obj *params)
{
	fz_obj *obj;
	int colortransform;

	FZ_NEWFILTER(fz_dctd, d, dctd);

	colortransform = -1; /* "unset" */

	if (params)
	{
		obj = fz_dictgets(params, "ColorTransform");
		if (obj)
			colortransform = fz_toint(obj);
	}

	d->colortransform = colortransform;
	d->stage = 0;

	/* setup error callback first thing */
	myiniterr(&d->err);
	d->cinfo.err = (struct jpeg_error_mgr*) &d->err;

	if (setjmp(d->err.jb))
		fz_warn("cannot initialise jpeg: %s", d->err.msg);

	/* create decompression object. this zeroes cinfo except for err. */
	jpeg_create_decompress(&d->cinfo);

	/* prepare source manager */
	d->cinfo.src = (struct jpeg_source_mgr *)&d->src;
	d->src.super.init_source = myinitsource;
	d->src.super.fill_input_buffer = myfillinput;
	d->src.super.skip_input_data = myskipinput;
	d->src.super.resync_to_restart = jpeg_resync_to_restart;
	d->src.super.term_source = mytermsource;

	d->src.super.bytes_in_buffer = 0;
	d->src.super.next_input_byte = nil;
	d->src.skip = 0;

	/* speed up jpeg decoding a bit */
	d->cinfo.dct_method = JDCT_FASTEST;
	d->cinfo.do_fancy_upsampling = FALSE;

	return (fz_filter *)d;
}
示例#28
0
文件: pdf_stream.c 项目: iroot/sopdf
/*
 * Build a filter for reading raw stream data.
 * This is a null filter to constrain reading to the
 * stream length, followed by a decryption filter.
 */
static fz_error *
buildrawfilter(fz_filter **filterp, pdf_xref *xref, fz_obj *stmobj, int oid, int gen)
{
	fz_error *error;
	fz_filter *base;
	fz_obj *stmlen;
	int len;

	stmlen = fz_dictgets(stmobj, "Length");
	error = pdf_resolve(&stmlen, xref);
	if (error)
		return fz_rethrow(error, "cannot resolve stream /Length");
	len = fz_toint(stmlen);
	fz_dropobj(stmlen);

	error = fz_newnullfilter(&base, len);
	if (error)
		return fz_rethrow(error, "cannot create null filter");

	if (xref->crypt)
	{
		fz_filter *crypt;
		fz_filter *pipe;

		error = pdf_cryptstream(&crypt, xref->crypt, oid, gen);
		if (error)
		{
			fz_dropfilter(base);
			return fz_rethrow(error, "cannot create decryption filter");
		}

		error = fz_newpipeline(&pipe, base, crypt);
		fz_dropfilter(base);
		fz_dropfilter(crypt);
		if (error)
			return fz_rethrow(error, "cannot create pipeline filter");

		*filterp = pipe;
	}
	else
	{
		*filterp = base;
	}

	return fz_okay;
}
示例#29
0
fz_stream *
fz_opendctd(fz_stream *chain, fz_obj *params)
{
	fz_dctd *state;
	fz_obj *obj;

	state = fz_malloc(sizeof(fz_dctd));
	memset(state, 0, sizeof(fz_dctd));
	state->chain = chain;
	state->colortransform = -1; /* unset */
	state->init = 0;

	obj = fz_dictgets(params, "ColorTransform");
	if (obj)
		state->colortransform = fz_toint(obj);

	return fz_newstream(state, readdctd, closedctd);
}
示例#30
0
static pdf_outline *
pdf_loadoutlineimp(pdf_xref *xref, fz_obj *dict)
{
	pdf_outline *node;
	fz_obj *obj;

	if (fz_isnull(dict))
		return nil;

	node = fz_malloc(sizeof(pdf_outline));
	node->title = nil;
	node->link = nil;
	node->child = nil;
	node->next = nil;
	node->count = 0;

	pdf_logpage("load outline {\n");

	obj = fz_dictgets(dict, "Title");
	if (obj)
	{
		node->title = pdf_toutf8(obj);
		pdf_logpage("title %s\n", node->title);
	}

	obj = fz_dictgets(dict, "Count");
	if (obj)
	{
		node->count = fz_toint(obj);
	}

	if (fz_dictgets(dict, "Dest") || fz_dictgets(dict, "A"))
	{
		node->link = pdf_loadlink(xref, dict);
	}

	obj = fz_dictgets(dict, "First");
	if (obj)
	{
		node->child = pdf_loadoutlineimp(xref, obj);
	}

	pdf_logpage("}\n");

	obj = fz_dictgets(dict, "Next");
	if (obj)
	{
		node->next = pdf_loadoutlineimp(xref, obj);
	}

	return node;
}