Esempio n. 1
0
void
fz_write_pbm(fz_context *ctx, fz_bitmap *bitmap, char *filename)
{
	FILE *fp;
	unsigned char *p;
	int h, bytestride;

	fp = fz_fopen(filename, "wb");
	if (!fp)
		fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));

	assert(bitmap->n == 1);

	fprintf(fp, "P4\n%d %d\n", bitmap->w, bitmap->h);

	p = bitmap->samples;

	h = bitmap->h;
	bytestride = (bitmap->w + 7) >> 3;
	while (h--)
	{
		fwrite(p, 1, bytestride, fp);
		p += bitmap->stride;
	}

	fclose(fp);
}
Esempio n. 2
0
void
fz_write_pcl_bitmap(fz_context *ctx, fz_bitmap *bitmap, char *filename, int append, fz_pcl_options *pcl)
{
	FILE *fp;
	fz_output *out = NULL;

	fp = fz_fopen(filename, append ? "ab" : "wb");
	if (!fp)
	{
		fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));
	}

	fz_var(out);

	fz_try(ctx)
	{
		out = fz_new_output_with_file(ctx, fp, 1);
		fz_output_pcl_bitmap(ctx, out, bitmap, pcl);
	}
	fz_always(ctx)
	{
		fz_drop_output(ctx, out);
	}
	fz_catch(ctx)
	{
		fz_rethrow(ctx);
	}
}
Esempio n. 3
0
int
fz_file_exists(fz_context *ctx, const char *path)
{
	FILE *file = fz_fopen(path, "rb");
	if (file)
		fclose(file);
	return !!file;
}
Esempio n. 4
0
fz_output *
fz_new_output_with_path(fz_context *ctx, const char *filename, int append)
{
	fz_output *out = NULL;

	FILE *file = fz_fopen(filename, append ? "ab" : "wb");
	if (!file)
		fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));

	fz_try(ctx)
	{
		out = fz_new_output_with_file_ptr(ctx, file, 1);
	}
	fz_catch(ctx)
	{
		fclose(file);
		fz_rethrow(ctx);
	}
	return out;
}
Esempio n. 5
0
fz_stream *
fz_open_file(fz_context *ctx, const char *name)
{
	FILE *f;
#if defined(_WIN32) || defined(_WIN64)
	char *s = (char*)name;
	wchar_t *wname, *d;
	int c;
	d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t));
	while (*s) {
		s += fz_chartorune(&c, s);
		*d++ = c;
	}
	*d = 0;
	f = _wfopen(wname, L"rb");
	fz_free(ctx, wname);
#else
	f = fz_fopen(name, "rb");
#endif
	if (f == NULL)
		fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s: %s", name, strerror(errno));
	return fz_open_file_ptr(ctx, f);
}
Esempio n. 6
0
static void savefont(pdf_obj *dict, int num)
{
    char namebuf[1024];
    fz_buffer *buf;
    pdf_obj *stream = NULL;
    pdf_obj *obj;
    char *ext = "";
    FILE *f;
    char *fontname = "font";
    int n, len;
    unsigned char *data;

    obj = pdf_dict_get(ctx, dict, PDF_NAME_FontName);
    if (obj)
        fontname = pdf_to_name(ctx, obj);

    obj = pdf_dict_get(ctx, dict, PDF_NAME_FontFile);
    if (obj)
    {
        stream = obj;
        ext = "pfa";
    }

    obj = pdf_dict_get(ctx, dict, PDF_NAME_FontFile2);
    if (obj)
    {
        stream = obj;
        ext = "ttf";
    }

    obj = pdf_dict_get(ctx, dict, PDF_NAME_FontFile3);
    if (obj)
    {
        stream = obj;

        obj = pdf_dict_get(ctx, obj, PDF_NAME_Subtype);
        if (obj && !pdf_is_name(ctx, obj))
            fz_throw(ctx, FZ_ERROR_GENERIC, "invalid font descriptor subtype");

        if (pdf_name_eq(ctx, obj, PDF_NAME_Type1C))
            ext = "cff";
        else if (pdf_name_eq(ctx, obj, PDF_NAME_CIDFontType0C))
            ext = "cid";
        else if (pdf_name_eq(ctx, obj, PDF_NAME_OpenType))
            ext = "otf";
        else
            fz_throw(ctx, FZ_ERROR_GENERIC, "unhandled font type '%s'", pdf_to_name(ctx, obj));
    }

    if (!stream)
    {
        fz_warn(ctx, "unhandled font type");
        return;
    }

    buf = pdf_load_stream(ctx, doc, pdf_to_num(ctx, stream), pdf_to_gen(ctx, stream));

    snprintf(namebuf, sizeof(namebuf), "%s-%04d.%s", fontname, num, ext);
    printf("extracting font %s\n", namebuf);

    f = fz_fopen(namebuf, "wb");
    if (!f)
        fz_throw(ctx, FZ_ERROR_GENERIC, "cannot create font file");

    len = fz_buffer_storage(ctx, buf, &data);
    n = fwrite(data, 1, len, f);
    if (n < len)
        fz_throw(ctx, FZ_ERROR_GENERIC, "cannot write font file");

    if (fclose(f) < 0)
        fz_throw(ctx, FZ_ERROR_GENERIC, "cannot close font file");

    fz_drop_buffer(ctx, buf);
}