⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cairo-scaled-font.c

📁 按照官方的说法:Cairo is a vector graphics library with cross-device output support. 翻译过来
💻 C
📖 第 1 页 / 共 3 页
字号:
/* $Id: cairo-scaled-font.c,v 1.12 2006-01-22 10:33:26 behdad Exp $ * * Copyright © 2005 Keith Packard * * 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 Keith Packard * * Contributor(s): *      Keith Packard <keithp@keithp.com> *	Carl D. Worth <cworth@cworth.org> *      Graydon Hoare <graydon@redhat.com> *      Owen Taylor <otaylor@redhat.com> */#include "cairoint.h"#include "cairo-scaled-font-test.h"static cairo_bool_t_cairo_scaled_glyph_keys_equal (const void *abstract_key_a, const void *abstract_key_b){    const cairo_scaled_glyph_t *key_a = abstract_key_a;    const cairo_scaled_glyph_t *key_b = abstract_key_b;    return (_cairo_scaled_glyph_index (key_a) ==	    _cairo_scaled_glyph_index (key_b));}static void_cairo_scaled_glyph_fini (cairo_scaled_glyph_t *scaled_glyph){    cairo_scaled_font_t	*scaled_font = scaled_glyph->scaled_font;    const cairo_surface_backend_t *surface_backend = scaled_font->surface_backend;    if (surface_backend != NULL && surface_backend->scaled_glyph_fini != NULL)	surface_backend->scaled_glyph_fini (scaled_glyph, scaled_font);    if (scaled_glyph->surface != NULL)	cairo_surface_destroy (&scaled_glyph->surface->base);    if (scaled_glyph->path != NULL)	_cairo_path_fixed_destroy (scaled_glyph->path);}static void_cairo_scaled_glyph_destroy (void *abstract_glyph){    cairo_scaled_glyph_t *scaled_glyph = abstract_glyph;    _cairo_scaled_glyph_fini (scaled_glyph);    free (scaled_glyph);}static const cairo_scaled_font_t _cairo_scaled_font_nil = {    { 0 },			/* hash_entry */    CAIRO_STATUS_NO_MEMORY,	/* status */    -1,				/* ref_count */    NULL,			/* font_face */    { 1., 0., 0., 1., 0, 0},	/* font_matrix */    { 1., 0., 0., 1., 0, 0},	/* ctm */    { CAIRO_ANTIALIAS_DEFAULT,	/* options */      CAIRO_SUBPIXEL_ORDER_DEFAULT,      CAIRO_HINT_STYLE_DEFAULT,      CAIRO_HINT_METRICS_DEFAULT} ,    { 1., 0., 0., 1., 0, 0},	/* scale */    { 0., 0., 0., 0., 0. },	/* extents */    NULL,			/* glyphs */    NULL,			/* surface_backend */    NULL,			/* surface_private */    CAIRO_SCALED_FONT_BACKEND_DEFAULT,};/** * _cairo_scaled_font_set_error: * @scaled_font: a scaled_font * @status: a status value indicating an error, (eg. not * CAIRO_STATUS_SUCCESS) * * Sets scaled_font->status to @status and calls _cairo_error; * * All assignments of an error status to scaled_font->status should happen * through _cairo_scaled_font_set_error() or else _cairo_error() should be * called immediately after the assignment. * * The purpose of this function is to allow the user to set a * breakpoint in _cairo_error() to generate a stack trace for when the * user causes cairo to detect an error. **/void_cairo_scaled_font_set_error (cairo_scaled_font_t *scaled_font,			      cairo_status_t status){    /* Don't overwrite an existing error. This preserves the first     * error, which is the most significant. It also avoids attempting     * to write to read-only data (eg. from a nil scaled_font). */    if (scaled_font->status == CAIRO_STATUS_SUCCESS)	scaled_font->status = status;    _cairo_error (status);}/** * cairo_scaled_font_get_type: * @scaled_font: a #cairo_scaled_font_t * * Return value: The type of @scaled_font. See #cairo_font_type_t. * * Since: 1.2 **/cairo_font_type_tcairo_scaled_font_get_type (cairo_scaled_font_t *scaled_font){    return scaled_font->backend->type;}/** * cairo_scaled_font_status: * @scaled_font: a #cairo_scaled_font_t * * Checks whether an error has previously occurred for this * scaled_font. * * Return value: %CAIRO_STATUS_SUCCESS or another error such as *   %CAIRO_STATUS_NO_MEMORY. **/cairo_status_tcairo_scaled_font_status (cairo_scaled_font_t *scaled_font){    return scaled_font->status;}/* Here we keep a unique mapping from * cairo_font_face_t/matrix/ctm/options => cairo_scaled_font_t. * * Here are the things that we want to map: * *  a) All otherwise referenced cairo_scaled_font_t's *  b) Some number of not otherwise referenced cairo_scaled_font_t's * * The implementation uses a hash table which covers (a) * completely. Then, for (b) we have an array of otherwise * unreferenced fonts (holdovers) which are expired in * least-recently-used order. * * The cairo_scaled_font_create code gets to treat this like a regular * hash table. All of the magic for the little holdover cache is in * cairo_scaled_font_reference and cairo_scaled_font_destroy. *//* This defines the size of the holdover array ... that is, the number * of scaled fonts we keep around even when not otherwise referenced */#define CAIRO_SCALED_FONT_MAX_HOLDOVERS 256typedef struct _cairo_scaled_font_map {    cairo_hash_table_t *hash_table;    cairo_scaled_font_t *holdovers[CAIRO_SCALED_FONT_MAX_HOLDOVERS];    int num_holdovers;} cairo_scaled_font_map_t;static cairo_scaled_font_map_t *cairo_scaled_font_map = NULL;CAIRO_MUTEX_DECLARE (cairo_scaled_font_map_mutex);static int_cairo_scaled_font_keys_equal (const void *abstract_key_a, const void *abstract_key_b);static cairo_scaled_font_map_t *_cairo_scaled_font_map_lock (void){    CAIRO_MUTEX_LOCK (cairo_scaled_font_map_mutex);    if (cairo_scaled_font_map == NULL) {	cairo_scaled_font_map = malloc (sizeof (cairo_scaled_font_map_t));	if (cairo_scaled_font_map == NULL)	    goto CLEANUP_MUTEX_LOCK;	cairo_scaled_font_map->hash_table =	    _cairo_hash_table_create (_cairo_scaled_font_keys_equal);	if (cairo_scaled_font_map->hash_table == NULL)	    goto CLEANUP_SCALED_FONT_MAP;	cairo_scaled_font_map->num_holdovers = 0;    }    return cairo_scaled_font_map; CLEANUP_SCALED_FONT_MAP:    free (cairo_scaled_font_map); CLEANUP_MUTEX_LOCK:    CAIRO_MUTEX_UNLOCK (cairo_scaled_font_map_mutex);    return NULL;}static void_cairo_scaled_font_map_unlock (void){   CAIRO_MUTEX_UNLOCK (cairo_scaled_font_map_mutex);}void_cairo_scaled_font_map_destroy (void){    int i;    cairo_scaled_font_map_t *font_map = cairo_scaled_font_map;    cairo_scaled_font_t *scaled_font;    if (font_map == NULL)	return;    CAIRO_MUTEX_UNLOCK (cairo_scaled_font_map_mutex);    for (i = 0; i < font_map->num_holdovers; i++) {	scaled_font = font_map->holdovers[i];	/* We should only get here through the reset_static_data path	 * and there had better not be any active references at that	 * point. */	assert (scaled_font->ref_count == 0);	_cairo_hash_table_remove (font_map->hash_table,				  &scaled_font->hash_entry);	_cairo_scaled_font_fini (scaled_font);	free (scaled_font);    }    _cairo_hash_table_destroy (font_map->hash_table);    free (cairo_scaled_font_map);    cairo_scaled_font_map = NULL;}/* Fowler / Noll / Vo (FNV) Hash (http://www.isthe.com/chongo/tech/comp/fnv/) * * Not necessarily better than a lot of other hashes, but should be OK, and * well tested with binary data. */#define FNV_32_PRIME ((uint32_t)0x01000193)#define FNV1_32_INIT ((uint32_t)0x811c9dc5)static uint32_t_hash_bytes_fnv (unsigned char *buffer,		 int            len,		 uint32_t       hval){    while (len--) {	hval *= FNV_32_PRIME;	hval ^= *buffer++;    }    return hval;}static void_cairo_scaled_font_init_key (cairo_scaled_font_t        *scaled_font,			     cairo_font_face_t	        *font_face,			     const cairo_matrix_t       *font_matrix,			     const cairo_matrix_t       *ctm,			     const cairo_font_options_t *options){    uint32_t hash = FNV1_32_INIT;    scaled_font->status = CAIRO_STATUS_SUCCESS;    scaled_font->font_face = font_face;    scaled_font->font_matrix = *font_matrix;    scaled_font->ctm = *ctm;    scaled_font->options = *options;    /* We do a bytewise hash on the font matrices, ignoring the     * translation values in the ctm */    hash = _hash_bytes_fnv ((unsigned char *)(&scaled_font->font_matrix.xx),			    sizeof(cairo_matrix_t), hash);    hash = _hash_bytes_fnv ((unsigned char *)(&scaled_font->ctm.xx),			    sizeof(double) * 4, hash);    hash ^= (unsigned long) scaled_font->font_face;    hash ^= cairo_font_options_hash (&scaled_font->options);    scaled_font->hash_entry.hash = hash;}static cairo_bool_t_cairo_scaled_font_keys_equal (const void *abstract_key_a, const void *abstract_key_b){    const cairo_scaled_font_t *key_a = abstract_key_a;    const cairo_scaled_font_t *key_b = abstract_key_b;    return (key_a->font_face == key_b->font_face &&	    memcmp ((unsigned char *)(&key_a->font_matrix.xx),		    (unsigned char *)(&key_b->font_matrix.xx),		    sizeof(cairo_matrix_t)) == 0 &&	    memcmp ((unsigned char *)(&key_a->ctm.xx),		    (unsigned char *)(&key_b->ctm.xx),		    sizeof(double) * 4) == 0 &&	    cairo_font_options_equal (&key_a->options, &key_b->options));}/* XXX: This 256 number is arbitary---we've never done any measurement * of this. In fact, having a per-font glyph caches each managed * separately is probably not waht we want anyway. Would probably be * much better to have a single cache for glyphs with random * replacement across all glyphs of all fonts. */static int max_glyphs_cached_per_font = 256;/* For internal testing purposes only. Not part of the supported API. */void_cairo_scaled_font_test_set_max_glyphs_cached_per_font (int max){    max_glyphs_cached_per_font = max;}/* * Basic cairo_scaled_font_t object management */cairo_status_t_cairo_scaled_font_init (cairo_scaled_font_t               *scaled_font,			 cairo_font_face_t		   *font_face,			 const cairo_matrix_t              *font_matrix,			 const cairo_matrix_t              *ctm,			 const cairo_font_options_t	   *options,			 const cairo_scaled_font_backend_t *backend){    scaled_font->ref_count = 1;    _cairo_scaled_font_init_key (scaled_font, font_face,				 font_matrix, ctm, options);    cairo_font_face_reference (font_face);    cairo_matrix_multiply (&scaled_font->scale,			   &scaled_font->font_matrix,			   &scaled_font->ctm);    scaled_font->glyphs = _cairo_cache_create (_cairo_scaled_glyph_keys_equal,					       _cairo_scaled_glyph_destroy,					       max_glyphs_cached_per_font);    scaled_font->surface_backend = NULL;    scaled_font->surface_private = NULL;    scaled_font->backend = backend;    return CAIRO_STATUS_SUCCESS;}void_cairo_scaled_font_freeze_cache (cairo_scaled_font_t *scaled_font){    _cairo_cache_freeze (scaled_font->glyphs);}void_cairo_scaled_font_thaw_cache (cairo_scaled_font_t *scaled_font){    _cairo_cache_thaw (scaled_font->glyphs);}void_cairo_scaled_font_set_metrics (cairo_scaled_font_t	    *scaled_font,				cairo_font_extents_t	    *fs_metrics){    double  font_scale_x, font_scale_y;    _cairo_matrix_compute_scale_factors (&scaled_font->font_matrix,					 &font_scale_x, &font_scale_y,					 /* XXX */ 1);    /*     * The font responded in unscaled units, scale by the font     * matrix scale factors to get to user space     */    scaled_font->extents.ascent = fs_metrics->ascent * font_scale_y;    scaled_font->extents.descent = fs_metrics->descent * font_scale_y;    scaled_font->extents.height = fs_metrics->height * font_scale_y;    scaled_font->extents.max_x_advance = fs_metrics->max_x_advance * font_scale_x;    scaled_font->extents.max_y_advance = fs_metrics->max_y_advance * font_scale_y;}void_cairo_scaled_font_fini (cairo_scaled_font_t *scaled_font){    if (scaled_font->font_face != NULL)	cairo_font_face_destroy (scaled_font->font_face);    if (scaled_font->glyphs != NULL)	_cairo_cache_destroy (scaled_font->glyphs);    if (scaled_font->surface_backend != NULL &&	scaled_font->surface_backend->scaled_font_fini != NULL)	scaled_font->surface_backend->scaled_font_fini (scaled_font);    scaled_font->backend->fini (scaled_font);}/** * cairo_scaled_font_create: * @font_face: a #cairo_font_face_t * @font_matrix: font space to user space transformation matrix for the *       font. In the simplest case of a N point font, this matrix is *       just a scale by N, but it can also be used to shear the font *       or stretch it unequally along the two axes. See *       cairo_set_font_matrix(). * @ctm: user to device transformation matrix with which the font will *       be used. * @options: options to use when getting metrics for the font and *           rendering with it. * * Creates a #cairo_scaled_font_t object from a font face and matrices that * describe the size of the font and the environment in which it will * be used. * * Return value: a newly created #cairo_scaled_font_t. Destroy with *  cairo_scaled_font_destroy() **/cairo_scaled_font_t *cairo_scaled_font_create (cairo_font_face_t          *font_face,			  const cairo_matrix_t       *font_matrix,			  const cairo_matrix_t       *ctm,			  const cairo_font_options_t *options){    cairo_status_t status;    cairo_scaled_font_map_t *font_map;    cairo_scaled_font_t key, *scaled_font = NULL;    if (font_face->status)	return (cairo_scaled_font_t *)&_cairo_scaled_font_nil;    font_map = _cairo_scaled_font_map_lock ();    if (font_map == NULL)	goto UNWIND;    _cairo_scaled_font_init_key (&key, font_face,				 font_matrix, ctm, options);    /* Return existing scaled_font if it exists in the hash table. */    if (_cairo_hash_table_lookup (font_map->hash_table, &key.hash_entry,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -