PS: Move type 3 support out into cairo-ps-font.c

This is in preparation for generalizing this code for sharing among
several backends.
This commit is contained in:
Carl Worth 2006-05-09 09:55:23 -07:00
parent 7ebdf4825d
commit f3703b1806
4 changed files with 295 additions and 170 deletions

View file

@ -1,7 +1,7 @@
if CAIRO_HAS_PS_SURFACE
libcairo_ps_headers = cairo-ps.h
libcairo_ps_sources = cairo-ps-surface.c
libcairo_ps_sources = cairo-ps-surface.c cairo-ps-font.c cairo-ps-font-private.h
libcairo_font_subset_sources = cairo-font-subset.c cairo-font-subset-private.h
endif

101
src/cairo-ps-font-private.h Normal file
View file

@ -0,0 +1,101 @@
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2003 University of Southern California
* Copyright © 2005 Red Hat, Inc
* Copyright © 2006 Keith Packard
* Copyright © 2006 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is University of Southern
* California.
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
* Kristian Høgsberg <krh@redhat.com>
* Keith Packard <keithp@keithp.com>
*/
/*
* Type1 and Type3 PS fonts can hold only 256 glyphs.
*
* XXX Work around this by placing each set of 256 glyphs in a separate
* font. No separate data structure is kept for this; the font name is
* generated from all but the low 8 bits of the output glyph id.
*/
#ifndef CAIRO_PS_FONT_PRIVATE_H
#define CAIRO_PS_FONT_PRIVATE_H
#include "cairoint.h"
typedef struct cairo_ps_glyph {
cairo_hash_entry_t base; /* font glyph index */
unsigned int output_glyph; /* PS sub-font glyph index */
} cairo_ps_glyph_t;
typedef struct cairo_ps_font {
cairo_hash_entry_t base;
cairo_scaled_font_t *scaled_font;
unsigned int output_font;
cairo_hash_table_t *glyphs;
unsigned int max_glyph;
} cairo_ps_font_t;
typedef struct _cairo_ps_font_glyph_select {
cairo_ps_glyph_t **glyphs;
int subfont;
int numglyph;
} cairo_ps_font_glyph_select_t;
cairo_private cairo_ps_font_t *
_cairo_ps_font_create (cairo_scaled_font_t *scaled_font,
unsigned int id);
cairo_private void
_cairo_ps_font_destroy (cairo_ps_font_t *ps_font);
cairo_private void
_cairo_ps_font_key_init (cairo_ps_font_t *ps_font,
cairo_scaled_font_t *scaled_font);
cairo_private void
_cairo_ps_font_select_glyphs (void *entry, void *closure);
cairo_private void
_cairo_ps_font_destroy_glyph (cairo_ps_font_t *ps_font,
cairo_ps_glyph_t *ps_glyph);
cairo_private cairo_bool_t
_cairo_ps_font_equal (const void *key_a, const void *key_b);
cairo_private cairo_status_t
_cairo_ps_font_find_glyph (cairo_ps_font_t *font,
cairo_scaled_font_t *scaled_font,
unsigned long index,
cairo_ps_glyph_t **result);
#endif /* CAIRO_PS_FONT_PRIVATE_H */

181
src/cairo-ps-font.c Normal file
View file

