📄 cairo-svg-surface.c
字号:
/* cairo - a vector graphics library with display and print output * * Copyright © 2004 Red Hat, Inc * Copyright © 2005-2006 Emmanuel Pacaud <emmanuel.pacaud@free.fr> * 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> * Emmanuel Pacaud <emmanuel.pacaud@univ-poitiers.fr> * Carl Worth <cworth@cworth.org> */#include "cairoint.h"#include "cairo-svg.h"#include "cairo-svg-test.h"#include "cairo-path-fixed-private.h"#include "cairo-ft-private.h"#include "cairo-meta-surface-private.h"#include "cairo-paginated-surface-private.h"#include "cairo-scaled-font-subsets-private.h"#include "cairo-output-stream-private.h"typedef struct cairo_svg_document cairo_svg_document_t;typedef struct cairo_svg_surface cairo_svg_surface_t;static const int invalid_pattern_id = -1;static const cairo_svg_version_t _cairo_svg_versions[] ={ CAIRO_SVG_VERSION_1_1, CAIRO_SVG_VERSION_1_2};#define CAIRO_SVG_VERSION_LAST ((int)(sizeof (_cairo_svg_versions) / sizeof (_cairo_svg_versions[0])))static const char * _cairo_svg_version_strings[CAIRO_SVG_VERSION_LAST] ={ "SVG 1.1", "SVG 1.2"};static const char * _cairo_svg_internal_version_strings[CAIRO_SVG_VERSION_LAST] ={ "1.1", "1.2"};struct cairo_svg_document { cairo_output_stream_t *output_stream; unsigned long refcount; cairo_surface_t *owner; cairo_bool_t finished; double width; double height; cairo_output_stream_t *xml_node_defs; cairo_output_stream_t *xml_node_glyphs; unsigned int surface_id; unsigned int linear_pattern_id; unsigned int radial_pattern_id; unsigned int pattern_id; unsigned int filter_id; unsigned int clip_id; unsigned int mask_id; cairo_bool_t alpha_filter; cairo_array_t meta_snapshots; cairo_svg_version_t svg_version; cairo_scaled_font_subsets_t *font_subsets;};struct cairo_svg_surface { cairo_surface_t base; cairo_content_t content; unsigned int id; double width; double height; cairo_svg_document_t *document; cairo_output_stream_t *xml_node; unsigned int clip_level; unsigned int base_clip; cairo_paginated_mode_t paginated_mode;};typedef struct { unsigned int id; cairo_meta_surface_t *meta;} cairo_meta_snapshot_t;static cairo_svg_document_t *_cairo_svg_document_create (cairo_output_stream_t *stream, double width, double height, cairo_svg_version_t version);static void_cairo_svg_document_destroy (cairo_svg_document_t *document);static cairo_status_t_cairo_svg_document_finish (cairo_svg_document_t *document);static cairo_svg_document_t *_cairo_svg_document_reference (cairo_svg_document_t *document);static cairo_surface_t *_cairo_svg_surface_create_for_document (cairo_svg_document_t *document, cairo_content_t content, double width, double height);static cairo_surface_t *_cairo_svg_surface_create_for_stream_internal (cairo_output_stream_t *stream, double width, double height, cairo_svg_version_t version);static const cairo_surface_backend_t cairo_svg_surface_backend;static const cairo_paginated_surface_backend_t cairo_svg_surface_paginated_backend;/** * cairo_svg_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 SVG 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_svg_surface_create_for_stream (cairo_write_func_t write_func, void *closure, double width, double height){ cairo_status_t status; cairo_output_stream_t *stream; stream = _cairo_output_stream_create (write_func, NULL, closure); status = _cairo_output_stream_get_status (stream); if (status) { _cairo_error (status); return (cairo_surface_t *) &_cairo_surface_nil; } return _cairo_svg_surface_create_for_stream_internal (stream, width, height, CAIRO_SVG_VERSION_1_1);}/** * cairo_svg_surface_create: * @filename: a filename for the SVG 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 SVG 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_svg_surface_create (const char *filename, double width, double height){ cairo_status_t status; cairo_output_stream_t *stream; stream = _cairo_output_stream_create_for_filename (filename); status = _cairo_output_stream_get_status (stream); if (status) { _cairo_error (status); return (cairo_surface_t *) &_cairo_surface_nil; } return _cairo_svg_surface_create_for_stream_internal (stream, width, height, CAIRO_SVG_VERSION_1_1);}static cairo_bool_t_cairo_surface_is_svg (cairo_surface_t *surface){ return surface->backend == &cairo_svg_surface_backend;}/* If the abstract_surface is a paginated surface, and that paginated * surface's target is a svg_surface, then set svg_surface to that * target. Otherwise return CAIRO_STATUS_SURFACE_TYPE_MISMATCH. */static cairo_status_t_extract_svg_surface (cairo_surface_t *surface, cairo_svg_surface_t **svg_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_svg (target)) return CAIRO_STATUS_SURFACE_TYPE_MISMATCH; *svg_surface = (cairo_svg_surface_t *) target; return CAIRO_STATUS_SUCCESS;}/** * cairo_svg_surface_restrict_to_version: * @surface: a SVG #cairo_surface_t * @version: SVG version * * Restricts the generated SVG file to @version. See cairo_svg_get_versions() * for a list of available version values that can be used here. * * This function should only be called before any drawing operations * have been performed on the given surface. The simplest way to do * this is to call this function immediately after creating the * surface. * * Since: 1.2 **/voidcairo_svg_surface_restrict_to_version (cairo_surface_t *abstract_surface, cairo_svg_version_t version){ cairo_svg_surface_t *surface; cairo_status_t status; status = _extract_svg_surface (abstract_surface, &surface); if (status) { _cairo_surface_set_error (abstract_surface, CAIRO_STATUS_SURFACE_TYPE_MISMATCH); return; } if (version >= 0 && version < CAIRO_SVG_VERSION_LAST) surface->document->svg_version = version;}/** * cairo_svg_get_versions: * @versions: supported version list * @num_versions: list length * * Used to retrieve the list of supported versions. See * cairo_svg_surface_restrict_to_version(). * * Since: 1.2 **/voidcairo_svg_get_versions (cairo_svg_version_t const **versions, int *num_versions){ if (versions != NULL) *versions = _cairo_svg_versions; if (num_versions != NULL) *num_versions = CAIRO_SVG_VERSION_LAST;}/** * cairo_svg_version_to_string: * @version: a version id * * Get the string representation of the given @version id. This function * will return NULL if @version isn't valid. See cairo_svg_get_versions() * for a way to get the list of valid version ids. * * Return value: the string associated to given version. * * Since: 1.2 **/const char *cairo_svg_version_to_string (cairo_svg_version_t version){ if (version < 0 || version >= CAIRO_SVG_VERSION_LAST) return NULL; return _cairo_svg_version_strings[version];}static cairo_surface_t *_cairo_svg_surface_create_for_document (cairo_svg_document_t *document, cairo_content_t content, double width, double height){ cairo_svg_surface_t *surface; surface = malloc (sizeof (cairo_svg_surface_t)); if (surface == NULL) { _cairo_error (CAIRO_STATUS_NO_MEMORY); return (cairo_surface_t*) &_cairo_surface_nil; } _cairo_surface_init (&surface->base, &cairo_svg_surface_backend, content); surface->width = width; surface->height = height; surface->document = _cairo_svg_document_reference (document); surface->clip_level = 0; surface->id = document->surface_id++; surface->base_clip = document->clip_id++; _cairo_output_stream_printf (document->xml_node_defs, "<clipPath id=\"clip%d\">\n" " <rect width=\"%f\" height=\"%f\"/>\n" "</clipPath>\n", surface->base_clip, width, height); surface->xml_node = _cairo_memory_stream_create (); if (content == CAIRO_CONTENT_COLOR) { _cairo_output_stream_printf (surface->xml_node, "<rect width=\"%f\" height=\"%f\" " "style=\"opacity: 1; stroke: none; " "fill: rgb(0,0,0);\"/>\n", width, height); } surface->paginated_mode = CAIRO_PAGINATED_MODE_ANALYZE; surface->content = content; return _cairo_paginated_surface_create (&surface->base, surface->content, surface->width, surface->height, &cairo_svg_surface_paginated_backend);}static cairo_surface_t *_cairo_svg_surface_create_for_stream_internal (cairo_output_stream_t *stream, double width, double height, cairo_svg_version_t version){ cairo_svg_document_t *document; cairo_surface_t *surface; document = _cairo_svg_document_create (stream, width, height, version); if (document == NULL) { _cairo_error (CAIRO_STATUS_NO_MEMORY); return (cairo_surface_t *) &_cairo_surface_nil; } surface = _cairo_svg_surface_create_for_document (document, CAIRO_CONTENT_COLOR_ALPHA, width, height); document->owner = surface; _cairo_svg_document_destroy (document); return surface;}static voidemit_transform (cairo_output_stream_t *output, char const *attribute_str, char const *trailer, cairo_matrix_t *matrix){ _cairo_output_stream_printf (output, "%s=\"matrix(%f,%f,%f,%f,%f,%f)\"%s", attribute_str, matrix->xx, matrix->yx, matrix->xy, matrix->yy, matrix->x0, matrix->y0, trailer);}typedef struct{ cairo_output_stream_t *output; cairo_matrix_t *ctm_inverse;} svg_path_info_t;static cairo_status_t_cairo_svg_path_move_to (void *closure, cairo_point_t *point){ svg_path_info_t *info = closure; double x = _cairo_fixed_to_double (point->x); double y = _cairo_fixed_to_double (point->y); if (info->ctm_inverse) cairo_matrix_transform_point (info->ctm_inverse, &x, &y); _cairo_output_stream_printf (info->output, "M %f %f ", x, y); return CAIRO_STATUS_SUCCESS;}static cairo_status_t_cairo_svg_path_line_to (void *closure, cairo_point_t *point){ svg_path_info_t *info = closure; double x = _cairo_fixed_to_double (point->x); double y = _cairo_fixed_to_double (point->y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -