1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2002 University of Southern California
4
 * Copyright © 2005 Red Hat, Inc.
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is University of Southern
32
 * California.
33
 *
34
 * Contributor(s):
35
 *	Carl D. Worth <cworth@cworth.org>
36
 */
37

            
38
#include "cairoint.h"
39

            
40
#include "cairo-clip-inline.h"
41
#include "cairo-clip-private.h"
42
#include "cairo-error-private.h"
43
#include "cairo-list-inline.h"
44
#include "cairo-gstate-private.h"
45
#include "cairo-pattern-private.h"
46
#include "cairo-traps-private.h"
47

            
48
static cairo_status_t
49
_cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other);
50

            
51
static cairo_status_t
52
_cairo_gstate_ensure_font_face (cairo_gstate_t *gstate);
53

            
54
static cairo_status_t
55
_cairo_gstate_ensure_scaled_font (cairo_gstate_t *gstate);
56

            
57
static void
58
_cairo_gstate_unset_scaled_font (cairo_gstate_t *gstate);
59

            
60
static void
61
_cairo_gstate_transform_glyphs_to_backend (cairo_gstate_t      *gstate,
62
                                           const cairo_glyph_t *glyphs,
63
                                           int                  num_glyphs,
64
					   const cairo_text_cluster_t	*clusters,
65
					   int			 num_clusters,
66
					   cairo_text_cluster_flags_t cluster_flags,
67
                                           cairo_glyph_t       *transformed_glyphs,
68
					   int			*num_transformed_glyphs,
69
					   cairo_text_cluster_t *transformed_clusters,
70
					   cairo_bool_t          perform_early_clip);
71

            
72
static void
73
12
_cairo_gstate_update_device_transform (cairo_observer_t *observer,
74
				       void *arg)
75
{
76
12
    cairo_gstate_t *gstate = cairo_container_of (observer,
77
						 cairo_gstate_t,
78
						 device_transform_observer);
79

            
80
24
    gstate->is_identity = (_cairo_matrix_is_identity (&gstate->ctm) &&
81
12
			   _cairo_matrix_is_identity (&gstate->target->device_transform));
82
12
}
83

            
84
cairo_status_t
85
34933
_cairo_gstate_init (cairo_gstate_t  *gstate,
86
		    cairo_surface_t *target)
87
{
88
    VG (VALGRIND_MAKE_MEM_UNDEFINED (gstate, sizeof (cairo_gstate_t)));
89

            
90
34933
    gstate->next = NULL;
91

            
92
34933
    gstate->op = CAIRO_GSTATE_OPERATOR_DEFAULT;
93
34933
    gstate->opacity = 1.;
94

            
95
34933
    gstate->tolerance = CAIRO_GSTATE_TOLERANCE_DEFAULT;
96
34933
    gstate->antialias = CAIRO_ANTIALIAS_DEFAULT;
97

            
98
34933
    _cairo_stroke_style_init (&gstate->stroke_style);
99

            
100
34933
    gstate->fill_rule = CAIRO_GSTATE_FILL_RULE_DEFAULT;
101

            
102
34933
    gstate->font_face = NULL;
103
34933
    gstate->scaled_font = NULL;
104
34933
    gstate->previous_scaled_font = NULL;
105

            
106
34933
    cairo_matrix_init_scale (&gstate->font_matrix,
107
			     CAIRO_GSTATE_DEFAULT_FONT_SIZE,
108
			     CAIRO_GSTATE_DEFAULT_FONT_SIZE);
109

            
110
34933
    _cairo_font_options_init_default (&gstate->font_options);
111

            
112
34933
    gstate->clip = NULL;
113

            
114
34933
    gstate->target = cairo_surface_reference (target);
115
34933
    gstate->parent_target = NULL;
116
34933
    gstate->original_target = cairo_surface_reference (target);
117

            
118
34933
    gstate->device_transform_observer.callback = _cairo_gstate_update_device_transform;
119
34933
    cairo_list_add (&gstate->device_transform_observer.link,
120
34933
		    &gstate->target->device_transform_observers);
121

            
122
34933
    gstate->is_identity = _cairo_matrix_is_identity (&gstate->target->device_transform);
123
34933
    cairo_matrix_init_identity (&gstate->ctm);
124
34933
    gstate->ctm_inverse = gstate->ctm;
125
34933
    gstate->source_ctm_inverse = gstate->ctm;
126

            
127
34933
    gstate->source = (cairo_pattern_t *) &_cairo_pattern_black.base;
128

            
129
    /* Now that the gstate is fully initialized and ready for the eventual
130
     * _cairo_gstate_fini(), we can check for errors (and not worry about
131
     * the resource deallocation). */
132
34933
    return target->status;
133
}
134

            
135
/**
136
 * _cairo_gstate_init_copy:
137
 *
138
 * Initialize @gstate by performing a deep copy of state fields from
139
 * @other. Note that gstate->next is not copied but is set to %NULL by
140
 * this function.
141
 **/
142
static cairo_status_t
143
65888
_cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other)
144
{
145
    cairo_status_t status;
146

            
147
    VG (VALGRIND_MAKE_MEM_UNDEFINED (gstate, sizeof (cairo_gstate_t)));
148

            
149
65888
    gstate->op = other->op;
150
65888
    gstate->opacity = other->opacity;
151

            
152
65888
    gstate->tolerance = other->tolerance;
153
65888
    gstate->antialias = other->antialias;
154

            
155
65888
    status = _cairo_stroke_style_init_copy (&gstate->stroke_style,
156
65888
					    &other->stroke_style);
157
65888
    if (unlikely (status))
158
	return status;
159

            
160
65888
    gstate->fill_rule = other->fill_rule;
161

            
162
65888
    gstate->font_face = cairo_font_face_reference (other->font_face);
163
65888
    gstate->scaled_font = cairo_scaled_font_reference (other->scaled_font);
164
65888
    gstate->previous_scaled_font = cairo_scaled_font_reference (other->previous_scaled_font);
165

            
166
65888
    gstate->font_matrix = other->font_matrix;
167

            
168
65888
    _cairo_font_options_init_copy (&gstate->font_options , &other->font_options);
169

            
170
65888
    gstate->clip = _cairo_clip_copy (other->clip);
171

            
172
65888
    gstate->target = cairo_surface_reference (other->target);
173
    /* parent_target is always set to NULL; it's only ever set by redirect_target */
174
65888
    gstate->parent_target = NULL;
175
65888
    gstate->original_target = cairo_surface_reference (other->original_target);
176

            
177
65888
    gstate->device_transform_observer.callback = _cairo_gstate_update_device_transform;
178
65888
    cairo_list_add (&gstate->device_transform_observer.link,
179
65888
		    &gstate->target->device_transform_observers);
180

            
181
65888
    gstate->is_identity = other->is_identity;
182
65888
    gstate->ctm = other->ctm;
183
65888
    gstate->ctm_inverse = other->ctm_inverse;
184
65888
    gstate->source_ctm_inverse = other->source_ctm_inverse;
185

            
186
65888
    gstate->source = cairo_pattern_reference (other->source);
187

            
188
65888
    gstate->next = NULL;
189

            
190
65888
    return CAIRO_STATUS_SUCCESS;
191
}
192

            
193
void
194
100815
_cairo_gstate_fini (cairo_gstate_t *gstate)
195
{
196
100815
    _cairo_stroke_style_fini (&gstate->stroke_style);
197
100815
    _cairo_font_options_fini (&gstate->font_options);
198

            
199
100815
    cairo_font_face_destroy (gstate->font_face);
200
100815
    gstate->font_face = NULL;
201

            
202
100815
    cairo_scaled_font_destroy (gstate->previous_scaled_font);
203
100815
    gstate->previous_scaled_font = NULL;
204

            
205
100815
    cairo_scaled_font_destroy (gstate->scaled_font);
206
100815
    gstate->scaled_font = NULL;
207

            
208
100815
    _cairo_clip_destroy (gstate->clip);
209

            
210
100815
    cairo_list_del (&gstate->device_transform_observer.link);
211

            
212
100815
    cairo_surface_destroy (gstate->target);
213
100815
    gstate->target = NULL;
214

            
215
100815
    cairo_surface_destroy (gstate->parent_target);
216
100815
    gstate->parent_target = NULL;
217

            
218
100815
    cairo_surface_destroy (gstate->original_target);
219
100815
    gstate->original_target = NULL;
220

            
221
100815
    cairo_pattern_destroy (gstate->source);
222
100815
    gstate->source = NULL;
223

            
224
    VG (VALGRIND_MAKE_MEM_UNDEFINED (gstate, sizeof (cairo_gstate_t)));
225
100815
}
226

            
227
/**
228
 * _cairo_gstate_save:
229
 * @gstate: input/output gstate pointer
230
 *
231
 * Makes a copy of the current state of @gstate and saves it
232
 * to @gstate->next, then put the address of the newly allcated
233
 * copy into @gstate.  _cairo_gstate_restore() reverses this.
234
 **/
235
cairo_status_t
236
65888
_cairo_gstate_save (cairo_gstate_t **gstate, cairo_gstate_t **freelist)
237
{
238
    cairo_gstate_t *top;
239
    cairo_status_t status;
240

            
241
    if (CAIRO_INJECT_FAULT ())
242
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
243

            
244
65888
    top = *freelist;
245
65888
    if (top == NULL) {
246
1551
	top = _cairo_calloc (sizeof (cairo_gstate_t));
247
1551
	if (unlikely (top == NULL))
248
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
249
    } else
250
64337
	*freelist = top->next;
251

            
252
65888
    status = _cairo_gstate_init_copy (top, *gstate);
253
65888
    if (unlikely (status)) {
254
	top->next = *freelist;
255
	*freelist = top;
256
	return status;
257
    }
258

            
259
65888
    top->next = *gstate;
260
65888
    *gstate = top;
261

            
262
65888
    return CAIRO_STATUS_SUCCESS;
263
}
264

            
265
/**
266
 * _cairo_gstate_restore:
267
 * @gstate: input/output gstate pointer
268
 *
269
 * Reverses the effects of one _cairo_gstate_save() call.
270
 **/
271
cairo_status_t
272
65891
_cairo_gstate_restore (cairo_gstate_t **gstate, cairo_gstate_t **freelist)
273
{
274
    cairo_gstate_t *top;
275

            
276
65891
    top = *gstate;
277
65891
    if (top->next == NULL)
278
3
	return _cairo_error (CAIRO_STATUS_INVALID_RESTORE);
279

            
280
65888
    *gstate = top->next;
281

            
282
65888
    _cairo_gstate_fini (top);
283
    VG (VALGRIND_MAKE_MEM_UNDEFINED (&top->next, sizeof (cairo_gstate_t *)));
284
65888
    top->next = *freelist;
285
65888
    *freelist = top;
286

            
287
65888
    return CAIRO_STATUS_SUCCESS;
288
}
289

            
290
/**
291
 * _cairo_gstate_redirect_target:
292
 * @gstate: a #cairo_gstate_t
293
 * @child: the new child target
294
 *
295
 * Redirect @gstate rendering to a "child" target. The original
296
 * "parent" target with which the gstate was created will not be
297
 * affected. See _cairo_gstate_get_target().
298
 **/
299
cairo_status_t
300
647
_cairo_gstate_redirect_target (cairo_gstate_t *gstate, cairo_surface_t *child)
301
{
302
    /* If this gstate is already redirected, this is an error; we need a
303
     * new gstate to be able to redirect */
304
647
    assert (gstate->parent_target == NULL);
305

            
306
    /* Set up our new parent_target based on our current target;
307
     * gstate->parent_target will take the ref that is held by gstate->target
308
     */
309
647
    gstate->parent_target = gstate->target;
310

            
311
    /* Now set up our new target; we overwrite gstate->target directly,
312
     * since its ref is now owned by gstate->parent_target */
313
647
    gstate->target = cairo_surface_reference (child);
314
647
    gstate->is_identity &= _cairo_matrix_is_identity (&child->device_transform);
315
647
    cairo_list_move (&gstate->device_transform_observer.link,
316
647
		     &gstate->target->device_transform_observers);
317

            
318
    /* The clip is in surface backend coordinates for the previous target;
319
     * translate it into the child's backend coordinates. */
320
647
    _cairo_clip_destroy (gstate->clip);
321
1294
    gstate->clip = _cairo_clip_copy_with_translation (gstate->next->clip,
322
647
						      child->device_transform.x0 - gstate->parent_target->device_transform.x0,
323
647
						      child->device_transform.y0 - gstate->parent_target->device_transform.y0);
324

            
325
647
    return CAIRO_STATUS_SUCCESS;
326
}
327

            
328
/**
329
 * _cairo_gstate_is_group:
330
 * @gstate: a #cairo_gstate_t
331
 *
332
 * Check if _cairo_gstate_redirect_target has been called on the head
333
 * of the stack.
334
 *
335
 * Return value: %TRUE if @gstate is redirected to a target different
336
 * than the previous state in the stack, %FALSE otherwise.
337
 **/
338
cairo_bool_t
339
65885
_cairo_gstate_is_group (cairo_gstate_t *gstate)
340
{
341
65885
    return gstate->parent_target != NULL;
342
}
343

            
344
/**
345
 * _cairo_gstate_get_target:
346
 * @gstate: a #cairo_gstate_t
347
 *
348
 * Return the current drawing target; if drawing is not redirected,
349
 * this will be the same as _cairo_gstate_get_original_target().
350
 *
351
 * Return value: the current target surface
352
 **/
353
cairo_surface_t *
354
2474
_cairo_gstate_get_target (cairo_gstate_t *gstate)
355
{
356
2474
    return gstate->target;
357
}
358

            
359
/**
360
 * _cairo_gstate_get_original_target:
361
 * @gstate: a #cairo_gstate_t
362
 *
363
 * Return the original target with which @gstate was created. This
364
 * function always returns the original target independent of any
365
 * child target that may have been set with
366
 * _cairo_gstate_redirect_target.
367
 *
368
 * Return value: the original target surface
369
 **/
370
cairo_surface_t *
371
47993
_cairo_gstate_get_original_target (cairo_gstate_t *gstate)
372
{
373
47993
    return gstate->original_target;
374
}
375

            
376
/**
377
 * _cairo_gstate_get_clip:
378
 * @gstate: a #cairo_gstate_t
379
 *
380
 * This space left intentionally blank.
381
 *
382
 * Return value: a pointer to the gstate's #cairo_clip_t structure.
383
 **/
384
cairo_clip_t *
385
659
_cairo_gstate_get_clip (cairo_gstate_t *gstate)
386
{
387
659
    return gstate->clip;
388
}
389

            
390
cairo_status_t
391
249550
_cairo_gstate_set_source (cairo_gstate_t  *gstate,
392
			  cairo_pattern_t *source)
393
{
394
249550
    if (source->status)
395
	return source->status;
396

            
397
249550
    source = cairo_pattern_reference (source);
398
249550
    cairo_pattern_destroy (gstate->source);
399
249550
    gstate->source = source;
400
249550
    gstate->source_ctm_inverse = gstate->ctm_inverse;
401

            
402
249550
    return CAIRO_STATUS_SUCCESS;
403
}
404

            
405
cairo_pattern_t *
406
33861
_cairo_gstate_get_source (cairo_gstate_t *gstate)
407
{
408
33861
    if (gstate->source == &_cairo_pattern_black.base) {
409
	/* do not expose the static object to the user */
410
1506
        gstate->source = _cairo_pattern_create_solid (CAIRO_COLOR_BLACK);
411
    }
412

            
413
33861
    return gstate->source;
414
}
415

            
416
cairo_status_t
417
5520
_cairo_gstate_set_operator (cairo_gstate_t *gstate, cairo_operator_t op)
418
{
419
5520
    gstate->op = op;
420

            
421
5520
    return CAIRO_STATUS_SUCCESS;
422
}
423

            
424
cairo_operator_t
425
12
_cairo_gstate_get_operator (cairo_gstate_t *gstate)
426
{
427
12
    return gstate->op;
428
}
429

            
430
cairo_status_t
431
_cairo_gstate_set_opacity (cairo_gstate_t *gstate, double op)
432
{
433
    gstate->opacity = op;
434

            
435
    return CAIRO_STATUS_SUCCESS;
436
}
437

            
438
double
439
_cairo_gstate_get_opacity (cairo_gstate_t *gstate)
440
{
441
    return gstate->opacity;
442
}
443

            
444
cairo_status_t
445
501
_cairo_gstate_set_tolerance (cairo_gstate_t *gstate, double tolerance)
446
{
447
501
    gstate->tolerance = tolerance;
448

            
449
501
    return CAIRO_STATUS_SUCCESS;
450
}
451

            
452
double
453
495400
_cairo_gstate_get_tolerance (cairo_gstate_t *gstate)
454
{
455
495400
    return gstate->tolerance;
456
}
457

            
458
cairo_status_t
459
902
_cairo_gstate_set_fill_rule (cairo_gstate_t *gstate, cairo_fill_rule_t fill_rule)
460
{
461
902
    gstate->fill_rule = fill_rule;
462

            
463
902
    return CAIRO_STATUS_SUCCESS;
464
}
465

            
466
cairo_fill_rule_t
467
21
_cairo_gstate_get_fill_rule (cairo_gstate_t *gstate)
468
{
469
21
    return gstate->fill_rule;
470
}
471

            
472
cairo_status_t
473
3705
_cairo_gstate_set_line_width (cairo_gstate_t *gstate, double width)
474
{
475
3705
    if (gstate->stroke_style.is_hairline)
476
15
	gstate->stroke_style.pre_hairline_line_width = width;
477
	else
478
3690
	gstate->stroke_style.line_width = width;
479

            
480
3705
    return CAIRO_STATUS_SUCCESS;
481
}
482

            
483
double
484
29259705
_cairo_gstate_get_line_width (cairo_gstate_t *gstate)
485
{
486
29259705
    return gstate->stroke_style.line_width;
487
}
488

            
489
cairo_status_t
490
30
_cairo_gstate_set_hairline (cairo_gstate_t *gstate, cairo_bool_t set_hairline)
491
{
492
30
    if (gstate->stroke_style.is_hairline != set_hairline) {
493
30
        gstate->stroke_style.is_hairline = set_hairline;
494

            
495
30
        if (set_hairline) {
496
15
            gstate->stroke_style.pre_hairline_line_width = gstate->stroke_style.line_width;
497
15
            gstate->stroke_style.line_width = 0.0;
498
        } else {
499
15
            gstate->stroke_style.line_width = gstate->stroke_style.pre_hairline_line_width;
500
        }
501
    }
502

            
503
30
    return CAIRO_STATUS_SUCCESS;
504
}
505

            
506
cairo_bool_t
507
45
_cairo_gstate_get_hairline (cairo_gstate_t *gstate)
508
{
509
45
    return gstate->stroke_style.is_hairline;
510
}
511

            
512
cairo_status_t
513
2598
_cairo_gstate_set_line_cap (cairo_gstate_t *gstate, cairo_line_cap_t line_cap)
514
{
515
2598
    gstate->stroke_style.line_cap = line_cap;
516

            
517
2598
    return CAIRO_STATUS_SUCCESS;
518
}
519

            
520
cairo_line_cap_t
521
12
_cairo_gstate_get_line_cap (cairo_gstate_t *gstate)
522
{
523
12
    return gstate->stroke_style.line_cap;
524
}
525

            
526
cairo_status_t
527
1527
_cairo_gstate_set_line_join (cairo_gstate_t *gstate, cairo_line_join_t line_join)
528
{
529
1527
    gstate->stroke_style.line_join = line_join;
530

            
531
1527
    return CAIRO_STATUS_SUCCESS;
532
}
533

            
534
cairo_line_join_t
535
12
_cairo_gstate_get_line_join (cairo_gstate_t *gstate)
536
{
537
12
    return gstate->stroke_style.line_join;
538
}
539

            
540
cairo_status_t
541
3000
_cairo_gstate_set_dash (cairo_gstate_t *gstate, const double *dash, int num_dashes, double offset)
542
{
543
    double dash_total, on_total, off_total;
544
    int i, j;
545

            
546
3000
    free (gstate->stroke_style.dash);
547

            
548
3000
    gstate->stroke_style.num_dashes = num_dashes;
549

            
550
3000
    if (gstate->stroke_style.num_dashes == 0) {
551
1767
	gstate->stroke_style.dash = NULL;
552
1767
	gstate->stroke_style.dash_offset = 0.0;
553
1767
	return CAIRO_STATUS_SUCCESS;
554
    }
555

            
556
1233
    gstate->stroke_style.dash = _cairo_malloc_ab (gstate->stroke_style.num_dashes, sizeof (double));
557
1233
    if (unlikely (gstate->stroke_style.dash == NULL)) {
558
	gstate->stroke_style.num_dashes = 0;
559
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
560
    }
561

            
562
1233
    on_total = off_total = dash_total = 0.0;
563
4131
    for (i = j = 0; i < num_dashes; i++) {
564
2898
	if (dash[i] < 0)
565
	    return _cairo_error (CAIRO_STATUS_INVALID_DASH);
566

            
567
2898
	if (dash[i] == 0 && i > 0 && i < num_dashes - 1) {
568
42
	    if (dash[++i] < 0)
569
		return _cairo_error (CAIRO_STATUS_INVALID_DASH);
570

            
571
42
	    gstate->stroke_style.dash[j-1] += dash[i];
572
42
	    gstate->stroke_style.num_dashes -= 2;
573
	} else
574
2856
	    gstate->stroke_style.dash[j++] = dash[i];
575

            
576
2898
	if (dash[i]) {
577
2832
	    dash_total += dash[i];
578
2832
	    if ((i & 1) == 0)
579
1524
		on_total += dash[i];
580
	    else
581
1308
		off_total += dash[i];
582
	}
583
    }
584

            
585
1233
    if (dash_total == 0.0)
586
	return _cairo_error (CAIRO_STATUS_INVALID_DASH);
587

            
588
    /* An odd dash value indicate symmetric repeating, so the total
589
     * is twice as long. */
590
1233
    if (gstate->stroke_style.num_dashes & 1) {
591
210
	dash_total *= 2;
592
210
	on_total += off_total;
593
    }
594

            
595
1233
    if (dash_total - on_total < CAIRO_FIXED_ERROR_DOUBLE) {
596
	/* Degenerate dash -> solid line */
597
27
	free (gstate->stroke_style.dash);
598
27
	gstate->stroke_style.dash = NULL;
599
27
	gstate->stroke_style.num_dashes = 0;
600
27
	gstate->stroke_style.dash_offset = 0.0;
601
27
	return CAIRO_STATUS_SUCCESS;
602
    }
603

            
604
    /* The dashing code doesn't like a negative offset or a big positive
605
     * offset, so we compute an equivalent offset which is guaranteed to be
606
     * positive and less than twice the pattern length. */
607
1206
    offset = fmod (offset, dash_total);
608
1206
    if (offset < 0.0)
609
69
	offset += dash_total;
610
1206
    if (offset <= 0.0)		/* Take care of -0 */
611
462
	offset = 0.0;
612
1206
    gstate->stroke_style.dash_offset = offset;
613

            
614
1206
    return CAIRO_STATUS_SUCCESS;
615
}
616

            
617
void
618
24
_cairo_gstate_get_dash (cairo_gstate_t *gstate,
619
			double         *dashes,
620
			int            *num_dashes,
621
			double         *offset)
622
{
623
24
    if (dashes) {
624
12
	memcpy (dashes,
625
12
		gstate->stroke_style.dash,
626
12
		sizeof (double) * gstate->stroke_style.num_dashes);
627
    }
628

            
629
24
    if (num_dashes)
630
12
	*num_dashes = gstate->stroke_style.num_dashes;
631

            
632
24
    if (offset)
633
12
	*offset = gstate->stroke_style.dash_offset;
634
24
}
635

            
636
cairo_status_t
637
297
_cairo_gstate_set_miter_limit (cairo_gstate_t *gstate, double limit)
638
{
639
297
    gstate->stroke_style.miter_limit = limit;
640

            
641
297
    return CAIRO_STATUS_SUCCESS;
642
}
643

            
644
double
645
12
_cairo_gstate_get_miter_limit (cairo_gstate_t *gstate)
646
{
647
12
    return gstate->stroke_style.miter_limit;
648
}
649

            
650
void
651
495448
_cairo_gstate_get_matrix (cairo_gstate_t *gstate, cairo_matrix_t *matrix)
652
{
653
495448
    *matrix = gstate->ctm;
654
495448
}
655

            
656
cairo_status_t
657
59541
_cairo_gstate_translate (cairo_gstate_t *gstate, double tx, double ty)
658
{
659
    cairo_matrix_t tmp;
660

            
661
59541
    if (! ISFINITE (tx) || ! ISFINITE (ty))
662
18
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
663

            
664
59523
    _cairo_gstate_unset_scaled_font (gstate);
665

            
666
59523
    cairo_matrix_init_translate (&tmp, tx, ty);
667
59523
    cairo_matrix_multiply (&gstate->ctm, &tmp, &gstate->ctm);
668
59523
    gstate->is_identity = FALSE;
669

            
670
    /* paranoid check against gradual numerical instability */
671
59523
    if (! _cairo_matrix_is_invertible (&gstate->ctm))
672
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
673

            
674
59523
    cairo_matrix_init_translate (&tmp, -tx, -ty);
675
59523
    cairo_matrix_multiply (&gstate->ctm_inverse, &gstate->ctm_inverse, &tmp);
676

            
677
59523
    return CAIRO_STATUS_SUCCESS;
678
}
679

            
680
cairo_status_t
681
3092
_cairo_gstate_scale (cairo_gstate_t *gstate, double sx, double sy)
682
{
683
    cairo_matrix_t tmp;
684

            
685
3092
    if (sx * sy == 0.) /* either sx or sy is 0, or det == 0 due to underflow */
686
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
687
3092
    if (! ISFINITE (sx) || ! ISFINITE (sy))
688
27
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
689

            
690
3065
    _cairo_gstate_unset_scaled_font (gstate);
691

            
692
3065
    cairo_matrix_init_scale (&tmp, sx, sy);
693
3065
    cairo_matrix_multiply (&gstate->ctm, &tmp, &gstate->ctm);
694
3065
    gstate->is_identity = FALSE;
695

            
696
    /* paranoid check against gradual numerical instability */
697
3065
    if (! _cairo_matrix_is_invertible (&gstate->ctm))
698
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
699

            
700
3065
    cairo_matrix_init_scale (&tmp, 1/sx, 1/sy);
701
3065
    cairo_matrix_multiply (&gstate->ctm_inverse, &gstate->ctm_inverse, &tmp);
702

            
703
3065
    return CAIRO_STATUS_SUCCESS;
704
}
705

            
706
cairo_status_t
707
901
_cairo_gstate_rotate (cairo_gstate_t *gstate, double angle)
708
{
709
    cairo_matrix_t tmp;
710

            
711
901
    if (angle == 0.)
712
48
	return CAIRO_STATUS_SUCCESS;
713

            
714
853
    if (! ISFINITE (angle))
715
6
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
716

            
717
847
    _cairo_gstate_unset_scaled_font (gstate);
718

            
719
847
    cairo_matrix_init_rotate (&tmp, angle);
720
847
    cairo_matrix_multiply (&gstate->ctm, &tmp, &gstate->ctm);
721
847
    gstate->is_identity = FALSE;
722

            
723
    /* paranoid check against gradual numerical instability */
724
847
    if (! _cairo_matrix_is_invertible (&gstate->ctm))
725
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
726

            
727
847
    cairo_matrix_init_rotate (&tmp, -angle);
728
847
    cairo_matrix_multiply (&gstate->ctm_inverse, &gstate->ctm_inverse, &tmp);
729

            
730
847
    return CAIRO_STATUS_SUCCESS;
731
}
732

            
733
cairo_status_t
734
276
_cairo_gstate_transform (cairo_gstate_t	      *gstate,
735
			 const cairo_matrix_t *matrix)
736
{
737
    cairo_matrix_t tmp;
738
    cairo_status_t status;
739

            
740
276
    if (! _cairo_matrix_is_invertible (matrix))
741
9
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
742

            
743
267
    if (_cairo_matrix_is_identity (matrix))
744
30
	return CAIRO_STATUS_SUCCESS;
745

            
746
237
    tmp = *matrix;
747
237
    status = cairo_matrix_invert (&tmp);
748
237
    if (unlikely (status))
749
	return status;
750

            
751
237
    _cairo_gstate_unset_scaled_font (gstate);
752

            
753
237
    cairo_matrix_multiply (&gstate->ctm, matrix, &gstate->ctm);
754
237
    cairo_matrix_multiply (&gstate->ctm_inverse, &gstate->ctm_inverse, &tmp);
755
237
    gstate->is_identity = FALSE;
756

            
757
    /* paranoid check against gradual numerical instability */
758
237
    if (! _cairo_matrix_is_invertible (&gstate->ctm))
759
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
760

            
761
237
    return CAIRO_STATUS_SUCCESS;
762
}
763

            
764
cairo_status_t
765
1338
_cairo_gstate_set_matrix (cairo_gstate_t       *gstate,
766
			  const cairo_matrix_t *matrix)
767
{
768
    cairo_status_t status;
769

            
770
1338
    if (memcmp (matrix, &gstate->ctm, sizeof (cairo_matrix_t)) == 0)
771
27
	return CAIRO_STATUS_SUCCESS;
772

            
773
1311
    if (! _cairo_matrix_is_invertible (matrix))
774
9
	return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
775

            
776
1302
    if (_cairo_matrix_is_identity (matrix)) {
777
24
	_cairo_gstate_identity_matrix (gstate);
778
24
	return CAIRO_STATUS_SUCCESS;
779
    }
780

            
781
1278
    _cairo_gstate_unset_scaled_font (gstate);
782

            
783
1278
    gstate->ctm = *matrix;
784
1278
    gstate->ctm_inverse = *matrix;
785
1278
    status = cairo_matrix_invert (&gstate->ctm_inverse);
786
1278
    assert (status == CAIRO_STATUS_SUCCESS);
787
1278
    gstate->is_identity = FALSE;
788

            
789
1278
    return CAIRO_STATUS_SUCCESS;
790
}
791

            
792
void
793
84
_cairo_gstate_identity_matrix (cairo_gstate_t *gstate)
794
{
795
84
    if (_cairo_matrix_is_identity (&gstate->ctm))
796
6
	return;
797

            
798
78
    _cairo_gstate_unset_scaled_font (gstate);
799

            
800
78
    cairo_matrix_init_identity (&gstate->ctm);
801
78
    cairo_matrix_init_identity (&gstate->ctm_inverse);
802
78
    gstate->is_identity = _cairo_matrix_is_identity (&gstate->target->device_transform);
803
}
804

            
805
void
806
6
_cairo_gstate_user_to_device (cairo_gstate_t *gstate, double *x, double *y)
807
{
808
6
    cairo_matrix_transform_point (&gstate->ctm, x, y);
809
6
}
810

            
811
void
812
6
_cairo_gstate_user_to_device_distance (cairo_gstate_t *gstate,
813
				       double *dx, double *dy)
814
{
815
6
    cairo_matrix_transform_distance (&gstate->ctm, dx, dy);
816
6
}
817

            
818
void
819
6
_cairo_gstate_device_to_user (cairo_gstate_t *gstate, double *x, double *y)
820
{
821
6
    cairo_matrix_transform_point (&gstate->ctm_inverse, x, y);
822
6
}
823

            
824
void
825
6
_cairo_gstate_device_to_user_distance (cairo_gstate_t *gstate,
826
				       double *dx, double *dy)