@ -0,0 +1,181 @@
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2003 University of Southern California
* Copyright © 2005 Red Hat, Inc
* Copyright © 2006 Keith Packard
* Copyright © 2006 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is University of Southern
* California.
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
* Kristian Høgsberg <krh@redhat.com>
* Keith Packard <keithp@keithp.com>
*/
#include "cairoint.h"
#include "cairo-ps-font-private.h"
static cairo_bool_t
_cairo_ps_glyph_equal (const void *key_a, const void *key_b)
{
const cairo_ps_glyph_t *ps_glyph_a = key_a;
const cairo_ps_glyph_t *ps_glyph_b = key_b;
return ps_glyph_a->base.hash == ps_glyph_b->base.hash;
}
static void
_cairo_ps_glyph_key_init (cairo_ps_glyph_t *ps_glyph,
unsigned long index)
{
ps_glyph->base.hash = index;
}
static cairo_ps_glyph_t *
_cairo_ps_glyph_create (cairo_ps_font_t *ps_font,
unsigned long index)
{
cairo_ps_glyph_t *ps_glyph = malloc (sizeof (cairo_ps_glyph_t));
if (!ps_glyph)
return NULL;
_cairo_ps_glyph_key_init (ps_glyph, index);
ps_glyph->output_glyph = ps_font->max_glyph++;
return ps_glyph;
}
static void
_cairo_ps_glyph_destroy (cairo_ps_glyph_t *ps_glyph)
{
free (ps_glyph);
}
cairo_status_t
_cairo_ps_font_find_glyph (cairo_ps_font_t *font,
cairo_scaled_font_t *scaled_font,
unsigned long index,
cairo_ps_glyph_t **result)
{
cairo_ps_glyph_t key;
cairo_ps_glyph_t *ps_glyph;
cairo_status_t status;
_cairo_ps_glyph_key_init (&key, index);
if (! _cairo_hash_table_lookup (font->glyphs,
&key.base,
(cairo_hash_entry_t **) &ps_glyph)) {
ps_glyph = _cairo_ps_glyph_create (font, index);
if (!ps_glyph)
return CAIRO_STATUS_NO_MEMORY;
status = _cairo_hash_table_insert (font->glyphs, &ps_glyph->base);
if (status)
return status;
}
*result = ps_glyph;
return CAIRO_STATUS_SUCCESS;
}
cairo_bool_t
_cairo_ps_font_equal (const void *key_a, const void *key_b)
{
const cairo_ps_font_t *ps_font_a = key_a;
const cairo_ps_font_t *ps_font_b = key_b;
return ps_font_a->scaled_font == ps_font_b->scaled_font;
}
void
_cairo_ps_font_key_init (cairo_ps_font_t *ps_font,
cairo_scaled_font_t *scaled_font)
{
ps_font->base.hash = (unsigned long) scaled_font;
ps_font->scaled_font = scaled_font;
}
cairo_ps_font_t *
_cairo_ps_font_create (cairo_scaled_font_t *scaled_font,
unsigned int id)
{
cairo_ps_font_t *ps_font = malloc (sizeof (cairo_ps_font_t));
if (!ps_font)
return NULL;
_cairo_ps_font_key_init (ps_font, scaled_font);
ps_font->glyphs = _cairo_hash_table_create (_cairo_ps_glyph_equal);
if (!ps_font->glyphs) {
free (ps_font);
return NULL;
}
ps_font->max_glyph = 0;
ps_font->output_font = id;
cairo_scaled_font_reference (ps_font->scaled_font);
return ps_font;
}
void
_cairo_ps_font_destroy_glyph (cairo_ps_font_t *ps_font,
cairo_ps_glyph_t *ps_glyph)
{
_cairo_hash_table_remove (ps_font->glyphs, &ps_glyph->base);
_cairo_ps_glyph_destroy (ps_glyph);
}
static void
_cairo_ps_font_destroy_glyph_callback (void *entry, void *closure)
{
cairo_ps_glyph_t *ps_glyph = entry;
cairo_ps_font_t *ps_font = closure;
_cairo_ps_font_destroy_glyph (ps_font, ps_glyph);
}
void
_cairo_ps_font_destroy (cairo_ps_font_t *ps_font)
{
_cairo_hash_table_foreach (ps_font->glyphs,
_cairo_ps_font_destroy_glyph_callback,
ps_font);
_cairo_hash_table_destroy (ps_font->glyphs);
cairo_scaled_font_destroy (ps_font->scaled_font);
free (ps_font);
}
void
_cairo_ps_font_select_glyphs (void *entry, void *closure)
{
cairo_ps_glyph_t *ps_glyph = entry;
cairo_ps_font_glyph_select_t *ps_glyph_select = closure;
if (ps_glyph->output_glyph >> 8 == ps_glyph_select->subfont) {
unsigned long sub_glyph = ps_glyph->output_glyph & 0xff;
ps_glyph_select->glyphs[sub_glyph] = ps_glyph;
if (sub_glyph >= ps_glyph_select->numglyph)
ps_glyph_select->numglyph = sub_glyph + 1;
}
}

View file

