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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
*
* 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@isi.edu>
*/
#include "cairoint.h"
/* First we implement a global font cache for named fonts. */
typedef struct {
cairo_cache_entry_base_t base;
const char *family;
cairo_font_slant_t slant;
cairo_font_weight_t weight;
} cairo_font_cache_key_t;
typedef struct {
cairo_font_cache_key_t key;
cairo_unscaled_font_t *unscaled;
} cairo_font_cache_entry_t;
static unsigned long
_font_cache_hash (void *cache, void *key)
{
cairo_font_cache_key_t *in;
in = (cairo_font_cache_key_t *) key;
unsigned long hash;
/* 1607 and 1451 are just a couple random primes. */
hash = _cairo_hash_string (in->family);
hash += ((unsigned long) in->slant) * 1607;
hash += ((unsigned long) in->weight) * 1451;
return hash;
}
static int
_font_cache_keys_equal (void *cache,
void *k1,
void *k2)
{
cairo_font_cache_key_t *a, *b;
a = (cairo_font_cache_key_t *) k1;
b = (cairo_font_cache_key_t *) k2;
return (strcmp (a->family, b->family) == 0)
&& (a->weight == b->weight)
&& (a->slant == b->slant);
}
static cairo_status_t
_font_cache_create_entry (void *cache,
void *key,
void **return_value)
{
cairo_font_cache_key_t *k;
cairo_font_cache_entry_t *entry;
k = (cairo_font_cache_key_t *) key;
const struct cairo_font_backend *backend = CAIRO_FONT_BACKEND_DEFAULT;
/* XXX: The current freetype backend may return NULL, (for example
* if no fonts are installed), but I would like to guarantee that
* the toy API always returns at least *some* font, so I would
* like to build in some sort fo font here, (even a really lame,
* ugly one if necessary). */
entry = malloc (sizeof (cairo_font_cache_entry_t));
if (entry == NULL)
goto FAIL;
entry->key.slant = k->slant;
entry->key.weight = k->weight;
entry->key.family = strdup(k->family);
if (entry->key.family == NULL)
goto FREE_ENTRY;
entry->unscaled = backend->create (k->family, k->slant, k->weight);
if (entry->unscaled == NULL)
goto FREE_FAMILY;
/* Not sure how to measure backend font mem; use a simple count for now.*/
entry->key.base.memory = 1;
*return_value = entry;
return CAIRO_STATUS_SUCCESS;
FREE_FAMILY:
free ((void *) entry->key.family);
FREE_ENTRY:
free (entry);
FAIL:
return CAIRO_STATUS_NO_MEMORY;
}
static void
_font_cache_destroy_entry (void *cache,
void *entry)
{
cairo_font_cache_entry_t *e;
e = (cairo_font_cache_entry_t *) entry;
_cairo_unscaled_font_destroy (e->unscaled);
free ((void *) e->key.family);
free (e);
}
static void
_font_cache_destroy_cache (void *cache)
{
free (cache);
}
const struct cairo_cache_backend cairo_font_cache_backend = {
_font_cache_hash,
_font_cache_keys_equal,
_font_cache_create_entry,
_font_cache_destroy_entry,
_font_cache_destroy_cache
};
static void
_lock_global_font_cache (void)
{
/* FIXME: implement locking. */
}
static void
_unlock_global_font_cache (void)
{
/* FIXME: implement locking. */
}
static cairo_cache_t *
_global_font_cache = NULL;
static cairo_cache_t *
_get_global_font_cache (void)
{
if (_global_font_cache == NULL) {
_global_font_cache = malloc (sizeof (cairo_cache_t));
if (_global_font_cache == NULL)
goto FAIL;
if (_cairo_cache_init (_global_font_cache,
&cairo_font_cache_backend,
CAIRO_FONT_CACHE_NUM_FONTS_DEFAULT))
goto FAIL;
}
return _global_font_cache;
FAIL:
if (_global_font_cache)
free (_global_font_cache);
_global_font_cache = NULL;
return NULL;
}
/* Now the internal "unscaled + scale" font API */
cairo_unscaled_font_t *
_cairo_unscaled_font_create (const char *family,
cairo_font_slant_t slant,
cairo_font_weight_t weight)
{
cairo_cache_t * cache;
cairo_font_cache_key_t key;
cairo_font_cache_entry_t *font;
cairo_status_t status;
_lock_global_font_cache ();
cache = _get_global_font_cache ();
if (cache == NULL) {
_unlock_global_font_cache ();
return NULL;
}
key.family = family;
key.slant = slant;
key.weight = weight;
status = _cairo_cache_lookup (cache, &key, (void **) &font);
if (status) {
_unlock_global_font_cache ();
return NULL;
}
_cairo_unscaled_font_reference (font->unscaled);
_unlock_global_font_cache ();
return font->unscaled;
}
void
_cairo_font_init (cairo_font_t *scaled,
cairo_font_scale_t *scale,
cairo_unscaled_font_t *unscaled)
{
scaled->scale = *scale;
scaled->unscaled = unscaled;
scaled->refcount = 1;
}
cairo_status_t
_cairo_unscaled_font_init (cairo_unscaled_font_t *font,
const struct cairo_font_backend *backend)
{
font->refcount = 1;
font->backend = backend;
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_unscaled_font_text_to_glyphs (cairo_unscaled_font_t *font,
cairo_font_scale_t *scale,
const unsigned char *utf8,
cairo_glyph_t **glyphs,
int *num_glyphs)
{
return font->backend->text_to_glyphs (font, scale, utf8, glyphs, num_glyphs);
}
cairo_status_t
_cairo_unscaled_font_glyph_extents (cairo_unscaled_font_t *font,
cairo_font_scale_t *scale,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_text_extents_t *extents)
{
return font->backend->glyph_extents(font, scale, glyphs, num_glyphs, extents);
}
cairo_status_t
_cairo_unscaled_font_glyph_bbox (cairo_unscaled_font_t *font,
cairo_font_scale_t *scale,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_box_t *bbox)
{
return font->backend->glyph_bbox (font, scale, glyphs, num_glyphs, bbox);
}
cairo_status_t
_cairo_unscaled_font_show_glyphs (cairo_unscaled_font_t *font,
cairo_font_scale_t *scale,
cairo_operator_t operator,
cairo_surface_t *source,
cairo_surface_t *surface,
int source_x,
int source_y,
cairo_glyph_t *glyphs,
int num_glyphs)
{
cairo_status_t status;
if (surface->backend->show_glyphs != NULL) {
status = surface->backend->show_glyphs (font, scale, operator, source,
surface, source_x, source_y,
glyphs, num_glyphs);
if (status == CAIRO_STATUS_SUCCESS)
return status;
}
/* Surface display routine either does not exist or failed. */
return font->backend->show_glyphs (font, scale, operator, source,
surface, source_x, source_y,
glyphs, num_glyphs);
}
cairo_status_t
_cairo_unscaled_font_glyph_path (cairo_unscaled_font_t *font,
cairo_font_scale_t *scale,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_path_t *path)
{
return font->backend->glyph_path (font, scale, glyphs, num_glyphs, path);
}
cairo_status_t
_cairo_unscaled_font_font_extents (cairo_unscaled_font_t *font,
cairo_font_scale_t *scale,
cairo_font_extents_t *extents)
{
return font->backend->font_extents(font, scale, extents);
}
void
_cairo_unscaled_font_reference (cairo_unscaled_font_t *font)
{
font->refcount++;
}
void
_cairo_unscaled_font_destroy (cairo_unscaled_font_t *font)
{
if (--(font->refcount) > 0)
return;
if (font->backend)
font->backend->destroy (font);
}
/* Public font API follows. */
void
cairo_font_reference (cairo_font_t *font)
{
font->refcount++;
}
void
cairo_font_destroy (cairo_font_t *font)
{
if (--(font->refcount) > 0)
return;
if (font->unscaled)
_cairo_unscaled_font_destroy (font->unscaled);
free (font);
}
void
cairo_font_set_transform (cairo_font_t *font,
cairo_matrix_t *matrix)
{
double dummy;
cairo_matrix_get_affine (matrix,
&font->scale.matrix[0][0],
&font->scale.matrix[0][1],
&font->scale.matrix[1][0],
&font->scale.matrix[1][1],
&dummy, &dummy);
}
void
cairo_font_current_transform (cairo_font_t *font,
cairo_matrix_t *matrix)
{
cairo_matrix_set_affine (matrix,
font->scale.matrix[0][0],
font->scale.matrix[0][1],
font->scale.matrix[1][0],
font->scale.matrix[1][1],
0, 0);
}
/* Now we implement functions to access a default global image & metrics
* cache.
*/
unsigned long
_cairo_glyph_cache_hash (void *cache, void *key)
{
cairo_glyph_cache_key_t *in;
in = (cairo_glyph_cache_key_t *) key;
return
((unsigned long) in->unscaled)
^ ((unsigned long) in->scale.matrix[0][0])
^ ((unsigned long) in->scale.matrix[0][1])
^ ((unsigned long) in->scale.matrix[1][0])
^ ((unsigned long) in->scale.matrix[1][1])
^ in->index;
}
int
_cairo_glyph_cache_keys_equal (void *cache,
void *k1,
void *k2)
{
cairo_glyph_cache_key_t *a, *b;
a = (cairo_glyph_cache_key_t *) k1;
b = (cairo_glyph_cache_key_t *) k2;
return (a->index == b->index)
&& (a->unscaled == b->unscaled)
&& (a->scale.matrix[0][0] == b->scale.matrix[0][0])
&& (a->scale.matrix[0][1] == b->scale.matrix[0][1])
&& (a->scale.matrix[1][0] == b->scale.matrix[1][0])
&& (a->scale.matrix[1][1] == b->scale.matrix[1][1]);
}
static cairo_status_t
_image_glyph_cache_create_entry (void *cache,
void *key,
void **return_value)
{
cairo_glyph_cache_key_t *k = (cairo_glyph_cache_key_t *) key;
cairo_image_glyph_cache_entry_t *im;
cairo_status_t status;
im = calloc (1, sizeof (cairo_image_glyph_cache_entry_t));
if (im == NULL)
return CAIRO_STATUS_NO_MEMORY;
im->key = *k;
status = im->key.unscaled->backend->create_glyph (im);
if (status != CAIRO_STATUS_SUCCESS) {
free (im);
return status;
}
_cairo_unscaled_font_reference (im->key.unscaled);
im->key.base.memory =
sizeof (cairo_image_glyph_cache_entry_t)
+ (im->image ?
sizeof (cairo_image_surface_t)
+ 28 * sizeof (int) /* rough guess at size of pixman image structure */
+ (im->image->height * im->image->stride) : 0);
*return_value = im;
return CAIRO_STATUS_SUCCESS;
}
static void
_image_glyph_cache_destroy_entry (void *cache,
void *value)
{
cairo_image_glyph_cache_entry_t *im;
im = (cairo_image_glyph_cache_entry_t *) value;
_cairo_unscaled_font_destroy (im->key.unscaled);
cairo_surface_destroy (&(im->image->base));
free (im);
}
static void
_image_glyph_cache_destroy_cache (void *cache)
{
free (cache);
}
const cairo_cache_backend_t cairo_image_cache_backend = {
_cairo_glyph_cache_hash,
_cairo_glyph_cache_keys_equal,
_image_glyph_cache_create_entry,
_image_glyph_cache_destroy_entry,
_image_glyph_cache_destroy_cache
};
void
_cairo_lock_global_image_glyph_cache()
{
/* FIXME: implement locking. */
}
void
_cairo_unlock_global_image_glyph_cache()
{
/* FIXME: implement locking. */
}
static cairo_cache_t *
_global_image_glyph_cache = NULL;
cairo_cache_t *
_cairo_get_global_image_glyph_cache ()
{
if (_global_image_glyph_cache == NULL) {
_global_image_glyph_cache = malloc (sizeof (cairo_cache_t));
if (_global_image_glyph_cache == NULL)
goto FAIL;
if (_cairo_cache_init (_global_image_glyph_cache,
&cairo_image_cache_backend,
CAIRO_IMAGE_GLYPH_CACHE_MEMORY_DEFAULT))
goto FAIL;
}
return _global_image_glyph_cache;
FAIL:
if (_global_image_glyph_cache)
free (_global_image_glyph_cache);
_global_image_glyph_cache = NULL;
return NULL;
}
|