827
{
828
6
    cairo_matrix_transform_distance (&gstate->ctm_inverse, dx, dy);
829
6
}
830

            
831
void
832
463893
_do_cairo_gstate_user_to_backend (cairo_gstate_t *gstate, double *x, double *y)
833
{
834
463893
    cairo_matrix_transform_point (&gstate->ctm, x, y);
835
463893
    cairo_matrix_transform_point (&gstate->target->device_transform, x, y);
836
463893
}
837

            
838
void
839
597297
_do_cairo_gstate_user_to_backend_distance (cairo_gstate_t *gstate, double *x, double *y)
840
{
841
597297
    cairo_matrix_transform_distance (&gstate->ctm, x, y);
842
597297
    cairo_matrix_transform_distance (&gstate->target->device_transform, x, y);
843
597297
}
844

            
845
void
846
5007
_do_cairo_gstate_backend_to_user (cairo_gstate_t *gstate, double *x, double *y)
847
{
848
5007
    cairo_matrix_transform_point (&gstate->target->device_transform_inverse, x, y);
849
5007
    cairo_matrix_transform_point (&gstate->ctm_inverse, x, y);
850
5007
}
851

            
852
void
853
_do_cairo_gstate_backend_to_user_distance (cairo_gstate_t *gstate, double *x, double *y)
854
{
855
    cairo_matrix_transform_distance (&gstate->target->device_transform_inverse, x, y);
856
    cairo_matrix_transform_distance (&gstate->ctm_inverse, x, y);
857
}
858

            
859
void
860
261
_cairo_gstate_backend_to_user_rectangle (cairo_gstate_t *gstate,
861
                                         double *x1, double *y1,
862
                                         double *x2, double *y2,
863
                                         cairo_bool_t *is_tight)
864
{
865
    cairo_matrix_t matrix_inverse;
866

            
867
522
    if (! _cairo_matrix_is_identity (&gstate->target->device_transform_inverse) ||
868
261
    ! _cairo_matrix_is_identity (&gstate->ctm_inverse))
869
    {
870
80
	cairo_matrix_multiply (&matrix_inverse,
871
80
			       &gstate->target->device_transform_inverse,
872
80
			       &gstate->ctm_inverse);
873
80
	_cairo_matrix_transform_bounding_box (&matrix_inverse,
874
					      x1, y1, x2, y2, is_tight);
875
    }
876

            
877
    else
878
    {
879
181
	if (is_tight)
880
6
	    *is_tight = TRUE;
881
    }
882
261
}
883

            
884
/* XXX: NYI
885
cairo_status_t
886
_cairo_gstate_stroke_to_path (cairo_gstate_t *gstate)
887
{
888
    cairo_status_t status;
889

            
890
    _cairo_pen_init (&gstate);
891
    return CAIRO_STATUS_SUCCESS;
892
}
893
*/
894

            
895
void
896
108
_cairo_gstate_path_extents (cairo_gstate_t     *gstate,
897
			    cairo_path_fixed_t *path,
898
			    double *x1, double *y1,
899
			    double *x2, double *y2)
900
{
901
    cairo_box_t box;
902
    double px1, py1, px2, py2;
903

            
904
108
    if (_cairo_path_fixed_extents (path, &box)) {
905
102
	px1 = _cairo_fixed_to_double (box.p1.x);
906
102
	py1 = _cairo_fixed_to_double (box.p1.y);
907
102
	px2 = _cairo_fixed_to_double (box.p2.x);
908
102
	py2 = _cairo_fixed_to_double (box.p2.y);
909

            
910
102
	_cairo_gstate_backend_to_user_rectangle (gstate,
911
						 &px1, &py1, &px2, &py2,
912
						 NULL);
913
    } else {
914
6
	px1 = 0.0;
915
6
	py1 = 0.0;
916
6
	px2 = 0.0;
917
6
	py2 = 0.0;
918
    }
919

            
920
108
    if (x1)
921
108
	*x1 = px1;
922
108
    if (y1)
923
108
	*y1 = py1;
924
108
    if (x2)
925
108
	*x2 = px2;
926
108
    if (y2)
927
108
	*y2 = py2;
928
108
}
929

            
930
static void
931
840598
_cairo_gstate_copy_pattern (cairo_pattern_t *pattern,
932
			    const cairo_pattern_t *original)
933
{
934
    /* First check if the we can replace the original with a much simpler
935
     * pattern. For example, gradients that are uniform or just have a single
936
     * stop can sometimes be replaced with a solid.
937
     */
938

            
939
840598
    if (_cairo_pattern_is_clear (original)) {
940
931
        _cairo_pattern_init_solid ((cairo_solid_pattern_t *) pattern,
941
				   CAIRO_COLOR_TRANSPARENT);
942
931
	return;
943
    }
944

            
945
839667
    if (original->type == CAIRO_PATTERN_TYPE_LINEAR ||
946
838731
	original->type == CAIRO_PATTERN_TYPE_RADIAL)
947
    {
948
        cairo_color_t color;
949
1449
	if (_cairo_gradient_pattern_is_solid ((cairo_gradient_pattern_t *) original,
950
					      NULL,
951
					      &color))
952
	{
953
54
	    _cairo_pattern_init_solid ((cairo_solid_pattern_t *) pattern,
954
				       &color);
955
54
	    return;
956
	}
957
    }
958

            
959
839613
    _cairo_pattern_init_static_copy (pattern, original);
960
}
961

            
962
static void
963
840598
_cairo_gstate_copy_transformed_pattern (cairo_gstate_t  *gstate,
964
					cairo_pattern_t *pattern,
965
					const cairo_pattern_t *original,
966
					const cairo_matrix_t  *ctm_inverse)
967
{
968
    /*
969
     * What calculations below do can be described in pseudo-code (so using nonexistent fields) as (using column vectors):
970
     * pattern->matrix = surface->device_transform *
971
     * 			 pattern->matrix *
972
     * 			 ctm_inverse *
973
     * 			 gstate->target->device_transform_inverse
974
     *
975
     * The inverse of which is:
976
     * pattern->matrix_inverse = gstate->target->device_transform *
977
     * 				 ctm *
978
     * 				 pattern->matrix_inverse *
979
     * 				 surface->device_transform_inverse
980
     */
981

            
982
840598
    _cairo_gstate_copy_pattern (pattern, original);
983

            
984
840598
    if (original->type == CAIRO_PATTERN_TYPE_SURFACE) {
985
	cairo_surface_pattern_t *surface_pattern;
986
	cairo_surface_t *surface;
987

            
988
318667
        surface_pattern = (cairo_surface_pattern_t *) original;
989
318667
        surface = surface_pattern->surface;
990

            
991
318667
	if (_cairo_surface_has_device_transform (surface))
992
339
	    _cairo_pattern_pretransform (pattern, &surface->device_transform);
993
    }
994

            
995
840598
    if (! _cairo_matrix_is_identity (ctm_inverse))
996
240521
	_cairo_pattern_transform (pattern, ctm_inverse);
997

            
998
840598
    if (_cairo_surface_has_device_transform (gstate->target)) {
999
828
        _cairo_pattern_transform (pattern,
828
                                  &gstate->target->device_transform_inverse);
    }
840598
}
static void
838759
_cairo_gstate_copy_transformed_source (cairo_gstate_t   *gstate,
				       cairo_pattern_t  *pattern)
{
838759
    _cairo_gstate_copy_transformed_pattern (gstate, pattern,
838759
					    gstate->source,
838759
					    &gstate->source_ctm_inverse);
838759
}
static void
1839
_cairo_gstate_copy_transformed_mask (cairo_gstate_t   *gstate,
				     cairo_pattern_t  *pattern,
				     cairo_pattern_t  *mask)
{
1839
    _cairo_gstate_copy_transformed_pattern (gstate, pattern,
					    mask,
1839
					    &gstate->ctm_inverse);
1839
}
static cairo_operator_t
801189
_reduce_op (cairo_gstate_t *gstate)
{
    cairo_operator_t op;
    const cairo_pattern_t *pattern;
801189
    op = gstate->op;
801189
    if (op != CAIRO_OPERATOR_SOURCE)
497187
	return op;
304002
    pattern = gstate->source;
304002
    if (pattern->type == CAIRO_PATTERN_TYPE_SOLID) {
3660
	const cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) pattern;
3660
	if (solid->color.alpha_short <= 0x00ff) {
165
	    op = CAIRO_OPERATOR_CLEAR;
3495
	} else if ((gstate->target->content & CAIRO_CONTENT_ALPHA) == 0) {
162
	    if ((solid->color.red_short |
162
		 solid->color.green_short |
162
		 solid->color.blue_short) <= 0x00ff)
	    {
		op = CAIRO_OPERATOR_CLEAR;
	    }
	}
300342
    } else if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
300207
	const cairo_surface_pattern_t *surface = (cairo_surface_pattern_t *) pattern;
300207
	if (surface->surface->is_clear &&
	    surface->surface->content & CAIRO_CONTENT_ALPHA)
	{
	    op = CAIRO_OPERATOR_CLEAR;
	}
    } else {
135
	const cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t *) pattern;
135
	if (gradient->n_stops == 0)
	    op = CAIRO_OPERATOR_CLEAR;
    }
304002
    return op;
}
static cairo_status_t
911080
_cairo_gstate_get_pattern_status (const cairo_pattern_t *pattern)
{
911080
    if (unlikely (pattern->type == CAIRO_PATTERN_TYPE_MESH &&
		  ((const cairo_mesh_pattern_t *) pattern)->current_patch))
    {
	/* If current patch != NULL, the pattern is under construction
	 * and cannot be used as a source */
	return CAIRO_STATUS_INVALID_MESH_CONSTRUCTION;
    }
911080
    return pattern->status;
}
cairo_status_t
374260
_cairo_gstate_paint (cairo_gstate_t *gstate)
{
    cairo_pattern_union_t source_pattern;
    const cairo_pattern_t *pattern;
    cairo_status_t status;
    cairo_operator_t op;
374260
    status = _cairo_gstate_get_pattern_status (gstate->source);
374260
    if (unlikely (status))
	return status;
374260
    if (gstate->op == CAIRO_OPERATOR_DEST)
9
	return CAIRO_STATUS_SUCCESS;
374251
    if (_cairo_clip_is_all_clipped (gstate->clip))
18
	return CAIRO_STATUS_SUCCESS;
374233
    op = _reduce_op (gstate);
374233
    if (op == CAIRO_OPERATOR_CLEAR) {
1980
	pattern = &_cairo_pattern_clear.base;
    } else {
372253
	_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
372253
	pattern = &source_pattern.base;
    }
374233
    return _cairo_surface_paint (gstate->target,
				 op, pattern,
374233
				 gstate->clip);
}
cairo_status_t
34404
_cairo_gstate_mask (cairo_gstate_t  *gstate,
		    cairo_pattern_t *mask)
{
    cairo_pattern_union_t source_pattern, mask_pattern;
    const cairo_pattern_t *source;
    cairo_operator_t op;
    cairo_status_t status;
34404
    status = _cairo_gstate_get_pattern_status (mask);
34404
    if (unlikely (status))
	return status;
34404
    status = _cairo_gstate_get_pattern_status (gstate->source);
34404
    if (unlikely (status))
	return status;
34404
    if (gstate->op == CAIRO_OPERATOR_DEST)
3
	return CAIRO_STATUS_SUCCESS;
34401
    if (_cairo_clip_is_all_clipped (gstate->clip))
6
	return CAIRO_STATUS_SUCCESS;
34395
    assert (gstate->opacity == 1.0);
34395
    if (_cairo_pattern_is_opaque (mask, NULL))
32415
	return _cairo_gstate_paint (gstate);
1980
    if (_cairo_pattern_is_clear (mask) &&
189
	_cairo_operator_bounded_by_mask (gstate->op))
    {
141
	return CAIRO_STATUS_SUCCESS;
    }
1839
    op = _reduce_op (gstate);
1839
    if (op == CAIRO_OPERATOR_CLEAR) {
12
	source = &_cairo_pattern_clear.base;
    } else {
1827
	_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
1827
	source = &source_pattern.base;
    }
1839
    _cairo_gstate_copy_transformed_mask (gstate, &mask_pattern.base, mask);
1839
    if (source->type == CAIRO_PATTERN_TYPE_SOLID && !source->is_foreground_marker &&
1218
	mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID &&
129
	_cairo_operator_bounded_by_source (op))
63
    {
63
	const cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) source;
	cairo_color_t combined;
63
	if (mask_pattern.base.has_component_alpha) {
#define M(R, A, B, c) R.c = A.c * B.c
	    M(combined, solid->color, mask_pattern.solid.color, red);
	    M(combined, solid->color, mask_pattern.solid.color, green);
	    M(combined, solid->color, mask_pattern.solid.color, blue);
	    M(combined, solid->color, mask_pattern.solid.color, alpha);
#undef M
	} else {
63
	    combined = solid->color;
63
	    _cairo_color_multiply_alpha (&combined, mask_pattern.solid.color.alpha);
	}
63
	_cairo_pattern_init_solid (&source_pattern.solid, &combined);
63
	status = _cairo_surface_paint (gstate->target, op,
				       &source_pattern.base,
63
				       gstate->clip);
    }
    else
    {
1776
	status = _cairo_surface_mask (gstate->target, op,
				      source,
				      &mask_pattern.base,
1776
				      gstate->clip);
    }
