📄 cairo-pdf-surface.c
字号:
/* cairo - a vector graphics library with display and print output * * Copyright © 2004 Red Hat, Inc * 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): * Kristian Høgsberg <krh@redhat.com> * Carl Worth <cworth@cworth.org> */#include "cairoint.h"#include "cairo-pdf.h"#include "cairo-pdf-test.h"#include "cairo-scaled-font-subsets-private.h"#include "cairo-ft-private.h"#include "cairo-paginated-surface-private.h"#include "cairo-path-fixed-private.h"#include "cairo-output-stream-private.h"#include <time.h>#include <zlib.h>/* Issues: * * - Why doesn't pages inherit /alpha%d GS dictionaries from the Pages * object? * * - We embed an image in the stream each time it's composited. We * could add generation counters to surfaces and remember the stream * ID for a particular generation for a particular surface. * * - Clipping: must be able to reset clipping * * - Images of other formats than 8 bit RGBA. * * - Backend specific meta data. * * - Surface patterns. * * - Alpha channels in gradients. * * - Should/does cairo support drawing into a scratch surface and then * using that as a fill pattern? For this backend, that would involve * using a tiling pattern (4.6.2). How do you create such a scratch * surface? cairo_surface_create_similar() ? * * - What if you create a similiar surface and does show_page and then * does show_surface on another surface? * * - Output TM so page scales to the right size - PDF default user * space has 1 unit = 1 / 72 inch. * * - Add test case for RGBA images. * * - Add test case for RGBA gradients. * * - Coordinate space for create_similar() args? * * - Investigate /Matrix entry in content stream dicts for pages * instead of outputting the cm operator in every page. */typedef struct _cairo_pdf_object { long offset;} cairo_pdf_object_t;typedef struct _cairo_pdf_resource { unsigned int id;} cairo_pdf_resource_t;typedef struct _cairo_pdf_font { unsigned int font_id; unsigned int subset_id; cairo_pdf_resource_t subset_resource;} cairo_pdf_font_t;typedef struct _cairo_pdf_surface { cairo_surface_t base; /* Prefer the name "output" here to avoid confusion over the * structure within a PDF document known as a "stream". */ cairo_output_stream_t *output; double width; double height; cairo_array_t objects; cairo_array_t pages; cairo_array_t patterns; cairo_array_t xobjects; cairo_array_t streams; cairo_array_t alphas; cairo_scaled_font_subsets_t *font_subsets; cairo_array_t fonts; cairo_pdf_resource_t next_available_resource; cairo_pdf_resource_t pages_resource; struct { cairo_bool_t active; cairo_pdf_resource_t self; cairo_pdf_resource_t length; long start_offset; } current_stream; cairo_bool_t has_clip; cairo_paginated_mode_t paginated_mode;} cairo_pdf_surface_t;#define PDF_SURFACE_MAX_GLYPHS_PER_FONT 256static cairo_pdf_resource_t_cairo_pdf_surface_new_object (cairo_pdf_surface_t *surface);static void_cairo_pdf_surface_clear (cairo_pdf_surface_t *surface);static cairo_pdf_resource_t_cairo_pdf_surface_open_stream (cairo_pdf_surface_t *surface, const char *fmt, ...) CAIRO_PRINTF_FORMAT(2, 3);static void_cairo_pdf_surface_close_stream (cairo_pdf_surface_t *surface);static void_cairo_pdf_surface_add_stream (cairo_pdf_surface_t *surface, cairo_pdf_resource_t stream);static void_cairo_pdf_surface_write_pages (cairo_pdf_surface_t *surface);static cairo_pdf_resource_t_cairo_pdf_surface_write_info (cairo_pdf_surface_t *surface);static cairo_pdf_resource_t_cairo_pdf_surface_write_catalog (cairo_pdf_surface_t *surface);static long_cairo_pdf_surface_write_xref (cairo_pdf_surface_t *surface);static cairo_status_t_cairo_pdf_surface_write_page (cairo_pdf_surface_t *surface);static cairo_status_t_cairo_pdf_surface_emit_font_subsets (cairo_pdf_surface_t *surface);static const cairo_surface_backend_t cairo_pdf_surface_backend;static const cairo_paginated_surface_backend_t cairo_pdf_surface_paginated_backend;static cairo_pdf_resource_t_cairo_pdf_surface_new_object (cairo_pdf_surface_t *surface){ cairo_pdf_resource_t resource; cairo_status_t status; cairo_pdf_object_t object; object.offset = _cairo_output_stream_get_position (surface->output); status = _cairo_array_append (&surface->objects, &object); if (status) { resource.id = 0; return resource; } resource = surface->next_available_resource; surface->next_available_resource.id++; return resource;}static void_cairo_pdf_surface_update_object (cairo_pdf_surface_t *surface, cairo_pdf_resource_t resource){ cairo_pdf_object_t *object; object = _cairo_array_index (&surface->objects, resource.id - 1); object->offset = _cairo_output_stream_get_position (surface->output);}static void_cairo_pdf_surface_add_stream (cairo_pdf_surface_t *surface, cairo_pdf_resource_t stream){ /* XXX: Should be checking the return value here. */ _cairo_array_append (&surface->streams, &stream);}static void_cairo_pdf_surface_add_pattern (cairo_pdf_surface_t *surface, cairo_pdf_resource_t pattern){ /* XXX: Should be checking the return value here. */ _cairo_array_append (&surface->patterns, &pattern);}static cairo_pdf_resource_t_cairo_pdf_surface_add_alpha (cairo_pdf_surface_t *surface, double alpha){ cairo_pdf_resource_t resource; int num_alphas, i; double other; num_alphas = _cairo_array_num_elements (&surface->alphas); for (i = 0; i < num_alphas; i++) { _cairo_array_copy_element (&surface->alphas, i, &other); if (alpha == other) { resource.id = i; return resource; } } /* XXX: Should be checking the return value here. */ _cairo_array_append (&surface->alphas, &alpha); resource.id = _cairo_array_num_elements (&surface->alphas) - 1; return resource;}static cairo_surface_t *_cairo_pdf_surface_create_for_stream_internal (cairo_output_stream_t *output, double width, double height){ cairo_pdf_surface_t *surface; surface = malloc (sizeof (cairo_pdf_surface_t)); if (surface == NULL) { _cairo_error (CAIRO_STATUS_NO_MEMORY); return (cairo_surface_t*) &_cairo_surface_nil; } _cairo_surface_init (&surface->base, &cairo_pdf_surface_backend, CAIRO_CONTENT_COLOR_ALPHA); surface->output = output; surface->width = width; surface->height = height; _cairo_array_init (&surface->objects, sizeof (cairo_pdf_object_t)); _cairo_array_init (&surface->pages, sizeof (cairo_pdf_resource_t)); _cairo_array_init (&surface->patterns, sizeof (cairo_pdf_resource_t)); _cairo_array_init (&surface->xobjects, sizeof (cairo_pdf_resource_t)); _cairo_array_init (&surface->streams, sizeof (cairo_pdf_resource_t)); _cairo_array_init (&surface->alphas, sizeof (double)); surface->font_subsets = _cairo_scaled_font_subsets_create (PDF_SURFACE_MAX_GLYPHS_PER_FONT); if (! surface->font_subsets) { _cairo_error (CAIRO_STATUS_NO_MEMORY); free (surface); return (cairo_surface_t*) &_cairo_surface_nil; } _cairo_array_init (&surface->fonts, sizeof (cairo_pdf_font_t)); surface->next_available_resource.id = 1; surface->pages_resource = _cairo_pdf_surface_new_object (surface); surface->current_stream.active = FALSE; surface->has_clip = FALSE; surface->paginated_mode = CAIRO_PAGINATED_MODE_ANALYZE; /* Document header */ _cairo_output_stream_printf (surface->output, "%%PDF-1.4\r\n"); return _cairo_paginated_surface_create (&surface->base, CAIRO_CONTENT_COLOR_ALPHA, width, height, &cairo_pdf_surface_paginated_backend);}/** * cairo_pdf_surface_create_for_stream: * @write_func: a #cairo_write_func_t to accept the output data * @closure: the closure argument for @write_func * @width_in_points: width of the surface, in points (1 point == 1/72.0 inch) * @height_in_points: height of the surface, in points (1 point == 1/72.0 inch) * * Creates a PDF surface of the specified size in points to be written * incrementally to the stream represented by @write_func and @closure. * * Return value: a pointer to the newly created surface. The caller * owns the surface and should call cairo_surface_destroy when done * with it. * * This function always returns a valid pointer, but it will return a * pointer to a "nil" surface if an error such as out of memory * occurs. You can use cairo_surface_status() to check for this. * * Since: 1.2 */cairo_surface_t *cairo_pdf_surface_create_for_stream (cairo_write_func_t write_func, void *closure, double width_in_points, double height_in_points){ cairo_status_t status; cairo_output_stream_t *output; output = _cairo_output_stream_create (write_func, NULL, closure); status = _cairo_output_stream_get_status (output); if (status) { _cairo_error (status); return (cairo_surface_t*) &_cairo_surface_nil; } return _cairo_pdf_surface_create_for_stream_internal (output, width_in_points, height_in_points);}/** * cairo_pdf_surface_create: * @filename: a filename for the PDF output (must be writable) * @width_in_points: width of the surface, in points (1 point == 1/72.0 inch) * @height_in_points: height of the surface, in points (1 point == 1/72.0 inch) * * Creates a PDF surface of the specified size in points to be written * to @filename. * * Return value: a pointer to the newly created surface. The caller * owns the surface and should call cairo_surface_destroy when done * with it. * * This function always returns a valid pointer, but it will return a * pointer to a "nil" surface if an error such as out of memory * occurs. You can use cairo_surface_status() to check for this. * * Since: 1.2 **/cairo_surface_t *cairo_pdf_surface_create (const char *filename, double width_in_points, double height_in_points){ cairo_status_t status; cairo_output_stream_t *output; output = _cairo_output_stream_create_for_filename (filename); status = _cairo_output_stream_get_status (output); if (status) { _cairo_error (status); return (cairo_surface_t*) &_cairo_surface_nil; } return _cairo_pdf_surface_create_for_stream_internal (output, width_in_points, height_in_points);}static cairo_bool_t_cairo_surface_is_pdf (cairo_surface_t *surface){ return surface->backend == &cairo_pdf_surface_backend;}/* If the abstract_surface is a paginated surface, and that paginated * surface's target is a pdf_surface, then set pdf_surface to that * target. Otherwise return CAIRO_STATUS_SURFACE_TYPE_MISMATCH. */static cairo_status_t_extract_pdf_surface (cairo_surface_t *surface, cairo_pdf_surface_t **pdf_surface){ cairo_surface_t *target; if (! _cairo_surface_is_paginated (surface)) return CAIRO_STATUS_SURFACE_TYPE_MISMATCH; target = _cairo_paginated_surface_get_target (surface); if (! _cairo_surface_is_pdf (target)) return CAIRO_STATUS_SURFACE_TYPE_MISMATCH; *pdf_surface = (cairo_pdf_surface_t *) target; return CAIRO_STATUS_SUCCESS;}/** * cairo_pdf_surface_set_size: * @surface: a PDF cairo_surface_t * @width_in_points: new surface width, in points (1 point == 1/72.0 inch) * @height_in_points: new surface height, in points (1 point == 1/72.0 inch) * * Changes the size of a PDF surface for the current (and * subsequent) pages. * * This function should only be called before any drawing operations * have been performed on the current page. The simplest way to do * this is to call this function immediately after creating the * surface or immediately after completing a page with either * cairo_show_page() or cairo_copy_page(). * * Since: 1.2 **/voidcairo_pdf_surface_set_size (cairo_surface_t *surface, double width_in_points, double height_in_points){ cairo_pdf_surface_t *pdf_surface; cairo_status_t status; status = _extract_pdf_surface (surface, &pdf_surface); if (status) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -