Merge branch 'type1-subsetting' into cairo

Conflicts:

	src/cairo-ps-surface.c
This commit is contained in:
Carl Worth 2006-06-13 15:51:57 -07:00
commit 1d18af9f5a
6 changed files with 1370 additions and 151 deletions

View file

@ -1,6 +1,7 @@
font_subset_sources = \
cairo-font-subset.c \
cairo-font-subset-private.h \
cairo-type1-subset.c \
cairo-scaled-font-subsets.c \
cairo-scaled-font-subsets-private.h

View file

@ -34,7 +34,6 @@
*/
#include "cairoint.h"
#include "cairo-font-subset-private.h"
#include "cairo-scaled-font-subsets-private.h"
/* XXX: Eventually, we need to handle other font backends */

View file

@ -1579,6 +1579,115 @@ _cairo_pdf_surface_write_pages (cairo_pdf_surface_t *surface)
"endobj\r\n");
}
static cairo_status_t
_cairo_pdf_surface_emit_type1_font_subset (cairo_pdf_surface_t *surface,
cairo_scaled_font_subset_t *font_subset)
{
cairo_pdf_resource_t stream, descriptor, subset_resource;
cairo_status_t status;
cairo_pdf_font_t font;
cairo_type1_subset_t subset;
unsigned long length, compressed_length;
char *compressed;
int i;
char name[64];
snprintf (name, sizeof name, "CairoFont-%d-%d",
font_subset->font_id, font_subset->subset_id);
status = _cairo_type1_subset_init (&subset, name, font_subset);
if (status)
return status;
/* We ignore the zero-trailer and set Length3 to 0. */
length = subset.header_length + subset.data_length;
compressed = compress_dup (subset.data, length, &compressed_length);
if (compressed == NULL) {
_cairo_type1_subset_fini (&subset);
return CAIRO_STATUS_NO_MEMORY;
}
stream = _cairo_pdf_surface_new_object (surface);
_cairo_output_stream_printf (surface->output,
"%d 0 obj\r\n"
"<< /Filter /FlateDecode\r\n"
" /Length %lu\r\n"
" /Length1 %lu\r\n"
" /Length2 %lu\r\n"
" /Length3 0\r\n"
">>\r\n"
"stream\r\n",
stream.id,
compressed_length,
subset.header_length,
subset.data_length);
_cairo_output_stream_write (surface->output, compressed, compressed_length);
_cairo_output_stream_printf (surface->output,
"\r\n"
"endstream\r\n"
"endobj\r\n");
free (compressed);
descriptor = _cairo_pdf_surface_new_object (surface);
_cairo_output_stream_printf (surface->output,
"%d 0 obj\r\n"
"<< /Type /FontDescriptor\r\n"
" /FontName /%s\r\n"
" /Flags 4\r\n"
" /FontBBox [ %ld %ld %ld %ld ]\r\n"
" /ItalicAngle 0\r\n"
" /Ascent %ld\r\n"
" /Descent %ld\r\n"
" /CapHeight 500\r\n"
" /StemV 80\r\n"
" /StemH 80\r\n"
" /FontFile %u 0 R\r\n"
">>\r\n"
"endobj\r\n",
descriptor.id,
subset.base_font,
subset.x_min,
subset.y_min,
subset.x_max,
subset.y_max,
subset.ascent,
subset.descent,
stream.id);
subset_resource = _cairo_pdf_surface_new_object (surface);
_cairo_output_stream_printf (surface->output,
"%d 0 obj\r\n"
"<< /Type /Font\r\n"
" /Subtype /Type1\r\n"
" /BaseFont /%s\r\n"
" /FirstChar 0\r\n"
" /LastChar %d\r\n"
" /FontDescriptor %d 0 R\r\n"
" /Widths [",
subset_resource.id,
subset.base_font,
font_subset->num_glyphs,
descriptor.id);
for (i = 0; i < font_subset->num_glyphs; i++)
_cairo_output_stream_printf (surface->output,
" %d",
subset.widths[i]);
_cairo_output_stream_printf (surface->output,
" ]\r\n"
">>\r\n"
"endobj\r\n");
font.font_id = font_subset->font_id;
font.subset_id = font_subset->subset_id;
font.subset_resource = subset_resource;
_cairo_array_append (&surface->fonts, &font);
_cairo_type1_subset_fini (&subset);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_pdf_surface_emit_truetype_font_subset (cairo_pdf_surface_t *surface,
cairo_scaled_font_subset_t *font_subset)
@ -1875,6 +1984,10 @@ _cairo_pdf_surface_emit_font_subset (cairo_scaled_font_subset_t *font_subset,
cairo_pdf_surface_t *surface = closure;
cairo_status_t status;
status = _cairo_pdf_surface_emit_type1_font_subset (surface, font_subset);
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
return;
status = _cairo_pdf_surface_emit_truetype_font_subset (surface, font_subset);
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
return;

View file

@ -85,6 +85,117 @@ typedef struct cairo_ps_surface {
#define PS_SURFACE_MAX_GLYPHS_PER_FONT 256
/* A word wrap stream can be used as a filter to do word wrapping on
* top of an existing output stream. The word wrapping is quite
* simple, using isspace to determine characters that separate
* words. Any word that will cause the column count exceeed the given
* max_column will have a '\n' character emitted before it.
*
* The stream is careful to maintain integrity for words that cross
* the boundary from one call to write to the next.
*
* Note: This stream does not guarantee that the output will never
* exceed max_column. In particular, if a single word is larger than
* max_column it will not be broken up.
*/
typedef struct _word_wrap_stream {
cairo_output_stream_t *output;
int max_column;
int column;
cairo_bool_t last_write_was_space;
} word_wrap_stream_t;
static int
_count_word_up_to (const unsigned char *s, int length)
{
int word = 0;
while (length--) {
if (! isspace (*s++))
word++;
else
return word;
}
return word;
}
static cairo_status_t
_word_wrap_stream_write (void *closure,
const unsigned char *data,
unsigned int length)
{
word_wrap_stream_t *stream = closure;
cairo_bool_t newline;
int word;
while (length) {
if (isspace (*data)) {
newline = (*data == '\n' || *data == '\r');
if (! newline && stream->column >= stream->max_column) {
_cairo_output_stream_printf (stream->output, "\n");
stream->column = 0;
}
_cairo_output_stream_write (stream->output, data, 1);
data++;
length--;
if (newline)
stream->column = 0;
else
stream->column++;
stream->last_write_was_space = TRUE;
} else {
word = _count_word_up_to (data, length);
/* Don't wrap if this word is a continuation of a word
* from a previous call to write. */
if (stream->column + word >= stream->max_column &&
stream->last_write_was_space)
{
_cairo_output_stream_printf (stream->output, "\n");
stream->column = 0;
}
_cairo_output_stream_write (stream->output, data, word);
data += word;
length -= word;
stream->column += word;
stream->last_write_was_space = FALSE;
}
}
return _cairo_output_stream_get_status (stream->output);
}
static cairo_status_t
_word_wrap_stream_close (void *closure)
{
cairo_status_t status;
word_wrap_stream_t *stream = closure;
status = _cairo_output_stream_get_status (stream->output);
free (stream);
return status;
}
static cairo_output_stream_t *
_word_wrap_stream_create (cairo_output_stream_t *output, int max_column)
{
word_wrap_stream_t *stream;
stream = malloc (sizeof (word_wrap_stream_t));
if (stream == NULL)
return (cairo_output_stream_t *) &cairo_output_stream_nil;
stream->output = output;
stream->max_column = max_column;
stream->column = 0;
stream->last_write_was_space = FALSE;
return _cairo_output_stream_create (_word_wrap_stream_write,
_word_wrap_stream_close, stream);
}
static cairo_status_t
_cairo_ps_surface_path_move_to (void *closure, cairo_point_t *point)
{
@ -142,6 +253,28 @@ _cairo_ps_surface_path_close_path (void *closure)
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_ps_surface_emit_path (cairo_output_stream_t *stream,
cairo_path_fixed_t *path)
{
cairo_output_stream_t *word_wrap;
cairo_status_t status;
word_wrap = _word_wrap_stream_create (stream, 79);
status = _cairo_path_fixed_interpret (path,
CAIRO_DIRECTION_FORWARD,
_cairo_ps_surface_path_move_to,
_cairo_ps_surface_path_line_to,
_cairo_ps_surface_path_curve_to,
_cairo_ps_surface_path_close_path,
word_wrap);
_cairo_output_stream_destroy (word_wrap);
return status;
}
static void
_cairo_ps_surface_emit_header (cairo_ps_surface_t *surface)
{
@ -209,6 +342,36 @@ _cairo_ps_surface_emit_header (cairo_ps_surface_t *surface)
}
}
static cairo_status_t
_cairo_ps_surface_emit_type1_font_subset (cairo_ps_surface_t *surface,
cairo_scaled_font_subset_t *font_subset)
{
cairo_type1_subset_t subset;
cairo_status_t status;
int length;
char name[64];
snprintf (name, sizeof name, "CairoFont-%d-%d",
font_subset->font_id, font_subset->subset_id);
status = _cairo_type1_subset_init (&subset, name, font_subset);
if (status)
return status;
/* FIXME: Figure out document structure convention for fonts */
_cairo_output_stream_printf (surface->final_stream,
"%% _cairo_ps_surface_emit_type1_font_subset\n");
length = subset.header_length + subset.data_length + subset.trailer_length;
_cairo_output_stream_write (surface->final_stream, subset.data, length);
_cairo_type1_subset_fini (&subset);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_ps_surface_emit_truetype_font_subset (cairo_ps_surface_t *surface,
cairo_scaled_font_subset_t *font_subset)
@ -299,13 +462,8 @@ _cairo_ps_surface_emit_outline_glyph_data (cairo_ps_surface_t *surface,
_cairo_fixed_to_double (scaled_glyph->bbox.p2.x),
-_cairo_fixed_to_double (scaled_glyph->bbox.p1.y));
status = _cairo_path_fixed_interpret (scaled_glyph->path,
CAIRO_DIRECTION_FORWARD,
_cairo_ps_surface_path_move_to,
_cairo_ps_surface_path_line_to,
_cairo_ps_surface_path_curve_to,
_cairo_ps_surface_path_close_path,
surface->final_stream);
status = _cairo_ps_surface_emit_path (surface->final_stream,
scaled_glyph->path);
_cairo_output_stream_printf (surface->final_stream,
"F\n");
@ -426,6 +584,10 @@ _cairo_ps_surface_emit_font_subset (cairo_scaled_font_subset_t *font_subset,
cairo_ps_surface_t *surface = closure;
cairo_status_t status;
status = _cairo_ps_surface_emit_type1_font_subset (surface, font_subset);
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
return;
status = _cairo_ps_surface_emit_truetype_font_subset (surface, font_subset);
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
return;
@ -875,117 +1037,6 @@ cairo_ps_surface_dsc_begin_page_setup (cairo_surface_t *surface)
}
}
/* A word wrap stream can be used as a filter to do word wrapping on
* top of an existing output stream. The word wrapping is quite
* simple, using isspace to determine characters that separate
* words. Any word that will cause the column count exceeed the given
* max_column will have a '\n' character emitted before it.
*
* The stream is careful to maintain integrity for words that cross
* the boundary from one call to write to the next.
*
* Note: This stream does not guarantee that the output will never
* exceed max_column. In particular, if a single word is larger than
* max_column it will not be broken up.
*/
typedef struct _word_wrap_stream {
cairo_output_stream_t *output;
int max_column;
int column;
cairo_bool_t last_write_was_space;
} word_wrap_stream_t;
static int
_count_word_up_to (const unsigned char *s, int length)
{
int word = 0;
while (length--) {
if (! isspace (*s++))
word++;
else
return word;
}
return word;
}
static cairo_status_t
_word_wrap_stream_write (void *closure,
const unsigned char *data,
unsigned int length)
{
word_wrap_stream_t *stream = closure;
cairo_bool_t newline;
int word;
while (length) {
if (isspace (*data)) {
newline = (*data == '\n' || *data == '\r');
if (! newline && stream->column >= stream->max_column) {
_cairo_output_stream_printf (stream->output, "\n");
stream->column = 0;
}
_cairo_output_stream_write (stream->output, data, 1);
data++;
length--;
if (newline)
stream->column = 0;
else
stream->column++;
stream->last_write_was_space = TRUE;
} else {
word = _count_word_up_to (data, length);
/* Don't wrap if this word is a continuation of a word
* from a previous call to write. */
if (stream->column + word >= stream->max_column &&
stream->last_write_was_space)
{
_cairo_output_stream_printf (stream->output, "\n");
stream->column = 0;
}
_cairo_output_stream_write (stream->output, data, word);
data += word;
length -= word;
stream->column += word;
stream->last_write_was_space = FALSE;
}
}
return _cairo_output_stream_get_status (stream->output);
}
static cairo_status_t
_word_wrap_stream_close (void *closure)
{
cairo_status_t status;
word_wrap_stream_t *stream = closure;
status = _cairo_output_stream_get_status (stream->output);
free (stream);
return status;
}
static cairo_output_stream_t *
_word_wrap_stream_create (cairo_output_stream_t *output, int max_column)
{
word_wrap_stream_t *stream;
stream = malloc (sizeof (word_wrap_stream_t));
if (stream == NULL)
return (cairo_output_stream_t *) &cairo_output_stream_nil;
stream->output = output;
stream->max_column = max_column;
stream->column = 0;
stream->last_write_was_space = FALSE;
return _cairo_output_stream_create (_word_wrap_stream_write,
_word_wrap_stream_close, stream);
}
static cairo_surface_t *
_cairo_ps_surface_create_similar (void *abstract_src,
cairo_content_t content,
@ -1004,16 +1055,9 @@ _cairo_ps_surface_finish (void *abstract_surface)
{
cairo_status_t status;
cairo_ps_surface_t *surface = abstract_surface;
cairo_output_stream_t *final_stream, *word_wrap;
int i, num_comments;
char **comments;
/* Save final_stream to be restored later. */
final_stream = surface->final_stream;
word_wrap = _word_wrap_stream_create (final_stream, 79);
surface->final_stream = word_wrap;
_cairo_ps_surface_emit_header (surface);
_cairo_ps_surface_emit_font_subsets (surface);
@ -1028,10 +1072,6 @@ _cairo_ps_surface_finish (void *abstract_surface)
fclose (surface->tmpfile);
/* Restore final stream before final cleanup. */
_cairo_output_stream_destroy (word_wrap);
surface->final_stream = final_stream;
_cairo_output_stream_close (surface->final_stream);
if (status == CAIRO_STATUS_SUCCESS)
status = _cairo_output_stream_get_status (surface->final_stream);
@ -1596,13 +1636,7 @@ _cairo_ps_surface_intersect_clip_path (void *abstract_surface,
return CAIRO_STATUS_SUCCESS;
}
status = _cairo_path_fixed_interpret (path,
CAIRO_DIRECTION_FORWARD,
_cairo_ps_surface_path_move_to,
_cairo_ps_surface_path_line_to,
_cairo_ps_surface_path_curve_to,
_cairo_ps_surface_path_close_path,
stream);
status = _cairo_ps_surface_emit_path (stream, path);
switch (fill_rule) {
case CAIRO_FILL_RULE_WINDING:
@ -1745,13 +1779,7 @@ _cairo_ps_surface_stroke (void *abstract_surface,
_cairo_output_stream_printf (stream,
"gsave\n");
status = _cairo_path_fixed_interpret (path,
CAIRO_DIRECTION_FORWARD,
_cairo_ps_surface_path_move_to,
_cairo_ps_surface_path_line_to,
_cairo_ps_surface_path_curve_to,
_cairo_ps_surface_path_close_path,
stream);
status = _cairo_ps_surface_emit_path (stream, path);
/*
* Switch to user space to set line parameters
@ -1811,13 +1839,7 @@ _cairo_ps_surface_fill (void *abstract_surface,
emit_pattern (surface, source);
status = _cairo_path_fixed_interpret (path,
CAIRO_DIRECTION_FORWARD,
_cairo_ps_surface_path_move_to,
_cairo_ps_surface_path_line_to,
_cairo_ps_surface_path_curve_to,
_cairo_ps_surface_path_close_path,
stream);
status = _cairo_ps_surface_emit_path (stream, path);
switch (fill_rule) {
case CAIRO_FILL_RULE_WINDING:

View file

@ -220,4 +220,48 @@ _cairo_truetype_subset_init (cairo_truetype_subset_t *truetype_subset,
cairo_private void
_cairo_truetype_subset_fini (cairo_truetype_subset_t *truetype_subset);
typedef struct _cairo_type1_subset {
char *base_font;
int *widths;
long x_min, y_min, x_max, y_max;
long ascent, descent;
char *data;
unsigned long header_length;
unsigned long data_length;
unsigned long trailer_length;
} cairo_type1_subset_t;
/**
* _cairo_type1_subset_init:
* @type1_subset: a #cairo_type1_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
*
* If possible (depending on the format of the underlying
* cairo_scaled_font_t and the font backend in use) generate a type1
* file corresponding to @font_subset and initialize @type1_subset
* with information about the subset and the type1 data.
*
* Return value: CAIRO_STATUS_SUCCESS if successful,
* CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a type1
* file, or an non-zero value indicating an error. Possible errors
* include CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_type1_subset_init (cairo_type1_subset_t *type_subset,
const char *name,
cairo_scaled_font_subset_t *font_subset);
/**
* _cairo_type1_subset_fini:
* @type1_subset: a #cairo_type1_subset_t
*
* Free all resources associated with @type1_subset. After this call,
* @type1_subset should not be used again without a subsequent call to
* _cairo_truetype_type1_init() again first.
**/
cairo_private void
_cairo_type1_subset_fini (cairo_type1_subset_t *subset);
#endif /* CAIRO_SCALED_FONT_SUBSETS_PRIVATE_H */

1040
src/cairo-type1-subset.c Normal file

File diff suppressed because it is too large Load diff