1839
    return status;
}
cairo_status_t
39730
_cairo_gstate_stroke (cairo_gstate_t *gstate, cairo_path_fixed_t *path)
{
    cairo_pattern_union_t source_pattern;
    cairo_stroke_style_t style;
    double dash[2];
    cairo_status_t status;
    cairo_matrix_t aggregate_transform;
    cairo_matrix_t aggregate_transform_inverse;
39730
    status = _cairo_gstate_get_pattern_status (gstate->source);
39730
    if (unlikely (status))
	return status;
39730
    if (gstate->op == CAIRO_OPERATOR_DEST)
	return CAIRO_STATUS_SUCCESS;
39730
    if (gstate->stroke_style.line_width <= 0.0 && !gstate->stroke_style.is_hairline)
9
	return CAIRO_STATUS_SUCCESS;
39721
    if (_cairo_clip_is_all_clipped (gstate->clip))
9
	return CAIRO_STATUS_SUCCESS;
39712
    assert (gstate->opacity == 1.0);
39712
    cairo_matrix_multiply (&aggregate_transform,
39712
                           &gstate->ctm,
39712
                           &gstate->target->device_transform);
39712
    cairo_matrix_multiply (&aggregate_transform_inverse,
39712
                           &gstate->target->device_transform_inverse,
39712
                           &gstate->ctm_inverse);
39712
    memcpy (&style, &gstate->stroke_style, sizeof (gstate->stroke_style));
39712
    if (_cairo_stroke_style_dash_can_approximate (&gstate->stroke_style, &aggregate_transform, gstate->tolerance)) {
        style.dash = dash;
        _cairo_stroke_style_dash_approximate (&gstate->stroke_style, &gstate->ctm, gstate->tolerance,
					      &style.dash_offset,
					      style.dash,
					      &style.num_dashes);
    }
39712
    _cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
39712
    return _cairo_surface_stroke (gstate->target,
				  gstate->op,
				  &source_pattern.base,
				  path,
				  &style,
				  &aggregate_transform,
				  &aggregate_transform_inverse,
				  gstate->tolerance,
				  gstate->antialias,
39712
				  gstate->clip);
}
cairo_status_t
15
_cairo_gstate_in_stroke (cairo_gstate_t	    *gstate,
			 cairo_path_fixed_t *path,
			 double		     x,
			 double		     y,
			 cairo_bool_t	    *inside_ret)
{
    cairo_status_t status;
    cairo_rectangle_int_t extents;
    cairo_box_t limit;
    cairo_traps_t traps;
15
    if (gstate->stroke_style.line_width <= 0.0) {
9
	*inside_ret = FALSE;
9
	return CAIRO_STATUS_SUCCESS;
    }
6
    _cairo_gstate_user_to_backend (gstate, &x, &y);
    /* Before we perform the expensive stroke analysis,
     * check whether the point is within the extents of the path.
     */
6
    _cairo_path_fixed_approximate_stroke_extents (path,
6
						  &gstate->stroke_style,
6
						  &gstate->ctm,
6
						  gstate->target->is_vector,
						  &extents);
6
    if (x < extents.x || x > extents.x + extents.width ||
3
	y < extents.y || y > extents.y + extents.height)
    {
3
	*inside_ret = FALSE;
3
	return CAIRO_STATUS_SUCCESS;
    }
3
    limit.p1.x = _cairo_fixed_from_double (x) - 1;
3
    limit.p1.y = _cairo_fixed_from_double (y) - 1;
3
    limit.p2.x = limit.p1.x + 2;
3
    limit.p2.y = limit.p1.y + 2;
3
    _cairo_traps_init (&traps);
3
    _cairo_traps_limit (&traps, &limit, 1);
3
    status = _cairo_path_fixed_stroke_polygon_to_traps (path,
3
							&gstate->stroke_style,
3
							&gstate->ctm,
3
							&gstate->ctm_inverse,
							gstate->tolerance,
							&traps);
3
    if (unlikely (status))
	goto BAIL;
3
    *inside_ret = _cairo_traps_contain (&traps, x, y);
3
BAIL:
3
    _cairo_traps_fini (&traps);
3
    return status;
}
cairo_status_t
393338
_cairo_gstate_fill (cairo_gstate_t *gstate, cairo_path_fixed_t *path)
{
    cairo_status_t status;
393338
    status = _cairo_gstate_get_pattern_status (gstate->source);
393338
    if (unlikely (status))
	return status;
393338
    if (gstate->op == CAIRO_OPERATOR_DEST)
2901
	return CAIRO_STATUS_SUCCESS;
390437
    if (_cairo_clip_is_all_clipped (gstate->clip))
12
	return CAIRO_STATUS_SUCCESS;
390425
    assert (gstate->opacity == 1.0);
390425
    if (_cairo_path_fixed_fill_is_empty (path)) {
210
	if (_cairo_operator_bounded_by_mask (gstate->op))
135
	    return CAIRO_STATUS_SUCCESS;
75
	status = _cairo_surface_paint (gstate->target,
				       CAIRO_OPERATOR_CLEAR,
				       &_cairo_pattern_clear.base,
75
				       gstate->clip);
    } else {
	cairo_pattern_union_t source_pattern;
	const cairo_pattern_t *pattern;
	cairo_operator_t op;
	cairo_rectangle_int_t extents;
	cairo_box_t box;
390215
	op = _reduce_op (gstate);
390215
	if (op == CAIRO_OPERATOR_CLEAR) {
141
	    pattern = &_cairo_pattern_clear.base;
	} else {
390074
	    _cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
390074
	    pattern = &source_pattern.base;
	}
	/* Toolkits often paint the entire background with a fill */
779691
	if (_cairo_surface_get_extents (gstate->target, &extents) &&
670187
	    _cairo_path_fixed_is_box (path, &box) &&
287849
	    box.p1.x <= _cairo_fixed_from_int (extents.x) &&
8561
	    box.p1.y <= _cairo_fixed_from_int (extents.y) &&
1664
	    box.p2.x >= _cairo_fixed_from_int (extents.x + extents.width) &&
241
	    box.p2.y >= _cairo_fixed_from_int (extents.y + extents.height))
	{
52
	    status = _cairo_surface_paint (gstate->target, op, pattern,
52
					   gstate->clip);
	}
	else
	{
390163
	    status = _cairo_surface_fill (gstate->target, op, pattern,
					  path,
					  gstate->fill_rule,
					  gstate->tolerance,
					  gstate->antialias,
390163
					  gstate->clip);
	}
    }
390290
    return status;
}
cairo_bool_t
145
_cairo_gstate_in_fill (cairo_gstate_t	  *gstate,
		       cairo_path_fixed_t *path,
		       double		   x,
		       double		   y)
{
145
    _cairo_gstate_user_to_backend (gstate, &x, &y);
145
    return _cairo_path_fixed_in_fill (path,
				      gstate->fill_rule,
				      gstate->tolerance,
				      x, y);
}
cairo_bool_t
6
_cairo_gstate_in_clip (cairo_gstate_t	  *gstate,
		       double		   x,
		       double		   y)
{
6
    cairo_clip_t *clip = gstate->clip;
    int i;
6
    if (_cairo_clip_is_all_clipped (clip))
	return FALSE;
6
    if (clip == NULL)
6
	return TRUE;
    _cairo_gstate_user_to_backend (gstate, &x, &y);
    if (x <  clip->extents.x ||
	x >= clip->extents.x + clip->extents.width ||
	y <  clip->extents.y ||
	y >= clip->extents.y + clip->extents.height)
    {
	return FALSE;
    }
    if (clip->num_boxes) {
	int fx, fy;
	fx = _cairo_fixed_from_double (x);
	fy = _cairo_fixed_from_double (y);
	for (i = 0; i < clip->num_boxes; i++) {
	    if (fx >= clip->boxes[i].p1.x && fx <= clip->boxes[i].p2.x &&
		fy >= clip->boxes[i].p1.y && fy <= clip->boxes[i].p2.y)
		break;
	}
	if (i == clip->num_boxes)
	    return FALSE;
    }
    if (clip->path) {
	cairo_clip_path_t *clip_path = clip->path;
	do {
	    if (! _cairo_path_fixed_in_fill (&clip_path->path,
					     clip_path->fill_rule,
					     clip_path->tolerance,
					     x, y))
		return FALSE;
	} while ((clip_path = clip_path->prev) != NULL);
    }
    return TRUE;
}
cairo_status_t
6
_cairo_gstate_copy_page (cairo_gstate_t *gstate)
{
6
    cairo_surface_copy_page (gstate->target);
6
    return cairo_surface_status (gstate->target);
}
cairo_status_t
6
_cairo_gstate_show_page (cairo_gstate_t *gstate)
{
6
    cairo_surface_show_page (gstate->target);
6
    return cairo_surface_status (gstate->target);
}
static void
120
_cairo_gstate_extents_to_user_rectangle (cairo_gstate_t	  *gstate,
					 const cairo_box_t *extents,
					 double *x1, double *y1,
					 double *x2, double *y2)
{
    double px1, py1, px2, py2;
120
    px1 = _cairo_fixed_to_double (extents->p1.x);
120
    py1 = _cairo_fixed_to_double (extents->p1.y);
120
    px2 = _cairo_fixed_to_double (extents->p2.x);
120
    py2 = _cairo_fixed_to_double (extents->p2.y);
120
    _cairo_gstate_backend_to_user_rectangle (gstate,
					     &px1, &py1, &px2, &py2,
					     NULL);
120
    if (x1)
120
	*x1 = px1;
120
    if (y1)
120
	*y1 = py1;
120
    if (x2)
120
	*x2 = px2;
120
    if (y2)
120
	*y2 = py2;
120
}
cairo_status_t
120
_cairo_gstate_stroke_extents (cairo_gstate_t	 *gstate,
			      cairo_path_fixed_t *path,
                              double *x1, double *y1,
			      double *x2, double *y2)
{
    cairo_int_status_t status;
    cairo_box_t extents;
    cairo_bool_t empty;
120
    if (x1)
120
	*x1 = 0.0;
120
    if (y1)
120
	*y1 = 0.0;
120
    if (x2)
120
	*x2 = 0.0;
120
    if (y2)
120
	*y2 = 0.0;
120
    if (gstate->stroke_style.line_width <= 0.0)
9
	return CAIRO_STATUS_SUCCESS;
111
    status = CAIRO_INT_STATUS_UNSUPPORTED;
111
    if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
	cairo_boxes_t boxes;
72
	_cairo_boxes_init (&boxes);
72
	status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
72
								&gstate->stroke_style,
72
								&gstate->ctm,
								gstate->antialias,
								&boxes);
