Example #1
0
static void merge_range(const char *range)
{
	int start, end, i, count;
	pdf_graft_map *graft_map;

	count = pdf_count_pages(ctx, doc_src);
	graft_map = pdf_new_graft_map(ctx, doc_des);

	fz_try(ctx)
	{
		while ((range = fz_parse_page_range(ctx, range, &start, &end, count)))
		{
			if (start < end)
				for (i = start; i <= end; ++i)
					page_merge(i, -1, graft_map);
			else
				for (i = start; i >= end; --i)
					page_merge(i, -1, graft_map);
		}
	}
	fz_always(ctx)
	{
		pdf_drop_graft_map(ctx, graft_map);
	}
	fz_catch(ctx)
	{
		fz_rethrow(ctx);
	}
}
Example #2
0
static void merge_range(char *range)
{
	int page, spage, epage, src_pagecount, des_pagecount;
	char *spec, *dash;
	pdf_graft_map *graft_map;

	src_pagecount = fz_count_pages(ctx, (fz_document*) doc_src);
	des_pagecount = fz_count_pages(ctx, (fz_document*) doc_des);
	spec = fz_strsep(&range, ",");
	graft_map = pdf_new_graft_map(ctx, doc_src);

	fz_try(ctx)
	{
		while (spec)
		{
			dash = strchr(spec, '-');

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

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

			spage = fz_clampi(spage, 1, src_pagecount);
			epage = fz_clampi(epage, 1, src_pagecount);

			if (spage < epage)
				for (page = spage; page <= epage; page++, des_pagecount++)
					page_merge(page, des_pagecount + 1, graft_map);
			else
				for (page = spage; page >= epage; page--, des_pagecount++)
					page_merge(page, des_pagecount + 1, graft_map);
			spec = fz_strsep(&range, ",");
		}
	}
	fz_always(ctx)
	{
		pdf_drop_graft_map(ctx, graft_map);
	}
	fz_catch(ctx)
	{
		fz_rethrow(ctx);
	}
}
Example #3
0
/* Graft object from dst to source */
pdf_obj *
pdf_graft_object(fz_context *ctx, pdf_document *dst, pdf_document *src, pdf_obj *obj_ref, pdf_graft_map *map)
{
	pdf_obj *val, *key;
	pdf_obj *new_obj = NULL;
	pdf_obj *new_dict = NULL;
	pdf_obj *new_array = NULL;
	pdf_obj *ref = NULL;
	fz_buffer *buffer = NULL;
	pdf_graft_map *drop_map = NULL;
	int new_num, src_num, len, i;

	if (map == NULL)
		drop_map = map = pdf_new_graft_map(ctx, src);

	if (pdf_is_indirect(ctx, obj_ref))
	{
		src_num = pdf_to_num(ctx, obj_ref);

		/* Check if we have done this one.  If yes, then drop map (if allocated)
		 * and return our indirect ref */
		if (map->dst_from_src[src_num] != 0)
		{
			int dest_num = map->dst_from_src[src_num];
			pdf_drop_graft_map(ctx, drop_map);
			return pdf_new_indirect(ctx, dst, dest_num, 0);
		}

		fz_var(buffer);
		fz_var(ref);

		fz_try(ctx)
		{
			/* Create new slot for our src object, set the mapping and call again
			 * using the resolved indirect reference */
			new_num = pdf_create_object(ctx, dst);
			map->dst_from_src[src_num] = new_num;
			new_obj = pdf_graft_object(ctx, dst, src, pdf_resolve_indirect(ctx, obj_ref), map);

			/* Return a ref to the new_obj making sure to attach any stream */
			pdf_update_object(ctx, dst, new_num, new_obj);
			pdf_drop_obj(ctx, new_obj);
			ref = pdf_new_indirect(ctx, dst, new_num, 0);
			if (pdf_is_stream(ctx, obj_ref))
			{
				buffer = pdf_load_raw_stream(ctx, src, src_num, 0);
				pdf_update_stream(ctx, dst, ref, buffer, 1);
			}
		}
		fz_always(ctx)
		{
			fz_drop_buffer(ctx, buffer);
			pdf_drop_graft_map(ctx, drop_map);
		}
		fz_catch(ctx)
		{
			pdf_drop_obj(ctx, ref);
			fz_rethrow(ctx);
		}
		return ref;
	}
	else if (pdf_is_dict(ctx, obj_ref))