Exemple #1
0
void
fz_write_base64(fz_context *ctx, fz_output *out, const unsigned char *data, int size, int newline)
{
	static const char set[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	int i;
	for (i = 0; i + 3 < size; i += 3)
	{
		int c = data[i];
		int d = data[i+1];
		int e = data[i+2];
		if (newline && (i & 15) == 0)
			fz_write_byte(ctx, out, '\n');
		fz_write_byte(ctx, out, set[c>>2]);
		fz_write_byte(ctx, out, set[((c&3)<<4)|(d>>4)]);
		fz_write_byte(ctx, out, set[((d&15)<<2)|(e>>6)]);
		fz_write_byte(ctx, out, set[e&63]);
	}
	if (size - i == 2)
	{
		int c = data[i];
		int d = data[i+1];
		fz_write_byte(ctx, out, set[c>>2]);
		fz_write_byte(ctx, out, set[((c&3)<<4)|(d>>4)]);
		fz_write_byte(ctx, out, set[((d&15)<<2)]);
		fz_write_byte(ctx, out, '=');
	}
Exemple #2
0
void
fz_print_stext_page_as_text(fz_context *ctx, fz_output *out, fz_stext_page *page)
{
	fz_stext_block *block;
	fz_stext_line *line;
	fz_stext_char *ch;
	char utf[10];
	int i, n;

	for (block = page->first_block; block; block = block->next)
	{
		if (block->type == FZ_STEXT_BLOCK_TEXT)
		{
			for (line = block->u.t.first_line; line; line = line->next)
			{
				for (ch = line->first_char; ch; ch = ch->next)
				{
					n = fz_runetochar(utf, ch->c);
					for (i = 0; i < n; i++)
						fz_write_byte(ctx, out, utf[i]);
				}
				fz_write_string(ctx, out, "\n");
			}
			fz_write_string(ctx, out, "\n");
		}
	}
}
Exemple #3
0
void
fz_print_stext_block_as_html(fz_context *ctx, fz_output *out, fz_stext_block *block)
{
	fz_stext_line *line;
	fz_stext_char *ch;
	int x, y;

	fz_font *font = NULL;
	float size = 0;
	int sup = 0;

	for (line = block->u.t.first_line; line; line = line->next)
	{
		x = line->bbox.x0;
		y = line->bbox.y0;

		fz_write_printf(ctx, out, "<p style=\"top:%dpt;left:%dpt;\">", y, x);
		font = NULL;

		for (ch = line->first_char; ch; ch = ch->next)
		{
			int ch_sup = detect_super_script(line, ch);
			if (ch->font != font || ch->size != size)
			{
				if (font)
					fz_print_style_end_html(ctx, out, font, size, sup);
				font = ch->font;
				size = ch->size;
				sup = ch_sup;
				fz_print_style_begin_html(ctx, out, font, size, sup);
			}

			switch (ch->c)
			{
			default:
				if (ch->c >= 32 && ch->c <= 127)
					fz_write_byte(ctx, out, ch->c);
				else
					fz_write_printf(ctx, out, "&#x%x;", ch->c);
				break;
			case '<': fz_write_string(ctx, out, "&lt;"); break;
			case '>': fz_write_string(ctx, out, "&gt;"); break;
			case '&': fz_write_string(ctx, out, "&amp;"); break;
			case '"': fz_write_string(ctx, out, "&quot;"); break;
			case '\'': fz_write_string(ctx, out, "&apos;"); break;
			}
		}

		if (font)
			fz_print_style_end_html(ctx, out, font, size, sup);

		fz_write_string(ctx, out, "</p>\n");
	}
}
Exemple #4
0
static void
fz_write_pdf_string(fz_context *ctx, fz_output *out, const unsigned char *str, int len)
{
	int i;

	for (i = 0; i < len; ++i)
		if (str[i] < 32 || str[i] >= 127)
			break;

	if (i < len)
	{
		fz_write_byte(ctx, out, '<');
		for (i = 0; i < len; ++i)
		{
			unsigned char c = str[i];
			fz_write_byte(ctx, out, "0123456789abcdef"[(c>>4)&15]);
			fz_write_byte(ctx, out, "0123456789abcdef"[(c)&15]);
		}
		fz_write_byte(ctx, out, '>');
	}
Exemple #5
0
void
fz_output_pcl(fz_output *out, const fz_pixmap *pixmap, fz_pcl_options *pcl)
{
    //unsigned char *sp;
    //int y, x, sn, dn, ss;
    fz_context *ctx;

    if (!out || !pixmap)
        return;

    ctx = out->ctx;

    if (pixmap->n != 1 && pixmap->n != 2 && pixmap->n != 4)
        fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or rgb to write as pcl");

    pcl_header(out, pcl, 1, pixmap->xres);

#if 0
    sn = pixmap->n;
    dn = pixmap->n;
    if (dn == 2 || dn == 4)
        dn--;

    /* Now output the actual bitmap, using a packbits like compression */
    sp = pixmap->samples;
    ss = pixmap->w * sn;
    y = 0;
    while (y < pixmap->h)
    {
        int yrep;

        assert(sp == pixmap->samples + y * ss);

        /* Count the number of times this line is repeated */
        for (yrep = 1; yrep < 256 && y+yrep < pixmap->h; yrep++)
        {
            if (memcmp(sp, sp + yrep * ss, ss) != 0)
                break;
        }
        fz_write_byte(out, yrep-1);

        /* Encode the line */
        x = 0;
        while (x < pixmap->w)
        {
            int d;

            assert(sp == pixmap->samples + y * ss + x * sn);

            /* How far do we have to look to find a repeated value? */
            for (d = 1; d < 128 && x+d < pixmap->w; d++)
            {
                if (memcmp(sp + (d-1)*sn, sp + d*sn, sn) == 0)
                    break;
            }
            if (d == 1)
            {
                int xrep;

                /* We immediately have a repeat (or we've hit
                 * the end of the line). Count the number of
                 * times this value is repeated. */
                for (xrep = 1; xrep < 128 && x+xrep < pixmap->w; xrep++)
                {
                    if (memcmp(sp, sp + xrep*sn, sn) != 0)
                        break;
                }
                fz_write_byte(out, xrep-1);
                fz_write(out, sp, dn);
                sp += sn*xrep;
                x += xrep;
            }
            else
            {
                fz_write_byte(out, 257-d);
                x += d;
                while (d > 0)
                {
                    fz_write(out, sp, dn);
                    sp += sn;
                    d--;
                }
            }
        }

        /* Move to the next line */
        sp += ss*(yrep-1);
        y += yrep;
    }
#endif
}
Exemple #6
0
static void
pwg_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *samples)
{
	pwg_band_writer *writer = (pwg_band_writer *)writer_;
	fz_output *out = writer->super.out;
	int w = writer->super.w;
	int h = writer->super.h;
	const unsigned char *sp = samples;
	int n = writer->super.n;
	int ss = w * n;
	int y, x;

	/* Now output the actual bitmap, using a packbits like compression */
	y = 0;
	while (y < h)
	{
		int yrep;

		assert(sp == samples + y * stride);

		/* Count the number of times this line is repeated */
		for (yrep = 1; yrep < 256 && y+yrep < h; yrep++)
		{
			if (memcmp(sp, sp + yrep * stride, ss) != 0)
				break;
		}
		fz_write_byte(ctx, out, yrep-1);

		/* Encode the line */
		x = 0;
		while (x < w)
		{
			int d;

			assert(sp == samples + y * stride + x * n);

			/* How far do we have to look to find a repeated value? */
			for (d = 1; d < 128 && x+d < w; d++)
			{
				if (memcmp(sp + (d-1)*n, sp + d*n, n) == 0)
					break;
			}
			if (d == 1)
			{
				int xrep;

				/* We immediately have a repeat (or we've hit
				 * the end of the line). Count the number of
				 * times this value is repeated. */
				for (xrep = 1; xrep < 128 && x+xrep < w; xrep++)
				{
					if (memcmp(sp, sp + xrep*n, n) != 0)
						break;
				}
				fz_write_byte(ctx, out, xrep-1);
				fz_write_data(ctx, out, sp, n);
				sp += n*xrep;
				x += xrep;
			}
			else
			{
				fz_write_byte(ctx, out, 257-d);
				x += d;
				while (d > 0)
				{
					fz_write_data(ctx, out, sp, n);
					sp += n;
					d--;
				}
			}
		}

		/* Move to the next line */
		sp += stride*(yrep-1);
		y += yrep;
	}
}
Exemple #7
0
static void
pwg_write_mono_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *samples)
{
	pwg_band_writer *writer = (pwg_band_writer *)writer_;
	fz_output *out = writer->super.out;
	int w = writer->super.w;
	int h = writer->super.h;
	const unsigned char *sp;
	int y, x;
	int byte_width;

	/* Now output the actual bitmap, using a packbits like compression */
	sp = samples;
	byte_width = (w+7)/8;
	y = 0;
	while (y < band_height)
	{
		int yrep;

		assert(sp == samples + y * stride);

		/* Count the number of times this line is repeated */
		for (yrep = 1; yrep < 256 && y+yrep < h; yrep++)
		{
			if (memcmp(sp, sp + yrep * stride, byte_width) != 0)
				break;
		}
		fz_write_byte(ctx, out, yrep-1);

		/* Encode the line */
		x = 0;
		while (x < byte_width)
		{
			int d;

			assert(sp == samples + y * stride + x);

			/* How far do we have to look to find a repeated value? */
			for (d = 1; d < 128 && x+d < byte_width; d++)
			{
				if (sp[d-1] == sp[d])
					break;
			}
			if (d == 1)
			{
				int xrep;

				/* We immediately have a repeat (or we've hit
				 * the end of the line). Count the number of
				 * times this value is repeated. */
				for (xrep = 1; xrep < 128 && x+xrep < byte_width; xrep++)
				{
					if (sp[0] != sp[xrep])
						break;
				}
				fz_write_byte(ctx, out, xrep-1);
				fz_write_data(ctx, out, sp, 1);
				sp += xrep;
				x += xrep;
			}
			else
			{
				fz_write_byte(ctx, out, 257-d);
				fz_write_data(ctx, out, sp, d);
				sp += d;
				x += d;
			}
		}

		/* Move to the next line */
		sp += stride*yrep - byte_width;
		y += yrep;
	}
}
Exemple #8
0
static void
fz_write_emit(fz_context *ctx, void *out, int c)
{
	fz_write_byte(ctx, out, c);
}
Exemple #9
0
void
fz_output_pwg_bitmap_page(fz_context *ctx, fz_output *out, const fz_bitmap *bitmap, const fz_pwg_options *pwg)
{
	unsigned char *sp;
	int y, x, ss;
	int byte_width;

	if (!out || !bitmap)
		return;

	output_header(ctx, out, pwg, bitmap->xres, bitmap->yres, bitmap->w, bitmap->h, 1);

	/* Now output the actual bitmap, using a packbits like compression */
	sp = bitmap->samples;
	ss = bitmap->stride;
	byte_width = (bitmap->w+7)/8;
	y = 0;
	while (y < bitmap->h)
	{
		int yrep;

		assert(sp == bitmap->samples + y * ss);

		/* Count the number of times this line is repeated */
		for (yrep = 1; yrep < 256 && y+yrep < bitmap->h; yrep++)
		{
			if (memcmp(sp, sp + yrep * ss, byte_width) != 0)
				break;
		}
		fz_write_byte(ctx, out, yrep-1);

		/* Encode the line */
		x = 0;
		while (x < byte_width)
		{
			int d;

			assert(sp == bitmap->samples + y * ss + x);

			/* How far do we have to look to find a repeated value? */
			for (d = 1; d < 128 && x+d < byte_width; d++)
			{
				if (sp[d-1] == sp[d])
					break;
			}
			if (d == 1)
			{
				int xrep;

				/* We immediately have a repeat (or we've hit
				 * the end of the line). Count the number of
				 * times this value is repeated. */
				for (xrep = 1; xrep < 128 && x+xrep < byte_width; xrep++)
				{
					if (sp[0] != sp[xrep])
						break;
				}
				fz_write_byte(ctx, out, xrep-1);
				fz_write(ctx, out, sp, 1);
				sp += xrep;
				x += xrep;
			}
			else
			{
				fz_write_byte(ctx, out, 257-d);
				fz_write(ctx, out, sp, d);
				sp += d;
				x += d;
			}
		}

		/* Move to the next line */
		sp += ss*yrep - byte_width;
		y += yrep;
	}
}
Exemple #10
0
void
fz_save_gproof(fz_context *ctx, const char *pdf_file, fz_document *doc, const char *filename, int res,
				const char *print_profile, const char *display_profile)
{
	int i;
	int num_pages = fz_count_pages(ctx, doc);
	fz_output *out;
	fz_page *page = NULL;

	fz_var(page);

	if (num_pages <= 0)
		fz_throw(ctx, FZ_ERROR_GENERIC, "Cannot write a 0 page GProof skeleton file");

	out = fz_new_output_with_path(ctx, filename, 0);

	fz_try(ctx)
	{
		/* File Signature: GPRO */
		fz_write_int32_le(ctx, out, 0x4f525047);

		/* Version = 1 */
		fz_write_byte(ctx, out, 1);
		fz_write_byte(ctx, out, 0);

		/* Resolution */
		fz_write_int32_le(ctx, out, res);

		/* Num Pages */
		fz_write_int32_le(ctx, out, num_pages);

		for (i = 0; i < num_pages; i++)
		{
			fz_rect rect;
			int w, h;

			page = fz_load_page(ctx, doc, i);
			fz_bound_page(ctx, page, &rect);
			fz_drop_page(ctx, page);
			page = NULL;

			/* Same lack of rounding as gs uses */
			w = (int)((rect.x1 - rect.x0) * res / 72.0);
			h = (int)((rect.y1 - rect.y0) * res / 72.0);
			fz_write_int32_le(ctx, out, w);
			fz_write_int32_le(ctx, out, h);
		}

		/* Filenames */
		fz_write(ctx, out, pdf_file, strlen(pdf_file)+1);
		fz_write(ctx, out, print_profile, strlen(print_profile) + 1);
		fz_write(ctx, out, display_profile, strlen(display_profile) + 1);

	}
	fz_always(ctx)
	{
		fz_drop_page(ctx, page);
		fz_drop_output(ctx, out);
	}
	fz_catch(ctx)
	{
		fz_rethrow(ctx);
	}
}