72
	empty = boxes.num_boxes == 0;
72
	if (! empty)
27
	    _cairo_boxes_extents (&boxes, &extents);
72
	_cairo_boxes_fini (&boxes);
    }
111
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
	cairo_polygon_t polygon;
57
	_cairo_polygon_init (&polygon, NULL, 0);
57
	status = _cairo_path_fixed_stroke_to_polygon (path,
57
						      &gstate->stroke_style,
57
						      &gstate->ctm,
57
						      &gstate->ctm_inverse,
						      gstate->tolerance,
						      &polygon);
57
	empty = polygon.num_edges == 0;
57
	if (! empty)
57
	    extents = polygon.extents;
57
	_cairo_polygon_fini (&polygon);
    }
111
    if (! empty) {
84
	_cairo_gstate_extents_to_user_rectangle (gstate, &extents,
						 x1, y1, x2, y2);
    }
111
    return status;
}
cairo_status_t
78
_cairo_gstate_fill_extents (cairo_gstate_t     *gstate,
			    cairo_path_fixed_t *path,
                            double *x1, double *y1,
			    double *x2, double *y2)
{
    cairo_status_t status;
    cairo_box_t extents;
    cairo_bool_t empty;
78
    if (x1)
78
	*x1 = 0.0;
78
    if (y1)
78
	*y1 = 0.0;
78
    if (x2)
78
	*x2 = 0.0;
78
    if (y2)
78
	*y2 = 0.0;
78
    if (_cairo_path_fixed_fill_is_empty (path))
36
	return CAIRO_STATUS_SUCCESS;
42
    if (_cairo_path_fixed_fill_is_rectilinear (path)) {
	cairo_boxes_t boxes;
36
	_cairo_boxes_init (&boxes);
36
	status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
							      gstate->fill_rule,
							      gstate->antialias,
							      &boxes);
36
	empty = boxes.num_boxes == 0;
36
	if (! empty)
30
	    _cairo_boxes_extents (&boxes, &extents);
36
	_cairo_boxes_fini (&boxes);
    } else {
	cairo_traps_t traps;
6
	_cairo_traps_init (&traps);
6
	status = _cairo_path_fixed_fill_to_traps (path,
						  gstate->fill_rule,
						  gstate->tolerance,
						  &traps);
6
	empty = traps.num_traps == 0;
6
	if (! empty)
6
	    _cairo_traps_extents (&traps, &extents);
6
	_cairo_traps_fini (&traps);
    }
42
    if (! empty) {
36
	_cairo_gstate_extents_to_user_rectangle (gstate, &extents,
						 x1, y1, x2, y2);
    }
