1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/* $Id$
*
* Copyright © 2004 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>
*/
#include "cairo-5c.h"
Value
do_Cairo_select_font_face (Value cv, Value fv, Value sv, Value wv)
{
cairo_5c_t *c5c = cairo_5c_get (cv);
char *family = StrzPart (fv, "invalid family");
cairo_font_slant_t slant = EnumIntPart (sv, "invalid slant");
cairo_font_weight_t weight = EnumIntPart (wv, "invalid weight");
if (!aborting)
cairo_select_font_face (c5c->cr, family, slant, weight);
return Void;
}
typedef struct {
DataType *data;
Value name;
cairo_font_face_t *font_face;
double size;
} cairo_5c_font_t;
static void
cairo_5c_font_mark (void *v)
{
cairo_5c_font_t *c5f = v;
MemReference (c5f->name);
}
static int
cairo_5c_font_free (void *v)
{
cairo_5c_font_t *c5f = v;
if (c5f->font_face) {
cairo_font_face_destroy (c5f->font_face);
c5f->font_face = NULL;
}
return 1;
}
static DataType Cairo5cFontType = {
cairo_5c_font_mark,
cairo_5c_font_free,
"Cairo5cFont"
};
#define CAIRO_5C_FONT_CACHE 31
#define CAIRO_5C_FONT_REHASH 29
typedef struct {
DataType *data;
cairo_5c_font_t *cache[CAIRO_5C_FONT_CACHE];
} cairo_5c_font_cache_t;
static void
cairo_5c_font_cache_mark (void *v)
{
cairo_5c_font_cache_t *c5fc = v;
int i;
for (i = 0; i < CAIRO_5C_FONT_CACHE; i++)
MemReference (c5fc->cache[i]);
}
static DataType Cairo5cFontCacheType = {
cairo_5c_font_cache_mark,
NULL,
"Cairo5cFontCache"
};
static cairo_5c_font_t *
cairo_5c_font_create (Value name)
{
ENTER ();
char *string_name = StrzPart (name, "invalid name");
FcPattern *pat, *match;
cairo_font_face_t *font_face;
double size = 0;
FcResult result;
cairo_5c_font_t *c5f;
if (aborting)
RETURN (NULL);
pat = FcNameParse ((FcChar8 *) string_name);
FcConfigSubstitute (0, pat, FcMatchPattern);
FcDefaultSubstitute (pat);
match = FcFontMatch (0, pat, &result);
FcPatternDestroy (pat);
if (!match)
{
RaiseStandardException (exception_open_error, 3,
NewStrString ("can't open font"),
FileGetError (ENOENT), name);
RETURN (NULL);
}
FcPatternGetDouble (match, FC_SIZE, 0, &size);
font_face = cairo_ft_font_face_create_for_pattern (match);
FcPatternDestroy (match);
if (!font_face)
{
RaiseStandardException (exception_open_error, 3,
NewStrString ("can't open font"),
FileGetError (ENOENT), name);
RETURN (NULL);
}
c5f = ALLOCATE (&Cairo5cFontType, sizeof (cairo_5c_font_t));
c5f->name = name;
c5f->font_face = font_face;
c5f->size = size;
RETURN (c5f);
}
static cairo_5c_font_cache_t *font_cache;
static int font_matches;
static int font_misses;
static cairo_5c_font_t *
cairo_5c_font_find (Value name)
{
ENTER ();
int hash = ValueInt (ValueHash (name));
cairo_5c_font_t *c5f;
int i;
int count;
if (!font_cache)
{
font_cache = ALLOCATE (&Cairo5cFontCacheType, sizeof (cairo_5c_font_cache_t));
for (i = 0; i < CAIRO_5C_FONT_CACHE; i++)
font_cache->cache[i] = NULL;
MemAddRoot (font_cache);
}
for (i = hash % CAIRO_5C_FONT_CACHE, count = 0;
(c5f = font_cache->cache[i]) && count < CAIRO_5C_FONT_CACHE;
i = (i + (hash % CAIRO_5C_FONT_REHASH + 1)) % CAIRO_5C_FONT_CACHE,
count++)
{
if (Equal (c5f->name, name) == TrueVal) {
++font_matches;
RETURN(c5f);
}
}
++font_misses;
c5f = cairo_5c_font_create (name);
if (c5f)
font_cache->cache[hash % CAIRO_5C_FONT_CACHE] = c5f;
RETURN (c5f);
}
Value
do_Cairo_set_font (Value cv, Value fv)
{
ENTER ();
cairo_5c_t *c5c = cairo_5c_get (cv);
cairo_5c_font_t *c5f = cairo_5c_font_find (fv);
if (!c5f)
RETURN (Void);
cairo_set_font_face (c5c->cr, c5f->font_face);
cairo_set_font_size (c5c->cr, c5f->size);
RETURN(Void);
}
Value
do_Cairo_set_font_size (Value cv, Value sv)
{
cairo_5c_t *c5c = cairo_5c_get (cv);
double size = DoublePart (sv, "invalid size");
if (!aborting)
cairo_set_font_size (c5c->cr, size);
return Void;
}
Value
do_Cairo_set_font_matrix (Value cv, Value mv)
{
ENTER ();
cairo_5c_t *c5c = cairo_5c_get (cv);
cairo_matrix_t matrix;
if (aborting)
RETURN(Void);
cairo_matrix_part (mv, &matrix, "invalid matrix");
if (aborting)
RETURN(Void);
cairo_set_font_matrix (c5c->cr, &matrix);
RETURN(Void);
}
Value
do_Cairo_get_font_matrix (Value cv)
{
ENTER ();
cairo_5c_t *c5c = cairo_5c_get (cv);
cairo_matrix_t matrix;
Value ret;
if (aborting)
RETURN(Void);
cairo_get_font_matrix (c5c->cr, &matrix);
ret = new_cairo_matrix (&matrix);
RETURN(ret);
}
Value
do_Cairo_font_extents (Value cv)
{
ENTER ();
cairo_5c_t *c5c = cairo_5c_get (cv);
cairo_font_extents_t extents;
Value ret;
BoxPtr box;
if (aborting)
return Void;
memset (&extents, '\0', sizeof (extents));
cairo_font_extents (c5c->cr, &extents);
ret = NewStruct (TypeCanon (typeCairoFontExtents)->structs.structs, False);
box = ret->structs.values;
BoxValueSet (box, 0, NewDoubleFloat (extents.ascent));
BoxValueSet (box, 1, NewDoubleFloat (extents.descent));
BoxValueSet (box, 2, NewDoubleFloat (extents.height));
BoxValueSet (box, 3, NewDoubleFloat (extents.max_x_advance));
BoxValueSet (box, 4, NewDoubleFloat (extents.max_y_advance));
RETURN (ret);
}
Value
do_Cairo_show_text (Value cv, Value uv)
{
cairo_5c_t *c5c = cairo_5c_get (cv);
char *utf8 = StrzPart (uv, "invalid text");
if (!aborting)
{
cairo_show_text (c5c->cr, utf8);
cairo_5c_dirty (c5c);
}
return Void;
}
Value
do_Cairo_text_path (Value cv, Value uv)
{
cairo_5c_t *c5c = cairo_5c_get (cv);
char *utf8 = StrzPart (uv, "invalid text");
if (!aborting)
cairo_text_path (c5c->cr, utf8);
return Void;
}
Value
do_Cairo_text_extents (Value cv, Value uv)
{
ENTER ();
cairo_5c_t *c5c = cairo_5c_get (cv);
char *utf8 = StrzPart (uv, "invalid text");
cairo_text_extents_t extents;
Value ret;
BoxPtr box;
if (aborting)
return Void;
memset (&extents, '\0', sizeof (extents));
cairo_text_extents (c5c->cr, utf8, &extents);
ret = NewStruct (TypeCanon (typeCairoTextExtents)->structs.structs, False);
box = ret->structs.values;
BoxValueSet (box, 0, NewDoubleFloat (extents.x_bearing));
BoxValueSet (box, 1, NewDoubleFloat (extents.y_bearing));
BoxValueSet (box, 2, NewDoubleFloat (extents.width));
BoxValueSet (box, 3, NewDoubleFloat (extents.height));
BoxValueSet (box, 4, NewDoubleFloat (extents.x_advance));
BoxValueSet (box, 5, NewDoubleFloat (extents.y_advance));
RETURN (ret);
}
|