@ -39,6 +39,7 @@
#include "cairoint.h"
#include "cairo-ps.h"
#include "cairo-ps-font-private.h"
#include "cairo-font-subset-private.h"
#include "cairo-paginated-surface-private.h"
#include "cairo-meta-surface-private.h"
@ -50,27 +51,6 @@
static const cairo_surface_backend_t cairo_ps_surface_backend;
static const cairo_paginated_surface_backend_t cairo_ps_surface_paginated_backend;
/*
* Type1 and Type3 PS fonts can hold only 256 glyphs.
*
* XXX Work around this by placing each set of 256 glyphs in a separate
* font. No separate data structure is kept for this; the font name is
* generated from all but the low 8 bits of the output glyph id.
*/
typedef struct cairo_ps_glyph {
cairo_hash_entry_t base; /* font glyph index */
unsigned int output_glyph; /* PS sub-font glyph index */
} cairo_ps_glyph_t;
typedef struct cairo_ps_font {
cairo_hash_entry_t base;
cairo_scaled_font_t *scaled_font;
unsigned int output_font;
cairo_hash_table_t *glyphs;
unsigned int max_glyph;
} cairo_ps_font_t;
typedef struct cairo_ps_surface {
cairo_surface_t base;
@ -232,135 +212,18 @@ _cairo_ps_surface_emit_header (cairo_ps_surface_t *surface)
}
}
static cairo_bool_t
_cairo_ps_glyph_equal (const void *key_a, const void *key_b)
{
const cairo_ps_glyph_t *ps_glyph_a = key_a;
const cairo_ps_glyph_t *ps_glyph_b = key_b;
return ps_glyph_a->base.hash == ps_glyph_b->base.hash;
}
static void
_cairo_ps_glyph_key_init (cairo_ps_glyph_t *ps_glyph,
unsigned long index)
{
ps_glyph->base.hash = index;
}
static cairo_ps_glyph_t *
_cairo_ps_glyph_create (cairo_ps_font_t *ps_font,
unsigned long index)
{
cairo_ps_glyph_t *ps_glyph = malloc (sizeof (cairo_ps_glyph_t));
if (!ps_glyph)
return NULL;
_cairo_ps_glyph_key_init (ps_glyph, index);
ps_glyph->output_glyph = ps_font->max_glyph++;
return ps_glyph;
}
static void
_cairo_ps_glyph_destroy (cairo_ps_glyph_t *ps_glyph)
{
free (ps_glyph);
}
static cairo_status_t
_cairo_ps_glyph_find (cairo_ps_font_t *font,
cairo_scaled_font_t *scaled_font,
unsigned long index,
cairo_ps_glyph_t **result)
{
cairo_ps_glyph_t key;
cairo_ps_glyph_t *ps_glyph;
cairo_status_t status;
_cairo_ps_glyph_key_init (&key, index);
if (!_cairo_hash_table_lookup (font->glyphs,
&key.base,
(cairo_hash_entry_t **) &ps_glyph)) {
ps_glyph = _cairo_ps_glyph_create (font, index);
if (!ps_glyph)
return CAIRO_STATUS_NO_MEMORY;
status = _cairo_hash_table_insert (font->glyphs, &ps_glyph->base);
if (status)
return status;
}
*result = ps_glyph;
return CAIRO_STATUS_SUCCESS;
}
static cairo_bool_t
_cairo_ps_font_equal (const void *key_a, const void *key_b)
{
const cairo_ps_font_t *ps_font_a = key_a;
const cairo_ps_font_t *ps_font_b = key_b;
return ps_font_a->scaled_font == ps_font_b->scaled_font;
}
static void
_cairo_ps_font_key_init (cairo_ps_font_t *ps_font,
cairo_scaled_font_t *scaled_font)
{
ps_font->base.hash = (unsigned long) scaled_font;
ps_font->scaled_font = scaled_font;
}
static cairo_ps_font_t *
_cairo_ps_font_create (cairo_ps_surface_t *surface,
cairo_scaled_font_t *scaled_font)
{
cairo_ps_font_t *ps_font = malloc (sizeof (cairo_ps_font_t));
if (!ps_font)
return NULL;
_cairo_ps_font_key_init (ps_font, scaled_font);
ps_font->glyphs = _cairo_hash_table_create (_cairo_ps_glyph_equal);
if (!ps_font->glyphs) {
free (ps_font);
return NULL;
}
ps_font->max_glyph = 0;
ps_font->output_font = surface->max_font++;
cairo_scaled_font_reference (ps_font->scaled_font);
return ps_font;
}
static void
_cairo_ps_font_destroy_glyph (void *entry, void *closure)
{
cairo_ps_glyph_t *ps_glyph = entry;
cairo_ps_font_t *ps_font = closure;
_cairo_hash_table_remove (ps_font->glyphs, &ps_glyph->base);
_cairo_ps_glyph_destroy (ps_glyph);
}
static void
_cairo_ps_font_destroy (cairo_ps_font_t *ps_font)
{
_cairo_hash_table_foreach (ps_font->glyphs,
_cairo_ps_font_destroy_glyph,
ps_font);
_cairo_hash_table_destroy (ps_font->glyphs);
cairo_scaled_font_destroy (ps_font->scaled_font);
free (ps_font);
}
static void
_cairo_ps_surface_destroy_font (cairo_ps_surface_t *surface,
cairo_ps_font_t *ps_font)
_cairo_ps_surface_destroy_ps_font (cairo_ps_surface_t *surface,
cairo_ps_font_t *ps_font)
{
_cairo_hash_table_remove (surface->fonts, &ps_font->base);
_cairo_ps_font_destroy (ps_font);
}
static cairo_status_t
_cairo_ps_font_find (cairo_ps_surface_t *surface,
cairo_scaled_font_t *scaled_font,
cairo_ps_font_t **result)
_cairo_ps_surface_find_ps_font (cairo_ps_surface_t *surface,
cairo_scaled_font_t *scaled_font,
cairo_ps_font_t **result)
{
cairo_ps_font_t key;
cairo_ps_font_t *ps_font;
@ -370,7 +233,7 @@ _cairo_ps_font_find (cairo_ps_surface_t *surface,
if (!_cairo_hash_table_lookup (surface->fonts, &key.base,
(cairo_hash_entry_t **) &ps_font))
{
ps_font = _cairo_ps_font_create (surface, scaled_font);
ps_font = _cairo_ps_font_create (scaled_font, surface->max_font++);
if (!ps_font)
return CAIRO_STATUS_NO_MEMORY;
status = _cairo_hash_table_insert (surface->fonts,
@ -382,26 +245,6 @@ _cairo_ps_font_find (cairo_ps_surface_t *surface,
return CAIRO_STATUS_SUCCESS;
}
typedef struct _cairo_ps_font_glyph_select {
cairo_ps_glyph_t **glyphs;
int subfont;
int numglyph;
} cairo_ps_font_glyph_select_t;
static void
_cairo_ps_font_select_glyphs (void *entry, void *closure)
{
cairo_ps_glyph_t *ps_glyph = entry;
cairo_ps_font_glyph_select_t *ps_glyph_select = closure;
if (ps_glyph->output_glyph >> 8 == ps_glyph_select->subfont) {
unsigned long sub_glyph = ps_glyph->output_glyph & 0xff;
ps_glyph_select->glyphs[sub_glyph] = ps_glyph;
if (sub_glyph >= ps_glyph_select->numglyph)
ps_glyph_select->numglyph = sub_glyph + 1;
}
}
static cairo_status_t
_cairo_ps_surface_emit_glyph (cairo_ps_surface_t *surface,
cairo_ps_font_t *ps_font,
@ -495,7 +338,7 @@ _cairo_ps_surface_emit_font (void *entry, void *closure)
_cairo_output_stream_printf (surface->final_stream,
"\t\t{ } %% %d\n", glyph);
}
_cairo_ps_font_destroy_glyph (ps_glyph, ps_font);
_cairo_ps_font_destroy_glyph (ps_font, ps_glyph);
}
_cairo_output_stream_printf (surface->final_stream,
"\t]\n"
@ -505,7 +348,7 @@ _cairo_ps_surface_emit_font (void *entry, void *closure)
"\t}\n"
">> definefont pop\n");
}
_cairo_ps_surface_destroy_font (surface, ps_font);
_cairo_ps_surface_destroy_ps_font (surface, ps_font);
}
@ -1940,7 +1783,7 @@ _cairo_ps_surface_show_glyphs (void *abstract_surface,
_cairo_output_stream_printf (stream,
"%% _cairo_ps_surface_show_glyphs\n");
status = _cairo_ps_font_find (surface, scaled_font, &ps_font);
status = _cairo_ps_surface_find_ps_font (surface, scaled_font, &ps_font);
if (status)
goto fallback;
@ -1948,8 +1791,8 @@ _cairo_ps_surface_show_glyphs (void *abstract_surface,
emit_pattern (surface, source);
for (i = 0; i < num_glyphs; i++) {
status = _cairo_ps_glyph_find (ps_font, scaled_font,
glyphs[i].index, &ps_glyph);
status = _cairo_ps_font_find_glyph (ps_font, scaled_font,
glyphs[i].index, &ps_glyph);
if (status) {
glyphs += i;
num_glyphs -= i;