42
    return status;
}
cairo_status_t
300435
_cairo_gstate_reset_clip (cairo_gstate_t *gstate)
{
300435
    _cairo_clip_destroy (gstate->clip);
300435
    gstate->clip = NULL;
300435
    return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
305463
_cairo_gstate_clip (cairo_gstate_t *gstate, cairo_path_fixed_t *path)
{
305463
    gstate->clip =
305463
	_cairo_clip_intersect_path (gstate->clip,
				    path,
				    gstate->fill_rule,
				    gstate->tolerance,
				    gstate->antialias);
    /* XXX */
305463
    return CAIRO_STATUS_SUCCESS;
}
static cairo_bool_t
34961
_cairo_gstate_int_clip_extents (cairo_gstate_t        *gstate,
				cairo_rectangle_int_t *extents)
{
    cairo_bool_t is_bounded;
34961
    is_bounded = _cairo_surface_get_extents (gstate->target, extents);
34961
    if (gstate->clip) {
97
	_cairo_rectangle_intersect (extents,
97
				    _cairo_clip_get_extents (gstate->clip));
97
	is_bounded = TRUE;
    }
34961
    return is_bounded;
}
cairo_bool_t
32
_cairo_gstate_clip_extents (cairo_gstate_t *gstate,
		            double         *x1,
		            double         *y1,
			    double         *x2,
			    double         *y2)
{
    cairo_rectangle_int_t extents;
    double px1, py1, px2, py2;
32
    if (! _cairo_gstate_int_clip_extents (gstate, &extents))
	return FALSE;
32
    px1 = extents.x;
32
    py1 = extents.y;
32
    px2 = extents.x + (int) extents.width;
32
    py2 = extents.y + (int) extents.height;
32
    _cairo_gstate_backend_to_user_rectangle (gstate,
					     &px1, &py1, &px2, &py2,
					     NULL);
32
    if (x1)
32
	*x1 = px1;
32
    if (y1)
32
	*y1 = py1;
32
    if (x2)
32
	*x2 = px2;
32
    if (y2)
32
	*y2 = py2;
32
    return TRUE;
}
cairo_rectangle_list_t*
15
_cairo_gstate_copy_clip_rectangle_list (cairo_gstate_t *gstate)
{
    cairo_rectangle_int_t extents;
    cairo_rectangle_list_t *list;
    cairo_clip_t *clip;
15
    if (_cairo_surface_get_extents (gstate->target, &extents))
15
	clip = _cairo_clip_copy_intersect_rectangle (gstate->clip, &extents);
    else
	clip = gstate->clip;
15
    list = _cairo_clip_copy_rectangle_list (clip, gstate);
15
    if (clip != gstate->clip)
14
	_cairo_clip_destroy (clip);
15
    return list;
}
cairo_status_t
3
_cairo_gstate_tag_begin (cairo_gstate_t *gstate,
			 const char *tag_name, const char *attributes)
{
3
    return _cairo_surface_tag (gstate->target,
			       TRUE, /* begin */
			       tag_name,
			       attributes ? attributes : "");
}
cairo_status_t
3
_cairo_gstate_tag_end (cairo_gstate_t *gstate,
		       const char *tag_name)
{
3
    return _cairo_surface_tag (gstate->target,
			       FALSE, /* begin */
			       tag_name,
			       NULL); /* attributes */
}
static void
192926
_cairo_gstate_unset_scaled_font (cairo_gstate_t *gstate)
{
192926
    if (gstate->scaled_font == NULL)
125813
	return;
67113
    if (gstate->previous_scaled_font != NULL)
66924
	cairo_scaled_font_destroy (gstate->previous_scaled_font);
67113
    gstate->previous_scaled_font = gstate->scaled_font;
67113
    gstate->scaled_font = NULL;
}
cairo_status_t
2745
_cairo_gstate_set_font_size (cairo_gstate_t *gstate,
			     double          size)
{
2745
    _cairo_gstate_unset_scaled_font (gstate);
2745
    cairo_matrix_init_scale (&gstate->font_matrix, size, size);
2745
    return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
27258
_cairo_gstate_set_font_matrix (cairo_gstate_t	    *gstate,
			       const cairo_matrix_t *matrix)
{
27258
    if (memcmp (matrix, &gstate->font_matrix, sizeof (cairo_matrix_t)) == 0)
27054
	return CAIRO_STATUS_SUCCESS;
204
    _cairo_gstate_unset_scaled_font (gstate);
204
    gstate->font_matrix = *matrix;
204
    return CAIRO_STATUS_SUCCESS;
}
void
51
_cairo_gstate_get_font_matrix (cairo_gstate_t *gstate,
			       cairo_matrix_t *matrix)
{
51
    *matrix = gstate->font_matrix;
51
}
void
30153
_cairo_gstate_set_font_options (cairo_gstate_t             *gstate,
				const cairo_font_options_t *options)
{
30153
    if (_cairo_font_options_compare (options, &gstate->font_options))
18
	return;
30135
    _cairo_gstate_unset_scaled_font (gstate);
30135
    _cairo_font_options_fini (&gstate->font_options);
30135
    _cairo_font_options_init_copy (&gstate->font_options, options);
}
void
81
_cairo_gstate_get_font_options (cairo_gstate_t       *gstate,
				cairo_font_options_t *options)
{
81
    _cairo_font_options_fini (options);
81
    _cairo_font_options_init_copy (options, &gstate->font_options);
81
}
cairo_status_t
34
_cairo_gstate_get_font_face (cairo_gstate_t     *gstate,
			     cairo_font_face_t **font_face)
{
    cairo_status_t status;
34
    status = _cairo_gstate_ensure_font_face (gstate);
34
    if (unlikely (status))
	return status;
34
    *font_face = gstate->font_face;
34
    return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
67990
_cairo_gstate_get_scaled_font (cairo_gstate_t       *gstate,
			       cairo_scaled_font_t **scaled_font)
{
    cairo_status_t status;
67990
    status = _cairo_gstate_ensure_scaled_font (gstate);
67990
    if (unlikely (status))
9
	return status;
67981
    *scaled_font = gstate->scaled_font;
67981
    return CAIRO_STATUS_SUCCESS;
}
/*
 * Like everything else in this file, fonts involve Too Many Coordinate Spaces;
 * it is easy to get confused about what's going on.
 *
 * The user's view
 * ---------------
 *
 * Users ask for things in user space. When cairo starts, a user space unit
 * is about 1/96 inch, which is similar to (but importantly different from)
 * the normal "point" units most users think in terms of. When a user
 * selects a font, its scale is set to "one user unit". The user can then
 * independently scale the user coordinate system *or* the font matrix, in
 * order to adjust the rendered size of the font.
 *
 * Metrics are returned in user space, whether they are obtained from
 * the currently selected font in a  #cairo_t or from a #cairo_scaled_font_t
 * which is a font specialized to a particular scale matrix, CTM, and target
 * surface.
 *
 * The font's view
 * ---------------
 *
 * Fonts are designed and stored (in say .ttf files) in "font space", which
 * describes an "EM Square" (a design tile) and has some abstract number
 * such as 1000, 1024, or 2048 units per "EM". This is basically an
 * uninteresting space for us, but we need to remember that it exists.
 *
 * Font resources (from libraries or operating systems) render themselves
 * to a particular device. Since they do not want to make most programmers
 * worry about the font design space, the scaling API is simplified to
 * involve just telling the font the required pixel size of the EM square
 * (that is, in device space).
 *
 *
 * Cairo's gstate view
 * -------------------
 *
 * In addition to the CTM and CTM inverse, we keep a matrix in the gstate
 * called the "font matrix" which describes the user's most recent
 * font-scaling or font-transforming request. This is kept in terms of an
 * abstract scale factor, composed with the CTM and used to set the font's
 * pixel size. So if the user asks to "scale the font by 12", the matrix
 * is:
 *
 *   [ 12.0, 0.0, 0.0, 12.0, 0.0, 0.0 ]
 *
 * It is an affine matrix, like all cairo matrices, where its tx and ty
 * components are used to "nudging" fonts around and are handled in gstate
 * and then ignored by the "scaled-font" layer.
 *
 * In order to perform any action on a font, we must build an object
 * called a #cairo_font_scale_t; this contains the central 2x2 matrix
 * resulting from "font matrix * CTM" (sans the font matrix translation
 * components as stated in the previous paragraph).
 *
 * We pass this to the font when making requests of it, which causes it to
 * reply for a particular [user request, device] combination, under the CTM
 * (to accommodate the "zoom in" == "bigger fonts" issue above).
 *
 * The other terms in our communication with the font are therefore in
 * device space. When we ask it to perform text->glyph conversion, it will
 * produce a glyph string in device space. Glyph vectors we pass to it for
 * measuring or rendering should be in device space. The metrics which we
 * get back from the font will be in device space. The contents of the
 * global glyph image cache will be in device space.
 *
 *
 * Cairo's public view
 * -------------------
 *
 * Since the values entering and leaving via public API calls are in user
 * space, the gstate functions typically need to multiply arguments by the
 * CTM (for user-input glyph vectors), and return values by the CTM inverse
 * (for font responses such as metrics or glyph vectors).
 *
 */
static cairo_status_t
95108
_cairo_gstate_ensure_font_face (cairo_gstate_t *gstate)
{
    cairo_font_face_t *font_face;
95108
    if (gstate->font_face != NULL)
95028
	return gstate->font_face->status;
80
    font_face = cairo_toy_font_face_create (CAIRO_FONT_FAMILY_DEFAULT,
					    CAIRO_FONT_SLANT_DEFAULT,
					    CAIRO_FONT_WEIGHT_DEFAULT);
80
    if (font_face->status)
	return font_face->status;
80
    gstate->font_face = font_face;
80
    return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
282187
_cairo_gstate_ensure_scaled_font (cairo_gstate_t *gstate)
{
    cairo_status_t status;
    cairo_font_options_t options;
    cairo_scaled_font_t *scaled_font;
    cairo_matrix_t font_ctm;
282187
    if (gstate->scaled_font != NULL)
187113
	return gstate->scaled_font->status;
95074
    status = _cairo_gstate_ensure_font_face (gstate);
95074
    if (unlikely (status))
	return status;
95074
    cairo_surface_get_font_options (gstate->target, &options);
95074
    cairo_font_options_merge (&options, &gstate->font_options);
95074
    cairo_matrix_multiply (&font_ctm,
95074
			   &gstate->ctm,
95074
			   &gstate->target->device_transform);
95074
    scaled_font = cairo_scaled_font_create (gstate->font_face,
95074
				            &gstate->font_matrix,
					    &font_ctm,
					    &options);
95074
    _cairo_font_options_fini (&options);
95074
    status = cairo_scaled_font_status (scaled_font);
95074
    if (unlikely (status))
9
	return status;
95065
    gstate->scaled_font = scaled_font;
95065
    return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
110793
_cairo_gstate_get_font_extents (cairo_gstate_t *gstate,
				cairo_font_extents_t *extents)
{
110793
    cairo_status_t status = _cairo_gstate_ensure_scaled_font (gstate);
110793
    if (unlikely (status))
	return status;
110793
    cairo_scaled_font_extents (gstate->scaled_font, extents);
110793
    return cairo_scaled_font_status (gstate->scaled_font);
}
cairo_status_t
141077
_cairo_gstate_set_font_face (cairo_gstate_t    *gstate,
			     cairo_font_face_t *font_face)
{
141077
    if (font_face && font_face->status)
	return _cairo_error (font_face->status);
141077
    if (font_face == gstate->font_face)
46263
	return CAIRO_STATUS_SUCCESS;
94814
    cairo_font_face_destroy (gstate->font_face);
94814
    gstate->font_face = cairo_font_face_reference (font_face);
94814
    _cairo_gstate_unset_scaled_font (gstate);
94814
    return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
68310
_cairo_gstate_glyph_extents (cairo_gstate_t *gstate,
			     const cairo_glyph_t *glyphs,
			     int num_glyphs,
			     cairo_text_extents_t *extents)
{
    cairo_status_t status;
68310
    status = _cairo_gstate_ensure_scaled_font (gstate);
68310
    if (unlikely (status))
	return status;
68310
    cairo_scaled_font_glyph_extents (gstate->scaled_font,
				     glyphs, num_glyphs,
				     extents);
68310
    return cairo_scaled_font_status (gstate->scaled_font);
}
cairo_status_t
34944
_cairo_gstate_show_text_glyphs (cairo_gstate_t		   *gstate,
				const cairo_glyph_t	   *glyphs,
				int			    num_glyphs,
				cairo_glyph_text_info_t    *info)
{
    cairo_glyph_t stack_transformed_glyphs[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t)];
    cairo_text_cluster_t stack_transformed_clusters[CAIRO_STACK_ARRAY_LENGTH (cairo_text_cluster_t)];
    cairo_pattern_union_t source_pattern;
    cairo_glyph_t *transformed_glyphs;
    const cairo_pattern_t *pattern;
    cairo_text_cluster_t *transformed_clusters;
    cairo_operator_t op;
    cairo_status_t status;
34944
    status = _cairo_gstate_get_pattern_status (gstate->source);
34944
    if (unlikely (status))
	return status;
34944
    if (gstate->op == CAIRO_OPERATOR_DEST)
3
	return CAIRO_STATUS_SUCCESS;
34941
    if (_cairo_clip_is_all_clipped (gstate->clip))
12
	return CAIRO_STATUS_SUCCESS;
34929
    status = _cairo_gstate_ensure_scaled_font (gstate);
34929
    if (unlikely (status))
	return status;
34929
    transformed_glyphs = stack_transformed_glyphs;
34929
    transformed_clusters = stack_transformed_clusters;
34929
    if (num_glyphs > ARRAY_LENGTH (stack_transformed_glyphs)) {
18
	transformed_glyphs = cairo_glyph_allocate (num_glyphs);
18
	if (unlikely (transformed_glyphs == NULL))
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    }
34929
    if (info != NULL) {
216
	if (info->num_clusters > ARRAY_LENGTH (stack_transformed_clusters)) {
	    transformed_clusters = cairo_text_cluster_allocate (info->num_clusters);
	    if (unlikely (transformed_clusters == NULL)) {
		status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
		goto CLEANUP_GLYPHS;
	    }
	}
216
	_cairo_gstate_transform_glyphs_to_backend (gstate,
						   glyphs, num_glyphs,
						   info->clusters,
						   info->num_clusters,
						   info->cluster_flags,
						   transformed_glyphs,
						   &num_glyphs,
						   transformed_clusters,
						   TRUE);
    } else {
34713
	_cairo_gstate_transform_glyphs_to_backend (gstate,
						   glyphs, num_glyphs,
						   NULL, 0, 0,
						   transformed_glyphs,
						   &num_glyphs,
						   NULL,
						   TRUE);
    }
34929
    if (num_glyphs == 0)
27
	goto CLEANUP_GLYPHS;
34902
    op = _reduce_op (gstate);
34902
    if (op == CAIRO_OPERATOR_CLEAR) {
9
	pattern = &_cairo_pattern_clear.base;
    } else {
34893
	_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
34893
	pattern = &source_pattern.base;
    }
    /* For really huge font sizes, we can just do path;fill instead of
     * show_glyphs, as show_glyphs would put excess pressure on the cache,
     * and moreover, not all components below us correctly handle huge font
     * sizes.  I wanted to set the limit at 256.  But alas, seems like cairo's
     * rasterizer is something like ten times slower than freetype's for huge
     * sizes.  So, no win just yet.  For now, do it for insanely-huge sizes,
     * just to make sure we don't make anyone unhappy.  When we get a really
     * fast rasterizer in cairo, we may want to readjust this.
     *
     * Needless to say, do this only if show_text_glyphs is not available. */
69501
    if (cairo_surface_has_show_text_glyphs (gstate->target) ||
34599
	_cairo_scaled_font_get_max_scale (gstate->scaled_font) <= 10240)
    {
34902
	if (info != NULL) {
210
	    status = _cairo_surface_show_text_glyphs (gstate->target, op, pattern,
						      info->utf8, info->utf8_len,
						      transformed_glyphs, num_glyphs,
						      transformed_clusters, info->num_clusters,
						      info->cluster_flags,
						      gstate->scaled_font,
210
						      gstate->clip);
	} else {
34692
	    status = _cairo_surface_show_text_glyphs (gstate->target, op, pattern,
						      NULL, 0,
						      transformed_glyphs, num_glyphs,
						      NULL, 0, 0,
						      gstate->scaled_font,
34692
						      gstate->clip);
	}
    }
    else
    {
	cairo_path_fixed_t path;
	_cairo_path_fixed_init (&path);
	status = _cairo_scaled_font_glyph_path (gstate->scaled_font,
						transformed_glyphs, num_glyphs,
						&path);
	if (status == CAIRO_STATUS_SUCCESS) {
	    status = _cairo_surface_fill (gstate->target, op, pattern,
					  &path,
					  CAIRO_FILL_RULE_WINDING,
					  gstate->tolerance,
					  gstate->scaled_font->options.antialias,
					  gstate->clip);
	}
	_cairo_path_fixed_fini (&path);
    }
34929
CLEANUP_GLYPHS:
34929
    if (transformed_glyphs != stack_transformed_glyphs)
18
      cairo_glyph_free (transformed_glyphs);
34929
    if (transformed_clusters != stack_transformed_clusters)
      cairo_text_cluster_free (transformed_clusters);
34929
    return status;
}
cairo_status_t
165
_cairo_gstate_glyph_path (cairo_gstate_t      *gstate,
			  const cairo_glyph_t *glyphs,
			  int		       num_glyphs,
			  cairo_path_fixed_t  *path)
{
    cairo_glyph_t stack_transformed_glyphs[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t)];
    cairo_glyph_t *transformed_glyphs;
    cairo_status_t status;
165
    status = _cairo_gstate_ensure_scaled_font (gstate);
165
    if (unlikely (status))
	return status;
165
    if (num_glyphs < ARRAY_LENGTH (stack_transformed_glyphs)) {
165
	transformed_glyphs = stack_transformed_glyphs;
    } else {
	transformed_glyphs = cairo_glyph_allocate (num_glyphs);
	if (unlikely (transformed_glyphs == NULL))
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    }
165
    _cairo_gstate_transform_glyphs_to_backend (gstate,
					       glyphs, num_glyphs,
					       NULL, 0, 0,
					       transformed_glyphs,
					       &num_glyphs, NULL, FALSE);
165
    status = _cairo_scaled_font_glyph_path (gstate->scaled_font,
					    transformed_glyphs, num_glyphs,
					    path);
165
    if (transformed_glyphs != stack_transformed_glyphs)
      cairo_glyph_free (transformed_glyphs);
165
    return status;
}
cairo_status_t
138
_cairo_gstate_set_antialias (cairo_gstate_t *gstate,
			     cairo_antialias_t antialias)
{
138
    gstate->antialias = antialias;
138
    return CAIRO_STATUS_SUCCESS;
}
cairo_antialias_t
6
_cairo_gstate_get_antialias (cairo_gstate_t *gstate)
{
6
    return gstate->antialias;
}
/**
 * _cairo_gstate_transform_glyphs_to_backend:
 * @gstate: a #cairo_gstate_t
 * @glyphs: the array of #cairo_glyph_t objects to be transformed
 * @num_glyphs: the number of elements in @glyphs
 * @transformed_glyphs: a pre-allocated array of at least @num_glyphs
 * #cairo_glyph_t objects
 * @num_transformed_glyphs: the number of elements in @transformed_glyphs
 * after dropping out of bounds glyphs, or %NULL if glyphs shouldn't be
 * dropped
 *
 * Transform an array of glyphs to backend space by first adding the offset
 * of the font matrix, then transforming from user space to backend space.
 * The result of the transformation is placed in @transformed_glyphs.
 *
 * This also uses information from the scaled font and the surface to
 * cull/drop glyphs that will not be visible.
 **/
static void
35094
_cairo_gstate_transform_glyphs_to_backend (cairo_gstate_t	*gstate,
                                           const cairo_glyph_t	*glyphs,
                                           int			 num_glyphs,
					   const cairo_text_cluster_t	*clusters,
					   int			 num_clusters,
					   cairo_text_cluster_flags_t cluster_flags,
                                           cairo_glyph_t	*transformed_glyphs,
					   int			*num_transformed_glyphs,
					   cairo_text_cluster_t *transformed_clusters,
					   cairo_bool_t         perform_early_clip)
{
    cairo_rectangle_int_t surface_extents;
35094
    cairo_matrix_t *ctm = &gstate->ctm;
35094
    cairo_matrix_t *font_matrix = &gstate->font_matrix;
35094
    cairo_matrix_t *device_transform = &gstate->target->device_transform;
35094
    cairo_bool_t drop = FALSE;
35094
    double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
    int i, j, k;
35094
    drop = TRUE;
35094
    if (!perform_early_clip || !_cairo_gstate_int_clip_extents (gstate, &surface_extents)) {
468
	drop = FALSE; /* unbounded surface */
    } else {
34626
	double scale10 = 10 * _cairo_scaled_font_get_max_scale (gstate->scaled_font);
34626
	if (surface_extents.width == 0 || surface_extents.height == 0) {
	  /* No visible area.  Don't draw anything */
27
	  *num_transformed_glyphs = 0;
27
	  return;
	}
	/* XXX We currently drop any glyphs that have their position outside
	 * of the surface boundaries by a safety margin depending on the
	 * font scale.  This however can fail in extreme cases where the
	 * font has really long swashes for example...  We can correctly
	 * handle that by looking the glyph up and using its device bbox
	 * to device if it's going to be visible, but I'm not inclined to
	 * do that now.
	 */
34599
	x1 = surface_extents.x - scale10;
34599
	y1 = surface_extents.y - scale10;
34599
	x2 = surface_extents.x + (int) surface_extents.width  + scale10;
34599
	y2 = surface_extents.y + (int) surface_extents.height + scale10;
    }
35067
    if (!drop)
468
	*num_transformed_glyphs = num_glyphs;
#define KEEP_GLYPH(glyph) (x1 <= glyph.x && glyph.x <= x2 && y1 <= glyph.y && glyph.y <= y2)
35067
    j = 0;
69627
    if (_cairo_matrix_is_identity (ctm) &&
34560
        _cairo_matrix_is_identity (device_transform) &&
34560
	font_matrix->x0 == 0 && font_matrix->y0 == 0)
    {
34548
	if (! drop) {
213
	    memcpy (transformed_glyphs, glyphs,
		    num_glyphs * sizeof (cairo_glyph_t));
213
	    memcpy (transformed_clusters, clusters,
		    num_clusters * sizeof (cairo_text_cluster_t));
213
	    j = num_glyphs;
34335
	} else if (num_clusters == 0) {
791763
	    for (i = 0; i < num_glyphs; i++) {
757428
		transformed_glyphs[j].index = glyphs[i].index;
757428
		transformed_glyphs[j].x = glyphs[i].x;
757428
		transformed_glyphs[j].y = glyphs[i].y;
757428
		if (KEEP_GLYPH (transformed_glyphs[j]))
756468
		    j++;
	    }
	} else {
	    const cairo_glyph_t *cur_glyph;
	    if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
		cur_glyph = glyphs + num_glyphs - 1;
	    else
		cur_glyph = glyphs;
	    for (i = 0; i < num_clusters; i++) {
		cairo_bool_t cluster_visible = FALSE;
		for (k = 0; k < clusters[i].num_glyphs; k++) {
		    transformed_glyphs[j+k].index = cur_glyph->index;
		    transformed_glyphs[j+k].x = cur_glyph->x;
		    transformed_glyphs[j+k].y = cur_glyph->y;
		    if (KEEP_GLYPH (transformed_glyphs[j+k]))
			cluster_visible = TRUE;
		    if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
			cur_glyph--;
		    else
			cur_glyph++;
		}
		transformed_clusters[i] = clusters[i];
		if (cluster_visible)
		    j += k;
		else
		    transformed_clusters[i].num_glyphs = 0;
	    }
	}
    }
702
    else if (_cairo_matrix_is_translation (ctm) &&
183
             _cairo_matrix_is_translation (device_transform))
183
    {
183
        double tx = font_matrix->x0 + ctm->x0 + device_transform->x0;
183
        double ty = font_matrix->y0 + ctm->y0 + device_transform->y0;
183
	if (! drop || num_clusters == 0) {
11931
	    for (i = 0; i < num_glyphs; i++) {
11748
		transformed_glyphs[j].index = glyphs[i].index;
11748
		transformed_glyphs[j].x = glyphs[i].x + tx;
11748
		transformed_glyphs[j].y = glyphs[i].y + ty;
11748
		if (!drop || KEEP_GLYPH (transformed_glyphs[j]))
11748
		    j++;
	    }
183
	    memcpy (transformed_clusters, clusters,
		    num_clusters * sizeof (cairo_text_cluster_t));
	} else {
	    const cairo_glyph_t *cur_glyph;
	    if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
		cur_glyph = glyphs + num_glyphs - 1;
	    else
		cur_glyph = glyphs;
	    for (i = 0; i < num_clusters; i++) {
		cairo_bool_t cluster_visible = FALSE;
		for (k = 0; k < clusters[i].num_glyphs; k++) {
		    transformed_glyphs[j+k].index = cur_glyph->index;
		    transformed_glyphs[j+k].x = cur_glyph->x + tx;
		    transformed_glyphs[j+k].y = cur_glyph->y + ty;
		    if (KEEP_GLYPH (transformed_glyphs[j+k]))
			cluster_visible = TRUE;
		    if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
			cur_glyph--;
		    else
			cur_glyph++;
		}
		transformed_clusters[i] = clusters[i];
		if (cluster_visible)
		    j += k;
		else
		    transformed_clusters[i].num_glyphs = 0;
	    }
	}
    }
    else
    {
        cairo_matrix_t aggregate_transform;
336
        cairo_matrix_init_translate (&aggregate_transform,
                                     gstate->font_matrix.x0,
                                     gstate->font_matrix.y0);
336
        cairo_matrix_multiply (&aggregate_transform,
                               &aggregate_transform, ctm);
336
        cairo_matrix_multiply (&aggregate_transform,
                               &aggregate_transform, device_transform);
336
	if (! drop || num_clusters == 0) {
1119
	    for (i = 0; i < num_glyphs; i++) {
783
		transformed_glyphs[j] = glyphs[i];
783
		cairo_matrix_transform_point (&aggregate_transform,
783
					      &transformed_glyphs[j].x,
783
					      &transformed_glyphs[j].y);
783
		if (! drop || KEEP_GLYPH (transformed_glyphs[j]))
783
		    j++;
	    }
336
	    memcpy (transformed_clusters, clusters,
		    num_clusters * sizeof (cairo_text_cluster_t));
	} else {
	    const cairo_glyph_t *cur_glyph;
	    if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
		cur_glyph = glyphs + num_glyphs - 1;
	    else
		cur_glyph = glyphs;
	    for (i = 0; i < num_clusters; i++) {
		cairo_bool_t cluster_visible = FALSE;
		for (k = 0; k < clusters[i].num_glyphs; k++) {
		    transformed_glyphs[j+k] = *cur_glyph;
		    cairo_matrix_transform_point (&aggregate_transform,
						  &transformed_glyphs[j+k].x,
						  &transformed_glyphs[j+k].y);
		    if (KEEP_GLYPH (transformed_glyphs[j+k]))
			cluster_visible = TRUE;
		    if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
			cur_glyph--;
		    else
			cur_glyph++;
		}
		transformed_clusters[i] = clusters[i];
		if (cluster_visible)
		    j += k;
		else
		    transformed_clusters[i].num_glyphs = 0;
	    }
	}
    }
35067
    *num_transformed_glyphs = j;
35067
    if (num_clusters != 0 && cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD) {
	for (i = 0; i < --j; i++) {
	    cairo_glyph_t tmp;
	    tmp = transformed_glyphs[i];
	    transformed_glyphs[i] = transformed_glyphs[j];
	    transformed_glyphs[j] = tmp;
	}
    }
}