repo_id
stringlengths 27
162
| file_path
stringlengths 42
195
| content
stringlengths 4
5.16M
| __index_level_0__
int64 0
0
|
---|---|---|---|
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-contour-inline.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Intel Corporation
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#ifndef CAIRO_CONTOUR_INLINE_H
#define CAIRO_CONTOUR_INLINE_H
#include "cairo-contour-private.h"
CAIRO_BEGIN_DECLS
static inline cairo_int_status_t
_cairo_contour_add_point (cairo_contour_t *contour,
const cairo_point_t *point)
{
struct _cairo_contour_chain *tail = contour->tail;
if (unlikely (tail->num_points == tail->size_points))
return __cairo_contour_add_point (contour, point);
tail->points[tail->num_points++] = *point;
return CAIRO_INT_STATUS_SUCCESS;
}
static inline cairo_point_t *
_cairo_contour_first_point (cairo_contour_t *c)
{
return &c->chain.points[0];
}
static inline cairo_point_t *
_cairo_contour_last_point (cairo_contour_t *c)
{
return &c->tail->points[c->tail->num_points-1];
}
static inline void
_cairo_contour_remove_last_point (cairo_contour_t *contour)
{
if (contour->chain.num_points == 0)
return;
if (--contour->tail->num_points == 0)
__cairo_contour_remove_last_chain (contour);
}
CAIRO_END_DECLS
#endif /* CAIRO_CONTOUR_INLINE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-contour-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Intel Corporation
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#ifndef CAIRO_CONTOUR_PRIVATE_H
#define CAIRO_CONTOUR_PRIVATE_H
#include "cairo-types-private.h"
#include "cairo-compiler-private.h"
#include "cairo-error-private.h"
#include "cairo-list-private.h"
#include <stdio.h>
CAIRO_BEGIN_DECLS
/* A contour is simply a closed chain of points that divide the infinite plane
* into inside and outside. Each contour is a simple polygon, that is it
* contains no holes or self-intersections, but maybe either concave or convex.
*/
struct _cairo_contour_chain {
cairo_point_t *points;
int num_points, size_points;
struct _cairo_contour_chain *next;
};
struct _cairo_contour_iter {
cairo_point_t *point;
cairo_contour_chain_t *chain;
};
struct _cairo_contour {
cairo_list_t next;
int direction;
cairo_contour_chain_t chain, *tail;
cairo_point_t embedded_points[64];
};
/* Initial definition of a shape is a set of contours (some representing holes) */
struct _cairo_shape {
cairo_list_t contours;
};
typedef struct _cairo_shape cairo_shape_t;
#if 0
cairo_private cairo_status_t
_cairo_shape_init_from_polygon (cairo_shape_t *shape,
const cairo_polygon_t *polygon);
cairo_private cairo_status_t
_cairo_shape_reduce (cairo_shape_t *shape, double tolerance);
#endif
cairo_private void
_cairo_contour_init (cairo_contour_t *contour,
int direction);
cairo_private cairo_int_status_t
__cairo_contour_add_point (cairo_contour_t *contour,
const cairo_point_t *point);
cairo_private void
_cairo_contour_simplify (cairo_contour_t *contour, double tolerance);
cairo_private void
_cairo_contour_reverse (cairo_contour_t *contour);
cairo_private cairo_int_status_t
_cairo_contour_add (cairo_contour_t *dst,
const cairo_contour_t *src);
cairo_private cairo_int_status_t
_cairo_contour_add_reversed (cairo_contour_t *dst,
const cairo_contour_t *src);
cairo_private void
__cairo_contour_remove_last_chain (cairo_contour_t *contour);
cairo_private void
_cairo_contour_reset (cairo_contour_t *contour);
cairo_private void
_cairo_contour_fini (cairo_contour_t *contour);
cairo_private void
_cairo_debug_print_contour (FILE *file, cairo_contour_t *contour);
CAIRO_END_DECLS
#endif /* CAIRO_CONTOUR_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-contour.c | /*
* Copyright © 2004 Carl Worth
* Copyright © 2006 Red Hat, Inc.
* Copyright © 2008 Chris Wilson
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Carl Worth
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-freelist-private.h"
#include "cairo-combsort-inline.h"
#include "cairo-contour-inline.h"
#include "cairo-contour-private.h"
void
_cairo_contour_init (cairo_contour_t *contour,
int direction)
{
contour->direction = direction;
contour->chain.points = contour->embedded_points;
contour->chain.next = NULL;
contour->chain.num_points = 0;
contour->chain.size_points = ARRAY_LENGTH (contour->embedded_points);
contour->tail = &contour->chain;
}
cairo_int_status_t
__cairo_contour_add_point (cairo_contour_t *contour,
const cairo_point_t *point)
{
cairo_contour_chain_t *tail = contour->tail;
cairo_contour_chain_t *next;
assert (tail->next == NULL);
next = _cairo_malloc_ab_plus_c (tail->size_points*2,
sizeof (cairo_point_t),
sizeof (cairo_contour_chain_t));
if (unlikely (next == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
next->size_points = tail->size_points*2;
next->num_points = 1;
next->points = (cairo_point_t *)(next+1);
next->next = NULL;
tail->next = next;
contour->tail = next;
next->points[0] = *point;
return CAIRO_INT_STATUS_SUCCESS;
}
static void
first_inc (cairo_contour_t *contour,
cairo_point_t **p,
cairo_contour_chain_t **chain)
{
if (*p == (*chain)->points + (*chain)->num_points) {
assert ((*chain)->next);
*chain = (*chain)->next;
*p = &(*chain)->points[0];
} else
++*p;
}
static void
last_dec (cairo_contour_t *contour,
cairo_point_t **p,
cairo_contour_chain_t **chain)
{
if (*p == (*chain)->points) {
cairo_contour_chain_t *prev;
assert (*chain != &contour->chain);
for (prev = &contour->chain; prev->next != *chain; prev = prev->next)
;
*chain = prev;
*p = &(*chain)->points[(*chain)->num_points-1];
} else
--*p;
}
void
_cairo_contour_reverse (cairo_contour_t *contour)
{
cairo_contour_chain_t *first_chain, *last_chain;
cairo_point_t *first, *last;
contour->direction = -contour->direction;
if (contour->chain.num_points <= 1)
return;
first_chain = &contour->chain;
last_chain = contour->tail;
first = &first_chain->points[0];
last = &last_chain->points[last_chain->num_points-1];
while (first != last) {
cairo_point_t p;
p = *first;
*first = *last;
*last = p;
first_inc (contour, &first, &first_chain);
last_dec (contour, &last, &last_chain);
}
}
cairo_int_status_t
_cairo_contour_add (cairo_contour_t *dst,
const cairo_contour_t *src)
{
const cairo_contour_chain_t *chain;
cairo_int_status_t status;
int i;
for (chain = &src->chain; chain; chain = chain->next) {
for (i = 0; i < chain->num_points; i++) {
status = _cairo_contour_add_point (dst, &chain->points[i]);
if (unlikely (status))
return status;
}
}
return CAIRO_INT_STATUS_SUCCESS;
}
static inline cairo_bool_t
iter_next (cairo_contour_iter_t *iter)
{
if (iter->point == &iter->chain->points[iter->chain->size_points-1]) {
iter->chain = iter->chain->next;
if (iter->chain == NULL)
return FALSE;
iter->point = &iter->chain->points[0];
return TRUE;
} else {
iter->point++;
return TRUE;
}
}
static cairo_bool_t
iter_equal (const cairo_contour_iter_t *i1,
const cairo_contour_iter_t *i2)
{
return i1->chain == i2->chain && i1->point == i2->point;
}
static void
iter_init (cairo_contour_iter_t *iter, cairo_contour_t *contour)
{
iter->chain = &contour->chain;
iter->point = &contour->chain.points[0];
}
static void
iter_init_last (cairo_contour_iter_t *iter, cairo_contour_t *contour)
{
iter->chain = contour->tail;
iter->point = &contour->tail->points[contour->tail->num_points-1];
}
static const cairo_contour_chain_t *prev_const_chain(const cairo_contour_t *contour,
const cairo_contour_chain_t *chain)
{
const cairo_contour_chain_t *prev;
if (chain == &contour->chain)
return NULL;
for (prev = &contour->chain; prev->next != chain; prev = prev->next)
;
return prev;
}
cairo_int_status_t
_cairo_contour_add_reversed (cairo_contour_t *dst,
const cairo_contour_t *src)
{
const cairo_contour_chain_t *last;
cairo_int_status_t status;
int i;
if (src->chain.num_points == 0)
return CAIRO_INT_STATUS_SUCCESS;
for (last = src->tail; last; last = prev_const_chain (src, last)) {
for (i = last->num_points-1; i >= 0; i--) {
status = _cairo_contour_add_point (dst, &last->points[i]);
if (unlikely (status))
return status;
}
}
return CAIRO_INT_STATUS_SUCCESS;
}
static cairo_uint64_t
point_distance_sq (const cairo_point_t *p1,
const cairo_point_t *p2)
{
int32_t dx = p1->x - p2->x;
int32_t dy = p1->y - p2->y;
return _cairo_int32x32_64_mul (dx, dx) + _cairo_int32x32_64_mul (dy, dy);
}
#define DELETED(p) ((p)->x == INT_MIN && (p)->y == INT_MAX)
#define MARK_DELETED(p) ((p)->x = INT_MIN, (p)->y = INT_MAX)
static cairo_bool_t
_cairo_contour_simplify_chain (cairo_contour_t *contour, const double tolerance,
const cairo_contour_iter_t *first,
const cairo_contour_iter_t *last)
{
cairo_contour_iter_t iter, furthest;
uint64_t max_error;
int x0, y0;
int nx, ny;
int count;
iter = *first;
iter_next (&iter);
if (iter_equal (&iter, last))
return FALSE;
x0 = first->point->x;
y0 = first->point->y;
nx = last->point->y - y0;
ny = x0 - last->point->x;
count = 0;
max_error = 0;
do {
cairo_point_t *p = iter.point;
if (! DELETED(p)) {
uint64_t d = (uint64_t)nx * (x0 - p->x) + (uint64_t)ny * (y0 - p->y);
if (d * d > max_error) {
max_error = d * d;
furthest = iter;
}
count++;
}
iter_next (&iter);
} while (! iter_equal (&iter, last));
if (count == 0)
return FALSE;
if (max_error > tolerance * ((uint64_t)nx * nx + (uint64_t)ny * ny)) {
cairo_bool_t simplified;
simplified = FALSE;
simplified |= _cairo_contour_simplify_chain (contour, tolerance,
first, &furthest);
simplified |= _cairo_contour_simplify_chain (contour, tolerance,
&furthest, last);
return simplified;
} else {
iter = *first;
iter_next (&iter);
do {
MARK_DELETED (iter.point);
iter_next (&iter);
} while (! iter_equal (&iter, last));
return TRUE;
}
}
void
_cairo_contour_simplify (cairo_contour_t *contour, double tolerance)
{
cairo_contour_chain_t *chain;
cairo_point_t *last = NULL;
cairo_contour_iter_t iter, furthest;
cairo_bool_t simplified;
uint64_t max = 0;
int i;
if (contour->chain.num_points <= 2)
return;
tolerance = tolerance * CAIRO_FIXED_ONE;
tolerance *= tolerance;
/* stage 1: vertex reduction */
for (chain = &contour->chain; chain; chain = chain->next) {
for (i = 0; i < chain->num_points; i++) {
if (last == NULL ||
point_distance_sq (last, &chain->points[i]) > tolerance) {
last = &chain->points[i];
} else {
MARK_DELETED (&chain->points[i]);
}
}
}
/* stage2: polygon simplification using Douglas-Peucker */
do {
last = &contour->chain.points[0];
iter_init (&furthest, contour);
max = 0;
for (chain = &contour->chain; chain; chain = chain->next) {
for (i = 0; i < chain->num_points; i++) {
uint64_t d;
if (DELETED (&chain->points[i]))
continue;
d = point_distance_sq (last, &chain->points[i]);
if (d > max) {
furthest.chain = chain;
furthest.point = &chain->points[i];
max = d;
}
}
}
assert (max);
simplified = FALSE;
iter_init (&iter, contour);
simplified |= _cairo_contour_simplify_chain (contour, tolerance,
&iter, &furthest);
iter_init_last (&iter, contour);
if (! iter_equal (&furthest, &iter))
simplified |= _cairo_contour_simplify_chain (contour, tolerance,
&furthest, &iter);
} while (simplified);
iter_init (&iter, contour);
for (chain = &contour->chain; chain; chain = chain->next) {
int num_points = chain->num_points;
chain->num_points = 0;
for (i = 0; i < num_points; i++) {
if (! DELETED(&chain->points[i])) {
if (iter.point != &chain->points[i])
*iter.point = chain->points[i];
iter.chain->num_points++;
iter_next (&iter);
}
}
}
if (iter.chain) {
cairo_contour_chain_t *next;
for (chain = iter.chain->next; chain; chain = next) {
next = chain->next;
free (chain);
}
iter.chain->next = NULL;
contour->tail = iter.chain;
}
}
void
_cairo_contour_reset (cairo_contour_t *contour)
{
_cairo_contour_fini (contour);
_cairo_contour_init (contour, contour->direction);
}
void
_cairo_contour_fini (cairo_contour_t *contour)
{
cairo_contour_chain_t *chain, *next;
for (chain = contour->chain.next; chain; chain = next) {
next = chain->next;
free (chain);
}
}
void
_cairo_debug_print_contour (FILE *file, cairo_contour_t *contour)
{
cairo_contour_chain_t *chain;
int num_points, size_points;
int i;
num_points = 0;
size_points = 0;
for (chain = &contour->chain; chain; chain = chain->next) {
num_points += chain->num_points;
size_points += chain->size_points;
}
fprintf (file, "contour: direction=%d, num_points=%d / %d\n",
contour->direction, num_points, size_points);
num_points = 0;
for (chain = &contour->chain; chain; chain = chain->next) {
for (i = 0; i < chain->num_points; i++) {
fprintf (file, " [%d] = (%f, %f)\n",
num_points++,
_cairo_fixed_to_double (chain->points[i].x),
_cairo_fixed_to_double (chain->points[i].y));
}
}
}
void
__cairo_contour_remove_last_chain (cairo_contour_t *contour)
{
cairo_contour_chain_t *chain;
if (contour->tail == &contour->chain)
return;
for (chain = &contour->chain; chain->next != contour->tail; chain = chain->next)
;
free (contour->tail);
contour->tail = chain;
chain->next = NULL;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-damage-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2012 Intel Corporation
*
* 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 Chris Wilson
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#ifndef CAIRO_DAMAGE_PRIVATE_H
#define CAIRO_DAMAGE_PRIVATE_H
#include "cairo-types-private.h"
#include <pixman/pixman.h>
CAIRO_BEGIN_DECLS
struct _cairo_damage {
cairo_status_t status;
cairo_region_t *region;
int dirty, remain;
struct _cairo_damage_chunk {
struct _cairo_damage_chunk *next;
cairo_box_t *base;
int count;
int size;
} chunks, *tail;
cairo_box_t boxes[32];
};
cairo_private cairo_damage_t *
_cairo_damage_create (void);
cairo_private cairo_damage_t *
_cairo_damage_create_in_error (cairo_status_t status);
cairo_private cairo_damage_t *
_cairo_damage_add_box (cairo_damage_t *damage,
const cairo_box_t *box);
cairo_private cairo_damage_t *
_cairo_damage_add_rectangle (cairo_damage_t *damage,
const cairo_rectangle_int_t *rect);
cairo_private cairo_damage_t *
_cairo_damage_add_region (cairo_damage_t *damage,
const cairo_region_t *region);
cairo_private cairo_damage_t *
_cairo_damage_reduce (cairo_damage_t *damage);
cairo_private void
_cairo_damage_destroy (cairo_damage_t *damage);
CAIRO_END_DECLS
#endif /* CAIRO_DAMAGE_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-damage.c | /*
* Copyright © 2012 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Chris Wilson
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-damage-private.h"
#include "cairo-region-private.h"
static const cairo_damage_t __cairo_damage__nil = { CAIRO_STATUS_NO_MEMORY };
cairo_damage_t *
_cairo_damage_create_in_error (cairo_status_t status)
{
_cairo_error_throw (status);
return (cairo_damage_t *) &__cairo_damage__nil;
}
cairo_damage_t *
_cairo_damage_create (void)
{
cairo_damage_t *damage;
damage = _cairo_malloc (sizeof (*damage));
if (unlikely (damage == NULL)) {
_cairo_error_throw(CAIRO_STATUS_NO_MEMORY);
return (cairo_damage_t *) &__cairo_damage__nil;
}
damage->status = CAIRO_STATUS_SUCCESS;
damage->region = NULL;
damage->dirty = 0;
damage->tail = &damage->chunks;
damage->chunks.base = damage->boxes;
damage->chunks.size = ARRAY_LENGTH(damage->boxes);
damage->chunks.count = 0;
damage->chunks.next = NULL;
damage->remain = damage->chunks.size;
return damage;
}
void
_cairo_damage_destroy (cairo_damage_t *damage)
{
struct _cairo_damage_chunk *chunk, *next;
if (damage == (cairo_damage_t *) &__cairo_damage__nil)
return;
for (chunk = damage->chunks.next; chunk != NULL; chunk = next) {
next = chunk->next;
free (chunk);
}
cairo_region_destroy (damage->region);
free (damage);
}
static cairo_damage_t *
_cairo_damage_add_boxes(cairo_damage_t *damage,
const cairo_box_t *boxes,
int count)
{
struct _cairo_damage_chunk *chunk;
int n, size;
TRACE ((stderr, "%s x%d\n", __FUNCTION__, count));
if (damage == NULL)
damage = _cairo_damage_create ();
if (damage->status)
return damage;
damage->dirty += count;
n = count;
if (n > damage->remain)
n = damage->remain;
memcpy (damage->tail->base + damage->tail->count, boxes,
n * sizeof (cairo_box_t));
count -= n;
damage->tail->count += n;
damage->remain -= n;
if (count == 0)
return damage;
size = 2 * damage->tail->size;
if (size < count)
size = (count + 64) & ~63;
chunk = _cairo_malloc (sizeof (*chunk) + sizeof (cairo_box_t) * size);
if (unlikely (chunk == NULL)) {
_cairo_damage_destroy (damage);
return (cairo_damage_t *) &__cairo_damage__nil;
}
chunk->next = NULL;
chunk->base = (cairo_box_t *) (chunk + 1);
chunk->size = size;
chunk->count = count;
damage->tail->next = chunk;
damage->tail = chunk;
memcpy (damage->tail->base, boxes + n,
count * sizeof (cairo_box_t));
damage->remain = size - count;
return damage;
}
cairo_damage_t *
_cairo_damage_add_box(cairo_damage_t *damage,
const cairo_box_t *box)
{
TRACE ((stderr, "%s: (%d, %d),(%d, %d)\n", __FUNCTION__,
box->p1.x, box->p1.y, box->p2.x, box->p2.y));
return _cairo_damage_add_boxes(damage, box, 1);
}
cairo_damage_t *
_cairo_damage_add_rectangle(cairo_damage_t *damage,
const cairo_rectangle_int_t *r)
{
cairo_box_t box;
TRACE ((stderr, "%s: (%d, %d)x(%d, %d)\n", __FUNCTION__,
r->x, r->y, r->width, r->height));
box.p1.x = r->x;
box.p1.y = r->y;
box.p2.x = r->x + r->width;
box.p2.y = r->y + r->height;
return _cairo_damage_add_boxes(damage, &box, 1);
}
cairo_damage_t *
_cairo_damage_add_region (cairo_damage_t *damage,
const cairo_region_t *region)
{
cairo_box_t *boxes;
int nbox;
TRACE ((stderr, "%s\n", __FUNCTION__));
boxes = _cairo_region_get_boxes (region, &nbox);
return _cairo_damage_add_boxes(damage, boxes, nbox);
}
cairo_damage_t *
_cairo_damage_reduce (cairo_damage_t *damage)
{
cairo_box_t *free_boxes = NULL;
cairo_box_t *boxes, *b;
struct _cairo_damage_chunk *chunk, *last;
TRACE ((stderr, "%s: dirty=%d\n", __FUNCTION__,
damage ? damage->dirty : -1));
if (damage == NULL || damage->status || !damage->dirty)
return damage;
if (damage->region) {
cairo_region_t *region;
region = damage->region;
damage->region = NULL;
damage = _cairo_damage_add_region (damage, region);
cairo_region_destroy (region);
if (unlikely (damage->status))
return damage;
}
boxes = damage->tail->base;
if (damage->dirty > damage->tail->size) {
boxes = free_boxes = _cairo_malloc (damage->dirty * sizeof (cairo_box_t));
if (unlikely (boxes == NULL)) {
_cairo_damage_destroy (damage);
return (cairo_damage_t *) &__cairo_damage__nil;
}
b = boxes;
last = NULL;
} else {
b = boxes + damage->tail->count;
last = damage->tail;
}
for (chunk = &damage->chunks; chunk != last; chunk = chunk->next) {
memcpy (b, chunk->base, chunk->count * sizeof (cairo_box_t));
b += chunk->count;
}
damage->region = _cairo_region_create_from_boxes (boxes, damage->dirty);
free (free_boxes);
if (unlikely (damage->region->status)) {
_cairo_damage_destroy (damage);
return (cairo_damage_t *) &__cairo_damage__nil;
}
damage->dirty = 0;
return damage;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-debug.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
*/
#include "cairoint.h"
#include "cairo-image-surface-private.h"
/**
* cairo_debug_reset_static_data:
*
* Resets all static data within cairo to its original state,
* (ie. identical to the state at the time of program invocation). For
* example, all caches within cairo will be flushed empty.
*
* This function is intended to be useful when using memory-checking
* tools such as valgrind. When valgrind's memcheck analyzes a
* cairo-using program without a call to cairo_debug_reset_static_data(),
* it will report all data reachable via cairo's static objects as
* "still reachable". Calling cairo_debug_reset_static_data() just prior
* to program termination will make it easier to get squeaky clean
* reports from valgrind.
*
* WARNING: It is only safe to call this function when there are no
* active cairo objects remaining, (ie. the appropriate destroy
* functions have been called as necessary). If there are active cairo
* objects, this call is likely to cause a crash, (eg. an assertion
* failure due to a hash table being destroyed when non-empty).
*
* Since: 1.0
**/
void
cairo_debug_reset_static_data (void)
{
CAIRO_MUTEX_INITIALIZE ();
_cairo_scaled_font_map_destroy ();
_cairo_toy_font_face_reset_static_data ();
#if CAIRO_HAS_FT_FONT
_cairo_ft_font_reset_static_data ();
#endif
#if CAIRO_HAS_WIN32_FONT
_cairo_win32_font_reset_static_data ();
#endif
_cairo_intern_string_reset_static_data ();
_cairo_scaled_font_reset_static_data ();
_cairo_pattern_reset_static_data ();
_cairo_clip_reset_static_data ();
_cairo_image_reset_static_data ();
_cairo_image_compositor_reset_static_data ();
#if CAIRO_HAS_DRM_SURFACE
_cairo_drm_device_reset_static_data ();
#endif
_cairo_default_context_reset_static_data ();
#if CAIRO_HAS_COGL_SURFACE
_cairo_cogl_context_reset_static_data ();
#endif
CAIRO_MUTEX_FINALIZE ();
}
#if HAVE_VALGRIND
void
_cairo_debug_check_image_surface_is_defined (const cairo_surface_t *surface)
{
const cairo_image_surface_t *image = (cairo_image_surface_t *) surface;
const uint8_t *bits;
int row, width;
if (surface == NULL)
return;
if (! RUNNING_ON_VALGRIND)
return;
bits = image->data;
switch (image->format) {
case CAIRO_FORMAT_A1:
width = (image->width + 7)/8;
break;
case CAIRO_FORMAT_A8:
width = image->width;
break;
case CAIRO_FORMAT_RGB16_565:
width = image->width*2;
break;
case CAIRO_FORMAT_RGB24:
case CAIRO_FORMAT_RGB30:
case CAIRO_FORMAT_ARGB32:
width = image->width*4;
break;
case CAIRO_FORMAT_RGB96F:
width = image->width*12;
break;
case CAIRO_FORMAT_RGBA128F:
width = image->width*16;
break;
case CAIRO_FORMAT_INVALID:
default:
/* XXX compute width from pixman bpp */
return;
}
for (row = 0; row < image->height; row++) {
VALGRIND_CHECK_MEM_IS_DEFINED (bits, width);
/* and then silence any future valgrind warnings */
VALGRIND_MAKE_MEM_DEFINED (bits, width);
bits += image->stride;
}
}
#endif
#if 0
void
_cairo_image_surface_write_to_ppm (cairo_image_surface_t *isurf, const char *fn)
{
char *fmt;
if (isurf->format == CAIRO_FORMAT_ARGB32 || isurf->format == CAIRO_FORMAT_RGB24)
fmt = "P6";
else if (isurf->format == CAIRO_FORMAT_A8)
fmt = "P5";
else
return;
FILE *fp = fopen(fn, "wb");
if (!fp)
return;
fprintf (fp, "%s %d %d 255\n", fmt,isurf->width, isurf->height);
for (int j = 0; j < isurf->height; j++) {
unsigned char *row = isurf->data + isurf->stride * j;
for (int i = 0; i < isurf->width; i++) {
if (isurf->format == CAIRO_FORMAT_ARGB32 || isurf->format == CAIRO_FORMAT_RGB24) {
unsigned char r = *row++;
unsigned char g = *row++;
unsigned char b = *row++;
*row++;
putc(r, fp);
putc(g, fp);
putc(b, fp);
} else {
unsigned char a = *row++;
putc(a, fp);
}
}
}
fclose (fp);
fprintf (stderr, "Wrote %s\n", fn);
}
#endif
static cairo_status_t
_print_move_to (void *closure,
const cairo_point_t *point)
{
fprintf (closure,
" %f %f m",
_cairo_fixed_to_double (point->x),
_cairo_fixed_to_double (point->y));
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_print_line_to (void *closure,
const cairo_point_t *point)
{
fprintf (closure,
" %f %f l",
_cairo_fixed_to_double (point->x),
_cairo_fixed_to_double (point->y));
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_print_curve_to (void *closure,
const cairo_point_t *p1,
const cairo_point_t *p2,
const cairo_point_t *p3)
{
fprintf (closure,
" %f %f %f %f %f %f c",
_cairo_fixed_to_double (p1->x),
_cairo_fixed_to_double (p1->y),
_cairo_fixed_to_double (p2->x),
_cairo_fixed_to_double (p2->y),
_cairo_fixed_to_double (p3->x),
_cairo_fixed_to_double (p3->y));
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_print_close (void *closure)
{
fprintf (closure, " h");
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_debug_print_path (FILE *stream, const cairo_path_fixed_t *path)
{
cairo_status_t status;
cairo_box_t box;
fprintf (stream,
"path: extents=(%f, %f), (%f, %f)\n",
_cairo_fixed_to_double (path->extents.p1.x),
_cairo_fixed_to_double (path->extents.p1.y),
_cairo_fixed_to_double (path->extents.p2.x),
_cairo_fixed_to_double (path->extents.p2.y));
status = _cairo_path_fixed_interpret (path,
_print_move_to,
_print_line_to,
_print_curve_to,
_print_close,
stream);
assert (status == CAIRO_STATUS_SUCCESS);
if (_cairo_path_fixed_is_box (path, &box)) {
fprintf (stream, "[box (%d, %d), (%d, %d)]",
box.p1.x, box.p1.y, box.p2.x, box.p2.y);
}
fprintf (stream, "\n");
}
void
_cairo_debug_print_polygon (FILE *stream, cairo_polygon_t *polygon)
{
int n;
fprintf (stream,
"polygon: extents=(%f, %f), (%f, %f)\n",
_cairo_fixed_to_double (polygon->extents.p1.x),
_cairo_fixed_to_double (polygon->extents.p1.y),
_cairo_fixed_to_double (polygon->extents.p2.x),
_cairo_fixed_to_double (polygon->extents.p2.y));
if (polygon->num_limits) {
fprintf (stream,
" : limit=(%f, %f), (%f, %f) x %d\n",
_cairo_fixed_to_double (polygon->limit.p1.x),
_cairo_fixed_to_double (polygon->limit.p1.y),
_cairo_fixed_to_double (polygon->limit.p2.x),
_cairo_fixed_to_double (polygon->limit.p2.y),
polygon->num_limits);
}
for (n = 0; n < polygon->num_edges; n++) {
cairo_edge_t *edge = &polygon->edges[n];
fprintf (stream,
" [%d] = [(%f, %f), (%f, %f)], top=%f, bottom=%f, dir=%d\n",
n,
_cairo_fixed_to_double (edge->line.p1.x),
_cairo_fixed_to_double (edge->line.p1.y),
_cairo_fixed_to_double (edge->line.p2.x),
_cairo_fixed_to_double (edge->line.p2.y),
_cairo_fixed_to_double (edge->top),
_cairo_fixed_to_double (edge->bottom),
edge->dir);
}
}
void
_cairo_debug_print_matrix (FILE *file, const cairo_matrix_t *matrix)
{
fprintf (file, "[%g %g %g %g %g %g]\n",
matrix->xx, matrix->yx,
matrix->xy, matrix->yy,
matrix->x0, matrix->y0);
}
void
_cairo_debug_print_rect (FILE *file, const cairo_rectangle_int_t *rect)
{
fprintf (file, "x: %d y: %d width: %d height: %d\n",
rect->x, rect->y,
rect->width, rect->height);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-default-context-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@redhat.com>
*/
#ifndef CAIRO_DEFAULT_CONTEXT_PRIVATE_H
#define CAIRO_DEFAULT_CONTEXT_PRIVATE_H
#include "cairo-private.h"
#include "cairo-gstate-private.h"
#include "cairo-path-fixed-private.h"
CAIRO_BEGIN_DECLS
typedef struct _cairo_default_context cairo_default_context_t;
struct _cairo_default_context {
cairo_t base;
cairo_gstate_t *gstate;
cairo_gstate_t gstate_tail[2];
cairo_gstate_t *gstate_freelist;
cairo_path_fixed_t path[1];
};
cairo_private cairo_t *
_cairo_default_context_create (void *target);
cairo_private cairo_status_t
_cairo_default_context_init (cairo_default_context_t *cr, void *target);
cairo_private void
_cairo_default_context_fini (cairo_default_context_t *cr);
CAIRO_END_DECLS
#endif /* CAIRO_DEFAULT_CONTEXT_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-default-context.c | /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-private.h"
#include "cairo-arc-private.h"
#include "cairo-backend-private.h"
#include "cairo-clip-inline.h"
#include "cairo-default-context-private.h"
#include "cairo-error-private.h"
#include "cairo-freed-pool-private.h"
#include "cairo-path-private.h"
#include "cairo-pattern-private.h"
#define CAIRO_TOLERANCE_MINIMUM _cairo_fixed_to_double(1)
#if !defined(INFINITY)
#define INFINITY HUGE_VAL
#endif
static freed_pool_t context_pool;
void
_cairo_default_context_reset_static_data (void)
{
_freed_pool_reset (&context_pool);
}
void
_cairo_default_context_fini (cairo_default_context_t *cr)
{
while (cr->gstate != &cr->gstate_tail[0]) {
if (_cairo_gstate_restore (&cr->gstate, &cr->gstate_freelist))
break;
}
_cairo_gstate_fini (cr->gstate);
cr->gstate_freelist = cr->gstate_freelist->next; /* skip over tail[1] */
while (cr->gstate_freelist != NULL) {
cairo_gstate_t *gstate = cr->gstate_freelist;
cr->gstate_freelist = gstate->next;
free (gstate);
}
_cairo_path_fixed_fini (cr->path);
_cairo_fini (&cr->base);
}
static void
_cairo_default_context_destroy (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_default_context_fini (cr);
/* mark the context as invalid to protect against misuse */
cr->base.status = CAIRO_STATUS_NULL_POINTER;
_freed_pool_put (&context_pool, cr);
}
static cairo_surface_t *
_cairo_default_context_get_original_target (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_original_target (cr->gstate);
}
static cairo_surface_t *
_cairo_default_context_get_current_target (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_target (cr->gstate);
}
static cairo_status_t
_cairo_default_context_save (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_save (&cr->gstate, &cr->gstate_freelist);
}
static cairo_status_t
_cairo_default_context_restore (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
if (unlikely (_cairo_gstate_is_group (cr->gstate)))
return _cairo_error (CAIRO_STATUS_INVALID_RESTORE);
return _cairo_gstate_restore (&cr->gstate, &cr->gstate_freelist);
}
static cairo_status_t
_cairo_default_context_push_group (void *abstract_cr, cairo_content_t content)
{
cairo_default_context_t *cr = abstract_cr;
cairo_surface_t *group_surface;
cairo_clip_t *clip;
cairo_status_t status;
clip = _cairo_gstate_get_clip (cr->gstate);
if (_cairo_clip_is_all_clipped (clip)) {
group_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
status = group_surface->status;
if (unlikely (status))
goto bail;
} else {
cairo_surface_t *parent_surface;
cairo_rectangle_int_t extents;
cairo_bool_t bounded, is_empty;
parent_surface = _cairo_gstate_get_target (cr->gstate);
if (unlikely (parent_surface->status))
return parent_surface->status;
if (unlikely (parent_surface->finished))
return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
/* Get the extents that we'll use in creating our new group surface */
bounded = _cairo_surface_get_extents (parent_surface, &extents);
if (clip)
/* XXX: This assignment just fixes a compiler warning? */
is_empty = _cairo_rectangle_intersect (&extents,
_cairo_clip_get_extents (clip));
if (!bounded) {
/* XXX: Generic solution? */
group_surface = cairo_recording_surface_create (content, NULL);
extents.x = extents.y = 0;
} else {
group_surface = _cairo_surface_create_scratch (parent_surface,
content,
extents.width,
extents.height,
CAIRO_COLOR_TRANSPARENT);
}
status = group_surface->status;
if (unlikely (status))
goto bail;
/* Set device offsets on the new surface so that logically it appears at
* the same location on the parent surface -- when we pop_group this,
* the source pattern will get fixed up for the appropriate target surface
* device offsets, so we want to set our own surface offsets from /that/,
* and not from the device origin. */
cairo_surface_set_device_offset (group_surface,
parent_surface->device_transform.x0 - extents.x,
parent_surface->device_transform.y0 - extents.y);
cairo_surface_set_device_scale (group_surface,
parent_surface->device_transform.xx,
parent_surface->device_transform.yy);
/* If we have a current path, we need to adjust it to compensate for
* the device offset just applied. */
_cairo_path_fixed_translate (cr->path,
_cairo_fixed_from_int (-extents.x),
_cairo_fixed_from_int (-extents.y));
}
/* create a new gstate for the redirect */
status = _cairo_gstate_save (&cr->gstate, &cr->gstate_freelist);
if (unlikely (status))
goto bail;
status = _cairo_gstate_redirect_target (cr->gstate, group_surface);
bail:
cairo_surface_destroy (group_surface);
return status;
}
static cairo_pattern_t *
_cairo_default_context_pop_group (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
cairo_surface_t *group_surface;
cairo_pattern_t *group_pattern;
cairo_surface_t *parent_surface;
cairo_matrix_t group_matrix;
cairo_status_t status;
/* Verify that we are at the right nesting level */
if (unlikely (! _cairo_gstate_is_group (cr->gstate)))
return _cairo_pattern_create_in_error (CAIRO_STATUS_INVALID_POP_GROUP);
/* Get a reference to the active surface before restoring */
group_surface = _cairo_gstate_get_target (cr->gstate);
group_surface = cairo_surface_reference (group_surface);
status = _cairo_gstate_restore (&cr->gstate, &cr->gstate_freelist);
assert (status == CAIRO_STATUS_SUCCESS);
parent_surface = _cairo_gstate_get_target (cr->gstate);
group_pattern = cairo_pattern_create_for_surface (group_surface);
status = group_pattern->status;
if (unlikely (status))
goto done;
_cairo_gstate_get_matrix (cr->gstate, &group_matrix);
cairo_pattern_set_matrix (group_pattern, &group_matrix);
/* If we have a current path, we need to adjust it to compensate for
* the device offset just removed. */
_cairo_path_fixed_translate (cr->path,
_cairo_fixed_from_int (parent_surface->device_transform.x0 - group_surface->device_transform.x0),
_cairo_fixed_from_int (parent_surface->device_transform.y0 - group_surface->device_transform.y0));
done:
cairo_surface_destroy (group_surface);
return group_pattern;
}
static cairo_status_t
_cairo_default_context_set_source (void *abstract_cr,
cairo_pattern_t *source)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_source (cr->gstate, source);
}
static cairo_bool_t
_current_source_matches_solid (const cairo_pattern_t *pattern,
double red,
double green,
double blue,
double alpha)
{
cairo_color_t color;
if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
return FALSE;
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
alpha = _cairo_restrict_value (alpha, 0.0, 1.0);
_cairo_color_init_rgba (&color, red, green, blue, alpha);
return _cairo_color_equal (&color,
&((cairo_solid_pattern_t *) pattern)->color);
}
static cairo_status_t
_cairo_default_context_set_source_rgba (void *abstract_cr, double red, double green, double blue, double alpha)
{
cairo_default_context_t *cr = abstract_cr;
cairo_pattern_t *pattern;
cairo_status_t status;
if (_current_source_matches_solid (cr->gstate->source,
red, green, blue, alpha))
return CAIRO_STATUS_SUCCESS;
/* push the current pattern to the freed lists */
_cairo_default_context_set_source (cr, (cairo_pattern_t *) &_cairo_pattern_black);
pattern = cairo_pattern_create_rgba (red, green, blue, alpha);
if (unlikely (pattern->status))
return pattern->status;
status = _cairo_default_context_set_source (cr, pattern);
cairo_pattern_destroy (pattern);
return status;
}
static cairo_status_t
_cairo_default_context_set_source_surface (void *abstract_cr,
cairo_surface_t *surface,
double x,
double y)
{
cairo_default_context_t *cr = abstract_cr;
cairo_pattern_t *pattern;
cairo_matrix_t matrix;
cairo_status_t status;
/* push the current pattern to the freed lists */
_cairo_default_context_set_source (cr, (cairo_pattern_t *) &_cairo_pattern_black);
pattern = cairo_pattern_create_for_surface (surface);
if (unlikely (pattern->status))
return pattern->status;
cairo_matrix_init_translate (&matrix, -x, -y);
cairo_pattern_set_matrix (pattern, &matrix);
status = _cairo_default_context_set_source (cr, pattern);
cairo_pattern_destroy (pattern);
return status;
}
static cairo_pattern_t *
_cairo_default_context_get_source (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_source (cr->gstate);
}
static cairo_status_t
_cairo_default_context_set_tolerance (void *abstract_cr,
double tolerance)
{
cairo_default_context_t *cr = abstract_cr;
if (tolerance < CAIRO_TOLERANCE_MINIMUM)
tolerance = CAIRO_TOLERANCE_MINIMUM;
return _cairo_gstate_set_tolerance (cr->gstate, tolerance);
}
static cairo_status_t
_cairo_default_context_set_operator (void *abstract_cr, cairo_operator_t op)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_operator (cr->gstate, op);
}
static cairo_status_t
_cairo_default_context_set_opacity (void *abstract_cr, double opacity)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_opacity (cr->gstate, opacity);
}
static cairo_status_t
_cairo_default_context_set_antialias (void *abstract_cr,
cairo_antialias_t antialias)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_antialias (cr->gstate, antialias);
}
static cairo_status_t
_cairo_default_context_set_fill_rule (void *abstract_cr,
cairo_fill_rule_t fill_rule)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_fill_rule (cr->gstate, fill_rule);
}
static cairo_status_t
_cairo_default_context_set_line_width (void *abstract_cr,
double line_width)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_line_width (cr->gstate, line_width);
}
static cairo_status_t
_cairo_default_context_set_line_cap (void *abstract_cr,
cairo_line_cap_t line_cap)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_line_cap (cr->gstate, line_cap);
}
static cairo_status_t
_cairo_default_context_set_line_join (void *abstract_cr,
cairo_line_join_t line_join)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_line_join (cr->gstate, line_join);
}
static cairo_status_t
_cairo_default_context_set_dash (void *abstract_cr,
const double *dashes,
int num_dashes,
double offset)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_dash (cr->gstate,
dashes, num_dashes, offset);
}
static cairo_status_t
_cairo_default_context_set_miter_limit (void *abstract_cr,
double limit)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_miter_limit (cr->gstate, limit);
}
static cairo_antialias_t
_cairo_default_context_get_antialias (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_antialias (cr->gstate);
}
static void
_cairo_default_context_get_dash (void *abstract_cr,
double *dashes,
int *num_dashes,
double *offset)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_get_dash (cr->gstate, dashes, num_dashes, offset);
}
static cairo_fill_rule_t
_cairo_default_context_get_fill_rule (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_fill_rule (cr->gstate);
}
static double
_cairo_default_context_get_line_width (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_line_width (cr->gstate);
}
static cairo_line_cap_t
_cairo_default_context_get_line_cap (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_line_cap (cr->gstate);
}
static cairo_line_join_t
_cairo_default_context_get_line_join (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_line_join (cr->gstate);
}
static double
_cairo_default_context_get_miter_limit (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_miter_limit (cr->gstate);
}
static cairo_operator_t
_cairo_default_context_get_operator (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_operator (cr->gstate);
}
static double
_cairo_default_context_get_opacity (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_opacity (cr->gstate);
}
static double
_cairo_default_context_get_tolerance (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_tolerance (cr->gstate);
}
/* Current transformation matrix */
static cairo_status_t
_cairo_default_context_translate (void *abstract_cr,
double tx,
double ty)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_translate (cr->gstate, tx, ty);
}
static cairo_status_t
_cairo_default_context_scale (void *abstract_cr,
double sx,
double sy)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_scale (cr->gstate, sx, sy);
}
static cairo_status_t
_cairo_default_context_rotate (void *abstract_cr,
double theta)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_rotate (cr->gstate, theta);
}
static cairo_status_t
_cairo_default_context_transform (void *abstract_cr,
const cairo_matrix_t *matrix)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_transform (cr->gstate, matrix);
}
static cairo_status_t
_cairo_default_context_set_matrix (void *abstract_cr,
const cairo_matrix_t *matrix)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_matrix (cr->gstate, matrix);
}
static cairo_status_t
_cairo_default_context_set_identity_matrix (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_identity_matrix (cr->gstate);
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_default_context_get_matrix (void *abstract_cr,
cairo_matrix_t *matrix)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_get_matrix (cr->gstate, matrix);
}
static void
_cairo_default_context_user_to_device (void *abstract_cr,
double *x,
double *y)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_user_to_device (cr->gstate, x, y);
}
static void
_cairo_default_context_user_to_device_distance (void *abstract_cr, double *dx, double *dy)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_user_to_device_distance (cr->gstate, dx, dy);
}
static void
_cairo_default_context_device_to_user (void *abstract_cr,
double *x,
double *y)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_device_to_user (cr->gstate, x, y);
}
static void
_cairo_default_context_device_to_user_distance (void *abstract_cr,
double *dx,
double *dy)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_device_to_user_distance (cr->gstate, dx, dy);
}
static void
_cairo_default_context_backend_to_user (void *abstract_cr,
double *x,
double *y)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_backend_to_user (cr->gstate, x, y);
}
static void
_cairo_default_context_backend_to_user_distance (void *abstract_cr, double *dx, double *dy)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_backend_to_user_distance (cr->gstate, dx, dy);
}
static void
_cairo_default_context_user_to_backend (void *abstract_cr,
double *x,
double *y)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_user_to_backend (cr->gstate, x, y);
}
static void
_cairo_default_context_user_to_backend_distance (void *abstract_cr,
double *dx,
double *dy)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_user_to_backend_distance (cr->gstate, dx, dy);
}
/* Path constructor */
static cairo_status_t
_cairo_default_context_new_path (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_path_fixed_fini (cr->path);
_cairo_path_fixed_init (cr->path);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_default_context_new_sub_path (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_path_fixed_new_sub_path (cr->path);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_default_context_move_to (void *abstract_cr, double x, double y)
{
cairo_default_context_t *cr = abstract_cr;
cairo_fixed_t x_fixed, y_fixed;
_cairo_gstate_user_to_backend (cr->gstate, &x, &y);
x_fixed = _cairo_fixed_from_double (x);
y_fixed = _cairo_fixed_from_double (y);
return _cairo_path_fixed_move_to (cr->path, x_fixed, y_fixed);
}
static cairo_status_t
_cairo_default_context_line_to (void *abstract_cr, double x, double y)
{
cairo_default_context_t *cr = abstract_cr;
cairo_fixed_t x_fixed, y_fixed;
_cairo_gstate_user_to_backend (cr->gstate, &x, &y);
x_fixed = _cairo_fixed_from_double (x);
y_fixed = _cairo_fixed_from_double (y);
return _cairo_path_fixed_line_to (cr->path, x_fixed, y_fixed);
}
static cairo_status_t
_cairo_default_context_curve_to (void *abstract_cr,
double x1, double y1,
double x2, double y2,
double x3, double y3)
{
cairo_default_context_t *cr = abstract_cr;
cairo_fixed_t x1_fixed, y1_fixed;
cairo_fixed_t x2_fixed, y2_fixed;
cairo_fixed_t x3_fixed, y3_fixed;
_cairo_gstate_user_to_backend (cr->gstate, &x1, &y1);
_cairo_gstate_user_to_backend (cr->gstate, &x2, &y2);
_cairo_gstate_user_to_backend (cr->gstate, &x3, &y3);
x1_fixed = _cairo_fixed_from_double (x1);
y1_fixed = _cairo_fixed_from_double (y1);
x2_fixed = _cairo_fixed_from_double (x2);
y2_fixed = _cairo_fixed_from_double (y2);
x3_fixed = _cairo_fixed_from_double (x3);
y3_fixed = _cairo_fixed_from_double (y3);
return _cairo_path_fixed_curve_to (cr->path,
x1_fixed, y1_fixed,
x2_fixed, y2_fixed,
x3_fixed, y3_fixed);
}
static cairo_status_t
_cairo_default_context_arc (void *abstract_cr,
double xc, double yc, double radius,
double angle1, double angle2,
cairo_bool_t forward)
{
cairo_default_context_t *cr = abstract_cr;
cairo_status_t status;
/* Do nothing, successfully, if radius is <= 0 */
if (radius <= 0.0) {
cairo_fixed_t x_fixed, y_fixed;
_cairo_gstate_user_to_backend (cr->gstate, &xc, &yc);
x_fixed = _cairo_fixed_from_double (xc);
y_fixed = _cairo_fixed_from_double (yc);
status = _cairo_path_fixed_line_to (cr->path, x_fixed, y_fixed);
if (unlikely (status))
return status;
status = _cairo_path_fixed_line_to (cr->path, x_fixed, y_fixed);
if (unlikely (status))
return status;
return CAIRO_STATUS_SUCCESS;
}
status = _cairo_default_context_line_to (cr,
xc + radius * cos (angle1),
yc + radius * sin (angle1));
if (unlikely (status))
return status;
if (forward)
_cairo_arc_path (&cr->base, xc, yc, radius, angle1, angle2);
else
_cairo_arc_path_negative (&cr->base, xc, yc, radius, angle1, angle2);
return CAIRO_STATUS_SUCCESS; /* any error will have already been set on cr */
}
static cairo_status_t
_cairo_default_context_rel_move_to (void *abstract_cr, double dx, double dy)
{
cairo_default_context_t *cr = abstract_cr;
cairo_fixed_t dx_fixed, dy_fixed;
_cairo_gstate_user_to_backend_distance (cr->gstate, &dx, &dy);
dx_fixed = _cairo_fixed_from_double (dx);
dy_fixed = _cairo_fixed_from_double (dy);
return _cairo_path_fixed_rel_move_to (cr->path, dx_fixed, dy_fixed);
}
static cairo_status_t
_cairo_default_context_rel_line_to (void *abstract_cr, double dx, double dy)
{
cairo_default_context_t *cr = abstract_cr;
cairo_fixed_t dx_fixed, dy_fixed;
_cairo_gstate_user_to_backend_distance (cr->gstate, &dx, &dy);
dx_fixed = _cairo_fixed_from_double (dx);
dy_fixed = _cairo_fixed_from_double (dy);
return _cairo_path_fixed_rel_line_to (cr->path, dx_fixed, dy_fixed);
}
static cairo_status_t
_cairo_default_context_rel_curve_to (void *abstract_cr,
double dx1, double dy1,
double dx2, double dy2,
double dx3, double dy3)
{
cairo_default_context_t *cr = abstract_cr;
cairo_fixed_t dx1_fixed, dy1_fixed;
cairo_fixed_t dx2_fixed, dy2_fixed;
cairo_fixed_t dx3_fixed, dy3_fixed;
_cairo_gstate_user_to_backend_distance (cr->gstate, &dx1, &dy1);
_cairo_gstate_user_to_backend_distance (cr->gstate, &dx2, &dy2);
_cairo_gstate_user_to_backend_distance (cr->gstate, &dx3, &dy3);
dx1_fixed = _cairo_fixed_from_double (dx1);
dy1_fixed = _cairo_fixed_from_double (dy1);
dx2_fixed = _cairo_fixed_from_double (dx2);
dy2_fixed = _cairo_fixed_from_double (dy2);
dx3_fixed = _cairo_fixed_from_double (dx3);
dy3_fixed = _cairo_fixed_from_double (dy3);
return _cairo_path_fixed_rel_curve_to (cr->path,
dx1_fixed, dy1_fixed,
dx2_fixed, dy2_fixed,
dx3_fixed, dy3_fixed);
}
static cairo_status_t
_cairo_default_context_close_path (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_path_fixed_close_path (cr->path);
}
static cairo_status_t
_cairo_default_context_rectangle (void *abstract_cr,
double x, double y,
double width, double height)
{
cairo_default_context_t *cr = abstract_cr;
cairo_status_t status;
status = _cairo_default_context_move_to (cr, x, y);
if (unlikely (status))
return status;
status = _cairo_default_context_rel_line_to (cr, width, 0);
if (unlikely (status))
return status;
status = _cairo_default_context_rel_line_to (cr, 0, height);
if (unlikely (status))
return status;
status = _cairo_default_context_rel_line_to (cr, -width, 0);
if (unlikely (status))
return status;
return _cairo_default_context_close_path (cr);
}
static void
_cairo_default_context_path_extents (void *abstract_cr,
double *x1,
double *y1,
double *x2,
double *y2)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_path_extents (cr->gstate,
cr->path,
x1, y1, x2, y2);
}
static cairo_bool_t
_cairo_default_context_has_current_point (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return cr->path->has_current_point;
}
static cairo_bool_t
_cairo_default_context_get_current_point (void *abstract_cr,
double *x,
double *y)
{
cairo_default_context_t *cr = abstract_cr;
cairo_fixed_t x_fixed, y_fixed;
if (_cairo_path_fixed_get_current_point (cr->path, &x_fixed, &y_fixed))
{
*x = _cairo_fixed_to_double (x_fixed);
*y = _cairo_fixed_to_double (y_fixed);
_cairo_gstate_backend_to_user (cr->gstate, x, y);
return TRUE;
}
else
{
return FALSE;
}
}
static cairo_path_t *
_cairo_default_context_copy_path (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_path_create (cr->path, &cr->base);
}
static cairo_path_t *
_cairo_default_context_copy_path_flat (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_path_create_flat (cr->path, &cr->base);
}
static cairo_status_t
_cairo_default_context_append_path (void *abstract_cr,
const cairo_path_t *path)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_path_append_to_context (path, &cr->base);
}
static cairo_status_t
_cairo_default_context_paint (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_paint (cr->gstate);
}
static cairo_status_t
_cairo_default_context_paint_with_alpha (void *abstract_cr,
double alpha)
{
cairo_default_context_t *cr = abstract_cr;
cairo_solid_pattern_t pattern;
cairo_status_t status;
cairo_color_t color;
if (CAIRO_ALPHA_IS_OPAQUE (alpha))
return _cairo_gstate_paint (cr->gstate);
if (CAIRO_ALPHA_IS_ZERO (alpha) &&
_cairo_operator_bounded_by_mask (cr->gstate->op)) {
return CAIRO_STATUS_SUCCESS;
}
_cairo_color_init_rgba (&color, 0., 0., 0., alpha);
_cairo_pattern_init_solid (&pattern, &color);
status = _cairo_gstate_mask (cr->gstate, &pattern.base);
_cairo_pattern_fini (&pattern.base);
return status;
}
static cairo_status_t
_cairo_default_context_mask (void *abstract_cr,
cairo_pattern_t *mask)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_mask (cr->gstate, mask);
}
static cairo_status_t
_cairo_default_context_stroke_preserve (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_stroke (cr->gstate, cr->path);
}
static cairo_status_t
_cairo_default_context_stroke (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
cairo_status_t status;
status = _cairo_gstate_stroke (cr->gstate, cr->path);
if (unlikely (status))
return status;
return _cairo_default_context_new_path (cr);
}
static cairo_status_t
_cairo_default_context_in_stroke (void *abstract_cr,
double x, double y,
cairo_bool_t *inside)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_in_stroke (cr->gstate,
cr->path,
x, y,
inside);
}
static cairo_status_t
_cairo_default_context_stroke_extents (void *abstract_cr,
double *x1, double *y1, double *x2, double *y2)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_stroke_extents (cr->gstate,
cr->path,
x1, y1, x2, y2);
}
static cairo_status_t
_cairo_default_context_fill_preserve (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_fill (cr->gstate, cr->path);
}
static cairo_status_t
_cairo_default_context_fill (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
cairo_status_t status;
status = _cairo_gstate_fill (cr->gstate, cr->path);
if (unlikely (status))
return status;
return _cairo_default_context_new_path (cr);
}
static cairo_status_t
_cairo_default_context_in_fill (void *abstract_cr,
double x, double y,
cairo_bool_t *inside)
{
cairo_default_context_t *cr = abstract_cr;
*inside = _cairo_gstate_in_fill (cr->gstate,
cr->path,
x, y);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_default_context_fill_extents (void *abstract_cr,
double *x1, double *y1, double *x2, double *y2)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_fill_extents (cr->gstate,
cr->path,
x1, y1, x2, y2);
}
static cairo_status_t
_cairo_default_context_clip_preserve (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_clip (cr->gstate, cr->path);
}
static cairo_status_t
_cairo_default_context_clip (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
cairo_status_t status;
status = _cairo_gstate_clip (cr->gstate, cr->path);
if (unlikely (status))
return status;
return _cairo_default_context_new_path (cr);
}
static cairo_status_t
_cairo_default_context_in_clip (void *abstract_cr,
double x, double y,
cairo_bool_t *inside)
{
cairo_default_context_t *cr = abstract_cr;
*inside = _cairo_gstate_in_clip (cr->gstate, x, y);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_default_context_reset_clip (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_reset_clip (cr->gstate);
}
static cairo_status_t
_cairo_default_context_clip_extents (void *abstract_cr,
double *x1, double *y1, double *x2, double *y2)
{
cairo_default_context_t *cr = abstract_cr;
if (! _cairo_gstate_clip_extents (cr->gstate, x1, y1, x2, y2)) {
*x1 = -INFINITY;
*y1 = -INFINITY;
*x2 = +INFINITY;
*y2 = +INFINITY;
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_rectangle_list_t *
_cairo_default_context_copy_clip_rectangle_list (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_copy_clip_rectangle_list (cr->gstate);
}
static cairo_status_t
_cairo_default_context_copy_page (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_copy_page (cr->gstate);
}
static cairo_status_t
_cairo_default_context_tag_begin (void *abstract_cr,
const char *tag_name, const char *attributes)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_tag_begin (cr->gstate, tag_name, attributes);
}
static cairo_status_t
_cairo_default_context_tag_end (void *abstract_cr,
const char *tag_name)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_tag_end (cr->gstate, tag_name);
}
static cairo_status_t
_cairo_default_context_show_page (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_show_page (cr->gstate);
}
static cairo_status_t
_cairo_default_context_set_font_face (void *abstract_cr,
cairo_font_face_t *font_face)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_font_face (cr->gstate, font_face);
}
static cairo_font_face_t *
_cairo_default_context_get_font_face (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
cairo_font_face_t *font_face;
cairo_status_t status;
status = _cairo_gstate_get_font_face (cr->gstate, &font_face);
if (unlikely (status)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_font_face_t *) &_cairo_font_face_nil;
}
return font_face;
}
static cairo_status_t
_cairo_default_context_font_extents (void *abstract_cr,
cairo_font_extents_t *extents)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_get_font_extents (cr->gstate, extents);
}
static cairo_status_t
_cairo_default_context_set_font_size (void *abstract_cr,
double size)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_font_size (cr->gstate, size);
}
static cairo_status_t
_cairo_default_context_set_font_matrix (void *abstract_cr,
const cairo_matrix_t *matrix)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_set_font_matrix (cr->gstate, matrix);
}
static void
_cairo_default_context_get_font_matrix (void *abstract_cr,
cairo_matrix_t *matrix)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_get_font_matrix (cr->gstate, matrix);
}
static cairo_status_t
_cairo_default_context_set_font_options (void *abstract_cr,
const cairo_font_options_t *options)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_set_font_options (cr->gstate, options);
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_default_context_get_font_options (void *abstract_cr,
cairo_font_options_t *options)
{
cairo_default_context_t *cr = abstract_cr;
_cairo_gstate_get_font_options (cr->gstate, options);
}
static cairo_status_t
_cairo_default_context_set_scaled_font (void *abstract_cr,
cairo_scaled_font_t *scaled_font)
{
cairo_default_context_t *cr = abstract_cr;
cairo_bool_t was_previous;
cairo_status_t status;
if (scaled_font == cr->gstate->scaled_font)
return CAIRO_STATUS_SUCCESS;
was_previous = scaled_font == cr->gstate->previous_scaled_font;
status = _cairo_gstate_set_font_face (cr->gstate, scaled_font->font_face);
if (unlikely (status))
return status;
status = _cairo_gstate_set_font_matrix (cr->gstate, &scaled_font->font_matrix);
if (unlikely (status))
return status;
_cairo_gstate_set_font_options (cr->gstate, &scaled_font->options);
if (was_previous)
cr->gstate->scaled_font = cairo_scaled_font_reference (scaled_font);
return CAIRO_STATUS_SUCCESS;
}
static cairo_scaled_font_t *
_cairo_default_context_get_scaled_font (void *abstract_cr)
{
cairo_default_context_t *cr = abstract_cr;
cairo_scaled_font_t *scaled_font;
cairo_status_t status;
status = _cairo_gstate_get_scaled_font (cr->gstate, &scaled_font);
if (unlikely (status))
return _cairo_scaled_font_create_in_error (status);
return scaled_font;
}
static cairo_status_t
_cairo_default_context_glyphs (void *abstract_cr,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_glyph_text_info_t *info)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_show_text_glyphs (cr->gstate, glyphs, num_glyphs, info);
}
static cairo_status_t
_cairo_default_context_glyph_path (void *abstract_cr,
const cairo_glyph_t *glyphs,
int num_glyphs)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_glyph_path (cr->gstate,
glyphs, num_glyphs,
cr->path);
}
static cairo_status_t
_cairo_default_context_glyph_extents (void *abstract_cr,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_text_extents_t *extents)
{
cairo_default_context_t *cr = abstract_cr;
return _cairo_gstate_glyph_extents (cr->gstate, glyphs, num_glyphs, extents);
}
static const cairo_backend_t _cairo_default_context_backend = {
CAIRO_TYPE_DEFAULT,
_cairo_default_context_destroy,
_cairo_default_context_get_original_target,
_cairo_default_context_get_current_target,
_cairo_default_context_save,
_cairo_default_context_restore,
_cairo_default_context_push_group,
_cairo_default_context_pop_group,
_cairo_default_context_set_source_rgba,
_cairo_default_context_set_source_surface,
_cairo_default_context_set_source,
_cairo_default_context_get_source,
_cairo_default_context_set_antialias,
_cairo_default_context_set_dash,
_cairo_default_context_set_fill_rule,
_cairo_default_context_set_line_cap,
_cairo_default_context_set_line_join,
_cairo_default_context_set_line_width,
_cairo_default_context_set_miter_limit,
_cairo_default_context_set_opacity,
_cairo_default_context_set_operator,
_cairo_default_context_set_tolerance,
_cairo_default_context_get_antialias,
_cairo_default_context_get_dash,
_cairo_default_context_get_fill_rule,
_cairo_default_context_get_line_cap,
_cairo_default_context_get_line_join,
_cairo_default_context_get_line_width,
_cairo_default_context_get_miter_limit,
_cairo_default_context_get_opacity,
_cairo_default_context_get_operator,
_cairo_default_context_get_tolerance,
_cairo_default_context_translate,
_cairo_default_context_scale,
_cairo_default_context_rotate,
_cairo_default_context_transform,
_cairo_default_context_set_matrix,
_cairo_default_context_set_identity_matrix,
_cairo_default_context_get_matrix,
_cairo_default_context_user_to_device,
_cairo_default_context_user_to_device_distance,
_cairo_default_context_device_to_user,
_cairo_default_context_device_to_user_distance,
_cairo_default_context_user_to_backend,
_cairo_default_context_user_to_backend_distance,
_cairo_default_context_backend_to_user,
_cairo_default_context_backend_to_user_distance,
_cairo_default_context_new_path,
_cairo_default_context_new_sub_path,
_cairo_default_context_move_to,
_cairo_default_context_rel_move_to,
_cairo_default_context_line_to,
_cairo_default_context_rel_line_to,
_cairo_default_context_curve_to,
_cairo_default_context_rel_curve_to,
NULL, /* arc-to */
NULL, /* rel-arc-to */
_cairo_default_context_close_path,
_cairo_default_context_arc,
_cairo_default_context_rectangle,
_cairo_default_context_path_extents,
_cairo_default_context_has_current_point,
_cairo_default_context_get_current_point,
_cairo_default_context_copy_path,
_cairo_default_context_copy_path_flat,
_cairo_default_context_append_path,
NULL, /* stroke-to-path */
_cairo_default_context_clip,
_cairo_default_context_clip_preserve,
_cairo_default_context_in_clip,
_cairo_default_context_clip_extents,
_cairo_default_context_reset_clip,
_cairo_default_context_copy_clip_rectangle_list,
_cairo_default_context_paint,
_cairo_default_context_paint_with_alpha,
_cairo_default_context_mask,
_cairo_default_context_stroke,
_cairo_default_context_stroke_preserve,
_cairo_default_context_in_stroke,
_cairo_default_context_stroke_extents,
_cairo_default_context_fill,
_cairo_default_context_fill_preserve,
_cairo_default_context_in_fill,
_cairo_default_context_fill_extents,
_cairo_default_context_set_font_face,
_cairo_default_context_get_font_face,
_cairo_default_context_set_font_size,
_cairo_default_context_set_font_matrix,
_cairo_default_context_get_font_matrix,
_cairo_default_context_set_font_options,
_cairo_default_context_get_font_options,
_cairo_default_context_set_scaled_font,
_cairo_default_context_get_scaled_font,
_cairo_default_context_font_extents,
_cairo_default_context_glyphs,
_cairo_default_context_glyph_path,
_cairo_default_context_glyph_extents,
_cairo_default_context_copy_page,
_cairo_default_context_show_page,
_cairo_default_context_tag_begin,
_cairo_default_context_tag_end,
};
cairo_status_t
_cairo_default_context_init (cairo_default_context_t *cr, void *target)
{
_cairo_init (&cr->base, &_cairo_default_context_backend);
_cairo_path_fixed_init (cr->path);
cr->gstate = &cr->gstate_tail[0];
cr->gstate_freelist = &cr->gstate_tail[1];
cr->gstate_tail[1].next = NULL;
return _cairo_gstate_init (cr->gstate, target);
}
cairo_t *
_cairo_default_context_create (void *target)
{
cairo_default_context_t *cr;
cairo_status_t status;
cr = _freed_pool_get (&context_pool);
if (unlikely (cr == NULL)) {
cr = _cairo_malloc (sizeof (cairo_default_context_t));
if (unlikely (cr == NULL))
return _cairo_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
status = _cairo_default_context_init (cr, target);
if (unlikely (status)) {
_freed_pool_put (&context_pool, cr);
return _cairo_create_in_error (status);
}
return &cr->base;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-deprecated.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2006 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
*/
#ifndef CAIRO_DEPRECATED_H
#define CAIRO_DEPRECATED_H
#define CAIRO_FONT_TYPE_ATSUI CAIRO_FONT_TYPE_QUARTZ
/* Obsolete functions. These definitions exist to coerce the compiler
* into providing a little bit of guidance with its error
* messages. The idea is to help users port their old code without
* having to dig through lots of documentation.
*
* The first set of REPLACED_BY functions is for functions whose names
* have just been changed. So fixing these up is mechanical, (and
* automated by means of the cairo/util/cairo-api-update script.
*
* The second set of DEPRECATED_BY functions is for functions where
* the replacement is used in a different way, (ie. different
* arguments, multiple functions instead of one, etc). Fixing these up
* will require a bit more work on the user's part, (and hopefully we
* can get cairo-api-update to find these and print some guiding
* information).
*/
#define cairo_current_font_extents cairo_current_font_extents_REPLACED_BY_cairo_font_extents
#define cairo_get_font_extents cairo_get_font_extents_REPLACED_BY_cairo_font_extents
#define cairo_current_operator cairo_current_operator_REPLACED_BY_cairo_get_operator
#define cairo_current_tolerance cairo_current_tolerance_REPLACED_BY_cairo_get_tolerance
#define cairo_current_point cairo_current_point_REPLACED_BY_cairo_get_current_point
#define cairo_current_fill_rule cairo_current_fill_rule_REPLACED_BY_cairo_get_fill_rule
#define cairo_current_line_width cairo_current_line_width_REPLACED_BY_cairo_get_line_width
#define cairo_current_line_cap cairo_current_line_cap_REPLACED_BY_cairo_get_line_cap
#define cairo_current_line_join cairo_current_line_join_REPLACED_BY_cairo_get_line_join
#define cairo_current_miter_limit cairo_current_miter_limit_REPLACED_BY_cairo_get_miter_limit
#define cairo_current_matrix cairo_current_matrix_REPLACED_BY_cairo_get_matrix
#define cairo_current_target_surface cairo_current_target_surface_REPLACED_BY_cairo_get_target
#define cairo_get_status cairo_get_status_REPLACED_BY_cairo_status
#define cairo_concat_matrix cairo_concat_matrix_REPLACED_BY_cairo_transform
#define cairo_scale_font cairo_scale_font_REPLACED_BY_cairo_set_font_size
#define cairo_select_font cairo_select_font_REPLACED_BY_cairo_select_font_face
#define cairo_transform_font cairo_transform_font_REPLACED_BY_cairo_set_font_matrix
#define cairo_transform_point cairo_transform_point_REPLACED_BY_cairo_user_to_device
#define cairo_transform_distance cairo_transform_distance_REPLACED_BY_cairo_user_to_device_distance
#define cairo_inverse_transform_point cairo_inverse_transform_point_REPLACED_BY_cairo_device_to_user
#define cairo_inverse_transform_distance cairo_inverse_transform_distance_REPLACED_BY_cairo_device_to_user_distance
#define cairo_init_clip cairo_init_clip_REPLACED_BY_cairo_reset_clip
#define cairo_surface_create_for_image cairo_surface_create_for_image_REPLACED_BY_cairo_image_surface_create_for_data
#define cairo_default_matrix cairo_default_matrix_REPLACED_BY_cairo_identity_matrix
#define cairo_matrix_set_affine cairo_matrix_set_affine_REPLACED_BY_cairo_matrix_init
#define cairo_matrix_set_identity cairo_matrix_set_identity_REPLACED_BY_cairo_matrix_init_identity
#define cairo_pattern_add_color_stop cairo_pattern_add_color_stop_REPLACED_BY_cairo_pattern_add_color_stop_rgba
#define cairo_set_rgb_color cairo_set_rgb_color_REPLACED_BY_cairo_set_source_rgb
#define cairo_set_pattern cairo_set_pattern_REPLACED_BY_cairo_set_source
#define cairo_xlib_surface_create_for_pixmap_with_visual cairo_xlib_surface_create_for_pixmap_with_visual_REPLACED_BY_cairo_xlib_surface_create
#define cairo_xlib_surface_create_for_window_with_visual cairo_xlib_surface_create_for_window_with_visual_REPLACED_BY_cairo_xlib_surface_create
#define cairo_xcb_surface_create_for_pixmap_with_visual cairo_xcb_surface_create_for_pixmap_with_visual_REPLACED_BY_cairo_xcb_surface_create
#define cairo_xcb_surface_create_for_window_with_visual cairo_xcb_surface_create_for_window_with_visual_REPLACED_BY_cairo_xcb_surface_create
#define cairo_ps_surface_set_dpi cairo_ps_surface_set_dpi_REPLACED_BY_cairo_surface_set_fallback_resolution
#define cairo_pdf_surface_set_dpi cairo_pdf_surface_set_dpi_REPLACED_BY_cairo_surface_set_fallback_resolution
#define cairo_svg_surface_set_dpi cairo_svg_surface_set_dpi_REPLACED_BY_cairo_surface_set_fallback_resolution
#define cairo_atsui_font_face_create_for_atsu_font_id cairo_atsui_font_face_create_for_atsu_font_id_REPLACED_BY_cairo_quartz_font_face_create_for_atsu_font_id
#define cairo_current_path cairo_current_path_DEPRECATED_BY_cairo_copy_path
#define cairo_current_path_flat cairo_current_path_flat_DEPRECATED_BY_cairo_copy_path_flat
#define cairo_get_path cairo_get_path_DEPRECATED_BY_cairo_copy_path
#define cairo_get_path_flat cairo_get_path_flat_DEPRECATED_BY_cairo_get_path_flat
#define cairo_set_alpha cairo_set_alpha_DEPRECATED_BY_cairo_set_source_rgba_OR_cairo_paint_with_alpha
#define cairo_show_surface cairo_show_surface_DEPRECATED_BY_cairo_set_source_surface_AND_cairo_paint
#define cairo_copy cairo_copy_DEPRECATED_BY_cairo_create_AND_MANY_INDIVIDUAL_FUNCTIONS
#define cairo_surface_set_repeat cairo_surface_set_repeat_DEPRECATED_BY_cairo_pattern_set_extend
#define cairo_surface_set_matrix cairo_surface_set_matrix_DEPRECATED_BY_cairo_pattern_set_matrix
#define cairo_surface_get_matrix cairo_surface_get_matrix_DEPRECATED_BY_cairo_pattern_get_matrix
#define cairo_surface_set_filter cairo_surface_set_filter_DEPRECATED_BY_cairo_pattern_set_filter
#define cairo_surface_get_filter cairo_surface_get_filter_DEPRECATED_BY_cairo_pattern_get_filter
#define cairo_matrix_create cairo_matrix_create_DEPRECATED_BY_cairo_matrix_t
#define cairo_matrix_destroy cairo_matrix_destroy_DEPRECATED_BY_cairo_matrix_t
#define cairo_matrix_copy cairo_matrix_copy_DEPRECATED_BY_cairo_matrix_t
#define cairo_matrix_get_affine cairo_matrix_get_affine_DEPRECATED_BY_cairo_matrix_t
#define cairo_set_target_surface cairo_set_target_surface_DEPRECATED_BY_cairo_create
#define cairo_set_target_image cairo_set_target_image_DEPRECATED_BY_cairo_image_surface_create_for_data
#define cairo_set_target_pdf cairo_set_target_pdf_DEPRECATED_BY_cairo_pdf_surface_create
#define cairo_set_target_png cairo_set_target_png_DEPRECATED_BY_cairo_surface_write_to_png
#define cairo_set_target_ps cairo_set_target_ps_DEPRECATED_BY_cairo_ps_surface_create
#define cairo_set_target_quartz cairo_set_target_quartz_DEPRECATED_BY_cairo_quartz_surface_create
#define cairo_set_target_win32 cairo_set_target_win32_DEPRECATED_BY_cairo_win32_surface_create
#define cairo_set_target_xcb cairo_set_target_xcb_DEPRECATED_BY_cairo_xcb_surface_create
#define cairo_set_target_drawable cairo_set_target_drawable_DEPRECATED_BY_cairo_xlib_surface_create
#define cairo_get_status_string cairo_get_status_string_DEPRECATED_BY_cairo_status_AND_cairo_status_to_string
#define cairo_status_string cairo_status_string_DEPRECATED_BY_cairo_status_AND_cairo_status_to_string
#endif /* CAIRO_DEPRECATED_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-device-private.h | /* Cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Intel Corporation.
*
* Contributors(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#ifndef _CAIRO_DEVICE_PRIVATE_H_
#define _CAIRO_DEVICE_PRIVATE_H_
#include "cairo-compiler-private.h"
#include "cairo-mutex-private.h"
#include "cairo-reference-count-private.h"
#include "cairo-types-private.h"
struct _cairo_device {
cairo_reference_count_t ref_count;
cairo_status_t status;
cairo_user_data_array_t user_data;
const cairo_device_backend_t *backend;
cairo_recursive_mutex_t mutex;
unsigned mutex_depth;
cairo_bool_t finished;
};
struct _cairo_device_backend {
cairo_device_type_t type;
void (*lock) (void *device);
void (*unlock) (void *device);
cairo_warn cairo_status_t (*flush) (void *device);
void (*finish) (void *device);
void (*destroy) (void *device);
};
cairo_private cairo_device_t *
_cairo_device_create_in_error (cairo_status_t status);
cairo_private void
_cairo_device_init (cairo_device_t *device,
const cairo_device_backend_t *backend);
cairo_private cairo_status_t
_cairo_device_set_error (cairo_device_t *device,
cairo_status_t error);
slim_hidden_proto_no_warn (cairo_device_reference);
slim_hidden_proto (cairo_device_acquire);
slim_hidden_proto (cairo_device_release);
slim_hidden_proto (cairo_device_flush);
slim_hidden_proto (cairo_device_finish);
slim_hidden_proto (cairo_device_destroy);
#endif /* _CAIRO_DEVICE_PRIVATE_H_ */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-device.c | /* Cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Intel Corporation.
*
* Contributors(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-device-private.h"
#include "cairo-error-private.h"
/**
* SECTION:cairo-device
* @Title: cairo_device_t
* @Short_Description: interface to underlying rendering system
* @See_Also: #cairo_surface_t
*
* Devices are the abstraction Cairo employs for the rendering system
* used by a #cairo_surface_t. You can get the device of a surface using
* cairo_surface_get_device().
*
* Devices are created using custom functions specific to the rendering
* system you want to use. See the documentation for the surface types
* for those functions.
*
* An important function that devices fulfill is sharing access to the
* rendering system between Cairo and your application. If you want to
* access a device directly that you used to draw to with Cairo, you must
* first call cairo_device_flush() to ensure that Cairo finishes all
* operations on the device and resets it to a clean state.
*
* Cairo also provides the functions cairo_device_acquire() and
* cairo_device_release() to synchronize access to the rendering system
* in a multithreaded environment. This is done internally, but can also
* be used by applications.
*
* Putting this all together, a function that works with devices should
* look something like this:
* <informalexample><programlisting>
* void
* my_device_modifying_function (cairo_device_t *device)
* {
* cairo_status_t status;
*
* // Ensure the device is properly reset
* cairo_device_flush (device);
* // Try to acquire the device
* status = cairo_device_acquire (device);
* if (status != CAIRO_STATUS_SUCCESS) {
* printf ("Failed to acquire the device: %s\n", cairo_status_to_string (status));
* return;
* }
*
* // Do the custom operations on the device here.
* // But do not call any Cairo functions that might acquire devices.
*
* // Release the device when done.
* cairo_device_release (device);
* }
* </programlisting></informalexample>
*
* <note><para>Please refer to the documentation of each backend for
* additional usage requirements, guarantees provided, and
* interactions with existing surface API of the device functions for
* surfaces of that type.
* </para></note>
**/
static const cairo_device_t _nil_device = {
CAIRO_REFERENCE_COUNT_INVALID,
CAIRO_STATUS_NO_MEMORY,
};
static const cairo_device_t _mismatch_device = {
CAIRO_REFERENCE_COUNT_INVALID,
CAIRO_STATUS_DEVICE_TYPE_MISMATCH,
};
static const cairo_device_t _invalid_device = {
CAIRO_REFERENCE_COUNT_INVALID,
CAIRO_STATUS_DEVICE_ERROR,
};
cairo_device_t *
_cairo_device_create_in_error (cairo_status_t status)
{
switch (status) {
case CAIRO_STATUS_NO_MEMORY:
return (cairo_device_t *) &_nil_device;
case CAIRO_STATUS_DEVICE_ERROR:
return (cairo_device_t *) &_invalid_device;
case CAIRO_STATUS_DEVICE_TYPE_MISMATCH:
return (cairo_device_t *) &_mismatch_device;
case CAIRO_STATUS_SUCCESS:
case CAIRO_STATUS_LAST_STATUS:
ASSERT_NOT_REACHED;
/* fall-through */
case CAIRO_STATUS_SURFACE_TYPE_MISMATCH:
case CAIRO_STATUS_INVALID_STATUS:
case CAIRO_STATUS_INVALID_FORMAT:
case CAIRO_STATUS_INVALID_VISUAL:
case CAIRO_STATUS_READ_ERROR:
case CAIRO_STATUS_WRITE_ERROR:
case CAIRO_STATUS_FILE_NOT_FOUND:
case CAIRO_STATUS_TEMP_FILE_ERROR:
case CAIRO_STATUS_INVALID_STRIDE:
case CAIRO_STATUS_INVALID_SIZE:
case CAIRO_STATUS_INVALID_RESTORE:
case CAIRO_STATUS_INVALID_POP_GROUP:
case CAIRO_STATUS_NO_CURRENT_POINT:
case CAIRO_STATUS_INVALID_MATRIX:
case CAIRO_STATUS_NULL_POINTER:
case CAIRO_STATUS_INVALID_STRING:
case CAIRO_STATUS_INVALID_PATH_DATA:
case CAIRO_STATUS_SURFACE_FINISHED:
case CAIRO_STATUS_PATTERN_TYPE_MISMATCH:
case CAIRO_STATUS_INVALID_DASH:
case CAIRO_STATUS_INVALID_DSC_COMMENT:
case CAIRO_STATUS_INVALID_INDEX:
case CAIRO_STATUS_CLIP_NOT_REPRESENTABLE:
case CAIRO_STATUS_FONT_TYPE_MISMATCH:
case CAIRO_STATUS_USER_FONT_IMMUTABLE:
case CAIRO_STATUS_USER_FONT_ERROR:
case CAIRO_STATUS_NEGATIVE_COUNT:
case CAIRO_STATUS_INVALID_CLUSTERS:
case CAIRO_STATUS_INVALID_SLANT:
case CAIRO_STATUS_INVALID_WEIGHT:
case CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED:
case CAIRO_STATUS_INVALID_CONTENT:
case CAIRO_STATUS_INVALID_MESH_CONSTRUCTION:
case CAIRO_STATUS_DEVICE_FINISHED:
case CAIRO_STATUS_JBIG2_GLOBAL_MISSING:
case CAIRO_STATUS_PNG_ERROR:
case CAIRO_STATUS_FREETYPE_ERROR:
case CAIRO_STATUS_WIN32_GDI_ERROR:
case CAIRO_STATUS_TAG_ERROR:
default:
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_device_t *) &_nil_device;
}
}
void
_cairo_device_init (cairo_device_t *device,
const cairo_device_backend_t *backend)
{
CAIRO_REFERENCE_COUNT_INIT (&device->ref_count, 1);
device->status = CAIRO_STATUS_SUCCESS;
device->backend = backend;
CAIRO_RECURSIVE_MUTEX_INIT (device->mutex);
device->mutex_depth = 0;
device->finished = FALSE;
_cairo_user_data_array_init (&device->user_data);
}
/**
* cairo_device_reference:
* @device: a #cairo_device_t
*
* Increases the reference count on @device by one. This prevents
* @device from being destroyed until a matching call to
* cairo_device_destroy() is made.
*
* Use cairo_device_get_reference_count() to get the number of references
* to a #cairo_device_t.
*
* Return value: the referenced #cairo_device_t.
*
* Since: 1.10
**/
cairo_device_t *
cairo_device_reference (cairo_device_t *device)
{
if (device == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&device->ref_count))
{
return device;
}
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&device->ref_count));
_cairo_reference_count_inc (&device->ref_count);
return device;
}
slim_hidden_def (cairo_device_reference);
/**
* cairo_device_status:
* @device: a #cairo_device_t
*
* Checks whether an error has previously occurred for this
* device.
*
* Return value: %CAIRO_STATUS_SUCCESS on success or an error code if
* the device is in an error state.
*
* Since: 1.10
**/
cairo_status_t
cairo_device_status (cairo_device_t *device)
{
if (device == NULL)
return CAIRO_STATUS_NULL_POINTER;
return device->status;
}
/**
* cairo_device_flush:
* @device: a #cairo_device_t
*
* Finish any pending operations for the device and also restore any
* temporary modifications cairo has made to the device's state.
* This function must be called before switching from using the
* device with Cairo to operating on it directly with native APIs.
* If the device doesn't support direct access, then this function
* does nothing.
*
* This function may acquire devices.
*
* Since: 1.10
**/
void
cairo_device_flush (cairo_device_t *device)
{
cairo_status_t status;
if (device == NULL || device->status)
return;
if (device->finished)
return;
if (device->backend->flush != NULL) {
status = device->backend->flush (device);
if (unlikely (status))
status = _cairo_device_set_error (device, status);
}
}
slim_hidden_def (cairo_device_flush);
/**
* cairo_device_finish:
* @device: the #cairo_device_t to finish
*
* This function finishes the device and drops all references to
* external resources. All surfaces, fonts and other objects created
* for this @device will be finished, too.
* Further operations on the @device will not affect the @device but
* will instead trigger a %CAIRO_STATUS_DEVICE_FINISHED error.
*
* When the last call to cairo_device_destroy() decreases the
* reference count to zero, cairo will call cairo_device_finish() if
* it hasn't been called already, before freeing the resources
* associated with the device.
*
* This function may acquire devices.
*
* Since: 1.10
**/
void
cairo_device_finish (cairo_device_t *device)
{
if (device == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&device->ref_count))
{
return;
}
if (device->finished)
return;
cairo_device_flush (device);
if (device->backend->finish != NULL)
device->backend->finish (device);
/* We only finish the device after the backend's callback returns because
* the device might still be needed during the callback
* (e.g. for cairo_device_acquire ()).
*/
device->finished = TRUE;
}
slim_hidden_def (cairo_device_finish);
/**
* cairo_device_destroy:
* @device: a #cairo_device_t
*
* Decreases the reference count on @device by one. If the result is
* zero, then @device and all associated resources are freed. See
* cairo_device_reference().
*
* This function may acquire devices if the last reference was dropped.
*
* Since: 1.10
**/
void
cairo_device_destroy (cairo_device_t *device)
{
cairo_user_data_array_t user_data;
if (device == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&device->ref_count))
{
return;
}
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&device->ref_count));
if (! _cairo_reference_count_dec_and_test (&device->ref_count))
return;
cairo_device_finish (device);
assert (device->mutex_depth == 0);
CAIRO_MUTEX_FINI (device->mutex);
user_data = device->user_data;
device->backend->destroy (device);
_cairo_user_data_array_fini (&user_data);
}
slim_hidden_def (cairo_device_destroy);
/**
* cairo_device_get_type:
* @device: a #cairo_device_t
*
* This function returns the type of the device. See #cairo_device_type_t
* for available types.
*
* Return value: The type of @device.
*
* Since: 1.10
**/
cairo_device_type_t
cairo_device_get_type (cairo_device_t *device)
{
if (device == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&device->ref_count))
{
return CAIRO_DEVICE_TYPE_INVALID;
}
return device->backend->type;
}
/**
* cairo_device_acquire:
* @device: a #cairo_device_t
*
* Acquires the @device for the current thread. This function will block
* until no other thread has acquired the device.
*
* If the return value is %CAIRO_STATUS_SUCCESS, you successfully acquired the
* device. From now on your thread owns the device and no other thread will be
* able to acquire it until a matching call to cairo_device_release(). It is
* allowed to recursively acquire the device multiple times from the same
* thread.
*
* <note><para>You must never acquire two different devices at the same time
* unless this is explicitly allowed. Otherwise the possibility of deadlocks
* exist.
*
* As various Cairo functions can acquire devices when called, these functions
* may also cause deadlocks when you call them with an acquired device. So you
* must not have a device acquired when calling them. These functions are
* marked in the documentation.
* </para></note>
*
* Return value: %CAIRO_STATUS_SUCCESS on success or an error code if
* the device is in an error state and could not be
* acquired. After a successful call to cairo_device_acquire(),
* a matching call to cairo_device_release() is required.
*
* Since: 1.10
**/
cairo_status_t
cairo_device_acquire (cairo_device_t *device)
{
if (device == NULL)
return CAIRO_STATUS_SUCCESS;
if (unlikely (device->status))
return device->status;
if (unlikely (device->finished))
return _cairo_device_set_error (device, CAIRO_STATUS_DEVICE_FINISHED);
CAIRO_MUTEX_LOCK (device->mutex);
if (device->mutex_depth++ == 0) {
if (device->backend->lock != NULL)
device->backend->lock (device);
}
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_device_acquire);
/**
* cairo_device_release:
* @device: a #cairo_device_t
*
* Releases a @device previously acquired using cairo_device_acquire(). See
* that function for details.
*
* Since: 1.10
**/
void
cairo_device_release (cairo_device_t *device)
{
if (device == NULL)
return;
assert (device->mutex_depth > 0);
if (--device->mutex_depth == 0) {
if (device->backend->unlock != NULL)
device->backend->unlock (device);
}
CAIRO_MUTEX_UNLOCK (device->mutex);
}
slim_hidden_def (cairo_device_release);
cairo_status_t
_cairo_device_set_error (cairo_device_t *device,
cairo_status_t status)
{
if (status == CAIRO_STATUS_SUCCESS)
return CAIRO_STATUS_SUCCESS;
_cairo_status_set_error (&device->status, status);
return _cairo_error (status);
}
/**
* cairo_device_get_reference_count:
* @device: a #cairo_device_t
*
* Returns the current reference count of @device.
*
* Return value: the current reference count of @device. If the
* object is a nil object, 0 will be returned.
*
* Since: 1.10
**/
unsigned int
cairo_device_get_reference_count (cairo_device_t *device)
{
if (device == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&device->ref_count))
return 0;
return CAIRO_REFERENCE_COUNT_GET_VALUE (&device->ref_count);
}
/**
* cairo_device_get_user_data:
* @device: a #cairo_device_t
* @key: the address of the #cairo_user_data_key_t the user data was
* attached to
*
* Return user data previously attached to @device using the
* specified key. If no user data has been attached with the given
* key this function returns %NULL.
*
* Return value: the user data previously attached or %NULL.
*
* Since: 1.10
**/
void *
cairo_device_get_user_data (cairo_device_t *device,
const cairo_user_data_key_t *key)
{
return _cairo_user_data_array_get_data (&device->user_data,
key);
}
/**
* cairo_device_set_user_data:
* @device: a #cairo_device_t
* @key: the address of a #cairo_user_data_key_t to attach the user data to
* @user_data: the user data to attach to the #cairo_device_t
* @destroy: a #cairo_destroy_func_t which will be called when the
* #cairo_t is destroyed or when new user data is attached using the
* same key.
*
* Attach user data to @device. To remove user data from a surface,
* call this function with the key that was used to set it and %NULL
* for @data.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
* slot could not be allocated for the user data.
*
* Since: 1.10
**/
cairo_status_t
cairo_device_set_user_data (cairo_device_t *device,
const cairo_user_data_key_t *key,
void *user_data,
cairo_destroy_func_t destroy)
{
if (CAIRO_REFERENCE_COUNT_IS_INVALID (&device->ref_count))
return device->status;
return _cairo_user_data_array_set_data (&device->user_data,
key, user_data, destroy);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-error-inline.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#ifndef _CAIRO_ERROR_INLINE_H_
#define _CAIRO_ERROR_INLINE_H_
#include "cairo-error-private.h"
CAIRO_BEGIN_DECLS
static inline cairo_status_t
_cairo_public_status (cairo_int_status_t status)
{
assert (status <= CAIRO_INT_STATUS_LAST_STATUS);
return (cairo_status_t) status;
}
#endif /* _CAIRO_ERROR_INLINE_H_ */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-error-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#ifndef _CAIRO_ERROR_PRIVATE_H_
#define _CAIRO_ERROR_PRIVATE_H_
#include "cairo.h"
#include "cairo-compiler-private.h"
#include "cairo-types-private.h"
#include <assert.h>
CAIRO_BEGIN_DECLS
/* _cairo_int_status: internal status
*
* Sure wish C had a real enum type so that this would be distinct
* from #cairo_status_t. Oh well, without that, I'll use this bogus 100
* offset. We want to keep it fit in int8_t as the compiler may choose
* that for #cairo_status_t
*/
enum _cairo_int_status {
CAIRO_INT_STATUS_SUCCESS = 0,
CAIRO_INT_STATUS_NO_MEMORY,
CAIRO_INT_STATUS_INVALID_RESTORE,
CAIRO_INT_STATUS_INVALID_POP_GROUP,
CAIRO_INT_STATUS_NO_CURRENT_POINT,
CAIRO_INT_STATUS_INVALID_MATRIX,
CAIRO_INT_STATUS_INVALID_STATUS,
CAIRO_INT_STATUS_NULL_POINTER,
CAIRO_INT_STATUS_INVALID_STRING,
CAIRO_INT_STATUS_INVALID_PATH_DATA,
CAIRO_INT_STATUS_READ_ERROR,
CAIRO_INT_STATUS_WRITE_ERROR,
CAIRO_INT_STATUS_SURFACE_FINISHED,
CAIRO_INT_STATUS_SURFACE_TYPE_MISMATCH,
CAIRO_INT_STATUS_PATTERN_TYPE_MISMATCH,
CAIRO_INT_STATUS_INVALID_CONTENT,
CAIRO_INT_STATUS_INVALID_FORMAT,
CAIRO_INT_STATUS_INVALID_VISUAL,
CAIRO_INT_STATUS_FILE_NOT_FOUND,
CAIRO_INT_STATUS_INVALID_DASH,
CAIRO_INT_STATUS_INVALID_DSC_COMMENT,
CAIRO_INT_STATUS_INVALID_INDEX,
CAIRO_INT_STATUS_CLIP_NOT_REPRESENTABLE,
CAIRO_INT_STATUS_TEMP_FILE_ERROR,
CAIRO_INT_STATUS_INVALID_STRIDE,
CAIRO_INT_STATUS_FONT_TYPE_MISMATCH,
CAIRO_INT_STATUS_USER_FONT_IMMUTABLE,
CAIRO_INT_STATUS_USER_FONT_ERROR,
CAIRO_INT_STATUS_NEGATIVE_COUNT,
CAIRO_INT_STATUS_INVALID_CLUSTERS,
CAIRO_INT_STATUS_INVALID_SLANT,
CAIRO_INT_STATUS_INVALID_WEIGHT,
CAIRO_INT_STATUS_INVALID_SIZE,
CAIRO_INT_STATUS_USER_FONT_NOT_IMPLEMENTED,
CAIRO_INT_STATUS_DEVICE_TYPE_MISMATCH,
CAIRO_INT_STATUS_DEVICE_ERROR,
CAIRO_INT_STATUS_INVALID_MESH_CONSTRUCTION,
CAIRO_INT_STATUS_DEVICE_FINISHED,
CAIRO_INT_STATUS_JBIG2_GLOBAL_MISSING,
CAIRO_INT_STATUS_PNG_ERROR,
CAIRO_INT_STATUS_FREETYPE_ERROR,
CAIRO_INT_STATUS_WIN32_GDI_ERROR,
CAIRO_INT_STATUS_TAG_ERROR,
CAIRO_INT_STATUS_LAST_STATUS,
CAIRO_INT_STATUS_UNSUPPORTED = 100,
CAIRO_INT_STATUS_DEGENERATE,
CAIRO_INT_STATUS_NOTHING_TO_DO,
CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY,
CAIRO_INT_STATUS_IMAGE_FALLBACK,
CAIRO_INT_STATUS_ANALYZE_RECORDING_SURFACE_PATTERN,
};
typedef enum _cairo_int_status cairo_int_status_t;
#define _cairo_status_is_error(status) \
(status != CAIRO_STATUS_SUCCESS && status < CAIRO_STATUS_LAST_STATUS)
#define _cairo_int_status_is_error(status) \
(status != CAIRO_INT_STATUS_SUCCESS && status < CAIRO_INT_STATUS_LAST_STATUS)
cairo_private cairo_status_t
_cairo_error (cairo_status_t status);
/* hide compiler warnings when discarding the return value */
#define _cairo_error_throw(status) do { \
cairo_status_t status__ = _cairo_error (status); \
(void) status__; \
} while (0)
CAIRO_END_DECLS
#endif /* _CAIRO_ERROR_PRIVATE_H_ */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-error.c | /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-private.h"
#include "cairo-compiler-private.h"
#include "cairo-error-private.h"
#include <assert.h>
/**
* _cairo_error:
* @status: a status value indicating an error, (eg. not
* %CAIRO_STATUS_SUCCESS)
*
* Checks that status is an error status, but does nothing else.
*
* All assignments of an error status to any user-visible object
* within the cairo application should result in a call to
* _cairo_error().
*
* The purpose of this function is to allow the user to set a
* breakpoint in _cairo_error() to generate a stack trace for when the
* user causes cairo to detect an error.
*
* Return value: the error status.
**/
cairo_status_t
_cairo_error (cairo_status_t status)
{
CAIRO_ENSURE_UNIQUE;
assert (_cairo_status_is_error (status));
return status;
}
COMPILE_TIME_ASSERT ((int)CAIRO_INT_STATUS_LAST_STATUS == (int)CAIRO_STATUS_LAST_STATUS);
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-fallback-compositor.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-compositor-private.h"
#include "cairo-image-surface-private.h"
#include "cairo-surface-offset-private.h"
/* high-level compositor interface */
static cairo_int_status_t
_cairo_fallback_compositor_paint (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents)
{
cairo_image_surface_t *image;
cairo_int_status_t status;
TRACE ((stderr, "%s\n", __FUNCTION__));
image = _cairo_surface_map_to_image (extents->surface, &extents->unbounded);
status = _cairo_surface_offset_paint (&image->base,
extents->unbounded.x,
extents->unbounded.y,
extents->op,
&extents->source_pattern.base,
extents->clip);
return _cairo_surface_unmap_image (extents->surface, image);
}
static cairo_int_status_t
_cairo_fallback_compositor_mask (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents)
{
cairo_image_surface_t *image;
cairo_int_status_t status;
TRACE ((stderr, "%s\n", __FUNCTION__));
image = _cairo_surface_map_to_image (extents->surface, &extents->unbounded);
status = _cairo_surface_offset_mask (&image->base,
extents->unbounded.x,
extents->unbounded.y,
extents->op,
&extents->source_pattern.base,
&extents->mask_pattern.base,
extents->clip);
return _cairo_surface_unmap_image (extents->surface, image);
}
static cairo_int_status_t
_cairo_fallback_compositor_stroke (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_antialias_t antialias)
{
cairo_image_surface_t *image;
cairo_int_status_t status;
TRACE ((stderr, "%s\n", __FUNCTION__));
image = _cairo_surface_map_to_image (extents->surface, &extents->unbounded);
status = _cairo_surface_offset_stroke (&image->base,
extents->unbounded.x,
extents->unbounded.y,
extents->op,
&extents->source_pattern.base,
path, style,
ctm, ctm_inverse,
tolerance,
antialias,
extents->clip);
return _cairo_surface_unmap_image (extents->surface, image);
}
static cairo_int_status_t
_cairo_fallback_compositor_fill (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias)
{
cairo_image_surface_t *image;
cairo_int_status_t status;
TRACE ((stderr, "%s\n", __FUNCTION__));
image = _cairo_surface_map_to_image (extents->surface, &extents->unbounded);
status = _cairo_surface_offset_fill (&image->base,
extents->unbounded.x,
extents->unbounded.y,
extents->op,
&extents->source_pattern.base,
path,
fill_rule, tolerance, antialias,
extents->clip);
return _cairo_surface_unmap_image (extents->surface, image);
}
static cairo_int_status_t
_cairo_fallback_compositor_glyphs (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
cairo_scaled_font_t *scaled_font,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_bool_t overlap)
{
cairo_image_surface_t *image;
cairo_int_status_t status;
TRACE ((stderr, "%s\n", __FUNCTION__));
image = _cairo_surface_map_to_image (extents->surface, &extents->unbounded);
status = _cairo_surface_offset_glyphs (&image->base,
extents->unbounded.x,
extents->unbounded.y,
extents->op,
&extents->source_pattern.base,
scaled_font, glyphs, num_glyphs,
extents->clip);
return _cairo_surface_unmap_image (extents->surface, image);
}
const cairo_compositor_t _cairo_fallback_compositor = {
&__cairo_no_compositor,
_cairo_fallback_compositor_paint,
_cairo_fallback_compositor_mask,
_cairo_fallback_compositor_stroke,
_cairo_fallback_compositor_fill,
_cairo_fallback_compositor_glyphs,
};
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-features.h | /* Generated by configure. Do not edit. */
#ifndef CAIRO_FEATURES_H
#define CAIRO_FEATURES_H
#define CAIRO_HAS_IMAGE_SURFACE 1
#define CAIRO_HAS_MIME_SURFACE 1
#define CAIRO_HAS_OBSERVER_SURFACE 1
#define CAIRO_HAS_RECORDING_SURFACE 1
#define CAIRO_HAS_USER_FONT 1
/*#undef CAIRO_HAS_BEOS_SURFACE */
/*#undef CAIRO_HAS_COGL_SURFACE */
/*#undef CAIRO_HAS_DIRECTFB_SURFACE */
/*#undef CAIRO_HAS_DRM_SURFACE */
/*#undef CAIRO_HAS_EGL_FUNCTIONS */
/*#undef CAIRO_HAS_FC_FONT */
/*#undef CAIRO_HAS_FT_FONT */
/*#undef CAIRO_HAS_GALLIUM_SURFACE */
/*#undef CAIRO_HAS_GLESV2_SURFACE */
/*#undef CAIRO_HAS_GLX_FUNCTIONS */
/*#undef CAIRO_HAS_GL_SURFACE */
/*#undef CAIRO_HAS_GOBJECT_FUNCTIONS */
/*#undef CAIRO_HAS_OS2_SURFACE */
/*#undef CAIRO_HAS_PDF_SURFACE */
/*#undef CAIRO_HAS_PNG_FUNCTIONS */
/*#undef CAIRO_HAS_PS_SURFACE */
/*#undef CAIRO_HAS_QT_SURFACE */
/*#undef CAIRO_HAS_QUARTZ_FONT */
/*#undef CAIRO_HAS_QUARTZ_IMAGE_SURFACE */
/*#undef CAIRO_HAS_QUARTZ_SURFACE */
/*#undef CAIRO_HAS_SCRIPT_SURFACE */
/*#undef CAIRO_HAS_SKIA_SURFACE */
/*#undef CAIRO_HAS_SVG_SURFACE */
/*#undef CAIRO_HAS_TEE_SURFACE */
/*#undef CAIRO_HAS_VG_SURFACE */
/*#undef CAIRO_HAS_WGL_FUNCTIONS */
/*#undef CAIRO_HAS_WIN32_FONT */
/*#undef CAIRO_HAS_WIN32_SURFACE */
/*#undef CAIRO_HAS_XCB_SHM_FUNCTIONS */
/*#undef CAIRO_HAS_XCB_SURFACE */
/*#undef CAIRO_HAS_XLIB_SURFACE */
/*#undef CAIRO_HAS_XLIB_XCB_FUNCTIONS */
/*#undef CAIRO_HAS_XLIB_XRENDER_SURFACE */
/*#undef CAIRO_HAS_XML_SURFACE */
#endif
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-fixed-private.h | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* Cairo - a vector graphics library with display and print output
*
* Copyright © 2007 Mozilla Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Mozilla Foundation
*
* Contributor(s):
* Vladimir Vukicevic <vladimir@pobox.com>
*/
#ifndef CAIRO_FIXED_PRIVATE_H
#define CAIRO_FIXED_PRIVATE_H
#include "cairo-fixed-type-private.h"
#include "cairo-wideint-private.h"
#include "cairoint.h"
/* Implementation */
#if (CAIRO_FIXED_BITS != 32)
# error CAIRO_FIXED_BITS must be 32, and the type must be a 32-bit type.
# error To remove this limitation, you will have to fix the tessellator.
#endif
#define CAIRO_FIXED_ONE ((cairo_fixed_t)(1 << CAIRO_FIXED_FRAC_BITS))
#define CAIRO_FIXED_ONE_DOUBLE ((double)(1 << CAIRO_FIXED_FRAC_BITS))
#define CAIRO_FIXED_EPSILON ((cairo_fixed_t)(1))
#define CAIRO_FIXED_ERROR_DOUBLE (1. / (2 * CAIRO_FIXED_ONE_DOUBLE))
#define CAIRO_FIXED_FRAC_MASK ((cairo_fixed_t)(((cairo_fixed_unsigned_t)(-1)) >> (CAIRO_FIXED_BITS - CAIRO_FIXED_FRAC_BITS)))
#define CAIRO_FIXED_WHOLE_MASK (~CAIRO_FIXED_FRAC_MASK)
static inline cairo_fixed_t
_cairo_fixed_from_int (int i)
{
return i << CAIRO_FIXED_FRAC_BITS;
}
/* This is the "magic number" approach to converting a double into fixed
* point as described here:
*
* http://www.stereopsis.com/sree/fpu2006.html (an overview)
* http://www.d6.com/users/checker/pdfs/gdmfp.pdf (in detail)
*
* The basic idea is to add a large enough number to the double that the
* literal floating point is moved up to the extent that it forces the
* double's value to be shifted down to the bottom of the mantissa (to make
* room for the large number being added in). Since the mantissa is, at a
* given moment in time, a fixed point integer itself, one can convert a
* float to various fixed point representations by moving around the point
* of a floating point number through arithmetic operations. This behavior
* is reliable on most modern platforms as it is mandated by the IEEE-754
* standard for floating point arithmetic.
*
* For our purposes, a "magic number" must be carefully selected that is
* both large enough to produce the desired point-shifting effect, and also
* has no lower bits in its representation that would interfere with our
* value at the bottom of the mantissa. The magic number is calculated as
* follows:
*
* (2 ^ (MANTISSA_SIZE - FRACTIONAL_SIZE)) * 1.5
*
* where in our case:
* - MANTISSA_SIZE for 64-bit doubles is 52
* - FRACTIONAL_SIZE for 16.16 fixed point is 16
*
* Although this approach provides a very large speedup of this function
* on a wide-array of systems, it does come with two caveats:
*
* 1) It uses banker's rounding as opposed to arithmetic rounding.
* 2) It doesn't function properly if the FPU is in single-precision
* mode.
*/
/* The 16.16 number must always be available */
#define CAIRO_MAGIC_NUMBER_FIXED_16_16 (103079215104.0)
#if CAIRO_FIXED_BITS <= 32
#define CAIRO_MAGIC_NUMBER_FIXED ((1LL << (52 - CAIRO_FIXED_FRAC_BITS)) * 1.5)
/* For 32-bit fixed point numbers */
static inline cairo_fixed_t
_cairo_fixed_from_double (double d)
{
union {
double d;
int32_t i[2];
} u;
u.d = d + CAIRO_MAGIC_NUMBER_FIXED;
#ifdef FLOAT_WORDS_BIGENDIAN
return u.i[1];
#else
return u.i[0];
#endif
}
#else
# error Please define a magic number for your fixed point type!
# error See cairo-fixed-private.h for details.
#endif
static inline cairo_fixed_t
_cairo_fixed_from_26_6 (uint32_t i)
{
#if CAIRO_FIXED_FRAC_BITS > 6
return i << (CAIRO_FIXED_FRAC_BITS - 6);
#else
return i >> (6 - CAIRO_FIXED_FRAC_BITS);
#endif
}
static inline cairo_fixed_t
_cairo_fixed_from_16_16 (uint32_t i)
{
#if CAIRO_FIXED_FRAC_BITS > 16
return i << (CAIRO_FIXED_FRAC_BITS - 16);
#else
return i >> (16 - CAIRO_FIXED_FRAC_BITS);
#endif
}
static inline double
_cairo_fixed_to_double (cairo_fixed_t f)
{
return ((double) f) / CAIRO_FIXED_ONE_DOUBLE;
}
static inline int
_cairo_fixed_is_integer (cairo_fixed_t f)
{
return (f & CAIRO_FIXED_FRAC_MASK) == 0;
}
static inline cairo_fixed_t
_cairo_fixed_floor (cairo_fixed_t f)
{
return f & ~CAIRO_FIXED_FRAC_MASK;
}
static inline cairo_fixed_t
_cairo_fixed_ceil (cairo_fixed_t f)
{
return _cairo_fixed_floor (f + CAIRO_FIXED_FRAC_MASK);
}
static inline cairo_fixed_t
_cairo_fixed_round (cairo_fixed_t f)
{
return _cairo_fixed_floor (f + (CAIRO_FIXED_FRAC_MASK+1)/2);
}
static inline cairo_fixed_t
_cairo_fixed_round_down (cairo_fixed_t f)
{
return _cairo_fixed_floor (f + CAIRO_FIXED_FRAC_MASK/2);
}
static inline int
_cairo_fixed_integer_part (cairo_fixed_t f)
{
return f >> CAIRO_FIXED_FRAC_BITS;
}
static inline int
_cairo_fixed_integer_round (cairo_fixed_t f)
{
return _cairo_fixed_integer_part (f + (CAIRO_FIXED_FRAC_MASK+1)/2);
}
static inline int
_cairo_fixed_integer_round_down (cairo_fixed_t f)
{
return _cairo_fixed_integer_part (f + CAIRO_FIXED_FRAC_MASK/2);
}
static inline int
_cairo_fixed_fractional_part (cairo_fixed_t f)
{
return f & CAIRO_FIXED_FRAC_MASK;
}
static inline int
_cairo_fixed_integer_floor (cairo_fixed_t f)
{
if (f >= 0)
return f >> CAIRO_FIXED_FRAC_BITS;
else
return -((-f - 1) >> CAIRO_FIXED_FRAC_BITS) - 1;
}
static inline int
_cairo_fixed_integer_ceil (cairo_fixed_t f)
{
if (f > 0)
return ((f - 1)>>CAIRO_FIXED_FRAC_BITS) + 1;
else
return - ((cairo_fixed_t)(-(cairo_fixed_unsigned_t)f) >> CAIRO_FIXED_FRAC_BITS);
}
/* A bunch of explicit 16.16 operators; we need these
* to interface with pixman and other backends that require
* 16.16 fixed point types.
*/
static inline cairo_fixed_16_16_t
_cairo_fixed_to_16_16 (cairo_fixed_t f)
{
#if (CAIRO_FIXED_FRAC_BITS == 16) && (CAIRO_FIXED_BITS == 32)
return f;
#elif CAIRO_FIXED_FRAC_BITS > 16
/* We're just dropping the low bits, so we won't ever got over/underflow here */
return f >> (CAIRO_FIXED_FRAC_BITS - 16);
#else
cairo_fixed_16_16_t x;
/* Handle overflow/underflow by clamping to the lowest/highest
* value representable as 16.16
*/
if ((f >> CAIRO_FIXED_FRAC_BITS) < INT16_MIN) {
x = INT32_MIN;
} else if ((f >> CAIRO_FIXED_FRAC_BITS) > INT16_MAX) {
x = INT32_MAX;
} else {
x = f << (16 - CAIRO_FIXED_FRAC_BITS);
}
return x;
#endif
}
static inline cairo_fixed_16_16_t
_cairo_fixed_16_16_from_double (double d)
{
union {
double d;
int32_t i[2];
} u;
u.d = d + CAIRO_MAGIC_NUMBER_FIXED_16_16;
#ifdef FLOAT_WORDS_BIGENDIAN
return u.i[1];
#else
return u.i[0];
#endif
}
static inline int
_cairo_fixed_16_16_floor (cairo_fixed_16_16_t f)
{
if (f >= 0)
return f >> 16;
else
return -((-f - 1) >> 16) - 1;
}
static inline double
_cairo_fixed_16_16_to_double (cairo_fixed_16_16_t f)
{
return ((double) f) / (double) (1 << 16);
}
#if CAIRO_FIXED_BITS == 32
static inline cairo_fixed_t
_cairo_fixed_mul (cairo_fixed_t a, cairo_fixed_t b)
{
cairo_int64_t temp = _cairo_int32x32_64_mul (a, b);
return _cairo_int64_to_int32(_cairo_int64_rsl (temp, CAIRO_FIXED_FRAC_BITS));
}
/* computes round (a * b / c) */
static inline cairo_fixed_t
_cairo_fixed_mul_div (cairo_fixed_t a, cairo_fixed_t b, cairo_fixed_t c)
{
cairo_int64_t ab = _cairo_int32x32_64_mul (a, b);
cairo_int64_t c64 = _cairo_int32_to_int64 (c);
return _cairo_int64_to_int32 (_cairo_int64_divrem (ab, c64).quo);
}
/* computes floor (a * b / c) */
static inline cairo_fixed_t
_cairo_fixed_mul_div_floor (cairo_fixed_t a, cairo_fixed_t b, cairo_fixed_t c)
{
return _cairo_int64_32_div (_cairo_int32x32_64_mul (a, b), c);
}
/* compute y from x so that (x,y), p1, and p2 are collinear */
static inline cairo_fixed_t
_cairo_edge_compute_intersection_y_for_x (const cairo_point_t *p1,
const cairo_point_t *p2,
cairo_fixed_t x)
{
cairo_fixed_t y, dx;
if (x == p1->x)
return p1->y;
if (x == p2->x)
return p2->y;
y = p1->y;
dx = p2->x - p1->x;
if (dx != 0)
y += _cairo_fixed_mul_div_floor (x - p1->x, p2->y - p1->y, dx);
return y;
}
/* compute x from y so that (x,y), p1, and p2 are collinear */
static inline cairo_fixed_t
_cairo_edge_compute_intersection_x_for_y (const cairo_point_t *p1,
const cairo_point_t *p2,
cairo_fixed_t y)
{
cairo_fixed_t x, dy;
if (y == p1->y)
return p1->x;
if (y == p2->y)
return p2->x;
x = p1->x;
dy = p2->y - p1->y;
if (dy != 0)
x += _cairo_fixed_mul_div_floor (y - p1->y, p2->x - p1->x, dy);
return x;
}
/* Intersect two segments based on the algorithm described at
* http://paulbourke.net/geometry/pointlineplane/. This implementation
* uses floating point math. */
static inline cairo_bool_t
_slow_segment_intersection (const cairo_point_t *seg1_p1,
const cairo_point_t *seg1_p2,
const cairo_point_t *seg2_p1,
const cairo_point_t *seg2_p2,
cairo_point_t *intersection)
{
double denominator, u_a, u_b;
double seg1_dx, seg1_dy, seg2_dx, seg2_dy, seg_start_dx, seg_start_dy;
seg1_dx = _cairo_fixed_to_double (seg1_p2->x - seg1_p1->x);
seg1_dy = _cairo_fixed_to_double (seg1_p2->y - seg1_p1->y);
seg2_dx = _cairo_fixed_to_double (seg2_p2->x - seg2_p1->x);
seg2_dy = _cairo_fixed_to_double (seg2_p2->y - seg2_p1->y);
denominator = (seg2_dy * seg1_dx) - (seg2_dx * seg1_dy);
if (denominator == 0)
return FALSE;
seg_start_dx = _cairo_fixed_to_double (seg1_p1->x - seg2_p1->x);
seg_start_dy = _cairo_fixed_to_double (seg1_p1->y - seg2_p1->y);
u_a = ((seg2_dx * seg_start_dy) - (seg2_dy * seg_start_dx)) / denominator;
u_b = ((seg1_dx * seg_start_dy) - (seg1_dy * seg_start_dx)) / denominator;
if (u_a <= 0 || u_a >= 1 || u_b <= 0 || u_b >= 1)
return FALSE;
intersection->x = seg1_p1->x + _cairo_fixed_from_double ((u_a * seg1_dx));
intersection->y = seg1_p1->y + _cairo_fixed_from_double ((u_a * seg1_dy));
return TRUE;
}
#else
# error Please define multiplication and other operands for your fixed-point type size
#endif
#endif /* CAIRO_FIXED_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-fixed-type-private.h | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* Cairo - a vector graphics library with display and print output
*
* Copyright © 2007 Mozilla Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Mozilla Foundation
*
* Contributor(s):
* Vladimir Vukicevic <vladimir@pobox.com>
*/
#ifndef CAIRO_FIXED_TYPE_PRIVATE_H
#define CAIRO_FIXED_TYPE_PRIVATE_H
#include "cairo-wideint-type-private.h"
/*
* Fixed-point configuration
*/
typedef int32_t cairo_fixed_16_16_t;
typedef cairo_int64_t cairo_fixed_32_32_t;
typedef cairo_int64_t cairo_fixed_48_16_t;
typedef cairo_int128_t cairo_fixed_64_64_t;
typedef cairo_int128_t cairo_fixed_96_32_t;
/* Eventually, we should allow changing this, but I think
* there are some assumptions in the tessellator about the
* size of a fixed type. For now, it must be 32.
*/
#define CAIRO_FIXED_BITS 32
/* The number of fractional bits. Changing this involves
* making sure that you compute a double-to-fixed magic number.
* (see below).
*/
#define CAIRO_FIXED_FRAC_BITS 8
/* A signed type %CAIRO_FIXED_BITS in size; the main fixed point type */
typedef int32_t cairo_fixed_t;
/* An unsigned type of the same size as #cairo_fixed_t */
typedef uint32_t cairo_fixed_unsigned_t;
typedef struct _cairo_point {
cairo_fixed_t x;
cairo_fixed_t y;
} cairo_point_t;
#endif /* CAIRO_FIXED_TYPE_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-font-face-twin-data.c | /* See cairo-font-face-twin.c for copyright info */
#include "cairoint.h"
const int8_t _cairo_twin_outlines[] = {
/* 0x0 '\0' offset 0 */
0, 24, 42, 0, 2, 2,
0, 24, /* snap_x */
-42, 0, /* snap_y */
'm', 0, 0,
'l', 0, -42,
'l', 24, -42,
'l', 24, 0,
'l', 0, 0,
'e',
'X', 'X',
/* 0x20 ' ' offset 28 */
0, 4, 0, 0, 0, 0,
/* snap_x */
/* snap_y */
'e',
'X', 'X', 'X',
'X', 'X',
/* 0x21 '!' offset 40 */
0, 0, 42, 0, 1, 3,
0, /* snap_x */
-42, -14, 0, /* snap_y */
'm', 0, -42,
'l', 0, -14,
'm', 0, 0,
'l', 0, 0,
'e',
'X', 'X', 'X', 'X', 'X', 'X',
'X', 'X', 'X', 'X', 'X', 'X',
'X', 'X', 'X', 'X', 'X', 'X',
'X', 'X', 'X', 'X', 'X', 'X',
'X', 'X', 'X',
/* 0x22 '"' offset 90 */
0, 16, 42, -28, 2, 2,
0, 16, /* snap_x */
-42, -28, /* snap_y */
'm', 0, -42,
'l', 0, -28,
'm', 16, -42,
'l', 16, -28,
'e',
'X',
/* 0x23 '#' offset 114 */
0, 30, 50, 14, 2, 5,
0, 30, /* snap_x */
-24, -21, -15, -12, 0, /* snap_y */
'm', 16, -50,
'l', 2, 14,
'm', 28, -50,
'l', 14, 14,
'm', 2, -24,
'l', 30, -24,
'm', 0, -12,
'l', 28, -12,
'e',
/* 0x24 '$' offset 152 */
0, 28, 50, 8, 4, 4,
0, 10, 18, 28, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 10, -50,
'l', 10, 8,
'm', 18, -50,
'l', 18, 8,
'm', 28, -36,
'c', 24, -42, 18, -42, 14, -42,
'c', 10, -42, 0, -42, 0, -34,
'c', 0, -25, 8, -24, 14, -22,
'c', 20, -20, 28, -19, 28, -9,
'c', 28, 0, 18, 0, 14, 0,
'c', 10, 0, 4, 0, 0, -6,
'e',
/* 0x25 '%' offset 224 */
0, 36, 42, 0, 4, 7,
0, 14, 22, 36, /* snap_x */
-42, -38, -28, -21, -15, -14, 0, /* snap_y */
'm', 10, -42,
'c', 12, -41, 14, -40, 14, -36,
'c', 14, -30, 11, -28, 6, -28,
'c', 2, -28, 0, -30, 0, -34,
'c', 0, -39, 3, -42, 8, -42,
'l', 10, -42,
'c', 18, -37, 28, -37, 36, -42,
'l', 0, 0,
'm', 28, -14,
'c', 24, -14, 22, -11, 22, -6,
'c', 22, -2, 24, 0, 28, 0,
'c', 33, 0, 36, -2, 36, -8,
'c', 36, -12, 34, -14, 30, -14,
'l', 28, -14,
'e',
'X', 'X', 'X',
/* 0x26 '&' offset 323 */
0, 40, 42, 0, 4, 4,
0, 10, 22, 40, /* snap_x */
-28, -21, -15, 0, /* snap_y */
'm', 40, -24,
'c', 40, -27, 39, -28, 37, -28,
'c', 29, -28, 32, 0, 12, 0,
'c', 0, 0, 0, -8, 0, -10,
'c', 0, -24, 22, -20, 22, -34,
'c', 22, -45, 10, -45, 10, -34,
'c', 10, -27, 25, 0, 36, 0,
'c', 39, 0, 40, -1, 40, -4,
'e',
/* 0x27 ''' offset 390 */
0, 4, 42, -30, 2, 2,
0, 4, /* snap_x */
-42, -28, /* snap_y */
'm', 2, -38,
'c', -1, -38, -1, -42, 2, -42,
'c', 6, -42, 5, -33, 0, -30,
'e',
'X',
/* 0x28 '(' offset 419 */
0, 14, 50, 14, 2, 2,
0, 14, /* snap_x */
-50, 14, /* snap_y */
'm', 14, -50,
'c', -5, -32, -5, -5, 14, 14,
'e',
'X',
/* 0x29 ')' offset 441 */
0, 14, 50, 14, 2, 2,
0, 14, /* snap_x */
-15, 14, /* snap_y */
'm', 0, -50,
'c', 19, -34, 19, -2, 0, 14,
'e',
'X',
/* 0x2a '*' offset 463 */
0, 20, 30, -6, 3, 3,
0, 10, 20, /* snap_x */
-21, -15, 0, /* snap_y */
'm', 10, -30,
'l', 10, -6,
'm', 0, -24,
'l', 20, -12,
'm', 20, -24,
'l', 0, -12,
'e',
/* 0x2b '+' offset 494 */
0, 36, 36, 0, 3, 4,
0, 18, 36, /* snap_x */
-21, -18, -15, 0, /* snap_y */
'm', 18, -36,
'l', 18, 0,
'm', 0, -18,
'l', 36, -18,
'e',
/* 0x2c ',' offset 520 */
0, 4, 4, 8, 2, 3,
0, 4, /* snap_x */
-21, -15, 0, /* snap_y */
'm', 4, -2,
'c', 4, 1, 0, 1, 0, -2,
'c', 0, -5, 4, -5, 4, -2,
'c', 4, 4, 2, 6, 0, 8,
'e',
/* 0x2d '-' offset 556 */
0, 36, 18, -18, 2, 4,
0, 36, /* snap_x */
-21, -18, -15, 0, /* snap_y */
'm', 0, -18,
'l', 36, -18,
'e',
/* 0x2e '.' offset 575 */
0, 4, 4, 0, 2, 3,
0, 4, /* snap_x */
-21, -15, 0, /* snap_y */
'm', 2, -4,
'c', -1, -4, -1, 0, 2, 0,
'c', 5, 0, 5, -4, 2, -4,
'e',
/* 0x2f '/' offset 604 */
0, 36, 50, 14, 2, 3,
0, 36, /* snap_x */
-21, -15, 0, /* snap_y */
'm', 36, -50,
'l', 0, 14,
'e',
/* 0x30 '0' offset 622 */
0, 28, 42, 0, 2, 4,
0, 28, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 14, -42,
'c', 9, -42, 0, -42, 0, -21,
'c', 0, 0, 9, 0, 14, 0,
'c', 19, 0, 28, 0, 28, -21,
'c', 28, -42, 19, -42, 14, -42,
'E',
/* 0x31 '1' offset 666 */
0, 28, 42, 0, 2, 3,
0, 17, 28 /* snap_x */
-42, -34, 0, /* snap_y */
'm', 7, -34,
'c', 11, -35, 15, -38, 17, -42,
'l', 17, 0,
'e',
/* 0x32 '2' offset 691 */
0, 28, 42, 0, 4, 4,
0, 2, 26, 28, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 2, -32,
'c', 2, -34, 2, -42, 14, -42,
'c', 26, -42, 26, -34, 26, -32,
'c', 26, -30, 25, -25, 10, -10,
'l', 0, 0,
'l', 28, 0,
'e',
/* 0x33 '3' offset 736 */
0, 28, 42, 0, 2, 5,
0, 28, /* snap_x */
-42, -26, -21, -15, 0, /* snap_y */
'm', 4, -42,
'l', 26, -42,
'l', 14, -26,
'c', 21, -26, 28, -26, 28, -14,
'c', 28, 0, 17, 0, 13, 0,
'c', 8, 0, 3, -1, 0, -8,
'e',
/* 0x34 '4' offset 780 */
0, 28, 42, 0, 3, 3,
0, 20, 30, /* snap_x */
-42, -14, 0, /* snap_y */
'm', 20, 0,
'l', 20, -42,
'l', 0, -14,
'l', 30, -14,
'e',
'X', 'X', 'X',
'X',
/* 0x35 '5' offset 809 */
0, 28, 42, 0, 2, 5,
0, 28, /* snap_x */
-42, -28, -21, -15, 0, /* snap_y */
'm', 24, -42,
'l', 4, -42,
'l', 2, -24,
'c', 5, -27, 10, -28, 13, -28,
'c', 16, -28, 28, -28, 28, -14,
'c', 28, 0, 16, 0, 13, 0,
'c', 10, 0, 3, 0, 0, -8,
'e',
/* 0x36 '6' offset 860 */
0, 28, 42, 0, 2, 5,
0, 26, /* snap_x */
-42, -26, -21, -15, 0, /* snap_y */
'm', 24, -36,
'c', 22, -41, 19, -42, 14, -42,
'c', 9, -42, 0, -41, 0, -19,
'c', 0, -1, 9, 0, 13, 0,
'c', 18, 0, 26, -3, 26, -13,
'c', 26, -18, 23, -26, 13, -26,
'c', 10, -26, 1, -24, 0, -14,
'e',
/* 0x37 '7' offset 919 */
0, 28, 42, 0, 2, 4,
0, 28, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 0, -42,
'l', 28, -42,
'l', 8, 0,
'e',
'X', 'X', 'X',
/* 0x38 '8' offset 944 */
0, 28, 42, 0, 4, 4,
0, 2, 26, 28, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 14, -42,
'c', 5, -42, 2, -40, 2, -34,
'c', 2, -18, 28, -32, 28, -11,
'c', 28, 0, 18, 0, 14, 0,
'c', 10, 0, 0, 0, 0, -11,
'c', 0, -32, 26, -18, 26, -34,
'c', 26, -40, 23, -42, 14, -42,
'E',
/* 0x39 '9' offset 1004 */
0, 28, 42, 0, 2, 5,
0, 26, /* snap_x */
-42, -21, -16, -15, 0, /* snap_y */
'm', 26, -28,
'c', 25, -16, 13, -16, 13, -16,
'c', 8, -16, 0, -19, 0, -29,
'c', 0, -34, 3, -42, 13, -42,
'c', 24, -42, 26, -32, 26, -23,
'c', 26, -14, 24, 0, 12, 0,
'c', 7, 0, 4, -2, 2, -6,
'e',
/* 0x3a ':' offset 1063 */
0, 4, 28, 0, 2, 3,
0, 4, /* snap_x */
-21, -15, 0, /* snap_y */
'm', 2, -28,
'c', -1, -28, -1, -24, 2, -24,
'c', 5, -24, 5, -28, 2, -28,
'm', 2, -4,
'c', -1, -4, -1, 0, 2, 0,
'c', 5, 0, 5, -4, 2, -4,
'e',
/* 0x3b ';' offset 1109 */
0, 4, 28, 8, 2, 3,
0, 4, /* snap_x */
-21, -15, 0, /* snap_y */
'm', 2, -28,
'c', -1, -28, -1, -24, 2, -24,
'c', 5, -24, 5, -28, 2, -28,
'm', 4, -2,
'c', 4, 1, 0, 1, 0, -2,
'c', 0, -5, 4, -5, 4, -2,
'c', 4, 3, 2, 6, 0, 8,
'e',
/* 0x3c '<' offset 1162 */
0, 32, 36, 0, 2, 3,
0, 32, /* snap_x */
-36, -18, 0, /* snap_y */
'm', 32, -36,
'l', 0, -18,
'l', 32, 0,
'e',
/* 0x3d '=' offset 1183 */
0, 36, 24, -12, 2, 2,
0, 36, /* snap_x */
-24, -15, /* snap_y */
'm', 0, -24,
'l', 36, -24,
'm', 0, -12,
'l', 36, -12,
'e',
'X', 'X', 'X',
/* 0x3e '>' offset 1209 */
0, 32, 36, 0, 2, 3,
0, 32, /* snap_x */
-36, -18, 0, /* snap_y */
'm', 0, -36,
'l', 32, -18,
'l', 0, 0,
'e',
/* 0x3f '?' offset 1230 */
0, 24, 42, 0, 3, 4,
0, 12, 24, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 0, -32,
'c', 0, -34, 0, -42, 12, -42,
'c', 24, -42, 24, -34, 24, -32,
'c', 24, -29, 24, -24, 12, -20,
'l', 12, -14,
'm', 12, 0,
'l', 12, 0,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X',
/* 0x40 '@' offset 1288 */
0, 42, 42, 0, 1, 6,
30, /* snap_x */
-42, -32, -21, -15, -10, 0, /* snap_y */
'm', 30, -26,
'c', 28, -31, 24, -32, 21, -32,
'c', 10, -32, 10, -23, 10, -19,
'c', 10, -13, 11, -10, 19, -10,
'c', 30, -10, 28, -21, 30, -32,
'c', 27, -10, 30, -10, 34, -10,
'c', 41, -10, 42, -19, 42, -22,
'c', 42, -34, 34, -42, 21, -42,
'c', 9, -42, 0, -34, 0, -21,
'c', 0, -9, 8, 0, 21, 0,
'c', 30, 0, 34, -3, 36, -6,
'e',
/* 0x41 'A' offset 1375 */
0, 32, 42, 0, 2, 3,
0, 32, /* snap_x */
-42, -14, 0, /* snap_y */
'm', 0, 0,
'l', 16, -42,
'l', 32, 0,
'm', 6, -14,
'l', 26, -14,
'e',
'X', 'X', 'X',
'X',
/* 0x42 'B' offset 1406 */
0, 28, 42, 0, 2, 3,
0, 28, /* snap_x */
-42, -22, 0, /* snap_y */
'm', 0, 0,
'l', 0, -42,
'l', 18, -42,
'c', 32, -42, 32, -22, 18, -22,
'l', 0, -22,
'l', 18, -22,
'c', 32, -22, 32, 0, 18, 0,
'E',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X',
/* 0x43 'C' offset 1455 */
0, 30, 42, 0, 2, 4,
0, 30, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 30, -32,
'c', 26, -42, 21, -42, 16, -42,
'c', 2, -42, 0, -29, 0, -21,
'c', 0, -13, 2, 0, 16, 0,
'c', 21, 0, 26, 0, 30, -10,
'e',
/* 0x44 'D' offset 1499 */
0, 28, 42, 0, 2, 2,
0, 28, /* snap_x */
-42, 0, /* snap_y */
'm', 0, 0,
'l', 0, -42,
'l', 14, -42,
'c', 33, -42, 33, 0, 14, 0,
'E',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X',
/* 0x45 'E' offset 1534 */
0, 26, 42, 0, 2, 3,
0, 26, /* snap_x */
-42, -22, 0, /* snap_y */
'm', 26, -42,
'l', 0, -42,
'l', 0, 0,
'l', 26, 0,
'm', 0, -22,
'l', 16, -22,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X',
/* 0x46 'F' offset 1572 */
0, 26, 42, 0, 2, 3,
0, 26, /* snap_x */
-42, -22, 0, /* snap_y */
'm', 0, 0,
'l', 0, -42,
'l', 26, -42,
'm', 0, -22,
'l', 16, -22,
'e',
'X', 'X', 'X',
'X', 'X',
/* 0x47 'G' offset 1604 */
0, 30, 42, 0, 2, 5,
0, 30, /* snap_x */
-42, -21, -16, -15, 0, /* snap_y */
'm', 30, -32,
'c', 26, -42, 21, -42, 16, -42,
'c', 2, -42, 0, -29, 0, -21,
'c', 0, -13, 2, 0, 16, 0,
'c', 28, 0, 30, -7, 30, -16,
'l', 20, -16,
'e',
'X', 'X', 'X',
/* 0x48 'H' offset 1655 */
0, 28, 42, 0, 2, 3,
0, 28, /* snap_x */
-42, -22, 0, /* snap_y */
'm', 0, -42,
'l', 0, 0,
'm', 28, -42,
'l', 28, 0,
'm', 0, -22,
'l', 28, -22,
'e',
'X',
/* 0x49 'I' offset 1686 */
0, 0, 42, 0, 1, 2,
0, /* snap_x */
-42, 0, /* snap_y */
'm', 0, -42,
'l', 0, 0,
'e',
'X',
/* 0x4a 'J' offset 1703 */
0, 20, 42, 0, 2, 3,
0, 20, /* snap_x */
-42, -15, 0, /* snap_y */
'm', 20, -42,
'l', 20, -10,
'c', 20, 3, 0, 3, 0, -10,
'l', 0, -14,
'e',
/* 0x4b 'K' offset 1731 */
0, 28, 42, 0, 2, 3,
0, 28, /* snap_x */
-42, -15, 0, /* snap_y */
'm', 0, -42,
'l', 0, 0,
'm', 28, -42,
'l', 0, -14,
'm', 10, -24,
'l', 28, 0,
'e',
/* 0x4c 'L' offset 1761 */
0, 24, 42, 0, 2, 2,
0, 24, /* snap_x */
-42, 0, /* snap_y */
'm', 0, -42,
'l', 0, 0,
'l', 24, 0,
'e',
'X', 'X', 'X',
'X',
/* 0x4d 'M' offset 1785 */
0, 32, 42, 0, 2, 2,
0, 32, /* snap_x */
-42, 0, /* snap_y */
'm', 0, 0,
'l', 0, -42,
'l', 16, 0,
'l', 32, -42,
'l', 32, 0,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X', 'X',
'X',
/* 0x4e 'N' offset 1821 */
0, 28, 42, 0, 2, 2,
0, 28, /* snap_x */
-42, 0, /* snap_y */
'm', 0, 0,
'l', 0, -42,
'l', 28, 0,
'l', 28, -42,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
'X',
/* 0x4f 'O' offset 1851 */
0, 32, 42, 0, 2, 4,
0, 32, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 16, -42,
'c', 2, -42, 0, -29, 0, -21,
'c', 0, -13, 2, 0, 16, 0,
'c', 30, 0, 32, -13, 32, -21,
'c', 32, -29, 30, -42, 16, -42,
'E',
/* 0x50 'P' offset 1895 */
0, 28, 42, 0, 2, 5,
0, 28, /* snap_x */
-42, -21, -20, -15, 0, /* snap_y */
'm', 0, 0,
'l', 0, -42,
'l', 18, -42,
'c', 32, -42, 32, -20, 18, -20,
'l', 0, -20,
'e',
'X', 'X', 'X',
/* 0x51 'Q' offset 1931 */
0, 32, 42, 4, 2, 4,
0, 32, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 16, -42,
'c', 2, -42, 0, -29, 0, -21,
'c', 0, -13, 2, 0, 16, 0,
'c', 30, 0, 32, -13, 32, -21,
'c', 32, -29, 30, -42, 16, -42,
'M', 18, -8,
'l', 30, 4,
'e',
/* 0x52 'R' offset 1981 */
0, 28, 42, 0, 2, 5,
0, 28, /* snap_x */
-42, -22, -21, -15, 0, /* snap_y */
'm', 0, 0,
'l', 0, -42,
'l', 18, -42,
'c', 32, -42, 31, -22, 18, -22,
'l', 0, -22,
'm', 14, -22,
'l', 28, 0,
'e',
'X', 'X', 'X',
/* 0x53 'S' offset 2023 */
0, 28, 42, 0, 2, 4,
0, 28, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 28, -36,
'c', 25, -41, 21, -42, 14, -42,
'c', 10, -42, 0, -42, 0, -34,
'c', 0, -17, 28, -28, 28, -9,
'c', 28, 0, 19, 0, 14, 0,
'c', 7, 0, 3, -1, 0, -6,
'e',
/* 0x54 'T' offset 2074 */
0, 28, 42, 0, 3, 4,
0, 14, 28, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 14, -42,
'l', 14, 0,
'm', 0, -42,
'l', 28, -42,
'e',
/* 0x55 'U' offset 2100 */
0, 28, 42, 0, 2, 2,
0, 28, /* snap_x */
-42, 0, /* snap_y */
'm', 0, -42,
'l', 0, -12,
'c', 0, 4, 28, 4, 28, -12,
'l', 28, -42,
'e',
'X',
/* 0x56 'V' offset 2128 */
0, 32, 42, 0, 2, 2,
0, 32, /* snap_x */
-42, 0, /* snap_y */
'm', 0, -42,
'l', 16, 0,
'l', 32, -42,
'e',
'X', 'X', 'X',
'X',
/* 0x57 'W' offset 2152 */
0, 40, 42, 0, 2, 2,
0, 40, /* snap_x */
-42, 0, /* snap_y */
'm', 0, -42,
'l', 10, 0,
'l', 20, -42,
'l', 30, 0,
'l', 40, -42,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X', 'X',
'X',
/* 0x58 'X' offset 2188 */
0, 28, 42, 0, 2, 2,
0, 28, /* snap_x */
-42, 0, /* snap_y */
'm', 0, -42,
'l', 28, 0,
'm', 28, -42,
'l', 0, 0,
'e',
'X',
/* 0x59 'Y' offset 2212 */
0, 32, 42, 0, 3, 3,
0, 16, 32, /* snap_x */
-42, -21, 0, /* snap_y */
'm', 0, -42,
'l', 16, -22,
'l', 16, 0,
'm', 32, -42,
'l', 16, -22,
'e',
/* 0x5a 'Z' offset 2240 */
0, 28, 42, 0, 2, 4,
0, 28, /* snap_x */
-42, -21, -15, 0, /* snap_y */
'm', 28, 0,
'l', 0, 0,
'l', 28, -42,
'l', 0, -42,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
/* 0x5b '[' offset 2271 */
0, 14, 44, 0, 2, 4,
0, 14, /* snap_x */
-44, -21, -15, 0, /* snap_y */
'm', 14, -44,
'l', 0, -44,
'l', 0, 0,
'l', 14, 0,
'e',
/* 0x5c '\' offset 2296 */
0, 36, 50, 14, 2, 3,
0, 36, /* snap_x */
-21, -15, 0, /* snap_y */
'm', 0, -50,
'l', 36, 14,
'e',
/* 0x5d ']' offset 2314 */
0, 14, 44, 0, 2, 4,
0, 14, /* snap_x */
-44, -21, -15, 0, /* snap_y */
'm', 0, -44,
'l', 14, -44,
'l', 14, 0,
'l', 0, 0,
'e',
/* 0x5e '^' offset 2339 */
0, 32, 46, -18, 2, 3,
0, 32, /* snap_x */
-21, -15, 0, /* snap_y */
'm', 0, -18,
'l', 16, -46,
'l', 32, -18,
'e',
'X', 'X', 'X',
/* 0x5f '_' offset 2363 */
0, 36, 0, 0, 2, 1,
0, 36, /* snap_x */
0, /* snap_y */
'm', 0, 0,
'l', 36, 0,
'e',
'X', 'X',
/* 0x60 '`' offset 2381 */
0, 4, 42, -30, 2, 2,
0, 4, /* snap_x */
-42, 0, /* snap_y */
'm', 4, -42,
'c', 2, -40, 0, -39, 0, -32,
'c', 0, -31, 1, -30, 2, -30,
'c', 5, -30, 5, -34, 2, -34,
'e',
'X',
/* 0x61 'a' offset 2417 */
0, 24, 28, 0, 2, 4,
0, 24, /* snap_x */
-28, -21, -15, 0, /* snap_y */
'm', 24, -28,
'l', 24, 0,
'm', 24, -22,
'c', 21, -27, 18, -28, 13, -28,
'c', 2, -28, 0, -19, 0, -14,
'c', 0, -9, 2, 0, 13, 0,
'c', 18, 0, 21, -1, 24, -6,
'e',
/* 0x62 'b' offset 2467 */
0, 24, 42, 0, 2, 4,
0, 24, /* snap_x */
-42, -28, -15, 0, /* snap_y */
'm', 0, -42,
'l', 0, 0,
'm', 0, -22,
'c', 3, -26, 6, -28, 11, -28,
'c', 22, -28, 24, -19, 24, -14,
'c', 24, -9, 22, 0, 11, 0,
'c', 6, 0, 3, -2, 0, -6,
'e',
/* 0x63 'c' offset 2517 */
0, 24, 28, 0, 2, 4,
0, 24, /* snap_x */
-28, -21, -15, 0, /* snap_y */
'm', 24, -22,
'c', 21, -26, 18, -28, 13, -28,
'c', 2, -28, 0, -19, 0, -14,
'c', 0, -9, 2, 0, 13, 0,
'c', 18, 0, 21, -2, 24, -6,
'e',
/* 0x64 'd' offset 2561 */
0, 24, 42, 0, 2, 4,
0, 24, /* snap_x */
-42, -28, -15, 0, /* snap_y */
'm', 24, -42,
'l', 24, 0,
'm', 24, -22,
'c', 21, -26, 18, -28, 13, -28,
'c', 2, -28, 0, -19, 0, -14,
'c', 0, -9, 2, 0, 13, 0,
'c', 18, 0, 21, -2, 24, -6,
'e',
/* 0x65 'e' offset 2611 */
0, 24, 28, 0, 2, 5,
0, 24, /* snap_x */
-28, -21, -16, -15, 0, /* snap_y */
'm', 0, -16,
'l', 24, -16,
'c', 24, -20, 24, -28, 13, -28,
'c', 2, -28, 0, -19, 0, -14,
'c', 0, -9, 2, 0, 13, 0,
'c', 18, 0, 21, -2, 24, -6,
'e',
/* 0x66 'f' offset 2659 */
0, 16, 42, 0, 3, 5,
0, 6, 16, /* snap_x */
-42, -28, -21, -15, 0, /* snap_y */
'm', 16, -42,
'c', 8, -42, 6, -40, 6, -34,
'l', 6, 0,
'm', 0, -28,
'l', 14, -28,
'e',
/* 0x67 'g' offset 2693 */
0, 24, 28, 14, 2, 5,
0, 24, /* snap_x */
-28, -21, -15, 0, 14, /* snap_y */
'm', 24, -28,
'l', 24, 4,
'c', 23, 14, 16, 14, 13, 14,
'c', 10, 14, 8, 14, 6, 12,
'm', 24, -22,
'c', 21, -26, 18, -28, 13, -28,
'c', 2, -28, 0, -19, 0, -14,
'c', 0, -9, 2, 0, 13, 0,
'c', 18, 0, 21, -2, 24, -6,
'e',
/* 0x68 'h' offset 2758 */
0, 22, 42, 0, 2, 4,
0, 22, /* snap_x */
-42, -28, -15, 0, /* snap_y */
'm', 0, -42,
'l', 0, 0,
'm', 0, -20,
'c', 8, -32, 22, -31, 22, -20,
'l', 22, 0,
'e',
/* 0x69 'i' offset 2790 */
0, 0, 44, 0, 1, 3,
0, /* snap_x */
-42, -28, 0, /* snap_y */
'm', 0, -42,
'l', 0, -42,
'm', 0, -28,
'l', 0, 0,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X',
'X', 'X',
/* 0x6a 'j' offset 2826 */
-8, 4, 44, 14, 3, 5,
-8, 2, 4, /* snap_x */
-42, -21, -15, 0, 14, /* snap_y */
'm', 2, -42,
'l', 2, -42,
'm', 2, -28,
'l', 2, 6,
'c', 2, 13, -1, 14, -8, 14,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X', 'X',
'X',
/* 0x6b 'k' offset 2870 */
0, 22, 42, 0, 2, 3,
0, 22, /* snap_x */
-42, -28, 0, /* snap_y */
'm', 0, -42,
'l', 0, 0,
'm', 20, -28,
'l', 0, -8,
'm', 8, -16,
'l', 22, 0,
'e',
/* 0x6c 'l' offset 2900 */
0, 0, 42, 0, 1, 2,
0, /* snap_x */
-42, 0, /* snap_y */
'm', 0, -42,
'l', 0, 0,
'e',
'X',
/* 0x6d 'm' offset 2917 */
0, 44, 28, 0, 3, 3,
0, 22, 44, /* snap_x */
-28, -21, 0, /* snap_y */
'm', 0, -28,
'l', 0, 0,
'm', 0, -20,
'c', 5, -29, 22, -33, 22, -20,
'l', 22, 0,
'm', 22, -20,
'c', 27, -29, 44, -33, 44, -20,
'l', 44, 0,
'e',
'X',
/* 0x6e 'n' offset 2963 */
0, 22, 28, 0, 2, 3,
0, 22, /* snap_x */
-28, -21, 0, /* snap_y */
'm', 0, -28,
'l', 0, 0,
'm', 0, -20,
'c', 4, -28, 22, -34, 22, -20,
'l', 22, 0,
'e',
'X',
/* 0x6f 'o' offset 2995 */
0, 26, 28, 0, 2, 4,
0, 26, /* snap_x */
-28, -21, -15, 0, /* snap_y */
'm', 13, -28,
'c', 2, -28, 0, -19, 0, -14,
'c', 0, -9, 2, 0, 13, 0,
'c', 24, 0, 26, -9, 26, -14,
'c', 26, -19, 24, -28, 13, -28,
'E',
/* 0x70 'p' offset 3039 */
0, 24, 28, 14, 2, 4,
0, 24, /* snap_x */
-28, -21, 0, 14, /* snap_y */
'm', 0, -28,
'l', 0, 14,
'm', 0, -22,
'c', 3, -26, 6, -28, 11, -28,
'c', 22, -28, 24, -19, 24, -14,
'c', 24, -9, 22, 0, 11, 0,
'c', 6, 0, 3, -2, 0, -6,
'e',
/* 0x71 'q' offset 3089 */
0, 24, 28, 14, 2, 4,
0, 24, /* snap_x */
-28, -21, 0, 14, /* snap_y */
'm', 24, -28,
'l', 24, 14,
'm', 24, -22,
'c', 21, -26, 18, -28, 13, -28,
'c', 2, -28, 0, -19, 0, -14,
'c', 0, -9, 2, 0, 13, 0,
'c', 18, 0, 21, -2, 24, -6,
'e',
/* 0x72 'r' offset 3139 */
0, 16, 28, 0, 2, 4,
0, 16, /* snap_x */
-28, -21, -15, 0, /* snap_y */
'm', 0, -28,
'l', 0, 0,
'm', 0, -16,
'c', 2, -27, 7, -28, 16, -28,
'e',
/* 0x73 's' offset 3168 */
0, 22, 28, 0, 2, 4,
0, 22, /* snap_x */
-28, -21, -15, 0, /* snap_y */
'm', 22, -22,
'c', 22, -27, 16, -28, 11, -28,
'c', 4, -28, 0, -26, 0, -22,
'c', 0, -11, 22, -20, 22, -7,
'c', 22, 0, 17, 0, 11, 0,
'c', 6, 0, 0, -1, 0, -6,
'e',
/* 0x74 't' offset 3219 */
0, 16, 42, 0, 3, 4,
0, 6, 16, /* snap_x */
-42, -28, -21, 0, /* snap_y */
'm', 6, -42,
'l', 6, -8,
'c', 6, -2, 8, 0, 16, 0,
'm', 0, -28,
'l', 14, -28,
'e',
/* 0x75 'u' offset 3252 */
0, 22, 28, 0, 2, 3,
0, 22, /* snap_x */
-28, -15, 0, /* snap_y */
'm', 0, -28,
'l', 0, -8,
'c', 0, 6, 18, 0, 22, -8,
'm', 22, -28,
'l', 22, 0,
'e',
/* 0x76 'v' offset 3283 */
0, 24, 28, 0, 2, 3,
0, 24, /* snap_x */
-28, -15, 0, /* snap_y */
'm', 0, -28,
'l', 12, 0,
'l', 24, -28,
'e',
'X', 'X', 'X',
/* 0x77 'w' offset 3307 */
0, 32, 28, 0, 2, 3,
0, 32, /* snap_x */
-28, -15, 0, /* snap_y */
'm', 0, -28,
'l', 8, 0,
'l', 16, -28,
'l', 24, 0,
'l', 32, -28,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
'X', 'X', 'X',
/* 0x78 'x' offset 3343 */
0, 22, 28, 0, 2, 2,
0, 22, /* snap_x */
-28, 0, /* snap_y */
'm', 0, -28,
'l', 22, 0,
'm', 22, -28,
'l', 0, 0,
'e',
'X',
/* 0x79 'y' offset 3367 */
-2, 24, 28, 14, 2, 4,
0, 24, /* snap_x */
-28, -15, 0, 14, /* snap_y */
'm', 0, -28,
'l', 12, 0,
'm', 24, -28,
'l', 12, 0,
'c', 6, 13, 0, 14, -2, 14,
'e',
/* 0x7a 'z' offset 3399 */
0, 22, 28, 0, 2, 4,
0, 22, /* snap_x */
-28, -21, -15, 0, /* snap_y */
'm', 22, 0,
'l', 0, 0,
'l', 22, -28,
'l', 0, -28,
'e',
'X', 'X', 'X',
'X', 'X', 'X',
/* 0x7b '{' offset 3430 */
0, 16, 44, 0, 3, 5,
0, 6, 16, /* snap_x */
-44, -24, -21, -15, 0, /* snap_y */
'm', 16, -44,
'c', 10, -44, 6, -42, 6, -36,
'l', 6, -24,
'l', 0, -24,
'l', 6, -24,
'l', 6, -8,
'c', 6, -2, 10, 0, 16, 0,
'e',
/* 0x7c '|' offset 3474 */
0, 0, 50, 14, 1, 2,
0, /* snap_x */
-50, 14, /* snap_y */
'm', 0, -50,
'l', 0, 14,
'e',
'X',
/* 0x7d '}' offset 3491 */
0, 16, 44, 0, 3, 5,
0, 10, 16, /* snap_x */
-44, -24, -21, -15, 0, /* snap_y */
'm', 0, -44,
'c', 6, -44, 10, -42, 10, -36,
'l', 10, -24,
'l', 16, -24,
'l', 10, -24,
'l', 10, -8,
'c', 10, -2, 6, 0, 0, 0,
'e',
/* 0x7e '~' offset 3535 */
0, 36, 24, -12, 2, 5,
0, 36, /* snap_x */
-24, -21, -15, -12, 0, /* snap_y */
'm', 0, -14,
'c', 1, -21, 4, -24, 8, -24,
'c', 18, -24, 18, -12, 28, -12,
'c', 32, -12, 35, -15, 36, -22,
'e',
};
const uint16_t _cairo_twin_charmap[128] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
28, 40, 90, 114, 152, 224, 323, 390,
419, 441, 463, 494, 520, 556, 575, 604,
622, 666, 691, 736, 780, 809, 860, 919,
944, 1004, 1063, 1109, 1162, 1183, 1209, 1230,
1288, 1375, 1406, 1455, 1499, 1534, 1572, 1604,
1655, 1686, 1703, 1731, 1761, 1785, 1821, 1851,
1895, 1931, 1981, 2023, 2074, 2100, 2128, 2152,
2188, 2212, 2240, 2271, 2296, 2314, 2339, 2363,
2381, 2417, 2467, 2517, 2561, 2611, 2659, 2693,
2758, 2790, 2826, 2870, 2900, 2917, 2963, 2995,
3039, 3089, 3139, 3168, 3219, 3252, 3283, 3307,
3343, 3367, 3399, 3430, 3474, 3491, 3535, 0,
};
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-font-face-twin.c | /*
* Copyright © 2004 Keith Packard
* Copyright © 2008 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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>
* Behdad Esfahbod <behdad@behdad.org>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include <math.h>
/*
* This file implements a user-font rendering the descendant of the Hershey
* font coded by Keith Packard for use in the Twin window system.
* The actual font data is in cairo-font-face-twin-data.c
*
* Ported to cairo user font and extended by Behdad Esfahbod.
*/
static cairo_user_data_key_t twin_properties_key;
/*
* Face properties
*/
/* We synthesize multiple faces from the twin data. Here is the parameters. */
/* The following tables and matching code are copied from Pango */
/* CSS weight */
typedef enum {
TWIN_WEIGHT_THIN = 100,
TWIN_WEIGHT_ULTRALIGHT = 200,
TWIN_WEIGHT_LIGHT = 300,
TWIN_WEIGHT_BOOK = 380,
TWIN_WEIGHT_NORMAL = 400,
TWIN_WEIGHT_MEDIUM = 500,
TWIN_WEIGHT_SEMIBOLD = 600,
TWIN_WEIGHT_BOLD = 700,
TWIN_WEIGHT_ULTRABOLD = 800,
TWIN_WEIGHT_HEAVY = 900,
TWIN_WEIGHT_ULTRAHEAVY = 1000
} twin_face_weight_t;
/* CSS stretch */
typedef enum {
TWIN_STRETCH_ULTRA_CONDENSED,
TWIN_STRETCH_EXTRA_CONDENSED,
TWIN_STRETCH_CONDENSED,
TWIN_STRETCH_SEMI_CONDENSED,
TWIN_STRETCH_NORMAL,
TWIN_STRETCH_SEMI_EXPANDED,
TWIN_STRETCH_EXPANDED,
TWIN_STRETCH_EXTRA_EXPANDED,
TWIN_STRETCH_ULTRA_EXPANDED
} twin_face_stretch_t;
typedef struct
{
int value;
const char str[16];
} FieldMap;
static const FieldMap slant_map[] = {
{ CAIRO_FONT_SLANT_NORMAL, "" },
{ CAIRO_FONT_SLANT_NORMAL, "Roman" },
{ CAIRO_FONT_SLANT_OBLIQUE, "Oblique" },
{ CAIRO_FONT_SLANT_ITALIC, "Italic" }
};
static const FieldMap smallcaps_map[] = {
{ FALSE, "" },
{ TRUE, "Small-Caps" }
};
static const FieldMap weight_map[] = {
{ TWIN_WEIGHT_THIN, "Thin" },
{ TWIN_WEIGHT_ULTRALIGHT, "Ultra-Light" },
{ TWIN_WEIGHT_ULTRALIGHT, "Extra-Light" },
{ TWIN_WEIGHT_LIGHT, "Light" },
{ TWIN_WEIGHT_BOOK, "Book" },
{ TWIN_WEIGHT_NORMAL, "" },
{ TWIN_WEIGHT_NORMAL, "Regular" },
{ TWIN_WEIGHT_MEDIUM, "Medium" },
{ TWIN_WEIGHT_SEMIBOLD, "Semi-Bold" },
{ TWIN_WEIGHT_SEMIBOLD, "Demi-Bold" },
{ TWIN_WEIGHT_BOLD, "Bold" },
{ TWIN_WEIGHT_ULTRABOLD, "Ultra-Bold" },
{ TWIN_WEIGHT_ULTRABOLD, "Extra-Bold" },
{ TWIN_WEIGHT_HEAVY, "Heavy" },
{ TWIN_WEIGHT_HEAVY, "Black" },
{ TWIN_WEIGHT_ULTRAHEAVY, "Ultra-Heavy" },
{ TWIN_WEIGHT_ULTRAHEAVY, "Extra-Heavy" },
{ TWIN_WEIGHT_ULTRAHEAVY, "Ultra-Black" },
{ TWIN_WEIGHT_ULTRAHEAVY, "Extra-Black" }
};
static const FieldMap stretch_map[] = {
{ TWIN_STRETCH_ULTRA_CONDENSED, "Ultra-Condensed" },
{ TWIN_STRETCH_EXTRA_CONDENSED, "Extra-Condensed" },
{ TWIN_STRETCH_CONDENSED, "Condensed" },
{ TWIN_STRETCH_SEMI_CONDENSED, "Semi-Condensed" },
{ TWIN_STRETCH_NORMAL, "" },
{ TWIN_STRETCH_SEMI_EXPANDED, "Semi-Expanded" },
{ TWIN_STRETCH_EXPANDED, "Expanded" },
{ TWIN_STRETCH_EXTRA_EXPANDED, "Extra-Expanded" },
{ TWIN_STRETCH_ULTRA_EXPANDED, "Ultra-Expanded" }
};
static const FieldMap monospace_map[] = {
{ FALSE, "" },
{ TRUE, "Mono" },
{ TRUE, "Monospace" }
};
typedef struct _twin_face_properties {
cairo_font_slant_t slant;
twin_face_weight_t weight;
twin_face_stretch_t stretch;
/* lets have some fun */
cairo_bool_t monospace;
cairo_bool_t smallcaps;
} twin_face_properties_t;
static cairo_bool_t
field_matches (const char *s1,
const char *s2,
int len)
{
int c1, c2;
while (len && *s1 && *s2)
{
#define TOLOWER(c) \
(((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))
c1 = TOLOWER (*s1);
c2 = TOLOWER (*s2);
if (c1 != c2) {
if (c1 == '-') {
s1++;
continue;
}
return FALSE;
}
s1++; s2++;
len--;
}
return len == 0 && *s1 == '\0';
}
static cairo_bool_t
parse_int (const char *word,
size_t wordlen,
int *out)
{
char *end;
long val = strtol (word, &end, 10);
int i = val;
if (end != word && (end == word + wordlen) && val >= 0 && val == i)
{
if (out)
*out = i;
return TRUE;
}
return FALSE;
}
static cairo_bool_t
find_field (const char *what,
const FieldMap *map,
int n_elements,
const char *str,
int len,
int *val)
{
int i;
cairo_bool_t had_prefix = FALSE;
if (what)
{
i = strlen (what);
if (len > i && 0 == strncmp (what, str, i) && str[i] == '=')
{
str += i + 1;
len -= i + 1;
had_prefix = TRUE;
}
}
for (i=0; i<n_elements; i++)
{
if (map[i].str[0] && field_matches (map[i].str, str, len))
{
if (val)
*val = map[i].value;
return TRUE;
}
}
if (!what || had_prefix)
return parse_int (str, len, val);
return FALSE;
}
static void
parse_field (twin_face_properties_t *props,
const char *str,
int len)
{
if (field_matches ("Normal", str, len))
return;
#define FIELD(NAME) \
if (find_field (STRINGIFY (NAME), NAME##_map, ARRAY_LENGTH (NAME##_map), str, len, \
(int *)(void *)&props->NAME)) \
return; \
FIELD (weight);
FIELD (slant);
FIELD (stretch);
FIELD (smallcaps);
FIELD (monospace);
#undef FIELD
}
static void
face_props_parse (twin_face_properties_t *props,
const char *s)
{
const char *start, *end;
for (start = end = s; *end; end++) {
if (*end != ' ' && *end != ':')
continue;
if (start < end)
parse_field (props, start, end - start);
start = end + 1;
}
if (start < end)
parse_field (props, start, end - start);
}
static twin_face_properties_t *
twin_font_face_create_properties (cairo_font_face_t *twin_face)
{
twin_face_properties_t *props;
props = _cairo_malloc (sizeof (twin_face_properties_t));
if (unlikely (props == NULL))
return NULL;
props->stretch = TWIN_STRETCH_NORMAL;
props->slant = CAIRO_FONT_SLANT_NORMAL;
props->weight = TWIN_WEIGHT_NORMAL;
props->monospace = FALSE;
props->smallcaps = FALSE;
if (unlikely (cairo_font_face_set_user_data (twin_face,
&twin_properties_key,
props, free))) {
free (props);
return NULL;
}
return props;
}
static cairo_status_t
twin_font_face_set_properties_from_toy (cairo_font_face_t *twin_face,
cairo_toy_font_face_t *toy_face)
{
twin_face_properties_t *props;
props = twin_font_face_create_properties (twin_face);
if (unlikely (props == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
props->slant = toy_face->slant;
props->weight = toy_face->weight == CAIRO_FONT_WEIGHT_NORMAL ?
TWIN_WEIGHT_NORMAL : TWIN_WEIGHT_BOLD;
face_props_parse (props, toy_face->family);
return CAIRO_STATUS_SUCCESS;
}
/*
* Scaled properties
*/
typedef struct _twin_scaled_properties {
twin_face_properties_t *face_props;
cairo_bool_t snap; /* hint outlines */
double weight; /* unhinted pen width */
double penx, peny; /* hinted pen width */
double marginl, marginr; /* hinted side margins */
double stretch; /* stretch factor */
} twin_scaled_properties_t;
static void
compute_hinting_scale (cairo_t *cr,
double x, double y,
double *scale, double *inv)
{
cairo_user_to_device_distance (cr, &x, &y);
*scale = x == 0 ? y : y == 0 ? x :sqrt (x*x + y*y);
*inv = 1 / *scale;
}
static void
compute_hinting_scales (cairo_t *cr,
double *x_scale, double *x_scale_inv,
double *y_scale, double *y_scale_inv)
{
double x, y;
x = 1; y = 0;
compute_hinting_scale (cr, x, y, x_scale, x_scale_inv);
x = 0; y = 1;
compute_hinting_scale (cr, x, y, y_scale, y_scale_inv);
}
#define SNAPXI(p) (_cairo_round ((p) * x_scale) * x_scale_inv)
#define SNAPYI(p) (_cairo_round ((p) * y_scale) * y_scale_inv)
/* This controls the global font size */
#define F(g) ((g) / 72.)
static void
twin_hint_pen_and_margins(cairo_t *cr,
double *penx, double *peny,
double *marginl, double *marginr)
{
double x_scale, x_scale_inv;
double y_scale, y_scale_inv;
double margin;
compute_hinting_scales (cr,
&x_scale, &x_scale_inv,
&y_scale, &y_scale_inv);
*penx = SNAPXI (*penx);
if (*penx < x_scale_inv)
*penx = x_scale_inv;
*peny = SNAPYI (*peny);
if (*peny < y_scale_inv)
*peny = y_scale_inv;
margin = *marginl + *marginr;
*marginl = SNAPXI (*marginl);
if (*marginl < x_scale_inv)
*marginl = x_scale_inv;
*marginr = margin - *marginl;
if (*marginr < 0)
*marginr = 0;
*marginr = SNAPXI (*marginr);
}
static cairo_status_t
twin_scaled_font_compute_properties (cairo_scaled_font_t *scaled_font,
cairo_t *cr)
{
cairo_status_t status;
twin_scaled_properties_t *props;
props = _cairo_malloc (sizeof (twin_scaled_properties_t));
if (unlikely (props == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
props->face_props = cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
&twin_properties_key);
props->snap = scaled_font->options.hint_style > CAIRO_HINT_STYLE_NONE;
/* weight */
props->weight = props->face_props->weight * (F (4) / TWIN_WEIGHT_NORMAL);
/* pen & margins */
props->penx = props->peny = props->weight;
props->marginl = props->marginr = F (4);
if (scaled_font->options.hint_style > CAIRO_HINT_STYLE_SLIGHT)
twin_hint_pen_and_margins(cr,
&props->penx, &props->peny,
&props->marginl, &props->marginr);
/* stretch */
props->stretch = 1 + .1 * ((int) props->face_props->stretch - (int) TWIN_STRETCH_NORMAL);
/* Save it */
status = cairo_scaled_font_set_user_data (scaled_font,
&twin_properties_key,
props, free);
if (unlikely (status))
goto FREE_PROPS;
return CAIRO_STATUS_SUCCESS;
FREE_PROPS:
free (props);
return status;
}
/*
* User-font implementation
*/
static cairo_status_t
twin_scaled_font_init (cairo_scaled_font_t *scaled_font,
cairo_t *cr,
cairo_font_extents_t *metrics)
{
metrics->ascent = F (54);
metrics->descent = 1 - metrics->ascent;
return twin_scaled_font_compute_properties (scaled_font, cr);
}
#define TWIN_GLYPH_MAX_SNAP_X 4
#define TWIN_GLYPH_MAX_SNAP_Y 7
typedef struct {
int n_snap_x;
int8_t snap_x[TWIN_GLYPH_MAX_SNAP_X];
double snapped_x[TWIN_GLYPH_MAX_SNAP_X];
int n_snap_y;
int8_t snap_y[TWIN_GLYPH_MAX_SNAP_Y];
double snapped_y[TWIN_GLYPH_MAX_SNAP_Y];
} twin_snap_info_t;
#define twin_glyph_left(g) ((g)[0])
#define twin_glyph_right(g) ((g)[1])
#define twin_glyph_ascent(g) ((g)[2])
#define twin_glyph_descent(g) ((g)[3])
#define twin_glyph_n_snap_x(g) ((g)[4])
#define twin_glyph_n_snap_y(g) ((g)[5])
#define twin_glyph_snap_x(g) (&g[6])
#define twin_glyph_snap_y(g) (twin_glyph_snap_x(g) + twin_glyph_n_snap_x(g))
#define twin_glyph_draw(g) (twin_glyph_snap_y(g) + twin_glyph_n_snap_y(g))
static void
twin_compute_snap (cairo_t *cr,
twin_snap_info_t *info,
const signed char *b)
{
int s, n;
const signed char *snap;
double x_scale, x_scale_inv;
double y_scale, y_scale_inv;
compute_hinting_scales (cr,
&x_scale, &x_scale_inv,
&y_scale, &y_scale_inv);
snap = twin_glyph_snap_x (b);
n = twin_glyph_n_snap_x (b);
info->n_snap_x = n;
assert (n <= TWIN_GLYPH_MAX_SNAP_X);
for (s = 0; s < n; s++) {
info->snap_x[s] = snap[s];
info->snapped_x[s] = SNAPXI (F (snap[s]));
}
snap = twin_glyph_snap_y (b);
n = twin_glyph_n_snap_y (b);
info->n_snap_y = n;
assert (n <= TWIN_GLYPH_MAX_SNAP_Y);
for (s = 0; s < n; s++) {
info->snap_y[s] = snap[s];
info->snapped_y[s] = SNAPYI (F (snap[s]));
}
}
static double
twin_snap (int8_t v, int n, int8_t *snap, double *snapped)
{
int s;
if (!n)
return F(v);
if (snap[0] == v)
return snapped[0];
for (s = 0; s < n - 1; s++)
{
if (snap[s+1] == v)
return snapped[s+1];
if (snap[s] <= v && v <= snap[s+1])
{
int before = snap[s];
int after = snap[s+1];
int dist = after - before;
double snap_before = snapped[s];
double snap_after = snapped[s+1];
double dist_before = v - before;
return snap_before + (snap_after - snap_before) * dist_before / dist;
}
}
return F(v);
}
#define SNAPX(p) twin_snap (p, info.n_snap_x, info.snap_x, info.snapped_x)
#define SNAPY(p) twin_snap (p, info.n_snap_y, info.snap_y, info.snapped_y)
static cairo_status_t
twin_scaled_font_render_glyph (cairo_scaled_font_t *scaled_font,
unsigned long glyph,
cairo_t *cr,
cairo_text_extents_t *metrics)
{
double x1, y1, x2, y2, x3, y3;
double marginl;
twin_scaled_properties_t *props;
twin_snap_info_t info;
const int8_t *b;
const int8_t *g;
int8_t w;
double gw;
props = cairo_scaled_font_get_user_data (scaled_font, &twin_properties_key);
/* Save glyph space, we need it when stroking */
cairo_save (cr);
/* center the pen */
cairo_translate (cr, props->penx * .5, -props->peny * .5);
/* small-caps */
if (props->face_props->smallcaps && glyph >= 'a' && glyph <= 'z') {
glyph += 'A' - 'a';
/* 28 and 42 are small and capital letter heights of the glyph data */
cairo_scale (cr, 1, 28. / 42);
}
/* slant */
if (props->face_props->slant != CAIRO_FONT_SLANT_NORMAL) {
cairo_matrix_t shear = { 1, 0, -.2, 1, 0, 0};
cairo_transform (cr, &shear);
}
b = _cairo_twin_outlines +
_cairo_twin_charmap[unlikely (glyph >= ARRAY_LENGTH (_cairo_twin_charmap)) ? 0 : glyph];
g = twin_glyph_draw(b);
w = twin_glyph_right(b);
gw = F(w);
marginl = props->marginl;
/* monospace */
if (props->face_props->monospace) {
double monow = F(24);
double extra = props->penx + props->marginl + props->marginr;
cairo_scale (cr, (monow + extra) / (gw + extra), 1);
gw = monow;
/* resnap margin for new transform */
{
double x, y, x_scale, x_scale_inv;
x = 1; y = 0;
compute_hinting_scale (cr, x, y, &x_scale, &x_scale_inv);
marginl = SNAPXI (marginl);
}
}
cairo_translate (cr, marginl, 0);
/* stretch */
cairo_scale (cr, props->stretch, 1);
if (props->snap)
twin_compute_snap (cr, &info, b);
else
info.n_snap_x = info.n_snap_y = 0;
/* advance width */
metrics->x_advance = gw * props->stretch + props->penx + props->marginl + props->marginr;
/* glyph shape */
for (;;) {
switch (*g++) {
case 'M':
cairo_close_path (cr);
/* fall through */
case 'm':
x1 = SNAPX(*g++);
y1 = SNAPY(*g++);
cairo_move_to (cr, x1, y1);
continue;
case 'L':
cairo_close_path (cr);
/* fall through */
case 'l':
x1 = SNAPX(*g++);
y1 = SNAPY(*g++);
cairo_line_to (cr, x1, y1);
continue;
case 'C':
cairo_close_path (cr);
/* fall through */
case 'c':
x1 = SNAPX(*g++);
y1 = SNAPY(*g++);
x2 = SNAPX(*g++);
y2 = SNAPY(*g++);
x3 = SNAPX(*g++);
y3 = SNAPY(*g++);
cairo_curve_to (cr, x1, y1, x2, y2, x3, y3);
continue;
case 'E':
cairo_close_path (cr);
/* fall through */
case 'e':
cairo_restore (cr); /* restore glyph space */
cairo_set_tolerance (cr, 0.01);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_width (cr, 1);
cairo_scale (cr, props->penx, props->peny);
cairo_stroke (cr);
break;
case 'X':
/* filler */
continue;
}
break;
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
twin_scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font,
unsigned long unicode,
unsigned long *glyph)
{
/* We use an identity charmap. Which means we could live
* with no unicode_to_glyph method too. But we define this
* to map all unknown chars to a single unknown glyph to
* reduce pressure on cache. */
if (likely (unicode < ARRAY_LENGTH (_cairo_twin_charmap)))
*glyph = unicode;
else
*glyph = 0;
return CAIRO_STATUS_SUCCESS;
}
/*
* Face constructor
*/
static cairo_font_face_t *
_cairo_font_face_twin_create_internal (void)
{
cairo_font_face_t *twin_font_face;
twin_font_face = cairo_user_font_face_create ();
cairo_user_font_face_set_init_func (twin_font_face, twin_scaled_font_init);
cairo_user_font_face_set_render_glyph_func (twin_font_face, twin_scaled_font_render_glyph);
cairo_user_font_face_set_unicode_to_glyph_func (twin_font_face, twin_scaled_font_unicode_to_glyph);
return twin_font_face;
}
cairo_font_face_t *
_cairo_font_face_twin_create_fallback (void)
{
cairo_font_face_t *twin_font_face;
twin_font_face = _cairo_font_face_twin_create_internal ();
if (! twin_font_face_create_properties (twin_font_face)) {
cairo_font_face_destroy (twin_font_face);
return (cairo_font_face_t *) &_cairo_font_face_nil;
}
return twin_font_face;
}
cairo_status_t
_cairo_font_face_twin_create_for_toy (cairo_toy_font_face_t *toy_face,
cairo_font_face_t **font_face)
{
cairo_status_t status;
cairo_font_face_t *twin_font_face;
twin_font_face = _cairo_font_face_twin_create_internal ();
status = twin_font_face_set_properties_from_toy (twin_font_face, toy_face);
if (status) {
cairo_font_face_destroy (twin_font_face);
return status;
}
*font_face = twin_font_face;
return CAIRO_STATUS_SUCCESS;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-font-face.c | /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Graydon Hoare <graydon@redhat.com>
* Owen Taylor <otaylor@redhat.com>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
/**
* SECTION:cairo-font-face
* @Title: cairo_font_face_t
* @Short_Description: Base class for font faces
* @See_Also: #cairo_scaled_font_t
*
* #cairo_font_face_t represents a particular font at a particular weight,
* slant, and other characteristic but no size, transformation, or size.
*
* Font faces are created using <firstterm>font-backend</firstterm>-specific
* constructors, typically of the form
* <function>cairo_<emphasis>backend</emphasis>_font_face_create(<!-- -->)</function>,
* or implicitly using the <firstterm>toy</firstterm> text API by way of
* cairo_select_font_face(). The resulting face can be accessed using
* cairo_get_font_face().
**/
/* #cairo_font_face_t */
const cairo_font_face_t _cairo_font_face_nil = {
{ 0 }, /* hash_entry */
CAIRO_STATUS_NO_MEMORY, /* status */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
{ 0, 0, 0, NULL }, /* user_data */
NULL
};
const cairo_font_face_t _cairo_font_face_nil_file_not_found = {
{ 0 }, /* hash_entry */
CAIRO_STATUS_FILE_NOT_FOUND, /* status */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
{ 0, 0, 0, NULL }, /* user_data */
NULL
};
cairo_status_t
_cairo_font_face_set_error (cairo_font_face_t *font_face,
cairo_status_t status)
{
if (status == CAIRO_STATUS_SUCCESS)
return status;
/* Don't overwrite an existing error. This preserves the first
* error, which is the most significant. */
_cairo_status_set_error (&font_face->status, status);
return _cairo_error (status);
}
void
_cairo_font_face_init (cairo_font_face_t *font_face,
const cairo_font_face_backend_t *backend)
{
CAIRO_MUTEX_INITIALIZE ();
font_face->status = CAIRO_STATUS_SUCCESS;
CAIRO_REFERENCE_COUNT_INIT (&font_face->ref_count, 1);
font_face->backend = backend;
_cairo_user_data_array_init (&font_face->user_data);
}
/**
* cairo_font_face_reference:
* @font_face: a #cairo_font_face_t, (may be %NULL in which case this
* function does nothing).
*
* Increases the reference count on @font_face by one. This prevents
* @font_face from being destroyed until a matching call to
* cairo_font_face_destroy() is made.
*
* Use cairo_font_face_get_reference_count() to get the number of
* references to a #cairo_font_face_t.
*
* Return value: the referenced #cairo_font_face_t.
*
* Since: 1.0
**/
cairo_font_face_t *
cairo_font_face_reference (cairo_font_face_t *font_face)
{
if (font_face == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
return font_face;
/* We would normally assert that we have a reference here but we
* can't get away with that due to the zombie case as documented
* in _cairo_ft_font_face_destroy. */
_cairo_reference_count_inc (&font_face->ref_count);
return font_face;
}
slim_hidden_def (cairo_font_face_reference);
static inline cairo_bool_t
__put(cairo_reference_count_t *v)
{
int c, old;
c = CAIRO_REFERENCE_COUNT_GET_VALUE(v);
while (c != 1 && (old = _cairo_atomic_int_cmpxchg_return_old(&v->ref_count, c, c - 1)) != c)
c = old;
return c != 1;
}
cairo_bool_t
_cairo_font_face_destroy (void *abstract_face)
{
#if 0 /* Nothing needs to be done, we can just drop the last reference */
cairo_font_face_t *font_face = abstract_face;
return _cairo_reference_count_dec_and_test (&font_face->ref_count);
#endif
return TRUE;
}
/**
* cairo_font_face_destroy:
* @font_face: a #cairo_font_face_t
*
* Decreases the reference count on @font_face by one. If the result
* is zero, then @font_face and all associated resources are freed.
* See cairo_font_face_reference().
*
* Since: 1.0
**/
void
cairo_font_face_destroy (cairo_font_face_t *font_face)
{
if (font_face == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
return;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&font_face->ref_count));
/* We allow resurrection to deal with some memory management for the
* FreeType backend where cairo_ft_font_face_t and cairo_ft_unscaled_font_t
* need to effectively mutually reference each other
*/
if (__put (&font_face->ref_count))
return;
if (! font_face->backend->destroy (font_face))
return;
_cairo_user_data_array_fini (&font_face->user_data);
free (font_face);
}
slim_hidden_def (cairo_font_face_destroy);
/**
* cairo_font_face_get_type:
* @font_face: a font face
*
* This function returns the type of the backend used to create
* a font face. See #cairo_font_type_t for available types.
*
* Return value: The type of @font_face.
*
* Since: 1.2
**/
cairo_font_type_t
cairo_font_face_get_type (cairo_font_face_t *font_face)
{
if (CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
return CAIRO_FONT_TYPE_TOY;
return font_face->backend->type;
}
/**
* cairo_font_face_get_reference_count:
* @font_face: a #cairo_font_face_t
*
* Returns the current reference count of @font_face.
*
* Return value: the current reference count of @font_face. If the
* object is a nil object, 0 will be returned.
*
* Since: 1.4
**/
unsigned int
cairo_font_face_get_reference_count (cairo_font_face_t *font_face)
{
if (font_face == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
return 0;
return CAIRO_REFERENCE_COUNT_GET_VALUE (&font_face->ref_count);
}
/**
* cairo_font_face_status:
* @font_face: a #cairo_font_face_t
*
* Checks whether an error has previously occurred for this
* font face
*
* Return value: %CAIRO_STATUS_SUCCESS or another error such as
* %CAIRO_STATUS_NO_MEMORY.
*
* Since: 1.0
**/
cairo_status_t
cairo_font_face_status (cairo_font_face_t *font_face)
{
return font_face->status;
}
/**
* cairo_font_face_get_user_data:
* @font_face: a #cairo_font_face_t
* @key: the address of the #cairo_user_data_key_t the user data was
* attached to
*
* Return user data previously attached to @font_face using the specified
* key. If no user data has been attached with the given key this
* function returns %NULL.
*
* Return value: the user data previously attached or %NULL.
*
* Since: 1.0
**/
void *
cairo_font_face_get_user_data (cairo_font_face_t *font_face,
const cairo_user_data_key_t *key)
{
return _cairo_user_data_array_get_data (&font_face->user_data,
key);
}
slim_hidden_def (cairo_font_face_get_user_data);
/**
* cairo_font_face_set_user_data:
* @font_face: a #cairo_font_face_t
* @key: the address of a #cairo_user_data_key_t to attach the user data to
* @user_data: the user data to attach to the font face
* @destroy: a #cairo_destroy_func_t which will be called when the
* font face is destroyed or when new user data is attached using the
* same key.
*
* Attach user data to @font_face. To remove user data from a font face,
* call this function with the key that was used to set it and %NULL
* for @data.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
* slot could not be allocated for the user data.
*
* Since: 1.0
**/
cairo_status_t
cairo_font_face_set_user_data (cairo_font_face_t *font_face,
const cairo_user_data_key_t *key,
void *user_data,
cairo_destroy_func_t destroy)
{
if (CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
return font_face->status;
return _cairo_user_data_array_set_data (&font_face->user_data,
key, user_data, destroy);
}
slim_hidden_def (cairo_font_face_set_user_data);
void
_cairo_unscaled_font_init (cairo_unscaled_font_t *unscaled_font,
const cairo_unscaled_font_backend_t *backend)
{
CAIRO_REFERENCE_COUNT_INIT (&unscaled_font->ref_count, 1);
unscaled_font->backend = backend;
}
cairo_unscaled_font_t *
_cairo_unscaled_font_reference (cairo_unscaled_font_t *unscaled_font)
{
if (unscaled_font == NULL)
return NULL;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&unscaled_font->ref_count));
_cairo_reference_count_inc (&unscaled_font->ref_count);
return unscaled_font;
}
void
_cairo_unscaled_font_destroy (cairo_unscaled_font_t *unscaled_font)
{
if (unscaled_font == NULL)
return;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&unscaled_font->ref_count));
if (__put (&unscaled_font->ref_count))
return;
if (! unscaled_font->backend->destroy (unscaled_font))
return;
free (unscaled_font);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-font-options.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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):
* Owen Taylor <otaylor@redhat.com>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
/**
* SECTION:cairo-font-options
* @Title: cairo_font_options_t
* @Short_Description: How a font should be rendered
* @See_Also: #cairo_scaled_font_t
*
* The font options specify how fonts should be rendered. Most of the
* time the font options implied by a surface are just right and do not
* need any changes, but for pixel-based targets tweaking font options
* may result in superior output on a particular display.
**/
static const cairo_font_options_t _cairo_font_options_nil = {
CAIRO_ANTIALIAS_DEFAULT,
CAIRO_SUBPIXEL_ORDER_DEFAULT,
CAIRO_LCD_FILTER_DEFAULT,
CAIRO_HINT_STYLE_DEFAULT,
CAIRO_HINT_METRICS_DEFAULT,
CAIRO_ROUND_GLYPH_POS_DEFAULT,
NULL
};
/**
* _cairo_font_options_init_default:
* @options: a #cairo_font_options_t
*
* Initializes all fields of the font options object to default values.
**/
void
_cairo_font_options_init_default (cairo_font_options_t *options)
{
options->antialias = CAIRO_ANTIALIAS_DEFAULT;
options->subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
options->lcd_filter = CAIRO_LCD_FILTER_DEFAULT;
options->hint_style = CAIRO_HINT_STYLE_DEFAULT;
options->hint_metrics = CAIRO_HINT_METRICS_DEFAULT;
options->round_glyph_positions = CAIRO_ROUND_GLYPH_POS_DEFAULT;
options->variations = NULL;
}
void
_cairo_font_options_init_copy (cairo_font_options_t *options,
const cairo_font_options_t *other)
{
options->antialias = other->antialias;
options->subpixel_order = other->subpixel_order;
options->lcd_filter = other->lcd_filter;
options->hint_style = other->hint_style;
options->hint_metrics = other->hint_metrics;
options->round_glyph_positions = other->round_glyph_positions;
options->variations = other->variations ? strdup (other->variations) : NULL;
}
/**
* cairo_font_options_create:
*
* Allocates a new font options object with all options initialized
* to default values.
*
* Return value: a newly allocated #cairo_font_options_t. Free with
* cairo_font_options_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_font_options_status().
*
* Since: 1.0
**/
cairo_font_options_t *
cairo_font_options_create (void)
{
cairo_font_options_t *options;
options = _cairo_malloc (sizeof (cairo_font_options_t));
if (!options) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_font_options_t *) &_cairo_font_options_nil;
}
_cairo_font_options_init_default (options);
return options;
}
/**
* cairo_font_options_copy:
* @original: a #cairo_font_options_t
*
* Allocates a new font options object copying the option values from
* @original.
*
* Return value: a newly allocated #cairo_font_options_t. Free with
* cairo_font_options_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_font_options_status().
*
* Since: 1.0
**/
cairo_font_options_t *
cairo_font_options_copy (const cairo_font_options_t *original)
{
cairo_font_options_t *options;
if (cairo_font_options_status ((cairo_font_options_t *) original))
return (cairo_font_options_t *) &_cairo_font_options_nil;
options = _cairo_malloc (sizeof (cairo_font_options_t));
if (!options) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_font_options_t *) &_cairo_font_options_nil;
}
_cairo_font_options_init_copy (options, original);
return options;
}
void
_cairo_font_options_fini (cairo_font_options_t *options)
{
free (options->variations);
}
/**
* cairo_font_options_destroy:
* @options: a #cairo_font_options_t
*
* Destroys a #cairo_font_options_t object created with
* cairo_font_options_create() or cairo_font_options_copy().
*
* Since: 1.0
**/
void
cairo_font_options_destroy (cairo_font_options_t *options)
{
if (cairo_font_options_status (options))
return;
_cairo_font_options_fini (options);
free (options);
}
/**
* cairo_font_options_status:
* @options: a #cairo_font_options_t
*
* Checks whether an error has previously occurred for this
* font options object
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.0
**/
cairo_status_t
cairo_font_options_status (cairo_font_options_t *options)
{
if (options == NULL)
return CAIRO_STATUS_NULL_POINTER;
else if (options == (cairo_font_options_t *) &_cairo_font_options_nil)
return CAIRO_STATUS_NO_MEMORY;
else
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_font_options_status);
/**
* cairo_font_options_merge:
* @options: a #cairo_font_options_t
* @other: another #cairo_font_options_t
*
* Merges non-default options from @other into @options, replacing
* existing values. This operation can be thought of as somewhat
* similar to compositing @other onto @options with the operation
* of %CAIRO_OPERATOR_OVER.
*
* Since: 1.0
**/
void
cairo_font_options_merge (cairo_font_options_t *options,
const cairo_font_options_t *other)
{
if (cairo_font_options_status (options))
return;
if (cairo_font_options_status ((cairo_font_options_t *) other))
return;
if (other->antialias != CAIRO_ANTIALIAS_DEFAULT)
options->antialias = other->antialias;
if (other->subpixel_order != CAIRO_SUBPIXEL_ORDER_DEFAULT)
options->subpixel_order = other->subpixel_order;
if (other->lcd_filter != CAIRO_LCD_FILTER_DEFAULT)
options->lcd_filter = other->lcd_filter;
if (other->hint_style != CAIRO_HINT_STYLE_DEFAULT)
options->hint_style = other->hint_style;
if (other->hint_metrics != CAIRO_HINT_METRICS_DEFAULT)
options->hint_metrics = other->hint_metrics;
if (other->round_glyph_positions != CAIRO_ROUND_GLYPH_POS_DEFAULT)
options->round_glyph_positions = other->round_glyph_positions;
if (other->variations) {
if (options->variations) {
char *p;
/* 'merge' variations by concatenating - later entries win */
p = malloc (strlen (other->variations) + strlen (options->variations) + 2);
p[0] = 0;
strcat (p, options->variations);
strcat (p, ",");
strcat (p, other->variations);
free (options->variations);
options->variations = p;
}
else {
options->variations = strdup (other->variations);
}
}
}
slim_hidden_def (cairo_font_options_merge);
/**
* cairo_font_options_equal:
* @options: a #cairo_font_options_t
* @other: another #cairo_font_options_t
*
* Compares two font options objects for equality.
*
* Return value: %TRUE if all fields of the two font options objects match.
* Note that this function will return %FALSE if either object is in
* error.
*
* Since: 1.0
**/
cairo_bool_t
cairo_font_options_equal (const cairo_font_options_t *options,
const cairo_font_options_t *other)
{
if (cairo_font_options_status ((cairo_font_options_t *) options))
return FALSE;
if (cairo_font_options_status ((cairo_font_options_t *) other))
return FALSE;
if (options == other)
return TRUE;
return (options->antialias == other->antialias &&
options->subpixel_order == other->subpixel_order &&
options->lcd_filter == other->lcd_filter &&
options->hint_style == other->hint_style &&
options->hint_metrics == other->hint_metrics &&
options->round_glyph_positions == other->round_glyph_positions &&
((options->variations == NULL && other->variations == NULL) ||
(options->variations != NULL && other->variations != NULL &&
strcmp (options->variations, other->variations) == 0)));
}
slim_hidden_def (cairo_font_options_equal);
/**
* cairo_font_options_hash:
* @options: a #cairo_font_options_t
*
* Compute a hash for the font options object; this value will
* be useful when storing an object containing a #cairo_font_options_t
* in a hash table.
*
* Return value: the hash value for the font options object.
* The return value can be cast to a 32-bit type if a
* 32-bit hash value is needed.
*
* Since: 1.0
**/
unsigned long
cairo_font_options_hash (const cairo_font_options_t *options)
{
unsigned long hash = 0;
if (cairo_font_options_status ((cairo_font_options_t *) options))
options = &_cairo_font_options_nil; /* force default values */
if (options->variations)
hash = _cairo_string_hash (options->variations, strlen (options->variations));
return ((options->antialias) |
(options->subpixel_order << 4) |
(options->lcd_filter << 8) |
(options->hint_style << 12) |
(options->hint_metrics << 16)) ^ hash;
}
slim_hidden_def (cairo_font_options_hash);
/**
* cairo_font_options_set_antialias:
* @options: a #cairo_font_options_t
* @antialias: the new antialiasing mode
*
* Sets the antialiasing mode for the font options object. This
* specifies the type of antialiasing to do when rendering text.
*
* Since: 1.0
**/
void
cairo_font_options_set_antialias (cairo_font_options_t *options,
cairo_antialias_t antialias)
{
if (cairo_font_options_status (options))
return;
options->antialias = antialias;
}
slim_hidden_def (cairo_font_options_set_antialias);
/**
* cairo_font_options_get_antialias:
* @options: a #cairo_font_options_t
*
* Gets the antialiasing mode for the font options object.
*
* Return value: the antialiasing mode
*
* Since: 1.0
**/
cairo_antialias_t
cairo_font_options_get_antialias (const cairo_font_options_t *options)
{
if (cairo_font_options_status ((cairo_font_options_t *) options))
return CAIRO_ANTIALIAS_DEFAULT;
return options->antialias;
}
/**
* cairo_font_options_set_subpixel_order:
* @options: a #cairo_font_options_t
* @subpixel_order: the new subpixel order
*
* Sets the subpixel order for the font options object. The subpixel
* order specifies the order of color elements within each pixel on
* the display device when rendering with an antialiasing mode of
* %CAIRO_ANTIALIAS_SUBPIXEL. See the documentation for
* #cairo_subpixel_order_t for full details.
*
* Since: 1.0
**/
void
cairo_font_options_set_subpixel_order (cairo_font_options_t *options,
cairo_subpixel_order_t subpixel_order)
{
if (cairo_font_options_status (options))
return;
options->subpixel_order = subpixel_order;
}
slim_hidden_def (cairo_font_options_set_subpixel_order);
/**
* cairo_font_options_get_subpixel_order:
* @options: a #cairo_font_options_t
*
* Gets the subpixel order for the font options object.
* See the documentation for #cairo_subpixel_order_t for full details.
*
* Return value: the subpixel order for the font options object
*
* Since: 1.0
**/
cairo_subpixel_order_t
cairo_font_options_get_subpixel_order (const cairo_font_options_t *options)
{
if (cairo_font_options_status ((cairo_font_options_t *) options))
return CAIRO_SUBPIXEL_ORDER_DEFAULT;
return options->subpixel_order;
}
/**
* _cairo_font_options_set_lcd_filter:
* @options: a #cairo_font_options_t
* @lcd_filter: the new LCD filter
*
* Sets the LCD filter for the font options object. The LCD filter
* specifies how pixels are filtered when rendered with an antialiasing
* mode of %CAIRO_ANTIALIAS_SUBPIXEL. See the documentation for
* #cairo_lcd_filter_t for full details.
**/
void
_cairo_font_options_set_lcd_filter (cairo_font_options_t *options,
cairo_lcd_filter_t lcd_filter)
{
if (cairo_font_options_status (options))
return;
options->lcd_filter = lcd_filter;
}
/**
* _cairo_font_options_get_lcd_filter:
* @options: a #cairo_font_options_t
*
* Gets the LCD filter for the font options object.
* See the documentation for #cairo_lcd_filter_t for full details.
*
* Return value: the LCD filter for the font options object
**/
cairo_lcd_filter_t
_cairo_font_options_get_lcd_filter (const cairo_font_options_t *options)
{
if (cairo_font_options_status ((cairo_font_options_t *) options))
return CAIRO_LCD_FILTER_DEFAULT;
return options->lcd_filter;
}
/**
* _cairo_font_options_set_round_glyph_positions:
* @options: a #cairo_font_options_t
* @round: the new rounding value
*
* Sets the rounding options for the font options object. If rounding is set, a
* glyph's position will be rounded to integer values.
**/
void
_cairo_font_options_set_round_glyph_positions (cairo_font_options_t *options,
cairo_round_glyph_positions_t round)
{
if (cairo_font_options_status (options))
return;
options->round_glyph_positions = round;
}
/**
* _cairo_font_options_get_round_glyph_positions:
* @options: a #cairo_font_options_t
*
* Gets the glyph position rounding option for the font options object.
*
* Return value: The round glyph posistions flag for the font options object.
**/
cairo_round_glyph_positions_t
_cairo_font_options_get_round_glyph_positions (const cairo_font_options_t *options)
{
if (cairo_font_options_status ((cairo_font_options_t *) options))
return CAIRO_ROUND_GLYPH_POS_DEFAULT;
return options->round_glyph_positions;
}
/**
* cairo_font_options_set_hint_style:
* @options: a #cairo_font_options_t
* @hint_style: the new hint style
*
* Sets the hint style for font outlines for the font options object.
* This controls whether to fit font outlines to the pixel grid,
* and if so, whether to optimize for fidelity or contrast.
* See the documentation for #cairo_hint_style_t for full details.
*
* Since: 1.0
**/
void
cairo_font_options_set_hint_style (cairo_font_options_t *options,
cairo_hint_style_t hint_style)
{
if (cairo_font_options_status (options))
return;
options->hint_style = hint_style;
}
slim_hidden_def (cairo_font_options_set_hint_style);
/**
* cairo_font_options_get_hint_style:
* @options: a #cairo_font_options_t
*
* Gets the hint style for font outlines for the font options object.
* See the documentation for #cairo_hint_style_t for full details.
*
* Return value: the hint style for the font options object
*
* Since: 1.0
**/
cairo_hint_style_t
cairo_font_options_get_hint_style (const cairo_font_options_t *options)
{
if (cairo_font_options_status ((cairo_font_options_t *) options))
return CAIRO_HINT_STYLE_DEFAULT;
return options->hint_style;
}
/**
* cairo_font_options_set_hint_metrics:
* @options: a #cairo_font_options_t
* @hint_metrics: the new metrics hinting mode
*
* Sets the metrics hinting mode for the font options object. This
* controls whether metrics are quantized to integer values in
* device units.
* See the documentation for #cairo_hint_metrics_t for full details.
*
* Since: 1.0
**/
void
cairo_font_options_set_hint_metrics (cairo_font_options_t *options,
cairo_hint_metrics_t hint_metrics)
{
if (cairo_font_options_status (options))
return;
options->hint_metrics = hint_metrics;
}
slim_hidden_def (cairo_font_options_set_hint_metrics);
/**
* cairo_font_options_get_hint_metrics:
* @options: a #cairo_font_options_t
*
* Gets the metrics hinting mode for the font options object.
* See the documentation for #cairo_hint_metrics_t for full details.
*
* Return value: the metrics hinting mode for the font options object
*
* Since: 1.0
**/
cairo_hint_metrics_t
cairo_font_options_get_hint_metrics (const cairo_font_options_t *options)
{
if (cairo_font_options_status ((cairo_font_options_t *) options))
return CAIRO_HINT_METRICS_DEFAULT;
return options->hint_metrics;
}
/**
* cairo_font_options_set_variations:
* @options: a #cairo_font_options_t
* @variations: the new font variations, or %NULL
*
* Sets the OpenType font variations for the font options object.
* Font variations are specified as a string with a format that
* is similar to the CSS font-variation-settings. The string contains
* a comma-separated list of axis assignments, which each assignment
* consists of a 4-character axis name and a value, separated by
* whitespace and optional equals sign.
*
* Examples:
*
* wght=200,wdth=140.5
*
* wght 200 , wdth 140.5
*
* Since: 1.16
**/
void
cairo_font_options_set_variations (cairo_font_options_t *options,
const char *variations)
{
char *tmp = variations ? strdup (variations) : NULL;
free (options->variations);
options->variations = tmp;
}
/**
* cairo_font_options_get_variations:
* @options: a #cairo_font_options_t
*
* Gets the OpenType font variations for the font options object.
* See cairo_font_options_set_variations() for details about the
* string format.
*
* Return value: the font variations for the font options object. The
* returned string belongs to the @options and must not be modified.
* It is valid until either the font options object is destroyed or
* the font variations in this object is modified with
* cairo_font_options_set_variations().
*
* Since: 1.16
**/
const char *
cairo_font_options_get_variations (cairo_font_options_t *options)
{
return options->variations;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-freed-pool-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#ifndef CAIRO_FREED_POOL_H
#define CAIRO_FREED_POOL_H
#include "cairoint.h"
#include "cairo-atomic-private.h"
CAIRO_BEGIN_DECLS
#define DISABLE_FREED_POOLS 0
#if HAS_ATOMIC_OPS && ! DISABLE_FREED_POOLS
/* Keep a stash of recently freed clip_paths, since we need to
* reallocate them frequently.
*/
#define MAX_FREED_POOL_SIZE 16
typedef struct {
void *pool[MAX_FREED_POOL_SIZE];
cairo_atomic_int_t top;
} freed_pool_t;
static cairo_always_inline void *
_atomic_fetch (void **slot)
{
void *ptr;
do {
ptr = _cairo_atomic_ptr_get (slot);
} while (! _cairo_atomic_ptr_cmpxchg (slot, ptr, NULL));
return ptr;
}
static cairo_always_inline cairo_bool_t
_atomic_store (void **slot, void *ptr)
{
return _cairo_atomic_ptr_cmpxchg (slot, NULL, ptr);
}
cairo_private void *
_freed_pool_get_search (freed_pool_t *pool);
static inline void *
_freed_pool_get (freed_pool_t *pool)
{
void *ptr;
int i;
i = _cairo_atomic_int_get_relaxed (&pool->top) - 1;
if (i < 0)
i = 0;
ptr = _atomic_fetch (&pool->pool[i]);
if (likely (ptr != NULL)) {
_cairo_atomic_int_set_relaxed (&pool->top, i);
return ptr;
}
/* either empty or contended */
return _freed_pool_get_search (pool);
}
cairo_private void
_freed_pool_put_search (freed_pool_t *pool, void *ptr);
static inline void
_freed_pool_put (freed_pool_t *pool, void *ptr)
{
int i;
i = _cairo_atomic_int_get_relaxed (&pool->top);
if (likely (i < ARRAY_LENGTH (pool->pool) &&
_atomic_store (&pool->pool[i], ptr)))
{
_cairo_atomic_int_set_relaxed (&pool->top, i + 1);
return;
}
/* either full or contended */
_freed_pool_put_search (pool, ptr);
}
cairo_private void
_freed_pool_reset (freed_pool_t *pool);
#define HAS_FREED_POOL 1
#else
/* A warning about an unused freed-pool in a build without atomics
* enabled usually indicates a missing _freed_pool_reset() in the
* static reset function */
typedef int freed_pool_t;
#define _freed_pool_get(pool) NULL
#define _freed_pool_put(pool, ptr) free(ptr)
#define _freed_pool_reset(ptr)
#endif
CAIRO_END_DECLS
#endif /* CAIRO_FREED_POOL_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-freelist-private.h | /*
* Copyright © 2006 Joonas Pihlaja
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#ifndef CAIRO_FREELIST_H
#define CAIRO_FREELIST_H
#include "cairo-types-private.h"
#include "cairo-compiler-private.h"
#include "cairo-freelist-type-private.h"
/* for stand-alone compilation*/
#ifndef VG
#define VG(x)
#endif
#ifndef NULL
#define NULL (void *) 0
#endif
/* Initialise a freelist that will be responsible for allocating
* nodes of size nodesize. */
cairo_private void
_cairo_freelist_init (cairo_freelist_t *freelist, unsigned nodesize);
/* Deallocate any nodes in the freelist. */
cairo_private void
_cairo_freelist_fini (cairo_freelist_t *freelist);
/* Allocate a new node from the freelist. If the freelist contains no
* nodes, a new one will be allocated using malloc(). The caller is
* responsible for calling _cairo_freelist_free() or free() on the
* returned node. Returns %NULL on memory allocation error. */
cairo_private void *
_cairo_freelist_alloc (cairo_freelist_t *freelist);
/* Allocate a new node from the freelist. If the freelist contains no
* nodes, a new one will be allocated using calloc(). The caller is
* responsible for calling _cairo_freelist_free() or free() on the
* returned node. Returns %NULL on memory allocation error. */
cairo_private void *
_cairo_freelist_calloc (cairo_freelist_t *freelist);
/* Return a node to the freelist. This does not deallocate the memory,
* but makes it available for later reuse by
* _cairo_freelist_alloc(). */
cairo_private void
_cairo_freelist_free (cairo_freelist_t *freelist, void *node);
cairo_private void
_cairo_freepool_init (cairo_freepool_t *freepool, unsigned nodesize);
cairo_private void
_cairo_freepool_fini (cairo_freepool_t *freepool);
static inline void
_cairo_freepool_reset (cairo_freepool_t *freepool)
{
while (freepool->pools != &freepool->embedded_pool) {
cairo_freelist_pool_t *pool = freepool->pools;
freepool->pools = pool->next;
pool->next = freepool->freepools;
freepool->freepools = pool;
}
freepool->embedded_pool.rem = sizeof (freepool->embedded_data);
freepool->embedded_pool.data = freepool->embedded_data;
}
cairo_private void *
_cairo_freepool_alloc_from_new_pool (cairo_freepool_t *freepool);
static inline void *
_cairo_freepool_alloc_from_pool (cairo_freepool_t *freepool)
{
cairo_freelist_pool_t *pool;
uint8_t *ptr;
pool = freepool->pools;
if (unlikely (freepool->nodesize > pool->rem))
return _cairo_freepool_alloc_from_new_pool (freepool);
ptr = pool->data;
pool->data += freepool->nodesize;
pool->rem -= freepool->nodesize;
VG (VALGRIND_MAKE_MEM_UNDEFINED (ptr, freepool->nodesize));
return ptr;
}
static inline void *
_cairo_freepool_alloc (cairo_freepool_t *freepool)
{
cairo_freelist_node_t *node;
node = freepool->first_free_node;
if (node == NULL)
return _cairo_freepool_alloc_from_pool (freepool);
VG (VALGRIND_MAKE_MEM_DEFINED (node, sizeof (node->next)));
freepool->first_free_node = node->next;
VG (VALGRIND_MAKE_MEM_UNDEFINED (node, freepool->nodesize));
return node;
}
cairo_private cairo_status_t
_cairo_freepool_alloc_array (cairo_freepool_t *freepool,
int count,
void **array);
static inline void
_cairo_freepool_free (cairo_freepool_t *freepool, void *ptr)
{
cairo_freelist_node_t *node = ptr;
node->next = freepool->first_free_node;
freepool->first_free_node = node;
VG (VALGRIND_MAKE_MEM_UNDEFINED (node, freepool->nodesize));
}
#endif /* CAIRO_FREELIST_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-freelist-type-private.h | /*
* Copyright © 2010 Joonas Pihlaja
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#ifndef CAIRO_FREELIST_TYPE_H
#define CAIRO_FREELIST_TYPE_H
#include "cairo-types-private.h"
#include "cairo-compiler-private.h"
typedef struct _cairo_freelist_node cairo_freelist_node_t;
struct _cairo_freelist_node {
cairo_freelist_node_t *next;
};
typedef struct _cairo_freelist {
cairo_freelist_node_t *first_free_node;
unsigned nodesize;
} cairo_freelist_t;
typedef struct _cairo_freelist_pool cairo_freelist_pool_t;
struct _cairo_freelist_pool {
cairo_freelist_pool_t *next;
unsigned size, rem;
uint8_t *data;
};
typedef struct _cairo_freepool {
cairo_freelist_node_t *first_free_node;
cairo_freelist_pool_t *pools;
cairo_freelist_pool_t *freepools;
unsigned nodesize;
cairo_freelist_pool_t embedded_pool;
uint8_t embedded_data[1000];
} cairo_freepool_t;
#endif /* CAIRO_FREELIST_TYPE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-freelist.c | /*
* Copyright © 2006 Joonas Pihlaja
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-freelist-private.h"
void
_cairo_freelist_init (cairo_freelist_t *freelist, unsigned nodesize)
{
memset (freelist, 0, sizeof (cairo_freelist_t));
freelist->nodesize = nodesize;
}
void
_cairo_freelist_fini (cairo_freelist_t *freelist)
{
cairo_freelist_node_t *node = freelist->first_free_node;
while (node) {
cairo_freelist_node_t *next;
VG (VALGRIND_MAKE_MEM_DEFINED (node, sizeof (node->next)));
next = node->next;
free (node);
node = next;
}
}
void *
_cairo_freelist_alloc (cairo_freelist_t *freelist)
{
if (freelist->first_free_node) {
cairo_freelist_node_t *node;
node = freelist->first_free_node;
VG (VALGRIND_MAKE_MEM_DEFINED (node, sizeof (node->next)));
freelist->first_free_node = node->next;
VG (VALGRIND_MAKE_MEM_UNDEFINED (node, freelist->nodesize));
return node;
}
return _cairo_malloc (freelist->nodesize);
}
void *
_cairo_freelist_calloc (cairo_freelist_t *freelist)
{
void *node = _cairo_freelist_alloc (freelist);
if (node)
memset (node, 0, freelist->nodesize);
return node;
}
void
_cairo_freelist_free (cairo_freelist_t *freelist, void *voidnode)
{
cairo_freelist_node_t *node = voidnode;
if (node) {
node->next = freelist->first_free_node;
freelist->first_free_node = node;
VG (VALGRIND_MAKE_MEM_UNDEFINED (node, freelist->nodesize));
}
}
void
_cairo_freepool_init (cairo_freepool_t *freepool, unsigned nodesize)
{
freepool->first_free_node = NULL;
freepool->pools = &freepool->embedded_pool;
freepool->freepools = NULL;
freepool->nodesize = nodesize;
freepool->embedded_pool.next = NULL;
freepool->embedded_pool.size = sizeof (freepool->embedded_data);
freepool->embedded_pool.rem = sizeof (freepool->embedded_data);
freepool->embedded_pool.data = freepool->embedded_data;
VG (VALGRIND_MAKE_MEM_UNDEFINED (freepool->embedded_data, sizeof (freepool->embedded_data)));
}
void
_cairo_freepool_fini (cairo_freepool_t *freepool)
{
cairo_freelist_pool_t *pool;
pool = freepool->pools;
while (pool != &freepool->embedded_pool) {
cairo_freelist_pool_t *next = pool->next;
free (pool);
pool = next;
}
pool = freepool->freepools;
while (pool != NULL) {
cairo_freelist_pool_t *next = pool->next;
free (pool);
pool = next;
}
VG (VALGRIND_MAKE_MEM_UNDEFINED (freepool, sizeof (freepool)));
}
void *
_cairo_freepool_alloc_from_new_pool (cairo_freepool_t *freepool)
{
cairo_freelist_pool_t *pool;
int poolsize;
if (freepool->freepools != NULL) {
pool = freepool->freepools;
freepool->freepools = pool->next;
poolsize = pool->size;
} else {
if (freepool->pools != &freepool->embedded_pool)
poolsize = 2 * freepool->pools->size;
else
poolsize = (128 * freepool->nodesize + 8191) & -8192;
pool = _cairo_malloc (sizeof (cairo_freelist_pool_t) + poolsize);
if (unlikely (pool == NULL))
return pool;
pool->size = poolsize;
}
pool->next = freepool->pools;
freepool->pools = pool;
pool->rem = poolsize - freepool->nodesize;
pool->data = (uint8_t *) (pool + 1) + freepool->nodesize;
VG (VALGRIND_MAKE_MEM_UNDEFINED (pool->data, pool->rem));
return pool + 1;
}
cairo_status_t
_cairo_freepool_alloc_array (cairo_freepool_t *freepool,
int count,
void **array)
{
int i;
for (i = 0; i < count; i++) {
cairo_freelist_node_t *node;
node = freepool->first_free_node;
if (likely (node != NULL)) {
VG (VALGRIND_MAKE_MEM_DEFINED (node, sizeof (node->next)));
freepool->first_free_node = node->next;
VG (VALGRIND_MAKE_MEM_UNDEFINED (node, freepool->nodesize));
} else {
node = _cairo_freepool_alloc_from_pool (freepool);
if (unlikely (node == NULL))
goto CLEANUP;
}
array[i] = node;
}
return CAIRO_STATUS_SUCCESS;
CLEANUP:
while (i--)
_cairo_freepool_free (freepool, array[i]);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-gstate-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@redhat.com>
*/
#ifndef CAIRO_GSTATE_PRIVATE_H
#define CAIRO_GSTATE_PRIVATE_H
#include "cairo-clip-private.h"
struct _cairo_gstate {
cairo_operator_t op;
double opacity;
double tolerance;
cairo_antialias_t antialias;
cairo_stroke_style_t stroke_style;
cairo_fill_rule_t fill_rule;
cairo_font_face_t *font_face;
cairo_scaled_font_t *scaled_font; /* Specific to the current CTM */
cairo_scaled_font_t *previous_scaled_font; /* holdover */
cairo_matrix_t font_matrix;
cairo_font_options_t font_options;
cairo_clip_t *clip;
cairo_surface_t *target; /* The target to which all rendering is directed */
cairo_surface_t *parent_target; /* The previous target which was receiving rendering */
cairo_surface_t *original_target; /* The original target the initial gstate was created with */
/* the user is allowed to update the device after we have cached the matrices... */
cairo_observer_t device_transform_observer;
cairo_matrix_t ctm;
cairo_matrix_t ctm_inverse;
cairo_matrix_t source_ctm_inverse; /* At the time ->source was set */
cairo_bool_t is_identity;
cairo_pattern_t *source;
struct _cairo_gstate *next;
};
/* cairo-gstate.c */
cairo_private cairo_status_t
_cairo_gstate_init (cairo_gstate_t *gstate,
cairo_surface_t *target);
cairo_private void
_cairo_gstate_fini (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_save (cairo_gstate_t **gstate, cairo_gstate_t **freelist);
cairo_private cairo_status_t
_cairo_gstate_restore (cairo_gstate_t **gstate, cairo_gstate_t **freelist);
cairo_private cairo_bool_t
_cairo_gstate_is_group (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_redirect_target (cairo_gstate_t *gstate, cairo_surface_t *child);
cairo_private cairo_surface_t *
_cairo_gstate_get_target (cairo_gstate_t *gstate);
cairo_private cairo_surface_t *
_cairo_gstate_get_original_target (cairo_gstate_t *gstate);
cairo_private cairo_clip_t *
_cairo_gstate_get_clip (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_source (cairo_gstate_t *gstate, cairo_pattern_t *source);
cairo_private cairo_pattern_t *
_cairo_gstate_get_source (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_operator (cairo_gstate_t *gstate, cairo_operator_t op);
cairo_private cairo_operator_t
_cairo_gstate_get_operator (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_opacity (cairo_gstate_t *gstate, double opacity);
cairo_private double
_cairo_gstate_get_opacity (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_tolerance (cairo_gstate_t *gstate, double tolerance);
cairo_private double
_cairo_gstate_get_tolerance (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_fill_rule (cairo_gstate_t *gstate, cairo_fill_rule_t fill_rule);
cairo_private cairo_fill_rule_t
_cairo_gstate_get_fill_rule (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_line_width (cairo_gstate_t *gstate, double width);
cairo_private double
_cairo_gstate_get_line_width (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_line_cap (cairo_gstate_t *gstate, cairo_line_cap_t line_cap);
cairo_private cairo_line_cap_t
_cairo_gstate_get_line_cap (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_line_join (cairo_gstate_t *gstate, cairo_line_join_t line_join);
cairo_private cairo_line_join_t
_cairo_gstate_get_line_join (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_set_dash (cairo_gstate_t *gstate, const double *dash, int num_dashes, double offset);
cairo_private void
_cairo_gstate_get_dash (cairo_gstate_t *gstate, double *dash, int *num_dashes, double *offset);
cairo_private cairo_status_t
_cairo_gstate_set_miter_limit (cairo_gstate_t *gstate, double limit);
cairo_private double
_cairo_gstate_get_miter_limit (cairo_gstate_t *gstate);
cairo_private void
_cairo_gstate_get_matrix (cairo_gstate_t *gstate, cairo_matrix_t *matrix);
cairo_private cairo_status_t
_cairo_gstate_translate (cairo_gstate_t *gstate, double tx, double ty);
cairo_private cairo_status_t
_cairo_gstate_scale (cairo_gstate_t *gstate, double sx, double sy);
cairo_private cairo_status_t
_cairo_gstate_rotate (cairo_gstate_t *gstate, double angle);
cairo_private cairo_status_t
_cairo_gstate_transform (cairo_gstate_t *gstate,
const cairo_matrix_t *matrix);
cairo_private cairo_status_t
_cairo_gstate_set_matrix (cairo_gstate_t *gstate,
const cairo_matrix_t *matrix);
cairo_private void
_cairo_gstate_identity_matrix (cairo_gstate_t *gstate);
cairo_private void
_cairo_gstate_user_to_device (cairo_gstate_t *gstate, double *x, double *y);
cairo_private void
_cairo_gstate_user_to_device_distance (cairo_gstate_t *gstate, double *dx, double *dy);
cairo_private void
_cairo_gstate_device_to_user (cairo_gstate_t *gstate, double *x, double *y);
cairo_private void
_cairo_gstate_device_to_user_distance (cairo_gstate_t *gstate, double *dx, double *dy);
cairo_private void
_do_cairo_gstate_user_to_backend (cairo_gstate_t *gstate, double *x, double *y);
static inline void
_cairo_gstate_user_to_backend (cairo_gstate_t *gstate, double *x, double *y)
{
if (! gstate->is_identity)
_do_cairo_gstate_user_to_backend (gstate, x, y);
}
cairo_private void
_do_cairo_gstate_user_to_backend_distance (cairo_gstate_t *gstate, double *x, double *y);
static inline void
_cairo_gstate_user_to_backend_distance (cairo_gstate_t *gstate, double *x, double *y)
{
if (! gstate->is_identity)
_do_cairo_gstate_user_to_backend_distance (gstate, x, y);
}
cairo_private void
_do_cairo_gstate_backend_to_user (cairo_gstate_t *gstate, double *x, double *y);
static inline void
_cairo_gstate_backend_to_user (cairo_gstate_t *gstate, double *x, double *y)
{
if (! gstate->is_identity)
_do_cairo_gstate_backend_to_user (gstate, x, y);
}
cairo_private void
_do_cairo_gstate_backend_to_user_distance (cairo_gstate_t *gstate, double *x, double *y);
static inline void
_cairo_gstate_backend_to_user_distance (cairo_gstate_t *gstate, double *x, double *y)
{
if (! gstate->is_identity)
_do_cairo_gstate_backend_to_user_distance (gstate, x, y);
}
cairo_private void
_cairo_gstate_backend_to_user_rectangle (cairo_gstate_t *gstate,
double *x1, double *y1,
double *x2, double *y2,
cairo_bool_t *is_tight);
cairo_private void
_cairo_gstate_path_extents (cairo_gstate_t *gstate,
cairo_path_fixed_t *path,
double *x1, double *y1,
double *x2, double *y2);
cairo_private cairo_status_t
_cairo_gstate_paint (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_mask (cairo_gstate_t *gstate,
cairo_pattern_t *mask);
cairo_private cairo_status_t
_cairo_gstate_stroke (cairo_gstate_t *gstate, cairo_path_fixed_t *path);
cairo_private cairo_status_t
_cairo_gstate_fill (cairo_gstate_t *gstate, cairo_path_fixed_t *path);
cairo_private cairo_status_t
_cairo_gstate_copy_page (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_show_page (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_stroke_extents (cairo_gstate_t *gstate,
cairo_path_fixed_t *path,
double *x1, double *y1,
double *x2, double *y2);
cairo_private cairo_status_t
_cairo_gstate_fill_extents (cairo_gstate_t *gstate,
cairo_path_fixed_t *path,
double *x1, double *y1,
double *x2, double *y2);
cairo_private cairo_status_t
_cairo_gstate_in_stroke (cairo_gstate_t *gstate,
cairo_path_fixed_t *path,
double x,
double y,
cairo_bool_t *inside_ret);
cairo_private cairo_bool_t
_cairo_gstate_in_fill (cairo_gstate_t *gstate,
cairo_path_fixed_t *path,
double x,
double y);
cairo_private cairo_bool_t
_cairo_gstate_in_clip (cairo_gstate_t *gstate,
double x,
double y);
cairo_private cairo_status_t
_cairo_gstate_clip (cairo_gstate_t *gstate, cairo_path_fixed_t *path);
cairo_private cairo_status_t
_cairo_gstate_reset_clip (cairo_gstate_t *gstate);
cairo_private cairo_bool_t
_cairo_gstate_clip_extents (cairo_gstate_t *gstate,
double *x1,
double *y1,
double *x2,
double *y2);
cairo_private cairo_rectangle_list_t*
_cairo_gstate_copy_clip_rectangle_list (cairo_gstate_t *gstate);
cairo_private cairo_status_t
_cairo_gstate_show_surface (cairo_gstate_t *gstate,
cairo_surface_t *surface,
double x,
double y,
double width,
double height);
cairo_private cairo_status_t
_cairo_gstate_tag_begin (cairo_gstate_t *gstate,
const char *tag_name,
const char *attributes);
cairo_private cairo_status_t
_cairo_gstate_tag_end (cairo_gstate_t *gstate,
const char *tag_name);
cairo_private cairo_status_t
_cairo_gstate_set_font_size (cairo_gstate_t *gstate,
double size);
cairo_private void
_cairo_gstate_get_font_matrix (cairo_gstate_t *gstate,
cairo_matrix_t *matrix);
cairo_private cairo_status_t
_cairo_gstate_set_font_matrix (cairo_gstate_t *gstate,
const cairo_matrix_t *matrix);
cairo_private void
_cairo_gstate_get_font_options (cairo_gstate_t *gstate,
cairo_font_options_t *options);
cairo_private void
_cairo_gstate_set_font_options (cairo_gstate_t *gstate,
const cairo_font_options_t *options);
cairo_private cairo_status_t
_cairo_gstate_get_font_face (cairo_gstate_t *gstate,
cairo_font_face_t **font_face);
cairo_private cairo_status_t
_cairo_gstate_get_scaled_font (cairo_gstate_t *gstate,
cairo_scaled_font_t **scaled_font);
cairo_private cairo_status_t
_cairo_gstate_get_font_extents (cairo_gstate_t *gstate,
cairo_font_extents_t *extents);
cairo_private cairo_status_t
_cairo_gstate_set_font_face (cairo_gstate_t *gstate,
cairo_font_face_t *font_face);
cairo_private cairo_status_t
_cairo_gstate_glyph_extents (cairo_gstate_t *gstate,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_text_extents_t *extents);
cairo_private cairo_status_t
_cairo_gstate_show_text_glyphs (cairo_gstate_t *gstate,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_glyph_text_info_t *info);
cairo_private cairo_status_t
_cairo_gstate_glyph_path (cairo_gstate_t *gstate,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_path_fixed_t *path);
cairo_private cairo_status_t
_cairo_gstate_set_antialias (cairo_gstate_t *gstate,
cairo_antialias_t antialias);
cairo_private cairo_antialias_t
_cairo_gstate_get_antialias (cairo_gstate_t *gstate);
#endif /* CAIRO_GSTATE_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-gstate.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-clip-inline.h"
#include "cairo-clip-private.h"
#include "cairo-error-private.h"
#include "cairo-list-inline.h"
#include "cairo-gstate-private.h"
#include "cairo-pattern-private.h"
#include "cairo-traps-private.h"
static cairo_status_t
_cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other);
static cairo_status_t
_cairo_gstate_ensure_font_face (cairo_gstate_t *gstate);
static cairo_status_t
_cairo_gstate_ensure_scaled_font (cairo_gstate_t *gstate);
static void
_cairo_gstate_unset_scaled_font (cairo_gstate_t *gstate);
static void
_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);
static void
_cairo_gstate_update_device_transform (cairo_observer_t *observer,
void *arg)
{
cairo_gstate_t *gstate = cairo_container_of (observer,
cairo_gstate_t,
device_transform_observer);
gstate->is_identity = (_cairo_matrix_is_identity (&gstate->ctm) &&
_cairo_matrix_is_identity (&gstate->target->device_transform));
}
cairo_status_t
_cairo_gstate_init (cairo_gstate_t *gstate,
cairo_surface_t *target)
{
VG (VALGRIND_MAKE_MEM_UNDEFINED (gstate, sizeof (cairo_gstate_t)));
gstate->next = NULL;
gstate->op = CAIRO_GSTATE_OPERATOR_DEFAULT;
gstate->opacity = 1.;
gstate->tolerance = CAIRO_GSTATE_TOLERANCE_DEFAULT;
gstate->antialias = CAIRO_ANTIALIAS_DEFAULT;
_cairo_stroke_style_init (&gstate->stroke_style);
gstate->fill_rule = CAIRO_GSTATE_FILL_RULE_DEFAULT;
gstate->font_face = NULL;
gstate->scaled_font = NULL;
gstate->previous_scaled_font = NULL;
cairo_matrix_init_scale (&gstate->font_matrix,
CAIRO_GSTATE_DEFAULT_FONT_SIZE,
CAIRO_GSTATE_DEFAULT_FONT_SIZE);
_cairo_font_options_init_default (&gstate->font_options);
gstate->clip = NULL;
gstate->target = cairo_surface_reference (target);
gstate->parent_target = NULL;
gstate->original_target = cairo_surface_reference (target);
gstate->device_transform_observer.callback = _cairo_gstate_update_device_transform;
cairo_list_add (&gstate->device_transform_observer.link,
&gstate->target->device_transform_observers);
gstate->is_identity = _cairo_matrix_is_identity (&gstate->target->device_transform);
cairo_matrix_init_identity (&gstate->ctm);
gstate->ctm_inverse = gstate->ctm;
gstate->source_ctm_inverse = gstate->ctm;
gstate->source = (cairo_pattern_t *) &_cairo_pattern_black.base;
/* Now that the gstate is fully initialized and ready for the eventual
* _cairo_gstate_fini(), we can check for errors (and not worry about
* the resource deallocation). */
return target->status;
}
/**
* _cairo_gstate_init_copy:
*
* Initialize @gstate by performing a deep copy of state fields from
* @other. Note that gstate->next is not copied but is set to %NULL by
* this function.
**/
static cairo_status_t
_cairo_gstate_init_copy (cairo_gstate_t *gstate, cairo_gstate_t *other)
{
cairo_status_t status;
VG (VALGRIND_MAKE_MEM_UNDEFINED (gstate, sizeof (cairo_gstate_t)));
gstate->op = other->op;
gstate->opacity = other->opacity;
gstate->tolerance = other->tolerance;
gstate->antialias = other->antialias;
status = _cairo_stroke_style_init_copy (&gstate->stroke_style,
&other->stroke_style);
if (unlikely (status))
return status;
gstate->fill_rule = other->fill_rule;
gstate->font_face = cairo_font_face_reference (other->font_face);
gstate->scaled_font = cairo_scaled_font_reference (other->scaled_font);
gstate->previous_scaled_font = cairo_scaled_font_reference (other->previous_scaled_font);
gstate->font_matrix = other->font_matrix;
_cairo_font_options_init_copy (&gstate->font_options , &other->font_options);
gstate->clip = _cairo_clip_copy (other->clip);
gstate->target = cairo_surface_reference (other->target);
/* parent_target is always set to NULL; it's only ever set by redirect_target */
gstate->parent_target = NULL;
gstate->original_target = cairo_surface_reference (other->original_target);
gstate->device_transform_observer.callback = _cairo_gstate_update_device_transform;
cairo_list_add (&gstate->device_transform_observer.link,
&gstate->target->device_transform_observers);
gstate->is_identity = other->is_identity;
gstate->ctm = other->ctm;
gstate->ctm_inverse = other->ctm_inverse;
gstate->source_ctm_inverse = other->source_ctm_inverse;
gstate->source = cairo_pattern_reference (other->source);
gstate->next = NULL;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_gstate_fini (cairo_gstate_t *gstate)
{
_cairo_stroke_style_fini (&gstate->stroke_style);
cairo_font_face_destroy (gstate->font_face);
gstate->font_face = NULL;
cairo_scaled_font_destroy (gstate->previous_scaled_font);
gstate->previous_scaled_font = NULL;
cairo_scaled_font_destroy (gstate->scaled_font);
gstate->scaled_font = NULL;
_cairo_clip_destroy (gstate->clip);
cairo_list_del (&gstate->device_transform_observer.link);
cairo_surface_destroy (gstate->target);
gstate->target = NULL;
cairo_surface_destroy (gstate->parent_target);
gstate->parent_target = NULL;
cairo_surface_destroy (gstate->original_target);
gstate->original_target = NULL;
cairo_pattern_destroy (gstate->source);
gstate->source = NULL;
VG (VALGRIND_MAKE_MEM_UNDEFINED (gstate, sizeof (cairo_gstate_t)));
}
/**
* _cairo_gstate_save:
* @gstate: input/output gstate pointer
*
* Makes a copy of the current state of @gstate and saves it
* to @gstate->next, then put the address of the newly allcated
* copy into @gstate. _cairo_gstate_restore() reverses this.
**/
cairo_status_t
_cairo_gstate_save (cairo_gstate_t **gstate, cairo_gstate_t **freelist)
{
cairo_gstate_t *top;
cairo_status_t status;
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
top = *freelist;
if (top == NULL) {
top = _cairo_malloc (sizeof (cairo_gstate_t));
if (unlikely (top == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
} else
*freelist = top->next;
status = _cairo_gstate_init_copy (top, *gstate);
if (unlikely (status)) {
top->next = *freelist;
*freelist = top;
return status;
}
top->next = *gstate;
*gstate = top;
return CAIRO_STATUS_SUCCESS;
}
/**
* _cairo_gstate_restore:
* @gstate: input/output gstate pointer
*
* Reverses the effects of one _cairo_gstate_save() call.
**/
cairo_status_t
_cairo_gstate_restore (cairo_gstate_t **gstate, cairo_gstate_t **freelist)
{
cairo_gstate_t *top;
top = *gstate;
if (top->next == NULL)
return _cairo_error (CAIRO_STATUS_INVALID_RESTORE);
*gstate = top->next;
_cairo_gstate_fini (top);
VG (VALGRIND_MAKE_MEM_UNDEFINED (&top->next, sizeof (cairo_gstate_t *)));
top->next = *freelist;
*freelist = top;
return CAIRO_STATUS_SUCCESS;
}
/**
* _cairo_gstate_redirect_target:
* @gstate: a #cairo_gstate_t
* @child: the new child target
*
* Redirect @gstate rendering to a "child" target. The original
* "parent" target with which the gstate was created will not be
* affected. See _cairo_gstate_get_target().
**/
cairo_status_t
_cairo_gstate_redirect_target (cairo_gstate_t *gstate, cairo_surface_t *child)
{
/* If this gstate is already redirected, this is an error; we need a
* new gstate to be able to redirect */
assert (gstate->parent_target == NULL);
/* Set up our new parent_target based on our current target;
* gstate->parent_target will take the ref that is held by gstate->target
*/
gstate->parent_target = gstate->target;
/* Now set up our new target; we overwrite gstate->target directly,
* since its ref is now owned by gstate->parent_target */
gstate->target = cairo_surface_reference (child);
gstate->is_identity &= _cairo_matrix_is_identity (&child->device_transform);
cairo_list_move (&gstate->device_transform_observer.link,
&gstate->target->device_transform_observers);
/* The clip is in surface backend coordinates for the previous target;
* translate it into the child's backend coordinates. */
_cairo_clip_destroy (gstate->clip);
gstate->clip = _cairo_clip_copy_with_translation (gstate->next->clip,
child->device_transform.x0 - gstate->parent_target->device_transform.x0,
child->device_transform.y0 - gstate->parent_target->device_transform.y0);
return CAIRO_STATUS_SUCCESS;
}
/**
* _cairo_gstate_is_group:
* @gstate: a #cairo_gstate_t
*
* Check if _cairo_gstate_redirect_target has been called on the head
* of the stack.
*
* Return value: %TRUE if @gstate is redirected to a target different
* than the previous state in the stack, %FALSE otherwise.
**/
cairo_bool_t
_cairo_gstate_is_group (cairo_gstate_t *gstate)
{
return gstate->parent_target != NULL;
}
/**
* _cairo_gstate_get_target:
* @gstate: a #cairo_gstate_t
*
* Return the current drawing target; if drawing is not redirected,
* this will be the same as _cairo_gstate_get_original_target().
*
* Return value: the current target surface
**/
cairo_surface_t *
_cairo_gstate_get_target (cairo_gstate_t *gstate)
{
return gstate->target;
}
/**
* _cairo_gstate_get_original_target:
* @gstate: a #cairo_gstate_t
*
* Return the original target with which @gstate was created. This
* function always returns the original target independent of any
* child target that may have been set with
* _cairo_gstate_redirect_target.
*
* Return value: the original target surface
**/
cairo_surface_t *
_cairo_gstate_get_original_target (cairo_gstate_t *gstate)
{
return gstate->original_target;
}
/**
* _cairo_gstate_get_clip:
* @gstate: a #cairo_gstate_t
*
* This space left intentionally blank.
*
* Return value: a pointer to the gstate's #cairo_clip_t structure.
**/
cairo_clip_t *
_cairo_gstate_get_clip (cairo_gstate_t *gstate)
{
return gstate->clip;
}
cairo_status_t
_cairo_gstate_set_source (cairo_gstate_t *gstate,
cairo_pattern_t *source)
{
if (source->status)
return source->status;
source = cairo_pattern_reference (source);
cairo_pattern_destroy (gstate->source);
gstate->source = source;
gstate->source_ctm_inverse = gstate->ctm_inverse;
return CAIRO_STATUS_SUCCESS;
}
cairo_pattern_t *
_cairo_gstate_get_source (cairo_gstate_t *gstate)
{
if (gstate->source == &_cairo_pattern_black.base) {
/* do not expose the static object to the user */
gstate->source = _cairo_pattern_create_solid (CAIRO_COLOR_BLACK);
}
return gstate->source;
}
cairo_status_t
_cairo_gstate_set_operator (cairo_gstate_t *gstate, cairo_operator_t op)
{
gstate->op = op;
return CAIRO_STATUS_SUCCESS;
}
cairo_operator_t
_cairo_gstate_get_operator (cairo_gstate_t *gstate)
{
return gstate->op;
}
cairo_status_t
_cairo_gstate_set_opacity (cairo_gstate_t *gstate, double op)
{
gstate->opacity = op;
return CAIRO_STATUS_SUCCESS;
}
double
_cairo_gstate_get_opacity (cairo_gstate_t *gstate)
{
return gstate->opacity;
}
cairo_status_t
_cairo_gstate_set_tolerance (cairo_gstate_t *gstate, double tolerance)
{
gstate->tolerance = tolerance;
return CAIRO_STATUS_SUCCESS;
}
double
_cairo_gstate_get_tolerance (cairo_gstate_t *gstate)
{
return gstate->tolerance;
}
cairo_status_t
_cairo_gstate_set_fill_rule (cairo_gstate_t *gstate, cairo_fill_rule_t fill_rule)
{
gstate->fill_rule = fill_rule;
return CAIRO_STATUS_SUCCESS;
}
cairo_fill_rule_t
_cairo_gstate_get_fill_rule (cairo_gstate_t *gstate)
{
return gstate->fill_rule;
}
cairo_status_t
_cairo_gstate_set_line_width (cairo_gstate_t *gstate, double width)
{
gstate->stroke_style.line_width = width;
return CAIRO_STATUS_SUCCESS;
}
double
_cairo_gstate_get_line_width (cairo_gstate_t *gstate)
{
return gstate->stroke_style.line_width;
}
cairo_status_t
_cairo_gstate_set_line_cap (cairo_gstate_t *gstate, cairo_line_cap_t line_cap)
{
gstate->stroke_style.line_cap = line_cap;
return CAIRO_STATUS_SUCCESS;
}
cairo_line_cap_t
_cairo_gstate_get_line_cap (cairo_gstate_t *gstate)
{
return gstate->stroke_style.line_cap;
}
cairo_status_t
_cairo_gstate_set_line_join (cairo_gstate_t *gstate, cairo_line_join_t line_join)
{
gstate->stroke_style.line_join = line_join;
return CAIRO_STATUS_SUCCESS;
}
cairo_line_join_t
_cairo_gstate_get_line_join (cairo_gstate_t *gstate)
{
return gstate->stroke_style.line_join;
}
cairo_status_t
_cairo_gstate_set_dash (cairo_gstate_t *gstate, const double *dash, int num_dashes, double offset)
{
double dash_total, on_total, off_total;
int i, j;
free (gstate->stroke_style.dash);
gstate->stroke_style.num_dashes = num_dashes;
if (gstate->stroke_style.num_dashes == 0) {
gstate->stroke_style.dash = NULL;
gstate->stroke_style.dash_offset = 0.0;
return CAIRO_STATUS_SUCCESS;
}
gstate->stroke_style.dash = _cairo_malloc_ab (gstate->stroke_style.num_dashes, sizeof (double));
if (unlikely (gstate->stroke_style.dash == NULL)) {
gstate->stroke_style.num_dashes = 0;
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
on_total = off_total = dash_total = 0.0;
for (i = j = 0; i < num_dashes; i++) {
if (dash[i] < 0)
return _cairo_error (CAIRO_STATUS_INVALID_DASH);
if (dash[i] == 0 && i > 0 && i < num_dashes - 1) {
if (dash[++i] < 0)
return _cairo_error (CAIRO_STATUS_INVALID_DASH);
gstate->stroke_style.dash[j-1] += dash[i];
gstate->stroke_style.num_dashes -= 2;
} else
gstate->stroke_style.dash[j++] = dash[i];
if (dash[i]) {
dash_total += dash[i];
if ((i & 1) == 0)
on_total += dash[i];
else
off_total += dash[i];
}
}
if (dash_total == 0.0)
return _cairo_error (CAIRO_STATUS_INVALID_DASH);
/* An odd dash value indicate symmetric repeating, so the total
* is twice as long. */
if (gstate->stroke_style.num_dashes & 1) {
dash_total *= 2;
on_total += off_total;
}
if (dash_total - on_total < CAIRO_FIXED_ERROR_DOUBLE) {
/* Degenerate dash -> solid line */
free (gstate->stroke_style.dash);
gstate->stroke_style.dash = NULL;
gstate->stroke_style.num_dashes = 0;
gstate->stroke_style.dash_offset = 0.0;
return CAIRO_STATUS_SUCCESS;
}
/* The dashing code doesn't like a negative offset or a big positive
* offset, so we compute an equivalent offset which is guaranteed to be
* positive and less than twice the pattern length. */
offset = fmod (offset, dash_total);
if (offset < 0.0)
offset += dash_total;
if (offset <= 0.0) /* Take care of -0 */
offset = 0.0;
gstate->stroke_style.dash_offset = offset;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_gstate_get_dash (cairo_gstate_t *gstate,
double *dashes,
int *num_dashes,
double *offset)
{
if (dashes) {
memcpy (dashes,
gstate->stroke_style.dash,
sizeof (double) * gstate->stroke_style.num_dashes);
}
if (num_dashes)
*num_dashes = gstate->stroke_style.num_dashes;
if (offset)
*offset = gstate->stroke_style.dash_offset;
}
cairo_status_t
_cairo_gstate_set_miter_limit (cairo_gstate_t *gstate, double limit)
{
gstate->stroke_style.miter_limit = limit;
return CAIRO_STATUS_SUCCESS;
}
double
_cairo_gstate_get_miter_limit (cairo_gstate_t *gstate)
{
return gstate->stroke_style.miter_limit;
}
void
_cairo_gstate_get_matrix (cairo_gstate_t *gstate, cairo_matrix_t *matrix)
{
*matrix = gstate->ctm;
}
cairo_status_t
_cairo_gstate_translate (cairo_gstate_t *gstate, double tx, double ty)
{
cairo_matrix_t tmp;
if (! ISFINITE (tx) || ! ISFINITE (ty))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
_cairo_gstate_unset_scaled_font (gstate);
cairo_matrix_init_translate (&tmp, tx, ty);
cairo_matrix_multiply (&gstate->ctm, &tmp, &gstate->ctm);
gstate->is_identity = FALSE;
/* paranoid check against gradual numerical instability */
if (! _cairo_matrix_is_invertible (&gstate->ctm))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
cairo_matrix_init_translate (&tmp, -tx, -ty);
cairo_matrix_multiply (&gstate->ctm_inverse, &gstate->ctm_inverse, &tmp);
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_gstate_scale (cairo_gstate_t *gstate, double sx, double sy)
{
cairo_matrix_t tmp;
if (sx * sy == 0.) /* either sx or sy is 0, or det == 0 due to underflow */
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
if (! ISFINITE (sx) || ! ISFINITE (sy))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
_cairo_gstate_unset_scaled_font (gstate);
cairo_matrix_init_scale (&tmp, sx, sy);
cairo_matrix_multiply (&gstate->ctm, &tmp, &gstate->ctm);
gstate->is_identity = FALSE;
/* paranoid check against gradual numerical instability */
if (! _cairo_matrix_is_invertible (&gstate->ctm))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
cairo_matrix_init_scale (&tmp, 1/sx, 1/sy);
cairo_matrix_multiply (&gstate->ctm_inverse, &gstate->ctm_inverse, &tmp);
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_gstate_rotate (cairo_gstate_t *gstate, double angle)
{
cairo_matrix_t tmp;
if (angle == 0.)
return CAIRO_STATUS_SUCCESS;
if (! ISFINITE (angle))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
_cairo_gstate_unset_scaled_font (gstate);
cairo_matrix_init_rotate (&tmp, angle);
cairo_matrix_multiply (&gstate->ctm, &tmp, &gstate->ctm);
gstate->is_identity = FALSE;
/* paranoid check against gradual numerical instability */
if (! _cairo_matrix_is_invertible (&gstate->ctm))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
cairo_matrix_init_rotate (&tmp, -angle);
cairo_matrix_multiply (&gstate->ctm_inverse, &gstate->ctm_inverse, &tmp);
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_gstate_transform (cairo_gstate_t *gstate,
const cairo_matrix_t *matrix)
{
cairo_matrix_t tmp;
cairo_status_t status;
if (! _cairo_matrix_is_invertible (matrix))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
if (_cairo_matrix_is_identity (matrix))
return CAIRO_STATUS_SUCCESS;
tmp = *matrix;
status = cairo_matrix_invert (&tmp);
if (unlikely (status))
return status;
_cairo_gstate_unset_scaled_font (gstate);
cairo_matrix_multiply (&gstate->ctm, matrix, &gstate->ctm);
cairo_matrix_multiply (&gstate->ctm_inverse, &gstate->ctm_inverse, &tmp);
gstate->is_identity = FALSE;
/* paranoid check against gradual numerical instability */
if (! _cairo_matrix_is_invertible (&gstate->ctm))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_gstate_set_matrix (cairo_gstate_t *gstate,
const cairo_matrix_t *matrix)
{
cairo_status_t status;
if (memcmp (matrix, &gstate->ctm, sizeof (cairo_matrix_t)) == 0)
return CAIRO_STATUS_SUCCESS;
if (! _cairo_matrix_is_invertible (matrix))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
if (_cairo_matrix_is_identity (matrix)) {
_cairo_gstate_identity_matrix (gstate);
return CAIRO_STATUS_SUCCESS;
}
_cairo_gstate_unset_scaled_font (gstate);
gstate->ctm = *matrix;
gstate->ctm_inverse = *matrix;
status = cairo_matrix_invert (&gstate->ctm_inverse);
assert (status == CAIRO_STATUS_SUCCESS);
gstate->is_identity = FALSE;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_gstate_identity_matrix (cairo_gstate_t *gstate)
{
if (_cairo_matrix_is_identity (&gstate->ctm))
return;
_cairo_gstate_unset_scaled_font (gstate);
cairo_matrix_init_identity (&gstate->ctm);
cairo_matrix_init_identity (&gstate->ctm_inverse);
gstate->is_identity = _cairo_matrix_is_identity (&gstate->target->device_transform);
}
void
_cairo_gstate_user_to_device (cairo_gstate_t *gstate, double *x, double *y)
{
cairo_matrix_transform_point (&gstate->ctm, x, y);
}
void
_cairo_gstate_user_to_device_distance (cairo_gstate_t *gstate,
double *dx, double *dy)
{
cairo_matrix_transform_distance (&gstate->ctm, dx, dy);
}
void
_cairo_gstate_device_to_user (cairo_gstate_t *gstate, double *x, double *y)
{
cairo_matrix_transform_point (&gstate->ctm_inverse, x, y);
}
void
_cairo_gstate_device_to_user_distance (cairo_gstate_t *gstate,
double *dx, double *dy)
{
cairo_matrix_transform_distance (&gstate->ctm_inverse, dx, dy);
}
void
_do_cairo_gstate_user_to_backend (cairo_gstate_t *gstate, double *x, double *y)
{
cairo_matrix_transform_point (&gstate->ctm, x, y);
cairo_matrix_transform_point (&gstate->target->device_transform, x, y);
}
void
_do_cairo_gstate_user_to_backend_distance (cairo_gstate_t *gstate, double *x, double *y)
{
cairo_matrix_transform_distance (&gstate->ctm, x, y);
cairo_matrix_transform_distance (&gstate->target->device_transform, x, y);
}
void
_do_cairo_gstate_backend_to_user (cairo_gstate_t *gstate, double *x, double *y)
{
cairo_matrix_transform_point (&gstate->target->device_transform_inverse, x, y);
cairo_matrix_transform_point (&gstate->ctm_inverse, x, y);
}
void
_do_cairo_gstate_backend_to_user_distance (cairo_gstate_t *gstate, double *x, double *y)
{
cairo_matrix_transform_distance (&gstate->target->device_transform_inverse, x, y);
cairo_matrix_transform_distance (&gstate->ctm_inverse, x, y);
}
void
_cairo_gstate_backend_to_user_rectangle (cairo_gstate_t *gstate,
double *x1, double *y1,
double *x2, double *y2,
cairo_bool_t *is_tight)
{
cairo_matrix_t matrix_inverse;
if (! _cairo_matrix_is_identity (&gstate->target->device_transform_inverse) ||
! _cairo_matrix_is_identity (&gstate->ctm_inverse))
{
cairo_matrix_multiply (&matrix_inverse,
&gstate->target->device_transform_inverse,
&gstate->ctm_inverse);
_cairo_matrix_transform_bounding_box (&matrix_inverse,
x1, y1, x2, y2, is_tight);
}
else
{
if (is_tight)
*is_tight = TRUE;
}
}
/* XXX: NYI
cairo_status_t
_cairo_gstate_stroke_to_path (cairo_gstate_t *gstate)
{
cairo_status_t status;
_cairo_pen_init (&gstate);
return CAIRO_STATUS_SUCCESS;
}
*/
void
_cairo_gstate_path_extents (cairo_gstate_t *gstate,
cairo_path_fixed_t *path,
double *x1, double *y1,
double *x2, double *y2)
{
cairo_box_t box;
double px1, py1, px2, py2;
if (_cairo_path_fixed_extents (path, &box)) {
px1 = _cairo_fixed_to_double (box.p1.x);
py1 = _cairo_fixed_to_double (box.p1.y);
px2 = _cairo_fixed_to_double (box.p2.x);
py2 = _cairo_fixed_to_double (box.p2.y);
_cairo_gstate_backend_to_user_rectangle (gstate,
&px1, &py1, &px2, &py2,
NULL);
} else {
px1 = 0.0;
py1 = 0.0;
px2 = 0.0;
py2 = 0.0;
}
if (x1)
*x1 = px1;
if (y1)
*y1 = py1;
if (x2)
*x2 = px2;
if (y2)
*y2 = py2;
}
static void
_cairo_gstate_copy_pattern (cairo_pattern_t *pattern,
const cairo_pattern_t *original)
{
/* First check if the we can replace the original with a much simpler
* pattern. For example, gradients that are uniform or just have a single
* stop can sometimes be replaced with a solid.
*/
if (_cairo_pattern_is_clear (original)) {
_cairo_pattern_init_solid ((cairo_solid_pattern_t *) pattern,
CAIRO_COLOR_TRANSPARENT);
return;
}
if (original->type == CAIRO_PATTERN_TYPE_LINEAR ||
original->type == CAIRO_PATTERN_TYPE_RADIAL)
{
cairo_color_t color;
if (_cairo_gradient_pattern_is_solid ((cairo_gradient_pattern_t *) original,
NULL,
&color))
{
_cairo_pattern_init_solid ((cairo_solid_pattern_t *) pattern,
&color);
return;
}
}
_cairo_pattern_init_static_copy (pattern, original);
}
static void
_cairo_gstate_copy_transformed_pattern (cairo_gstate_t *gstate,
cairo_pattern_t *pattern,
const cairo_pattern_t *original,
const cairo_matrix_t *ctm_inverse)
{
_cairo_gstate_copy_pattern (pattern, original);
/* apply device_transform first so that it is transformed by ctm_inverse */
if (original->type == CAIRO_PATTERN_TYPE_SURFACE) {
cairo_surface_pattern_t *surface_pattern;
cairo_surface_t *surface;
surface_pattern = (cairo_surface_pattern_t *) original;
surface = surface_pattern->surface;
if (_cairo_surface_has_device_transform (surface))
_cairo_pattern_pretransform (pattern, &surface->device_transform);
}
if (! _cairo_matrix_is_identity (ctm_inverse))
_cairo_pattern_transform (pattern, ctm_inverse);
if (_cairo_surface_has_device_transform (gstate->target)) {
_cairo_pattern_transform (pattern,
&gstate->target->device_transform_inverse);
}
}
static void
_cairo_gstate_copy_transformed_source (cairo_gstate_t *gstate,
cairo_pattern_t *pattern)
{
_cairo_gstate_copy_transformed_pattern (gstate, pattern,
gstate->source,
&gstate->source_ctm_inverse);
}
static void
_cairo_gstate_copy_transformed_mask (cairo_gstate_t *gstate,
cairo_pattern_t *pattern,
cairo_pattern_t *mask)
{
_cairo_gstate_copy_transformed_pattern (gstate, pattern,
mask,
&gstate->ctm_inverse);
}
static cairo_operator_t
_reduce_op (cairo_gstate_t *gstate)
{
cairo_operator_t op;
const cairo_pattern_t *pattern;
op = gstate->op;
if (op != CAIRO_OPERATOR_SOURCE)
return op;
pattern = gstate->source;
if (pattern->type == CAIRO_PATTERN_TYPE_SOLID) {
const cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) pattern;
if (solid->color.alpha_short <= 0x00ff) {
op = CAIRO_OPERATOR_CLEAR;
} else if ((gstate->target->content & CAIRO_CONTENT_ALPHA) == 0) {
if ((solid->color.red_short |
solid->color.green_short |
solid->color.blue_short) <= 0x00ff)
{
op = CAIRO_OPERATOR_CLEAR;
}
}
} else if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
const cairo_surface_pattern_t *surface = (cairo_surface_pattern_t *) pattern;
if (surface->surface->is_clear &&
surface->surface->content & CAIRO_CONTENT_ALPHA)
{
op = CAIRO_OPERATOR_CLEAR;
}
} else {
const cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t *) pattern;
if (gradient->n_stops == 0)
op = CAIRO_OPERATOR_CLEAR;
}
return op;
}
static cairo_status_t
_cairo_gstate_get_pattern_status (const cairo_pattern_t *pattern)
{
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;
}
return pattern->status;
}
cairo_status_t
_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;
status = _cairo_gstate_get_pattern_status (gstate->source);
if (unlikely (status))
return status;
if (gstate->op == CAIRO_OPERATOR_DEST)
return CAIRO_STATUS_SUCCESS;
if (_cairo_clip_is_all_clipped (gstate->clip))
return CAIRO_STATUS_SUCCESS;
op = _reduce_op (gstate);
if (op == CAIRO_OPERATOR_CLEAR) {
pattern = &_cairo_pattern_clear.base;
} else {
_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
pattern = &source_pattern.base;
}
return _cairo_surface_paint (gstate->target,
op, pattern,
gstate->clip);
}
cairo_status_t
_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;
status = _cairo_gstate_get_pattern_status (mask);
if (unlikely (status))
return status;
status = _cairo_gstate_get_pattern_status (gstate->source);
if (unlikely (status))
return status;
if (gstate->op == CAIRO_OPERATOR_DEST)
return CAIRO_STATUS_SUCCESS;
if (_cairo_clip_is_all_clipped (gstate->clip))
return CAIRO_STATUS_SUCCESS;
assert (gstate->opacity == 1.0);
if (_cairo_pattern_is_opaque (mask, NULL))
return _cairo_gstate_paint (gstate);
if (_cairo_pattern_is_clear (mask) &&
_cairo_operator_bounded_by_mask (gstate->op))
{
return CAIRO_STATUS_SUCCESS;
}
op = _reduce_op (gstate);
if (op == CAIRO_OPERATOR_CLEAR) {
source = &_cairo_pattern_clear.base;
} else {
_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
source = &source_pattern.base;
}
_cairo_gstate_copy_transformed_mask (gstate, &mask_pattern.base, mask);
if (source->type == CAIRO_PATTERN_TYPE_SOLID &&
mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID &&
_cairo_operator_bounded_by_source (op))
{
const cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) source;
cairo_color_t combined;
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 {
combined = solid->color;
_cairo_color_multiply_alpha (&combined, mask_pattern.solid.color.alpha);
}
_cairo_pattern_init_solid (&source_pattern.solid, &combined);
status = _cairo_surface_paint (gstate->target, op,
&source_pattern.base,
gstate->clip);
}
else
{
status = _cairo_surface_mask (gstate->target, op,
source,
&mask_pattern.base,
gstate->clip);
}
return status;
}
cairo_status_t
_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;
status = _cairo_gstate_get_pattern_status (gstate->source);
if (unlikely (status))
return status;
if (gstate->op == CAIRO_OPERATOR_DEST)
return CAIRO_STATUS_SUCCESS;
if (gstate->stroke_style.line_width <= 0.0)
return CAIRO_STATUS_SUCCESS;
if (_cairo_clip_is_all_clipped (gstate->clip))
return CAIRO_STATUS_SUCCESS;
assert (gstate->opacity == 1.0);
cairo_matrix_multiply (&aggregate_transform,
&gstate->ctm,
&gstate->target->device_transform);
cairo_matrix_multiply (&aggregate_transform_inverse,
&gstate->target->device_transform_inverse,
&gstate->ctm_inverse);
memcpy (&style, &gstate->stroke_style, sizeof (gstate->stroke_style));
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);
}
_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
return _cairo_surface_stroke (gstate->target,
gstate->op,
&source_pattern.base,
path,
&style,
&aggregate_transform,
&aggregate_transform_inverse,
gstate->tolerance,
gstate->antialias,
gstate->clip);
}
cairo_status_t
_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;
if (gstate->stroke_style.line_width <= 0.0) {
*inside_ret = FALSE;
return CAIRO_STATUS_SUCCESS;
}
_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.
*/
_cairo_path_fixed_approximate_stroke_extents (path,
&gstate->stroke_style,
&gstate->ctm,
gstate->target->is_vector,
&extents);
if (x < extents.x || x > extents.x + extents.width ||
y < extents.y || y > extents.y + extents.height)
{
*inside_ret = FALSE;
return CAIRO_STATUS_SUCCESS;
}
limit.p1.x = _cairo_fixed_from_double (x) - 1;
limit.p1.y = _cairo_fixed_from_double (y) - 1;
limit.p2.x = limit.p1.x + 2;
limit.p2.y = limit.p1.y + 2;
_cairo_traps_init (&traps);
_cairo_traps_limit (&traps, &limit, 1);
status = _cairo_path_fixed_stroke_polygon_to_traps (path,
&gstate->stroke_style,
&gstate->ctm,
&gstate->ctm_inverse,
gstate->tolerance,
&traps);
if (unlikely (status))
goto BAIL;
*inside_ret = _cairo_traps_contain (&traps, x, y);
BAIL:
_cairo_traps_fini (&traps);
return status;
}
cairo_status_t
_cairo_gstate_fill (cairo_gstate_t *gstate, cairo_path_fixed_t *path)
{
cairo_status_t status;
status = _cairo_gstate_get_pattern_status (gstate->source);
if (unlikely (status))
return status;
if (gstate->op == CAIRO_OPERATOR_DEST)
return CAIRO_STATUS_SUCCESS;
if (_cairo_clip_is_all_clipped (gstate->clip))
return CAIRO_STATUS_SUCCESS;
assert (gstate->opacity == 1.0);
if (_cairo_path_fixed_fill_is_empty (path)) {
if (_cairo_operator_bounded_by_mask (gstate->op))
return CAIRO_STATUS_SUCCESS;
status = _cairo_surface_paint (gstate->target,
CAIRO_OPERATOR_CLEAR,
&_cairo_pattern_clear.base,
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;
op = _reduce_op (gstate);
if (op == CAIRO_OPERATOR_CLEAR) {
pattern = &_cairo_pattern_clear.base;
} else {
_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
pattern = &source_pattern.base;
}
/* Toolkits often paint the entire background with a fill */
if (_cairo_surface_get_extents (gstate->target, &extents) &&
_cairo_path_fixed_is_box (path, &box) &&
box.p1.x <= _cairo_fixed_from_int (extents.x) &&
box.p1.y <= _cairo_fixed_from_int (extents.y) &&
box.p2.x >= _cairo_fixed_from_int (extents.x + extents.width) &&
box.p2.y >= _cairo_fixed_from_int (extents.y + extents.height))
{
status = _cairo_surface_paint (gstate->target, op, pattern,
gstate->clip);
}
else
{
status = _cairo_surface_fill (gstate->target, op, pattern,
path,
gstate->fill_rule,
gstate->tolerance,
gstate->antialias,
gstate->clip);
}
}
return status;
}
cairo_bool_t
_cairo_gstate_in_fill (cairo_gstate_t *gstate,
cairo_path_fixed_t *path,
double x,
double y)
{
_cairo_gstate_user_to_backend (gstate, &x, &y);
return _cairo_path_fixed_in_fill (path,
gstate->fill_rule,
gstate->tolerance,
x, y);
}
cairo_bool_t
_cairo_gstate_in_clip (cairo_gstate_t *gstate,
double x,
double y)
{
cairo_clip_t *clip = gstate->clip;
int i;
if (_cairo_clip_is_all_clipped (clip))
return FALSE;
if (clip == NULL)
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
_cairo_gstate_copy_page (cairo_gstate_t *gstate)
{
cairo_surface_copy_page (gstate->target);
return cairo_surface_status (gstate->target);
}
cairo_status_t
_cairo_gstate_show_page (cairo_gstate_t *gstate)
{
cairo_surface_show_page (gstate->target);
return cairo_surface_status (gstate->target);
}
static void
_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;
px1 = _cairo_fixed_to_double (extents->p1.x);
py1 = _cairo_fixed_to_double (extents->p1.y);
px2 = _cairo_fixed_to_double (extents->p2.x);
py2 = _cairo_fixed_to_double (extents->p2.y);
_cairo_gstate_backend_to_user_rectangle (gstate,
&px1, &py1, &px2, &py2,
NULL);
if (x1)
*x1 = px1;
if (y1)
*y1 = py1;
if (x2)
*x2 = px2;
if (y2)
*y2 = py2;
}
cairo_status_t
_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;
if (x1)
*x1 = 0.0;
if (y1)
*y1 = 0.0;
if (x2)
*x2 = 0.0;
if (y2)
*y2 = 0.0;
if (gstate->stroke_style.line_width <= 0.0)
return CAIRO_STATUS_SUCCESS;
status = CAIRO_INT_STATUS_UNSUPPORTED;
if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
cairo_boxes_t boxes;
_cairo_boxes_init (&boxes);
status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
&gstate->stroke_style,
&gstate->ctm,
gstate->antialias,
&boxes);
empty = boxes.num_boxes == 0;
if (! empty)
_cairo_boxes_extents (&boxes, &extents);
_cairo_boxes_fini (&boxes);
}
if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
cairo_polygon_t polygon;
_cairo_polygon_init (&polygon, NULL, 0);
status = _cairo_path_fixed_stroke_to_polygon (path,
&gstate->stroke_style,
&gstate->ctm,
&gstate->ctm_inverse,
gstate->tolerance,
&polygon);
empty = polygon.num_edges == 0;
if (! empty)
extents = polygon.extents;
_cairo_polygon_fini (&polygon);
}
if (! empty) {
_cairo_gstate_extents_to_user_rectangle (gstate, &extents,
x1, y1, x2, y2);
}
return status;
}
cairo_status_t
_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;
if (x1)
*x1 = 0.0;
if (y1)
*y1 = 0.0;
if (x2)
*x2 = 0.0;
if (y2)
*y2 = 0.0;
if (_cairo_path_fixed_fill_is_empty (path))
return CAIRO_STATUS_SUCCESS;
if (_cairo_path_fixed_fill_is_rectilinear (path)) {
cairo_boxes_t boxes;
_cairo_boxes_init (&boxes);
status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
gstate->fill_rule,
gstate->antialias,
&boxes);
empty = boxes.num_boxes == 0;
if (! empty)
_cairo_boxes_extents (&boxes, &extents);
_cairo_boxes_fini (&boxes);
} else {
cairo_traps_t traps;
_cairo_traps_init (&traps);
status = _cairo_path_fixed_fill_to_traps (path,
gstate->fill_rule,
gstate->tolerance,
&traps);
empty = traps.num_traps == 0;
if (! empty)
_cairo_traps_extents (&traps, &extents);
_cairo_traps_fini (&traps);
}
if (! empty) {
_cairo_gstate_extents_to_user_rectangle (gstate, &extents,
x1, y1, x2, y2);
}
return status;
}
cairo_status_t
_cairo_gstate_reset_clip (cairo_gstate_t *gstate)
{
_cairo_clip_destroy (gstate->clip);
gstate->clip = NULL;
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_gstate_clip (cairo_gstate_t *gstate, cairo_path_fixed_t *path)
{
gstate->clip =
_cairo_clip_intersect_path (gstate->clip,
path,
gstate->fill_rule,
gstate->tolerance,
gstate->antialias);
/* XXX */
return CAIRO_STATUS_SUCCESS;
}
static cairo_bool_t
_cairo_gstate_int_clip_extents (cairo_gstate_t *gstate,
cairo_rectangle_int_t *extents)
{
cairo_bool_t is_bounded;
is_bounded = _cairo_surface_get_extents (gstate->target, extents);
if (gstate->clip) {
_cairo_rectangle_intersect (extents,
_cairo_clip_get_extents (gstate->clip));
is_bounded = TRUE;
}
return is_bounded;
}
cairo_bool_t
_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;
if (! _cairo_gstate_int_clip_extents (gstate, &extents))
return FALSE;
px1 = extents.x;
py1 = extents.y;
px2 = extents.x + (int) extents.width;
py2 = extents.y + (int) extents.height;
_cairo_gstate_backend_to_user_rectangle (gstate,
&px1, &py1, &px2, &py2,
NULL);
if (x1)
*x1 = px1;
if (y1)
*y1 = py1;
if (x2)
*x2 = px2;
if (y2)
*y2 = py2;
return TRUE;
}
cairo_rectangle_list_t*
_cairo_gstate_copy_clip_rectangle_list (cairo_gstate_t *gstate)
{
cairo_rectangle_int_t extents;
cairo_rectangle_list_t *list;
cairo_clip_t *clip;
if (_cairo_surface_get_extents (gstate->target, &extents))
clip = _cairo_clip_copy_intersect_rectangle (gstate->clip, &extents);
else
clip = gstate->clip;
list = _cairo_clip_copy_rectangle_list (clip, gstate);
if (clip != gstate->clip)
_cairo_clip_destroy (clip);
return list;
}
cairo_status_t
_cairo_gstate_tag_begin (cairo_gstate_t *gstate,
const char *tag_name, const char *attributes)
{
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;
status = _cairo_gstate_get_pattern_status (gstate->source);
if (unlikely (status))
return status;
cairo_matrix_multiply (&aggregate_transform,
&gstate->ctm,
&gstate->target->device_transform);
cairo_matrix_multiply (&aggregate_transform_inverse,
&gstate->target->device_transform_inverse,
&gstate->ctm_inverse);
memcpy (&style, &gstate->stroke_style, sizeof (gstate->stroke_style));
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);
}
_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
return _cairo_surface_tag (gstate->target,
TRUE, /* begin */
tag_name,
attributes ? attributes : "",
&source_pattern.base,
&style,
&aggregate_transform,
&aggregate_transform_inverse,
gstate->clip);
}
cairo_status_t
_cairo_gstate_tag_end (cairo_gstate_t *gstate,
const char *tag_name)
{
return _cairo_surface_tag (gstate->target,
FALSE, /* begin */
tag_name,
NULL, /* attributes */
NULL, /* source */
NULL, /* stroke_style */
NULL, /* ctm */
NULL, /* ctm_inverse*/
NULL); /* clip */
}
static void
_cairo_gstate_unset_scaled_font (cairo_gstate_t *gstate)
{
if (gstate->scaled_font == NULL)
return;
if (gstate->previous_scaled_font != NULL)
cairo_scaled_font_destroy (gstate->previous_scaled_font);
gstate->previous_scaled_font = gstate->scaled_font;
gstate->scaled_font = NULL;
}
cairo_status_t
_cairo_gstate_set_font_size (cairo_gstate_t *gstate,
double size)
{
_cairo_gstate_unset_scaled_font (gstate);
cairo_matrix_init_scale (&gstate->font_matrix, size, size);
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_gstate_set_font_matrix (cairo_gstate_t *gstate,
const cairo_matrix_t *matrix)
{
if (memcmp (matrix, &gstate->font_matrix, sizeof (cairo_matrix_t)) == 0)
return CAIRO_STATUS_SUCCESS;
_cairo_gstate_unset_scaled_font (gstate);
gstate->font_matrix = *matrix;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_gstate_get_font_matrix (cairo_gstate_t *gstate,
cairo_matrix_t *matrix)
{
*matrix = gstate->font_matrix;
}
void
_cairo_gstate_set_font_options (cairo_gstate_t *gstate,
const cairo_font_options_t *options)
{
if (memcmp (options, &gstate->font_options, sizeof (cairo_font_options_t)) == 0)
return;
_cairo_gstate_unset_scaled_font (gstate);
_cairo_font_options_init_copy (&gstate->font_options, options);
}
void
_cairo_gstate_get_font_options (cairo_gstate_t *gstate,
cairo_font_options_t *options)
{
*options = gstate->font_options;
}
cairo_status_t
_cairo_gstate_get_font_face (cairo_gstate_t *gstate,
cairo_font_face_t **font_face)
{
cairo_status_t status;
status = _cairo_gstate_ensure_font_face (gstate);
if (unlikely (status))
return status;
*font_face = gstate->font_face;
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_gstate_get_scaled_font (cairo_gstate_t *gstate,
cairo_scaled_font_t **scaled_font)
{
cairo_status_t status;
status = _cairo_gstate_ensure_scaled_font (gstate);
if (unlikely (status))
return status;
*scaled_font = gstate->scaled_font;
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
_cairo_gstate_ensure_font_face (cairo_gstate_t *gstate)
{
cairo_font_face_t *font_face;
if (gstate->font_face != NULL)
return gstate->font_face->status;
font_face = cairo_toy_font_face_create (CAIRO_FONT_FAMILY_DEFAULT,
CAIRO_FONT_SLANT_DEFAULT,
CAIRO_FONT_WEIGHT_DEFAULT);
if (font_face->status)
return font_face->status;
gstate->font_face = font_face;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_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;
if (gstate->scaled_font != NULL)
return gstate->scaled_font->status;
status = _cairo_gstate_ensure_font_face (gstate);
if (unlikely (status))
return status;
cairo_surface_get_font_options (gstate->target, &options);
cairo_font_options_merge (&options, &gstate->font_options);
cairo_matrix_multiply (&font_ctm,
&gstate->ctm,
&gstate->target->device_transform);
scaled_font = cairo_scaled_font_create (gstate->font_face,
&gstate->font_matrix,
&font_ctm,
&options);
status = cairo_scaled_font_status (scaled_font);
if (unlikely (status))
return status;
gstate->scaled_font = scaled_font;
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_gstate_get_font_extents (cairo_gstate_t *gstate,
cairo_font_extents_t *extents)
{
cairo_status_t status = _cairo_gstate_ensure_scaled_font (gstate);
if (unlikely (status))
return status;
cairo_scaled_font_extents (gstate->scaled_font, extents);
return cairo_scaled_font_status (gstate->scaled_font);
}
cairo_status_t
_cairo_gstate_set_font_face (cairo_gstate_t *gstate,
cairo_font_face_t *font_face)
{
if (font_face && font_face->status)
return _cairo_error (font_face->status);
if (font_face == gstate->font_face)
return CAIRO_STATUS_SUCCESS;
cairo_font_face_destroy (gstate->font_face);
gstate->font_face = cairo_font_face_reference (font_face);
_cairo_gstate_unset_scaled_font (gstate);
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_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;
status = _cairo_gstate_ensure_scaled_font (gstate);
if (unlikely (status))
return status;
cairo_scaled_font_glyph_extents (gstate->scaled_font,
glyphs, num_glyphs,
extents);
return cairo_scaled_font_status (gstate->scaled_font);
}
cairo_status_t
_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;
status = _cairo_gstate_get_pattern_status (gstate->source);
if (unlikely (status))
return status;
if (gstate->op == CAIRO_OPERATOR_DEST)
return CAIRO_STATUS_SUCCESS;
if (_cairo_clip_is_all_clipped (gstate->clip))
return CAIRO_STATUS_SUCCESS;
status = _cairo_gstate_ensure_scaled_font (gstate);
if (unlikely (status))
return status;
transformed_glyphs = stack_transformed_glyphs;
transformed_clusters = stack_transformed_clusters;
if (num_glyphs > ARRAY_LENGTH (stack_transformed_glyphs)) {
transformed_glyphs = cairo_glyph_allocate (num_glyphs);
if (unlikely (transformed_glyphs == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
if (info != NULL) {
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;
}
}
_cairo_gstate_transform_glyphs_to_backend (gstate,
glyphs, num_glyphs,
info->clusters,
info->num_clusters,
info->cluster_flags,
transformed_glyphs,
&num_glyphs,
transformed_clusters);
} else {
_cairo_gstate_transform_glyphs_to_backend (gstate,
glyphs, num_glyphs,
NULL, 0, 0,
transformed_glyphs,
&num_glyphs,
NULL);
}
if (num_glyphs == 0)
goto CLEANUP_GLYPHS;
op = _reduce_op (gstate);
if (op == CAIRO_OPERATOR_CLEAR) {
pattern = &_cairo_pattern_clear.base;
} else {
_cairo_gstate_copy_transformed_source (gstate, &source_pattern.base);
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. */
if (cairo_surface_has_show_text_glyphs (gstate->target) ||
_cairo_scaled_font_get_max_scale (gstate->scaled_font) <= 10240)
{
if (info != NULL) {
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,
gstate->clip);
} else {
status = _cairo_surface_show_text_glyphs (gstate->target, op, pattern,
NULL, 0,
transformed_glyphs, num_glyphs,
NULL, 0, 0,
gstate->scaled_font,
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);
}
CLEANUP_GLYPHS:
if (transformed_glyphs != stack_transformed_glyphs)
cairo_glyph_free (transformed_glyphs);
if (transformed_clusters != stack_transformed_clusters)
cairo_text_cluster_free (transformed_clusters);
return status;
}
cairo_status_t
_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;
status = _cairo_gstate_ensure_scaled_font (gstate);
if (unlikely (status))
return status;
if (num_glyphs < ARRAY_LENGTH (stack_transformed_glyphs)) {
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);
}
_cairo_gstate_transform_glyphs_to_backend (gstate,
glyphs, num_glyphs,
NULL, 0, 0,
transformed_glyphs,
&num_glyphs, NULL);
status = _cairo_scaled_font_glyph_path (gstate->scaled_font,
transformed_glyphs, num_glyphs,
path);
if (transformed_glyphs != stack_transformed_glyphs)
cairo_glyph_free (transformed_glyphs);
return status;
}
cairo_status_t
_cairo_gstate_set_antialias (cairo_gstate_t *gstate,
cairo_antialias_t antialias)
{
gstate->antialias = antialias;
return CAIRO_STATUS_SUCCESS;
}
cairo_antialias_t
_cairo_gstate_get_antialias (cairo_gstate_t *gstate)
{
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
_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_rectangle_int_t surface_extents;
cairo_matrix_t *ctm = &gstate->ctm;
cairo_matrix_t *font_matrix = &gstate->font_matrix;
cairo_matrix_t *device_transform = &gstate->target->device_transform;
cairo_bool_t drop = FALSE;
double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
int i, j, k;
drop = TRUE;
if (! _cairo_gstate_int_clip_extents (gstate, &surface_extents)) {
drop = FALSE; /* unbounded surface */
} else {
double scale10 = 10 * _cairo_scaled_font_get_max_scale (gstate->scaled_font);
if (surface_extents.width == 0 || surface_extents.height == 0) {
/* No visible area. Don't draw anything */
*num_transformed_glyphs = 0;
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.
*/
x1 = surface_extents.x - scale10;
y1 = surface_extents.y - scale10;
x2 = surface_extents.x + (int) surface_extents.width + scale10;
y2 = surface_extents.y + (int) surface_extents.height + scale10;
}
if (!drop)
*num_transformed_glyphs = num_glyphs;
#define KEEP_GLYPH(glyph) (x1 <= glyph.x && glyph.x <= x2 && y1 <= glyph.y && glyph.y <= y2)
j = 0;
if (_cairo_matrix_is_identity (ctm) &&
_cairo_matrix_is_identity (device_transform) &&
font_matrix->x0 == 0 && font_matrix->y0 == 0)
{
if (! drop) {
memcpy (transformed_glyphs, glyphs,
num_glyphs * sizeof (cairo_glyph_t));
memcpy (transformed_clusters, clusters,
num_clusters * sizeof (cairo_text_cluster_t));
j = num_glyphs;
} else if (num_clusters == 0) {
for (i = 0; i < num_glyphs; i++) {
transformed_glyphs[j].index = glyphs[i].index;
transformed_glyphs[j].x = glyphs[i].x;
transformed_glyphs[j].y = glyphs[i].y;
if (KEEP_GLYPH (transformed_glyphs[j]))
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;
}
}
}
else if (_cairo_matrix_is_translation (ctm) &&
_cairo_matrix_is_translation (device_transform))
{
double tx = font_matrix->x0 + ctm->x0 + device_transform->x0;
double ty = font_matrix->y0 + ctm->y0 + device_transform->y0;
if (! drop || num_clusters == 0) {
for (i = 0; i < num_glyphs; i++) {
transformed_glyphs[j].index = glyphs[i].index;
transformed_glyphs[j].x = glyphs[i].x + tx;
transformed_glyphs[j].y = glyphs[i].y + ty;
if (!drop || KEEP_GLYPH (transformed_glyphs[j]))
j++;
}
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;
cairo_matrix_init_translate (&aggregate_transform,
gstate->font_matrix.x0,
gstate->font_matrix.y0);
cairo_matrix_multiply (&aggregate_transform,
&aggregate_transform, ctm);
cairo_matrix_multiply (&aggregate_transform,
&aggregate_transform, device_transform);
if (! drop || num_clusters == 0) {
for (i = 0; i < num_glyphs; i++) {
transformed_glyphs[j] = glyphs[i];
cairo_matrix_transform_point (&aggregate_transform,
&transformed_glyphs[j].x,
&transformed_glyphs[j].y);
if (! drop || KEEP_GLYPH (transformed_glyphs[j]))
j++;
}
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;
}
}
}
*num_transformed_glyphs = j;
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;
}
}
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-hash-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2004 Red Hat, Inc.
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Keith Packard <keithp@keithp.com>
* Graydon Hoare <graydon@redhat.com>
* Carl Worth <cworth@cworth.org>
*/
#ifndef CAIRO_HASH_PRIVATE_H
#define CAIRO_HASH_PRIVATE_H
#include "cairo-compiler-private.h"
#include "cairo-types-private.h"
/* XXX: I'd like this file to be self-contained in terms of
* includeability, but that's not really possible with the current
* monolithic cairoint.h. So, for now, just include cairoint.h instead
* if you want to include this file. */
typedef cairo_bool_t
(*cairo_hash_keys_equal_func_t) (const void *key_a, const void *key_b);
typedef cairo_bool_t
(*cairo_hash_predicate_func_t) (const void *entry);
typedef void
(*cairo_hash_callback_func_t) (void *entry,
void *closure);
cairo_private cairo_hash_table_t *
_cairo_hash_table_create (cairo_hash_keys_equal_func_t keys_equal);
cairo_private void
_cairo_hash_table_destroy (cairo_hash_table_t *hash_table);
cairo_private void *
_cairo_hash_table_lookup (cairo_hash_table_t *hash_table,
cairo_hash_entry_t *key);
cairo_private void *
_cairo_hash_table_random_entry (cairo_hash_table_t *hash_table,
cairo_hash_predicate_func_t predicate);
cairo_private cairo_status_t
_cairo_hash_table_insert (cairo_hash_table_t *hash_table,
cairo_hash_entry_t *entry);
cairo_private void
_cairo_hash_table_remove (cairo_hash_table_t *hash_table,
cairo_hash_entry_t *key);
cairo_private void
_cairo_hash_table_foreach (cairo_hash_table_t *hash_table,
cairo_hash_callback_func_t hash_callback,
void *closure);
#endif
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-hash.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2004 Red Hat, Inc.
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Keith Packard <keithp@keithp.com>
* Graydon Hoare <graydon@redhat.com>
* Carl Worth <cworth@cworth.org>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
/*
* An entry can be in one of three states:
*
* FREE: Entry has never been used, terminates all searches.
* Appears in the table as a %NULL pointer.
*
* DEAD: Entry had been live in the past. A dead entry can be reused
* but does not terminate a search for an exact entry.
* Appears in the table as a pointer to DEAD_ENTRY.
*
* LIVE: Entry is currently being used.
* Appears in the table as any non-%NULL, non-DEAD_ENTRY pointer.
*/
#define DEAD_ENTRY ((cairo_hash_entry_t *) 0x1)
#define ENTRY_IS_FREE(entry) ((entry) == NULL)
#define ENTRY_IS_DEAD(entry) ((entry) == DEAD_ENTRY)
#define ENTRY_IS_LIVE(entry) ((entry) > DEAD_ENTRY)
/*
* This table is open-addressed with double hashing. Each table size
* is a prime and it makes for the "first" hash modulus; a second
* prime (2 less than the first prime) serves as the "second" hash
* modulus, which is smaller and thus guarantees a complete
* permutation of table indices.
*
* Hash tables are rehashed in order to keep between 12.5% and 50%
* entries in the hash table alive and at least 25% free. When table
* size is changed, the new table has about 25% live elements.
*
* The free entries guarantee an expected constant-time lookup.
* Doubling/halving the table in the described fashion guarantees
* amortized O(1) insertion/removal.
*
* This structure, and accompanying table, is borrowed/modified from the
* file xserver/render/glyph.c in the freedesktop.org x server, with
* permission (and suggested modification of doubling sizes) by Keith
* Packard.
*/
static const unsigned long hash_table_sizes[] = {
43,
73,
151,
283,
571,
1153,
2269,
4519,
9013,
18043,
36109,
72091,
144409,
288361,
576883,
1153459,
2307163,
4613893,
9227641,
18455029,
36911011,
73819861,
147639589,
295279081,
590559793
};
struct _cairo_hash_table {
cairo_hash_keys_equal_func_t keys_equal;
cairo_hash_entry_t *cache[32];
const unsigned long *table_size;
cairo_hash_entry_t **entries;
unsigned long live_entries;
unsigned long free_entries;
unsigned long iterating; /* Iterating, no insert, no resize */
};
/**
* _cairo_hash_table_uid_keys_equal:
* @key_a: the first key to be compared
* @key_b: the second key to be compared
*
* Provides a #cairo_hash_keys_equal_func_t which always returns
* %TRUE. This is useful to create hash tables using keys whose hash
* completely describes the key, because in this special case
* comparing the hashes is sufficient to guarantee that the keys are
* equal.
*
* Return value: %TRUE.
**/
static cairo_bool_t
_cairo_hash_table_uid_keys_equal (const void *key_a, const void *key_b)
{
return TRUE;
}
/**
* _cairo_hash_table_create:
* @keys_equal: a function to return %TRUE if two keys are equal
*
* Creates a new hash table which will use the keys_equal() function
* to compare hash keys. Data is provided to the hash table in the
* form of user-derived versions of #cairo_hash_entry_t. A hash entry
* must be able to hold both a key (including a hash code) and a
* value. Sometimes only the key will be necessary, (as in
* _cairo_hash_table_remove), and other times both a key and a value
* will be necessary, (as in _cairo_hash_table_insert).
*
* If @keys_equal is %NULL, two keys will be considered equal if and
* only if their hashes are equal.
*
* See #cairo_hash_entry_t for more details.
*
* Return value: the new hash table or %NULL if out of memory.
**/
cairo_hash_table_t *
_cairo_hash_table_create (cairo_hash_keys_equal_func_t keys_equal)
{
cairo_hash_table_t *hash_table;
hash_table = _cairo_malloc (sizeof (cairo_hash_table_t));
if (unlikely (hash_table == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return NULL;
}
if (keys_equal == NULL)
hash_table->keys_equal = _cairo_hash_table_uid_keys_equal;
else
hash_table->keys_equal = keys_equal;
memset (&hash_table->cache, 0, sizeof (hash_table->cache));
hash_table->table_size = &hash_table_sizes[0];
hash_table->entries = calloc (*hash_table->table_size,
sizeof (cairo_hash_entry_t *));
if (unlikely (hash_table->entries == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
free (hash_table);
return NULL;
}
hash_table->live_entries = 0;
hash_table->free_entries = *hash_table->table_size;
hash_table->iterating = 0;
return hash_table;
}
/**
* _cairo_hash_table_destroy:
* @hash_table: an empty hash table to destroy
*
* Immediately destroys the given hash table, freeing all resources
* associated with it.
*
* WARNING: The hash_table must have no live entries in it before
* _cairo_hash_table_destroy is called. It is a fatal error otherwise,
* and this function will halt. The rationale for this behavior is to
* avoid memory leaks and to avoid needless complication of the API
* with destroy notify callbacks.
*
* WARNING: The hash_table must have no running iterators in it when
* _cairo_hash_table_destroy is called. It is a fatal error otherwise,
* and this function will halt.
**/
void
_cairo_hash_table_destroy (cairo_hash_table_t *hash_table)
{
/* The hash table must be empty. Otherwise, halt. */
assert (hash_table->live_entries == 0);
/* No iterators can be running. Otherwise, halt. */
assert (hash_table->iterating == 0);
free (hash_table->entries);
free (hash_table);
}
static cairo_hash_entry_t **
_cairo_hash_table_lookup_unique_key (cairo_hash_table_t *hash_table,
cairo_hash_entry_t *key)
{
unsigned long table_size, i, idx, step;
cairo_hash_entry_t **entry;
table_size = *hash_table->table_size;
idx = key->hash % table_size;
entry = &hash_table->entries[idx];
if (! ENTRY_IS_LIVE (*entry))
return entry;
i = 1;
step = 1 + key->hash % (table_size - 2);
do {
idx += step;
if (idx >= table_size)
idx -= table_size;
entry = &hash_table->entries[idx];
if (! ENTRY_IS_LIVE (*entry))
return entry;
} while (++i < table_size);
ASSERT_NOT_REACHED;
return NULL;
}
/**
* _cairo_hash_table_manage:
* @hash_table: a hash table
*
* Resize the hash table if the number of entries has gotten much
* bigger or smaller than the ideal number of entries for the current
* size and guarantee some free entries to be used as lookup
* termination points.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful or
* %CAIRO_STATUS_NO_MEMORY if out of memory.
**/
static cairo_status_t
_cairo_hash_table_manage (cairo_hash_table_t *hash_table)
{
cairo_hash_table_t tmp;
unsigned long new_size, i;
/* Keep between 12.5% and 50% entries in the hash table alive and
* at least 25% free. */
unsigned long live_high = *hash_table->table_size >> 1;
unsigned long live_low = live_high >> 2;
unsigned long free_low = live_high >> 1;
tmp = *hash_table;
if (hash_table->live_entries > live_high)
{
tmp.table_size = hash_table->table_size + 1;
/* This code is being abused if we can't make a table big enough. */
assert (tmp.table_size - hash_table_sizes <
ARRAY_LENGTH (hash_table_sizes));
}
else if (hash_table->live_entries < live_low)
{
/* Can't shrink if we're at the smallest size */
if (hash_table->table_size == &hash_table_sizes[0])
tmp.table_size = hash_table->table_size;
else
tmp.table_size = hash_table->table_size - 1;
}
if (tmp.table_size == hash_table->table_size &&
hash_table->free_entries > free_low)
{
/* The number of live entries is within the desired bounds
* (we're not going to resize the table) and we have enough
* free entries. Do nothing. */
return CAIRO_STATUS_SUCCESS;
}
new_size = *tmp.table_size;
tmp.entries = calloc (new_size, sizeof (cairo_hash_entry_t*));
if (unlikely (tmp.entries == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
for (i = 0; i < *hash_table->table_size; ++i) {
if (ENTRY_IS_LIVE (hash_table->entries[i])) {
*_cairo_hash_table_lookup_unique_key (&tmp, hash_table->entries[i])
= hash_table->entries[i];
}
}
free (hash_table->entries);
hash_table->entries = tmp.entries;
hash_table->table_size = tmp.table_size;
hash_table->free_entries = new_size - hash_table->live_entries;
return CAIRO_STATUS_SUCCESS;
}
/**
* _cairo_hash_table_lookup:
* @hash_table: a hash table
* @key: the key of interest
*
* Performs a lookup in @hash_table looking for an entry which has a
* key that matches @key, (as determined by the keys_equal() function
* passed to _cairo_hash_table_create).
*
* Return value: the matching entry, of %NULL if no match was found.
**/
void *
_cairo_hash_table_lookup (cairo_hash_table_t *hash_table,
cairo_hash_entry_t *key)
{
cairo_hash_entry_t *entry;
unsigned long table_size, i, idx, step;
unsigned long hash = key->hash;
entry = hash_table->cache[hash & 31];
if (entry && entry->hash == hash && hash_table->keys_equal (key, entry))
return entry;
table_size = *hash_table->table_size;
idx = hash % table_size;
entry = hash_table->entries[idx];
if (ENTRY_IS_LIVE (entry)) {
if (entry->hash == hash && hash_table->keys_equal (key, entry))
goto insert_cache;
} else if (ENTRY_IS_FREE (entry))
return NULL;
i = 1;
step = 1 + hash % (table_size - 2);
do {
idx += step;
if (idx >= table_size)
idx -= table_size;
entry = hash_table->entries[idx];
if (ENTRY_IS_LIVE (entry)) {
if (entry->hash == hash && hash_table->keys_equal (key, entry))
goto insert_cache;
} else if (ENTRY_IS_FREE (entry))
return NULL;
} while (++i < table_size);
ASSERT_NOT_REACHED;
return NULL;
insert_cache:
hash_table->cache[hash & 31] = entry;
return entry;
}
/**
* _cairo_hash_table_random_entry:
* @hash_table: a hash table
* @predicate: a predicate function.
*
* Find a random entry in the hash table satisfying the given
* @predicate.
*
* We use the same algorithm as the lookup algorithm to walk over the
* entries in the hash table in a pseudo-random order. Walking
* linearly would favor entries following gaps in the hash table. We
* could also call rand() repeatedly, which works well for almost-full
* tables, but degrades when the table is almost empty, or predicate
* returns %TRUE for most entries.
*
* Return value: a random live entry or %NULL if there are no entries
* that match the given predicate. In particular, if predicate is
* %NULL, a %NULL return value indicates that the table is empty.
**/
void *
_cairo_hash_table_random_entry (cairo_hash_table_t *hash_table,
cairo_hash_predicate_func_t predicate)
{
cairo_hash_entry_t *entry;
unsigned long hash;
unsigned long table_size, i, idx, step;
assert (predicate != NULL);
table_size = *hash_table->table_size;
hash = rand ();
idx = hash % table_size;
entry = hash_table->entries[idx];
if (ENTRY_IS_LIVE (entry) && predicate (entry))
return entry;
i = 1;
step = 1 + hash % (table_size - 2);
do {
idx += step;
if (idx >= table_size)
idx -= table_size;
entry = hash_table->entries[idx];
if (ENTRY_IS_LIVE (entry) && predicate (entry))
return entry;
} while (++i < table_size);
return NULL;
}
/**
* _cairo_hash_table_insert:
* @hash_table: a hash table
* @key_and_value: an entry to be inserted
*
* Insert the entry #key_and_value into the hash table.
*
* WARNING: There must not be an existing entry in the hash table
* with a matching key.
*
* WARNING: It is a fatal error to insert an element while
* an iterator is running
*
* Instead of using insert to replace an entry, consider just editing
* the entry obtained with _cairo_hash_table_lookup. Or if absolutely
* necessary, use _cairo_hash_table_remove first.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful or
* %CAIRO_STATUS_NO_MEMORY if insufficient memory is available.
**/
cairo_status_t
_cairo_hash_table_insert (cairo_hash_table_t *hash_table,
cairo_hash_entry_t *key_and_value)
{
cairo_hash_entry_t **entry;
cairo_status_t status;
/* Insert is illegal while an iterator is running. */
assert (hash_table->iterating == 0);
status = _cairo_hash_table_manage (hash_table);
if (unlikely (status))
return status;
entry = _cairo_hash_table_lookup_unique_key (hash_table, key_and_value);
if (ENTRY_IS_FREE (*entry))
hash_table->free_entries--;
*entry = key_and_value;
hash_table->cache[key_and_value->hash & 31] = key_and_value;
hash_table->live_entries++;
return CAIRO_STATUS_SUCCESS;
}
static cairo_hash_entry_t **
_cairo_hash_table_lookup_exact_key (cairo_hash_table_t *hash_table,
cairo_hash_entry_t *key)
{
unsigned long table_size, i, idx, step;
cairo_hash_entry_t **entry;
table_size = *hash_table->table_size;
idx = key->hash % table_size;
entry = &hash_table->entries[idx];
if (*entry == key)
return entry;
i = 1;
step = 1 + key->hash % (table_size - 2);
do {
idx += step;
if (idx >= table_size)
idx -= table_size;
entry = &hash_table->entries[idx];
if (*entry == key)
return entry;
} while (++i < table_size);
ASSERT_NOT_REACHED;
return NULL;
}
/**
* _cairo_hash_table_remove:
* @hash_table: a hash table
* @key: key of entry to be removed
*
* Remove an entry from the hash table which points to @key.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful or
* %CAIRO_STATUS_NO_MEMORY if out of memory.
**/
void
_cairo_hash_table_remove (cairo_hash_table_t *hash_table,
cairo_hash_entry_t *key)
{
*_cairo_hash_table_lookup_exact_key (hash_table, key) = DEAD_ENTRY;
hash_table->live_entries--;
hash_table->cache[key->hash & 31] = NULL;
/* Check for table resize. Don't do this when iterating as this will
* reorder elements of the table and cause the iteration to potentially
* skip some elements. */
if (hash_table->iterating == 0) {
/* This call _can_ fail, but only in failing to allocate new
* memory to shrink the hash table. It does leave the table in a
* consistent state, and we've already succeeded in removing the
* entry, so we don't examine the failure status of this call. */
_cairo_hash_table_manage (hash_table);
}
}
/**
* _cairo_hash_table_foreach:
* @hash_table: a hash table
* @hash_callback: function to be called for each live entry
* @closure: additional argument to be passed to @hash_callback
*
* Call @hash_callback for each live entry in the hash table, in a
* non-specified order.
*
* Entries in @hash_table may be removed by code executed from @hash_callback.
*
* Entries may not be inserted to @hash_table, nor may @hash_table
* be destroyed by code executed from @hash_callback. The relevant
* functions will halt in these cases.
**/
void
_cairo_hash_table_foreach (cairo_hash_table_t *hash_table,
cairo_hash_callback_func_t hash_callback,
void *closure)
{
unsigned long i;
cairo_hash_entry_t *entry;
/* Mark the table for iteration */
++hash_table->iterating;
for (i = 0; i < *hash_table->table_size; i++) {
entry = hash_table->entries[i];
if (ENTRY_IS_LIVE(entry))
hash_callback (entry, closure);
}
/* If some elements were deleted during the iteration,
* the table may need resizing. Just do this every time
* as the check is inexpensive.
*/
if (--hash_table->iterating == 0) {
/* Should we fail to shrink the hash table, it is left unaltered,
* and we don't need to propagate the error status. */
_cairo_hash_table_manage (hash_table);
}
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-hull.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2003 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-slope-private.h"
typedef struct cairo_hull {
cairo_point_t point;
cairo_slope_t slope;
int discard;
int id;
} cairo_hull_t;
static void
_cairo_hull_init (cairo_hull_t *hull,
cairo_pen_vertex_t *vertices,
int num_vertices)
{
cairo_point_t *p, *extremum, tmp;
int i;
extremum = &vertices[0].point;
for (i = 1; i < num_vertices; i++) {
p = &vertices[i].point;
if (p->y < extremum->y || (p->y == extremum->y && p->x < extremum->x))
extremum = p;
}
/* Put the extremal point at the beginning of the array */
tmp = *extremum;
*extremum = vertices[0].point;
vertices[0].point = tmp;
for (i = 0; i < num_vertices; i++) {
hull[i].point = vertices[i].point;
_cairo_slope_init (&hull[i].slope, &hull[0].point, &hull[i].point);
/* give each point a unique id for later comparison */
hull[i].id = i;
/* Don't discard by default */
hull[i].discard = 0;
/* Discard all points coincident with the extremal point */
if (i != 0 && hull[i].slope.dx == 0 && hull[i].slope.dy == 0)
hull[i].discard = 1;
}
}
static inline cairo_int64_t
_slope_length (cairo_slope_t *slope)
{
return _cairo_int64_add (_cairo_int32x32_64_mul (slope->dx, slope->dx),
_cairo_int32x32_64_mul (slope->dy, slope->dy));
}
static int
_cairo_hull_vertex_compare (const void *av, const void *bv)
{
cairo_hull_t *a = (cairo_hull_t *) av;
cairo_hull_t *b = (cairo_hull_t *) bv;
int ret;
/* Some libraries are reported to actually compare identical
* pointers and require the result to be 0. This is the crazy world we
* have to live in.
*/
if (a == b)
return 0;
ret = _cairo_slope_compare (&a->slope, &b->slope);
/*
* In the case of two vertices with identical slope from the
* extremal point discard the nearer point.
*/
if (ret == 0) {
int cmp;
cmp = _cairo_int64_cmp (_slope_length (&a->slope),
_slope_length (&b->slope));
/*
* Use the points' ids to ensure a well-defined ordering,
* and avoid setting discard on both points.
*/
if (cmp < 0 || (cmp == 0 && a->id < b->id)) {
a->discard = 1;
ret = -1;
} else {
b->discard = 1;
ret = 1;
}
}
return ret;
}
static int
_cairo_hull_prev_valid (cairo_hull_t *hull, int num_hull, int index)
{
/* hull[0] is always valid, and we never need to wraparound, (if
* we are passed an index of 0 here, then the calling loop is just
* about to terminate). */
if (index == 0)
return 0;
do {
index--;
} while (hull[index].discard);
return index;
}
static int
_cairo_hull_next_valid (cairo_hull_t *hull, int num_hull, int index)
{
do {
index = (index + 1) % num_hull;
} while (hull[index].discard);
return index;
}
static void
_cairo_hull_eliminate_concave (cairo_hull_t *hull, int num_hull)
{
int i, j, k;
cairo_slope_t slope_ij, slope_jk;
i = 0;
j = _cairo_hull_next_valid (hull, num_hull, i);
k = _cairo_hull_next_valid (hull, num_hull, j);
do {
_cairo_slope_init (&slope_ij, &hull[i].point, &hull[j].point);
_cairo_slope_init (&slope_jk, &hull[j].point, &hull[k].point);
/* Is the angle formed by ij and jk concave? */
if (_cairo_slope_compare (&slope_ij, &slope_jk) >= 0) {
if (i == k)
return;
hull[j].discard = 1;
j = i;
i = _cairo_hull_prev_valid (hull, num_hull, j);
} else {
i = j;
j = k;
k = _cairo_hull_next_valid (hull, num_hull, j);
}
} while (j != 0);
}
static void
_cairo_hull_to_pen (cairo_hull_t *hull, cairo_pen_vertex_t *vertices, int *num_vertices)
{
int i, j = 0;
for (i = 0; i < *num_vertices; i++) {
if (hull[i].discard)
continue;
vertices[j++].point = hull[i].point;
}
*num_vertices = j;
}
/* Given a set of vertices, compute the convex hull using the Graham
scan algorithm. */
cairo_status_t
_cairo_hull_compute (cairo_pen_vertex_t *vertices, int *num_vertices)
{
cairo_hull_t hull_stack[CAIRO_STACK_ARRAY_LENGTH (cairo_hull_t)];
cairo_hull_t *hull;
int num_hull = *num_vertices;
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (num_hull > ARRAY_LENGTH (hull_stack)) {
hull = _cairo_malloc_ab (num_hull, sizeof (cairo_hull_t));
if (unlikely (hull == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
} else {
hull = hull_stack;
}
_cairo_hull_init (hull, vertices, num_hull);
qsort (hull + 1, num_hull - 1,
sizeof (cairo_hull_t), _cairo_hull_vertex_compare);
_cairo_hull_eliminate_concave (hull, num_hull);
_cairo_hull_to_pen (hull, vertices, num_vertices);
if (hull != hull_stack)
free (hull);
return CAIRO_STATUS_SUCCESS;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-image-compositor.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2003 University of Southern California
* Copyright © 2009,2010,2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
/* The primarily reason for keeping a traps-compositor around is
* for validating cairo-xlib (which currently also uses traps).
*/
#include "cairoint.h"
#include "cairo-image-surface-private.h"
#include "cairo-compositor-private.h"
#include "cairo-spans-compositor-private.h"
#include "cairo-region-private.h"
#include "cairo-traps-private.h"
#include "cairo-tristrip-private.h"
#include "cairo-pixman-private.h"
static pixman_image_t *
to_pixman_image (cairo_surface_t *s)
{
return ((cairo_image_surface_t *)s)->pixman_image;
}
static cairo_int_status_t
acquire (void *abstract_dst)
{
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
release (void *abstract_dst)
{
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
set_clip_region (void *_surface,
cairo_region_t *region)
{
cairo_image_surface_t *surface = _surface;
pixman_region32_t *rgn = region ? ®ion->rgn : NULL;
if (! pixman_image_set_clip_region32 (surface->pixman_image, rgn))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
draw_image_boxes (void *_dst,
cairo_image_surface_t *image,
cairo_boxes_t *boxes,
int dx, int dy)
{
cairo_image_surface_t *dst = _dst;
struct _cairo_boxes_chunk *chunk;
int i;
TRACE ((stderr, "%s x %d\n", __FUNCTION__, boxes->num_boxes));
for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
cairo_box_t *b = &chunk->base[i];
int x = _cairo_fixed_integer_part (b->p1.x);
int y = _cairo_fixed_integer_part (b->p1.y);
int w = _cairo_fixed_integer_part (b->p2.x) - x;
int h = _cairo_fixed_integer_part (b->p2.y) - y;
if (dst->pixman_format != image->pixman_format ||
! pixman_blt ((uint32_t *)image->data, (uint32_t *)dst->data,
image->stride / sizeof (uint32_t),
dst->stride / sizeof (uint32_t),
PIXMAN_FORMAT_BPP (image->pixman_format),
PIXMAN_FORMAT_BPP (dst->pixman_format),
x + dx, y + dy,
x, y,
w, h))
{
pixman_image_composite32 (PIXMAN_OP_SRC,
image->pixman_image, NULL, dst->pixman_image,
x + dx, y + dy,
0, 0,
x, y,
w, h);
}
}
}
return CAIRO_STATUS_SUCCESS;
}
static inline uint32_t
color_to_uint32 (const cairo_color_t *color)
{
return
(color->alpha_short >> 8 << 24) |
(color->red_short >> 8 << 16) |
(color->green_short & 0xff00) |
(color->blue_short >> 8);
}
static inline cairo_bool_t
color_to_pixel (const cairo_color_t *color,
pixman_format_code_t format,
uint32_t *pixel)
{
uint32_t c;
if (!(format == PIXMAN_a8r8g8b8 ||
format == PIXMAN_x8r8g8b8 ||
format == PIXMAN_a8b8g8r8 ||
format == PIXMAN_x8b8g8r8 ||
format == PIXMAN_b8g8r8a8 ||
format == PIXMAN_b8g8r8x8 ||
format == PIXMAN_r5g6b5 ||
format == PIXMAN_b5g6r5 ||
format == PIXMAN_a8))
{
return FALSE;
}
c = color_to_uint32 (color);
if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR) {
c = ((c & 0xff000000) >> 0) |
((c & 0x00ff0000) >> 16) |
((c & 0x0000ff00) >> 0) |
((c & 0x000000ff) << 16);
}
if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA) {
c = ((c & 0xff000000) >> 24) |
((c & 0x00ff0000) >> 8) |
((c & 0x0000ff00) << 8) |
((c & 0x000000ff) << 24);
}
if (format == PIXMAN_a8) {
c = c >> 24;
} else if (format == PIXMAN_r5g6b5 || format == PIXMAN_b5g6r5) {
c = ((((c) >> 3) & 0x001f) |
(((c) >> 5) & 0x07e0) |
(((c) >> 8) & 0xf800));
}
*pixel = c;
return TRUE;
}
static pixman_op_t
_pixman_operator (cairo_operator_t op)
{
switch ((int) op) {
case CAIRO_OPERATOR_CLEAR:
return PIXMAN_OP_CLEAR;
case CAIRO_OPERATOR_SOURCE:
return PIXMAN_OP_SRC;
case CAIRO_OPERATOR_OVER:
return PIXMAN_OP_OVER;
case CAIRO_OPERATOR_IN:
return PIXMAN_OP_IN;
case CAIRO_OPERATOR_OUT:
return PIXMAN_OP_OUT;
case CAIRO_OPERATOR_ATOP:
return PIXMAN_OP_ATOP;
case CAIRO_OPERATOR_DEST:
return PIXMAN_OP_DST;
case CAIRO_OPERATOR_DEST_OVER:
return PIXMAN_OP_OVER_REVERSE;
case CAIRO_OPERATOR_DEST_IN:
return PIXMAN_OP_IN_REVERSE;
case CAIRO_OPERATOR_DEST_OUT:
return PIXMAN_OP_OUT_REVERSE;
case CAIRO_OPERATOR_DEST_ATOP:
return PIXMAN_OP_ATOP_REVERSE;
case CAIRO_OPERATOR_XOR:
return PIXMAN_OP_XOR;
case CAIRO_OPERATOR_ADD:
return PIXMAN_OP_ADD;
case CAIRO_OPERATOR_SATURATE:
return PIXMAN_OP_SATURATE;
case CAIRO_OPERATOR_MULTIPLY:
return PIXMAN_OP_MULTIPLY;
case CAIRO_OPERATOR_SCREEN:
return PIXMAN_OP_SCREEN;
case CAIRO_OPERATOR_OVERLAY:
return PIXMAN_OP_OVERLAY;
case CAIRO_OPERATOR_DARKEN:
return PIXMAN_OP_DARKEN;
case CAIRO_OPERATOR_LIGHTEN:
return PIXMAN_OP_LIGHTEN;
case CAIRO_OPERATOR_COLOR_DODGE:
return PIXMAN_OP_COLOR_DODGE;
case CAIRO_OPERATOR_COLOR_BURN:
return PIXMAN_OP_COLOR_BURN;
case CAIRO_OPERATOR_HARD_LIGHT:
return PIXMAN_OP_HARD_LIGHT;
case CAIRO_OPERATOR_SOFT_LIGHT:
return PIXMAN_OP_SOFT_LIGHT;
case CAIRO_OPERATOR_DIFFERENCE:
return PIXMAN_OP_DIFFERENCE;
case CAIRO_OPERATOR_EXCLUSION:
return PIXMAN_OP_EXCLUSION;
case CAIRO_OPERATOR_HSL_HUE:
return PIXMAN_OP_HSL_HUE;
case CAIRO_OPERATOR_HSL_SATURATION:
return PIXMAN_OP_HSL_SATURATION;
case CAIRO_OPERATOR_HSL_COLOR:
return PIXMAN_OP_HSL_COLOR;
case CAIRO_OPERATOR_HSL_LUMINOSITY:
return PIXMAN_OP_HSL_LUMINOSITY;
default:
ASSERT_NOT_REACHED;
return PIXMAN_OP_OVER;
}
}
static cairo_bool_t
__fill_reduces_to_source (cairo_operator_t op,
const cairo_color_t *color,
const cairo_image_surface_t *dst)
{
if (op == CAIRO_OPERATOR_SOURCE || op == CAIRO_OPERATOR_CLEAR)
return TRUE;
if (op == CAIRO_OPERATOR_OVER && CAIRO_COLOR_IS_OPAQUE (color))
return TRUE;
if (dst->base.is_clear)
return op == CAIRO_OPERATOR_OVER || op == CAIRO_OPERATOR_ADD;
return FALSE;
}
static cairo_bool_t
fill_reduces_to_source (cairo_operator_t op,
const cairo_color_t *color,
const cairo_image_surface_t *dst,
uint32_t *pixel)
{
if (__fill_reduces_to_source (op, color, dst)) {
return color_to_pixel (color, dst->pixman_format, pixel);
}
return FALSE;
}
static cairo_int_status_t
fill_rectangles (void *_dst,
cairo_operator_t op,
const cairo_color_t *color,
cairo_rectangle_int_t *rects,
int num_rects)
{
cairo_image_surface_t *dst = _dst;
uint32_t pixel;
int i;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (fill_reduces_to_source (op, color, dst, &pixel)) {
for (i = 0; i < num_rects; i++) {
pixman_fill ((uint32_t *) dst->data, dst->stride / sizeof (uint32_t),
PIXMAN_FORMAT_BPP (dst->pixman_format),
rects[i].x, rects[i].y,
rects[i].width, rects[i].height,
pixel);
}
} else {
pixman_image_t *src = _pixman_image_for_color (color);
if (unlikely (src == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
op = _pixman_operator (op);
for (i = 0; i < num_rects; i++) {
pixman_image_composite32 (op,
src, NULL, dst->pixman_image,
0, 0,
0, 0,
rects[i].x, rects[i].y,
rects[i].width, rects[i].height);
}
pixman_image_unref (src);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
fill_boxes (void *_dst,
cairo_operator_t op,
const cairo_color_t *color,
cairo_boxes_t *boxes)
{
cairo_image_surface_t *dst = _dst;
struct _cairo_boxes_chunk *chunk;
uint32_t pixel;
int i;
TRACE ((stderr, "%s x %d\n", __FUNCTION__, boxes->num_boxes));
if (fill_reduces_to_source (op, color, dst, &pixel)) {
for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
int x = _cairo_fixed_integer_part (chunk->base[i].p1.x);
int y = _cairo_fixed_integer_part (chunk->base[i].p1.y);
int w = _cairo_fixed_integer_part (chunk->base[i].p2.x) - x;
int h = _cairo_fixed_integer_part (chunk->base[i].p2.y) - y;
pixman_fill ((uint32_t *) dst->data,
dst->stride / sizeof (uint32_t),
PIXMAN_FORMAT_BPP (dst->pixman_format),
x, y, w, h, pixel);
}
}
}
else
{
pixman_image_t *src = _pixman_image_for_color (color);
if (unlikely (src == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
op = _pixman_operator (op);
for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
int x1 = _cairo_fixed_integer_part (chunk->base[i].p1.x);
int y1 = _cairo_fixed_integer_part (chunk->base[i].p1.y);
int x2 = _cairo_fixed_integer_part (chunk->base[i].p2.x);
int y2 = _cairo_fixed_integer_part (chunk->base[i].p2.y);
pixman_image_composite32 (op,
src, NULL, dst->pixman_image,
0, 0,
0, 0,
x1, y1,
x2-x1, y2-y1);
}
}
pixman_image_unref (src);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
composite (void *_dst,
cairo_operator_t op,
cairo_surface_t *abstract_src,
cairo_surface_t *abstract_mask,
int src_x,
int src_y,
int mask_x,
int mask_y,
int dst_x,
int dst_y,
unsigned int width,
unsigned int height)
{
cairo_image_source_t *src = (cairo_image_source_t *)abstract_src;
cairo_image_source_t *mask = (cairo_image_source_t *)abstract_mask;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (mask) {
pixman_image_composite32 (_pixman_operator (op),
src->pixman_image, mask->pixman_image, to_pixman_image (_dst),
src_x, src_y,
mask_x, mask_y,
dst_x, dst_y,
width, height);
} else {
pixman_image_composite32 (_pixman_operator (op),
src->pixman_image, NULL, to_pixman_image (_dst),
src_x, src_y,
0, 0,
dst_x, dst_y,
width, height);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
lerp (void *_dst,
cairo_surface_t *abstract_src,
cairo_surface_t *abstract_mask,
int src_x,
int src_y,
int mask_x,
int mask_y,
int dst_x,
int dst_y,
unsigned int width,
unsigned int height)
{
cairo_image_surface_t *dst = _dst;
cairo_image_source_t *src = (cairo_image_source_t *)abstract_src;
cairo_image_source_t *mask = (cairo_image_source_t *)abstract_mask;
TRACE ((stderr, "%s\n", __FUNCTION__));
#if PIXMAN_HAS_OP_LERP
pixman_image_composite32 (PIXMAN_OP_LERP_SRC,
src->pixman_image, mask->pixman_image, dst->pixman_image,
src_x, src_y,
mask_x, mask_y,
dst_x, dst_y,
width, height);
#else
/* Punch the clip out of the destination */
TRACE ((stderr, "%s - OUT_REVERSE (mask=%d/%p, dst=%d/%p)\n",
__FUNCTION__,
mask->base.unique_id, mask->pixman_image,
dst->base.unique_id, dst->pixman_image));
pixman_image_composite32 (PIXMAN_OP_OUT_REVERSE,
mask->pixman_image, NULL, dst->pixman_image,
mask_x, mask_y,
0, 0,
dst_x, dst_y,
width, height);
/* Now add the two results together */
TRACE ((stderr, "%s - ADD (src=%d/%p, mask=%d/%p, dst=%d/%p)\n",
__FUNCTION__,
src->base.unique_id, src->pixman_image,
mask->base.unique_id, mask->pixman_image,
dst->base.unique_id, dst->pixman_image));
pixman_image_composite32 (PIXMAN_OP_ADD,
src->pixman_image, mask->pixman_image, dst->pixman_image,
src_x, src_y,
mask_x, mask_y,
dst_x, dst_y,
width, height);
#endif
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
composite_boxes (void *_dst,
cairo_operator_t op,
cairo_surface_t *abstract_src,
cairo_surface_t *abstract_mask,
int src_x,
int src_y,
int mask_x,
int mask_y,
int dst_x,
int dst_y,
cairo_boxes_t *boxes,
const cairo_rectangle_int_t *extents)
{
pixman_image_t *dst = to_pixman_image (_dst);
pixman_image_t *src = ((cairo_image_source_t *)abstract_src)->pixman_image;
pixman_image_t *mask = abstract_mask ? ((cairo_image_source_t *)abstract_mask)->pixman_image : NULL;
pixman_image_t *free_src = NULL;
struct _cairo_boxes_chunk *chunk;
int i;
/* XXX consider using a region? saves multiple prepare-composite */
TRACE ((stderr, "%s x %d\n", __FUNCTION__, boxes->num_boxes));
if (((cairo_surface_t *)_dst)->is_clear &&
(op == CAIRO_OPERATOR_SOURCE ||
op == CAIRO_OPERATOR_OVER ||
op == CAIRO_OPERATOR_ADD)) {
op = PIXMAN_OP_SRC;
} else if (mask) {
if (op == CAIRO_OPERATOR_CLEAR) {
#if PIXMAN_HAS_OP_LERP
op = PIXMAN_OP_LERP_CLEAR;
#else
free_src = src = _pixman_image_for_color (CAIRO_COLOR_WHITE);
if (unlikely (src == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
op = PIXMAN_OP_OUT_REVERSE;
#endif
} else if (op == CAIRO_OPERATOR_SOURCE) {
#if PIXMAN_HAS_OP_LERP
op = PIXMAN_OP_LERP_SRC;
#else
return CAIRO_INT_STATUS_UNSUPPORTED;
#endif
} else {
op = _pixman_operator (op);
}
} else {
op = _pixman_operator (op);
}
for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
int x1 = _cairo_fixed_integer_part (chunk->base[i].p1.x);
int y1 = _cairo_fixed_integer_part (chunk->base[i].p1.y);
int x2 = _cairo_fixed_integer_part (chunk->base[i].p2.x);
int y2 = _cairo_fixed_integer_part (chunk->base[i].p2.y);
pixman_image_composite32 (op, src, mask, dst,
x1 + src_x, y1 + src_y,
x1 + mask_x, y1 + mask_y,
x1 + dst_x, y1 + dst_y,
x2 - x1, y2 - y1);
}
}
if (free_src)
pixman_image_unref (free_src);
return CAIRO_STATUS_SUCCESS;
}
#define CAIRO_FIXED_16_16_MIN _cairo_fixed_from_int (-32768)
#define CAIRO_FIXED_16_16_MAX _cairo_fixed_from_int (32767)
static cairo_bool_t
line_exceeds_16_16 (const cairo_line_t *line)
{
return
line->p1.x <= CAIRO_FIXED_16_16_MIN ||
line->p1.x >= CAIRO_FIXED_16_16_MAX ||
line->p2.x <= CAIRO_FIXED_16_16_MIN ||
line->p2.x >= CAIRO_FIXED_16_16_MAX ||
line->p1.y <= CAIRO_FIXED_16_16_MIN ||
line->p1.y >= CAIRO_FIXED_16_16_MAX ||
line->p2.y <= CAIRO_FIXED_16_16_MIN ||
line->p2.y >= CAIRO_FIXED_16_16_MAX;
}
static void
project_line_x_onto_16_16 (const cairo_line_t *line,
cairo_fixed_t top,
cairo_fixed_t bottom,
pixman_line_fixed_t *out)
{
/* XXX use fixed-point arithmetic? */
cairo_point_double_t p1, p2;
double m;
p1.x = _cairo_fixed_to_double (line->p1.x);
p1.y = _cairo_fixed_to_double (line->p1.y);
p2.x = _cairo_fixed_to_double (line->p2.x);
p2.y = _cairo_fixed_to_double (line->p2.y);
m = (p2.x - p1.x) / (p2.y - p1.y);
out->p1.x = _cairo_fixed_16_16_from_double (p1.x + m * _cairo_fixed_to_double (top - line->p1.y));
out->p2.x = _cairo_fixed_16_16_from_double (p1.x + m * _cairo_fixed_to_double (bottom - line->p1.y));
}
void
_pixman_image_add_traps (pixman_image_t *image,
int dst_x, int dst_y,
cairo_traps_t *traps)
{
cairo_trapezoid_t *t = traps->traps;
int num_traps = traps->num_traps;
while (num_traps--) {
pixman_trapezoid_t trap;
/* top/bottom will be clamped to surface bounds */
trap.top = _cairo_fixed_to_16_16 (t->top);
trap.bottom = _cairo_fixed_to_16_16 (t->bottom);
/* However, all the other coordinates will have been left untouched so
* as not to introduce numerical error. Recompute them if they
* exceed the 16.16 limits.
*/
if (unlikely (line_exceeds_16_16 (&t->left))) {
project_line_x_onto_16_16 (&t->left, t->top, t->bottom, &trap.left);
trap.left.p1.y = trap.top;
trap.left.p2.y = trap.bottom;
} else {
trap.left.p1.x = _cairo_fixed_to_16_16 (t->left.p1.x);
trap.left.p1.y = _cairo_fixed_to_16_16 (t->left.p1.y);
trap.left.p2.x = _cairo_fixed_to_16_16 (t->left.p2.x);
trap.left.p2.y = _cairo_fixed_to_16_16 (t->left.p2.y);
}
if (unlikely (line_exceeds_16_16 (&t->right))) {
project_line_x_onto_16_16 (&t->right, t->top, t->bottom, &trap.right);
trap.right.p1.y = trap.top;
trap.right.p2.y = trap.bottom;
} else {
trap.right.p1.x = _cairo_fixed_to_16_16 (t->right.p1.x);
trap.right.p1.y = _cairo_fixed_to_16_16 (t->right.p1.y);
trap.right.p2.x = _cairo_fixed_to_16_16 (t->right.p2.x);
trap.right.p2.y = _cairo_fixed_to_16_16 (t->right.p2.y);
}
pixman_rasterize_trapezoid (image, &trap, -dst_x, -dst_y);
t++;
}
}
static cairo_int_status_t
composite_traps (void *_dst,
cairo_operator_t op,
cairo_surface_t *abstract_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
const cairo_rectangle_int_t *extents,
cairo_antialias_t antialias,
cairo_traps_t *traps)
{
cairo_image_surface_t *dst = (cairo_image_surface_t *) _dst;
cairo_image_source_t *src = (cairo_image_source_t *) abstract_src;
cairo_int_status_t status;
pixman_image_t *mask;
pixman_format_code_t format;
TRACE ((stderr, "%s\n", __FUNCTION__));
/* pixman doesn't eliminate self-intersecting trapezoids/edges */
status = _cairo_bentley_ottmann_tessellate_traps (traps,
CAIRO_FILL_RULE_WINDING);
if (status != CAIRO_INT_STATUS_SUCCESS)
return status;
/* Special case adding trapezoids onto a mask surface; we want to avoid
* creating an intermediate temporary mask unnecessarily.
*
* We make the assumption here that the portion of the trapezoids
* contained within the surface is bounded by [dst_x,dst_y,width,height];
* the Cairo core code passes bounds based on the trapezoid extents.
*/
format = antialias == CAIRO_ANTIALIAS_NONE ? PIXMAN_a1 : PIXMAN_a8;
if (dst->pixman_format == format &&
(abstract_src == NULL ||
(op == CAIRO_OPERATOR_ADD && src->is_opaque_solid)))
{
_pixman_image_add_traps (dst->pixman_image, dst_x, dst_y, traps);
return CAIRO_STATUS_SUCCESS;
}
mask = pixman_image_create_bits (format,
extents->width, extents->height,
NULL, 0);
if (unlikely (mask == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
_pixman_image_add_traps (mask, extents->x, extents->y, traps);
pixman_image_composite32 (_pixman_operator (op),
src->pixman_image, mask, dst->pixman_image,
extents->x + src_x, extents->y + src_y,
0, 0,
extents->x - dst_x, extents->y - dst_y,
extents->width, extents->height);
pixman_image_unref (mask);
return CAIRO_STATUS_SUCCESS;
}
#if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0,22,0)
static void
set_point (pixman_point_fixed_t *p, cairo_point_t *c)
{
p->x = _cairo_fixed_to_16_16 (c->x);
p->y = _cairo_fixed_to_16_16 (c->y);
}
void
_pixman_image_add_tristrip (pixman_image_t *image,
int dst_x, int dst_y,
cairo_tristrip_t *strip)
{
pixman_triangle_t tri;
pixman_point_fixed_t *p[3] = {&tri.p1, &tri.p2, &tri.p3 };
int n;
set_point (p[0], &strip->points[0]);
set_point (p[1], &strip->points[1]);
set_point (p[2], &strip->points[2]);
pixman_add_triangles (image, -dst_x, -dst_y, 1, &tri);
for (n = 3; n < strip->num_points; n++) {
set_point (p[n%3], &strip->points[n]);
pixman_add_triangles (image, -dst_x, -dst_y, 1, &tri);
}
}
static cairo_int_status_t
composite_tristrip (void *_dst,
cairo_operator_t op,
cairo_surface_t *abstract_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
const cairo_rectangle_int_t *extents,
cairo_antialias_t antialias,
cairo_tristrip_t *strip)
{
cairo_image_surface_t *dst = (cairo_image_surface_t *) _dst;
cairo_image_source_t *src = (cairo_image_source_t *) abstract_src;
pixman_image_t *mask;
pixman_format_code_t format;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (strip->num_points < 3)
return CAIRO_STATUS_SUCCESS;
if (1) { /* pixman doesn't eliminate self-intersecting triangles/edges */
cairo_int_status_t status;
cairo_traps_t traps;
int n;
_cairo_traps_init (&traps);
for (n = 0; n < strip->num_points; n++) {
cairo_point_t p[4];
p[0] = strip->points[0];
p[1] = strip->points[1];
p[2] = strip->points[2];
p[3] = strip->points[0];
_cairo_traps_tessellate_convex_quad (&traps, p);
}
status = composite_traps (_dst, op, abstract_src,
src_x, src_y,
dst_x, dst_y,
extents, antialias, &traps);
_cairo_traps_fini (&traps);
return status;
}
format = antialias == CAIRO_ANTIALIAS_NONE ? PIXMAN_a1 : PIXMAN_a8;
if (dst->pixman_format == format &&
(abstract_src == NULL ||
(op == CAIRO_OPERATOR_ADD && src->is_opaque_solid)))
{
_pixman_image_add_tristrip (dst->pixman_image, dst_x, dst_y, strip);
return CAIRO_STATUS_SUCCESS;
}
mask = pixman_image_create_bits (format,
extents->width, extents->height,
NULL, 0);
if (unlikely (mask == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
_pixman_image_add_tristrip (mask, extents->x, extents->y, strip);
pixman_image_composite32 (_pixman_operator (op),
src->pixman_image, mask, dst->pixman_image,
extents->x + src_x, extents->y + src_y,
0, 0,
extents->x - dst_x, extents->y - dst_y,
extents->width, extents->height);
pixman_image_unref (mask);
return CAIRO_STATUS_SUCCESS;
}
#endif
static cairo_int_status_t
check_composite_glyphs (const cairo_composite_rectangles_t *extents,
cairo_scaled_font_t *scaled_font,
cairo_glyph_t *glyphs,
int *num_glyphs)
{
return CAIRO_STATUS_SUCCESS;
}
#if HAS_PIXMAN_GLYPHS
static pixman_glyph_cache_t *global_glyph_cache;
static inline pixman_glyph_cache_t *
get_glyph_cache (void)
{
if (!global_glyph_cache)
global_glyph_cache = pixman_glyph_cache_create ();
return global_glyph_cache;
}
void
_cairo_image_compositor_reset_static_data (void)
{
CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);
if (global_glyph_cache)
pixman_glyph_cache_destroy (global_glyph_cache);
global_glyph_cache = NULL;
CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
}
void
_cairo_image_scaled_glyph_fini (cairo_scaled_font_t *scaled_font,
cairo_scaled_glyph_t *scaled_glyph)
{
CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);
if (global_glyph_cache) {
pixman_glyph_cache_remove (
global_glyph_cache, scaled_font,
(void *)_cairo_scaled_glyph_index (scaled_glyph));
}
CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
}
static cairo_int_status_t
composite_glyphs (void *_dst,
cairo_operator_t op,
cairo_surface_t *_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
cairo_composite_glyphs_info_t *info)
{
cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
pixman_glyph_cache_t *glyph_cache;
pixman_glyph_t pglyphs_stack[CAIRO_STACK_ARRAY_LENGTH (pixman_glyph_t)];
pixman_glyph_t *pglyphs = pglyphs_stack;
pixman_glyph_t *pg;
int i;
TRACE ((stderr, "%s\n", __FUNCTION__));
CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);
glyph_cache = get_glyph_cache();
if (unlikely (glyph_cache == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto out_unlock;
}
pixman_glyph_cache_freeze (glyph_cache);
if (info->num_glyphs > ARRAY_LENGTH (pglyphs_stack)) {
pglyphs = _cairo_malloc_ab (info->num_glyphs, sizeof (pixman_glyph_t));
if (unlikely (pglyphs == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto out_thaw;
}
}
pg = pglyphs;
for (i = 0; i < info->num_glyphs; i++) {
unsigned long index = info->glyphs[i].index;
const void *glyph;
glyph = pixman_glyph_cache_lookup (glyph_cache, info->font, (void *)index);
if (!glyph) {
cairo_scaled_glyph_t *scaled_glyph;
cairo_image_surface_t *glyph_surface;
/* This call can actually end up recursing, so we have to
* drop the mutex around it.
*/
CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
status = _cairo_scaled_glyph_lookup (info->font, index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
CAIRO_MUTEX_LOCK (_cairo_glyph_cache_mutex);
if (unlikely (status))
goto out_thaw;
glyph_surface = scaled_glyph->surface;
glyph = pixman_glyph_cache_insert (glyph_cache, info->font, (void *)index,
glyph_surface->base.device_transform.x0,
glyph_surface->base.device_transform.y0,
glyph_surface->pixman_image);
if (unlikely (!glyph)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto out_thaw;
}
}
pg->x = _cairo_lround (info->glyphs[i].x);
pg->y = _cairo_lround (info->glyphs[i].y);
pg->glyph = glyph;
pg++;
}
if (info->use_mask) {
pixman_format_code_t mask_format;
mask_format = pixman_glyph_get_mask_format (glyph_cache, pg - pglyphs, pglyphs);
pixman_composite_glyphs (_pixman_operator (op),
((cairo_image_source_t *)_src)->pixman_image,
to_pixman_image (_dst),
mask_format,
info->extents.x + src_x, info->extents.y + src_y,
info->extents.x, info->extents.y,
info->extents.x - dst_x, info->extents.y - dst_y,
info->extents.width, info->extents.height,
glyph_cache, pg - pglyphs, pglyphs);
} else {
pixman_composite_glyphs_no_mask (_pixman_operator (op),
((cairo_image_source_t *)_src)->pixman_image,
to_pixman_image (_dst),
src_x, src_y,
- dst_x, - dst_y,
glyph_cache, pg - pglyphs, pglyphs);
}
out_thaw:
pixman_glyph_cache_thaw (glyph_cache);
if (pglyphs != pglyphs_stack)
free(pglyphs);
out_unlock:
CAIRO_MUTEX_UNLOCK (_cairo_glyph_cache_mutex);
return status;
}
#else
void
_cairo_image_compositor_reset_static_data (void)
{
}
void
_cairo_image_scaled_glyph_fini (cairo_scaled_font_t *scaled_font,
cairo_scaled_glyph_t *scaled_glyph)
{
}
static cairo_int_status_t
composite_one_glyph (void *_dst,
cairo_operator_t op,
cairo_surface_t *_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
cairo_composite_glyphs_info_t *info)
{
cairo_image_surface_t *glyph_surface;
cairo_scaled_glyph_t *scaled_glyph;
cairo_status_t status;
int x, y;
TRACE ((stderr, "%s\n", __FUNCTION__));
status = _cairo_scaled_glyph_lookup (info->font,
info->glyphs[0].index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status))
return status;
glyph_surface = scaled_glyph->surface;
if (glyph_surface->width == 0 || glyph_surface->height == 0)
return CAIRO_INT_STATUS_NOTHING_TO_DO;
/* round glyph locations to the nearest pixel */
/* XXX: FRAGILE: We're ignoring device_transform scaling here. A bug? */
x = _cairo_lround (info->glyphs[0].x -
glyph_surface->base.device_transform.x0);
y = _cairo_lround (info->glyphs[0].y -
glyph_surface->base.device_transform.y0);
pixman_image_composite32 (_pixman_operator (op),
((cairo_image_source_t *)_src)->pixman_image,
glyph_surface->pixman_image,
to_pixman_image (_dst),
x + src_x, y + src_y,
0, 0,
x - dst_x, y - dst_y,
glyph_surface->width,
glyph_surface->height);
return CAIRO_INT_STATUS_SUCCESS;
}
static cairo_int_status_t
composite_glyphs_via_mask (void *_dst,
cairo_operator_t op,
cairo_surface_t *_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
cairo_composite_glyphs_info_t *info)
{
cairo_scaled_glyph_t *glyph_cache[64];
pixman_image_t *white = _pixman_image_for_color (CAIRO_COLOR_WHITE);
cairo_scaled_glyph_t *scaled_glyph;
uint8_t buf[2048];
pixman_image_t *mask;
pixman_format_code_t format;
cairo_status_t status;
int i;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (unlikely (white == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
/* XXX convert the glyphs to common formats a8/a8r8g8b8 to hit
* optimised paths through pixman. Should we increase the bit
* depth of the target surface, we should reconsider the appropriate
* mask formats.
*/
status = _cairo_scaled_glyph_lookup (info->font,
info->glyphs[0].index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status)) {
pixman_image_unref (white);
return status;
}
memset (glyph_cache, 0, sizeof (glyph_cache));
glyph_cache[info->glyphs[0].index % ARRAY_LENGTH (glyph_cache)] = scaled_glyph;
format = PIXMAN_a8;
i = (info->extents.width + 3) & ~3;
if (scaled_glyph->surface->base.content & CAIRO_CONTENT_COLOR) {
format = PIXMAN_a8r8g8b8;
i = info->extents.width * 4;
}
if (i * info->extents.height > (int) sizeof (buf)) {
mask = pixman_image_create_bits (format,
info->extents.width,
info->extents.height,
NULL, 0);
} else {
memset (buf, 0, i * info->extents.height);
mask = pixman_image_create_bits (format,
info->extents.width,
info->extents.height,
(uint32_t *)buf, i);
}
if (unlikely (mask == NULL)) {
pixman_image_unref (white);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
status = CAIRO_STATUS_SUCCESS;
for (i = 0; i < info->num_glyphs; i++) {
unsigned long glyph_index = info->glyphs[i].index;
int cache_index = glyph_index % ARRAY_LENGTH (glyph_cache);
cairo_image_surface_t *glyph_surface;
int x, y;
scaled_glyph = glyph_cache[cache_index];
if (scaled_glyph == NULL ||
_cairo_scaled_glyph_index (scaled_glyph) != glyph_index)
{
status = _cairo_scaled_glyph_lookup (info->font, glyph_index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status)) {
pixman_image_unref (mask);
pixman_image_unref (white);
return status;
}
glyph_cache[cache_index] = scaled_glyph;
}
glyph_surface = scaled_glyph->surface;
if (glyph_surface->width && glyph_surface->height) {
if (glyph_surface->base.content & CAIRO_CONTENT_COLOR &&
format == PIXMAN_a8) {
pixman_image_t *ca_mask;
format = PIXMAN_a8r8g8b8;
ca_mask = pixman_image_create_bits (format,
info->extents.width,
info->extents.height,
NULL, 0);
if (unlikely (ca_mask == NULL)) {
pixman_image_unref (mask);
pixman_image_unref (white);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
pixman_image_composite32 (PIXMAN_OP_SRC,
white, mask, ca_mask,
0, 0,
0, 0,
0, 0,
info->extents.width,
info->extents.height);
pixman_image_unref (mask);
mask = ca_mask;
}
/* round glyph locations to the nearest pixel */
/* XXX: FRAGILE: We're ignoring device_transform scaling here. A bug? */
x = _cairo_lround (info->glyphs[i].x -
glyph_surface->base.device_transform.x0);
y = _cairo_lround (info->glyphs[i].y -
glyph_surface->base.device_transform.y0);
if (glyph_surface->pixman_format == format) {
pixman_image_composite32 (PIXMAN_OP_ADD,
glyph_surface->pixman_image, NULL, mask,
0, 0,
0, 0,
x - info->extents.x, y - info->extents.y,
glyph_surface->width,
glyph_surface->height);
} else {
pixman_image_composite32 (PIXMAN_OP_ADD,
white, glyph_surface->pixman_image, mask,
0, 0,
0, 0,
x - info->extents.x, y - info->extents.y,
glyph_surface->width,
glyph_surface->height);
}
}
}
if (format == PIXMAN_a8r8g8b8)
pixman_image_set_component_alpha (mask, TRUE);
pixman_image_composite32 (_pixman_operator (op),
((cairo_image_source_t *)_src)->pixman_image,
mask,
to_pixman_image (_dst),
info->extents.x + src_x, info->extents.y + src_y,
0, 0,
info->extents.x - dst_x, info->extents.y - dst_y,
info->extents.width, info->extents.height);
pixman_image_unref (mask);
pixman_image_unref (white);
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
composite_glyphs (void *_dst,
cairo_operator_t op,
cairo_surface_t *_src,
int src_x,
int src_y,
int dst_x,
int dst_y,
cairo_composite_glyphs_info_t *info)
{
cairo_scaled_glyph_t *glyph_cache[64];
pixman_image_t *dst, *src;
cairo_status_t status;
int i;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (info->num_glyphs == 1)
return composite_one_glyph(_dst, op, _src, src_x, src_y, dst_x, dst_y, info);
if (info->use_mask)
return composite_glyphs_via_mask(_dst, op, _src, src_x, src_y, dst_x, dst_y, info);
op = _pixman_operator (op);
dst = to_pixman_image (_dst);
src = ((cairo_image_source_t *)_src)->pixman_image;
memset (glyph_cache, 0, sizeof (glyph_cache));
status = CAIRO_STATUS_SUCCESS;
for (i = 0; i < info->num_glyphs; i++) {
int x, y;
cairo_image_surface_t *glyph_surface;
cairo_scaled_glyph_t *scaled_glyph;
unsigned long glyph_index = info->glyphs[i].index;
int cache_index = glyph_index % ARRAY_LENGTH (glyph_cache);
scaled_glyph = glyph_cache[cache_index];
if (scaled_glyph == NULL ||
_cairo_scaled_glyph_index (scaled_glyph) != glyph_index)
{
status = _cairo_scaled_glyph_lookup (info->font, glyph_index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status))
break;
glyph_cache[cache_index] = scaled_glyph;
}
glyph_surface = scaled_glyph->surface;
if (glyph_surface->width && glyph_surface->height) {
/* round glyph locations to the nearest pixel */
/* XXX: FRAGILE: We're ignoring device_transform scaling here. A bug? */
x = _cairo_lround (info->glyphs[i].x -
glyph_surface->base.device_transform.x0);
y = _cairo_lround (info->glyphs[i].y -
glyph_surface->base.device_transform.y0);
pixman_image_composite32 (op, src, glyph_surface->pixman_image, dst,
x + src_x, y + src_y,
0, 0,
x - dst_x, y - dst_y,
glyph_surface->width,
glyph_surface->height);
}
}
return status;
}
#endif
static cairo_int_status_t
check_composite (const cairo_composite_rectangles_t *extents)
{
return CAIRO_STATUS_SUCCESS;
}
const cairo_compositor_t *
_cairo_image_traps_compositor_get (void)
{
static cairo_atomic_once_t once = CAIRO_ATOMIC_ONCE_INIT;
static cairo_traps_compositor_t compositor;
if (_cairo_atomic_init_once_enter(&once)) {
_cairo_traps_compositor_init(&compositor,
&__cairo_no_compositor);
compositor.acquire = acquire;
compositor.release = release;
compositor.set_clip_region = set_clip_region;
compositor.pattern_to_surface = _cairo_image_source_create_for_pattern;
compositor.draw_image_boxes = draw_image_boxes;
//compositor.copy_boxes = copy_boxes;
compositor.fill_boxes = fill_boxes;
compositor.check_composite = check_composite;
compositor.composite = composite;
compositor.lerp = lerp;
//compositor.check_composite_boxes = check_composite_boxes;
compositor.composite_boxes = composite_boxes;
//compositor.check_composite_traps = check_composite_traps;
compositor.composite_traps = composite_traps;
//compositor.check_composite_tristrip = check_composite_traps;
#if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0,22,0)
compositor.composite_tristrip = composite_tristrip;
#endif
compositor.check_composite_glyphs = check_composite_glyphs;
compositor.composite_glyphs = composite_glyphs;
_cairo_atomic_init_once_leave(&once);
}
return &compositor.base;
}
const cairo_compositor_t *
_cairo_image_mask_compositor_get (void)
{
static cairo_atomic_once_t once = CAIRO_ATOMIC_ONCE_INIT;
static cairo_mask_compositor_t compositor;
if (_cairo_atomic_init_once_enter(&once)) {
_cairo_mask_compositor_init (&compositor,
_cairo_image_traps_compositor_get ());
compositor.acquire = acquire;
compositor.release = release;
compositor.set_clip_region = set_clip_region;
compositor.pattern_to_surface = _cairo_image_source_create_for_pattern;
compositor.draw_image_boxes = draw_image_boxes;
compositor.fill_rectangles = fill_rectangles;
compositor.fill_boxes = fill_boxes;
compositor.check_composite = check_composite;
compositor.composite = composite;
//compositor.lerp = lerp;
//compositor.check_composite_boxes = check_composite_boxes;
compositor.composite_boxes = composite_boxes;
compositor.check_composite_glyphs = check_composite_glyphs;
compositor.composite_glyphs = composite_glyphs;
_cairo_atomic_init_once_leave(&once);
}
return &compositor.base;
}
#if PIXMAN_HAS_COMPOSITOR
typedef struct _cairo_image_span_renderer {
cairo_span_renderer_t base;
pixman_image_compositor_t *compositor;
pixman_image_t *src, *mask;
float opacity;
cairo_rectangle_int_t extents;
} cairo_image_span_renderer_t;
COMPILE_TIME_ASSERT (sizeof (cairo_image_span_renderer_t) <= sizeof (cairo_abstract_span_renderer_t));
static cairo_status_t
_cairo_image_bounded_opaque_spans (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
do {
if (spans[0].coverage)
pixman_image_compositor_blt (r->compositor,
spans[0].x, y,
spans[1].x - spans[0].x, height,
spans[0].coverage);
spans++;
} while (--num_spans > 1);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_image_bounded_spans (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
do {
if (spans[0].coverage) {
pixman_image_compositor_blt (r->compositor,
spans[0].x, y,
spans[1].x - spans[0].x, height,
r->opacity * spans[0].coverage);
}
spans++;
} while (--num_spans > 1);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_image_unbounded_spans (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
assert (y + height <= r->extents.height);
if (y > r->extents.y) {
pixman_image_compositor_blt (r->compositor,
r->extents.x, r->extents.y,
r->extents.width, y - r->extents.y,
0);
}
if (num_spans == 0) {
pixman_image_compositor_blt (r->compositor,
r->extents.x, y,
r->extents.width, height,
0);
} else {
if (spans[0].x != r->extents.x) {
pixman_image_compositor_blt (r->compositor,
r->extents.x, y,
spans[0].x - r->extents.x,
height,
0);
}
do {
assert (spans[0].x < r->extents.x + r->extents.width);
pixman_image_compositor_blt (r->compositor,
spans[0].x, y,
spans[1].x - spans[0].x, height,
r->opacity * spans[0].coverage);
spans++;
} while (--num_spans > 1);
if (spans[0].x != r->extents.x + r->extents.width) {
assert (spans[0].x < r->extents.x + r->extents.width);
pixman_image_compositor_blt (r->compositor,
spans[0].x, y,
r->extents.x + r->extents.width - spans[0].x, height,
0);
}
}
r->extents.y = y + height;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_image_clipped_spans (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
assert (num_spans);
do {
if (! spans[0].inverse)
pixman_image_compositor_blt (r->compositor,
spans[0].x, y,
spans[1].x - spans[0].x, height,
r->opacity * spans[0].coverage);
spans++;
} while (--num_spans > 1);
r->extents.y = y + height;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_image_finish_unbounded_spans (void *abstract_renderer)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (r->extents.y < r->extents.height) {
pixman_image_compositor_blt (r->compositor,
r->extents.x, r->extents.y,
r->extents.width,
r->extents.height - r->extents.y,
0);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
span_renderer_init (cairo_abstract_span_renderer_t *_r,
const cairo_composite_rectangles_t *composite,
cairo_bool_t needs_clip)
{
cairo_image_span_renderer_t *r = (cairo_image_span_renderer_t *)_r;
cairo_image_surface_t *dst = (cairo_image_surface_t *)composite->surface;
const cairo_pattern_t *source = &composite->source_pattern.base;
cairo_operator_t op = composite->op;
int src_x, src_y;
int mask_x, mask_y;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (op == CAIRO_OPERATOR_CLEAR) {
op = PIXMAN_OP_LERP_CLEAR;
} else if (dst->base.is_clear &&
(op == CAIRO_OPERATOR_SOURCE ||
op == CAIRO_OPERATOR_OVER ||
op == CAIRO_OPERATOR_ADD)) {
op = PIXMAN_OP_SRC;
} else if (op == CAIRO_OPERATOR_SOURCE) {
op = PIXMAN_OP_LERP_SRC;
} else {
op = _pixman_operator (op);
}
r->compositor = NULL;
r->mask = NULL;
r->src = _pixman_image_for_pattern (dst, source, FALSE,
&composite->unbounded,
&composite->source_sample_area,
&src_x, &src_y);
if (unlikely (r->src == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
r->opacity = 1.0;
if (composite->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID) {
r->opacity = composite->mask_pattern.solid.color.alpha;
} else {
r->mask = _pixman_image_for_pattern (dst,
&composite->mask_pattern.base,
TRUE,
&composite->unbounded,
&composite->mask_sample_area,
&mask_x, &mask_y);
if (unlikely (r->mask == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
/* XXX Component-alpha? */
if ((dst->base.content & CAIRO_CONTENT_COLOR) == 0 &&
_cairo_pattern_is_opaque (source, &composite->source_sample_area))
{
pixman_image_unref (r->src);
r->src = r->mask;
src_x = mask_x;
src_y = mask_y;
r->mask = NULL;
}
}
if (composite->is_bounded) {
if (r->opacity == 1.)
r->base.render_rows = _cairo_image_bounded_opaque_spans;
else
r->base.render_rows = _cairo_image_bounded_spans;
r->base.finish = NULL;
} else {
if (needs_clip)
r->base.render_rows = _cairo_image_clipped_spans;
else
r->base.render_rows = _cairo_image_unbounded_spans;
r->base.finish = _cairo_image_finish_unbounded_spans;
r->extents = composite->unbounded;
r->extents.height += r->extents.y;
}
r->compositor =
pixman_image_create_compositor (op, r->src, r->mask, dst->pixman_image,
composite->unbounded.x + src_x,
composite->unbounded.y + src_y,
composite->unbounded.x + mask_x,
composite->unbounded.y + mask_y,
composite->unbounded.x,
composite->unbounded.y,
composite->unbounded.width,
composite->unbounded.height);
if (unlikely (r->compositor == NULL))
return CAIRO_INT_STATUS_NOTHING_TO_DO;
return CAIRO_STATUS_SUCCESS;
}
static void
span_renderer_fini (cairo_abstract_span_renderer_t *_r,
cairo_int_status_t status)
{
cairo_image_span_renderer_t *r = (cairo_image_span_renderer_t *) _r;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (status == CAIRO_INT_STATUS_SUCCESS && r->base.finish)
r->base.finish (r);
if (r->compositor)
pixman_image_compositor_destroy (r->compositor);
if (r->src)
pixman_image_unref (r->src);
if (r->mask)
pixman_image_unref (r->mask);
}
#else
typedef struct _cairo_image_span_renderer {
cairo_span_renderer_t base;
const cairo_composite_rectangles_t *composite;
float opacity;
uint8_t op;
int bpp;
pixman_image_t *src, *mask;
union {
struct fill {
ptrdiff_t stride;
uint8_t *data;
uint32_t pixel;
} fill;
struct blit {
int stride;
uint8_t *data;
int src_stride;
uint8_t *src_data;
} blit;
struct composite {
pixman_image_t *dst;
int src_x, src_y;
int mask_x, mask_y;
int run_length;
} composite;
struct finish {
cairo_rectangle_int_t extents;
int src_x, src_y;
ptrdiff_t stride;
uint8_t *data;
} mask;
} u;
uint8_t _buf[0];
#define SZ_BUF (int)(sizeof (cairo_abstract_span_renderer_t) - sizeof (cairo_image_span_renderer_t))
} cairo_image_span_renderer_t;
COMPILE_TIME_ASSERT (sizeof (cairo_image_span_renderer_t) <= sizeof (cairo_abstract_span_renderer_t));
static cairo_status_t
_cairo_image_spans (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
uint8_t *mask, *row;
int len;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
mask = r->u.mask.data + (y - r->u.mask.extents.y) * r->u.mask.stride;
mask += spans[0].x - r->u.mask.extents.x;
row = mask;
do {
len = spans[1].x - spans[0].x;
if (spans[0].coverage) {
*row++ = r->opacity * spans[0].coverage;
if (--len)
memset (row, row[-1], len);
}
row += len;
spans++;
} while (--num_spans > 1);
len = row - mask;
row = mask;
while (--height) {
mask += r->u.mask.stride;
memcpy (mask, row, len);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_image_spans_and_zero (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
uint8_t *mask;
int len;
mask = r->u.mask.data;
if (y > r->u.mask.extents.y) {
len = (y - r->u.mask.extents.y) * r->u.mask.stride;
memset (mask, 0, len);
mask += len;
}
r->u.mask.extents.y = y + height;
r->u.mask.data = mask + height * r->u.mask.stride;
if (num_spans == 0) {
memset (mask, 0, height * r->u.mask.stride);
} else {
uint8_t *row = mask;
if (spans[0].x != r->u.mask.extents.x) {
len = spans[0].x - r->u.mask.extents.x;
memset (row, 0, len);
row += len;
}
do {
len = spans[1].x - spans[0].x;
*row++ = r->opacity * spans[0].coverage;
if (len > 1) {
memset (row, row[-1], --len);
row += len;
}
spans++;
} while (--num_spans > 1);
if (spans[0].x != r->u.mask.extents.x + r->u.mask.extents.width) {
len = r->u.mask.extents.x + r->u.mask.extents.width - spans[0].x;
memset (row, 0, len);
}
row = mask;
while (--height) {
mask += r->u.mask.stride;
memcpy (mask, row, r->u.mask.extents.width);
}
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_image_finish_spans_and_zero (void *abstract_renderer)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (r->u.mask.extents.y < r->u.mask.extents.height)
memset (r->u.mask.data, 0, (r->u.mask.extents.height - r->u.mask.extents.y) * r->u.mask.stride);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_fill8_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (likely(h == 1)) {
do {
if (spans[0].coverage) {
int len = spans[1].x - spans[0].x;
uint8_t *d = r->u.fill.data + r->u.fill.stride*y + spans[0].x;
if (len == 1)
*d = r->u.fill.pixel;
else
memset(d, r->u.fill.pixel, len);
}
spans++;
} while (--num_spans > 1);
} else {
do {
if (spans[0].coverage) {
int yy = y, hh = h;
do {
int len = spans[1].x - spans[0].x;
uint8_t *d = r->u.fill.data + r->u.fill.stride*yy + spans[0].x;
if (len == 1)
*d = r->u.fill.pixel;
else
memset(d, r->u.fill.pixel, len);
yy++;
} while (--hh);
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_fill16_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (likely(h == 1)) {
do {
if (spans[0].coverage) {
int len = spans[1].x - spans[0].x;
uint16_t *d = (uint16_t*)(r->u.fill.data + r->u.fill.stride*y + spans[0].x*2);
while (len-- > 0)
*d++ = r->u.fill.pixel;
}
spans++;
} while (--num_spans > 1);
} else {
do {
if (spans[0].coverage) {
int yy = y, hh = h;
do {
int len = spans[1].x - spans[0].x;
uint16_t *d = (uint16_t*)(r->u.fill.data + r->u.fill.stride*yy + spans[0].x*2);
while (len-- > 0)
*d++ = r->u.fill.pixel;
yy++;
} while (--hh);
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_fill32_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (likely(h == 1)) {
do {
if (spans[0].coverage) {
int len = spans[1].x - spans[0].x;
if (len > 32) {
pixman_fill ((uint32_t *)r->u.fill.data, r->u.fill.stride / sizeof(uint32_t), r->bpp,
spans[0].x, y, len, 1, r->u.fill.pixel);
} else {
uint32_t *d = (uint32_t*)(r->u.fill.data + r->u.fill.stride*y + spans[0].x*4);
while (len-- > 0)
*d++ = r->u.fill.pixel;
}
}
spans++;
} while (--num_spans > 1);
} else {
do {
if (spans[0].coverage) {
if (spans[1].x - spans[0].x > 16) {
pixman_fill ((uint32_t *)r->u.fill.data, r->u.fill.stride / sizeof(uint32_t), r->bpp,
spans[0].x, y, spans[1].x - spans[0].x, h,
r->u.fill.pixel);
} else {
int yy = y, hh = h;
do {
int len = spans[1].x - spans[0].x;
uint32_t *d = (uint32_t*)(r->u.fill.data + r->u.fill.stride*yy + spans[0].x*4);
while (len-- > 0)
*d++ = r->u.fill.pixel;
yy++;
} while (--hh);
}
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
#if 0
static cairo_status_t
_fill_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
do {
if (spans[0].coverage) {
pixman_fill ((uint32_t *) r->data, r->stride, r->bpp,
spans[0].x, y,
spans[1].x - spans[0].x, h,
r->pixel);
}
spans++;
} while (--num_spans > 1);
return CAIRO_STATUS_SUCCESS;
}
#endif
static cairo_status_t
_blit_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
int cpp;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
cpp = r->bpp/8;
if (likely (h == 1)) {
uint8_t *src = r->u.blit.src_data + y*r->u.blit.src_stride;
uint8_t *dst = r->u.blit.data + y*r->u.blit.stride;
do {
if (spans[0].coverage) {
void *s = src + spans[0].x*cpp;
void *d = dst + spans[0].x*cpp;
int len = (spans[1].x - spans[0].x) * cpp;
switch (len) {
case 1:
*(uint8_t *)d = *(uint8_t *)s;
break;
case 2:
*(uint16_t *)d = *(uint16_t *)s;
break;
case 4:
*(uint32_t *)d = *(uint32_t *)s;
break;
#if HAVE_UINT64_T
case 8:
*(uint64_t *)d = *(uint64_t *)s;
break;
#endif
default:
memcpy(d, s, len);
break;
}
}
spans++;
} while (--num_spans > 1);
} else {
do {
if (spans[0].coverage) {
int yy = y, hh = h;
do {
void *src = r->u.blit.src_data + yy*r->u.blit.src_stride + spans[0].x*cpp;
void *dst = r->u.blit.data + yy*r->u.blit.stride + spans[0].x*cpp;
int len = (spans[1].x - spans[0].x) * cpp;
switch (len) {
case 1:
*(uint8_t *)dst = *(uint8_t *)src;
break;
case 2:
*(uint16_t *)dst = *(uint16_t *)src;
break;
case 4:
*(uint32_t *)dst = *(uint32_t *)src;
break;
#if HAVE_UINT64_T
case 8:
*(uint64_t *)dst = *(uint64_t *)src;
break;
#endif
default:
memcpy(dst, src, len);
break;
}
yy++;
} while (--hh);
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_mono_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
do {
if (spans[0].coverage) {
pixman_image_composite32 (r->op,
r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x, y + r->u.composite.src_y,
0, 0,
spans[0].x, y,
spans[1].x - spans[0].x, h);
}
spans++;
} while (--num_spans > 1);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_mono_unbounded_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0) {
pixman_image_composite32 (PIXMAN_OP_CLEAR,
r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x, y + r->u.composite.src_y,
0, 0,
r->composite->unbounded.x, y,
r->composite->unbounded.width, h);
r->u.composite.mask_y = y + h;
return CAIRO_STATUS_SUCCESS;
}
if (y != r->u.composite.mask_y) {
pixman_image_composite32 (PIXMAN_OP_CLEAR,
r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x, y + r->u.composite.src_y,
0, 0,
r->composite->unbounded.x, r->u.composite.mask_y,
r->composite->unbounded.width, y - r->u.composite.mask_y);
}
if (spans[0].x != r->composite->unbounded.x) {
pixman_image_composite32 (PIXMAN_OP_CLEAR,
r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x, y + r->u.composite.src_y,
0, 0,
r->composite->unbounded.x, y,
spans[0].x - r->composite->unbounded.x, h);
}
do {
int op = spans[0].coverage ? r->op : PIXMAN_OP_CLEAR;
pixman_image_composite32 (op,
r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x, y + r->u.composite.src_y,
0, 0,
spans[0].x, y,
spans[1].x - spans[0].x, h);
spans++;
} while (--num_spans > 1);
if (spans[0].x != r->composite->unbounded.x + r->composite->unbounded.width) {
pixman_image_composite32 (PIXMAN_OP_CLEAR,
r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x, y + r->u.composite.src_y,
0, 0,
spans[0].x, y,
r->composite->unbounded.x + r->composite->unbounded.width - spans[0].x, h);
}
r->u.composite.mask_y = y + h;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_mono_finish_unbounded_spans (void *abstract_renderer)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (r->u.composite.mask_y < r->composite->unbounded.y + r->composite->unbounded.height) {
pixman_image_composite32 (PIXMAN_OP_CLEAR,
r->src, NULL, r->u.composite.dst,
r->composite->unbounded.x + r->u.composite.src_x, r->u.composite.mask_y + r->u.composite.src_y,
0, 0,
r->composite->unbounded.x, r->u.composite.mask_y,
r->composite->unbounded.width,
r->composite->unbounded.y + r->composite->unbounded.height - r->u.composite.mask_y);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
mono_renderer_init (cairo_image_span_renderer_t *r,
const cairo_composite_rectangles_t *composite,
cairo_antialias_t antialias,
cairo_bool_t needs_clip)
{
cairo_image_surface_t *dst = (cairo_image_surface_t *)composite->surface;
if (antialias != CAIRO_ANTIALIAS_NONE)
return CAIRO_INT_STATUS_UNSUPPORTED;
if (!_cairo_pattern_is_opaque_solid (&composite->mask_pattern.base))
return CAIRO_INT_STATUS_UNSUPPORTED;
r->base.render_rows = NULL;
if (composite->source_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID) {
const cairo_color_t *color;
color = &composite->source_pattern.solid.color;
if (composite->op == CAIRO_OPERATOR_CLEAR)
color = CAIRO_COLOR_TRANSPARENT;
if (fill_reduces_to_source (composite->op, color, dst, &r->u.fill.pixel)) {
/* Use plain C for the fill operations as the span length is
* typically small, too small to payback the startup overheads of
* using SSE2 etc.
*/
switch (PIXMAN_FORMAT_BPP(dst->pixman_format)) {
case 8: r->base.render_rows = _fill8_spans; break;
case 16: r->base.render_rows = _fill16_spans; break;
case 32: r->base.render_rows = _fill32_spans; break;
default: break;
}
r->u.fill.data = dst->data;
r->u.fill.stride = dst->stride;
}
} else if ((composite->op == CAIRO_OPERATOR_SOURCE ||
(composite->op == CAIRO_OPERATOR_OVER &&
(dst->base.is_clear || (dst->base.content & CAIRO_CONTENT_ALPHA) == 0))) &&
composite->source_pattern.base.type == CAIRO_PATTERN_TYPE_SURFACE &&
composite->source_pattern.surface.surface->backend->type == CAIRO_SURFACE_TYPE_IMAGE &&
to_image_surface(composite->source_pattern.surface.surface)->format == dst->format)
{
cairo_image_surface_t *src =
to_image_surface(composite->source_pattern.surface.surface);
int tx, ty;
if (_cairo_matrix_is_integer_translation(&composite->source_pattern.base.matrix,
&tx, &ty) &&
composite->bounded.x + tx >= 0 &&
composite->bounded.y + ty >= 0 &&
composite->bounded.x + composite->bounded.width + tx <= src->width &&
composite->bounded.y + composite->bounded.height + ty <= src->height) {
r->u.blit.stride = dst->stride;
r->u.blit.data = dst->data;
r->u.blit.src_stride = src->stride;
r->u.blit.src_data = src->data + src->stride * ty + tx * 4;
r->base.render_rows = _blit_spans;
}
}
if (r->base.render_rows == NULL) {
r->src = _pixman_image_for_pattern (dst, &composite->source_pattern.base, FALSE,
&composite->unbounded,
&composite->source_sample_area,
&r->u.composite.src_x, &r->u.composite.src_y);
if (unlikely (r->src == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
r->u.composite.dst = to_pixman_image (composite->surface);
r->op = _pixman_operator (composite->op);
if (composite->is_bounded == 0) {
r->base.render_rows = _mono_unbounded_spans;
r->base.finish = _mono_finish_unbounded_spans;
r->u.composite.mask_y = composite->unbounded.y;
} else
r->base.render_rows = _mono_spans;
}
r->bpp = PIXMAN_FORMAT_BPP(dst->pixman_format);
return CAIRO_INT_STATUS_SUCCESS;
}
#define ONE_HALF 0x7f
#define RB_MASK 0x00ff00ff
#define RB_ONE_HALF 0x007f007f
#define RB_MASK_PLUS_ONE 0x01000100
#define G_SHIFT 8
static inline uint32_t
mul8x2_8 (uint32_t a, uint8_t b)
{
uint32_t t = (a & RB_MASK) * b + RB_ONE_HALF;
return ((t + ((t >> G_SHIFT) & RB_MASK)) >> G_SHIFT) & RB_MASK;
}
static inline uint32_t
add8x2_8x2 (uint32_t a, uint32_t b)
{
uint32_t t = a + b;
t |= RB_MASK_PLUS_ONE - ((t >> G_SHIFT) & RB_MASK);
return t & RB_MASK;
}
static inline uint8_t
mul8_8 (uint8_t a, uint8_t b)
{
uint16_t t = a * (uint16_t)b + ONE_HALF;
return ((t >> G_SHIFT) + t) >> G_SHIFT;
}
static inline uint32_t
lerp8x4 (uint32_t src, uint8_t a, uint32_t dst)
{
return (add8x2_8x2 (mul8x2_8 (src, a),
mul8x2_8 (dst, ~a)) |
add8x2_8x2 (mul8x2_8 (src >> G_SHIFT, a),
mul8x2_8 (dst >> G_SHIFT, ~a)) << G_SHIFT);
}
static cairo_status_t
_fill_a8_lerp_opaque_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (likely(h == 1)) {
uint8_t *d = r->u.fill.data + r->u.fill.stride*y;
do {
uint8_t a = spans[0].coverage;
if (a) {
int len = spans[1].x - spans[0].x;
if (a == 0xff) {
memset(d + spans[0].x, r->u.fill.pixel, len);
} else {
uint8_t s = mul8_8(a, r->u.fill.pixel);
uint8_t *dst = d + spans[0].x;
a = ~a;
while (len-- > 0) {
uint8_t t = mul8_8(*dst, a);
*dst++ = t + s;
}
}
}
spans++;
} while (--num_spans > 1);
} else {
do {
uint8_t a = spans[0].coverage;
if (a) {
int yy = y, hh = h;
if (a == 0xff) {
do {
int len = spans[1].x - spans[0].x;
uint8_t *d = r->u.fill.data + r->u.fill.stride*yy + spans[0].x;
memset(d, r->u.fill.pixel, len);
yy++;
} while (--hh);
} else {
uint8_t s = mul8_8(a, r->u.fill.pixel);
a = ~a;
do {
int len = spans[1].x - spans[0].x;
uint8_t *d = r->u.fill.data + r->u.fill.stride*yy + spans[0].x;
while (len-- > 0) {
uint8_t t = mul8_8(*d, a);
*d++ = t + s;
}
yy++;
} while (--hh);
}
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_fill_xrgb32_lerp_opaque_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (likely(h == 1)) {
do {
uint8_t a = spans[0].coverage;
if (a) {
int len = spans[1].x - spans[0].x;
uint32_t *d = (uint32_t*)(r->u.fill.data + r->u.fill.stride*y + spans[0].x*4);
if (a == 0xff) {
if (len > 31) {
pixman_fill ((uint32_t *)r->u.fill.data, r->u.fill.stride / sizeof(uint32_t), 32,
spans[0].x, y, len, 1, r->u.fill.pixel);
} else {
uint32_t *d = (uint32_t*)(r->u.fill.data + r->u.fill.stride*y + spans[0].x*4);
while (len-- > 0)
*d++ = r->u.fill.pixel;
}
} else while (len-- > 0) {
*d = lerp8x4 (r->u.fill.pixel, a, *d);
d++;
}
}
spans++;
} while (--num_spans > 1);
} else {
do {
uint8_t a = spans[0].coverage;
if (a) {
if (a == 0xff) {
if (spans[1].x - spans[0].x > 16) {
pixman_fill ((uint32_t *)r->u.fill.data, r->u.fill.stride / sizeof(uint32_t), 32,
spans[0].x, y, spans[1].x - spans[0].x, h,
r->u.fill.pixel);
} else {
int yy = y, hh = h;
do {
int len = spans[1].x - spans[0].x;
uint32_t *d = (uint32_t*)(r->u.fill.data + r->u.fill.stride*yy + spans[0].x*4);
while (len-- > 0)
*d++ = r->u.fill.pixel;
yy++;
} while (--hh);
}
} else {
int yy = y, hh = h;
do {
int len = spans[1].x - spans[0].x;
uint32_t *d = (uint32_t *)(r->u.fill.data + r->u.fill.stride*yy + spans[0].x*4);
while (len-- > 0) {
*d = lerp8x4 (r->u.fill.pixel, a, *d);
d++;
}
yy++;
} while (--hh);
}
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_fill_a8_lerp_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (likely(h == 1)) {
do {
uint8_t a = mul8_8 (spans[0].coverage, r->bpp);
if (a) {
int len = spans[1].x - spans[0].x;
uint8_t *d = r->u.fill.data + r->u.fill.stride*y + spans[0].x;
uint16_t p = (uint16_t)a * r->u.fill.pixel + 0x7f;
uint16_t ia = ~a;
while (len-- > 0) {
uint16_t t = *d*ia + p;
*d++ = (t + (t>>8)) >> 8;
}
}
spans++;
} while (--num_spans > 1);
} else {
do {
uint8_t a = mul8_8 (spans[0].coverage, r->bpp);
if (a) {
int yy = y, hh = h;
uint16_t p = (uint16_t)a * r->u.fill.pixel + 0x7f;
uint16_t ia = ~a;
do {
int len = spans[1].x - spans[0].x;
uint8_t *d = r->u.fill.data + r->u.fill.stride*yy + spans[0].x;
while (len-- > 0) {
uint16_t t = *d*ia + p;
*d++ = (t + (t>>8)) >> 8;
}
yy++;
} while (--hh);
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_fill_xrgb32_lerp_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (likely(h == 1)) {
do {
uint8_t a = mul8_8 (spans[0].coverage, r->bpp);
if (a) {
int len = spans[1].x - spans[0].x;
uint32_t *d = (uint32_t*)(r->u.fill.data + r->u.fill.stride*y + spans[0].x*4);
while (len-- > 0) {
*d = lerp8x4 (r->u.fill.pixel, a, *d);
d++;
}
}
spans++;
} while (--num_spans > 1);
} else {
do {
uint8_t a = mul8_8 (spans[0].coverage, r->bpp);
if (a) {
int yy = y, hh = h;
do {
int len = spans[1].x - spans[0].x;
uint32_t *d = (uint32_t *)(r->u.fill.data + r->u.fill.stride*yy + spans[0].x*4);
while (len-- > 0) {
*d = lerp8x4 (r->u.fill.pixel, a, *d);
d++;
}
yy++;
} while (--hh);
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_blit_xrgb32_lerp_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans, unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (likely(h == 1)) {
uint8_t *src = r->u.blit.src_data + y*r->u.blit.src_stride;
uint8_t *dst = r->u.blit.data + y*r->u.blit.stride;
do {
uint8_t a = mul8_8 (spans[0].coverage, r->bpp);
if (a) {
uint32_t *s = (uint32_t*)src + spans[0].x;
uint32_t *d = (uint32_t*)dst + spans[0].x;
int len = spans[1].x - spans[0].x;
if (a == 0xff) {
if (len == 1)
*d = *s;
else
memcpy(d, s, len*4);
} else {
while (len-- > 0) {
*d = lerp8x4 (*s, a, *d);
s++, d++;
}
}
}
spans++;
} while (--num_spans > 1);
} else {
do {
uint8_t a = mul8_8 (spans[0].coverage, r->bpp);
if (a) {
int yy = y, hh = h;
do {
uint32_t *s = (uint32_t *)(r->u.blit.src_data + yy*r->u.blit.src_stride + spans[0].x * 4);
uint32_t *d = (uint32_t *)(r->u.blit.data + yy*r->u.blit.stride + spans[0].x * 4);
int len = spans[1].x - spans[0].x;
if (a == 0xff) {
if (len == 1)
*d = *s;
else
memcpy(d, s, len * 4);
} else {
while (len-- > 0) {
*d = lerp8x4 (*s, a, *d);
s++, d++;
}
}
yy++;
} while (--hh);
}
spans++;
} while (--num_spans > 1);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_inplace_spans (void *abstract_renderer,
int y, int h,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
uint8_t *mask;
int x0, x1;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
if (num_spans == 2 && spans[0].coverage == 0xff) {
pixman_image_composite32 (r->op, r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
spans[0].x, y,
spans[1].x - spans[0].x, h);
return CAIRO_STATUS_SUCCESS;
}
mask = (uint8_t *)pixman_image_get_data (r->mask);
x1 = x0 = spans[0].x;
do {
int len = spans[1].x - spans[0].x;
*mask++ = spans[0].coverage;
if (len > 1) {
if (len >= r->u.composite.run_length && spans[0].coverage == 0xff) {
if (x1 != x0) {
pixman_image_composite32 (r->op, r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
x1 - x0, h);
}
pixman_image_composite32 (r->op, r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
spans[0].x, y,
len, h);
mask = (uint8_t *)pixman_image_get_data (r->mask);
x0 = spans[1].x;
} else if (spans[0].coverage == 0x0 &&
x1 - x0 > r->u.composite.run_length) {
pixman_image_composite32 (r->op, r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
x1 - x0, h);
mask = (uint8_t *)pixman_image_get_data (r->mask);
x0 = spans[1].x;
}else {
memset (mask, spans[0].coverage, --len);
mask += len;
}
}
x1 = spans[1].x;
spans++;
} while (--num_spans > 1);
if (x1 != x0) {
pixman_image_composite32 (r->op, r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
x1 - x0, h);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_inplace_opacity_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
uint8_t *mask;
int x0, x1;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
mask = (uint8_t *)pixman_image_get_data (r->mask);
x1 = x0 = spans[0].x;
do {
int len = spans[1].x - spans[0].x;
uint8_t m = mul8_8(spans[0].coverage, r->bpp);
*mask++ = m;
if (len > 1) {
if (m == 0 &&
x1 - x0 > r->u.composite.run_length) {
pixman_image_composite32 (r->op, r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
x1 - x0, h);
mask = (uint8_t *)pixman_image_get_data (r->mask);
x0 = spans[1].x;
}else {
memset (mask, m, --len);
mask += len;
}
}
x1 = spans[1].x;
spans++;
} while (--num_spans > 1);
if (x1 != x0) {
pixman_image_composite32 (r->op, r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
x1 - x0, h);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_inplace_src_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
uint8_t *m;
int x0;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
x0 = spans[0].x;
m = r->_buf;
do {
int len = spans[1].x - spans[0].x;
if (len >= r->u.composite.run_length && spans[0].coverage == 0xff) {
if (spans[0].x != x0) {
#if PIXMAN_HAS_OP_LERP
pixman_image_composite32 (PIXMAN_OP_LERP_SRC,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#else
pixman_image_composite32 (PIXMAN_OP_OUT_REVERSE,
r->mask, NULL, r->u.composite.dst,
0, 0,
0, 0,
x0, y,
spans[0].x - x0, h);
pixman_image_composite32 (PIXMAN_OP_ADD,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#endif
}
pixman_image_composite32 (PIXMAN_OP_SRC,
r->src, NULL, r->u.composite.dst,
spans[0].x + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
spans[0].x, y,
spans[1].x - spans[0].x, h);
m = r->_buf;
x0 = spans[1].x;
} else if (spans[0].coverage == 0x0) {
if (spans[0].x != x0) {
#if PIXMAN_HAS_OP_LERP
pixman_image_composite32 (PIXMAN_OP_LERP_SRC,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#else
pixman_image_composite32 (PIXMAN_OP_OUT_REVERSE,
r->mask, NULL, r->u.composite.dst,
0, 0,
0, 0,
x0, y,
spans[0].x - x0, h);
pixman_image_composite32 (PIXMAN_OP_ADD,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#endif
}
m = r->_buf;
x0 = spans[1].x;
} else {
*m++ = spans[0].coverage;
if (len > 1) {
memset (m, spans[0].coverage, --len);
m += len;
}
}
spans++;
} while (--num_spans > 1);
if (spans[0].x != x0) {
#if PIXMAN_HAS_OP_LERP
pixman_image_composite32 (PIXMAN_OP_LERP_SRC,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#else
pixman_image_composite32 (PIXMAN_OP_OUT_REVERSE,
r->mask, NULL, r->u.composite.dst,
0, 0,
0, 0,
x0, y,
spans[0].x - x0, h);
pixman_image_composite32 (PIXMAN_OP_ADD,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#endif
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_inplace_src_opacity_spans (void *abstract_renderer, int y, int h,
const cairo_half_open_span_t *spans,
unsigned num_spans)
{
cairo_image_span_renderer_t *r = abstract_renderer;
uint8_t *mask;
int x0;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
x0 = spans[0].x;
mask = (uint8_t *)pixman_image_get_data (r->mask);
do {
int len = spans[1].x - spans[0].x;
uint8_t m = mul8_8(spans[0].coverage, r->bpp);
if (m == 0) {
if (spans[0].x != x0) {
#if PIXMAN_HAS_OP_LERP
pixman_image_composite32 (PIXMAN_OP_LERP_SRC,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#else
pixman_image_composite32 (PIXMAN_OP_OUT_REVERSE,
r->mask, NULL, r->u.composite.dst,
0, 0,
0, 0,
x0, y,
spans[0].x - x0, h);
pixman_image_composite32 (PIXMAN_OP_ADD,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#endif
}
mask = (uint8_t *)pixman_image_get_data (r->mask);
x0 = spans[1].x;
} else {
*mask++ = m;
if (len > 1) {
memset (mask, m, --len);
mask += len;
}
}
spans++;
} while (--num_spans > 1);
if (spans[0].x != x0) {
#if PIXMAN_HAS_OP_LERP
pixman_image_composite32 (PIXMAN_OP_LERP_SRC,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#else
pixman_image_composite32 (PIXMAN_OP_OUT_REVERSE,
r->mask, NULL, r->u.composite.dst,
0, 0,
0, 0,
x0, y,
spans[0].x - x0, h);
pixman_image_composite32 (PIXMAN_OP_ADD,
r->src, r->mask, r->u.composite.dst,
x0 + r->u.composite.src_x,
y + r->u.composite.src_y,
0, 0,
x0, y,
spans[0].x - x0, h);
#endif
}
return CAIRO_STATUS_SUCCESS;
}
static void free_pixels (pixman_image_t *image, void *data)
{
free (data);
}
static cairo_int_status_t
inplace_renderer_init (cairo_image_span_renderer_t *r,
const cairo_composite_rectangles_t *composite,
cairo_antialias_t antialias,
cairo_bool_t needs_clip)
{
cairo_image_surface_t *dst = (cairo_image_surface_t *)composite->surface;
uint8_t *buf;
if (composite->mask_pattern.base.type != CAIRO_PATTERN_TYPE_SOLID)
return CAIRO_INT_STATUS_UNSUPPORTED;
r->base.render_rows = NULL;
r->bpp = composite->mask_pattern.solid.color.alpha_short >> 8;
if (composite->source_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID) {
const cairo_color_t *color;
color = &composite->source_pattern.solid.color;
if (composite->op == CAIRO_OPERATOR_CLEAR)
color = CAIRO_COLOR_TRANSPARENT;
if (fill_reduces_to_source (composite->op, color, dst, &r->u.fill.pixel)) {
/* Use plain C for the fill operations as the span length is
* typically small, too small to payback the startup overheads of
* using SSE2 etc.
*/
if (r->bpp == 0xff) {
switch (dst->format) {
case CAIRO_FORMAT_A8:
r->base.render_rows = _fill_a8_lerp_opaque_spans;
break;
case CAIRO_FORMAT_RGB24:
case CAIRO_FORMAT_ARGB32:
r->base.render_rows = _fill_xrgb32_lerp_opaque_spans;
break;
case CAIRO_FORMAT_A1:
case CAIRO_FORMAT_RGB16_565:
case CAIRO_FORMAT_RGB30:
case CAIRO_FORMAT_RGB96F:
case CAIRO_FORMAT_RGBA128F:
case CAIRO_FORMAT_INVALID:
default: break;
}
} else {
switch (dst->format) {
case CAIRO_FORMAT_A8:
r->base.render_rows = _fill_a8_lerp_spans;
break;
case CAIRO_FORMAT_RGB24:
case CAIRO_FORMAT_ARGB32:
r->base.render_rows = _fill_xrgb32_lerp_spans;
break;
case CAIRO_FORMAT_A1:
case CAIRO_FORMAT_RGB16_565:
case CAIRO_FORMAT_RGB30:
case CAIRO_FORMAT_RGB96F:
case CAIRO_FORMAT_RGBA128F:
case CAIRO_FORMAT_INVALID:
default: break;
}
}
r->u.fill.data = dst->data;
r->u.fill.stride = dst->stride;
}
} else if ((dst->format == CAIRO_FORMAT_ARGB32 || dst->format == CAIRO_FORMAT_RGB24) &&
(composite->op == CAIRO_OPERATOR_SOURCE ||
(composite->op == CAIRO_OPERATOR_OVER &&
(dst->base.is_clear || (dst->base.content & CAIRO_CONTENT_ALPHA) == 0))) &&
composite->source_pattern.base.type == CAIRO_PATTERN_TYPE_SURFACE &&
composite->source_pattern.surface.surface->backend->type == CAIRO_SURFACE_TYPE_IMAGE &&
to_image_surface(composite->source_pattern.surface.surface)->format == dst->format)
{
cairo_image_surface_t *src =
to_image_surface(composite->source_pattern.surface.surface);
int tx, ty;
if (_cairo_matrix_is_integer_translation(&composite->source_pattern.base.matrix,
&tx, &ty) &&
composite->bounded.x + tx >= 0 &&
composite->bounded.y + ty >= 0 &&
composite->bounded.x + composite->bounded.width + tx <= src->width &&
composite->bounded.y + composite->bounded.height + ty <= src->height) {
assert(PIXMAN_FORMAT_BPP(dst->pixman_format) == 32);
r->u.blit.stride = dst->stride;
r->u.blit.data = dst->data;
r->u.blit.src_stride = src->stride;
r->u.blit.src_data = src->data + src->stride * ty + tx * 4;
r->base.render_rows = _blit_xrgb32_lerp_spans;
}
}
if (r->base.render_rows == NULL) {
const cairo_pattern_t *src = &composite->source_pattern.base;
unsigned int width;
if (composite->is_bounded == 0)
return CAIRO_INT_STATUS_UNSUPPORTED;
r->base.render_rows = r->bpp == 0xff ? _inplace_spans : _inplace_opacity_spans;
width = (composite->bounded.width + 3) & ~3;
r->u.composite.run_length = 8;
if (src->type == CAIRO_PATTERN_TYPE_LINEAR ||
src->type == CAIRO_PATTERN_TYPE_RADIAL)
r->u.composite.run_length = 256;
if (dst->base.is_clear &&
(composite->op == CAIRO_OPERATOR_SOURCE ||
composite->op == CAIRO_OPERATOR_OVER ||
composite->op == CAIRO_OPERATOR_ADD)) {
r->op = PIXMAN_OP_SRC;
} else if (composite->op == CAIRO_OPERATOR_SOURCE) {
r->base.render_rows = r->bpp == 0xff ? _inplace_src_spans : _inplace_src_opacity_spans;
r->u.composite.mask_y = r->composite->unbounded.y;
width = (composite->unbounded.width + 3) & ~3;
} else if (composite->op == CAIRO_OPERATOR_CLEAR) {
r->op = PIXMAN_OP_OUT_REVERSE;
src = NULL;
} else {
r->op = _pixman_operator (composite->op);
}
r->src = _pixman_image_for_pattern (dst, src, FALSE,
&composite->bounded,
&composite->source_sample_area,
&r->u.composite.src_x, &r->u.composite.src_y);
if (unlikely (r->src == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
/* Create an effectively unbounded mask by repeating the single line */
buf = r->_buf;
if (width > SZ_BUF) {
buf = _cairo_malloc (width);
if (unlikely (buf == NULL)) {
pixman_image_unref (r->src);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
}
r->mask = pixman_image_create_bits (PIXMAN_a8,
width, composite->unbounded.height,
(uint32_t *)buf, 0);
if (unlikely (r->mask == NULL)) {
pixman_image_unref (r->src);
if (buf != r->_buf)
free (buf);
return _cairo_error(CAIRO_STATUS_NO_MEMORY);
}
if (buf != r->_buf)
pixman_image_set_destroy_function (r->mask, free_pixels, buf);
r->u.composite.dst = dst->pixman_image;
}
return CAIRO_INT_STATUS_SUCCESS;
}
static cairo_int_status_t
span_renderer_init (cairo_abstract_span_renderer_t *_r,
const cairo_composite_rectangles_t *composite,
cairo_antialias_t antialias,
cairo_bool_t needs_clip)
{
cairo_image_span_renderer_t *r = (cairo_image_span_renderer_t *)_r;
cairo_image_surface_t *dst = (cairo_image_surface_t *)composite->surface;
const cairo_pattern_t *source = &composite->source_pattern.base;
cairo_operator_t op = composite->op;
cairo_int_status_t status;
TRACE ((stderr, "%s: antialias=%d, needs_clip=%d\n", __FUNCTION__,
antialias, needs_clip));
if (needs_clip)
return CAIRO_INT_STATUS_UNSUPPORTED;
r->composite = composite;
r->mask = NULL;
r->src = NULL;
r->base.finish = NULL;
status = mono_renderer_init (r, composite, antialias, needs_clip);
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
return status;
status = inplace_renderer_init (r, composite, antialias, needs_clip);
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
return status;
r->bpp = 0;
if (op == CAIRO_OPERATOR_CLEAR) {
#if PIXMAN_HAS_OP_LERP
op = PIXMAN_OP_LERP_CLEAR;
#else
source = &_cairo_pattern_white.base;
op = PIXMAN_OP_OUT_REVERSE;
#endif
} else if (dst->base.is_clear &&
(op == CAIRO_OPERATOR_SOURCE ||
op == CAIRO_OPERATOR_OVER ||
op == CAIRO_OPERATOR_ADD)) {
op = PIXMAN_OP_SRC;
} else if (op == CAIRO_OPERATOR_SOURCE) {
if (_cairo_pattern_is_opaque (&composite->source_pattern.base,
&composite->source_sample_area))
{
op = PIXMAN_OP_OVER;
}
else
{
#if PIXMAN_HAS_OP_LERP
op = PIXMAN_OP_LERP_SRC;
#else
return CAIRO_INT_STATUS_UNSUPPORTED;
#endif
}
} else {
op = _pixman_operator (op);
}
r->op = op;
r->src = _pixman_image_for_pattern (dst, source, FALSE,
&composite->unbounded,
&composite->source_sample_area,
&r->u.mask.src_x, &r->u.mask.src_y);
if (unlikely (r->src == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
r->opacity = 1.0;
if (composite->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID) {
r->opacity = composite->mask_pattern.solid.color.alpha;
} else {
pixman_image_t *mask;
int mask_x, mask_y;
mask = _pixman_image_for_pattern (dst,
&composite->mask_pattern.base,
TRUE,
&composite->unbounded,
&composite->mask_sample_area,
&mask_x, &mask_y);
if (unlikely (mask == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
/* XXX Component-alpha? */
if ((dst->base.content & CAIRO_CONTENT_COLOR) == 0 &&
_cairo_pattern_is_opaque (source, &composite->source_sample_area))
{
pixman_image_unref (r->src);
r->src = mask;
r->u.mask.src_x = mask_x;
r->u.mask.src_y = mask_y;
mask = NULL;
}
if (mask) {
pixman_image_unref (mask);
return CAIRO_INT_STATUS_UNSUPPORTED;
}
}
r->u.mask.extents = composite->unbounded;
r->u.mask.stride = (r->u.mask.extents.width + 3) & ~3;
if (r->u.mask.extents.height * r->u.mask.stride > SZ_BUF) {
r->mask = pixman_image_create_bits (PIXMAN_a8,
r->u.mask.extents.width,
r->u.mask.extents.height,
NULL, 0);
r->base.render_rows = _cairo_image_spans;
r->base.finish = NULL;
} else {
r->mask = pixman_image_create_bits (PIXMAN_a8,
r->u.mask.extents.width,
r->u.mask.extents.height,
(uint32_t *)r->_buf, r->u.mask.stride);
r->base.render_rows = _cairo_image_spans_and_zero;
r->base.finish = _cairo_image_finish_spans_and_zero;
}
if (unlikely (r->mask == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
r->u.mask.data = (uint8_t *) pixman_image_get_data (r->mask);
r->u.mask.stride = pixman_image_get_stride (r->mask);
r->u.mask.extents.height += r->u.mask.extents.y;
return CAIRO_STATUS_SUCCESS;
}
static void
span_renderer_fini (cairo_abstract_span_renderer_t *_r,
cairo_int_status_t status)
{
cairo_image_span_renderer_t *r = (cairo_image_span_renderer_t *) _r;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
if (r->base.finish)
r->base.finish (r);
}
if (likely (status == CAIRO_INT_STATUS_SUCCESS && r->bpp == 0)) {
const cairo_composite_rectangles_t *composite = r->composite;
pixman_image_composite32 (r->op, r->src, r->mask,
to_pixman_image (composite->surface),
composite->unbounded.x + r->u.mask.src_x,
composite->unbounded.y + r->u.mask.src_y,
0, 0,
composite->unbounded.x,
composite->unbounded.y,
composite->unbounded.width,
composite->unbounded.height);
}
if (r->src)
pixman_image_unref (r->src);
if (r->mask)
pixman_image_unref (r->mask);
}
#endif
const cairo_compositor_t *
_cairo_image_spans_compositor_get (void)
{
static cairo_atomic_once_t once = CAIRO_ATOMIC_ONCE_INIT;
static cairo_spans_compositor_t spans;
static cairo_compositor_t shape;
if (_cairo_atomic_init_once_enter(&once)) {
_cairo_shape_mask_compositor_init (&shape,
_cairo_image_traps_compositor_get());
shape.glyphs = NULL;
_cairo_spans_compositor_init (&spans, &shape);
spans.flags = 0;
#if PIXMAN_HAS_OP_LERP
spans.flags |= CAIRO_SPANS_COMPOSITOR_HAS_LERP;
#endif
//spans.acquire = acquire;
//spans.release = release;
spans.fill_boxes = fill_boxes;
spans.draw_image_boxes = draw_image_boxes;
//spans.copy_boxes = copy_boxes;
spans.pattern_to_surface = _cairo_image_source_create_for_pattern;
//spans.check_composite_boxes = check_composite_boxes;
spans.composite_boxes = composite_boxes;
//spans.check_span_renderer = check_span_renderer;
spans.renderer_init = span_renderer_init;
spans.renderer_fini = span_renderer_fini;
_cairo_atomic_init_once_leave(&once);
}
return &spans.base;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-image-info-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2008 Adrian Johnson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Adrian Johnson.
*
* Contributor(s):
* Adrian Johnson <ajohnson@redneon.com>
*/
#ifndef CAIRO_IMAGE_INFO_PRIVATE_H
#define CAIRO_IMAGE_INFO_PRIVATE_H
#include "cairoint.h"
typedef struct _cairo_image_info {
int width;
int height;
int num_components;
int bits_per_component;
} cairo_image_info_t;
cairo_private cairo_int_status_t
_cairo_image_info_get_jpeg_info (cairo_image_info_t *info,
const unsigned char *data,
long length);
cairo_private cairo_int_status_t
_cairo_image_info_get_jpx_info (cairo_image_info_t *info,
const unsigned char *data,
unsigned long length);
cairo_private cairo_int_status_t
_cairo_image_info_get_png_info (cairo_image_info_t *info,
const unsigned char *data,
unsigned long length);
cairo_private cairo_int_status_t
_cairo_image_info_get_jbig2_info (cairo_image_info_t *info,
const unsigned char *data,
unsigned long length);
#endif /* CAIRO_IMAGE_INFO_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-image-info.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2008 Adrian Johnson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Adrian Johnson.
*
* Contributor(s):
* Adrian Johnson <ajohnson@redneon.com>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-image-info-private.h"
/* JPEG (image/jpeg)
*
* http://www.w3.org/Graphics/JPEG/itu-t81.pdf
*/
/* Markers with no parameters. All other markers are followed by a two
* byte length of the parameters. */
#define TEM 0x01
#define RST_begin 0xd0
#define RST_end 0xd7
#define SOI 0xd8
#define EOI 0xd9
/* Start of frame markers. */
#define SOF0 0xc0
#define SOF1 0xc1
#define SOF2 0xc2
#define SOF3 0xc3
#define SOF5 0xc5
#define SOF6 0xc6
#define SOF7 0xc7
#define SOF9 0xc9
#define SOF10 0xca
#define SOF11 0xcb
#define SOF13 0xcd
#define SOF14 0xce
#define SOF15 0xcf
static const unsigned char *
_jpeg_skip_segment (const unsigned char *p)
{
int len;
p++;
len = (p[0] << 8) | p[1];
return p + len;
}
static void
_jpeg_extract_info (cairo_image_info_t *info, const unsigned char *p)
{
info->width = (p[6] << 8) + p[7];
info->height = (p[4] << 8) + p[5];
info->num_components = p[8];
info->bits_per_component = p[3];
}
cairo_int_status_t
_cairo_image_info_get_jpeg_info (cairo_image_info_t *info,
const unsigned char *data,
long length)
{
const unsigned char *p = data;
while (p + 1 < data + length) {
if (*p != 0xff)
return CAIRO_INT_STATUS_UNSUPPORTED;
p++;
switch (*p) {
/* skip fill bytes */
case 0xff:
p++;
break;
case TEM:
case SOI:
case EOI:
p++;
break;
case SOF0:
case SOF1:
case SOF2:
case SOF3:
case SOF5:
case SOF6:
case SOF7:
case SOF9:
case SOF10:
case SOF11:
case SOF13:
case SOF14:
case SOF15:
/* Start of frame found. Extract the image parameters. */
if (p + 8 > data + length)
return CAIRO_INT_STATUS_UNSUPPORTED;
_jpeg_extract_info (info, p);
return CAIRO_STATUS_SUCCESS;
default:
if (*p >= RST_begin && *p <= RST_end) {
p++;
break;
}
if (p + 3 > data + length)
return CAIRO_INT_STATUS_UNSUPPORTED;
p = _jpeg_skip_segment (p);
break;
}
}
return CAIRO_STATUS_SUCCESS;
}
/* JPEG 2000 (image/jp2)
*
* http://www.jpeg.org/public/15444-1annexi.pdf
*/
#define JPX_FILETYPE 0x66747970
#define JPX_JP2_HEADER 0x6A703268
#define JPX_IMAGE_HEADER 0x69686472
static const unsigned char _jpx_signature[] = {
0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a
};
static const unsigned char *
_jpx_next_box (const unsigned char *p)
{
return p + get_unaligned_be32 (p);
}
static const unsigned char *
_jpx_get_box_contents (const unsigned char *p)
{
return p + 8;
}
static cairo_bool_t
_jpx_match_box (const unsigned char *p, const unsigned char *end, uint32_t type)
{
uint32_t length;
if (p + 8 < end) {
length = get_unaligned_be32 (p);
if (get_unaligned_be32 (p + 4) == type && p + length < end)
return TRUE;
}
return FALSE;
}
static const unsigned char *
_jpx_find_box (const unsigned char *p, const unsigned char *end, uint32_t type)
{
while (p < end) {
if (_jpx_match_box (p, end, type))
return p;
p = _jpx_next_box (p);
}
return NULL;
}
static void
_jpx_extract_info (const unsigned char *p, cairo_image_info_t *info)
{
info->height = get_unaligned_be32 (p);
info->width = get_unaligned_be32 (p + 4);
info->num_components = (p[8] << 8) + p[9];
info->bits_per_component = p[10];
}
cairo_int_status_t
_cairo_image_info_get_jpx_info (cairo_image_info_t *info,
const unsigned char *data,
unsigned long length)
{
const unsigned char *p = data;
const unsigned char *end = data + length;
/* First 12 bytes must be the JPEG 2000 signature box. */
if (length < ARRAY_LENGTH(_jpx_signature) ||
memcmp(p, _jpx_signature, ARRAY_LENGTH(_jpx_signature)) != 0)
return CAIRO_INT_STATUS_UNSUPPORTED;
p += ARRAY_LENGTH(_jpx_signature);
/* Next box must be a File Type Box */
if (! _jpx_match_box (p, end, JPX_FILETYPE))
return CAIRO_INT_STATUS_UNSUPPORTED;
p = _jpx_next_box (p);
/* Locate the JP2 header box. */
p = _jpx_find_box (p, end, JPX_JP2_HEADER);
if (!p)
return CAIRO_INT_STATUS_UNSUPPORTED;
/* Step into the JP2 header box. First box must be the Image
* Header */
p = _jpx_get_box_contents (p);
if (! _jpx_match_box (p, end, JPX_IMAGE_HEADER))
return CAIRO_INT_STATUS_UNSUPPORTED;
/* Get the image info */
p = _jpx_get_box_contents (p);
_jpx_extract_info (p, info);
return CAIRO_STATUS_SUCCESS;
}
/* PNG (image/png)
*
* http://www.w3.org/TR/2003/REC-PNG-20031110/
*/
#define PNG_IHDR 0x49484452
static const unsigned char _png_magic[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
cairo_int_status_t
_cairo_image_info_get_png_info (cairo_image_info_t *info,
const unsigned char *data,
unsigned long length)
{
const unsigned char *p = data;
const unsigned char *end = data + length;
if (length < 8 || memcmp (data, _png_magic, 8) != 0)
return CAIRO_INT_STATUS_UNSUPPORTED;
p += 8;
/* The first chunk must be IDHR. IDHR has 13 bytes of data plus
* the 12 bytes of overhead for the chunk. */
if (p + 13 + 12 > end)
return CAIRO_INT_STATUS_UNSUPPORTED;
p += 4;
if (get_unaligned_be32 (p) != PNG_IHDR)
return CAIRO_INT_STATUS_UNSUPPORTED;
p += 4;
info->width = get_unaligned_be32 (p);
p += 4;
info->height = get_unaligned_be32 (p);
return CAIRO_STATUS_SUCCESS;
}
static const unsigned char *
_jbig2_find_data_end (const unsigned char *p,
const unsigned char *end,
int type)
{
unsigned char end_seq[2];
int mmr;
/* Segments of type "Immediate generic region" may have an
* unspecified data length. The JBIG2 specification specifies the
* method to find the end of the data for these segments. */
if (type == 36 || type == 38 || type == 39) {
if (p + 18 < end) {
mmr = p[17] & 0x01;
if (mmr) {
/* MMR encoding ends with 0x00, 0x00 */
end_seq[0] = 0x00;
end_seq[1] = 0x00;
} else {
/* Template encoding ends with 0xff, 0xac */
end_seq[0] = 0xff;
end_seq[1] = 0xac;
}
p += 18;
while (p < end) {
if (p[0] == end_seq[0] && p[1] == end_seq[1]) {
/* Skip the 2 terminating bytes and the 4 byte row count that follows. */
p += 6;
if (p < end)
return p;
}
p++;
}
}
}
return NULL;
}
static const unsigned char *
_jbig2_get_next_segment (const unsigned char *p,
const unsigned char *end,
int *type,
const unsigned char **data,
unsigned long *data_len)
{
unsigned long seg_num;
cairo_bool_t big_page_size;
int num_segs;
int ref_seg_bytes;
int referred_size;
if (p + 6 >= end)
return NULL;
seg_num = get_unaligned_be32 (p);
*type = p[4] & 0x3f;
big_page_size = (p[4] & 0x40) != 0;
p += 5;
num_segs = p[0] >> 5;
if (num_segs == 7) {
num_segs = get_unaligned_be32 (p) & 0x1fffffff;
ref_seg_bytes = 4 + ((num_segs + 1)/8);
} else {
ref_seg_bytes = 1;
}
p += ref_seg_bytes;
if (seg_num <= 256)
referred_size = 1;
else if (seg_num <= 65536)
referred_size = 2;
else
referred_size = 4;
p += num_segs * referred_size;
p += big_page_size ? 4 : 1;
if (p + 4 >= end)
return NULL;
*data_len = get_unaligned_be32 (p);
p += 4;
*data = p;
if (*data_len == 0xffffffff) {
/* if data length is -1 we have to scan through the data to find the end */
p = _jbig2_find_data_end (*data, end, *type);
if (!p || p >= end)
return NULL;
*data_len = p - *data;
} else {
p += *data_len;
}
if (p < end)
return p;
else
return NULL;
}
static void
_jbig2_extract_info (cairo_image_info_t *info, const unsigned char *p)
{
info->width = get_unaligned_be32 (p);
info->height = get_unaligned_be32 (p + 4);
info->num_components = 1;
info->bits_per_component = 1;
}
cairo_int_status_t
_cairo_image_info_get_jbig2_info (cairo_image_info_t *info,
const unsigned char *data,
unsigned long length)
{
const unsigned char *p = data;
const unsigned char *end = data + length;
int seg_type;
const unsigned char *seg_data;
unsigned long seg_data_len;
while (p && p < end) {
p = _jbig2_get_next_segment (p, end, &seg_type, &seg_data, &seg_data_len);
if (p && seg_type == 48 && seg_data_len > 8) {
/* page information segment */
_jbig2_extract_info (info, seg_data);
return CAIRO_STATUS_SUCCESS;
}
}
return CAIRO_INT_STATUS_UNSUPPORTED;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-image-source.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2003 University of Southern California
* Copyright © 2009,2010,2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
/* The purpose of this file/surface is to simply translate a pattern
* to a pixman_image_t and thence to feed it back to the general
* compositor interface.
*/
#include "cairoint.h"
#include "cairo-image-surface-private.h"
#include "cairo-compositor-private.h"
#include "cairo-error-private.h"
#include "cairo-pattern-inline.h"
#include "cairo-paginated-private.h"
#include "cairo-recording-surface-private.h"
#include "cairo-surface-observer-private.h"
#include "cairo-surface-snapshot-inline.h"
#include "cairo-surface-subsurface-private.h"
#define PIXMAN_MAX_INT ((pixman_fixed_1 >> 1) - pixman_fixed_e) /* need to ensure deltas also fit */
#if CAIRO_NO_MUTEX
#define PIXMAN_HAS_ATOMIC_OPS 1
#endif
#if PIXMAN_HAS_ATOMIC_OPS
static pixman_image_t *__pixman_transparent_image;
static pixman_image_t *__pixman_black_image;
static pixman_image_t *__pixman_white_image;
static pixman_image_t *
_pixman_transparent_image (void)
{
pixman_image_t *image;
TRACE ((stderr, "%s\n", __FUNCTION__));
image = __pixman_transparent_image;
if (unlikely (image == NULL)) {
pixman_color_t color;
color.red = 0x00;
color.green = 0x00;
color.blue = 0x00;
color.alpha = 0x00;
image = pixman_image_create_solid_fill (&color);
if (unlikely (image == NULL))
return NULL;
if (_cairo_atomic_ptr_cmpxchg (&__pixman_transparent_image,
NULL, image))
{
pixman_image_ref (image);
}
} else {
pixman_image_ref (image);
}
return image;
}
static pixman_image_t *
_pixman_black_image (void)
{
pixman_image_t *image;
TRACE ((stderr, "%s\n", __FUNCTION__));
image = __pixman_black_image;
if (unlikely (image == NULL)) {
pixman_color_t color;
color.red = 0x00;
color.green = 0x00;
color.blue = 0x00;
color.alpha = 0xffff;
image = pixman_image_create_solid_fill (&color);
if (unlikely (image == NULL))
return NULL;
if (_cairo_atomic_ptr_cmpxchg (&__pixman_black_image,
NULL, image))
{
pixman_image_ref (image);
}
} else {
pixman_image_ref (image);
}
return image;
}
static pixman_image_t *
_pixman_white_image (void)
{
pixman_image_t *image;
TRACE ((stderr, "%s\n", __FUNCTION__));
image = __pixman_white_image;
if (unlikely (image == NULL)) {
pixman_color_t color;
color.red = 0xffff;
color.green = 0xffff;
color.blue = 0xffff;
color.alpha = 0xffff;
image = pixman_image_create_solid_fill (&color);
if (unlikely (image == NULL))
return NULL;
if (_cairo_atomic_ptr_cmpxchg (&__pixman_white_image,
NULL, image))
{
pixman_image_ref (image);
}
} else {
pixman_image_ref (image);
}
return image;
}
static uint32_t
hars_petruska_f54_1_random (void)
{
#define rol(x,k) ((x << k) | (x >> (32-k)))
static uint32_t x;
return x = (x ^ rol (x, 5) ^ rol (x, 24)) + 0x37798849;
#undef rol
}
static struct {
cairo_color_t color;
pixman_image_t *image;
} cache[16];
static int n_cached;
#else /* !PIXMAN_HAS_ATOMIC_OPS */
static pixman_image_t *
_pixman_transparent_image (void)
{
TRACE ((stderr, "%s\n", __FUNCTION__));
return _pixman_image_for_color (CAIRO_COLOR_TRANSPARENT);
}
static pixman_image_t *
_pixman_black_image (void)
{
TRACE ((stderr, "%s\n", __FUNCTION__));
return _pixman_image_for_color (CAIRO_COLOR_BLACK);
}
static pixman_image_t *
_pixman_white_image (void)
{
TRACE ((stderr, "%s\n", __FUNCTION__));
return _pixman_image_for_color (CAIRO_COLOR_WHITE);
}
#endif /* !PIXMAN_HAS_ATOMIC_OPS */
pixman_image_t *
_pixman_image_for_color (const cairo_color_t *cairo_color)
{
pixman_color_t color;
pixman_image_t *image;
#if PIXMAN_HAS_ATOMIC_OPS
int i;
if (CAIRO_COLOR_IS_CLEAR (cairo_color))
return _pixman_transparent_image ();
if (CAIRO_COLOR_IS_OPAQUE (cairo_color)) {
if (cairo_color->red_short <= 0x00ff &&
cairo_color->green_short <= 0x00ff &&
cairo_color->blue_short <= 0x00ff)
{
return _pixman_black_image ();
}
if (cairo_color->red_short >= 0xff00 &&
cairo_color->green_short >= 0xff00 &&
cairo_color->blue_short >= 0xff00)
{
return _pixman_white_image ();
}
}
CAIRO_MUTEX_LOCK (_cairo_image_solid_cache_mutex);
for (i = 0; i < n_cached; i++) {
if (_cairo_color_equal (&cache[i].color, cairo_color)) {
image = pixman_image_ref (cache[i].image);
goto UNLOCK;
}
}
#endif
color.red = cairo_color->red_short;
color.green = cairo_color->green_short;
color.blue = cairo_color->blue_short;
color.alpha = cairo_color->alpha_short;
image = pixman_image_create_solid_fill (&color);
#if PIXMAN_HAS_ATOMIC_OPS
if (image == NULL)
goto UNLOCK;
if (n_cached < ARRAY_LENGTH (cache)) {
i = n_cached++;
} else {
i = hars_petruska_f54_1_random () % ARRAY_LENGTH (cache);
pixman_image_unref (cache[i].image);
}
cache[i].image = pixman_image_ref (image);
cache[i].color = *cairo_color;
UNLOCK:
CAIRO_MUTEX_UNLOCK (_cairo_image_solid_cache_mutex);
#endif
return image;
}
void
_cairo_image_reset_static_data (void)
{
#if PIXMAN_HAS_ATOMIC_OPS
while (n_cached)
pixman_image_unref (cache[--n_cached].image);
if (__pixman_transparent_image) {
pixman_image_unref (__pixman_transparent_image);
__pixman_transparent_image = NULL;
}
if (__pixman_black_image) {
pixman_image_unref (__pixman_black_image);
__pixman_black_image = NULL;
}
if (__pixman_white_image) {
pixman_image_unref (__pixman_white_image);
__pixman_white_image = NULL;
}
#endif
}
static pixman_image_t *
_pixman_image_for_gradient (const cairo_gradient_pattern_t *pattern,
const cairo_rectangle_int_t *extents,
int *ix, int *iy)
{
pixman_image_t *pixman_image;
pixman_gradient_stop_t pixman_stops_static[2];
pixman_gradient_stop_t *pixman_stops = pixman_stops_static;
pixman_transform_t pixman_transform;
cairo_matrix_t matrix;
cairo_circle_double_t extremes[2];
pixman_point_fixed_t p1, p2;
unsigned int i;
cairo_int_status_t status;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (pattern->n_stops > ARRAY_LENGTH(pixman_stops_static)) {
pixman_stops = _cairo_malloc_ab (pattern->n_stops,
sizeof(pixman_gradient_stop_t));
if (unlikely (pixman_stops == NULL))
return NULL;
}
for (i = 0; i < pattern->n_stops; i++) {
pixman_stops[i].x = _cairo_fixed_16_16_from_double (pattern->stops[i].offset);
pixman_stops[i].color.red = pattern->stops[i].color.red_short;
pixman_stops[i].color.green = pattern->stops[i].color.green_short;
pixman_stops[i].color.blue = pattern->stops[i].color.blue_short;
pixman_stops[i].color.alpha = pattern->stops[i].color.alpha_short;
}
_cairo_gradient_pattern_fit_to_range (pattern, PIXMAN_MAX_INT >> 1, &matrix, extremes);
p1.x = _cairo_fixed_16_16_from_double (extremes[0].center.x);
p1.y = _cairo_fixed_16_16_from_double (extremes[0].center.y);
p2.x = _cairo_fixed_16_16_from_double (extremes[1].center.x);
p2.y = _cairo_fixed_16_16_from_double (extremes[1].center.y);
if (pattern->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
pixman_image = pixman_image_create_linear_gradient (&p1, &p2,
pixman_stops,
pattern->n_stops);
} else {
pixman_fixed_t r1, r2;
r1 = _cairo_fixed_16_16_from_double (extremes[0].radius);
r2 = _cairo_fixed_16_16_from_double (extremes[1].radius);
pixman_image = pixman_image_create_radial_gradient (&p1, &p2, r1, r2,
pixman_stops,
pattern->n_stops);
}
if (pixman_stops != pixman_stops_static)
free (pixman_stops);
if (unlikely (pixman_image == NULL))
return NULL;
*ix = *iy = 0;
status = _cairo_matrix_to_pixman_matrix_offset (&matrix, pattern->base.filter,
extents->x + extents->width/2.,
extents->y + extents->height/2.,
&pixman_transform, ix, iy);
if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
if (unlikely (status != CAIRO_INT_STATUS_SUCCESS) ||
! pixman_image_set_transform (pixman_image, &pixman_transform))
{
pixman_image_unref (pixman_image);
return NULL;
}
}
{
pixman_repeat_t pixman_repeat;
switch (pattern->base.extend) {
default:
case CAIRO_EXTEND_NONE:
pixman_repeat = PIXMAN_REPEAT_NONE;
break;
case CAIRO_EXTEND_REPEAT:
pixman_repeat = PIXMAN_REPEAT_NORMAL;
break;
case CAIRO_EXTEND_REFLECT:
pixman_repeat = PIXMAN_REPEAT_REFLECT;
break;
case CAIRO_EXTEND_PAD:
pixman_repeat = PIXMAN_REPEAT_PAD;
break;
}
pixman_image_set_repeat (pixman_image, pixman_repeat);
}
return pixman_image;
}
static pixman_image_t *
_pixman_image_for_mesh (const cairo_mesh_pattern_t *pattern,
const cairo_rectangle_int_t *extents,
int *tx, int *ty)
{
pixman_image_t *image;
int width, height;
TRACE ((stderr, "%s\n", __FUNCTION__));
*tx = -extents->x;
*ty = -extents->y;
width = extents->width;
height = extents->height;
image = pixman_image_create_bits (PIXMAN_a8r8g8b8, width, height, NULL, 0);
if (unlikely (image == NULL))
return NULL;
_cairo_mesh_pattern_rasterize (pattern,
pixman_image_get_data (image),
width, height,
pixman_image_get_stride (image),
*tx, *ty);
return image;
}
struct acquire_source_cleanup {
cairo_surface_t *surface;
cairo_image_surface_t *image;
void *image_extra;
};
static void
_acquire_source_cleanup (pixman_image_t *pixman_image,
void *closure)
{
struct acquire_source_cleanup *data = closure;
_cairo_surface_release_source_image (data->surface,
data->image,
data->image_extra);
free (data);
}
static void
_defer_free_cleanup (pixman_image_t *pixman_image,
void *closure)
{
cairo_surface_destroy (closure);
}
static uint16_t
expand_channel (uint16_t v, uint32_t bits)
{
int offset = 16 - bits;
while (offset > 0) {
v |= v >> bits;
offset -= bits;
bits += bits;
}
return v;
}
static pixman_image_t *
_pixel_to_solid (cairo_image_surface_t *image, int x, int y)
{
uint32_t pixel;
float *rgba;
pixman_color_t color;
TRACE ((stderr, "%s\n", __FUNCTION__));
switch (image->format) {
default:
case CAIRO_FORMAT_INVALID:
ASSERT_NOT_REACHED;
return NULL;
case CAIRO_FORMAT_A1:
pixel = *(uint8_t *) (image->data + y * image->stride + x/8);
return pixel & (1 << (x&7)) ? _pixman_black_image () : _pixman_transparent_image ();
case CAIRO_FORMAT_A8:
color.alpha = *(uint8_t *) (image->data + y * image->stride + x);
color.alpha |= color.alpha << 8;
if (color.alpha == 0)
return _pixman_transparent_image ();
if (color.alpha == 0xffff)
return _pixman_black_image ();
color.red = color.green = color.blue = 0;
return pixman_image_create_solid_fill (&color);
case CAIRO_FORMAT_RGB16_565:
pixel = *(uint16_t *) (image->data + y * image->stride + 2 * x);
if (pixel == 0)
return _pixman_black_image ();
if (pixel == 0xffff)
return _pixman_white_image ();
color.alpha = 0xffff;
color.red = expand_channel ((pixel >> 11 & 0x1f) << 11, 5);
color.green = expand_channel ((pixel >> 5 & 0x3f) << 10, 6);
color.blue = expand_channel ((pixel & 0x1f) << 11, 5);
return pixman_image_create_solid_fill (&color);
case CAIRO_FORMAT_RGB30:
pixel = *(uint32_t *) (image->data + y * image->stride + 4 * x);
pixel &= 0x3fffffff; /* ignore alpha bits */
if (pixel == 0)
return _pixman_black_image ();
if (pixel == 0x3fffffff)
return _pixman_white_image ();
/* convert 10bpc to 16bpc */
color.alpha = 0xffff;
color.red = expand_channel((pixel >> 20) & 0x3fff, 10);
color.green = expand_channel((pixel >> 10) & 0x3fff, 10);
color.blue = expand_channel(pixel & 0x3fff, 10);
return pixman_image_create_solid_fill (&color);
case CAIRO_FORMAT_ARGB32:
case CAIRO_FORMAT_RGB24:
pixel = *(uint32_t *) (image->data + y * image->stride + 4 * x);
color.alpha = image->format == CAIRO_FORMAT_ARGB32 ? (pixel >> 24) | (pixel >> 16 & 0xff00) : 0xffff;
if (color.alpha == 0)
return _pixman_transparent_image ();
if (pixel == 0xffffffff)
return _pixman_white_image ();
if (color.alpha == 0xffff && (pixel & 0xffffff) == 0)
return _pixman_black_image ();
color.red = (pixel >> 16 & 0xff) | (pixel >> 8 & 0xff00);
color.green = (pixel >> 8 & 0xff) | (pixel & 0xff00);
color.blue = (pixel & 0xff) | (pixel << 8 & 0xff00);
return pixman_image_create_solid_fill (&color);
case CAIRO_FORMAT_RGB96F:
case CAIRO_FORMAT_RGBA128F:
if (image->format == CAIRO_FORMAT_RGBA128F)
{
rgba = (float *)&image->data[y * image->stride + 16 * x];
color.alpha = 65535.f * rgba[3];
if (color.alpha == 0)
return _pixman_transparent_image ();
}
else
{
rgba = (float *)&image->data[y * image->stride + 12 * x];
color.alpha = 0xffff;
}
if (color.alpha == 0xffff && rgba[0] == 0.f && rgba[1] == 0.f && rgba[2] == 0.f)
return _pixman_black_image ();
if (color.alpha == 0xffff && rgba[0] == 1.f && rgba[1] == 1.f && rgba[2] == 1.f)
return _pixman_white_image ();
color.red = rgba[0] * 65535.f;
color.green = rgba[1] * 65535.f;
color.blue = rgba[2] * 65535.f;
return pixman_image_create_solid_fill (&color);
}
}
/* ========================================================================== */
/* Index into filter table */
typedef enum
{
KERNEL_IMPULSE,
KERNEL_BOX,
KERNEL_LINEAR,
KERNEL_MITCHELL,
KERNEL_NOTCH,
KERNEL_CATMULL_ROM,
KERNEL_LANCZOS3,
KERNEL_LANCZOS3_STRETCHED,
KERNEL_TENT
} kernel_t;
/* Produce contribution of a filter of size r for pixel centered on x.
For a typical low-pass function this evaluates the function at x/r.
If the frequency is higher than 1/2, such as when r is less than 1,
this may need to integrate several samples, see cubic for examples.
*/
typedef double (* kernel_func_t) (double x, double r);
/* Return maximum number of pixels that will be non-zero. Except for
impluse this is the maximum of 2 and the width of the non-zero part
of the filter rounded up to the next integer.
*/
typedef int (* kernel_width_func_t) (double r);
/* Table of filters */
typedef struct
{
kernel_t kernel;
kernel_func_t func;
kernel_width_func_t width;
} filter_info_t;
/* PIXMAN_KERNEL_IMPULSE: Returns pixel nearest the center. This
matches PIXMAN_FILTER_NEAREST. This is useful if you wish to
combine the result of nearest in one direction with another filter
in the other.
*/
static double
impulse_kernel (double x, double r)
{
return 1;
}
static int
impulse_width (double r)
{
return 1;
}
/* PIXMAN_KERNEL_BOX: Intersection of a box of width r with square
pixels. This is the smallest possible filter such that the output
image contains an equal contribution from all the input
pixels. Lots of software uses this. The function is a trapazoid of
width r+1, not a box.
When r == 1.0, PIXMAN_KERNEL_BOX, PIXMAN_KERNEL_LINEAR, and
PIXMAN_KERNEL_TENT all produce the same filter, allowing
them to be exchanged at this point.
*/
static double
box_kernel (double x, double r)
{
return MAX (0.0, MIN (MIN (r, 1.0),
MIN ((r + 1) / 2 - x, (r + 1) / 2 + x)));
}
static int
box_width (double r)
{
return r < 1.0 ? 2 : ceil(r + 1);
}
/* PIXMAN_KERNEL_LINEAR: Weighted sum of the two pixels nearest the
center, or a triangle of width 2. This matches
PIXMAN_FILTER_BILINEAR. This is useful if you wish to combine the
result of bilinear in one direction with another filter in the
other. This is not a good filter if r > 1. You may actually want
PIXMAN_FILTER_TENT.
When r == 1.0, PIXMAN_KERNEL_BOX, PIXMAN_KERNEL_LINEAR, and
PIXMAN_KERNEL_TENT all produce the same filter, allowing
them to be exchanged at this point.
*/
static double
linear_kernel (double x, double r)
{
return MAX (1.0 - fabs(x), 0.0);
}
static int
linear_width (double r)
{
return 2;
}
/* Cubic functions described in the Mitchell-Netravali paper.
http://mentallandscape.com/Papers_siggraph88.pdf. This describes
all possible cubic functions that can be used for sampling.
*/
static double
general_cubic (double x, double r, double B, double C)
{
double ax;
if (r < 1.0)
return
general_cubic(x * 2 - .5, r * 2, B, C) +
general_cubic(x * 2 + .5, r * 2, B, C);
ax = fabs (x / r);
if (ax < 1)
{
return (((12 - 9 * B - 6 * C) * ax +
(-18 + 12 * B + 6 * C)) * ax * ax +
(6 - 2 * B)) / 6;
}
else if (ax < 2)
{
return ((((-B - 6 * C) * ax +
(6 * B + 30 * C)) * ax +
(-12 * B - 48 * C)) * ax +
(8 * B + 24 * C)) / 6;
}
else
{
return 0.0;
}
}
static int
cubic_width (double r)
{
return MAX (2, ceil (r * 4));
}
/* PIXMAN_KERNEL_CATMULL_ROM: Catmull-Rom interpolation. Often called
"cubic interpolation", "b-spline", or just "cubic" by other
software. This filter has negative values so it can produce ringing
and output pixels outside the range of input pixels. This is very
close to lanczos2 so there is no reason to supply that as well.
*/
static double
cubic_kernel (double x, double r)
{
return general_cubic (x, r, 0.0, 0.5);
}
/* PIXMAN_KERNEL_MITCHELL: Cubic recommended by the Mitchell-Netravali
paper. This has negative values and because the values at +/-1 are
not zero it does not interpolate the pixels, meaning it will change
an image even if there is no translation.
*/
static double
mitchell_kernel (double x, double r)
{
return general_cubic (x, r, 1/3.0, 1/3.0);
}
/* PIXMAN_KERNEL_NOTCH: Cubic recommended by the Mitchell-Netravali
paper to remove postaliasing artifacts. This does not remove
aliasing already present in the source image, though it may appear
to due to it's excessive blurriness. In any case this is more
useful than gaussian for image reconstruction.
*/
static double
notch_kernel (double x, double r)
{
return general_cubic (x, r, 1.5, -0.25);
}
/* PIXMAN_KERNEL_LANCZOS3: lanczos windowed sinc function from -3 to
+3. Very popular with high-end software though I think any
advantage over cubics is hidden by quantization and programming
mistakes. You will see LANCZOS5 or even 7 sometimes.
*/
static double
sinc (double x)
{
return x ? sin (M_PI * x) / (M_PI * x) : 1.0;
}
static double
lanczos (double x, double n)
{
return fabs (x) < n ? sinc (x) * sinc (x * (1.0 / n)) : 0.0;
}
static double
lanczos3_kernel (double x, double r)
{
if (r < 1.0)
return
lanczos3_kernel (x * 2 - .5, r * 2) +
lanczos3_kernel (x * 2 + .5, r * 2);
else
return lanczos (x / r, 3.0);
}
static int
lanczos3_width (double r)
{
return MAX (2, ceil (r * 6));
}
/* PIXMAN_KERNEL_LANCZOS3_STRETCHED - The LANCZOS3 kernel widened by
4/3. Recommended by Jim Blinn
http://graphics.cs.cmu.edu/nsp/course/15-462/Fall07/462/papers/jaggy.pdf
*/
static double
nice_kernel (double x, double r)
{
return lanczos3_kernel (x, r * (4.0/3));
}
static int
nice_width (double r)
{
return MAX (2.0, ceil (r * 8));
}
/* PIXMAN_KERNEL_TENT: Triangle of width 2r. Lots of software uses
this as a "better" filter, twice the size of a box but smaller than
a cubic.
When r == 1.0, PIXMAN_KERNEL_BOX, PIXMAN_KERNEL_LINEAR, and
PIXMAN_KERNEL_TENT all produce the same filter, allowing
them to be exchanged at this point.
*/
static double
tent_kernel (double x, double r)
{
if (r < 1.0)
return box_kernel(x, r);
else
return MAX (1.0 - fabs(x / r), 0.0);
}
static int
tent_width (double r)
{
return r < 1.0 ? 2 : ceil(2 * r);
}
static const filter_info_t filters[] =
{
{ KERNEL_IMPULSE, impulse_kernel, impulse_width },
{ KERNEL_BOX, box_kernel, box_width },
{ KERNEL_LINEAR, linear_kernel, linear_width },
{ KERNEL_MITCHELL, mitchell_kernel, cubic_width },
{ KERNEL_NOTCH, notch_kernel, cubic_width },
{ KERNEL_CATMULL_ROM, cubic_kernel, cubic_width },
{ KERNEL_LANCZOS3, lanczos3_kernel, lanczos3_width },
{ KERNEL_LANCZOS3_STRETCHED,nice_kernel, nice_width },
{ KERNEL_TENT, tent_kernel, tent_width }
};
/* Fills in one dimension of the filter array */
static void get_filter(kernel_t filter, double r,
int width, int subsample,
pixman_fixed_t* out)
{
int i;
pixman_fixed_t *p = out;
int n_phases = 1 << subsample;
double step = 1.0 / n_phases;
kernel_func_t func = filters[filter].func;
/* special-case the impulse filter: */
if (width <= 1)
{
for (i = 0; i < n_phases; ++i)
*p++ = pixman_fixed_1;
return;
}
for (i = 0; i < n_phases; ++i)
{
double frac = (i + .5) * step;
/* Center of left-most pixel: */
double x1 = ceil (frac - width / 2.0 - 0.5) - frac + 0.5;
double total = 0;
pixman_fixed_t new_total = 0;
int j;
for (j = 0; j < width; ++j)
{
double v = func(x1 + j, r);
total += v;
p[j] = pixman_double_to_fixed (v);
}
/* Normalize */
total = 1 / total;
for (j = 0; j < width; ++j)
new_total += (p[j] *= total);
/* Put any error on center pixel */
p[width / 2] += (pixman_fixed_1 - new_total);
p += width;
}
}
/* Create the parameter list for a SEPARABLE_CONVOLUTION filter
* with the given kernels and scale parameters.
*/
static pixman_fixed_t *
create_separable_convolution (int *n_values,
kernel_t xfilter,
double sx,
kernel_t yfilter,
double sy)
{
int xwidth, xsubsample, ywidth, ysubsample, size_x, size_y;
pixman_fixed_t *params;
xwidth = filters[xfilter].width(sx);
xsubsample = 0;
if (xwidth > 1)
while (sx * (1 << xsubsample) <= 128.0) xsubsample++;
size_x = (1 << xsubsample) * xwidth;
ywidth = filters[yfilter].width(sy);
ysubsample = 0;
if (ywidth > 1)
while (sy * (1 << ysubsample) <= 128.0) ysubsample++;
size_y = (1 << ysubsample) * ywidth;
*n_values = 4 + size_x + size_y;
params = _cairo_malloc (*n_values * sizeof (pixman_fixed_t));
if (!params) return 0;
params[0] = pixman_int_to_fixed (xwidth);
params[1] = pixman_int_to_fixed (ywidth);
params[2] = pixman_int_to_fixed (xsubsample);
params[3] = pixman_int_to_fixed (ysubsample);
get_filter(xfilter, sx, xwidth, xsubsample, params + 4);
get_filter(yfilter, sy, ywidth, ysubsample, params + 4 + size_x);
return params;
}
/* ========================================================================== */
static cairo_bool_t
_pixman_image_set_properties (pixman_image_t *pixman_image,
const cairo_pattern_t *pattern,
const cairo_rectangle_int_t *extents,
int *ix,int *iy)
{
pixman_transform_t pixman_transform;
cairo_int_status_t status;
status = _cairo_matrix_to_pixman_matrix_offset (&pattern->matrix,
pattern->filter,
extents->x + extents->width/2.,
extents->y + extents->height/2.,
&pixman_transform, ix, iy);
if (status == CAIRO_INT_STATUS_NOTHING_TO_DO)
{
/* If the transform is an identity, we don't need to set it
* and we can use any filtering, so choose the fastest one. */
pixman_image_set_filter (pixman_image, PIXMAN_FILTER_NEAREST, NULL, 0);
}
else if (unlikely (status != CAIRO_INT_STATUS_SUCCESS ||
! pixman_image_set_transform (pixman_image,
&pixman_transform)))
{
return FALSE;
}
else
{
pixman_filter_t pixman_filter;
kernel_t kernel;
double dx, dy;
/* Compute scale factors from the pattern matrix. These scale
* factors are from user to pattern space, and as such they
* are greater than 1.0 for downscaling and less than 1.0 for
* upscaling. The factors are the size of an axis-aligned
* rectangle with the same area as the parallelgram a 1x1
* square transforms to.
*/
dx = hypot (pattern->matrix.xx, pattern->matrix.xy);
dy = hypot (pattern->matrix.yx, pattern->matrix.yy);
/* Clip at maximum pixman_fixed number. Besides making it
* passable to pixman, this avoids errors from inf and nan.
*/
if (! (dx < 0x7FFF)) dx = 0x7FFF;
if (! (dy < 0x7FFF)) dy = 0x7FFF;
switch (pattern->filter) {
case CAIRO_FILTER_FAST:
pixman_filter = PIXMAN_FILTER_FAST;
break;
case CAIRO_FILTER_GOOD:
pixman_filter = PIXMAN_FILTER_SEPARABLE_CONVOLUTION;
kernel = KERNEL_BOX;
/* Clip the filter size to prevent extreme slowness. This
value could be raised if 2-pass filtering is done */
if (dx > 16.0) dx = 16.0;
if (dy > 16.0) dy = 16.0;
/* Match the bilinear filter for scales > .75: */
if (dx < 1.0/0.75) dx = 1.0;
if (dy < 1.0/0.75) dy = 1.0;
break;
case CAIRO_FILTER_BEST:
pixman_filter = PIXMAN_FILTER_SEPARABLE_CONVOLUTION;
kernel = KERNEL_CATMULL_ROM; /* LANCZOS3 is better but not much */
/* Clip the filter size to prevent extreme slowness. This
value could be raised if 2-pass filtering is done */
if (dx > 16.0) { dx = 16.0; kernel = KERNEL_BOX; }
/* blur up to 2x scale, then blend to square pixels for larger: */
else if (dx < 1.0) {
if (dx < 1.0/128) dx = 1.0/127;
else if (dx < 0.5) dx = 1.0 / (1.0 / dx - 1.0);
else dx = 1.0;
}
if (dy > 16.0) { dy = 16.0; kernel = KERNEL_BOX; }
else if (dy < 1.0) {
if (dy < 1.0/128) dy = 1.0/127;
else if (dy < 0.5) dy = 1.0 / (1.0 / dy - 1.0);
else dy = 1.0;
}
break;
case CAIRO_FILTER_NEAREST:
pixman_filter = PIXMAN_FILTER_NEAREST;
break;
case CAIRO_FILTER_BILINEAR:
pixman_filter = PIXMAN_FILTER_BILINEAR;
break;
case CAIRO_FILTER_GAUSSIAN:
/* XXX: The GAUSSIAN value has no implementation in cairo
* whatsoever, so it was really a mistake to have it in the
* API. We could fix this by officially deprecating it, or
* else inventing semantics and providing an actual
* implementation for it. */
default:
pixman_filter = PIXMAN_FILTER_BEST;
}
if (pixman_filter == PIXMAN_FILTER_SEPARABLE_CONVOLUTION) {
int n_params;
pixman_fixed_t *params;
params = create_separable_convolution
(&n_params, kernel, dx, kernel, dy);
pixman_image_set_filter (pixman_image, pixman_filter,
params, n_params);
free (params);
} else {
pixman_image_set_filter (pixman_image, pixman_filter, NULL, 0);
}
}
{
pixman_repeat_t pixman_repeat;
switch (pattern->extend) {
default:
case CAIRO_EXTEND_NONE:
pixman_repeat = PIXMAN_REPEAT_NONE;
break;
case CAIRO_EXTEND_REPEAT:
pixman_repeat = PIXMAN_REPEAT_NORMAL;
break;
case CAIRO_EXTEND_REFLECT:
pixman_repeat = PIXMAN_REPEAT_REFLECT;
break;
case CAIRO_EXTEND_PAD:
pixman_repeat = PIXMAN_REPEAT_PAD;
break;
}
pixman_image_set_repeat (pixman_image, pixman_repeat);
}
if (pattern->has_component_alpha)
pixman_image_set_component_alpha (pixman_image, TRUE);
return TRUE;
}
struct proxy {
cairo_surface_t base;
cairo_surface_t *image;
};
static cairo_status_t
proxy_acquire_source_image (void *abstract_surface,
cairo_image_surface_t **image_out,
void **image_extra)
{
struct proxy *proxy = abstract_surface;
return _cairo_surface_acquire_source_image (proxy->image, image_out, image_extra);
}
static void
proxy_release_source_image (void *abstract_surface,
cairo_image_surface_t *image,
void *image_extra)
{
struct proxy *proxy = abstract_surface;
_cairo_surface_release_source_image (proxy->image, image, image_extra);
}
static cairo_status_t
proxy_finish (void *abstract_surface)
{
return CAIRO_STATUS_SUCCESS;
}
static const cairo_surface_backend_t proxy_backend = {
CAIRO_INTERNAL_SURFACE_TYPE_NULL,
proxy_finish,
NULL,
NULL, /* create similar */
NULL, /* create similar image */
NULL, /* map to image */
NULL, /* unmap image */
_cairo_surface_default_source,
proxy_acquire_source_image,
proxy_release_source_image,
};
static cairo_surface_t *
attach_proxy (cairo_surface_t *source,
cairo_surface_t *image)
{
struct proxy *proxy;
proxy = _cairo_malloc (sizeof (*proxy));
if (unlikely (proxy == NULL))
return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
_cairo_surface_init (&proxy->base, &proxy_backend, NULL, image->content, FALSE);
proxy->image = image;
_cairo_surface_attach_snapshot (source, &proxy->base, NULL);
return &proxy->base;
}
static void
detach_proxy (cairo_surface_t *source,
cairo_surface_t *proxy)
{
cairo_surface_finish (proxy);
cairo_surface_destroy (proxy);
}
static cairo_surface_t *
get_proxy (cairo_surface_t *proxy)
{
return ((struct proxy *)proxy)->image;
}
static pixman_image_t *
_pixman_image_for_recording (cairo_image_surface_t *dst,
const cairo_surface_pattern_t *pattern,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
const cairo_rectangle_int_t *sample,
int *ix, int *iy)
{
cairo_surface_t *source, *clone, *proxy;
cairo_rectangle_int_t limit;
cairo_rectangle_int_t src_limit;
pixman_image_t *pixman_image;
cairo_status_t status;
cairo_extend_t extend;
cairo_matrix_t *m, matrix;
double sx = 1.0, sy = 1.0;
int tx = 0, ty = 0;
TRACE ((stderr, "%s\n", __FUNCTION__));
*ix = *iy = 0;
source = _cairo_pattern_get_source (pattern, &limit);
src_limit = limit;
extend = pattern->base.extend;
if (_cairo_rectangle_contains_rectangle (&limit, sample))
extend = CAIRO_EXTEND_NONE;
if (extend == CAIRO_EXTEND_NONE) {
if (! _cairo_rectangle_intersect (&limit, sample))
return _pixman_transparent_image ();
}
if (! _cairo_matrix_is_identity (&pattern->base.matrix)) {
double x1, y1, x2, y2;
matrix = pattern->base.matrix;
status = cairo_matrix_invert (&matrix);
assert (status == CAIRO_STATUS_SUCCESS);
x1 = limit.x;
y1 = limit.y;
x2 = limit.x + limit.width;
y2 = limit.y + limit.height;
_cairo_matrix_transform_bounding_box (&matrix,
&x1, &y1, &x2, &y2, NULL);
limit.x = floor (x1);
limit.y = floor (y1);
limit.width = ceil (x2) - limit.x;
limit.height = ceil (y2) - limit.y;
sx = (double)src_limit.width / limit.width;
sy = (double)src_limit.height / limit.height;
}
tx = limit.x;
ty = limit.y;
/* XXX transformations! */
proxy = _cairo_surface_has_snapshot (source, &proxy_backend);
if (proxy != NULL) {
clone = cairo_surface_reference (get_proxy (proxy));
goto done;
}
if (is_mask) {
clone = cairo_image_surface_create (CAIRO_FORMAT_A8,
limit.width, limit.height);
} else {
if (dst->base.content == source->content)
clone = cairo_image_surface_create (dst->format,
limit.width, limit.height);
else
clone = _cairo_image_surface_create_with_content (source->content,
limit.width,
limit.height);
}
m = NULL;
if (extend == CAIRO_EXTEND_NONE) {
matrix = pattern->base.matrix;
if (tx | ty)
cairo_matrix_translate (&matrix, tx, ty);
m = &matrix;
} else {
cairo_matrix_init_scale (&matrix, sx, sy);
cairo_matrix_translate (&matrix, src_limit.x/sx, src_limit.y/sy);
m = &matrix;
}
/* Handle recursion by returning future reads from the current image */
proxy = attach_proxy (source, clone);
status = _cairo_recording_surface_replay_with_clip (source, m, clone, NULL);
detach_proxy (source, proxy);
if (unlikely (status)) {
cairo_surface_destroy (clone);
return NULL;
}
done:
pixman_image = pixman_image_ref (((cairo_image_surface_t *)clone)->pixman_image);
cairo_surface_destroy (clone);
if (extend == CAIRO_EXTEND_NONE) {
*ix = -limit.x;
*iy = -limit.y;
} else {
cairo_pattern_union_t tmp_pattern;
_cairo_pattern_init_static_copy (&tmp_pattern.base, &pattern->base);
matrix = pattern->base.matrix;
status = cairo_matrix_invert(&matrix);
assert (status == CAIRO_STATUS_SUCCESS);
cairo_matrix_translate (&matrix, src_limit.x, src_limit.y);
cairo_matrix_scale (&matrix, sx, sy);
status = cairo_matrix_invert(&matrix);
assert (status == CAIRO_STATUS_SUCCESS);
cairo_pattern_set_matrix (&tmp_pattern.base, &matrix);
if (! _pixman_image_set_properties (pixman_image,
&tmp_pattern.base, extents,
ix, iy)) {
pixman_image_unref (pixman_image);
pixman_image= NULL;
}
}
return pixman_image;
}
static pixman_image_t *
_pixman_image_for_surface (cairo_image_surface_t *dst,
const cairo_surface_pattern_t *pattern,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
const cairo_rectangle_int_t *sample,
int *ix, int *iy)
{
cairo_extend_t extend = pattern->base.extend;
pixman_image_t *pixman_image;
TRACE ((stderr, "%s\n", __FUNCTION__));
*ix = *iy = 0;
pixman_image = NULL;
if (pattern->surface->type == CAIRO_SURFACE_TYPE_RECORDING)
return _pixman_image_for_recording(dst, pattern,
is_mask, extents, sample,
ix, iy);
if (pattern->surface->type == CAIRO_SURFACE_TYPE_IMAGE &&
(! is_mask || ! pattern->base.has_component_alpha ||
(pattern->surface->content & CAIRO_CONTENT_COLOR) == 0))
{
cairo_surface_t *defer_free = NULL;
cairo_image_surface_t *source = (cairo_image_surface_t *) pattern->surface;
cairo_surface_type_t type;
if (_cairo_surface_is_snapshot (&source->base)) {
defer_free = _cairo_surface_snapshot_get_target (&source->base);
source = (cairo_image_surface_t *) defer_free;
}
type = source->base.backend->type;
if (type == CAIRO_SURFACE_TYPE_IMAGE) {
if (extend != CAIRO_EXTEND_NONE &&
sample->x >= 0 &&
sample->y >= 0 &&
sample->x + sample->width <= source->width &&
sample->y + sample->height <= source->height)
{
extend = CAIRO_EXTEND_NONE;
}
if (sample->width == 1 && sample->height == 1) {
if (sample->x < 0 ||
sample->y < 0 ||
sample->x >= source->width ||
sample->y >= source->height)
{
if (extend == CAIRO_EXTEND_NONE) {
cairo_surface_destroy (defer_free);
return _pixman_transparent_image ();
}
}
else
{
pixman_image = _pixel_to_solid (source,
sample->x, sample->y);
if (pixman_image) {
cairo_surface_destroy (defer_free);
return pixman_image;
}
}
}
#if PIXMAN_HAS_ATOMIC_OPS
/* avoid allocating a 'pattern' image if we can reuse the original */
if (extend == CAIRO_EXTEND_NONE &&
_cairo_matrix_is_pixman_translation (&pattern->base.matrix,
pattern->base.filter,
ix, iy))
{
cairo_surface_destroy (defer_free);
return pixman_image_ref (source->pixman_image);
}
#endif
pixman_image = pixman_image_create_bits (source->pixman_format,
source->width,
source->height,
(uint32_t *) source->data,
source->stride);
if (unlikely (pixman_image == NULL)) {
cairo_surface_destroy (defer_free);
return NULL;
}
if (defer_free) {
pixman_image_set_destroy_function (pixman_image,
_defer_free_cleanup,
defer_free);
}
} else if (type == CAIRO_SURFACE_TYPE_SUBSURFACE) {
cairo_surface_subsurface_t *sub;
cairo_bool_t is_contained = FALSE;
sub = (cairo_surface_subsurface_t *) source;
source = (cairo_image_surface_t *) sub->target;
if (sample->x >= 0 &&
sample->y >= 0 &&
sample->x + sample->width <= sub->extents.width &&
sample->y + sample->height <= sub->extents.height)
{
is_contained = TRUE;
}
if (sample->width == 1 && sample->height == 1) {
if (is_contained) {
pixman_image = _pixel_to_solid (source,
sub->extents.x + sample->x,
sub->extents.y + sample->y);
if (pixman_image)
return pixman_image;
} else {
if (extend == CAIRO_EXTEND_NONE)
return _pixman_transparent_image ();
}
}
#if PIXMAN_HAS_ATOMIC_OPS
*ix = sub->extents.x;
*iy = sub->extents.y;
if (is_contained &&
_cairo_matrix_is_pixman_translation (&pattern->base.matrix,
pattern->base.filter,
ix, iy))
{
return pixman_image_ref (source->pixman_image);
}
#endif
/* Avoid sub-byte offsets, force a copy in that case. */
if (PIXMAN_FORMAT_BPP (source->pixman_format) >= 8) {
if (is_contained) {
void *data = source->data
+ sub->extents.x * PIXMAN_FORMAT_BPP(source->pixman_format)/8
+ sub->extents.y * source->stride;
pixman_image = pixman_image_create_bits (source->pixman_format,
sub->extents.width,
sub->extents.height,
data,
source->stride);
if (unlikely (pixman_image == NULL))
return NULL;
} else {
/* XXX for a simple translation and EXTEND_NONE we can
* fix up the pattern matrix instead.
*/
}
}
}
}
if (pixman_image == NULL) {
struct acquire_source_cleanup *cleanup;
cairo_image_surface_t *image;
void *extra;
cairo_status_t status;
status = _cairo_surface_acquire_source_image (pattern->surface, &image, &extra);
if (unlikely (status))
return NULL;
pixman_image = pixman_image_create_bits (image->pixman_format,
image->width,
image->height,
(uint32_t *) image->data,
image->stride);
if (unlikely (pixman_image == NULL)) {
_cairo_surface_release_source_image (pattern->surface, image, extra);
return NULL;
}
cleanup = _cairo_malloc (sizeof (*cleanup));
if (unlikely (cleanup == NULL)) {
_cairo_surface_release_source_image (pattern->surface, image, extra);
pixman_image_unref (pixman_image);
return NULL;
}
cleanup->surface = pattern->surface;
cleanup->image = image;
cleanup->image_extra = extra;
pixman_image_set_destroy_function (pixman_image,
_acquire_source_cleanup, cleanup);
}
if (! _pixman_image_set_properties (pixman_image,
&pattern->base, extents,
ix, iy)) {
pixman_image_unref (pixman_image);
pixman_image= NULL;
}
return pixman_image;
}
struct raster_source_cleanup {
const cairo_pattern_t *pattern;
cairo_surface_t *surface;
cairo_image_surface_t *image;
void *image_extra;
};
static void
_raster_source_cleanup (pixman_image_t *pixman_image,
void *closure)
{
struct raster_source_cleanup *data = closure;
_cairo_surface_release_source_image (data->surface,
data->image,
data->image_extra);
_cairo_raster_source_pattern_release (data->pattern,
data->surface);
free (data);
}
static pixman_image_t *
_pixman_image_for_raster (cairo_image_surface_t *dst,
const cairo_raster_source_pattern_t *pattern,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
const cairo_rectangle_int_t *sample,
int *ix, int *iy)
{
pixman_image_t *pixman_image;
struct raster_source_cleanup *cleanup;
cairo_image_surface_t *image;
void *extra;
cairo_status_t status;
cairo_surface_t *surface;
TRACE ((stderr, "%s\n", __FUNCTION__));
*ix = *iy = 0;
surface = _cairo_raster_source_pattern_acquire (&pattern->base,
&dst->base, NULL);
if (unlikely (surface == NULL || surface->status))
return NULL;
status = _cairo_surface_acquire_source_image (surface, &image, &extra);
if (unlikely (status)) {
_cairo_raster_source_pattern_release (&pattern->base, surface);
return NULL;
}
assert (image->width == pattern->extents.width);
assert (image->height == pattern->extents.height);
pixman_image = pixman_image_create_bits (image->pixman_format,
image->width,
image->height,
(uint32_t *) image->data,
image->stride);
if (unlikely (pixman_image == NULL)) {
_cairo_surface_release_source_image (surface, image, extra);
_cairo_raster_source_pattern_release (&pattern->base, surface);
return NULL;
}
cleanup = _cairo_malloc (sizeof (*cleanup));
if (unlikely (cleanup == NULL)) {
pixman_image_unref (pixman_image);
_cairo_surface_release_source_image (surface, image, extra);
_cairo_raster_source_pattern_release (&pattern->base, surface);
return NULL;
}
cleanup->pattern = &pattern->base;
cleanup->surface = surface;
cleanup->image = image;
cleanup->image_extra = extra;
pixman_image_set_destroy_function (pixman_image,
_raster_source_cleanup, cleanup);
if (! _pixman_image_set_properties (pixman_image,
&pattern->base, extents,
ix, iy)) {
pixman_image_unref (pixman_image);
pixman_image= NULL;
}
return pixman_image;
}
pixman_image_t *
_pixman_image_for_pattern (cairo_image_surface_t *dst,
const cairo_pattern_t *pattern,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
const cairo_rectangle_int_t *sample,
int *tx, int *ty)
{
*tx = *ty = 0;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (pattern == NULL)
return _pixman_white_image ();
switch (pattern->type) {
default:
ASSERT_NOT_REACHED;
case CAIRO_PATTERN_TYPE_SOLID:
return _pixman_image_for_color (&((const cairo_solid_pattern_t *) pattern)->color);
case CAIRO_PATTERN_TYPE_RADIAL:
case CAIRO_PATTERN_TYPE_LINEAR:
return _pixman_image_for_gradient ((const cairo_gradient_pattern_t *) pattern,
extents, tx, ty);
case CAIRO_PATTERN_TYPE_MESH:
return _pixman_image_for_mesh ((const cairo_mesh_pattern_t *) pattern,
extents, tx, ty);
case CAIRO_PATTERN_TYPE_SURFACE:
return _pixman_image_for_surface (dst,
(const cairo_surface_pattern_t *) pattern,
is_mask, extents, sample,
tx, ty);
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
return _pixman_image_for_raster (dst,
(const cairo_raster_source_pattern_t *) pattern,
is_mask, extents, sample,
tx, ty);
}
}
static cairo_status_t
_cairo_image_source_finish (void *abstract_surface)
{
cairo_image_source_t *source = abstract_surface;
pixman_image_unref (source->pixman_image);
return CAIRO_STATUS_SUCCESS;
}
const cairo_surface_backend_t _cairo_image_source_backend = {
CAIRO_SURFACE_TYPE_IMAGE,
_cairo_image_source_finish,
NULL, /* read-only wrapper */
};
cairo_surface_t *
_cairo_image_source_create_for_pattern (cairo_surface_t *dst,
const cairo_pattern_t *pattern,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
const cairo_rectangle_int_t *sample,
int *src_x, int *src_y)
{
cairo_image_source_t *source;
TRACE ((stderr, "%s\n", __FUNCTION__));
source = _cairo_malloc (sizeof (cairo_image_source_t));
if (unlikely (source == NULL))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
source->pixman_image =
_pixman_image_for_pattern ((cairo_image_surface_t *)dst,
pattern, is_mask,
extents, sample,
src_x, src_y);
if (unlikely (source->pixman_image == NULL)) {
free (source);
return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
}
_cairo_surface_init (&source->base,
&_cairo_image_source_backend,
NULL, /* device */
CAIRO_CONTENT_COLOR_ALPHA,
FALSE); /* is_vector */
source->is_opaque_solid =
pattern == NULL || _cairo_pattern_is_opaque_solid (pattern);
return &source->base;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-image-surface-inline.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is University of Southern
* California.
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
*/
#ifndef CAIRO_IMAGE_SURFACE_INLINE_H
#define CAIRO_IMAGE_SURFACE_INLINE_H
#include "cairo-surface-private.h"
#include "cairo-image-surface-private.h"
CAIRO_BEGIN_DECLS
static inline cairo_image_surface_t *
_cairo_image_surface_create_in_error (cairo_status_t status)
{
return (cairo_image_surface_t *) _cairo_surface_create_in_error (status);
}
static inline void
_cairo_image_surface_set_parent (cairo_image_surface_t *image,
cairo_surface_t *parent)
{
image->parent = parent;
}
static inline cairo_bool_t
_cairo_image_surface_is_clone (cairo_image_surface_t *image)
{
return image->parent != NULL;
}
/**
* _cairo_surface_is_image:
* @surface: a #cairo_surface_t
*
* Checks if a surface is an #cairo_image_surface_t
*
* Return value: %TRUE if the surface is an image surface
**/
static inline cairo_bool_t
_cairo_surface_is_image (const cairo_surface_t *surface)
{
/* _cairo_surface_nil sets a NULL backend so be safe */
return surface->backend && surface->backend->type == CAIRO_SURFACE_TYPE_IMAGE;
}
/**
* _cairo_surface_is_image_source:
* @surface: a #cairo_surface_t
*
* Checks if a surface is an #cairo_image_source_t
*
* Return value: %TRUE if the surface is an image source
**/
static inline cairo_bool_t
_cairo_surface_is_image_source (const cairo_surface_t *surface)
{
return surface->backend == &_cairo_image_source_backend;
}
CAIRO_END_DECLS
#endif /* CAIRO_IMAGE_SURFACE_INLINE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-image-surface-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is University of Southern
* California.
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
*/
#ifndef CAIRO_IMAGE_SURFACE_PRIVATE_H
#define CAIRO_IMAGE_SURFACE_PRIVATE_H
#include "cairo-surface-private.h"
#include <stddef.h>
#include <pixman/pixman.h>
CAIRO_BEGIN_DECLS
/* The canonical image backend */
struct _cairo_image_surface {
cairo_surface_t base;
pixman_image_t *pixman_image;
const cairo_compositor_t *compositor;
/* Parenting is tricky wrt lifetime tracking...
*
* One use for tracking the parent of an image surface is for
* create_similar_image() where we wish to create a device specific
* surface but return an image surface to the user. In such a case,
* the image may be owned by the device specific surface, its parent,
* but the user lifetime tracking is then performed on the image. So
* when the image is then finalized we call cairo_surface_destroy()
* on the parent. However, for normal usage where the lifetime tracking
* is done on the parent surface, we need to be careful to unhook
* the image->parent pointer before finalizing the image.
*/
cairo_surface_t *parent;
pixman_format_code_t pixman_format;
cairo_format_t format;
unsigned char *data;
int width;
int height;
ptrdiff_t stride;
int depth;
unsigned owns_data : 1;
unsigned transparency : 2;
unsigned color : 2;
};
#define to_image_surface(S) ((cairo_image_surface_t *)(S))
/* A wrapper for holding pixman images returned by create_for_pattern */
typedef struct _cairo_image_source {
cairo_surface_t base;
pixman_image_t *pixman_image;
unsigned is_opaque_solid : 1;
} cairo_image_source_t;
cairo_private extern const cairo_surface_backend_t _cairo_image_surface_backend;
cairo_private extern const cairo_surface_backend_t _cairo_image_source_backend;
cairo_private const cairo_compositor_t *
_cairo_image_mask_compositor_get (void);
cairo_private const cairo_compositor_t *
_cairo_image_traps_compositor_get (void);
cairo_private const cairo_compositor_t *
_cairo_image_spans_compositor_get (void);
#define _cairo_image_default_compositor_get _cairo_image_spans_compositor_get
cairo_private cairo_int_status_t
_cairo_image_surface_paint (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_clip_t *clip);
cairo_private cairo_int_status_t
_cairo_image_surface_mask (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_pattern_t *mask,
const cairo_clip_t *clip);
cairo_private cairo_int_status_t
_cairo_image_surface_stroke (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_antialias_t antialias,
const cairo_clip_t *clip);
cairo_private cairo_int_status_t
_cairo_image_surface_fill (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias,
const cairo_clip_t *clip);
cairo_private cairo_int_status_t
_cairo_image_surface_glyphs (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_scaled_font_t *scaled_font,
const cairo_clip_t *clip);
cairo_private void
_cairo_image_surface_init (cairo_image_surface_t *surface,
pixman_image_t *pixman_image,
pixman_format_code_t pixman_format);
cairo_private cairo_surface_t *
_cairo_image_surface_create_similar (void *abstract_other,
cairo_content_t content,
int width,
int height);
cairo_private cairo_image_surface_t *
_cairo_image_surface_map_to_image (void *abstract_other,
const cairo_rectangle_int_t *extents);
cairo_private cairo_int_status_t
_cairo_image_surface_unmap_image (void *abstract_surface,
cairo_image_surface_t *image);
cairo_private cairo_surface_t *
_cairo_image_surface_source (void *abstract_surface,
cairo_rectangle_int_t *extents);
cairo_private cairo_status_t
_cairo_image_surface_acquire_source_image (void *abstract_surface,
cairo_image_surface_t **image_out,
void **image_extra);
cairo_private void
_cairo_image_surface_release_source_image (void *abstract_surface,
cairo_image_surface_t *image,
void *image_extra);
cairo_private cairo_surface_t *
_cairo_image_surface_snapshot (void *abstract_surface);
cairo_private_no_warn cairo_bool_t
_cairo_image_surface_get_extents (void *abstract_surface,
cairo_rectangle_int_t *rectangle);
cairo_private void
_cairo_image_surface_get_font_options (void *abstract_surface,
cairo_font_options_t *options);
cairo_private cairo_surface_t *
_cairo_image_source_create_for_pattern (cairo_surface_t *dst,
const cairo_pattern_t *pattern,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
const cairo_rectangle_int_t *sample,
int *src_x, int *src_y);
cairo_private cairo_status_t
_cairo_image_surface_finish (void *abstract_surface);
cairo_private pixman_image_t *
_pixman_image_for_color (const cairo_color_t *cairo_color);
cairo_private pixman_image_t *
_pixman_image_for_pattern (cairo_image_surface_t *dst,
const cairo_pattern_t *pattern,
cairo_bool_t is_mask,
const cairo_rectangle_int_t *extents,
const cairo_rectangle_int_t *sample,
int *tx, int *ty);
cairo_private void
_pixman_image_add_traps (pixman_image_t *image,
int dst_x, int dst_y,
cairo_traps_t *traps);
cairo_private void
_pixman_image_add_tristrip (pixman_image_t *image,
int dst_x, int dst_y,
cairo_tristrip_t *strip);
cairo_private cairo_image_surface_t *
_cairo_image_surface_clone_subimage (cairo_surface_t *surface,
const cairo_rectangle_int_t *extents);
/* Similar to clone; but allow format conversion */
cairo_private cairo_image_surface_t *
_cairo_image_surface_create_from_image (cairo_image_surface_t *other,
pixman_format_code_t format,
int x, int y, int width, int height,
int stride);
CAIRO_END_DECLS
#endif /* CAIRO_IMAGE_SURFACE_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-image-surface.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2003 University of Southern California
* Copyright © 2009,2010,2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-boxes-private.h"
#include "cairo-clip-private.h"
#include "cairo-composite-rectangles-private.h"
#include "cairo-compositor-private.h"
#include "cairo-default-context-private.h"
#include "cairo-error-private.h"
#include "cairo-image-surface-inline.h"
#include "cairo-paginated-private.h"
#include "cairo-pattern-private.h"
#include "cairo-pixman-private.h"
#include "cairo-recording-surface-private.h"
#include "cairo-region-private.h"
#include "cairo-scaled-font-private.h"
#include "cairo-surface-snapshot-inline.h"
#include "cairo-surface-snapshot-private.h"
#include "cairo-surface-subsurface-private.h"
/* Limit on the width / height of an image surface in pixels. This is
* mainly determined by coordinates of things sent to pixman at the
* moment being in 16.16 format. */
#define MAX_IMAGE_SIZE 32767
/**
* SECTION:cairo-image
* @Title: Image Surfaces
* @Short_Description: Rendering to memory buffers
* @See_Also: #cairo_surface_t
*
* Image surfaces provide the ability to render to memory buffers
* either allocated by cairo or by the calling code. The supported
* image formats are those defined in #cairo_format_t.
**/
/**
* CAIRO_HAS_IMAGE_SURFACE:
*
* Defined if the image surface backend is available.
* The image surface backend is always built in.
* This macro was added for completeness in cairo 1.8.
*
* Since: 1.8
**/
static cairo_bool_t
_cairo_image_surface_is_size_valid (int width, int height)
{
return 0 <= width && width <= MAX_IMAGE_SIZE &&
0 <= height && height <= MAX_IMAGE_SIZE;
}
cairo_format_t
_cairo_format_from_pixman_format (pixman_format_code_t pixman_format)
{
switch (pixman_format) {
case PIXMAN_rgba_float:
return CAIRO_FORMAT_RGBA128F;
case PIXMAN_rgb_float:
return CAIRO_FORMAT_RGB96F;
case PIXMAN_a8r8g8b8:
return CAIRO_FORMAT_ARGB32;
case PIXMAN_x2r10g10b10:
return CAIRO_FORMAT_RGB30;
case PIXMAN_x8r8g8b8:
return CAIRO_FORMAT_RGB24;
case PIXMAN_a8:
return CAIRO_FORMAT_A8;
case PIXMAN_a1:
return CAIRO_FORMAT_A1;
case PIXMAN_r5g6b5:
return CAIRO_FORMAT_RGB16_565;
#if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0,22,0)
case PIXMAN_r8g8b8a8: case PIXMAN_r8g8b8x8:
#endif
#if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0,27,2)
case PIXMAN_a8r8g8b8_sRGB:
#endif
case PIXMAN_a8b8g8r8: case PIXMAN_x8b8g8r8: case PIXMAN_r8g8b8:
case PIXMAN_b8g8r8: case PIXMAN_b5g6r5:
case PIXMAN_a1r5g5b5: case PIXMAN_x1r5g5b5: case PIXMAN_a1b5g5r5:
case PIXMAN_x1b5g5r5: case PIXMAN_a4r4g4b4: case PIXMAN_x4r4g4b4:
case PIXMAN_a4b4g4r4: case PIXMAN_x4b4g4r4: case PIXMAN_r3g3b2:
case PIXMAN_b2g3r3: case PIXMAN_a2r2g2b2: case PIXMAN_a2b2g2r2:
case PIXMAN_c8: case PIXMAN_g8: case PIXMAN_x4a4:
case PIXMAN_a4: case PIXMAN_r1g2b1: case PIXMAN_b1g2r1:
case PIXMAN_a1r1g1b1: case PIXMAN_a1b1g1r1: case PIXMAN_c4:
case PIXMAN_g4: case PIXMAN_g1:
case PIXMAN_yuy2: case PIXMAN_yv12:
case PIXMAN_b8g8r8x8:
case PIXMAN_b8g8r8a8:
case PIXMAN_a2b10g10r10:
case PIXMAN_x2b10g10r10:
case PIXMAN_a2r10g10b10:
#if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0,22,0)
case PIXMAN_x14r6g6b6:
#endif
default:
return CAIRO_FORMAT_INVALID;
}
return CAIRO_FORMAT_INVALID;
}
cairo_content_t
_cairo_content_from_pixman_format (pixman_format_code_t pixman_format)
{
cairo_content_t content;
content = 0;
if (PIXMAN_FORMAT_RGB (pixman_format))
content |= CAIRO_CONTENT_COLOR;
if (PIXMAN_FORMAT_A (pixman_format))
content |= CAIRO_CONTENT_ALPHA;
return content;
}
void
_cairo_image_surface_init (cairo_image_surface_t *surface,
pixman_image_t *pixman_image,
pixman_format_code_t pixman_format)
{
surface->parent = NULL;
surface->pixman_image = pixman_image;
surface->pixman_format = pixman_format;
surface->format = _cairo_format_from_pixman_format (pixman_format);
surface->data = (uint8_t *) pixman_image_get_data (pixman_image);
surface->owns_data = FALSE;
surface->transparency = CAIRO_IMAGE_UNKNOWN;
surface->color = CAIRO_IMAGE_UNKNOWN_COLOR;
surface->width = pixman_image_get_width (pixman_image);
surface->height = pixman_image_get_height (pixman_image);
surface->stride = pixman_image_get_stride (pixman_image);
surface->depth = pixman_image_get_depth (pixman_image);
surface->base.is_clear = surface->width == 0 || surface->height == 0;
surface->compositor = _cairo_image_spans_compositor_get ();
}
cairo_surface_t *
_cairo_image_surface_create_for_pixman_image (pixman_image_t *pixman_image,
pixman_format_code_t pixman_format)
{
cairo_image_surface_t *surface;
surface = _cairo_malloc (sizeof (cairo_image_surface_t));
if (unlikely (surface == NULL))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
_cairo_surface_init (&surface->base,
&_cairo_image_surface_backend,
NULL, /* device */
_cairo_content_from_pixman_format (pixman_format),
FALSE); /* is_vector */
_cairo_image_surface_init (surface, pixman_image, pixman_format);
return &surface->base;
}
cairo_bool_t
_pixman_format_from_masks (cairo_format_masks_t *masks,
pixman_format_code_t *format_ret)
{
pixman_format_code_t format;
int format_type;
int a, r, g, b;
cairo_format_masks_t format_masks;
a = _cairo_popcount (masks->alpha_mask);
r = _cairo_popcount (masks->red_mask);
g = _cairo_popcount (masks->green_mask);
b = _cairo_popcount (masks->blue_mask);
if (masks->red_mask) {
if (masks->red_mask > masks->blue_mask)
format_type = PIXMAN_TYPE_ARGB;
else
format_type = PIXMAN_TYPE_ABGR;
} else if (masks->alpha_mask) {
format_type = PIXMAN_TYPE_A;
} else {
return FALSE;
}
format = PIXMAN_FORMAT (masks->bpp, format_type, a, r, g, b);
if (! pixman_format_supported_destination (format))
return FALSE;
/* Sanity check that we got out of PIXMAN_FORMAT exactly what we
* expected. This avoid any problems from something bizarre like
* alpha in the least-significant bits, or insane channel order,
* or whatever. */
if (!_pixman_format_to_masks (format, &format_masks) ||
masks->bpp != format_masks.bpp ||
masks->red_mask != format_masks.red_mask ||
masks->green_mask != format_masks.green_mask ||
masks->blue_mask != format_masks.blue_mask)
{
return FALSE;
}
*format_ret = format;
return TRUE;
}
/* A mask consisting of N bits set to 1. */
#define MASK(N) ((1UL << (N))-1)
cairo_bool_t
_pixman_format_to_masks (pixman_format_code_t format,
cairo_format_masks_t *masks)
{
int a, r, g, b;
masks->bpp = PIXMAN_FORMAT_BPP (format);
/* Number of bits in each channel */
a = PIXMAN_FORMAT_A (format);
r = PIXMAN_FORMAT_R (format);
g = PIXMAN_FORMAT_G (format);
b = PIXMAN_FORMAT_B (format);
switch (PIXMAN_FORMAT_TYPE (format)) {
case PIXMAN_TYPE_ARGB:
masks->alpha_mask = MASK (a) << (r + g + b);
masks->red_mask = MASK (r) << (g + b);
masks->green_mask = MASK (g) << (b);
masks->blue_mask = MASK (b);
return TRUE;
case PIXMAN_TYPE_ABGR:
masks->alpha_mask = MASK (a) << (b + g + r);
masks->blue_mask = MASK (b) << (g + r);
masks->green_mask = MASK (g) << (r);
masks->red_mask = MASK (r);
return TRUE;
#ifdef PIXMAN_TYPE_BGRA
case PIXMAN_TYPE_BGRA:
masks->blue_mask = MASK (b) << (masks->bpp - b);
masks->green_mask = MASK (g) << (masks->bpp - b - g);
masks->red_mask = MASK (r) << (masks->bpp - b - g - r);
masks->alpha_mask = MASK (a);
return TRUE;
#endif
case PIXMAN_TYPE_A:
masks->alpha_mask = MASK (a);
masks->red_mask = 0;
masks->green_mask = 0;
masks->blue_mask = 0;
return TRUE;
case PIXMAN_TYPE_OTHER:
case PIXMAN_TYPE_COLOR:
case PIXMAN_TYPE_GRAY:
case PIXMAN_TYPE_YUY2:
case PIXMAN_TYPE_YV12:
default:
masks->alpha_mask = 0;
masks->red_mask = 0;
masks->green_mask = 0;
masks->blue_mask = 0;
return FALSE;
}
}
pixman_format_code_t
_cairo_format_to_pixman_format_code (cairo_format_t format)
{
pixman_format_code_t ret;
switch (format) {
case CAIRO_FORMAT_A1:
ret = PIXMAN_a1;
break;
case CAIRO_FORMAT_A8:
ret = PIXMAN_a8;
break;
case CAIRO_FORMAT_RGB24:
ret = PIXMAN_x8r8g8b8;
break;
case CAIRO_FORMAT_RGB30:
ret = PIXMAN_x2r10g10b10;
break;
case CAIRO_FORMAT_RGB16_565:
ret = PIXMAN_r5g6b5;
break;
case CAIRO_FORMAT_RGB96F:
ret = PIXMAN_rgb_float;
break;
case CAIRO_FORMAT_RGBA128F:
ret = PIXMAN_rgba_float;
break;
case CAIRO_FORMAT_ARGB32:
case CAIRO_FORMAT_INVALID:
default:
ret = PIXMAN_a8r8g8b8;
break;
}
return ret;
}
cairo_surface_t *
_cairo_image_surface_create_with_pixman_format (unsigned char *data,
pixman_format_code_t pixman_format,
int width,
int height,
int stride)
{
cairo_surface_t *surface;
pixman_image_t *pixman_image;
if (! _cairo_image_surface_is_size_valid (width, height))
{
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
}
pixman_image = pixman_image_create_bits (pixman_format, width, height,
(uint32_t *) data, stride);
if (unlikely (pixman_image == NULL))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
surface = _cairo_image_surface_create_for_pixman_image (pixman_image,
pixman_format);
if (unlikely (surface->status)) {
pixman_image_unref (pixman_image);
return surface;
}
/* we can not make any assumptions about the initial state of user data */
surface->is_clear = data == NULL;
return surface;
}
/**
* cairo_image_surface_create:
* @format: format of pixels in the surface to create
* @width: width of the surface, in pixels
* @height: height of the surface, in pixels
*
* Creates an image surface of the specified format and
* dimensions. Initially the surface contents are set to 0.
* (Specifically, within each pixel, each color or alpha channel
* belonging to format will be 0. The contents of bits within a pixel,
* but not belonging to the given format are undefined).
*
* Return value: a pointer to the newly created surface. The caller
* owns the surface and should call cairo_surface_destroy() when done
* with it.
*
* This function always returns a valid pointer, but it will return a
* pointer to a "nil" surface if an error such as out of memory
* occurs. You can use cairo_surface_status() to check for this.
*
* Since: 1.0
**/
cairo_surface_t *
cairo_image_surface_create (cairo_format_t format,
int width,
int height)
{
pixman_format_code_t pixman_format;
if (! CAIRO_FORMAT_VALID (format))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
pixman_format = _cairo_format_to_pixman_format_code (format);
return _cairo_image_surface_create_with_pixman_format (NULL, pixman_format,
width, height, -1);
}
slim_hidden_def (cairo_image_surface_create);
cairo_surface_t *
_cairo_image_surface_create_with_content (cairo_content_t content,
int width,
int height)
{
return cairo_image_surface_create (_cairo_format_from_content (content),
width, height);
}
/**
* cairo_format_stride_for_width:
* @format: A #cairo_format_t value
* @width: The desired width of an image surface to be created.
*
* This function provides a stride value that will respect all
* alignment requirements of the accelerated image-rendering code
* within cairo. Typical usage will be of the form:
*
* <informalexample><programlisting>
* int stride;
* unsigned char *data;
* cairo_surface_t *surface;
*
* stride = cairo_format_stride_for_width (format, width);
* data = malloc (stride * height);
* surface = cairo_image_surface_create_for_data (data, format,
* width, height,
* stride);
* </programlisting></informalexample>
*
* Return value: the appropriate stride to use given the desired
* format and width, or -1 if either the format is invalid or the width
* too large.
*
* Since: 1.6
**/
int
cairo_format_stride_for_width (cairo_format_t format,
int width)
{
int bpp;
if (! CAIRO_FORMAT_VALID (format)) {
_cairo_error_throw (CAIRO_STATUS_INVALID_FORMAT);
return -1;
}
bpp = _cairo_format_bits_per_pixel (format);
if ((unsigned) (width) >= (INT32_MAX - 7) / (unsigned) (bpp))
return -1;
return CAIRO_STRIDE_FOR_WIDTH_BPP (width, bpp);
}
slim_hidden_def (cairo_format_stride_for_width);
/**
* cairo_image_surface_create_for_data:
* @data: a pointer to a buffer supplied by the application in which
* to write contents. This pointer must be suitably aligned for any
* kind of variable, (for example, a pointer returned by malloc).
* @format: the format of pixels in the buffer
* @width: the width of the image to be stored in the buffer
* @height: the height of the image to be stored in the buffer
* @stride: the number of bytes between the start of rows in the
* buffer as allocated. This value should always be computed by
* cairo_format_stride_for_width() before allocating the data
* buffer.
*
* Creates an image surface for the provided pixel data. The output
* buffer must be kept around until the #cairo_surface_t is destroyed
* or cairo_surface_finish() is called on the surface. The initial
* contents of @data will be used as the initial image contents; you
* must explicitly clear the buffer, using, for example,
* cairo_rectangle() and cairo_fill() if you want it cleared.
*
* Note that the stride may be larger than
* width*bytes_per_pixel to provide proper alignment for each pixel
* and row. This alignment is required to allow high-performance rendering
* within cairo. The correct way to obtain a legal stride value is to
* call cairo_format_stride_for_width() with the desired format and
* maximum image width value, and then use the resulting stride value
* to allocate the data and to create the image surface. See
* cairo_format_stride_for_width() for example code.
*
* Return value: a pointer to the newly created surface. The caller
* owns the surface and should call cairo_surface_destroy() when done
* with it.
*
* This function always returns a valid pointer, but it will return a
* pointer to a "nil" surface in the case of an error such as out of
* memory or an invalid stride value. In case of invalid stride value
* the error status of the returned surface will be
* %CAIRO_STATUS_INVALID_STRIDE. You can use
* cairo_surface_status() to check for this.
*
* See cairo_surface_set_user_data() for a means of attaching a
* destroy-notification fallback to the surface if necessary.
*
* Since: 1.0
**/
cairo_surface_t *
cairo_image_surface_create_for_data (unsigned char *data,
cairo_format_t format,
int width,
int height,
int stride)
{
pixman_format_code_t pixman_format;
int minstride;
if (! CAIRO_FORMAT_VALID (format))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
if ((stride & (CAIRO_STRIDE_ALIGNMENT-1)) != 0)
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
if (! _cairo_image_surface_is_size_valid (width, height))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
minstride = cairo_format_stride_for_width (format, width);
if (stride < 0) {
if (stride > -minstride) {
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
}
} else {
if (stride < minstride) {
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
}
}
pixman_format = _cairo_format_to_pixman_format_code (format);
return _cairo_image_surface_create_with_pixman_format (data,
pixman_format,
width, height,
stride);
}
slim_hidden_def (cairo_image_surface_create_for_data);
/**
* cairo_image_surface_get_data:
* @surface: a #cairo_image_surface_t
*
* Get a pointer to the data of the image surface, for direct
* inspection or modification.
*
* A call to cairo_surface_flush() is required before accessing the
* pixel data to ensure that all pending drawing operations are
* finished. A call to cairo_surface_mark_dirty() is required after
* the data is modified.
*
* Return value: a pointer to the image data of this surface or %NULL
* if @surface is not an image surface, or if cairo_surface_finish()
* has been called.
*
* Since: 1.2
**/
unsigned char *
cairo_image_surface_get_data (cairo_surface_t *surface)
{
cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
if (! _cairo_surface_is_image (surface)) {
_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
return NULL;
}
return image_surface->data;
}
slim_hidden_def (cairo_image_surface_get_data);
/**
* cairo_image_surface_get_format:
* @surface: a #cairo_image_surface_t
*
* Get the format of the surface.
*
* Return value: the format of the surface
*
* Since: 1.2
**/
cairo_format_t
cairo_image_surface_get_format (cairo_surface_t *surface)
{
cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
if (! _cairo_surface_is_image (surface)) {
_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
return CAIRO_FORMAT_INVALID;
}
return image_surface->format;
}
slim_hidden_def (cairo_image_surface_get_format);
/**
* cairo_image_surface_get_width:
* @surface: a #cairo_image_surface_t
*
* Get the width of the image surface in pixels.
*
* Return value: the width of the surface in pixels.
*
* Since: 1.0
**/
int
cairo_image_surface_get_width (cairo_surface_t *surface)
{
cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
if (! _cairo_surface_is_image (surface)) {
_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
return 0;
}
return image_surface->width;
}
slim_hidden_def (cairo_image_surface_get_width);
/**
* cairo_image_surface_get_height:
* @surface: a #cairo_image_surface_t
*
* Get the height of the image surface in pixels.
*
* Return value: the height of the surface in pixels.
*
* Since: 1.0
**/
int
cairo_image_surface_get_height (cairo_surface_t *surface)
{
cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
if (! _cairo_surface_is_image (surface)) {
_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
return 0;
}
return image_surface->height;
}
slim_hidden_def (cairo_image_surface_get_height);
/**
* cairo_image_surface_get_stride:
* @surface: a #cairo_image_surface_t
*
* Get the stride of the image surface in bytes
*
* Return value: the stride of the image surface in bytes (or 0 if
* @surface is not an image surface). The stride is the distance in
* bytes from the beginning of one row of the image data to the
* beginning of the next row.
*
* Since: 1.2
**/
int
cairo_image_surface_get_stride (cairo_surface_t *surface)
{
cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
if (! _cairo_surface_is_image (surface)) {
_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
return 0;
}
return image_surface->stride;
}
slim_hidden_def (cairo_image_surface_get_stride);
cairo_format_t
_cairo_format_from_content (cairo_content_t content)
{
switch (content) {
case CAIRO_CONTENT_COLOR:
return CAIRO_FORMAT_RGB24;
case CAIRO_CONTENT_ALPHA:
return CAIRO_FORMAT_A8;
case CAIRO_CONTENT_COLOR_ALPHA:
return CAIRO_FORMAT_ARGB32;
}
ASSERT_NOT_REACHED;
return CAIRO_FORMAT_INVALID;
}
cairo_content_t
_cairo_content_from_format (cairo_format_t format)
{
switch (format) {
case CAIRO_FORMAT_RGBA128F:
case CAIRO_FORMAT_ARGB32:
return CAIRO_CONTENT_COLOR_ALPHA;
case CAIRO_FORMAT_RGB96F:
case CAIRO_FORMAT_RGB30:
return CAIRO_CONTENT_COLOR;
case CAIRO_FORMAT_RGB24:
return CAIRO_CONTENT_COLOR;
case CAIRO_FORMAT_RGB16_565:
return CAIRO_CONTENT_COLOR;
case CAIRO_FORMAT_A8:
case CAIRO_FORMAT_A1:
return CAIRO_CONTENT_ALPHA;
case CAIRO_FORMAT_INVALID:
break;
}
ASSERT_NOT_REACHED;
return CAIRO_CONTENT_COLOR_ALPHA;
}
int
_cairo_format_bits_per_pixel (cairo_format_t format)
{
switch (format) {
case CAIRO_FORMAT_RGBA128F:
return 128;
case CAIRO_FORMAT_RGB96F:
return 96;
case CAIRO_FORMAT_ARGB32:
case CAIRO_FORMAT_RGB30:
case CAIRO_FORMAT_RGB24:
return 32;
case CAIRO_FORMAT_RGB16_565:
return 16;
case CAIRO_FORMAT_A8:
return 8;
case CAIRO_FORMAT_A1:
return 1;
case CAIRO_FORMAT_INVALID:
default:
ASSERT_NOT_REACHED;
return 0;
}
}
cairo_surface_t *
_cairo_image_surface_create_similar (void *abstract_other,
cairo_content_t content,
int width,
int height)
{
cairo_image_surface_t *other = abstract_other;
TRACE ((stderr, "%s (other=%u)\n", __FUNCTION__, other->base.unique_id));
if (! _cairo_image_surface_is_size_valid (width, height))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
if (content == other->base.content) {
return _cairo_image_surface_create_with_pixman_format (NULL,
other->pixman_format,
width, height,
0);
}
return _cairo_image_surface_create_with_content (content,
width, height);
}
cairo_surface_t *
_cairo_image_surface_snapshot (void *abstract_surface)
{
cairo_image_surface_t *image = abstract_surface;
cairo_image_surface_t *clone;
/* If we own the image, we can simply steal the memory for the snapshot */
if (image->owns_data && image->base._finishing) {
clone = (cairo_image_surface_t *)
_cairo_image_surface_create_for_pixman_image (image->pixman_image,
image->pixman_format);
if (unlikely (clone->base.status))
return &clone->base;
image->pixman_image = NULL;
image->owns_data = FALSE;
clone->transparency = image->transparency;
clone->color = image->color;
clone->owns_data = TRUE;
return &clone->base;
}
clone = (cairo_image_surface_t *)
_cairo_image_surface_create_with_pixman_format (NULL,
image->pixman_format,
image->width,
image->height,
0);
if (unlikely (clone->base.status))
return &clone->base;
if (clone->stride == image->stride) {
memcpy (clone->data, image->data, clone->stride * clone->height);
} else {
pixman_image_composite32 (PIXMAN_OP_SRC,
image->pixman_image, NULL, clone->pixman_image,
0, 0,
0, 0,
0, 0,
image->width, image->height);
}
clone->base.is_clear = FALSE;
return &clone->base;
}
cairo_image_surface_t *
_cairo_image_surface_map_to_image (void *abstract_other,
const cairo_rectangle_int_t *extents)
{
cairo_image_surface_t *other = abstract_other;
cairo_surface_t *surface;
uint8_t *data;
data = other->data;
data += extents->y * other->stride;
data += extents->x * PIXMAN_FORMAT_BPP (other->pixman_format)/ 8;
surface =
_cairo_image_surface_create_with_pixman_format (data,
other->pixman_format,
extents->width,
extents->height,
other->stride);
cairo_surface_set_device_offset (surface, -extents->x, -extents->y);
return (cairo_image_surface_t *) surface;
}
cairo_int_status_t
_cairo_image_surface_unmap_image (void *abstract_surface,
cairo_image_surface_t *image)
{
cairo_surface_finish (&image->base);
cairo_surface_destroy (&image->base);
return CAIRO_INT_STATUS_SUCCESS;
}
cairo_status_t
_cairo_image_surface_finish (void *abstract_surface)
{
cairo_image_surface_t *surface = abstract_surface;
if (surface->pixman_image) {
pixman_image_unref (surface->pixman_image);
surface->pixman_image = NULL;
}
if (surface->owns_data) {
free (surface->data);
surface->data = NULL;
}
if (surface->parent) {
cairo_surface_t *parent = surface->parent;
surface->parent = NULL;
cairo_surface_destroy (parent);
}
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_image_surface_assume_ownership_of_data (cairo_image_surface_t *surface)
{
surface->owns_data = TRUE;
}
cairo_surface_t *
_cairo_image_surface_source (void *abstract_surface,
cairo_rectangle_int_t *extents)
{
cairo_image_surface_t *surface = abstract_surface;
if (extents) {
extents->x = extents->y = 0;
extents->width = surface->width;
extents->height = surface->height;
}
return &surface->base;
}
cairo_status_t
_cairo_image_surface_acquire_source_image (void *abstract_surface,
cairo_image_surface_t **image_out,
void **image_extra)
{
*image_out = abstract_surface;
*image_extra = NULL;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_image_surface_release_source_image (void *abstract_surface,
cairo_image_surface_t *image,
void *image_extra)
{
}
/* high level image interface */
cairo_bool_t
_cairo_image_surface_get_extents (void *abstract_surface,
cairo_rectangle_int_t *rectangle)
{
cairo_image_surface_t *surface = abstract_surface;
rectangle->x = 0;
rectangle->y = 0;
rectangle->width = surface->width;
rectangle->height = surface->height;
return TRUE;
}
cairo_int_status_t
_cairo_image_surface_paint (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_clip_t *clip)
{
cairo_image_surface_t *surface = abstract_surface;
TRACE ((stderr, "%s (surface=%d)\n",
__FUNCTION__, surface->base.unique_id));
return _cairo_compositor_paint (surface->compositor,
&surface->base, op, source, clip);
}
cairo_int_status_t
_cairo_image_surface_mask (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_pattern_t *mask,
const cairo_clip_t *clip)
{
cairo_image_surface_t *surface = abstract_surface;
TRACE ((stderr, "%s (surface=%d)\n",
__FUNCTION__, surface->base.unique_id));
return _cairo_compositor_mask (surface->compositor,
&surface->base, op, source, mask, clip);
}
cairo_int_status_t
_cairo_image_surface_stroke (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_antialias_t antialias,
const cairo_clip_t *clip)
{
cairo_image_surface_t *surface = abstract_surface;
TRACE ((stderr, "%s (surface=%d)\n",
__FUNCTION__, surface->base.unique_id));
return _cairo_compositor_stroke (surface->compositor, &surface->base,
op, source, path,
style, ctm, ctm_inverse,
tolerance, antialias, clip);
}
cairo_int_status_t
_cairo_image_surface_fill (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias,
const cairo_clip_t *clip)
{
cairo_image_surface_t *surface = abstract_surface;
TRACE ((stderr, "%s (surface=%d)\n",
__FUNCTION__, surface->base.unique_id));
return _cairo_compositor_fill (surface->compositor, &surface->base,
op, source, path,
fill_rule, tolerance, antialias,
clip);
}
cairo_int_status_t
_cairo_image_surface_glyphs (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_scaled_font_t *scaled_font,
const cairo_clip_t *clip)
{
cairo_image_surface_t *surface = abstract_surface;
TRACE ((stderr, "%s (surface=%d)\n",
__FUNCTION__, surface->base.unique_id));
return _cairo_compositor_glyphs (surface->compositor, &surface->base,
op, source,
glyphs, num_glyphs, scaled_font,
clip);
}
void
_cairo_image_surface_get_font_options (void *abstract_surface,
cairo_font_options_t *options)
{
_cairo_font_options_init_default (options);
cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
_cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
}
const cairo_surface_backend_t _cairo_image_surface_backend = {
CAIRO_SURFACE_TYPE_IMAGE,
_cairo_image_surface_finish,
_cairo_default_context_create,
_cairo_image_surface_create_similar,
NULL, /* create similar image */
_cairo_image_surface_map_to_image,
_cairo_image_surface_unmap_image,
_cairo_image_surface_source,
_cairo_image_surface_acquire_source_image,
_cairo_image_surface_release_source_image,
_cairo_image_surface_snapshot,
NULL, /* copy_page */
NULL, /* show_page */
_cairo_image_surface_get_extents,
_cairo_image_surface_get_font_options,
NULL, /* flush */
NULL,
_cairo_image_surface_paint,
_cairo_image_surface_mask,
_cairo_image_surface_stroke,
_cairo_image_surface_fill,
NULL, /* fill-stroke */
_cairo_image_surface_glyphs,
};
/* A convenience function for when one needs to coerce an image
* surface to an alternate format. */
cairo_image_surface_t *
_cairo_image_surface_coerce (cairo_image_surface_t *surface)
{
return _cairo_image_surface_coerce_to_format (surface,
_cairo_format_from_content (surface->base.content));
}
/* A convenience function for when one needs to coerce an image
* surface to an alternate format. */
cairo_image_surface_t *
_cairo_image_surface_coerce_to_format (cairo_image_surface_t *surface,
cairo_format_t format)
{
cairo_image_surface_t *clone;
cairo_status_t status;
status = surface->base.status;
if (unlikely (status))
return (cairo_image_surface_t *)_cairo_surface_create_in_error (status);
if (surface->format == format)
return (cairo_image_surface_t *)cairo_surface_reference(&surface->base);
clone = (cairo_image_surface_t *)
cairo_image_surface_create (format, surface->width, surface->height);
if (unlikely (clone->base.status))
return clone;
pixman_image_composite32 (PIXMAN_OP_SRC,
surface->pixman_image, NULL, clone->pixman_image,
0, 0,
0, 0,
0, 0,
surface->width, surface->height);
clone->base.is_clear = FALSE;
clone->base.device_transform =
surface->base.device_transform;
clone->base.device_transform_inverse =
surface->base.device_transform_inverse;
return clone;
}
cairo_image_surface_t *
_cairo_image_surface_create_from_image (cairo_image_surface_t *other,
pixman_format_code_t format,
int x, int y,
int width, int height, int stride)
{
cairo_image_surface_t *surface;
cairo_status_t status;
pixman_image_t *image;
void *mem = NULL;
status = other->base.status;
if (unlikely (status))
goto cleanup;
if (stride) {
mem = _cairo_malloc_ab (height, stride);
if (unlikely (mem == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto cleanup;
}
}
image = pixman_image_create_bits (format, width, height, mem, stride);
if (unlikely (image == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto cleanup_mem;
}
surface = (cairo_image_surface_t *)
_cairo_image_surface_create_for_pixman_image (image, format);
if (unlikely (surface->base.status)) {
status = surface->base.status;
goto cleanup_image;
}
pixman_image_composite32 (PIXMAN_OP_SRC,
other->pixman_image, NULL, image,
x, y,
0, 0,
0, 0,
width, height);
surface->base.is_clear = FALSE;
surface->owns_data = mem != NULL;
return surface;
cleanup_image:
pixman_image_unref (image);
cleanup_mem:
free (mem);
cleanup:
return (cairo_image_surface_t *) _cairo_surface_create_in_error (status);
}
static cairo_image_transparency_t
_cairo_image_compute_transparency (cairo_image_surface_t *image)
{
int x, y;
cairo_image_transparency_t transparency;
if ((image->base.content & CAIRO_CONTENT_ALPHA) == 0)
return CAIRO_IMAGE_IS_OPAQUE;
if (image->base.is_clear)
return CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
if ((image->base.content & CAIRO_CONTENT_COLOR) == 0) {
if (image->format == CAIRO_FORMAT_A1) {
return CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
} else if (image->format == CAIRO_FORMAT_A8) {
for (y = 0; y < image->height; y++) {
uint8_t *alpha = (uint8_t *) (image->data + y * image->stride);
for (x = 0; x < image->width; x++, alpha++) {
if (*alpha > 0 && *alpha < 255)
return CAIRO_IMAGE_HAS_ALPHA;
}
}
return CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
} else {
return CAIRO_IMAGE_HAS_ALPHA;
}
}
if (image->format == CAIRO_FORMAT_RGB16_565) {
return CAIRO_IMAGE_IS_OPAQUE;
}
if (image->format != CAIRO_FORMAT_ARGB32)
return CAIRO_IMAGE_HAS_ALPHA;
transparency = CAIRO_IMAGE_IS_OPAQUE;
for (y = 0; y < image->height; y++) {
uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
for (x = 0; x < image->width; x++, pixel++) {
int a = (*pixel & 0xff000000) >> 24;
if (a > 0 && a < 255) {
return CAIRO_IMAGE_HAS_ALPHA;
} else if (a == 0) {
transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
}
}
}
return transparency;
}
cairo_image_transparency_t
_cairo_image_analyze_transparency (cairo_image_surface_t *image)
{
if (_cairo_surface_is_snapshot (&image->base)) {
if (image->transparency == CAIRO_IMAGE_UNKNOWN)
image->transparency = _cairo_image_compute_transparency (image);
return image->transparency;
}
return _cairo_image_compute_transparency (image);
}
static cairo_image_color_t
_cairo_image_compute_color (cairo_image_surface_t *image)
{
int x, y;
cairo_image_color_t color;
if (image->format == CAIRO_FORMAT_A1)
return CAIRO_IMAGE_IS_MONOCHROME;
if (image->format == CAIRO_FORMAT_A8)
return CAIRO_IMAGE_IS_GRAYSCALE;
if (image->format == CAIRO_FORMAT_ARGB32) {
color = CAIRO_IMAGE_IS_MONOCHROME;
for (y = 0; y < image->height; y++) {
uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
for (x = 0; x < image->width; x++, pixel++) {
int a = (*pixel & 0xff000000) >> 24;
int r = (*pixel & 0x00ff0000) >> 16;
int g = (*pixel & 0x0000ff00) >> 8;
int b = (*pixel & 0x000000ff);
if (a == 0) {
r = g = b = 0;
} else {
r = (r * 255 + a / 2) / a;
g = (g * 255 + a / 2) / a;
b = (b * 255 + a / 2) / a;
}
if (!(r == g && g == b))
return CAIRO_IMAGE_IS_COLOR;
else if (r > 0 && r < 255)
color = CAIRO_IMAGE_IS_GRAYSCALE;
}
}
return color;
}
if (image->format == CAIRO_FORMAT_RGB24) {
color = CAIRO_IMAGE_IS_MONOCHROME;
for (y = 0; y < image->height; y++) {
uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
for (x = 0; x < image->width; x++, pixel++) {
int r = (*pixel & 0x00ff0000) >> 16;
int g = (*pixel & 0x0000ff00) >> 8;
int b = (*pixel & 0x000000ff);
if (!(r == g && g == b))
return CAIRO_IMAGE_IS_COLOR;
else if (r > 0 && r < 255)
color = CAIRO_IMAGE_IS_GRAYSCALE;
}
}
return color;
}
return CAIRO_IMAGE_IS_COLOR;
}
cairo_image_color_t
_cairo_image_analyze_color (cairo_image_surface_t *image)
{
if (_cairo_surface_is_snapshot (&image->base)) {
if (image->color == CAIRO_IMAGE_UNKNOWN_COLOR)
image->color = _cairo_image_compute_color (image);
return image->color;
}
return _cairo_image_compute_color (image);
}
cairo_image_surface_t *
_cairo_image_surface_clone_subimage (cairo_surface_t *surface,
const cairo_rectangle_int_t *extents)
{
cairo_surface_t *image;
cairo_surface_pattern_t pattern;
cairo_status_t status;
image = cairo_surface_create_similar_image (surface,
_cairo_format_from_content (surface->content),
extents->width,
extents->height);
if (image->status)
return to_image_surface (image);
/* TODO: check me with non-identity device_transform. Should we
* clone the scaling, too? */
cairo_surface_set_device_offset (image,
-extents->x,
-extents->y);
_cairo_pattern_init_for_surface (&pattern, surface);
pattern.base.filter = CAIRO_FILTER_NEAREST;
status = _cairo_surface_paint (image,
CAIRO_OPERATOR_SOURCE,
&pattern.base,
NULL);
_cairo_pattern_fini (&pattern.base);
if (unlikely (status))
goto error;
/* We use the parent as a flag during map-to-image/umap-image that the
* resultant image came from a fallback rather than as direct call
* to the backend's map_to_image(). Whilst we use it as a simple flag,
* we need to make sure the parent surface obeys the reference counting
* semantics and is consistent for all callers.
*/
_cairo_image_surface_set_parent (to_image_surface (image),
cairo_surface_reference (surface));
return to_image_surface (image);
error:
cairo_surface_destroy (image);
return to_image_surface (_cairo_surface_create_in_error (status));
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-line-inline.h | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2014 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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.
*
*/
#ifndef CAIRO_LINE_INLINE_H
#define CAIRO_LINE_INLINE_H
#include "cairo-types-private.h"
#include "cairo-compiler-private.h"
#include "cairo-fixed-private.h"
#include "cairo-line-private.h"
static inline int
cairo_lines_equal (const cairo_line_t *a, const cairo_line_t *b)
{
return (a->p1.x == b->p1.x && a->p1.y == b->p1.y &&
a->p2.x == b->p2.x && a->p2.y == b->p2.y);
}
#endif /* CAIRO_LINE_INLINE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-line-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2014 Intel Corporation, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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.
*
*/
#ifndef CAIRO_LINE_PRIVATE_H
#define CAIRO_LINE_PRIVATE_H
#include "cairo-types-private.h"
#include "cairo-error-private.h"
#include "cairo-compiler-private.h"
CAIRO_BEGIN_DECLS
cairo_private int
cairo_lines_compare_at_y(const cairo_line_t *a,
const cairo_line_t *b,
int y);
CAIRO_END_DECLS
#endif /* CAIRO_LINE_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-line.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/*
* Copyright © 2004 Carl Worth
* Copyright © 2006 Red Hat, Inc.
* Copyright © 2008 Chris Wilson
* Copyright © 2014 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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):
* Carl D. Worth <cworth@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*
*/
#include "cairoint.h"
#include "cairo-line-inline.h"
#include "cairo-slope-private.h"
static int
line_compare_for_y_against_x (const cairo_line_t *a,
int32_t y,
int32_t x)
{
int32_t adx, ady;
int32_t dx, dy;
cairo_int64_t L, R;
if (x < a->p1.x && x < a->p2.x)
return 1;
if (x > a->p1.x && x > a->p2.x)
return -1;
adx = a->p2.x - a->p1.x;
dx = x - a->p1.x;
if (adx == 0)
return -dx;
if (dx == 0 || (adx ^ dx) < 0)
return adx;
dy = y - a->p1.y;
ady = a->p2.y - a->p1.y;
L = _cairo_int32x32_64_mul (dy, adx);
R = _cairo_int32x32_64_mul (dx, ady);
return _cairo_int64_cmp (L, R);
}
/*
* We need to compare the x-coordinates of a pair of lines for a particular y,
* without loss of precision.
*
* The x-coordinate along an edge for a given y is:
* X = A_x + (Y - A_y) * A_dx / A_dy
*
* So the inequality we wish to test is:
* A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy,
* where ∘ is our inequality operator.
*
* By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are
* all positive, so we can rearrange it thus without causing a sign change:
* A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy
* - (Y - A_y) * A_dx * B_dy
*
* Given the assumption that all the deltas fit within 32 bits, we can compute
* this comparison directly using 128 bit arithmetic. For certain, but common,
* input we can reduce this down to a single 32 bit compare by inspecting the
* deltas.
*
* (And put the burden of the work on developing fast 128 bit ops, which are
* required throughout the tessellator.)
*
* See the similar discussion for _slope_compare().
*/
static int
lines_compare_x_for_y_general (const cairo_line_t *a,
const cairo_line_t *b,
int32_t y)
{
/* XXX: We're assuming here that dx and dy will still fit in 32
* bits. That's not true in general as there could be overflow. We
* should prevent that before the tessellation algorithm
* begins.
*/
int32_t dx = 0;
int32_t adx = 0, ady = 0;
int32_t bdx = 0, bdy = 0;
enum {
HAVE_NONE = 0x0,
HAVE_DX = 0x1,
HAVE_ADX = 0x2,
HAVE_DX_ADX = HAVE_DX | HAVE_ADX,
HAVE_BDX = 0x4,
HAVE_DX_BDX = HAVE_DX | HAVE_BDX,
HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX,
HAVE_ALL = HAVE_DX | HAVE_ADX | HAVE_BDX
} have_dx_adx_bdx = HAVE_ALL;
ady = a->p2.y - a->p1.y;
adx = a->p2.x - a->p1.x;
if (adx == 0)
have_dx_adx_bdx &= ~HAVE_ADX;
bdy = b->p2.y - b->p1.y;
bdx = b->p2.x - b->p1.x;
if (bdx == 0)
have_dx_adx_bdx &= ~HAVE_BDX;
dx = a->p1.x - b->p1.x;
if (dx == 0)
have_dx_adx_bdx &= ~HAVE_DX;
#define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx)
#define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->p1.y)
#define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->p1.y)
switch (have_dx_adx_bdx) {
default:
case HAVE_NONE:
return 0;
case HAVE_DX:
/* A_dy * B_dy * (A_x - B_x) ∘ 0 */
return dx; /* ady * bdy is positive definite */
case HAVE_ADX:
/* 0 ∘ - (Y - A_y) * A_dx * B_dy */
return adx; /* bdy * (y - a->top.y) is positive definite */
case HAVE_BDX:
/* 0 ∘ (Y - B_y) * B_dx * A_dy */
return -bdx; /* ady * (y - b->top.y) is positive definite */
case HAVE_ADX_BDX:
/* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
if ((adx ^ bdx) < 0) {
return adx;
} else if (a->p1.y == b->p1.y) { /* common origin */
cairo_int64_t adx_bdy, bdx_ady;
/* ∴ A_dx * B_dy ∘ B_dx * A_dy */
adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
return _cairo_int64_cmp (adx_bdy, bdx_ady);
} else
return _cairo_int128_cmp (A, B);
case HAVE_DX_ADX:
/* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
if ((-adx ^ dx) < 0) {
return dx;
} else {
cairo_int64_t ady_dx, dy_adx;
ady_dx = _cairo_int32x32_64_mul (ady, dx);
dy_adx = _cairo_int32x32_64_mul (a->p1.y - y, adx);
return _cairo_int64_cmp (ady_dx, dy_adx);
}
case HAVE_DX_BDX:
/* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
if ((bdx ^ dx) < 0) {
return dx;
} else {
cairo_int64_t bdy_dx, dy_bdx;
bdy_dx = _cairo_int32x32_64_mul (bdy, dx);
dy_bdx = _cairo_int32x32_64_mul (y - b->p1.y, bdx);
return _cairo_int64_cmp (bdy_dx, dy_bdx);
}
case HAVE_ALL:
/* XXX try comparing (a->p2.x - b->p2.x) et al */
return _cairo_int128_cmp (L, _cairo_int128_sub (B, A));
}
#undef B
#undef A
#undef L
}
static int
lines_compare_x_for_y (const cairo_line_t *a,
const cairo_line_t *b,
int32_t y)
{
/* If the sweep-line is currently on an end-point of a line,
* then we know its precise x value (and considering that we often need to
* compare events at end-points, this happens frequently enough to warrant
* special casing).
*/
enum {
HAVE_NEITHER = 0x0,
HAVE_AX = 0x1,
HAVE_BX = 0x2,
HAVE_BOTH = HAVE_AX | HAVE_BX
} have_ax_bx = HAVE_BOTH;
int32_t ax = 0, bx = 0;
if (y == a->p1.y)
ax = a->p1.x;
else if (y == a->p2.y)
ax = a->p2.x;
else
have_ax_bx &= ~HAVE_AX;
if (y == b->p1.y)
bx = b->p1.x;
else if (y == b->p2.y)
bx = b->p2.x;
else
have_ax_bx &= ~HAVE_BX;
switch (have_ax_bx) {
default:
case HAVE_NEITHER:
return lines_compare_x_for_y_general (a, b, y);
case HAVE_AX:
return -line_compare_for_y_against_x (b, y, ax);
case HAVE_BX:
return line_compare_for_y_against_x (a, y, bx);
case HAVE_BOTH:
return ax - bx;
}
}
static int bbox_compare (const cairo_line_t *a,
const cairo_line_t *b)
{
int32_t amin, amax;
int32_t bmin, bmax;
if (a->p1.x < a->p2.x) {
amin = a->p1.x;
amax = a->p2.x;
} else {
amin = a->p2.x;
amax = a->p1.x;
}
if (b->p1.x < b->p2.x) {
bmin = b->p1.x;
bmax = b->p2.x;
} else {
bmin = b->p2.x;
bmax = b->p1.x;
}
if (amax < bmin)
return -1;
if (amin > bmax)
return +1;
return 0;
}
int cairo_lines_compare_at_y (const cairo_line_t *a,
const cairo_line_t *b,
int y)
{
cairo_slope_t sa, sb;
int ret;
if (cairo_lines_equal (a, b))
return 0;
/* Don't bother solving for abscissa if the edges' bounding boxes
* can be used to order them.
*/
ret = bbox_compare (a, b);
if (ret)
return ret;
ret = lines_compare_x_for_y (a, b, y);
if (ret)
return ret;
_cairo_slope_init (&sa, &a->p1, &a->p2);
_cairo_slope_init (&sb, &b->p1, &b->p2);
return _cairo_slope_compare (&sb, &sa);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-list-inline.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Chris Wilson.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*
*/
#ifndef CAIRO_LIST_INLINE_H
#define CAIRO_LIST_INLINE_H
#include "cairo-list-private.h"
#define cairo_list_entry(ptr, type, member) \
cairo_container_of(ptr, type, member)
#define cairo_list_first_entry(ptr, type, member) \
cairo_list_entry((ptr)->next, type, member)
#define cairo_list_last_entry(ptr, type, member) \
cairo_list_entry((ptr)->prev, type, member)
#define cairo_list_foreach(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
#define cairo_list_foreach_entry(pos, type, head, member) \
for (pos = cairo_list_entry((head)->next, type, member);\
&pos->member != (head); \
pos = cairo_list_entry(pos->member.next, type, member))
#define cairo_list_foreach_entry_safe(pos, n, type, head, member) \
for (pos = cairo_list_entry ((head)->next, type, member),\
n = cairo_list_entry (pos->member.next, type, member);\
&pos->member != (head); \
pos = n, n = cairo_list_entry (n->member.next, type, member))
#define cairo_list_foreach_entry_reverse(pos, type, head, member) \
for (pos = cairo_list_entry((head)->prev, type, member);\
&pos->member != (head); \
pos = cairo_list_entry(pos->member.prev, type, member))
#define cairo_list_foreach_entry_reverse_safe(pos, n, type, head, member) \
for (pos = cairo_list_entry((head)->prev, type, member),\
n = cairo_list_entry (pos->member.prev, type, member);\
&pos->member != (head); \
pos = n, n = cairo_list_entry (n->member.prev, type, member))
#ifdef CAIRO_LIST_DEBUG
static inline void
_cairo_list_validate (const cairo_list_t *link)
{
assert (link->next->prev == link);
assert (link->prev->next == link);
}
static inline void
cairo_list_validate (const cairo_list_t *head)
{
cairo_list_t *link;
cairo_list_foreach (link, head)
_cairo_list_validate (link);
}
static inline cairo_bool_t
cairo_list_is_empty (const cairo_list_t *head);
static inline void
cairo_list_validate_is_empty (const cairo_list_t *head)
{
assert (head->next == NULL || (cairo_list_is_empty (head) && head->next == head->prev));
}
#else
#define _cairo_list_validate(link)
#define cairo_list_validate(head)
#define cairo_list_validate_is_empty(head)
#endif
static inline void
cairo_list_init (cairo_list_t *entry)
{
entry->next = entry;
entry->prev = entry;
}
static inline void
__cairo_list_add (cairo_list_t *entry,
cairo_list_t *prev,
cairo_list_t *next)
{
next->prev = entry;
entry->next = next;
entry->prev = prev;
prev->next = entry;
}
static inline void
cairo_list_add (cairo_list_t *entry, cairo_list_t *head)
{
cairo_list_validate (head);
cairo_list_validate_is_empty (entry);
__cairo_list_add (entry, head, head->next);
cairo_list_validate (head);
}
static inline void
cairo_list_add_tail (cairo_list_t *entry, cairo_list_t *head)
{
cairo_list_validate (head);
cairo_list_validate_is_empty (entry);
__cairo_list_add (entry, head->prev, head);
cairo_list_validate (head);
}
static inline void
__cairo_list_del (cairo_list_t *prev, cairo_list_t *next)
{
next->prev = prev;
prev->next = next;
}
static inline void
_cairo_list_del (cairo_list_t *entry)
{
__cairo_list_del (entry->prev, entry->next);
}
static inline void
cairo_list_del (cairo_list_t *entry)
{
_cairo_list_del (entry);
cairo_list_init (entry);
}
static inline void
cairo_list_move (cairo_list_t *entry, cairo_list_t *head)
{
cairo_list_validate (head);
__cairo_list_del (entry->prev, entry->next);
__cairo_list_add (entry, head, head->next);
cairo_list_validate (head);
}
static inline void
cairo_list_move_tail (cairo_list_t *entry, cairo_list_t *head)
{
cairo_list_validate (head);
__cairo_list_del (entry->prev, entry->next);
__cairo_list_add (entry, head->prev, head);
cairo_list_validate (head);
}
static inline void
cairo_list_swap (cairo_list_t *entry, cairo_list_t *other)
{
__cairo_list_add (entry, other->prev, other->next);
cairo_list_init (other);
}
static inline cairo_bool_t
cairo_list_is_first (const cairo_list_t *entry,
const cairo_list_t *head)
{
cairo_list_validate (head);
return entry->prev == head;
}
static inline cairo_bool_t
cairo_list_is_last (const cairo_list_t *entry,
const cairo_list_t *head)
{
cairo_list_validate (head);
return entry->next == head;
}
static inline cairo_bool_t
cairo_list_is_empty (const cairo_list_t *head)
{
cairo_list_validate (head);
return head->next == head;
}
static inline cairo_bool_t
cairo_list_is_singular (const cairo_list_t *head)
{
cairo_list_validate (head);
return head->next == head || head->next == head->prev;
}
#endif /* CAIRO_LIST_INLINE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-list-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Chris Wilson.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*
*/
#ifndef CAIRO_LIST_PRIVATE_H
#define CAIRO_LIST_PRIVATE_H
#include "cairo-compiler-private.h"
/* Basic circular, doubly linked list implementation */
typedef struct _cairo_list {
struct _cairo_list *next, *prev;
} cairo_list_t;
#endif /* CAIRO_LIST_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-lzw.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2006 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
typedef struct _lzw_buf {
cairo_status_t status;
unsigned char *data;
int data_size;
int num_data;
uint32_t pending;
unsigned int pending_bits;
} lzw_buf_t;
/* An lzw_buf_t is a simple, growable chunk of memory for holding
* variable-size objects of up to 16 bits each.
*
* Initialize an lzw_buf_t to the given size in bytes.
*
* To store objects into the lzw_buf_t, call _lzw_buf_store_bits and
* when finished, call _lzw_buf_store_pending, (which flushes out the
* last few bits that hadn't yet made a complete byte yet).
*
* Instead of returning failure from any functions, lzw_buf_t provides
* a status value that the caller can query, (and should query at
* least once when done with the object). The status value will be
* either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY;
*/
static void
_lzw_buf_init (lzw_buf_t *buf, int size)
{
if (size == 0)
size = 16;
buf->status = CAIRO_STATUS_SUCCESS;
buf->data_size = size;
buf->num_data = 0;
buf->pending = 0;
buf->pending_bits = 0;
buf->data = _cairo_malloc (size);
if (unlikely (buf->data == NULL)) {
buf->data_size = 0;
buf->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
return;
}
}
/* Increase the buffer size by doubling.
*
* Returns %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*/
static cairo_status_t
_lzw_buf_grow (lzw_buf_t *buf)
{
int new_size = buf->data_size * 2;
unsigned char *new_data;
if (buf->status)
return buf->status;
new_data = NULL;
/* check for integer overflow */
if (new_size / 2 == buf->data_size)
new_data = realloc (buf->data, new_size);
if (unlikely (new_data == NULL)) {
free (buf->data);
buf->data_size = 0;
buf->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
return buf->status;
}
buf->data = new_data;
buf->data_size = new_size;
return CAIRO_STATUS_SUCCESS;
}
/* Store the lowest num_bits bits of values into buf.
*
* Note: The bits of value above size_in_bits must be 0, (so don't lie
* about the size).
*
* See also _lzw_buf_store_pending which must be called after the last
* call to _lzw_buf_store_bits.
*
* Sets buf->status to either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY.
*/
static void
_lzw_buf_store_bits (lzw_buf_t *buf, uint16_t value, int num_bits)
{
cairo_status_t status;
assert (value <= (1 << num_bits) - 1);
if (buf->status)
return;
buf->pending = (buf->pending << num_bits) | value;
buf->pending_bits += num_bits;
while (buf->pending_bits >= 8) {
if (buf->num_data >= buf->data_size) {
status = _lzw_buf_grow (buf);
if (unlikely (status))
return;
}
buf->data[buf->num_data++] = buf->pending >> (buf->pending_bits - 8);
buf->pending_bits -= 8;
}
}
/* Store the last remaining pending bits into the buffer.
*
* Note: This function must be called after the last call to
* _lzw_buf_store_bits.
*
* Sets buf->status to either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY.
*/
static void
_lzw_buf_store_pending (lzw_buf_t *buf)
{
cairo_status_t status;
if (buf->status)
return;
if (buf->pending_bits == 0)
return;
assert (buf->pending_bits < 8);
if (buf->num_data >= buf->data_size) {
status = _lzw_buf_grow (buf);
if (unlikely (status))
return;
}
buf->data[buf->num_data++] = buf->pending << (8 - buf->pending_bits);
buf->pending_bits = 0;
}
/* LZW defines a few magic code values */
#define LZW_CODE_CLEAR_TABLE 256
#define LZW_CODE_EOD 257
#define LZW_CODE_FIRST 258
/* We pack three separate values into a symbol as follows:
*
* 12 bits (31 down to 20): CODE: code value used to represent this symbol
* 12 bits (19 down to 8): PREV: previous code value in chain
* 8 bits ( 7 down to 0): NEXT: next byte value in chain
*/
typedef uint32_t lzw_symbol_t;
#define LZW_SYMBOL_SET(sym, prev, next) ((sym) = ((prev) << 8)|(next))
#define LZW_SYMBOL_SET_CODE(sym, code, prev, next) ((sym) = ((code << 20)|(prev) << 8)|(next))
#define LZW_SYMBOL_GET_CODE(sym) (((sym) >> 20))
#define LZW_SYMBOL_GET_PREV(sym) (((sym) >> 8) & 0x7ff)
#define LZW_SYMBOL_GET_BYTE(sym) (((sym) >> 0) & 0x0ff)
/* The PREV+NEXT fields can be seen as the key used to fetch values
* from the hash table, while the code is the value fetched.
*/
#define LZW_SYMBOL_KEY_MASK 0x000fffff
/* Since code values are only stored starting with 258 we can safely
* use a zero value to represent free slots in the hash table. */
#define LZW_SYMBOL_FREE 0x00000000
/* These really aren't very free for modifying. First, the PostScript
* specification sets the 9-12 bit range. Second, the encoding of
* lzw_symbol_t above also relies on 2 of LZW_BITS_MAX plus one byte
* fitting within 32 bits.
*
* But other than that, the LZW compression scheme could function with
* more bits per code.
*/
#define LZW_BITS_MIN 9
#define LZW_BITS_MAX 12
#define LZW_BITS_BOUNDARY(bits) ((1<<(bits))-1)
#define LZW_MAX_SYMBOLS (1<<LZW_BITS_MAX)
#define LZW_SYMBOL_TABLE_SIZE 9013
#define LZW_SYMBOL_MOD1 LZW_SYMBOL_TABLE_SIZE
#define LZW_SYMBOL_MOD2 9011
typedef struct _lzw_symbol_table {
lzw_symbol_t table[LZW_SYMBOL_TABLE_SIZE];
} lzw_symbol_table_t;
/* Initialize the hash table to entirely empty */
static void
_lzw_symbol_table_init (lzw_symbol_table_t *table)
{
memset (table->table, 0, LZW_SYMBOL_TABLE_SIZE * sizeof (lzw_symbol_t));
}
/* Lookup a symbol in the symbol table. The PREV and NEXT fields of
* symbol form the key for the lookup.
*
* If successful, then this function returns %TRUE and slot_ret will be
* left pointing at the result that will have the CODE field of
* interest.
*
* If the lookup fails, then this function returns %FALSE and slot_ret
* will be pointing at the location in the table to which a new CODE
* value should be stored along with PREV and NEXT.
*/
static cairo_bool_t
_lzw_symbol_table_lookup (lzw_symbol_table_t *table,
lzw_symbol_t symbol,
lzw_symbol_t **slot_ret)
{
/* The algorithm here is identical to that in cairo-hash.c. We
* copy it here to allow for a rather more efficient
* implementation due to several circumstances that do not apply
* to the more general case:
*
* 1) We have a known bound on the total number of symbols, so we
* have a fixed-size table without any copying when growing
*
* 2) We never delete any entries, so we don't need to
* support/check for DEAD entries during lookup.
*
* 3) The object fits in 32 bits so we store each object in its
* entirety within the table rather than storing objects
* externally and putting pointers in the table, (which here
* would just double the storage requirements and have negative
* impacts on memory locality).
*/
int i, idx, step, hash = symbol & LZW_SYMBOL_KEY_MASK;
lzw_symbol_t candidate;
idx = hash % LZW_SYMBOL_MOD1;
step = 0;
*slot_ret = NULL;
for (i = 0; i < LZW_SYMBOL_TABLE_SIZE; i++)
{
candidate = table->table[idx];
if (candidate == LZW_SYMBOL_FREE)
{
*slot_ret = &table->table[idx];
return FALSE;
}
else /* candidate is LIVE */
{
if ((candidate & LZW_SYMBOL_KEY_MASK) ==
(symbol & LZW_SYMBOL_KEY_MASK))
{
*slot_ret = &table->table[idx];
return TRUE;
}
}
if (step == 0) {
step = hash % LZW_SYMBOL_MOD2;
if (step == 0)
step = 1;
}
idx += step;
if (idx >= LZW_SYMBOL_TABLE_SIZE)
idx -= LZW_SYMBOL_TABLE_SIZE;
}
return FALSE;
}
/* Compress a bytestream using the LZW algorithm.
*
* This is an original implementation based on reading the
* specification of the LZWDecode filter in the PostScript Language
* Reference. The free parameters in the LZW algorithm are set to the
* values mandated by PostScript, (symbols encoded with widths from 9
* to 12 bits).
*
* This function returns a pointer to a newly allocated buffer holding
* the compressed data, or %NULL if an out-of-memory situation
* occurs.
*
* Notice that any one of the _lzw_buf functions called here could
* trigger an out-of-memory condition. But lzw_buf_t uses cairo's
* shutdown-on-error idiom, so it's safe to continue to call into
* lzw_buf without having to check for errors, (until a final check at
* the end).
*/
unsigned char *
_cairo_lzw_compress (unsigned char *data, unsigned long *size_in_out)
{
int bytes_remaining = *size_in_out;
lzw_buf_t buf;
lzw_symbol_table_t table;
lzw_symbol_t symbol, *slot = NULL; /* just to squelch a warning */
int code_next = LZW_CODE_FIRST;
int code_bits = LZW_BITS_MIN;
int prev, next = 0; /* just to squelch a warning */
if (*size_in_out == 0)
return NULL;
_lzw_buf_init (&buf, *size_in_out);
_lzw_symbol_table_init (&table);
/* The LZW header is a clear table code. */
_lzw_buf_store_bits (&buf, LZW_CODE_CLEAR_TABLE, code_bits);
while (1) {
/* Find the longest existing code in the symbol table that
* matches the current input, if any. */
prev = *data++;
bytes_remaining--;
if (bytes_remaining) {
do
{
next = *data++;
bytes_remaining--;
LZW_SYMBOL_SET (symbol, prev, next);
if (_lzw_symbol_table_lookup (&table, symbol, &slot))
prev = LZW_SYMBOL_GET_CODE (*slot);
} while (bytes_remaining && *slot != LZW_SYMBOL_FREE);
if (*slot == LZW_SYMBOL_FREE) {
data--;
bytes_remaining++;
}
}
/* Write the code into the output. This is either a byte read
* directly from the input, or a code from the last successful
* lookup. */
_lzw_buf_store_bits (&buf, prev, code_bits);
if (bytes_remaining == 0)
break;
LZW_SYMBOL_SET_CODE (*slot, code_next++, prev, next);
if (code_next > LZW_BITS_BOUNDARY(code_bits))
{
code_bits++;
if (code_bits > LZW_BITS_MAX) {
_lzw_symbol_table_init (&table);
_lzw_buf_store_bits (&buf, LZW_CODE_CLEAR_TABLE, code_bits - 1);
code_bits = LZW_BITS_MIN;
code_next = LZW_CODE_FIRST;
}
}
}
/* The LZW footer is an end-of-data code. */
_lzw_buf_store_bits (&buf, LZW_CODE_EOD, code_bits);
_lzw_buf_store_pending (&buf);
/* See if we ever ran out of memory while writing to buf. */
if (buf.status == CAIRO_STATUS_NO_MEMORY) {
*size_in_out = 0;
return NULL;
}
assert (buf.status == CAIRO_STATUS_SUCCESS);
*size_in_out = buf.num_data;
return buf.data;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-malloc-private.h | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* Cairo - a vector graphics library with display and print output
*
* Copyright © 2007 Mozilla Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Mozilla Foundation
*
* Contributor(s):
* Vladimir Vukicevic <vladimir@pobox.com>
*/
#ifndef CAIRO_MALLOC_PRIVATE_H
#define CAIRO_MALLOC_PRIVATE_H
#include "cairo-wideint-private.h"
#include <stdlib.h>
#if HAVE_MEMFAULT
#include <memfault.h>
#define CAIRO_INJECT_FAULT() MEMFAULT_INJECT_FAULT()
#else
#define CAIRO_INJECT_FAULT() 0
#endif
/**
* _cairo_malloc:
* @size: size in bytes
*
* Allocate @size memory using malloc().
* The memory should be freed using free().
* malloc is skipped, if 0 bytes are requested, and %NULL will be returned.
*
* Return value: A pointer to the newly allocated memory, or %NULL in
* case of malloc() failure or size is 0.
**/
#define _cairo_malloc(size) \
((size) > 0 ? malloc((unsigned) (size)) : NULL)
/**
* _cairo_malloc_ab:
* @a: number of elements to allocate
* @size: size of each element
*
* Allocates @a*@size memory using _cairo_malloc(), taking care to not
* overflow when doing the multiplication. Behaves much like
* calloc(), except that the returned memory is not set to zero.
* The memory should be freed using free().
*
* @size should be a constant so that the compiler can optimize
* out a constant division.
*
* Return value: A pointer to the newly allocated memory, or %NULL in
* case of malloc() failure or overflow.
**/
#define _cairo_malloc_ab(a, size) \
((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
_cairo_malloc((unsigned) (a) * (unsigned) (size)))
/**
* _cairo_realloc_ab:
* @ptr: original pointer to block of memory to be resized
* @a: number of elements to allocate
* @size: size of each element
*
* Reallocates @ptr a block of @a*@size memory using realloc(), taking
* care to not overflow when doing the multiplication. The memory
* should be freed using free().
*
* @size should be a constant so that the compiler can optimize
* out a constant division.
*
* Return value: A pointer to the newly allocated memory, or %NULL in
* case of realloc() failure or overflow (whereupon the original block
* of memory * is left untouched).
**/
#define _cairo_realloc_ab(ptr, a, size) \
((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
realloc(ptr, (unsigned) (a) * (unsigned) (size)))
/**
* _cairo_malloc_abc:
* @a: first factor of number of elements to allocate
* @b: second factor of number of elements to allocate
* @size: size of each element
*
* Allocates @a*@b*@size memory using _cairo_malloc(), taking care to not
* overflow when doing the multiplication. Behaves like
* _cairo_malloc_ab(). The memory should be freed using free().
*
* @size should be a constant so that the compiler can optimize
* out a constant division.
*
* Return value: A pointer to the newly allocated memory, or %NULL in
* case of malloc() failure or overflow.
**/
#define _cairo_malloc_abc(a, b, size) \
((b) && (unsigned) (a) >= INT32_MAX / (unsigned) (b) ? NULL : \
(size) && (unsigned) ((a)*(b)) >= INT32_MAX / (unsigned) (size) ? NULL : \
_cairo_malloc((unsigned) (a) * (unsigned) (b) * (unsigned) (size)))
/**
* _cairo_malloc_ab_plus_c:
* @a: number of elements to allocate
* @size: size of each element
* @c: additional size to allocate
*
* Allocates @a*@size+@c memory using _cairo_malloc(), taking care to not
* overflow when doing the arithmetic. Behaves similar to
* _cairo_malloc_ab(). The memory should be freed using free().
*
* Return value: A pointer to the newly allocated memory, or %NULL in
* case of malloc() failure or overflow.
**/
#define _cairo_malloc_ab_plus_c(a, size, c) \
((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
(unsigned) (c) >= INT32_MAX - (unsigned) (a) * (unsigned) (size) ? NULL : \
_cairo_malloc((unsigned) (a) * (unsigned) (size) + (unsigned) (c)))
#endif /* CAIRO_MALLOC_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mask-compositor.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
/* This compositor renders the shape to a mask using an image surface
* then calls composite.
*/
#include "cairoint.h"
#include "cairo-clip-inline.h"
#include "cairo-compositor-private.h"
#include "cairo-image-surface-private.h"
#include "cairo-pattern-inline.h"
#include "cairo-region-private.h"
#include "cairo-surface-observer-private.h"
#include "cairo-surface-offset-private.h"
#include "cairo-surface-snapshot-private.h"
#include "cairo-surface-subsurface-private.h"
typedef cairo_int_status_t
(*draw_func_t) (const cairo_mask_compositor_t *compositor,
cairo_surface_t *dst,
void *closure,
cairo_operator_t op,
const cairo_pattern_t *src,
const cairo_rectangle_int_t *src_sample,
int dst_x,
int dst_y,
const cairo_rectangle_int_t *extents,
cairo_clip_t *clip);
static void do_unaligned_row(void (*blt)(void *closure,
int16_t x, int16_t y,
int16_t w, int16_t h,
uint16_t coverage),
void *closure,
const cairo_box_t *b,
int tx, int y, int h,
uint16_t coverage)
{
int x1 = _cairo_fixed_integer_part (b->p1.x) - tx;
int x2 = _cairo_fixed_integer_part (b->p2.x) - tx;
if (x2 > x1) {
if (! _cairo_fixed_is_integer (b->p1.x)) {
blt(closure, x1, y, 1, h,
coverage * (256 - _cairo_fixed_fractional_part (b->p1.x)));
x1++;
}
if (x2 > x1)
blt(closure, x1, y, x2-x1, h, (coverage << 8) - (coverage >> 8));
if (! _cairo_fixed_is_integer (b->p2.x))
blt(closure, x2, y, 1, h,
coverage * _cairo_fixed_fractional_part (b->p2.x));
} else
blt(closure, x1, y, 1, h,
coverage * (b->p2.x - b->p1.x));
}
static void do_unaligned_box(void (*blt)(void *closure,
int16_t x, int16_t y,
int16_t w, int16_t h,
uint16_t coverage),
void *closure,
const cairo_box_t *b, int tx, int ty)
{
int y1 = _cairo_fixed_integer_part (b->p1.y) - ty;
int y2 = _cairo_fixed_integer_part (b->p2.y) - ty;
if (y2 > y1) {
if (! _cairo_fixed_is_integer (b->p1.y)) {
do_unaligned_row(blt, closure, b, tx, y1, 1,
256 - _cairo_fixed_fractional_part (b->p1.y));
y1++;
}
if (y2 > y1)
do_unaligned_row(blt, closure, b, tx, y1, y2-y1, 256);
if (! _cairo_fixed_is_integer (b->p2.y))
do_unaligned_row(blt, closure, b, tx, y2, 1,
_cairo_fixed_fractional_part (b->p2.y));
} else
do_unaligned_row(blt, closure, b, tx, y1, 1,
b->p2.y - b->p1.y);
}
struct blt_in {
const cairo_mask_compositor_t *compositor;
cairo_surface_t *dst;
};
static void blt_in(void *closure,
int16_t x, int16_t y,
int16_t w, int16_t h,
uint16_t coverage)
{
struct blt_in *info = closure;
cairo_color_t color;
cairo_rectangle_int_t rect;
if (coverage == 0xffff)
return;
rect.x = x;
rect.y = y;
rect.width = w;
rect.height = h;
_cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double) 0xffff);
info->compositor->fill_rectangles (info->dst, CAIRO_OPERATOR_IN,
&color, &rect, 1);
}
static cairo_surface_t *
create_composite_mask (const cairo_mask_compositor_t *compositor,
cairo_surface_t *dst,
void *draw_closure,
draw_func_t draw_func,
draw_func_t mask_func,
const cairo_composite_rectangles_t *extents)
{
cairo_surface_t *surface;
cairo_int_status_t status;
struct blt_in info;
int i;
surface = _cairo_surface_create_scratch (dst, CAIRO_CONTENT_ALPHA,
extents->bounded.width,
extents->bounded.height,
NULL);
if (unlikely (surface->status))
return surface;
status = compositor->acquire (surface);
if (unlikely (status)) {
cairo_surface_destroy (surface);
return _cairo_int_surface_create_in_error (status);
}
if (!surface->is_clear) {
cairo_rectangle_int_t rect;
rect.x = rect.y = 0;
rect.width = extents->bounded.width;
rect.height = extents->bounded.height;
status = compositor->fill_rectangles (surface, CAIRO_OPERATOR_CLEAR,
CAIRO_COLOR_TRANSPARENT,
&rect, 1);
if (unlikely (status))
goto error;
}
if (mask_func) {
status = mask_func (compositor, surface, draw_closure,
CAIRO_OPERATOR_SOURCE, NULL, NULL,
extents->bounded.x, extents->bounded.y,
&extents->bounded, extents->clip);
if (likely (status != CAIRO_INT_STATUS_UNSUPPORTED))
goto out;
}
/* Is it worth setting the clip region here? */
status = draw_func (compositor, surface, draw_closure,
CAIRO_OPERATOR_ADD, NULL, NULL,
extents->bounded.x, extents->bounded.y,
&extents->bounded, NULL);
if (unlikely (status))
goto error;
info.compositor = compositor;
info.dst = surface;
for (i = 0; i < extents->clip->num_boxes; i++) {
cairo_box_t *b = &extents->clip->boxes[i];
if (! _cairo_fixed_is_integer (b->p1.x) ||
! _cairo_fixed_is_integer (b->p1.y) ||
! _cairo_fixed_is_integer (b->p2.x) ||
! _cairo_fixed_is_integer (b->p2.y))
{
do_unaligned_box(blt_in, &info, b,
extents->bounded.x,
extents->bounded.y);
}
}
if (extents->clip->path != NULL) {
status = _cairo_clip_combine_with_surface (extents->clip, surface,
extents->bounded.x,
extents->bounded.y);
if (unlikely (status))
goto error;
}
out:
compositor->release (surface);
surface->is_clear = FALSE;
return surface;
error:
compositor->release (surface);
if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
cairo_surface_destroy (surface);
surface = _cairo_int_surface_create_in_error (status);
}
return surface;
}
/* Handles compositing with a clip surface when the operator allows
* us to combine the clip with the mask
*/
static cairo_status_t
clip_and_composite_with_mask (const cairo_mask_compositor_t *compositor,
void *draw_closure,
draw_func_t draw_func,
draw_func_t mask_func,
cairo_operator_t op,
cairo_pattern_t *pattern,
const cairo_composite_rectangles_t*extents)
{
cairo_surface_t *dst = extents->surface;
cairo_surface_t *mask, *src;
int src_x, src_y;
mask = create_composite_mask (compositor, dst, draw_closure,
draw_func, mask_func,
extents);
if (unlikely (mask->status))
return mask->status;
if (pattern != NULL || dst->content != CAIRO_CONTENT_ALPHA) {
src = compositor->pattern_to_surface (dst,
&extents->source_pattern.base,
FALSE,
&extents->bounded,
&extents->source_sample_area,
&src_x, &src_y);
if (unlikely (src->status)) {
cairo_surface_destroy (mask);
return src->status;
}
compositor->composite (dst, op, src, mask,
extents->bounded.x + src_x,
extents->bounded.y + src_y,
0, 0,
extents->bounded.x, extents->bounded.y,
extents->bounded.width, extents->bounded.height);
cairo_surface_destroy (src);
} else {
compositor->composite (dst, op, mask, NULL,
0, 0,
0, 0,
extents->bounded.x, extents->bounded.y,
extents->bounded.width, extents->bounded.height);
}
cairo_surface_destroy (mask);
return CAIRO_STATUS_SUCCESS;
}
static cairo_surface_t *
get_clip_source (const cairo_mask_compositor_t *compositor,
cairo_clip_t *clip,
cairo_surface_t *dst,
const cairo_rectangle_int_t *bounds,
int *out_x, int *out_y)
{
cairo_surface_pattern_t pattern;
cairo_rectangle_int_t r;
cairo_surface_t *surface;
surface = _cairo_clip_get_image (clip, dst, bounds);
if (unlikely (surface->status))
return surface;
_cairo_pattern_init_for_surface (&pattern, surface);
pattern.base.filter = CAIRO_FILTER_NEAREST;
cairo_surface_destroy (surface);
r.x = r.y = 0;
r.width = bounds->width;
r.height = bounds->height;
surface = compositor->pattern_to_surface (dst, &pattern.base, TRUE,
&r, &r, out_x, out_y);
_cairo_pattern_fini (&pattern.base);
*out_x += -bounds->x;
*out_y += -bounds->y;
return surface;
}
/* Handles compositing with a clip surface when we have to do the operation
* in two pieces and combine them together.
*/
static cairo_status_t
clip_and_composite_combine (const cairo_mask_compositor_t *compositor,
void *draw_closure,
draw_func_t draw_func,
cairo_operator_t op,
const cairo_pattern_t *pattern,
const cairo_composite_rectangles_t*extents)
{
cairo_surface_t *dst = extents->surface;
cairo_surface_t *tmp, *clip;
cairo_status_t status;
int clip_x, clip_y;
tmp = _cairo_surface_create_scratch (dst, dst->content,
extents->bounded.width,
extents->bounded.height,
NULL);
if (unlikely (tmp->status))
return tmp->status;
compositor->composite (tmp, CAIRO_OPERATOR_SOURCE, dst, NULL,
extents->bounded.x, extents->bounded.y,
0, 0,
0, 0,
extents->bounded.width, extents->bounded.height);
status = draw_func (compositor, tmp, draw_closure, op,
pattern, &extents->source_sample_area,
extents->bounded.x, extents->bounded.y,
&extents->bounded, NULL);
if (unlikely (status))
goto cleanup;
clip = get_clip_source (compositor,
extents->clip, dst, &extents->bounded,
&clip_x, &clip_y);
if (unlikely ((status = clip->status)))
goto cleanup;
if (dst->is_clear) {
compositor->composite (dst, CAIRO_OPERATOR_SOURCE, tmp, clip,
0, 0,
clip_x, clip_y,
extents->bounded.x, extents->bounded.y,
extents->bounded.width, extents->bounded.height);
} else {
/* Punch the clip out of the destination */
compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, clip, NULL,
clip_x, clip_y,
0, 0,
extents->bounded.x, extents->bounded.y,
extents->bounded.width, extents->bounded.height);
/* Now add the two results together */
compositor->composite (dst, CAIRO_OPERATOR_ADD, tmp, clip,
0, 0,
clip_x, clip_y,
extents->bounded.x, extents->bounded.y,
extents->bounded.width, extents->bounded.height);
}
cairo_surface_destroy (clip);
cleanup:
cairo_surface_destroy (tmp);
return status;
}
/* Handles compositing for %CAIRO_OPERATOR_SOURCE, which is special; it's
* defined as (src IN mask IN clip) ADD (dst OUT (mask IN clip))
*/
static cairo_status_t
clip_and_composite_source (const cairo_mask_compositor_t *compositor,
void *draw_closure,
draw_func_t draw_func,
draw_func_t mask_func,
cairo_pattern_t *pattern,
const cairo_composite_rectangles_t *extents)
{
cairo_surface_t *dst = extents->surface;
cairo_surface_t *mask, *src;
int src_x, src_y;
/* Create a surface that is mask IN clip */
mask = create_composite_mask (compositor, dst, draw_closure,
draw_func, mask_func,
extents);
if (unlikely (mask->status))
return mask->status;
src = compositor->pattern_to_surface (dst,
pattern,
FALSE,
&extents->bounded,
&extents->source_sample_area,
&src_x, &src_y);
if (unlikely (src->status)) {
cairo_surface_destroy (mask);
return src->status;
}
if (dst->is_clear) {
compositor->composite (dst, CAIRO_OPERATOR_SOURCE, src, mask,
extents->bounded.x + src_x, extents->bounded.y + src_y,
0, 0,
extents->bounded.x, extents->bounded.y,
extents->bounded.width, extents->bounded.height);
} else {
/* Compute dest' = dest OUT (mask IN clip) */
compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
0, 0, 0, 0,
extents->bounded.x, extents->bounded.y,
extents->bounded.width, extents->bounded.height);
/* Now compute (src IN (mask IN clip)) ADD dest' */
compositor->composite (dst, CAIRO_OPERATOR_ADD, src, mask,
extents->bounded.x + src_x, extents->bounded.y + src_y,
0, 0,
extents->bounded.x, extents->bounded.y,
extents->bounded.width, extents->bounded.height);
}
cairo_surface_destroy (src);
cairo_surface_destroy (mask);
return CAIRO_STATUS_SUCCESS;
}
static cairo_bool_t
can_reduce_alpha_op (cairo_operator_t op)
{
int iop = op;
switch (iop) {
case CAIRO_OPERATOR_OVER:
case CAIRO_OPERATOR_SOURCE:
case CAIRO_OPERATOR_ADD:
return TRUE;
default:
return FALSE;
}
}
static cairo_bool_t
reduce_alpha_op (cairo_surface_t *dst,
cairo_operator_t op,
const cairo_pattern_t *pattern)
{
return dst->is_clear &&
dst->content == CAIRO_CONTENT_ALPHA &&
_cairo_pattern_is_opaque_solid (pattern) &&
can_reduce_alpha_op (op);
}
static cairo_status_t
fixup_unbounded (const cairo_mask_compositor_t *compositor,
cairo_surface_t *dst,
const cairo_composite_rectangles_t *extents)
{
cairo_rectangle_int_t rects[4];
int n;
if (extents->bounded.width == extents->unbounded.width &&
extents->bounded.height == extents->unbounded.height)
{
return CAIRO_STATUS_SUCCESS;
}
n = 0;
if (extents->bounded.width == 0 || extents->bounded.height == 0) {
rects[n].x = extents->unbounded.x;
rects[n].width = extents->unbounded.width;
rects[n].y = extents->unbounded.y;
rects[n].height = extents->unbounded.height;
n++;
} else {
/* top */
if (extents->bounded.y != extents->unbounded.y) {
rects[n].x = extents->unbounded.x;
rects[n].width = extents->unbounded.width;
rects[n].y = extents->unbounded.y;
rects[n].height = extents->bounded.y - extents->unbounded.y;
n++;
}
/* left */
if (extents->bounded.x != extents->unbounded.x) {
rects[n].x = extents->unbounded.x;
rects[n].width = extents->bounded.x - extents->unbounded.x;
rects[n].y = extents->bounded.y;
rects[n].height = extents->bounded.height;
n++;
}
/* right */
if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
rects[n].x = extents->bounded.x + extents->bounded.width;
rects[n].width = extents->unbounded.x + extents->unbounded.width - rects[n].x;
rects[n].y = extents->bounded.y;
rects[n].height = extents->bounded.height;
n++;
}
/* bottom */
if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
rects[n].x = extents->unbounded.x;
rects[n].width = extents->unbounded.width;
rects[n].y = extents->bounded.y + extents->bounded.height;
rects[n].height = extents->unbounded.y + extents->unbounded.height - rects[n].y;
n++;
}
}
return compositor->fill_rectangles (dst, CAIRO_OPERATOR_CLEAR,
CAIRO_COLOR_TRANSPARENT,
rects, n);
}
static cairo_status_t
fixup_unbounded_with_mask (const cairo_mask_compositor_t *compositor,
cairo_surface_t *dst,
const cairo_composite_rectangles_t *extents)
{
cairo_surface_t *mask;
int mask_x, mask_y;
mask = get_clip_source (compositor,
extents->clip, dst, &extents->unbounded,
&mask_x, &mask_y);
if (unlikely (mask->status))
return mask->status;
/* top */
if (extents->bounded.y != extents->unbounded.y) {
int x = extents->unbounded.x;
int y = extents->unbounded.y;
int width = extents->unbounded.width;
int height = extents->bounded.y - y;
compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
x + mask_x, y + mask_y,
0, 0,
x, y,
width, height);
}
/* left */
if (extents->bounded.x != extents->unbounded.x) {
int x = extents->unbounded.x;
int y = extents->bounded.y;
int width = extents->bounded.x - x;
int height = extents->bounded.height;
compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
x + mask_x, y + mask_y,
0, 0,
x, y,
width, height);
}
/* right */
if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
int x = extents->bounded.x + extents->bounded.width;
int y = extents->bounded.y;
int width = extents->unbounded.x + extents->unbounded.width - x;
int height = extents->bounded.height;
compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
x + mask_x, y + mask_y,
0, 0,
x, y,
width, height);
}
/* bottom */
if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
int x = extents->unbounded.x;
int y = extents->bounded.y + extents->bounded.height;
int width = extents->unbounded.width;
int height = extents->unbounded.y + extents->unbounded.height - y;
compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
x + mask_x, y + mask_y,
0, 0,
x, y,
width, height);
}
cairo_surface_destroy (mask);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
fixup_unbounded_boxes (const cairo_mask_compositor_t *compositor,
const cairo_composite_rectangles_t *extents,
cairo_boxes_t *boxes)
{
cairo_surface_t *dst = extents->surface;
cairo_boxes_t clear;
cairo_region_t *clip_region;
cairo_box_t box;
cairo_status_t status;
struct _cairo_boxes_chunk *chunk;
int i;
assert (boxes->is_pixel_aligned);
clip_region = NULL;
if (_cairo_clip_is_region (extents->clip) &&
(clip_region = _cairo_clip_get_region (extents->clip)) &&
cairo_region_contains_rectangle (clip_region,
&extents->bounded) == CAIRO_REGION_OVERLAP_IN)
clip_region = NULL;
if (boxes->num_boxes <= 1 && clip_region == NULL)
return fixup_unbounded (compositor, dst, extents);
_cairo_boxes_init (&clear);
box.p1.x = _cairo_fixed_from_int (extents->unbounded.x + extents->unbounded.width);
box.p1.y = _cairo_fixed_from_int (extents->unbounded.y);
box.p2.x = _cairo_fixed_from_int (extents->unbounded.x);
box.p2.y = _cairo_fixed_from_int (extents->unbounded.y + extents->unbounded.height);
if (clip_region == NULL) {
cairo_boxes_t tmp;
_cairo_boxes_init (&tmp);
status = _cairo_boxes_add (&tmp, CAIRO_ANTIALIAS_DEFAULT, &box);
assert (status == CAIRO_STATUS_SUCCESS);
tmp.chunks.next = &boxes->chunks;
tmp.num_boxes += boxes->num_boxes;
status = _cairo_bentley_ottmann_tessellate_boxes (&tmp,
CAIRO_FILL_RULE_WINDING,
&clear);
tmp.chunks.next = NULL;
} else {
pixman_box32_t *pbox;
pbox = pixman_region32_rectangles (&clip_region->rgn, &i);
_cairo_boxes_limit (&clear, (cairo_box_t *) pbox, i);
status = _cairo_boxes_add (&clear, CAIRO_ANTIALIAS_DEFAULT, &box);
assert (status == CAIRO_STATUS_SUCCESS);
for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
status = _cairo_boxes_add (&clear,
CAIRO_ANTIALIAS_DEFAULT,
&chunk->base[i]);
if (unlikely (status)) {
_cairo_boxes_fini (&clear);
return status;
}
}
}
status = _cairo_bentley_ottmann_tessellate_boxes (&clear,
CAIRO_FILL_RULE_WINDING,
&clear);
}
if (likely (status == CAIRO_STATUS_SUCCESS)) {
status = compositor->fill_boxes (dst,
CAIRO_OPERATOR_CLEAR,
CAIRO_COLOR_TRANSPARENT,
&clear);
}
_cairo_boxes_fini (&clear);
return status;
}
enum {
NEED_CLIP_REGION = 0x1,
NEED_CLIP_SURFACE = 0x2,
FORCE_CLIP_REGION = 0x4,
};
static cairo_bool_t
need_bounded_clip (cairo_composite_rectangles_t *extents)
{
unsigned int flags = NEED_CLIP_REGION;
if (! _cairo_clip_is_region (extents->clip))
flags |= NEED_CLIP_SURFACE;
return flags;
}
static cairo_bool_t
need_unbounded_clip (cairo_composite_rectangles_t *extents)
{
unsigned int flags = 0;
if (! extents->is_bounded) {
flags |= NEED_CLIP_REGION;
if (! _cairo_clip_is_region (extents->clip))
flags |= NEED_CLIP_SURFACE;
}
if (extents->clip->path != NULL)
flags |= NEED_CLIP_SURFACE;
return flags;
}
static cairo_status_t
clip_and_composite (const cairo_mask_compositor_t *compositor,
draw_func_t draw_func,
draw_func_t mask_func,
void *draw_closure,
cairo_composite_rectangles_t*extents,
unsigned int need_clip)
{
cairo_surface_t *dst = extents->surface;
cairo_operator_t op = extents->op;
cairo_pattern_t *src = &extents->source_pattern.base;
cairo_region_t *clip_region = NULL;
cairo_status_t status;
compositor->acquire (dst);
if (need_clip & NEED_CLIP_REGION) {
clip_region = _cairo_clip_get_region (extents->clip);
if ((need_clip & FORCE_CLIP_REGION) == 0 &&
_cairo_composite_rectangles_can_reduce_clip (extents,
extents->clip))
clip_region = NULL;
if (clip_region != NULL) {
status = compositor->set_clip_region (dst, clip_region);
if (unlikely (status)) {
compositor->release (dst);
return status;
}
}
}
if (reduce_alpha_op (dst, op, &extents->source_pattern.base)) {
op = CAIRO_OPERATOR_ADD;
src = NULL;
}
if (op == CAIRO_OPERATOR_SOURCE) {
status = clip_and_composite_source (compositor,
draw_closure, draw_func, mask_func,
src, extents);
} else {
if (op == CAIRO_OPERATOR_CLEAR) {
op = CAIRO_OPERATOR_DEST_OUT;
src = NULL;
}
if (need_clip & NEED_CLIP_SURFACE) {
if (extents->is_bounded) {
status = clip_and_composite_with_mask (compositor,
draw_closure,
draw_func,
mask_func,
op, src, extents);
} else {
status = clip_and_composite_combine (compositor,
draw_closure,
draw_func,
op, src, extents);
}
} else {
status = draw_func (compositor,
dst, draw_closure,
op, src, &extents->source_sample_area,
0, 0,
&extents->bounded,
extents->clip);
}
}
if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded) {
if (need_clip & NEED_CLIP_SURFACE)
status = fixup_unbounded_with_mask (compositor, dst, extents);
else
status = fixup_unbounded (compositor, dst, extents);
}
if (clip_region)
compositor->set_clip_region (dst, NULL);
compositor->release (dst);
return status;
}
static cairo_int_status_t
trim_extents_to_boxes (cairo_composite_rectangles_t *extents,
cairo_boxes_t *boxes)
{
cairo_box_t box;
_cairo_boxes_extents (boxes, &box);
return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
}
static cairo_status_t
upload_boxes (const cairo_mask_compositor_t *compositor,
cairo_composite_rectangles_t *extents,
cairo_boxes_t *boxes)
{
cairo_surface_t *dst = extents->surface;
const cairo_pattern_t *source = &extents->source_pattern.base;
cairo_surface_t *src;
cairo_rectangle_int_t limit;
cairo_int_status_t status;
int tx, ty;
src = _cairo_pattern_get_source ((cairo_surface_pattern_t *)source, &limit);
if (!(src->type == CAIRO_SURFACE_TYPE_IMAGE || src->type == dst->type))
return CAIRO_INT_STATUS_UNSUPPORTED;
if (! _cairo_matrix_is_integer_translation (&source->matrix, &tx, &ty))
return CAIRO_INT_STATUS_UNSUPPORTED;
/* Check that the data is entirely within the image */
if (extents->bounded.x + tx < limit.x || extents->bounded.y + ty < limit.y)
return CAIRO_INT_STATUS_UNSUPPORTED;
if (extents->bounded.x + extents->bounded.width + tx > limit.x + limit.width ||
extents->bounded.y + extents->bounded.height + ty > limit.y + limit.height)
return CAIRO_INT_STATUS_UNSUPPORTED;
tx += limit.x;
ty += limit.y;
if (src->type == CAIRO_SURFACE_TYPE_IMAGE)
status = compositor->draw_image_boxes (dst,
(cairo_image_surface_t *)src,
boxes, tx, ty);
else
status = compositor->copy_boxes (dst, src, boxes, &extents->bounded,
tx, ty);
return status;
}
static cairo_status_t
composite_boxes (const cairo_mask_compositor_t *compositor,
const cairo_composite_rectangles_t *extents,
cairo_boxes_t *boxes)
{
cairo_surface_t *dst = extents->surface;
cairo_operator_t op = extents->op;
const cairo_pattern_t *source = &extents->source_pattern.base;
cairo_bool_t need_clip_mask = extents->clip->path != NULL;
cairo_status_t status;
if (need_clip_mask &&
(! extents->is_bounded || op == CAIRO_OPERATOR_SOURCE))
{
return CAIRO_INT_STATUS_UNSUPPORTED;
}
status = compositor->acquire (dst);
if (unlikely (status))
return status;
if (! need_clip_mask && source->type == CAIRO_PATTERN_TYPE_SOLID) {
const cairo_color_t *color;
color = &((cairo_solid_pattern_t *) source)->color;
status = compositor->fill_boxes (dst, op, color, boxes);
} else {
cairo_surface_t *src, *mask = NULL;
int src_x, src_y;
int mask_x = 0, mask_y = 0;
if (need_clip_mask) {
mask = get_clip_source (compositor,
extents->clip, dst, &extents->bounded,
&mask_x, &mask_y);
if (unlikely (mask->status))
return mask->status;
if (op == CAIRO_OPERATOR_CLEAR) {
source = NULL;
op = CAIRO_OPERATOR_DEST_OUT;
}
}
if (source || mask == NULL) {
src = compositor->pattern_to_surface (dst, source, FALSE,
&extents->bounded,
&extents->source_sample_area,
&src_x, &src_y);
} else {
src = mask;
src_x = mask_x;
src_y = mask_y;
mask = NULL;
}
status = compositor->composite_boxes (dst, op, src, mask,
src_x, src_y,
mask_x, mask_y,
0, 0,
boxes, &extents->bounded);
cairo_surface_destroy (src);
cairo_surface_destroy (mask);
}
if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded)
status = fixup_unbounded_boxes (compositor, extents, boxes);
compositor->release (dst);
return status;
}
static cairo_status_t
clip_and_composite_boxes (const cairo_mask_compositor_t *compositor,
cairo_composite_rectangles_t *extents,
cairo_boxes_t *boxes)
{
cairo_surface_t *dst = extents->surface;
cairo_int_status_t status;
if (boxes->num_boxes == 0) {
if (extents->is_bounded)
return CAIRO_STATUS_SUCCESS;
return fixup_unbounded_boxes (compositor, extents, boxes);
}
if (! boxes->is_pixel_aligned)
return CAIRO_INT_STATUS_UNSUPPORTED;
status = trim_extents_to_boxes (extents, boxes);
if (unlikely (status))
return status;
if (extents->source_pattern.base.type == CAIRO_PATTERN_TYPE_SURFACE &&
extents->clip->path == NULL &&
(extents->op == CAIRO_OPERATOR_SOURCE ||
(dst->is_clear && (extents->op == CAIRO_OPERATOR_OVER ||
extents->op == CAIRO_OPERATOR_ADD))))
{
status = upload_boxes (compositor, extents, boxes);
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
return status;
}
return composite_boxes (compositor, extents, boxes);
}
/* high-level compositor interface */
static cairo_int_status_t
_cairo_mask_compositor_paint (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents)
{
cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
cairo_boxes_t boxes;
cairo_int_status_t status;
status = compositor->check_composite (extents);
if (unlikely (status))
return status;
_cairo_clip_steal_boxes (extents->clip, &boxes);
status = clip_and_composite_boxes (compositor, extents, &boxes);
_cairo_clip_unsteal_boxes (extents->clip, &boxes);
return status;
}
struct composite_opacity_info {
const cairo_mask_compositor_t *compositor;
uint8_t op;
cairo_surface_t *dst;
cairo_surface_t *src;
int src_x, src_y;
double opacity;
};
static void composite_opacity(void *closure,
int16_t x, int16_t y,
int16_t w, int16_t h,
uint16_t coverage)
{
struct composite_opacity_info *info = closure;
const cairo_mask_compositor_t *compositor = info->compositor;
cairo_surface_t *mask;
int mask_x, mask_y;
cairo_color_t color;
cairo_solid_pattern_t solid;
_cairo_color_init_rgba (&color, 0, 0, 0, info->opacity * coverage);
_cairo_pattern_init_solid (&solid, &color);
mask = compositor->pattern_to_surface (info->dst, &solid.base, TRUE,
&_cairo_unbounded_rectangle,
&_cairo_unbounded_rectangle,
&mask_x, &mask_y);
if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
if (info->src) {
compositor->composite (info->dst, info->op, info->src, mask,
x + info->src_x, y + info->src_y,
mask_x, mask_y,
x, y,
w, h);
} else {
compositor->composite (info->dst, info->op, mask, NULL,
mask_x, mask_y,
0, 0,
x, y,
w, h);
}
}
cairo_surface_destroy (mask);
}
static cairo_int_status_t
composite_opacity_boxes (const cairo_mask_compositor_t *compositor,
cairo_surface_t *dst,
void *closure,
cairo_operator_t op,
const cairo_pattern_t *src_pattern,
const cairo_rectangle_int_t *src_sample,
int dst_x,
int dst_y,
const cairo_rectangle_int_t *extents,
cairo_clip_t *clip)
{
const cairo_solid_pattern_t *mask_pattern = closure;
struct composite_opacity_info info;
int i;
assert (clip);
info.compositor = compositor;
info.op = op;
info.dst = dst;
if (src_pattern != NULL) {
info.src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
extents, src_sample,
&info.src_x, &info.src_y);
if (unlikely (info.src->status))
return info.src->status;
} else
info.src = NULL;
info.opacity = mask_pattern->color.alpha / (double) 0xffff;
/* XXX for lots of boxes create a clip region for the fully opaque areas */
for (i = 0; i < clip->num_boxes; i++)
do_unaligned_box(composite_opacity, &info,
&clip->boxes[i], dst_x, dst_y);
cairo_surface_destroy (info.src);
return CAIRO_STATUS_SUCCESS;
}
struct composite_box_info {
const cairo_mask_compositor_t *compositor;
cairo_surface_t *dst;
cairo_surface_t *src;
int src_x, src_y;
uint8_t op;
};
static void composite_box(void *closure,
int16_t x, int16_t y,
int16_t w, int16_t h,
uint16_t coverage)
{
struct composite_box_info *info = closure;
const cairo_mask_compositor_t *compositor = info->compositor;
if (! CAIRO_ALPHA_SHORT_IS_OPAQUE (coverage)) {
cairo_surface_t *mask;
cairo_color_t color;
cairo_solid_pattern_t solid;
int mask_x, mask_y;
_cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double)0xffff);
_cairo_pattern_init_solid (&solid, &color);
mask = compositor->pattern_to_surface (info->dst, &solid.base, FALSE,
&_cairo_unbounded_rectangle,
&_cairo_unbounded_rectangle,
&mask_x, &mask_y);
if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
compositor->composite (info->dst, info->op, info->src, mask,
x + info->src_x, y + info->src_y,
mask_x, mask_y,
x, y,
w, h);
}
cairo_surface_destroy (mask);
} else {
compositor->composite (info->dst, info->op, info->src, NULL,
x + info->src_x, y + info->src_y,
0, 0,
x, y,
w, h);
}
}
static cairo_int_status_t
composite_mask_clip_boxes (const cairo_mask_compositor_t *compositor,
cairo_surface_t *dst,
void *closure,
cairo_operator_t op,
const cairo_pattern_t *src_pattern,
const cairo_rectangle_int_t *src_sample,
int dst_x,
int dst_y,
const cairo_rectangle_int_t *extents,
cairo_clip_t *clip)
{
cairo_composite_rectangles_t *composite = closure;
struct composite_box_info info;
int i;
assert (src_pattern == NULL);
assert (op == CAIRO_OPERATOR_SOURCE);
info.compositor = compositor;
info.op = CAIRO_OPERATOR_SOURCE;
info.dst = dst;
info.src = compositor->pattern_to_surface (dst,
&composite->mask_pattern.base,
FALSE, extents,
&composite->mask_sample_area,
&info.src_x, &info.src_y);
if (unlikely (info.src->status))
return info.src->status;
info.src_x += dst_x;
info.src_y += dst_y;
for (i = 0; i < clip->num_boxes; i++)
do_unaligned_box(composite_box, &info, &clip->boxes[i], dst_x, dst_y);
cairo_surface_destroy (info.src);
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
composite_mask (const cairo_mask_compositor_t *compositor,
cairo_surface_t *dst,
void *closure,
cairo_operator_t op,
const cairo_pattern_t *src_pattern,
const cairo_rectangle_int_t *src_sample,
int dst_x,
int dst_y,
const cairo_rectangle_int_t *extents,
cairo_clip_t *clip)
{
cairo_composite_rectangles_t *composite = closure;
cairo_surface_t *src, *mask;
int src_x, src_y;
int mask_x, mask_y;
if (src_pattern != NULL) {
src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
extents, src_sample,
&src_x, &src_y);
if (unlikely (src->status))
return src->status;
mask = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, TRUE,
extents, &composite->mask_sample_area,
&mask_x, &mask_y);
if (unlikely (mask->status)) {
cairo_surface_destroy (src);
return mask->status;
}
compositor->composite (dst, op, src, mask,
extents->x + src_x, extents->y + src_y,
extents->x + mask_x, extents->y + mask_y,
extents->x - dst_x, extents->y - dst_y,
extents->width, extents->height);
cairo_surface_destroy (mask);
cairo_surface_destroy (src);
} else {
src = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, FALSE,
extents, &composite->mask_sample_area,
&src_x, &src_y);
if (unlikely (src->status))
return src->status;
compositor->composite (dst, op, src, NULL,
extents->x + src_x, extents->y + src_y,
0, 0,
extents->x - dst_x, extents->y - dst_y,
extents->width, extents->height);
cairo_surface_destroy (src);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
_cairo_mask_compositor_mask (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents)
{
const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
status = compositor->check_composite (extents);
if (unlikely (status))
return status;
if (extents->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID &&
extents->clip->path == NULL &&
_cairo_clip_is_region (extents->clip)) {
status = clip_and_composite (compositor,
composite_opacity_boxes,
composite_opacity_boxes,
&extents->mask_pattern.solid,
extents, need_unbounded_clip (extents));
} else {
status = clip_and_composite (compositor,
composite_mask,
extents->clip->path == NULL ? composite_mask_clip_boxes : NULL,
extents,
extents, need_bounded_clip (extents));
}
return status;
}
static cairo_int_status_t
_cairo_mask_compositor_stroke (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_antialias_t antialias)
{
const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
cairo_surface_t *mask;
cairo_surface_pattern_t pattern;
cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
status = compositor->check_composite (extents);
if (unlikely (status))
return status;
if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
cairo_boxes_t boxes;
_cairo_boxes_init_with_clip (&boxes, extents->clip);
status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
style,
ctm,
antialias,
&boxes);
if (likely (status == CAIRO_INT_STATUS_SUCCESS))
status = clip_and_composite_boxes (compositor, extents, &boxes);
_cairo_boxes_fini (&boxes);
}
if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
mask = cairo_surface_create_similar_image (extents->surface,
CAIRO_FORMAT_A8,
extents->bounded.width,
extents->bounded.height);
if (unlikely (mask->status))
return mask->status;
status = _cairo_surface_offset_stroke (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_ADD,
&_cairo_pattern_white.base,
path, style, ctm, ctm_inverse,
tolerance, antialias,
extents->clip);
if (unlikely (status)) {
cairo_surface_destroy (mask);
return status;
}
_cairo_pattern_init_for_surface (&pattern, mask);
cairo_surface_destroy (mask);
cairo_matrix_init_translate (&pattern.base.matrix,
-extents->bounded.x,
-extents->bounded.y);
pattern.base.filter = CAIRO_FILTER_NEAREST;
pattern.base.extend = CAIRO_EXTEND_NONE;
status = _cairo_surface_mask (extents->surface,
extents->op,
&extents->source_pattern.base,
&pattern.base,
extents->clip);
_cairo_pattern_fini (&pattern.base);
}
return status;
}
static cairo_int_status_t
_cairo_mask_compositor_fill (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias)
{
const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
cairo_surface_t *mask;
cairo_surface_pattern_t pattern;
cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
status = compositor->check_composite (extents);
if (unlikely (status))
return status;
if (_cairo_path_fixed_fill_is_rectilinear (path)) {
cairo_boxes_t boxes;
_cairo_boxes_init_with_clip (&boxes, extents->clip);
status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
fill_rule,
antialias,
&boxes);
if (likely (status == CAIRO_INT_STATUS_SUCCESS))
status = clip_and_composite_boxes (compositor, extents, &boxes);
_cairo_boxes_fini (&boxes);
}
if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
mask = cairo_surface_create_similar_image (extents->surface,
CAIRO_FORMAT_A8,
extents->bounded.width,
extents->bounded.height);
if (unlikely (mask->status))
return mask->status;
status = _cairo_surface_offset_fill (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_ADD,
&_cairo_pattern_white.base,
path, fill_rule, tolerance, antialias,
extents->clip);
if (unlikely (status)) {
cairo_surface_destroy (mask);
return status;
}
_cairo_pattern_init_for_surface (&pattern, mask);
cairo_surface_destroy (mask);
cairo_matrix_init_translate (&pattern.base.matrix,
-extents->bounded.x,
-extents->bounded.y);
pattern.base.filter = CAIRO_FILTER_NEAREST;
pattern.base.extend = CAIRO_EXTEND_NONE;
status = _cairo_surface_mask (extents->surface,
extents->op,
&extents->source_pattern.base,
&pattern.base,
extents->clip);
_cairo_pattern_fini (&pattern.base);
}
return status;
}
static cairo_int_status_t
_cairo_mask_compositor_glyphs (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
cairo_scaled_font_t *scaled_font,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_bool_t overlap)
{
const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
cairo_surface_t *mask;
cairo_surface_pattern_t pattern;
cairo_int_status_t status;
status = compositor->check_composite (extents);
if (unlikely (status))
return CAIRO_INT_STATUS_UNSUPPORTED;
mask = cairo_surface_create_similar_image (extents->surface,
CAIRO_FORMAT_A8,
extents->bounded.width,
extents->bounded.height);
if (unlikely (mask->status))
return mask->status;
status = _cairo_surface_offset_glyphs (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_ADD,
&_cairo_pattern_white.base,
scaled_font, glyphs, num_glyphs,
extents->clip);
if (unlikely (status)) {
cairo_surface_destroy (mask);
return status;
}
_cairo_pattern_init_for_surface (&pattern, mask);
cairo_surface_destroy (mask);
cairo_matrix_init_translate (&pattern.base.matrix,
-extents->bounded.x,
-extents->bounded.y);
pattern.base.filter = CAIRO_FILTER_NEAREST;
pattern.base.extend = CAIRO_EXTEND_NONE;
status = _cairo_surface_mask (extents->surface,
extents->op,
&extents->source_pattern.base,
&pattern.base,
extents->clip);
_cairo_pattern_fini (&pattern.base);
return status;
}
void
_cairo_mask_compositor_init (cairo_mask_compositor_t *compositor,
const cairo_compositor_t *delegate)
{
compositor->base.delegate = delegate;
compositor->base.paint = _cairo_mask_compositor_paint;
compositor->base.mask = _cairo_mask_compositor_mask;
compositor->base.fill = _cairo_mask_compositor_fill;
compositor->base.stroke = _cairo_mask_compositor_stroke;
compositor->base.glyphs = _cairo_mask_compositor_glyphs;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-matrix.c | /* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include <float.h>
#define PIXMAN_MAX_INT ((pixman_fixed_1 >> 1) - pixman_fixed_e) /* need to ensure deltas also fit */
/**
* SECTION:cairo-matrix
* @Title: cairo_matrix_t
* @Short_Description: Generic matrix operations
* @See_Also: #cairo_t
*
* #cairo_matrix_t is used throughout cairo to convert between different
* coordinate spaces. A #cairo_matrix_t holds an affine transformation,
* such as a scale, rotation, shear, or a combination of these.
* The transformation of a point (<literal>x</literal>,<literal>y</literal>)
* is given by:
*
* <programlisting>
* x_new = xx * x + xy * y + x0;
* y_new = yx * x + yy * y + y0;
* </programlisting>
*
* The current transformation matrix of a #cairo_t, represented as a
* #cairo_matrix_t, defines the transformation from user-space
* coordinates to device-space coordinates. See cairo_get_matrix() and
* cairo_set_matrix().
**/
static void
_cairo_matrix_scalar_multiply (cairo_matrix_t *matrix, double scalar);
static void
_cairo_matrix_compute_adjoint (cairo_matrix_t *matrix);
/**
* cairo_matrix_init_identity:
* @matrix: a #cairo_matrix_t
*
* Modifies @matrix to be an identity transformation.
*
* Since: 1.0
**/
void
cairo_matrix_init_identity (cairo_matrix_t *matrix)
{
cairo_matrix_init (matrix,
1, 0,
0, 1,
0, 0);
}
slim_hidden_def(cairo_matrix_init_identity);
/**
* cairo_matrix_init:
* @matrix: a #cairo_matrix_t
* @xx: xx component of the affine transformation
* @yx: yx component of the affine transformation
* @xy: xy component of the affine transformation
* @yy: yy component of the affine transformation
* @x0: X translation component of the affine transformation
* @y0: Y translation component of the affine transformation
*
* Sets @matrix to be the affine transformation given by
* @xx, @yx, @xy, @yy, @x0, @y0. The transformation is given
* by:
* <programlisting>
* x_new = xx * x + xy * y + x0;
* y_new = yx * x + yy * y + y0;
* </programlisting>
*
* Since: 1.0
**/
void
cairo_matrix_init (cairo_matrix_t *matrix,
double xx, double yx,
double xy, double yy,
double x0, double y0)
{
matrix->xx = xx; matrix->yx = yx;
matrix->xy = xy; matrix->yy = yy;
matrix->x0 = x0; matrix->y0 = y0;
}
slim_hidden_def(cairo_matrix_init);
/**
* _cairo_matrix_get_affine:
* @matrix: a #cairo_matrix_t
* @xx: location to store xx component of matrix
* @yx: location to store yx component of matrix
* @xy: location to store xy component of matrix
* @yy: location to store yy component of matrix
* @x0: location to store x0 (X-translation component) of matrix, or %NULL
* @y0: location to store y0 (Y-translation component) of matrix, or %NULL
*
* Gets the matrix values for the affine transformation that @matrix represents.
* See cairo_matrix_init().
*
*
* This function is a leftover from the old public API, but is still
* mildly useful as an internal means for getting at the matrix
* members in a positional way. For example, when reassigning to some
* external matrix type, or when renaming members to more meaningful
* names (such as a,b,c,d,e,f) for particular manipulations.
**/
void
_cairo_matrix_get_affine (const cairo_matrix_t *matrix,
double *xx, double *yx,
double *xy, double *yy,
double *x0, double *y0)
{
*xx = matrix->xx;
*yx = matrix->yx;
*xy = matrix->xy;
*yy = matrix->yy;
if (x0)
*x0 = matrix->x0;
if (y0)
*y0 = matrix->y0;
}
/**
* cairo_matrix_init_translate:
* @matrix: a #cairo_matrix_t
* @tx: amount to translate in the X direction
* @ty: amount to translate in the Y direction
*
* Initializes @matrix to a transformation that translates by @tx and
* @ty in the X and Y dimensions, respectively.
*
* Since: 1.0
**/
void
cairo_matrix_init_translate (cairo_matrix_t *matrix,
double tx, double ty)
{
cairo_matrix_init (matrix,
1, 0,
0, 1,
tx, ty);
}
slim_hidden_def(cairo_matrix_init_translate);
/**
* cairo_matrix_translate:
* @matrix: a #cairo_matrix_t
* @tx: amount to translate in the X direction
* @ty: amount to translate in the Y direction
*
* Applies a translation by @tx, @ty to the transformation in
* @matrix. The effect of the new transformation is to first translate
* the coordinates by @tx and @ty, then apply the original transformation
* to the coordinates.
*
* Since: 1.0
**/
void
cairo_matrix_translate (cairo_matrix_t *matrix, double tx, double ty)
{
cairo_matrix_t tmp;
cairo_matrix_init_translate (&tmp, tx, ty);
cairo_matrix_multiply (matrix, &tmp, matrix);
}
slim_hidden_def (cairo_matrix_translate);
/**
* cairo_matrix_init_scale:
* @matrix: a #cairo_matrix_t
* @sx: scale factor in the X direction
* @sy: scale factor in the Y direction
*
* Initializes @matrix to a transformation that scales by @sx and @sy
* in the X and Y dimensions, respectively.
*
* Since: 1.0
**/
void
cairo_matrix_init_scale (cairo_matrix_t *matrix,
double sx, double sy)
{
cairo_matrix_init (matrix,
sx, 0,
0, sy,
0, 0);
}
slim_hidden_def(cairo_matrix_init_scale);
/**
* cairo_matrix_scale:
* @matrix: a #cairo_matrix_t
* @sx: scale factor in the X direction
* @sy: scale factor in the Y direction
*
* Applies scaling by @sx, @sy to the transformation in @matrix. The
* effect of the new transformation is to first scale the coordinates
* by @sx and @sy, then apply the original transformation to the coordinates.
*
* Since: 1.0
**/
void
cairo_matrix_scale (cairo_matrix_t *matrix, double sx, double sy)
{
cairo_matrix_t tmp;
cairo_matrix_init_scale (&tmp, sx, sy);
cairo_matrix_multiply (matrix, &tmp, matrix);
}
slim_hidden_def(cairo_matrix_scale);
/**
* cairo_matrix_init_rotate:
* @matrix: a #cairo_matrix_t
* @radians: angle of rotation, in radians. The direction of rotation
* is defined such that positive angles rotate in the direction from
* the positive X axis toward the positive Y axis. With the default
* axis orientation of cairo, positive angles rotate in a clockwise
* direction.
*
* Initialized @matrix to a transformation that rotates by @radians.
*
* Since: 1.0
**/
void
cairo_matrix_init_rotate (cairo_matrix_t *matrix,
double radians)
{
double s;
double c;
s = sin (radians);
c = cos (radians);
cairo_matrix_init (matrix,
c, s,
-s, c,
0, 0);
}
slim_hidden_def(cairo_matrix_init_rotate);
/**
* cairo_matrix_rotate:
* @matrix: a #cairo_matrix_t
* @radians: angle of rotation, in radians. The direction of rotation
* is defined such that positive angles rotate in the direction from
* the positive X axis toward the positive Y axis. With the default
* axis orientation of cairo, positive angles rotate in a clockwise
* direction.
*
* Applies rotation by @radians to the transformation in
* @matrix. The effect of the new transformation is to first rotate the
* coordinates by @radians, then apply the original transformation
* to the coordinates.
*
* Since: 1.0
**/
void
cairo_matrix_rotate (cairo_matrix_t *matrix, double radians)
{
cairo_matrix_t tmp;
cairo_matrix_init_rotate (&tmp, radians);
cairo_matrix_multiply (matrix, &tmp, matrix);
}
/**
* cairo_matrix_multiply:
* @result: a #cairo_matrix_t in which to store the result
* @a: a #cairo_matrix_t
* @b: a #cairo_matrix_t
*
* Multiplies the affine transformations in @a and @b together
* and stores the result in @result. The effect of the resulting
* transformation is to first apply the transformation in @a to the
* coordinates and then apply the transformation in @b to the
* coordinates.
*
* It is allowable for @result to be identical to either @a or @b.
*
* Since: 1.0
**/
/*
* XXX: The ordering of the arguments to this function corresponds
* to [row_vector]*A*B. If we want to use column vectors instead,
* then we need to switch the two arguments and fix up all
* uses.
*/
void
cairo_matrix_multiply (cairo_matrix_t *result, const cairo_matrix_t *a, const cairo_matrix_t *b)
{
cairo_matrix_t r;
r.xx = a->xx * b->xx + a->yx * b->xy;
r.yx = a->xx * b->yx + a->yx * b->yy;
r.xy = a->xy * b->xx + a->yy * b->xy;
r.yy = a->xy * b->yx + a->yy * b->yy;
r.x0 = a->x0 * b->xx + a->y0 * b->xy + b->x0;
r.y0 = a->x0 * b->yx + a->y0 * b->yy + b->y0;
*result = r;
}
slim_hidden_def(cairo_matrix_multiply);
void
_cairo_matrix_multiply (cairo_matrix_t *r,
const cairo_matrix_t *a,
const cairo_matrix_t *b)
{
r->xx = a->xx * b->xx + a->yx * b->xy;
r->yx = a->xx * b->yx + a->yx * b->yy;
r->xy = a->xy * b->xx + a->yy * b->xy;
r->yy = a->xy * b->yx + a->yy * b->yy;
r->x0 = a->x0 * b->xx + a->y0 * b->xy + b->x0;
r->y0 = a->x0 * b->yx + a->y0 * b->yy + b->y0;
}
/**
* cairo_matrix_transform_distance:
* @matrix: a #cairo_matrix_t
* @dx: X component of a distance vector. An in/out parameter
* @dy: Y component of a distance vector. An in/out parameter
*
* Transforms the distance vector (@dx,@dy) by @matrix. This is
* similar to cairo_matrix_transform_point() except that the translation
* components of the transformation are ignored. The calculation of
* the returned vector is as follows:
*
* <programlisting>
* dx2 = dx1 * a + dy1 * c;
* dy2 = dx1 * b + dy1 * d;
* </programlisting>
*
* Affine transformations are position invariant, so the same vector
* always transforms to the same vector. If (@x1,@y1) transforms
* to (@x2,@y2) then (@x1+@dx1,@y1+@dy1) will transform to
* (@x1+@dx2,@y1+@dy2) for all values of @x1 and @x2.
*
* Since: 1.0
**/
void
cairo_matrix_transform_distance (const cairo_matrix_t *matrix, double *dx, double *dy)
{
double new_x, new_y;
new_x = (matrix->xx * *dx + matrix->xy * *dy);
new_y = (matrix->yx * *dx + matrix->yy * *dy);
*dx = new_x;
*dy = new_y;
}
slim_hidden_def(cairo_matrix_transform_distance);
/**
* cairo_matrix_transform_point:
* @matrix: a #cairo_matrix_t
* @x: X position. An in/out parameter
* @y: Y position. An in/out parameter
*
* Transforms the point (@x, @y) by @matrix.
*
* Since: 1.0
**/
void
cairo_matrix_transform_point (const cairo_matrix_t *matrix, double *x, double *y)
{
cairo_matrix_transform_distance (matrix, x, y);
*x += matrix->x0;
*y += matrix->y0;
}
slim_hidden_def(cairo_matrix_transform_point);
void
_cairo_matrix_transform_bounding_box (const cairo_matrix_t *matrix,
double *x1, double *y1,
double *x2, double *y2,
cairo_bool_t *is_tight)
{
int i;
double quad_x[4], quad_y[4];
double min_x, max_x;
double min_y, max_y;
if (matrix->xy == 0. && matrix->yx == 0.) {
/* non-rotation/skew matrix, just map the two extreme points */
if (matrix->xx != 1.) {
quad_x[0] = *x1 * matrix->xx;
quad_x[1] = *x2 * matrix->xx;
if (quad_x[0] < quad_x[1]) {
*x1 = quad_x[0];
*x2 = quad_x[1];
} else {
*x1 = quad_x[1];
*x2 = quad_x[0];
}
}
if (matrix->x0 != 0.) {
*x1 += matrix->x0;
*x2 += matrix->x0;
}
if (matrix->yy != 1.) {
quad_y[0] = *y1 * matrix->yy;
quad_y[1] = *y2 * matrix->yy;
if (quad_y[0] < quad_y[1]) {
*y1 = quad_y[0];
*y2 = quad_y[1];
} else {
*y1 = quad_y[1];
*y2 = quad_y[0];
}
}
if (matrix->y0 != 0.) {
*y1 += matrix->y0;
*y2 += matrix->y0;
}
if (is_tight)
*is_tight = TRUE;
return;
}
/* general matrix */
quad_x[0] = *x1;
quad_y[0] = *y1;
cairo_matrix_transform_point (matrix, &quad_x[0], &quad_y[0]);
quad_x[1] = *x2;
quad_y[1] = *y1;
cairo_matrix_transform_point (matrix, &quad_x[1], &quad_y[1]);
quad_x[2] = *x1;
quad_y[2] = *y2;
cairo_matrix_transform_point (matrix, &quad_x[2], &quad_y[2]);
quad_x[3] = *x2;
quad_y[3] = *y2;
cairo_matrix_transform_point (matrix, &quad_x[3], &quad_y[3]);
min_x = max_x = quad_x[0];
min_y = max_y = quad_y[0];
for (i=1; i < 4; i++) {
if (quad_x[i] < min_x)
min_x = quad_x[i];
if (quad_x[i] > max_x)
max_x = quad_x[i];
if (quad_y[i] < min_y)
min_y = quad_y[i];
if (quad_y[i] > max_y)
max_y = quad_y[i];
}
*x1 = min_x;
*y1 = min_y;
*x2 = max_x;
*y2 = max_y;
if (is_tight) {
/* it's tight if and only if the four corner points form an axis-aligned
rectangle.
And that's true if and only if we can derive corners 0 and 3 from
corners 1 and 2 in one of two straightforward ways...
We could use a tolerance here but for now we'll fall back to FALSE in the case
of floating point error.
*/
*is_tight =
(quad_x[1] == quad_x[0] && quad_y[1] == quad_y[3] &&
quad_x[2] == quad_x[3] && quad_y[2] == quad_y[0]) ||
(quad_x[1] == quad_x[3] && quad_y[1] == quad_y[0] &&
quad_x[2] == quad_x[0] && quad_y[2] == quad_y[3]);
}
}
cairo_private void
_cairo_matrix_transform_bounding_box_fixed (const cairo_matrix_t *matrix,
cairo_box_t *bbox,
cairo_bool_t *is_tight)
{
double x1, y1, x2, y2;
_cairo_box_to_doubles (bbox, &x1, &y1, &x2, &y2);
_cairo_matrix_transform_bounding_box (matrix, &x1, &y1, &x2, &y2, is_tight);
_cairo_box_from_doubles (bbox, &x1, &y1, &x2, &y2);
}
static void
_cairo_matrix_scalar_multiply (cairo_matrix_t *matrix, double scalar)
{
matrix->xx *= scalar;
matrix->yx *= scalar;
matrix->xy *= scalar;
matrix->yy *= scalar;
matrix->x0 *= scalar;
matrix->y0 *= scalar;
}
/* This function isn't a correct adjoint in that the implicit 1 in the
homogeneous result should actually be ad-bc instead. But, since this
adjoint is only used in the computation of the inverse, which
divides by det (A)=ad-bc anyway, everything works out in the end. */
static void
_cairo_matrix_compute_adjoint (cairo_matrix_t *matrix)
{
/* adj (A) = transpose (C:cofactor (A,i,j)) */
double a, b, c, d, tx, ty;
_cairo_matrix_get_affine (matrix,
&a, &b,
&c, &d,
&tx, &ty);
cairo_matrix_init (matrix,
d, -b,
-c, a,
c*ty - d*tx, b*tx - a*ty);
}
/**
* cairo_matrix_invert:
* @matrix: a #cairo_matrix_t
*
* Changes @matrix to be the inverse of its original value. Not
* all transformation matrices have inverses; if the matrix
* collapses points together (it is <firstterm>degenerate</firstterm>),
* then it has no inverse and this function will fail.
*
* Returns: If @matrix has an inverse, modifies @matrix to
* be the inverse matrix and returns %CAIRO_STATUS_SUCCESS. Otherwise,
* returns %CAIRO_STATUS_INVALID_MATRIX.
*
* Since: 1.0
**/
cairo_status_t
cairo_matrix_invert (cairo_matrix_t *matrix)
{
double det;
/* Simple scaling|translation matrices are quite common... */
if (matrix->xy == 0. && matrix->yx == 0.) {
matrix->x0 = -matrix->x0;
matrix->y0 = -matrix->y0;
if (matrix->xx != 1.) {
if (matrix->xx == 0.)
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
matrix->xx = 1. / matrix->xx;
matrix->x0 *= matrix->xx;
}
if (matrix->yy != 1.) {
if (matrix->yy == 0.)
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
matrix->yy = 1. / matrix->yy;
matrix->y0 *= matrix->yy;
}
return CAIRO_STATUS_SUCCESS;
}
/* inv (A) = 1/det (A) * adj (A) */
det = _cairo_matrix_compute_determinant (matrix);
if (! ISFINITE (det))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
if (det == 0)
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
_cairo_matrix_compute_adjoint (matrix);
_cairo_matrix_scalar_multiply (matrix, 1 / det);
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def(cairo_matrix_invert);
cairo_bool_t
_cairo_matrix_is_invertible (const cairo_matrix_t *matrix)
{
double det;
det = _cairo_matrix_compute_determinant (matrix);
return ISFINITE (det) && det != 0.;
}
cairo_bool_t
_cairo_matrix_is_scale_0 (const cairo_matrix_t *matrix)
{
return matrix->xx == 0. &&
matrix->xy == 0. &&
matrix->yx == 0. &&
matrix->yy == 0.;
}
double
_cairo_matrix_compute_determinant (const cairo_matrix_t *matrix)
{
double a, b, c, d;
a = matrix->xx; b = matrix->yx;
c = matrix->xy; d = matrix->yy;
return a*d - b*c;
}
/**
* _cairo_matrix_compute_basis_scale_factors:
* @matrix: a matrix
* @basis_scale: the scale factor in the direction of basis
* @normal_scale: the scale factor in the direction normal to the basis
* @x_basis: basis to use. X basis if true, Y basis otherwise.
*
* Computes |Mv| and det(M)/|Mv| for v=[1,0] if x_basis is true, and v=[0,1]
* otherwise, and M is @matrix.
*
* Return value: the scale factor of @matrix on the height of the font,
* or 1.0 if @matrix is %NULL.
**/
cairo_status_t
_cairo_matrix_compute_basis_scale_factors (const cairo_matrix_t *matrix,
double *basis_scale, double *normal_scale,
cairo_bool_t x_basis)
{
double det;
det = _cairo_matrix_compute_determinant (matrix);
if (! ISFINITE (det))
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
if (det == 0)
{
*basis_scale = *normal_scale = 0;
}
else
{
double x = x_basis != 0;
double y = x == 0;
double major, minor;
cairo_matrix_transform_distance (matrix, &x, &y);
major = hypot (x, y);
/*
* ignore mirroring
*/
if (det < 0)
det = -det;
if (major)
minor = det / major;
else
minor = 0.0;
if (x_basis)
{
*basis_scale = major;
*normal_scale = minor;
}
else
{
*basis_scale = minor;
*normal_scale = major;
}
}
return CAIRO_STATUS_SUCCESS;
}
cairo_bool_t
_cairo_matrix_is_integer_translation (const cairo_matrix_t *matrix,
int *itx, int *ity)
{
if (_cairo_matrix_is_translation (matrix))
{
cairo_fixed_t x0_fixed = _cairo_fixed_from_double (matrix->x0);
cairo_fixed_t y0_fixed = _cairo_fixed_from_double (matrix->y0);
if (_cairo_fixed_is_integer (x0_fixed) &&
_cairo_fixed_is_integer (y0_fixed))
{
if (itx)
*itx = _cairo_fixed_integer_part (x0_fixed);
if (ity)
*ity = _cairo_fixed_integer_part (y0_fixed);
return TRUE;
}
}
return FALSE;
}
#define SCALING_EPSILON _cairo_fixed_to_double(1)
/* This only returns true if the matrix is 90 degree rotations or
* flips. It appears calling code is relying on this. It will return
* false for other rotations even if the scale is one. Approximations
* are allowed to handle matricies filled in using trig functions
* such as sin(M_PI_2).
*/
cairo_bool_t
_cairo_matrix_has_unity_scale (const cairo_matrix_t *matrix)
{
/* check that the determinant is near +/-1 */
double det = _cairo_matrix_compute_determinant (matrix);
if (fabs (det * det - 1.0) < SCALING_EPSILON) {
/* check that one axis is close to zero */
if (fabs (matrix->xy) < SCALING_EPSILON &&
fabs (matrix->yx) < SCALING_EPSILON)
return TRUE;
if (fabs (matrix->xx) < SCALING_EPSILON &&
fabs (matrix->yy) < SCALING_EPSILON)
return TRUE;
/* If rotations are allowed then it must instead test for
* orthogonality. This is xx*xy+yx*yy ~= 0.
*/
}
return FALSE;
}
/* By pixel exact here, we mean a matrix that is composed only of
* 90 degree rotations, flips, and integer translations and produces a 1:1
* mapping between source and destination pixels. If we transform an image
* with a pixel-exact matrix, filtering is not useful.
*/
cairo_bool_t
_cairo_matrix_is_pixel_exact (const cairo_matrix_t *matrix)
{
cairo_fixed_t x0_fixed, y0_fixed;
if (! _cairo_matrix_has_unity_scale (matrix))
return FALSE;
x0_fixed = _cairo_fixed_from_double (matrix->x0);
y0_fixed = _cairo_fixed_from_double (matrix->y0);
return _cairo_fixed_is_integer (x0_fixed) && _cairo_fixed_is_integer (y0_fixed);
}
/*
A circle in user space is transformed into an ellipse in device space.
The following is a derivation of a formula to calculate the length of the
major axis for this ellipse; this is useful for error bounds calculations.
Thanks to Walter Brisken <wbrisken@aoc.nrao.edu> for this derivation:
1. First some notation:
All capital letters represent vectors in two dimensions. A prime '
represents a transformed coordinate. Matrices are written in underlined
form, ie _R_. Lowercase letters represent scalar real values.
2. The question has been posed: What is the maximum expansion factor
achieved by the linear transformation
X' = X _R_
where _R_ is a real-valued 2x2 matrix with entries:
_R_ = [a b]
[c d] .
In other words, what is the maximum radius, MAX[ |X'| ], reached for any
X on the unit circle ( |X| = 1 ) ?
3. Some useful formulae
(A) through (C) below are standard double-angle formulae. (D) is a lesser
known result and is derived below:
(A) sin²(θ) = (1 - cos(2*θ))/2
(B) cos²(θ) = (1 + cos(2*θ))/2
(C) sin(θ)*cos(θ) = sin(2*θ)/2
(D) MAX[a*cos(θ) + b*sin(θ)] = sqrt(a² + b²)
Proof of (D):
find the maximum of the function by setting the derivative to zero:
-a*sin(θ)+b*cos(θ) = 0
From this it follows that
tan(θ) = b/a
and hence
sin(θ) = b/sqrt(a² + b²)
and
cos(θ) = a/sqrt(a² + b²)
Thus the maximum value is
MAX[a*cos(θ) + b*sin(θ)] = (a² + b²)/sqrt(a² + b²)
= sqrt(a² + b²)
4. Derivation of maximum expansion
To find MAX[ |X'| ] we search brute force method using calculus. The unit
circle on which X is constrained is to be parameterized by t:
X(θ) = (cos(θ), sin(θ))
Thus
X'(θ) = X(θ) * _R_ = (cos(θ), sin(θ)) * [a b]
[c d]
= (a*cos(θ) + c*sin(θ), b*cos(θ) + d*sin(θ)).
Define
r(θ) = |X'(θ)|
Thus
r²(θ) = (a*cos(θ) + c*sin(θ))² + (b*cos(θ) + d*sin(θ))²
= (a² + b²)*cos²(θ) + (c² + d²)*sin²(θ)
+ 2*(a*c + b*d)*cos(θ)*sin(θ)
Now apply the double angle formulae (A) to (C) from above:
r²(θ) = (a² + b² + c² + d²)/2
+ (a² + b² - c² - d²)*cos(2*θ)/2
+ (a*c + b*d)*sin(2*θ)
= f + g*cos(φ) + h*sin(φ)
Where
f = (a² + b² + c² + d²)/2
g = (a² + b² - c² - d²)/2
h = (a*c + d*d)
φ = 2*θ
It is clear that MAX[ |X'| ] = sqrt(MAX[ r² ]). Here we determine MAX[ r² ]
using (D) from above:
MAX[ r² ] = f + sqrt(g² + h²)
And finally
MAX[ |X'| ] = sqrt( f + sqrt(g² + h²) )
Which is the solution to this problem.
Walter Brisken
2004/10/08
(Note that the minor axis length is at the minimum of the above solution,
which is just sqrt ( f - sqrt(g² + h²) ) given the symmetry of (D)).
For another derivation of the same result, using Singular Value Decomposition,
see doc/tutorial/src/singular.c.
*/
/* determine the length of the major axis of a circle of the given radius
after applying the transformation matrix. */
double
_cairo_matrix_transformed_circle_major_axis (const cairo_matrix_t *matrix,
double radius)
{
double a, b, c, d, f, g, h, i, j;
if (_cairo_matrix_has_unity_scale (matrix))
return radius;
_cairo_matrix_get_affine (matrix,
&a, &b,
&c, &d,
NULL, NULL);
i = a*a + b*b;
j = c*c + d*d;
f = 0.5 * (i + j);
g = 0.5 * (i - j);
h = a*c + b*d;
return radius * sqrt (f + hypot (g, h));
/*
* we don't need the minor axis length, which is
* double min = radius * sqrt (f - sqrt (g*g+h*h));
*/
}
static const pixman_transform_t pixman_identity_transform = {{
{1 << 16, 0, 0},
{ 0, 1 << 16, 0},
{ 0, 0, 1 << 16}
}};
static cairo_status_t
_cairo_matrix_to_pixman_matrix (const cairo_matrix_t *matrix,
pixman_transform_t *pixman_transform,
double xc,
double yc)
{
cairo_matrix_t inv;
unsigned max_iterations;
pixman_transform->matrix[0][0] = _cairo_fixed_16_16_from_double (matrix->xx);
pixman_transform->matrix[0][1] = _cairo_fixed_16_16_from_double (matrix->xy);
pixman_transform->matrix[0][2] = _cairo_fixed_16_16_from_double (matrix->x0);
pixman_transform->matrix[1][0] = _cairo_fixed_16_16_from_double (matrix->yx);
pixman_transform->matrix[1][1] = _cairo_fixed_16_16_from_double (matrix->yy);
pixman_transform->matrix[1][2] = _cairo_fixed_16_16_from_double (matrix->y0);
pixman_transform->matrix[2][0] = 0;
pixman_transform->matrix[2][1] = 0;
pixman_transform->matrix[2][2] = 1 << 16;
/* The conversion above breaks cairo's translation invariance:
* a translation of (a, b) in device space translates to
* a translation of (xx * a + xy * b, yx * a + yy * b)
* for cairo, while pixman uses rounded versions of xx ... yy.
* This error increases as a and b get larger.
*
* To compensate for this, we fix the point (xc, yc) in pattern
* space and adjust pixman's transform to agree with cairo's at
* that point.
*/
if (_cairo_matrix_has_unity_scale (matrix))
return CAIRO_STATUS_SUCCESS;
if (unlikely (fabs (matrix->xx) > PIXMAN_MAX_INT ||
fabs (matrix->xy) > PIXMAN_MAX_INT ||
fabs (matrix->x0) > PIXMAN_MAX_INT ||
fabs (matrix->yx) > PIXMAN_MAX_INT ||
fabs (matrix->yy) > PIXMAN_MAX_INT ||
fabs (matrix->y0) > PIXMAN_MAX_INT))
{
return _cairo_error (CAIRO_STATUS_INVALID_MATRIX);
}
/* Note: If we can't invert the transformation, skip the adjustment. */
inv = *matrix;
if (cairo_matrix_invert (&inv) != CAIRO_STATUS_SUCCESS)
return CAIRO_STATUS_SUCCESS;
/* find the pattern space coordinate that maps to (xc, yc) */
max_iterations = 5;
do {
double x,y;
pixman_vector_t vector;
cairo_fixed_16_16_t dx, dy;
vector.vector[0] = _cairo_fixed_16_16_from_double (xc);
vector.vector[1] = _cairo_fixed_16_16_from_double (yc);
vector.vector[2] = 1 << 16;
/* If we can't transform the reference point, skip the adjustment. */
if (! pixman_transform_point_3d (pixman_transform, &vector))
return CAIRO_STATUS_SUCCESS;
x = pixman_fixed_to_double (vector.vector[0]);
y = pixman_fixed_to_double (vector.vector[1]);
cairo_matrix_transform_point (&inv, &x, &y);
/* Ideally, the vector should now be (xc, yc).
* We can now compensate for the resulting error.
*/
x -= xc;
y -= yc;
cairo_matrix_transform_distance (matrix, &x, &y);
dx = _cairo_fixed_16_16_from_double (x);
dy = _cairo_fixed_16_16_from_double (y);
pixman_transform->matrix[0][2] -= dx;
pixman_transform->matrix[1][2] -= dy;
if (dx == 0 && dy == 0)
return CAIRO_STATUS_SUCCESS;
} while (--max_iterations);
/* We didn't find an exact match between cairo and pixman, but
* the matrix should be mostly correct */
return CAIRO_STATUS_SUCCESS;
}
static inline double
_pixman_nearest_sample (double d)
{
return ceil (d - .5);
}
/**
* _cairo_matrix_is_pixman_translation:
* @matrix: a matrix
* @filter: the filter to be used on the pattern transformed by @matrix
* @x_offset: the translation in the X direction
* @y_offset: the translation in the Y direction
*
* Checks if @matrix translated by (x_offset, y_offset) can be
* represented using just an offset (within the range pixman can
* accept) and an identity matrix.
*
* Passing a non-zero value in x_offset/y_offset has the same effect
* as applying cairo_matrix_translate(matrix, x_offset, y_offset) and
* setting x_offset and y_offset to 0.
*
* Upon return x_offset and y_offset contain the translation vector if
* the return value is %TRUE. If the return value is %FALSE, they will
* not be modified.
*
* Return value: %TRUE if @matrix can be represented as a pixman
* translation, %FALSE otherwise.
**/
cairo_bool_t
_cairo_matrix_is_pixman_translation (const cairo_matrix_t *matrix,
cairo_filter_t filter,
int *x_offset,
int *y_offset)
{
double tx, ty;
if (!_cairo_matrix_is_translation (matrix))
return FALSE;
if (matrix->x0 == 0. && matrix->y0 == 0.)
return TRUE;
tx = matrix->x0 + *x_offset;
ty = matrix->y0 + *y_offset;
if (filter == CAIRO_FILTER_FAST || filter == CAIRO_FILTER_NEAREST) {
tx = _pixman_nearest_sample (tx);
ty = _pixman_nearest_sample (ty);
} else if (tx != floor (tx) || ty != floor (ty)) {
return FALSE;
}
if (fabs (tx) > PIXMAN_MAX_INT || fabs (ty) > PIXMAN_MAX_INT)
return FALSE;
*x_offset = _cairo_lround (tx);
*y_offset = _cairo_lround (ty);
return TRUE;
}
/**
* _cairo_matrix_to_pixman_matrix_offset:
* @matrix: a matrix
* @filter: the filter to be used on the pattern transformed by @matrix
* @xc: the X coordinate of the point to fix in pattern space
* @yc: the Y coordinate of the point to fix in pattern space
* @out_transform: the transformation which best approximates @matrix
* @x_offset: the translation in the X direction
* @y_offset: the translation in the Y direction
*
* This function tries to represent @matrix translated by (x_offset,
* y_offset) as a %pixman_transform_t and an translation.
*
* Passing a non-zero value in x_offset/y_offset has the same effect
* as applying cairo_matrix_translate(matrix, x_offset, y_offset) and
* setting x_offset and y_offset to 0.
*
* If it is possible to represent the matrix with an identity
* %pixman_transform_t and a translation within the valid range for
* pixman, this function will set @out_transform to be the identity,
* @x_offset and @y_offset to be the translation vector and will
* return %CAIRO_INT_STATUS_NOTHING_TO_DO. Otherwise it will try to
* evenly divide the translational component of @matrix between
* @out_transform and (@x_offset, @y_offset).
*
* Upon return x_offset and y_offset contain the translation vector.
*
* Return value: %CAIRO_INT_STATUS_NOTHING_TO_DO if the out_transform
* is the identity, %CAIRO_STATUS_INVALID_MATRIX if it was not
* possible to represent @matrix as a pixman_transform_t without
* overflows, %CAIRO_STATUS_SUCCESS otherwise.
**/
cairo_status_t
_cairo_matrix_to_pixman_matrix_offset (const cairo_matrix_t *matrix,
cairo_filter_t filter,
double xc,
double yc,
pixman_transform_t *out_transform,
int *x_offset,
int *y_offset)
{
cairo_bool_t is_pixman_translation;
is_pixman_translation = _cairo_matrix_is_pixman_translation (matrix,
filter,
x_offset,
y_offset);
if (is_pixman_translation) {
*out_transform = pixman_identity_transform;
return CAIRO_INT_STATUS_NOTHING_TO_DO;
} else {
cairo_matrix_t m;
m = *matrix;
cairo_matrix_translate (&m, *x_offset, *y_offset);
if (m.x0 != 0.0 || m.y0 != 0.0) {
double tx, ty, norm;
int i, j;
/* pixman also limits the [xy]_offset to 16 bits so evenly
* spread the bits between the two.
*
* To do this, find the solutions of:
* |x| = |x*m.xx + y*m.xy + m.x0|
* |y| = |x*m.yx + y*m.yy + m.y0|
*
* and select the one whose maximum norm is smallest.
*/
tx = m.x0;
ty = m.y0;
norm = MAX (fabs (tx), fabs (ty));
for (i = -1; i < 2; i+=2) {
for (j = -1; j < 2; j+=2) {
double x, y, den, new_norm;
den = (m.xx + i) * (m.yy + j) - m.xy * m.yx;
if (fabs (den) < DBL_EPSILON)
continue;
x = m.y0 * m.xy - m.x0 * (m.yy + j);
y = m.x0 * m.yx - m.y0 * (m.xx + i);
den = 1 / den;
x *= den;
y *= den;
new_norm = MAX (fabs (x), fabs (y));
if (norm > new_norm) {
norm = new_norm;
tx = x;
ty = y;
}
}
}
tx = floor (tx);
ty = floor (ty);
*x_offset = -tx;
*y_offset = -ty;
cairo_matrix_translate (&m, tx, ty);
} else {
*x_offset = 0;
*y_offset = 0;
}
return _cairo_matrix_to_pixman_matrix (&m, out_transform, xc, yc);
}
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mempool-private.h | /* Cairo - a vector graphics library with display and print output
*
* Copyright © 2007 Chris Wilson
* Copyright © 2009 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributors(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#ifndef CAIRO_MEMPOOL_PRIVATE_H
#define CAIRO_MEMPOOL_PRIVATE_H
#include "cairo-compiler-private.h"
#include "cairo-error-private.h"
#include <stddef.h> /* for size_t */
CAIRO_BEGIN_DECLS
typedef struct _cairo_mempool cairo_mempool_t;
struct _cairo_mempool {
char *base;
struct _cairo_memblock {
int bits;
cairo_list_t link;
} *blocks;
cairo_list_t free[32];
unsigned char *map;
unsigned int num_blocks;
int min_bits; /* Minimum block size is 1 << min_bits */
int num_sizes;
int max_free_bits;
size_t free_bytes;
size_t max_bytes;
};
cairo_private cairo_status_t
_cairo_mempool_init (cairo_mempool_t *pool,
void *base,
size_t bytes,
int min_bits,
int num_sizes);
cairo_private void *
_cairo_mempool_alloc (cairo_mempool_t *pi, size_t bytes);
cairo_private void
_cairo_mempool_free (cairo_mempool_t *pi, void *storage);
cairo_private void
_cairo_mempool_fini (cairo_mempool_t *pool);
CAIRO_END_DECLS
#endif /* CAIRO_MEMPOOL_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mempool.c | /* Cairo - a vector graphics library with display and print output
*
* Copyright © 2007 Chris Wilson
* Copyright © 2009 Intel Corporation
*
* 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 recipoolent 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributors(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-mempool-private.h"
#include "cairo-list-inline.h"
/* a simple buddy allocator for memory pools
* XXX fragmentation? use Doug Lea's malloc?
*/
#define BITTEST(p, n) ((p)->map[(n) >> 3] & (128 >> ((n) & 7)))
#define BITSET(p, n) ((p)->map[(n) >> 3] |= (128 >> ((n) & 7)))
#define BITCLEAR(p, n) ((p)->map[(n) >> 3] &= ~(128 >> ((n) & 7)))
static void
clear_bits (cairo_mempool_t *pool, size_t first, size_t last)
{
size_t i, n = last;
size_t first_full = (first + 7) & ~7;
size_t past_full = last & ~7;
size_t bytes;
if (n > first_full)
n = first_full;
for (i = first; i < n; i++)
BITCLEAR (pool, i);
if (past_full > first_full) {
bytes = past_full - first_full;
bytes = bytes >> 3;
memset (pool->map + (first_full >> 3), 0, bytes);
}
if (past_full < n)
past_full = n;
for (i = past_full; i < last; i++)
BITCLEAR (pool, i);
}
static void
free_bits (cairo_mempool_t *pool, size_t start, int bits, cairo_bool_t clear)
{
struct _cairo_memblock *block;
if (clear)
clear_bits (pool, start, start + (1 << bits));
block = pool->blocks + start;
block->bits = bits;
cairo_list_add (&block->link, &pool->free[bits]);
pool->free_bytes += 1 << (bits + pool->min_bits);
if (bits > pool->max_free_bits)
pool->max_free_bits = bits;
}
/* Add a chunk to the free list */
static void
free_blocks (cairo_mempool_t *pool,
size_t first,
size_t last,
cairo_bool_t clear)
{
size_t i, len;
int bits = 0;
for (i = first, len = 1; i < last; i += len) {
/* To avoid cost quadratic in the number of different
* blocks produced from this chunk of store, we have to
* use the size of the previous block produced from this
* chunk as the starting point to work out the size of the
* next block we can produce. If you look at the binary
* representation of the starting points of the blocks
* produced, you can see that you first of all increase the
* size of the blocks produced up to some maximum as the
* address dealt with gets offsets added on which zap out
* low order bits, then decrease as the low order bits of the
* final block produced get added in. E.g. as you go from
* 001 to 0111 you generate blocks
* of size 001 at 001 taking you to 010
* of size 010 at 010 taking you to 100
* of size 010 at 100 taking you to 110
* of size 001 at 110 taking you to 111
* So the maximum total cost of the loops below this comment
* is one trip from the lowest blocksize to the highest and
* back again.
*/
while (bits < pool->num_sizes - 1) {
size_t next_bits = bits + 1;
size_t next_len = len << 1;
if (i + next_bits > last) {
/* off end of chunk to be freed */
break;
}
if (i & (next_len - 1)) /* block would not be on boundary */
break;
bits = next_bits;
len = next_len;
}
do {
if (i + len <= last && /* off end of chunk to be freed */
(i & (len - 1)) == 0) /* block would not be on boundary */
break;
bits--; len >>=1;
} while (len);
if (len == 0)
break;
free_bits (pool, i, bits, clear);
}
}
static struct _cairo_memblock *
get_buddy (cairo_mempool_t *pool, size_t offset, int bits)
{
struct _cairo_memblock *block;
if (offset + (1 << bits) >= pool->num_blocks)
return NULL; /* invalid */
if (BITTEST (pool, offset + (1 << bits) - 1))
return NULL; /* buddy is allocated */
block = pool->blocks + offset;
if (block->bits != bits)
return NULL; /* buddy is partially allocated */
return block;
}
static void
merge_buddies (cairo_mempool_t *pool,
struct _cairo_memblock *block,
int max_bits)
{
size_t block_offset = block - pool->blocks;
int bits = block->bits;
while (bits < max_bits - 1) {
/* while you can, merge two blocks and get a legal block size */
size_t buddy_offset = block_offset ^ (1 << bits);
block = get_buddy (pool, buddy_offset, bits);
if (block == NULL)
break;
cairo_list_del (&block->link);
/* Merged block starts at buddy */
if (buddy_offset < block_offset)
block_offset = buddy_offset;
bits++;
}
block = pool->blocks + block_offset;
block->bits = bits;
cairo_list_add (&block->link, &pool->free[bits]);
if (bits > pool->max_free_bits)
pool->max_free_bits = bits;
}
/* attempt to merge all available buddies up to a particular size */
static int
merge_bits (cairo_mempool_t *pool, int max_bits)
{
struct _cairo_memblock *block, *buddy, *next;
int bits;
for (bits = 0; bits < max_bits - 1; bits++) {
cairo_list_foreach_entry_safe (block, next,
struct _cairo_memblock,
&pool->free[bits],
link)
{
size_t buddy_offset = (block - pool->blocks) ^ (1 << bits);
buddy = get_buddy (pool, buddy_offset, bits);
if (buddy == NULL)
continue;
if (buddy == next) {
next = cairo_container_of (buddy->link.next,
struct _cairo_memblock,
link);
}
cairo_list_del (&block->link);
merge_buddies (pool, block, max_bits);
}
}
return pool->max_free_bits;
}
/* find store for 1 << bits blocks */
static void *
buddy_malloc (cairo_mempool_t *pool, int bits)
{
size_t past, offset;
struct _cairo_memblock *block;
int b;
if (bits > pool->max_free_bits && bits > merge_bits (pool, bits))
return NULL;
/* Find a list with blocks big enough on it */
block = NULL;
for (b = bits; b <= pool->max_free_bits; b++) {
if (! cairo_list_is_empty (&pool->free[b])) {
block = cairo_list_first_entry (&pool->free[b],
struct _cairo_memblock,
link);
break;
}
}
assert (block != NULL);
cairo_list_del (&block->link);
while (cairo_list_is_empty (&pool->free[pool->max_free_bits])) {
if (--pool->max_free_bits == -1)
break;
}
/* Mark end of allocated area */
offset = block - pool->blocks;
past = offset + (1 << bits);
BITSET (pool, past - 1);
block->bits = bits;
/* If we used a larger free block than we needed, free the rest */
pool->free_bytes -= 1 << (b + pool->min_bits);
free_blocks (pool, past, offset + (1 << b), 0);
return pool->base + ((block - pool->blocks) << pool->min_bits);
}
cairo_status_t
_cairo_mempool_init (cairo_mempool_t *pool,
void *base, size_t bytes,
int min_bits, int num_sizes)
{
unsigned long tmp;
int num_blocks;
int i;
/* Align the start to an integral chunk */
tmp = ((unsigned long) base) & ((1 << min_bits) - 1);
if (tmp) {
tmp = (1 << min_bits) - tmp;
base = (char *)base + tmp;
bytes -= tmp;
}
assert ((((unsigned long) base) & ((1 << min_bits) - 1)) == 0);
assert (num_sizes < ARRAY_LENGTH (pool->free));
pool->base = base;
pool->free_bytes = 0;
pool->max_bytes = bytes;
pool->max_free_bits = -1;
num_blocks = bytes >> min_bits;
pool->blocks = calloc (num_blocks, sizeof (struct _cairo_memblock));
if (pool->blocks == NULL)
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
pool->num_blocks = num_blocks;
pool->min_bits = min_bits;
pool->num_sizes = num_sizes;
for (i = 0; i < ARRAY_LENGTH (pool->free); i++)
cairo_list_init (&pool->free[i]);
pool->map = _cairo_malloc ((num_blocks + 7) >> 3);
if (pool->map == NULL) {
free (pool->blocks);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
memset (pool->map, -1, (num_blocks + 7) >> 3);
clear_bits (pool, 0, num_blocks);
/* Now add all blocks to the free list */
free_blocks (pool, 0, num_blocks, 1);
return CAIRO_STATUS_SUCCESS;
}
void *
_cairo_mempool_alloc (cairo_mempool_t *pool, size_t bytes)
{
size_t size;
int bits;
size = 1 << pool->min_bits;
for (bits = 0; size < bytes; bits++)
size <<= 1;
if (bits >= pool->num_sizes)
return NULL;
return buddy_malloc (pool, bits);
}
void
_cairo_mempool_free (cairo_mempool_t *pool, void *storage)
{
size_t block_offset;
struct _cairo_memblock *block;
block_offset = ((char *)storage - pool->base) >> pool->min_bits;
block = pool->blocks + block_offset;
BITCLEAR (pool, block_offset + ((1 << block->bits) - 1));
pool->free_bytes += 1 << (block->bits + pool->min_bits);
merge_buddies (pool, block, pool->num_sizes);
}
void
_cairo_mempool_fini (cairo_mempool_t *pool)
{
free (pool->map);
free (pool->blocks);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mesh-pattern-rasterizer.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright 2009 Andrea Canciani
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Andrea Canciani.
*
* Contributor(s):
* Andrea Canciani <ranma42@gmail.com>
*/
#include "cairoint.h"
#include "cairo-array-private.h"
#include "cairo-pattern-private.h"
/*
* Rasterizer for mesh patterns.
*
* This implementation is based on techniques derived from several
* papers (available from ACM):
*
* - Lien, Shantz and Pratt "Adaptive Forward Differencing for
* Rendering Curves and Surfaces" (discussion of the AFD technique,
* bound of 1/sqrt(2) on step length without proof)
*
* - Popescu and Rosen, "Forward rasterization" (description of
* forward rasterization, proof of the previous bound)
*
* - Klassen, "Integer Forward Differencing of Cubic Polynomials:
* Analysis and Algorithms"
*
* - Klassen, "Exact Integer Hybrid Subdivision and Forward
* Differencing of Cubics" (improving the bound on the minimum
* number of steps)
*
* - Chang, Shantz and Rocchetti, "Rendering Cubic Curves and Surfaces
* with Integer Adaptive Forward Differencing" (analysis of forward
* differencing applied to Bezier patches)
*
* Notes:
* - Poor performance expected in degenerate cases
*
* - Patches mostly outside the drawing area are drawn completely (and
* clipped), wasting time
*
* - Both previous problems are greatly reduced by splitting until a
* reasonably small size and clipping the new tiles: execution time
* is quadratic in the convex-hull diameter instead than linear to
* the painted area. Splitting the tiles doesn't change the painted
* area but (usually) reduces the bounding box area (bbox area can
* remain the same after splitting, but cannot grow)
*
* - The initial implementation used adaptive forward differencing,
* but simple forward differencing scored better in benchmarks
*
* Idea:
*
* We do a sampling over the cubic patch with step du and dv (in the
* two parameters) that guarantees that any point of our sampling will
* be at most at 1/sqrt(2) from its adjacent points. In formulae
* (assuming B is the patch):
*
* |B(u,v) - B(u+du,v)| < 1/sqrt(2)
* |B(u,v) - B(u,v+dv)| < 1/sqrt(2)
*
* This means that every pixel covered by the patch will contain at
* least one of the samples, thus forward rasterization can be
* performed. Sketch of proof (from Popescu and Rosen):
*
* Let's take the P pixel we're interested into. If we assume it to be
* square, its boundaries define 9 regions on the plane:
*
* 1|2|3
* -+-+-
* 8|P|4
* -+-+-
* 7|6|5
*
* Let's check that the pixel P will contain at least one point
* assuming that it is covered by the patch.
*
* Since the pixel is covered by the patch, its center will belong to
* (at least) one of the quads:
*
* {(B(u,v), B(u+du,v), B(u,v+dv), B(u+du,v+dv)) for u,v in [0,1]}
*
* If P doesn't contain any of the corners of the quad:
*
* - if one of the corners is in 1,3,5 or 7, other two of them have to
* be in 2,4,6 or 8, thus if the last corner is not in P, the length
* of one of the edges will be > 1/sqrt(2)
*
* - if none of the corners is in 1,3,5 or 7, all of them are in 2,4,6
* and/or 8. If they are all in different regions, they can't
* satisfy the distance constraint. If two of them are in the same
* region (let's say 2), no point is in 6 and again it is impossible
* to have the center of P in the quad respecting the distance
* constraint (both these assertions can be checked by continuity
* considering the length of the edges of a quad with the vertices
* on the edges of P)
*
* Each of the cases led to a contradiction, so P contains at least
* one of the corners of the quad.
*/
/*
* Make sure that errors are less than 1 in fixed point math if you
* change these values.
*
* The error is amplified by about steps^3/4 times.
* The rasterizer always uses a number of steps that is a power of 2.
*
* 256 is the maximum allowed number of steps (to have error < 1)
* using 8.24 for the differences.
*/
#define STEPS_MAX_V 256.0
#define STEPS_MAX_U 256.0
/*
* If the patch/curve is only partially visible, split it to a finer
* resolution to get higher chances to clip (part of) it.
*
* These values have not been computed, but simply obtained
* empirically (by benchmarking some patches). They should never be
* greater than STEPS_MAX_V (or STEPS_MAX_U), but they can be as small
* as 1 (depending on how much you want to spend time in splitting the
* patch/curve when trying to save some rasterization time).
*/
#define STEPS_CLIP_V 64.0
#define STEPS_CLIP_U 64.0
/* Utils */
static inline double
sqlen (cairo_point_double_t p0, cairo_point_double_t p1)
{
cairo_point_double_t delta;
delta.x = p0.x - p1.x;
delta.y = p0.y - p1.y;
return delta.x * delta.x + delta.y * delta.y;
}
static inline int16_t
_color_delta_to_shifted_short (int32_t from, int32_t to, int shift)
{
int32_t delta = to - from;
/* We need to round toward zero, because otherwise adding the
* delta 2^shift times can overflow */
if (delta >= 0)
return delta >> shift;
else
return -((-delta) >> shift);
}
/*
* Convert a number of steps to the equivalent shift.
*
* Input: the square of the minimum number of steps
*
* Output: the smallest integer x such that 2^x > steps
*/
static inline int
sqsteps2shift (double steps_sq)
{
int r;
frexp (MAX (1.0, steps_sq), &r);
return (r + 1) >> 1;
}
/*
* FD functions
*
* A Bezier curve is defined (with respect to a parameter t in
* [0,1]) from its nodes (x,y,z,w) like this:
*
* B(t) = x(1-t)^3 + 3yt(1-t)^2 + 3zt^2(1-t) + wt^3
*
* To efficiently evaluate a Bezier curve, the rasterizer uses forward
* differences. Given x, y, z, w (the 4 nodes of the Bezier curve), it
* is possible to convert them to forward differences form and walk
* over the curve using fd_init (), fd_down () and fd_fwd ().
*
* f[0] is always the value of the Bezier curve for "current" t.
*/
/*
* Initialize the coefficient for forward differences.
*
* Input: x,y,z,w are the 4 nodes of the Bezier curve
*
* Output: f[i] is the i-th difference of the curve
*
* f[0] is the value of the curve for t==0, i.e. f[0]==x.
*
* The initial step is 1; this means that each step increases t by 1
* (so fd_init () immediately followed by fd_fwd (f) n times makes
* f[0] be the value of the curve for t==n).
*/
static inline void
fd_init (double x, double y, double z, double w, double f[4])
{
f[0] = x;
f[1] = w - x;
f[2] = 6. * (w - 2. * z + y);
f[3] = 6. * (w - 3. * z + 3. * y - x);
}
/*
* Halve the step of the coefficients for forward differences.
*
* Input: f[i] is the i-th difference of the curve
*
* Output: f[i] is the i-th difference of the curve with half the
* original step
*
* f[0] is not affected, so the current t is not changed.
*
* The other coefficients are changed so that the step is half the
* original step. This means that doing fd_fwd (f) n times with the
* input f results in the same f[0] as doing fd_fwd (f) 2n times with
* the output f.
*/
static inline void
fd_down (double f[4])
{
f[3] *= 0.125;
f[2] = f[2] * 0.25 - f[3];
f[1] = (f[1] - f[2]) * 0.5;
}
/*
* Perform one step of forward differences along the curve.
*
* Input: f[i] is the i-th difference of the curve
*
* Output: f[i] is the i-th difference of the curve after one step
*/
static inline void
fd_fwd (double f[4])
{
f[0] += f[1];
f[1] += f[2];
f[2] += f[3];
}
/*
* Transform to integer forward differences.
*
* Input: d[n] is the n-th difference (in double precision)
*
* Output: i[n] is the n-th difference (in fixed point precision)
*
* i[0] is 9.23 fixed point, other differences are 4.28 fixed point.
*/
static inline void
fd_fixed (double d[4], int32_t i[4])
{
i[0] = _cairo_fixed_16_16_from_double (256 * 2 * d[0]);
i[1] = _cairo_fixed_16_16_from_double (256 * 16 * d[1]);
i[2] = _cairo_fixed_16_16_from_double (256 * 16 * d[2]);
i[3] = _cairo_fixed_16_16_from_double (256 * 16 * d[3]);
}
/*
* Perform one step of integer forward differences along the curve.
*
* Input: f[n] is the n-th difference
*
* Output: f[n] is the n-th difference
*
* f[0] is 9.23 fixed point, other differences are 4.28 fixed point.
*/
static inline void
fd_fixed_fwd (int32_t f[4])
{
f[0] += (f[1] >> 5) + ((f[1] >> 4) & 1);
f[1] += f[2];
f[2] += f[3];
}
/*
* Compute the minimum number of steps that guarantee that walking
* over a curve will leave no holes.
*
* Input: p[0..3] the nodes of the Bezier curve
*
* Returns: the square of the number of steps
*
* Idea:
*
* We want to make sure that at every step we move by less than
* 1/sqrt(2).
*
* The derivative of the cubic Bezier with nodes (p0, p1, p2, p3) is
* the quadratic Bezier with nodes (p1-p0, p2-p1, p3-p2) scaled by 3,
* so (since a Bezier curve is always bounded by its convex hull), we
* can say that:
*
* max(|B'(t)|) <= 3 max (|p1-p0|, |p2-p1|, |p3-p2|)
*
* We can improve this by noticing that a quadratic Bezier (a,b,c) is
* bounded by the quad (a,lerp(a,b,t),lerp(b,c,t),c) for any t, so
* (substituting the previous values, using t=0.5 and simplifying):
*
* max(|B'(t)|) <= 3 max (|p1-p0|, |p2-p0|/2, |p3-p1|/2, |p3-p2|)
*
* So, to guarantee a maximum step length of 1/sqrt(2) we must do:
*
* 3 max (|p1-p0|, |p2-p0|/2, |p3-p1|/2, |p3-p2|) sqrt(2) steps
*/
static inline double
bezier_steps_sq (cairo_point_double_t p[4])
{
double tmp = sqlen (p[0], p[1]);
tmp = MAX (tmp, sqlen (p[2], p[3]));
tmp = MAX (tmp, sqlen (p[0], p[2]) * .25);
tmp = MAX (tmp, sqlen (p[1], p[3]) * .25);
return 18.0 * tmp;
}
/*
* Split a 1D Bezier cubic using de Casteljau's algorithm.
*
* Input: x,y,z,w the nodes of the Bezier curve
*
* Output: x0,y0,z0,w0 and x1,y1,z1,w1 are respectively the nodes of
* the first half and of the second half of the curve
*
* The output control nodes have to be distinct.
*/
static inline void
split_bezier_1D (double x, double y, double z, double w,
double *x0, double *y0, double *z0, double *w0,
double *x1, double *y1, double *z1, double *w1)
{
double tmp;
*x0 = x;
*w1 = w;
tmp = 0.5 * (y + z);
*y0 = 0.5 * (x + y);
*z1 = 0.5 * (z + w);
*z0 = 0.5 * (*y0 + tmp);
*y1 = 0.5 * (tmp + *z1);
*w0 = *x1 = 0.5 * (*z0 + *y1);
}
/*
* Split a Bezier curve using de Casteljau's algorithm.
*
* Input: p[0..3] the nodes of the Bezier curve
*
* Output: fst_half[0..3] and snd_half[0..3] are respectively the
* nodes of the first and of the second half of the curve
*
* fst_half and snd_half must be different, but they can be the same as
* nodes.
*/
static void
split_bezier (cairo_point_double_t p[4],
cairo_point_double_t fst_half[4],
cairo_point_double_t snd_half[4])
{
split_bezier_1D (p[0].x, p[1].x, p[2].x, p[3].x,
&fst_half[0].x, &fst_half[1].x, &fst_half[2].x, &fst_half[3].x,
&snd_half[0].x, &snd_half[1].x, &snd_half[2].x, &snd_half[3].x);
split_bezier_1D (p[0].y, p[1].y, p[2].y, p[3].y,
&fst_half[0].y, &fst_half[1].y, &fst_half[2].y, &fst_half[3].y,
&snd_half[0].y, &snd_half[1].y, &snd_half[2].y, &snd_half[3].y);
}
typedef enum _intersection {
INSIDE = -1, /* the interval is entirely contained in the reference interval */
OUTSIDE = 0, /* the interval has no intersection with the reference interval */
PARTIAL = 1 /* the interval intersects the reference interval (but is not fully inside it) */
} intersection_t;
/*
* Check if an interval if inside another.
*
* Input: a,b are the extrema of the first interval
* c,d are the extrema of the second interval
*
* Returns: INSIDE iff [a,b) intersection [c,d) = [a,b)
* OUTSIDE iff [a,b) intersection [c,d) = {}
* PARTIAL otherwise
*
* The function assumes a < b and c < d
*
* Note: Bitwise-anding the results along each component gives the
* expected result for [a,b) x [A,B) intersection [c,d) x [C,D).
*/
static inline int
intersect_interval (double a, double b, double c, double d)
{
if (c <= a && b <= d)
return INSIDE;
else if (a >= d || b <= c)
return OUTSIDE;
else
return PARTIAL;
}
/*
* Set the color of a pixel.
*
* Input: data is the base pointer of the image
* width, height are the dimensions of the image
* stride is the stride in bytes between adjacent rows
* x, y are the coordinates of the pixel to be colored
* r,g,b,a are the color components of the color to be set
*
* Output: the (x,y) pixel in data has the (r,g,b,a) color
*
* The input color components are not premultiplied, but the data
* stored in the image is assumed to be in CAIRO_FORMAT_ARGB32 (8 bpc,
* premultiplied).
*
* If the pixel to be set is outside the image, this function does
* nothing.
*/
static inline void
draw_pixel (unsigned char *data, int width, int height, int stride,
int x, int y, uint16_t r, uint16_t g, uint16_t b, uint16_t a)
{
if (likely (0 <= x && 0 <= y && x < width && y < height)) {
uint32_t tr, tg, tb, ta;
/* Premultiply and round */
ta = a;
tr = r * ta + 0x8000;
tg = g * ta + 0x8000;
tb = b * ta + 0x8000;
tr += tr >> 16;
tg += tg >> 16;
tb += tb >> 16;
*((uint32_t*) (data + y*(ptrdiff_t)stride + 4*x)) = ((ta << 16) & 0xff000000) |
((tr >> 8) & 0xff0000) | ((tg >> 16) & 0xff00) | (tb >> 24);
}
}
/*
* Forward-rasterize a cubic curve using forward differences.
*
* Input: data is the base pointer of the image
* width, height are the dimensions of the image
* stride is the stride in bytes between adjacent rows
* ushift is log2(n) if n is the number of desired steps
* dxu[i], dyu[i] are the x,y forward differences of the curve
* r0,g0,b0,a0 are the color components of the start point
* r3,g3,b3,a3 are the color components of the end point
*
* Output: data will be changed to have the requested curve drawn in
* the specified colors
*
* The input color components are not premultiplied, but the data
* stored in the image is assumed to be in CAIRO_FORMAT_ARGB32 (8 bpc,
* premultiplied).
*
* The function draws n+1 pixels, that is from the point at step 0 to
* the point at step n, both included. This is the discrete equivalent
* to drawing the curve for values of the interpolation parameter in
* [0,1] (including both extremes).
*/
static inline void
rasterize_bezier_curve (unsigned char *data, int width, int height, int stride,
int ushift, double dxu[4], double dyu[4],
uint16_t r0, uint16_t g0, uint16_t b0, uint16_t a0,
uint16_t r3, uint16_t g3, uint16_t b3, uint16_t a3)
{
int32_t xu[4], yu[4];
int x0, y0, u, usteps = 1 << ushift;
uint16_t r = r0, g = g0, b = b0, a = a0;
int16_t dr = _color_delta_to_shifted_short (r0, r3, ushift);
int16_t dg = _color_delta_to_shifted_short (g0, g3, ushift);
int16_t db = _color_delta_to_shifted_short (b0, b3, ushift);
int16_t da = _color_delta_to_shifted_short (a0, a3, ushift);
fd_fixed (dxu, xu);
fd_fixed (dyu, yu);
/*
* Use (dxu[0],dyu[0]) as origin for the forward differences.
*
* This makes it possible to handle much larger coordinates (the
* ones that can be represented as cairo_fixed_t)
*/
x0 = _cairo_fixed_from_double (dxu[0]);
y0 = _cairo_fixed_from_double (dyu[0]);
xu[0] = 0;
yu[0] = 0;
for (u = 0; u <= usteps; ++u) {
/*
* This rasterizer assumes that pixels are integer aligned
* squares, so a generic (x,y) point belongs to the pixel with
* top-left coordinates (floor(x), floor(y))
*/
int x = _cairo_fixed_integer_floor (x0 + (xu[0] >> 15) + ((xu[0] >> 14) & 1));
int y = _cairo_fixed_integer_floor (y0 + (yu[0] >> 15) + ((yu[0] >> 14) & 1));
draw_pixel (data, width, height, stride, x, y, r, g, b, a);
fd_fixed_fwd (xu);
fd_fixed_fwd (yu);
r += dr;
g += dg;
b += db;
a += da;
}
}
/*
* Clip, split and rasterize a Bezier curve.
*
* Input: data is the base pointer of the image
* width, height are the dimensions of the image
* stride is the stride in bytes between adjacent rows
* p[i] is the i-th node of the Bezier curve
* c0[i] is the i-th color component at the start point
* c3[i] is the i-th color component at the end point
*
* Output: data will be changed to have the requested curve drawn in
* the specified colors
*
* The input color components are not premultiplied, but the data
* stored in the image is assumed to be in CAIRO_FORMAT_ARGB32 (8 bpc,
* premultiplied).
*
* The color components are red, green, blue and alpha, in this order.
*
* The function guarantees that it will draw the curve with a step
* small enough to never have a distance above 1/sqrt(2) between two
* consecutive points (which is needed to ensure that no hole can
* appear when using this function to rasterize a patch).
*/
static void
draw_bezier_curve (unsigned char *data, int width, int height, int stride,
cairo_point_double_t p[4], double c0[4], double c3[4])
{
double top, bottom, left, right, steps_sq;
int i, v;
top = bottom = p[0].y;
for (i = 1; i < 4; ++i) {
top = MIN (top, p[i].y);
bottom = MAX (bottom, p[i].y);
}
/* Check visibility */
v = intersect_interval (top, bottom, 0, height);
if (v == OUTSIDE)
return;
left = right = p[0].x;
for (i = 1; i < 4; ++i) {
left = MIN (left, p[i].x);
right = MAX (right, p[i].x);
}
v &= intersect_interval (left, right, 0, width);
if (v == OUTSIDE)
return;
steps_sq = bezier_steps_sq (p);
if (steps_sq >= (v == INSIDE ? STEPS_MAX_U * STEPS_MAX_U : STEPS_CLIP_U * STEPS_CLIP_U)) {
/*
* The number of steps is greater than the threshold. This
* means that either the error would become too big if we
* directly rasterized it or that we can probably save some
* time by splitting the curve and clipping part of it
*/
cairo_point_double_t first[4], second[4];
double midc[4];
split_bezier (p, first, second);
midc[0] = (c0[0] + c3[0]) * 0.5;
midc[1] = (c0[1] + c3[1]) * 0.5;
midc[2] = (c0[2] + c3[2]) * 0.5;
midc[3] = (c0[3] + c3[3]) * 0.5;
draw_bezier_curve (data, width, height, stride, first, c0, midc);
draw_bezier_curve (data, width, height, stride, second, midc, c3);
} else {
double xu[4], yu[4];
int ushift = sqsteps2shift (steps_sq), k;
fd_init (p[0].x, p[1].x, p[2].x, p[3].x, xu);
fd_init (p[0].y, p[1].y, p[2].y, p[3].y, yu);
for (k = 0; k < ushift; ++k) {
fd_down (xu);
fd_down (yu);
}
rasterize_bezier_curve (data, width, height, stride, ushift,
xu, yu,
_cairo_color_double_to_short (c0[0]),
_cairo_color_double_to_short (c0[1]),
_cairo_color_double_to_short (c0[2]),
_cairo_color_double_to_short (c0[3]),
_cairo_color_double_to_short (c3[0]),
_cairo_color_double_to_short (c3[1]),
_cairo_color_double_to_short (c3[2]),
_cairo_color_double_to_short (c3[3]));
/* Draw the end point, to make sure that we didn't leave it
* out because of rounding */
draw_pixel (data, width, height, stride,
_cairo_fixed_integer_floor (_cairo_fixed_from_double (p[3].x)),
_cairo_fixed_integer_floor (_cairo_fixed_from_double (p[3].y)),
_cairo_color_double_to_short (c3[0]),
_cairo_color_double_to_short (c3[1]),
_cairo_color_double_to_short (c3[2]),
_cairo_color_double_to_short (c3[3]));
}
}
/*
* Forward-rasterize a cubic Bezier patch using forward differences.
*
* Input: data is the base pointer of the image
* width, height are the dimensions of the image
* stride is the stride in bytes between adjacent rows
* vshift is log2(n) if n is the number of desired steps
* p[i][j], p[i][j] are the the nodes of the Bezier patch
* col[i][j] is the j-th color component of the i-th corner
*
* Output: data will be changed to have the requested patch drawn in
* the specified colors
*
* The nodes of the patch are as follows:
*
* u\v 0 - > 1
* 0 p00 p01 p02 p03
* | p10 p11 p12 p13
* v p20 p21 p22 p23
* 1 p30 p31 p32 p33
*
* i.e. u varies along the first component (rows), v varies along the
* second one (columns).
*
* The color components are red, green, blue and alpha, in this order.
* c[0..3] are the colors in p00, p30, p03, p33 respectively
*
* The input color components are not premultiplied, but the data
* stored in the image is assumed to be in CAIRO_FORMAT_ARGB32 (8 bpc,
* premultiplied).
*
* If the patch folds over itself, the part with the highest v
* parameter is considered above. If both have the same v, the one
* with the highest u parameter is above.
*
* The function draws n+1 curves, that is from the curve at step 0 to
* the curve at step n, both included. This is the discrete equivalent
* to drawing the patch for values of the interpolation parameter in
* [0,1] (including both extremes).
*/
static inline void
rasterize_bezier_patch (unsigned char *data, int width, int height, int stride, int vshift,
cairo_point_double_t p[4][4], double col[4][4])
{
double pv[4][2][4], cstart[4], cend[4], dcstart[4], dcend[4];
int v, i, k;
v = 1 << vshift;
/*
* pv[i][0] is the function (represented using forward
* differences) mapping v to the x coordinate of the i-th node of
* the Bezier curve with parameter u.
* (Likewise p[i][0] gives the y coordinate).
*
* This means that (pv[0][0][0],pv[0][1][0]),
* (pv[1][0][0],pv[1][1][0]), (pv[2][0][0],pv[2][1][0]) and
* (pv[3][0][0],pv[3][1][0]) are the nodes of the Bezier curve for
* the "current" v value (see the FD comments for more details).
*/
for (i = 0; i < 4; ++i) {
fd_init (p[i][0].x, p[i][1].x, p[i][2].x, p[i][3].x, pv[i][0]);
fd_init (p[i][0].y, p[i][1].y, p[i][2].y, p[i][3].y, pv[i][1]);
for (k = 0; k < vshift; ++k) {
fd_down (pv[i][0]);
fd_down (pv[i][1]);
}
}
for (i = 0; i < 4; ++i) {
cstart[i] = col[0][i];
cend[i] = col[1][i];
dcstart[i] = (col[2][i] - col[0][i]) / v;
dcend[i] = (col[3][i] - col[1][i]) / v;
}
v++;
while (v--) {
cairo_point_double_t nodes[4];
for (i = 0; i < 4; ++i) {
nodes[i].x = pv[i][0][0];
nodes[i].y = pv[i][1][0];
}
draw_bezier_curve (data, width, height, stride, nodes, cstart, cend);
for (i = 0; i < 4; ++i) {
fd_fwd (pv[i][0]);
fd_fwd (pv[i][1]);
cstart[i] += dcstart[i];
cend[i] += dcend[i];
}
}
}
/*
* Clip, split and rasterize a Bezier cubic patch.
*
* Input: data is the base pointer of the image
* width, height are the dimensions of the image
* stride is the stride in bytes between adjacent rows
* p[i][j], p[i][j] are the nodes of the patch
* col[i][j] is the j-th color component of the i-th corner
*
* Output: data will be changed to have the requested patch drawn in
* the specified colors
*
* The nodes of the patch are as follows:
*
* u\v 0 - > 1
* 0 p00 p01 p02 p03
* | p10 p11 p12 p13
* v p20 p21 p22 p23
* 1 p30 p31 p32 p33
*
* i.e. u varies along the first component (rows), v varies along the
* second one (columns).
*
* The color components are red, green, blue and alpha, in this order.
* c[0..3] are the colors in p00, p30, p03, p33 respectively
*
* The input color components are not premultiplied, but the data
* stored in the image is assumed to be in CAIRO_FORMAT_ARGB32 (8 bpc,
* premultiplied).
*
* If the patch folds over itself, the part with the highest v
* parameter is considered above. If both have the same v, the one
* with the highest u parameter is above.
*
* The function guarantees that it will draw the patch with a step
* small enough to never have a distance above 1/sqrt(2) between two
* adjacent points (which guarantees that no hole can appear).
*
* This function can be used to rasterize a tile of PDF type 7
* shadings (see http://www.adobe.com/devnet/pdf/pdf_reference.html).
*/
static void
draw_bezier_patch (unsigned char *data, int width, int height, int stride,
cairo_point_double_t p[4][4], double c[4][4])
{
double top, bottom, left, right, steps_sq;
int i, j, v;
top = bottom = p[0][0].y;
for (i = 0; i < 4; ++i) {
for (j= 0; j < 4; ++j) {
top = MIN (top, p[i][j].y);
bottom = MAX (bottom, p[i][j].y);
}
}
v = intersect_interval (top, bottom, 0, height);
if (v == OUTSIDE)
return;
left = right = p[0][0].x;
for (i = 0; i < 4; ++i) {
for (j= 0; j < 4; ++j) {
left = MIN (left, p[i][j].x);
right = MAX (right, p[i][j].x);
}
}
v &= intersect_interval (left, right, 0, width);
if (v == OUTSIDE)
return;
steps_sq = 0;
for (i = 0; i < 4; ++i)
steps_sq = MAX (steps_sq, bezier_steps_sq (p[i]));
if (steps_sq >= (v == INSIDE ? STEPS_MAX_V * STEPS_MAX_V : STEPS_CLIP_V * STEPS_CLIP_V)) {
/* The number of steps is greater than the threshold. This
* means that either the error would become too big if we
* directly rasterized it or that we can probably save some
* time by splitting the curve and clipping part of it. The
* patch is only split in the v direction to guarantee that
* rasterizing each part will overwrite parts with low v with
* overlapping parts with higher v. */
cairo_point_double_t first[4][4], second[4][4];
double subc[4][4];
for (i = 0; i < 4; ++i)
split_bezier (p[i], first[i], second[i]);
for (i = 0; i < 4; ++i) {
subc[0][i] = c[0][i];
subc[1][i] = c[1][i];
subc[2][i] = 0.5 * (c[0][i] + c[2][i]);
subc[3][i] = 0.5 * (c[1][i] + c[3][i]);
}
draw_bezier_patch (data, width, height, stride, first, subc);
for (i = 0; i < 4; ++i) {
subc[0][i] = subc[2][i];
subc[1][i] = subc[3][i];
subc[2][i] = c[2][i];
subc[3][i] = c[3][i];
}
draw_bezier_patch (data, width, height, stride, second, subc);
} else {
rasterize_bezier_patch (data, width, height, stride, sqsteps2shift (steps_sq), p, c);
}
}
/*
* Draw a tensor product shading pattern.
*
* Input: mesh is the mesh pattern
* data is the base pointer of the image
* width, height are the dimensions of the image
* stride is the stride in bytes between adjacent rows
*
* Output: data will be changed to have the pattern drawn on it
*
* data is assumed to be clear and its content is assumed to be in
* CAIRO_FORMAT_ARGB32 (8 bpc, premultiplied).
*
* This function can be used to rasterize a PDF type 7 shading (see
* http://www.adobe.com/devnet/pdf/pdf_reference.html).
*/
void
_cairo_mesh_pattern_rasterize (const cairo_mesh_pattern_t *mesh,
void *data,
int width,
int height,
int stride,
double x_offset,
double y_offset)
{
cairo_point_double_t nodes[4][4];
double colors[4][4];
cairo_matrix_t p2u;
unsigned int i, j, k, n;
cairo_status_t status;
const cairo_mesh_patch_t *patch;
const cairo_color_t *c;
assert (mesh->base.status == CAIRO_STATUS_SUCCESS);
assert (mesh->current_patch == NULL);
p2u = mesh->base.matrix;
status = cairo_matrix_invert (&p2u);
assert (status == CAIRO_STATUS_SUCCESS);
n = _cairo_array_num_elements (&mesh->patches);
patch = _cairo_array_index_const (&mesh->patches, 0);
for (i = 0; i < n; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
nodes[j][k] = patch->points[j][k];
cairo_matrix_transform_point (&p2u, &nodes[j][k].x, &nodes[j][k].y);
nodes[j][k].x += x_offset;
nodes[j][k].y += y_offset;
}
}
c = &patch->colors[0];
colors[0][0] = c->red;
colors[0][1] = c->green;
colors[0][2] = c->blue;
colors[0][3] = c->alpha;
c = &patch->colors[3];
colors[1][0] = c->red;
colors[1][1] = c->green;
colors[1][2] = c->blue;
colors[1][3] = c->alpha;
c = &patch->colors[1];
colors[2][0] = c->red;
colors[2][1] = c->green;
colors[2][2] = c->blue;
colors[2][3] = c->alpha;
c = &patch->colors[2];
colors[3][0] = c->red;
colors[3][1] = c->green;
colors[3][2] = c->blue;
colors[3][3] = c->alpha;
draw_bezier_patch (data, width, height, stride, nodes, colors);
patch++;
}
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-misc.c | /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2007 Adrian Johnson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Adrian Johnson <ajohnson@redneon.com>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include <stdio.h>
#include <errno.h>
#include <locale.h>
#ifdef HAVE_XLOCALE_H
#include <xlocale.h>
#endif
COMPILE_TIME_ASSERT ((int)CAIRO_STATUS_LAST_STATUS < (int)CAIRO_INT_STATUS_UNSUPPORTED);
COMPILE_TIME_ASSERT (CAIRO_INT_STATUS_LAST_STATUS <= 127);
/**
* SECTION:cairo-status
* @Title: Error handling
* @Short_Description: Decoding cairo's status
* @See_Also: cairo_status(), cairo_surface_status(), cairo_pattern_status(),
* cairo_font_face_status(), cairo_scaled_font_status(),
* cairo_region_status()
*
* Cairo uses a single status type to represent all kinds of errors. A status
* value of %CAIRO_STATUS_SUCCESS represents no error and has an integer value
* of zero. All other status values represent an error.
*
* Cairo's error handling is designed to be easy to use and safe. All major
* cairo objects <firstterm>retain</firstterm> an error status internally which
* can be queried anytime by the users using cairo*_status() calls. In
* the mean time, it is safe to call all cairo functions normally even if the
* underlying object is in an error status. This means that no error handling
* code is required before or after each individual cairo function call.
**/
/* Public stuff */
/**
* cairo_status_to_string:
* @status: a cairo status
*
* Provides a human-readable description of a #cairo_status_t.
*
* Returns: a string representation of the status
*
* Since: 1.0
**/
const char *
cairo_status_to_string (cairo_status_t status)
{
switch (status) {
case CAIRO_STATUS_SUCCESS:
return "no error has occurred";
case CAIRO_STATUS_NO_MEMORY:
return "out of memory";
case CAIRO_STATUS_INVALID_RESTORE:
return "cairo_restore() without matching cairo_save()";
case CAIRO_STATUS_INVALID_POP_GROUP:
return "no saved group to pop, i.e. cairo_pop_group() without matching cairo_push_group()";
case CAIRO_STATUS_NO_CURRENT_POINT:
return "no current point defined";
case CAIRO_STATUS_INVALID_MATRIX:
return "invalid matrix (not invertible)";
case CAIRO_STATUS_INVALID_STATUS:
return "invalid value for an input cairo_status_t";
case CAIRO_STATUS_NULL_POINTER:
return "NULL pointer";
case CAIRO_STATUS_INVALID_STRING:
return "input string not valid UTF-8";
case CAIRO_STATUS_INVALID_PATH_DATA:
return "input path data not valid";
case CAIRO_STATUS_READ_ERROR:
return "error while reading from input stream";
case CAIRO_STATUS_WRITE_ERROR:
return "error while writing to output stream";
case CAIRO_STATUS_SURFACE_FINISHED:
return "the target surface has been finished";
case CAIRO_STATUS_SURFACE_TYPE_MISMATCH:
return "the surface type is not appropriate for the operation";
case CAIRO_STATUS_PATTERN_TYPE_MISMATCH:
return "the pattern type is not appropriate for the operation";
case CAIRO_STATUS_INVALID_CONTENT:
return "invalid value for an input cairo_content_t";
case CAIRO_STATUS_INVALID_FORMAT:
return "invalid value for an input cairo_format_t";
case CAIRO_STATUS_INVALID_VISUAL:
return "invalid value for an input Visual*";
case CAIRO_STATUS_FILE_NOT_FOUND:
return "file not found";
case CAIRO_STATUS_INVALID_DASH:
return "invalid value for a dash setting";
case CAIRO_STATUS_INVALID_DSC_COMMENT:
return "invalid value for a DSC comment";
case CAIRO_STATUS_INVALID_INDEX:
return "invalid index passed to getter";
case CAIRO_STATUS_CLIP_NOT_REPRESENTABLE:
return "clip region not representable in desired format";
case CAIRO_STATUS_TEMP_FILE_ERROR:
return "error creating or writing to a temporary file";
case CAIRO_STATUS_INVALID_STRIDE:
return "invalid value for stride";
case CAIRO_STATUS_FONT_TYPE_MISMATCH:
return "the font type is not appropriate for the operation";
case CAIRO_STATUS_USER_FONT_IMMUTABLE:
return "the user-font is immutable";
case CAIRO_STATUS_USER_FONT_ERROR:
return "error occurred in a user-font callback function";
case CAIRO_STATUS_NEGATIVE_COUNT:
return "negative number used where it is not allowed";
case CAIRO_STATUS_INVALID_CLUSTERS:
return "input clusters do not represent the accompanying text and glyph arrays";
case CAIRO_STATUS_INVALID_SLANT:
return "invalid value for an input cairo_font_slant_t";
case CAIRO_STATUS_INVALID_WEIGHT:
return "invalid value for an input cairo_font_weight_t";
case CAIRO_STATUS_INVALID_SIZE:
return "invalid value (typically too big) for the size of the input (surface, pattern, etc.)";
case CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED:
return "user-font method not implemented";
case CAIRO_STATUS_DEVICE_TYPE_MISMATCH:
return "the device type is not appropriate for the operation";
case CAIRO_STATUS_DEVICE_ERROR:
return "an operation to the device caused an unspecified error";
case CAIRO_STATUS_INVALID_MESH_CONSTRUCTION:
return "invalid operation during mesh pattern construction";
case CAIRO_STATUS_DEVICE_FINISHED:
return "the target device has been finished";
case CAIRO_STATUS_JBIG2_GLOBAL_MISSING:
return "CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID used but no CAIRO_MIME_TYPE_JBIG2_GLOBAL data provided";
case CAIRO_STATUS_PNG_ERROR:
return "error occurred in libpng while reading from or writing to a PNG file";
case CAIRO_STATUS_FREETYPE_ERROR:
return "error occurred in libfreetype";
case CAIRO_STATUS_WIN32_GDI_ERROR:
return "error occurred in the Windows Graphics Device Interface";
case CAIRO_STATUS_TAG_ERROR:
return "invalid tag name, attributes, or nesting";
default:
case CAIRO_STATUS_LAST_STATUS:
return "<unknown error status>";
}
}
/**
* cairo_glyph_allocate:
* @num_glyphs: number of glyphs to allocate
*
* Allocates an array of #cairo_glyph_t's.
* This function is only useful in implementations of
* #cairo_user_scaled_font_text_to_glyphs_func_t where the user
* needs to allocate an array of glyphs that cairo will free.
* For all other uses, user can use their own allocation method
* for glyphs.
*
* This function returns %NULL if @num_glyphs is not positive,
* or if out of memory. That means, the %NULL return value
* signals out-of-memory only if @num_glyphs was positive.
*
* Returns: the newly allocated array of glyphs that should be
* freed using cairo_glyph_free()
*
* Since: 1.8
**/
cairo_glyph_t *
cairo_glyph_allocate (int num_glyphs)
{
if (num_glyphs <= 0)
return NULL;
return _cairo_malloc_ab (num_glyphs, sizeof (cairo_glyph_t));
}
slim_hidden_def (cairo_glyph_allocate);
/**
* cairo_glyph_free:
* @glyphs: array of glyphs to free, or %NULL
*
* Frees an array of #cairo_glyph_t's allocated using cairo_glyph_allocate().
* This function is only useful to free glyph array returned
* by cairo_scaled_font_text_to_glyphs() where cairo returns
* an array of glyphs that the user will free.
* For all other uses, user can use their own allocation method
* for glyphs.
*
* Since: 1.8
**/
void
cairo_glyph_free (cairo_glyph_t *glyphs)
{
free (glyphs);
}
slim_hidden_def (cairo_glyph_free);
/**
* cairo_text_cluster_allocate:
* @num_clusters: number of text_clusters to allocate
*
* Allocates an array of #cairo_text_cluster_t's.
* This function is only useful in implementations of
* #cairo_user_scaled_font_text_to_glyphs_func_t where the user
* needs to allocate an array of text clusters that cairo will free.
* For all other uses, user can use their own allocation method
* for text clusters.
*
* This function returns %NULL if @num_clusters is not positive,
* or if out of memory. That means, the %NULL return value
* signals out-of-memory only if @num_clusters was positive.
*
* Returns: the newly allocated array of text clusters that should be
* freed using cairo_text_cluster_free()
*
* Since: 1.8
**/
cairo_text_cluster_t *
cairo_text_cluster_allocate (int num_clusters)
{
if (num_clusters <= 0)
return NULL;
return _cairo_malloc_ab (num_clusters, sizeof (cairo_text_cluster_t));
}
slim_hidden_def (cairo_text_cluster_allocate);
/**
* cairo_text_cluster_free:
* @clusters: array of text clusters to free, or %NULL
*
* Frees an array of #cairo_text_cluster's allocated using cairo_text_cluster_allocate().
* This function is only useful to free text cluster array returned
* by cairo_scaled_font_text_to_glyphs() where cairo returns
* an array of text clusters that the user will free.
* For all other uses, user can use their own allocation method
* for text clusters.
*
* Since: 1.8
**/
void
cairo_text_cluster_free (cairo_text_cluster_t *clusters)
{
free (clusters);
}
slim_hidden_def (cairo_text_cluster_free);
/* Private stuff */
/**
* _cairo_validate_text_clusters:
* @utf8: UTF-8 text
* @utf8_len: length of @utf8 in bytes
* @glyphs: array of glyphs
* @num_glyphs: number of glyphs
* @clusters: array of cluster mapping information
* @num_clusters: number of clusters in the mapping
* @cluster_flags: cluster flags
*
* Check that clusters cover the entire glyphs and utf8 arrays,
* and that cluster boundaries are UTF-8 boundaries.
*
* Return value: %CAIRO_STATUS_SUCCESS upon success, or
* %CAIRO_STATUS_INVALID_CLUSTERS on error.
* The error is either invalid UTF-8 input,
* or bad cluster mapping.
**/
cairo_status_t
_cairo_validate_text_clusters (const char *utf8,
int utf8_len,
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_status_t status;
unsigned int n_bytes = 0;
unsigned int n_glyphs = 0;
int i;
for (i = 0; i < num_clusters; i++) {
int cluster_bytes = clusters[i].num_bytes;
int cluster_glyphs = clusters[i].num_glyphs;
if (cluster_bytes < 0 || cluster_glyphs < 0)
goto BAD;
/* A cluster should cover at least one character or glyph.
* I can't see any use for a 0,0 cluster.
* I can't see an immediate use for a zero-text cluster
* right now either, but they don't harm.
* Zero-glyph clusters on the other hand are useful for
* things like U+200C ZERO WIDTH NON-JOINER */
if (cluster_bytes == 0 && cluster_glyphs == 0)
goto BAD;
/* Since n_bytes and n_glyphs are unsigned, but the rest of
* values involved are signed, we can detect overflow easily */
if (n_bytes+cluster_bytes > (unsigned int)utf8_len || n_glyphs+cluster_glyphs > (unsigned int)num_glyphs)
goto BAD;
/* Make sure we've got valid UTF-8 for the cluster */
status = _cairo_utf8_to_ucs4 (utf8+n_bytes, cluster_bytes, NULL, NULL);
if (unlikely (status))
return _cairo_error (CAIRO_STATUS_INVALID_CLUSTERS);
n_bytes += cluster_bytes ;
n_glyphs += cluster_glyphs;
}
if (n_bytes != (unsigned int) utf8_len || n_glyphs != (unsigned int) num_glyphs) {
BAD:
return _cairo_error (CAIRO_STATUS_INVALID_CLUSTERS);
}
return CAIRO_STATUS_SUCCESS;
}
/**
* _cairo_operator_bounded_by_mask:
* @op: a #cairo_operator_t
*
* A bounded operator is one where mask pixel
* of zero results in no effect on the destination image.
*
* Unbounded operators often require special handling; if you, for
* example, draw trapezoids with an unbounded operator, the effect
* extends past the bounding box of the trapezoids.
*
* Return value: %TRUE if the operator is bounded by the mask operand
**/
cairo_bool_t
_cairo_operator_bounded_by_mask (cairo_operator_t op)
{
switch (op) {
case CAIRO_OPERATOR_CLEAR:
case CAIRO_OPERATOR_SOURCE:
case CAIRO_OPERATOR_OVER:
case CAIRO_OPERATOR_ATOP:
case CAIRO_OPERATOR_DEST:
case CAIRO_OPERATOR_DEST_OVER:
case CAIRO_OPERATOR_DEST_OUT:
case CAIRO_OPERATOR_XOR:
case CAIRO_OPERATOR_ADD:
case CAIRO_OPERATOR_SATURATE:
case CAIRO_OPERATOR_MULTIPLY:
case CAIRO_OPERATOR_SCREEN:
case CAIRO_OPERATOR_OVERLAY:
case CAIRO_OPERATOR_DARKEN:
case CAIRO_OPERATOR_LIGHTEN:
case CAIRO_OPERATOR_COLOR_DODGE:
case CAIRO_OPERATOR_COLOR_BURN:
case CAIRO_OPERATOR_HARD_LIGHT:
case CAIRO_OPERATOR_SOFT_LIGHT:
case CAIRO_OPERATOR_DIFFERENCE:
case CAIRO_OPERATOR_EXCLUSION:
case CAIRO_OPERATOR_HSL_HUE:
case CAIRO_OPERATOR_HSL_SATURATION:
case CAIRO_OPERATOR_HSL_COLOR:
case CAIRO_OPERATOR_HSL_LUMINOSITY:
return TRUE;
case CAIRO_OPERATOR_OUT:
case CAIRO_OPERATOR_IN:
case CAIRO_OPERATOR_DEST_IN:
case CAIRO_OPERATOR_DEST_ATOP:
return FALSE;
}
ASSERT_NOT_REACHED;
return FALSE;
}
/**
* _cairo_operator_bounded_by_source:
* @op: a #cairo_operator_t
*
* A bounded operator is one where source pixels of zero
* (in all four components, r, g, b and a) effect no change
* in the resulting destination image.
*
* Unbounded operators often require special handling; if you, for
* example, copy a surface with the SOURCE operator, the effect
* extends past the bounding box of the source surface.
*
* Return value: %TRUE if the operator is bounded by the source operand
**/
cairo_bool_t
_cairo_operator_bounded_by_source (cairo_operator_t op)
{
switch (op) {
case CAIRO_OPERATOR_OVER:
case CAIRO_OPERATOR_ATOP:
case CAIRO_OPERATOR_DEST:
case CAIRO_OPERATOR_DEST_OVER:
case CAIRO_OPERATOR_DEST_OUT:
case CAIRO_OPERATOR_XOR:
case CAIRO_OPERATOR_ADD:
case CAIRO_OPERATOR_SATURATE:
case CAIRO_OPERATOR_MULTIPLY:
case CAIRO_OPERATOR_SCREEN:
case CAIRO_OPERATOR_OVERLAY:
case CAIRO_OPERATOR_DARKEN:
case CAIRO_OPERATOR_LIGHTEN:
case CAIRO_OPERATOR_COLOR_DODGE:
case CAIRO_OPERATOR_COLOR_BURN:
case CAIRO_OPERATOR_HARD_LIGHT:
case CAIRO_OPERATOR_SOFT_LIGHT:
case CAIRO_OPERATOR_DIFFERENCE:
case CAIRO_OPERATOR_EXCLUSION:
case CAIRO_OPERATOR_HSL_HUE:
case CAIRO_OPERATOR_HSL_SATURATION:
case CAIRO_OPERATOR_HSL_COLOR:
case CAIRO_OPERATOR_HSL_LUMINOSITY:
return TRUE;
case CAIRO_OPERATOR_CLEAR:
case CAIRO_OPERATOR_SOURCE:
case CAIRO_OPERATOR_OUT:
case CAIRO_OPERATOR_IN:
case CAIRO_OPERATOR_DEST_IN:
case CAIRO_OPERATOR_DEST_ATOP:
return FALSE;
}
ASSERT_NOT_REACHED;
return FALSE;
}
uint32_t
_cairo_operator_bounded_by_either (cairo_operator_t op)
{
switch (op) {
default:
ASSERT_NOT_REACHED;
case CAIRO_OPERATOR_OVER:
case CAIRO_OPERATOR_ATOP:
case CAIRO_OPERATOR_DEST:
case CAIRO_OPERATOR_DEST_OVER:
case CAIRO_OPERATOR_DEST_OUT:
case CAIRO_OPERATOR_XOR:
case CAIRO_OPERATOR_ADD:
case CAIRO_OPERATOR_SATURATE:
case CAIRO_OPERATOR_MULTIPLY:
case CAIRO_OPERATOR_SCREEN:
case CAIRO_OPERATOR_OVERLAY:
case CAIRO_OPERATOR_DARKEN:
case CAIRO_OPERATOR_LIGHTEN:
case CAIRO_OPERATOR_COLOR_DODGE:
case CAIRO_OPERATOR_COLOR_BURN:
case CAIRO_OPERATOR_HARD_LIGHT:
case CAIRO_OPERATOR_SOFT_LIGHT:
case CAIRO_OPERATOR_DIFFERENCE:
case CAIRO_OPERATOR_EXCLUSION:
case CAIRO_OPERATOR_HSL_HUE:
case CAIRO_OPERATOR_HSL_SATURATION:
case CAIRO_OPERATOR_HSL_COLOR:
case CAIRO_OPERATOR_HSL_LUMINOSITY:
return CAIRO_OPERATOR_BOUND_BY_MASK | CAIRO_OPERATOR_BOUND_BY_SOURCE;
case CAIRO_OPERATOR_CLEAR:
case CAIRO_OPERATOR_SOURCE:
return CAIRO_OPERATOR_BOUND_BY_MASK;
case CAIRO_OPERATOR_OUT:
case CAIRO_OPERATOR_IN:
case CAIRO_OPERATOR_DEST_IN:
case CAIRO_OPERATOR_DEST_ATOP:
return 0;
}
}
#if DISABLE_SOME_FLOATING_POINT
/* This function is identical to the C99 function lround(), except that it
* performs arithmetic rounding (floor(d + .5) instead of away-from-zero rounding) and
* has a valid input range of (INT_MIN, INT_MAX] instead of
* [INT_MIN, INT_MAX]. It is much faster on both x86 and FPU-less systems
* than other commonly used methods for rounding (lround, round, rint, lrint
* or float (d + 0.5)).
*
* The reason why this function is much faster on x86 than other
* methods is due to the fact that it avoids the fldcw instruction.
* This instruction incurs a large performance penalty on modern Intel
* processors due to how it prevents efficient instruction pipelining.
*
* The reason why this function is much faster on FPU-less systems is for
* an entirely different reason. All common rounding methods involve multiple
* floating-point operations. Each one of these operations has to be
* emulated in software, which adds up to be a large performance penalty.
* This function doesn't perform any floating-point calculations, and thus
* avoids this penalty.
*/
int
_cairo_lround (double d)
{
uint32_t top, shift_amount, output;
union {
double d;
uint64_t ui64;
uint32_t ui32[2];
} u;
u.d = d;
/* If the integer word order doesn't match the float word order, we swap
* the words of the input double. This is needed because we will be
* treating the whole double as a 64-bit unsigned integer. Notice that we
* use WORDS_BIGENDIAN to detect the integer word order, which isn't
* exactly correct because WORDS_BIGENDIAN refers to byte order, not word
* order. Thus, we are making the assumption that the byte order is the
* same as the integer word order which, on the modern machines that we
* care about, is OK.
*/
#if ( defined(FLOAT_WORDS_BIGENDIAN) && !defined(WORDS_BIGENDIAN)) || \
(!defined(FLOAT_WORDS_BIGENDIAN) && defined(WORDS_BIGENDIAN))
{
uint32_t temp = u.ui32[0];
u.ui32[0] = u.ui32[1];
u.ui32[1] = temp;
}
#endif
#ifdef WORDS_BIGENDIAN
#define MSW (0) /* Most Significant Word */
#define LSW (1) /* Least Significant Word */
#else
#define MSW (1)
#define LSW (0)
#endif
/* By shifting the most significant word of the input double to the
* right 20 places, we get the very "top" of the double where the exponent
* and sign bit lie.
*/
top = u.ui32[MSW] >> 20;
/* Here, we calculate how much we have to shift the mantissa to normalize
* it to an integer value. We extract the exponent "top" by masking out the
* sign bit, then we calculate the shift amount by subtracting the exponent
* from the bias. Notice that the correct bias for 64-bit doubles is
* actually 1075, but we use 1053 instead for two reasons:
*
* 1) To perform rounding later on, we will first need the target
* value in a 31.1 fixed-point format. Thus, the bias needs to be one
* less: (1075 - 1: 1074).
*
* 2) To avoid shifting the mantissa as a full 64-bit integer (which is
* costly on certain architectures), we break the shift into two parts.
* First, the upper and lower parts of the mantissa are shifted
* individually by a constant amount that all valid inputs will require
* at the very least. This amount is chosen to be 21, because this will
* allow the two parts of the mantissa to later be combined into a
* single 32-bit representation, on which the remainder of the shift
* will be performed. Thus, we decrease the bias by an additional 21:
* (1074 - 21: 1053).
*/
shift_amount = 1053 - (top & 0x7FF);
/* We are done with the exponent portion in "top", so here we shift it off
* the end.
*/
top >>= 11;
/* Before we perform any operations on the mantissa, we need to OR in
* the implicit 1 at the top (see the IEEE-754 spec). We needn't mask
* off the sign bit nor the exponent bits because these higher bits won't
* make a bit of difference in the rest of our calculations.
*/
u.ui32[MSW] |= 0x100000;
/* If the input double is negative, we have to decrease the mantissa
* by a hair. This is an important part of performing arithmetic rounding,
* as negative numbers must round towards positive infinity in the
* halfwase case of -x.5. Since "top" contains only the sign bit at this
* point, we can just decrease the mantissa by the value of "top".
*/
u.ui64 -= top;
/* By decrementing "top", we create a bitmask with a value of either
* 0x0 (if the input was negative) or 0xFFFFFFFF (if the input was positive
* and thus the unsigned subtraction underflowed) that we'll use later.
*/
top--;
/* Here, we shift the mantissa by the constant value as described above.
* We can emulate a 64-bit shift right by 21 through shifting the top 32
* bits left 11 places and ORing in the bottom 32 bits shifted 21 places
* to the right. Both parts of the mantissa are now packed into a single
* 32-bit integer. Although we severely truncate the lower part in the
* process, we still have enough significant bits to perform the conversion
* without error (for all valid inputs).
*/
output = (u.ui32[MSW] << 11) | (u.ui32[LSW] >> 21);
/* Next, we perform the shift that converts the X.Y fixed-point number
* currently found in "output" to the desired 31.1 fixed-point format
* needed for the following rounding step. It is important to consider
* all possible values for "shift_amount" at this point:
*
* - {shift_amount < 0} Since shift_amount is an unsigned integer, it
* really can't have a value less than zero. But, if the shift_amount
* calculation above caused underflow (which would happen with
* input > INT_MAX or input <= INT_MIN) then shift_amount will now be
* a very large number, and so this shift will result in complete
* garbage. But that's OK, as the input was out of our range, so our
* output is undefined.
*
* - {shift_amount > 31} If the magnitude of the input was very small
* (i.e. |input| << 1.0), shift_amount will have a value greater than
* 31. Thus, this shift will also result in garbage. After performing
* the shift, we will zero-out "output" if this is the case.
*
* - {0 <= shift_amount < 32} In this case, the shift will properly convert
* the mantissa into a 31.1 fixed-point number.
*/
output >>= shift_amount;
/* This is where we perform rounding with the 31.1 fixed-point number.
* Since what we're after is arithmetic rounding, we simply add the single
* fractional bit into the integer part of "output", and just keep the
* integer part.
*/
output = (output >> 1) + (output & 1);
/* Here, we zero-out the result if the magnitude if the input was very small
* (as explained in the section above). Notice that all input out of the
* valid range is also caught by this condition, which means we produce 0
* for all invalid input, which is a nice side effect.
*
* The most straightforward way to do this would be:
*
* if (shift_amount > 31)
* output = 0;
*
* But we can use a little trick to avoid the potential branch. The
* expression (shift_amount > 31) will be either 1 or 0, which when
* decremented will be either 0x0 or 0xFFFFFFFF (unsigned underflow),
* which can be used to conditionally mask away all the bits in "output"
* (in the 0x0 case), effectively zeroing it out. Certain, compilers would
* have done this for us automatically.
*/
output &= ((shift_amount > 31) - 1);
/* If the input double was a negative number, then we have to negate our
* output. The most straightforward way to do this would be:
*
* if (!top)
* output = -output;
*
* as "top" at this point is either 0x0 (if the input was negative) or
* 0xFFFFFFFF (if the input was positive). But, we can use a trick to
* avoid the branch. Observe that the following snippet of code has the
* same effect as the reference snippet above:
*
* if (!top)
* output = 0 - output;
* else
* output = output - 0;
*
* Armed with the bitmask found in "top", we can condense the two statements
* into the following:
*
* output = (output & top) - (output & ~top);
*
* where, in the case that the input double was negative, "top" will be 0,
* and the statement will be equivalent to:
*
* output = (0) - (output);
*
* and if the input double was positive, "top" will be 0xFFFFFFFF, and the
* statement will be equivalent to:
*
* output = (output) - (0);
*
* Which, as pointed out earlier, is equivalent to the original reference
* snippet.
*/
output = (output & top) - (output & ~top);
return output;
#undef MSW
#undef LSW
}
#endif
/* Convert a 32-bit IEEE single precision floating point number to a
* 'half' representation (s10.5)
*/
uint16_t
_cairo_half_from_float (float f)
{
union {
uint32_t ui;
float f;
} u;
int s, e, m;
u.f = f;
s = (u.ui >> 16) & 0x00008000;
e = ((u.ui >> 23) & 0x000000ff) - (127 - 15);
m = u.ui & 0x007fffff;
if (e <= 0) {
if (e < -10) {
/* underflow */
return 0;
}
m = (m | 0x00800000) >> (1 - e);
/* round to nearest, round 0.5 up. */
if (m & 0x00001000)
m += 0x00002000;
return s | (m >> 13);
} else if (e == 0xff - (127 - 15)) {
if (m == 0) {
/* infinity */
return s | 0x7c00;
} else {
/* nan */
m >>= 13;
return s | 0x7c00 | m | (m == 0);
}
} else {
/* round to nearest, round 0.5 up. */
if (m & 0x00001000) {
m += 0x00002000;
if (m & 0x00800000) {
m = 0;
e += 1;
}
}
if (e > 30) {
/* overflow -> infinity */
return s | 0x7c00;
}
return s | (e << 10) | (m >> 13);
}
}
#ifndef __BIONIC__
# include <locale.h>
const char *
_cairo_get_locale_decimal_point (void)
{
struct lconv *locale_data = localeconv ();
return locale_data->decimal_point;
}
#else
/* Android's Bionic libc doesn't provide decimal_point */
const char *
_cairo_get_locale_decimal_point (void)
{
return ".";
}
#endif
#if defined (HAVE_NEWLOCALE) && defined (HAVE_STRTOD_L)
static locale_t C_locale;
static locale_t
get_C_locale (void)
{
locale_t C;
retry:
C = (locale_t) _cairo_atomic_ptr_get ((void **) &C_locale);
if (unlikely (!C)) {
C = newlocale (LC_ALL_MASK, "C", NULL);
if (!_cairo_atomic_ptr_cmpxchg ((void **) &C_locale, NULL, C)) {
freelocale (C_locale);
goto retry;
}
}
return C;
}
double
_cairo_strtod (const char *nptr, char **endptr)
{
return strtod_l (nptr, endptr, get_C_locale ());
}
#else
/* strtod replacement that ignores locale and only accepts decimal points */
double
_cairo_strtod (const char *nptr, char **endptr)
{
const char *decimal_point;
int decimal_point_len;
const char *p;
char buf[100];
char *bufptr;
char *bufend = buf + sizeof(buf) - 1;
double value;
char *end;
int delta;
cairo_bool_t have_dp;
decimal_point = _cairo_get_locale_decimal_point ();
decimal_point_len = strlen (decimal_point);
assert (decimal_point_len != 0);
p = nptr;
bufptr = buf;
delta = 0;
have_dp = FALSE;
while (*p && _cairo_isspace (*p)) {
p++;
delta++;
}
while (*p && (bufptr + decimal_point_len < bufend)) {
if (_cairo_isdigit (*p)) {
*bufptr++ = *p;
} else if (*p == '.') {
if (have_dp)
break;
strncpy (bufptr, decimal_point, decimal_point_len);
bufptr += decimal_point_len;
delta -= decimal_point_len - 1;
have_dp = TRUE;
} else {
break;
}
p++;
}
*bufptr = 0;
value = strtod (buf, &end);
if (endptr) {
if (end == buf)
*endptr = (char*)(nptr);
else
*endptr = (char*)(nptr + (end - buf) + delta);
}
return value;
}
#endif
/**
* _cairo_fopen:
* @filename: filename to open
* @mode: mode string with which to open the file
* @file_out: reference to file handle
*
* Exactly like the C library function, but possibly doing encoding
* conversion on the filename. On all platforms, the filename is
* passed directly to the system, but on Windows, the filename is
* interpreted as UTF-8, rather than in a codepage that would depend
* on system settings.
*
* Return value: CAIRO_STATUS_SUCCESS when the filename was converted
* successfully to the native encoding, or the error reported by
* _cairo_utf8_to_utf16 otherwise. To check if the file handle could
* be obtained, dereference file_out and compare its value against
* NULL
**/
cairo_status_t
_cairo_fopen (const char *filename, const char *mode, FILE **file_out)
{
FILE *result;
#ifdef _WIN32 /* also defined on x86_64 */
uint16_t *filename_w;
uint16_t *mode_w;
cairo_status_t status;
*file_out = NULL;
if (filename == NULL || mode == NULL) {
errno = EINVAL;
return CAIRO_STATUS_SUCCESS;
}
if ((status = _cairo_utf8_to_utf16 (filename, -1, &filename_w, NULL)) != CAIRO_STATUS_SUCCESS) {
errno = EINVAL;
return status;
}
if ((status = _cairo_utf8_to_utf16 (mode, -1, &mode_w, NULL)) != CAIRO_STATUS_SUCCESS) {
free (filename_w);
errno = EINVAL;
return status;
}
result = _wfopen(filename_w, mode_w);
free (filename_w);
free (mode_w);
#else /* Use fopen directly */
result = fopen (filename, mode);
#endif
*file_out = result;
return CAIRO_STATUS_SUCCESS;
}
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
/* We require Windows 2000 features such as ETO_PDY */
#if !defined(WINVER) || (WINVER < 0x0500)
# define WINVER 0x0500
#endif
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
# define _WIN32_WINNT 0x0500
#endif
#include <windows.h>
#include <io.h>
#if !_WIN32_WCE
/* tmpfile() replacement for Windows.
*
* On Windows tmpfile() creates the file in the root directory. This
* may fail due to insufficient privileges. However, this isn't a
* problem on Windows CE so we don't use it there.
*/
FILE *
_cairo_win32_tmpfile (void)
{
DWORD path_len;
WCHAR path_name[MAX_PATH + 1];
WCHAR file_name[MAX_PATH + 1];
HANDLE handle;
int fd;
FILE *fp;
path_len = GetTempPathW (MAX_PATH, path_name);
if (path_len <= 0 || path_len >= MAX_PATH)
return NULL;
if (GetTempFileNameW (path_name, L"ps_", 0, file_name) == 0)
return NULL;
handle = CreateFileW (file_name,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
NULL);
if (handle == INVALID_HANDLE_VALUE) {
DeleteFileW (file_name);
return NULL;
}
fd = _open_osfhandle((intptr_t) handle, 0);
if (fd < 0) {
CloseHandle (handle);
return NULL;
}
fp = fdopen(fd, "w+b");
if (fp == NULL) {
_close(fd);
return NULL;
}
return fp;
}
#endif /* !_WIN32_WCE */
#endif /* _WIN32 */
typedef struct _cairo_intern_string {
cairo_hash_entry_t hash_entry;
int len;
char *string;
} cairo_intern_string_t;
static cairo_hash_table_t *_cairo_intern_string_ht;
unsigned long
_cairo_string_hash (const char *str, int len)
{
const signed char *p = (const signed char *) str;
unsigned int h = *p;
for (p += 1; len > 0; len--, p++)
h = (h << 5) - h + *p;
return h;
}
static cairo_bool_t
_intern_string_equal (const void *_a, const void *_b)
{
const cairo_intern_string_t *a = _a;
const cairo_intern_string_t *b = _b;
if (a->len != b->len)
return FALSE;
return memcmp (a->string, b->string, a->len) == 0;
}
cairo_status_t
_cairo_intern_string (const char **str_inout, int len)
{
char *str = (char *) *str_inout;
cairo_intern_string_t tmpl, *istring;
cairo_status_t status = CAIRO_STATUS_SUCCESS;
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (len < 0)
len = strlen (str);
tmpl.hash_entry.hash = _cairo_string_hash (str, len);
tmpl.len = len;
tmpl.string = (char *) str;
CAIRO_MUTEX_LOCK (_cairo_intern_string_mutex);
if (_cairo_intern_string_ht == NULL) {
_cairo_intern_string_ht = _cairo_hash_table_create (_intern_string_equal);
if (unlikely (_cairo_intern_string_ht == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto BAIL;
}
}
istring = _cairo_hash_table_lookup (_cairo_intern_string_ht,
&tmpl.hash_entry);
if (istring == NULL) {
istring = _cairo_malloc (sizeof (cairo_intern_string_t) + len + 1);
if (likely (istring != NULL)) {
istring->hash_entry.hash = tmpl.hash_entry.hash;
istring->len = tmpl.len;
istring->string = (char *) (istring + 1);
memcpy (istring->string, str, len);
istring->string[len] = '\0';
status = _cairo_hash_table_insert (_cairo_intern_string_ht,
&istring->hash_entry);
if (unlikely (status)) {
free (istring);
goto BAIL;
}
} else {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto BAIL;
}
}
*str_inout = istring->string;
BAIL:
CAIRO_MUTEX_UNLOCK (_cairo_intern_string_mutex);
return status;
}
static void
_intern_string_pluck (void *entry, void *closure)
{
_cairo_hash_table_remove (closure, entry);
free (entry);
}
void
_cairo_intern_string_reset_static_data (void)
{
CAIRO_MUTEX_LOCK (_cairo_intern_string_mutex);
if (_cairo_intern_string_ht != NULL) {
_cairo_hash_table_foreach (_cairo_intern_string_ht,
_intern_string_pluck,
_cairo_intern_string_ht);
_cairo_hash_table_destroy(_cairo_intern_string_ht);
_cairo_intern_string_ht = NULL;
}
CAIRO_MUTEX_UNLOCK (_cairo_intern_string_mutex);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mono-scan-converter.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/*
* Copyright (c) 2011 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "cairoint.h"
#include "cairo-spans-private.h"
#include "cairo-error-private.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
struct quorem {
int32_t quo;
int32_t rem;
};
struct edge {
struct edge *next, *prev;
int32_t height_left;
int32_t dir;
int32_t vertical;
int32_t dy;
struct quorem x;
struct quorem dxdy;
};
/* A collection of sorted and vertically clipped edges of the polygon.
* Edges are moved from the polygon to an active list while scan
* converting. */
struct polygon {
/* The vertical clip extents. */
int32_t ymin, ymax;
int num_edges;
struct edge *edges;
/* Array of edges all starting in the same bucket. An edge is put
* into bucket EDGE_BUCKET_INDEX(edge->ytop, polygon->ymin) when
* it is added to the polygon. */
struct edge **y_buckets;
struct edge *y_buckets_embedded[64];
struct edge edges_embedded[32];
};
struct mono_scan_converter {
struct polygon polygon[1];
/* Leftmost edge on the current scan line. */
struct edge head, tail;
int is_vertical;
cairo_half_open_span_t *spans;
cairo_half_open_span_t spans_embedded[64];
int num_spans;
/* Clip box. */
int32_t xmin, xmax;
int32_t ymin, ymax;
};
#define I(x) _cairo_fixed_integer_round_down(x)
/* Compute the floored division a/b. Assumes / and % perform symmetric
* division. */
inline static struct quorem
floored_divrem(int a, int b)
{
struct quorem qr;
qr.quo = a/b;
qr.rem = a%b;
if ((a^b)<0 && qr.rem) {
qr.quo -= 1;
qr.rem += b;
}
return qr;
}
/* Compute the floored division (x*a)/b. Assumes / and % perform symmetric
* division. */
static struct quorem
floored_muldivrem(int x, int a, int b)
{
struct quorem qr;
long long xa = (long long)x*a;
qr.quo = xa/b;
qr.rem = xa%b;
if ((xa>=0) != (b>=0) && qr.rem) {
qr.quo -= 1;
qr.rem += b;
}
return qr;
}
static cairo_status_t
polygon_init (struct polygon *polygon, int ymin, int ymax)
{
unsigned h = ymax - ymin + 1;
polygon->y_buckets = polygon->y_buckets_embedded;
if (h > ARRAY_LENGTH (polygon->y_buckets_embedded)) {
polygon->y_buckets = _cairo_malloc_ab (h, sizeof (struct edge *));
if (unlikely (NULL == polygon->y_buckets))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
memset (polygon->y_buckets, 0, h * sizeof (struct edge *));
polygon->y_buckets[h-1] = (void *)-1;
polygon->ymin = ymin;
polygon->ymax = ymax;
return CAIRO_STATUS_SUCCESS;
}
static void
polygon_fini (struct polygon *polygon)
{
if (polygon->y_buckets != polygon->y_buckets_embedded)
free (polygon->y_buckets);
if (polygon->edges != polygon->edges_embedded)
free (polygon->edges);
}
static void
_polygon_insert_edge_into_its_y_bucket(struct polygon *polygon,
struct edge *e,
int y)
{
struct edge **ptail = &polygon->y_buckets[y - polygon->ymin];
if (*ptail)
(*ptail)->prev = e;
e->next = *ptail;
e->prev = NULL;
*ptail = e;
}
inline static void
polygon_add_edge (struct polygon *polygon,
const cairo_edge_t *edge)
{
struct edge *e;
cairo_fixed_t dx;
cairo_fixed_t dy;
int y, ytop, ybot;
int ymin = polygon->ymin;
int ymax = polygon->ymax;
y = I(edge->top);
ytop = MAX(y, ymin);
y = I(edge->bottom);
ybot = MIN(y, ymax);
if (ybot <= ytop)
return;
e = polygon->edges + polygon->num_edges++;
e->height_left = ybot - ytop;
e->dir = edge->dir;
dx = edge->line.p2.x - edge->line.p1.x;
dy = edge->line.p2.y - edge->line.p1.y;
if (dx == 0) {
e->vertical = TRUE;
e->x.quo = edge->line.p1.x;
e->x.rem = 0;
e->dxdy.quo = 0;
e->dxdy.rem = 0;
e->dy = 0;
} else {
e->vertical = FALSE;
e->dxdy = floored_muldivrem (dx, CAIRO_FIXED_ONE, dy);
e->dy = dy;
e->x = floored_muldivrem (ytop * CAIRO_FIXED_ONE + CAIRO_FIXED_FRAC_MASK/2 - edge->line.p1.y,
dx, dy);
e->x.quo += edge->line.p1.x;
}
e->x.rem -= dy;
_polygon_insert_edge_into_its_y_bucket (polygon, e, ytop);
}
static struct edge *
merge_sorted_edges (struct edge *head_a, struct edge *head_b)
{
struct edge *head, **next, *prev;
int32_t x;
prev = head_a->prev;
next = &head;
if (head_a->x.quo <= head_b->x.quo) {
head = head_a;
} else {
head = head_b;
head_b->prev = prev;
goto start_with_b;
}
do {
x = head_b->x.quo;
while (head_a != NULL && head_a->x.quo <= x) {
prev = head_a;
next = &head_a->next;
head_a = head_a->next;
}
head_b->prev = prev;
*next = head_b;
if (head_a == NULL)
return head;
start_with_b:
x = head_a->x.quo;
while (head_b != NULL && head_b->x.quo <= x) {
prev = head_b;
next = &head_b->next;
head_b = head_b->next;
}
head_a->prev = prev;
*next = head_a;
if (head_b == NULL)
return head;
} while (1);
}
static struct edge *
sort_edges (struct edge *list,
unsigned int level,
struct edge **head_out)
{
struct edge *head_other, *remaining;
unsigned int i;
head_other = list->next;
if (head_other == NULL) {
*head_out = list;
return NULL;
}
remaining = head_other->next;
if (list->x.quo <= head_other->x.quo) {
*head_out = list;
head_other->next = NULL;
} else {
*head_out = head_other;
head_other->prev = list->prev;
head_other->next = list;
list->prev = head_other;
list->next = NULL;
}
for (i = 0; i < level && remaining; i++) {
remaining = sort_edges (remaining, i, &head_other);
*head_out = merge_sorted_edges (*head_out, head_other);
}
return remaining;
}
static struct edge *
merge_unsorted_edges (struct edge *head, struct edge *unsorted)
{
sort_edges (unsorted, UINT_MAX, &unsorted);
return merge_sorted_edges (head, unsorted);
}
inline static void
active_list_merge_edges (struct mono_scan_converter *c, struct edge *edges)
{
struct edge *e;
for (e = edges; c->is_vertical && e; e = e->next)
c->is_vertical = e->vertical;
c->head.next = merge_unsorted_edges (c->head.next, edges);
}
inline static void
add_span (struct mono_scan_converter *c, int x1, int x2)
{
int n;
if (x1 < c->xmin)
x1 = c->xmin;
if (x2 > c->xmax)
x2 = c->xmax;
if (x2 <= x1)
return;
n = c->num_spans++;
c->spans[n].x = x1;
c->spans[n].coverage = 255;
n = c->num_spans++;
c->spans[n].x = x2;
c->spans[n].coverage = 0;
}
inline static void
row (struct mono_scan_converter *c, unsigned int mask)
{
struct edge *edge = c->head.next;
int xstart = INT_MIN, prev_x = INT_MIN;
int winding = 0;
c->num_spans = 0;
while (&c->tail != edge) {
struct edge *next = edge->next;
int xend = I(edge->x.quo);
if (--edge->height_left) {
if (!edge->vertical) {
edge->x.quo += edge->dxdy.quo;
edge->x.rem += edge->dxdy.rem;
if (edge->x.rem >= 0) {
++edge->x.quo;
edge->x.rem -= edge->dy;
}
}
if (edge->x.quo < prev_x) {
struct edge *pos = edge->prev;
pos->next = next;
next->prev = pos;
do {
pos = pos->prev;
} while (edge->x.quo < pos->x.quo);
pos->next->prev = edge;
edge->next = pos->next;
edge->prev = pos;
pos->next = edge;
} else
prev_x = edge->x.quo;
} else {
edge->prev->next = next;
next->prev = edge->prev;
}
winding += edge->dir;
if ((winding & mask) == 0) {
if (I(next->x.quo) > xend + 1) {
add_span (c, xstart, xend);
xstart = INT_MIN;
}
} else if (xstart == INT_MIN)
xstart = xend;
edge = next;
}
}
inline static void dec (struct edge *e, int h)
{
e->height_left -= h;
if (e->height_left == 0) {
e->prev->next = e->next;
e->next->prev = e->prev;
}
}
static cairo_status_t
_mono_scan_converter_init(struct mono_scan_converter *c,
int xmin, int ymin,
int xmax, int ymax)
{
cairo_status_t status;
int max_num_spans;
status = polygon_init (c->polygon, ymin, ymax);
if (unlikely (status))
return status;
max_num_spans = xmax - xmin + 1;
if (max_num_spans > ARRAY_LENGTH(c->spans_embedded)) {
c->spans = _cairo_malloc_ab (max_num_spans,
sizeof (cairo_half_open_span_t));
if (unlikely (c->spans == NULL)) {
polygon_fini (c->polygon);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
} else
c->spans = c->spans_embedded;
c->xmin = xmin;
c->xmax = xmax;
c->ymin = ymin;
c->ymax = ymax;
c->head.vertical = 1;
c->head.height_left = INT_MAX;
c->head.x.quo = _cairo_fixed_from_int (_cairo_fixed_integer_part (INT_MIN));
c->head.prev = NULL;
c->head.next = &c->tail;
c->tail.prev = &c->head;
c->tail.next = NULL;
c->tail.x.quo = _cairo_fixed_from_int (_cairo_fixed_integer_part (INT_MAX));
c->tail.height_left = INT_MAX;
c->tail.vertical = 1;
c->is_vertical = 1;
return CAIRO_STATUS_SUCCESS;
}
static void
_mono_scan_converter_fini(struct mono_scan_converter *self)
{
if (self->spans != self->spans_embedded)
free (self->spans);
polygon_fini(self->polygon);
}
static cairo_status_t
mono_scan_converter_allocate_edges(struct mono_scan_converter *c,
int num_edges)
{
c->polygon->num_edges = 0;
c->polygon->edges = c->polygon->edges_embedded;
if (num_edges > ARRAY_LENGTH (c->polygon->edges_embedded)) {
c->polygon->edges = _cairo_malloc_ab (num_edges, sizeof (struct edge));
if (unlikely (c->polygon->edges == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
return CAIRO_STATUS_SUCCESS;
}
static void
mono_scan_converter_add_edge (struct mono_scan_converter *c,
const cairo_edge_t *edge)
{
polygon_add_edge (c->polygon, edge);
}
static void
step_edges (struct mono_scan_converter *c, int count)
{
struct edge *edge;
for (edge = c->head.next; edge != &c->tail; edge = edge->next) {
edge->height_left -= count;
if (! edge->height_left) {
edge->prev->next = edge->next;
edge->next->prev = edge->prev;
}
}
}
static cairo_status_t
mono_scan_converter_render(struct mono_scan_converter *c,
unsigned int winding_mask,
cairo_span_renderer_t *renderer)
{
struct polygon *polygon = c->polygon;
int i, j, h = c->ymax - c->ymin;
cairo_status_t status;
for (i = 0; i < h; i = j) {
j = i + 1;
if (polygon->y_buckets[i])
active_list_merge_edges (c, polygon->y_buckets[i]);
if (c->is_vertical) {
int min_height;
struct edge *e;
e = c->head.next;
min_height = e->height_left;
while (e != &c->tail) {
if (e->height_left < min_height)
min_height = e->height_left;
e = e->next;
}
while (--min_height >= 1 && polygon->y_buckets[j] == NULL)
j++;
if (j != i + 1)
step_edges (c, j - (i + 1));
}
row (c, winding_mask);
if (c->num_spans) {
status = renderer->render_rows (renderer, c->ymin+i, j-i,
c->spans, c->num_spans);
if (unlikely (status))
return status;
}
/* XXX recompute after dropping edges? */
if (c->head.next == &c->tail)
c->is_vertical = 1;
}
return CAIRO_STATUS_SUCCESS;
}
struct _cairo_mono_scan_converter {
cairo_scan_converter_t base;
struct mono_scan_converter converter[1];
cairo_fill_rule_t fill_rule;
};
typedef struct _cairo_mono_scan_converter cairo_mono_scan_converter_t;
static void
_cairo_mono_scan_converter_destroy (void *converter)
{
cairo_mono_scan_converter_t *self = converter;
_mono_scan_converter_fini (self->converter);
free(self);
}
cairo_status_t
_cairo_mono_scan_converter_add_polygon (void *converter,
const cairo_polygon_t *polygon)
{
cairo_mono_scan_converter_t *self = converter;
cairo_status_t status;
int i;
#if 0
FILE *file = fopen ("polygon.txt", "w");
_cairo_debug_print_polygon (file, polygon);
fclose (file);
#endif
status = mono_scan_converter_allocate_edges (self->converter,
polygon->num_edges);
if (unlikely (status))
return status;
for (i = 0; i < polygon->num_edges; i++)
mono_scan_converter_add_edge (self->converter, &polygon->edges[i]);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_mono_scan_converter_generate (void *converter,
cairo_span_renderer_t *renderer)
{
cairo_mono_scan_converter_t *self = converter;
return mono_scan_converter_render (self->converter,
self->fill_rule == CAIRO_FILL_RULE_WINDING ? ~0 : 1,
renderer);
}
cairo_scan_converter_t *
_cairo_mono_scan_converter_create (int xmin,
int ymin,
int xmax,
int ymax,
cairo_fill_rule_t fill_rule)
{
cairo_mono_scan_converter_t *self;
cairo_status_t status;
self = _cairo_malloc (sizeof(struct _cairo_mono_scan_converter));
if (unlikely (self == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto bail_nomem;
}
self->base.destroy = _cairo_mono_scan_converter_destroy;
self->base.generate = _cairo_mono_scan_converter_generate;
status = _mono_scan_converter_init (self->converter,
xmin, ymin, xmax, ymax);
if (unlikely (status))
goto bail;
self->fill_rule = fill_rule;
return &self->base;
bail:
self->base.destroy(&self->base);
bail_nomem:
return _cairo_scan_converter_create_in_error (status);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mutex-impl-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005,2007 Red Hat, Inc.
* Copyright © 2007 Mathias Hasselmann
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Mathias Hasselmann <mathias.hasselmann@gmx.de>
* Behdad Esfahbod <behdad@behdad.org>
*/
#ifndef CAIRO_MUTEX_IMPL_PRIVATE_H
#define CAIRO_MUTEX_IMPL_PRIVATE_H
#include "cairo.h"
#if HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_LOCKDEP
#include <lockdep.h>
#endif
/* A fully qualified no-operation statement */
#define CAIRO_MUTEX_IMPL_NOOP do {/*no-op*/} while (0)
/* And one that evaluates its argument once */
#define CAIRO_MUTEX_IMPL_NOOP1(expr) do { (void)(expr); } while (0)
/* Note: 'if (expr) {}' is an alternative to '(void)(expr);' that will 'use' the
* result of __attribute__((warn_used_result)) functions. */
/* Cairo mutex implementation:
*
* Any new mutex implementation needs to do the following:
*
* - Condition on the right header or feature. Headers are
* preferred as eg. you still can use win32 mutex implementation
* on a win32 system even if you do not compile the win32
* surface/backend.
*
* - typedef #cairo_mutex_impl_t to the proper mutex type on your target
* system. Note that you may or may not need to use a pointer,
* depending on what kinds of initialization your mutex
* implementation supports. No trailing semicolon needed.
* You should be able to compile the following snippet (don't try
* running it):
*
* <programlisting>
* cairo_mutex_impl_t _cairo_some_mutex;
* </programlisting>
*
* - #define %CAIRO_MUTEX_IMPL_<NAME> 1 with suitable name for your platform. You
* can later use this symbol in cairo-system.c.
*
* - #define CAIRO_MUTEX_IMPL_LOCK(mutex) and CAIRO_MUTEX_IMPL_UNLOCK(mutex) to
* proper statement to lock/unlock the mutex object passed in.
* You can (and should) assume that the mutex is already
* initialized, and is-not-already-locked/is-locked,
* respectively. Use the "do { ... } while (0)" idiom if necessary.
* No trailing semicolons are needed (in any macro you define here).
* You should be able to compile the following snippet:
*
* <programlisting>
* cairo_mutex_impl_t _cairo_some_mutex;
*
* if (1)
* CAIRO_MUTEX_IMPL_LOCK (_cairo_some_mutex);
* else
* CAIRO_MUTEX_IMPL_UNLOCK (_cairo_some_mutex);
* </programlisting>
*
* - #define %CAIRO_MUTEX_IMPL_NIL_INITIALIZER to something that can
* initialize the #cairo_mutex_impl_t type you defined. Most of the
* time one of 0, %NULL, or {} works. At this point
* you should be able to compile the following snippet:
*
* <programlisting>
* cairo_mutex_impl_t _cairo_some_mutex = CAIRO_MUTEX_IMPL_NIL_INITIALIZER;
*
* if (1)
* CAIRO_MUTEX_IMPL_LOCK (_cairo_some_mutex);
* else
* CAIRO_MUTEX_IMPL_UNLOCK (_cairo_some_mutex);
* </programlisting>
*
* - If the above code is not enough to initialize a mutex on
* your platform, #define CAIRO_MUTEX_IMPL_INIT(mutex) to statement
* to initialize the mutex (allocate resources, etc). Such that
* you should be able to compile AND RUN the following snippet:
*
* <programlisting>
* cairo_mutex_impl_t _cairo_some_mutex = CAIRO_MUTEX_IMPL_NIL_INITIALIZER;
*
* CAIRO_MUTEX_IMPL_INIT (_cairo_some_mutex);
*
* if (1)
* CAIRO_MUTEX_IMPL_LOCK (_cairo_some_mutex);
* else
* CAIRO_MUTEX_IMPL_UNLOCK (_cairo_some_mutex);
* </programlisting>
*
* - If you define CAIRO_MUTEX_IMPL_INIT(mutex), cairo will use it to
* initialize all static mutex'es. If for any reason that should
* not happen (eg. %CAIRO_MUTEX_IMPL_INIT is just a faster way than
* what cairo does using %CAIRO_MUTEX_IMPL_NIL_INITIALIZER), then
* <programlisting>
* #define CAIRO_MUTEX_IMPL_INITIALIZE() CAIRO_MUTEX_IMPL_NOOP
* </programlisting>
*
* - If your system supports freeing a mutex object (deallocating
* resources, etc), then #define CAIRO_MUTEX_IMPL_FINI(mutex) to do
* that.
*
* - If you define CAIRO_MUTEX_IMPL_FINI(mutex), cairo will use it to
* define a finalizer function to finalize all static mutex'es.
* However, it's up to you to call CAIRO_MUTEX_IMPL_FINALIZE() at
* proper places, eg. when the system is unloading the cairo library.
* So, if for any reason finalizing static mutex'es is not needed
* (eg. you never call CAIRO_MUTEX_IMPL_FINALIZE()), then
* <programlisting>
* #define CAIRO_MUTEX_IMPL_FINALIZE() CAIRO_MUTEX_IMPL_NOOP
* </programlisting>
*
* - That is all. If for any reason you think the above API is
* not enough to implement #cairo_mutex_impl_t on your system, please
* stop and write to the cairo mailing list about it. DO NOT
* poke around cairo-mutex-private.h for possible solutions.
*/
#if CAIRO_NO_MUTEX
/* No mutexes */
typedef int cairo_mutex_impl_t;
# define CAIRO_MUTEX_IMPL_NO 1
# define CAIRO_MUTEX_IMPL_INITIALIZE() CAIRO_MUTEX_IMPL_NOOP
# define CAIRO_MUTEX_IMPL_LOCK(mutex) CAIRO_MUTEX_IMPL_NOOP1(mutex)
# define CAIRO_MUTEX_IMPL_UNLOCK(mutex) CAIRO_MUTEX_IMPL_NOOP1(mutex)
# define CAIRO_MUTEX_IMPL_NIL_INITIALIZER 0
# define CAIRO_MUTEX_HAS_RECURSIVE_IMPL 1
typedef int cairo_recursive_mutex_impl_t;
# define CAIRO_RECURSIVE_MUTEX_IMPL_INIT(mutex)
# define CAIRO_RECURSIVE_MUTEX_IMPL_NIL_INITIALIZER 0
#elif defined(_WIN32) /******************************************************/
#define WIN32_LEAN_AND_MEAN
/* We require Windows 2000 features such as ETO_PDY */
#if !defined(WINVER) || (WINVER < 0x0500)
# define WINVER 0x0500
#endif
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
# define _WIN32_WINNT 0x0500
#endif
# include <windows.h>
typedef CRITICAL_SECTION cairo_mutex_impl_t;
# define CAIRO_MUTEX_IMPL_WIN32 1
# define CAIRO_MUTEX_IMPL_LOCK(mutex) EnterCriticalSection (&(mutex))
# define CAIRO_MUTEX_IMPL_UNLOCK(mutex) LeaveCriticalSection (&(mutex))
# define CAIRO_MUTEX_IMPL_INIT(mutex) InitializeCriticalSection (&(mutex))
# define CAIRO_MUTEX_IMPL_FINI(mutex) DeleteCriticalSection (&(mutex))
# define CAIRO_MUTEX_IMPL_NIL_INITIALIZER { NULL, 0, 0, NULL, NULL, 0 }
#elif defined __OS2__ /******************************************************/
# define INCL_BASE
# define INCL_PM
# include <os2.h>
typedef HMTX cairo_mutex_impl_t;
# define CAIRO_MUTEX_IMPL_OS2 1
# define CAIRO_MUTEX_IMPL_LOCK(mutex) DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT)
# define CAIRO_MUTEX_IMPL_UNLOCK(mutex) DosReleaseMutexSem(mutex)
# define CAIRO_MUTEX_IMPL_INIT(mutex) DosCreateMutexSem (NULL, &(mutex), 0L, FALSE)
# define CAIRO_MUTEX_IMPL_FINI(mutex) DosCloseMutexSem (mutex)
# define CAIRO_MUTEX_IMPL_NIL_INITIALIZER 0
#elif CAIRO_HAS_BEOS_SURFACE /***********************************************/
typedef BLocker* cairo_mutex_impl_t;
# define CAIRO_MUTEX_IMPL_BEOS 1
# define CAIRO_MUTEX_IMPL_LOCK(mutex) (mutex)->Lock()
# define CAIRO_MUTEX_IMPL_UNLOCK(mutex) (mutex)->Unlock()
# define CAIRO_MUTEX_IMPL_INIT(mutex) (mutex) = new BLocker()
# define CAIRO_MUTEX_IMPL_FINI(mutex) delete (mutex)
# define CAIRO_MUTEX_IMPL_NIL_INITIALIZER NULL
#elif CAIRO_HAS_PTHREAD /* and finally if there are no native mutexes ********/
# include <pthread.h>
typedef pthread_mutex_t cairo_mutex_impl_t;
typedef pthread_mutex_t cairo_recursive_mutex_impl_t;
# define CAIRO_MUTEX_IMPL_PTHREAD 1
#if HAVE_LOCKDEP
/* expose all mutexes to the validator */
# define CAIRO_MUTEX_IMPL_INIT(mutex) pthread_mutex_init (&(mutex), NULL)
#endif
# define CAIRO_MUTEX_IMPL_LOCK(mutex) pthread_mutex_lock (&(mutex))
# define CAIRO_MUTEX_IMPL_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
#if HAVE_LOCKDEP
# define CAIRO_MUTEX_IS_LOCKED(mutex) LOCKDEP_IS_LOCKED (&(mutex))
# define CAIRO_MUTEX_IS_UNLOCKED(mutex) LOCKDEP_IS_UNLOCKED (&(mutex))
#endif
# define CAIRO_MUTEX_IMPL_FINI(mutex) pthread_mutex_destroy (&(mutex))
#if ! HAVE_LOCKDEP
# define CAIRO_MUTEX_IMPL_FINALIZE() CAIRO_MUTEX_IMPL_NOOP
#endif
# define CAIRO_MUTEX_IMPL_NIL_INITIALIZER PTHREAD_MUTEX_INITIALIZER
# define CAIRO_MUTEX_HAS_RECURSIVE_IMPL 1
# define CAIRO_RECURSIVE_MUTEX_IMPL_INIT(mutex) do { \
pthread_mutexattr_t attr; \
pthread_mutexattr_init (&attr); \
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); \
pthread_mutex_init (&(mutex), &attr); \
pthread_mutexattr_destroy (&attr); \
} while (0)
# define CAIRO_RECURSIVE_MUTEX_IMPL_NIL_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
#else /**********************************************************************/
# error "XXX: No mutex implementation found. Cairo will not work with multiple threads. Define CAIRO_NO_MUTEX to 1 to acknowledge and accept this limitation and compile cairo without thread-safety support."
#endif
/* By default mutex implementations are assumed to be recursive */
#if ! CAIRO_MUTEX_HAS_RECURSIVE_IMPL
# define CAIRO_MUTEX_HAS_RECURSIVE_IMPL 1
typedef cairo_mutex_impl_t cairo_recursive_mutex_impl_t;
# define CAIRO_RECURSIVE_MUTEX_IMPL_INIT(mutex) CAIRO_MUTEX_IMPL_INIT(mutex)
# define CAIRO_RECURSIVE_MUTEX_IMPL_NIL_INITIALIZER CAIRO_MUTEX_IMPL_NIL_INITIALIZER
#endif
#endif
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mutex-list-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2007 Mathias Hasselmann
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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.
*
* Contributor(s):
* Mathias Hasselmann <mathias.hasselmann@gmx.de>
*/
#ifndef CAIRO_FEATURES_H
/* This block is to just make this header file standalone */
#define CAIRO_MUTEX_DECLARE(mutex)
#endif
CAIRO_MUTEX_DECLARE (_cairo_pattern_solid_surface_cache_lock)
CAIRO_MUTEX_DECLARE (_cairo_image_solid_cache_mutex)
CAIRO_MUTEX_DECLARE (_cairo_toy_font_face_mutex)
CAIRO_MUTEX_DECLARE (_cairo_intern_string_mutex)
CAIRO_MUTEX_DECLARE (_cairo_scaled_font_map_mutex)
CAIRO_MUTEX_DECLARE (_cairo_scaled_glyph_page_cache_mutex)
CAIRO_MUTEX_DECLARE (_cairo_scaled_font_error_mutex)
CAIRO_MUTEX_DECLARE (_cairo_glyph_cache_mutex)
#if CAIRO_HAS_FT_FONT
CAIRO_MUTEX_DECLARE (_cairo_ft_unscaled_font_map_mutex)
#endif
#if CAIRO_HAS_WIN32_FONT
CAIRO_MUTEX_DECLARE (_cairo_win32_font_face_mutex)
#endif
#if CAIRO_HAS_XLIB_SURFACE
CAIRO_MUTEX_DECLARE (_cairo_xlib_display_mutex)
#endif
#if CAIRO_HAS_XCB_SURFACE
CAIRO_MUTEX_DECLARE (_cairo_xcb_connections_mutex)
#endif
#if CAIRO_HAS_GL_SURFACE
CAIRO_MUTEX_DECLARE (_cairo_gl_context_mutex)
#endif
#if !defined (HAS_ATOMIC_OPS) || defined (ATOMIC_OP_NEEDS_MEMORY_BARRIER)
CAIRO_MUTEX_DECLARE (_cairo_atomic_mutex)
#endif
#if CAIRO_HAS_DRM_SURFACE
CAIRO_MUTEX_DECLARE (_cairo_drm_device_mutex)
#endif
/* Undefine, to err on unintended inclusion */
#undef CAIRO_MUTEX_DECLARE
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mutex-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005,2007 Red Hat, Inc.
* Copyright © 2007 Mathias Hasselmann
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Mathias Hasselmann <mathias.hasselmann@gmx.de>
* Behdad Esfahbod <behdad@behdad.org>
*/
#ifndef CAIRO_MUTEX_PRIVATE_H
#define CAIRO_MUTEX_PRIVATE_H
#include "cairo-mutex-type-private.h"
CAIRO_BEGIN_DECLS
#if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER
cairo_private void _cairo_mutex_initialize (void);
#endif
#if _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
cairo_private void _cairo_mutex_finalize (void);
#endif
/* only if using static initializer and/or finalizer define the boolean */
#if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER || _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
cairo_private extern cairo_bool_t _cairo_mutex_initialized;
#endif
/* Finally, extern the static mutexes and undef */
#define CAIRO_MUTEX_DECLARE(mutex) cairo_private extern cairo_mutex_t mutex;
#include "cairo-mutex-list-private.h"
#undef CAIRO_MUTEX_DECLARE
CAIRO_END_DECLS
#endif
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mutex-type-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005,2007 Red Hat, Inc.
* Copyright © 2007 Mathias Hasselmann
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Mathias Hasselmann <mathias.hasselmann@gmx.de>
* Behdad Esfahbod <behdad@behdad.org>
*/
#ifndef CAIRO_MUTEX_TYPE_PRIVATE_H
#define CAIRO_MUTEX_TYPE_PRIVATE_H
#include "cairo-compiler-private.h"
#include "cairo-mutex-impl-private.h"
/* Only the following four are mandatory at this point */
#ifndef CAIRO_MUTEX_IMPL_LOCK
# error "CAIRO_MUTEX_IMPL_LOCK not defined. Check cairo-mutex-impl-private.h."
#endif
#ifndef CAIRO_MUTEX_IMPL_UNLOCK
# error "CAIRO_MUTEX_IMPL_UNLOCK not defined. Check cairo-mutex-impl-private.h."
#endif
#ifndef CAIRO_MUTEX_IMPL_NIL_INITIALIZER
# error "CAIRO_MUTEX_IMPL_NIL_INITIALIZER not defined. Check cairo-mutex-impl-private.h."
#endif
#ifndef CAIRO_RECURSIVE_MUTEX_IMPL_INIT
# error "CAIRO_RECURSIVE_MUTEX_IMPL_INIT not defined. Check cairo-mutex-impl-private.h."
#endif
/* make sure implementations don't fool us: we decide these ourself */
#undef _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER
#undef _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
#ifdef CAIRO_MUTEX_IMPL_INIT
/* If %CAIRO_MUTEX_IMPL_INIT is defined, we may need to initialize all
* static mutex'es. */
# ifndef CAIRO_MUTEX_IMPL_INITIALIZE
# define CAIRO_MUTEX_IMPL_INITIALIZE() do { \
if (!_cairo_mutex_initialized) \
_cairo_mutex_initialize (); \
} while(0)
/* and make sure we implement the above */
# define _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER 1
# endif /* CAIRO_MUTEX_IMPL_INITIALIZE */
#else /* no CAIRO_MUTEX_IMPL_INIT */
/* Otherwise we probably don't need to initialize static mutex'es, */
# ifndef CAIRO_MUTEX_IMPL_INITIALIZE
# define CAIRO_MUTEX_IMPL_INITIALIZE() CAIRO_MUTEX_IMPL_NOOP
# endif /* CAIRO_MUTEX_IMPL_INITIALIZE */
/* and dynamic ones can be initialized using the static initializer. */
# define CAIRO_MUTEX_IMPL_INIT(mutex) do { \
cairo_mutex_t _tmp_mutex = CAIRO_MUTEX_IMPL_NIL_INITIALIZER; \
memcpy (&(mutex), &_tmp_mutex, sizeof (_tmp_mutex)); \
} while (0)
#endif /* CAIRO_MUTEX_IMPL_INIT */
#ifdef CAIRO_MUTEX_IMPL_FINI
/* If %CAIRO_MUTEX_IMPL_FINI is defined, we may need to finalize all
* static mutex'es. */
# ifndef CAIRO_MUTEX_IMPL_FINALIZE
# define CAIRO_MUTEX_IMPL_FINALIZE() do { \
if (_cairo_mutex_initialized) \
_cairo_mutex_finalize (); \
} while(0)
/* and make sure we implement the above */
# define _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER 1
# endif /* CAIRO_MUTEX_IMPL_FINALIZE */
#else /* no CAIRO_MUTEX_IMPL_FINI */
/* Otherwise we probably don't need to finalize static mutex'es, */
# ifndef CAIRO_MUTEX_IMPL_FINALIZE
# define CAIRO_MUTEX_IMPL_FINALIZE() CAIRO_MUTEX_IMPL_NOOP
# endif /* CAIRO_MUTEX_IMPL_FINALIZE */
/* neither do the dynamic ones. */
# define CAIRO_MUTEX_IMPL_FINI(mutex) CAIRO_MUTEX_IMPL_NOOP1(mutex)
#endif /* CAIRO_MUTEX_IMPL_FINI */
#ifndef _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER
#define _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER 0
#endif
#ifndef _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
#define _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER 0
#endif
/* Make sure everything we want is defined */
#ifndef CAIRO_MUTEX_IMPL_INITIALIZE
# error "CAIRO_MUTEX_IMPL_INITIALIZE not defined"
#endif
#ifndef CAIRO_MUTEX_IMPL_FINALIZE
# error "CAIRO_MUTEX_IMPL_FINALIZE not defined"
#endif
#ifndef CAIRO_MUTEX_IMPL_LOCK
# error "CAIRO_MUTEX_IMPL_LOCK not defined"
#endif
#ifndef CAIRO_MUTEX_IMPL_UNLOCK
# error "CAIRO_MUTEX_IMPL_UNLOCK not defined"
#endif
#ifndef CAIRO_MUTEX_IMPL_INIT
# error "CAIRO_MUTEX_IMPL_INIT not defined"
#endif
#ifndef CAIRO_MUTEX_IMPL_FINI
# error "CAIRO_MUTEX_IMPL_FINI not defined"
#endif
#ifndef CAIRO_MUTEX_IMPL_NIL_INITIALIZER
# error "CAIRO_MUTEX_IMPL_NIL_INITIALIZER not defined"
#endif
/* Public interface. */
/* By default it simply uses the implementation provided.
* But we can provide for debugging features by overriding them */
#ifndef CAIRO_MUTEX_DEBUG
typedef cairo_mutex_impl_t cairo_mutex_t;
typedef cairo_recursive_mutex_impl_t cairo_recursive_mutex_t;
#else
# define cairo_mutex_t cairo_mutex_impl_t
#endif
#define CAIRO_MUTEX_INITIALIZE CAIRO_MUTEX_IMPL_INITIALIZE
#define CAIRO_MUTEX_FINALIZE CAIRO_MUTEX_IMPL_FINALIZE
#define CAIRO_MUTEX_LOCK CAIRO_MUTEX_IMPL_LOCK
#define CAIRO_MUTEX_UNLOCK CAIRO_MUTEX_IMPL_UNLOCK
#define CAIRO_MUTEX_INIT CAIRO_MUTEX_IMPL_INIT
#define CAIRO_MUTEX_FINI CAIRO_MUTEX_IMPL_FINI
#define CAIRO_MUTEX_NIL_INITIALIZER CAIRO_MUTEX_IMPL_NIL_INITIALIZER
#define CAIRO_RECURSIVE_MUTEX_INIT CAIRO_RECURSIVE_MUTEX_IMPL_INIT
#define CAIRO_RECURSIVE_MUTEX_NIL_INITIALIZER CAIRO_RECURSIVE_MUTEX_IMPL_NIL_INITIALIZER
#ifndef CAIRO_MUTEX_IS_LOCKED
# define CAIRO_MUTEX_IS_LOCKED(name) 1
#endif
#ifndef CAIRO_MUTEX_IS_UNLOCKED
# define CAIRO_MUTEX_IS_UNLOCKED(name) 1
#endif
/* Debugging support */
#ifdef CAIRO_MUTEX_DEBUG
/* TODO add mutex debugging facilities here (eg deadlock detection) */
#endif /* CAIRO_MUTEX_DEBUG */
#endif
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-mutex.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2007 Mathias Hasselmann
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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.
*
* Contributor(s):
* Mathias Hasselmann <mathias.hasselmann@gmx.de>
*/
#include "cairoint.h"
#include "cairo-mutex-private.h"
#define CAIRO_MUTEX_DECLARE(mutex) cairo_mutex_t mutex = CAIRO_MUTEX_NIL_INITIALIZER;
#include "cairo-mutex-list-private.h"
#undef CAIRO_MUTEX_DECLARE
#if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER || _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
# if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER
# define _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE FALSE
# else
# define _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE TRUE
# endif
cairo_bool_t _cairo_mutex_initialized = _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE;
# undef _CAIRO_MUTEX_IMPL_INITIALIZED_DEFAULT_VALUE
#endif
#if _CAIRO_MUTEX_IMPL_USE_STATIC_INITIALIZER
void _cairo_mutex_initialize (void)
{
if (_cairo_mutex_initialized)
return;
_cairo_mutex_initialized = TRUE;
#define CAIRO_MUTEX_DECLARE(mutex) CAIRO_MUTEX_INIT (mutex);
#include "cairo-mutex-list-private.h"
#undef CAIRO_MUTEX_DECLARE
}
#endif
#if _CAIRO_MUTEX_IMPL_USE_STATIC_FINALIZER
void _cairo_mutex_finalize (void)
{
if (!_cairo_mutex_initialized)
return;
_cairo_mutex_initialized = FALSE;
#define CAIRO_MUTEX_DECLARE(mutex) CAIRO_MUTEX_FINI (mutex);
#include "cairo-mutex-list-private.h"
#undef CAIRO_MUTEX_DECLARE
}
#endif
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-no-compositor.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-compositor-private.h"
static cairo_int_status_t
_cairo_no_compositor_paint (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents)
{
ASSERT_NOT_REACHED;
return CAIRO_INT_STATUS_NOTHING_TO_DO;
}
static cairo_int_status_t
_cairo_no_compositor_mask (const cairo_compositor_t *compositor,
cairo_composite_rectangles_t *extents)
{
ASSERT_NOT_REACHED;
return CAIRO_INT_STATUS_NOTHING_TO_DO;
}
static cairo_int_status_t
_cairo_no_compositor_stroke (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_antialias_t antialias)
{
ASSERT_NOT_REACHED;
return CAIRO_INT_STATUS_NOTHING_TO_DO;
}
static cairo_int_status_t
_cairo_no_compositor_fill (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias)
{
ASSERT_NOT_REACHED;
return CAIRO_INT_STATUS_NOTHING_TO_DO;
}
static cairo_int_status_t
_cairo_no_compositor_glyphs (const cairo_compositor_t *compositor,
cairo_composite_rectangles_t *extents,
cairo_scaled_font_t *scaled_font,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_bool_t overlap)
{
ASSERT_NOT_REACHED;
return CAIRO_INT_STATUS_NOTHING_TO_DO;
}
const cairo_compositor_t __cairo_no_compositor = {
NULL,
_cairo_no_compositor_paint,
_cairo_no_compositor_mask,
_cairo_no_compositor_stroke,
_cairo_no_compositor_fill,
_cairo_no_compositor_glyphs,
};
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-observer.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2010 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Intel Corporation
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-list-inline.h"
void
_cairo_observers_notify (cairo_list_t *observers, void *arg)
{
cairo_observer_t *obs, *next;
cairo_list_foreach_entry_safe (obs, next,
cairo_observer_t,
observers, link)
{
obs->callback (obs, arg);
}
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-output-stream-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2006 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Author(s):
* Kristian Høgsberg <krh@redhat.com>
*/
#ifndef CAIRO_OUTPUT_STREAM_PRIVATE_H
#define CAIRO_OUTPUT_STREAM_PRIVATE_H
#include "cairo-compiler-private.h"
#include "cairo-types-private.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
typedef cairo_status_t
(*cairo_output_stream_write_func_t) (cairo_output_stream_t *output_stream,
const unsigned char *data,
unsigned int length);
typedef cairo_status_t
(*cairo_output_stream_flush_func_t) (cairo_output_stream_t *output_stream);
typedef cairo_status_t
(*cairo_output_stream_close_func_t) (cairo_output_stream_t *output_stream);
struct _cairo_output_stream {
cairo_output_stream_write_func_t write_func;
cairo_output_stream_flush_func_t flush_func;
cairo_output_stream_close_func_t close_func;
unsigned long position;
cairo_status_t status;
cairo_bool_t closed;
};
extern const cairo_private cairo_output_stream_t _cairo_output_stream_nil;
cairo_private void
_cairo_output_stream_init (cairo_output_stream_t *stream,
cairo_output_stream_write_func_t write_func,
cairo_output_stream_flush_func_t flush_func,
cairo_output_stream_close_func_t close_func);
cairo_private cairo_status_t
_cairo_output_stream_fini (cairo_output_stream_t *stream);
/* We already have the following declared in cairo.h:
typedef cairo_status_t (*cairo_write_func_t) (void *closure,
const unsigned char *data,
unsigned int length);
*/
typedef cairo_status_t (*cairo_close_func_t) (void *closure);
/* This function never returns %NULL. If an error occurs (NO_MEMORY)
* while trying to create the output stream this function returns a
* valid pointer to a nil output stream.
*
* Note that even with a nil surface, the close_func callback will be
* called by a call to _cairo_output_stream_close or
* _cairo_output_stream_destroy.
*/
cairo_private cairo_output_stream_t *
_cairo_output_stream_create (cairo_write_func_t write_func,
cairo_close_func_t close_func,
void *closure);
cairo_private cairo_output_stream_t *
_cairo_output_stream_create_in_error (cairo_status_t status);
/* Tries to flush any buffer maintained by the stream or its delegates. */
cairo_private cairo_status_t
_cairo_output_stream_flush (cairo_output_stream_t *stream);
/* Returns the final status value associated with this object, just
* before its last gasp. This final status value will capture any
* status failure returned by the stream's close_func as well. */
cairo_private cairo_status_t
_cairo_output_stream_close (cairo_output_stream_t *stream);
/* Returns the final status value associated with this object, just
* before its last gasp. This final status value will capture any
* status failure returned by the stream's close_func as well. */
cairo_private cairo_status_t
_cairo_output_stream_destroy (cairo_output_stream_t *stream);
cairo_private void
_cairo_output_stream_write (cairo_output_stream_t *stream,
const void *data, size_t length);
cairo_private void
_cairo_output_stream_write_hex_string (cairo_output_stream_t *stream,
const unsigned char *data,
size_t length);
cairo_private void
_cairo_output_stream_vprintf (cairo_output_stream_t *stream,
const char *fmt,
va_list ap) CAIRO_PRINTF_FORMAT ( 2, 0);
cairo_private void
_cairo_output_stream_printf (cairo_output_stream_t *stream,
const char *fmt,
...) CAIRO_PRINTF_FORMAT (2, 3);
/* Print matrix element values with rounding of insignificant digits. */
cairo_private void
_cairo_output_stream_print_matrix (cairo_output_stream_t *stream,
const cairo_matrix_t *matrix);
cairo_private long
_cairo_output_stream_get_position (cairo_output_stream_t *stream);
cairo_private cairo_status_t
_cairo_output_stream_get_status (cairo_output_stream_t *stream);
/* This function never returns %NULL. If an error occurs (NO_MEMORY or
* WRITE_ERROR) while trying to create the output stream this function
* returns a valid pointer to a nil output stream.
*
* Note: Even if a nil surface is returned, the caller should still
* call _cairo_output_stream_destroy (or _cairo_output_stream_close at
* least) in order to ensure that everything is properly cleaned up.
*/
cairo_private cairo_output_stream_t *
_cairo_output_stream_create_for_filename (const char *filename);
/* This function never returns %NULL. If an error occurs (NO_MEMORY or
* WRITE_ERROR) while trying to create the output stream this function
* returns a valid pointer to a nil output stream.
*
* The caller still "owns" file and is responsible for calling fclose
* on it when finished. The stream will not do this itself.
*/
cairo_private cairo_output_stream_t *
_cairo_output_stream_create_for_file (FILE *file);
cairo_private cairo_output_stream_t *
_cairo_memory_stream_create (void);
cairo_private void
_cairo_memory_stream_copy (cairo_output_stream_t *base,
cairo_output_stream_t *dest);
cairo_private int
_cairo_memory_stream_length (cairo_output_stream_t *stream);
cairo_private cairo_status_t
_cairo_memory_stream_destroy (cairo_output_stream_t *abstract_stream,
unsigned char **data_out,
unsigned long *length_out);
cairo_private cairo_output_stream_t *
_cairo_null_stream_create (void);
/* cairo-base85-stream.c */
cairo_private cairo_output_stream_t *
_cairo_base85_stream_create (cairo_output_stream_t *output);
/* cairo-base64-stream.c */
cairo_private cairo_output_stream_t *
_cairo_base64_stream_create (cairo_output_stream_t *output);
/* cairo-deflate-stream.c */
cairo_private cairo_output_stream_t *
_cairo_deflate_stream_create (cairo_output_stream_t *output);
#endif /* CAIRO_OUTPUT_STREAM_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-output-stream.c | /* cairo-output-stream.c: Output stream abstraction
*
* Copyright © 2005 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Author(s):
* Kristian Høgsberg <krh@redhat.com>
*/
#define _DEFAULT_SOURCE /* for snprintf() */
#include "cairoint.h"
#include "cairo-output-stream-private.h"
#include "cairo-array-private.h"
#include "cairo-error-private.h"
#include "cairo-compiler-private.h"
#include <stdio.h>
#include <errno.h>
/* Numbers printed with %f are printed with this number of significant
* digits after the decimal.
*/
#define SIGNIFICANT_DIGITS_AFTER_DECIMAL 6
/* Numbers printed with %g are assumed to only have %CAIRO_FIXED_FRAC_BITS
* bits of precision available after the decimal point.
*
* FIXED_POINT_DECIMAL_DIGITS specifies the minimum number of decimal
* digits after the decimal point required to preserve the available
* precision.
*
* The conversion is:
*
* <programlisting>
* FIXED_POINT_DECIMAL_DIGITS = ceil( CAIRO_FIXED_FRAC_BITS * ln(2)/ln(10) )
* </programlisting>
*
* We can replace ceil(x) with (int)(x+1) since x will never be an
* integer for any likely value of %CAIRO_FIXED_FRAC_BITS.
*/
#define FIXED_POINT_DECIMAL_DIGITS ((int)(CAIRO_FIXED_FRAC_BITS*0.301029996 + 1))
void
_cairo_output_stream_init (cairo_output_stream_t *stream,
cairo_output_stream_write_func_t write_func,
cairo_output_stream_flush_func_t flush_func,
cairo_output_stream_close_func_t close_func)
{
stream->write_func = write_func;
stream->flush_func = flush_func;
stream->close_func = close_func;
stream->position = 0;
stream->status = CAIRO_STATUS_SUCCESS;
stream->closed = FALSE;
}
cairo_status_t
_cairo_output_stream_fini (cairo_output_stream_t *stream)
{
return _cairo_output_stream_close (stream);
}
const cairo_output_stream_t _cairo_output_stream_nil = {
NULL, /* write_func */
NULL, /* flush_func */
NULL, /* close_func */
0, /* position */
CAIRO_STATUS_NO_MEMORY,
FALSE /* closed */
};
static const cairo_output_stream_t _cairo_output_stream_nil_write_error = {
NULL, /* write_func */
NULL, /* flush_func */
NULL, /* close_func */
0, /* position */
CAIRO_STATUS_WRITE_ERROR,
FALSE /* closed */
};
typedef struct _cairo_output_stream_with_closure {
cairo_output_stream_t base;
cairo_write_func_t write_func;
cairo_close_func_t close_func;
void *closure;
} cairo_output_stream_with_closure_t;
static cairo_status_t
closure_write (cairo_output_stream_t *stream,
const unsigned char *data, unsigned int length)
{
cairo_output_stream_with_closure_t *stream_with_closure =
(cairo_output_stream_with_closure_t *) stream;
if (stream_with_closure->write_func == NULL)
return CAIRO_STATUS_SUCCESS;
return stream_with_closure->write_func (stream_with_closure->closure,
data, length);
}
static cairo_status_t
closure_close (cairo_output_stream_t *stream)
{
cairo_output_stream_with_closure_t *stream_with_closure =
(cairo_output_stream_with_closure_t *) stream;
if (stream_with_closure->close_func != NULL)
return stream_with_closure->close_func (stream_with_closure->closure);
else
return CAIRO_STATUS_SUCCESS;
}
cairo_output_stream_t *
_cairo_output_stream_create (cairo_write_func_t write_func,
cairo_close_func_t close_func,
void *closure)
{
cairo_output_stream_with_closure_t *stream;
stream = _cairo_malloc (sizeof (cairo_output_stream_with_closure_t));
if (unlikely (stream == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_output_stream_t *) &_cairo_output_stream_nil;
}
_cairo_output_stream_init (&stream->base,
closure_write, NULL, closure_close);
stream->write_func = write_func;
stream->close_func = close_func;
stream->closure = closure;
return &stream->base;
}
cairo_output_stream_t *
_cairo_output_stream_create_in_error (cairo_status_t status)
{
cairo_output_stream_t *stream;
/* check for the common ones */
if (status == CAIRO_STATUS_NO_MEMORY)
return (cairo_output_stream_t *) &_cairo_output_stream_nil;
if (status == CAIRO_STATUS_WRITE_ERROR)
return (cairo_output_stream_t *) &_cairo_output_stream_nil_write_error;
stream = _cairo_malloc (sizeof (cairo_output_stream_t));
if (unlikely (stream == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_output_stream_t *) &_cairo_output_stream_nil;
}
_cairo_output_stream_init (stream, NULL, NULL, NULL);
stream->status = status;
return stream;
}
cairo_status_t
_cairo_output_stream_flush (cairo_output_stream_t *stream)
{
cairo_status_t status;
if (stream->closed)
return stream->status;
if (stream == &_cairo_output_stream_nil ||
stream == &_cairo_output_stream_nil_write_error)
{
return stream->status;
}
if (stream->flush_func) {
status = stream->flush_func (stream);
/* Don't overwrite a pre-existing status failure. */
if (stream->status == CAIRO_STATUS_SUCCESS)
stream->status = status;
}
return stream->status;
}
cairo_status_t
_cairo_output_stream_close (cairo_output_stream_t *stream)
{
cairo_status_t status;
if (stream->closed)
return stream->status;
if (stream == &_cairo_output_stream_nil ||
stream == &_cairo_output_stream_nil_write_error)
{
return stream->status;
}
if (stream->close_func) {
status = stream->close_func (stream);
/* Don't overwrite a pre-existing status failure. */
if (stream->status == CAIRO_STATUS_SUCCESS)
stream->status = status;
}
stream->closed = TRUE;
return stream->status;
}
cairo_status_t
_cairo_output_stream_destroy (cairo_output_stream_t *stream)
{
cairo_status_t status;
assert (stream != NULL);
if (stream == &_cairo_output_stream_nil ||
stream == &_cairo_output_stream_nil_write_error)
{
return stream->status;
}
status = _cairo_output_stream_fini (stream);
free (stream);
return status;
}
void
_cairo_output_stream_write (cairo_output_stream_t *stream,
const void *data, size_t length)
{
if (length == 0)
return;
if (stream->status)
return;
stream->status = stream->write_func (stream, data, length);
stream->position += length;
}
void
_cairo_output_stream_write_hex_string (cairo_output_stream_t *stream,
const unsigned char *data,
size_t length)
{
const char hex_chars[] = "0123456789abcdef";
char buffer[2];
unsigned int i, column;
if (stream->status)
return;
for (i = 0, column = 0; i < length; i++, column++) {
if (column == 38) {
_cairo_output_stream_write (stream, "\n", 1);
column = 0;
}
buffer[0] = hex_chars[(data[i] >> 4) & 0x0f];
buffer[1] = hex_chars[data[i] & 0x0f];
_cairo_output_stream_write (stream, buffer, 2);
}
}
/* Format a double in a locale independent way and trim trailing
* zeros. Based on code from Alex Larson <alexl@redhat.com>.
* https://mail.gnome.org/archives/gtk-devel-list/2001-October/msg00087.html
*
* The code in the patch is copyright Red Hat, Inc under the LGPL, but
* has been relicensed under the LGPL/MPL dual license for inclusion
* into cairo (see COPYING). -- Kristian Høgsberg <krh@redhat.com>
*/
static void
_cairo_dtostr (char *buffer, size_t size, double d, cairo_bool_t limited_precision)
{
const char *decimal_point;
int decimal_point_len;
char *p;
int decimal_len;
int num_zeros, decimal_digits;
/* Omit the minus sign from negative zero. */
if (d == 0.0)
d = 0.0;
decimal_point = _cairo_get_locale_decimal_point ();
decimal_point_len = strlen (decimal_point);
assert (decimal_point_len != 0);
if (limited_precision) {
snprintf (buffer, size, "%.*f", FIXED_POINT_DECIMAL_DIGITS, d);
} else {
/* Using "%f" to print numbers less than 0.1 will result in
* reduced precision due to the default 6 digits after the
* decimal point.
*
* For numbers is < 0.1, we print with maximum precision and count
* the number of zeros between the decimal point and the first
* significant digit. We then print the number again with the
* number of decimal places that gives us the required number of
* significant digits. This ensures the number is correctly
* rounded.
*/
if (fabs (d) >= 0.1) {
snprintf (buffer, size, "%f", d);
} else {
snprintf (buffer, size, "%.18f", d);
p = buffer;
if (*p == '+' || *p == '-')
p++;
while (_cairo_isdigit (*p))
p++;
if (strncmp (p, decimal_point, decimal_point_len) == 0)
p += decimal_point_len;
num_zeros = 0;
while (*p++ == '0')
num_zeros++;
decimal_digits = num_zeros + SIGNIFICANT_DIGITS_AFTER_DECIMAL;
if (decimal_digits < 18)
snprintf (buffer, size, "%.*f", decimal_digits, d);
}
}
p = buffer;
if (*p == '+' || *p == '-')
p++;
while (_cairo_isdigit (*p))
p++;
if (strncmp (p, decimal_point, decimal_point_len) == 0) {
*p = '.';
decimal_len = strlen (p + decimal_point_len);
memmove (p + 1, p + decimal_point_len, decimal_len);
p[1 + decimal_len] = 0;
/* Remove trailing zeros and decimal point if possible. */
for (p = p + decimal_len; *p == '0'; p--)
*p = 0;
if (*p == '.') {
*p = 0;
p--;
}
}
}
enum {
LENGTH_MODIFIER_LONG = 0x100
};
/* Here's a limited reimplementation of printf. The reason for doing
* this is primarily to special case handling of doubles. We want
* locale independent formatting of doubles and we want to trim
* trailing zeros. This is handled by dtostr() above, and the code
* below handles everything else by calling snprintf() to do the
* formatting. This functionality is only for internal use and we
* only implement the formats we actually use.
*/
void
_cairo_output_stream_vprintf (cairo_output_stream_t *stream,
const char *fmt, va_list ap)
{
#define SINGLE_FMT_BUFFER_SIZE 32
char buffer[512], single_fmt[SINGLE_FMT_BUFFER_SIZE];
int single_fmt_length;
char *p;
const char *f, *start;
int length_modifier, width;
cairo_bool_t var_width;
if (stream->status)
return;
f = fmt;
p = buffer;
while (*f != '\0') {
if (p == buffer + sizeof (buffer)) {
_cairo_output_stream_write (stream, buffer, sizeof (buffer));
p = buffer;
}
if (*f != '%') {
*p++ = *f++;
continue;
}
start = f;
f++;
if (*f == '0')
f++;
var_width = FALSE;
if (*f == '*') {
var_width = TRUE;
f++;
}
while (_cairo_isdigit (*f))
f++;
length_modifier = 0;
if (*f == 'l') {
length_modifier = LENGTH_MODIFIER_LONG;
f++;
}
/* The only format strings exist in the cairo implementation
* itself. So there's an internal consistency problem if any
* of them is larger than our format buffer size. */
single_fmt_length = f - start + 1;
assert (single_fmt_length + 1 <= SINGLE_FMT_BUFFER_SIZE);
/* Reuse the format string for this conversion. */
memcpy (single_fmt, start, single_fmt_length);
single_fmt[single_fmt_length] = '\0';
/* Flush contents of buffer before snprintf()'ing into it. */
_cairo_output_stream_write (stream, buffer, p - buffer);
/* We group signed and unsigned together in this switch, the
* only thing that matters here is the size of the arguments,
* since we're just passing the data through to sprintf(). */
switch (*f | length_modifier) {
case '%':
buffer[0] = *f;
buffer[1] = 0;
break;
case 'd':
case 'u':
case 'o':
case 'x':
case 'X':
if (var_width) {
width = va_arg (ap, int);
snprintf (buffer, sizeof buffer,
single_fmt, width, va_arg (ap, int));
} else {
snprintf (buffer, sizeof buffer, single_fmt, va_arg (ap, int));
}
break;
case 'd' | LENGTH_MODIFIER_LONG:
case 'u' | LENGTH_MODIFIER_LONG:
case 'o' | LENGTH_MODIFIER_LONG:
case 'x' | LENGTH_MODIFIER_LONG:
case 'X' | LENGTH_MODIFIER_LONG:
if (var_width) {
width = va_arg (ap, int);
snprintf (buffer, sizeof buffer,
single_fmt, width, va_arg (ap, long int));
} else {
snprintf (buffer, sizeof buffer,
single_fmt, va_arg (ap, long int));
}
break;
case 's': {
/* Write out strings as they may be larger than the buffer. */
const char *s = va_arg (ap, const char *);
int len = strlen(s);
_cairo_output_stream_write (stream, s, len);
buffer[0] = 0;
}
break;
case 'f':
_cairo_dtostr (buffer, sizeof buffer, va_arg (ap, double), FALSE);
break;
case 'g':
_cairo_dtostr (buffer, sizeof buffer, va_arg (ap, double), TRUE);
break;
case 'c':
buffer[0] = va_arg (ap, int);
buffer[1] = 0;
break;
default:
ASSERT_NOT_REACHED;
}
p = buffer + strlen (buffer);
f++;
}
_cairo_output_stream_write (stream, buffer, p - buffer);
}
void
_cairo_output_stream_printf (cairo_output_stream_t *stream,
const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
_cairo_output_stream_vprintf (stream, fmt, ap);
va_end (ap);
}
/* Matrix elements that are smaller than the value of the largest element * MATRIX_ROUNDING_TOLERANCE
* are rounded down to zero. */
#define MATRIX_ROUNDING_TOLERANCE 1e-12
void
_cairo_output_stream_print_matrix (cairo_output_stream_t *stream,
const cairo_matrix_t *matrix)
{
cairo_matrix_t m;
double s, e;
m = *matrix;
s = fabs (m.xx);
if (fabs (m.xy) > s)
s = fabs (m.xy);
if (fabs (m.yx) > s)
s = fabs (m.yx);
if (fabs (m.yy) > s)
s = fabs (m.yy);
e = s * MATRIX_ROUNDING_TOLERANCE;
if (fabs(m.xx) < e)
m.xx = 0;
if (fabs(m.xy) < e)
m.xy = 0;
if (fabs(m.yx) < e)
m.yx = 0;
if (fabs(m.yy) < e)
m.yy = 0;
if (fabs(m.x0) < e)
m.x0 = 0;
if (fabs(m.y0) < e)
m.y0 = 0;
_cairo_output_stream_printf (stream,
"%f %f %f %f %f %f",
m.xx, m.yx, m.xy, m.yy, m.x0, m.y0);
}
long
_cairo_output_stream_get_position (cairo_output_stream_t *stream)
{
return stream->position;
}
cairo_status_t
_cairo_output_stream_get_status (cairo_output_stream_t *stream)
{
return stream->status;
}
/* Maybe this should be a configure time option, so embedded targets
* don't have to pull in stdio. */
typedef struct _stdio_stream {
cairo_output_stream_t base;
FILE *file;
} stdio_stream_t;
static cairo_status_t
stdio_write (cairo_output_stream_t *base,
const unsigned char *data, unsigned int length)
{
stdio_stream_t *stream = (stdio_stream_t *) base;
if (fwrite (data, 1, length, stream->file) != length)
return _cairo_error (CAIRO_STATUS_WRITE_ERROR);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
stdio_flush (cairo_output_stream_t *base)
{
stdio_stream_t *stream = (stdio_stream_t *) base;
fflush (stream->file);
if (ferror (stream->file))
return _cairo_error (CAIRO_STATUS_WRITE_ERROR);
else
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
stdio_close (cairo_output_stream_t *base)
{
cairo_status_t status;
stdio_stream_t *stream = (stdio_stream_t *) base;
status = stdio_flush (base);
fclose (stream->file);
return status;
}
cairo_output_stream_t *
_cairo_output_stream_create_for_file (FILE *file)
{
stdio_stream_t *stream;
if (file == NULL) {
_cairo_error_throw (CAIRO_STATUS_WRITE_ERROR);
return (cairo_output_stream_t *) &_cairo_output_stream_nil_write_error;
}
stream = _cairo_malloc (sizeof *stream);
if (unlikely (stream == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_output_stream_t *) &_cairo_output_stream_nil;
}
_cairo_output_stream_init (&stream->base,
stdio_write, stdio_flush, stdio_flush);
stream->file = file;
return &stream->base;
}
cairo_output_stream_t *
_cairo_output_stream_create_for_filename (const char *filename)
{
stdio_stream_t *stream;
FILE *file;
cairo_status_t status;
if (filename == NULL)
return _cairo_null_stream_create ();
status = _cairo_fopen (filename, "wb", &file);
if (status != CAIRO_STATUS_SUCCESS)
return _cairo_output_stream_create_in_error (status);
if (file == NULL) {
switch (errno) {
case ENOMEM:
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_output_stream_t *) &_cairo_output_stream_nil;
default:
_cairo_error_throw (CAIRO_STATUS_WRITE_ERROR);
return (cairo_output_stream_t *) &_cairo_output_stream_nil_write_error;
}
}
stream = _cairo_malloc (sizeof *stream);
if (unlikely (stream == NULL)) {
fclose (file);
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_output_stream_t *) &_cairo_output_stream_nil;
}
_cairo_output_stream_init (&stream->base,
stdio_write, stdio_flush, stdio_close);
stream->file = file;
return &stream->base;
}
typedef struct _memory_stream {
cairo_output_stream_t base;
cairo_array_t array;
} memory_stream_t;
static cairo_status_t
memory_write (cairo_output_stream_t *base,
const unsigned char *data, unsigned int length)
{
memory_stream_t *stream = (memory_stream_t *) base;
return _cairo_array_append_multiple (&stream->array, data, length);
}
static cairo_status_t
memory_close (cairo_output_stream_t *base)
{
memory_stream_t *stream = (memory_stream_t *) base;
_cairo_array_fini (&stream->array);
return CAIRO_STATUS_SUCCESS;
}
cairo_output_stream_t *
_cairo_memory_stream_create (void)
{
memory_stream_t *stream;
stream = _cairo_malloc (sizeof *stream);
if (unlikely (stream == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_output_stream_t *) &_cairo_output_stream_nil;
}
_cairo_output_stream_init (&stream->base, memory_write, NULL, memory_close);
_cairo_array_init (&stream->array, 1);
return &stream->base;
}
cairo_status_t
_cairo_memory_stream_destroy (cairo_output_stream_t *abstract_stream,
unsigned char **data_out,
unsigned long *length_out)
{
memory_stream_t *stream;
cairo_status_t status;
status = abstract_stream->status;
if (unlikely (status))
return _cairo_output_stream_destroy (abstract_stream);
stream = (memory_stream_t *) abstract_stream;
*length_out = _cairo_array_num_elements (&stream->array);
*data_out = _cairo_malloc (*length_out);
if (unlikely (*data_out == NULL)) {
status = _cairo_output_stream_destroy (abstract_stream);
assert (status == CAIRO_STATUS_SUCCESS);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
memcpy (*data_out, _cairo_array_index (&stream->array, 0), *length_out);
return _cairo_output_stream_destroy (abstract_stream);
}
void
_cairo_memory_stream_copy (cairo_output_stream_t *base,
cairo_output_stream_t *dest)
{
memory_stream_t *stream = (memory_stream_t *) base;
if (dest->status)
return;
if (base->status) {
dest->status = base->status;
return;
}
_cairo_output_stream_write (dest,
_cairo_array_index (&stream->array, 0),
_cairo_array_num_elements (&stream->array));
}
int
_cairo_memory_stream_length (cairo_output_stream_t *base)
{
memory_stream_t *stream = (memory_stream_t *) base;
return _cairo_array_num_elements (&stream->array);
}
static cairo_status_t
null_write (cairo_output_stream_t *base,
const unsigned char *data, unsigned int length)
{
return CAIRO_STATUS_SUCCESS;
}
cairo_output_stream_t *
_cairo_null_stream_create (void)
{
cairo_output_stream_t *stream;
stream = _cairo_malloc (sizeof *stream);
if (unlikely (stream == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_output_stream_t *) &_cairo_output_stream_nil;
}
_cairo_output_stream_init (stream, null_write, NULL, NULL);
return stream;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-paginated-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl Worth <cworth@cworth.org>
*/
#ifndef CAIRO_PAGINATED_H
#define CAIRO_PAGINATED_H
#include "cairoint.h"
struct _cairo_paginated_surface_backend {
/* Optional. Will be called once for each page.
*
* Note: With respect to the order of drawing operations as seen
* by the target, this call will occur before any drawing
* operations for the relevant page. However, with respect to the
* function calls as made by the user, this call will be *after*
* any drawing operations for the page, (that is, it will occur
* during the user's call to cairo_show_page or cairo_copy_page).
*/
cairo_warn cairo_int_status_t
(*start_page) (void *surface);
/* Required. Will be called twice for each page, once with an
* argument of CAIRO_PAGINATED_MODE_ANALYZE and once with
* CAIRO_PAGINATED_MODE_RENDER. See more details in the
* documentation for _cairo_paginated_surface_create below.
*/
cairo_warn cairo_int_status_t
(*set_paginated_mode) (void *surface,
cairo_paginated_mode_t mode);
/* Optional. Specifies the smallest box that encloses all objects
* on the page. Will be called at the end of the ANALYZE phase but
* before the mode is changed to RENDER.
*/
cairo_warn cairo_int_status_t
(*set_bounding_box) (void *surface,
cairo_box_t *bbox);
/* Optional. Indicates whether the page requires fallback images.
* Will be called at the end of the ANALYZE phase but before the
* mode is changed to RENDER.
*/
cairo_warn cairo_int_status_t
(*set_fallback_images_required) (void *surface,
cairo_bool_t fallbacks_required);
cairo_bool_t
(*supports_fine_grained_fallbacks) (void *surface);
/* Optional. Indicates whether the page requires a thumbnail image to be
* supplied. If a thumbnail is required, set width, height to size required
* and return TRUE.
*/
cairo_bool_t
(*requires_thumbnail_image) (void *surface,
int *width,
int *height);
/* If thumbbail image requested, this function will be called before
* _show_page().
*/
cairo_warn cairo_int_status_t
(*set_thumbnail_image) (void *surface,
cairo_image_surface_t *image);
};
/* A #cairo_paginated_surface_t provides a very convenient wrapper that
* is well-suited for doing the analysis common to most surfaces that
* have paginated output, (that is, things directed at printers, or
* for saving content in files such as PostScript or PDF files).
*
* To use the paginated surface, you'll first need to create your
* 'real' surface using _cairo_surface_init() and the standard
* #cairo_surface_backend_t. Then you also call
* _cairo_paginated_surface_create which takes its own, much simpler,
* #cairo_paginated_surface_backend_t. You are free to return the result
* of _cairo_paginated_surface_create() from your public
* cairo_<foo>_surface_create(). The paginated backend will be careful
* to not let the user see that they really got a "wrapped"
* surface. See test-paginated-surface.c for a fairly minimal example
* of a paginated-using surface. That should be a reasonable example
* to follow.
*
* What the paginated surface does is first save all drawing
* operations for a page into a recording-surface. Then when the user calls
* cairo_show_page(), the paginated surface performs the following
* sequence of operations (using the backend functions passed to
* cairo_paginated_surface_create()):
*
* 1. Calls start_page() (if not %NULL). At this point, it is appropriate
* for the target to emit any page-specific header information into
* its output.
*
* 2. Calls set_paginated_mode() with an argument of %CAIRO_PAGINATED_MODE_ANALYZE
*
* 3. Replays the recording-surface to the target surface, (with an
* analysis surface inserted between which watches the return value
* from each operation). This analysis stage is used to decide which
* operations will require fallbacks.
*
* 4. Calls set_bounding_box() to provide the target surface with the
* tight bounding box of the page.
*
* 5. Calls set_paginated_mode() with an argument of %CAIRO_PAGINATED_MODE_RENDER
*
* 6. Replays a subset of the recording-surface operations to the target surface
*
* 7. Calls set_paginated_mode() with an argument of %CAIRO_PAGINATED_MODE_FALLBACK
*
* 8. Replays the remaining operations to an image surface, sets an
* appropriate clip on the target, then paints the resulting image
* surface to the target.
*
* So, the target will see drawing operations during three separate
* stages, (ANALYZE, RENDER and FALLBACK). During the ANALYZE phase
* the target should not actually perform any rendering, (for example,
* if performing output to a file, no output should be generated
* during this stage). Instead the drawing functions simply need to
* return %CAIRO_STATUS_SUCCESS or %CAIRO_INT_STATUS_UNSUPPORTED to
* indicate whether rendering would be supported. And it should do
* this as quickly as possible. The FALLBACK phase allows the surface
* to distinguish fallback images from native rendering in case they
* need to be handled as a special case.
*
* Note: The paginated surface layer assumes that the target surface
* is "blank" by default at the beginning of each page, without any
* need for an explicit erase operation, (as opposed to an image
* surface, for example, which might have uninitialized content
* originally). As such, it optimizes away CLEAR operations that
* happen at the beginning of each page---the target surface will not
* even see these operations.
*/
cairo_private cairo_surface_t *
_cairo_paginated_surface_create (cairo_surface_t *target,
cairo_content_t content,
const cairo_paginated_surface_backend_t *backend);
cairo_private cairo_surface_t *
_cairo_paginated_surface_get_target (cairo_surface_t *surface);
cairo_private cairo_surface_t *
_cairo_paginated_surface_get_recording (cairo_surface_t *surface);
cairo_private cairo_bool_t
_cairo_surface_is_paginated (cairo_surface_t *surface);
cairo_private cairo_status_t
_cairo_paginated_surface_set_size (cairo_surface_t *surface,
int width,
int height);
#endif /* CAIRO_PAGINATED_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-paginated-surface-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl Worth <cworth@cworth.org>
*/
#ifndef CAIRO_PAGINATED_SURFACE_H
#define CAIRO_PAGINATED_SURFACE_H
#include "cairo.h"
#include "cairo-surface-private.h"
typedef struct _cairo_paginated_surface {
cairo_surface_t base;
/* The target surface to hold the final result. */
cairo_surface_t *target;
cairo_content_t content;
/* Paginated-surface specific functions for the target */
const cairo_paginated_surface_backend_t *backend;
/* A cairo_recording_surface to record all operations. To be replayed
* against target, and also against image surface as necessary for
* fallbacks. */
cairo_surface_t *recording_surface;
int page_num;
} cairo_paginated_surface_t;
#endif /* CAIRO_PAGINATED_SURFACE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-paginated-surface.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc
* Copyright © 2007 Adrian Johnson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl Worth <cworth@cworth.org>
* Keith Packard <keithp@keithp.com>
* Adrian Johnson <ajohnson@redneon.com>
*/
/* The paginated surface layer exists to provide as much code sharing
* as possible for the various paginated surface backends in cairo
* (PostScript, PDF, etc.). See cairo-paginated-private.h for
* more details on how it works and how to use it.
*/
#include "cairoint.h"
#include "cairo-paginated-private.h"
#include "cairo-paginated-surface-private.h"
#include "cairo-recording-surface-private.h"
#include "cairo-analysis-surface-private.h"
#include "cairo-error-private.h"
#include "cairo-image-surface-private.h"
#include "cairo-surface-subsurface-inline.h"
static const cairo_surface_backend_t cairo_paginated_surface_backend;
static cairo_int_status_t
_cairo_paginated_surface_show_page (void *abstract_surface);
static cairo_surface_t *
_cairo_paginated_surface_create_similar (void *abstract_surface,
cairo_content_t content,
int width,
int height)
{
cairo_rectangle_t rect;
rect.x = rect.y = 0.;
rect.width = width;
rect.height = height;
return cairo_recording_surface_create (content, &rect);
}
static cairo_surface_t *
_create_recording_surface_for_target (cairo_surface_t *target,
cairo_content_t content)
{
cairo_rectangle_int_t rect;
if (_cairo_surface_get_extents (target, &rect)) {
cairo_rectangle_t recording_extents;
recording_extents.x = rect.x;
recording_extents.y = rect.y;
recording_extents.width = rect.width;
recording_extents.height = rect.height;
return cairo_recording_surface_create (content, &recording_extents);
} else {
return cairo_recording_surface_create (content, NULL);
}
}
cairo_surface_t *
_cairo_paginated_surface_create (cairo_surface_t *target,
cairo_content_t content,
const cairo_paginated_surface_backend_t *backend)
{
cairo_paginated_surface_t *surface;
cairo_status_t status;
surface = _cairo_malloc (sizeof (cairo_paginated_surface_t));
if (unlikely (surface == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto FAIL;
}
_cairo_surface_init (&surface->base,
&cairo_paginated_surface_backend,
NULL, /* device */
content,
target->is_vector);
/* Override surface->base.type with target's type so we don't leak
* evidence of the paginated wrapper out to the user. */
surface->base.type = target->type;
surface->target = cairo_surface_reference (target);
surface->content = content;
surface->backend = backend;
surface->recording_surface = _create_recording_surface_for_target (target, content);
status = surface->recording_surface->status;
if (unlikely (status))
goto FAIL_CLEANUP_SURFACE;
surface->page_num = 1;
surface->base.is_clear = TRUE;
return &surface->base;
FAIL_CLEANUP_SURFACE:
cairo_surface_destroy (target);
free (surface);
FAIL:
return _cairo_surface_create_in_error (status);
}
cairo_bool_t
_cairo_surface_is_paginated (cairo_surface_t *surface)
{
return surface->backend == &cairo_paginated_surface_backend;
}
cairo_surface_t *
_cairo_paginated_surface_get_target (cairo_surface_t *surface)
{
cairo_paginated_surface_t *paginated_surface;
assert (_cairo_surface_is_paginated (surface));
paginated_surface = (cairo_paginated_surface_t *) surface;
return paginated_surface->target;
}
cairo_surface_t *
_cairo_paginated_surface_get_recording (cairo_surface_t *surface)
{
cairo_paginated_surface_t *paginated_surface;
assert (_cairo_surface_is_paginated (surface));
paginated_surface = (cairo_paginated_surface_t *) surface;
return paginated_surface->recording_surface;
}
cairo_status_t
_cairo_paginated_surface_set_size (cairo_surface_t *surface,
int width,
int height)
{
cairo_paginated_surface_t *paginated_surface;
cairo_status_t status;
cairo_rectangle_t recording_extents;
assert (_cairo_surface_is_paginated (surface));
paginated_surface = (cairo_paginated_surface_t *) surface;
recording_extents.x = 0;
recording_extents.y = 0;
recording_extents.width = width;
recording_extents.height = height;
cairo_surface_destroy (paginated_surface->recording_surface);
paginated_surface->recording_surface = cairo_recording_surface_create (paginated_surface->content,
&recording_extents);
status = paginated_surface->recording_surface->status;
if (unlikely (status))
return _cairo_surface_set_error (surface, status);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_paginated_surface_finish (void *abstract_surface)
{
cairo_paginated_surface_t *surface = abstract_surface;
cairo_status_t status = CAIRO_STATUS_SUCCESS;
if (! surface->base.is_clear || surface->page_num == 1) {
/* Bypass some of the sanity checking in cairo-surface.c, as we
* know that the surface is finished...
*/
status = _cairo_paginated_surface_show_page (surface);
}
/* XXX We want to propagate any errors from destroy(), but those are not
* returned via the api. So we need to explicitly finish the target,
* and check the status afterwards. However, we can only call finish()
* on the target, if we own it.
*/
if (CAIRO_REFERENCE_COUNT_GET_VALUE (&surface->target->ref_count) == 1)
cairo_surface_finish (surface->target);
if (status == CAIRO_STATUS_SUCCESS)
status = cairo_surface_status (surface->target);
cairo_surface_destroy (surface->target);
cairo_surface_finish (surface->recording_surface);
if (status == CAIRO_STATUS_SUCCESS)
status = cairo_surface_status (surface->recording_surface);
cairo_surface_destroy (surface->recording_surface);
return status;
}
static cairo_surface_t *
_cairo_paginated_surface_create_image_surface (void *abstract_surface,
int width,
int height)
{
cairo_paginated_surface_t *surface = abstract_surface;
cairo_surface_t *image;
cairo_font_options_t options;
image = _cairo_image_surface_create_with_content (surface->content,
width,
height);
cairo_surface_get_font_options (&surface->base, &options);
_cairo_surface_set_font_options (image, &options);
return image;
}
static cairo_surface_t *
_cairo_paginated_surface_source (void *abstract_surface,
cairo_rectangle_int_t *extents)
{
cairo_paginated_surface_t *surface = abstract_surface;
return _cairo_surface_get_source (surface->target, extents);
}
static cairo_status_t
_cairo_paginated_surface_acquire_source_image (void *abstract_surface,
cairo_image_surface_t **image_out,
void **image_extra)
{
cairo_paginated_surface_t *surface = abstract_surface;
cairo_bool_t is_bounded;
cairo_surface_t *image;
cairo_status_t status;
cairo_rectangle_int_t extents;
is_bounded = _cairo_surface_get_extents (surface->target, &extents);
if (! is_bounded)
return CAIRO_INT_STATUS_UNSUPPORTED;
image = _cairo_paginated_surface_create_image_surface (surface,
extents.width,
extents.height);
status = _cairo_recording_surface_replay (surface->recording_surface, image);
if (unlikely (status)) {
cairo_surface_destroy (image);
return status;
}
*image_out = (cairo_image_surface_t*) image;
*image_extra = NULL;
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_paginated_surface_release_source_image (void *abstract_surface,
cairo_image_surface_t *image,
void *image_extra)
{
cairo_surface_destroy (&image->base);
}
static cairo_int_status_t
_paint_thumbnail_image (cairo_paginated_surface_t *surface,
int width,
int height)
{
cairo_surface_pattern_t pattern;
cairo_rectangle_int_t extents;
double x_scale;
double y_scale;
cairo_surface_t *image = NULL;
cairo_surface_t *opaque = NULL;
cairo_status_t status = CAIRO_STATUS_SUCCESS;
_cairo_surface_get_extents (surface->target, &extents);
x_scale = (double)width / extents.width;
y_scale = (double)height / extents.height;
image = _cairo_paginated_surface_create_image_surface (surface, width, height);
cairo_surface_set_device_scale (image, x_scale, y_scale);
cairo_surface_set_device_offset (image, -extents.x*x_scale, -extents.y*y_scale);
status = _cairo_recording_surface_replay (surface->recording_surface, image);
if (unlikely (status))
goto cleanup;
/* flatten transparency */
opaque = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
if (unlikely (opaque->status)) {
status = opaque->status;
goto cleanup;
}
status = _cairo_surface_paint (opaque,
CAIRO_OPERATOR_SOURCE,
&_cairo_pattern_white.base,
NULL);
if (unlikely (status))
goto cleanup;
_cairo_pattern_init_for_surface (&pattern, image);
pattern.base.filter = CAIRO_FILTER_NEAREST;
status = _cairo_surface_paint (opaque, CAIRO_OPERATOR_OVER, &pattern.base, NULL);
_cairo_pattern_fini (&pattern.base);
if (unlikely (status))
goto cleanup;
status = surface->backend->set_thumbnail_image (surface->target, (cairo_image_surface_t *)opaque);
cleanup:
if (image)
cairo_surface_destroy (image);
if (opaque)
cairo_surface_destroy (opaque);
return status;
}
static cairo_int_status_t
_paint_fallback_image (cairo_paginated_surface_t *surface,
cairo_rectangle_int_t *rect)
{
double x_scale = surface->base.x_fallback_resolution / surface->target->x_resolution;
double y_scale = surface->base.y_fallback_resolution / surface->target->y_resolution;
int x, y, width, height;
cairo_status_t status;
cairo_surface_t *image;
cairo_surface_pattern_t pattern;
cairo_clip_t *clip;
x = rect->x;
y = rect->y;
width = rect->width;
height = rect->height;
image = _cairo_paginated_surface_create_image_surface (surface,
ceil (width * x_scale),
ceil (height * y_scale));
cairo_surface_set_device_scale (image, x_scale, y_scale);
/* set_device_offset just sets the x0/y0 components of the matrix;
* so we have to do the scaling manually. */
cairo_surface_set_device_offset (image, -x*x_scale, -y*y_scale);
status = _cairo_recording_surface_replay (surface->recording_surface, image);
if (unlikely (status))
goto CLEANUP_IMAGE;
_cairo_pattern_init_for_surface (&pattern, image);
cairo_matrix_init (&pattern.base.matrix,
x_scale, 0, 0, y_scale, -x*x_scale, -y*y_scale);
/* the fallback should be rendered at native resolution, so disable
* filtering (if possible) to avoid introducing potential artifacts. */
pattern.base.filter = CAIRO_FILTER_NEAREST;
clip = _cairo_clip_intersect_rectangle (NULL, rect);
status = _cairo_surface_paint (surface->target,
CAIRO_OPERATOR_SOURCE,
&pattern.base, clip);
_cairo_clip_destroy (clip);
_cairo_pattern_fini (&pattern.base);
CLEANUP_IMAGE:
cairo_surface_destroy (image);
return status;
}
static cairo_int_status_t
_paint_page (cairo_paginated_surface_t *surface)
{
cairo_surface_t *analysis;
cairo_int_status_t status;
cairo_bool_t has_supported, has_page_fallback, has_finegrained_fallback;
if (unlikely (surface->target->status))
return surface->target->status;
analysis = _cairo_analysis_surface_create (surface->target);
if (unlikely (analysis->status))
return _cairo_surface_set_error (surface->target, analysis->status);
status = surface->backend->set_paginated_mode (surface->target,
CAIRO_PAGINATED_MODE_ANALYZE);
if (unlikely (status))
goto FAIL;
status = _cairo_recording_surface_replay_and_create_regions (surface->recording_surface,
NULL, analysis, FALSE);
if (status)
goto FAIL;
assert (analysis->status == CAIRO_STATUS_SUCCESS);
if (surface->backend->set_bounding_box) {
cairo_box_t bbox;
_cairo_analysis_surface_get_bounding_box (analysis, &bbox);
status = surface->backend->set_bounding_box (surface->target, &bbox);
if (unlikely (status))
goto FAIL;
}
if (surface->backend->set_fallback_images_required) {
cairo_bool_t has_fallbacks = _cairo_analysis_surface_has_unsupported (analysis);
status = surface->backend->set_fallback_images_required (surface->target,
has_fallbacks);
if (unlikely (status))
goto FAIL;
}
/* Finer grained fallbacks are currently only supported for some
* surface types */
if (surface->backend->supports_fine_grained_fallbacks != NULL &&
surface->backend->supports_fine_grained_fallbacks (surface->target))
{
has_supported = _cairo_analysis_surface_has_supported (analysis);
has_page_fallback = FALSE;
has_finegrained_fallback = _cairo_analysis_surface_has_unsupported (analysis);
}
else
{
if (_cairo_analysis_surface_has_unsupported (analysis)) {
has_supported = FALSE;
has_page_fallback = TRUE;
} else {
has_supported = TRUE;
has_page_fallback = FALSE;
}
has_finegrained_fallback = FALSE;
}
if (has_supported) {
status = surface->backend->set_paginated_mode (surface->target,
CAIRO_PAGINATED_MODE_RENDER);
if (unlikely (status))
goto FAIL;
status = _cairo_recording_surface_replay_region (surface->recording_surface,
NULL,
surface->target,
CAIRO_RECORDING_REGION_NATIVE);
assert (status != CAIRO_INT_STATUS_UNSUPPORTED);
if (unlikely (status))
goto FAIL;
}
if (has_page_fallback) {
cairo_rectangle_int_t extents;
cairo_bool_t is_bounded;
status = surface->backend->set_paginated_mode (surface->target,
CAIRO_PAGINATED_MODE_FALLBACK);
if (unlikely (status))
goto FAIL;
is_bounded = _cairo_surface_get_extents (surface->target, &extents);
if (! is_bounded) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto FAIL;
}
status = _paint_fallback_image (surface, &extents);
if (unlikely (status))
goto FAIL;
}
if (has_finegrained_fallback) {
cairo_region_t *region;
int num_rects, i;
status = surface->backend->set_paginated_mode (surface->target,
CAIRO_PAGINATED_MODE_FALLBACK);
if (unlikely (status))
goto FAIL;
region = _cairo_analysis_surface_get_unsupported (analysis);
num_rects = cairo_region_num_rectangles (region);
for (i = 0; i < num_rects; i++) {
cairo_rectangle_int_t rect;
cairo_region_get_rectangle (region, i, &rect);
status = _paint_fallback_image (surface, &rect);
if (unlikely (status))
goto FAIL;
}
}
if (surface->backend->requires_thumbnail_image) {
int width, height;
if (surface->backend->requires_thumbnail_image (surface->target, &width, &height))
_paint_thumbnail_image (surface, width, height);
}
FAIL:
cairo_surface_destroy (analysis);
return _cairo_surface_set_error (surface->target, status);
}
static cairo_status_t
_start_page (cairo_paginated_surface_t *surface)
{
if (surface->target->status)
return surface->target->status;
if (! surface->backend->start_page)
return CAIRO_STATUS_SUCCESS;
return _cairo_surface_set_error (surface->target,
surface->backend->start_page (surface->target));
}
static cairo_int_status_t
_cairo_paginated_surface_copy_page (void *abstract_surface)
{
cairo_status_t status;
cairo_paginated_surface_t *surface = abstract_surface;
status = _start_page (surface);
if (unlikely (status))
return status;
status = _paint_page (surface);
if (unlikely (status))
return status;
surface->page_num++;
/* XXX: It might make sense to add some support here for calling
* cairo_surface_copy_page on the target surface. It would be an
* optimization for the output, but the interaction with image
* fallbacks gets tricky. For now, we just let the target see a
* show_page and we implement the copying by simply not destroying
* the recording-surface. */
cairo_surface_show_page (surface->target);
return cairo_surface_status (surface->target);
}
static cairo_int_status_t
_cairo_paginated_surface_show_page (void *abstract_surface)
{
cairo_status_t status;
cairo_paginated_surface_t *surface = abstract_surface;
status = _start_page (surface);
if (unlikely (status))
return status;
status = _paint_page (surface);
if (unlikely (status))
return status;
cairo_surface_show_page (surface->target);
status = surface->target->status;
if (unlikely (status))
return status;
status = surface->recording_surface->status;
if (unlikely (status))
return status;
if (! surface->base.finished) {
cairo_surface_destroy (surface->recording_surface);
surface->recording_surface = _create_recording_surface_for_target (surface->target,
surface->content);
status = surface->recording_surface->status;
if (unlikely (status))
return status;
surface->page_num++;
surface->base.is_clear = TRUE;
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_bool_t
_cairo_paginated_surface_get_extents (void *abstract_surface,
cairo_rectangle_int_t *rectangle)
{
cairo_paginated_surface_t *surface = abstract_surface;
return _cairo_surface_get_extents (surface->target, rectangle);
}
static void
_cairo_paginated_surface_get_font_options (void *abstract_surface,
cairo_font_options_t *options)
{
cairo_paginated_surface_t *surface = abstract_surface;
cairo_surface_get_font_options (surface->target, options);
}
static cairo_int_status_t
_cairo_paginated_surface_paint (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_clip_t *clip)
{
cairo_paginated_surface_t *surface = abstract_surface;
return _cairo_surface_paint (surface->recording_surface, op, source, clip);
}
static cairo_int_status_t
_cairo_paginated_surface_mask (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_pattern_t *mask,
const cairo_clip_t *clip)
{
cairo_paginated_surface_t *surface = abstract_surface;
return _cairo_surface_mask (surface->recording_surface, op, source, mask, clip);
}
static cairo_int_status_t
_cairo_paginated_surface_stroke (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_antialias_t antialias,
const cairo_clip_t *clip)
{
cairo_paginated_surface_t *surface = abstract_surface;
return _cairo_surface_stroke (surface->recording_surface, op, source,
path, style,
ctm, ctm_inverse,
tolerance, antialias,
clip);
}
static cairo_int_status_t
_cairo_paginated_surface_fill (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias,
const cairo_clip_t *clip)
{
cairo_paginated_surface_t *surface = abstract_surface;
return _cairo_surface_fill (surface->recording_surface, op, source,
path, fill_rule,
tolerance, antialias,
clip);
}
static cairo_bool_t
_cairo_paginated_surface_has_show_text_glyphs (void *abstract_surface)
{
cairo_paginated_surface_t *surface = abstract_surface;
return cairo_surface_has_show_text_glyphs (surface->target);
}
static cairo_int_status_t
_cairo_paginated_surface_show_text_glyphs (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const char *utf8,
int utf8_len,
cairo_glyph_t *glyphs,
int num_glyphs,
const cairo_text_cluster_t *clusters,
int num_clusters,
cairo_text_cluster_flags_t cluster_flags,
cairo_scaled_font_t *scaled_font,
const cairo_clip_t *clip)
{
cairo_paginated_surface_t *surface = abstract_surface;
return _cairo_surface_show_text_glyphs (surface->recording_surface, op, source,
utf8, utf8_len,
glyphs, num_glyphs,
clusters, num_clusters,
cluster_flags,
scaled_font,
clip);
}
static const char **
_cairo_paginated_surface_get_supported_mime_types (void *abstract_surface)
{
cairo_paginated_surface_t *surface = abstract_surface;
if (surface->target->backend->get_supported_mime_types)
return surface->target->backend->get_supported_mime_types (surface->target);
return NULL;
}
static cairo_int_status_t
_cairo_paginated_surface_tag (void *abstract_surface,
cairo_bool_t begin,
const char *tag_name,
const char *attributes,
const cairo_pattern_t *source,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
const cairo_clip_t *clip)
{
cairo_paginated_surface_t *surface = abstract_surface;
return _cairo_surface_tag (surface->recording_surface,
begin, tag_name, attributes,
source, style,
ctm, ctm_inverse,
clip);
}
static cairo_surface_t *
_cairo_paginated_surface_snapshot (void *abstract_other)
{
cairo_paginated_surface_t *other = abstract_other;
return other->recording_surface->backend->snapshot (other->recording_surface);
}
static cairo_t *
_cairo_paginated_context_create (void *target)
{
cairo_paginated_surface_t *surface = target;
if (_cairo_surface_is_subsurface (&surface->base))
surface = (cairo_paginated_surface_t *)
_cairo_surface_subsurface_get_target (&surface->base);
return surface->recording_surface->backend->create_context (target);
}
static const cairo_surface_backend_t cairo_paginated_surface_backend = {
CAIRO_INTERNAL_SURFACE_TYPE_PAGINATED,
_cairo_paginated_surface_finish,
_cairo_paginated_context_create,
_cairo_paginated_surface_create_similar,
NULL, /* create similar image */
NULL, /* map to image */
NULL, /* unmap image */
_cairo_paginated_surface_source,
_cairo_paginated_surface_acquire_source_image,
_cairo_paginated_surface_release_source_image,
_cairo_paginated_surface_snapshot,
_cairo_paginated_surface_copy_page,
_cairo_paginated_surface_show_page,
_cairo_paginated_surface_get_extents,
_cairo_paginated_surface_get_font_options,
NULL, /* flush */
NULL, /* mark_dirty_rectangle */
_cairo_paginated_surface_paint,
_cairo_paginated_surface_mask,
_cairo_paginated_surface_stroke,
_cairo_paginated_surface_fill,
NULL, /* fill_stroke */
NULL, /* show_glyphs */
_cairo_paginated_surface_has_show_text_glyphs,
_cairo_paginated_surface_show_text_glyphs,
_cairo_paginated_surface_get_supported_mime_types,
_cairo_paginated_surface_tag,
};
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-bounds.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2003 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-box-inline.h"
#include "cairo-error-private.h"
#include "cairo-path-fixed-private.h"
typedef struct _cairo_path_bounder {
cairo_point_t current_point;
cairo_bool_t has_extents;
cairo_box_t extents;
} cairo_path_bounder_t;
static cairo_status_t
_cairo_path_bounder_move_to (void *closure,
const cairo_point_t *point)
{
cairo_path_bounder_t *bounder = closure;
bounder->current_point = *point;
if (likely (bounder->has_extents)) {
_cairo_box_add_point (&bounder->extents, point);
} else {
bounder->has_extents = TRUE;
_cairo_box_set (&bounder->extents, point, point);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_path_bounder_line_to (void *closure,
const cairo_point_t *point)
{
cairo_path_bounder_t *bounder = closure;
bounder->current_point = *point;
_cairo_box_add_point (&bounder->extents, point);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_path_bounder_curve_to (void *closure,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
cairo_path_bounder_t *bounder = closure;
_cairo_box_add_curve_to (&bounder->extents,
&bounder->current_point,
b, c, d);
bounder->current_point = *d;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_path_bounder_close_path (void *closure)
{
return CAIRO_STATUS_SUCCESS;
}
cairo_bool_t
_cairo_path_bounder_extents (const cairo_path_fixed_t *path,
cairo_box_t *extents)
{
cairo_path_bounder_t bounder;
cairo_status_t status;
bounder.has_extents = FALSE;
status = _cairo_path_fixed_interpret (path,
_cairo_path_bounder_move_to,
_cairo_path_bounder_line_to,
_cairo_path_bounder_curve_to,
_cairo_path_bounder_close_path,
&bounder);
assert (!status);
if (bounder.has_extents)
*extents = bounder.extents;
return bounder.has_extents;
}
void
_cairo_path_fixed_approximate_clip_extents (const cairo_path_fixed_t *path,
cairo_rectangle_int_t *extents)
{
_cairo_path_fixed_approximate_fill_extents (path, extents);
}
void
_cairo_path_fixed_approximate_fill_extents (const cairo_path_fixed_t *path,
cairo_rectangle_int_t *extents)
{
_cairo_path_fixed_fill_extents (path, CAIRO_FILL_RULE_WINDING, 0, extents);
}
void
_cairo_path_fixed_fill_extents (const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_rectangle_int_t *extents)
{
if (path->extents.p1.x < path->extents.p2.x &&
path->extents.p1.y < path->extents.p2.y) {
_cairo_box_round_to_rectangle (&path->extents, extents);
} else {
extents->x = extents->y = 0;
extents->width = extents->height = 0;
}
}
/* Adjusts the fill extents (above) by the device-space pen. */
void
_cairo_path_fixed_approximate_stroke_extents (const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
cairo_bool_t is_vector,
cairo_rectangle_int_t *extents)
{
if (path->has_extents) {
cairo_box_t box_extents;
double dx, dy;
_cairo_stroke_style_max_distance_from_path (style, path, ctm, &dx, &dy);
if (is_vector)
{
/* When calculating extents for vector surfaces, ensure lines thinner
* than the fixed point resolution are not optimized away. */
double min = _cairo_fixed_to_double (CAIRO_FIXED_EPSILON*2);
if (dx < min)
dx = min;
if (dy < min)
dy = min;
}
box_extents = path->extents;
box_extents.p1.x -= _cairo_fixed_from_double (dx);
box_extents.p1.y -= _cairo_fixed_from_double (dy);
box_extents.p2.x += _cairo_fixed_from_double (dx);
box_extents.p2.y += _cairo_fixed_from_double (dy);
_cairo_box_round_to_rectangle (&box_extents, extents);
} else {
extents->x = extents->y = 0;
extents->width = extents->height = 0;
}
}
cairo_status_t
_cairo_path_fixed_stroke_extents (const cairo_path_fixed_t *path,
const cairo_stroke_style_t *stroke_style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_rectangle_int_t *extents)
{
cairo_polygon_t polygon;
cairo_status_t status;
cairo_stroke_style_t style;
/* When calculating extents for vector surfaces, ensure lines thinner
* than one point are not optimized away. */
double min_line_width = _cairo_matrix_transformed_circle_major_axis (ctm_inverse, 1.0);
if (stroke_style->line_width < min_line_width)
{
style = *stroke_style;
style.line_width = min_line_width;
stroke_style = &style;
}
_cairo_polygon_init (&polygon, NULL, 0);
status = _cairo_path_fixed_stroke_to_polygon (path,
stroke_style,
ctm, ctm_inverse,
tolerance,
&polygon);
_cairo_box_round_to_rectangle (&polygon.extents, extents);
_cairo_polygon_fini (&polygon);
return status;
}
cairo_bool_t
_cairo_path_fixed_extents (const cairo_path_fixed_t *path,
cairo_box_t *box)
{
*box = path->extents;
return path->has_extents;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-fill.c | /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-boxes-private.h"
#include "cairo-error-private.h"
#include "cairo-path-fixed-private.h"
#include "cairo-region-private.h"
#include "cairo-traps-private.h"
typedef struct cairo_filler {
cairo_polygon_t *polygon;
double tolerance;
cairo_box_t limit;
cairo_bool_t has_limits;
cairo_point_t current_point;
cairo_point_t last_move_to;
} cairo_filler_t;
static cairo_status_t
_cairo_filler_line_to (void *closure,
const cairo_point_t *point)
{
cairo_filler_t *filler = closure;
cairo_status_t status;
status = _cairo_polygon_add_external_edge (filler->polygon,
&filler->current_point,
point);
filler->current_point = *point;
return status;
}
static cairo_status_t
_cairo_filler_close (void *closure)
{
cairo_filler_t *filler = closure;
/* close the subpath */
return _cairo_filler_line_to (closure, &filler->last_move_to);
}
static cairo_status_t
_cairo_filler_move_to (void *closure,
const cairo_point_t *point)
{
cairo_filler_t *filler = closure;
cairo_status_t status;
/* close current subpath */
status = _cairo_filler_close (closure);
if (unlikely (status))
return status;
/* make sure that the closure represents a degenerate path */
filler->current_point = *point;
filler->last_move_to = *point;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_filler_curve_to (void *closure,
const cairo_point_t *p1,
const cairo_point_t *p2,
const cairo_point_t *p3)
{
cairo_filler_t *filler = closure;
cairo_spline_t spline;
if (filler->has_limits) {
if (! _cairo_spline_intersects (&filler->current_point, p1, p2, p3,
&filler->limit))
return _cairo_filler_line_to (filler, p3);
}
if (! _cairo_spline_init (&spline,
(cairo_spline_add_point_func_t)_cairo_filler_line_to, filler,
&filler->current_point, p1, p2, p3))
{
return _cairo_filler_line_to (closure, p3);
}
return _cairo_spline_decompose (&spline, filler->tolerance);
}
cairo_status_t
_cairo_path_fixed_fill_to_polygon (const cairo_path_fixed_t *path,
double tolerance,
cairo_polygon_t *polygon)
{
cairo_filler_t filler;
cairo_status_t status;
filler.polygon = polygon;
filler.tolerance = tolerance;
filler.has_limits = FALSE;
if (polygon->num_limits) {
filler.has_limits = TRUE;
filler.limit = polygon->limit;
}
/* make sure that the closure represents a degenerate path */
filler.current_point.x = 0;
filler.current_point.y = 0;
filler.last_move_to = filler.current_point;
status = _cairo_path_fixed_interpret (path,
_cairo_filler_move_to,
_cairo_filler_line_to,
_cairo_filler_curve_to,
_cairo_filler_close,
&filler);
if (unlikely (status))
return status;
return _cairo_filler_close (&filler);
}
typedef struct cairo_filler_rectilinear_aligned {
cairo_polygon_t *polygon;
cairo_point_t current_point;
cairo_point_t last_move_to;
} cairo_filler_ra_t;
static cairo_status_t
_cairo_filler_ra_line_to (void *closure,
const cairo_point_t *point)
{
cairo_filler_ra_t *filler = closure;
cairo_status_t status;
cairo_point_t p;
p.x = _cairo_fixed_round_down (point->x);
p.y = _cairo_fixed_round_down (point->y);
status = _cairo_polygon_add_external_edge (filler->polygon,
&filler->current_point,
&p);
filler->current_point = p;
return status;
}
static cairo_status_t
_cairo_filler_ra_close (void *closure)
{
cairo_filler_ra_t *filler = closure;
return _cairo_filler_ra_line_to (closure, &filler->last_move_to);
}
static cairo_status_t
_cairo_filler_ra_move_to (void *closure,
const cairo_point_t *point)
{
cairo_filler_ra_t *filler = closure;
cairo_status_t status;
cairo_point_t p;
/* close current subpath */
status = _cairo_filler_ra_close (closure);
if (unlikely (status))
return status;
p.x = _cairo_fixed_round_down (point->x);
p.y = _cairo_fixed_round_down (point->y);
/* make sure that the closure represents a degenerate path */
filler->current_point = p;
filler->last_move_to = p;
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_path_fixed_fill_rectilinear_to_polygon (const cairo_path_fixed_t *path,
cairo_antialias_t antialias,
cairo_polygon_t *polygon)
{
cairo_filler_ra_t filler;
cairo_status_t status;
if (antialias != CAIRO_ANTIALIAS_NONE)
return _cairo_path_fixed_fill_to_polygon (path, 0., polygon);
filler.polygon = polygon;
/* make sure that the closure represents a degenerate path */
filler.current_point.x = 0;
filler.current_point.y = 0;
filler.last_move_to = filler.current_point;
status = _cairo_path_fixed_interpret_flat (path,
_cairo_filler_ra_move_to,
_cairo_filler_ra_line_to,
_cairo_filler_ra_close,
&filler,
0.);
if (unlikely (status))
return status;
return _cairo_filler_ra_close (&filler);
}
cairo_status_t
_cairo_path_fixed_fill_to_traps (const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_traps_t *traps)
{
cairo_polygon_t polygon;
cairo_status_t status;
if (_cairo_path_fixed_fill_is_empty (path))
return CAIRO_STATUS_SUCCESS;
_cairo_polygon_init (&polygon, traps->limits, traps->num_limits);
status = _cairo_path_fixed_fill_to_polygon (path, tolerance, &polygon);
if (unlikely (status || polygon.num_edges == 0))
goto CLEANUP;
status = _cairo_bentley_ottmann_tessellate_polygon (traps,
&polygon, fill_rule);
CLEANUP:
_cairo_polygon_fini (&polygon);
return status;
}
static cairo_status_t
_cairo_path_fixed_fill_rectilinear_tessellate_to_boxes (const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
cairo_antialias_t antialias,
cairo_boxes_t *boxes)
{
cairo_polygon_t polygon;
cairo_status_t status;
_cairo_polygon_init (&polygon, boxes->limits, boxes->num_limits);
boxes->num_limits = 0;
/* tolerance will be ignored as the path is rectilinear */
status = _cairo_path_fixed_fill_rectilinear_to_polygon (path, antialias, &polygon);
if (likely (status == CAIRO_STATUS_SUCCESS)) {
status =
_cairo_bentley_ottmann_tessellate_rectilinear_polygon_to_boxes (&polygon,
fill_rule,
boxes);
}
_cairo_polygon_fini (&polygon);
return status;
}
cairo_status_t
_cairo_path_fixed_fill_rectilinear_to_boxes (const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
cairo_antialias_t antialias,
cairo_boxes_t *boxes)
{
cairo_path_fixed_iter_t iter;
cairo_status_t status;
cairo_box_t box;
if (_cairo_path_fixed_is_box (path, &box))
return _cairo_boxes_add (boxes, antialias, &box);
_cairo_path_fixed_iter_init (&iter, path);
while (_cairo_path_fixed_iter_is_fill_box (&iter, &box)) {
if (box.p1.y == box.p2.y || box.p1.x == box.p2.x)
continue;
if (box.p1.y > box.p2.y) {
cairo_fixed_t t;
t = box.p1.y;
box.p1.y = box.p2.y;
box.p2.y = t;
t = box.p1.x;
box.p1.x = box.p2.x;
box.p2.x = t;
}
status = _cairo_boxes_add (boxes, antialias, &box);
if (unlikely (status))
return status;
}
if (_cairo_path_fixed_iter_at_end (&iter))
return _cairo_bentley_ottmann_tessellate_boxes (boxes, fill_rule, boxes);
/* path is not rectangular, try extracting clipped rectilinear edges */
_cairo_boxes_clear (boxes);
return _cairo_path_fixed_fill_rectilinear_tessellate_to_boxes (path,
fill_rule,
antialias,
boxes);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-fixed-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@redhat.com>
*/
#ifndef CAIRO_PATH_FIXED_PRIVATE_H
#define CAIRO_PATH_FIXED_PRIVATE_H
#include "cairo-types-private.h"
#include "cairo-compiler-private.h"
#include "cairo-list-private.h"
#define WATCH_PATH 0
#if WATCH_PATH
#include <stdio.h>
#endif
enum cairo_path_op {
CAIRO_PATH_OP_MOVE_TO = 0,
CAIRO_PATH_OP_LINE_TO = 1,
CAIRO_PATH_OP_CURVE_TO = 2,
CAIRO_PATH_OP_CLOSE_PATH = 3
};
/* we want to make sure a single byte is used for the enum */
typedef char cairo_path_op_t;
/* make _cairo_path_fixed fit into ~512 bytes -- about 50 items */
#define CAIRO_PATH_BUF_SIZE ((512 - sizeof (cairo_path_buf_t)) \
/ (2 * sizeof (cairo_point_t) + sizeof (cairo_path_op_t)))
#define cairo_path_head(path__) (&(path__)->buf.base)
#define cairo_path_tail(path__) cairo_path_buf_prev (cairo_path_head (path__))
#define cairo_path_buf_next(pos__) \
cairo_list_entry ((pos__)->link.next, cairo_path_buf_t, link)
#define cairo_path_buf_prev(pos__) \
cairo_list_entry ((pos__)->link.prev, cairo_path_buf_t, link)
#define cairo_path_foreach_buf_start(pos__, path__) \
pos__ = cairo_path_head (path__); do
#define cairo_path_foreach_buf_end(pos__, path__) \
while ((pos__ = cairo_path_buf_next (pos__)) != cairo_path_head (path__))
typedef struct _cairo_path_buf {
cairo_list_t link;
unsigned int num_ops;
unsigned int size_ops;
unsigned int num_points;
unsigned int size_points;
cairo_path_op_t *op;
cairo_point_t *points;
} cairo_path_buf_t;
typedef struct _cairo_path_buf_fixed {
cairo_path_buf_t base;
cairo_path_op_t op[CAIRO_PATH_BUF_SIZE];
cairo_point_t points[2 * CAIRO_PATH_BUF_SIZE];
} cairo_path_buf_fixed_t;
/*
NOTES:
has_curve_to => !stroke_is_rectilinear
fill_is_rectilinear => stroke_is_rectilinear
fill_is_empty => fill_is_rectilinear
fill_maybe_region => fill_is_rectilinear
*/
struct _cairo_path_fixed {
cairo_point_t last_move_point;
cairo_point_t current_point;
unsigned int has_current_point : 1;
unsigned int needs_move_to : 1;
unsigned int has_extents : 1;
unsigned int has_curve_to : 1;
unsigned int stroke_is_rectilinear : 1;
unsigned int fill_is_rectilinear : 1;
unsigned int fill_maybe_region : 1;
unsigned int fill_is_empty : 1;
cairo_box_t extents;
cairo_path_buf_fixed_t buf;
};
cairo_private void
_cairo_path_fixed_translate (cairo_path_fixed_t *path,
cairo_fixed_t offx,
cairo_fixed_t offy);
cairo_private cairo_status_t
_cairo_path_fixed_append (cairo_path_fixed_t *path,
const cairo_path_fixed_t *other,
cairo_fixed_t tx,
cairo_fixed_t ty);
cairo_private unsigned long
_cairo_path_fixed_hash (const cairo_path_fixed_t *path);
cairo_private unsigned long
_cairo_path_fixed_size (const cairo_path_fixed_t *path);
cairo_private cairo_bool_t
_cairo_path_fixed_equal (const cairo_path_fixed_t *a,
const cairo_path_fixed_t *b);
typedef struct _cairo_path_fixed_iter {
const cairo_path_buf_t *first;
const cairo_path_buf_t *buf;
unsigned int n_op;
unsigned int n_point;
} cairo_path_fixed_iter_t;
cairo_private void
_cairo_path_fixed_iter_init (cairo_path_fixed_iter_t *iter,
const cairo_path_fixed_t *path);
cairo_private cairo_bool_t
_cairo_path_fixed_iter_is_fill_box (cairo_path_fixed_iter_t *_iter,
cairo_box_t *box);
cairo_private cairo_bool_t
_cairo_path_fixed_iter_at_end (const cairo_path_fixed_iter_t *iter);
static inline cairo_bool_t
_cairo_path_fixed_fill_is_empty (const cairo_path_fixed_t *path)
{
return path->fill_is_empty;
}
static inline cairo_bool_t
_cairo_path_fixed_fill_is_rectilinear (const cairo_path_fixed_t *path)
{
if (! path->fill_is_rectilinear)
return 0;
if (! path->has_current_point || path->needs_move_to)
return 1;
/* check whether the implicit close preserves the rectilinear property */
return path->current_point.x == path->last_move_point.x ||
path->current_point.y == path->last_move_point.y;
}
static inline cairo_bool_t
_cairo_path_fixed_stroke_is_rectilinear (const cairo_path_fixed_t *path)
{
return path->stroke_is_rectilinear;
}
static inline cairo_bool_t
_cairo_path_fixed_fill_maybe_region (const cairo_path_fixed_t *path)
{
if (! path->fill_maybe_region)
return 0;
if (! path->has_current_point || path->needs_move_to)
return 1;
/* check whether the implicit close preserves the rectilinear property
* (the integer point property is automatically preserved)
*/
return path->current_point.x == path->last_move_point.x ||
path->current_point.y == path->last_move_point.y;
}
cairo_private cairo_bool_t
_cairo_path_fixed_is_stroke_box (const cairo_path_fixed_t *path,
cairo_box_t *box);
cairo_private cairo_bool_t
_cairo_path_fixed_is_simple_quad (const cairo_path_fixed_t *path);
#endif /* CAIRO_PATH_FIXED_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-fixed.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-box-inline.h"
#include "cairo-error-private.h"
#include "cairo-list-inline.h"
#include "cairo-path-fixed-private.h"
#include "cairo-slope-private.h"
static cairo_status_t
_cairo_path_fixed_add (cairo_path_fixed_t *path,
cairo_path_op_t op,
const cairo_point_t *points,
int num_points);
static void
_cairo_path_fixed_add_buf (cairo_path_fixed_t *path,
cairo_path_buf_t *buf);
static cairo_path_buf_t *
_cairo_path_buf_create (int size_ops, int size_points);
static void
_cairo_path_buf_destroy (cairo_path_buf_t *buf);
static void
_cairo_path_buf_add_op (cairo_path_buf_t *buf,
cairo_path_op_t op);
static void
_cairo_path_buf_add_points (cairo_path_buf_t *buf,
const cairo_point_t *points,
int num_points);
void
_cairo_path_fixed_init (cairo_path_fixed_t *path)
{
VG (VALGRIND_MAKE_MEM_UNDEFINED (path, sizeof (cairo_path_fixed_t)));
cairo_list_init (&path->buf.base.link);
path->buf.base.num_ops = 0;
path->buf.base.num_points = 0;
path->buf.base.size_ops = ARRAY_LENGTH (path->buf.op);
path->buf.base.size_points = ARRAY_LENGTH (path->buf.points);
path->buf.base.op = path->buf.op;
path->buf.base.points = path->buf.points;
path->current_point.x = 0;
path->current_point.y = 0;
path->last_move_point = path->current_point;
path->has_current_point = FALSE;
path->needs_move_to = TRUE;
path->has_extents = FALSE;
path->has_curve_to = FALSE;
path->stroke_is_rectilinear = TRUE;
path->fill_is_rectilinear = TRUE;
path->fill_maybe_region = TRUE;
path->fill_is_empty = TRUE;
path->extents.p1.x = path->extents.p1.y = 0;
path->extents.p2.x = path->extents.p2.y = 0;
}
cairo_status_t
_cairo_path_fixed_init_copy (cairo_path_fixed_t *path,
const cairo_path_fixed_t *other)
{
cairo_path_buf_t *buf, *other_buf;
unsigned int num_points, num_ops;
VG (VALGRIND_MAKE_MEM_UNDEFINED (path, sizeof (cairo_path_fixed_t)));
cairo_list_init (&path->buf.base.link);
path->buf.base.op = path->buf.op;
path->buf.base.points = path->buf.points;
path->buf.base.size_ops = ARRAY_LENGTH (path->buf.op);
path->buf.base.size_points = ARRAY_LENGTH (path->buf.points);
path->current_point = other->current_point;
path->last_move_point = other->last_move_point;
path->has_current_point = other->has_current_point;
path->needs_move_to = other->needs_move_to;
path->has_extents = other->has_extents;
path->has_curve_to = other->has_curve_to;
path->stroke_is_rectilinear = other->stroke_is_rectilinear;
path->fill_is_rectilinear = other->fill_is_rectilinear;
path->fill_maybe_region = other->fill_maybe_region;
path->fill_is_empty = other->fill_is_empty;
path->extents = other->extents;
path->buf.base.num_ops = other->buf.base.num_ops;
path->buf.base.num_points = other->buf.base.num_points;
memcpy (path->buf.op, other->buf.base.op,
other->buf.base.num_ops * sizeof (other->buf.op[0]));
memcpy (path->buf.points, other->buf.points,
other->buf.base.num_points * sizeof (other->buf.points[0]));
num_points = num_ops = 0;
for (other_buf = cairo_path_buf_next (cairo_path_head (other));
other_buf != cairo_path_head (other);
other_buf = cairo_path_buf_next (other_buf))
{
num_ops += other_buf->num_ops;
num_points += other_buf->num_points;
}
if (num_ops) {
buf = _cairo_path_buf_create (num_ops, num_points);
if (unlikely (buf == NULL)) {
_cairo_path_fixed_fini (path);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
for (other_buf = cairo_path_buf_next (cairo_path_head (other));
other_buf != cairo_path_head (other);
other_buf = cairo_path_buf_next (other_buf))
{
memcpy (buf->op + buf->num_ops, other_buf->op,
other_buf->num_ops * sizeof (buf->op[0]));
buf->num_ops += other_buf->num_ops;
memcpy (buf->points + buf->num_points, other_buf->points,
other_buf->num_points * sizeof (buf->points[0]));
buf->num_points += other_buf->num_points;
}
_cairo_path_fixed_add_buf (path, buf);
}
return CAIRO_STATUS_SUCCESS;
}
unsigned long
_cairo_path_fixed_hash (const cairo_path_fixed_t *path)
{
unsigned long hash = _CAIRO_HASH_INIT_VALUE;
const cairo_path_buf_t *buf;
unsigned int count;
count = 0;
cairo_path_foreach_buf_start (buf, path) {
hash = _cairo_hash_bytes (hash, buf->op,
buf->num_ops * sizeof (buf->op[0]));
count += buf->num_ops;
} cairo_path_foreach_buf_end (buf, path);
hash = _cairo_hash_bytes (hash, &count, sizeof (count));
count = 0;
cairo_path_foreach_buf_start (buf, path) {
hash = _cairo_hash_bytes (hash, buf->points,
buf->num_points * sizeof (buf->points[0]));
count += buf->num_points;
} cairo_path_foreach_buf_end (buf, path);
hash = _cairo_hash_bytes (hash, &count, sizeof (count));
return hash;
}
unsigned long
_cairo_path_fixed_size (const cairo_path_fixed_t *path)
{
const cairo_path_buf_t *buf;
int num_points, num_ops;
num_ops = num_points = 0;
cairo_path_foreach_buf_start (buf, path) {
num_ops += buf->num_ops;
num_points += buf->num_points;
} cairo_path_foreach_buf_end (buf, path);
return num_ops * sizeof (buf->op[0]) +
num_points * sizeof (buf->points[0]);
}
cairo_bool_t
_cairo_path_fixed_equal (const cairo_path_fixed_t *a,
const cairo_path_fixed_t *b)
{
const cairo_path_buf_t *buf_a, *buf_b;
const cairo_path_op_t *ops_a, *ops_b;
const cairo_point_t *points_a, *points_b;
int num_points_a, num_ops_a;
int num_points_b, num_ops_b;
if (a == b)
return TRUE;
/* use the flags to quickly differentiate based on contents */
if (a->has_curve_to != b->has_curve_to)
{
return FALSE;
}
if (a->extents.p1.x != b->extents.p1.x ||
a->extents.p1.y != b->extents.p1.y ||
a->extents.p2.x != b->extents.p2.x ||
a->extents.p2.y != b->extents.p2.y)
{
return FALSE;
}
num_ops_a = num_points_a = 0;
cairo_path_foreach_buf_start (buf_a, a) {
num_ops_a += buf_a->num_ops;
num_points_a += buf_a->num_points;
} cairo_path_foreach_buf_end (buf_a, a);
num_ops_b = num_points_b = 0;
cairo_path_foreach_buf_start (buf_b, b) {
num_ops_b += buf_b->num_ops;
num_points_b += buf_b->num_points;
} cairo_path_foreach_buf_end (buf_b, b);
if (num_ops_a == 0 && num_ops_b == 0)
return TRUE;
if (num_ops_a != num_ops_b || num_points_a != num_points_b)
return FALSE;
buf_a = cairo_path_head (a);
num_points_a = buf_a->num_points;
num_ops_a = buf_a->num_ops;
ops_a = buf_a->op;
points_a = buf_a->points;
buf_b = cairo_path_head (b);
num_points_b = buf_b->num_points;
num_ops_b = buf_b->num_ops;
ops_b = buf_b->op;
points_b = buf_b->points;
while (TRUE) {
int num_ops = MIN (num_ops_a, num_ops_b);
int num_points = MIN (num_points_a, num_points_b);
if (memcmp (ops_a, ops_b, num_ops * sizeof (cairo_path_op_t)))
return FALSE;
if (memcmp (points_a, points_b, num_points * sizeof (cairo_point_t)))
return FALSE;
num_ops_a -= num_ops;
ops_a += num_ops;
num_points_a -= num_points;
points_a += num_points;
if (num_ops_a == 0 || num_points_a == 0) {
if (num_ops_a || num_points_a)
return FALSE;
buf_a = cairo_path_buf_next (buf_a);
if (buf_a == cairo_path_head (a))
break;
num_points_a = buf_a->num_points;
num_ops_a = buf_a->num_ops;
ops_a = buf_a->op;
points_a = buf_a->points;
}
num_ops_b -= num_ops;
ops_b += num_ops;
num_points_b -= num_points;
points_b += num_points;
if (num_ops_b == 0 || num_points_b == 0) {
if (num_ops_b || num_points_b)
return FALSE;
buf_b = cairo_path_buf_next (buf_b);
if (buf_b == cairo_path_head (b))
break;
num_points_b = buf_b->num_points;
num_ops_b = buf_b->num_ops;
ops_b = buf_b->op;
points_b = buf_b->points;
}
}
return TRUE;
}
cairo_path_fixed_t *
_cairo_path_fixed_create (void)
{
cairo_path_fixed_t *path;
path = _cairo_malloc (sizeof (cairo_path_fixed_t));
if (!path) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return NULL;
}
_cairo_path_fixed_init (path);
return path;
}
void
_cairo_path_fixed_fini (cairo_path_fixed_t *path)
{
cairo_path_buf_t *buf;
buf = cairo_path_buf_next (cairo_path_head (path));
while (buf != cairo_path_head (path)) {
cairo_path_buf_t *this = buf;
buf = cairo_path_buf_next (buf);
_cairo_path_buf_destroy (this);
}
VG (VALGRIND_MAKE_MEM_UNDEFINED (path, sizeof (cairo_path_fixed_t)));
}
void
_cairo_path_fixed_destroy (cairo_path_fixed_t *path)
{
_cairo_path_fixed_fini (path);
free (path);
}
static cairo_path_op_t
_cairo_path_fixed_last_op (cairo_path_fixed_t *path)
{
cairo_path_buf_t *buf;
buf = cairo_path_tail (path);
assert (buf->num_ops != 0);
return buf->op[buf->num_ops - 1];
}
static inline const cairo_point_t *
_cairo_path_fixed_penultimate_point (cairo_path_fixed_t *path)
{
cairo_path_buf_t *buf;
buf = cairo_path_tail (path);
if (likely (buf->num_points >= 2)) {
return &buf->points[buf->num_points - 2];
} else {
cairo_path_buf_t *prev_buf = cairo_path_buf_prev (buf);
assert (prev_buf->num_points >= 2 - buf->num_points);
return &prev_buf->points[prev_buf->num_points - (2 - buf->num_points)];
}
}
static void
_cairo_path_fixed_drop_line_to (cairo_path_fixed_t *path)
{
cairo_path_buf_t *buf;
assert (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO);
buf = cairo_path_tail (path);
buf->num_points--;
buf->num_ops--;
}
cairo_status_t
_cairo_path_fixed_move_to (cairo_path_fixed_t *path,
cairo_fixed_t x,
cairo_fixed_t y)
{
_cairo_path_fixed_new_sub_path (path);
path->has_current_point = TRUE;
path->current_point.x = x;
path->current_point.y = y;
path->last_move_point = path->current_point;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_path_fixed_move_to_apply (cairo_path_fixed_t *path)
{
if (likely (! path->needs_move_to))
return CAIRO_STATUS_SUCCESS;
path->needs_move_to = FALSE;
if (path->has_extents) {
_cairo_box_add_point (&path->extents, &path->current_point);
} else {
_cairo_box_set (&path->extents, &path->current_point, &path->current_point);
path->has_extents = TRUE;
}
if (path->fill_maybe_region) {
path->fill_maybe_region = _cairo_fixed_is_integer (path->current_point.x) &&
_cairo_fixed_is_integer (path->current_point.y);
}
path->last_move_point = path->current_point;
return _cairo_path_fixed_add (path, CAIRO_PATH_OP_MOVE_TO, &path->current_point, 1);
}
void
_cairo_path_fixed_new_sub_path (cairo_path_fixed_t *path)
{
if (! path->needs_move_to) {
/* If the current subpath doesn't need_move_to, it contains at least one command */
if (path->fill_is_rectilinear) {
/* Implicitly close for fill */
path->fill_is_rectilinear = path->current_point.x == path->last_move_point.x ||
path->current_point.y == path->last_move_point.y;
path->fill_maybe_region &= path->fill_is_rectilinear;
}
path->needs_move_to = TRUE;
}
path->has_current_point = FALSE;
}
cairo_status_t
_cairo_path_fixed_rel_move_to (cairo_path_fixed_t *path,
cairo_fixed_t dx,
cairo_fixed_t dy)
{
if (unlikely (! path->has_current_point))
return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
return _cairo_path_fixed_move_to (path,
path->current_point.x + dx,
path->current_point.y + dy);
}
cairo_status_t
_cairo_path_fixed_line_to (cairo_path_fixed_t *path,
cairo_fixed_t x,
cairo_fixed_t y)
{
cairo_status_t status;
cairo_point_t point;
point.x = x;
point.y = y;
/* When there is not yet a current point, the line_to operation
* becomes a move_to instead. Note: We have to do this by
* explicitly calling into _cairo_path_fixed_move_to to ensure
* that the last_move_point state is updated properly.
*/
if (! path->has_current_point)
return _cairo_path_fixed_move_to (path, point.x, point.y);
status = _cairo_path_fixed_move_to_apply (path);
if (unlikely (status))
return status;
/* If the previous op was but the initial MOVE_TO and this segment
* is degenerate, then we can simply skip this point. Note that
* a move-to followed by a degenerate line-to is a valid path for
* stroking, but at all other times is simply a degenerate segment.
*/
if (_cairo_path_fixed_last_op (path) != CAIRO_PATH_OP_MOVE_TO) {
if (x == path->current_point.x && y == path->current_point.y)
return CAIRO_STATUS_SUCCESS;
}
/* If the previous op was also a LINE_TO with the same gradient,
* then just change its end-point rather than adding a new op.
*/
if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO) {
const cairo_point_t *p;
p = _cairo_path_fixed_penultimate_point (path);
if (p->x == path->current_point.x && p->y == path->current_point.y) {
/* previous line element was degenerate, replace */
_cairo_path_fixed_drop_line_to (path);
} else {
cairo_slope_t prev, self;
_cairo_slope_init (&prev, p, &path->current_point);
_cairo_slope_init (&self, &path->current_point, &point);
if (_cairo_slope_equal (&prev, &self) &&
/* cannot trim anti-parallel segments whilst stroking */
! _cairo_slope_backwards (&prev, &self))
{
_cairo_path_fixed_drop_line_to (path);
/* In this case the flags might be more restrictive than
* what we actually need.
* When changing the flags definition we should check if
* changing the line_to point can affect them.
*/
}
}
}
if (path->stroke_is_rectilinear) {
path->stroke_is_rectilinear = path->current_point.x == x ||
path->current_point.y == y;
path->fill_is_rectilinear &= path->stroke_is_rectilinear;
path->fill_maybe_region &= path->fill_is_rectilinear;
if (path->fill_maybe_region) {
path->fill_maybe_region = _cairo_fixed_is_integer (x) &&
_cairo_fixed_is_integer (y);
}
if (path->fill_is_empty) {
path->fill_is_empty = path->current_point.x == x &&
path->current_point.y == y;
}
}
path->current_point = point;
_cairo_box_add_point (&path->extents, &point);
return _cairo_path_fixed_add (path, CAIRO_PATH_OP_LINE_TO, &point, 1);
}
cairo_status_t
_cairo_path_fixed_rel_line_to (cairo_path_fixed_t *path,
cairo_fixed_t dx,
cairo_fixed_t dy)
{
if (unlikely (! path->has_current_point))
return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
return _cairo_path_fixed_line_to (path,
path->current_point.x + dx,
path->current_point.y + dy);
}
cairo_status_t
_cairo_path_fixed_curve_to (cairo_path_fixed_t *path,
cairo_fixed_t x0, cairo_fixed_t y0,
cairo_fixed_t x1, cairo_fixed_t y1,
cairo_fixed_t x2, cairo_fixed_t y2)
{
cairo_status_t status;
cairo_point_t point[3];
/* If this curves does not move, replace it with a line-to.
* This frequently happens with rounded-rectangles and r==0.
*/
if (path->current_point.x == x2 && path->current_point.y == y2) {
if (x1 == x2 && x0 == x2 && y1 == y2 && y0 == y2)
return _cairo_path_fixed_line_to (path, x2, y2);
/* We may want to check for the absence of a cusp, in which case
* we can also replace the curve-to with a line-to.
*/
}
/* make sure subpaths are started properly */
if (! path->has_current_point) {
status = _cairo_path_fixed_move_to (path, x0, y0);
assert (status == CAIRO_STATUS_SUCCESS);
}
status = _cairo_path_fixed_move_to_apply (path);
if (unlikely (status))
return status;
/* If the previous op was a degenerate LINE_TO, drop it. */
if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO) {
const cairo_point_t *p;
p = _cairo_path_fixed_penultimate_point (path);
if (p->x == path->current_point.x && p->y == path->current_point.y) {
/* previous line element was degenerate, replace */
_cairo_path_fixed_drop_line_to (path);
}
}
point[0].x = x0; point[0].y = y0;
point[1].x = x1; point[1].y = y1;
point[2].x = x2; point[2].y = y2;
_cairo_box_add_curve_to (&path->extents, &path->current_point,
&point[0], &point[1], &point[2]);
path->current_point = point[2];
path->has_curve_to = TRUE;
path->stroke_is_rectilinear = FALSE;
path->fill_is_rectilinear = FALSE;
path->fill_maybe_region = FALSE;
path->fill_is_empty = FALSE;
return _cairo_path_fixed_add (path, CAIRO_PATH_OP_CURVE_TO, point, 3);
}
cairo_status_t
_cairo_path_fixed_rel_curve_to (cairo_path_fixed_t *path,
cairo_fixed_t dx0, cairo_fixed_t dy0,
cairo_fixed_t dx1, cairo_fixed_t dy1,
cairo_fixed_t dx2, cairo_fixed_t dy2)
{
if (unlikely (! path->has_current_point))
return _cairo_error (CAIRO_STATUS_NO_CURRENT_POINT);
return _cairo_path_fixed_curve_to (path,
path->current_point.x + dx0,
path->current_point.y + dy0,
path->current_point.x + dx1,
path->current_point.y + dy1,
path->current_point.x + dx2,
path->current_point.y + dy2);
}
cairo_status_t
_cairo_path_fixed_close_path (cairo_path_fixed_t *path)
{
cairo_status_t status;
if (! path->has_current_point)
return CAIRO_STATUS_SUCCESS;
/*
* Add a line_to, to compute flags and solve any degeneracy.
* It will be removed later (if it was actually added).
*/
status = _cairo_path_fixed_line_to (path,
path->last_move_point.x,
path->last_move_point.y);
if (unlikely (status))
return status;
/*
* If the command used to close the path is a line_to, drop it.
* We must check that last command is actually a line_to,
* because the path could have been closed with a curve_to (and
* the previous line_to not added as it would be degenerate).
*/
if (_cairo_path_fixed_last_op (path) == CAIRO_PATH_OP_LINE_TO)
_cairo_path_fixed_drop_line_to (path);
path->needs_move_to = TRUE; /* After close_path, add an implicit move_to */
return _cairo_path_fixed_add (path, CAIRO_PATH_OP_CLOSE_PATH, NULL, 0);
}
cairo_bool_t
_cairo_path_fixed_get_current_point (cairo_path_fixed_t *path,
cairo_fixed_t *x,
cairo_fixed_t *y)
{
if (! path->has_current_point)
return FALSE;
*x = path->current_point.x;
*y = path->current_point.y;
return TRUE;
}
static cairo_status_t
_cairo_path_fixed_add (cairo_path_fixed_t *path,
cairo_path_op_t op,
const cairo_point_t *points,
int num_points)
{
cairo_path_buf_t *buf = cairo_path_tail (path);
if (buf->num_ops + 1 > buf->size_ops ||
buf->num_points + num_points > buf->size_points)
{
buf = _cairo_path_buf_create (buf->num_ops * 2, buf->num_points * 2);
if (unlikely (buf == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
_cairo_path_fixed_add_buf (path, buf);
}
if (WATCH_PATH) {
const char *op_str[] = {
"move-to",
"line-to",
"curve-to",
"close-path",
};
char buf[1024];
int len = 0;
int i;
len += snprintf (buf + len, sizeof (buf), "[");
for (i = 0; i < num_points; i++) {
if (i != 0)
len += snprintf (buf + len, sizeof (buf), " ");
len += snprintf (buf + len, sizeof (buf), "(%f, %f)",
_cairo_fixed_to_double (points[i].x),
_cairo_fixed_to_double (points[i].y));
}
len += snprintf (buf + len, sizeof (buf), "]");
#define STRINGIFYFLAG(x) (path->x ? #x " " : "")
fprintf (stderr,
"_cairo_path_fixed_add (%s, %s) [%s%s%s%s%s%s%s%s]\n",
op_str[(int) op], buf,
STRINGIFYFLAG(has_current_point),
STRINGIFYFLAG(needs_move_to),
STRINGIFYFLAG(has_extents),
STRINGIFYFLAG(has_curve_to),
STRINGIFYFLAG(stroke_is_rectilinear),
STRINGIFYFLAG(fill_is_rectilinear),
STRINGIFYFLAG(fill_is_empty),
STRINGIFYFLAG(fill_maybe_region)
);
#undef STRINGIFYFLAG
}
_cairo_path_buf_add_op (buf, op);
_cairo_path_buf_add_points (buf, points, num_points);
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_path_fixed_add_buf (cairo_path_fixed_t *path,
cairo_path_buf_t *buf)
{
cairo_list_add_tail (&buf->link, &cairo_path_head (path)->link);
}
COMPILE_TIME_ASSERT (sizeof (cairo_path_op_t) == 1);
static cairo_path_buf_t *
_cairo_path_buf_create (int size_ops, int size_points)
{
cairo_path_buf_t *buf;
/* adjust size_ops to ensure that buf->points is naturally aligned */
size_ops += sizeof (double) - ((sizeof (cairo_path_buf_t) + size_ops) % sizeof (double));
buf = _cairo_malloc_ab_plus_c (size_points, sizeof (cairo_point_t), size_ops + sizeof (cairo_path_buf_t));
if (buf) {
buf->num_ops = 0;
buf->num_points = 0;
buf->size_ops = size_ops;
buf->size_points = size_points;
buf->op = (cairo_path_op_t *) (buf + 1);
buf->points = (cairo_point_t *) (buf->op + size_ops);
}
return buf;
}
static void
_cairo_path_buf_destroy (cairo_path_buf_t *buf)
{
free (buf);
}
static void
_cairo_path_buf_add_op (cairo_path_buf_t *buf,
cairo_path_op_t op)
{
buf->op[buf->num_ops++] = op;
}
static void
_cairo_path_buf_add_points (cairo_path_buf_t *buf,
const cairo_point_t *points,
int num_points)
{
if (num_points == 0)
return;
memcpy (buf->points + buf->num_points,
points,
sizeof (points[0]) * num_points);
buf->num_points += num_points;
}
cairo_status_t
_cairo_path_fixed_interpret (const cairo_path_fixed_t *path,
cairo_path_fixed_move_to_func_t *move_to,
cairo_path_fixed_line_to_func_t *line_to,
cairo_path_fixed_curve_to_func_t *curve_to,
cairo_path_fixed_close_path_func_t *close_path,
void *closure)
{
const cairo_path_buf_t *buf;
cairo_status_t status;
cairo_path_foreach_buf_start (buf, path) {
const cairo_point_t *points = buf->points;
unsigned int i;
for (i = 0; i < buf->num_ops; i++) {
switch (buf->op[i]) {
case CAIRO_PATH_OP_MOVE_TO:
status = (*move_to) (closure, &points[0]);
points += 1;
break;
case CAIRO_PATH_OP_LINE_TO:
status = (*line_to) (closure, &points[0]);
points += 1;
break;
case CAIRO_PATH_OP_CURVE_TO:
status = (*curve_to) (closure, &points[0], &points[1], &points[2]);
points += 3;
break;
default:
ASSERT_NOT_REACHED;
case CAIRO_PATH_OP_CLOSE_PATH:
status = (*close_path) (closure);
break;
}
if (unlikely (status))
return status;
}
} cairo_path_foreach_buf_end (buf, path);
if (path->needs_move_to && path->has_current_point)
return (*move_to) (closure, &path->current_point);
return CAIRO_STATUS_SUCCESS;
}
typedef struct _cairo_path_fixed_append_closure {
cairo_point_t offset;
cairo_path_fixed_t *path;
} cairo_path_fixed_append_closure_t;
static cairo_status_t
_append_move_to (void *abstract_closure,
const cairo_point_t *point)
{
cairo_path_fixed_append_closure_t *closure = abstract_closure;
return _cairo_path_fixed_move_to (closure->path,
point->x + closure->offset.x,
point->y + closure->offset.y);
}
static cairo_status_t
_append_line_to (void *abstract_closure,
const cairo_point_t *point)
{
cairo_path_fixed_append_closure_t *closure = abstract_closure;
return _cairo_path_fixed_line_to (closure->path,
point->x + closure->offset.x,
point->y + closure->offset.y);
}
static cairo_status_t
_append_curve_to (void *abstract_closure,
const cairo_point_t *p0,
const cairo_point_t *p1,
const cairo_point_t *p2)
{
cairo_path_fixed_append_closure_t *closure = abstract_closure;
return _cairo_path_fixed_curve_to (closure->path,
p0->x + closure->offset.x,
p0->y + closure->offset.y,
p1->x + closure->offset.x,
p1->y + closure->offset.y,
p2->x + closure->offset.x,
p2->y + closure->offset.y);
}
static cairo_status_t
_append_close_path (void *abstract_closure)
{
cairo_path_fixed_append_closure_t *closure = abstract_closure;
return _cairo_path_fixed_close_path (closure->path);
}
cairo_status_t
_cairo_path_fixed_append (cairo_path_fixed_t *path,
const cairo_path_fixed_t *other,
cairo_fixed_t tx,
cairo_fixed_t ty)
{
cairo_path_fixed_append_closure_t closure;
closure.path = path;
closure.offset.x = tx;
closure.offset.y = ty;
return _cairo_path_fixed_interpret (other,
_append_move_to,
_append_line_to,
_append_curve_to,
_append_close_path,
&closure);
}
static void
_cairo_path_fixed_offset_and_scale (cairo_path_fixed_t *path,
cairo_fixed_t offx,
cairo_fixed_t offy,
cairo_fixed_t scalex,
cairo_fixed_t scaley)
{
cairo_path_buf_t *buf;
unsigned int i;
if (scalex == CAIRO_FIXED_ONE && scaley == CAIRO_FIXED_ONE) {
_cairo_path_fixed_translate (path, offx, offy);
return;
}
path->last_move_point.x = _cairo_fixed_mul (scalex, path->last_move_point.x) + offx;
path->last_move_point.y = _cairo_fixed_mul (scaley, path->last_move_point.y) + offy;
path->current_point.x = _cairo_fixed_mul (scalex, path->current_point.x) + offx;
path->current_point.y = _cairo_fixed_mul (scaley, path->current_point.y) + offy;
path->fill_maybe_region = TRUE;
cairo_path_foreach_buf_start (buf, path) {
for (i = 0; i < buf->num_points; i++) {
if (scalex != CAIRO_FIXED_ONE)
buf->points[i].x = _cairo_fixed_mul (buf->points[i].x, scalex);
buf->points[i].x += offx;
if (scaley != CAIRO_FIXED_ONE)
buf->points[i].y = _cairo_fixed_mul (buf->points[i].y, scaley);
buf->points[i].y += offy;
if (path->fill_maybe_region) {
path->fill_maybe_region = _cairo_fixed_is_integer (buf->points[i].x) &&
_cairo_fixed_is_integer (buf->points[i].y);
}
}
} cairo_path_foreach_buf_end (buf, path);
path->fill_maybe_region &= path->fill_is_rectilinear;
path->extents.p1.x = _cairo_fixed_mul (scalex, path->extents.p1.x) + offx;
path->extents.p2.x = _cairo_fixed_mul (scalex, path->extents.p2.x) + offx;
if (scalex < 0) {
cairo_fixed_t t = path->extents.p1.x;
path->extents.p1.x = path->extents.p2.x;
path->extents.p2.x = t;
}
path->extents.p1.y = _cairo_fixed_mul (scaley, path->extents.p1.y) + offy;
path->extents.p2.y = _cairo_fixed_mul (scaley, path->extents.p2.y) + offy;
if (scaley < 0) {
cairo_fixed_t t = path->extents.p1.y;
path->extents.p1.y = path->extents.p2.y;
path->extents.p2.y = t;
}
}
void
_cairo_path_fixed_translate (cairo_path_fixed_t *path,
cairo_fixed_t offx,
cairo_fixed_t offy)
{
cairo_path_buf_t *buf;
unsigned int i;
if (offx == 0 && offy == 0)
return;
path->last_move_point.x += offx;
path->last_move_point.y += offy;
path->current_point.x += offx;
path->current_point.y += offy;
path->fill_maybe_region = TRUE;
cairo_path_foreach_buf_start (buf, path) {
for (i = 0; i < buf->num_points; i++) {
buf->points[i].x += offx;
buf->points[i].y += offy;
if (path->fill_maybe_region) {
path->fill_maybe_region = _cairo_fixed_is_integer (buf->points[i].x) &&
_cairo_fixed_is_integer (buf->points[i].y);
}
}
} cairo_path_foreach_buf_end (buf, path);
path->fill_maybe_region &= path->fill_is_rectilinear;
path->extents.p1.x += offx;
path->extents.p1.y += offy;
path->extents.p2.x += offx;
path->extents.p2.y += offy;
}
static inline void
_cairo_path_fixed_transform_point (cairo_point_t *p,
const cairo_matrix_t *matrix)
{
double dx, dy;
dx = _cairo_fixed_to_double (p->x);
dy = _cairo_fixed_to_double (p->y);
cairo_matrix_transform_point (matrix, &dx, &dy);
p->x = _cairo_fixed_from_double (dx);
p->y = _cairo_fixed_from_double (dy);
}
/**
* _cairo_path_fixed_transform:
* @path: a #cairo_path_fixed_t to be transformed
* @matrix: a #cairo_matrix_t
*
* Transform the fixed-point path according to the given matrix.
* There is a fast path for the case where @matrix has no rotation
* or shear.
**/
void
_cairo_path_fixed_transform (cairo_path_fixed_t *path,
const cairo_matrix_t *matrix)
{
cairo_box_t extents;
cairo_point_t point;
cairo_path_buf_t *buf;
unsigned int i;
if (matrix->yx == 0.0 && matrix->xy == 0.0) {
/* Fast path for the common case of scale+transform */
_cairo_path_fixed_offset_and_scale (path,
_cairo_fixed_from_double (matrix->x0),
_cairo_fixed_from_double (matrix->y0),
_cairo_fixed_from_double (matrix->xx),
_cairo_fixed_from_double (matrix->yy));
return;
}
_cairo_path_fixed_transform_point (&path->last_move_point, matrix);
_cairo_path_fixed_transform_point (&path->current_point, matrix);
buf = cairo_path_head (path);
if (buf->num_points == 0)
return;
extents = path->extents;
point = buf->points[0];
_cairo_path_fixed_transform_point (&point, matrix);
_cairo_box_set (&path->extents, &point, &point);
cairo_path_foreach_buf_start (buf, path) {
for (i = 0; i < buf->num_points; i++) {
_cairo_path_fixed_transform_point (&buf->points[i], matrix);
_cairo_box_add_point (&path->extents, &buf->points[i]);
}
} cairo_path_foreach_buf_end (buf, path);
if (path->has_curve_to) {
cairo_bool_t is_tight;
_cairo_matrix_transform_bounding_box_fixed (matrix, &extents, &is_tight);
if (!is_tight) {
cairo_bool_t has_extents;
has_extents = _cairo_path_bounder_extents (path, &extents);
assert (has_extents);
}
path->extents = extents;
}
/* flags might become more strict than needed */
path->stroke_is_rectilinear = FALSE;
path->fill_is_rectilinear = FALSE;
path->fill_is_empty = FALSE;
path->fill_maybe_region = FALSE;
}
/* Closure for path flattening */
typedef struct cairo_path_flattener {
double tolerance;
cairo_point_t current_point;
cairo_path_fixed_move_to_func_t *move_to;
cairo_path_fixed_line_to_func_t *line_to;
cairo_path_fixed_close_path_func_t *close_path;
void *closure;
} cpf_t;
static cairo_status_t
_cpf_move_to (void *closure,
const cairo_point_t *point)
{
cpf_t *cpf = closure;
cpf->current_point = *point;
return cpf->move_to (cpf->closure, point);
}
static cairo_status_t
_cpf_line_to (void *closure,
const cairo_point_t *point)
{
cpf_t *cpf = closure;
cpf->current_point = *point;
return cpf->line_to (cpf->closure, point);
}
static cairo_status_t
_cpf_curve_to (void *closure,
const cairo_point_t *p1,
const cairo_point_t *p2,
const cairo_point_t *p3)
{
cpf_t *cpf = closure;
cairo_spline_t spline;
cairo_point_t *p0 = &cpf->current_point;
if (! _cairo_spline_init (&spline,
(cairo_spline_add_point_func_t)cpf->line_to,
cpf->closure,
p0, p1, p2, p3))
{
return _cpf_line_to (closure, p3);
}
cpf->current_point = *p3;
return _cairo_spline_decompose (&spline, cpf->tolerance);
}
static cairo_status_t
_cpf_close_path (void *closure)
{
cpf_t *cpf = closure;
return cpf->close_path (cpf->closure);
}
cairo_status_t
_cairo_path_fixed_interpret_flat (const cairo_path_fixed_t *path,
cairo_path_fixed_move_to_func_t *move_to,
cairo_path_fixed_line_to_func_t *line_to,
cairo_path_fixed_close_path_func_t *close_path,
void *closure,
double tolerance)
{
cpf_t flattener;
if (! path->has_curve_to) {
return _cairo_path_fixed_interpret (path,
move_to,
line_to,
NULL,
close_path,
closure);
}
flattener.tolerance = tolerance;
flattener.move_to = move_to;
flattener.line_to = line_to;
flattener.close_path = close_path;
flattener.closure = closure;
return _cairo_path_fixed_interpret (path,
_cpf_move_to,
_cpf_line_to,
_cpf_curve_to,
_cpf_close_path,
&flattener);
}
static inline void
_canonical_box (cairo_box_t *box,
const cairo_point_t *p1,
const cairo_point_t *p2)
{
if (p1->x <= p2->x) {
box->p1.x = p1->x;
box->p2.x = p2->x;
} else {
box->p1.x = p2->x;
box->p2.x = p1->x;
}
if (p1->y <= p2->y) {
box->p1.y = p1->y;
box->p2.y = p2->y;
} else {
box->p1.y = p2->y;
box->p2.y = p1->y;
}
}
static inline cairo_bool_t
_path_is_quad (const cairo_path_fixed_t *path)
{
const cairo_path_buf_t *buf = cairo_path_head (path);
/* Do we have the right number of ops? */
if (buf->num_ops < 4 || buf->num_ops > 6)
return FALSE;
/* Check whether the ops are those that would be used for a rectangle */
if (buf->op[0] != CAIRO_PATH_OP_MOVE_TO ||
buf->op[1] != CAIRO_PATH_OP_LINE_TO ||
buf->op[2] != CAIRO_PATH_OP_LINE_TO ||
buf->op[3] != CAIRO_PATH_OP_LINE_TO)
{
return FALSE;
}
/* we accept an implicit close for filled paths */
if (buf->num_ops > 4) {
/* Now, there are choices. The rectangle might end with a LINE_TO
* (to the original point), but this isn't required. If it
* doesn't, then it must end with a CLOSE_PATH. */
if (buf->op[4] == CAIRO_PATH_OP_LINE_TO) {
if (buf->points[4].x != buf->points[0].x ||
buf->points[4].y != buf->points[0].y)
return FALSE;
} else if (buf->op[4] != CAIRO_PATH_OP_CLOSE_PATH) {
return FALSE;
}
if (buf->num_ops == 6) {
/* A trailing CLOSE_PATH or MOVE_TO is ok */
if (buf->op[5] != CAIRO_PATH_OP_MOVE_TO &&
buf->op[5] != CAIRO_PATH_OP_CLOSE_PATH)
return FALSE;
}
}
return TRUE;
}
static inline cairo_bool_t
_points_form_rect (const cairo_point_t *points)
{
if (points[0].y == points[1].y &&
points[1].x == points[2].x &&
points[2].y == points[3].y &&
points[3].x == points[0].x)
return TRUE;
if (points[0].x == points[1].x &&
points[1].y == points[2].y &&
points[2].x == points[3].x &&
points[3].y == points[0].y)
return TRUE;
return FALSE;
}
/*
* Check whether the given path contains a single rectangle.
*/
cairo_bool_t
_cairo_path_fixed_is_box (const cairo_path_fixed_t *path,
cairo_box_t *box)
{
const cairo_path_buf_t *buf;
if (! path->fill_is_rectilinear)
return FALSE;
if (! _path_is_quad (path))
return FALSE;
buf = cairo_path_head (path);
if (_points_form_rect (buf->points)) {
_canonical_box (box, &buf->points[0], &buf->points[2]);
return TRUE;
}
return FALSE;
}
/* Determine whether two lines A->B and C->D intersect based on the
* algorithm described here: http://paulbourke.net/geometry/pointlineplane/ */
static inline cairo_bool_t
_lines_intersect_or_are_coincident (cairo_point_t a,
cairo_point_t b,
cairo_point_t c,
cairo_point_t d)
{
cairo_int64_t numerator_a, numerator_b, denominator;
cairo_bool_t denominator_negative;
denominator = _cairo_int64_sub (_cairo_int32x32_64_mul (d.y - c.y, b.x - a.x),
_cairo_int32x32_64_mul (d.x - c.x, b.y - a.y));
numerator_a = _cairo_int64_sub (_cairo_int32x32_64_mul (d.x - c.x, a.y - c.y),
_cairo_int32x32_64_mul (d.y - c.y, a.x - c.x));
numerator_b = _cairo_int64_sub (_cairo_int32x32_64_mul (b.x - a.x, a.y - c.y),
_cairo_int32x32_64_mul (b.y - a.y, a.x - c.x));
if (_cairo_int64_is_zero (denominator)) {
/* If the denominator and numerators are both zero,
* the lines are coincident. */
if (_cairo_int64_is_zero (numerator_a) && _cairo_int64_is_zero (numerator_b))
return TRUE;
/* Otherwise, a zero denominator indicates the lines are
* parallel and never intersect. */
return FALSE;
}
/* The lines intersect if both quotients are between 0 and 1 (exclusive). */
/* We first test whether either quotient is a negative number. */
denominator_negative = _cairo_int64_negative (denominator);
if (_cairo_int64_negative (numerator_a) ^ denominator_negative)
return FALSE;
if (_cairo_int64_negative (numerator_b) ^ denominator_negative)
return FALSE;
/* A zero quotient indicates an "intersection" at an endpoint, which
* we aren't considering a true intersection. */
if (_cairo_int64_is_zero (numerator_a) || _cairo_int64_is_zero (numerator_b))
return FALSE;
/* If the absolute value of the numerator is larger than or equal to the
* denominator the result of the division would be greater than or equal
* to one. */
if (! denominator_negative) {
if (! _cairo_int64_lt (numerator_a, denominator) ||
! _cairo_int64_lt (numerator_b, denominator))
return FALSE;
} else {
if (! _cairo_int64_lt (denominator, numerator_a) ||
! _cairo_int64_lt (denominator, numerator_b))
return FALSE;
}
return TRUE;
}
cairo_bool_t
_cairo_path_fixed_is_simple_quad (const cairo_path_fixed_t *path)
{
const cairo_point_t *points;
if (! _path_is_quad (path))
return FALSE;
points = cairo_path_head (path)->points;
if (_points_form_rect (points))
return TRUE;
if (_lines_intersect_or_are_coincident (points[0], points[1],
points[3], points[2]))
return FALSE;
if (_lines_intersect_or_are_coincident (points[0], points[3],
points[1], points[2]))
return FALSE;
return TRUE;
}
cairo_bool_t
_cairo_path_fixed_is_stroke_box (const cairo_path_fixed_t *path,
cairo_box_t *box)
{
const cairo_path_buf_t *buf = cairo_path_head (path);
if (! path->fill_is_rectilinear)
return FALSE;
/* Do we have the right number of ops? */
if (buf->num_ops != 5)
return FALSE;
/* Check whether the ops are those that would be used for a rectangle */
if (buf->op[0] != CAIRO_PATH_OP_MOVE_TO ||
buf->op[1] != CAIRO_PATH_OP_LINE_TO ||
buf->op[2] != CAIRO_PATH_OP_LINE_TO ||
buf->op[3] != CAIRO_PATH_OP_LINE_TO ||
buf->op[4] != CAIRO_PATH_OP_CLOSE_PATH)
{
return FALSE;
}
/* Ok, we may have a box, if the points line up */
if (buf->points[0].y == buf->points[1].y &&
buf->points[1].x == buf->points[2].x &&
buf->points[2].y == buf->points[3].y &&
buf->points[3].x == buf->points[0].x)
{
_canonical_box (box, &buf->points[0], &buf->points[2]);
return TRUE;
}
if (buf->points[0].x == buf->points[1].x &&
buf->points[1].y == buf->points[2].y &&
buf->points[2].x == buf->points[3].x &&
buf->points[3].y == buf->points[0].y)
{
_canonical_box (box, &buf->points[0], &buf->points[2]);
return TRUE;
}
return FALSE;
}
/*
* Check whether the given path contains a single rectangle
* that is logically equivalent to:
* <informalexample><programlisting>
* cairo_move_to (cr, x, y);
* cairo_rel_line_to (cr, width, 0);
* cairo_rel_line_to (cr, 0, height);
* cairo_rel_line_to (cr, -width, 0);
* cairo_close_path (cr);
* </programlisting></informalexample>
*/
cairo_bool_t
_cairo_path_fixed_is_rectangle (const cairo_path_fixed_t *path,
cairo_box_t *box)
{
const cairo_path_buf_t *buf;
if (! _cairo_path_fixed_is_box (path, box))
return FALSE;
/* This check is valid because the current implementation of
* _cairo_path_fixed_is_box () only accepts rectangles like:
* move,line,line,line[,line|close[,close|move]]. */
buf = cairo_path_head (path);
if (buf->num_ops > 4)
return TRUE;
return FALSE;
}
void
_cairo_path_fixed_iter_init (cairo_path_fixed_iter_t *iter,
const cairo_path_fixed_t *path)
{
iter->first = iter->buf = cairo_path_head (path);
iter->n_op = 0;
iter->n_point = 0;
}
static cairo_bool_t
_cairo_path_fixed_iter_next_op (cairo_path_fixed_iter_t *iter)
{
if (++iter->n_op >= iter->buf->num_ops) {
iter->buf = cairo_path_buf_next (iter->buf);
if (iter->buf == iter->first) {
iter->buf = NULL;
return FALSE;
}
iter->n_op = 0;
iter->n_point = 0;
}
return TRUE;
}
cairo_bool_t
_cairo_path_fixed_iter_is_fill_box (cairo_path_fixed_iter_t *_iter,
cairo_box_t *box)
{
cairo_point_t points[5];
cairo_path_fixed_iter_t iter;
if (_iter->buf == NULL)
return FALSE;
iter = *_iter;
if (iter.n_op == iter.buf->num_ops && ! _cairo_path_fixed_iter_next_op (&iter))
return FALSE;
/* Check whether the ops are those that would be used for a rectangle */
if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_MOVE_TO)
return FALSE;
points[0] = iter.buf->points[iter.n_point++];
if (! _cairo_path_fixed_iter_next_op (&iter))
return FALSE;
if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_LINE_TO)
return FALSE;
points[1] = iter.buf->points[iter.n_point++];
if (! _cairo_path_fixed_iter_next_op (&iter))
return FALSE;
/* a horizontal/vertical closed line is also a degenerate rectangle */
switch (iter.buf->op[iter.n_op]) {
case CAIRO_PATH_OP_CLOSE_PATH:
_cairo_path_fixed_iter_next_op (&iter);
case CAIRO_PATH_OP_MOVE_TO: /* implicit close */
box->p1 = box->p2 = points[0];
*_iter = iter;
return TRUE;
default:
return FALSE;
case CAIRO_PATH_OP_LINE_TO:
break;
}
points[2] = iter.buf->points[iter.n_point++];
if (! _cairo_path_fixed_iter_next_op (&iter))
return FALSE;
if (iter.buf->op[iter.n_op] != CAIRO_PATH_OP_LINE_TO)
return FALSE;
points[3] = iter.buf->points[iter.n_point++];
/* Now, there are choices. The rectangle might end with a LINE_TO
* (to the original point), but this isn't required. If it
* doesn't, then it must end with a CLOSE_PATH (which may be implicit). */
if (! _cairo_path_fixed_iter_next_op (&iter)) {
/* implicit close due to fill */
} else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_LINE_TO) {
points[4] = iter.buf->points[iter.n_point++];
if (points[4].x != points[0].x || points[4].y != points[0].y)
return FALSE;
_cairo_path_fixed_iter_next_op (&iter);
} else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_CLOSE_PATH) {
_cairo_path_fixed_iter_next_op (&iter);
} else if (iter.buf->op[iter.n_op] == CAIRO_PATH_OP_MOVE_TO) {
/* implicit close-path due to new-sub-path */
} else {
return FALSE;
}
/* Ok, we may have a box, if the points line up */
if (points[0].y == points[1].y &&
points[1].x == points[2].x &&
points[2].y == points[3].y &&
points[3].x == points[0].x)
{
box->p1 = points[0];
box->p2 = points[2];
*_iter = iter;
return TRUE;
}
if (points[0].x == points[1].x &&
points[1].y == points[2].y &&
points[2].x == points[3].x &&
points[3].y == points[0].y)
{
box->p1 = points[1];
box->p2 = points[3];
*_iter = iter;
return TRUE;
}
return FALSE;
}
cairo_bool_t
_cairo_path_fixed_iter_at_end (const cairo_path_fixed_iter_t *iter)
{
if (iter->buf == NULL)
return TRUE;
return iter->n_op == iter->buf->num_ops;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-in-fill.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2008 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Chris Wilson.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-path-fixed-private.h"
typedef struct cairo_in_fill {
double tolerance;
cairo_bool_t on_edge;
int winding;
cairo_fixed_t x, y;
cairo_bool_t has_current_point;
cairo_point_t current_point;
cairo_point_t first_point;
} cairo_in_fill_t;
static void
_cairo_in_fill_init (cairo_in_fill_t *in_fill,
double tolerance,
double x,
double y)
{
in_fill->on_edge = FALSE;
in_fill->winding = 0;
in_fill->tolerance = tolerance;
in_fill->x = _cairo_fixed_from_double (x);
in_fill->y = _cairo_fixed_from_double (y);
in_fill->has_current_point = FALSE;
in_fill->current_point.x = 0;
in_fill->current_point.y = 0;
}
static void
_cairo_in_fill_fini (cairo_in_fill_t *in_fill)
{
}
static int
edge_compare_for_y_against_x (const cairo_point_t *p1,
const cairo_point_t *p2,
cairo_fixed_t y,
cairo_fixed_t x)
{
cairo_fixed_t adx, ady;
cairo_fixed_t dx, dy;
cairo_int64_t L, R;
adx = p2->x - p1->x;
dx = x - p1->x;
if (adx == 0)
return -dx;
if ((adx ^ dx) < 0)
return adx;
dy = y - p1->y;
ady = p2->y - p1->y;
L = _cairo_int32x32_64_mul (dy, adx);
R = _cairo_int32x32_64_mul (dx, ady);
return _cairo_int64_cmp (L, R);
}
static void
_cairo_in_fill_add_edge (cairo_in_fill_t *in_fill,
const cairo_point_t *p1,
const cairo_point_t *p2)
{
int dir;
if (in_fill->on_edge)
return;
/* count the number of edge crossing to -∞ */
dir = 1;
if (p2->y < p1->y) {
const cairo_point_t *tmp;
tmp = p1;
p1 = p2;
p2 = tmp;
dir = -1;
}
/* First check whether the query is on an edge */
if ((p1->x == in_fill->x && p1->y == in_fill->y) ||
(p2->x == in_fill->x && p2->y == in_fill->y) ||
(! (p2->y < in_fill->y || p1->y > in_fill->y ||
(p1->x > in_fill->x && p2->x > in_fill->x) ||
(p1->x < in_fill->x && p2->x < in_fill->x)) &&
edge_compare_for_y_against_x (p1, p2, in_fill->y, in_fill->x) == 0))
{
in_fill->on_edge = TRUE;
return;
}
/* edge is entirely above or below, note the shortening rule */
if (p2->y <= in_fill->y || p1->y > in_fill->y)
return;
/* edge lies wholly to the right */
if (p1->x >= in_fill->x && p2->x >= in_fill->x)
return;
if ((p1->x <= in_fill->x && p2->x <= in_fill->x) ||
edge_compare_for_y_against_x (p1, p2, in_fill->y, in_fill->x) < 0)
{
in_fill->winding += dir;
}
}
static cairo_status_t
_cairo_in_fill_move_to (void *closure,
const cairo_point_t *point)
{
cairo_in_fill_t *in_fill = closure;
/* implicit close path */
if (in_fill->has_current_point) {
_cairo_in_fill_add_edge (in_fill,
&in_fill->current_point,
&in_fill->first_point);
}
in_fill->first_point = *point;
in_fill->current_point = *point;
in_fill->has_current_point = TRUE;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_in_fill_line_to (void *closure,
const cairo_point_t *point)
{
cairo_in_fill_t *in_fill = closure;
if (in_fill->has_current_point)
_cairo_in_fill_add_edge (in_fill, &in_fill->current_point, point);
in_fill->current_point = *point;
in_fill->has_current_point = TRUE;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_in_fill_curve_to (void *closure,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
cairo_in_fill_t *in_fill = closure;
cairo_spline_t spline;
cairo_fixed_t top, bot, left;
/* first reject based on bbox */
bot = top = in_fill->current_point.y;
if (b->y < top) top = b->y;
if (b->y > bot) bot = b->y;
if (c->y < top) top = c->y;
if (c->y > bot) bot = c->y;
if (d->y < top) top = d->y;
if (d->y > bot) bot = d->y;
if (bot < in_fill->y || top > in_fill->y) {
in_fill->current_point = *d;
return CAIRO_STATUS_SUCCESS;
}
left = in_fill->current_point.x;
if (b->x < left) left = b->x;
if (c->x < left) left = c->x;
if (d->x < left) left = d->x;
if (left > in_fill->x) {
in_fill->current_point = *d;
return CAIRO_STATUS_SUCCESS;
}
/* XXX Investigate direct inspection of the inflections? */
if (! _cairo_spline_init (&spline,
(cairo_spline_add_point_func_t)_cairo_in_fill_line_to,
in_fill,
&in_fill->current_point, b, c, d))
{
return CAIRO_STATUS_SUCCESS;
}
return _cairo_spline_decompose (&spline, in_fill->tolerance);
}
static cairo_status_t
_cairo_in_fill_close_path (void *closure)
{
cairo_in_fill_t *in_fill = closure;
if (in_fill->has_current_point) {
_cairo_in_fill_add_edge (in_fill,
&in_fill->current_point,
&in_fill->first_point);
in_fill->has_current_point = FALSE;
}
return CAIRO_STATUS_SUCCESS;
}
cairo_bool_t
_cairo_path_fixed_in_fill (const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
double x,
double y)
{
cairo_in_fill_t in_fill;
cairo_status_t status;
cairo_bool_t is_inside;
if (_cairo_path_fixed_fill_is_empty (path))
return FALSE;
_cairo_in_fill_init (&in_fill, tolerance, x, y);
status = _cairo_path_fixed_interpret (path,
_cairo_in_fill_move_to,
_cairo_in_fill_line_to,
_cairo_in_fill_curve_to,
_cairo_in_fill_close_path,
&in_fill);
assert (status == CAIRO_STATUS_SUCCESS);
_cairo_in_fill_close_path (&in_fill);
if (in_fill.on_edge) {
is_inside = TRUE;
} else switch (fill_rule) {
case CAIRO_FILL_RULE_EVEN_ODD:
is_inside = in_fill.winding & 1;
break;
case CAIRO_FILL_RULE_WINDING:
is_inside = in_fill.winding != 0;
break;
default:
ASSERT_NOT_REACHED;
is_inside = FALSE;
break;
}
_cairo_in_fill_fini (&in_fill);
return is_inside;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2006 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@redhat.com>
*/
#ifndef CAIRO_PATH_PRIVATE_H
#define CAIRO_PATH_PRIVATE_H
#include "cairoint.h"
cairo_private cairo_path_t *
_cairo_path_create (cairo_path_fixed_t *path,
cairo_t *cr);
cairo_private cairo_path_t *
_cairo_path_create_flat (cairo_path_fixed_t *path,
cairo_t *cr);
cairo_private cairo_path_t *
_cairo_path_create_in_error (cairo_status_t status);
cairo_private cairo_status_t
_cairo_path_append_to_context (const cairo_path_t *path,
cairo_t *cr);
#endif /* CAIRO_PATH_DATA_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-stroke-boxes.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#define _DEFAULT_SOURCE /* for hypot() */
#include "cairoint.h"
#include "cairo-box-inline.h"
#include "cairo-boxes-private.h"
#include "cairo-error-private.h"
#include "cairo-path-fixed-private.h"
#include "cairo-slope-private.h"
#include "cairo-stroke-dash-private.h"
typedef struct _segment_t {
cairo_point_t p1, p2;
unsigned flags;
#define HORIZONTAL 0x1
#define FORWARDS 0x2
#define JOIN 0x4
} segment_t;
typedef struct _cairo_rectilinear_stroker {
const cairo_stroke_style_t *stroke_style;
const cairo_matrix_t *ctm;
cairo_antialias_t antialias;
cairo_fixed_t half_line_x, half_line_y;
cairo_boxes_t *boxes;
cairo_point_t current_point;
cairo_point_t first_point;
cairo_bool_t open_sub_path;
cairo_stroker_dash_t dash;
cairo_bool_t has_bounds;
cairo_box_t bounds;
int num_segments;
int segments_size;
segment_t *segments;
segment_t segments_embedded[8]; /* common case is a single rectangle */
} cairo_rectilinear_stroker_t;
static void
_cairo_rectilinear_stroker_limit (cairo_rectilinear_stroker_t *stroker,
const cairo_box_t *boxes,
int num_boxes)
{
stroker->has_bounds = TRUE;
_cairo_boxes_get_extents (boxes, num_boxes, &stroker->bounds);
stroker->bounds.p1.x -= stroker->half_line_x;
stroker->bounds.p2.x += stroker->half_line_x;
stroker->bounds.p1.y -= stroker->half_line_y;
stroker->bounds.p2.y += stroker->half_line_y;
}
static cairo_bool_t
_cairo_rectilinear_stroker_init (cairo_rectilinear_stroker_t *stroker,
const cairo_stroke_style_t *stroke_style,
const cairo_matrix_t *ctm,
cairo_antialias_t antialias,
cairo_boxes_t *boxes)
{
/* This special-case rectilinear stroker only supports
* miter-joined lines (not curves) and a translation-only matrix
* (though it could probably be extended to support a matrix with
* uniform, integer scaling).
*
* It also only supports horizontal and vertical line_to
* elements. But we don't catch that here, but instead return
* UNSUPPORTED from _cairo_rectilinear_stroker_line_to if any
* non-rectilinear line_to is encountered.
*/
if (stroke_style->line_join != CAIRO_LINE_JOIN_MITER)
return FALSE;
/* If the miter limit turns right angles into bevels, then we
* can't use this optimization. Remember, the ratio is
* 1/sin(ɸ/2). So the cutoff is 1/sin(π/4.0) or ⎷2,
* which we round for safety. */
if (stroke_style->miter_limit < M_SQRT2)
return FALSE;
if (! (stroke_style->line_cap == CAIRO_LINE_CAP_BUTT ||
stroke_style->line_cap == CAIRO_LINE_CAP_SQUARE))
{
return FALSE;
}
if (! _cairo_matrix_is_scale (ctm))
return FALSE;
stroker->stroke_style = stroke_style;
stroker->ctm = ctm;
stroker->antialias = antialias;
stroker->half_line_x =
_cairo_fixed_from_double (fabs(ctm->xx) * stroke_style->line_width / 2.0);
stroker->half_line_y =
_cairo_fixed_from_double (fabs(ctm->yy) * stroke_style->line_width / 2.0);
stroker->open_sub_path = FALSE;
stroker->segments = stroker->segments_embedded;
stroker->segments_size = ARRAY_LENGTH (stroker->segments_embedded);
stroker->num_segments = 0;
_cairo_stroker_dash_init (&stroker->dash, stroke_style);
stroker->has_bounds = FALSE;
stroker->boxes = boxes;
return TRUE;
}
static void
_cairo_rectilinear_stroker_fini (cairo_rectilinear_stroker_t *stroker)
{
if (stroker->segments != stroker->segments_embedded)
free (stroker->segments);
}
static cairo_status_t
_cairo_rectilinear_stroker_add_segment (cairo_rectilinear_stroker_t *stroker,
const cairo_point_t *p1,
const cairo_point_t *p2,
unsigned flags)
{
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (stroker->num_segments == stroker->segments_size) {
int new_size = stroker->segments_size * 2;
segment_t *new_segments;
if (stroker->segments == stroker->segments_embedded) {
new_segments = _cairo_malloc_ab (new_size, sizeof (segment_t));
if (unlikely (new_segments == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
memcpy (new_segments, stroker->segments,
stroker->num_segments * sizeof (segment_t));
} else {
new_segments = _cairo_realloc_ab (stroker->segments,
new_size, sizeof (segment_t));
if (unlikely (new_segments == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
stroker->segments_size = new_size;
stroker->segments = new_segments;
}
stroker->segments[stroker->num_segments].p1 = *p1;
stroker->segments[stroker->num_segments].p2 = *p2;
stroker->segments[stroker->num_segments].flags = flags;
stroker->num_segments++;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_rectilinear_stroker_emit_segments (cairo_rectilinear_stroker_t *stroker)
{
cairo_line_cap_t line_cap = stroker->stroke_style->line_cap;
cairo_fixed_t half_line_x = stroker->half_line_x;
cairo_fixed_t half_line_y = stroker->half_line_y;
cairo_status_t status;
int i, j;
/* For each segment we generate a single rectangle.
* This rectangle is based on a perpendicular extension (by half the
* line width) of the segment endpoints * after some adjustments of the
* endpoints to account for caps and joins.
*/
for (i = 0; i < stroker->num_segments; i++) {
cairo_bool_t lengthen_initial, lengthen_final;
cairo_point_t *a, *b;
cairo_box_t box;
a = &stroker->segments[i].p1;
b = &stroker->segments[i].p2;
/* We adjust the initial point of the segment to extend the
* rectangle to include the previous cap or join, (this
* adjustment applies to all segments except for the first
* segment of open, butt-capped paths). However, we must be
* careful not to emit a miter join across a degenerate segment
* which has been elided.
*
* Overlapping segments will be eliminated by the tessellation.
* Ideally, we would not emit these self-intersections at all,
* but that is tricky with segments shorter than half_line_width.
*/
j = i == 0 ? stroker->num_segments - 1 : i-1;
lengthen_initial = (stroker->segments[i].flags ^ stroker->segments[j].flags) & HORIZONTAL;
j = i == stroker->num_segments - 1 ? 0 : i+1;
lengthen_final = (stroker->segments[i].flags ^ stroker->segments[j].flags) & HORIZONTAL;
if (stroker->open_sub_path) {
if (i == 0)
lengthen_initial = line_cap != CAIRO_LINE_CAP_BUTT;
if (i == stroker->num_segments - 1)
lengthen_final = line_cap != CAIRO_LINE_CAP_BUTT;
}
/* Perform the adjustments of the endpoints. */
if (lengthen_initial | lengthen_final) {
if (a->y == b->y) {
if (a->x < b->x) {
if (lengthen_initial)
a->x -= half_line_x;
if (lengthen_final)
b->x += half_line_x;
} else {
if (lengthen_initial)
a->x += half_line_x;
if (lengthen_final)
b->x -= half_line_x;
}
} else {
if (a->y < b->y) {
if (lengthen_initial)
a->y -= half_line_y;
if (lengthen_final)
b->y += half_line_y;
} else {
if (lengthen_initial)
a->y += half_line_y;
if (lengthen_final)
b->y -= half_line_y;
}
}
}
/* Form the rectangle by expanding by half the line width in
* either perpendicular direction. */
if (a->y == b->y) {
a->y -= half_line_y;
b->y += half_line_y;
} else {
a->x -= half_line_x;
b->x += half_line_x;
}
if (a->x < b->x) {
box.p1.x = a->x;
box.p2.x = b->x;
} else {
box.p1.x = b->x;
box.p2.x = a->x;
}
if (a->y < b->y) {
box.p1.y = a->y;
box.p2.y = b->y;
} else {
box.p1.y = b->y;
box.p2.y = a->y;
}
status = _cairo_boxes_add (stroker->boxes, stroker->antialias, &box);
if (unlikely (status))
return status;
}
stroker->num_segments = 0;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_rectilinear_stroker_emit_segments_dashed (cairo_rectilinear_stroker_t *stroker)
{
cairo_status_t status;
cairo_line_cap_t line_cap = stroker->stroke_style->line_cap;
cairo_fixed_t half_line_x = stroker->half_line_x;
cairo_fixed_t half_line_y = stroker->half_line_y;
int i;
for (i = 0; i < stroker->num_segments; i++) {
cairo_point_t *a, *b;
cairo_bool_t is_horizontal;
cairo_box_t box;
a = &stroker->segments[i].p1;
b = &stroker->segments[i].p2;
is_horizontal = stroker->segments[i].flags & HORIZONTAL;
/* Handle the joins for a potentially degenerate segment. */
if (line_cap == CAIRO_LINE_CAP_BUTT &&
stroker->segments[i].flags & JOIN &&
(i != stroker->num_segments - 1 ||
(! stroker->open_sub_path && stroker->dash.dash_starts_on)))
{
cairo_slope_t out_slope;
int j = (i + 1) % stroker->num_segments;
cairo_bool_t forwards = !!(stroker->segments[i].flags & FORWARDS);
_cairo_slope_init (&out_slope,
&stroker->segments[j].p1,
&stroker->segments[j].p2);
box.p2 = box.p1 = stroker->segments[i].p2;
if (is_horizontal) {
if (forwards)
box.p2.x += half_line_x;
else
box.p1.x -= half_line_x;
if (out_slope.dy > 0)
box.p1.y -= half_line_y;
else
box.p2.y += half_line_y;
} else {
if (forwards)
box.p2.y += half_line_y;
else
box.p1.y -= half_line_y;
if (out_slope.dx > 0)
box.p1.x -= half_line_x;
else
box.p2.x += half_line_x;
}
status = _cairo_boxes_add (stroker->boxes, stroker->antialias, &box);
if (unlikely (status))
return status;
}
/* Perform the adjustments of the endpoints. */
if (is_horizontal) {
if (line_cap == CAIRO_LINE_CAP_SQUARE) {
if (a->x <= b->x) {
a->x -= half_line_x;
b->x += half_line_x;
} else {
a->x += half_line_x;
b->x -= half_line_x;
}
}
a->y += half_line_y;
b->y -= half_line_y;
} else {
if (line_cap == CAIRO_LINE_CAP_SQUARE) {
if (a->y <= b->y) {
a->y -= half_line_y;
b->y += half_line_y;
} else {
a->y += half_line_y;
b->y -= half_line_y;
}
}
a->x += half_line_x;
b->x -= half_line_x;
}
if (a->x == b->x && a->y == b->y)
continue;
if (a->x < b->x) {
box.p1.x = a->x;
box.p2.x = b->x;
} else {
box.p1.x = b->x;
box.p2.x = a->x;
}
if (a->y < b->y) {
box.p1.y = a->y;
box.p2.y = b->y;
} else {
box.p1.y = b->y;
box.p2.y = a->y;
}
status = _cairo_boxes_add (stroker->boxes, stroker->antialias, &box);
if (unlikely (status))
return status;
}
stroker->num_segments = 0;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_rectilinear_stroker_move_to (void *closure,
const cairo_point_t *point)
{
cairo_rectilinear_stroker_t *stroker = closure;
cairo_status_t status;
if (stroker->dash.dashed)
status = _cairo_rectilinear_stroker_emit_segments_dashed (stroker);
else
status = _cairo_rectilinear_stroker_emit_segments (stroker);
if (unlikely (status))
return status;
/* reset the dash pattern for new sub paths */
_cairo_stroker_dash_start (&stroker->dash);
stroker->current_point = *point;
stroker->first_point = *point;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_rectilinear_stroker_line_to (void *closure,
const cairo_point_t *b)
{
cairo_rectilinear_stroker_t *stroker = closure;
cairo_point_t *a = &stroker->current_point;
cairo_status_t status;
/* We only support horizontal or vertical elements. */
assert (a->x == b->x || a->y == b->y);
/* We don't draw anything for degenerate paths. */
if (a->x == b->x && a->y == b->y)
return CAIRO_STATUS_SUCCESS;
status = _cairo_rectilinear_stroker_add_segment (stroker, a, b,
(a->y == b->y) | JOIN);
stroker->current_point = *b;
stroker->open_sub_path = TRUE;
return status;
}
static cairo_status_t
_cairo_rectilinear_stroker_line_to_dashed (void *closure,
const cairo_point_t *point)
{
cairo_rectilinear_stroker_t *stroker = closure;
const cairo_point_t *a = &stroker->current_point;
const cairo_point_t *b = point;
cairo_bool_t fully_in_bounds;
double sf, sign, remain;
cairo_fixed_t mag;
cairo_status_t status;
cairo_line_t segment;
cairo_bool_t dash_on = FALSE;
unsigned is_horizontal;
/* We don't draw anything for degenerate paths. */
if (a->x == b->x && a->y == b->y)
return CAIRO_STATUS_SUCCESS;
/* We only support horizontal or vertical elements. */
assert (a->x == b->x || a->y == b->y);
fully_in_bounds = TRUE;
if (stroker->has_bounds &&
(! _cairo_box_contains_point (&stroker->bounds, a) ||
! _cairo_box_contains_point (&stroker->bounds, b)))
{
fully_in_bounds = FALSE;
}
is_horizontal = a->y == b->y;
if (is_horizontal) {
mag = b->x - a->x;
sf = fabs (stroker->ctm->xx);
} else {
mag = b->y - a->y;
sf = fabs (stroker->ctm->yy);
}
if (mag < 0) {
remain = _cairo_fixed_to_double (-mag);
sign = 1.;
} else {
remain = _cairo_fixed_to_double (mag);
is_horizontal |= FORWARDS;
sign = -1.;
}
segment.p2 = segment.p1 = *a;
while (remain > 0.) {
double step_length;
step_length = MIN (sf * stroker->dash.dash_remain, remain);
remain -= step_length;
mag = _cairo_fixed_from_double (sign*remain);
if (is_horizontal & 0x1)
segment.p2.x = b->x + mag;
else
segment.p2.y = b->y + mag;
if (stroker->dash.dash_on &&
(fully_in_bounds ||
_cairo_box_intersects_line_segment (&stroker->bounds, &segment)))
{
status = _cairo_rectilinear_stroker_add_segment (stroker,
&segment.p1,
&segment.p2,
is_horizontal | (remain <= 0.) << 2);
if (unlikely (status))
return status;
dash_on = TRUE;
}
else
{
dash_on = FALSE;
}
_cairo_stroker_dash_step (&stroker->dash, step_length / sf);
segment.p1 = segment.p2;
}
if (stroker->dash.dash_on && ! dash_on &&
(fully_in_bounds ||
_cairo_box_intersects_line_segment (&stroker->bounds, &segment)))
{
/* This segment ends on a transition to dash_on, compute a new face
* and add cap for the beginning of the next dash_on step.
*/
status = _cairo_rectilinear_stroker_add_segment (stroker,
&segment.p1,
&segment.p1,
is_horizontal | JOIN);
if (unlikely (status))
return status;
}
stroker->current_point = *point;
stroker->open_sub_path = TRUE;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_rectilinear_stroker_close_path (void *closure)
{
cairo_rectilinear_stroker_t *stroker = closure;
cairo_status_t status;
/* We don't draw anything for degenerate paths. */
if (! stroker->open_sub_path)
return CAIRO_STATUS_SUCCESS;
if (stroker->dash.dashed) {
status = _cairo_rectilinear_stroker_line_to_dashed (stroker,
&stroker->first_point);
} else {
status = _cairo_rectilinear_stroker_line_to (stroker,
&stroker->first_point);
}
if (unlikely (status))
return status;
stroker->open_sub_path = FALSE;
if (stroker->dash.dashed)
status = _cairo_rectilinear_stroker_emit_segments_dashed (stroker);
else
status = _cairo_rectilinear_stroker_emit_segments (stroker);
if (unlikely (status))
return status;
return CAIRO_STATUS_SUCCESS;
}
cairo_int_status_t
_cairo_path_fixed_stroke_rectilinear_to_boxes (const cairo_path_fixed_t *path,
const cairo_stroke_style_t *stroke_style,
const cairo_matrix_t *ctm,
cairo_antialias_t antialias,
cairo_boxes_t *boxes)
{
cairo_rectilinear_stroker_t rectilinear_stroker;
cairo_int_status_t status;
cairo_box_t box;
assert (_cairo_path_fixed_stroke_is_rectilinear (path));
if (! _cairo_rectilinear_stroker_init (&rectilinear_stroker,
stroke_style, ctm, antialias,
boxes))
{
return CAIRO_INT_STATUS_UNSUPPORTED;
}
if (! rectilinear_stroker.dash.dashed &&
_cairo_path_fixed_is_stroke_box (path, &box) &&
/* if the segments overlap we need to feed them into the tessellator */
box.p2.x - box.p1.x > 2* rectilinear_stroker.half_line_x &&
box.p2.y - box.p1.y > 2* rectilinear_stroker.half_line_y)
{
cairo_box_t b;
/* top */
b.p1.x = box.p1.x - rectilinear_stroker.half_line_x;
b.p2.x = box.p2.x + rectilinear_stroker.half_line_x;
b.p1.y = box.p1.y - rectilinear_stroker.half_line_y;
b.p2.y = box.p1.y + rectilinear_stroker.half_line_y;
status = _cairo_boxes_add (boxes, antialias, &b);
assert (status == CAIRO_INT_STATUS_SUCCESS);
/* left (excluding top/bottom) */
b.p1.x = box.p1.x - rectilinear_stroker.half_line_x;
b.p2.x = box.p1.x + rectilinear_stroker.half_line_x;
b.p1.y = box.p1.y + rectilinear_stroker.half_line_y;
b.p2.y = box.p2.y - rectilinear_stroker.half_line_y;
status = _cairo_boxes_add (boxes, antialias, &b);
assert (status == CAIRO_INT_STATUS_SUCCESS);
/* right (excluding top/bottom) */
b.p1.x = box.p2.x - rectilinear_stroker.half_line_x;
b.p2.x = box.p2.x + rectilinear_stroker.half_line_x;
b.p1.y = box.p1.y + rectilinear_stroker.half_line_y;
b.p2.y = box.p2.y - rectilinear_stroker.half_line_y;
status = _cairo_boxes_add (boxes, antialias, &b);
assert (status == CAIRO_INT_STATUS_SUCCESS);
/* bottom */
b.p1.x = box.p1.x - rectilinear_stroker.half_line_x;
b.p2.x = box.p2.x + rectilinear_stroker.half_line_x;
b.p1.y = box.p2.y - rectilinear_stroker.half_line_y;
b.p2.y = box.p2.y + rectilinear_stroker.half_line_y;
status = _cairo_boxes_add (boxes, antialias, &b);
assert (status == CAIRO_INT_STATUS_SUCCESS);
goto done;
}
if (boxes->num_limits) {
_cairo_rectilinear_stroker_limit (&rectilinear_stroker,
boxes->limits,
boxes->num_limits);
}
status = _cairo_path_fixed_interpret (path,
_cairo_rectilinear_stroker_move_to,
rectilinear_stroker.dash.dashed ?
_cairo_rectilinear_stroker_line_to_dashed :
_cairo_rectilinear_stroker_line_to,
NULL,
_cairo_rectilinear_stroker_close_path,
&rectilinear_stroker);
if (unlikely (status))
goto BAIL;
if (rectilinear_stroker.dash.dashed)
status = _cairo_rectilinear_stroker_emit_segments_dashed (&rectilinear_stroker);
else
status = _cairo_rectilinear_stroker_emit_segments (&rectilinear_stroker);
if (unlikely (status))
goto BAIL;
/* As we incrementally tessellate, we do not eliminate self-intersections */
status = _cairo_bentley_ottmann_tessellate_boxes (boxes,
CAIRO_FILL_RULE_WINDING,
boxes);
if (unlikely (status))
goto BAIL;
done:
_cairo_rectilinear_stroker_fini (&rectilinear_stroker);
return CAIRO_STATUS_SUCCESS;
BAIL:
_cairo_rectilinear_stroker_fini (&rectilinear_stroker);
_cairo_boxes_clear (boxes);
return status;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-stroke-polygon.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#define _DEFAULT_SOURCE /* for hypot() */
#include "cairoint.h"
#include "cairo-box-inline.h"
#include "cairo-boxes-private.h"
#include "cairo-contour-inline.h"
#include "cairo-contour-private.h"
#include "cairo-error-private.h"
#include "cairo-path-fixed-private.h"
#include "cairo-slope-private.h"
#define DEBUG 0
struct stroker {
cairo_stroke_style_t style;
#if DEBUG
cairo_contour_t path;
#endif
struct stroke_contour {
/* Note that these are not strictly contours as they may intersect */
cairo_contour_t contour;
} cw, ccw;
cairo_uint64_t contour_tolerance;
cairo_polygon_t *polygon;
const cairo_matrix_t *ctm;
const cairo_matrix_t *ctm_inverse;
double tolerance;
double spline_cusp_tolerance;
double half_line_width;
cairo_bool_t ctm_det_positive;
cairo_pen_t pen;
cairo_point_t first_point;
cairo_bool_t has_initial_sub_path;
cairo_bool_t has_current_face;
cairo_stroke_face_t current_face;
cairo_bool_t has_first_face;
cairo_stroke_face_t first_face;
cairo_bool_t has_bounds;
cairo_box_t bounds;
};
static inline double
normalize_slope (double *dx, double *dy);
static void
compute_face (const cairo_point_t *point,
const cairo_slope_t *dev_slope,
struct stroker *stroker,
cairo_stroke_face_t *face);
static cairo_uint64_t
point_distance_sq (const cairo_point_t *p1,
const cairo_point_t *p2)
{
int32_t dx = p1->x - p2->x;
int32_t dy = p1->y - p2->y;
return _cairo_int32x32_64_mul (dx, dx) + _cairo_int32x32_64_mul (dy, dy);
}
static cairo_bool_t
within_tolerance (const cairo_point_t *p1,
const cairo_point_t *p2,
cairo_uint64_t tolerance)
{
return FALSE;
return _cairo_int64_lt (point_distance_sq (p1, p2), tolerance);
}
static void
contour_add_point (struct stroker *stroker,
struct stroke_contour *c,
const cairo_point_t *point)
{
if (! within_tolerance (point, _cairo_contour_last_point (&c->contour),
stroker->contour_tolerance))
_cairo_contour_add_point (&c->contour, point);
//*_cairo_contour_last_point (&c->contour) = *point;
}
static void
translate_point (cairo_point_t *point, const cairo_point_t *offset)
{
point->x += offset->x;
point->y += offset->y;
}
static int
slope_compare_sgn (double dx1, double dy1, double dx2, double dy2)
{
double c = (dx1 * dy2 - dx2 * dy1);
if (c > 0) return 1;
if (c < 0) return -1;
return 0;
}
static inline int
range_step (int i, int step, int max)
{
i += step;
if (i < 0)
i = max - 1;
if (i >= max)
i = 0;
return i;
}
/*
* Construct a fan around the midpoint using the vertices from pen between
* inpt and outpt.
*/
static void
add_fan (struct stroker *stroker,
const cairo_slope_t *in_vector,
const cairo_slope_t *out_vector,
const cairo_point_t *midpt,
cairo_bool_t clockwise,
struct stroke_contour *c)
{
cairo_pen_t *pen = &stroker->pen;
int start, stop;
if (stroker->has_bounds &&
! _cairo_box_contains_point (&stroker->bounds, midpt))
return;
assert (stroker->pen.num_vertices);
if (clockwise) {
_cairo_pen_find_active_cw_vertices (pen,
in_vector, out_vector,
&start, &stop);
while (start != stop) {
cairo_point_t p = *midpt;
translate_point (&p, &pen->vertices[start].point);
contour_add_point (stroker, c, &p);
if (++start == pen->num_vertices)
start = 0;
}
} else {
_cairo_pen_find_active_ccw_vertices (pen,
in_vector, out_vector,
&start, &stop);
while (start != stop) {
cairo_point_t p = *midpt;
translate_point (&p, &pen->vertices[start].point);
contour_add_point (stroker, c, &p);
if (start-- == 0)
start += pen->num_vertices;
}
}
}
static int
join_is_clockwise (const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out)
{
return _cairo_slope_compare (&in->dev_vector, &out->dev_vector) < 0;
}
static void
inner_join (struct stroker *stroker,
const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out,
int clockwise)
{
#if 0
cairo_point_t last;
const cairo_point_t *p, *outpt;
struct stroke_contour *inner;
cairo_int64_t d_p, d_last;
cairo_int64_t half_line_width;
cairo_bool_t negate;
/* XXX line segments shorter than line-width */
if (clockwise) {
inner = &stroker->ccw;
outpt = &out->ccw;
negate = 1;
} else {
inner = &stroker->cw;
outpt = &out->cw;
negate = 0;
}
half_line_width = CAIRO_FIXED_ONE*CAIRO_FIXED_ONE/2 * stroker->style.line_width * out->length + .5;
/* On the inside, the previous end-point is always
* closer to the new face by definition.
*/
last = *_cairo_contour_last_point (&inner->contour);
d_last = distance_from_face (out, &last, negate);
_cairo_contour_remove_last_point (&inner->contour);
prev:
if (inner->contour.chain.num_points == 0) {
contour_add_point (stroker, inner, outpt);
return;
}
p = _cairo_contour_last_point (&inner->contour);
d_p = distance_from_face (out, p, negate);
if (_cairo_int64_lt (d_p, half_line_width) &&
!_cairo_int64_negative (distance_along_face (out, p)))
{
last = *p;
d_last = d_p;
_cairo_contour_remove_last_point (&inner->contour);
goto prev;
}
compute_inner_joint (&last, d_last, p, d_p, half_line_width);
contour_add_point (stroker, inner, &last);
#else
const cairo_point_t *outpt;
struct stroke_contour *inner;
if (clockwise) {
inner = &stroker->ccw;
outpt = &out->ccw;
} else {
inner = &stroker->cw;
outpt = &out->cw;
}
contour_add_point (stroker, inner, &in->point);
contour_add_point (stroker, inner, outpt);
#endif
}
static void
inner_close (struct stroker *stroker,
const cairo_stroke_face_t *in,
cairo_stroke_face_t *out)
{
#if 0
cairo_point_t last;
const cairo_point_t *p, *outpt, *inpt;
struct stroke_contour *inner;
struct _cairo_contour_chain *chain;
/* XXX line segments shorter than line-width */
if (join_is_clockwise (in, out)) {
inner = &stroker->ccw;
outpt = &in->ccw;
inpt = &out->ccw;
} else {
inner = &stroker->cw;
outpt = &in->cw;
inpt = &out->cw;
}
if (inner->contour.chain.num_points == 0) {
contour_add_point (stroker, inner, &in->point);
contour_add_point (stroker, inner, inpt);
*_cairo_contour_first_point (&inner->contour) =
*_cairo_contour_last_point (&inner->contour);
return;
}
line_width = stroker->style.line_width/2;
line_width *= CAIRO_FIXED_ONE;
d_last = sign * distance_from_face (out, outpt);
last = *outpt;
for (chain = &inner->contour.chain; chain; chain = chain->next) {
for (i = 0; i < chain->num_points; i++) {
p = &chain->points[i];
if ((d_p = sign * distance_from_face (in, p)) >= line_width &&
distance_from_edge (stroker, inpt, &last, p) < line_width)
{
goto out;
}
if (p->x != last.x || p->y != last.y) {
last = *p;
d_last = d_p;
}
}
}
out:
if (d_p != d_last) {
double dot = (line_width - d_last) / (d_p - d_last);
last.x += dot * (p->x - last.x);
last.y += dot * (p->y - last.y);
}
*_cairo_contour_last_point (&inner->contour) = last;
for (chain = &inner->contour.chain; chain; chain = chain->next) {
for (i = 0; i < chain->num_points; i++) {
cairo_point_t *pp = &chain->points[i];
if (pp == p)
return;
*pp = last;
}
}
#else
const cairo_point_t *inpt;
struct stroke_contour *inner;
if (join_is_clockwise (in, out)) {
inner = &stroker->ccw;
inpt = &out->ccw;
} else {
inner = &stroker->cw;
inpt = &out->cw;
}
contour_add_point (stroker, inner, &in->point);
contour_add_point (stroker, inner, inpt);
*_cairo_contour_first_point (&inner->contour) =
*_cairo_contour_last_point (&inner->contour);
#endif
}
static void
outer_close (struct stroker *stroker,
const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out)
{
const cairo_point_t *inpt, *outpt;
struct stroke_contour *outer;
int clockwise;
if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
{
return;
}
clockwise = join_is_clockwise (in, out);
if (clockwise) {
inpt = &in->cw;
outpt = &out->cw;
outer = &stroker->cw;
} else {
inpt = &in->ccw;
outpt = &out->ccw;
outer = &stroker->ccw;
}
if (within_tolerance (inpt, outpt, stroker->contour_tolerance)) {
*_cairo_contour_first_point (&outer->contour) =
*_cairo_contour_last_point (&outer->contour);
return;
}
switch (stroker->style.line_join) {
case CAIRO_LINE_JOIN_ROUND:
/* construct a fan around the common midpoint */
if ((in->dev_slope.x * out->dev_slope.x +
in->dev_slope.y * out->dev_slope.y) < stroker->spline_cusp_tolerance)
{
add_fan (stroker,
&in->dev_vector, &out->dev_vector, &in->point,
clockwise, outer);
break;
}
case CAIRO_LINE_JOIN_MITER:
default: {
/* dot product of incoming slope vector with outgoing slope vector */
double in_dot_out = in->dev_slope.x * out->dev_slope.x +
in->dev_slope.y * out->dev_slope.y;
double ml = stroker->style.miter_limit;
/* Check the miter limit -- lines meeting at an acute angle
* can generate long miters, the limit converts them to bevel
*
* Consider the miter join formed when two line segments
* meet at an angle psi:
*
* /.\
* /. .\
* /./ \.\
* /./psi\.\
*
* We can zoom in on the right half of that to see:
*
* |\
* | \ psi/2
* | \
* | \
* | \
* | \
* miter \
* length \
* | \
* | .\
* | . \
* |. line \
* \ width \
* \ \
*
*
* The right triangle in that figure, (the line-width side is
* shown faintly with three '.' characters), gives us the
* following expression relating miter length, angle and line
* width:
*
* 1 /sin (psi/2) = miter_length / line_width
*
* The right-hand side of this relationship is the same ratio
* in which the miter limit (ml) is expressed. We want to know
* when the miter length is within the miter limit. That is
* when the following condition holds:
*
* 1/sin(psi/2) <= ml
* 1 <= ml sin(psi/2)
* 1 <= ml² sin²(psi/2)
* 2 <= ml² 2 sin²(psi/2)
* 2·sin²(psi/2) = 1-cos(psi)
* 2 <= ml² (1-cos(psi))
*
* in · out = |in| |out| cos (psi)
*
* in and out are both unit vectors, so:
*
* in · out = cos (psi)
*
* 2 <= ml² (1 - in · out)
*
*/
if (2 <= ml * ml * (1 + in_dot_out)) {
double x1, y1, x2, y2;
double mx, my;
double dx1, dx2, dy1, dy2;
double ix, iy;
double fdx1, fdy1, fdx2, fdy2;
double mdx, mdy;
/*
* we've got the points already transformed to device
* space, but need to do some computation with them and
* also need to transform the slope from user space to
* device space
*/
/* outer point of incoming line face */
x1 = _cairo_fixed_to_double (inpt->x);
y1 = _cairo_fixed_to_double (inpt->y);
dx1 = in->dev_slope.x;
dy1 = in->dev_slope.y;
/* outer point of outgoing line face */
x2 = _cairo_fixed_to_double (outpt->x);
y2 = _cairo_fixed_to_double (outpt->y);
dx2 = out->dev_slope.x;
dy2 = out->dev_slope.y;
/*
* Compute the location of the outer corner of the miter.
* That's pretty easy -- just the intersection of the two
* outer edges. We've got slopes and points on each
* of those edges. Compute my directly, then compute
* mx by using the edge with the larger dy; that avoids
* dividing by values close to zero.
*/
my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
(dx1 * dy2 - dx2 * dy1));
if (fabs (dy1) >= fabs (dy2))
mx = (my - y1) * dx1 / dy1 + x1;
else
mx = (my - y2) * dx2 / dy2 + x2;
/*
* When the two outer edges are nearly parallel, slight
* perturbations in the position of the outer points of the lines
* caused by representing them in fixed point form can cause the
* intersection point of the miter to move a large amount. If
* that moves the miter intersection from between the two faces,
* then draw a bevel instead.
*/
ix = _cairo_fixed_to_double (in->point.x);
iy = _cairo_fixed_to_double (in->point.y);
/* slope of one face */
fdx1 = x1 - ix; fdy1 = y1 - iy;
/* slope of the other face */
fdx2 = x2 - ix; fdy2 = y2 - iy;
/* slope from the intersection to the miter point */
mdx = mx - ix; mdy = my - iy;
/*
* Make sure the miter point line lies between the two
* faces by comparing the slopes
*/
if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
slope_compare_sgn (fdx2, fdy2, mdx, mdy))
{
cairo_point_t p;
p.x = _cairo_fixed_from_double (mx);
p.y = _cairo_fixed_from_double (my);
*_cairo_contour_last_point (&outer->contour) = p;
*_cairo_contour_first_point (&outer->contour) = p;
return;
}
}
break;
}
case CAIRO_LINE_JOIN_BEVEL:
break;
}
contour_add_point (stroker, outer, outpt);
}
static void
outer_join (struct stroker *stroker,
const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out,
int clockwise)
{
const cairo_point_t *inpt, *outpt;
struct stroke_contour *outer;
if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
{
return;
}
if (clockwise) {
inpt = &in->cw;
outpt = &out->cw;
outer = &stroker->cw;
} else {
inpt = &in->ccw;
outpt = &out->ccw;
outer = &stroker->ccw;
}
switch (stroker->style.line_join) {
case CAIRO_LINE_JOIN_ROUND:
/* construct a fan around the common midpoint */
add_fan (stroker,
&in->dev_vector, &out->dev_vector, &in->point,
clockwise, outer);
break;
case CAIRO_LINE_JOIN_MITER:
default: {
/* dot product of incoming slope vector with outgoing slope vector */
double in_dot_out = in->dev_slope.x * out->dev_slope.x +
in->dev_slope.y * out->dev_slope.y;
double ml = stroker->style.miter_limit;
/* Check the miter limit -- lines meeting at an acute angle
* can generate long miters, the limit converts them to bevel
*
* Consider the miter join formed when two line segments
* meet at an angle psi:
*
* /.\
* /. .\
* /./ \.\
* /./psi\.\
*
* We can zoom in on the right half of that to see:
*
* |\
* | \ psi/2
* | \
* | \
* | \
* | \
* miter \
* length \
* | \
* | .\
* | . \
* |. line \
* \ width \
* \ \
*
*
* The right triangle in that figure, (the line-width side is
* shown faintly with three '.' characters), gives us the
* following expression relating miter length, angle and line
* width:
*
* 1 /sin (psi/2) = miter_length / line_width
*
* The right-hand side of this relationship is the same ratio
* in which the miter limit (ml) is expressed. We want to know
* when the miter length is within the miter limit. That is
* when the following condition holds:
*
* 1/sin(psi/2) <= ml
* 1 <= ml sin(psi/2)
* 1 <= ml² sin²(psi/2)
* 2 <= ml² 2 sin²(psi/2)
* 2·sin²(psi/2) = 1-cos(psi)
* 2 <= ml² (1-cos(psi))
*
* in · out = |in| |out| cos (psi)
*
* in and out are both unit vectors, so:
*
* in · out = cos (psi)
*
* 2 <= ml² (1 - in · out)
*
*/
if (2 <= ml * ml * (1 + in_dot_out)) {
double x1, y1, x2, y2;
double mx, my;
double dx1, dx2, dy1, dy2;
double ix, iy;
double fdx1, fdy1, fdx2, fdy2;
double mdx, mdy;
/*
* we've got the points already transformed to device
* space, but need to do some computation with them and
* also need to transform the slope from user space to
* device space
*/
/* outer point of incoming line face */
x1 = _cairo_fixed_to_double (inpt->x);
y1 = _cairo_fixed_to_double (inpt->y);
dx1 = in->dev_slope.x;
dy1 = in->dev_slope.y;
/* outer point of outgoing line face */
x2 = _cairo_fixed_to_double (outpt->x);
y2 = _cairo_fixed_to_double (outpt->y);
dx2 = out->dev_slope.x;
dy2 = out->dev_slope.y;
/*
* Compute the location of the outer corner of the miter.
* That's pretty easy -- just the intersection of the two
* outer edges. We've got slopes and points on each
* of those edges. Compute my directly, then compute
* mx by using the edge with the larger dy; that avoids
* dividing by values close to zero.
*/
my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
(dx1 * dy2 - dx2 * dy1));
if (fabs (dy1) >= fabs (dy2))
mx = (my - y1) * dx1 / dy1 + x1;
else
mx = (my - y2) * dx2 / dy2 + x2;
/*
* When the two outer edges are nearly parallel, slight
* perturbations in the position of the outer points of the lines
* caused by representing them in fixed point form can cause the
* intersection point of the miter to move a large amount. If
* that moves the miter intersection from between the two faces,
* then draw a bevel instead.
*/
ix = _cairo_fixed_to_double (in->point.x);
iy = _cairo_fixed_to_double (in->point.y);
/* slope of one face */
fdx1 = x1 - ix; fdy1 = y1 - iy;
/* slope of the other face */
fdx2 = x2 - ix; fdy2 = y2 - iy;
/* slope from the intersection to the miter point */
mdx = mx - ix; mdy = my - iy;
/*
* Make sure the miter point line lies between the two
* faces by comparing the slopes
*/
if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
slope_compare_sgn (fdx2, fdy2, mdx, mdy))
{
cairo_point_t p;
p.x = _cairo_fixed_from_double (mx);
p.y = _cairo_fixed_from_double (my);
*_cairo_contour_last_point (&outer->contour) = p;
return;
}
}
break;
}
case CAIRO_LINE_JOIN_BEVEL:
break;
}
contour_add_point (stroker,outer, outpt);
}
static void
add_cap (struct stroker *stroker,
const cairo_stroke_face_t *f,
struct stroke_contour *c)
{
switch (stroker->style.line_cap) {
case CAIRO_LINE_CAP_ROUND: {
cairo_slope_t slope;
slope.dx = -f->dev_vector.dx;
slope.dy = -f->dev_vector.dy;
add_fan (stroker, &f->dev_vector, &slope, &f->point, FALSE, c);
break;
}
case CAIRO_LINE_CAP_SQUARE: {
cairo_slope_t fvector;
cairo_point_t p;
double dx, dy;
dx = f->usr_vector.x;
dy = f->usr_vector.y;
dx *= stroker->half_line_width;
dy *= stroker->half_line_width;
cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);
fvector.dx = _cairo_fixed_from_double (dx);
fvector.dy = _cairo_fixed_from_double (dy);
p.x = f->ccw.x + fvector.dx;
p.y = f->ccw.y + fvector.dy;
contour_add_point (stroker, c, &p);
p.x = f->cw.x + fvector.dx;
p.y = f->cw.y + fvector.dy;
contour_add_point (stroker, c, &p);
}
case CAIRO_LINE_CAP_BUTT:
default:
break;
}
contour_add_point (stroker, c, &f->cw);
}
static void
add_leading_cap (struct stroker *stroker,
const cairo_stroke_face_t *face,
struct stroke_contour *c)
{
cairo_stroke_face_t reversed;
cairo_point_t t;
reversed = *face;
/* The initial cap needs an outward facing vector. Reverse everything */
reversed.usr_vector.x = -reversed.usr_vector.x;
reversed.usr_vector.y = -reversed.usr_vector.y;
reversed.dev_vector.dx = -reversed.dev_vector.dx;
reversed.dev_vector.dy = -reversed.dev_vector.dy;
t = reversed.cw;
reversed.cw = reversed.ccw;
reversed.ccw = t;
add_cap (stroker, &reversed, c);
}
static void
add_trailing_cap (struct stroker *stroker,
const cairo_stroke_face_t *face,
struct stroke_contour *c)
{
add_cap (stroker, face, c);
}
static inline double
normalize_slope (double *dx, double *dy)
{
double dx0 = *dx, dy0 = *dy;
double mag;
assert (dx0 != 0.0 || dy0 != 0.0);
if (dx0 == 0.0) {
*dx = 0.0;
if (dy0 > 0.0) {
mag = dy0;
*dy = 1.0;
} else {
mag = -dy0;
*dy = -1.0;
}
} else if (dy0 == 0.0) {
*dy = 0.0;
if (dx0 > 0.0) {
mag = dx0;
*dx = 1.0;
} else {
mag = -dx0;
*dx = -1.0;
}
} else {
mag = hypot (dx0, dy0);
*dx = dx0 / mag;
*dy = dy0 / mag;
}
return mag;
}
static void
compute_face (const cairo_point_t *point,
const cairo_slope_t *dev_slope,
struct stroker *stroker,
cairo_stroke_face_t *face)
{
double face_dx, face_dy;
cairo_point_t offset_ccw, offset_cw;
double slope_dx, slope_dy;
slope_dx = _cairo_fixed_to_double (dev_slope->dx);
slope_dy = _cairo_fixed_to_double (dev_slope->dy);
face->length = normalize_slope (&slope_dx, &slope_dy);
face->dev_slope.x = slope_dx;
face->dev_slope.y = slope_dy;
/*
* rotate to get a line_width/2 vector along the face, note that
* the vector must be rotated the right direction in device space,
* but by 90° in user space. So, the rotation depends on
* whether the ctm reflects or not, and that can be determined
* by looking at the determinant of the matrix.
*/
if (! _cairo_matrix_is_identity (stroker->ctm_inverse)) {
/* Normalize the matrix! */
cairo_matrix_transform_distance (stroker->ctm_inverse,
&slope_dx, &slope_dy);
normalize_slope (&slope_dx, &slope_dy);
if (stroker->ctm_det_positive) {
face_dx = - slope_dy * stroker->half_line_width;
face_dy = slope_dx * stroker->half_line_width;
} else {
face_dx = slope_dy * stroker->half_line_width;
face_dy = - slope_dx * stroker->half_line_width;
}
/* back to device space */
cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);
} else {
face_dx = - slope_dy * stroker->half_line_width;
face_dy = slope_dx * stroker->half_line_width;
}
offset_ccw.x = _cairo_fixed_from_double (face_dx);
offset_ccw.y = _cairo_fixed_from_double (face_dy);
offset_cw.x = -offset_ccw.x;
offset_cw.y = -offset_ccw.y;
face->ccw = *point;
translate_point (&face->ccw, &offset_ccw);
face->point = *point;
face->cw = *point;
translate_point (&face->cw, &offset_cw);
face->usr_vector.x = slope_dx;
face->usr_vector.y = slope_dy;
face->dev_vector = *dev_slope;
}
static void
add_caps (struct stroker *stroker)
{
/* check for a degenerative sub_path */
if (stroker->has_initial_sub_path &&
! stroker->has_first_face &&
! stroker->has_current_face &&
stroker->style.line_cap == CAIRO_LINE_CAP_ROUND)
{
/* pick an arbitrary slope to use */
cairo_slope_t slope = { CAIRO_FIXED_ONE, 0 };
cairo_stroke_face_t face;
/* arbitrarily choose first_point */
compute_face (&stroker->first_point, &slope, stroker, &face);
add_leading_cap (stroker, &face, &stroker->ccw);
add_trailing_cap (stroker, &face, &stroker->ccw);
/* ensure the circle is complete */
_cairo_contour_add_point (&stroker->ccw.contour,
_cairo_contour_first_point (&stroker->ccw.contour));
_cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
_cairo_contour_reset (&stroker->ccw.contour);
} else {
if (stroker->has_current_face)
add_trailing_cap (stroker, &stroker->current_face, &stroker->ccw);
#if DEBUG
{
FILE *file = fopen ("contours.txt", "a");
_cairo_debug_print_contour (file, &stroker->path);
_cairo_debug_print_contour (file, &stroker->cw.contour);
_cairo_debug_print_contour (file, &stroker->ccw.contour);
fclose (file);
_cairo_contour_reset (&stroker->path);
}
#endif
_cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
_cairo_contour_reset (&stroker->ccw.contour);
if (stroker->has_first_face) {
_cairo_contour_add_point (&stroker->ccw.contour,
&stroker->first_face.cw);
add_leading_cap (stroker, &stroker->first_face, &stroker->ccw);
#if DEBUG
{
FILE *file = fopen ("contours.txt", "a");
_cairo_debug_print_contour (file, &stroker->ccw.contour);
fclose (file);
}
#endif
_cairo_polygon_add_contour (stroker->polygon,
&stroker->ccw.contour);
_cairo_contour_reset (&stroker->ccw.contour);
}
_cairo_polygon_add_contour (stroker->polygon, &stroker->cw.contour);
_cairo_contour_reset (&stroker->cw.contour);
}
}
static cairo_status_t
close_path (void *closure);
static cairo_status_t
move_to (void *closure,
const cairo_point_t *point)
{
struct stroker *stroker = closure;
/* Cap the start and end of the previous sub path as needed */
add_caps (stroker);
stroker->has_first_face = FALSE;
stroker->has_current_face = FALSE;
stroker->has_initial_sub_path = FALSE;
stroker->first_point = *point;
#if DEBUG
_cairo_contour_add_point (&stroker->path, point);
#endif
stroker->current_face.point = *point;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
line_to (void *closure,
const cairo_point_t *point)
{
struct stroker *stroker = closure;
cairo_stroke_face_t start;
cairo_point_t *p1 = &stroker->current_face.point;
cairo_slope_t dev_slope;
stroker->has_initial_sub_path = TRUE;
if (p1->x == point->x && p1->y == point->y)
return CAIRO_STATUS_SUCCESS;
#if DEBUG
_cairo_contour_add_point (&stroker->path, point);
#endif
_cairo_slope_init (&dev_slope, p1, point);
compute_face (p1, &dev_slope, stroker, &start);
if (stroker->has_current_face) {
int clockwise = _cairo_slope_compare (&stroker->current_face.dev_vector,
&start.dev_vector);
if (clockwise) {
clockwise = clockwise < 0;
/* Join with final face from previous segment */
if (! within_tolerance (&stroker->current_face.ccw, &start.ccw,
stroker->contour_tolerance) ||
! within_tolerance (&stroker->current_face.cw, &start.cw,
stroker->contour_tolerance))
{
outer_join (stroker, &stroker->current_face, &start, clockwise);
inner_join (stroker, &stroker->current_face, &start, clockwise);
}
}
} else {
if (! stroker->has_first_face) {
/* Save sub path's first face in case needed for closing join */
stroker->first_face = start;
stroker->has_first_face = TRUE;
}
stroker->has_current_face = TRUE;
contour_add_point (stroker, &stroker->cw, &start.cw);
contour_add_point (stroker, &stroker->ccw, &start.ccw);
}
stroker->current_face = start;
stroker->current_face.point = *point;
stroker->current_face.ccw.x += dev_slope.dx;
stroker->current_face.ccw.y += dev_slope.dy;
stroker->current_face.cw.x += dev_slope.dx;
stroker->current_face.cw.y += dev_slope.dy;
contour_add_point (stroker, &stroker->cw, &stroker->current_face.cw);
contour_add_point (stroker, &stroker->ccw, &stroker->current_face.ccw);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
spline_to (void *closure,
const cairo_point_t *point,
const cairo_slope_t *tangent)
{
struct stroker *stroker = closure;
cairo_stroke_face_t face;
#if DEBUG
_cairo_contour_add_point (&stroker->path, point);
#endif
if ((tangent->dx | tangent->dy) == 0) {
struct stroke_contour *outer;
cairo_point_t t;
int clockwise;
face = stroker->current_face;
face.usr_vector.x = -face.usr_vector.x;
face.usr_vector.y = -face.usr_vector.y;
face.dev_vector.dx = -face.dev_vector.dx;
face.dev_vector.dy = -face.dev_vector.dy;
t = face.cw;
face.cw = face.ccw;
face.ccw = t;
clockwise = join_is_clockwise (&stroker->current_face, &face);
if (clockwise) {
outer = &stroker->cw;
} else {
outer = &stroker->ccw;
}
add_fan (stroker,
&stroker->current_face.dev_vector,
&face.dev_vector,
&stroker->current_face.point,
clockwise, outer);
} else {
compute_face (point, tangent, stroker, &face);
if ((face.dev_slope.x * stroker->current_face.dev_slope.x +
face.dev_slope.y * stroker->current_face.dev_slope.y) < stroker->spline_cusp_tolerance)
{
struct stroke_contour *outer;
int clockwise = join_is_clockwise (&stroker->current_face, &face);
stroker->current_face.cw.x += face.point.x - stroker->current_face.point.x;
stroker->current_face.cw.y += face.point.y - stroker->current_face.point.y;
contour_add_point (stroker, &stroker->cw, &stroker->current_face.cw);
stroker->current_face.ccw.x += face.point.x - stroker->current_face.point.x;
stroker->current_face.ccw.y += face.point.y - stroker->current_face.point.y;
contour_add_point (stroker, &stroker->ccw, &stroker->current_face.ccw);
if (clockwise) {
outer = &stroker->cw;
} else {
outer = &stroker->ccw;
}
add_fan (stroker,
&stroker->current_face.dev_vector,
&face.dev_vector,
&stroker->current_face.point,
clockwise, outer);
}
contour_add_point (stroker, &stroker->cw, &face.cw);
contour_add_point (stroker, &stroker->ccw, &face.ccw);
}
stroker->current_face = face;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
curve_to (void *closure,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
struct stroker *stroker = closure;
cairo_spline_t spline;
cairo_stroke_face_t face;
if (stroker->has_bounds &&
! _cairo_spline_intersects (&stroker->current_face.point, b, c, d,
&stroker->bounds))
return line_to (closure, d);
if (! _cairo_spline_init (&spline, spline_to, stroker,
&stroker->current_face.point, b, c, d))
return line_to (closure, d);
compute_face (&stroker->current_face.point, &spline.initial_slope,
stroker, &face);
if (stroker->has_current_face) {
int clockwise = join_is_clockwise (&stroker->current_face, &face);
/* Join with final face from previous segment */
outer_join (stroker, &stroker->current_face, &face, clockwise);
inner_join (stroker, &stroker->current_face, &face, clockwise);
} else {
if (! stroker->has_first_face) {
/* Save sub path's first face in case needed for closing join */
stroker->first_face = face;
stroker->has_first_face = TRUE;
}
stroker->has_current_face = TRUE;
contour_add_point (stroker, &stroker->cw, &face.cw);
contour_add_point (stroker, &stroker->ccw, &face.ccw);
}
stroker->current_face = face;
return _cairo_spline_decompose (&spline, stroker->tolerance);
}
static cairo_status_t
close_path (void *closure)
{
struct stroker *stroker = closure;
cairo_status_t status;
status = line_to (stroker, &stroker->first_point);
if (unlikely (status))
return status;
if (stroker->has_first_face && stroker->has_current_face) {
/* Join first and final faces of sub path */
outer_close (stroker, &stroker->current_face, &stroker->first_face);
inner_close (stroker, &stroker->current_face, &stroker->first_face);
#if 0
*_cairo_contour_first_point (&stroker->ccw.contour) =
*_cairo_contour_last_point (&stroker->ccw.contour);
*_cairo_contour_first_point (&stroker->cw.contour) =
*_cairo_contour_last_point (&stroker->cw.contour);
#endif
_cairo_polygon_add_contour (stroker->polygon, &stroker->cw.contour);
_cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
#if DEBUG
{
FILE *file = fopen ("contours.txt", "a");
_cairo_debug_print_contour (file, &stroker->path);
_cairo_debug_print_contour (file, &stroker->cw.contour);
_cairo_debug_print_contour (file, &stroker->ccw.contour);
fclose (file);
_cairo_contour_reset (&stroker->path);
}
#endif
_cairo_contour_reset (&stroker->cw.contour);
_cairo_contour_reset (&stroker->ccw.contour);
} else {
/* Cap the start and end of the sub path as needed */
add_caps (stroker);
}
stroker->has_initial_sub_path = FALSE;
stroker->has_first_face = FALSE;
stroker->has_current_face = FALSE;
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_path_fixed_stroke_to_polygon (const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_polygon_t *polygon)
{
struct stroker stroker;
cairo_status_t status;
if (style->num_dashes) {
return _cairo_path_fixed_stroke_dashed_to_polygon (path,
style,
ctm,
ctm_inverse,
tolerance,
polygon);
}
stroker.has_bounds = polygon->num_limits;
if (stroker.has_bounds) {
/* Extend the bounds in each direction to account for the maximum area
* we might generate trapezoids, to capture line segments that are
* outside of the bounds but which might generate rendering that's
* within bounds.
*/
double dx, dy;
cairo_fixed_t fdx, fdy;
int i;
stroker.bounds = polygon->limits[0];
for (i = 1; i < polygon->num_limits; i++)
_cairo_box_add_box (&stroker.bounds, &polygon->limits[i]);
_cairo_stroke_style_max_distance_from_path (style, path, ctm, &dx, &dy);
fdx = _cairo_fixed_from_double (dx);
fdy = _cairo_fixed_from_double (dy);
stroker.bounds.p1.x -= fdx;
stroker.bounds.p2.x += fdx;
stroker.bounds.p1.y -= fdy;
stroker.bounds.p2.y += fdy;
}
stroker.style = *style;
stroker.ctm = ctm;
stroker.ctm_inverse = ctm_inverse;
stroker.tolerance = tolerance;
stroker.half_line_width = style->line_width / 2.;
/* To test whether we need to join two segments of a spline using
* a round-join or a bevel-join, we can inspect the angle between the
* two segments. If the difference between the chord distance
* (half-line-width times the cosine of the bisection angle) and the
* half-line-width itself is greater than tolerance then we need to
* inject a point.
*/
stroker.spline_cusp_tolerance = 1 - tolerance / stroker.half_line_width;
stroker.spline_cusp_tolerance *= stroker.spline_cusp_tolerance;
stroker.spline_cusp_tolerance *= 2;
stroker.spline_cusp_tolerance -= 1;
stroker.ctm_det_positive =
_cairo_matrix_compute_determinant (ctm) >= 0.0;
stroker.pen.num_vertices = 0;
if (path->has_curve_to ||
style->line_join == CAIRO_LINE_JOIN_ROUND ||
style->line_cap == CAIRO_LINE_CAP_ROUND) {
status = _cairo_pen_init (&stroker.pen,
stroker.half_line_width,
tolerance, ctm);
if (unlikely (status))
return status;
/* If the line width is so small that the pen is reduced to a
single point, then we have nothing to do. */
if (stroker.pen.num_vertices <= 1)
return CAIRO_STATUS_SUCCESS;
}
stroker.has_current_face = FALSE;
stroker.has_first_face = FALSE;
stroker.has_initial_sub_path = FALSE;
#if DEBUG
remove ("contours.txt");
remove ("polygons.txt");
_cairo_contour_init (&stroker.path, 0);
#endif
_cairo_contour_init (&stroker.cw.contour, 1);
_cairo_contour_init (&stroker.ccw.contour, -1);
tolerance *= CAIRO_FIXED_ONE;
tolerance *= tolerance;
stroker.contour_tolerance = tolerance;
stroker.polygon = polygon;
status = _cairo_path_fixed_interpret (path,
move_to,
line_to,
curve_to,
close_path,
&stroker);
/* Cap the start and end of the final sub path as needed */
if (likely (status == CAIRO_STATUS_SUCCESS))
add_caps (&stroker);
_cairo_contour_fini (&stroker.cw.contour);
_cairo_contour_fini (&stroker.ccw.contour);
if (stroker.pen.num_vertices)
_cairo_pen_fini (&stroker.pen);
#if DEBUG
{
FILE *file = fopen ("polygons.txt", "a");
_cairo_debug_print_polygon (file, polygon);
fclose (file);
}
#endif
return status;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-stroke-traps.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2013 Intel Corporation
*
* 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-box-inline.h"
#include "cairo-path-fixed-private.h"
#include "cairo-slope-private.h"
#include "cairo-stroke-dash-private.h"
#include "cairo-traps-private.h"
#include <float.h>
struct stroker {
const cairo_stroke_style_t *style;
const cairo_matrix_t *ctm;
const cairo_matrix_t *ctm_inverse;
double spline_cusp_tolerance;
double half_line_width;
double tolerance;
double ctm_determinant;
cairo_bool_t ctm_det_positive;
cairo_line_join_t line_join;
cairo_traps_t *traps;
cairo_pen_t pen;
cairo_point_t first_point;
cairo_bool_t has_initial_sub_path;
cairo_bool_t has_current_face;
cairo_stroke_face_t current_face;
cairo_bool_t has_first_face;
cairo_stroke_face_t first_face;
cairo_stroker_dash_t dash;
cairo_bool_t has_bounds;
cairo_box_t tight_bounds;
cairo_box_t line_bounds;
cairo_box_t join_bounds;
};
static cairo_status_t
stroker_init (struct stroker *stroker,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_traps_t *traps)
{
cairo_status_t status;
stroker->style = style;
stroker->ctm = ctm;
stroker->ctm_inverse = NULL;
if (! _cairo_matrix_is_identity (ctm_inverse))
stroker->ctm_inverse = ctm_inverse;
stroker->line_join = style->line_join;
stroker->half_line_width = style->line_width / 2.0;
stroker->tolerance = tolerance;
stroker->traps = traps;
/* To test whether we need to join two segments of a spline using
* a round-join or a bevel-join, we can inspect the angle between the
* two segments. If the difference between the chord distance
* (half-line-width times the cosine of the bisection angle) and the
* half-line-width itself is greater than tolerance then we need to
* inject a point.
*/
stroker->spline_cusp_tolerance = 1 - tolerance / stroker->half_line_width;
stroker->spline_cusp_tolerance *= stroker->spline_cusp_tolerance;
stroker->spline_cusp_tolerance *= 2;
stroker->spline_cusp_tolerance -= 1;
stroker->ctm_determinant = _cairo_matrix_compute_determinant (stroker->ctm);
stroker->ctm_det_positive = stroker->ctm_determinant >= 0.0;
status = _cairo_pen_init (&stroker->pen,
stroker->half_line_width,
tolerance, ctm);
if (unlikely (status))
return status;
stroker->has_current_face = FALSE;
stroker->has_first_face = FALSE;
stroker->has_initial_sub_path = FALSE;
_cairo_stroker_dash_init (&stroker->dash, style);
stroker->has_bounds = traps->num_limits;
if (stroker->has_bounds) {
/* Extend the bounds in each direction to account for the maximum area
* we might generate trapezoids, to capture line segments that are outside
* of the bounds but which might generate rendering that's within bounds.
*/
double dx, dy;
cairo_fixed_t fdx, fdy;
stroker->tight_bounds = traps->bounds;
_cairo_stroke_style_max_distance_from_path (stroker->style, path,
stroker->ctm, &dx, &dy);
_cairo_stroke_style_max_line_distance_from_path (stroker->style, path,
stroker->ctm, &dx, &dy);
fdx = _cairo_fixed_from_double (dx);
fdy = _cairo_fixed_from_double (dy);
stroker->line_bounds = stroker->tight_bounds;
stroker->line_bounds.p1.x -= fdx;
stroker->line_bounds.p2.x += fdx;
stroker->line_bounds.p1.y -= fdy;
stroker->line_bounds.p2.y += fdy;
_cairo_stroke_style_max_join_distance_from_path (stroker->style, path,
stroker->ctm, &dx, &dy);
fdx = _cairo_fixed_from_double (dx);
fdy = _cairo_fixed_from_double (dy);
stroker->join_bounds = stroker->tight_bounds;
stroker->join_bounds.p1.x -= fdx;
stroker->join_bounds.p2.x += fdx;
stroker->join_bounds.p1.y -= fdy;
stroker->join_bounds.p2.y += fdy;
}
return CAIRO_STATUS_SUCCESS;
}
static void
stroker_fini (struct stroker *stroker)
{
_cairo_pen_fini (&stroker->pen);
}
static void
translate_point (cairo_point_t *point, cairo_point_t *offset)
{
point->x += offset->x;
point->y += offset->y;
}
static int
join_is_clockwise (const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out)
{
return _cairo_slope_compare (&in->dev_vector, &out->dev_vector) < 0;
}
static int
slope_compare_sgn (double dx1, double dy1, double dx2, double dy2)
{
double c = dx1 * dy2 - dx2 * dy1;
if (c > 0) return 1;
if (c < 0) return -1;
return 0;
}
static cairo_bool_t
stroker_intersects_join (const struct stroker *stroker,
const cairo_point_t *in,
const cairo_point_t *out)
{
cairo_line_t segment;
if (! stroker->has_bounds)
return TRUE;
segment.p1 = *in;
segment.p2 = *out;
return _cairo_box_intersects_line_segment (&stroker->join_bounds, &segment);
}
static void
join (struct stroker *stroker,
cairo_stroke_face_t *in,
cairo_stroke_face_t *out)
{
int clockwise = join_is_clockwise (out, in);
cairo_point_t *inpt, *outpt;
if (in->cw.x == out->cw.x &&
in->cw.y == out->cw.y &&
in->ccw.x == out->ccw.x &&
in->ccw.y == out->ccw.y)
{
return;
}
if (clockwise) {
inpt = &in->ccw;
outpt = &out->ccw;
} else {
inpt = &in->cw;
outpt = &out->cw;
}
if (! stroker_intersects_join (stroker, inpt, outpt))
return;
switch (stroker->line_join) {
case CAIRO_LINE_JOIN_ROUND:
/* construct a fan around the common midpoint */
if ((in->dev_slope.x * out->dev_slope.x +
in->dev_slope.y * out->dev_slope.y) < stroker->spline_cusp_tolerance)
{
int start, stop;
cairo_point_t tri[3], edges[4];
cairo_pen_t *pen = &stroker->pen;
edges[0] = in->cw;
edges[1] = in->ccw;
tri[0] = in->point;
tri[1] = *inpt;
if (clockwise) {
_cairo_pen_find_active_ccw_vertices (pen,
&in->dev_vector, &out->dev_vector,
&start, &stop);
while (start != stop) {
tri[2] = in->point;
translate_point (&tri[2], &pen->vertices[start].point);
edges[2] = in->point;
edges[3] = tri[2];
_cairo_traps_tessellate_triangle_with_edges (stroker->traps,
tri, edges);
tri[1] = tri[2];
edges[0] = edges[2];
edges[1] = edges[3];
if (start-- == 0)
start += pen->num_vertices;
}
} else {
_cairo_pen_find_active_cw_vertices (pen,
&in->dev_vector, &out->dev_vector,
&start, &stop);
while (start != stop) {
tri[2] = in->point;
translate_point (&tri[2], &pen->vertices[start].point);
edges[2] = in->point;
edges[3] = tri[2];
_cairo_traps_tessellate_triangle_with_edges (stroker->traps,
tri, edges);
tri[1] = tri[2];
edges[0] = edges[2];
edges[1] = edges[3];
if (++start == pen->num_vertices)
start = 0;
}
}
tri[2] = *outpt;
edges[2] = out->cw;
edges[3] = out->ccw;
_cairo_traps_tessellate_triangle_with_edges (stroker->traps,
tri, edges);
} else {
cairo_point_t t[] = { { in->point.x, in->point.y}, { inpt->x, inpt->y }, { outpt->x, outpt->y } };
cairo_point_t e[] = { { in->cw.x, in->cw.y}, { in->ccw.x, in->ccw.y },
{ out->cw.x, out->cw.y}, { out->ccw.x, out->ccw.y } };
_cairo_traps_tessellate_triangle_with_edges (stroker->traps, t, e);
}
break;
case CAIRO_LINE_JOIN_MITER:
default: {
/* dot product of incoming slope vector with outgoing slope vector */
double in_dot_out = (-in->usr_vector.x * out->usr_vector.x +
-in->usr_vector.y * out->usr_vector.y);
double ml = stroker->style->miter_limit;
/* Check the miter limit -- lines meeting at an acute angle
* can generate long miters, the limit converts them to bevel
*
* Consider the miter join formed when two line segments
* meet at an angle psi:
*
* /.\
* /. .\
* /./ \.\
* /./psi\.\
*
* We can zoom in on the right half of that to see:
*
* |\
* | \ psi/2
* | \
* | \
* | \
* | \
* miter \
* length \
* | \
* | .\
* | . \
* |. line \
* \ width \
* \ \
*
*
* The right triangle in that figure, (the line-width side is
* shown faintly with three '.' characters), gives us the
* following expression relating miter length, angle and line
* width:
*
* 1 /sin (psi/2) = miter_length / line_width
*
* The right-hand side of this relationship is the same ratio
* in which the miter limit (ml) is expressed. We want to know
* when the miter length is within the miter limit. That is
* when the following condition holds:
*
* 1/sin(psi/2) <= ml
* 1 <= ml sin(psi/2)
* 1 <= ml² sin²(psi/2)
* 2 <= ml² 2 sin²(psi/2)
* 2·sin²(psi/2) = 1-cos(psi)
* 2 <= ml² (1-cos(psi))
*
* in · out = |in| |out| cos (psi)
*
* in and out are both unit vectors, so:
*
* in · out = cos (psi)
*
* 2 <= ml² (1 - in · out)
*
*/
if (2 <= ml * ml * (1 - in_dot_out)) {
double x1, y1, x2, y2;
double mx, my;
double dx1, dx2, dy1, dy2;
cairo_point_t outer;
cairo_point_t quad[4];
double ix, iy;
double fdx1, fdy1, fdx2, fdy2;
double mdx, mdy;
/*
* we've got the points already transformed to device
* space, but need to do some computation with them and
* also need to transform the slope from user space to
* device space
*/
/* outer point of incoming line face */
x1 = _cairo_fixed_to_double (inpt->x);
y1 = _cairo_fixed_to_double (inpt->y);
dx1 = in->usr_vector.x;
dy1 = in->usr_vector.y;
cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);
/* outer point of outgoing line face */
x2 = _cairo_fixed_to_double (outpt->x);
y2 = _cairo_fixed_to_double (outpt->y);
dx2 = out->usr_vector.x;
dy2 = out->usr_vector.y;
cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
/*
* Compute the location of the outer corner of the miter.
* That's pretty easy -- just the intersection of the two
* outer edges. We've got slopes and points on each
* of those edges. Compute my directly, then compute
* mx by using the edge with the larger dy; that avoids
* dividing by values close to zero.
*/
my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
(dx1 * dy2 - dx2 * dy1));
if (fabs (dy1) >= fabs (dy2))
mx = (my - y1) * dx1 / dy1 + x1;
else
mx = (my - y2) * dx2 / dy2 + x2;
/*
* When the two outer edges are nearly parallel, slight
* perturbations in the position of the outer points of the lines
* caused by representing them in fixed point form can cause the
* intersection point of the miter to move a large amount. If
* that moves the miter intersection from between the two faces,
* then draw a bevel instead.
*/
ix = _cairo_fixed_to_double (in->point.x);
iy = _cairo_fixed_to_double (in->point.y);
/* slope of one face */
fdx1 = x1 - ix; fdy1 = y1 - iy;
/* slope of the other face */
fdx2 = x2 - ix; fdy2 = y2 - iy;
/* slope from the intersection to the miter point */
mdx = mx - ix; mdy = my - iy;
/*
* Make sure the miter point line lies between the two
* faces by comparing the slopes
*/
if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
slope_compare_sgn (fdx2, fdy2, mdx, mdy))
{
/*
* Draw the quadrilateral
*/
outer.x = _cairo_fixed_from_double (mx);
outer.y = _cairo_fixed_from_double (my);
quad[0] = in->point;
quad[1] = *inpt;
quad[2] = outer;
quad[3] = *outpt;
_cairo_traps_tessellate_convex_quad (stroker->traps, quad);
break;
}
}
/* fall through ... */
}
case CAIRO_LINE_JOIN_BEVEL: {
cairo_point_t t[] = { { in->point.x, in->point.y }, { inpt->x, inpt->y }, { outpt->x, outpt->y } };
cairo_point_t e[] = { { in->cw.x, in->cw.y }, { in->ccw.x, in->ccw.y },
{ out->cw.x, out->cw.y }, { out->ccw.x, out->ccw.y } };
_cairo_traps_tessellate_triangle_with_edges (stroker->traps, t, e);
break;
}
}
}
static void
add_cap (struct stroker *stroker, cairo_stroke_face_t *f)
{
switch (stroker->style->line_cap) {
case CAIRO_LINE_CAP_ROUND: {
int start, stop;
cairo_slope_t in_slope, out_slope;
cairo_point_t tri[3], edges[4];
cairo_pen_t *pen = &stroker->pen;
in_slope = f->dev_vector;
out_slope.dx = -in_slope.dx;
out_slope.dy = -in_slope.dy;
_cairo_pen_find_active_cw_vertices (pen, &in_slope, &out_slope,
&start, &stop);
edges[0] = f->cw;
edges[1] = f->ccw;
tri[0] = f->point;
tri[1] = f->cw;
while (start != stop) {
tri[2] = f->point;
translate_point (&tri[2], &pen->vertices[start].point);
edges[2] = f->point;
edges[3] = tri[2];
_cairo_traps_tessellate_triangle_with_edges (stroker->traps,
tri, edges);
tri[1] = tri[2];
edges[0] = edges[2];
edges[1] = edges[3];
if (++start == pen->num_vertices)
start = 0;
}
tri[2] = f->ccw;
edges[2] = f->cw;
edges[3] = f->ccw;
_cairo_traps_tessellate_triangle_with_edges (stroker->traps,
tri, edges);
break;
}
case CAIRO_LINE_CAP_SQUARE: {
double dx, dy;
cairo_slope_t fvector;
cairo_point_t quad[4];
dx = f->usr_vector.x;
dy = f->usr_vector.y;
dx *= stroker->half_line_width;
dy *= stroker->half_line_width;
cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);
fvector.dx = _cairo_fixed_from_double (dx);
fvector.dy = _cairo_fixed_from_double (dy);
quad[0] = f->cw;
quad[1].x = f->cw.x + fvector.dx;
quad[1].y = f->cw.y + fvector.dy;
quad[2].x = f->ccw.x + fvector.dx;
quad[2].y = f->ccw.y + fvector.dy;
quad[3] = f->ccw;
_cairo_traps_tessellate_convex_quad (stroker->traps, quad);
break;
}
case CAIRO_LINE_CAP_BUTT:
default:
break;
}
}
static void
add_leading_cap (struct stroker *stroker,
cairo_stroke_face_t *face)
{
cairo_stroke_face_t reversed;
cairo_point_t t;
reversed = *face;
/* The initial cap needs an outward facing vector. Reverse everything */
reversed.usr_vector.x = -reversed.usr_vector.x;
reversed.usr_vector.y = -reversed.usr_vector.y;
reversed.dev_vector.dx = -reversed.dev_vector.dx;
reversed.dev_vector.dy = -reversed.dev_vector.dy;
t = reversed.cw;
reversed.cw = reversed.ccw;
reversed.ccw = t;
add_cap (stroker, &reversed);
}
static void
add_trailing_cap (struct stroker *stroker, cairo_stroke_face_t *face)
{
add_cap (stroker, face);
}
static inline double
normalize_slope (double *dx, double *dy)
{
double dx0 = *dx, dy0 = *dy;
if (dx0 == 0.0 && dy0 == 0.0)
return 0;
if (dx0 == 0.0) {
*dx = 0.0;
if (dy0 > 0.0) {
*dy = 1.0;
return dy0;
} else {
*dy = -1.0;
return -dy0;
}
} else if (dy0 == 0.0) {
*dy = 0.0;
if (dx0 > 0.0) {
*dx = 1.0;
return dx0;
} else {
*dx = -1.0;
return -dx0;
}
} else {
double mag = hypot (dx0, dy0);
*dx = dx0 / mag;
*dy = dy0 / mag;
return mag;
}
}
static void
compute_face (const cairo_point_t *point,
const cairo_slope_t *dev_slope,
struct stroker *stroker,
cairo_stroke_face_t *face)
{
double face_dx, face_dy;
cairo_point_t offset_ccw, offset_cw;
double slope_dx, slope_dy;
slope_dx = _cairo_fixed_to_double (dev_slope->dx);
slope_dy = _cairo_fixed_to_double (dev_slope->dy);
face->length = normalize_slope (&slope_dx, &slope_dy);
face->dev_slope.x = slope_dx;
face->dev_slope.y = slope_dy;
/*
* rotate to get a line_width/2 vector along the face, note that
* the vector must be rotated the right direction in device space,
* but by 90° in user space. So, the rotation depends on
* whether the ctm reflects or not, and that can be determined
* by looking at the determinant of the matrix.
*/
if (stroker->ctm_inverse) {
cairo_matrix_transform_distance (stroker->ctm_inverse, &slope_dx, &slope_dy);
normalize_slope (&slope_dx, &slope_dy);
if (stroker->ctm_det_positive) {
face_dx = - slope_dy * stroker->half_line_width;
face_dy = slope_dx * stroker->half_line_width;
} else {
face_dx = slope_dy * stroker->half_line_width;
face_dy = - slope_dx * stroker->half_line_width;
}
/* back to device space */
cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);
} else {
face_dx = - slope_dy * stroker->half_line_width;
face_dy = slope_dx * stroker->half_line_width;
}
offset_ccw.x = _cairo_fixed_from_double (face_dx);
offset_ccw.y = _cairo_fixed_from_double (face_dy);
offset_cw.x = -offset_ccw.x;
offset_cw.y = -offset_ccw.y;
face->ccw = *point;
translate_point (&face->ccw, &offset_ccw);
face->point = *point;
face->cw = *point;
translate_point (&face->cw, &offset_cw);
face->usr_vector.x = slope_dx;
face->usr_vector.y = slope_dy;
face->dev_vector = *dev_slope;
}
static void
add_caps (struct stroker *stroker)
{
/* check for a degenerative sub_path */
if (stroker->has_initial_sub_path &&
!stroker->has_first_face &&
!stroker->has_current_face &&
stroker->style->line_cap == CAIRO_LINE_CAP_ROUND)
{
/* pick an arbitrary slope to use */
cairo_slope_t slope = { CAIRO_FIXED_ONE, 0 };
cairo_stroke_face_t face;
/* arbitrarily choose first_point
* first_point and current_point should be the same */
compute_face (&stroker->first_point, &slope, stroker, &face);
add_leading_cap (stroker, &face);
add_trailing_cap (stroker, &face);
}
if (stroker->has_first_face)
add_leading_cap (stroker, &stroker->first_face);
if (stroker->has_current_face)
add_trailing_cap (stroker, &stroker->current_face);
}
static cairo_bool_t
stroker_intersects_edge (const struct stroker *stroker,
const cairo_stroke_face_t *start,
const cairo_stroke_face_t *end)
{
cairo_box_t box;
if (! stroker->has_bounds)
return TRUE;
if (_cairo_box_contains_point (&stroker->tight_bounds, &start->cw))
return TRUE;
box.p2 = box.p1 = start->cw;
if (_cairo_box_contains_point (&stroker->tight_bounds, &start->ccw))
return TRUE;
_cairo_box_add_point (&box, &start->ccw);
if (_cairo_box_contains_point (&stroker->tight_bounds, &end->cw))
return TRUE;
_cairo_box_add_point (&box, &end->cw);
if (_cairo_box_contains_point (&stroker->tight_bounds, &end->ccw))
return TRUE;
_cairo_box_add_point (&box, &end->ccw);
return (box.p2.x > stroker->tight_bounds.p1.x &&
box.p1.x < stroker->tight_bounds.p2.x &&
box.p2.y > stroker->tight_bounds.p1.y &&
box.p1.y < stroker->tight_bounds.p2.y);
}
static void
add_sub_edge (struct stroker *stroker,
const cairo_point_t *p1, const cairo_point_t *p2,
const cairo_slope_t *dev_slope,
cairo_stroke_face_t *start, cairo_stroke_face_t *end)
{
cairo_point_t rectangle[4];
compute_face (p1, dev_slope, stroker, start);
*end = *start;
end->point = *p2;
rectangle[0].x = p2->x - p1->x;
rectangle[0].y = p2->y - p1->y;
translate_point (&end->ccw, &rectangle[0]);
translate_point (&end->cw, &rectangle[0]);
if (p1->x == p2->x && p1->y == p2->y)
return;
if (! stroker_intersects_edge (stroker, start, end))
return;
rectangle[0] = start->cw;
rectangle[1] = start->ccw;
rectangle[2] = end->ccw;
rectangle[3] = end->cw;
_cairo_traps_tessellate_convex_quad (stroker->traps, rectangle);
}
static cairo_status_t
move_to (void *closure, const cairo_point_t *point)
{
struct stroker *stroker = closure;
/* Cap the start and end of the previous sub path as needed */
add_caps (stroker);
stroker->first_point = *point;
stroker->current_face.point = *point;
stroker->has_first_face = FALSE;
stroker->has_current_face = FALSE;
stroker->has_initial_sub_path = FALSE;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
move_to_dashed (void *closure, const cairo_point_t *point)
{
/* reset the dash pattern for new sub paths */
struct stroker *stroker = closure;
_cairo_stroker_dash_start (&stroker->dash);
return move_to (closure, point);
}
static cairo_status_t
line_to (void *closure, const cairo_point_t *point)
{
struct stroker *stroker = closure;
cairo_stroke_face_t start, end;
const cairo_point_t *p1 = &stroker->current_face.point;
const cairo_point_t *p2 = point;
cairo_slope_t dev_slope;
stroker->has_initial_sub_path = TRUE;
if (p1->x == p2->x && p1->y == p2->y)
return CAIRO_STATUS_SUCCESS;
_cairo_slope_init (&dev_slope, p1, p2);
add_sub_edge (stroker, p1, p2, &dev_slope, &start, &end);
if (stroker->has_current_face) {
/* Join with final face from previous segment */
join (stroker, &stroker->current_face, &start);
} else if (!stroker->has_first_face) {
/* Save sub path's first face in case needed for closing join */
stroker->first_face = start;
stroker->has_first_face = TRUE;
}
stroker->current_face = end;
stroker->has_current_face = TRUE;
return CAIRO_STATUS_SUCCESS;
}
/*
* Dashed lines. Cap each dash end, join around turns when on
*/
static cairo_status_t
line_to_dashed (void *closure, const cairo_point_t *point)
{
struct stroker *stroker = closure;
double mag, remain, step_length = 0;
double slope_dx, slope_dy;
double dx2, dy2;
cairo_stroke_face_t sub_start, sub_end;
const cairo_point_t *p1 = &stroker->current_face.point;
const cairo_point_t *p2 = point;
cairo_slope_t dev_slope;
cairo_line_t segment;
cairo_bool_t fully_in_bounds;
stroker->has_initial_sub_path = stroker->dash.dash_starts_on;
if (p1->x == p2->x && p1->y == p2->y)
return CAIRO_STATUS_SUCCESS;
fully_in_bounds = TRUE;
if (stroker->has_bounds &&
(! _cairo_box_contains_point (&stroker->join_bounds, p1) ||
! _cairo_box_contains_point (&stroker->join_bounds, p2)))
{
fully_in_bounds = FALSE;
}
_cairo_slope_init (&dev_slope, p1, p2);
slope_dx = _cairo_fixed_to_double (p2->x - p1->x);
slope_dy = _cairo_fixed_to_double (p2->y - p1->y);
if (stroker->ctm_inverse)
cairo_matrix_transform_distance (stroker->ctm_inverse, &slope_dx, &slope_dy);
mag = normalize_slope (&slope_dx, &slope_dy);
if (mag <= DBL_EPSILON)
return CAIRO_STATUS_SUCCESS;
remain = mag;
segment.p1 = *p1;
while (remain) {
step_length = MIN (stroker->dash.dash_remain, remain);
remain -= step_length;
dx2 = slope_dx * (mag - remain);
dy2 = slope_dy * (mag - remain);
cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
segment.p2.x = _cairo_fixed_from_double (dx2) + p1->x;
segment.p2.y = _cairo_fixed_from_double (dy2) + p1->y;
if (stroker->dash.dash_on &&
(fully_in_bounds ||
(! stroker->has_first_face && stroker->dash.dash_starts_on) ||
_cairo_box_intersects_line_segment (&stroker->join_bounds, &segment)))
{
add_sub_edge (stroker,
&segment.p1, &segment.p2,
&dev_slope,
&sub_start, &sub_end);
if (stroker->has_current_face) {
/* Join with final face from previous segment */
join (stroker, &stroker->current_face, &sub_start);
stroker->has_current_face = FALSE;
} else if (! stroker->has_first_face && stroker->dash.dash_starts_on) {
/* Save sub path's first face in case needed for closing join */
stroker->first_face = sub_start;
stroker->has_first_face = TRUE;
} else {
/* Cap dash start if not connecting to a previous segment */
add_leading_cap (stroker, &sub_start);
}
if (remain) {
/* Cap dash end if not at end of segment */
add_trailing_cap (stroker, &sub_end);
} else {
stroker->current_face = sub_end;
stroker->has_current_face = TRUE;
}
} else {
if (stroker->has_current_face) {
/* Cap final face from previous segment */
add_trailing_cap (stroker, &stroker->current_face);
stroker->has_current_face = FALSE;
}
}
_cairo_stroker_dash_step (&stroker->dash, step_length);
segment.p1 = segment.p2;
}
if (stroker->dash.dash_on && ! stroker->has_current_face) {
/* This segment ends on a transition to dash_on, compute a new face
* and add cap for the beginning of the next dash_on step.
*
* Note: this will create a degenerate cap if this is not the last line
* in the path. Whether this behaviour is desirable or not is debatable.
* On one side these degenerate caps can not be reproduced with regular
* path stroking.
* On the other hand, Acroread 7 also produces the degenerate caps.
*/
compute_face (point, &dev_slope, stroker, &stroker->current_face);
add_leading_cap (stroker, &stroker->current_face);
stroker->has_current_face = TRUE;
} else
stroker->current_face.point = *point;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
spline_to (void *closure,
const cairo_point_t *point,
const cairo_slope_t *tangent)
{
struct stroker *stroker = closure;
cairo_stroke_face_t face;
if ((tangent->dx | tangent->dy) == 0) {
cairo_point_t t;
face = stroker->current_face;
face.usr_vector.x = -face.usr_vector.x;
face.usr_vector.y = -face.usr_vector.y;
face.dev_slope.x = -face.dev_slope.x;
face.dev_slope.y = -face.dev_slope.y;
face.dev_vector.dx = -face.dev_vector.dx;
face.dev_vector.dy = -face.dev_vector.dy;
t = face.cw;
face.cw = face.ccw;
face.ccw = t;
join (stroker, &stroker->current_face, &face);
} else {
cairo_point_t rectangle[4];
compute_face (&stroker->current_face.point, tangent, stroker, &face);
join (stroker, &stroker->current_face, &face);
rectangle[0] = face.cw;
rectangle[1] = face.ccw;
rectangle[2].x = point->x - face.point.x;
rectangle[2].y = point->y - face.point.y;
face.point = *point;
translate_point (&face.ccw, &rectangle[2]);
translate_point (&face.cw, &rectangle[2]);
rectangle[2] = face.ccw;
rectangle[3] = face.cw;
_cairo_traps_tessellate_convex_quad (stroker->traps, rectangle);
}
stroker->current_face = face;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
curve_to (void *closure,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
struct stroker *stroker = closure;
cairo_line_join_t line_join_save;
cairo_spline_t spline;
cairo_stroke_face_t face;
cairo_status_t status;
if (stroker->has_bounds &&
! _cairo_spline_intersects (&stroker->current_face.point, b, c, d,
&stroker->line_bounds))
return line_to (closure, d);
if (! _cairo_spline_init (&spline, spline_to, stroker,
&stroker->current_face.point, b, c, d))
return line_to (closure, d);
compute_face (&stroker->current_face.point, &spline.initial_slope,
stroker, &face);
if (stroker->has_current_face) {
/* Join with final face from previous segment */
join (stroker, &stroker->current_face, &face);
} else {
if (! stroker->has_first_face) {
/* Save sub path's first face in case needed for closing join */
stroker->first_face = face;
stroker->has_first_face = TRUE;
}
stroker->has_current_face = TRUE;
}
stroker->current_face = face;
/* Temporarily modify the stroker to use round joins to guarantee
* smooth stroked curves. */
line_join_save = stroker->line_join;
stroker->line_join = CAIRO_LINE_JOIN_ROUND;
status = _cairo_spline_decompose (&spline, stroker->tolerance);
stroker->line_join = line_join_save;
return status;
}
static cairo_status_t
curve_to_dashed (void *closure,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
struct stroker *stroker = closure;
cairo_spline_t spline;
cairo_line_join_t line_join_save;
cairo_spline_add_point_func_t func;
cairo_status_t status;
func = (cairo_spline_add_point_func_t)line_to_dashed;
if (stroker->has_bounds &&
! _cairo_spline_intersects (&stroker->current_face.point, b, c, d,
&stroker->line_bounds))
return func (closure, d, NULL);
if (! _cairo_spline_init (&spline, func, stroker,
&stroker->current_face.point, b, c, d))
return func (closure, d, NULL);
/* Temporarily modify the stroker to use round joins to guarantee
* smooth stroked curves. */
line_join_save = stroker->line_join;
stroker->line_join = CAIRO_LINE_JOIN_ROUND;
status = _cairo_spline_decompose (&spline, stroker->tolerance);
stroker->line_join = line_join_save;
return status;
}
static cairo_status_t
_close_path (struct stroker *stroker)
{
if (stroker->has_first_face && stroker->has_current_face) {
/* Join first and final faces of sub path */
join (stroker, &stroker->current_face, &stroker->first_face);
} else {
/* Cap the start and end of the sub path as needed */
add_caps (stroker);
}
stroker->has_initial_sub_path = FALSE;
stroker->has_first_face = FALSE;
stroker->has_current_face = FALSE;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
close_path (void *closure)
{
struct stroker *stroker = closure;
cairo_status_t status;
status = line_to (stroker, &stroker->first_point);
if (unlikely (status))
return status;
return _close_path (stroker);
}
static cairo_status_t
close_path_dashed (void *closure)
{
struct stroker *stroker = closure;
cairo_status_t status;
status = line_to_dashed (stroker, &stroker->first_point);
if (unlikely (status))
return status;
return _close_path (stroker);
}
cairo_int_status_t
_cairo_path_fixed_stroke_to_traps (const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_traps_t *traps)
{
struct stroker stroker;
cairo_status_t status;
status = stroker_init (&stroker, path, style,
ctm, ctm_inverse, tolerance,
traps);
if (unlikely (status))
return status;
if (stroker.dash.dashed)
status = _cairo_path_fixed_interpret (path,
move_to_dashed,
line_to_dashed,
curve_to_dashed,
close_path_dashed,
&stroker);
else
status = _cairo_path_fixed_interpret (path,
move_to,
line_to,
curve_to,
close_path,
&stroker);
assert(status == CAIRO_STATUS_SUCCESS);
add_caps (&stroker);
stroker_fini (&stroker);
return traps->status;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-stroke-tristrip.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#define _DEFAULT_SOURCE /* for hypot() */
#include "cairoint.h"
#include "cairo-box-inline.h"
#include "cairo-boxes-private.h"
#include "cairo-error-private.h"
#include "cairo-path-fixed-private.h"
#include "cairo-slope-private.h"
#include "cairo-tristrip-private.h"
struct stroker {
cairo_stroke_style_t style;
cairo_tristrip_t *strip;
const cairo_matrix_t *ctm;
const cairo_matrix_t *ctm_inverse;
double tolerance;
cairo_bool_t ctm_det_positive;
cairo_pen_t pen;
cairo_bool_t has_sub_path;
cairo_point_t first_point;
cairo_bool_t has_current_face;
cairo_stroke_face_t current_face;
cairo_bool_t has_first_face;
cairo_stroke_face_t first_face;
cairo_box_t limit;
cairo_bool_t has_limits;
};
static inline double
normalize_slope (double *dx, double *dy);
static void
compute_face (const cairo_point_t *point,
const cairo_slope_t *dev_slope,
struct stroker *stroker,
cairo_stroke_face_t *face);
static void
translate_point (cairo_point_t *point, const cairo_point_t *offset)
{
point->x += offset->x;
point->y += offset->y;
}
static int
slope_compare_sgn (double dx1, double dy1, double dx2, double dy2)
{
double c = (dx1 * dy2 - dx2 * dy1);
if (c > 0) return 1;
if (c < 0) return -1;
return 0;
}
static inline int
range_step (int i, int step, int max)
{
i += step;
if (i < 0)
i = max - 1;
if (i >= max)
i = 0;
return i;
}
/*
* Construct a fan around the midpoint using the vertices from pen between
* inpt and outpt.
*/
static void
add_fan (struct stroker *stroker,
const cairo_slope_t *in_vector,
const cairo_slope_t *out_vector,
const cairo_point_t *midpt,
const cairo_point_t *inpt,
const cairo_point_t *outpt,
cairo_bool_t clockwise)
{
int start, stop, step, i, npoints;
if (clockwise) {
step = 1;
start = _cairo_pen_find_active_cw_vertex_index (&stroker->pen,
in_vector);
if (_cairo_slope_compare (&stroker->pen.vertices[start].slope_cw,
in_vector) < 0)
start = range_step (start, 1, stroker->pen.num_vertices);
stop = _cairo_pen_find_active_cw_vertex_index (&stroker->pen,
out_vector);
if (_cairo_slope_compare (&stroker->pen.vertices[stop].slope_ccw,
out_vector) > 0)
{
stop = range_step (stop, -1, stroker->pen.num_vertices);
if (_cairo_slope_compare (&stroker->pen.vertices[stop].slope_cw,
in_vector) < 0)
return;
}
npoints = stop - start;
} else {
step = -1;
start = _cairo_pen_find_active_ccw_vertex_index (&stroker->pen,
in_vector);
if (_cairo_slope_compare (&stroker->pen.vertices[start].slope_ccw,
in_vector) < 0)
start = range_step (start, -1, stroker->pen.num_vertices);
stop = _cairo_pen_find_active_ccw_vertex_index (&stroker->pen,
out_vector);
if (_cairo_slope_compare (&stroker->pen.vertices[stop].slope_cw,
out_vector) > 0)
{
stop = range_step (stop, 1, stroker->pen.num_vertices);
if (_cairo_slope_compare (&stroker->pen.vertices[stop].slope_ccw,
in_vector) < 0)
return;
}
npoints = start - stop;
}
stop = range_step (stop, step, stroker->pen.num_vertices);
if (npoints < 0)
npoints += stroker->pen.num_vertices;
if (npoints <= 1)
return;
for (i = start;
i != stop;
i = range_step (i, step, stroker->pen.num_vertices))
{
cairo_point_t p = *midpt;
translate_point (&p, &stroker->pen.vertices[i].point);
//contour_add_point (stroker, c, &p);
}
}
static int
join_is_clockwise (const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out)
{
return _cairo_slope_compare (&in->dev_vector, &out->dev_vector) < 0;
}
static void
inner_join (struct stroker *stroker,
const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out,
int clockwise)
{
const cairo_point_t *outpt;
if (clockwise) {
outpt = &out->ccw;
} else {
outpt = &out->cw;
}
//contour_add_point (stroker, inner, &in->point);
//contour_add_point (stroker, inner, outpt);
}
static void
inner_close (struct stroker *stroker,
const cairo_stroke_face_t *in,
cairo_stroke_face_t *out)
{
const cairo_point_t *inpt;
if (join_is_clockwise (in, out)) {
inpt = &out->ccw;
} else {
inpt = &out->cw;
}
//contour_add_point (stroker, inner, &in->point);
//contour_add_point (stroker, inner, inpt);
//*_cairo_contour_first_point (&inner->contour) =
//*_cairo_contour_last_point (&inner->contour);
}
static void
outer_close (struct stroker *stroker,
const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out)
{
const cairo_point_t *inpt, *outpt;
int clockwise;
if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
{
return;
}
clockwise = join_is_clockwise (in, out);
if (clockwise) {
inpt = &in->cw;
outpt = &out->cw;
} else {
inpt = &in->ccw;
outpt = &out->ccw;
}
switch (stroker->style.line_join) {
case CAIRO_LINE_JOIN_ROUND:
/* construct a fan around the common midpoint */
add_fan (stroker,
&in->dev_vector,
&out->dev_vector,
&in->point, inpt, outpt,
clockwise);
break;
case CAIRO_LINE_JOIN_MITER:
default: {
/* dot product of incoming slope vector with outgoing slope vector */
double in_dot_out = -in->usr_vector.x * out->usr_vector.x +
-in->usr_vector.y * out->usr_vector.y;
double ml = stroker->style.miter_limit;
/* Check the miter limit -- lines meeting at an acute angle
* can generate long miters, the limit converts them to bevel
*
* Consider the miter join formed when two line segments
* meet at an angle psi:
*
* /.\
* /. .\
* /./ \.\
* /./psi\.\
*
* We can zoom in on the right half of that to see:
*
* |\
* | \ psi/2
* | \
* | \
* | \
* | \
* miter \
* length \
* | \
* | .\
* | . \
* |. line \
* \ width \
* \ \
*
*
* The right triangle in that figure, (the line-width side is
* shown faintly with three '.' characters), gives us the
* following expression relating miter length, angle and line
* width:
*
* 1 /sin (psi/2) = miter_length / line_width
*
* The right-hand side of this relationship is the same ratio
* in which the miter limit (ml) is expressed. We want to know
* when the miter length is within the miter limit. That is
* when the following condition holds:
*
* 1/sin(psi/2) <= ml
* 1 <= ml sin(psi/2)
* 1 <= ml² sin²(psi/2)
* 2 <= ml² 2 sin²(psi/2)
* 2·sin²(psi/2) = 1-cos(psi)
* 2 <= ml² (1-cos(psi))
*
* in · out = |in| |out| cos (psi)
*
* in and out are both unit vectors, so:
*
* in · out = cos (psi)
*
* 2 <= ml² (1 - in · out)
*
*/
if (2 <= ml * ml * (1 - in_dot_out)) {
double x1, y1, x2, y2;
double mx, my;
double dx1, dx2, dy1, dy2;
double ix, iy;
double fdx1, fdy1, fdx2, fdy2;
double mdx, mdy;
/*
* we've got the points already transformed to device
* space, but need to do some computation with them and
* also need to transform the slope from user space to
* device space
*/
/* outer point of incoming line face */
x1 = _cairo_fixed_to_double (inpt->x);
y1 = _cairo_fixed_to_double (inpt->y);
dx1 = in->usr_vector.x;
dy1 = in->usr_vector.y;
cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);
/* outer point of outgoing line face */
x2 = _cairo_fixed_to_double (outpt->x);
y2 = _cairo_fixed_to_double (outpt->y);
dx2 = out->usr_vector.x;
dy2 = out->usr_vector.y;
cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
/*
* Compute the location of the outer corner of the miter.
* That's pretty easy -- just the intersection of the two
* outer edges. We've got slopes and points on each
* of those edges. Compute my directly, then compute
* mx by using the edge with the larger dy; that avoids
* dividing by values close to zero.
*/
my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
(dx1 * dy2 - dx2 * dy1));
if (fabs (dy1) >= fabs (dy2))
mx = (my - y1) * dx1 / dy1 + x1;
else
mx = (my - y2) * dx2 / dy2 + x2;
/*
* When the two outer edges are nearly parallel, slight
* perturbations in the position of the outer points of the lines
* caused by representing them in fixed point form can cause the
* intersection point of the miter to move a large amount. If
* that moves the miter intersection from between the two faces,
* then draw a bevel instead.
*/
ix = _cairo_fixed_to_double (in->point.x);
iy = _cairo_fixed_to_double (in->point.y);
/* slope of one face */
fdx1 = x1 - ix; fdy1 = y1 - iy;
/* slope of the other face */
fdx2 = x2 - ix; fdy2 = y2 - iy;
/* slope from the intersection to the miter point */
mdx = mx - ix; mdy = my - iy;
/*
* Make sure the miter point line lies between the two
* faces by comparing the slopes
*/
if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
slope_compare_sgn (fdx2, fdy2, mdx, mdy))
{
cairo_point_t p;
p.x = _cairo_fixed_from_double (mx);
p.y = _cairo_fixed_from_double (my);
//*_cairo_contour_last_point (&outer->contour) = p;
//*_cairo_contour_first_point (&outer->contour) = p;
return;
}
}
break;
}
case CAIRO_LINE_JOIN_BEVEL:
break;
}
//contour_add_point (stroker, outer, outpt);
}
static void
outer_join (struct stroker *stroker,
const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out,
int clockwise)
{
const cairo_point_t *inpt, *outpt;
if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
{
return;
}
if (clockwise) {
inpt = &in->cw;
outpt = &out->cw;
} else {
inpt = &in->ccw;
outpt = &out->ccw;
}
switch (stroker->style.line_join) {
case CAIRO_LINE_JOIN_ROUND:
/* construct a fan around the common midpoint */
add_fan (stroker,
&in->dev_vector,
&out->dev_vector,
&in->point, inpt, outpt,
clockwise);
break;
case CAIRO_LINE_JOIN_MITER:
default: {
/* dot product of incoming slope vector with outgoing slope vector */
double in_dot_out = -in->usr_vector.x * out->usr_vector.x +
-in->usr_vector.y * out->usr_vector.y;
double ml = stroker->style.miter_limit;
/* Check the miter limit -- lines meeting at an acute angle
* can generate long miters, the limit converts them to bevel
*
* Consider the miter join formed when two line segments
* meet at an angle psi:
*
* /.\
* /. .\
* /./ \.\
* /./psi\.\
*
* We can zoom in on the right half of that to see:
*
* |\
* | \ psi/2
* | \
* | \
* | \
* | \
* miter \
* length \
* | \
* | .\
* | . \
* |. line \
* \ width \
* \ \
*
*
* The right triangle in that figure, (the line-width side is
* shown faintly with three '.' characters), gives us the
* following expression relating miter length, angle and line
* width:
*
* 1 /sin (psi/2) = miter_length / line_width
*
* The right-hand side of this relationship is the same ratio
* in which the miter limit (ml) is expressed. We want to know
* when the miter length is within the miter limit. That is
* when the following condition holds:
*
* 1/sin(psi/2) <= ml
* 1 <= ml sin(psi/2)
* 1 <= ml² sin²(psi/2)
* 2 <= ml² 2 sin²(psi/2)
* 2·sin²(psi/2) = 1-cos(psi)
* 2 <= ml² (1-cos(psi))
*
* in · out = |in| |out| cos (psi)
*
* in and out are both unit vectors, so:
*
* in · out = cos (psi)
*
* 2 <= ml² (1 - in · out)
*
*/
if (2 <= ml * ml * (1 - in_dot_out)) {
double x1, y1, x2, y2;
double mx, my;
double dx1, dx2, dy1, dy2;
double ix, iy;
double fdx1, fdy1, fdx2, fdy2;
double mdx, mdy;
/*
* we've got the points already transformed to device
* space, but need to do some computation with them and
* also need to transform the slope from user space to
* device space
*/
/* outer point of incoming line face */
x1 = _cairo_fixed_to_double (inpt->x);
y1 = _cairo_fixed_to_double (inpt->y);
dx1 = in->usr_vector.x;
dy1 = in->usr_vector.y;
cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);
/* outer point of outgoing line face */
x2 = _cairo_fixed_to_double (outpt->x);
y2 = _cairo_fixed_to_double (outpt->y);
dx2 = out->usr_vector.x;
dy2 = out->usr_vector.y;
cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
/*
* Compute the location of the outer corner of the miter.
* That's pretty easy -- just the intersection of the two
* outer edges. We've got slopes and points on each
* of those edges. Compute my directly, then compute
* mx by using the edge with the larger dy; that avoids
* dividing by values close to zero.
*/
my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
(dx1 * dy2 - dx2 * dy1));
if (fabs (dy1) >= fabs (dy2))
mx = (my - y1) * dx1 / dy1 + x1;
else
mx = (my - y2) * dx2 / dy2 + x2;
/*
* When the two outer edges are nearly parallel, slight
* perturbations in the position of the outer points of the lines
* caused by representing them in fixed point form can cause the
* intersection point of the miter to move a large amount. If
* that moves the miter intersection from between the two faces,
* then draw a bevel instead.
*/
ix = _cairo_fixed_to_double (in->point.x);
iy = _cairo_fixed_to_double (in->point.y);
/* slope of one face */
fdx1 = x1 - ix; fdy1 = y1 - iy;
/* slope of the other face */
fdx2 = x2 - ix; fdy2 = y2 - iy;
/* slope from the intersection to the miter point */
mdx = mx - ix; mdy = my - iy;
/*
* Make sure the miter point line lies between the two
* faces by comparing the slopes
*/
if (slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
slope_compare_sgn (fdx2, fdy2, mdx, mdy))
{
cairo_point_t p;
p.x = _cairo_fixed_from_double (mx);
p.y = _cairo_fixed_from_double (my);
//*_cairo_contour_last_point (&outer->contour) = p;
return;
}
}
break;
}
case CAIRO_LINE_JOIN_BEVEL:
break;
}
//contour_add_point (stroker,outer, outpt);
}
static void
add_cap (struct stroker *stroker,
const cairo_stroke_face_t *f)
{
switch (stroker->style.line_cap) {
case CAIRO_LINE_CAP_ROUND: {
cairo_slope_t slope;
slope.dx = -f->dev_vector.dx;
slope.dy = -f->dev_vector.dy;
add_fan (stroker, &f->dev_vector, &slope,
&f->point, &f->ccw, &f->cw,
FALSE);
break;
}
case CAIRO_LINE_CAP_SQUARE: {
double dx, dy;
cairo_slope_t fvector;
cairo_point_t quad[4];
dx = f->usr_vector.x;
dy = f->usr_vector.y;
dx *= stroker->style.line_width / 2.0;
dy *= stroker->style.line_width / 2.0;
cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);
fvector.dx = _cairo_fixed_from_double (dx);
fvector.dy = _cairo_fixed_from_double (dy);
quad[0] = f->ccw;
quad[1].x = f->ccw.x + fvector.dx;
quad[1].y = f->ccw.y + fvector.dy;
quad[2].x = f->cw.x + fvector.dx;
quad[2].y = f->cw.y + fvector.dy;
quad[3] = f->cw;
//contour_add_point (stroker, c, &quad[1]);
//contour_add_point (stroker, c, &quad[2]);
}
case CAIRO_LINE_CAP_BUTT:
default:
break;
}
//contour_add_point (stroker, c, &f->cw);
}
static void
add_leading_cap (struct stroker *stroker,
const cairo_stroke_face_t *face)
{
cairo_stroke_face_t reversed;
cairo_point_t t;
reversed = *face;
/* The initial cap needs an outward facing vector. Reverse everything */
reversed.usr_vector.x = -reversed.usr_vector.x;
reversed.usr_vector.y = -reversed.usr_vector.y;
reversed.dev_vector.dx = -reversed.dev_vector.dx;
reversed.dev_vector.dy = -reversed.dev_vector.dy;
t = reversed.cw;
reversed.cw = reversed.ccw;
reversed.ccw = t;
add_cap (stroker, &reversed);
}
static void
add_trailing_cap (struct stroker *stroker,
const cairo_stroke_face_t *face)
{
add_cap (stroker, face);
}
static inline double
normalize_slope (double *dx, double *dy)
{
double dx0 = *dx, dy0 = *dy;
double mag;
assert (dx0 != 0.0 || dy0 != 0.0);
if (dx0 == 0.0) {
*dx = 0.0;
if (dy0 > 0.0) {
mag = dy0;
*dy = 1.0;
} else {
mag = -dy0;
*dy = -1.0;
}
} else if (dy0 == 0.0) {
*dy = 0.0;
if (dx0 > 0.0) {
mag = dx0;
*dx = 1.0;
} else {
mag = -dx0;
*dx = -1.0;
}
} else {
mag = hypot (dx0, dy0);
*dx = dx0 / mag;
*dy = dy0 / mag;
}
return mag;
}
static void
compute_face (const cairo_point_t *point,
const cairo_slope_t *dev_slope,
struct stroker *stroker,
cairo_stroke_face_t *face)
{
double face_dx, face_dy;
cairo_point_t offset_ccw, offset_cw;
double slope_dx, slope_dy;
slope_dx = _cairo_fixed_to_double (dev_slope->dx);
slope_dy = _cairo_fixed_to_double (dev_slope->dy);
face->length = normalize_slope (&slope_dx, &slope_dy);
face->dev_slope.x = slope_dx;
face->dev_slope.y = slope_dy;
/*
* rotate to get a line_width/2 vector along the face, note that
* the vector must be rotated the right direction in device space,
* but by 90° in user space. So, the rotation depends on
* whether the ctm reflects or not, and that can be determined
* by looking at the determinant of the matrix.
*/
if (! _cairo_matrix_is_identity (stroker->ctm_inverse)) {
/* Normalize the matrix! */
cairo_matrix_transform_distance (stroker->ctm_inverse,
&slope_dx, &slope_dy);
normalize_slope (&slope_dx, &slope_dy);
if (stroker->ctm_det_positive) {
face_dx = - slope_dy * (stroker->style.line_width / 2.0);
face_dy = slope_dx * (stroker->style.line_width / 2.0);
} else {
face_dx = slope_dy * (stroker->style.line_width / 2.0);
face_dy = - slope_dx * (stroker->style.line_width / 2.0);
}
/* back to device space */
cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);
} else {
face_dx = - slope_dy * (stroker->style.line_width / 2.0);
face_dy = slope_dx * (stroker->style.line_width / 2.0);
}
offset_ccw.x = _cairo_fixed_from_double (face_dx);
offset_ccw.y = _cairo_fixed_from_double (face_dy);
offset_cw.x = -offset_ccw.x;
offset_cw.y = -offset_ccw.y;
face->ccw = *point;
translate_point (&face->ccw, &offset_ccw);
face->point = *point;
face->cw = *point;
translate_point (&face->cw, &offset_cw);
face->usr_vector.x = slope_dx;
face->usr_vector.y = slope_dy;
face->dev_vector = *dev_slope;
}
static void
add_caps (struct stroker *stroker)
{
/* check for a degenerative sub_path */
if (stroker->has_sub_path &&
! stroker->has_first_face &&
! stroker->has_current_face &&
stroker->style.line_cap == CAIRO_LINE_CAP_ROUND)
{
/* pick an arbitrary slope to use */
cairo_slope_t slope = { CAIRO_FIXED_ONE, 0 };
cairo_stroke_face_t face;
/* arbitrarily choose first_point */
compute_face (&stroker->first_point, &slope, stroker, &face);
add_leading_cap (stroker, &face);
add_trailing_cap (stroker, &face);
/* ensure the circle is complete */
//_cairo_contour_add_point (&stroker->ccw.contour,
//_cairo_contour_first_point (&stroker->ccw.contour));
} else {
if (stroker->has_current_face)
add_trailing_cap (stroker, &stroker->current_face);
//_cairo_polygon_add_contour (stroker->polygon, &stroker->ccw.contour);
//_cairo_contour_reset (&stroker->ccw.contour);
if (stroker->has_first_face) {
//_cairo_contour_add_point (&stroker->ccw.contour,
//&stroker->first_face.cw);
add_leading_cap (stroker, &stroker->first_face);
//_cairo_polygon_add_contour (stroker->polygon,
//&stroker->ccw.contour);
//_cairo_contour_reset (&stroker->ccw.contour);
}
}
}
static cairo_status_t
move_to (void *closure,
const cairo_point_t *point)
{
struct stroker *stroker = closure;
/* Cap the start and end of the previous sub path as needed */
add_caps (stroker);
stroker->has_first_face = FALSE;
stroker->has_current_face = FALSE;
stroker->has_sub_path = FALSE;
stroker->first_point = *point;
stroker->current_face.point = *point;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
line_to (void *closure,
const cairo_point_t *point)
{
struct stroker *stroker = closure;
cairo_stroke_face_t start;
cairo_point_t *p1 = &stroker->current_face.point;
cairo_slope_t dev_slope;
stroker->has_sub_path = TRUE;
if (p1->x == point->x && p1->y == point->y)
return CAIRO_STATUS_SUCCESS;
_cairo_slope_init (&dev_slope, p1, point);
compute_face (p1, &dev_slope, stroker, &start);
if (stroker->has_current_face) {
int clockwise = join_is_clockwise (&stroker->current_face, &start);
/* Join with final face from previous segment */
outer_join (stroker, &stroker->current_face, &start, clockwise);
inner_join (stroker, &stroker->current_face, &start, clockwise);
} else {
if (! stroker->has_first_face) {
/* Save sub path's first face in case needed for closing join */
stroker->first_face = start;
_cairo_tristrip_move_to (stroker->strip, &start.cw);
stroker->has_first_face = TRUE;
}
stroker->has_current_face = TRUE;
_cairo_tristrip_add_point (stroker->strip, &start.cw);
_cairo_tristrip_add_point (stroker->strip, &start.ccw);
}
stroker->current_face = start;
stroker->current_face.point = *point;
stroker->current_face.ccw.x += dev_slope.dx;
stroker->current_face.ccw.y += dev_slope.dy;
stroker->current_face.cw.x += dev_slope.dx;
stroker->current_face.cw.y += dev_slope.dy;
_cairo_tristrip_add_point (stroker->strip, &stroker->current_face.cw);
_cairo_tristrip_add_point (stroker->strip, &stroker->current_face.ccw);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
spline_to (void *closure,
const cairo_point_t *point,
const cairo_slope_t *tangent)
{
struct stroker *stroker = closure;
cairo_stroke_face_t face;
if (tangent->dx == 0 && tangent->dy == 0) {
const cairo_point_t *inpt, *outpt;
cairo_point_t t;
int clockwise;
face = stroker->current_face;
face.usr_vector.x = -face.usr_vector.x;
face.usr_vector.y = -face.usr_vector.y;
face.dev_vector.dx = -face.dev_vector.dx;
face.dev_vector.dy = -face.dev_vector.dy;
t = face.cw;
face.cw = face.ccw;
face.ccw = t;
clockwise = join_is_clockwise (&stroker->current_face, &face);
if (clockwise) {
inpt = &stroker->current_face.cw;
outpt = &face.cw;
} else {
inpt = &stroker->current_face.ccw;
outpt = &face.ccw;
}
add_fan (stroker,
&stroker->current_face.dev_vector,
&face.dev_vector,
&stroker->current_face.point, inpt, outpt,
clockwise);
} else {
compute_face (point, tangent, stroker, &face);
if (face.dev_slope.x * stroker->current_face.dev_slope.x +
face.dev_slope.y * stroker->current_face.dev_slope.y < 0)
{
const cairo_point_t *inpt, *outpt;
int clockwise = join_is_clockwise (&stroker->current_face, &face);
stroker->current_face.cw.x += face.point.x - stroker->current_face.point.x;
stroker->current_face.cw.y += face.point.y - stroker->current_face.point.y;
//contour_add_point (stroker, &stroker->cw, &stroker->current_face.cw);
stroker->current_face.ccw.x += face.point.x - stroker->current_face.point.x;
stroker->current_face.ccw.y += face.point.y - stroker->current_face.point.y;
//contour_add_point (stroker, &stroker->ccw, &stroker->current_face.ccw);
if (clockwise) {
inpt = &stroker->current_face.cw;
outpt = &face.cw;
} else {
inpt = &stroker->current_face.ccw;
outpt = &face.ccw;
}
add_fan (stroker,
&stroker->current_face.dev_vector,
&face.dev_vector,
&stroker->current_face.point, inpt, outpt,
clockwise);
}
_cairo_tristrip_add_point (stroker->strip, &face.cw);
_cairo_tristrip_add_point (stroker->strip, &face.ccw);
}
stroker->current_face = face;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
curve_to (void *closure,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
struct stroker *stroker = closure;
cairo_spline_t spline;
cairo_stroke_face_t face;
if (stroker->has_limits) {
if (! _cairo_spline_intersects (&stroker->current_face.point, b, c, d,
&stroker->limit))
return line_to (closure, d);
}
if (! _cairo_spline_init (&spline, spline_to, stroker,
&stroker->current_face.point, b, c, d))
return line_to (closure, d);
compute_face (&stroker->current_face.point, &spline.initial_slope,
stroker, &face);
if (stroker->has_current_face) {
int clockwise = join_is_clockwise (&stroker->current_face, &face);
/* Join with final face from previous segment */
outer_join (stroker, &stroker->current_face, &face, clockwise);
inner_join (stroker, &stroker->current_face, &face, clockwise);
} else {
if (! stroker->has_first_face) {
/* Save sub path's first face in case needed for closing join */
stroker->first_face = face;
_cairo_tristrip_move_to (stroker->strip, &face.cw);
stroker->has_first_face = TRUE;
}
stroker->has_current_face = TRUE;
_cairo_tristrip_add_point (stroker->strip, &face.cw);
_cairo_tristrip_add_point (stroker->strip, &face.ccw);
}
stroker->current_face = face;
return _cairo_spline_decompose (&spline, stroker->tolerance);
}
static cairo_status_t
close_path (void *closure)
{
struct stroker *stroker = closure;
cairo_status_t status;
status = line_to (stroker, &stroker->first_point);
if (unlikely (status))
return status;
if (stroker->has_first_face && stroker->has_current_face) {
/* Join first and final faces of sub path */
outer_close (stroker, &stroker->current_face, &stroker->first_face);
inner_close (stroker, &stroker->current_face, &stroker->first_face);
} else {
/* Cap the start and end of the sub path as needed */
add_caps (stroker);
}
stroker->has_sub_path = FALSE;
stroker->has_first_face = FALSE;
stroker->has_current_face = FALSE;
return CAIRO_STATUS_SUCCESS;
}
cairo_int_status_t
_cairo_path_fixed_stroke_to_tristrip (const cairo_path_fixed_t *path,
const cairo_stroke_style_t*style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_tristrip_t *strip)
{
struct stroker stroker;
cairo_int_status_t status;
int i;
if (style->num_dashes)
return CAIRO_INT_STATUS_UNSUPPORTED;
stroker.style = *style;
stroker.ctm = ctm;
stroker.ctm_inverse = ctm_inverse;
stroker.tolerance = tolerance;
stroker.ctm_det_positive =
_cairo_matrix_compute_determinant (ctm) >= 0.0;
status = _cairo_pen_init (&stroker.pen,
style->line_width / 2.0,
tolerance, ctm);
if (unlikely (status))
return status;
if (stroker.pen.num_vertices <= 1)
return CAIRO_INT_STATUS_NOTHING_TO_DO;
stroker.has_current_face = FALSE;
stroker.has_first_face = FALSE;
stroker.has_sub_path = FALSE;
stroker.has_limits = strip->num_limits > 0;
stroker.limit = strip->limits[0];
for (i = 1; i < strip->num_limits; i++)
_cairo_box_add_box (&stroker.limit, &strip->limits[i]);
stroker.strip = strip;
status = _cairo_path_fixed_interpret (path,
move_to,
line_to,
curve_to,
close_path,
&stroker);
/* Cap the start and end of the final sub path as needed */
if (likely (status == CAIRO_INT_STATUS_SUCCESS))
add_caps (&stroker);
_cairo_pen_fini (&stroker.pen);
return status;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path-stroke.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#define _DEFAULT_SOURCE /* for hypot() */
#include "cairoint.h"
#include "cairo-box-inline.h"
#include "cairo-boxes-private.h"
#include "cairo-error-private.h"
#include "cairo-path-fixed-private.h"
#include "cairo-slope-private.h"
#include "cairo-stroke-dash-private.h"
#include "cairo-traps-private.h"
typedef struct cairo_stroker {
cairo_stroke_style_t style;
const cairo_matrix_t *ctm;
const cairo_matrix_t *ctm_inverse;
double half_line_width;
double tolerance;
double spline_cusp_tolerance;
double ctm_determinant;
cairo_bool_t ctm_det_positive;
void *closure;
cairo_status_t (*add_external_edge) (void *closure,
const cairo_point_t *p1,
const cairo_point_t *p2);
cairo_status_t (*add_triangle) (void *closure,
const cairo_point_t triangle[3]);
cairo_status_t (*add_triangle_fan) (void *closure,
const cairo_point_t *midpt,
const cairo_point_t *points,
int npoints);
cairo_status_t (*add_convex_quad) (void *closure,
const cairo_point_t quad[4]);
cairo_pen_t pen;
cairo_point_t current_point;
cairo_point_t first_point;
cairo_bool_t has_initial_sub_path;
cairo_bool_t has_current_face;
cairo_stroke_face_t current_face;
cairo_bool_t has_first_face;
cairo_stroke_face_t first_face;
cairo_stroker_dash_t dash;
cairo_bool_t has_bounds;
cairo_box_t bounds;
} cairo_stroker_t;
static void
_cairo_stroker_limit (cairo_stroker_t *stroker,
const cairo_path_fixed_t *path,
const cairo_box_t *boxes,
int num_boxes)
{
double dx, dy;
cairo_fixed_t fdx, fdy;
stroker->has_bounds = TRUE;
_cairo_boxes_get_extents (boxes, num_boxes, &stroker->bounds);
/* Extend the bounds in each direction to account for the maximum area
* we might generate trapezoids, to capture line segments that are outside
* of the bounds but which might generate rendering that's within bounds.
*/
_cairo_stroke_style_max_distance_from_path (&stroker->style, path,
stroker->ctm, &dx, &dy);
fdx = _cairo_fixed_from_double (dx);
fdy = _cairo_fixed_from_double (dy);
stroker->bounds.p1.x -= fdx;
stroker->bounds.p2.x += fdx;
stroker->bounds.p1.y -= fdy;
stroker->bounds.p2.y += fdy;
}
static cairo_status_t
_cairo_stroker_init (cairo_stroker_t *stroker,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *stroke_style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
const cairo_box_t *limits,
int num_limits)
{
cairo_status_t status;
stroker->style = *stroke_style;
stroker->ctm = ctm;
stroker->ctm_inverse = ctm_inverse;
stroker->tolerance = tolerance;
stroker->half_line_width = stroke_style->line_width / 2.0;
/* To test whether we need to join two segments of a spline using
* a round-join or a bevel-join, we can inspect the angle between the
* two segments. If the difference between the chord distance
* (half-line-width times the cosine of the bisection angle) and the
* half-line-width itself is greater than tolerance then we need to
* inject a point.
*/
stroker->spline_cusp_tolerance = 1 - tolerance / stroker->half_line_width;
stroker->spline_cusp_tolerance *= stroker->spline_cusp_tolerance;
stroker->spline_cusp_tolerance *= 2;
stroker->spline_cusp_tolerance -= 1;
stroker->ctm_determinant = _cairo_matrix_compute_determinant (stroker->ctm);
stroker->ctm_det_positive = stroker->ctm_determinant >= 0.0;
status = _cairo_pen_init (&stroker->pen,
stroker->half_line_width, tolerance, ctm);
if (unlikely (status))
return status;
stroker->has_current_face = FALSE;
stroker->has_first_face = FALSE;
stroker->has_initial_sub_path = FALSE;
_cairo_stroker_dash_init (&stroker->dash, stroke_style);
stroker->add_external_edge = NULL;
stroker->has_bounds = FALSE;
if (num_limits)
_cairo_stroker_limit (stroker, path, limits, num_limits);
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_stroker_fini (cairo_stroker_t *stroker)
{
_cairo_pen_fini (&stroker->pen);
}
static void
_translate_point (cairo_point_t *point, const cairo_point_t *offset)
{
point->x += offset->x;
point->y += offset->y;
}
static int
_cairo_stroker_join_is_clockwise (const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out)
{
cairo_slope_t in_slope, out_slope;
_cairo_slope_init (&in_slope, &in->point, &in->cw);
_cairo_slope_init (&out_slope, &out->point, &out->cw);
return _cairo_slope_compare (&in_slope, &out_slope) < 0;
}
/**
* _cairo_slope_compare_sgn:
*
* Return -1, 0 or 1 depending on the relative slopes of
* two lines.
**/
static int
_cairo_slope_compare_sgn (double dx1, double dy1, double dx2, double dy2)
{
double c = (dx1 * dy2 - dx2 * dy1);
if (c > 0) return 1;
if (c < 0) return -1;
return 0;
}
static inline int
_range_step (int i, int step, int max)
{
i += step;
if (i < 0)
i = max - 1;
if (i >= max)
i = 0;
return i;
}
/*
* Construct a fan around the midpoint using the vertices from pen between
* inpt and outpt.
*/
static cairo_status_t
_tessellate_fan (cairo_stroker_t *stroker,
const cairo_slope_t *in_vector,
const cairo_slope_t *out_vector,
const cairo_point_t *midpt,
const cairo_point_t *inpt,
const cairo_point_t *outpt,
cairo_bool_t clockwise)
{
cairo_point_t stack_points[64], *points = stack_points;
cairo_pen_t *pen = &stroker->pen;
int start, stop, num_points = 0;
cairo_status_t status;
if (stroker->has_bounds &&
! _cairo_box_contains_point (&stroker->bounds, midpt))
goto BEVEL;
assert (stroker->pen.num_vertices);
if (clockwise) {
_cairo_pen_find_active_ccw_vertices (pen,
in_vector, out_vector,
&start, &stop);
if (stroker->add_external_edge) {
cairo_point_t last;
last = *inpt;
while (start != stop) {
cairo_point_t p = *midpt;
_translate_point (&p, &pen->vertices[start].point);
status = stroker->add_external_edge (stroker->closure,
&last, &p);
if (unlikely (status))
return status;
last = p;
if (start-- == 0)
start += pen->num_vertices;
}
status = stroker->add_external_edge (stroker->closure,
&last, outpt);
} else {
if (start == stop)
goto BEVEL;
num_points = stop - start;
if (num_points < 0)
num_points += pen->num_vertices;
num_points += 2;
if (num_points > ARRAY_LENGTH(stack_points)) {
points = _cairo_malloc_ab (num_points, sizeof (cairo_point_t));
if (unlikely (points == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
points[0] = *inpt;
num_points = 1;
while (start != stop) {
points[num_points] = *midpt;
_translate_point (&points[num_points], &pen->vertices[start].point);
num_points++;
if (start-- == 0)
start += pen->num_vertices;
}
points[num_points++] = *outpt;
}
} else {
_cairo_pen_find_active_cw_vertices (pen,
in_vector, out_vector,
&start, &stop);
if (stroker->add_external_edge) {
cairo_point_t last;
last = *inpt;
while (start != stop) {
cairo_point_t p = *midpt;
_translate_point (&p, &pen->vertices[start].point);
status = stroker->add_external_edge (stroker->closure,
&p, &last);
if (unlikely (status))
return status;
last = p;
if (++start == pen->num_vertices)
start = 0;
}
status = stroker->add_external_edge (stroker->closure,
outpt, &last);
} else {
if (start == stop)
goto BEVEL;
num_points = stop - start;
if (num_points < 0)
num_points += pen->num_vertices;
num_points += 2;
if (num_points > ARRAY_LENGTH(stack_points)) {
points = _cairo_malloc_ab (num_points, sizeof (cairo_point_t));
if (unlikely (points == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
points[0] = *inpt;
num_points = 1;
while (start != stop) {
points[num_points] = *midpt;
_translate_point (&points[num_points], &pen->vertices[start].point);
num_points++;
if (++start == pen->num_vertices)
start = 0;
}
points[num_points++] = *outpt;
}
}
if (num_points) {
status = stroker->add_triangle_fan (stroker->closure,
midpt, points, num_points);
}
if (points != stack_points)
free (points);
return status;
BEVEL:
/* Ensure a leak free connection... */
if (stroker->add_external_edge != NULL) {
if (clockwise)
return stroker->add_external_edge (stroker->closure, inpt, outpt);
else
return stroker->add_external_edge (stroker->closure, outpt, inpt);
} else {
stack_points[0] = *midpt;
stack_points[1] = *inpt;
stack_points[2] = *outpt;
return stroker->add_triangle (stroker->closure, stack_points);
}
}
static cairo_status_t
_cairo_stroker_join (cairo_stroker_t *stroker,
const cairo_stroke_face_t *in,
const cairo_stroke_face_t *out)
{
int clockwise = _cairo_stroker_join_is_clockwise (out, in);
const cairo_point_t *inpt, *outpt;
cairo_point_t points[4];
cairo_status_t status;
if (in->cw.x == out->cw.x && in->cw.y == out->cw.y &&
in->ccw.x == out->ccw.x && in->ccw.y == out->ccw.y)
{
return CAIRO_STATUS_SUCCESS;
}
if (clockwise) {
if (stroker->add_external_edge != NULL) {
status = stroker->add_external_edge (stroker->closure,
&out->cw, &in->point);
if (unlikely (status))
return status;
status = stroker->add_external_edge (stroker->closure,
&in->point, &in->cw);
if (unlikely (status))
return status;
}
inpt = &in->ccw;
outpt = &out->ccw;
} else {
if (stroker->add_external_edge != NULL) {
status = stroker->add_external_edge (stroker->closure,
&in->ccw, &in->point);
if (unlikely (status))
return status;
status = stroker->add_external_edge (stroker->closure,
&in->point, &out->ccw);
if (unlikely (status))
return status;
}
inpt = &in->cw;
outpt = &out->cw;
}
switch (stroker->style.line_join) {
case CAIRO_LINE_JOIN_ROUND:
/* construct a fan around the common midpoint */
return _tessellate_fan (stroker,
&in->dev_vector,
&out->dev_vector,
&in->point, inpt, outpt,
clockwise);
case CAIRO_LINE_JOIN_MITER:
default: {
/* dot product of incoming slope vector with outgoing slope vector */
double in_dot_out = -in->usr_vector.x * out->usr_vector.x +
-in->usr_vector.y * out->usr_vector.y;
double ml = stroker->style.miter_limit;
/* Check the miter limit -- lines meeting at an acute angle
* can generate long miters, the limit converts them to bevel
*
* Consider the miter join formed when two line segments
* meet at an angle psi:
*
* /.\
* /. .\
* /./ \.\
* /./psi\.\
*
* We can zoom in on the right half of that to see:
*
* |\
* | \ psi/2
* | \
* | \
* | \
* | \
* miter \
* length \
* | \
* | .\
* | . \
* |. line \
* \ width \
* \ \
*
*
* The right triangle in that figure, (the line-width side is
* shown faintly with three '.' characters), gives us the
* following expression relating miter length, angle and line
* width:
*
* 1 /sin (psi/2) = miter_length / line_width
*
* The right-hand side of this relationship is the same ratio
* in which the miter limit (ml) is expressed. We want to know
* when the miter length is within the miter limit. That is
* when the following condition holds:
*
* 1/sin(psi/2) <= ml
* 1 <= ml sin(psi/2)
* 1 <= ml² sin²(psi/2)
* 2 <= ml² 2 sin²(psi/2)
* 2·sin²(psi/2) = 1-cos(psi)
* 2 <= ml² (1-cos(psi))
*
* in · out = |in| |out| cos (psi)
*
* in and out are both unit vectors, so:
*
* in · out = cos (psi)
*
* 2 <= ml² (1 - in · out)
*
*/
if (2 <= ml * ml * (1 - in_dot_out)) {
double x1, y1, x2, y2;
double mx, my;
double dx1, dx2, dy1, dy2;
double ix, iy;
double fdx1, fdy1, fdx2, fdy2;
double mdx, mdy;
/*
* we've got the points already transformed to device
* space, but need to do some computation with them and
* also need to transform the slope from user space to
* device space
*/
/* outer point of incoming line face */
x1 = _cairo_fixed_to_double (inpt->x);
y1 = _cairo_fixed_to_double (inpt->y);
dx1 = in->usr_vector.x;
dy1 = in->usr_vector.y;
cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);
/* outer point of outgoing line face */
x2 = _cairo_fixed_to_double (outpt->x);
y2 = _cairo_fixed_to_double (outpt->y);
dx2 = out->usr_vector.x;
dy2 = out->usr_vector.y;
cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
/*
* Compute the location of the outer corner of the miter.
* That's pretty easy -- just the intersection of the two
* outer edges. We've got slopes and points on each
* of those edges. Compute my directly, then compute
* mx by using the edge with the larger dy; that avoids
* dividing by values close to zero.
*/
my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /
(dx1 * dy2 - dx2 * dy1));
if (fabs (dy1) >= fabs (dy2))
mx = (my - y1) * dx1 / dy1 + x1;
else
mx = (my - y2) * dx2 / dy2 + x2;
/*
* When the two outer edges are nearly parallel, slight
* perturbations in the position of the outer points of the lines
* caused by representing them in fixed point form can cause the
* intersection point of the miter to move a large amount. If
* that moves the miter intersection from between the two faces,
* then draw a bevel instead.
*/
ix = _cairo_fixed_to_double (in->point.x);
iy = _cairo_fixed_to_double (in->point.y);
/* slope of one face */
fdx1 = x1 - ix; fdy1 = y1 - iy;
/* slope of the other face */
fdx2 = x2 - ix; fdy2 = y2 - iy;
/* slope from the intersection to the miter point */
mdx = mx - ix; mdy = my - iy;
/*
* Make sure the miter point line lies between the two
* faces by comparing the slopes
*/
if (_cairo_slope_compare_sgn (fdx1, fdy1, mdx, mdy) !=
_cairo_slope_compare_sgn (fdx2, fdy2, mdx, mdy))
{
if (stroker->add_external_edge != NULL) {
points[0].x = _cairo_fixed_from_double (mx);
points[0].y = _cairo_fixed_from_double (my);
if (clockwise) {
status = stroker->add_external_edge (stroker->closure,
inpt, &points[0]);
if (unlikely (status))
return status;
status = stroker->add_external_edge (stroker->closure,
&points[0], outpt);
if (unlikely (status))
return status;
} else {
status = stroker->add_external_edge (stroker->closure,
outpt, &points[0]);
if (unlikely (status))
return status;
status = stroker->add_external_edge (stroker->closure,
&points[0], inpt);
if (unlikely (status))
return status;
}
return CAIRO_STATUS_SUCCESS;
} else {
points[0] = in->point;
points[1] = *inpt;
points[2].x = _cairo_fixed_from_double (mx);
points[2].y = _cairo_fixed_from_double (my);
points[3] = *outpt;
return stroker->add_convex_quad (stroker->closure, points);
}
}
}
}
/* fall through ... */
case CAIRO_LINE_JOIN_BEVEL:
if (stroker->add_external_edge != NULL) {
if (clockwise) {
return stroker->add_external_edge (stroker->closure,
inpt, outpt);
} else {
return stroker->add_external_edge (stroker->closure,
outpt, inpt);
}
} else {
points[0] = in->point;
points[1] = *inpt;
points[2] = *outpt;
return stroker->add_triangle (stroker->closure, points);
}
}
}
static cairo_status_t
_cairo_stroker_add_cap (cairo_stroker_t *stroker,
const cairo_stroke_face_t *f)
{
switch (stroker->style.line_cap) {
case CAIRO_LINE_CAP_ROUND: {
cairo_slope_t slope;
slope.dx = -f->dev_vector.dx;
slope.dy = -f->dev_vector.dy;
return _tessellate_fan (stroker,
&f->dev_vector,
&slope,
&f->point, &f->cw, &f->ccw,
FALSE);
}
case CAIRO_LINE_CAP_SQUARE: {
double dx, dy;
cairo_slope_t fvector;
cairo_point_t quad[4];
dx = f->usr_vector.x;
dy = f->usr_vector.y;
dx *= stroker->half_line_width;
dy *= stroker->half_line_width;
cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);
fvector.dx = _cairo_fixed_from_double (dx);
fvector.dy = _cairo_fixed_from_double (dy);
quad[0] = f->ccw;
quad[1].x = f->ccw.x + fvector.dx;
quad[1].y = f->ccw.y + fvector.dy;
quad[2].x = f->cw.x + fvector.dx;
quad[2].y = f->cw.y + fvector.dy;
quad[3] = f->cw;
if (stroker->add_external_edge != NULL) {
cairo_status_t status;
status = stroker->add_external_edge (stroker->closure,
&quad[0], &quad[1]);
if (unlikely (status))
return status;
status = stroker->add_external_edge (stroker->closure,
&quad[1], &quad[2]);
if (unlikely (status))
return status;
status = stroker->add_external_edge (stroker->closure,
&quad[2], &quad[3]);
if (unlikely (status))
return status;
return CAIRO_STATUS_SUCCESS;
} else {
return stroker->add_convex_quad (stroker->closure, quad);
}
}
case CAIRO_LINE_CAP_BUTT:
default:
if (stroker->add_external_edge != NULL) {
return stroker->add_external_edge (stroker->closure,
&f->ccw, &f->cw);
} else {
return CAIRO_STATUS_SUCCESS;
}
}
}
static cairo_status_t
_cairo_stroker_add_leading_cap (cairo_stroker_t *stroker,
const cairo_stroke_face_t *face)
{
cairo_stroke_face_t reversed;
cairo_point_t t;
reversed = *face;
/* The initial cap needs an outward facing vector. Reverse everything */
reversed.usr_vector.x = -reversed.usr_vector.x;
reversed.usr_vector.y = -reversed.usr_vector.y;
reversed.dev_vector.dx = -reversed.dev_vector.dx;
reversed.dev_vector.dy = -reversed.dev_vector.dy;
t = reversed.cw;
reversed.cw = reversed.ccw;
reversed.ccw = t;
return _cairo_stroker_add_cap (stroker, &reversed);
}
static cairo_status_t
_cairo_stroker_add_trailing_cap (cairo_stroker_t *stroker,
const cairo_stroke_face_t *face)
{
return _cairo_stroker_add_cap (stroker, face);
}
static inline cairo_bool_t
_compute_normalized_device_slope (double *dx, double *dy,
const cairo_matrix_t *ctm_inverse,
double *mag_out)
{
double dx0 = *dx, dy0 = *dy;
double mag;
cairo_matrix_transform_distance (ctm_inverse, &dx0, &dy0);
if (dx0 == 0.0 && dy0 == 0.0) {
if (mag_out)
*mag_out = 0.0;
return FALSE;
}
if (dx0 == 0.0) {
*dx = 0.0;
if (dy0 > 0.0) {
mag = dy0;
*dy = 1.0;
} else {
mag = -dy0;
*dy = -1.0;
}
} else if (dy0 == 0.0) {
*dy = 0.0;
if (dx0 > 0.0) {
mag = dx0;
*dx = 1.0;
} else {
mag = -dx0;
*dx = -1.0;
}
} else {
mag = hypot (dx0, dy0);
*dx = dx0 / mag;
*dy = dy0 / mag;
}
if (mag_out)
*mag_out = mag;
return TRUE;
}
static void
_compute_face (const cairo_point_t *point,
const cairo_slope_t *dev_slope,
double slope_dx,
double slope_dy,
cairo_stroker_t *stroker,
cairo_stroke_face_t *face)
{
double face_dx, face_dy;
cairo_point_t offset_ccw, offset_cw;
/*
* rotate to get a line_width/2 vector along the face, note that
* the vector must be rotated the right direction in device space,
* but by 90° in user space. So, the rotation depends on
* whether the ctm reflects or not, and that can be determined
* by looking at the determinant of the matrix.
*/
if (stroker->ctm_det_positive)
{
face_dx = - slope_dy * stroker->half_line_width;
face_dy = slope_dx * stroker->half_line_width;
}
else
{
face_dx = slope_dy * stroker->half_line_width;
face_dy = - slope_dx * stroker->half_line_width;
}
/* back to device space */
cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);
offset_ccw.x = _cairo_fixed_from_double (face_dx);
offset_ccw.y = _cairo_fixed_from_double (face_dy);
offset_cw.x = -offset_ccw.x;
offset_cw.y = -offset_ccw.y;
face->ccw = *point;
_translate_point (&face->ccw, &offset_ccw);
face->point = *point;
face->cw = *point;
_translate_point (&face->cw, &offset_cw);
face->usr_vector.x = slope_dx;
face->usr_vector.y = slope_dy;
face->dev_vector = *dev_slope;
}
static cairo_status_t
_cairo_stroker_add_caps (cairo_stroker_t *stroker)
{
cairo_status_t status;
/* check for a degenerative sub_path */
if (stroker->has_initial_sub_path
&& ! stroker->has_first_face
&& ! stroker->has_current_face
&& stroker->style.line_cap == CAIRO_LINE_CAP_ROUND)
{
/* pick an arbitrary slope to use */
double dx = 1.0, dy = 0.0;
cairo_slope_t slope = { CAIRO_FIXED_ONE, 0 };
cairo_stroke_face_t face;
_compute_normalized_device_slope (&dx, &dy,
stroker->ctm_inverse, NULL);
/* arbitrarily choose first_point
* first_point and current_point should be the same */
_compute_face (&stroker->first_point, &slope, dx, dy, stroker, &face);
status = _cairo_stroker_add_leading_cap (stroker, &face);
if (unlikely (status))
return status;
status = _cairo_stroker_add_trailing_cap (stroker, &face);
if (unlikely (status))
return status;
}
if (stroker->has_first_face) {
status = _cairo_stroker_add_leading_cap (stroker,
&stroker->first_face);
if (unlikely (status))
return status;
}
if (stroker->has_current_face) {
status = _cairo_stroker_add_trailing_cap (stroker,
&stroker->current_face);
if (unlikely (status))
return status;
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_stroker_add_sub_edge (cairo_stroker_t *stroker,
const cairo_point_t *p1,
const cairo_point_t *p2,
cairo_slope_t *dev_slope,
double slope_dx, double slope_dy,
cairo_stroke_face_t *start,
cairo_stroke_face_t *end)
{
_compute_face (p1, dev_slope, slope_dx, slope_dy, stroker, start);
*end = *start;
if (p1->x == p2->x && p1->y == p2->y)
return CAIRO_STATUS_SUCCESS;
end->point = *p2;
end->ccw.x += p2->x - p1->x;
end->ccw.y += p2->y - p1->y;
end->cw.x += p2->x - p1->x;
end->cw.y += p2->y - p1->y;
if (stroker->add_external_edge != NULL) {
cairo_status_t status;
status = stroker->add_external_edge (stroker->closure,
&end->cw, &start->cw);
if (unlikely (status))
return status;
status = stroker->add_external_edge (stroker->closure,
&start->ccw, &end->ccw);
if (unlikely (status))
return status;
return CAIRO_STATUS_SUCCESS;
} else {
cairo_point_t quad[4];
quad[0] = start->cw;
quad[1] = end->cw;
quad[2] = end->ccw;
quad[3] = start->ccw;
return stroker->add_convex_quad (stroker->closure, quad);
}
}
static cairo_status_t
_cairo_stroker_move_to (void *closure,
const cairo_point_t *point)
{
cairo_stroker_t *stroker = closure;
cairo_status_t status;
/* reset the dash pattern for new sub paths */
_cairo_stroker_dash_start (&stroker->dash);
/* Cap the start and end of the previous sub path as needed */
status = _cairo_stroker_add_caps (stroker);
if (unlikely (status))
return status;
stroker->first_point = *point;
stroker->current_point = *point;
stroker->has_first_face = FALSE;
stroker->has_current_face = FALSE;
stroker->has_initial_sub_path = FALSE;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_stroker_line_to (void *closure,
const cairo_point_t *point)
{
cairo_stroker_t *stroker = closure;
cairo_stroke_face_t start, end;
cairo_point_t *p1 = &stroker->current_point;
cairo_slope_t dev_slope;
double slope_dx, slope_dy;
cairo_status_t status;
stroker->has_initial_sub_path = TRUE;
if (p1->x == point->x && p1->y == point->y)
return CAIRO_STATUS_SUCCESS;
_cairo_slope_init (&dev_slope, p1, point);
slope_dx = _cairo_fixed_to_double (point->x - p1->x);
slope_dy = _cairo_fixed_to_double (point->y - p1->y);
_compute_normalized_device_slope (&slope_dx, &slope_dy,
stroker->ctm_inverse, NULL);
status = _cairo_stroker_add_sub_edge (stroker,
p1, point,
&dev_slope,
slope_dx, slope_dy,
&start, &end);
if (unlikely (status))
return status;
if (stroker->has_current_face) {
/* Join with final face from previous segment */
status = _cairo_stroker_join (stroker,
&stroker->current_face,
&start);
if (unlikely (status))
return status;
} else if (! stroker->has_first_face) {
/* Save sub path's first face in case needed for closing join */
stroker->first_face = start;
stroker->has_first_face = TRUE;
}
stroker->current_face = end;
stroker->has_current_face = TRUE;
stroker->current_point = *point;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_stroker_spline_to (void *closure,
const cairo_point_t *point,
const cairo_slope_t *tangent)
{
cairo_stroker_t *stroker = closure;
cairo_stroke_face_t new_face;
double slope_dx, slope_dy;
cairo_point_t points[3];
cairo_point_t intersect_point;
stroker->has_initial_sub_path = TRUE;
if (stroker->current_point.x == point->x &&
stroker->current_point.y == point->y)
return CAIRO_STATUS_SUCCESS;
slope_dx = _cairo_fixed_to_double (tangent->dx);
slope_dy = _cairo_fixed_to_double (tangent->dy);
if (! _compute_normalized_device_slope (&slope_dx, &slope_dy,
stroker->ctm_inverse, NULL))
return CAIRO_STATUS_SUCCESS;
_compute_face (point, tangent,
slope_dx, slope_dy,
stroker, &new_face);
assert (stroker->has_current_face);
if ((new_face.dev_slope.x * stroker->current_face.dev_slope.x +
new_face.dev_slope.y * stroker->current_face.dev_slope.y) < stroker->spline_cusp_tolerance) {
const cairo_point_t *inpt, *outpt;
int clockwise = _cairo_stroker_join_is_clockwise (&new_face,
&stroker->current_face);
if (clockwise) {
inpt = &stroker->current_face.cw;
outpt = &new_face.cw;
} else {
inpt = &stroker->current_face.ccw;
outpt = &new_face.ccw;
}
_tessellate_fan (stroker,
&stroker->current_face.dev_vector,
&new_face.dev_vector,
&stroker->current_face.point,
inpt, outpt,
clockwise);
}
if (_slow_segment_intersection (&stroker->current_face.cw,
&stroker->current_face.ccw,
&new_face.cw,
&new_face.ccw,
&intersect_point)) {
points[0] = stroker->current_face.ccw;
points[1] = new_face.ccw;
points[2] = intersect_point;
stroker->add_triangle (stroker->closure, points);
points[0] = stroker->current_face.cw;
points[1] = new_face.cw;
stroker->add_triangle (stroker->closure, points);
} else {
points[0] = stroker->current_face.ccw;
points[1] = stroker->current_face.cw;
points[2] = new_face.cw;
stroker->add_triangle (stroker->closure, points);
points[0] = stroker->current_face.ccw;
points[1] = new_face.cw;
points[2] = new_face.ccw;
stroker->add_triangle (stroker->closure, points);
}
stroker->current_face = new_face;
stroker->has_current_face = TRUE;
stroker->current_point = *point;
return CAIRO_STATUS_SUCCESS;
}
/*
* Dashed lines. Cap each dash end, join around turns when on
*/
static cairo_status_t
_cairo_stroker_line_to_dashed (void *closure,
const cairo_point_t *p2)
{
cairo_stroker_t *stroker = closure;
double mag, remain, step_length = 0;
double slope_dx, slope_dy;
double dx2, dy2;
cairo_stroke_face_t sub_start, sub_end;
cairo_point_t *p1 = &stroker->current_point;
cairo_slope_t dev_slope;
cairo_line_t segment;
cairo_bool_t fully_in_bounds;
cairo_status_t status;
stroker->has_initial_sub_path = stroker->dash.dash_starts_on;
if (p1->x == p2->x && p1->y == p2->y)
return CAIRO_STATUS_SUCCESS;
fully_in_bounds = TRUE;
if (stroker->has_bounds &&
(! _cairo_box_contains_point (&stroker->bounds, p1) ||
! _cairo_box_contains_point (&stroker->bounds, p2)))
{
fully_in_bounds = FALSE;
}
_cairo_slope_init (&dev_slope, p1, p2);
slope_dx = _cairo_fixed_to_double (p2->x - p1->x);
slope_dy = _cairo_fixed_to_double (p2->y - p1->y);
if (! _compute_normalized_device_slope (&slope_dx, &slope_dy,
stroker->ctm_inverse, &mag))
{
return CAIRO_STATUS_SUCCESS;
}
remain = mag;
segment.p1 = *p1;
while (remain) {
step_length = MIN (stroker->dash.dash_remain, remain);
remain -= step_length;
dx2 = slope_dx * (mag - remain);
dy2 = slope_dy * (mag - remain);
cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);
segment.p2.x = _cairo_fixed_from_double (dx2) + p1->x;
segment.p2.y = _cairo_fixed_from_double (dy2) + p1->y;
if (stroker->dash.dash_on &&
(fully_in_bounds ||
(! stroker->has_first_face && stroker->dash.dash_starts_on) ||
_cairo_box_intersects_line_segment (&stroker->bounds, &segment)))
{
status = _cairo_stroker_add_sub_edge (stroker,
&segment.p1, &segment.p2,
&dev_slope,
slope_dx, slope_dy,
&sub_start, &sub_end);
if (unlikely (status))
return status;
if (stroker->has_current_face)
{
/* Join with final face from previous segment */
status = _cairo_stroker_join (stroker,
&stroker->current_face,
&sub_start);
if (unlikely (status))
return status;
stroker->has_current_face = FALSE;
}
else if (! stroker->has_first_face &&
stroker->dash.dash_starts_on)
{
/* Save sub path's first face in case needed for closing join */
stroker->first_face = sub_start;
stroker->has_first_face = TRUE;
}
else
{
/* Cap dash start if not connecting to a previous segment */
status = _cairo_stroker_add_leading_cap (stroker, &sub_start);
if (unlikely (status))
return status;
}
if (remain) {
/* Cap dash end if not at end of segment */
status = _cairo_stroker_add_trailing_cap (stroker, &sub_end);
if (unlikely (status))
return status;
} else {
stroker->current_face = sub_end;
stroker->has_current_face = TRUE;
}
} else {
if (stroker->has_current_face) {
/* Cap final face from previous segment */
status = _cairo_stroker_add_trailing_cap (stroker,
&stroker->current_face);
if (unlikely (status))
return status;
stroker->has_current_face = FALSE;
}
}
_cairo_stroker_dash_step (&stroker->dash, step_length);
segment.p1 = segment.p2;
}
if (stroker->dash.dash_on && ! stroker->has_current_face) {
/* This segment ends on a transition to dash_on, compute a new face
* and add cap for the beginning of the next dash_on step.
*
* Note: this will create a degenerate cap if this is not the last line
* in the path. Whether this behaviour is desirable or not is debatable.
* On one side these degenerate caps can not be reproduced with regular
* path stroking.
* On the other hand, Acroread 7 also produces the degenerate caps.
*/
_compute_face (p2, &dev_slope,
slope_dx, slope_dy,
stroker,
&stroker->current_face);
status = _cairo_stroker_add_leading_cap (stroker,
&stroker->current_face);
if (unlikely (status))
return status;
stroker->has_current_face = TRUE;
}
stroker->current_point = *p2;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_stroker_curve_to (void *closure,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
cairo_stroker_t *stroker = closure;
cairo_spline_t spline;
cairo_line_join_t line_join_save;
cairo_stroke_face_t face;
double slope_dx, slope_dy;
cairo_spline_add_point_func_t line_to;
cairo_spline_add_point_func_t spline_to;
cairo_status_t status = CAIRO_STATUS_SUCCESS;
line_to = stroker->dash.dashed ?
(cairo_spline_add_point_func_t) _cairo_stroker_line_to_dashed :
(cairo_spline_add_point_func_t) _cairo_stroker_line_to;
/* spline_to is only capable of rendering non-degenerate splines. */
spline_to = stroker->dash.dashed ?
(cairo_spline_add_point_func_t) _cairo_stroker_line_to_dashed :
(cairo_spline_add_point_func_t) _cairo_stroker_spline_to;
if (! _cairo_spline_init (&spline,
spline_to,
stroker,
&stroker->current_point, b, c, d))
{
cairo_slope_t fallback_slope;
_cairo_slope_init (&fallback_slope, &stroker->current_point, d);
return line_to (closure, d, &fallback_slope);
}
/* If the line width is so small that the pen is reduced to a
single point, then we have nothing to do. */
if (stroker->pen.num_vertices <= 1)
return CAIRO_STATUS_SUCCESS;
/* Compute the initial face */
if (! stroker->dash.dashed || stroker->dash.dash_on) {
slope_dx = _cairo_fixed_to_double (spline.initial_slope.dx);
slope_dy = _cairo_fixed_to_double (spline.initial_slope.dy);
if (_compute_normalized_device_slope (&slope_dx, &slope_dy,
stroker->ctm_inverse, NULL))
{
_compute_face (&stroker->current_point,
&spline.initial_slope,
slope_dx, slope_dy,
stroker, &face);
}
if (stroker->has_current_face) {
status = _cairo_stroker_join (stroker,
&stroker->current_face, &face);
if (unlikely (status))
return status;
} else if (! stroker->has_first_face) {
stroker->first_face = face;
stroker->has_first_face = TRUE;
}
stroker->current_face = face;
stroker->has_current_face = TRUE;
}
/* Temporarily modify the stroker to use round joins to guarantee
* smooth stroked curves. */
line_join_save = stroker->style.line_join;
stroker->style.line_join = CAIRO_LINE_JOIN_ROUND;
status = _cairo_spline_decompose (&spline, stroker->tolerance);
if (unlikely (status))
return status;
/* And join the final face */
if (! stroker->dash.dashed || stroker->dash.dash_on) {
slope_dx = _cairo_fixed_to_double (spline.final_slope.dx);
slope_dy = _cairo_fixed_to_double (spline.final_slope.dy);
if (_compute_normalized_device_slope (&slope_dx, &slope_dy,
stroker->ctm_inverse, NULL))
{
_compute_face (&stroker->current_point,
&spline.final_slope,
slope_dx, slope_dy,
stroker, &face);
}
status = _cairo_stroker_join (stroker, &stroker->current_face, &face);
if (unlikely (status))
return status;
stroker->current_face = face;
}
stroker->style.line_join = line_join_save;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_stroker_close_path (void *closure)
{
cairo_stroker_t *stroker = closure;
cairo_status_t status;
if (stroker->dash.dashed)
status = _cairo_stroker_line_to_dashed (stroker, &stroker->first_point);
else
status = _cairo_stroker_line_to (stroker, &stroker->first_point);
if (unlikely (status))
return status;
if (stroker->has_first_face && stroker->has_current_face) {
/* Join first and final faces of sub path */
status = _cairo_stroker_join (stroker,
&stroker->current_face,
&stroker->first_face);
if (unlikely (status))
return status;
} else {
/* Cap the start and end of the sub path as needed */
status = _cairo_stroker_add_caps (stroker);
if (unlikely (status))
return status;
}
stroker->has_initial_sub_path = FALSE;
stroker->has_first_face = FALSE;
stroker->has_current_face = FALSE;
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_path_fixed_stroke_to_shaper (cairo_path_fixed_t *path,
const cairo_stroke_style_t *stroke_style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_status_t (*add_triangle) (void *closure,
const cairo_point_t triangle[3]),
cairo_status_t (*add_triangle_fan) (void *closure,
const cairo_point_t *midpt,
const cairo_point_t *points,
int npoints),
cairo_status_t (*add_convex_quad) (void *closure,
const cairo_point_t quad[4]),
void *closure)
{
cairo_stroker_t stroker;
cairo_status_t status;
status = _cairo_stroker_init (&stroker, path, stroke_style,
ctm, ctm_inverse, tolerance,
NULL, 0);
if (unlikely (status))
return status;
stroker.add_triangle = add_triangle;
stroker.add_triangle_fan = add_triangle_fan;
stroker.add_convex_quad = add_convex_quad;
stroker.closure = closure;
status = _cairo_path_fixed_interpret (path,
_cairo_stroker_move_to,
stroker.dash.dashed ?
_cairo_stroker_line_to_dashed :
_cairo_stroker_line_to,
_cairo_stroker_curve_to,
_cairo_stroker_close_path,
&stroker);
if (unlikely (status))
goto BAIL;
/* Cap the start and end of the final sub path as needed */
status = _cairo_stroker_add_caps (&stroker);
BAIL:
_cairo_stroker_fini (&stroker);
return status;
}
cairo_status_t
_cairo_path_fixed_stroke_dashed_to_polygon (const cairo_path_fixed_t *path,
const cairo_stroke_style_t *stroke_style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_polygon_t *polygon)
{
cairo_stroker_t stroker;
cairo_status_t status;
status = _cairo_stroker_init (&stroker, path, stroke_style,
ctm, ctm_inverse, tolerance,
polygon->limits, polygon->num_limits);
if (unlikely (status))
return status;
stroker.add_external_edge = _cairo_polygon_add_external_edge,
stroker.closure = polygon;
status = _cairo_path_fixed_interpret (path,
_cairo_stroker_move_to,
stroker.dash.dashed ?
_cairo_stroker_line_to_dashed :
_cairo_stroker_line_to,
_cairo_stroker_curve_to,
_cairo_stroker_close_path,
&stroker);
if (unlikely (status))
goto BAIL;
/* Cap the start and end of the final sub path as needed */
status = _cairo_stroker_add_caps (&stroker);
BAIL:
_cairo_stroker_fini (&stroker);
return status;
}
cairo_int_status_t
_cairo_path_fixed_stroke_polygon_to_traps (const cairo_path_fixed_t *path,
const cairo_stroke_style_t *stroke_style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_traps_t *traps)
{
cairo_int_status_t status;
cairo_polygon_t polygon;
_cairo_polygon_init (&polygon, traps->limits, traps->num_limits);
status = _cairo_path_fixed_stroke_to_polygon (path,
stroke_style,
ctm,
ctm_inverse,
tolerance,
&polygon);
if (unlikely (status))
goto BAIL;
status = _cairo_polygon_status (&polygon);
if (unlikely (status))
goto BAIL;
status = _cairo_bentley_ottmann_tessellate_polygon (traps, &polygon,
CAIRO_FILL_RULE_WINDING);
BAIL:
_cairo_polygon_fini (&polygon);
return status;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-path.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2006 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@redhat.com>
*/
#include "cairoint.h"
#include "cairo-private.h"
#include "cairo-backend-private.h"
#include "cairo-error-private.h"
#include "cairo-path-private.h"
#include "cairo-path-fixed-private.h"
/**
* SECTION:cairo-paths
* @Title: Paths
* @Short_Description: Creating paths and manipulating path data
*
* Paths are the most basic drawing tools and are primarily used to implicitly
* generate simple masks.
**/
static const cairo_path_t _cairo_path_nil = { CAIRO_STATUS_NO_MEMORY, NULL, 0 };
/* Closure for path interpretation. */
typedef struct cairo_path_count {
int count;
} cpc_t;
static cairo_status_t
_cpc_move_to (void *closure,
const cairo_point_t *point)
{
cpc_t *cpc = closure;
cpc->count += 2;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cpc_line_to (void *closure,
const cairo_point_t *point)
{
cpc_t *cpc = closure;
cpc->count += 2;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cpc_curve_to (void *closure,
const cairo_point_t *p1,
const cairo_point_t *p2,
const cairo_point_t *p3)
{
cpc_t *cpc = closure;
cpc->count += 4;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cpc_close_path (void *closure)
{
cpc_t *cpc = closure;
cpc->count += 1;
return CAIRO_STATUS_SUCCESS;
}
static int
_cairo_path_count (cairo_path_t *path,
cairo_path_fixed_t *path_fixed,
double tolerance,
cairo_bool_t flatten)
{
cairo_status_t status;
cpc_t cpc;
cpc.count = 0;
if (flatten) {
status = _cairo_path_fixed_interpret_flat (path_fixed,
_cpc_move_to,
_cpc_line_to,
_cpc_close_path,
&cpc,
tolerance);
} else {
status = _cairo_path_fixed_interpret (path_fixed,
_cpc_move_to,
_cpc_line_to,
_cpc_curve_to,
_cpc_close_path,
&cpc);
}
if (unlikely (status))
return -1;
return cpc.count;
}
/* Closure for path interpretation. */
typedef struct cairo_path_populate {
cairo_path_data_t *data;
cairo_t *cr;
} cpp_t;
static cairo_status_t
_cpp_move_to (void *closure,
const cairo_point_t *point)
{
cpp_t *cpp = closure;
cairo_path_data_t *data = cpp->data;
double x, y;
x = _cairo_fixed_to_double (point->x);
y = _cairo_fixed_to_double (point->y);
_cairo_backend_to_user (cpp->cr, &x, &y);
data->header.type = CAIRO_PATH_MOVE_TO;
data->header.length = 2;
/* We index from 1 to leave room for data->header */
data[1].point.x = x;
data[1].point.y = y;
cpp->data += data->header.length;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cpp_line_to (void *closure,
const cairo_point_t *point)
{
cpp_t *cpp = closure;
cairo_path_data_t *data = cpp->data;
double x, y;
x = _cairo_fixed_to_double (point->x);
y = _cairo_fixed_to_double (point->y);
_cairo_backend_to_user (cpp->cr, &x, &y);
data->header.type = CAIRO_PATH_LINE_TO;
data->header.length = 2;
/* We index from 1 to leave room for data->header */
data[1].point.x = x;
data[1].point.y = y;
cpp->data += data->header.length;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cpp_curve_to (void *closure,
const cairo_point_t *p1,
const cairo_point_t *p2,
const cairo_point_t *p3)
{
cpp_t *cpp = closure;
cairo_path_data_t *data = cpp->data;
double x1, y1;
double x2, y2;
double x3, y3;
x1 = _cairo_fixed_to_double (p1->x);
y1 = _cairo_fixed_to_double (p1->y);
_cairo_backend_to_user (cpp->cr, &x1, &y1);
x2 = _cairo_fixed_to_double (p2->x);
y2 = _cairo_fixed_to_double (p2->y);
_cairo_backend_to_user (cpp->cr, &x2, &y2);
x3 = _cairo_fixed_to_double (p3->x);
y3 = _cairo_fixed_to_double (p3->y);
_cairo_backend_to_user (cpp->cr, &x3, &y3);
data->header.type = CAIRO_PATH_CURVE_TO;
data->header.length = 4;
/* We index from 1 to leave room for data->header */
data[1].point.x = x1;
data[1].point.y = y1;
data[2].point.x = x2;
data[2].point.y = y2;
data[3].point.x = x3;
data[3].point.y = y3;
cpp->data += data->header.length;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cpp_close_path (void *closure)
{
cpp_t *cpp = closure;
cairo_path_data_t *data = cpp->data;
data->header.type = CAIRO_PATH_CLOSE_PATH;
data->header.length = 1;
cpp->data += data->header.length;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_path_populate (cairo_path_t *path,
cairo_path_fixed_t *path_fixed,
cairo_t *cr,
cairo_bool_t flatten)
{
cairo_status_t status;
cpp_t cpp;
cpp.data = path->data;
cpp.cr = cr;
if (flatten) {
status = _cairo_path_fixed_interpret_flat (path_fixed,
_cpp_move_to,
_cpp_line_to,
_cpp_close_path,
&cpp,
cairo_get_tolerance (cr));
} else {
status = _cairo_path_fixed_interpret (path_fixed,
_cpp_move_to,
_cpp_line_to,
_cpp_curve_to,
_cpp_close_path,
&cpp);
}
if (unlikely (status))
return status;
/* Sanity check the count */
assert (cpp.data - path->data == path->num_data);
return CAIRO_STATUS_SUCCESS;
}
cairo_path_t *
_cairo_path_create_in_error (cairo_status_t status)
{
cairo_path_t *path;
/* special case NO_MEMORY so as to avoid allocations */
if (status == CAIRO_STATUS_NO_MEMORY)
return (cairo_path_t*) &_cairo_path_nil;
path = _cairo_malloc (sizeof (cairo_path_t));
if (unlikely (path == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_path_t*) &_cairo_path_nil;
}
path->num_data = 0;
path->data = NULL;
path->status = status;
return path;
}
static cairo_path_t *
_cairo_path_create_internal (cairo_path_fixed_t *path_fixed,
cairo_t *cr,
cairo_bool_t flatten)
{
cairo_path_t *path;
path = _cairo_malloc (sizeof (cairo_path_t));
if (unlikely (path == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_path_t*) &_cairo_path_nil;
}
path->num_data = _cairo_path_count (path, path_fixed,
cairo_get_tolerance (cr),
flatten);
if (path->num_data < 0) {
free (path);
return (cairo_path_t*) &_cairo_path_nil;
}
if (path->num_data) {
path->data = _cairo_malloc_ab (path->num_data,
sizeof (cairo_path_data_t));
if (unlikely (path->data == NULL)) {
free (path);
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_path_t*) &_cairo_path_nil;
}
path->status = _cairo_path_populate (path, path_fixed, cr, flatten);
} else {
path->data = NULL;
path->status = CAIRO_STATUS_SUCCESS;
}
return path;
}
/**
* cairo_path_destroy:
* @path: a path previously returned by either cairo_copy_path() or
* cairo_copy_path_flat().
*
* Immediately releases all memory associated with @path. After a call
* to cairo_path_destroy() the @path pointer is no longer valid and
* should not be used further.
*
* Note: cairo_path_destroy() should only be called with a
* pointer to a #cairo_path_t returned by a cairo function. Any path
* that is created manually (ie. outside of cairo) should be destroyed
* manually as well.
*
* Since: 1.0
**/
void
cairo_path_destroy (cairo_path_t *path)
{
if (path == NULL || path == &_cairo_path_nil)
return;
free (path->data);
free (path);
}
slim_hidden_def (cairo_path_destroy);
/**
* _cairo_path_create:
* @path: a fixed-point, device-space path to be converted and copied
* @cr: the current graphics context
*
* Creates a user-space #cairo_path_t copy of the given device-space
* @path. The @cr parameter provides the inverse CTM for the
* conversion.
*
* Return value: the new copy of the path. If there is insufficient
* memory a pointer to a special static nil #cairo_path_t will be
* returned instead with status==%CAIRO_STATUS_NO_MEMORY and
* data==%NULL.
**/
cairo_path_t *
_cairo_path_create (cairo_path_fixed_t *path,
cairo_t *cr)
{
return _cairo_path_create_internal (path, cr, FALSE);
}
/**
* _cairo_path_create_flat:
* @path: a fixed-point, device-space path to be flattened, converted and copied
* @cr: the current graphics context
*
* Creates a flattened, user-space #cairo_path_t copy of the given
* device-space @path. The @cr parameter provide the inverse CTM
* for the conversion, as well as the tolerance value to control the
* accuracy of the flattening.
*
* Return value: the flattened copy of the path. If there is insufficient
* memory a pointer to a special static nil #cairo_path_t will be
* returned instead with status==%CAIRO_STATUS_NO_MEMORY and
* data==%NULL.
**/
cairo_path_t *
_cairo_path_create_flat (cairo_path_fixed_t *path,
cairo_t *cr)
{
return _cairo_path_create_internal (path, cr, TRUE);
}
/**
* _cairo_path_append_to_context:
* @path: the path data to be appended
* @cr: a cairo context
*
* Append @path to the current path within @cr.
*
* Return value: %CAIRO_STATUS_INVALID_PATH_DATA if the data in @path
* is invalid, and %CAIRO_STATUS_SUCCESS otherwise.
**/
cairo_status_t
_cairo_path_append_to_context (const cairo_path_t *path,
cairo_t *cr)
{
const cairo_path_data_t *p, *end;
end = &path->data[path->num_data];
for (p = &path->data[0]; p < end; p += p->header.length) {
switch (p->header.type) {
case CAIRO_PATH_MOVE_TO:
if (unlikely (p->header.length < 2))
return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA);
cairo_move_to (cr, p[1].point.x, p[1].point.y);
break;
case CAIRO_PATH_LINE_TO:
if (unlikely (p->header.length < 2))
return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA);
cairo_line_to (cr, p[1].point.x, p[1].point.y);
break;
case CAIRO_PATH_CURVE_TO:
if (unlikely (p->header.length < 4))
return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA);
cairo_curve_to (cr,
p[1].point.x, p[1].point.y,
p[2].point.x, p[2].point.y,
p[3].point.x, p[3].point.y);
break;
case CAIRO_PATH_CLOSE_PATH:
if (unlikely (p->header.length < 1))
return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA);
cairo_close_path (cr);
break;
default:
return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA);
}
if (unlikely (cr->status))
return cr->status;
}
return CAIRO_STATUS_SUCCESS;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-pattern-inline.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@redhat.com>
*/
#ifndef CAIRO_PATTERN_INLINE_H
#define CAIRO_PATTERN_INLINE_H
#include "cairo-pattern-private.h"
#include "cairo-list-inline.h"
CAIRO_BEGIN_DECLS
static inline void
_cairo_pattern_add_observer (cairo_pattern_t *pattern,
cairo_pattern_observer_t *observer,
void (*func) (cairo_pattern_observer_t *,
cairo_pattern_t *,
unsigned int))
{
observer->notify = func;
cairo_list_add (&observer->link, &pattern->observers);
}
static inline cairo_surface_t *
_cairo_pattern_get_source (const cairo_surface_pattern_t *pattern,
cairo_rectangle_int_t *extents)
{
return _cairo_surface_get_source (pattern->surface, extents);
}
CAIRO_END_DECLS
#endif /* CAIRO_PATTERN_INLINE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-pattern-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@redhat.com>
*/
#ifndef CAIRO_PATTERN_PRIVATE_H
#define CAIRO_PATTERN_PRIVATE_H
#include "cairo-error-private.h"
#include "cairo-types-private.h"
#include "cairo-list-private.h"
#include "cairo-surface-private.h"
#include <stdio.h> /* FILE* */
CAIRO_BEGIN_DECLS
typedef struct _cairo_pattern_observer cairo_pattern_observer_t;
enum {
CAIRO_PATTERN_NOTIFY_MATRIX = 0x1,
CAIRO_PATTERN_NOTIFY_FILTER = 0x2,
CAIRO_PATTERN_NOTIFY_EXTEND = 0x4,
CAIRO_PATTERN_NOTIFY_OPACITY = 0x9,
};
struct _cairo_pattern_observer {
void (*notify) (cairo_pattern_observer_t *,
cairo_pattern_t *pattern,
unsigned int flags);
cairo_list_t link;
};
struct _cairo_pattern {
cairo_reference_count_t ref_count;
cairo_status_t status;
cairo_user_data_array_t user_data;
cairo_list_t observers;
cairo_pattern_type_t type;
cairo_filter_t filter;
cairo_extend_t extend;
cairo_bool_t has_component_alpha;
cairo_matrix_t matrix;
double opacity;
};
struct _cairo_solid_pattern {
cairo_pattern_t base;
cairo_color_t color;
};
typedef struct _cairo_surface_pattern {
cairo_pattern_t base;
cairo_surface_t *surface;
} cairo_surface_pattern_t;
typedef struct _cairo_gradient_stop {
double offset;
cairo_color_stop_t color;
} cairo_gradient_stop_t;
typedef struct _cairo_gradient_pattern {
cairo_pattern_t base;
unsigned int n_stops;
unsigned int stops_size;
cairo_gradient_stop_t *stops;
cairo_gradient_stop_t stops_embedded[2];
} cairo_gradient_pattern_t;
typedef struct _cairo_linear_pattern {
cairo_gradient_pattern_t base;
cairo_point_double_t pd1;
cairo_point_double_t pd2;
} cairo_linear_pattern_t;
typedef struct _cairo_radial_pattern {
cairo_gradient_pattern_t base;
cairo_circle_double_t cd1;
cairo_circle_double_t cd2;
} cairo_radial_pattern_t;
typedef union {
cairo_gradient_pattern_t base;
cairo_linear_pattern_t linear;
cairo_radial_pattern_t radial;
} cairo_gradient_pattern_union_t;
/*
* A mesh patch is a tensor-product patch (bicubic Bezier surface
* patch). It has 16 control points. Each set of 4 points along the
* sides of the 4x4 grid of control points is a Bezier curve that
* defines one side of the patch. A color is assigned to each
* corner. The inner 4 points provide additional control over the
* shape and the color mapping.
*
* Cairo uses the same convention as the PDF Reference for numbering
* the points and side of the patch.
*
*
* Side 1
*
* p[0][3] p[1][3] p[2][3] p[3][3]
* Side 0 p[0][2] p[1][2] p[2][2] p[3][2] Side 2
* p[0][1] p[1][1] p[2][1] p[3][1]
* p[0][0] p[1][0] p[2][0] p[3][0]
*
* Side 3
*
*
* Point Color
* -------------------------
* points[0][0] colors[0]
* points[0][3] colors[1]
* points[3][3] colors[2]
* points[3][0] colors[3]
*/
typedef struct _cairo_mesh_patch {
cairo_point_double_t points[4][4];
cairo_color_t colors[4];
} cairo_mesh_patch_t;
typedef struct _cairo_mesh_pattern {
cairo_pattern_t base;
cairo_array_t patches;
cairo_mesh_patch_t *current_patch;
int current_side;
cairo_bool_t has_control_point[4];
cairo_bool_t has_color[4];
} cairo_mesh_pattern_t;
typedef struct _cairo_raster_source_pattern {
cairo_pattern_t base;
cairo_content_t content;
cairo_rectangle_int_t extents;
cairo_raster_source_acquire_func_t acquire;
cairo_raster_source_release_func_t release;
cairo_raster_source_snapshot_func_t snapshot;
cairo_raster_source_copy_func_t copy;
cairo_raster_source_finish_func_t finish;
/* an explicit pre-allocated member in preference to the general user-data */
void *user_data;
} cairo_raster_source_pattern_t;
typedef union {
cairo_pattern_t base;
cairo_solid_pattern_t solid;
cairo_surface_pattern_t surface;
cairo_gradient_pattern_union_t gradient;
cairo_mesh_pattern_t mesh;
cairo_raster_source_pattern_t raster_source;
} cairo_pattern_union_t;
/* cairo-pattern.c */
cairo_private cairo_pattern_t *
_cairo_pattern_create_in_error (cairo_status_t status);
cairo_private cairo_status_t
_cairo_pattern_create_copy (cairo_pattern_t **pattern,
const cairo_pattern_t *other);
cairo_private void
_cairo_pattern_init (cairo_pattern_t *pattern,
cairo_pattern_type_t type);
cairo_private cairo_status_t
_cairo_pattern_init_copy (cairo_pattern_t *pattern,
const cairo_pattern_t *other);
cairo_private void
_cairo_pattern_init_static_copy (cairo_pattern_t *pattern,
const cairo_pattern_t *other);
cairo_private cairo_status_t
_cairo_pattern_init_snapshot (cairo_pattern_t *pattern,
const cairo_pattern_t *other);
cairo_private void
_cairo_pattern_init_solid (cairo_solid_pattern_t *pattern,
const cairo_color_t *color);
cairo_private void
_cairo_pattern_init_for_surface (cairo_surface_pattern_t *pattern,
cairo_surface_t *surface);
cairo_private void
_cairo_pattern_fini (cairo_pattern_t *pattern);
cairo_private cairo_pattern_t *
_cairo_pattern_create_solid (const cairo_color_t *color);
cairo_private void
_cairo_pattern_transform (cairo_pattern_t *pattern,
const cairo_matrix_t *ctm_inverse);
cairo_private void
_cairo_pattern_pretransform (cairo_pattern_t *pattern,
const cairo_matrix_t *ctm);
cairo_private cairo_bool_t
_cairo_pattern_is_opaque_solid (const cairo_pattern_t *pattern);
cairo_private cairo_bool_t
_cairo_pattern_is_opaque (const cairo_pattern_t *pattern,
const cairo_rectangle_int_t *extents);
cairo_private cairo_bool_t
_cairo_pattern_is_clear (const cairo_pattern_t *pattern);
cairo_private cairo_bool_t
_cairo_gradient_pattern_is_solid (const cairo_gradient_pattern_t *gradient,
const cairo_rectangle_int_t *extents,
cairo_color_t *color);
cairo_private cairo_bool_t
_cairo_pattern_is_constant_alpha (const cairo_pattern_t *abstract_pattern,
const cairo_rectangle_int_t *extents,
double *alpha);
cairo_private void
_cairo_gradient_pattern_fit_to_range (const cairo_gradient_pattern_t *gradient,
double max_value,
cairo_matrix_t *out_matrix,
cairo_circle_double_t out_circle[2]);
cairo_private cairo_bool_t
_cairo_radial_pattern_focus_is_inside (const cairo_radial_pattern_t *radial);
cairo_private void
_cairo_gradient_pattern_box_to_parameter (const cairo_gradient_pattern_t *gradient,
double x0, double y0,
double x1, double y1,
double tolerance,
double out_range[2]);
cairo_private void
_cairo_gradient_pattern_interpolate (const cairo_gradient_pattern_t *gradient,
double t,
cairo_circle_double_t *out_circle);
cairo_private void
_cairo_pattern_alpha_range (const cairo_pattern_t *pattern,
double *out_min,
double *out_max);
cairo_private cairo_bool_t
_cairo_mesh_pattern_coord_box (const cairo_mesh_pattern_t *mesh,
double *out_xmin,
double *out_ymin,
double *out_xmax,
double *out_ymax);
cairo_private void
_cairo_pattern_sampled_area (const cairo_pattern_t *pattern,
const cairo_rectangle_int_t *extents,
cairo_rectangle_int_t *sample);
cairo_private void
_cairo_pattern_get_extents (const cairo_pattern_t *pattern,
cairo_rectangle_int_t *extents,
cairo_bool_t is_vector);
cairo_private cairo_int_status_t
_cairo_pattern_get_ink_extents (const cairo_pattern_t *pattern,
cairo_rectangle_int_t *extents);
cairo_private unsigned long
_cairo_pattern_hash (const cairo_pattern_t *pattern);
cairo_private unsigned long
_cairo_linear_pattern_hash (unsigned long hash,
const cairo_linear_pattern_t *linear);
cairo_private unsigned long
_cairo_radial_pattern_hash (unsigned long hash,
const cairo_radial_pattern_t *radial);
cairo_private cairo_bool_t
_cairo_linear_pattern_equal (const cairo_linear_pattern_t *a,
const cairo_linear_pattern_t *b);
cairo_private unsigned long
_cairo_pattern_size (const cairo_pattern_t *pattern);
cairo_private cairo_bool_t
_cairo_radial_pattern_equal (const cairo_radial_pattern_t *a,
const cairo_radial_pattern_t *b);
cairo_private cairo_bool_t
_cairo_pattern_equal (const cairo_pattern_t *a,
const cairo_pattern_t *b);
cairo_private cairo_filter_t
_cairo_pattern_analyze_filter (const cairo_pattern_t *pattern);
/* cairo-mesh-pattern-rasterizer.c */
cairo_private void
_cairo_mesh_pattern_rasterize (const cairo_mesh_pattern_t *mesh,
void *data,
int width,
int height,
int stride,
double x_offset,
double y_offset);
cairo_private cairo_surface_t *
_cairo_raster_source_pattern_acquire (const cairo_pattern_t *abstract_pattern,
cairo_surface_t *target,
const cairo_rectangle_int_t *extents);
cairo_private void
_cairo_raster_source_pattern_release (const cairo_pattern_t *abstract_pattern,
cairo_surface_t *surface);
cairo_private cairo_status_t
_cairo_raster_source_pattern_snapshot (cairo_pattern_t *abstract_pattern);
cairo_private cairo_status_t
_cairo_raster_source_pattern_init_copy (cairo_pattern_t *pattern,
const cairo_pattern_t *other);
cairo_private void
_cairo_raster_source_pattern_finish (cairo_pattern_t *abstract_pattern);
cairo_private void
_cairo_debug_print_pattern (FILE *file, const cairo_pattern_t *pattern);
CAIRO_END_DECLS
#endif /* CAIRO_PATTERN_PRIVATE */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-pattern.c | /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2004 David Reveman
* Copyright © 2005 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of David
* Reveman not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. David Reveman makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL DAVID REVEMAN BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Authors: David Reveman <davidr@novell.com>
* Keith Packard <keithp@keithp.com>
* Carl Worth <cworth@cworth.org>
*/
#include "cairoint.h"
#include "cairo-array-private.h"
#include "cairo-error-private.h"
#include "cairo-freed-pool-private.h"
#include "cairo-image-surface-private.h"
#include "cairo-list-inline.h"
#include "cairo-path-private.h"
#include "cairo-pattern-private.h"
#include "cairo-recording-surface-inline.h"
#include "cairo-surface-snapshot-inline.h"
#include <float.h>
#define PIXMAN_MAX_INT ((pixman_fixed_1 >> 1) - pixman_fixed_e) /* need to ensure deltas also fit */
/**
* SECTION:cairo-pattern
* @Title: cairo_pattern_t
* @Short_Description: Sources for drawing
* @See_Also: #cairo_t, #cairo_surface_t
*
* #cairo_pattern_t is the paint with which cairo draws.
* The primary use of patterns is as the source for all cairo drawing
* operations, although they can also be used as masks, that is, as the
* brush too.
*
* A cairo pattern is created by using one of the many constructors,
* of the form
* <function>cairo_pattern_create_<emphasis>type</emphasis>()</function>
* or implicitly through
* <function>cairo_set_source_<emphasis>type</emphasis>()</function>
* functions.
**/
static freed_pool_t freed_pattern_pool[5];
static const cairo_solid_pattern_t _cairo_pattern_nil = {
{
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_NO_MEMORY, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ NULL, NULL }, /* observers */
CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_FILTER_DEFAULT, /* filter */
CAIRO_EXTEND_GRADIENT_DEFAULT, /* extend */
FALSE, /* has component alpha */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
1.0 /* opacity */
}
};
static const cairo_solid_pattern_t _cairo_pattern_nil_null_pointer = {
{
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_NULL_POINTER, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ NULL, NULL }, /* observers */
CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_FILTER_DEFAULT, /* filter */
CAIRO_EXTEND_GRADIENT_DEFAULT, /* extend */
FALSE, /* has component alpha */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
1.0 /* opacity */
}
};
const cairo_solid_pattern_t _cairo_pattern_black = {
{
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_SUCCESS, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ NULL, NULL }, /* observers */
CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_FILTER_NEAREST, /* filter */
CAIRO_EXTEND_REPEAT, /* extend */
FALSE, /* has component alpha */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
1.0 /* opacity */
},
{ 0., 0., 0., 1., 0, 0, 0, 0xffff },/* color (double rgba, short rgba) */
};
const cairo_solid_pattern_t _cairo_pattern_clear = {
{
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_SUCCESS, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ NULL, NULL }, /* observers */
CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_FILTER_NEAREST, /* filter */
CAIRO_EXTEND_REPEAT, /* extend */
FALSE, /* has component alpha */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
1.0 /* opacity */
},
{ 0., 0., 0., 0., 0, 0, 0, 0 },/* color (double rgba, short rgba) */
};
const cairo_solid_pattern_t _cairo_pattern_white = {
{
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_SUCCESS, /* status */
{ 0, 0, 0, NULL }, /* user_data */
{ NULL, NULL }, /* observers */
CAIRO_PATTERN_TYPE_SOLID, /* type */
CAIRO_FILTER_NEAREST, /* filter */
CAIRO_EXTEND_REPEAT, /* extend */
FALSE, /* has component alpha */
{ 1., 0., 0., 1., 0., 0., }, /* matrix */
1.0 /* opacity */
},
{ 1., 1., 1., 1., 0xffff, 0xffff, 0xffff, 0xffff },/* color (double rgba, short rgba) */
};
static void
_cairo_pattern_notify_observers (cairo_pattern_t *pattern,
unsigned int flags)
{
cairo_pattern_observer_t *pos;
cairo_list_foreach_entry (pos, cairo_pattern_observer_t, &pattern->observers, link)
pos->notify (pos, pattern, flags);
}
/**
* _cairo_pattern_set_error:
* @pattern: a pattern
* @status: a status value indicating an error
*
* Atomically sets pattern->status to @status and calls _cairo_error;
* Does nothing if status is %CAIRO_STATUS_SUCCESS.
*
* All assignments of an error status to pattern->status should happen
* through _cairo_pattern_set_error(). Note that due to the nature of
* the atomic operation, it is not safe to call this function on the nil
* objects.
*
* The purpose of this function is to allow the user to set a
* breakpoint in _cairo_error() to generate a stack trace for when the
* user causes cairo to detect an error.
**/
static cairo_status_t
_cairo_pattern_set_error (cairo_pattern_t *pattern,
cairo_status_t status)
{
if (status == CAIRO_STATUS_SUCCESS)
return status;
/* Don't overwrite an existing error. This preserves the first
* error, which is the most significant. */
_cairo_status_set_error (&pattern->status, status);
return _cairo_error (status);
}
void
_cairo_pattern_init (cairo_pattern_t *pattern, cairo_pattern_type_t type)
{
#if HAVE_VALGRIND
switch (type) {
case CAIRO_PATTERN_TYPE_SOLID:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_solid_pattern_t));
break;
case CAIRO_PATTERN_TYPE_SURFACE:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_surface_pattern_t));
break;
case CAIRO_PATTERN_TYPE_LINEAR:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_linear_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RADIAL:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_radial_pattern_t));
break;
case CAIRO_PATTERN_TYPE_MESH:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_mesh_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
break;
}
#endif
pattern->type = type;
pattern->status = CAIRO_STATUS_SUCCESS;
/* Set the reference count to zero for on-stack patterns.
* Callers needs to explicitly increment the count for heap allocations. */
CAIRO_REFERENCE_COUNT_INIT (&pattern->ref_count, 0);
_cairo_user_data_array_init (&pattern->user_data);
if (type == CAIRO_PATTERN_TYPE_SURFACE ||
type == CAIRO_PATTERN_TYPE_RASTER_SOURCE)
pattern->extend = CAIRO_EXTEND_SURFACE_DEFAULT;
else
pattern->extend = CAIRO_EXTEND_GRADIENT_DEFAULT;
pattern->filter = CAIRO_FILTER_DEFAULT;
pattern->opacity = 1.0;
pattern->has_component_alpha = FALSE;
cairo_matrix_init_identity (&pattern->matrix);
cairo_list_init (&pattern->observers);
}
static cairo_status_t
_cairo_gradient_pattern_init_copy (cairo_gradient_pattern_t *pattern,
const cairo_gradient_pattern_t *other)
{
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (other->base.type == CAIRO_PATTERN_TYPE_LINEAR)
{
cairo_linear_pattern_t *dst = (cairo_linear_pattern_t *) pattern;
cairo_linear_pattern_t *src = (cairo_linear_pattern_t *) other;
*dst = *src;
}
else
{
cairo_radial_pattern_t *dst = (cairo_radial_pattern_t *) pattern;
cairo_radial_pattern_t *src = (cairo_radial_pattern_t *) other;
*dst = *src;
}
if (other->stops == other->stops_embedded)
pattern->stops = pattern->stops_embedded;
else if (other->stops)
{
pattern->stops = _cairo_malloc_ab (other->stops_size,
sizeof (cairo_gradient_stop_t));
if (unlikely (pattern->stops == NULL)) {
pattern->stops_size = 0;
pattern->n_stops = 0;
return _cairo_pattern_set_error (&pattern->base, CAIRO_STATUS_NO_MEMORY);
}
memcpy (pattern->stops, other->stops,
other->n_stops * sizeof (cairo_gradient_stop_t));
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_mesh_pattern_init_copy (cairo_mesh_pattern_t *pattern,
const cairo_mesh_pattern_t *other)
{
*pattern = *other;
_cairo_array_init (&pattern->patches, sizeof (cairo_mesh_patch_t));
return _cairo_array_append_multiple (&pattern->patches,
_cairo_array_index_const (&other->patches, 0),
_cairo_array_num_elements (&other->patches));
}
cairo_status_t
_cairo_pattern_init_copy (cairo_pattern_t *pattern,
const cairo_pattern_t *other)
{
cairo_status_t status;
if (other->status)
return _cairo_pattern_set_error (pattern, other->status);
switch (other->type) {
case CAIRO_PATTERN_TYPE_SOLID: {
cairo_solid_pattern_t *dst = (cairo_solid_pattern_t *) pattern;
cairo_solid_pattern_t *src = (cairo_solid_pattern_t *) other;
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_solid_pattern_t)));
*dst = *src;
} break;
case CAIRO_PATTERN_TYPE_SURFACE: {
cairo_surface_pattern_t *dst = (cairo_surface_pattern_t *) pattern;
cairo_surface_pattern_t *src = (cairo_surface_pattern_t *) other;
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_surface_pattern_t)));
*dst = *src;
cairo_surface_reference (dst->surface);
} break;
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL: {
cairo_gradient_pattern_t *dst = (cairo_gradient_pattern_t *) pattern;
cairo_gradient_pattern_t *src = (cairo_gradient_pattern_t *) other;
if (other->type == CAIRO_PATTERN_TYPE_LINEAR) {
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_linear_pattern_t)));
} else {
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_radial_pattern_t)));
}
status = _cairo_gradient_pattern_init_copy (dst, src);
if (unlikely (status))
return status;
} break;
case CAIRO_PATTERN_TYPE_MESH: {
cairo_mesh_pattern_t *dst = (cairo_mesh_pattern_t *) pattern;
cairo_mesh_pattern_t *src = (cairo_mesh_pattern_t *) other;
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_mesh_pattern_t)));
status = _cairo_mesh_pattern_init_copy (dst, src);
if (unlikely (status))
return status;
} break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE: {
status = _cairo_raster_source_pattern_init_copy (pattern, other);
if (unlikely (status))
return status;
} break;
}
/* The reference count and user_data array are unique to the copy. */
CAIRO_REFERENCE_COUNT_INIT (&pattern->ref_count, 0);
_cairo_user_data_array_init (&pattern->user_data);
cairo_list_init (&pattern->observers);
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_pattern_init_static_copy (cairo_pattern_t *pattern,
const cairo_pattern_t *other)
{
int size;
assert (other->status == CAIRO_STATUS_SUCCESS);
switch (other->type) {
default:
ASSERT_NOT_REACHED;
case CAIRO_PATTERN_TYPE_SOLID:
size = sizeof (cairo_solid_pattern_t);
break;
case CAIRO_PATTERN_TYPE_SURFACE:
size = sizeof (cairo_surface_pattern_t);
break;
case CAIRO_PATTERN_TYPE_LINEAR:
size = sizeof (cairo_linear_pattern_t);
break;
case CAIRO_PATTERN_TYPE_RADIAL:
size = sizeof (cairo_radial_pattern_t);
break;
case CAIRO_PATTERN_TYPE_MESH:
size = sizeof (cairo_mesh_pattern_t);
break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
size = sizeof (cairo_raster_source_pattern_t);
break;
}
memcpy (pattern, other, size);
CAIRO_REFERENCE_COUNT_INIT (&pattern->ref_count, 0);
_cairo_user_data_array_init (&pattern->user_data);
cairo_list_init (&pattern->observers);
}
cairo_status_t
_cairo_pattern_init_snapshot (cairo_pattern_t *pattern,
const cairo_pattern_t *other)
{
cairo_status_t status;
/* We don't bother doing any fancy copy-on-write implementation
* for the pattern's data. It's generally quite tiny. */
status = _cairo_pattern_init_copy (pattern, other);
if (unlikely (status))
return status;
/* But we do let the surface snapshot stuff be as fancy as it
* would like to be. */
if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
cairo_surface_pattern_t *surface_pattern =
(cairo_surface_pattern_t *) pattern;
cairo_surface_t *surface = surface_pattern->surface;
surface_pattern->surface = _cairo_surface_snapshot (surface);
cairo_surface_destroy (surface);
status = surface_pattern->surface->status;
} else if (pattern->type == CAIRO_PATTERN_TYPE_RASTER_SOURCE)
status = _cairo_raster_source_pattern_snapshot (pattern);
return status;
}
void
_cairo_pattern_fini (cairo_pattern_t *pattern)
{
_cairo_user_data_array_fini (&pattern->user_data);
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
break;
case CAIRO_PATTERN_TYPE_SURFACE: {
cairo_surface_pattern_t *surface_pattern =
(cairo_surface_pattern_t *) pattern;
cairo_surface_destroy (surface_pattern->surface);
} break;
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL: {
cairo_gradient_pattern_t *gradient =
(cairo_gradient_pattern_t *) pattern;
if (gradient->stops && gradient->stops != gradient->stops_embedded)
free (gradient->stops);
} break;
case CAIRO_PATTERN_TYPE_MESH: {
cairo_mesh_pattern_t *mesh =
(cairo_mesh_pattern_t *) pattern;
_cairo_array_fini (&mesh->patches);
} break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
_cairo_raster_source_pattern_finish (pattern);
break;
}
#if HAVE_VALGRIND
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_solid_pattern_t));
break;
case CAIRO_PATTERN_TYPE_SURFACE:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_surface_pattern_t));
break;
case CAIRO_PATTERN_TYPE_LINEAR:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_linear_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RADIAL:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_radial_pattern_t));
break;
case CAIRO_PATTERN_TYPE_MESH:
VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_mesh_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
break;
}
#endif
}
cairo_status_t
_cairo_pattern_create_copy (cairo_pattern_t **pattern_out,
const cairo_pattern_t *other)
{
cairo_pattern_t *pattern;
cairo_status_t status;
if (other->status)
return other->status;
switch (other->type) {
case CAIRO_PATTERN_TYPE_SOLID:
pattern = _cairo_malloc (sizeof (cairo_solid_pattern_t));
break;
case CAIRO_PATTERN_TYPE_SURFACE:
pattern = _cairo_malloc (sizeof (cairo_surface_pattern_t));
break;
case CAIRO_PATTERN_TYPE_LINEAR:
pattern = _cairo_malloc (sizeof (cairo_linear_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RADIAL:
pattern = _cairo_malloc (sizeof (cairo_radial_pattern_t));
break;
case CAIRO_PATTERN_TYPE_MESH:
pattern = _cairo_malloc (sizeof (cairo_mesh_pattern_t));
break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
pattern = _cairo_malloc (sizeof (cairo_raster_source_pattern_t));
break;
default:
ASSERT_NOT_REACHED;
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
}
if (unlikely (pattern == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
status = _cairo_pattern_init_copy (pattern, other);
if (unlikely (status)) {
free (pattern);
return status;
}
CAIRO_REFERENCE_COUNT_INIT (&pattern->ref_count, 1);
*pattern_out = pattern;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_pattern_init_solid (cairo_solid_pattern_t *pattern,
const cairo_color_t *color)
{
_cairo_pattern_init (&pattern->base, CAIRO_PATTERN_TYPE_SOLID);
pattern->color = *color;
}
void
_cairo_pattern_init_for_surface (cairo_surface_pattern_t *pattern,
cairo_surface_t *surface)
{
if (surface->status) {
/* Force to solid to simplify the pattern_fini process. */
_cairo_pattern_init (&pattern->base, CAIRO_PATTERN_TYPE_SOLID);
_cairo_pattern_set_error (&pattern->base, surface->status);
return;
}
_cairo_pattern_init (&pattern->base, CAIRO_PATTERN_TYPE_SURFACE);
pattern->surface = cairo_surface_reference (surface);
}
static void
_cairo_pattern_init_gradient (cairo_gradient_pattern_t *pattern,
cairo_pattern_type_t type)
{
_cairo_pattern_init (&pattern->base, type);
pattern->n_stops = 0;
pattern->stops_size = 0;
pattern->stops = NULL;
}
static void
_cairo_pattern_init_linear (cairo_linear_pattern_t *pattern,
double x0, double y0, double x1, double y1)
{
_cairo_pattern_init_gradient (&pattern->base, CAIRO_PATTERN_TYPE_LINEAR);
pattern->pd1.x = x0;
pattern->pd1.y = y0;
pattern->pd2.x = x1;
pattern->pd2.y = y1;
}
static void
_cairo_pattern_init_radial (cairo_radial_pattern_t *pattern,
double cx0, double cy0, double radius0,
double cx1, double cy1, double radius1)
{
_cairo_pattern_init_gradient (&pattern->base, CAIRO_PATTERN_TYPE_RADIAL);
pattern->cd1.center.x = cx0;
pattern->cd1.center.y = cy0;
pattern->cd1.radius = fabs (radius0);
pattern->cd2.center.x = cx1;
pattern->cd2.center.y = cy1;
pattern->cd2.radius = fabs (radius1);
}
cairo_pattern_t *
_cairo_pattern_create_solid (const cairo_color_t *color)
{
cairo_solid_pattern_t *pattern;
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_SOLID]);
if (unlikely (pattern == NULL)) {
/* None cached, need to create a new pattern. */
pattern = _cairo_malloc (sizeof (cairo_solid_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *) &_cairo_pattern_nil;
}
}
_cairo_pattern_init_solid (pattern, color);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);
return &pattern->base;
}
cairo_pattern_t *
_cairo_pattern_create_in_error (cairo_status_t status)
{
cairo_pattern_t *pattern;
if (status == CAIRO_STATUS_NO_MEMORY)
return (cairo_pattern_t *)&_cairo_pattern_nil.base;
CAIRO_MUTEX_INITIALIZE ();
pattern = _cairo_pattern_create_solid (CAIRO_COLOR_BLACK);
if (pattern->status == CAIRO_STATUS_SUCCESS)
status = _cairo_pattern_set_error (pattern, status);
return pattern;
}
/**
* cairo_pattern_create_rgb:
* @red: red component of the color
* @green: green component of the color
* @blue: blue component of the color
*
* Creates a new #cairo_pattern_t corresponding to an opaque color. The
* color components are floating point numbers in the range 0 to 1.
* If the values passed in are outside that range, they will be
* clamped.
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
*
* Since: 1.0
**/
cairo_pattern_t *
cairo_pattern_create_rgb (double red, double green, double blue)
{
return cairo_pattern_create_rgba (red, green, blue, 1.0);
}
slim_hidden_def (cairo_pattern_create_rgb);
/**
* cairo_pattern_create_rgba:
* @red: red component of the color
* @green: green component of the color
* @blue: blue component of the color
* @alpha: alpha component of the color
*
* Creates a new #cairo_pattern_t corresponding to a translucent color.
* The color components are floating point numbers in the range 0 to
* 1. If the values passed in are outside that range, they will be
* clamped.
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
*
* Since: 1.0
**/
cairo_pattern_t *
cairo_pattern_create_rgba (double red, double green, double blue,
double alpha)
{
cairo_color_t color;
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
alpha = _cairo_restrict_value (alpha, 0.0, 1.0);
_cairo_color_init_rgba (&color, red, green, blue, alpha);
CAIRO_MUTEX_INITIALIZE ();
return _cairo_pattern_create_solid (&color);
}
slim_hidden_def (cairo_pattern_create_rgba);
/**
* cairo_pattern_create_for_surface:
* @surface: the surface
*
* Create a new #cairo_pattern_t for the given surface.
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
*
* Since: 1.0
**/
cairo_pattern_t *
cairo_pattern_create_for_surface (cairo_surface_t *surface)
{
cairo_surface_pattern_t *pattern;
if (surface == NULL) {
_cairo_error_throw (CAIRO_STATUS_NULL_POINTER);
return (cairo_pattern_t*) &_cairo_pattern_nil_null_pointer;
}
if (surface->status)
return _cairo_pattern_create_in_error (surface->status);
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_SURFACE]);
if (unlikely (pattern == NULL)) {
pattern = _cairo_malloc (sizeof (cairo_surface_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *)&_cairo_pattern_nil.base;
}
}
CAIRO_MUTEX_INITIALIZE ();
_cairo_pattern_init_for_surface (pattern, surface);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);
return &pattern->base;
}
slim_hidden_def (cairo_pattern_create_for_surface);
/**
* cairo_pattern_create_linear:
* @x0: x coordinate of the start point
* @y0: y coordinate of the start point
* @x1: x coordinate of the end point
* @y1: y coordinate of the end point
*
* Create a new linear gradient #cairo_pattern_t along the line defined
* by (x0, y0) and (x1, y1). Before using the gradient pattern, a
* number of color stops should be defined using
* cairo_pattern_add_color_stop_rgb() or
* cairo_pattern_add_color_stop_rgba().
*
* Note: The coordinates here are in pattern space. For a new pattern,
* pattern space is identical to user space, but the relationship
* between the spaces can be changed with cairo_pattern_set_matrix().
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
*
* Since: 1.0
**/
cairo_pattern_t *
cairo_pattern_create_linear (double x0, double y0, double x1, double y1)
{
cairo_linear_pattern_t *pattern;
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_LINEAR]);
if (unlikely (pattern == NULL)) {
pattern = _cairo_malloc (sizeof (cairo_linear_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *) &_cairo_pattern_nil.base;
}
}
CAIRO_MUTEX_INITIALIZE ();
_cairo_pattern_init_linear (pattern, x0, y0, x1, y1);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.base.ref_count, 1);
return &pattern->base.base;
}
/**
* cairo_pattern_create_radial:
* @cx0: x coordinate for the center of the start circle
* @cy0: y coordinate for the center of the start circle
* @radius0: radius of the start circle
* @cx1: x coordinate for the center of the end circle
* @cy1: y coordinate for the center of the end circle
* @radius1: radius of the end circle
*
* Creates a new radial gradient #cairo_pattern_t between the two
* circles defined by (cx0, cy0, radius0) and (cx1, cy1, radius1). Before using the
* gradient pattern, a number of color stops should be defined using
* cairo_pattern_add_color_stop_rgb() or
* cairo_pattern_add_color_stop_rgba().
*
* Note: The coordinates here are in pattern space. For a new pattern,
* pattern space is identical to user space, but the relationship
* between the spaces can be changed with cairo_pattern_set_matrix().
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the
* returned object and should call cairo_pattern_destroy() when
* finished with it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect
* the status of a pattern use cairo_pattern_status().
*
* Since: 1.0
**/
cairo_pattern_t *
cairo_pattern_create_radial (double cx0, double cy0, double radius0,
double cx1, double cy1, double radius1)
{
cairo_radial_pattern_t *pattern;
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_RADIAL]);
if (unlikely (pattern == NULL)) {
pattern = _cairo_malloc (sizeof (cairo_radial_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *) &_cairo_pattern_nil.base;
}
}
CAIRO_MUTEX_INITIALIZE ();
_cairo_pattern_init_radial (pattern, cx0, cy0, radius0, cx1, cy1, radius1);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.base.ref_count, 1);
return &pattern->base.base;
}
/* This order is specified in the diagram in the documentation for
* cairo_pattern_create_mesh() */
static const int mesh_path_point_i[12] = { 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 2, 1 };
static const int mesh_path_point_j[12] = { 0, 1, 2, 3, 3, 3, 3, 2, 1, 0, 0, 0 };
static const int mesh_control_point_i[4] = { 1, 1, 2, 2 };
static const int mesh_control_point_j[4] = { 1, 2, 2, 1 };
/**
* cairo_pattern_create_mesh:
*
* Create a new mesh pattern.
*
* Mesh patterns are tensor-product patch meshes (type 7 shadings in
* PDF). Mesh patterns may also be used to create other types of
* shadings that are special cases of tensor-product patch meshes such
* as Coons patch meshes (type 6 shading in PDF) and Gouraud-shaded
* triangle meshes (type 4 and 5 shadings in PDF).
*
* Mesh patterns consist of one or more tensor-product patches, which
* should be defined before using the mesh pattern. Using a mesh
* pattern with a partially defined patch as source or mask will put
* the context in an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* A tensor-product patch is defined by 4 Bézier curves (side 0, 1, 2,
* 3) and by 4 additional control points (P0, P1, P2, P3) that provide
* further control over the patch and complete the definition of the
* tensor-product patch. The corner C0 is the first point of the
* patch.
*
* Degenerate sides are permitted so straight lines may be used. A
* zero length line on one side may be used to create 3 sided patches.
*
* <informalexample><screen>
* C1 Side 1 C2
* +---------------+
* | |
* | P1 P2 |
* | |
* Side 0 | | Side 2
* | |
* | |
* | P0 P3 |
* | |
* +---------------+
* C0 Side 3 C3
* </screen></informalexample>
*
* Each patch is constructed by first calling
* cairo_mesh_pattern_begin_patch(), then cairo_mesh_pattern_move_to()
* to specify the first point in the patch (C0). Then the sides are
* specified with calls to cairo_mesh_pattern_curve_to() and
* cairo_mesh_pattern_line_to().
*
* The four additional control points (P0, P1, P2, P3) in a patch can
* be specified with cairo_mesh_pattern_set_control_point().
*
* At each corner of the patch (C0, C1, C2, C3) a color may be
* specified with cairo_mesh_pattern_set_corner_color_rgb() or
* cairo_mesh_pattern_set_corner_color_rgba(). Any corner whose color
* is not explicitly specified defaults to transparent black.
*
* A Coons patch is a special case of the tensor-product patch where
* the control points are implicitly defined by the sides of the
* patch. The default value for any control point not specified is the
* implicit value for a Coons patch, i.e. if no control points are
* specified the patch is a Coons patch.
*
* A triangle is a special case of the tensor-product patch where the
* control points are implicitly defined by the sides of the patch,
* all the sides are lines and one of them has length 0, i.e. if the
* patch is specified using just 3 lines, it is a triangle. If the
* corners connected by the 0-length side have the same color, the
* patch is a Gouraud-shaded triangle.
*
* Patches may be oriented differently to the above diagram. For
* example the first point could be at the top left. The diagram only
* shows the relationship between the sides, corners and control
* points. Regardless of where the first point is located, when
* specifying colors, corner 0 will always be the first point, corner
* 1 the point between side 0 and side 1 etc.
*
* Calling cairo_mesh_pattern_end_patch() completes the current
* patch. If less than 4 sides have been defined, the first missing
* side is defined as a line from the current point to the first point
* of the patch (C0) and the other sides are degenerate lines from C0
* to C0. The corners between the added sides will all be coincident
* with C0 of the patch and their color will be set to be the same as
* the color of C0.
*
* Additional patches may be added with additional calls to
* cairo_mesh_pattern_begin_patch()/cairo_mesh_pattern_end_patch().
*
* <informalexample><programlisting>
* cairo_pattern_t *pattern = cairo_pattern_create_mesh ();
*
* /* Add a Coons patch */
* cairo_mesh_pattern_begin_patch (pattern);
* cairo_mesh_pattern_move_to (pattern, 0, 0);
* cairo_mesh_pattern_curve_to (pattern, 30, -30, 60, 30, 100, 0);
* cairo_mesh_pattern_curve_to (pattern, 60, 30, 130, 60, 100, 100);
* cairo_mesh_pattern_curve_to (pattern, 60, 70, 30, 130, 0, 100);
* cairo_mesh_pattern_curve_to (pattern, 30, 70, -30, 30, 0, 0);
* cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 0, 0);
* cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, 0, 1, 0);
* cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, 0, 0, 1);
* cairo_mesh_pattern_set_corner_color_rgb (pattern, 3, 1, 1, 0);
* cairo_mesh_pattern_end_patch (pattern);
*
* /* Add a Gouraud-shaded triangle */
* cairo_mesh_pattern_begin_patch (pattern)
* cairo_mesh_pattern_move_to (pattern, 100, 100);
* cairo_mesh_pattern_line_to (pattern, 130, 130);
* cairo_mesh_pattern_line_to (pattern, 130, 70);
* cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 0, 0);
* cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, 0, 1, 0);
* cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, 0, 0, 1);
* cairo_mesh_pattern_end_patch (pattern)
* </programlisting></informalexample>
*
* When two patches overlap, the last one that has been added is drawn
* over the first one.
*
* When a patch folds over itself, points are sorted depending on
* their parameter coordinates inside the patch. The v coordinate
* ranges from 0 to 1 when moving from side 3 to side 1; the u
* coordinate ranges from 0 to 1 when going from side 0 to side
* 2. Points with higher v coordinate hide points with lower v
* coordinate. When two points have the same v coordinate, the one
* with higher u coordinate is above. This means that points nearer to
* side 1 are above points nearer to side 3; when this is not
* sufficient to decide which point is above (for example when both
* points belong to side 1 or side 3) points nearer to side 2 are
* above points nearer to side 0.
*
* For a complete definition of tensor-product patches, see the PDF
* specification (ISO32000), which describes the parametrization in
* detail.
*
* Note: The coordinates are always in pattern space. For a new
* pattern, pattern space is identical to user space, but the
* relationship between the spaces can be changed with
* cairo_pattern_set_matrix().
*
* Return value: the newly created #cairo_pattern_t if successful, or
* an error pattern in case of no memory. The caller owns the returned
* object and should call cairo_pattern_destroy() when finished with
* it.
*
* This function will always return a valid pointer, but if an error
* occurred the pattern status will be set to an error. To inspect the
* status of a pattern use cairo_pattern_status().
*
* Since: 1.12
**/
cairo_pattern_t *
cairo_pattern_create_mesh (void)
{
cairo_mesh_pattern_t *pattern;
pattern =
_freed_pool_get (&freed_pattern_pool[CAIRO_PATTERN_TYPE_MESH]);
if (unlikely (pattern == NULL)) {
pattern = _cairo_malloc (sizeof (cairo_mesh_pattern_t));
if (unlikely (pattern == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_pattern_t *) &_cairo_pattern_nil.base;
}
}
CAIRO_MUTEX_INITIALIZE ();
_cairo_pattern_init (&pattern->base, CAIRO_PATTERN_TYPE_MESH);
_cairo_array_init (&pattern->patches, sizeof (cairo_mesh_patch_t));
pattern->current_patch = NULL;
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);
return &pattern->base;
}
/**
* cairo_pattern_reference:
* @pattern: a #cairo_pattern_t
*
* Increases the reference count on @pattern by one. This prevents
* @pattern from being destroyed until a matching call to
* cairo_pattern_destroy() is made.
*
* Use cairo_pattern_get_reference_count() to get the number of
* references to a #cairo_pattern_t.
*
* Return value: the referenced #cairo_pattern_t.
*
* Since: 1.0
**/
cairo_pattern_t *
cairo_pattern_reference (cairo_pattern_t *pattern)
{
if (pattern == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
return pattern;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&pattern->ref_count));
_cairo_reference_count_inc (&pattern->ref_count);
return pattern;
}
slim_hidden_def (cairo_pattern_reference);
/**
* cairo_pattern_get_type:
* @pattern: a #cairo_pattern_t
*
* Get the pattern's type. See #cairo_pattern_type_t for available
* types.
*
* Return value: The type of @pattern.
*
* Since: 1.2
**/
cairo_pattern_type_t
cairo_pattern_get_type (cairo_pattern_t *pattern)
{
return pattern->type;
}
/**
* cairo_pattern_status:
* @pattern: a #cairo_pattern_t
*
* Checks whether an error has previously occurred for this
* pattern.
*
* Return value: %CAIRO_STATUS_SUCCESS, %CAIRO_STATUS_NO_MEMORY,
* %CAIRO_STATUS_INVALID_MATRIX, %CAIRO_STATUS_PATTERN_TYPE_MISMATCH,
* or %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.0
**/
cairo_status_t
cairo_pattern_status (cairo_pattern_t *pattern)
{
return pattern->status;
}
/**
* cairo_pattern_destroy:
* @pattern: a #cairo_pattern_t
*
* Decreases the reference count on @pattern by one. If the result is
* zero, then @pattern and all associated resources are freed. See
* cairo_pattern_reference().
*
* Since: 1.0
**/
void
cairo_pattern_destroy (cairo_pattern_t *pattern)
{
cairo_pattern_type_t type;
if (pattern == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
return;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&pattern->ref_count));
if (! _cairo_reference_count_dec_and_test (&pattern->ref_count))
return;
type = pattern->type;
_cairo_pattern_fini (pattern);
/* maintain a small cache of freed patterns */
if (type < ARRAY_LENGTH (freed_pattern_pool))
_freed_pool_put (&freed_pattern_pool[type], pattern);
else
free (pattern);
}
slim_hidden_def (cairo_pattern_destroy);
/**
* cairo_pattern_get_reference_count:
* @pattern: a #cairo_pattern_t
*
* Returns the current reference count of @pattern.
*
* Return value: the current reference count of @pattern. If the
* object is a nil object, 0 will be returned.
*
* Since: 1.4
**/
unsigned int
cairo_pattern_get_reference_count (cairo_pattern_t *pattern)
{
if (pattern == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
return 0;
return CAIRO_REFERENCE_COUNT_GET_VALUE (&pattern->ref_count);
}
/**
* cairo_pattern_get_user_data:
* @pattern: a #cairo_pattern_t
* @key: the address of the #cairo_user_data_key_t the user data was
* attached to
*
* Return user data previously attached to @pattern using the
* specified key. If no user data has been attached with the given
* key this function returns %NULL.
*
* Return value: the user data previously attached or %NULL.
*
* Since: 1.4
**/
void *
cairo_pattern_get_user_data (cairo_pattern_t *pattern,
const cairo_user_data_key_t *key)
{
return _cairo_user_data_array_get_data (&pattern->user_data,
key);
}
/**
* cairo_pattern_set_user_data:
* @pattern: a #cairo_pattern_t
* @key: the address of a #cairo_user_data_key_t to attach the user data to
* @user_data: the user data to attach to the #cairo_pattern_t
* @destroy: a #cairo_destroy_func_t which will be called when the
* #cairo_t is destroyed or when new user data is attached using the
* same key.
*
* Attach user data to @pattern. To remove user data from a surface,
* call this function with the key that was used to set it and %NULL
* for @data.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
* slot could not be allocated for the user data.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_set_user_data (cairo_pattern_t *pattern,
const cairo_user_data_key_t *key,
void *user_data,
cairo_destroy_func_t destroy)
{
if (CAIRO_REFERENCE_COUNT_IS_INVALID (&pattern->ref_count))
return pattern->status;
return _cairo_user_data_array_set_data (&pattern->user_data,
key, user_data, destroy);
}
/**
* cairo_mesh_pattern_begin_patch:
* @pattern: a #cairo_pattern_t
*
* Begin a patch in a mesh pattern.
*
* After calling this function, the patch shape should be defined with
* cairo_mesh_pattern_move_to(), cairo_mesh_pattern_line_to() and
* cairo_mesh_pattern_curve_to().
*
* After defining the patch, cairo_mesh_pattern_end_patch() must be
* called before using @pattern as a source or mask.
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern already has a
* current patch, it will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_mesh_pattern_begin_patch (cairo_pattern_t *pattern)
{
cairo_mesh_pattern_t *mesh;
cairo_status_t status;
cairo_mesh_patch_t *current_patch;
int i;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
status = _cairo_array_allocate (&mesh->patches, 1, (void **) ¤t_patch);
if (unlikely (status)) {
_cairo_pattern_set_error (pattern, status);
return;
}
mesh->current_patch = current_patch;
mesh->current_side = -2; /* no current point */
for (i = 0; i < 4; i++)
mesh->has_control_point[i] = FALSE;
for (i = 0; i < 4; i++)
mesh->has_color[i] = FALSE;
}
static void
_calc_control_point (cairo_mesh_patch_t *patch, int control_point)
{
/* The Coons patch is a special case of the Tensor Product patch
* where the four control points are:
*
* P11 = S(1/3, 1/3)
* P12 = S(1/3, 2/3)
* P21 = S(2/3, 1/3)
* P22 = S(2/3, 2/3)
*
* where S is the gradient surface.
*
* When one or more control points has not been specified
* calculated the Coons patch control points are substituted. If
* no control points are specified the gradient will be a Coons
* patch.
*
* The equations below are defined in the ISO32000 standard.
*/
cairo_point_double_t *p[3][3];
int cp_i, cp_j, i, j;
cp_i = mesh_control_point_i[control_point];
cp_j = mesh_control_point_j[control_point];
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
p[i][j] = &patch->points[cp_i ^ i][cp_j ^ j];
p[0][0]->x = (- 4 * p[1][1]->x
+ 6 * (p[1][0]->x + p[0][1]->x)
- 2 * (p[1][2]->x + p[2][1]->x)
+ 3 * (p[2][0]->x + p[0][2]->x)
- 1 * p[2][2]->x) * (1. / 9);
p[0][0]->y = (- 4 * p[1][1]->y
+ 6 * (p[1][0]->y + p[0][1]->y)
- 2 * (p[1][2]->y + p[2][1]->y)
+ 3 * (p[2][0]->y + p[0][2]->y)
- 1 * p[2][2]->y) * (1. / 9);
}
/**
* cairo_mesh_pattern_end_patch:
* @pattern: a #cairo_pattern_t
*
* Indicates the end of the current patch in a mesh pattern.
*
* If the current patch has less than 4 sides, it is closed with a
* straight line from the current point to the first point of the
* patch as if cairo_mesh_pattern_line_to() was used.
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern has no current
* patch or the current patch has no current point, @pattern will be
* put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_mesh_pattern_end_patch (cairo_pattern_t *pattern)
{
cairo_mesh_pattern_t *mesh;
cairo_mesh_patch_t *current_patch;
int i;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
current_patch = mesh->current_patch;
if (unlikely (!current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (unlikely (mesh->current_side == -2)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
while (mesh->current_side < 3) {
int corner_num;
cairo_mesh_pattern_line_to (pattern,
current_patch->points[0][0].x,
current_patch->points[0][0].y);
corner_num = mesh->current_side + 1;
if (corner_num < 4 && ! mesh->has_color[corner_num]) {
current_patch->colors[corner_num] = current_patch->colors[0];
mesh->has_color[corner_num] = TRUE;
}
}
for (i = 0; i < 4; i++) {
if (! mesh->has_control_point[i])
_calc_control_point (current_patch, i);
}
for (i = 0; i < 4; i++) {
if (! mesh->has_color[i])
current_patch->colors[i] = *CAIRO_COLOR_TRANSPARENT;
}
mesh->current_patch = NULL;
}
/**
* cairo_mesh_pattern_curve_to:
* @pattern: a #cairo_pattern_t
* @x1: the X coordinate of the first control point
* @y1: the Y coordinate of the first control point
* @x2: the X coordinate of the second control point
* @y2: the Y coordinate of the second control point
* @x3: the X coordinate of the end of the curve
* @y3: the Y coordinate of the end of the curve
*
* Adds a cubic Bézier spline to the current patch from the current
* point to position (@x3, @y3) in pattern-space coordinates, using
* (@x1, @y1) and (@x2, @y2) as the control points.
*
* If the current patch has no current point before the call to
* cairo_mesh_pattern_curve_to(), this function will behave as if
* preceded by a call to cairo_mesh_pattern_move_to(@pattern, @x1,
* @y1).
*
* After this call the current point will be (@x3, @y3).
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern has no current
* patch or the current patch already has 4 sides, @pattern will be
* put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_mesh_pattern_curve_to (cairo_pattern_t *pattern,
double x1, double y1,
double x2, double y2,
double x3, double y3)
{
cairo_mesh_pattern_t *mesh;
int current_point, i, j;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (unlikely (mesh->current_side == 3)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (mesh->current_side == -2)
cairo_mesh_pattern_move_to (pattern, x1, y1);
assert (mesh->current_side >= -1);
assert (pattern->status == CAIRO_STATUS_SUCCESS);
mesh->current_side++;
current_point = 3 * mesh->current_side;
current_point++;
i = mesh_path_point_i[current_point];
j = mesh_path_point_j[current_point];
mesh->current_patch->points[i][j].x = x1;
mesh->current_patch->points[i][j].y = y1;
current_point++;
i = mesh_path_point_i[current_point];
j = mesh_path_point_j[current_point];
mesh->current_patch->points[i][j].x = x2;
mesh->current_patch->points[i][j].y = y2;
current_point++;
if (current_point < 12) {
i = mesh_path_point_i[current_point];
j = mesh_path_point_j[current_point];
mesh->current_patch->points[i][j].x = x3;
mesh->current_patch->points[i][j].y = y3;
}
}
slim_hidden_def (cairo_mesh_pattern_curve_to);
/**
* cairo_mesh_pattern_line_to:
* @pattern: a #cairo_pattern_t
* @x: the X coordinate of the end of the new line
* @y: the Y coordinate of the end of the new line
*
* Adds a line to the current patch from the current point to position
* (@x, @y) in pattern-space coordinates.
*
* If there is no current point before the call to
* cairo_mesh_pattern_line_to() this function will behave as
* cairo_mesh_pattern_move_to(@pattern, @x, @y).
*
* After this call the current point will be (@x, @y).
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern has no current
* patch or the current patch already has 4 sides, @pattern will be
* put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_mesh_pattern_line_to (cairo_pattern_t *pattern,
double x, double y)
{
cairo_mesh_pattern_t *mesh;
cairo_point_double_t last_point;
int last_point_idx, i, j;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (unlikely (mesh->current_side == 3)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (mesh->current_side == -2) {
cairo_mesh_pattern_move_to (pattern, x, y);
return;
}
last_point_idx = 3 * (mesh->current_side + 1);
i = mesh_path_point_i[last_point_idx];
j = mesh_path_point_j[last_point_idx];
last_point = mesh->current_patch->points[i][j];
cairo_mesh_pattern_curve_to (pattern,
(2 * last_point.x + x) * (1. / 3),
(2 * last_point.y + y) * (1. / 3),
(last_point.x + 2 * x) * (1. / 3),
(last_point.y + 2 * y) * (1. / 3),
x, y);
}
slim_hidden_def (cairo_mesh_pattern_line_to);
/**
* cairo_mesh_pattern_move_to:
* @pattern: a #cairo_pattern_t
* @x: the X coordinate of the new position
* @y: the Y coordinate of the new position
*
* Define the first point of the current patch in a mesh pattern.
*
* After this call the current point will be (@x, @y).
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @pattern has no current
* patch or the current patch already has at least one side, @pattern
* will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_mesh_pattern_move_to (cairo_pattern_t *pattern,
double x, double y)
{
cairo_mesh_pattern_t *mesh;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
if (unlikely (mesh->current_side >= 0)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
mesh->current_side = -1;
mesh->current_patch->points[0][0].x = x;
mesh->current_patch->points[0][0].y = y;
}
slim_hidden_def (cairo_mesh_pattern_move_to);
/**
* cairo_mesh_pattern_set_control_point:
* @pattern: a #cairo_pattern_t
* @point_num: the control point to set the position for
* @x: the X coordinate of the control point
* @y: the Y coordinate of the control point
*
* Set an internal control point of the current patch.
*
* Valid values for @point_num are from 0 to 3 and identify the
* control points as explained in cairo_pattern_create_mesh().
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @point_num is not valid,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_INDEX. If @pattern has no current patch,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_mesh_pattern_set_control_point (cairo_pattern_t *pattern,
unsigned int point_num,
double x,
double y)
{
cairo_mesh_pattern_t *mesh;
int i, j;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
if (unlikely (point_num > 3)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_INDEX);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
i = mesh_control_point_i[point_num];
j = mesh_control_point_j[point_num];
mesh->current_patch->points[i][j].x = x;
mesh->current_patch->points[i][j].y = y;
mesh->has_control_point[point_num] = TRUE;
}
/* make room for at least one more color stop */
static cairo_status_t
_cairo_pattern_gradient_grow (cairo_gradient_pattern_t *pattern)
{
cairo_gradient_stop_t *new_stops;
int old_size = pattern->stops_size;
int embedded_size = ARRAY_LENGTH (pattern->stops_embedded);
int new_size = 2 * MAX (old_size, 4);
/* we have a local buffer at pattern->stops_embedded. try to fulfill the request
* from there. */
if (old_size < embedded_size) {
pattern->stops = pattern->stops_embedded;
pattern->stops_size = embedded_size;
return CAIRO_STATUS_SUCCESS;
}
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
assert (pattern->n_stops <= pattern->stops_size);
if (pattern->stops == pattern->stops_embedded) {
new_stops = _cairo_malloc_ab (new_size, sizeof (cairo_gradient_stop_t));
if (new_stops)
memcpy (new_stops, pattern->stops, old_size * sizeof (cairo_gradient_stop_t));
} else {
new_stops = _cairo_realloc_ab (pattern->stops,
new_size,
sizeof (cairo_gradient_stop_t));
}
if (unlikely (new_stops == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
pattern->stops = new_stops;
pattern->stops_size = new_size;
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_mesh_pattern_set_corner_color (cairo_mesh_pattern_t *mesh,
unsigned int corner_num,
double red, double green, double blue,
double alpha)
{
cairo_color_t *color;
assert (mesh->current_patch);
assert (corner_num <= 3);
color = &mesh->current_patch->colors[corner_num];
color->red = red;
color->green = green;
color->blue = blue;
color->alpha = alpha;
color->red_short = _cairo_color_double_to_short (red);
color->green_short = _cairo_color_double_to_short (green);
color->blue_short = _cairo_color_double_to_short (blue);
color->alpha_short = _cairo_color_double_to_short (alpha);
mesh->has_color[corner_num] = TRUE;
}
/**
* cairo_mesh_pattern_set_corner_color_rgb:
* @pattern: a #cairo_pattern_t
* @corner_num: the corner to set the color for
* @red: red component of color
* @green: green component of color
* @blue: blue component of color
*
* Sets the color of a corner of the current patch in a mesh pattern.
*
* The color is specified in the same way as in cairo_set_source_rgb().
*
* Valid values for @corner_num are from 0 to 3 and identify the
* corners as explained in cairo_pattern_create_mesh().
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @corner_num is not valid,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_INDEX. If @pattern has no current patch,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_mesh_pattern_set_corner_color_rgb (cairo_pattern_t *pattern,
unsigned int corner_num,
double red, double green, double blue)
{
cairo_mesh_pattern_set_corner_color_rgba (pattern, corner_num, red, green, blue, 1.0);
}
/**
* cairo_mesh_pattern_set_corner_color_rgba:
* @pattern: a #cairo_pattern_t
* @corner_num: the corner to set the color for
* @red: red component of color
* @green: green component of color
* @blue: blue component of color
* @alpha: alpha component of color
*
* Sets the color of a corner of the current patch in a mesh pattern.
*
* The color is specified in the same way as in cairo_set_source_rgba().
*
* Valid values for @corner_num are from 0 to 3 and identify the
* corners as explained in cairo_pattern_create_mesh().
*
* Note: If @pattern is not a mesh pattern then @pattern will be put
* into an error status with a status of
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH. If @corner_num is not valid,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_INDEX. If @pattern has no current patch,
* @pattern will be put into an error status with a status of
* %CAIRO_STATUS_INVALID_MESH_CONSTRUCTION.
*
* Since: 1.12
**/
void
cairo_mesh_pattern_set_corner_color_rgba (cairo_pattern_t *pattern,
unsigned int corner_num,
double red, double green, double blue,
double alpha)
{
cairo_mesh_pattern_t *mesh;
if (unlikely (pattern->status))
return;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
if (unlikely (corner_num > 3)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_INDEX);
return;
}
mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (!mesh->current_patch)) {
_cairo_pattern_set_error (pattern, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION);
return;
}
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
alpha = _cairo_restrict_value (alpha, 0.0, 1.0);
_cairo_mesh_pattern_set_corner_color (mesh, corner_num, red, green, blue, alpha);
}
slim_hidden_def (cairo_mesh_pattern_set_corner_color_rgba);
static void
_cairo_pattern_add_color_stop (cairo_gradient_pattern_t *pattern,
double offset,
double red,
double green,
double blue,
double alpha)
{
cairo_gradient_stop_t *stops;
unsigned int i;
if (pattern->n_stops >= pattern->stops_size) {
cairo_status_t status = _cairo_pattern_gradient_grow (pattern);
if (unlikely (status)) {
status = _cairo_pattern_set_error (&pattern->base, status);
return;
}
}
stops = pattern->stops;
for (i = 0; i < pattern->n_stops; i++)
{
if (offset < stops[i].offset)
{
memmove (&stops[i + 1], &stops[i],
sizeof (cairo_gradient_stop_t) * (pattern->n_stops - i));
break;
}
}
stops[i].offset = offset;
stops[i].color.red = red;
stops[i].color.green = green;
stops[i].color.blue = blue;
stops[i].color.alpha = alpha;
stops[i].color.red_short = _cairo_color_double_to_short (red);
stops[i].color.green_short = _cairo_color_double_to_short (green);
stops[i].color.blue_short = _cairo_color_double_to_short (blue);
stops[i].color.alpha_short = _cairo_color_double_to_short (alpha);
pattern->n_stops++;
}
/**
* cairo_pattern_add_color_stop_rgb:
* @pattern: a #cairo_pattern_t
* @offset: an offset in the range [0.0 .. 1.0]
* @red: red component of color
* @green: green component of color
* @blue: blue component of color
*
* Adds an opaque color stop to a gradient pattern. The offset
* specifies the location along the gradient's control vector. For
* example, a linear gradient's control vector is from (x0,y0) to
* (x1,y1) while a radial gradient's control vector is from any point
* on the start circle to the corresponding point on the end circle.
*
* The color is specified in the same way as in cairo_set_source_rgb().
*
* If two (or more) stops are specified with identical offset values,
* they will be sorted according to the order in which the stops are
* added, (stops added earlier will compare less than stops added
* later). This can be useful for reliably making sharp color
* transitions instead of the typical blend.
*
*
* Note: If the pattern is not a gradient pattern, (eg. a linear or
* radial pattern), then the pattern will be put into an error status
* with a status of %CAIRO_STATUS_PATTERN_TYPE_MISMATCH.
*
* Since: 1.0
**/
void
cairo_pattern_add_color_stop_rgb (cairo_pattern_t *pattern,
double offset,
double red,
double green,
double blue)
{
cairo_pattern_add_color_stop_rgba (pattern, offset, red, green, blue, 1.0);
}
/**
* cairo_pattern_add_color_stop_rgba:
* @pattern: a #cairo_pattern_t
* @offset: an offset in the range [0.0 .. 1.0]
* @red: red component of color
* @green: green component of color
* @blue: blue component of color
* @alpha: alpha component of color
*
* Adds a translucent color stop to a gradient pattern. The offset
* specifies the location along the gradient's control vector. For
* example, a linear gradient's control vector is from (x0,y0) to
* (x1,y1) while a radial gradient's control vector is from any point
* on the start circle to the corresponding point on the end circle.
*
* The color is specified in the same way as in cairo_set_source_rgba().
*
* If two (or more) stops are specified with identical offset values,
* they will be sorted according to the order in which the stops are
* added, (stops added earlier will compare less than stops added
* later). This can be useful for reliably making sharp color
* transitions instead of the typical blend.
*
* Note: If the pattern is not a gradient pattern, (eg. a linear or
* radial pattern), then the pattern will be put into an error status
* with a status of %CAIRO_STATUS_PATTERN_TYPE_MISMATCH.
*
* Since: 1.0
**/
void
cairo_pattern_add_color_stop_rgba (cairo_pattern_t *pattern,
double offset,
double red,
double green,
double blue,
double alpha)
{
if (pattern->status)
return;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
{
_cairo_pattern_set_error (pattern, CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
return;
}
offset = _cairo_restrict_value (offset, 0.0, 1.0);
red = _cairo_restrict_value (red, 0.0, 1.0);
green = _cairo_restrict_value (green, 0.0, 1.0);
blue = _cairo_restrict_value (blue, 0.0, 1.0);
alpha = _cairo_restrict_value (alpha, 0.0, 1.0);
_cairo_pattern_add_color_stop ((cairo_gradient_pattern_t *) pattern,
offset, red, green, blue, alpha);
}
slim_hidden_def (cairo_pattern_add_color_stop_rgba);
/**
* cairo_pattern_set_matrix:
* @pattern: a #cairo_pattern_t
* @matrix: a #cairo_matrix_t
*
* Sets the pattern's transformation matrix to @matrix. This matrix is
* a transformation from user space to pattern space.
*
* When a pattern is first created it always has the identity matrix
* for its transformation matrix, which means that pattern space is
* initially identical to user space.
*
* Important: Please note that the direction of this transformation
* matrix is from user space to pattern space. This means that if you
* imagine the flow from a pattern to user space (and on to device
* space), then coordinates in that flow will be transformed by the
* inverse of the pattern matrix.
*
* For example, if you want to make a pattern appear twice as large as
* it does by default the correct code to use is:
*
* <informalexample><programlisting>
* cairo_matrix_init_scale (&matrix, 0.5, 0.5);
* cairo_pattern_set_matrix (pattern, &matrix);
* </programlisting></informalexample>
*
* Meanwhile, using values of 2.0 rather than 0.5 in the code above
* would cause the pattern to appear at half of its default size.
*
* Also, please note the discussion of the user-space locking
* semantics of cairo_set_source().
*
* Since: 1.0
**/
void
cairo_pattern_set_matrix (cairo_pattern_t *pattern,
const cairo_matrix_t *matrix)
{
cairo_matrix_t inverse;
cairo_status_t status;
if (pattern->status)
return;
if (memcmp (&pattern->matrix, matrix, sizeof (cairo_matrix_t)) == 0)
return;
pattern->matrix = *matrix;
_cairo_pattern_notify_observers (pattern, CAIRO_PATTERN_NOTIFY_MATRIX);
inverse = *matrix;
status = cairo_matrix_invert (&inverse);
if (unlikely (status))
status = _cairo_pattern_set_error (pattern, status);
}
slim_hidden_def (cairo_pattern_set_matrix);
/**
* cairo_pattern_get_matrix:
* @pattern: a #cairo_pattern_t
* @matrix: return value for the matrix
*
* Stores the pattern's transformation matrix into @matrix.
*
* Since: 1.0
**/
void
cairo_pattern_get_matrix (cairo_pattern_t *pattern, cairo_matrix_t *matrix)
{
*matrix = pattern->matrix;
}
/**
* cairo_pattern_set_filter:
* @pattern: a #cairo_pattern_t
* @filter: a #cairo_filter_t describing the filter to use for resizing
* the pattern
*
* Sets the filter to be used for resizing when using this pattern.
* See #cairo_filter_t for details on each filter.
*
* * Note that you might want to control filtering even when you do not
* have an explicit #cairo_pattern_t object, (for example when using
* cairo_set_source_surface()). In these cases, it is convenient to
* use cairo_get_source() to get access to the pattern that cairo
* creates implicitly. For example:
*
* <informalexample><programlisting>
* cairo_set_source_surface (cr, image, x, y);
* cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
* </programlisting></informalexample>
*
* Since: 1.0
**/
void
cairo_pattern_set_filter (cairo_pattern_t *pattern, cairo_filter_t filter)
{
if (pattern->status)
return;
pattern->filter = filter;
_cairo_pattern_notify_observers (pattern, CAIRO_PATTERN_NOTIFY_FILTER);
}
/**
* cairo_pattern_get_filter:
* @pattern: a #cairo_pattern_t
*
* Gets the current filter for a pattern. See #cairo_filter_t
* for details on each filter.
*
* Return value: the current filter used for resizing the pattern.
*
* Since: 1.0
**/
cairo_filter_t
cairo_pattern_get_filter (cairo_pattern_t *pattern)
{
return pattern->filter;
}
/**
* cairo_pattern_set_extend:
* @pattern: a #cairo_pattern_t
* @extend: a #cairo_extend_t describing how the area outside of the
* pattern will be drawn
*
* Sets the mode to be used for drawing outside the area of a pattern.
* See #cairo_extend_t for details on the semantics of each extend
* strategy.
*
* The default extend mode is %CAIRO_EXTEND_NONE for surface patterns
* and %CAIRO_EXTEND_PAD for gradient patterns.
*
* Since: 1.0
**/
void
cairo_pattern_set_extend (cairo_pattern_t *pattern, cairo_extend_t extend)
{
if (pattern->status)
return;
pattern->extend = extend;
_cairo_pattern_notify_observers (pattern, CAIRO_PATTERN_NOTIFY_EXTEND);
}
/**
* cairo_pattern_get_extend:
* @pattern: a #cairo_pattern_t
*
* Gets the current extend mode for a pattern. See #cairo_extend_t
* for details on the semantics of each extend strategy.
*
* Return value: the current extend strategy used for drawing the
* pattern.
*
* Since: 1.0
**/
cairo_extend_t
cairo_pattern_get_extend (cairo_pattern_t *pattern)
{
return pattern->extend;
}
slim_hidden_def (cairo_pattern_get_extend);
void
_cairo_pattern_pretransform (cairo_pattern_t *pattern,
const cairo_matrix_t *ctm)
{
if (pattern->status)
return;
cairo_matrix_multiply (&pattern->matrix, &pattern->matrix, ctm);
}
void
_cairo_pattern_transform (cairo_pattern_t *pattern,
const cairo_matrix_t *ctm_inverse)
{
if (pattern->status)
return;
cairo_matrix_multiply (&pattern->matrix, ctm_inverse, &pattern->matrix);
}
static cairo_bool_t
_linear_pattern_is_degenerate (const cairo_linear_pattern_t *linear)
{
return fabs (linear->pd1.x - linear->pd2.x) < DBL_EPSILON &&
fabs (linear->pd1.y - linear->pd2.y) < DBL_EPSILON;
}
static cairo_bool_t
_radial_pattern_is_degenerate (const cairo_radial_pattern_t *radial)
{
/* A radial pattern is considered degenerate if it can be
* represented as a solid or clear pattern. This corresponds to
* one of the two cases:
*
* 1) The radii are both very small:
* |dr| < DBL_EPSILON && min (r0, r1) < DBL_EPSILON
*
* 2) The two circles have about the same radius and are very
* close to each other (approximately a cylinder gradient that
* doesn't move with the parameter):
* |dr| < DBL_EPSILON && max (|dx|, |dy|) < 2 * DBL_EPSILON
*
* These checks are consistent with the assumptions used in
* _cairo_radial_pattern_box_to_parameter ().
*/
return fabs (radial->cd1.radius - radial->cd2.radius) < DBL_EPSILON &&
(MIN (radial->cd1.radius, radial->cd2.radius) < DBL_EPSILON ||
MAX (fabs (radial->cd1.center.x - radial->cd2.center.x),
fabs (radial->cd1.center.y - radial->cd2.center.y)) < 2 * DBL_EPSILON);
}
static void
_cairo_linear_pattern_box_to_parameter (const cairo_linear_pattern_t *linear,
double x0, double y0,
double x1, double y1,
double range[2])
{
double t0, tdx, tdy;
double p1x, p1y, pdx, pdy, invsqnorm;
assert (! _linear_pattern_is_degenerate (linear));
/*
* Linear gradients are othrogonal to the line passing through
* their extremes. Because of convexity, the parameter range can
* be computed as the convex hull (one the real line) of the
* parameter values of the 4 corners of the box.
*
* The parameter value t for a point (x,y) can be computed as:
*
* t = (p2 - p1) . (x,y) / |p2 - p1|^2
*
* t0 is the t value for the top left corner
* tdx is the difference between left and right corners
* tdy is the difference between top and bottom corners
*/
p1x = linear->pd1.x;
p1y = linear->pd1.y;
pdx = linear->pd2.x - p1x;
pdy = linear->pd2.y - p1y;
invsqnorm = 1.0 / (pdx * pdx + pdy * pdy);
pdx *= invsqnorm;
pdy *= invsqnorm;
t0 = (x0 - p1x) * pdx + (y0 - p1y) * pdy;
tdx = (x1 - x0) * pdx;
tdy = (y1 - y0) * pdy;
/*
* Because of the linearity of the t value, tdx can simply be
* added the t0 to move along the top edge. After this, range[0]
* and range[1] represent the parameter range for the top edge, so
* extending it to include the whole box simply requires adding
* tdy to the correct extreme.
*/
range[0] = range[1] = t0;
if (tdx < 0)
range[0] += tdx;
else
range[1] += tdx;
if (tdy < 0)
range[0] += tdy;
else
range[1] += tdy;
}
static cairo_bool_t
_extend_range (double range[2], double value, cairo_bool_t valid)
{
if (!valid)
range[0] = range[1] = value;
else if (value < range[0])
range[0] = value;
else if (value > range[1])
range[1] = value;
return TRUE;
}
/*
* _cairo_radial_pattern_focus_is_inside:
*
* Returns %TRUE if and only if the focus point exists and is
* contained in one of the two extreme circles. This condition is
* equivalent to one of the two extreme circles being completely
* contained in the other one.
*
* Note: if the focus is on the border of one of the two circles (in
* which case the circles are tangent in the focus point), it is not
* considered as contained in the circle, hence this function returns
* %FALSE.
*
*/
cairo_bool_t
_cairo_radial_pattern_focus_is_inside (const cairo_radial_pattern_t *radial)
{
double cx, cy, cr, dx, dy, dr;
cx = radial->cd1.center.x;
cy = radial->cd1.center.y;
cr = radial->cd1.radius;
dx = radial->cd2.center.x - cx;
dy = radial->cd2.center.y - cy;
dr = radial->cd2.radius - cr;
return dx*dx + dy*dy < dr*dr;
}
static void
_cairo_radial_pattern_box_to_parameter (const cairo_radial_pattern_t *radial,
double x0, double y0,
double x1, double y1,
double tolerance,
double range[2])
{
double cx, cy, cr, dx, dy, dr;
double a, x_focus, y_focus;
double mindr, minx, miny, maxx, maxy;
cairo_bool_t valid;
assert (! _radial_pattern_is_degenerate (radial));
assert (x0 < x1);
assert (y0 < y1);
tolerance = MAX (tolerance, DBL_EPSILON);
range[0] = range[1] = 0;
valid = FALSE;
x_focus = y_focus = 0; /* silence gcc */
cx = radial->cd1.center.x;
cy = radial->cd1.center.y;
cr = radial->cd1.radius;
dx = radial->cd2.center.x - cx;
dy = radial->cd2.center.y - cy;
dr = radial->cd2.radius - cr;
/* translate by -(cx, cy) to simplify computations */
x0 -= cx;
y0 -= cy;
x1 -= cx;
y1 -= cy;
/* enlarge boundaries slightly to avoid rounding problems in the
* parameter range computation */
x0 -= DBL_EPSILON;
y0 -= DBL_EPSILON;
x1 += DBL_EPSILON;
y1 += DBL_EPSILON;
/* enlarge boundaries even more to avoid rounding problems when
* testing if a point belongs to the box */
minx = x0 - DBL_EPSILON;
miny = y0 - DBL_EPSILON;
maxx = x1 + DBL_EPSILON;
maxy = y1 + DBL_EPSILON;
/* we don't allow negative radiuses, so we will be checking that
* t*dr >= mindr to consider t valid */
mindr = -(cr + DBL_EPSILON);
/*
* After the previous transformations, the start circle is
* centered in the origin and has radius cr. A 1-unit change in
* the t parameter corresponds to dx,dy,dr changes in the x,y,r of
* the circle (center coordinates, radius).
*
* To compute the minimum range needed to correctly draw the
* pattern, we start with an empty range and extend it to include
* the circles touching the bounding box or within it.
*/
/*
* Focus, the point where the circle has radius == 0.
*
* r = cr + t * dr = 0
* t = -cr / dr
*
* If the radius is constant (dr == 0) there is no focus (the
* gradient represents a cylinder instead of a cone).
*/
if (fabs (dr) >= DBL_EPSILON) {
double t_focus;
t_focus = -cr / dr;
x_focus = t_focus * dx;
y_focus = t_focus * dy;
if (minx <= x_focus && x_focus <= maxx &&
miny <= y_focus && y_focus <= maxy)
{
valid = _extend_range (range, t_focus, valid);
}
}
/*
* Circles externally tangent to box edges.
*
* All circles have center in (dx, dy) * t
*
* If the circle is tangent to the line defined by the edge of the
* box, then at least one of the following holds true:
*
* (dx*t) + (cr + dr*t) == x0 (left edge)
* (dx*t) - (cr + dr*t) == x1 (right edge)
* (dy*t) + (cr + dr*t) == y0 (top edge)
* (dy*t) - (cr + dr*t) == y1 (bottom edge)
*
* The solution is only valid if the tangent point is actually on
* the edge, i.e. if its y coordinate is in [y0,y1] for left/right
* edges and if its x coordinate is in [x0,x1] for top/bottom
* edges.
*
* For the first equation:
*
* (dx + dr) * t = x0 - cr
* t = (x0 - cr) / (dx + dr)
* y = dy * t
*
* in the code this becomes:
*
* t_edge = (num) / (den)
* v = (delta) * t_edge
*
* If the denominator in t is 0, the pattern is tangent to a line
* parallel to the edge under examination. The corner-case where
* the boundary line is the same as the edge is handled by the
* focus point case and/or by the a==0 case.
*/
#define T_EDGE(num,den,delta,lower,upper) \
if (fabs (den) >= DBL_EPSILON) { \
double t_edge, v; \
\
t_edge = (num) / (den); \
v = t_edge * (delta); \
if (t_edge * dr >= mindr && (lower) <= v && v <= (upper)) \
valid = _extend_range (range, t_edge, valid); \
}
/* circles tangent (externally) to left/right/top/bottom edge */
T_EDGE (x0 - cr, dx + dr, dy, miny, maxy);
T_EDGE (x1 + cr, dx - dr, dy, miny, maxy);
T_EDGE (y0 - cr, dy + dr, dx, minx, maxx);
T_EDGE (y1 + cr, dy - dr, dx, minx, maxx);
#undef T_EDGE
/*
* Circles passing through a corner.
*
* A circle passing through the point (x,y) satisfies:
*
* (x-t*dx)^2 + (y-t*dy)^2 == (cr + t*dr)^2
*
* If we set:
* a = dx^2 + dy^2 - dr^2
* b = x*dx + y*dy + cr*dr
* c = x^2 + y^2 - cr^2
* we have:
* a*t^2 - 2*b*t + c == 0
*/
a = dx * dx + dy * dy - dr * dr;
if (fabs (a) < DBL_EPSILON * DBL_EPSILON) {
double b, maxd2;
/* Ensure that gradients with both a and dr small are
* considered degenerate.
* The floating point version of the degeneracy test implemented
* in _radial_pattern_is_degenerate() is:
*
* 1) The circles are practically the same size:
* |dr| < DBL_EPSILON
* AND
* 2a) The circles are both very small:
* min (r0, r1) < DBL_EPSILON
* OR
* 2b) The circles are very close to each other:
* max (|dx|, |dy|) < 2 * DBL_EPSILON
*
* Assuming that the gradient is not degenerate, we want to
* show that |a| < DBL_EPSILON^2 implies |dr| >= DBL_EPSILON.
*
* If the gradient is not degenerate yet it has |dr| <
* DBL_EPSILON, (2b) is false, thus:
*
* max (|dx|, |dy|) >= 2*DBL_EPSILON
* which implies:
* 4*DBL_EPSILON^2 <= max (|dx|, |dy|)^2 <= dx^2 + dy^2
*
* From the definition of a, we get:
* a = dx^2 + dy^2 - dr^2 < DBL_EPSILON^2
* dx^2 + dy^2 - DBL_EPSILON^2 < dr^2
* 3*DBL_EPSILON^2 < dr^2
*
* which is inconsistent with the hypotheses, thus |dr| <
* DBL_EPSILON is false or the gradient is degenerate.
*/
assert (fabs (dr) >= DBL_EPSILON);
/*
* If a == 0, all the circles are tangent to a line in the
* focus point. If this line is within the box extents, we
* should add the circle with infinite radius, but this would
* make the range unbounded, so we add the smallest circle whose
* distance to the desired (degenerate) circle within the
* bounding box does not exceed tolerance.
*
* The equation of the line is b==0, i.e.:
* x*dx + y*dy + cr*dr == 0
*
* We compute the intersection of the line with the box and
* keep the intersection with maximum square distance (maxd2)
* from the focus point.
*
* In the code the intersection is represented in another
* coordinate system, whose origin is the focus point and
* which has a u,v axes, which are respectively orthogonal and
* parallel to the edge being intersected.
*
* The intersection is valid only if it belongs to the box,
* otherwise it is ignored.
*
* For example:
*
* y = y0
* x*dx + y0*dy + cr*dr == 0
* x = -(y0*dy + cr*dr) / dx
*
* which in (u,v) is:
* u = y0 - y_focus
* v = -(y0*dy + cr*dr) / dx - x_focus
*
* In the code:
* u = (edge) - (u_origin)
* v = -((edge) * (delta) + cr*dr) / (den) - v_focus
*/
#define T_EDGE(edge,delta,den,lower,upper,u_origin,v_origin) \
if (fabs (den) >= DBL_EPSILON) { \
double v; \
\
v = -((edge) * (delta) + cr * dr) / (den); \
if ((lower) <= v && v <= (upper)) { \
double u, d2; \
\
u = (edge) - (u_origin); \
v -= (v_origin); \
d2 = u*u + v*v; \
if (maxd2 < d2) \
maxd2 = d2; \
} \
}
maxd2 = 0;
/* degenerate circles (lines) passing through each edge */
T_EDGE (y0, dy, dx, minx, maxx, y_focus, x_focus);
T_EDGE (y1, dy, dx, minx, maxx, y_focus, x_focus);
T_EDGE (x0, dx, dy, miny, maxy, x_focus, y_focus);
T_EDGE (x1, dx, dy, miny, maxy, x_focus, y_focus);
#undef T_EDGE
/*
* The limit circle can be transformed rigidly to the y=0 line
* and the circles tangent to it in (0,0) are:
*
* x^2 + (y-r)^2 = r^2 <=> x^2 + y^2 - 2*y*r = 0
*
* y is the distance from the line, in our case tolerance;
* x is the distance along the line, i.e. sqrt(maxd2),
* so:
*
* r = cr + dr * t = (maxd2 + tolerance^2) / (2*tolerance)
* t = (r - cr) / dr =
* (maxd2 + tolerance^2 - 2*tolerance*cr) / (2*tolerance*dr)
*/
if (maxd2 > 0) {
double t_limit = maxd2 + tolerance*tolerance - 2*tolerance*cr;
t_limit /= 2 * tolerance * dr;
valid = _extend_range (range, t_limit, valid);
}
/*
* Nondegenerate, nonlimit circles passing through the corners.
*
* a == 0 && a*t^2 - 2*b*t + c == 0
*
* t = c / (2*b)
*
* The b == 0 case has just been handled, so we only have to
* compute this if b != 0.
*/
#define T_CORNER(x,y) \
b = (x) * dx + (y) * dy + cr * dr; \
if (fabs (b) >= DBL_EPSILON) { \
double t_corner; \
double x2 = (x) * (x); \
double y2 = (y) * (y); \
double cr2 = (cr) * (cr); \
double c = x2 + y2 - cr2; \
\
t_corner = 0.5 * c / b; \
if (t_corner * dr >= mindr) \
valid = _extend_range (range, t_corner, valid); \
}
/* circles touching each corner */
T_CORNER (x0, y0);
T_CORNER (x0, y1);
T_CORNER (x1, y0);
T_CORNER (x1, y1);
#undef T_CORNER
} else {
double inva, b, c, d;
inva = 1 / a;
/*
* Nondegenerate, nonlimit circles passing through the corners.
*
* a != 0 && a*t^2 - 2*b*t + c == 0
*
* t = (b +- sqrt (b*b - a*c)) / a
*
* If the argument of sqrt() is negative, then no circle
* passes through the corner.
*/
#define T_CORNER(x,y) \
b = (x) * dx + (y) * dy + cr * dr; \
c = (x) * (x) + (y) * (y) - cr * cr; \
d = b * b - a * c; \
if (d >= 0) { \
double t_corner; \
\
d = sqrt (d); \
t_corner = (b + d) * inva; \
if (t_corner * dr >= mindr) \
valid = _extend_range (range, t_corner, valid); \
t_corner = (b - d) * inva; \
if (t_corner * dr >= mindr) \
valid = _extend_range (range, t_corner, valid); \
}
/* circles touching each corner */
T_CORNER (x0, y0);
T_CORNER (x0, y1);
T_CORNER (x1, y0);
T_CORNER (x1, y1);
#undef T_CORNER
}
}
/**
* _cairo_gradient_pattern_box_to_parameter:
*
* Compute a interpolation range sufficient to draw (within the given
* tolerance) the gradient in the given box getting the same result as
* using the (-inf, +inf) range.
*
* Assumes that the pattern is not degenerate. This can be guaranteed
* by simplifying it to a solid clear if _cairo_pattern_is_clear or to
* a solid color if _cairo_gradient_pattern_is_solid.
*
* The range isn't guaranteed to be minimal, but it tries to.
**/
void
_cairo_gradient_pattern_box_to_parameter (const cairo_gradient_pattern_t *gradient,
double x0, double y0,
double x1, double y1,
double tolerance,
double out_range[2])
{
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
_cairo_linear_pattern_box_to_parameter ((cairo_linear_pattern_t *) gradient,
x0, y0, x1, y1, out_range);
} else {
_cairo_radial_pattern_box_to_parameter ((cairo_radial_pattern_t *) gradient,
x0, y0, x1, y1, tolerance, out_range);
}
}
/**
* _cairo_gradient_pattern_interpolate:
*
* Interpolate between the start and end objects of linear or radial
* gradients. The interpolated object is stored in out_circle, with
* the radius being zero in the linear gradient case.
**/
void
_cairo_gradient_pattern_interpolate (const cairo_gradient_pattern_t *gradient,
double t,
cairo_circle_double_t *out_circle)
{
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
#define lerp(a,b) (a)*(1-t) + (b)*t
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
out_circle->center.x = lerp (linear->pd1.x, linear->pd2.x);
out_circle->center.y = lerp (linear->pd1.y, linear->pd2.y);
out_circle->radius = 0;
} else {
cairo_radial_pattern_t *radial = (cairo_radial_pattern_t *) gradient;
out_circle->center.x = lerp (radial->cd1.center.x, radial->cd2.center.x);
out_circle->center.y = lerp (radial->cd1.center.y, radial->cd2.center.y);
out_circle->radius = lerp (radial->cd1.radius , radial->cd2.radius);
}
#undef lerp
}
/**
* _cairo_gradient_pattern_fit_to_range:
*
* Scale the extremes of a gradient to guarantee that the coordinates
* and their deltas are within the range (-max_value, max_value). The
* new extremes are stored in out_circle.
*
* The pattern matrix is scaled to guarantee that the aspect of the
* gradient is the same and the result is stored in out_matrix.
*
**/
void
_cairo_gradient_pattern_fit_to_range (const cairo_gradient_pattern_t *gradient,
double max_value,
cairo_matrix_t *out_matrix,
cairo_circle_double_t out_circle[2])
{
double dim;
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
out_circle[0].center = linear->pd1;
out_circle[0].radius = 0;
out_circle[1].center = linear->pd2;
out_circle[1].radius = 0;
dim = fabs (linear->pd1.x);
dim = MAX (dim, fabs (linear->pd1.y));
dim = MAX (dim, fabs (linear->pd2.x));
dim = MAX (dim, fabs (linear->pd2.y));
dim = MAX (dim, fabs (linear->pd1.x - linear->pd2.x));
dim = MAX (dim, fabs (linear->pd1.y - linear->pd2.y));
} else {
cairo_radial_pattern_t *radial = (cairo_radial_pattern_t *) gradient;
out_circle[0] = radial->cd1;
out_circle[1] = radial->cd2;
dim = fabs (radial->cd1.center.x);
dim = MAX (dim, fabs (radial->cd1.center.y));
dim = MAX (dim, fabs (radial->cd1.radius));
dim = MAX (dim, fabs (radial->cd2.center.x));
dim = MAX (dim, fabs (radial->cd2.center.y));
dim = MAX (dim, fabs (radial->cd2.radius));
dim = MAX (dim, fabs (radial->cd1.center.x - radial->cd2.center.x));
dim = MAX (dim, fabs (radial->cd1.center.y - radial->cd2.center.y));
dim = MAX (dim, fabs (radial->cd1.radius - radial->cd2.radius));
}
if (unlikely (dim > max_value)) {
cairo_matrix_t scale;
dim = max_value / dim;
out_circle[0].center.x *= dim;
out_circle[0].center.y *= dim;
out_circle[0].radius *= dim;
out_circle[1].center.x *= dim;
out_circle[1].center.y *= dim;
out_circle[1].radius *= dim;
cairo_matrix_init_scale (&scale, dim, dim);
cairo_matrix_multiply (out_matrix, &gradient->base.matrix, &scale);
} else {
*out_matrix = gradient->base.matrix;
}
}
static cairo_bool_t
_gradient_is_clear (const cairo_gradient_pattern_t *gradient,
const cairo_rectangle_int_t *extents)
{
unsigned int i;
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
if (gradient->n_stops == 0 ||
(gradient->base.extend == CAIRO_EXTEND_NONE &&
gradient->stops[0].offset == gradient->stops[gradient->n_stops - 1].offset))
return TRUE;
if (gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL) {
/* degenerate radial gradients are clear */
if (_radial_pattern_is_degenerate ((cairo_radial_pattern_t *) gradient))
return TRUE;
} else if (gradient->base.extend == CAIRO_EXTEND_NONE) {
/* EXTEND_NONE degenerate linear gradients are clear */
if (_linear_pattern_is_degenerate ((cairo_linear_pattern_t *) gradient))
return TRUE;
}
/* Check if the extents intersect the drawn part of the pattern. */
if (extents != NULL &&
(gradient->base.extend == CAIRO_EXTEND_NONE ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL))
{
double t[2];
_cairo_gradient_pattern_box_to_parameter (gradient,
extents->x,
extents->y,
extents->x + extents->width,
extents->y + extents->height,
DBL_EPSILON,
t);
if (gradient->base.extend == CAIRO_EXTEND_NONE &&
(t[0] >= gradient->stops[gradient->n_stops - 1].offset ||
t[1] <= gradient->stops[0].offset))
{
return TRUE;
}
if (t[0] == t[1])
return TRUE;
}
for (i = 0; i < gradient->n_stops; i++)
if (! CAIRO_COLOR_IS_CLEAR (&gradient->stops[i].color))
return FALSE;
return TRUE;
}
static void
_gradient_color_average (const cairo_gradient_pattern_t *gradient,
cairo_color_t *color)
{
double delta0, delta1;
double r, g, b, a;
unsigned int i, start = 1, end;
assert (gradient->n_stops > 0);
assert (gradient->base.extend != CAIRO_EXTEND_NONE);
if (gradient->n_stops == 1) {
_cairo_color_init_rgba (color,
gradient->stops[0].color.red,
gradient->stops[0].color.green,
gradient->stops[0].color.blue,
gradient->stops[0].color.alpha);
return;
}
end = gradient->n_stops - 1;
switch (gradient->base.extend) {
case CAIRO_EXTEND_REPEAT:
/*
* Sa, Sb and Sy, Sz are the first two and last two stops respectively.
* The weight of the first and last stop can be computed as the area of
* the following triangles (taken with height 1, since the whole [0-1]
* will have total weight 1 this way): b*h/2
*
* + +
* / |\ / | \
* / | \ / | \
* / | \ / | \
* ~~~~~+---+---+---+~~~~~~~+-------+---+---+~~~~~
* -1+Sz 0 Sa Sb Sy Sz 1 1+Sa
*
* For the first stop: (Sb-(-1+Sz)/2 = (1+Sb-Sz)/2
* For the last stop: ((1+Sa)-Sy)/2 = (1+Sa-Sy)/2
* Halving the result is done after summing up all the areas.
*/
delta0 = 1.0 + gradient->stops[1].offset - gradient->stops[end].offset;
delta1 = 1.0 + gradient->stops[0].offset - gradient->stops[end-1].offset;
break;
case CAIRO_EXTEND_REFLECT:
/*
* Sa, Sb and Sy, Sz are the first two and last two stops respectively.
* The weight of the first and last stop can be computed as the area of
* the following trapezoids (taken with height 1, since the whole [0-1]
* will have total weight 1 this way): (b+B)*h/2
*
* +-------+ +---+
* | |\ / | |
* | | \ / | |
* | | \ / | |
* +-------+---+~~~~~~~+-------+---+
* 0 Sa Sb Sy Sz 1
*
* For the first stop: (Sa+Sb)/2
* For the last stop: ((1-Sz) + (1-Sy))/2 = (2-Sy-Sz)/2
* Halving the result is done after summing up all the areas.
*/
delta0 = gradient->stops[0].offset + gradient->stops[1].offset;
delta1 = 2.0 - gradient->stops[end-1].offset - gradient->stops[end].offset;
break;
case CAIRO_EXTEND_PAD:
/* PAD is computed as the average of the first and last stop:
* - take both of them with weight 1 (they will be halved
* after the whole sum has been computed).
* - avoid summing any of the inner stops.
*/
delta0 = delta1 = 1.0;
start = end;
break;
case CAIRO_EXTEND_NONE:
default:
ASSERT_NOT_REACHED;
_cairo_color_init_rgba (color, 0, 0, 0, 0);
return;
}
r = delta0 * gradient->stops[0].color.red;
g = delta0 * gradient->stops[0].color.green;
b = delta0 * gradient->stops[0].color.blue;
a = delta0 * gradient->stops[0].color.alpha;
for (i = start; i < end; ++i) {
/* Inner stops weight is the same as the area of the triangle they influence
* (which goes from the stop before to the stop after), again with height 1
* since the whole must sum up to 1: b*h/2
* Halving is done after the whole sum has been computed.
*/
double delta = gradient->stops[i+1].offset - gradient->stops[i-1].offset;
r += delta * gradient->stops[i].color.red;
g += delta * gradient->stops[i].color.green;
b += delta * gradient->stops[i].color.blue;
a += delta * gradient->stops[i].color.alpha;
}
r += delta1 * gradient->stops[end].color.red;
g += delta1 * gradient->stops[end].color.green;
b += delta1 * gradient->stops[end].color.blue;
a += delta1 * gradient->stops[end].color.alpha;
_cairo_color_init_rgba (color, r * .5, g * .5, b * .5, a * .5);
}
/**
* _cairo_pattern_alpha_range:
*
* Convenience function to determine the minimum and maximum alpha in
* the drawn part of a pattern (i.e. ignoring clear parts caused by
* extend modes and/or pattern shape).
*
* If not NULL, out_min and out_max will be set respectively to the
* minimum and maximum alpha value of the pattern.
**/
void
_cairo_pattern_alpha_range (const cairo_pattern_t *pattern,
double *out_min,
double *out_max)
{
double alpha_min, alpha_max;
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID: {
const cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) pattern;
alpha_min = alpha_max = solid->color.alpha;
break;
}
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL: {
const cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t *) pattern;
unsigned int i;
assert (gradient->n_stops >= 1);
alpha_min = alpha_max = gradient->stops[0].color.alpha;
for (i = 1; i < gradient->n_stops; i++) {
if (alpha_min > gradient->stops[i].color.alpha)
alpha_min = gradient->stops[i].color.alpha;
else if (alpha_max < gradient->stops[i].color.alpha)
alpha_max = gradient->stops[i].color.alpha;
}
break;
}
case CAIRO_PATTERN_TYPE_MESH: {
const cairo_mesh_pattern_t *mesh = (const cairo_mesh_pattern_t *) pattern;
const cairo_mesh_patch_t *patch = _cairo_array_index_const (&mesh->patches, 0);
unsigned int i, j, n = _cairo_array_num_elements (&mesh->patches);
assert (n >= 1);
alpha_min = alpha_max = patch[0].colors[0].alpha;
for (i = 0; i < n; i++) {
for (j = 0; j < 4; j++) {
if (patch[i].colors[j].alpha < alpha_min)
alpha_min = patch[i].colors[j].alpha;
else if (patch[i].colors[j].alpha > alpha_max)
alpha_max = patch[i].colors[j].alpha;
}
}
break;
}
default:
ASSERT_NOT_REACHED;
/* fall through */
case CAIRO_PATTERN_TYPE_SURFACE:
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
alpha_min = 0;
alpha_max = 1;
break;
}
if (out_min)
*out_min = alpha_min;
if (out_max)
*out_max = alpha_max;
}
/**
* _cairo_mesh_pattern_coord_box:
*
* Convenience function to determine the range of the coordinates of
* the points used to define the patches of the mesh.
*
* This is guaranteed to contain the pattern extents, but might not be
* tight, just like a Bezier curve is always inside the convex hull of
* the control points.
*
* This function cannot be used while the mesh is being constructed.
*
* The function returns TRUE and sets the output parameters to define
* the coordinate range if the mesh pattern contains at least one
* patch, otherwise it returns FALSE.
**/
cairo_bool_t
_cairo_mesh_pattern_coord_box (const cairo_mesh_pattern_t *mesh,
double *out_xmin,
double *out_ymin,
double *out_xmax,
double *out_ymax)
{
const cairo_mesh_patch_t *patch;
unsigned int num_patches, i, j, k;
double x0, y0, x1, y1;
assert (mesh->current_patch == NULL);
num_patches = _cairo_array_num_elements (&mesh->patches);
if (num_patches == 0)
return FALSE;
patch = _cairo_array_index_const (&mesh->patches, 0);
x0 = x1 = patch->points[0][0].x;
y0 = y1 = patch->points[0][0].y;
for (i = 0; i < num_patches; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
x0 = MIN (x0, patch[i].points[j][k].x);
y0 = MIN (y0, patch[i].points[j][k].y);
x1 = MAX (x1, patch[i].points[j][k].x);
y1 = MAX (y1, patch[i].points[j][k].y);
}
}
}
*out_xmin = x0;
*out_ymin = y0;
*out_xmax = x1;
*out_ymax = y1;
return TRUE;
}
/**
* _cairo_gradient_pattern_is_solid:
*
* Convenience function to determine whether a gradient pattern is
* a solid color within the given extents. In this case the color
* argument is initialized to the color the pattern represents.
* This functions doesn't handle completely transparent gradients,
* thus it should be called only after _cairo_pattern_is_clear has
* returned FALSE.
*
* Return value: %TRUE if the pattern is a solid color.
**/
cairo_bool_t
_cairo_gradient_pattern_is_solid (const cairo_gradient_pattern_t *gradient,
const cairo_rectangle_int_t *extents,
cairo_color_t *color)
{
unsigned int i;
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
/* TODO: radial */
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
if (_linear_pattern_is_degenerate (linear)) {
_gradient_color_average (gradient, color);
return TRUE;
}
if (gradient->base.extend == CAIRO_EXTEND_NONE) {
double t[2];
/* We already know that the pattern is not clear, thus if some
* part of it is clear, the whole is not solid.
*/
if (extents == NULL)
return FALSE;
_cairo_linear_pattern_box_to_parameter (linear,
extents->x,
extents->y,
extents->x + extents->width,
extents->y + extents->height,
t);
if (t[0] < 0.0 || t[1] > 1.0)
return FALSE;
}
} else
return FALSE;
for (i = 1; i < gradient->n_stops; i++)
if (! _cairo_color_stop_equal (&gradient->stops[0].color,
&gradient->stops[i].color))
return FALSE;
_cairo_color_init_rgba (color,
gradient->stops[0].color.red,
gradient->stops[0].color.green,
gradient->stops[0].color.blue,
gradient->stops[0].color.alpha);
return TRUE;
}
/**
* _cairo_pattern_is_constant_alpha:
*
* Convenience function to determine whether a pattern has constant
* alpha within the given extents. In this case the alpha argument is
* initialized to the alpha within the extents.
*
* Return value: %TRUE if the pattern has constant alpha.
**/
cairo_bool_t
_cairo_pattern_is_constant_alpha (const cairo_pattern_t *abstract_pattern,
const cairo_rectangle_int_t *extents,
double *alpha)
{
const cairo_pattern_union_t *pattern;
cairo_color_t color;
if (_cairo_pattern_is_clear (abstract_pattern)) {
*alpha = 0.0;
return TRUE;
}
if (_cairo_pattern_is_opaque (abstract_pattern, extents)) {
*alpha = 1.0;
return TRUE;
}
pattern = (cairo_pattern_union_t *) abstract_pattern;
switch (pattern->base.type) {
case CAIRO_PATTERN_TYPE_SOLID:
*alpha = pattern->solid.color.alpha;
return TRUE;
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL:
if (_cairo_gradient_pattern_is_solid (&pattern->gradient.base, extents, &color)) {
*alpha = color.alpha;
return TRUE;
} else {
return FALSE;
}
/* TODO: need to test these as well */
case CAIRO_PATTERN_TYPE_SURFACE:
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
case CAIRO_PATTERN_TYPE_MESH:
return FALSE;
}
ASSERT_NOT_REACHED;
return FALSE;
}
static cairo_bool_t
_mesh_is_clear (const cairo_mesh_pattern_t *mesh)
{
double x1, y1, x2, y2;
cairo_bool_t is_valid;
is_valid = _cairo_mesh_pattern_coord_box (mesh, &x1, &y1, &x2, &y2);
if (!is_valid)
return TRUE;
if (x2 - x1 < DBL_EPSILON || y2 - y1 < DBL_EPSILON)
return TRUE;
return FALSE;
}
/**
* _cairo_pattern_is_opaque_solid:
*
* Convenience function to determine whether a pattern is an opaque
* (alpha==1.0) solid color pattern. This is done by testing whether
* the pattern's alpha value when converted to a byte is 255, so if a
* backend actually supported deep alpha channels this function might
* not do the right thing.
*
* Return value: %TRUE if the pattern is an opaque, solid color.
**/
cairo_bool_t
_cairo_pattern_is_opaque_solid (const cairo_pattern_t *pattern)
{
cairo_solid_pattern_t *solid;
if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
return FALSE;
solid = (cairo_solid_pattern_t *) pattern;
return CAIRO_COLOR_IS_OPAQUE (&solid->color);
}
static cairo_bool_t
_surface_is_opaque (const cairo_surface_pattern_t *pattern,
const cairo_rectangle_int_t *sample)
{
cairo_rectangle_int_t extents;
if (pattern->surface->content & CAIRO_CONTENT_ALPHA)
return FALSE;
if (pattern->base.extend != CAIRO_EXTEND_NONE)
return TRUE;
if (! _cairo_surface_get_extents (pattern->surface, &extents))
return TRUE;
if (sample == NULL)
return FALSE;
return _cairo_rectangle_contains_rectangle (&extents, sample);
}
static cairo_bool_t
_raster_source_is_opaque (const cairo_raster_source_pattern_t *pattern,
const cairo_rectangle_int_t *sample)
{
if (pattern->content & CAIRO_CONTENT_ALPHA)
return FALSE;
if (pattern->base.extend != CAIRO_EXTEND_NONE)
return TRUE;
if (sample == NULL)
return FALSE;
return _cairo_rectangle_contains_rectangle (&pattern->extents, sample);
}
static cairo_bool_t
_surface_is_clear (const cairo_surface_pattern_t *pattern)
{
cairo_rectangle_int_t extents;
if (_cairo_surface_get_extents (pattern->surface, &extents) &&
(extents.width == 0 || extents.height == 0))
return TRUE;
return pattern->surface->is_clear &&
pattern->surface->content & CAIRO_CONTENT_ALPHA;
}
static cairo_bool_t
_raster_source_is_clear (const cairo_raster_source_pattern_t *pattern)
{
return pattern->extents.width == 0 || pattern->extents.height == 0;
}
static cairo_bool_t
_gradient_is_opaque (const cairo_gradient_pattern_t *gradient,
const cairo_rectangle_int_t *sample)
{
unsigned int i;
assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
if (gradient->n_stops == 0 ||
(gradient->base.extend == CAIRO_EXTEND_NONE &&
gradient->stops[0].offset == gradient->stops[gradient->n_stops - 1].offset))
return FALSE;
if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
if (gradient->base.extend == CAIRO_EXTEND_NONE) {
double t[2];
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
/* EXTEND_NONE degenerate radial gradients are clear */
if (_linear_pattern_is_degenerate (linear))
return FALSE;
if (sample == NULL)
return FALSE;
_cairo_linear_pattern_box_to_parameter (linear,
sample->x,
sample->y,
sample->x + sample->width,
sample->y + sample->height,
t);
if (t[0] < 0.0 || t[1] > 1.0)
return FALSE;
}
} else
return FALSE; /* TODO: check actual intersection */
for (i = 0; i < gradient->n_stops; i++)
if (! CAIRO_COLOR_IS_OPAQUE (&gradient->stops[i].color))
return FALSE;
return TRUE;
}
/**
* _cairo_pattern_is_opaque:
*
* Convenience function to determine whether a pattern is an opaque
* pattern (of any type). The same caveats that apply to
* _cairo_pattern_is_opaque_solid apply here as well.
*
* Return value: %TRUE if the pattern is a opaque.
**/
cairo_bool_t
_cairo_pattern_is_opaque (const cairo_pattern_t *abstract_pattern,
const cairo_rectangle_int_t *sample)
{
const cairo_pattern_union_t *pattern;
if (abstract_pattern->has_component_alpha)
return FALSE;
pattern = (cairo_pattern_union_t *) abstract_pattern;
switch (pattern->base.type) {
case CAIRO_PATTERN_TYPE_SOLID:
return _cairo_pattern_is_opaque_solid (abstract_pattern);
case CAIRO_PATTERN_TYPE_SURFACE:
return _surface_is_opaque (&pattern->surface, sample);
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
return _raster_source_is_opaque (&pattern->raster_source, sample);
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL:
return _gradient_is_opaque (&pattern->gradient.base, sample);
case CAIRO_PATTERN_TYPE_MESH:
return FALSE;
}
ASSERT_NOT_REACHED;
return FALSE;
}
cairo_bool_t
_cairo_pattern_is_clear (const cairo_pattern_t *abstract_pattern)
{
const cairo_pattern_union_t *pattern;
if (abstract_pattern->has_component_alpha)
return FALSE;
pattern = (cairo_pattern_union_t *) abstract_pattern;
switch (abstract_pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
return CAIRO_COLOR_IS_CLEAR (&pattern->solid.color);
case CAIRO_PATTERN_TYPE_SURFACE:
return _surface_is_clear (&pattern->surface);
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
return _raster_source_is_clear (&pattern->raster_source);
case CAIRO_PATTERN_TYPE_LINEAR:
case CAIRO_PATTERN_TYPE_RADIAL:
return _gradient_is_clear (&pattern->gradient.base, NULL);
case CAIRO_PATTERN_TYPE_MESH:
return _mesh_is_clear (&pattern->mesh);
}
ASSERT_NOT_REACHED;
return FALSE;
}
/*
* Will given row of back-translation matrix work with bilinear scale?
* This is true for scales larger than 1. Also it was judged acceptable
* for scales larger than .75. And if there is integer translation
* then a scale of exactly .5 works.
*/
static int
use_bilinear(double x, double y, double t)
{
/* This is the inverse matrix! */
double h = x*x + y*y;
if (h < 1.0 / (0.75 * 0.75))
return TRUE; /* scale > .75 */
if ((h > 3.99 && h < 4.01) /* scale is 1/2 */
&& !_cairo_fixed_from_double(x*y) /* parallel to an axis */
&& _cairo_fixed_is_integer (_cairo_fixed_from_double (t)))
return TRUE;
return FALSE;
}
/**
* _cairo_pattern_analyze_filter:
* @pattern: surface pattern
* Returns: the optimized #cairo_filter_t to use with @pattern.
*
* Possibly optimize the filter to a simpler value depending on transformation
**/
cairo_filter_t
_cairo_pattern_analyze_filter (const cairo_pattern_t *pattern)
{
switch (pattern->filter) {
case CAIRO_FILTER_GOOD:
case CAIRO_FILTER_BEST:
case CAIRO_FILTER_BILINEAR:
case CAIRO_FILTER_FAST:
/* If source pixels map 1:1 onto destination pixels, we do
* not need to filter (and do not want to filter, since it
* will cause blurriness)
*/
if (_cairo_matrix_is_pixel_exact (&pattern->matrix)) {
return CAIRO_FILTER_NEAREST;
} else {
/* Use BILINEAR for any scale greater than .75 instead
* of GOOD. For scales of 1 and larger this is identical,
* for the smaller sizes it was judged that the artifacts
* were not worse than the artifacts from a box filer.
* BILINEAR can also be used if the scale is exactly .5
* and the translation in that direction is an integer.
*/
if (pattern->filter == CAIRO_FILTER_GOOD &&
use_bilinear (pattern->matrix.xx, pattern->matrix.xy,
pattern->matrix.x0) &&
use_bilinear (pattern->matrix.yx, pattern->matrix.yy,
pattern->matrix.y0))
return CAIRO_FILTER_BILINEAR;
}
break;
case CAIRO_FILTER_NEAREST:
case CAIRO_FILTER_GAUSSIAN:
default:
break;
}
return pattern->filter;
}
/**
* _cairo_hypot:
* Returns: value similar to hypot(@x,@y)
*
* May want to replace this with Manhattan distance (abs(x)+abs(y)) if
* hypot is too slow, as there is no need for accuracy here.
**/
static inline double
_cairo_hypot(double x, double y)
{
return hypot(x, y);
}
/**
* _cairo_pattern_sampled_area:
*
* Return region of @pattern that will be sampled to fill @extents,
* based on the transformation and filter.
*
* This does not include pixels that are mulitiplied by values very
* close to zero by the ends of filters. This is so that transforms
* that should be the identity or 90 degree rotations do not expand
* the source unexpectedly.
*
* XXX: We don't actually have any way of querying the backend for
* the filter radius, so we just guess base on what we know that
* backends do currently (see bug #10508)
**/
void
_cairo_pattern_sampled_area (const cairo_pattern_t *pattern,
const cairo_rectangle_int_t *extents,
cairo_rectangle_int_t *sample)
{
double x1, x2, y1, y2;
double padx, pady;
/* Assume filters are interpolating, which means identity
cannot change the image */
if (_cairo_matrix_is_identity (&pattern->matrix)) {
*sample = *extents;
return;
}
/* Transform the centers of the corner pixels */
x1 = extents->x + 0.5;
y1 = extents->y + 0.5;
x2 = x1 + (extents->width - 1);
y2 = y1 + (extents->height - 1);
_cairo_matrix_transform_bounding_box (&pattern->matrix,
&x1, &y1, &x2, &y2,
NULL);
/* How far away from center will it actually sample?
* This is the distance from a transformed pixel center to the
* furthest sample of reasonable size.
*/
switch (pattern->filter) {
case CAIRO_FILTER_NEAREST:
case CAIRO_FILTER_FAST:
/* Correct value is zero, but when the sample is on an integer
* it is unknown if the backend will sample the pixel to the
* left or right. This value makes it include both possible pixels.
*/
padx = pady = 0.004;
break;
case CAIRO_FILTER_BILINEAR:
case CAIRO_FILTER_GAUSSIAN:
default:
/* Correct value is .5 */
padx = pady = 0.495;
break;
case CAIRO_FILTER_GOOD:
/* Correct value is max(width,1)*.5 */
padx = _cairo_hypot (pattern->matrix.xx, pattern->matrix.xy);
if (padx <= 1.0) padx = 0.495;
else if (padx >= 16.0) padx = 7.92;
else padx *= 0.495;
pady = _cairo_hypot (pattern->matrix.yx, pattern->matrix.yy);
if (pady <= 1.0) pady = 0.495;
else if (pady >= 16.0) pady = 7.92;
else pady *= 0.495;
break;
case CAIRO_FILTER_BEST:
/* Correct value is width*2 */
padx = _cairo_hypot (pattern->matrix.xx, pattern->matrix.xy) * 1.98;
if (padx > 7.92) padx = 7.92;
pady = _cairo_hypot (pattern->matrix.yx, pattern->matrix.yy) * 1.98;
if (pady > 7.92) pady = 7.92;
break;
}
/* round furthest samples to edge of pixels */
x1 = floor (x1 - padx);
if (x1 < CAIRO_RECT_INT_MIN) x1 = CAIRO_RECT_INT_MIN;
sample->x = x1;
y1 = floor (y1 - pady);
if (y1 < CAIRO_RECT_INT_MIN) y1 = CAIRO_RECT_INT_MIN;
sample->y = y1;
x2 = floor (x2 + padx) + 1.0;
if (x2 > CAIRO_RECT_INT_MAX) x2 = CAIRO_RECT_INT_MAX;
sample->width = x2 - x1;
y2 = floor (y2 + pady) + 1.0;
if (y2 > CAIRO_RECT_INT_MAX) y2 = CAIRO_RECT_INT_MAX;
sample->height = y2 - y1;
}
/**
* _cairo_pattern_get_extents:
*
* Return the "target-space" extents of @pattern in @extents.
*
* For unbounded patterns, the @extents will be initialized with
* "infinite" extents, (minimum and maximum fixed-point values).
*
* When is_vector is TRUE, avoid rounding to zero widths or heights that
* are less than 1 unit.
*
* XXX: Currently, bounded gradient patterns will also return
* "infinite" extents, though it would be possible to optimize these
* with a little more work.
**/
void
_cairo_pattern_get_extents (const cairo_pattern_t *pattern,
cairo_rectangle_int_t *extents,
cairo_bool_t is_vector)
{
double x1, y1, x2, y2;
int ix1, ix2, iy1, iy2;
cairo_bool_t round_x = FALSE;
cairo_bool_t round_y = FALSE;
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
goto UNBOUNDED;
case CAIRO_PATTERN_TYPE_SURFACE:
{
cairo_rectangle_int_t surface_extents;
const cairo_surface_pattern_t *surface_pattern =
(const cairo_surface_pattern_t *) pattern;
cairo_surface_t *surface = surface_pattern->surface;
if (! _cairo_surface_get_extents (surface, &surface_extents))
goto UNBOUNDED;
if (surface_extents.width == 0 || surface_extents.height == 0)
goto EMPTY;
if (pattern->extend != CAIRO_EXTEND_NONE)
goto UNBOUNDED;
x1 = surface_extents.x;
y1 = surface_extents.y;
x2 = surface_extents.x + (int) surface_extents.width;
y2 = surface_extents.y + (int) surface_extents.height;
goto HANDLE_FILTER;
}
break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
{
const cairo_raster_source_pattern_t *raster =
(const cairo_raster_source_pattern_t *) pattern;
if (raster->extents.width == 0 || raster->extents.height == 0)
goto EMPTY;
if (pattern->extend != CAIRO_EXTEND_NONE)
goto UNBOUNDED;
x1 = raster->extents.x;
y1 = raster->extents.y;
x2 = raster->extents.x + (int) raster->extents.width;
y2 = raster->extents.y + (int) raster->extents.height;
}
HANDLE_FILTER:
switch (pattern->filter) {
case CAIRO_FILTER_NEAREST:
case CAIRO_FILTER_FAST:
round_x = round_y = TRUE;
/* We don't know which way .5 will go, so fudge it slightly. */
x1 -= 0.004;
y1 -= 0.004;
x2 += 0.004;
y2 += 0.004;
break;
case CAIRO_FILTER_BEST:
/* Assume best filter will produce nice antialiased edges */
break;
case CAIRO_FILTER_BILINEAR:
case CAIRO_FILTER_GAUSSIAN:
case CAIRO_FILTER_GOOD:
default:
/* These filters can blur the edge out 1/2 pixel when scaling up */
if (_cairo_hypot (pattern->matrix.xx, pattern->matrix.yx) < 1.0) {
x1 -= 0.5;
x2 += 0.5;
round_x = TRUE;
}
if (_cairo_hypot (pattern->matrix.xy, pattern->matrix.yy) < 1.0) {
y1 -= 0.5;
y2 += 0.5;
round_y = TRUE;
}
break;
}
break;
case CAIRO_PATTERN_TYPE_RADIAL:
{
const cairo_radial_pattern_t *radial =
(const cairo_radial_pattern_t *) pattern;
double cx1, cy1;
double cx2, cy2;
double r1, r2;
if (_radial_pattern_is_degenerate (radial)) {
/* cairo-gstate should have optimised degenerate
* patterns to solid clear patterns, so we can ignore
* them here. */
goto EMPTY;
}
/* TODO: in some cases (focus outside/on the circle) it is
* half-bounded. */
if (pattern->extend != CAIRO_EXTEND_NONE)
goto UNBOUNDED;
cx1 = radial->cd1.center.x;
cy1 = radial->cd1.center.y;
r1 = radial->cd1.radius;
cx2 = radial->cd2.center.x;
cy2 = radial->cd2.center.y;
r2 = radial->cd2.radius;
x1 = MIN (cx1 - r1, cx2 - r2);
y1 = MIN (cy1 - r1, cy2 - r2);
x2 = MAX (cx1 + r1, cx2 + r2);
y2 = MAX (cy1 + r1, cy2 + r2);
}
break;
case CAIRO_PATTERN_TYPE_LINEAR:
{
const cairo_linear_pattern_t *linear =
(const cairo_linear_pattern_t *) pattern;
if (pattern->extend != CAIRO_EXTEND_NONE)
goto UNBOUNDED;
if (_linear_pattern_is_degenerate (linear)) {
/* cairo-gstate should have optimised degenerate
* patterns to solid ones, so we can again ignore
* them here. */
goto EMPTY;
}
/* TODO: to get tight extents, use the matrix to transform
* the pattern instead of transforming the extents later. */
if (pattern->matrix.xy != 0. || pattern->matrix.yx != 0.)
goto UNBOUNDED;
if (linear->pd1.x == linear->pd2.x) {
x1 = -HUGE_VAL;
x2 = HUGE_VAL;
y1 = MIN (linear->pd1.y, linear->pd2.y);
y2 = MAX (linear->pd1.y, linear->pd2.y);
} else if (linear->pd1.y == linear->pd2.y) {
x1 = MIN (linear->pd1.x, linear->pd2.x);
x2 = MAX (linear->pd1.x, linear->pd2.x);
y1 = -HUGE_VAL;
y2 = HUGE_VAL;
} else {
goto UNBOUNDED;
}
/* The current linear renderer just point-samples in the middle
of the pixels, similar to the NEAREST filter: */
round_x = round_y = TRUE;
}
break;
case CAIRO_PATTERN_TYPE_MESH:
{
const cairo_mesh_pattern_t *mesh =
(const cairo_mesh_pattern_t *) pattern;
if (! _cairo_mesh_pattern_coord_box (mesh, &x1, &y1, &x2, &y2))
goto EMPTY;
}
break;
default:
ASSERT_NOT_REACHED;
}
if (_cairo_matrix_is_translation (&pattern->matrix)) {
x1 -= pattern->matrix.x0; x2 -= pattern->matrix.x0;
y1 -= pattern->matrix.y0; y2 -= pattern->matrix.y0;
} else {
cairo_matrix_t imatrix;
cairo_status_t status;
imatrix = pattern->matrix;
status = cairo_matrix_invert (&imatrix);
/* cairo_pattern_set_matrix ensures the matrix is invertible */
assert (status == CAIRO_STATUS_SUCCESS);
_cairo_matrix_transform_bounding_box (&imatrix,
&x1, &y1, &x2, &y2,
NULL);
}
if (!round_x) {
x1 -= 0.5;
x2 += 0.5;
}
if (x1 < CAIRO_RECT_INT_MIN)
ix1 = CAIRO_RECT_INT_MIN;
else
ix1 = _cairo_lround (x1);
if (x2 > CAIRO_RECT_INT_MAX)
ix2 = CAIRO_RECT_INT_MAX;
else
ix2 = _cairo_lround (x2);
extents->x = ix1; extents->width = ix2 - ix1;
if (is_vector && extents->width == 0 && x1 != x2)
extents->width += 1;
if (!round_y) {
y1 -= 0.5;
y2 += 0.5;
}
if (y1 < CAIRO_RECT_INT_MIN)
iy1 = CAIRO_RECT_INT_MIN;
else
iy1 = _cairo_lround (y1);
if (y2 > CAIRO_RECT_INT_MAX)
iy2 = CAIRO_RECT_INT_MAX;
else
iy2 = _cairo_lround (y2);
extents->y = iy1; extents->height = iy2 - iy1;
if (is_vector && extents->height == 0 && y1 != y2)
extents->height += 1;
return;
UNBOUNDED:
/* unbounded patterns -> 'infinite' extents */
_cairo_unbounded_rectangle_init (extents);
return;
EMPTY:
extents->x = extents->y = 0;
extents->width = extents->height = 0;
return;
}
/**
* _cairo_pattern_get_ink_extents:
*
* Return the "target-space" inked extents of @pattern in @extents.
**/
cairo_int_status_t
_cairo_pattern_get_ink_extents (const cairo_pattern_t *pattern,
cairo_rectangle_int_t *extents)
{
if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE &&
pattern->extend == CAIRO_EXTEND_NONE)
{
const cairo_surface_pattern_t *surface_pattern =
(const cairo_surface_pattern_t *) pattern;
cairo_surface_t *surface = surface_pattern->surface;
surface = _cairo_surface_get_source (surface, NULL);
if (_cairo_surface_is_recording (surface)) {
cairo_matrix_t imatrix;
cairo_box_t box;
cairo_status_t status;
imatrix = pattern->matrix;
status = cairo_matrix_invert (&imatrix);
/* cairo_pattern_set_matrix ensures the matrix is invertible */
assert (status == CAIRO_STATUS_SUCCESS);
status = _cairo_recording_surface_get_ink_bbox ((cairo_recording_surface_t *)surface,
&box, &imatrix);
if (unlikely (status))
return status;
_cairo_box_round_to_rectangle (&box, extents);
return CAIRO_STATUS_SUCCESS;
}
}
_cairo_pattern_get_extents (pattern, extents, TRUE);
return CAIRO_STATUS_SUCCESS;
}
static unsigned long
_cairo_solid_pattern_hash (unsigned long hash,
const cairo_solid_pattern_t *solid)
{
hash = _cairo_hash_bytes (hash, &solid->color, sizeof (solid->color));
return hash;
}
static unsigned long
_cairo_gradient_color_stops_hash (unsigned long hash,
const cairo_gradient_pattern_t *gradient)
{
unsigned int n;
hash = _cairo_hash_bytes (hash,
&gradient->n_stops,
sizeof (gradient->n_stops));
for (n = 0; n < gradient->n_stops; n++) {
hash = _cairo_hash_bytes (hash,
&gradient->stops[n].offset,
sizeof (double));
hash = _cairo_hash_bytes (hash,
&gradient->stops[n].color,
sizeof (cairo_color_stop_t));
}
return hash;
}
unsigned long
_cairo_linear_pattern_hash (unsigned long hash,
const cairo_linear_pattern_t *linear)
{
hash = _cairo_hash_bytes (hash, &linear->pd1, sizeof (linear->pd1));
hash = _cairo_hash_bytes (hash, &linear->pd2, sizeof (linear->pd2));
return _cairo_gradient_color_stops_hash (hash, &linear->base);
}
unsigned long
_cairo_radial_pattern_hash (unsigned long hash,
const cairo_radial_pattern_t *radial)
{
hash = _cairo_hash_bytes (hash, &radial->cd1.center, sizeof (radial->cd1.center));
hash = _cairo_hash_bytes (hash, &radial->cd1.radius, sizeof (radial->cd1.radius));
hash = _cairo_hash_bytes (hash, &radial->cd2.center, sizeof (radial->cd2.center));
hash = _cairo_hash_bytes (hash, &radial->cd2.radius, sizeof (radial->cd2.radius));
return _cairo_gradient_color_stops_hash (hash, &radial->base);
}
static unsigned long
_cairo_mesh_pattern_hash (unsigned long hash, const cairo_mesh_pattern_t *mesh)
{
const cairo_mesh_patch_t *patch = _cairo_array_index_const (&mesh->patches, 0);
unsigned int i, n = _cairo_array_num_elements (&mesh->patches);
for (i = 0; i < n; i++)
hash = _cairo_hash_bytes (hash, patch + i, sizeof (cairo_mesh_patch_t));
return hash;
}
static unsigned long
_cairo_surface_pattern_hash (unsigned long hash,
const cairo_surface_pattern_t *surface)
{
hash ^= surface->surface->unique_id;
return hash;
}
static unsigned long
_cairo_raster_source_pattern_hash (unsigned long hash,
const cairo_raster_source_pattern_t *raster)
{
hash ^= (uintptr_t)raster->user_data;
return hash;
}
unsigned long
_cairo_pattern_hash (const cairo_pattern_t *pattern)
{
unsigned long hash = _CAIRO_HASH_INIT_VALUE;
if (pattern->status)
return 0;
hash = _cairo_hash_bytes (hash, &pattern->type, sizeof (pattern->type));
if (pattern->type != CAIRO_PATTERN_TYPE_SOLID) {
hash = _cairo_hash_bytes (hash,
&pattern->matrix, sizeof (pattern->matrix));
hash = _cairo_hash_bytes (hash,
&pattern->filter, sizeof (pattern->filter));
hash = _cairo_hash_bytes (hash,
&pattern->extend, sizeof (pattern->extend));
hash = _cairo_hash_bytes (hash,
&pattern->has_component_alpha,
sizeof (pattern->has_component_alpha));
}
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID:
return _cairo_solid_pattern_hash (hash, (cairo_solid_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_LINEAR:
return _cairo_linear_pattern_hash (hash, (cairo_linear_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_RADIAL:
return _cairo_radial_pattern_hash (hash, (cairo_radial_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_MESH:
return _cairo_mesh_pattern_hash (hash, (cairo_mesh_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_SURFACE:
return _cairo_surface_pattern_hash (hash, (cairo_surface_pattern_t *) pattern);
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
return _cairo_raster_source_pattern_hash (hash, (cairo_raster_source_pattern_t *) pattern);
default:
ASSERT_NOT_REACHED;
return FALSE;
}
}
static cairo_bool_t
_cairo_solid_pattern_equal (const cairo_solid_pattern_t *a,
const cairo_solid_pattern_t *b)
{
return _cairo_color_equal (&a->color, &b->color);
}
static cairo_bool_t
_cairo_gradient_color_stops_equal (const cairo_gradient_pattern_t *a,
const cairo_gradient_pattern_t *b)
{
unsigned int n;
if (a->n_stops != b->n_stops)
return FALSE;
for (n = 0; n < a->n_stops; n++) {
if (a->stops[n].offset != b->stops[n].offset)
return FALSE;
if (! _cairo_color_stop_equal (&a->stops[n].color, &b->stops[n].color))
return FALSE;
}
return TRUE;
}
cairo_bool_t
_cairo_linear_pattern_equal (const cairo_linear_pattern_t *a,
const cairo_linear_pattern_t *b)
{
if (a->pd1.x != b->pd1.x)
return FALSE;
if (a->pd1.y != b->pd1.y)
return FALSE;
if (a->pd2.x != b->pd2.x)
return FALSE;
if (a->pd2.y != b->pd2.y)
return FALSE;
return _cairo_gradient_color_stops_equal (&a->base, &b->base);
}
cairo_bool_t
_cairo_radial_pattern_equal (const cairo_radial_pattern_t *a,
const cairo_radial_pattern_t *b)
{
if (a->cd1.center.x != b->cd1.center.x)
return FALSE;
if (a->cd1.center.y != b->cd1.center.y)
return FALSE;
if (a->cd1.radius != b->cd1.radius)
return FALSE;
if (a->cd2.center.x != b->cd2.center.x)
return FALSE;
if (a->cd2.center.y != b->cd2.center.y)
return FALSE;
if (a->cd2.radius != b->cd2.radius)
return FALSE;
return _cairo_gradient_color_stops_equal (&a->base, &b->base);
}
static cairo_bool_t
_cairo_mesh_pattern_equal (const cairo_mesh_pattern_t *a,
const cairo_mesh_pattern_t *b)
{
const cairo_mesh_patch_t *patch_a, *patch_b;
unsigned int i, num_patches_a, num_patches_b;
num_patches_a = _cairo_array_num_elements (&a->patches);
num_patches_b = _cairo_array_num_elements (&b->patches);
if (num_patches_a != num_patches_b)
return FALSE;
for (i = 0; i < num_patches_a; i++) {
patch_a = _cairo_array_index_const (&a->patches, i);
patch_b = _cairo_array_index_const (&b->patches, i);
if (memcmp (patch_a, patch_b, sizeof(cairo_mesh_patch_t)) != 0)
return FALSE;
}
return TRUE;
}
static cairo_bool_t
_cairo_surface_pattern_equal (const cairo_surface_pattern_t *a,
const cairo_surface_pattern_t *b)
{
return a->surface->unique_id == b->surface->unique_id;
}
static cairo_bool_t
_cairo_raster_source_pattern_equal (const cairo_raster_source_pattern_t *a,
const cairo_raster_source_pattern_t *b)
{
return a->user_data == b->user_data;
}
cairo_bool_t
_cairo_pattern_equal (const cairo_pattern_t *a, const cairo_pattern_t *b)
{
if (a->status || b->status)
return FALSE;
if (a == b)
return TRUE;
if (a->type != b->type)
return FALSE;
if (a->has_component_alpha != b->has_component_alpha)
return FALSE;
if (a->type != CAIRO_PATTERN_TYPE_SOLID) {
if (memcmp (&a->matrix, &b->matrix, sizeof (cairo_matrix_t)))
return FALSE;
if (a->filter != b->filter)
return FALSE;
if (a->extend != b->extend)
return FALSE;
}
switch (a->type) {
case CAIRO_PATTERN_TYPE_SOLID:
return _cairo_solid_pattern_equal ((cairo_solid_pattern_t *) a,
(cairo_solid_pattern_t *) b);
case CAIRO_PATTERN_TYPE_LINEAR:
return _cairo_linear_pattern_equal ((cairo_linear_pattern_t *) a,
(cairo_linear_pattern_t *) b);
case CAIRO_PATTERN_TYPE_RADIAL:
return _cairo_radial_pattern_equal ((cairo_radial_pattern_t *) a,
(cairo_radial_pattern_t *) b);
case CAIRO_PATTERN_TYPE_MESH:
return _cairo_mesh_pattern_equal ((cairo_mesh_pattern_t *) a,
(cairo_mesh_pattern_t *) b);
case CAIRO_PATTERN_TYPE_SURFACE:
return _cairo_surface_pattern_equal ((cairo_surface_pattern_t *) a,
(cairo_surface_pattern_t *) b);
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
return _cairo_raster_source_pattern_equal ((cairo_raster_source_pattern_t *) a,
(cairo_raster_source_pattern_t *) b);
default:
ASSERT_NOT_REACHED;
return FALSE;
}
}
/**
* cairo_pattern_get_rgba:
* @pattern: a #cairo_pattern_t
* @red: return value for red component of color, or %NULL
* @green: return value for green component of color, or %NULL
* @blue: return value for blue component of color, or %NULL
* @alpha: return value for alpha component of color, or %NULL
*
* Gets the solid color for a solid color pattern.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a solid
* color pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_rgba (cairo_pattern_t *pattern,
double *red, double *green,
double *blue, double *alpha)
{
cairo_solid_pattern_t *solid = (cairo_solid_pattern_t*) pattern;
double r0, g0, b0, a0;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
_cairo_color_get_rgba (&solid->color, &r0, &g0, &b0, &a0);
if (red)
*red = r0;
if (green)
*green = g0;
if (blue)
*blue = b0;
if (alpha)
*alpha = a0;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_surface:
* @pattern: a #cairo_pattern_t
* @surface: return value for surface of pattern, or %NULL
*
* Gets the surface of a surface pattern. The reference returned in
* @surface is owned by the pattern; the caller should call
* cairo_surface_reference() if the surface is to be retained.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a surface
* pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_surface (cairo_pattern_t *pattern,
cairo_surface_t **surface)
{
cairo_surface_pattern_t *spat = (cairo_surface_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_SURFACE)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (surface)
*surface = spat->surface;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_color_stop_rgba:
* @pattern: a #cairo_pattern_t
* @index: index of the stop to return data for
* @offset: return value for the offset of the stop, or %NULL
* @red: return value for red component of color, or %NULL
* @green: return value for green component of color, or %NULL
* @blue: return value for blue component of color, or %NULL
* @alpha: return value for alpha component of color, or %NULL
*
* Gets the color and offset information at the given @index for a
* gradient pattern. Values of @index range from 0 to n-1
* where n is the number returned
* by cairo_pattern_get_color_stop_count().
*
* Return value: %CAIRO_STATUS_SUCCESS, or %CAIRO_STATUS_INVALID_INDEX
* if @index is not valid for the given pattern. If the pattern is
* not a gradient pattern, %CAIRO_STATUS_PATTERN_TYPE_MISMATCH is
* returned.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_color_stop_rgba (cairo_pattern_t *pattern,
int index, double *offset,
double *red, double *green,
double *blue, double *alpha)
{
cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (index < 0 || (unsigned int) index >= gradient->n_stops)
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
if (offset)
*offset = gradient->stops[index].offset;
if (red)
*red = gradient->stops[index].color.red;
if (green)
*green = gradient->stops[index].color.green;
if (blue)
*blue = gradient->stops[index].color.blue;
if (alpha)
*alpha = gradient->stops[index].color.alpha;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_color_stop_count:
* @pattern: a #cairo_pattern_t
* @count: return value for the number of color stops, or %NULL
*
* Gets the number of color stops specified in the given gradient
* pattern.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if @pattern is not a gradient
* pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_color_stop_count (cairo_pattern_t *pattern,
int *count)
{
cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR &&
pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (count)
*count = gradient->n_stops;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_linear_points:
* @pattern: a #cairo_pattern_t
* @x0: return value for the x coordinate of the first point, or %NULL
* @y0: return value for the y coordinate of the first point, or %NULL
* @x1: return value for the x coordinate of the second point, or %NULL
* @y1: return value for the y coordinate of the second point, or %NULL
*
* Gets the gradient endpoints for a linear gradient.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if @pattern is not a linear
* gradient pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_linear_points (cairo_pattern_t *pattern,
double *x0, double *y0,
double *x1, double *y1)
{
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (x0)
*x0 = linear->pd1.x;
if (y0)
*y0 = linear->pd1.y;
if (x1)
*x1 = linear->pd2.x;
if (y1)
*y1 = linear->pd2.y;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_pattern_get_radial_circles:
* @pattern: a #cairo_pattern_t
* @x0: return value for the x coordinate of the center of the first circle, or %NULL
* @y0: return value for the y coordinate of the center of the first circle, or %NULL
* @r0: return value for the radius of the first circle, or %NULL
* @x1: return value for the x coordinate of the center of the second circle, or %NULL
* @y1: return value for the y coordinate of the center of the second circle, or %NULL
* @r1: return value for the radius of the second circle, or %NULL
*
* Gets the gradient endpoint circles for a radial gradient, each
* specified as a center coordinate and a radius.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if @pattern is not a radial
* gradient pattern.
*
* Since: 1.4
**/
cairo_status_t
cairo_pattern_get_radial_circles (cairo_pattern_t *pattern,
double *x0, double *y0, double *r0,
double *x1, double *y1, double *r1)
{
cairo_radial_pattern_t *radial = (cairo_radial_pattern_t*) pattern;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (x0)
*x0 = radial->cd1.center.x;
if (y0)
*y0 = radial->cd1.center.y;
if (r0)
*r0 = radial->cd1.radius;
if (x1)
*x1 = radial->cd2.center.x;
if (y1)
*y1 = radial->cd2.center.y;
if (r1)
*r1 = radial->cd2.radius;
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_mesh_pattern_get_patch_count:
* @pattern: a #cairo_pattern_t
* @count: return value for the number patches, or %NULL
*
* Gets the number of patches specified in the given mesh pattern.
*
* The number only includes patches which have been finished by
* calling cairo_mesh_pattern_end_patch(). For example it will be 0
* during the definition of the first patch.
*
* Return value: %CAIRO_STATUS_SUCCESS, or
* %CAIRO_STATUS_PATTERN_TYPE_MISMATCH if @pattern is not a mesh
* pattern.
*
* Since: 1.12
**/
cairo_status_t
cairo_mesh_pattern_get_patch_count (cairo_pattern_t *pattern,
unsigned int *count)
{
cairo_mesh_pattern_t *mesh = (cairo_mesh_pattern_t *) pattern;
if (unlikely (pattern->status))
return pattern->status;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH))
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (count) {
*count = _cairo_array_num_elements (&mesh->patches);
if (mesh->current_patch)
*count -= 1;
}
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_mesh_pattern_get_patch_count);
/**
* cairo_mesh_pattern_get_path:
* @pattern: a #cairo_pattern_t
* @patch_num: the patch number to return data for
*
* Gets path defining the patch @patch_num for a mesh
* pattern.
*
* @patch_num can range from 0 to n-1 where n is the number returned by
* cairo_mesh_pattern_get_patch_count().
*
* Return value: the path defining the patch, or a path with status
* %CAIRO_STATUS_INVALID_INDEX if @patch_num or @point_num is not
* valid for @pattern. If @pattern is not a mesh pattern, a path with
* status %CAIRO_STATUS_PATTERN_TYPE_MISMATCH is returned.
*
* Since: 1.12
**/
cairo_path_t *
cairo_mesh_pattern_get_path (cairo_pattern_t *pattern,
unsigned int patch_num)
{
cairo_mesh_pattern_t *mesh = (cairo_mesh_pattern_t *) pattern;
const cairo_mesh_patch_t *patch;
cairo_path_t *path;
cairo_path_data_t *data;
unsigned int patch_count;
int l, current_point;
if (unlikely (pattern->status))
return _cairo_path_create_in_error (pattern->status);
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH))
return _cairo_path_create_in_error (_cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH));
patch_count = _cairo_array_num_elements (&mesh->patches);
if (mesh->current_patch)
patch_count--;
if (unlikely (patch_num >= patch_count))
return _cairo_path_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_INDEX));
patch = _cairo_array_index_const (&mesh->patches, patch_num);
path = _cairo_malloc (sizeof (cairo_path_t));
if (path == NULL)
return _cairo_path_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
path->num_data = 18;
path->data = _cairo_malloc_ab (path->num_data,
sizeof (cairo_path_data_t));
if (path->data == NULL) {
free (path);
return _cairo_path_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
data = path->data;
data[0].header.type = CAIRO_PATH_MOVE_TO;
data[0].header.length = 2;
data[1].point.x = patch->points[0][0].x;
data[1].point.y = patch->points[0][0].y;
data += data[0].header.length;
current_point = 0;
for (l = 0; l < 4; l++) {
int i, j, k;
data[0].header.type = CAIRO_PATH_CURVE_TO;
data[0].header.length = 4;
for (k = 1; k < 4; k++) {
current_point = (current_point + 1) % 12;
i = mesh_path_point_i[current_point];
j = mesh_path_point_j[current_point];
data[k].point.x = patch->points[i][j].x;
data[k].point.y = patch->points[i][j].y;
}
data += data[0].header.length;
}
path->status = CAIRO_STATUS_SUCCESS;
return path;
}
slim_hidden_def (cairo_mesh_pattern_get_path);
/**
* cairo_mesh_pattern_get_corner_color_rgba:
* @pattern: a #cairo_pattern_t
* @patch_num: the patch number to return data for
* @corner_num: the corner number to return data for
* @red: return value for red component of color, or %NULL
* @green: return value for green component of color, or %NULL
* @blue: return value for blue component of color, or %NULL
* @alpha: return value for alpha component of color, or %NULL
*
* Gets the color information in corner @corner_num of patch
* @patch_num for a mesh pattern.
*
* @patch_num can range from 0 to n-1 where n is the number returned by
* cairo_mesh_pattern_get_patch_count().
*
* Valid values for @corner_num are from 0 to 3 and identify the
* corners as explained in cairo_pattern_create_mesh().
*
* Return value: %CAIRO_STATUS_SUCCESS, or %CAIRO_STATUS_INVALID_INDEX
* if @patch_num or @corner_num is not valid for @pattern. If
* @pattern is not a mesh pattern, %CAIRO_STATUS_PATTERN_TYPE_MISMATCH
* is returned.
*
* Since: 1.12
**/
cairo_status_t
cairo_mesh_pattern_get_corner_color_rgba (cairo_pattern_t *pattern,
unsigned int patch_num,
unsigned int corner_num,
double *red, double *green,
double *blue, double *alpha)
{
cairo_mesh_pattern_t *mesh = (cairo_mesh_pattern_t *) pattern;
unsigned int patch_count;
const cairo_mesh_patch_t *patch;
if (unlikely (pattern->status))
return pattern->status;
if (unlikely (pattern->type != CAIRO_PATTERN_TYPE_MESH))
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (unlikely (corner_num > 3))
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
patch_count = _cairo_array_num_elements (&mesh->patches);
if (mesh->current_patch)
patch_count--;
if (unlikely (patch_num >= patch_count))
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
patch = _cairo_array_index_const (&mesh->patches, patch_num);
if (red)
*red = patch->colors[corner_num].red;
if (green)
*green = patch->colors[corner_num].green;
if (blue)
*blue = patch->colors[corner_num].blue;
if (alpha)
*alpha = patch->colors[corner_num].alpha;
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_mesh_pattern_get_corner_color_rgba);
/**
* cairo_mesh_pattern_get_control_point:
* @pattern: a #cairo_pattern_t
* @patch_num: the patch number to return data for
* @point_num: the control point number to return data for
* @x: return value for the x coordinate of the control point, or %NULL
* @y: return value for the y coordinate of the control point, or %NULL
*
* Gets the control point @point_num of patch @patch_num for a mesh
* pattern.
*
* @patch_num can range from 0 to n-1 where n is the number returned by
* cairo_mesh_pattern_get_patch_count().
*
* Valid values for @point_num are from 0 to 3 and identify the
* control points as explained in cairo_pattern_create_mesh().
*
* Return value: %CAIRO_STATUS_SUCCESS, or %CAIRO_STATUS_INVALID_INDEX
* if @patch_num or @point_num is not valid for @pattern. If @pattern
* is not a mesh pattern, %CAIRO_STATUS_PATTERN_TYPE_MISMATCH is
* returned.
*
* Since: 1.12
**/
cairo_status_t
cairo_mesh_pattern_get_control_point (cairo_pattern_t *pattern,
unsigned int patch_num,
unsigned int point_num,
double *x, double *y)
{
cairo_mesh_pattern_t *mesh = (cairo_mesh_pattern_t *) pattern;
const cairo_mesh_patch_t *patch;
unsigned int patch_count;
int i, j;
if (pattern->status)
return pattern->status;
if (pattern->type != CAIRO_PATTERN_TYPE_MESH)
return _cairo_error (CAIRO_STATUS_PATTERN_TYPE_MISMATCH);
if (point_num > 3)
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
patch_count = _cairo_array_num_elements (&mesh->patches);
if (mesh->current_patch)
patch_count--;
if (unlikely (patch_num >= patch_count))
return _cairo_error (CAIRO_STATUS_INVALID_INDEX);
patch = _cairo_array_index_const (&mesh->patches, patch_num);
i = mesh_control_point_i[point_num];
j = mesh_control_point_j[point_num];
if (x)
*x = patch->points[i][j].x;
if (y)
*y = patch->points[i][j].y;
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_mesh_pattern_get_control_point);
void
_cairo_pattern_reset_static_data (void)
{
int i;
for (i = 0; i < ARRAY_LENGTH (freed_pattern_pool); i++)
_freed_pool_reset (&freed_pattern_pool[i]);
}
static void
_cairo_debug_print_surface_pattern (FILE *file,
const cairo_surface_pattern_t *pattern)
{
const char *s;
switch (pattern->surface->type) {
case CAIRO_SURFACE_TYPE_IMAGE: s = "image"; break;
case CAIRO_SURFACE_TYPE_PDF: s = "pdf"; break;
case CAIRO_SURFACE_TYPE_PS: s = "ps"; break;
case CAIRO_SURFACE_TYPE_XLIB: s = "xlib"; break;
case CAIRO_SURFACE_TYPE_XCB: s = "xcb"; break;
case CAIRO_SURFACE_TYPE_GLITZ: s = "glitz"; break;
case CAIRO_SURFACE_TYPE_QUARTZ: s = "quartz"; break;
case CAIRO_SURFACE_TYPE_WIN32: s = "win32"; break;
case CAIRO_SURFACE_TYPE_BEOS: s = "beos"; break;
case CAIRO_SURFACE_TYPE_DIRECTFB: s = "directfb"; break;
case CAIRO_SURFACE_TYPE_SVG: s = "svg"; break;
case CAIRO_SURFACE_TYPE_OS2: s = "os2"; break;
case CAIRO_SURFACE_TYPE_WIN32_PRINTING: s = "win32_printing"; break;
case CAIRO_SURFACE_TYPE_QUARTZ_IMAGE: s = "quartz_image"; break;
case CAIRO_SURFACE_TYPE_SCRIPT: s = "script"; break;
case CAIRO_SURFACE_TYPE_QT: s = "qt"; break;
case CAIRO_SURFACE_TYPE_RECORDING: s = "recording"; break;
case CAIRO_SURFACE_TYPE_VG: s = "vg"; break;
case CAIRO_SURFACE_TYPE_GL: s = "gl"; break;
case CAIRO_SURFACE_TYPE_DRM: s = "drm"; break;
case CAIRO_SURFACE_TYPE_TEE: s = "tee"; break;
case CAIRO_SURFACE_TYPE_XML: s = "xml"; break;
case CAIRO_SURFACE_TYPE_SKIA: s = "skia"; break; /* Deprecated */
case CAIRO_SURFACE_TYPE_SUBSURFACE: s = "subsurface"; break;
case CAIRO_SURFACE_TYPE_COGL: s = "cogl"; break;
default: s = "invalid"; ASSERT_NOT_REACHED; break;
}
fprintf (file, " surface type: %s\n", s);
}
static void
_cairo_debug_print_raster_source_pattern (FILE *file,
const cairo_raster_source_pattern_t *raster)
{
fprintf (file, " content: %x, size %dx%d\n", raster->content, raster->extents.width, raster->extents.height);
}
static void
_cairo_debug_print_linear_pattern (FILE *file,
const cairo_linear_pattern_t *pattern)
{
}
static void
_cairo_debug_print_radial_pattern (FILE *file,
const cairo_radial_pattern_t *pattern)
{
}
static void
_cairo_debug_print_mesh_pattern (FILE *file,
const cairo_mesh_pattern_t *pattern)
{
}
void
_cairo_debug_print_pattern (FILE *file, const cairo_pattern_t *pattern)
{
const char *s;
switch (pattern->type) {
case CAIRO_PATTERN_TYPE_SOLID: s = "solid"; break;
case CAIRO_PATTERN_TYPE_SURFACE: s = "surface"; break;
case CAIRO_PATTERN_TYPE_LINEAR: s = "linear"; break;
case CAIRO_PATTERN_TYPE_RADIAL: s = "radial"; break;
case CAIRO_PATTERN_TYPE_MESH: s = "mesh"; break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE: s = "raster"; break;
default: s = "invalid"; ASSERT_NOT_REACHED; break;
}
fprintf (file, "pattern: %s\n", s);
if (pattern->type == CAIRO_PATTERN_TYPE_SOLID)
return;
switch (pattern->extend) {
case CAIRO_EXTEND_NONE: s = "none"; break;
case CAIRO_EXTEND_REPEAT: s = "repeat"; break;
case CAIRO_EXTEND_REFLECT: s = "reflect"; break;
case CAIRO_EXTEND_PAD: s = "pad"; break;
default: s = "invalid"; ASSERT_NOT_REACHED; break;
}
fprintf (file, " extend: %s\n", s);
switch (pattern->filter) {
case CAIRO_FILTER_FAST: s = "fast"; break;
case CAIRO_FILTER_GOOD: s = "good"; break;
case CAIRO_FILTER_BEST: s = "best"; break;
case CAIRO_FILTER_NEAREST: s = "nearest"; break;
case CAIRO_FILTER_BILINEAR: s = "bilinear"; break;
case CAIRO_FILTER_GAUSSIAN: s = "gaussian"; break;
default: s = "invalid"; ASSERT_NOT_REACHED; break;
}
fprintf (file, " filter: %s\n", s);
fprintf (file, " matrix: [%g %g %g %g %g %g]\n",
pattern->matrix.xx, pattern->matrix.yx,
pattern->matrix.xy, pattern->matrix.yy,
pattern->matrix.x0, pattern->matrix.y0);
switch (pattern->type) {
default:
case CAIRO_PATTERN_TYPE_SOLID:
break;
case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
_cairo_debug_print_raster_source_pattern (file, (cairo_raster_source_pattern_t *)pattern);
break;
case CAIRO_PATTERN_TYPE_SURFACE:
_cairo_debug_print_surface_pattern (file, (cairo_surface_pattern_t *)pattern);
break;
case CAIRO_PATTERN_TYPE_LINEAR:
_cairo_debug_print_linear_pattern (file, (cairo_linear_pattern_t *)pattern);
break;
case CAIRO_PATTERN_TYPE_RADIAL:
_cairo_debug_print_radial_pattern (file, (cairo_radial_pattern_t *)pattern);
break;
case CAIRO_PATTERN_TYPE_MESH:
_cairo_debug_print_mesh_pattern (file, (cairo_mesh_pattern_t *)pattern);
break;
}
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-pen.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2008 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-slope-private.h"
static void
_cairo_pen_compute_slopes (cairo_pen_t *pen);
cairo_status_t
_cairo_pen_init (cairo_pen_t *pen,
double radius,
double tolerance,
const cairo_matrix_t *ctm)
{
int i;
int reflect;
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
VG (VALGRIND_MAKE_MEM_UNDEFINED (pen, sizeof (cairo_pen_t)));
pen->radius = radius;
pen->tolerance = tolerance;
reflect = _cairo_matrix_compute_determinant (ctm) < 0.;
pen->num_vertices = _cairo_pen_vertices_needed (tolerance,
radius,
ctm);
if (pen->num_vertices > ARRAY_LENGTH (pen->vertices_embedded)) {
pen->vertices = _cairo_malloc_ab (pen->num_vertices,
sizeof (cairo_pen_vertex_t));
if (unlikely (pen->vertices == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
} else {
pen->vertices = pen->vertices_embedded;
}
/*
* Compute pen coordinates. To generate the right ellipse, compute points around
* a circle in user space and transform them to device space. To get a consistent
* orientation in device space, flip the pen if the transformation matrix
* is reflecting
*/
for (i=0; i < pen->num_vertices; i++) {
cairo_pen_vertex_t *v = &pen->vertices[i];
double theta = 2 * M_PI * i / (double) pen->num_vertices, dx, dy;
if (reflect)
theta = -theta;
dx = radius * cos (theta);
dy = radius * sin (theta);
cairo_matrix_transform_distance (ctm, &dx, &dy);
v->point.x = _cairo_fixed_from_double (dx);
v->point.y = _cairo_fixed_from_double (dy);
}
_cairo_pen_compute_slopes (pen);
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_pen_fini (cairo_pen_t *pen)
{
if (pen->vertices != pen->vertices_embedded)
free (pen->vertices);
VG (VALGRIND_MAKE_MEM_UNDEFINED (pen, sizeof (cairo_pen_t)));
}
cairo_status_t
_cairo_pen_init_copy (cairo_pen_t *pen, const cairo_pen_t *other)
{
VG (VALGRIND_MAKE_MEM_UNDEFINED (pen, sizeof (cairo_pen_t)));
*pen = *other;
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
pen->vertices = pen->vertices_embedded;
if (pen->num_vertices) {
if (pen->num_vertices > ARRAY_LENGTH (pen->vertices_embedded)) {
pen->vertices = _cairo_malloc_ab (pen->num_vertices,
sizeof (cairo_pen_vertex_t));
if (unlikely (pen->vertices == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
memcpy (pen->vertices, other->vertices,
pen->num_vertices * sizeof (cairo_pen_vertex_t));
}
return CAIRO_STATUS_SUCCESS;
}
cairo_status_t
_cairo_pen_add_points (cairo_pen_t *pen, cairo_point_t *point, int num_points)
{
cairo_status_t status;
int num_vertices;
int i;
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
num_vertices = pen->num_vertices + num_points;
if (num_vertices > ARRAY_LENGTH (pen->vertices_embedded) ||
pen->vertices != pen->vertices_embedded)
{
cairo_pen_vertex_t *vertices;
if (pen->vertices == pen->vertices_embedded) {
vertices = _cairo_malloc_ab (num_vertices,
sizeof (cairo_pen_vertex_t));
if (unlikely (vertices == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
memcpy (vertices, pen->vertices,
pen->num_vertices * sizeof (cairo_pen_vertex_t));
} else {
vertices = _cairo_realloc_ab (pen->vertices,
num_vertices,
sizeof (cairo_pen_vertex_t));
if (unlikely (vertices == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
pen->vertices = vertices;
}
pen->num_vertices = num_vertices;
/* initialize new vertices */
for (i=0; i < num_points; i++)
pen->vertices[pen->num_vertices-num_points+i].point = point[i];
status = _cairo_hull_compute (pen->vertices, &pen->num_vertices);
if (unlikely (status))
return status;
_cairo_pen_compute_slopes (pen);
return CAIRO_STATUS_SUCCESS;
}
/*
The circular pen in user space is transformed into an ellipse in
device space.
We construct the pen by computing points along the circumference
using equally spaced angles.
We show that this approximation to the ellipse has maximum error at the
major axis of the ellipse.
Set
M = major axis length
m = minor axis length
Align 'M' along the X axis and 'm' along the Y axis and draw
an ellipse parameterized by angle 't':
x = M cos t y = m sin t
Perturb t by ± d and compute two new points (x+,y+), (x-,y-).
The distance from the average of these two points to (x,y) represents
the maximum error in approximating the ellipse with a polygon formed
from vertices 2∆ radians apart.
x+ = M cos (t+∆) y+ = m sin (t+∆)
x- = M cos (t-∆) y- = m sin (t-∆)
Now compute the approximation error, E:
Ex = (x - (x+ + x-) / 2)
Ex = (M cos(t) - (Mcos(t+∆) + Mcos(t-∆))/2)
= M (cos(t) - (cos(t)cos(∆) + sin(t)sin(∆) +
cos(t)cos(∆) - sin(t)sin(∆))/2)
= M(cos(t) - cos(t)cos(∆))
= M cos(t) (1 - cos(∆))
Ey = y - (y+ - y-) / 2
= m sin (t) - (m sin(t+∆) + m sin(t-∆)) / 2
= m (sin(t) - (sin(t)cos(∆) + cos(t)sin(∆) +
sin(t)cos(∆) - cos(t)sin(∆))/2)
= m (sin(t) - sin(t)cos(∆))
= m sin(t) (1 - cos(∆))
E² = Ex² + Ey²
= (M cos(t) (1 - cos (∆)))² + (m sin(t) (1-cos(∆)))²
= (1 - cos(∆))² (M² cos²(t) + m² sin²(t))
= (1 - cos(∆))² ((m² + M² - m²) cos² (t) + m² sin²(t))
= (1 - cos(∆))² (M² - m²) cos² (t) + (1 - cos(∆))² m²
Find the extremum by differentiation wrt t and setting that to zero
∂(E²)/∂(t) = (1-cos(∆))² (M² - m²) (-2 cos(t) sin(t))
0 = 2 cos (t) sin (t)
0 = sin (2t)
t = nπ
Which is to say that the maximum and minimum errors occur on the
axes of the ellipse at 0 and π radians:
E²(0) = (1-cos(∆))² (M² - m²) + (1-cos(∆))² m²
= (1-cos(∆))² M²
E²(π) = (1-cos(∆))² m²
maximum error = M (1-cos(∆))
minimum error = m (1-cos(∆))
We must make maximum error ≤ tolerance, so compute the ∆ needed:
tolerance = M (1-cos(∆))
tolerance / M = 1 - cos (∆)
cos(∆) = 1 - tolerance/M
∆ = acos (1 - tolerance / M);
Remembering that ∆ is half of our angle between vertices,
the number of vertices is then
vertices = ceil(2π/2∆).
= ceil(π/∆).
Note that this also equation works for M == m (a circle) as it
doesn't matter where on the circle the error is computed.
*/
int
_cairo_pen_vertices_needed (double tolerance,
double radius,
const cairo_matrix_t *matrix)
{
/*
* the pen is a circle that gets transformed to an ellipse by matrix.
* compute major axis length for a pen with the specified radius.
* we don't need the minor axis length.
*/
double major_axis = _cairo_matrix_transformed_circle_major_axis (matrix,
radius);
int num_vertices;
if (tolerance >= 4*major_axis) { /* XXX relaxed from 2*major for inkscape */
num_vertices = 1;
} else if (tolerance >= major_axis) {
num_vertices = 4;
} else {
num_vertices = ceil (2*M_PI / acos (1 - tolerance / major_axis));
/* number of vertices must be even */
if (num_vertices % 2)
num_vertices++;
/* And we must always have at least 4 vertices. */
if (num_vertices < 4)
num_vertices = 4;
}
return num_vertices;
}
static void
_cairo_pen_compute_slopes (cairo_pen_t *pen)
{
int i, i_prev;
cairo_pen_vertex_t *prev, *v, *next;
for (i=0, i_prev = pen->num_vertices - 1;
i < pen->num_vertices;
i_prev = i++) {
prev = &pen->vertices[i_prev];
v = &pen->vertices[i];
next = &pen->vertices[(i + 1) % pen->num_vertices];
_cairo_slope_init (&v->slope_cw, &prev->point, &v->point);
_cairo_slope_init (&v->slope_ccw, &v->point, &next->point);
}
}
/*
* Find active pen vertex for clockwise edge of stroke at the given slope.
*
* The strictness of the inequalities here is delicate. The issue is
* that the slope_ccw member of one pen vertex will be equivalent to
* the slope_cw member of the next pen vertex in a counterclockwise
* order. However, for this function, we care strongly about which
* vertex is returned.
*
* [I think the "care strongly" above has to do with ensuring that the
* pen's "extra points" from the spline's initial and final slopes are
* properly found when beginning the spline stroking.]
*/
int
_cairo_pen_find_active_cw_vertex_index (const cairo_pen_t *pen,
const cairo_slope_t *slope)
{
int i;
for (i=0; i < pen->num_vertices; i++) {
if ((_cairo_slope_compare (slope, &pen->vertices[i].slope_ccw) < 0) &&
(_cairo_slope_compare (slope, &pen->vertices[i].slope_cw) >= 0))
break;
}
/* If the desired slope cannot be found between any of the pen
* vertices, then we must have a degenerate pen, (such as a pen
* that's been transformed to a line). In that case, we consider
* the first pen vertex as the appropriate clockwise vertex.
*/
if (i == pen->num_vertices)
i = 0;
return i;
}
/* Find active pen vertex for counterclockwise edge of stroke at the given slope.
*
* Note: See the comments for _cairo_pen_find_active_cw_vertex_index
* for some details about the strictness of the inequalities here.
*/
int
_cairo_pen_find_active_ccw_vertex_index (const cairo_pen_t *pen,
const cairo_slope_t *slope)
{
cairo_slope_t slope_reverse;
int i;
slope_reverse = *slope;
slope_reverse.dx = -slope_reverse.dx;
slope_reverse.dy = -slope_reverse.dy;
for (i=pen->num_vertices-1; i >= 0; i--) {
if ((_cairo_slope_compare (&pen->vertices[i].slope_ccw, &slope_reverse) >= 0) &&
(_cairo_slope_compare (&pen->vertices[i].slope_cw, &slope_reverse) < 0))
break;
}
/* If the desired slope cannot be found between any of the pen
* vertices, then we must have a degenerate pen, (such as a pen
* that's been transformed to a line). In that case, we consider
* the last pen vertex as the appropriate counterclockwise vertex.
*/
if (i < 0)
i = pen->num_vertices - 1;
return i;
}
void
_cairo_pen_find_active_cw_vertices (const cairo_pen_t *pen,
const cairo_slope_t *in,
const cairo_slope_t *out,
int *start, int *stop)
{
int lo = 0, hi = pen->num_vertices;
int i;
i = (lo + hi) >> 1;
do {
if (_cairo_slope_compare (&pen->vertices[i].slope_cw, in) < 0)
lo = i;
else
hi = i;
i = (lo + hi) >> 1;
} while (hi - lo > 1);
if (_cairo_slope_compare (&pen->vertices[i].slope_cw, in) < 0)
if (++i == pen->num_vertices)
i = 0;
*start = i;
if (_cairo_slope_compare (out, &pen->vertices[i].slope_ccw) >= 0) {
lo = i;
hi = i + pen->num_vertices;
i = (lo + hi) >> 1;
do {
int j = i;
if (j >= pen->num_vertices)
j -= pen->num_vertices;
if (_cairo_slope_compare (&pen->vertices[j].slope_cw, out) > 0)
hi = i;
else
lo = i;
i = (lo + hi) >> 1;
} while (hi - lo > 1);
if (i >= pen->num_vertices)
i -= pen->num_vertices;
}
*stop = i;
}
void
_cairo_pen_find_active_ccw_vertices (const cairo_pen_t *pen,
const cairo_slope_t *in,
const cairo_slope_t *out,
int *start, int *stop)
{
int lo = 0, hi = pen->num_vertices;
int i;
i = (lo + hi) >> 1;
do {
if (_cairo_slope_compare (in, &pen->vertices[i].slope_ccw) < 0)
lo = i;
else
hi = i;
i = (lo + hi) >> 1;
} while (hi - lo > 1);
if (_cairo_slope_compare (in, &pen->vertices[i].slope_ccw) < 0)
if (++i == pen->num_vertices)
i = 0;
*start = i;
if (_cairo_slope_compare (&pen->vertices[i].slope_cw, out) <= 0) {
lo = i;
hi = i + pen->num_vertices;
i = (lo + hi) >> 1;
do {
int j = i;
if (j >= pen->num_vertices)
j -= pen->num_vertices;
if (_cairo_slope_compare (out, &pen->vertices[j].slope_ccw) > 0)
hi = i;
else
lo = i;
i = (lo + hi) >> 1;
} while (hi - lo > 1);
if (i >= pen->num_vertices)
i -= pen->num_vertices;
}
*stop = i;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-pixman-private.h | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright ©2013 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#ifndef CAIRO_PIXMAN_PRIVATE_H
#define CAIRO_PIXMAN_PRIVATE_H
#include "cairo-pixman-private.h" /* keep make check happy */
#include <pixman/pixman.h>
#if PIXMAN_VERSION < PIXMAN_VERSION_ENCODE(0,22,0)
#define pixman_image_composite32 pixman_image_composite
#define pixman_image_get_component_alpha(i) 0
#define pixman_image_set_component_alpha(i, x) do { } while (0)
#endif
#endif
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-polygon-intersect.c | /*
* Copyright © 2004 Carl Worth
* Copyright © 2006 Red Hat, Inc.
* Copyright © 2008 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Carl Worth
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
/* Provide definitions for standalone compilation */
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-freelist-private.h"
#include "cairo-combsort-inline.h"
typedef struct _cairo_bo_intersect_ordinate {
int32_t ordinate;
enum { EXCESS = -1, EXACT = 0, DEFAULT = 1 } approx;
} cairo_bo_intersect_ordinate_t;
typedef struct _cairo_bo_intersect_point {
cairo_bo_intersect_ordinate_t x;
cairo_bo_intersect_ordinate_t y;
} cairo_bo_intersect_point_t;
typedef struct _cairo_bo_edge cairo_bo_edge_t;
typedef struct _cairo_bo_deferred {
cairo_bo_edge_t *other;
int32_t top;
} cairo_bo_deferred_t;
struct _cairo_bo_edge {
int a_or_b;
cairo_edge_t edge;
cairo_bo_edge_t *prev;
cairo_bo_edge_t *next;
cairo_bo_deferred_t deferred;
};
/* the parent is always given by index/2 */
#define PQ_PARENT_INDEX(i) ((i) >> 1)
#define PQ_FIRST_ENTRY 1
/* left and right children are index * 2 and (index * 2) +1 respectively */
#define PQ_LEFT_CHILD_INDEX(i) ((i) << 1)
typedef enum {
CAIRO_BO_EVENT_TYPE_STOP = -1,
CAIRO_BO_EVENT_TYPE_INTERSECTION,
CAIRO_BO_EVENT_TYPE_START
} cairo_bo_event_type_t;
typedef struct _cairo_bo_event {
cairo_bo_event_type_t type;
cairo_bo_intersect_point_t point;
} cairo_bo_event_t;
typedef struct _cairo_bo_start_event {
cairo_bo_event_type_t type;
cairo_bo_intersect_point_t point;
cairo_bo_edge_t edge;
} cairo_bo_start_event_t;
typedef struct _cairo_bo_queue_event {
cairo_bo_event_type_t type;
cairo_bo_intersect_point_t point;
cairo_bo_edge_t *e1;
cairo_bo_edge_t *e2;
} cairo_bo_queue_event_t;
typedef struct _pqueue {
int size, max_size;
cairo_bo_event_t **elements;
cairo_bo_event_t *elements_embedded[1024];
} pqueue_t;
typedef struct _cairo_bo_event_queue {
cairo_freepool_t pool;
pqueue_t pqueue;
cairo_bo_event_t **start_events;
} cairo_bo_event_queue_t;
typedef struct _cairo_bo_sweep_line {
cairo_bo_edge_t *head;
int32_t current_y;
cairo_bo_edge_t *current_edge;
} cairo_bo_sweep_line_t;
static cairo_fixed_t
_line_compute_intersection_x_for_y (const cairo_line_t *line,
cairo_fixed_t y)
{
cairo_fixed_t x, dy;
if (y == line->p1.y)
return line->p1.x;
if (y == line->p2.y)
return line->p2.x;
x = line->p1.x;
dy = line->p2.y - line->p1.y;
if (dy != 0) {
x += _cairo_fixed_mul_div_floor (y - line->p1.y,
line->p2.x - line->p1.x,
dy);
}
return x;
}
static inline int
_cairo_bo_point32_compare (cairo_bo_intersect_point_t const *a,
cairo_bo_intersect_point_t const *b)
{
int cmp;
cmp = a->y.ordinate - b->y.ordinate;
if (cmp)
return cmp;
cmp = a->y.approx - b->y.approx;
if (cmp)
return cmp;
return a->x.ordinate - b->x.ordinate;
}
/* Compare the slope of a to the slope of b, returning 1, 0, -1 if the
* slope a is respectively greater than, equal to, or less than the
* slope of b.
*
* For each edge, consider the direction vector formed from:
*
* top -> bottom
*
* which is:
*
* (dx, dy) = (line.p2.x - line.p1.x, line.p2.y - line.p1.y)
*
* We then define the slope of each edge as dx/dy, (which is the
* inverse of the slope typically used in math instruction). We never
* compute a slope directly as the value approaches infinity, but we
* can derive a slope comparison without division as follows, (where
* the ? represents our compare operator).
*
* 1. slope(a) ? slope(b)
* 2. adx/ady ? bdx/bdy
* 3. (adx * bdy) ? (bdx * ady)
*
* Note that from step 2 to step 3 there is no change needed in the
* sign of the result since both ady and bdy are guaranteed to be
* greater than or equal to 0.
*
* When using this slope comparison to sort edges, some care is needed
* when interpreting the results. Since the slope compare operates on
* distance vectors from top to bottom it gives a correct left to
* right sort for edges that have a common top point, (such as two
* edges with start events at the same location). On the other hand,
* the sense of the result will be exactly reversed for two edges that
* have a common stop point.
*/
static inline int
_slope_compare (const cairo_bo_edge_t *a,
const cairo_bo_edge_t *b)
{
/* XXX: We're assuming here that dx and dy will still fit in 32
* bits. That's not true in general as there could be overflow. We
* should prevent that before the tessellation algorithm
* begins.
*/
int32_t adx = a->edge.line.p2.x - a->edge.line.p1.x;
int32_t bdx = b->edge.line.p2.x - b->edge.line.p1.x;
/* Since the dy's are all positive by construction we can fast
* path several common cases.
*/
/* First check for vertical lines. */
if (adx == 0)
return -bdx;
if (bdx == 0)
return adx;
/* Then where the two edges point in different directions wrt x. */
if ((adx ^ bdx) < 0)
return adx;
/* Finally we actually need to do the general comparison. */
{
int32_t ady = a->edge.line.p2.y - a->edge.line.p1.y;
int32_t bdy = b->edge.line.p2.y - b->edge.line.p1.y;
cairo_int64_t adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
cairo_int64_t bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
return _cairo_int64_cmp (adx_bdy, bdx_ady);
}
}
/*
* We need to compare the x-coordinates of a pair of lines for a particular y,
* without loss of precision.
*
* The x-coordinate along an edge for a given y is:
* X = A_x + (Y - A_y) * A_dx / A_dy
*
* So the inequality we wish to test is:
* A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy,
* where ∘ is our inequality operator.
*
* By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are
* all positive, so we can rearrange it thus without causing a sign change:
* A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy
* - (Y - A_y) * A_dx * B_dy
*
* Given the assumption that all the deltas fit within 32 bits, we can compute
* this comparison directly using 128 bit arithmetic. For certain, but common,
* input we can reduce this down to a single 32 bit compare by inspecting the
* deltas.
*
* (And put the burden of the work on developing fast 128 bit ops, which are
* required throughout the tessellator.)
*
* See the similar discussion for _slope_compare().
*/
static int
edges_compare_x_for_y_general (const cairo_bo_edge_t *a,
const cairo_bo_edge_t *b,
int32_t y)
{
/* XXX: We're assuming here that dx and dy will still fit in 32
* bits. That's not true in general as there could be overflow. We
* should prevent that before the tessellation algorithm
* begins.
*/
int32_t dx;
int32_t adx, ady;
int32_t bdx, bdy;
enum {
HAVE_NONE = 0x0,
HAVE_DX = 0x1,
HAVE_ADX = 0x2,
HAVE_DX_ADX = HAVE_DX | HAVE_ADX,
HAVE_BDX = 0x4,
HAVE_DX_BDX = HAVE_DX | HAVE_BDX,
HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX,
HAVE_ALL = HAVE_DX | HAVE_ADX | HAVE_BDX
} have_dx_adx_bdx = HAVE_ALL;
/* don't bother solving for abscissa if the edges' bounding boxes
* can be used to order them. */
{
int32_t amin, amax;
int32_t bmin, bmax;
if (a->edge.line.p1.x < a->edge.line.p2.x) {
amin = a->edge.line.p1.x;
amax = a->edge.line.p2.x;
} else {
amin = a->edge.line.p2.x;
amax = a->edge.line.p1.x;
}
if (b->edge.line.p1.x < b->edge.line.p2.x) {
bmin = b->edge.line.p1.x;
bmax = b->edge.line.p2.x;
} else {
bmin = b->edge.line.p2.x;
bmax = b->edge.line.p1.x;
}
if (amax < bmin) return -1;
if (amin > bmax) return +1;
}
ady = a->edge.line.p2.y - a->edge.line.p1.y;
adx = a->edge.line.p2.x - a->edge.line.p1.x;
if (adx == 0)
have_dx_adx_bdx &= ~HAVE_ADX;
bdy = b->edge.line.p2.y - b->edge.line.p1.y;
bdx = b->edge.line.p2.x - b->edge.line.p1.x;
if (bdx == 0)
have_dx_adx_bdx &= ~HAVE_BDX;
dx = a->edge.line.p1.x - b->edge.line.p1.x;
if (dx == 0)
have_dx_adx_bdx &= ~HAVE_DX;
#define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx)
#define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->edge.line.p1.y)
#define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->edge.line.p1.y)
switch (have_dx_adx_bdx) {
default:
case HAVE_NONE:
return 0;
case HAVE_DX:
/* A_dy * B_dy * (A_x - B_x) ∘ 0 */
return dx; /* ady * bdy is positive definite */
case HAVE_ADX:
/* 0 ∘ - (Y - A_y) * A_dx * B_dy */
return adx; /* bdy * (y - a->top.y) is positive definite */
case HAVE_BDX:
/* 0 ∘ (Y - B_y) * B_dx * A_dy */
return -bdx; /* ady * (y - b->top.y) is positive definite */
case HAVE_ADX_BDX:
/* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
if ((adx ^ bdx) < 0) {
return adx;
} else if (a->edge.line.p1.y == b->edge.line.p1.y) { /* common origin */
cairo_int64_t adx_bdy, bdx_ady;
/* ∴ A_dx * B_dy ∘ B_dx * A_dy */
adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
return _cairo_int64_cmp (adx_bdy, bdx_ady);
} else
return _cairo_int128_cmp (A, B);
case HAVE_DX_ADX:
/* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
if ((-adx ^ dx) < 0) {
return dx;
} else {
cairo_int64_t ady_dx, dy_adx;
ady_dx = _cairo_int32x32_64_mul (ady, dx);
dy_adx = _cairo_int32x32_64_mul (a->edge.line.p1.y - y, adx);
return _cairo_int64_cmp (ady_dx, dy_adx);
}
case HAVE_DX_BDX:
/* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
if ((bdx ^ dx) < 0) {
return dx;
} else {
cairo_int64_t bdy_dx, dy_bdx;
bdy_dx = _cairo_int32x32_64_mul (bdy, dx);
dy_bdx = _cairo_int32x32_64_mul (y - b->edge.line.p1.y, bdx);
return _cairo_int64_cmp (bdy_dx, dy_bdx);
}
case HAVE_ALL:
/* XXX try comparing (a->edge.line.p2.x - b->edge.line.p2.x) et al */
return _cairo_int128_cmp (L, _cairo_int128_sub (B, A));
}
#undef B
#undef A
#undef L
}
/*
* We need to compare the x-coordinate of a line for a particular y wrt to a
* given x, without loss of precision.
*
* The x-coordinate along an edge for a given y is:
* X = A_x + (Y - A_y) * A_dx / A_dy
*
* So the inequality we wish to test is:
* A_x + (Y - A_y) * A_dx / A_dy ∘ X
* where ∘ is our inequality operator.
*
* By construction, we know that A_dy (and (Y - A_y)) are
* all positive, so we can rearrange it thus without causing a sign change:
* (Y - A_y) * A_dx ∘ (X - A_x) * A_dy
*
* Given the assumption that all the deltas fit within 32 bits, we can compute
* this comparison directly using 64 bit arithmetic.
*
* See the similar discussion for _slope_compare() and
* edges_compare_x_for_y_general().
*/
static int
edge_compare_for_y_against_x (const cairo_bo_edge_t *a,
int32_t y,
int32_t x)
{
int32_t adx, ady;
int32_t dx, dy;
cairo_int64_t L, R;
if (x < a->edge.line.p1.x && x < a->edge.line.p2.x)
return 1;
if (x > a->edge.line.p1.x && x > a->edge.line.p2.x)
return -1;
adx = a->edge.line.p2.x - a->edge.line.p1.x;
dx = x - a->edge.line.p1.x;
if (adx == 0)
return -dx;
if (dx == 0 || (adx ^ dx) < 0)
return adx;
dy = y - a->edge.line.p1.y;
ady = a->edge.line.p2.y - a->edge.line.p1.y;
L = _cairo_int32x32_64_mul (dy, adx);
R = _cairo_int32x32_64_mul (dx, ady);
return _cairo_int64_cmp (L, R);
}
static int
edges_compare_x_for_y (const cairo_bo_edge_t *a,
const cairo_bo_edge_t *b,
int32_t y)
{
/* If the sweep-line is currently on an end-point of a line,
* then we know its precise x value (and considering that we often need to
* compare events at end-points, this happens frequently enough to warrant
* special casing).
*/
enum {
HAVE_NEITHER = 0x0,
HAVE_AX = 0x1,
HAVE_BX = 0x2,
HAVE_BOTH = HAVE_AX | HAVE_BX
} have_ax_bx = HAVE_BOTH;
int32_t ax = 0, bx = 0;
if (y == a->edge.line.p1.y)
ax = a->edge.line.p1.x;
else if (y == a->edge.line.p2.y)
ax = a->edge.line.p2.x;
else
have_ax_bx &= ~HAVE_AX;
if (y == b->edge.line.p1.y)
bx = b->edge.line.p1.x;
else if (y == b->edge.line.p2.y)
bx = b->edge.line.p2.x;
else
have_ax_bx &= ~HAVE_BX;
switch (have_ax_bx) {
default:
case HAVE_NEITHER:
return edges_compare_x_for_y_general (a, b, y);
case HAVE_AX:
return -edge_compare_for_y_against_x (b, y, ax);
case HAVE_BX:
return edge_compare_for_y_against_x (a, y, bx);
case HAVE_BOTH:
return ax - bx;
}
}
static inline int
_line_equal (const cairo_line_t *a, const cairo_line_t *b)
{
return a->p1.x == b->p1.x && a->p1.y == b->p1.y &&
a->p2.x == b->p2.x && a->p2.y == b->p2.y;
}
static int
_cairo_bo_sweep_line_compare_edges (cairo_bo_sweep_line_t *sweep_line,
const cairo_bo_edge_t *a,
const cairo_bo_edge_t *b)
{
int cmp;
/* compare the edges if not identical */
if (! _line_equal (&a->edge.line, &b->edge.line)) {
cmp = edges_compare_x_for_y (a, b, sweep_line->current_y);
if (cmp)
return cmp;
/* The two edges intersect exactly at y, so fall back on slope
* comparison. We know that this compare_edges function will be
* called only when starting a new edge, (not when stopping an
* edge), so we don't have to worry about conditionally inverting
* the sense of _slope_compare. */
cmp = _slope_compare (a, b);
if (cmp)
return cmp;
}
/* We've got two collinear edges now. */
return b->edge.bottom - a->edge.bottom;
}
static inline cairo_int64_t
det32_64 (int32_t a, int32_t b,
int32_t c, int32_t d)
{
/* det = a * d - b * c */
return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d),
_cairo_int32x32_64_mul (b, c));
}
static inline cairo_int128_t
det64x32_128 (cairo_int64_t a, int32_t b,
cairo_int64_t c, int32_t d)
{
/* det = a * d - b * c */
return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d),
_cairo_int64x32_128_mul (c, b));
}
static inline cairo_bo_intersect_ordinate_t
round_to_nearest (cairo_quorem64_t d,
cairo_int64_t den)
{
cairo_bo_intersect_ordinate_t ordinate;
int32_t quo = d.quo;
cairo_int64_t drem_2 = _cairo_int64_mul (d.rem, _cairo_int32_to_int64 (2));
/* assert (! _cairo_int64_negative (den));*/
if (_cairo_int64_lt (drem_2, _cairo_int64_negate (den))) {
quo -= 1;
drem_2 = _cairo_int64_negate (drem_2);
} else if (_cairo_int64_le (den, drem_2)) {
quo += 1;
drem_2 = _cairo_int64_negate (drem_2);
}
ordinate.ordinate = quo;
ordinate.approx = _cairo_int64_is_zero (drem_2) ? EXACT : _cairo_int64_negative (drem_2) ? EXCESS : DEFAULT;
return ordinate;
}
/* Compute the intersection of two lines as defined by two edges. The
* result is provided as a coordinate pair of 128-bit integers.
*
* Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or
* %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel.
*/
static cairo_bool_t
intersect_lines (cairo_bo_edge_t *a,
cairo_bo_edge_t *b,
cairo_bo_intersect_point_t *intersection)
{
cairo_int64_t a_det, b_det;
/* XXX: We're assuming here that dx and dy will still fit in 32
* bits. That's not true in general as there could be overflow. We
* should prevent that before the tessellation algorithm begins.
* What we're doing to mitigate this is to perform clamping in
* cairo_bo_tessellate_polygon().
*/
int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x;
int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y;
int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x;
int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y;
cairo_int64_t den_det;
cairo_int64_t R;
cairo_quorem64_t qr;
den_det = det32_64 (dx1, dy1, dx2, dy2);
/* Q: Can we determine that the lines do not intersect (within range)
* much more cheaply than computing the intersection point i.e. by
* avoiding the division?
*
* X = ax + t * adx = bx + s * bdx;
* Y = ay + t * ady = by + s * bdy;
* ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx)
* => t * L = R
*
* Therefore we can reject any intersection (under the criteria for
* valid intersection events) if:
* L^R < 0 => t < 0, or
* L<R => t > 1
*
* (where top/bottom must at least extend to the line endpoints).
*
* A similar substitution can be performed for s, yielding:
* s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by)
*/
R = det32_64 (dx2, dy2,
b->edge.line.p1.x - a->edge.line.p1.x,
b->edge.line.p1.y - a->edge.line.p1.y);
if (_cairo_int64_le (den_det, R))
return FALSE;
R = det32_64 (dy1, dx1,
a->edge.line.p1.y - b->edge.line.p1.y,
a->edge.line.p1.x - b->edge.line.p1.x);
if (_cairo_int64_le (den_det, R))
return FALSE;
/* We now know that the two lines should intersect within range. */
a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y,
a->edge.line.p2.x, a->edge.line.p2.y);
b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y,
b->edge.line.p2.x, b->edge.line.p2.y);
/* x = det (a_det, dx1, b_det, dx2) / den_det */
qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1,
b_det, dx2),
den_det);
if (_cairo_int64_eq (qr.rem, den_det))
return FALSE;
intersection->x = round_to_nearest (qr, den_det);
/* y = det (a_det, dy1, b_det, dy2) / den_det */
qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1,
b_det, dy2),
den_det);
if (_cairo_int64_eq (qr.rem, den_det))
return FALSE;
intersection->y = round_to_nearest (qr, den_det);
return TRUE;
}
static int
_cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t a,
int32_t b)
{
/* First compare the quotient */
if (a.ordinate > b)
return +1;
if (a.ordinate < b)
return -1;
return a.approx; /* == EXCESS ? -1 : a.approx == EXACT ? 0 : 1;*/
}
/* Does the given edge contain the given point. The point must already
* be known to be contained within the line determined by the edge,
* (most likely the point results from an intersection of this edge
* with another).
*
* If we had exact arithmetic, then this function would simply be a
* matter of examining whether the y value of the point lies within
* the range of y values of the edge. But since intersection points
* are not exact due to being rounded to the nearest integer within
* the available precision, we must also examine the x value of the
* point.
*
* The definition of "contains" here is that the given intersection
* point will be seen by the sweep line after the start event for the
* given edge and before the stop event for the edge. See the comments
* in the implementation for more details.
*/
static cairo_bool_t
_cairo_bo_edge_contains_intersect_point (cairo_bo_edge_t *edge,
cairo_bo_intersect_point_t *point)
{
return _cairo_bo_intersect_ordinate_32_compare (point->y,
edge->edge.bottom) < 0;
}
/* Compute the intersection of two edges. The result is provided as a
* coordinate pair of 128-bit integers.
*
* Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection
* that is within both edges, %CAIRO_BO_STATUS_NO_INTERSECTION if the
* intersection of the lines defined by the edges occurs outside of
* one or both edges, and %CAIRO_BO_STATUS_PARALLEL if the two edges
* are exactly parallel.
*
* Note that when determining if a candidate intersection is "inside"
* an edge, we consider both the infinitesimal shortening and the
* infinitesimal tilt rules described by John Hobby. Specifically, if
* the intersection is exactly the same as an edge point, it is
* effectively outside (no intersection is returned). Also, if the
* intersection point has the same
*/
static cairo_bool_t
_cairo_bo_edge_intersect (cairo_bo_edge_t *a,
cairo_bo_edge_t *b,
cairo_bo_intersect_point_t *intersection)
{
if (! intersect_lines (a, b, intersection))
return FALSE;
if (! _cairo_bo_edge_contains_intersect_point (a, intersection))
return FALSE;
if (! _cairo_bo_edge_contains_intersect_point (b, intersection))
return FALSE;
return TRUE;
}
static inline int
cairo_bo_event_compare (const cairo_bo_event_t *a,
const cairo_bo_event_t *b)
{
int cmp;
cmp = _cairo_bo_point32_compare (&a->point, &b->point);
if (cmp)
return cmp;
cmp = a->type - b->type;
if (cmp)
return cmp;
return a < b ? -1 : a == b ? 0 : 1;
}
static inline void
_pqueue_init (pqueue_t *pq)
{
pq->max_size = ARRAY_LENGTH (pq->elements_embedded);
pq->size = 0;
pq->elements = pq->elements_embedded;
}
static inline void
_pqueue_fini (pqueue_t *pq)
{
if (pq->elements != pq->elements_embedded)
free (pq->elements);
}
static cairo_status_t
_pqueue_grow (pqueue_t *pq)
{
cairo_bo_event_t **new_elements;
pq->max_size *= 2;
if (pq->elements == pq->elements_embedded) {
new_elements = _cairo_malloc_ab (pq->max_size,
sizeof (cairo_bo_event_t *));
if (unlikely (new_elements == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
memcpy (new_elements, pq->elements_embedded,
sizeof (pq->elements_embedded));
} else {
new_elements = _cairo_realloc_ab (pq->elements,
pq->max_size,
sizeof (cairo_bo_event_t *));
if (unlikely (new_elements == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
pq->elements = new_elements;
return CAIRO_STATUS_SUCCESS;
}
static inline cairo_status_t
_pqueue_push (pqueue_t *pq, cairo_bo_event_t *event)
{
cairo_bo_event_t **elements;
int i, parent;
if (unlikely (pq->size + 1 == pq->max_size)) {
cairo_status_t status;
status = _pqueue_grow (pq);
if (unlikely (status))
return status;
}
elements = pq->elements;
for (i = ++pq->size;
i != PQ_FIRST_ENTRY &&
cairo_bo_event_compare (event,
elements[parent = PQ_PARENT_INDEX (i)]) < 0;
i = parent)
{
elements[i] = elements[parent];
}
elements[i] = event;
return CAIRO_STATUS_SUCCESS;
}
static inline void
_pqueue_pop (pqueue_t *pq)
{
cairo_bo_event_t **elements = pq->elements;
cairo_bo_event_t *tail;
int child, i;
tail = elements[pq->size--];
if (pq->size == 0) {
elements[PQ_FIRST_ENTRY] = NULL;
return;
}
for (i = PQ_FIRST_ENTRY;
(child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size;
i = child)
{
if (child != pq->size &&
cairo_bo_event_compare (elements[child+1],
elements[child]) < 0)
{
child++;
}
if (cairo_bo_event_compare (elements[child], tail) >= 0)
break;
elements[i] = elements[child];
}
elements[i] = tail;
}
static inline cairo_status_t
_cairo_bo_event_queue_insert (cairo_bo_event_queue_t *queue,
cairo_bo_event_type_t type,
cairo_bo_edge_t *e1,
cairo_bo_edge_t *e2,
const cairo_bo_intersect_point_t *point)
{
cairo_bo_queue_event_t *event;
event = _cairo_freepool_alloc (&queue->pool);
if (unlikely (event == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
event->type = type;
event->e1 = e1;
event->e2 = e2;
event->point = *point;
return _pqueue_push (&queue->pqueue, (cairo_bo_event_t *) event);
}
static void
_cairo_bo_event_queue_delete (cairo_bo_event_queue_t *queue,
cairo_bo_event_t *event)
{
_cairo_freepool_free (&queue->pool, event);
}
static cairo_bo_event_t *
_cairo_bo_event_dequeue (cairo_bo_event_queue_t *event_queue)
{
cairo_bo_event_t *event, *cmp;
event = event_queue->pqueue.elements[PQ_FIRST_ENTRY];
cmp = *event_queue->start_events;
if (event == NULL ||
(cmp != NULL && cairo_bo_event_compare (cmp, event) < 0))
{
event = cmp;
event_queue->start_events++;
}
else
{
_pqueue_pop (&event_queue->pqueue);
}
return event;
}
CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
cairo_bo_event_t *,
cairo_bo_event_compare)
static void
_cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
cairo_bo_event_t **start_events,
int num_events)
{
_cairo_bo_event_queue_sort (start_events, num_events);
start_events[num_events] = NULL;
event_queue->start_events = start_events;
_cairo_freepool_init (&event_queue->pool,
sizeof (cairo_bo_queue_event_t));
_pqueue_init (&event_queue->pqueue);
event_queue->pqueue.elements[PQ_FIRST_ENTRY] = NULL;
}
static cairo_status_t
event_queue_insert_stop (cairo_bo_event_queue_t *event_queue,
cairo_bo_edge_t *edge)
{
cairo_bo_intersect_point_t point;
point.y.ordinate = edge->edge.bottom;
point.y.approx = EXACT;
point.x.ordinate = _line_compute_intersection_x_for_y (&edge->edge.line,
point.y.ordinate);
point.x.approx = EXACT;
return _cairo_bo_event_queue_insert (event_queue,
CAIRO_BO_EVENT_TYPE_STOP,
edge, NULL,
&point);
}
static void
_cairo_bo_event_queue_fini (cairo_bo_event_queue_t *event_queue)
{
_pqueue_fini (&event_queue->pqueue);
_cairo_freepool_fini (&event_queue->pool);
}
static inline cairo_status_t
event_queue_insert_if_intersect_below_current_y (cairo_bo_event_queue_t *event_queue,
cairo_bo_edge_t *left,
cairo_bo_edge_t *right)
{
cairo_bo_intersect_point_t intersection;
if (_line_equal (&left->edge.line, &right->edge.line))
return CAIRO_STATUS_SUCCESS;
/* The names "left" and "right" here are correct descriptions of
* the order of the two edges within the active edge list. So if a
* slope comparison also puts left less than right, then we know
* that the intersection of these two segments has already
* occurred before the current sweep line position. */
if (_slope_compare (left, right) <= 0)
return CAIRO_STATUS_SUCCESS;
if (! _cairo_bo_edge_intersect (left, right, &intersection))
return CAIRO_STATUS_SUCCESS;
return _cairo_bo_event_queue_insert (event_queue,
CAIRO_BO_EVENT_TYPE_INTERSECTION,
left, right,
&intersection);
}
static void
_cairo_bo_sweep_line_init (cairo_bo_sweep_line_t *sweep_line)
{
sweep_line->head = NULL;
sweep_line->current_y = INT32_MIN;
sweep_line->current_edge = NULL;
}
static cairo_status_t
sweep_line_insert (cairo_bo_sweep_line_t *sweep_line,
cairo_bo_edge_t *edge)
{
if (sweep_line->current_edge != NULL) {
cairo_bo_edge_t *prev, *next;
int cmp;
cmp = _cairo_bo_sweep_line_compare_edges (sweep_line,
sweep_line->current_edge,
edge);
if (cmp < 0) {
prev = sweep_line->current_edge;
next = prev->next;
while (next != NULL &&
_cairo_bo_sweep_line_compare_edges (sweep_line,
next, edge) < 0)
{
prev = next, next = prev->next;
}
prev->next = edge;
edge->prev = prev;
edge->next = next;
if (next != NULL)
next->prev = edge;
} else if (cmp > 0) {
next = sweep_line->current_edge;
prev = next->prev;
while (prev != NULL &&
_cairo_bo_sweep_line_compare_edges (sweep_line,
prev, edge) > 0)
{
next = prev, prev = next->prev;
}
next->prev = edge;
edge->next = next;
edge->prev = prev;
if (prev != NULL)
prev->next = edge;
else
sweep_line->head = edge;
} else {
prev = sweep_line->current_edge;
edge->prev = prev;
edge->next = prev->next;
if (prev->next != NULL)
prev->next->prev = edge;
prev->next = edge;
}
} else {
sweep_line->head = edge;
}
sweep_line->current_edge = edge;
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_bo_sweep_line_delete (cairo_bo_sweep_line_t *sweep_line,
cairo_bo_edge_t *edge)
{
if (edge->prev != NULL)
edge->prev->next = edge->next;
else
sweep_line->head = edge->next;
if (edge->next != NULL)
edge->next->prev = edge->prev;
if (sweep_line->current_edge == edge)
sweep_line->current_edge = edge->prev ? edge->prev : edge->next;
}
static void
_cairo_bo_sweep_line_swap (cairo_bo_sweep_line_t *sweep_line,
cairo_bo_edge_t *left,
cairo_bo_edge_t *right)
{
if (left->prev != NULL)
left->prev->next = right;
else
sweep_line->head = right;
if (right->next != NULL)
right->next->prev = left;
left->next = right->next;
right->next = left;
right->prev = left->prev;
left->prev = right;
}
static inline cairo_bool_t
edges_colinear (const cairo_bo_edge_t *a, const cairo_bo_edge_t *b)
{
if (_line_equal (&a->edge.line, &b->edge.line))
return TRUE;
if (_slope_compare (a, b))
return FALSE;
/* The choice of y is not truly arbitrary since we must guarantee that it
* is greater than the start of either line.
*/
if (a->edge.line.p1.y == b->edge.line.p1.y) {
return a->edge.line.p1.x == b->edge.line.p1.x;
} else if (a->edge.line.p1.y < b->edge.line.p1.y) {
return edge_compare_for_y_against_x (b,
a->edge.line.p1.y,
a->edge.line.p1.x) == 0;
} else {
return edge_compare_for_y_against_x (a,
b->edge.line.p1.y,
b->edge.line.p1.x) == 0;
}
}
static void
edges_end (cairo_bo_edge_t *left,
int32_t bot,
cairo_polygon_t *polygon)
{
cairo_bo_deferred_t *l = &left->deferred;
cairo_bo_edge_t *right = l->other;
assert(right->deferred.other == NULL);
if (likely (l->top < bot)) {
_cairo_polygon_add_line (polygon, &left->edge.line, l->top, bot, 1);
_cairo_polygon_add_line (polygon, &right->edge.line, l->top, bot, -1);
}
l->other = NULL;
}
static inline void
edges_start_or_continue (cairo_bo_edge_t *left,
cairo_bo_edge_t *right,
int top,
cairo_polygon_t *polygon)
{
assert (right != NULL);
assert (right->deferred.other == NULL);
if (left->deferred.other == right)
return;
if (left->deferred.other != NULL) {
if (edges_colinear (left->deferred.other, right)) {
cairo_bo_edge_t *old = left->deferred.other;
/* continuation on right, extend right to cover both */
assert (old->deferred.other == NULL);
assert (old->edge.line.p2.y > old->edge.line.p1.y);
if (old->edge.line.p1.y < right->edge.line.p1.y)
right->edge.line.p1 = old->edge.line.p1;
if (old->edge.line.p2.y > right->edge.line.p2.y)
right->edge.line.p2 = old->edge.line.p2;
left->deferred.other = right;
return;
}
edges_end (left, top, polygon);
}
if (! edges_colinear (left, right)) {
left->deferred.top = top;
left->deferred.other = right;
}
}
#define is_zero(w) ((w)[0] == 0 || (w)[1] == 0)
static inline void
active_edges (cairo_bo_edge_t *left,
int32_t top,
cairo_polygon_t *polygon)
{
cairo_bo_edge_t *right;
int winding[2] = {0, 0};
/* Yes, this is naive. Consider this a placeholder. */
while (left != NULL) {
assert (is_zero (winding));
do {
winding[left->a_or_b] += left->edge.dir;
if (! is_zero (winding))
break;
if unlikely ((left->deferred.other))
edges_end (left, top, polygon);
left = left->next;
if (! left)
return;
} while (1);
right = left->next;
do {
if unlikely ((right->deferred.other))
edges_end (right, top, polygon);
winding[right->a_or_b] += right->edge.dir;
if (is_zero (winding)) {
if (right->next == NULL ||
! edges_colinear (right, right->next))
break;
}
right = right->next;
} while (1);
edges_start_or_continue (left, right, top, polygon);
left = right->next;
}
}
static cairo_status_t
intersection_sweep (cairo_bo_event_t **start_events,
int num_events,
cairo_polygon_t *polygon)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS; /* silence compiler */
cairo_bo_event_queue_t event_queue;
cairo_bo_sweep_line_t sweep_line;
cairo_bo_event_t *event;
cairo_bo_edge_t *left, *right;
cairo_bo_edge_t *e1, *e2;
_cairo_bo_event_queue_init (&event_queue, start_events, num_events);
_cairo_bo_sweep_line_init (&sweep_line);
while ((event = _cairo_bo_event_dequeue (&event_queue))) {
if (event->point.y.ordinate != sweep_line.current_y) {
active_edges (sweep_line.head,
sweep_line.current_y,
polygon);
sweep_line.current_y = event->point.y.ordinate;
}
switch (event->type) {
case CAIRO_BO_EVENT_TYPE_START:
e1 = &((cairo_bo_start_event_t *) event)->edge;
status = sweep_line_insert (&sweep_line, e1);
if (unlikely (status))
goto unwind;
status = event_queue_insert_stop (&event_queue, e1);
if (unlikely (status))
goto unwind;
left = e1->prev;
right = e1->next;
if (left != NULL) {
status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, e1);
if (unlikely (status))
goto unwind;
}
if (right != NULL) {
status = event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
if (unlikely (status))
goto unwind;
}
break;
case CAIRO_BO_EVENT_TYPE_STOP:
e1 = ((cairo_bo_queue_event_t *) event)->e1;
_cairo_bo_event_queue_delete (&event_queue, event);
if (e1->deferred.other)
edges_end (e1, sweep_line.current_y, polygon);
left = e1->prev;
right = e1->next;
_cairo_bo_sweep_line_delete (&sweep_line, e1);
if (left != NULL && right != NULL) {
status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, right);
if (unlikely (status))
goto unwind;
}
break;
case CAIRO_BO_EVENT_TYPE_INTERSECTION:
e1 = ((cairo_bo_queue_event_t *) event)->e1;
e2 = ((cairo_bo_queue_event_t *) event)->e2;
_cairo_bo_event_queue_delete (&event_queue, event);
/* skip this intersection if its edges are not adjacent */
if (e2 != e1->next)
break;
if (e1->deferred.other)
edges_end (e1, sweep_line.current_y, polygon);
if (e2->deferred.other)
edges_end (e2, sweep_line.current_y, polygon);
left = e1->prev;
right = e2->next;
_cairo_bo_sweep_line_swap (&sweep_line, e1, e2);
/* after the swap e2 is left of e1 */
if (left != NULL) {
status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, e2);
if (unlikely (status))
goto unwind;
}
if (right != NULL) {
status = event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
if (unlikely (status))
goto unwind;
}
break;
}
}
unwind:
_cairo_bo_event_queue_fini (&event_queue);
return status;
}
cairo_status_t
_cairo_polygon_intersect (cairo_polygon_t *a, int winding_a,
cairo_polygon_t *b, int winding_b)
{
cairo_status_t status;
cairo_bo_start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (cairo_bo_start_event_t)];
cairo_bo_start_event_t *events;
cairo_bo_event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1];
cairo_bo_event_t **event_ptrs;
int num_events;
int i, j;
/* XXX lazy */
if (winding_a != CAIRO_FILL_RULE_WINDING) {
status = _cairo_polygon_reduce (a, winding_a);
if (unlikely (status))
return status;
}
if (winding_b != CAIRO_FILL_RULE_WINDING) {
status = _cairo_polygon_reduce (b, winding_b);
if (unlikely (status))
return status;
}
if (unlikely (0 == a->num_edges))
return CAIRO_STATUS_SUCCESS;
if (unlikely (0 == b->num_edges)) {
a->num_edges = 0;
return CAIRO_STATUS_SUCCESS;
}
events = stack_events;
event_ptrs = stack_event_ptrs;
num_events = a->num_edges + b->num_edges;
if (num_events > ARRAY_LENGTH (stack_events)) {
events = _cairo_malloc_ab_plus_c (num_events,
sizeof (cairo_bo_start_event_t) +
sizeof (cairo_bo_event_t *),
sizeof (cairo_bo_event_t *));
if (unlikely (events == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
event_ptrs = (cairo_bo_event_t **) (events + num_events);
}
j = 0;
for (i = 0; i < a->num_edges; i++) {
event_ptrs[j] = (cairo_bo_event_t *) &events[j];
events[j].type = CAIRO_BO_EVENT_TYPE_START;
events[j].point.y.ordinate = a->edges[i].top;
events[j].point.y.approx = EXACT;
events[j].point.x.ordinate =
_line_compute_intersection_x_for_y (&a->edges[i].line,
events[j].point.y.ordinate);
events[j].point.x.approx = EXACT;
events[j].edge.a_or_b = 0;
events[j].edge.edge = a->edges[i];
events[j].edge.deferred.other = NULL;
events[j].edge.prev = NULL;
events[j].edge.next = NULL;
j++;
}
for (i = 0; i < b->num_edges; i++) {
event_ptrs[j] = (cairo_bo_event_t *) &events[j];
events[j].type = CAIRO_BO_EVENT_TYPE_START;
events[j].point.y.ordinate = b->edges[i].top;
events[j].point.y.approx = EXACT;
events[j].point.x.ordinate =
_line_compute_intersection_x_for_y (&b->edges[i].line,
events[j].point.y.ordinate);
events[j].point.x.approx = EXACT;
events[j].edge.a_or_b = 1;
events[j].edge.edge = b->edges[i];
events[j].edge.deferred.other = NULL;
events[j].edge.prev = NULL;
events[j].edge.next = NULL;
j++;
}
assert (j == num_events);
#if 0
{
FILE *file = fopen ("clip_a.txt", "w");
_cairo_debug_print_polygon (file, a);
fclose (file);
}
{
FILE *file = fopen ("clip_b.txt", "w");
_cairo_debug_print_polygon (file, b);
fclose (file);
}
#endif
a->num_edges = 0;
status = intersection_sweep (event_ptrs, num_events, a);
if (events != stack_events)
free (events);
#if 0
{
FILE *file = fopen ("clip_result.txt", "w");
_cairo_debug_print_polygon (file, a);
fclose (file);
}
#endif
return status;
}
cairo_status_t
_cairo_polygon_intersect_with_boxes (cairo_polygon_t *polygon,
cairo_fill_rule_t *winding,
cairo_box_t *boxes,
int num_boxes)
{
cairo_polygon_t b;
cairo_status_t status;
int n;
if (num_boxes == 0) {
polygon->num_edges = 0;
return CAIRO_STATUS_SUCCESS;
}
for (n = 0; n < num_boxes; n++) {
if (polygon->extents.p1.x >= boxes[n].p1.x &&
polygon->extents.p2.x <= boxes[n].p2.x &&
polygon->extents.p1.y >= boxes[n].p1.y &&
polygon->extents.p2.y <= boxes[n].p2.y)
{
return CAIRO_STATUS_SUCCESS;
}
}
_cairo_polygon_init (&b, NULL, 0);
for (n = 0; n < num_boxes; n++) {
if (boxes[n].p2.x > polygon->extents.p1.x &&
boxes[n].p1.x < polygon->extents.p2.x &&
boxes[n].p2.y > polygon->extents.p1.y &&
boxes[n].p1.y < polygon->extents.p2.y)
{
cairo_point_t p1, p2;
p1.y = boxes[n].p1.y;
p2.y = boxes[n].p2.y;
p2.x = p1.x = boxes[n].p1.x;
_cairo_polygon_add_external_edge (&b, &p1, &p2);
p2.x = p1.x = boxes[n].p2.x;
_cairo_polygon_add_external_edge (&b, &p2, &p1);
}
}
status = _cairo_polygon_intersect (polygon, *winding,
&b, CAIRO_FILL_RULE_WINDING);
_cairo_polygon_fini (&b);
*winding = CAIRO_FILL_RULE_WINDING;
return status;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-polygon-reduce.c | /*
* Copyright © 2004 Carl Worth
* Copyright © 2006 Red Hat, Inc.
* Copyright © 2008 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Carl Worth
*
* Contributor(s):
* Carl D. Worth <cworth@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
/* Provide definitions for standalone compilation */
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-freelist-private.h"
#include "cairo-combsort-inline.h"
#define DEBUG_POLYGON 0
typedef cairo_point_t cairo_bo_point32_t;
typedef struct _cairo_bo_intersect_ordinate {
int32_t ordinate;
enum { EXACT, INEXACT } exactness;
} cairo_bo_intersect_ordinate_t;
typedef struct _cairo_bo_intersect_point {
cairo_bo_intersect_ordinate_t x;
cairo_bo_intersect_ordinate_t y;
} cairo_bo_intersect_point_t;
typedef struct _cairo_bo_edge cairo_bo_edge_t;
typedef struct _cairo_bo_deferred {
cairo_bo_edge_t *right;
int32_t top;
} cairo_bo_deferred_t;
struct _cairo_bo_edge {
cairo_edge_t edge;
cairo_bo_edge_t *prev;
cairo_bo_edge_t *next;
cairo_bo_deferred_t deferred;
};
/* the parent is always given by index/2 */
#define PQ_PARENT_INDEX(i) ((i) >> 1)
#define PQ_FIRST_ENTRY 1
/* left and right children are index * 2 and (index * 2) +1 respectively */
#define PQ_LEFT_CHILD_INDEX(i) ((i) << 1)
typedef enum {
CAIRO_BO_EVENT_TYPE_STOP,
CAIRO_BO_EVENT_TYPE_INTERSECTION,
CAIRO_BO_EVENT_TYPE_START
} cairo_bo_event_type_t;
typedef struct _cairo_bo_event {
cairo_bo_event_type_t type;
cairo_point_t point;
} cairo_bo_event_t;
typedef struct _cairo_bo_start_event {
cairo_bo_event_type_t type;
cairo_point_t point;
cairo_bo_edge_t edge;
} cairo_bo_start_event_t;
typedef struct _cairo_bo_queue_event {
cairo_bo_event_type_t type;
cairo_point_t point;
cairo_bo_edge_t *e1;
cairo_bo_edge_t *e2;
} cairo_bo_queue_event_t;
typedef struct _pqueue {
int size, max_size;
cairo_bo_event_t **elements;
cairo_bo_event_t *elements_embedded[1024];
} pqueue_t;
typedef struct _cairo_bo_event_queue {
cairo_freepool_t pool;
pqueue_t pqueue;
cairo_bo_event_t **start_events;
} cairo_bo_event_queue_t;
typedef struct _cairo_bo_sweep_line {
cairo_bo_edge_t *head;
int32_t current_y;
cairo_bo_edge_t *current_edge;
} cairo_bo_sweep_line_t;
static cairo_fixed_t
_line_compute_intersection_x_for_y (const cairo_line_t *line,
cairo_fixed_t y)
{
cairo_fixed_t x, dy;
if (y == line->p1.y)
return line->p1.x;
if (y == line->p2.y)
return line->p2.x;
x = line->p1.x;
dy = line->p2.y - line->p1.y;
if (dy != 0) {
x += _cairo_fixed_mul_div_floor (y - line->p1.y,
line->p2.x - line->p1.x,
dy);
}
return x;
}
static inline int
_cairo_bo_point32_compare (cairo_bo_point32_t const *a,
cairo_bo_point32_t const *b)
{
int cmp;
cmp = a->y - b->y;
if (cmp)
return cmp;
return a->x - b->x;
}
/* Compare the slope of a to the slope of b, returning 1, 0, -1 if the
* slope a is respectively greater than, equal to, or less than the
* slope of b.
*
* For each edge, consider the direction vector formed from:
*
* top -> bottom
*
* which is:
*
* (dx, dy) = (line.p2.x - line.p1.x, line.p2.y - line.p1.y)
*
* We then define the slope of each edge as dx/dy, (which is the
* inverse of the slope typically used in math instruction). We never
* compute a slope directly as the value approaches infinity, but we
* can derive a slope comparison without division as follows, (where
* the ? represents our compare operator).
*
* 1. slope(a) ? slope(b)
* 2. adx/ady ? bdx/bdy
* 3. (adx * bdy) ? (bdx * ady)
*
* Note that from step 2 to step 3 there is no change needed in the
* sign of the result since both ady and bdy are guaranteed to be
* greater than or equal to 0.
*
* When using this slope comparison to sort edges, some care is needed
* when interpreting the results. Since the slope compare operates on
* distance vectors from top to bottom it gives a correct left to
* right sort for edges that have a common top point, (such as two
* edges with start events at the same location). On the other hand,
* the sense of the result will be exactly reversed for two edges that
* have a common stop point.
*/
static inline int
_slope_compare (const cairo_bo_edge_t *a,
const cairo_bo_edge_t *b)
{
/* XXX: We're assuming here that dx and dy will still fit in 32
* bits. That's not true in general as there could be overflow. We
* should prevent that before the tessellation algorithm
* begins.
*/
int32_t adx = a->edge.line.p2.x - a->edge.line.p1.x;
int32_t bdx = b->edge.line.p2.x - b->edge.line.p1.x;
/* Since the dy's are all positive by construction we can fast
* path several common cases.
*/
/* First check for vertical lines. */
if (adx == 0)
return -bdx;
if (bdx == 0)
return adx;
/* Then where the two edges point in different directions wrt x. */
if ((adx ^ bdx) < 0)
return adx;
/* Finally we actually need to do the general comparison. */
{
int32_t ady = a->edge.line.p2.y - a->edge.line.p1.y;
int32_t bdy = b->edge.line.p2.y - b->edge.line.p1.y;
cairo_int64_t adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
cairo_int64_t bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
return _cairo_int64_cmp (adx_bdy, bdx_ady);
}
}
/*
* We need to compare the x-coordinates of a pair of lines for a particular y,
* without loss of precision.
*
* The x-coordinate along an edge for a given y is:
* X = A_x + (Y - A_y) * A_dx / A_dy
*
* So the inequality we wish to test is:
* A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy,
* where ∘ is our inequality operator.
*
* By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are
* all positive, so we can rearrange it thus without causing a sign change:
* A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy
* - (Y - A_y) * A_dx * B_dy
*
* Given the assumption that all the deltas fit within 32 bits, we can compute
* this comparison directly using 128 bit arithmetic. For certain, but common,
* input we can reduce this down to a single 32 bit compare by inspecting the
* deltas.
*
* (And put the burden of the work on developing fast 128 bit ops, which are
* required throughout the tessellator.)
*
* See the similar discussion for _slope_compare().
*/
static int
edges_compare_x_for_y_general (const cairo_bo_edge_t *a,
const cairo_bo_edge_t *b,
int32_t y)
{
/* XXX: We're assuming here that dx and dy will still fit in 32
* bits. That's not true in general as there could be overflow. We
* should prevent that before the tessellation algorithm
* begins.
*/
int32_t dx;
int32_t adx, ady;
int32_t bdx, bdy;
enum {
HAVE_NONE = 0x0,
HAVE_DX = 0x1,
HAVE_ADX = 0x2,
HAVE_DX_ADX = HAVE_DX | HAVE_ADX,
HAVE_BDX = 0x4,
HAVE_DX_BDX = HAVE_DX | HAVE_BDX,
HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX,
HAVE_ALL = HAVE_DX | HAVE_ADX | HAVE_BDX
} have_dx_adx_bdx = HAVE_ALL;
/* don't bother solving for abscissa if the edges' bounding boxes
* can be used to order them. */
{
int32_t amin, amax;
int32_t bmin, bmax;
if (a->edge.line.p1.x < a->edge.line.p2.x) {
amin = a->edge.line.p1.x;
amax = a->edge.line.p2.x;
} else {
amin = a->edge.line.p2.x;
amax = a->edge.line.p1.x;
}
if (b->edge.line.p1.x < b->edge.line.p2.x) {
bmin = b->edge.line.p1.x;
bmax = b->edge.line.p2.x;
} else {
bmin = b->edge.line.p2.x;
bmax = b->edge.line.p1.x;
}
if (amax < bmin) return -1;
if (amin > bmax) return +1;
}
ady = a->edge.line.p2.y - a->edge.line.p1.y;
adx = a->edge.line.p2.x - a->edge.line.p1.x;
if (adx == 0)
have_dx_adx_bdx &= ~HAVE_ADX;
bdy = b->edge.line.p2.y - b->edge.line.p1.y;
bdx = b->edge.line.p2.x - b->edge.line.p1.x;
if (bdx == 0)
have_dx_adx_bdx &= ~HAVE_BDX;
dx = a->edge.line.p1.x - b->edge.line.p1.x;
if (dx == 0)
have_dx_adx_bdx &= ~HAVE_DX;
#define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx)
#define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->edge.line.p1.y)
#define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->edge.line.p1.y)
switch (have_dx_adx_bdx) {
default:
case HAVE_NONE:
return 0;
case HAVE_DX:
/* A_dy * B_dy * (A_x - B_x) ∘ 0 */
return dx; /* ady * bdy is positive definite */
case HAVE_ADX:
/* 0 ∘ - (Y - A_y) * A_dx * B_dy */
return adx; /* bdy * (y - a->top.y) is positive definite */
case HAVE_BDX:
/* 0 ∘ (Y - B_y) * B_dx * A_dy */
return -bdx; /* ady * (y - b->top.y) is positive definite */
case HAVE_ADX_BDX:
/* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
if ((adx ^ bdx) < 0) {
return adx;
} else if (a->edge.line.p1.y == b->edge.line.p1.y) { /* common origin */
cairo_int64_t adx_bdy, bdx_ady;
/* ∴ A_dx * B_dy ∘ B_dx * A_dy */
adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
return _cairo_int64_cmp (adx_bdy, bdx_ady);
} else
return _cairo_int128_cmp (A, B);
case HAVE_DX_ADX:
/* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
if ((-adx ^ dx) < 0) {
return dx;
} else {
cairo_int64_t ady_dx, dy_adx;
ady_dx = _cairo_int32x32_64_mul (ady, dx);
dy_adx = _cairo_int32x32_64_mul (a->edge.line.p1.y - y, adx);
return _cairo_int64_cmp (ady_dx, dy_adx);
}
case HAVE_DX_BDX:
/* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
if ((bdx ^ dx) < 0) {
return dx;
} else {
cairo_int64_t bdy_dx, dy_bdx;
bdy_dx = _cairo_int32x32_64_mul (bdy, dx);
dy_bdx = _cairo_int32x32_64_mul (y - b->edge.line.p1.y, bdx);
return _cairo_int64_cmp (bdy_dx, dy_bdx);
}
case HAVE_ALL:
/* XXX try comparing (a->edge.line.p2.x - b->edge.line.p2.x) et al */
return _cairo_int128_cmp (L, _cairo_int128_sub (B, A));
}
#undef B
#undef A
#undef L
}
/*
* We need to compare the x-coordinate of a line for a particular y wrt to a
* given x, without loss of precision.
*
* The x-coordinate along an edge for a given y is:
* X = A_x + (Y - A_y) * A_dx / A_dy
*
* So the inequality we wish to test is:
* A_x + (Y - A_y) * A_dx / A_dy ∘ X
* where ∘ is our inequality operator.
*
* By construction, we know that A_dy (and (Y - A_y)) are
* all positive, so we can rearrange it thus without causing a sign change:
* (Y - A_y) * A_dx ∘ (X - A_x) * A_dy
*
* Given the assumption that all the deltas fit within 32 bits, we can compute
* this comparison directly using 64 bit arithmetic.
*
* See the similar discussion for _slope_compare() and
* edges_compare_x_for_y_general().
*/
static int
edge_compare_for_y_against_x (const cairo_bo_edge_t *a,
int32_t y,
int32_t x)
{
int32_t adx, ady;
int32_t dx, dy;
cairo_int64_t L, R;
if (x < a->edge.line.p1.x && x < a->edge.line.p2.x)
return 1;
if (x > a->edge.line.p1.x && x > a->edge.line.p2.x)
return -1;
adx = a->edge.line.p2.x - a->edge.line.p1.x;
dx = x - a->edge.line.p1.x;
if (adx == 0)
return -dx;
if (dx == 0 || (adx ^ dx) < 0)
return adx;
dy = y - a->edge.line.p1.y;
ady = a->edge.line.p2.y - a->edge.line.p1.y;
L = _cairo_int32x32_64_mul (dy, adx);
R = _cairo_int32x32_64_mul (dx, ady);
return _cairo_int64_cmp (L, R);
}
static int
edges_compare_x_for_y (const cairo_bo_edge_t *a,
const cairo_bo_edge_t *b,
int32_t y)
{
/* If the sweep-line is currently on an end-point of a line,
* then we know its precise x value (and considering that we often need to
* compare events at end-points, this happens frequently enough to warrant
* special casing).
*/
enum {
HAVE_NEITHER = 0x0,
HAVE_AX = 0x1,
HAVE_BX = 0x2,
HAVE_BOTH = HAVE_AX | HAVE_BX
} have_ax_bx = HAVE_BOTH;
int32_t ax = 0, bx = 0;
if (y == a->edge.line.p1.y)
ax = a->edge.line.p1.x;
else if (y == a->edge.line.p2.y)
ax = a->edge.line.p2.x;
else
have_ax_bx &= ~HAVE_AX;
if (y == b->edge.line.p1.y)
bx = b->edge.line.p1.x;
else if (y == b->edge.line.p2.y)
bx = b->edge.line.p2.x;
else
have_ax_bx &= ~HAVE_BX;
switch (have_ax_bx) {
default:
case HAVE_NEITHER:
return edges_compare_x_for_y_general (a, b, y);
case HAVE_AX:
return -edge_compare_for_y_against_x (b, y, ax);
case HAVE_BX:
return edge_compare_for_y_against_x (a, y, bx);
case HAVE_BOTH:
return ax - bx;
}
}
static inline int
_line_equal (const cairo_line_t *a, const cairo_line_t *b)
{
return (a->p1.x == b->p1.x && a->p1.y == b->p1.y &&
a->p2.x == b->p2.x && a->p2.y == b->p2.y);
}
static int
_cairo_bo_sweep_line_compare_edges (cairo_bo_sweep_line_t *sweep_line,
const cairo_bo_edge_t *a,
const cairo_bo_edge_t *b)
{
int cmp;
/* compare the edges if not identical */
if (! _line_equal (&a->edge.line, &b->edge.line)) {
cmp = edges_compare_x_for_y (a, b, sweep_line->current_y);
if (cmp)
return cmp;
/* The two edges intersect exactly at y, so fall back on slope
* comparison. We know that this compare_edges function will be
* called only when starting a new edge, (not when stopping an
* edge), so we don't have to worry about conditionally inverting
* the sense of _slope_compare. */
cmp = _slope_compare (a, b);
if (cmp)
return cmp;
}
/* We've got two collinear edges now. */
return b->edge.bottom - a->edge.bottom;
}
static inline cairo_int64_t
det32_64 (int32_t a, int32_t b,
int32_t c, int32_t d)
{
/* det = a * d - b * c */
return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d),
_cairo_int32x32_64_mul (b, c));
}
static inline cairo_int128_t
det64x32_128 (cairo_int64_t a, int32_t b,
cairo_int64_t c, int32_t d)
{
/* det = a * d - b * c */
return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d),
_cairo_int64x32_128_mul (c, b));
}
/* Compute the intersection of two lines as defined by two edges. The
* result is provided as a coordinate pair of 128-bit integers.
*
* Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or
* %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel.
*/
static cairo_bool_t
intersect_lines (cairo_bo_edge_t *a,
cairo_bo_edge_t *b,
cairo_bo_intersect_point_t *intersection)
{
cairo_int64_t a_det, b_det;
/* XXX: We're assuming here that dx and dy will still fit in 32
* bits. That's not true in general as there could be overflow. We
* should prevent that before the tessellation algorithm begins.
* What we're doing to mitigate this is to perform clamping in
* cairo_bo_tessellate_polygon().
*/
int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x;
int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y;
int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x;
int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y;
cairo_int64_t den_det;
cairo_int64_t R;
cairo_quorem64_t qr;
den_det = det32_64 (dx1, dy1, dx2, dy2);
/* Q: Can we determine that the lines do not intersect (within range)
* much more cheaply than computing the intersection point i.e. by
* avoiding the division?
*
* X = ax + t * adx = bx + s * bdx;
* Y = ay + t * ady = by + s * bdy;
* ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx)
* => t * L = R
*
* Therefore we can reject any intersection (under the criteria for
* valid intersection events) if:
* L^R < 0 => t < 0, or
* L<R => t > 1
*
* (where top/bottom must at least extend to the line endpoints).
*
* A similar substitution can be performed for s, yielding:
* s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by)
*/
R = det32_64 (dx2, dy2,
b->edge.line.p1.x - a->edge.line.p1.x,
b->edge.line.p1.y - a->edge.line.p1.y);
if (_cairo_int64_negative (den_det)) {
if (_cairo_int64_ge (den_det, R))
return FALSE;
} else {
if (_cairo_int64_le (den_det, R))
return FALSE;
}
R = det32_64 (dy1, dx1,
a->edge.line.p1.y - b->edge.line.p1.y,
a->edge.line.p1.x - b->edge.line.p1.x);
if (_cairo_int64_negative (den_det)) {
if (_cairo_int64_ge (den_det, R))
return FALSE;
} else {
if (_cairo_int64_le (den_det, R))
return FALSE;
}
/* We now know that the two lines should intersect within range. */
a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y,
a->edge.line.p2.x, a->edge.line.p2.y);
b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y,
b->edge.line.p2.x, b->edge.line.p2.y);
/* x = det (a_det, dx1, b_det, dx2) / den_det */
qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1,
b_det, dx2),
den_det);
if (_cairo_int64_eq (qr.rem, den_det))
return FALSE;
#if 0
intersection->x.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
#else
intersection->x.exactness = EXACT;
if (! _cairo_int64_is_zero (qr.rem)) {
if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
qr.rem = _cairo_int64_negate (qr.rem);
qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
if (_cairo_int64_ge (qr.rem, den_det)) {
qr.quo = _cairo_int64_add (qr.quo,
_cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
} else
intersection->x.exactness = INEXACT;
}
#endif
intersection->x.ordinate = _cairo_int64_to_int32 (qr.quo);
/* y = det (a_det, dy1, b_det, dy2) / den_det */
qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1,
b_det, dy2),
den_det);
if (_cairo_int64_eq (qr.rem, den_det))
return FALSE;
#if 0
intersection->y.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
#else
intersection->y.exactness = EXACT;
if (! _cairo_int64_is_zero (qr.rem)) {
if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
qr.rem = _cairo_int64_negate (qr.rem);
qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
if (_cairo_int64_ge (qr.rem, den_det)) {
qr.quo = _cairo_int64_add (qr.quo,
_cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
} else
intersection->y.exactness = INEXACT;
}
#endif
intersection->y.ordinate = _cairo_int64_to_int32 (qr.quo);
return TRUE;
}
static int
_cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t a,
int32_t b)
{
/* First compare the quotient */
if (a.ordinate > b)
return +1;
if (a.ordinate < b)
return -1;
/* With quotient identical, if remainder is 0 then compare equal */
/* Otherwise, the non-zero remainder makes a > b */
return INEXACT == a.exactness;
}
/* Does the given edge contain the given point. The point must already
* be known to be contained within the line determined by the edge,
* (most likely the point results from an intersection of this edge
* with another).
*
* If we had exact arithmetic, then this function would simply be a
* matter of examining whether the y value of the point lies within
* the range of y values of the edge. But since intersection points
* are not exact due to being rounded to the nearest integer within
* the available precision, we must also examine the x value of the
* point.
*
* The definition of "contains" here is that the given intersection
* point will be seen by the sweep line after the start event for the
* given edge and before the stop event for the edge. See the comments
* in the implementation for more details.
*/
static cairo_bool_t
_cairo_bo_edge_contains_intersect_point (cairo_bo_edge_t *edge,
cairo_bo_intersect_point_t *point)
{
int cmp_top, cmp_bottom;
/* XXX: When running the actual algorithm, we don't actually need to
* compare against edge->top at all here, since any intersection above
* top is eliminated early via a slope comparison. We're leaving these
* here for now only for the sake of the quadratic-time intersection
* finder which needs them.
*/
cmp_top = _cairo_bo_intersect_ordinate_32_compare (point->y,
edge->edge.top);
cmp_bottom = _cairo_bo_intersect_ordinate_32_compare (point->y,
edge->edge.bottom);
if (cmp_top < 0 || cmp_bottom > 0)
{
return FALSE;
}
if (cmp_top > 0 && cmp_bottom < 0)
{
return TRUE;
}
/* At this stage, the point lies on the same y value as either
* edge->top or edge->bottom, so we have to examine the x value in
* order to properly determine containment. */
/* If the y value of the point is the same as the y value of the
* top of the edge, then the x value of the point must be greater
* to be considered as inside the edge. Similarly, if the y value
* of the point is the same as the y value of the bottom of the
* edge, then the x value of the point must be less to be
* considered as inside. */
if (cmp_top == 0) {
cairo_fixed_t top_x;
top_x = _line_compute_intersection_x_for_y (&edge->edge.line,
edge->edge.top);
return _cairo_bo_intersect_ordinate_32_compare (point->x, top_x) > 0;
} else { /* cmp_bottom == 0 */
cairo_fixed_t bot_x;
bot_x = _line_compute_intersection_x_for_y (&edge->edge.line,
edge->edge.bottom);
return _cairo_bo_intersect_ordinate_32_compare (point->x, bot_x) < 0;
}
}
/* Compute the intersection of two edges. The result is provided as a
* coordinate pair of 128-bit integers.
*
* Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection
* that is within both edges, %CAIRO_BO_STATUS_NO_INTERSECTION if the
* intersection of the lines defined by the edges occurs outside of
* one or both edges, and %CAIRO_BO_STATUS_PARALLEL if the two edges
* are exactly parallel.
*
* Note that when determining if a candidate intersection is "inside"
* an edge, we consider both the infinitesimal shortening and the
* infinitesimal tilt rules described by John Hobby. Specifically, if
* the intersection is exactly the same as an edge point, it is
* effectively outside (no intersection is returned). Also, if the
* intersection point has the same
*/
static cairo_bool_t
_cairo_bo_edge_intersect (cairo_bo_edge_t *a,
cairo_bo_edge_t *b,
cairo_bo_point32_t *intersection)
{
cairo_bo_intersect_point_t quorem;
if (! intersect_lines (a, b, &quorem))
return FALSE;
if (! _cairo_bo_edge_contains_intersect_point (a, &quorem))
return FALSE;
if (! _cairo_bo_edge_contains_intersect_point (b, &quorem))
return FALSE;
/* Now that we've correctly compared the intersection point and
* determined that it lies within the edge, then we know that we
* no longer need any more bits of storage for the intersection
* than we do for our edge coordinates. We also no longer need the
* remainder from the division. */
intersection->x = quorem.x.ordinate;
intersection->y = quorem.y.ordinate;
return TRUE;
}
static inline int
cairo_bo_event_compare (const cairo_bo_event_t *a,
const cairo_bo_event_t *b)
{
int cmp;
cmp = _cairo_bo_point32_compare (&a->point, &b->point);
if (cmp)
return cmp;
cmp = a->type - b->type;
if (cmp)
return cmp;
return a - b;
}
static inline void
_pqueue_init (pqueue_t *pq)
{
pq->max_size = ARRAY_LENGTH (pq->elements_embedded);
pq->size = 0;
pq->elements = pq->elements_embedded;
}
static inline void
_pqueue_fini (pqueue_t *pq)
{
if (pq->elements != pq->elements_embedded)
free (pq->elements);
}
static cairo_status_t
_pqueue_grow (pqueue_t *pq)
{
cairo_bo_event_t **new_elements;
pq->max_size *= 2;
if (pq->elements == pq->elements_embedded) {
new_elements = _cairo_malloc_ab (pq->max_size,
sizeof (cairo_bo_event_t *));
if (unlikely (new_elements == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
memcpy (new_elements, pq->elements_embedded,
sizeof (pq->elements_embedded));
} else {
new_elements = _cairo_realloc_ab (pq->elements,
pq->max_size,
sizeof (cairo_bo_event_t *));
if (unlikely (new_elements == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
pq->elements = new_elements;
return CAIRO_STATUS_SUCCESS;
}
static inline cairo_status_t
_pqueue_push (pqueue_t *pq, cairo_bo_event_t *event)
{
cairo_bo_event_t **elements;
int i, parent;
if (unlikely (pq->size + 1 == pq->max_size)) {
cairo_status_t status;
status = _pqueue_grow (pq);
if (unlikely (status))
return status;
}
elements = pq->elements;
for (i = ++pq->size;
i != PQ_FIRST_ENTRY &&
cairo_bo_event_compare (event,
elements[parent = PQ_PARENT_INDEX (i)]) < 0;
i = parent)
{
elements[i] = elements[parent];
}
elements[i] = event;
return CAIRO_STATUS_SUCCESS;
}
static inline void
_pqueue_pop (pqueue_t *pq)
{
cairo_bo_event_t **elements = pq->elements;
cairo_bo_event_t *tail;
int child, i;
tail = elements[pq->size--];
if (pq->size == 0) {
elements[PQ_FIRST_ENTRY] = NULL;
return;
}
for (i = PQ_FIRST_ENTRY;
(child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size;
i = child)
{
if (child != pq->size &&
cairo_bo_event_compare (elements[child+1],
elements[child]) < 0)
{
child++;
}
if (cairo_bo_event_compare (elements[child], tail) >= 0)
break;
elements[i] = elements[child];
}
elements[i] = tail;
}
static inline cairo_status_t
_cairo_bo_event_queue_insert (cairo_bo_event_queue_t *queue,
cairo_bo_event_type_t type,
cairo_bo_edge_t *e1,
cairo_bo_edge_t *e2,
const cairo_point_t *point)
{
cairo_bo_queue_event_t *event;
event = _cairo_freepool_alloc (&queue->pool);
if (unlikely (event == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
event->type = type;
event->e1 = e1;
event->e2 = e2;
event->point = *point;
return _pqueue_push (&queue->pqueue, (cairo_bo_event_t *) event);
}
static void
_cairo_bo_event_queue_delete (cairo_bo_event_queue_t *queue,
cairo_bo_event_t *event)
{
_cairo_freepool_free (&queue->pool, event);
}
static cairo_bo_event_t *
_cairo_bo_event_dequeue (cairo_bo_event_queue_t *event_queue)
{
cairo_bo_event_t *event, *cmp;
event = event_queue->pqueue.elements[PQ_FIRST_ENTRY];
cmp = *event_queue->start_events;
if (event == NULL ||
(cmp != NULL && cairo_bo_event_compare (cmp, event) < 0))
{
event = cmp;
event_queue->start_events++;
}
else
{
_pqueue_pop (&event_queue->pqueue);
}
return event;
}
CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
cairo_bo_event_t *,
cairo_bo_event_compare)
static void
_cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
cairo_bo_event_t **start_events,
int num_events)
{
_cairo_bo_event_queue_sort (start_events, num_events);
start_events[num_events] = NULL;
event_queue->start_events = start_events;
_cairo_freepool_init (&event_queue->pool,
sizeof (cairo_bo_queue_event_t));
_pqueue_init (&event_queue->pqueue);
event_queue->pqueue.elements[PQ_FIRST_ENTRY] = NULL;
}
static cairo_status_t
_cairo_bo_event_queue_insert_stop (cairo_bo_event_queue_t *event_queue,
cairo_bo_edge_t *edge)
{
cairo_bo_point32_t point;
point.y = edge->edge.bottom;
point.x = _line_compute_intersection_x_for_y (&edge->edge.line,
point.y);
return _cairo_bo_event_queue_insert (event_queue,
CAIRO_BO_EVENT_TYPE_STOP,
edge, NULL,
&point);
}
static void
_cairo_bo_event_queue_fini (cairo_bo_event_queue_t *event_queue)
{
_pqueue_fini (&event_queue->pqueue);
_cairo_freepool_fini (&event_queue->pool);
}
static inline cairo_status_t
_cairo_bo_event_queue_insert_if_intersect_below_current_y (cairo_bo_event_queue_t *event_queue,
cairo_bo_edge_t *left,
cairo_bo_edge_t *right)
{
cairo_bo_point32_t intersection;
if (_line_equal (&left->edge.line, &right->edge.line))
return CAIRO_STATUS_SUCCESS;
/* The names "left" and "right" here are correct descriptions of
* the order of the two edges within the active edge list. So if a
* slope comparison also puts left less than right, then we know
* that the intersection of these two segments has already
* occurred before the current sweep line position. */
if (_slope_compare (left, right) <= 0)
return CAIRO_STATUS_SUCCESS;
if (! _cairo_bo_edge_intersect (left, right, &intersection))
return CAIRO_STATUS_SUCCESS;
return _cairo_bo_event_queue_insert (event_queue,
CAIRO_BO_EVENT_TYPE_INTERSECTION,
left, right,
&intersection);
}
static void
_cairo_bo_sweep_line_init (cairo_bo_sweep_line_t *sweep_line)
{
sweep_line->head = NULL;
sweep_line->current_y = INT32_MIN;
sweep_line->current_edge = NULL;
}
static cairo_status_t
_cairo_bo_sweep_line_insert (cairo_bo_sweep_line_t *sweep_line,
cairo_bo_edge_t *edge)
{
if (sweep_line->current_edge != NULL) {
cairo_bo_edge_t *prev, *next;
int cmp;
cmp = _cairo_bo_sweep_line_compare_edges (sweep_line,
sweep_line->current_edge,
edge);
if (cmp < 0) {
prev = sweep_line->current_edge;
next = prev->next;
while (next != NULL &&
_cairo_bo_sweep_line_compare_edges (sweep_line,
next, edge) < 0)
{
prev = next, next = prev->next;
}
prev->next = edge;
edge->prev = prev;
edge->next = next;
if (next != NULL)
next->prev = edge;
} else if (cmp > 0) {
next = sweep_line->current_edge;
prev = next->prev;
while (prev != NULL &&
_cairo_bo_sweep_line_compare_edges (sweep_line,
prev, edge) > 0)
{
next = prev, prev = next->prev;
}
next->prev = edge;
edge->next = next;
edge->prev = prev;
if (prev != NULL)
prev->next = edge;
else
sweep_line->head = edge;
} else {
prev = sweep_line->current_edge;
edge->prev = prev;
edge->next = prev->next;
if (prev->next != NULL)
prev->next->prev = edge;
prev->next = edge;
}
} else {
sweep_line->head = edge;
}
sweep_line->current_edge = edge;
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_bo_sweep_line_delete (cairo_bo_sweep_line_t *sweep_line,
cairo_bo_edge_t *edge)
{
if (edge->prev != NULL)
edge->prev->next = edge->next;
else
sweep_line->head = edge->next;
if (edge->next != NULL)
edge->next->prev = edge->prev;
if (sweep_line->current_edge == edge)
sweep_line->current_edge = edge->prev ? edge->prev : edge->next;
}
static void
_cairo_bo_sweep_line_swap (cairo_bo_sweep_line_t *sweep_line,
cairo_bo_edge_t *left,
cairo_bo_edge_t *right)
{
if (left->prev != NULL)
left->prev->next = right;
else
sweep_line->head = right;
if (right->next != NULL)
right->next->prev = left;
left->next = right->next;
right->next = left;
right->prev = left->prev;
left->prev = right;
}
static inline cairo_bool_t
edges_colinear (const cairo_bo_edge_t *a, const cairo_bo_edge_t *b)
{
if (_line_equal (&a->edge.line, &b->edge.line))
return TRUE;
if (_slope_compare (a, b))
return FALSE;
/* The choice of y is not truly arbitrary since we must guarantee that it
* is greater than the start of either line.
*/
if (a->edge.line.p1.y == b->edge.line.p1.y) {
return a->edge.line.p1.x == b->edge.line.p1.x;
} else if (a->edge.line.p2.y == b->edge.line.p2.y) {
return a->edge.line.p2.x == b->edge.line.p2.x;
} else if (a->edge.line.p1.y < b->edge.line.p1.y) {
return edge_compare_for_y_against_x (b,
a->edge.line.p1.y,
a->edge.line.p1.x) == 0;
} else {
return edge_compare_for_y_against_x (a,
b->edge.line.p1.y,
b->edge.line.p1.x) == 0;
}
}
static void
_cairo_bo_edge_end (cairo_bo_edge_t *left,
int32_t bot,
cairo_polygon_t *polygon)
{
cairo_bo_deferred_t *d = &left->deferred;
if (likely (d->top < bot)) {
_cairo_polygon_add_line (polygon,
&left->edge.line,
d->top, bot,
1);
_cairo_polygon_add_line (polygon,
&d->right->edge.line,
d->top, bot,
-1);
}
d->right = NULL;
}
static inline void
_cairo_bo_edge_start_or_continue (cairo_bo_edge_t *left,
cairo_bo_edge_t *right,
int top,
cairo_polygon_t *polygon)
{
if (left->deferred.right == right)
return;
if (left->deferred.right != NULL) {
if (right != NULL && edges_colinear (left->deferred.right, right))
{
/* continuation on right, so just swap edges */
left->deferred.right = right;
return;
}
_cairo_bo_edge_end (left, top, polygon);
}
if (right != NULL && ! edges_colinear (left, right)) {
left->deferred.top = top;
left->deferred.right = right;
}
}
static inline void
_active_edges_to_polygon (cairo_bo_edge_t *left,
int32_t top,
cairo_fill_rule_t fill_rule,
cairo_polygon_t *polygon)
{
cairo_bo_edge_t *right;
unsigned int mask;
if (fill_rule == CAIRO_FILL_RULE_WINDING)
mask = ~0;
else
mask = 1;
while (left != NULL) {
int in_out = left->edge.dir;
right = left->next;
if (left->deferred.right == NULL) {
while (right != NULL && right->deferred.right == NULL)
right = right->next;
if (right != NULL && edges_colinear (left, right)) {
/* continuation on left */
left->deferred = right->deferred;
right->deferred.right = NULL;
}
}
right = left->next;
while (right != NULL) {
if (right->deferred.right != NULL)
_cairo_bo_edge_end (right, top, polygon);
in_out += right->edge.dir;
if ((in_out & mask) == 0) {
/* skip co-linear edges */
if (right->next == NULL || !edges_colinear (right, right->next))
break;
}
right = right->next;
}
_cairo_bo_edge_start_or_continue (left, right, top, polygon);
left = right;
if (left != NULL)
left = left->next;
}
}
static cairo_status_t
_cairo_bentley_ottmann_tessellate_bo_edges (cairo_bo_event_t **start_events,
int num_events,
cairo_fill_rule_t fill_rule,
cairo_polygon_t *polygon)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS; /* silence compiler */
cairo_bo_event_queue_t event_queue;
cairo_bo_sweep_line_t sweep_line;
cairo_bo_event_t *event;
cairo_bo_edge_t *left, *right;
cairo_bo_edge_t *e1, *e2;
_cairo_bo_event_queue_init (&event_queue, start_events, num_events);
_cairo_bo_sweep_line_init (&sweep_line);
while ((event = _cairo_bo_event_dequeue (&event_queue))) {
if (event->point.y != sweep_line.current_y) {
_active_edges_to_polygon (sweep_line.head,
sweep_line.current_y,
fill_rule, polygon);
sweep_line.current_y = event->point.y;
}
switch (event->type) {
case CAIRO_BO_EVENT_TYPE_START:
e1 = &((cairo_bo_start_event_t *) event)->edge;
status = _cairo_bo_sweep_line_insert (&sweep_line, e1);
if (unlikely (status))
goto unwind;
status = _cairo_bo_event_queue_insert_stop (&event_queue, e1);
if (unlikely (status))
goto unwind;
left = e1->prev;
right = e1->next;
if (left != NULL) {
status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, e1);
if (unlikely (status))
goto unwind;
}
if (right != NULL) {
status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
if (unlikely (status))
goto unwind;
}
break;
case CAIRO_BO_EVENT_TYPE_STOP:
e1 = ((cairo_bo_queue_event_t *) event)->e1;
_cairo_bo_event_queue_delete (&event_queue, event);
left = e1->prev;
right = e1->next;
_cairo_bo_sweep_line_delete (&sweep_line, e1);
if (e1->deferred.right != NULL)
_cairo_bo_edge_end (e1, e1->edge.bottom, polygon);
if (left != NULL && right != NULL) {
status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, right);
if (unlikely (status))
goto unwind;
}
break;
case CAIRO_BO_EVENT_TYPE_INTERSECTION:
e1 = ((cairo_bo_queue_event_t *) event)->e1;
e2 = ((cairo_bo_queue_event_t *) event)->e2;
_cairo_bo_event_queue_delete (&event_queue, event);
/* skip this intersection if its edges are not adjacent */
if (e2 != e1->next)
break;
left = e1->prev;
right = e2->next;
_cairo_bo_sweep_line_swap (&sweep_line, e1, e2);
/* after the swap e2 is left of e1 */
if (left != NULL) {
status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, e2);
if (unlikely (status))
goto unwind;
}
if (right != NULL) {
status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
if (unlikely (status))
goto unwind;
}
break;
}
}
unwind:
_cairo_bo_event_queue_fini (&event_queue);
return status;
}
cairo_status_t
_cairo_polygon_reduce (cairo_polygon_t *polygon,
cairo_fill_rule_t fill_rule)
{
cairo_status_t status;
cairo_bo_start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (cairo_bo_start_event_t)];
cairo_bo_start_event_t *events;
cairo_bo_event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1];
cairo_bo_event_t **event_ptrs;
int num_limits;
int num_events;
int i;
num_events = polygon->num_edges;
if (unlikely (0 == num_events))
return CAIRO_STATUS_SUCCESS;
if (DEBUG_POLYGON) {
FILE *file = fopen ("reduce_in.txt", "w");
_cairo_debug_print_polygon (file, polygon);
fclose (file);
}
events = stack_events;
event_ptrs = stack_event_ptrs;
if (num_events > ARRAY_LENGTH (stack_events)) {
events = _cairo_malloc_ab_plus_c (num_events,
sizeof (cairo_bo_start_event_t) +
sizeof (cairo_bo_event_t *),
sizeof (cairo_bo_event_t *));
if (unlikely (events == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
event_ptrs = (cairo_bo_event_t **) (events + num_events);
}
for (i = 0; i < num_events; i++) {
event_ptrs[i] = (cairo_bo_event_t *) &events[i];
events[i].type = CAIRO_BO_EVENT_TYPE_START;
events[i].point.y = polygon->edges[i].top;
events[i].point.x =
_line_compute_intersection_x_for_y (&polygon->edges[i].line,
events[i].point.y);
events[i].edge.edge = polygon->edges[i];
events[i].edge.deferred.right = NULL;
events[i].edge.prev = NULL;
events[i].edge.next = NULL;
}
num_limits = polygon->num_limits; polygon->num_limits = 0;
polygon->num_edges = 0;
status = _cairo_bentley_ottmann_tessellate_bo_edges (event_ptrs,
num_events,
fill_rule,
polygon);
polygon->num_limits = num_limits;
if (events != stack_events)
free (events);
if (DEBUG_POLYGON) {
FILE *file = fopen ("reduce_out.txt", "w");
_cairo_debug_print_polygon (file, polygon);
fclose (file);
}
return status;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-polygon.c | /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-boxes-private.h"
#include "cairo-contour-private.h"
#include "cairo-error-private.h"
#define DEBUG_POLYGON 0
#if DEBUG_POLYGON && !NDEBUG
static void
assert_last_edge_is_valid(cairo_polygon_t *polygon,
const cairo_box_t *limit)
{
cairo_edge_t *edge;
cairo_fixed_t x;
edge = &polygon->edges[polygon->num_edges-1];
assert (edge->bottom > edge->top);
assert (edge->top >= limit->p1.y);
assert (edge->bottom <= limit->p2.y);
x = _cairo_edge_compute_intersection_x_for_y (&edge->line.p1,
&edge->line.p2,
edge->top);
assert (x >= limit->p1.x);
assert (x <= limit->p2.x);
x = _cairo_edge_compute_intersection_x_for_y (&edge->line.p1,
&edge->line.p2,
edge->bottom);
assert (x >= limit->p1.x);
assert (x <= limit->p2.x);
}
#else
#define assert_last_edge_is_valid(p, l)
#endif
static void
_cairo_polygon_add_edge (cairo_polygon_t *polygon,
const cairo_point_t *p1,
const cairo_point_t *p2,
int dir);
void
_cairo_polygon_limit (cairo_polygon_t *polygon,
const cairo_box_t *limits,
int num_limits)
{
int n;
polygon->limits = limits;
polygon->num_limits = num_limits;
if (polygon->num_limits) {
polygon->limit = limits[0];
for (n = 1; n < num_limits; n++) {
if (limits[n].p1.x < polygon->limit.p1.x)
polygon->limit.p1.x = limits[n].p1.x;
if (limits[n].p1.y < polygon->limit.p1.y)
polygon->limit.p1.y = limits[n].p1.y;
if (limits[n].p2.x > polygon->limit.p2.x)
polygon->limit.p2.x = limits[n].p2.x;
if (limits[n].p2.y > polygon->limit.p2.y)
polygon->limit.p2.y = limits[n].p2.y;
}
}
}
void
_cairo_polygon_limit_to_clip (cairo_polygon_t *polygon,
const cairo_clip_t *clip)
{
if (clip)
_cairo_polygon_limit (polygon, clip->boxes, clip->num_boxes);
else
_cairo_polygon_limit (polygon, 0, 0);
}
void
_cairo_polygon_init (cairo_polygon_t *polygon,
const cairo_box_t *limits,
int num_limits)
{
VG (VALGRIND_MAKE_MEM_UNDEFINED (polygon, sizeof (cairo_polygon_t)));
polygon->status = CAIRO_STATUS_SUCCESS;
polygon->num_edges = 0;
polygon->edges = polygon->edges_embedded;
polygon->edges_size = ARRAY_LENGTH (polygon->edges_embedded);
polygon->extents.p1.x = polygon->extents.p1.y = INT32_MAX;
polygon->extents.p2.x = polygon->extents.p2.y = INT32_MIN;
_cairo_polygon_limit (polygon, limits, num_limits);
}
void
_cairo_polygon_init_with_clip (cairo_polygon_t *polygon,
const cairo_clip_t *clip)
{
if (clip)
_cairo_polygon_init (polygon, clip->boxes, clip->num_boxes);
else
_cairo_polygon_init (polygon, 0, 0);
}
cairo_status_t
_cairo_polygon_init_boxes (cairo_polygon_t *polygon,
const cairo_boxes_t *boxes)
{
const struct _cairo_boxes_chunk *chunk;
int i;
VG (VALGRIND_MAKE_MEM_UNDEFINED (polygon, sizeof (cairo_polygon_t)));
polygon->status = CAIRO_STATUS_SUCCESS;
polygon->num_edges = 0;
polygon->edges = polygon->edges_embedded;
polygon->edges_size = ARRAY_LENGTH (polygon->edges_embedded);
if (boxes->num_boxes > ARRAY_LENGTH (polygon->edges_embedded)/2) {
polygon->edges_size = 2 * boxes->num_boxes;
polygon->edges = _cairo_malloc_ab (polygon->edges_size,
2*sizeof(cairo_edge_t));
if (unlikely (polygon->edges == NULL))
return polygon->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
polygon->extents.p1.x = polygon->extents.p1.y = INT32_MAX;
polygon->extents.p2.x = polygon->extents.p2.y = INT32_MIN;
polygon->limits = NULL;
polygon->num_limits = 0;
for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
for (i = 0; i < chunk->count; i++) {
cairo_point_t p1, p2;
p1 = chunk->base[i].p1;
p2.x = p1.x;
p2.y = chunk->base[i].p2.y;
_cairo_polygon_add_edge (polygon, &p1, &p2, 1);
p1 = chunk->base[i].p2;
p2.x = p1.x;
p2.y = chunk->base[i].p1.y;
_cairo_polygon_add_edge (polygon, &p1, &p2, 1);
}
}
return polygon->status;
}
cairo_status_t
_cairo_polygon_init_box_array (cairo_polygon_t *polygon,
cairo_box_t *boxes,
int num_boxes)
{
int i;
VG (VALGRIND_MAKE_MEM_UNDEFINED (polygon, sizeof (cairo_polygon_t)));
polygon->status = CAIRO_STATUS_SUCCESS;
polygon->num_edges = 0;
polygon->edges = polygon->edges_embedded;
polygon->edges_size = ARRAY_LENGTH (polygon->edges_embedded);
if (num_boxes > ARRAY_LENGTH (polygon->edges_embedded)/2) {
polygon->edges_size = 2 * num_boxes;
polygon->edges = _cairo_malloc_ab (polygon->edges_size,
2*sizeof(cairo_edge_t));
if (unlikely (polygon->edges == NULL))
return polygon->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
polygon->extents.p1.x = polygon->extents.p1.y = INT32_MAX;
polygon->extents.p2.x = polygon->extents.p2.y = INT32_MIN;
polygon->limits = NULL;
polygon->num_limits = 0;
for (i = 0; i < num_boxes; i++) {
cairo_point_t p1, p2;
p1 = boxes[i].p1;
p2.x = p1.x;
p2.y = boxes[i].p2.y;
_cairo_polygon_add_edge (polygon, &p1, &p2, 1);
p1 = boxes[i].p2;
p2.x = p1.x;
p2.y = boxes[i].p1.y;
_cairo_polygon_add_edge (polygon, &p1, &p2, 1);
}
return polygon->status;
}
void
_cairo_polygon_fini (cairo_polygon_t *polygon)
{
if (polygon->edges != polygon->edges_embedded)
free (polygon->edges);
VG (VALGRIND_MAKE_MEM_UNDEFINED (polygon, sizeof (cairo_polygon_t)));
}
/* make room for at least one more edge */
static cairo_bool_t
_cairo_polygon_grow (cairo_polygon_t *polygon)
{
cairo_edge_t *new_edges;
int old_size = polygon->edges_size;
int new_size = 4 * old_size;
if (CAIRO_INJECT_FAULT ()) {
polygon->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
return FALSE;
}
if (polygon->edges == polygon->edges_embedded) {
new_edges = _cairo_malloc_ab (new_size, sizeof (cairo_edge_t));
if (new_edges != NULL)
memcpy (new_edges, polygon->edges, old_size * sizeof (cairo_edge_t));
} else {
new_edges = _cairo_realloc_ab (polygon->edges,
new_size, sizeof (cairo_edge_t));
}
if (unlikely (new_edges == NULL)) {
polygon->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
return FALSE;
}
polygon->edges = new_edges;
polygon->edges_size = new_size;
return TRUE;
}
static void
_add_edge (cairo_polygon_t *polygon,
const cairo_point_t *p1,
const cairo_point_t *p2,
int top, int bottom,
int dir)
{
cairo_edge_t *edge;
assert (top < bottom);
if (unlikely (polygon->num_edges == polygon->edges_size)) {
if (! _cairo_polygon_grow (polygon))
return;
}
edge = &polygon->edges[polygon->num_edges++];
edge->line.p1 = *p1;
edge->line.p2 = *p2;
edge->top = top;
edge->bottom = bottom;
edge->dir = dir;
if (top < polygon->extents.p1.y)
polygon->extents.p1.y = top;
if (bottom > polygon->extents.p2.y)
polygon->extents.p2.y = bottom;
if (p1->x < polygon->extents.p1.x || p1->x > polygon->extents.p2.x) {
cairo_fixed_t x = p1->x;
if (top != p1->y)
x = _cairo_edge_compute_intersection_x_for_y (p1, p2, top);
if (x < polygon->extents.p1.x)
polygon->extents.p1.x = x;
if (x > polygon->extents.p2.x)
polygon->extents.p2.x = x;
}
if (p2->x < polygon->extents.p1.x || p2->x > polygon->extents.p2.x) {
cairo_fixed_t x = p2->x;
if (bottom != p2->y)
x = _cairo_edge_compute_intersection_x_for_y (p1, p2, bottom);
if (x < polygon->extents.p1.x)
polygon->extents.p1.x = x;
if (x > polygon->extents.p2.x)
polygon->extents.p2.x = x;
}
}
static void
_add_clipped_edge (cairo_polygon_t *polygon,
const cairo_point_t *p1,
const cairo_point_t *p2,
const int top, const int bottom,
const int dir)
{
cairo_point_t bot_left, top_right;
cairo_fixed_t top_y, bot_y;
int n;
for (n = 0; n < polygon->num_limits; n++) {
const cairo_box_t *limits = &polygon->limits[n];
cairo_fixed_t pleft, pright;
if (top >= limits->p2.y)
continue;
if (bottom <= limits->p1.y)
continue;
bot_left.x = limits->p1.x;
bot_left.y = limits->p2.y;
top_right.x = limits->p2.x;
top_right.y = limits->p1.y;
/* The useful region */
top_y = MAX (top, limits->p1.y);
bot_y = MIN (bottom, limits->p2.y);
/* The projection of the edge on the horizontal axis */
pleft = MIN (p1->x, p2->x);
pright = MAX (p1->x, p2->x);
if (limits->p1.x <= pleft && pright <= limits->p2.x) {
/* Projection of the edge completely contained in the box:
* clip vertically by restricting top and bottom */
_add_edge (polygon, p1, p2, top_y, bot_y, dir);
assert_last_edge_is_valid (polygon, limits);
} else if (pright <= limits->p1.x) {
/* Projection of the edge to the left of the box:
* replace with the left side of the box (clipped top/bottom) */
_add_edge (polygon, &limits->p1, &bot_left, top_y, bot_y, dir);
assert_last_edge_is_valid (polygon, limits);
} else if (limits->p2.x <= pleft) {
/* Projection of the edge to the right of the box:
* replace with the right side of the box (clipped top/bottom) */
_add_edge (polygon, &top_right, &limits->p2, top_y, bot_y, dir);
assert_last_edge_is_valid (polygon, limits);
} else {
/* The edge and the box intersect in a generic way */
cairo_fixed_t left_y, right_y;
cairo_bool_t top_left_to_bottom_right;
/*
* The edge intersects the lines corresponding to the left
* and right sides of the limit box at left_y and right_y,
* but we need to add edges for the range from top_y to
* bot_y.
*
* For both intersections, there are three cases:
*
* 1) It is outside the vertical range of the limit
* box. In this case we can simply further clip the
* edge we will be emitting (i.e. restrict its
* top/bottom limits to those of the limit box).
*
* 2) It is inside the vertical range of the limit
* box. In this case, we need to add the vertical edge
* connecting the correct vertex to the intersection,
* in order to preserve the winding count.
*
* 3) It is exactly on the box. In this case, do nothing.
*
* These operations restrict the active range (stored in
* top_y/bot_y) so that the p1-p2 edge is completely
* inside the box if it is clipped to this vertical range.
*/
top_left_to_bottom_right = (p1->x <= p2->x) == (p1->y <= p2->y);
if (top_left_to_bottom_right) {
if (pleft >= limits->p1.x) {
left_y = top_y;
} else {
left_y = _cairo_edge_compute_intersection_y_for_x (p1, p2,
limits->p1.x);
if (_cairo_edge_compute_intersection_x_for_y (p1, p2, left_y) < limits->p1.x)
left_y++;
}
left_y = MIN (left_y, bot_y);
if (top_y < left_y) {
_add_edge (polygon, &limits->p1, &bot_left,
top_y, left_y, dir);
assert_last_edge_is_valid (polygon, limits);
top_y = left_y;
}
if (pright <= limits->p2.x) {
right_y = bot_y;
} else {
right_y = _cairo_edge_compute_intersection_y_for_x (p1, p2,
limits->p2.x);
if (_cairo_edge_compute_intersection_x_for_y (p1, p2, right_y) > limits->p2.x)
right_y--;
}
right_y = MAX (right_y, top_y);
if (bot_y > right_y) {
_add_edge (polygon, &top_right, &limits->p2,
right_y, bot_y, dir);
assert_last_edge_is_valid (polygon, limits);
bot_y = right_y;
}
} else {
if (pright <= limits->p2.x) {
right_y = top_y;
} else {
right_y = _cairo_edge_compute_intersection_y_for_x (p1, p2,
limits->p2.x);
if (_cairo_edge_compute_intersection_x_for_y (p1, p2, right_y) > limits->p2.x)
right_y++;
}
right_y = MIN (right_y, bot_y);
if (top_y < right_y) {
_add_edge (polygon, &top_right, &limits->p2,
top_y, right_y, dir);
assert_last_edge_is_valid (polygon, limits);
top_y = right_y;
}
if (pleft >= limits->p1.x) {
left_y = bot_y;
} else {
left_y = _cairo_edge_compute_intersection_y_for_x (p1, p2,
limits->p1.x);
if (_cairo_edge_compute_intersection_x_for_y (p1, p2, left_y) < limits->p1.x)
left_y--;
}
left_y = MAX (left_y, top_y);
if (bot_y > left_y) {
_add_edge (polygon, &limits->p1, &bot_left,
left_y, bot_y, dir);
assert_last_edge_is_valid (polygon, limits);
bot_y = left_y;
}
}
if (top_y != bot_y) {
_add_edge (polygon, p1, p2, top_y, bot_y, dir);
assert_last_edge_is_valid (polygon, limits);
}
}
}
}
static void
_cairo_polygon_add_edge (cairo_polygon_t *polygon,
const cairo_point_t *p1,
const cairo_point_t *p2,
int dir)
{
/* drop horizontal edges */
if (p1->y == p2->y)
return;
if (p1->y > p2->y) {
const cairo_point_t *t;
t = p1, p1 = p2, p2 = t;
dir = -dir;
}
if (polygon->num_limits) {
if (p2->y <= polygon->limit.p1.y)
return;
if (p1->y >= polygon->limit.p2.y)
return;
_add_clipped_edge (polygon, p1, p2, p1->y, p2->y, dir);
} else
_add_edge (polygon, p1, p2, p1->y, p2->y, dir);
}
cairo_status_t
_cairo_polygon_add_external_edge (void *polygon,
const cairo_point_t *p1,
const cairo_point_t *p2)
{
_cairo_polygon_add_edge (polygon, p1, p2, 1);
return _cairo_polygon_status (polygon);
}
cairo_status_t
_cairo_polygon_add_line (cairo_polygon_t *polygon,
const cairo_line_t *line,
int top, int bottom,
int dir)
{
/* drop horizontal edges */
if (line->p1.y == line->p2.y)
return CAIRO_STATUS_SUCCESS;
if (bottom <= top)
return CAIRO_STATUS_SUCCESS;
if (polygon->num_limits) {
if (line->p2.y <= polygon->limit.p1.y)
return CAIRO_STATUS_SUCCESS;
if (line->p1.y >= polygon->limit.p2.y)
return CAIRO_STATUS_SUCCESS;
_add_clipped_edge (polygon, &line->p1, &line->p2, top, bottom, dir);
} else
_add_edge (polygon, &line->p1, &line->p2, top, bottom, dir);
return polygon->status;
}
cairo_status_t
_cairo_polygon_add_contour (cairo_polygon_t *polygon,
const cairo_contour_t *contour)
{
const struct _cairo_contour_chain *chain;
const cairo_point_t *prev = NULL;
int i;
if (contour->chain.num_points <= 1)
return CAIRO_INT_STATUS_SUCCESS;
prev = &contour->chain.points[0];
for (chain = &contour->chain; chain; chain = chain->next) {
for (i = 0; i < chain->num_points; i++) {
_cairo_polygon_add_edge (polygon, prev, &chain->points[i],
contour->direction);
prev = &chain->points[i];
}
}
return polygon->status;
}
void
_cairo_polygon_translate (cairo_polygon_t *polygon, int dx, int dy)
{
int n;
dx = _cairo_fixed_from_int (dx);
dy = _cairo_fixed_from_int (dy);
polygon->extents.p1.x += dx;
polygon->extents.p2.x += dx;
polygon->extents.p1.y += dy;
polygon->extents.p2.y += dy;
for (n = 0; n < polygon->num_edges; n++) {
cairo_edge_t *e = &polygon->edges[n];
e->top += dy;
e->bottom += dy;
e->line.p1.x += dx;
e->line.p2.x += dx;
e->line.p1.y += dy;
e->line.p2.y += dy;
}
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Carl D. Worth <cworth@redhat.com>
*/
#ifndef CAIRO_PRIVATE_H
#define CAIRO_PRIVATE_H
#include "cairo-types-private.h"
#include "cairo-reference-count-private.h"
CAIRO_BEGIN_DECLS
struct _cairo {
cairo_reference_count_t ref_count;
cairo_status_t status;
cairo_user_data_array_t user_data;
const cairo_backend_t *backend;
};
cairo_private cairo_t *
_cairo_create_in_error (cairo_status_t status);
cairo_private void
_cairo_init (cairo_t *cr,
const cairo_backend_t *backend);
cairo_private void
_cairo_fini (cairo_t *cr);
CAIRO_END_DECLS
#endif /* CAIRO_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-raster-source-pattern.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2011 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-pattern-private.h"
/**
* SECTION:cairo-raster-source
* @Title: Raster Sources
* @Short_Description: Supplying arbitrary image data
* @See_Also: #cairo_pattern_t
*
* The raster source provides the ability to supply arbitrary pixel data
* whilst rendering. The pixels are queried at the time of rasterisation
* by means of user callback functions, allowing for the ultimate
* flexibility. For example, in handling compressed image sources, you
* may keep a MRU cache of decompressed images and decompress sources on the
* fly and discard old ones to conserve memory.
*
* For the raster source to be effective, you must at least specify
* the acquire and release callbacks which are used to retrieve the pixel
* data for the region of interest and demark when it can be freed afterwards.
* Other callbacks are provided for when the pattern is copied temporarily
* during rasterisation, or more permanently as a snapshot in order to keep
* the pixel data available for printing.
**/
cairo_surface_t *
_cairo_raster_source_pattern_acquire (const cairo_pattern_t *abstract_pattern,
cairo_surface_t *target,
const cairo_rectangle_int_t *extents)
{
cairo_raster_source_pattern_t *pattern =
(cairo_raster_source_pattern_t *) abstract_pattern;
if (pattern->acquire == NULL)
return NULL;
if (extents == NULL)
extents = &pattern->extents;
return pattern->acquire (&pattern->base, pattern->user_data,
target, extents);
}
void
_cairo_raster_source_pattern_release (const cairo_pattern_t *abstract_pattern,
cairo_surface_t *surface)
{
cairo_raster_source_pattern_t *pattern =
(cairo_raster_source_pattern_t *) abstract_pattern;
if (pattern->release == NULL)
return;
pattern->release (&pattern->base, pattern->user_data, surface);
}
cairo_status_t
_cairo_raster_source_pattern_init_copy (cairo_pattern_t *abstract_pattern,
const cairo_pattern_t *other)
{
cairo_raster_source_pattern_t *pattern =
(cairo_raster_source_pattern_t *) abstract_pattern;
cairo_status_t status;
VG (VALGRIND_MAKE_MEM_UNDEFINED (pattern, sizeof (cairo_raster_source_pattern_t)));
memcpy(pattern, other, sizeof (cairo_raster_source_pattern_t));
status = CAIRO_STATUS_SUCCESS;
if (pattern->copy)
status = pattern->copy (&pattern->base, pattern->user_data, other);
return status;
}
cairo_status_t
_cairo_raster_source_pattern_snapshot (cairo_pattern_t *abstract_pattern)
{
cairo_raster_source_pattern_t *pattern =
(cairo_raster_source_pattern_t *) abstract_pattern;
if (pattern->snapshot == NULL)
return CAIRO_STATUS_SUCCESS;
return pattern->snapshot (&pattern->base, pattern->user_data);
}
void
_cairo_raster_source_pattern_finish (cairo_pattern_t *abstract_pattern)
{
cairo_raster_source_pattern_t *pattern =
(cairo_raster_source_pattern_t *) abstract_pattern;
if (pattern->finish == NULL)
return;
pattern->finish (&pattern->base, pattern->user_data);
}
/* Public interface */
/**
* cairo_pattern_create_raster_source:
* @user_data: the user data to be passed to all callbacks
* @content: content type for the pixel data that will be returned. Knowing
* the content type ahead of time is used for analysing the operation and
* picking the appropriate rendering path.
* @width: maximum size of the sample area
* @height: maximum size of the sample area
*
* Creates a new user pattern for providing pixel data.
*
* Use the setter functions to associate callbacks with the returned
* pattern. The only mandatory callback is acquire.
*
* Return value: a newly created #cairo_pattern_t. Free with
* cairo_pattern_destroy() when you are done using it.
*
* Since: 1.12
**/
cairo_pattern_t *
cairo_pattern_create_raster_source (void *user_data,
cairo_content_t content,
int width, int height)
{
cairo_raster_source_pattern_t *pattern;
CAIRO_MUTEX_INITIALIZE ();
if (width < 0 || height < 0)
return _cairo_pattern_create_in_error (CAIRO_STATUS_INVALID_SIZE);
if (! CAIRO_CONTENT_VALID (content))
return _cairo_pattern_create_in_error (CAIRO_STATUS_INVALID_CONTENT);
pattern = calloc (1, sizeof (*pattern));
if (unlikely (pattern == NULL))
return _cairo_pattern_create_in_error (CAIRO_STATUS_NO_MEMORY);
_cairo_pattern_init (&pattern->base,
CAIRO_PATTERN_TYPE_RASTER_SOURCE);
CAIRO_REFERENCE_COUNT_INIT (&pattern->base.ref_count, 1);
pattern->content = content;
pattern->extents.x = 0;
pattern->extents.y = 0;
pattern->extents.width = width;
pattern->extents.height = height;
pattern->user_data = user_data;
return &pattern->base;
}
/**
* cairo_raster_source_pattern_set_callback_data:
* @pattern: the pattern to update
* @data: the user data to be passed to all callbacks
*
* Updates the user data that is provided to all callbacks.
*
* Since: 1.12
**/
void
cairo_raster_source_pattern_set_callback_data (cairo_pattern_t *abstract_pattern,
void *data)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
pattern->user_data = data;
}
/**
* cairo_raster_source_pattern_get_callback_data:
* @pattern: the pattern to update
*
* Queries the current user data.
*
* Return value: the current user-data passed to each callback
*
* Since: 1.12
**/
void *
cairo_raster_source_pattern_get_callback_data (cairo_pattern_t *abstract_pattern)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return NULL;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
return pattern->user_data;
}
/**
* cairo_raster_source_pattern_set_acquire:
* @pattern: the pattern to update
* @acquire: acquire callback
* @release: release callback
*
* Specifies the callbacks used to generate the image surface for a rendering
* operation (acquire) and the function used to cleanup that surface afterwards.
*
* The @acquire callback should create a surface (preferably an image
* surface created to match the target using
* cairo_surface_create_similar_image()) that defines at least the region
* of interest specified by extents. The surface is allowed to be the entire
* sample area, but if it does contain a subsection of the sample area,
* the surface extents should be provided by setting the device offset (along
* with its width and height) using cairo_surface_set_device_offset().
*
* Since: 1.12
**/
void
cairo_raster_source_pattern_set_acquire (cairo_pattern_t *abstract_pattern,
cairo_raster_source_acquire_func_t acquire,
cairo_raster_source_release_func_t release)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
pattern->acquire = acquire;
pattern->release = release;
}
/**
* cairo_raster_source_pattern_get_acquire:
* @pattern: the pattern to query
* @acquire: return value for the current acquire callback
* @release: return value for the current release callback
*
* Queries the current acquire and release callbacks.
*
* Since: 1.12
**/
void
cairo_raster_source_pattern_get_acquire (cairo_pattern_t *abstract_pattern,
cairo_raster_source_acquire_func_t *acquire,
cairo_raster_source_release_func_t *release)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
if (acquire)
*acquire = pattern->acquire;
if (release)
*release = pattern->release;
}
/**
* cairo_raster_source_pattern_set_snapshot:
* @pattern: the pattern to update
* @snapshot: snapshot callback
*
* Sets the callback that will be used whenever a snapshot is taken of the
* pattern, that is whenever the current contents of the pattern should be
* preserved for later use. This is typically invoked whilst printing.
*
* Since: 1.12
**/
void
cairo_raster_source_pattern_set_snapshot (cairo_pattern_t *abstract_pattern,
cairo_raster_source_snapshot_func_t snapshot)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
pattern->snapshot = snapshot;
}
/**
* cairo_raster_source_pattern_get_snapshot:
* @pattern: the pattern to query
*
* Queries the current snapshot callback.
*
* Return value: the current snapshot callback
*
* Since: 1.12
**/
cairo_raster_source_snapshot_func_t
cairo_raster_source_pattern_get_snapshot (cairo_pattern_t *abstract_pattern)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return NULL;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
return pattern->snapshot;
}
/**
* cairo_raster_source_pattern_set_copy:
* @pattern: the pattern to update
* @copy: the copy callback
*
* Updates the copy callback which is used whenever a temporary copy of the
* pattern is taken.
*
* Since: 1.12
**/
void
cairo_raster_source_pattern_set_copy (cairo_pattern_t *abstract_pattern,
cairo_raster_source_copy_func_t copy)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
pattern->copy = copy;
}
/**
* cairo_raster_source_pattern_get_copy:
* @pattern: the pattern to query
*
* Queries the current copy callback.
*
* Return value: the current copy callback
*
* Since: 1.12
**/
cairo_raster_source_copy_func_t
cairo_raster_source_pattern_get_copy (cairo_pattern_t *abstract_pattern)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return NULL;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
return pattern->copy;
}
/**
* cairo_raster_source_pattern_set_finish:
* @pattern: the pattern to update
* @finish: the finish callback
*
* Updates the finish callback which is used whenever a pattern (or a copy
* thereof) will no longer be used.
*
* Since: 1.12
**/
void
cairo_raster_source_pattern_set_finish (cairo_pattern_t *abstract_pattern,
cairo_raster_source_finish_func_t finish)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
pattern->finish = finish;
}
/**
* cairo_raster_source_pattern_get_finish:
* @pattern: the pattern to query
*
* Queries the current finish callback.
*
* Return value: the current finish callback
*
* Since: 1.12
**/
cairo_raster_source_finish_func_t
cairo_raster_source_pattern_get_finish (cairo_pattern_t *abstract_pattern)
{
cairo_raster_source_pattern_t *pattern;
if (abstract_pattern->type != CAIRO_PATTERN_TYPE_RASTER_SOURCE)
return NULL;
pattern = (cairo_raster_source_pattern_t *) abstract_pattern;
return pattern->finish;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-recording-surface-inline.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Kristian Høgsberg <krh@redhat.com>
* Adrian Johnson <ajohnson@redneon.com>
*/
#ifndef CAIRO_RECORDING_SURFACE_INLINE_H
#define CAIRO_RECORDING_SURFACE_INLINE_H
#include "cairo-recording-surface-private.h"
static inline cairo_bool_t
_cairo_recording_surface_get_bounds (cairo_surface_t *surface,
cairo_rectangle_t *extents)
{
cairo_recording_surface_t *recording = (cairo_recording_surface_t *)surface;
if (recording->unbounded)
return FALSE;
*extents = recording->extents_pixels;
return TRUE;
}
/**
* _cairo_surface_is_recording:
* @surface: a #cairo_surface_t
*
* Checks if a surface is a #cairo_recording_surface_t
*
* Return value: %TRUE if the surface is a recording surface
**/
static inline cairo_bool_t
_cairo_surface_is_recording (const cairo_surface_t *surface)
{
return surface->backend->type == CAIRO_SURFACE_TYPE_RECORDING;
}
#endif /* CAIRO_RECORDING_SURFACE_INLINE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-recording-surface-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Kristian Høgsberg <krh@redhat.com>
* Adrian Johnson <ajohnson@redneon.com>
*/
#ifndef CAIRO_RECORDING_SURFACE_H
#define CAIRO_RECORDING_SURFACE_H
#include "cairoint.h"
#include "cairo-path-fixed-private.h"
#include "cairo-pattern-private.h"
#include "cairo-surface-backend-private.h"
typedef enum {
/* The 5 basic drawing operations. */
CAIRO_COMMAND_PAINT,
CAIRO_COMMAND_MASK,
CAIRO_COMMAND_STROKE,
CAIRO_COMMAND_FILL,
CAIRO_COMMAND_SHOW_TEXT_GLYPHS,
/* cairo_tag_begin()/cairo_tag_end() */
CAIRO_COMMAND_TAG,
} cairo_command_type_t;
typedef enum {
CAIRO_RECORDING_REGION_ALL,
CAIRO_RECORDING_REGION_NATIVE,
CAIRO_RECORDING_REGION_IMAGE_FALLBACK
} cairo_recording_region_type_t;
typedef struct _cairo_command_header {
cairo_command_type_t type;
cairo_recording_region_type_t region;
cairo_operator_t op;
cairo_rectangle_int_t extents;
cairo_clip_t *clip;
int index;
struct _cairo_command_header *chain;
} cairo_command_header_t;
typedef struct _cairo_command_paint {
cairo_command_header_t header;
cairo_pattern_union_t source;
} cairo_command_paint_t;
typedef struct _cairo_command_mask {
cairo_command_header_t header;
cairo_pattern_union_t source;
cairo_pattern_union_t mask;
} cairo_command_mask_t;
typedef struct _cairo_command_stroke {
cairo_command_header_t header;
cairo_pattern_union_t source;
cairo_path_fixed_t path;
cairo_stroke_style_t style;
cairo_matrix_t ctm;
cairo_matrix_t ctm_inverse;
double tolerance;
cairo_antialias_t antialias;
} cairo_command_stroke_t;
typedef struct _cairo_command_fill {
cairo_command_header_t header;
cairo_pattern_union_t source;
cairo_path_fixed_t path;
cairo_fill_rule_t fill_rule;
double tolerance;
cairo_antialias_t antialias;
} cairo_command_fill_t;
typedef struct _cairo_command_show_text_glyphs {
cairo_command_header_t header;
cairo_pattern_union_t source;
char *utf8;
int utf8_len;
cairo_glyph_t *glyphs;
unsigned int num_glyphs;
cairo_text_cluster_t *clusters;
int num_clusters;
cairo_text_cluster_flags_t cluster_flags;
cairo_scaled_font_t *scaled_font;
} cairo_command_show_text_glyphs_t;
typedef struct _cairo_command_tag {
cairo_command_header_t header;
cairo_bool_t begin;
char *tag_name;
char *attributes;
cairo_pattern_union_t source;
cairo_stroke_style_t style;
cairo_matrix_t ctm;
cairo_matrix_t ctm_inverse;
} cairo_command_tag_t;
typedef union _cairo_command {
cairo_command_header_t header;
cairo_command_paint_t paint;
cairo_command_mask_t mask;
cairo_command_stroke_t stroke;
cairo_command_fill_t fill;
cairo_command_show_text_glyphs_t show_text_glyphs;
cairo_command_tag_t tag;
} cairo_command_t;
typedef struct _cairo_recording_surface {
cairo_surface_t base;
/* A recording-surface is logically unbounded, but when used as a
* source we need to render it to an image, so we need a size at
* which to create that image. */
cairo_rectangle_t extents_pixels;
cairo_rectangle_int_t extents;
cairo_bool_t unbounded;
cairo_array_t commands;
unsigned int *indices;
unsigned int num_indices;
cairo_bool_t optimize_clears;
cairo_bool_t has_bilevel_alpha;
cairo_bool_t has_only_op_over;
struct bbtree {
cairo_box_t extents;
struct bbtree *left, *right;
cairo_command_header_t *chain;
} bbtree;
} cairo_recording_surface_t;
slim_hidden_proto (cairo_recording_surface_create);
cairo_private cairo_int_status_t
_cairo_recording_surface_get_path (cairo_surface_t *surface,
cairo_path_fixed_t *path);
cairo_private cairo_status_t
_cairo_recording_surface_replay_one (cairo_recording_surface_t *surface,
long unsigned index,
cairo_surface_t *target);
cairo_private cairo_status_t
_cairo_recording_surface_replay (cairo_surface_t *surface,
cairo_surface_t *target);
cairo_private cairo_status_t
_cairo_recording_surface_replay_with_clip (cairo_surface_t *surface,
const cairo_matrix_t *surface_transform,
cairo_surface_t *target,
const cairo_clip_t *target_clip);
cairo_private cairo_status_t
_cairo_recording_surface_replay_and_create_regions (cairo_surface_t *surface,
const cairo_matrix_t *surface_transform,
cairo_surface_t *target,
cairo_bool_t surface_is_unbounded);
cairo_private cairo_status_t
_cairo_recording_surface_replay_region (cairo_surface_t *surface,
const cairo_rectangle_int_t *surface_extents,
cairo_surface_t *target,
cairo_recording_region_type_t region);
cairo_private cairo_status_t
_cairo_recording_surface_get_bbox (cairo_recording_surface_t *recording,
cairo_box_t *bbox,
const cairo_matrix_t *transform);
cairo_private cairo_status_t
_cairo_recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
cairo_box_t *bbox,
const cairo_matrix_t *transform);
cairo_private cairo_bool_t
_cairo_recording_surface_has_only_bilevel_alpha (cairo_recording_surface_t *surface);
cairo_private cairo_bool_t
_cairo_recording_surface_has_only_op_over (cairo_recording_surface_t *surface);
#endif /* CAIRO_RECORDING_SURFACE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-recording-surface.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc
* Copyright © 2007 Adrian Johnson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Kristian Høgsberg <krh@redhat.com>
* Carl Worth <cworth@cworth.org>
* Adrian Johnson <ajohnson@redneon.com>
*/
/**
* SECTION:cairo-recording
* @Title: Recording Surfaces
* @Short_Description: Records all drawing operations
* @See_Also: #cairo_surface_t
*
* A recording surface is a surface that records all drawing operations at
* the highest level of the surface backend interface, (that is, the
* level of paint, mask, stroke, fill, and show_text_glyphs). The recording
* surface can then be "replayed" against any target surface by using it
* as a source surface.
*
* If you want to replay a surface so that the results in target will be
* identical to the results that would have been obtained if the original
* operations applied to the recording surface had instead been applied to the
* target surface, you can use code like this:
* <informalexample><programlisting>
* cairo_t *cr;
*
* cr = cairo_create (target);
* cairo_set_source_surface (cr, recording_surface, 0.0, 0.0);
* cairo_paint (cr);
* cairo_destroy (cr);
* </programlisting></informalexample>
*
* A recording surface is logically unbounded, i.e. it has no implicit constraint
* on the size of the drawing surface. However, in practice this is rarely
* useful as you wish to replay against a particular target surface with
* known bounds. For this case, it is more efficient to specify the target
* extents to the recording surface upon creation.
*
* The recording phase of the recording surface is careful to snapshot all
* necessary objects (paths, patterns, etc.), in order to achieve
* accurate replay. The efficiency of the recording surface could be
* improved by improving the implementation of snapshot for the
* various objects. For example, it would be nice to have a
* copy-on-write implementation for _cairo_surface_snapshot.
**/
#include "cairoint.h"
#include "cairo-array-private.h"
#include "cairo-analysis-surface-private.h"
#include "cairo-clip-private.h"
#include "cairo-combsort-inline.h"
#include "cairo-composite-rectangles-private.h"
#include "cairo-default-context-private.h"
#include "cairo-error-private.h"
#include "cairo-image-surface-private.h"
#include "cairo-recording-surface-inline.h"
#include "cairo-surface-snapshot-inline.h"
#include "cairo-surface-wrapper-private.h"
#include "cairo-traps-private.h"
typedef enum {
CAIRO_RECORDING_REPLAY,
CAIRO_RECORDING_CREATE_REGIONS
} cairo_recording_replay_type_t;
static const cairo_surface_backend_t cairo_recording_surface_backend;
/**
* CAIRO_HAS_RECORDING_SURFACE:
*
* Defined if the recording surface backend is available.
* The recording surface backend is always built in.
* This macro was added for completeness in cairo 1.10.
*
* Since: 1.10
**/
/* Currently all recording surfaces do have a size which should be passed
* in as the maximum size of any target surface against which the
* recording-surface will ever be replayed.
*
* XXX: The naming of "pixels" in the size here is a misnomer. It's
* actually a size in whatever device-space units are desired (again,
* according to the intended replay target).
*/
static int bbtree_left_or_right (struct bbtree *bbt,
const cairo_box_t *box)
{
int left, right;
if (bbt->left) {
cairo_box_t *e = &bbt->left->extents;
cairo_box_t b;
b.p1.x = MIN (e->p1.x, box->p1.x);
b.p1.y = MIN (e->p1.y, box->p1.y);
b.p2.x = MAX (e->p2.x, box->p2.x);
b.p2.y = MAX (e->p2.y, box->p2.y);
left = _cairo_fixed_integer_part (b.p2.x - b.p1.x) * _cairo_fixed_integer_part (b.p2.y - b.p1.y);
left -= _cairo_fixed_integer_part (e->p2.x - e->p1.x) * _cairo_fixed_integer_part (e->p2.y - e->p1.y);
} else
left = 0;
if (bbt->right) {
cairo_box_t *e = &bbt->right->extents;
cairo_box_t b;
b.p1.x = MIN (e->p1.x, box->p1.x);
b.p1.y = MIN (e->p1.y, box->p1.y);
b.p2.x = MAX (e->p2.x, box->p2.x);
b.p2.y = MAX (e->p2.y, box->p2.y);
right = _cairo_fixed_integer_part (b.p2.x - b.p1.x) * _cairo_fixed_integer_part (b.p2.y - b.p1.y);
right -= _cairo_fixed_integer_part (e->p2.x - e->p1.x) * _cairo_fixed_integer_part (e->p2.y - e->p1.y);
} else
right = 0;
return left <= right;
}
#define INVALID_CHAIN ((cairo_command_header_t *)-1)
static struct bbtree *
bbtree_new (const cairo_box_t *box, cairo_command_header_t *chain)
{
struct bbtree *bbt = _cairo_malloc (sizeof (*bbt));
if (bbt == NULL)
return NULL;
bbt->extents = *box;
bbt->left = bbt->right = NULL;
bbt->chain = chain;
return bbt;
}
static void
bbtree_init (struct bbtree *bbt, cairo_command_header_t *header)
{
_cairo_box_from_rectangle (&bbt->extents, &header->extents);
bbt->chain = header;
}
static cairo_status_t
bbtree_add (struct bbtree *bbt,
cairo_command_header_t *header,
const cairo_box_t *box)
{
if (box->p1.x < bbt->extents.p1.x || box->p1.y < bbt->extents.p1.y ||
box->p2.x > bbt->extents.p2.x || box->p2.y > bbt->extents.p2.y)
{
if (bbt->chain) {
if (bbtree_left_or_right (bbt, &bbt->extents)) {
if (bbt->left == NULL) {
bbt->left = bbtree_new (&bbt->extents, bbt->chain);
if (unlikely (bbt->left == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
} else
bbtree_add (bbt->left, bbt->chain, &bbt->extents);
} else {
if (bbt->right == NULL) {
bbt->right = bbtree_new (&bbt->extents, bbt->chain);
if (unlikely (bbt->right == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
} else
bbtree_add (bbt->right, bbt->chain, &bbt->extents);
}
bbt->chain = NULL;
}
bbt->extents.p1.x = MIN (bbt->extents.p1.x, box->p1.x);
bbt->extents.p1.y = MIN (bbt->extents.p1.y, box->p1.y);
bbt->extents.p2.x = MAX (bbt->extents.p2.x, box->p2.x);
bbt->extents.p2.y = MAX (bbt->extents.p2.y, box->p2.y);
}
if (box->p1.x == bbt->extents.p1.x && box->p1.y == bbt->extents.p1.y &&
box->p2.x == bbt->extents.p2.x && box->p2.y == bbt->extents.p2.y)
{
cairo_command_header_t *last = header;
while (last->chain) /* expected to be infrequent */
last = last->chain;
last->chain = bbt->chain;
bbt->chain = header;
return CAIRO_STATUS_SUCCESS;
}
if (bbtree_left_or_right (bbt, box)) {
if (bbt->left == NULL) {
bbt->left = bbtree_new (box, header);
if (unlikely (bbt->left == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
} else
return bbtree_add (bbt->left, header, box);
} else {
if (bbt->right == NULL) {
bbt->right = bbtree_new (box, header);
if (unlikely (bbt->right == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
} else
return bbtree_add (bbt->right, header, box);
}
return CAIRO_STATUS_SUCCESS;
}
static void bbtree_del (struct bbtree *bbt)
{
if (bbt->left)
bbtree_del (bbt->left);
if (bbt->right)
bbtree_del (bbt->right);
free (bbt);
}
static cairo_bool_t box_outside (const cairo_box_t *a, const cairo_box_t *b)
{
return
a->p1.x >= b->p2.x || a->p1.y >= b->p2.y ||
a->p2.x <= b->p1.x || a->p2.y <= b->p1.y;
}
static void
bbtree_foreach_mark_visible (struct bbtree *bbt,
const cairo_box_t *box,
unsigned int **indices)
{
cairo_command_header_t *chain;
for (chain = bbt->chain; chain; chain = chain->chain)
*(*indices)++ = chain->index;
if (bbt->left && ! box_outside (box, &bbt->left->extents))
bbtree_foreach_mark_visible (bbt->left, box, indices);
if (bbt->right && ! box_outside (box, &bbt->right->extents))
bbtree_foreach_mark_visible (bbt->right, box, indices);
}
static inline int intcmp (const unsigned int a, const unsigned int b)
{
return a - b;
}
CAIRO_COMBSORT_DECLARE (sort_indices, unsigned int, intcmp)
static inline int sizecmp (unsigned int a, unsigned int b, cairo_command_header_t **elements)
{
const cairo_rectangle_int_t *r;
r = &elements[a]->extents;
a = r->width * r->height;
r = &elements[b]->extents;
b = r->width * r->height;
return b - a;
}
CAIRO_COMBSORT_DECLARE_WITH_DATA (sort_commands, unsigned int, sizecmp)
static void
_cairo_recording_surface_destroy_bbtree (cairo_recording_surface_t *surface)
{
cairo_command_t **elements;
int i, num_elements;
if (surface->bbtree.chain == INVALID_CHAIN)
return;
if (surface->bbtree.left) {
bbtree_del (surface->bbtree.left);
surface->bbtree.left = NULL;
}
if (surface->bbtree.right) {
bbtree_del (surface->bbtree.right);
surface->bbtree.right = NULL;
}
elements = _cairo_array_index (&surface->commands, 0);
num_elements = surface->commands.num_elements;
for (i = 0; i < num_elements; i++)
elements[i]->header.chain = NULL;
surface->bbtree.chain = INVALID_CHAIN;
}
static cairo_status_t
_cairo_recording_surface_create_bbtree (cairo_recording_surface_t *surface)
{
cairo_command_t **elements = _cairo_array_index (&surface->commands, 0);
unsigned int *indices;
cairo_status_t status;
unsigned int i, count;
count = surface->commands.num_elements;
if (count > surface->num_indices) {
free (surface->indices);
surface->indices = _cairo_malloc_ab (count, sizeof (int));
if (unlikely (surface->indices == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
surface->num_indices = count;
}
indices = surface->indices;
for (i = 0; i < count; i++)
indices[i] = i;
sort_commands (indices, count, elements);
bbtree_init (&surface->bbtree, &elements[indices[0]]->header);
for (i = 1; i < count; i++) {
cairo_command_header_t *header = &elements[indices[i]]->header;
cairo_box_t box;
_cairo_box_from_rectangle (&box, &header->extents);
status = bbtree_add (&surface->bbtree, header, &box);
if (unlikely (status))
goto cleanup;
}
return CAIRO_STATUS_SUCCESS;
cleanup:
bbtree_del (&surface->bbtree);
return status;
}
/**
* cairo_recording_surface_create:
* @content: the content of the recording surface
* @extents: the extents to record in pixels, can be %NULL to record
* unbounded operations.
*
* Creates a recording-surface which can be used to record all drawing operations
* at the highest level (that is, the level of paint, mask, stroke, fill
* and show_text_glyphs). The recording surface can then be "replayed" against
* any target surface by using it as a source to drawing operations.
*
* The recording phase of the recording surface is careful to snapshot all
* necessary objects (paths, patterns, etc.), in order to achieve
* accurate replay.
*
* Return value: a pointer to the newly created surface. The caller
* owns the surface and should call cairo_surface_destroy() when done
* with it.
*
* Since: 1.10
**/
cairo_surface_t *
cairo_recording_surface_create (cairo_content_t content,
const cairo_rectangle_t *extents)
{
cairo_recording_surface_t *surface;
surface = _cairo_malloc (sizeof (cairo_recording_surface_t));
if (unlikely (surface == NULL))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
_cairo_surface_init (&surface->base,
&cairo_recording_surface_backend,
NULL, /* device */
content,
TRUE); /* is_vector */
surface->unbounded = TRUE;
/* unbounded -> 'infinite' extents */
if (extents != NULL) {
surface->extents_pixels = *extents;
/* XXX check for overflow */
surface->extents.x = floor (extents->x);
surface->extents.y = floor (extents->y);
surface->extents.width = ceil (extents->x + extents->width) - surface->extents.x;
surface->extents.height = ceil (extents->y + extents->height) - surface->extents.y;
surface->unbounded = FALSE;
}
_cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
surface->base.is_clear = TRUE;
surface->bbtree.left = surface->bbtree.right = NULL;
surface->bbtree.chain = INVALID_CHAIN;
surface->indices = NULL;
surface->num_indices = 0;
surface->optimize_clears = TRUE;
surface->has_bilevel_alpha = FALSE;
surface->has_only_op_over = FALSE;
return &surface->base;
}
slim_hidden_def (cairo_recording_surface_create);
static cairo_surface_t *
_cairo_recording_surface_create_similar (void *abstract_surface,
cairo_content_t content,
int width,
int height)
{
cairo_rectangle_t extents;
extents.x = extents.y = 0;
extents.width = width;
extents.height = height;
return cairo_recording_surface_create (content, &extents);
}
static cairo_status_t
_cairo_recording_surface_finish (void *abstract_surface)
{
cairo_recording_surface_t *surface = abstract_surface;
cairo_command_t **elements;
int i, num_elements;
num_elements = surface->commands.num_elements;
elements = _cairo_array_index (&surface->commands, 0);
for (i = 0; i < num_elements; i++) {
cairo_command_t *command = elements[i];
switch (command->header.type) {
case CAIRO_COMMAND_PAINT:
_cairo_pattern_fini (&command->paint.source.base);
break;
case CAIRO_COMMAND_MASK:
_cairo_pattern_fini (&command->mask.source.base);
_cairo_pattern_fini (&command->mask.mask.base);
break;
case CAIRO_COMMAND_STROKE:
_cairo_pattern_fini (&command->stroke.source.base);
_cairo_path_fixed_fini (&command->stroke.path);
_cairo_stroke_style_fini (&command->stroke.style);
break;
case CAIRO_COMMAND_FILL:
_cairo_pattern_fini (&command->fill.source.base);
_cairo_path_fixed_fini (&command->fill.path);
break;
case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
_cairo_pattern_fini (&command->show_text_glyphs.source.base);
free (command->show_text_glyphs.utf8);
free (command->show_text_glyphs.glyphs);
free (command->show_text_glyphs.clusters);
cairo_scaled_font_destroy (command->show_text_glyphs.scaled_font);
break;
case CAIRO_COMMAND_TAG:
free (command->tag.tag_name);
if (command->tag.begin) {
free (command->tag.attributes);
_cairo_pattern_fini (&command->tag.source.base);
_cairo_stroke_style_fini (&command->tag.style);
}
break;
default:
ASSERT_NOT_REACHED;
}
_cairo_clip_destroy (command->header.clip);
free (command);
}
_cairo_array_fini (&surface->commands);
if (surface->bbtree.left)
bbtree_del (surface->bbtree.left);
if (surface->bbtree.right)
bbtree_del (surface->bbtree.right);
free (surface->indices);
return CAIRO_STATUS_SUCCESS;
}
struct proxy {
cairo_surface_t base;
cairo_surface_t *image;
};
static cairo_status_t
proxy_acquire_source_image (void *abstract_surface,
cairo_image_surface_t **image_out,
void **image_extra)
{
struct proxy *proxy = abstract_surface;
return _cairo_surface_acquire_source_image (proxy->image, image_out, image_extra);
}
static void
proxy_release_source_image (void *abstract_surface,
cairo_image_surface_t *image,
void *image_extra)
{
struct proxy *proxy = abstract_surface;
_cairo_surface_release_source_image (proxy->image, image, image_extra);
}
static cairo_status_t
proxy_finish (void *abstract_surface)
{
return CAIRO_STATUS_SUCCESS;
}
static const cairo_surface_backend_t proxy_backend = {
CAIRO_INTERNAL_SURFACE_TYPE_NULL,
proxy_finish,
NULL,
NULL, /* create similar */
NULL, /* create similar image */
NULL, /* map to image */
NULL, /* unmap image */
_cairo_surface_default_source,
proxy_acquire_source_image,
proxy_release_source_image,
};
static cairo_surface_t *
attach_proxy (cairo_surface_t *source,
cairo_surface_t *image)
{
struct proxy *proxy;
proxy = _cairo_malloc (sizeof (*proxy));
if (unlikely (proxy == NULL))
return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
_cairo_surface_init (&proxy->base, &proxy_backend, NULL, image->content, FALSE);
proxy->image = image;
_cairo_surface_attach_snapshot (source, &proxy->base, NULL);
return &proxy->base;
}
static void
detach_proxy (cairo_surface_t *source,
cairo_surface_t *proxy)
{
cairo_surface_finish (proxy);
cairo_surface_destroy (proxy);
}
static cairo_surface_t *
get_proxy (cairo_surface_t *proxy)
{
return ((struct proxy *)proxy)->image;
}
static cairo_status_t
_cairo_recording_surface_acquire_source_image (void *abstract_surface,
cairo_image_surface_t **image_out,
void **image_extra)
{
cairo_recording_surface_t *surface = abstract_surface;
cairo_surface_t *image, *proxy;
cairo_status_t status;
proxy = _cairo_surface_has_snapshot (abstract_surface, &proxy_backend);
if (proxy != NULL) {
*image_out = (cairo_image_surface_t *)
cairo_surface_reference (get_proxy (proxy));
*image_extra = NULL;
return CAIRO_STATUS_SUCCESS;
}
assert (! surface->unbounded);
image = _cairo_image_surface_create_with_content (surface->base.content,
surface->extents.width,
surface->extents.height);
if (unlikely (image->status))
return image->status;
/* Handle recursion by returning future reads from the current image */
proxy = attach_proxy (abstract_surface, image);
status = _cairo_recording_surface_replay (&surface->base, image);
detach_proxy (abstract_surface, proxy);
if (unlikely (status)) {
cairo_surface_destroy (image);
return status;
}
*image_out = (cairo_image_surface_t *) image;
*image_extra = NULL;
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_recording_surface_release_source_image (void *abstract_surface,
cairo_image_surface_t *image,
void *image_extra)
{
cairo_surface_destroy (&image->base);
}
static cairo_status_t
_command_init (cairo_recording_surface_t *surface,
cairo_command_header_t *command,
cairo_command_type_t type,
cairo_operator_t op,
cairo_composite_rectangles_t *composite)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
command->type = type;
command->op = op;
command->region = CAIRO_RECORDING_REGION_ALL;
command->extents = composite->unbounded;
command->chain = NULL;
command->index = surface->commands.num_elements;
/* steal the clip */
command->clip = NULL;
if (! _cairo_composite_rectangles_can_reduce_clip (composite,
composite->clip))
{
command->clip = composite->clip;
composite->clip = NULL;
}
return status;
}
static void
_cairo_recording_surface_break_self_copy_loop (cairo_recording_surface_t *surface)
{
cairo_surface_flush (&surface->base);
}
static cairo_status_t
_cairo_recording_surface_commit (cairo_recording_surface_t *surface,
cairo_command_header_t *command)
{
_cairo_recording_surface_break_self_copy_loop (surface);
return _cairo_array_append (&surface->commands, &command);
}
static void
_cairo_recording_surface_reset (cairo_recording_surface_t *surface)
{
/* Reset the commands and temporaries */
_cairo_recording_surface_finish (surface);
surface->bbtree.left = surface->bbtree.right = NULL;
surface->bbtree.chain = INVALID_CHAIN;
surface->indices = NULL;
surface->num_indices = 0;
_cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
}
static cairo_int_status_t
_cairo_recording_surface_paint (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_clip_t *clip)
{
cairo_status_t status;
cairo_recording_surface_t *surface = abstract_surface;
cairo_command_paint_t *command;
cairo_composite_rectangles_t composite;
TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
if (op == CAIRO_OPERATOR_CLEAR && clip == NULL) {
if (surface->optimize_clears) {
_cairo_recording_surface_reset (surface);
return CAIRO_STATUS_SUCCESS;
}
}
if (clip == NULL && surface->optimize_clears &&
(op == CAIRO_OPERATOR_SOURCE ||
(op == CAIRO_OPERATOR_OVER &&
(surface->base.is_clear || _cairo_pattern_is_opaque_solid (source)))))
{
_cairo_recording_surface_reset (surface);
}
status = _cairo_composite_rectangles_init_for_paint (&composite,
&surface->base,
op, source,
clip);
if (unlikely (status))
return status;
command = _cairo_malloc (sizeof (cairo_command_paint_t));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_COMPOSITE;
}
status = _command_init (surface,
&command->header, CAIRO_COMMAND_PAINT, op,
&composite);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_pattern_init_snapshot (&command->source.base, source);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto CLEANUP_SOURCE;
_cairo_recording_surface_destroy_bbtree (surface);
_cairo_composite_rectangles_fini (&composite);
return CAIRO_STATUS_SUCCESS;
CLEANUP_SOURCE:
_cairo_pattern_fini (&command->source.base);
CLEANUP_COMMAND:
_cairo_clip_destroy (command->header.clip);
free (command);
CLEANUP_COMPOSITE:
_cairo_composite_rectangles_fini (&composite);
return status;
}
static cairo_int_status_t
_cairo_recording_surface_mask (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_pattern_t *mask,
const cairo_clip_t *clip)
{
cairo_status_t status;
cairo_recording_surface_t *surface = abstract_surface;
cairo_command_mask_t *command;
cairo_composite_rectangles_t composite;
TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
status = _cairo_composite_rectangles_init_for_mask (&composite,
&surface->base,
op, source, mask,
clip);
if (unlikely (status))
return status;
command = _cairo_malloc (sizeof (cairo_command_mask_t));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_COMPOSITE;
}
status = _command_init (surface,
&command->header, CAIRO_COMMAND_MASK, op,
&composite);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_pattern_init_snapshot (&command->source.base, source);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_pattern_init_snapshot (&command->mask.base, mask);
if (unlikely (status))
goto CLEANUP_SOURCE;
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto CLEANUP_MASK;
_cairo_recording_surface_destroy_bbtree (surface);
_cairo_composite_rectangles_fini (&composite);
return CAIRO_STATUS_SUCCESS;
CLEANUP_MASK:
_cairo_pattern_fini (&command->mask.base);
CLEANUP_SOURCE:
_cairo_pattern_fini (&command->source.base);
CLEANUP_COMMAND:
_cairo_clip_destroy (command->header.clip);
free (command);
CLEANUP_COMPOSITE:
_cairo_composite_rectangles_fini (&composite);
return status;
}
static cairo_int_status_t
_cairo_recording_surface_stroke (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_antialias_t antialias,
const cairo_clip_t *clip)
{
cairo_status_t status;
cairo_recording_surface_t *surface = abstract_surface;
cairo_command_stroke_t *command;
cairo_composite_rectangles_t composite;
TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
status = _cairo_composite_rectangles_init_for_stroke (&composite,
&surface->base,
op, source,
path, style, ctm,
clip);
if (unlikely (status))
return status;
command = _cairo_malloc (sizeof (cairo_command_stroke_t));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_COMPOSITE;
}
status = _command_init (surface,
&command->header, CAIRO_COMMAND_STROKE, op,
&composite);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_pattern_init_snapshot (&command->source.base, source);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_path_fixed_init_copy (&command->path, path);
if (unlikely (status))
goto CLEANUP_SOURCE;
status = _cairo_stroke_style_init_copy (&command->style, style);
if (unlikely (status))
goto CLEANUP_PATH;
command->ctm = *ctm;
command->ctm_inverse = *ctm_inverse;
command->tolerance = tolerance;
command->antialias = antialias;
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto CLEANUP_STYLE;
_cairo_recording_surface_destroy_bbtree (surface);
_cairo_composite_rectangles_fini (&composite);
return CAIRO_STATUS_SUCCESS;
CLEANUP_STYLE:
_cairo_stroke_style_fini (&command->style);
CLEANUP_PATH:
_cairo_path_fixed_fini (&command->path);
CLEANUP_SOURCE:
_cairo_pattern_fini (&command->source.base);
CLEANUP_COMMAND:
_cairo_clip_destroy (command->header.clip);
free (command);
CLEANUP_COMPOSITE:
_cairo_composite_rectangles_fini (&composite);
return status;
}
static cairo_int_status_t
_cairo_recording_surface_fill (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias,
const cairo_clip_t *clip)
{
cairo_status_t status;
cairo_recording_surface_t *surface = abstract_surface;
cairo_command_fill_t *command;
cairo_composite_rectangles_t composite;
TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
status = _cairo_composite_rectangles_init_for_fill (&composite,
&surface->base,
op, source, path,
clip);
if (unlikely (status))
return status;
command = _cairo_malloc (sizeof (cairo_command_fill_t));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_COMPOSITE;
}
status =_command_init (surface,
&command->header, CAIRO_COMMAND_FILL, op,
&composite);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_pattern_init_snapshot (&command->source.base, source);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_path_fixed_init_copy (&command->path, path);
if (unlikely (status))
goto CLEANUP_SOURCE;
command->fill_rule = fill_rule;
command->tolerance = tolerance;
command->antialias = antialias;
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto CLEANUP_PATH;
_cairo_recording_surface_destroy_bbtree (surface);
_cairo_composite_rectangles_fini (&composite);
return CAIRO_STATUS_SUCCESS;
CLEANUP_PATH:
_cairo_path_fixed_fini (&command->path);
CLEANUP_SOURCE:
_cairo_pattern_fini (&command->source.base);
CLEANUP_COMMAND:
_cairo_clip_destroy (command->header.clip);
free (command);
CLEANUP_COMPOSITE:
_cairo_composite_rectangles_fini (&composite);
return status;
}
static cairo_bool_t
_cairo_recording_surface_has_show_text_glyphs (void *abstract_surface)
{
return TRUE;
}
static cairo_int_status_t
_cairo_recording_surface_show_text_glyphs (void *abstract_surface,
cairo_operator_t op,
const cairo_pattern_t *source,
const char *utf8,
int utf8_len,
cairo_glyph_t *glyphs,
int num_glyphs,
const cairo_text_cluster_t *clusters,
int num_clusters,
cairo_text_cluster_flags_t cluster_flags,
cairo_scaled_font_t *scaled_font,
const cairo_clip_t *clip)
{
cairo_status_t status;
cairo_recording_surface_t *surface = abstract_surface;
cairo_command_show_text_glyphs_t *command;
cairo_composite_rectangles_t composite;
TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
status = _cairo_composite_rectangles_init_for_glyphs (&composite,
&surface->base,
op, source,
scaled_font,
glyphs, num_glyphs,
clip,
NULL);
if (unlikely (status))
return status;
command = _cairo_malloc (sizeof (cairo_command_show_text_glyphs_t));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_COMPOSITE;
}
status = _command_init (surface,
&command->header, CAIRO_COMMAND_SHOW_TEXT_GLYPHS,
op, &composite);
if (unlikely (status))
goto CLEANUP_COMMAND;
status = _cairo_pattern_init_snapshot (&command->source.base, source);
if (unlikely (status))
goto CLEANUP_COMMAND;
command->utf8 = NULL;
command->utf8_len = utf8_len;
command->glyphs = NULL;
command->num_glyphs = num_glyphs;
command->clusters = NULL;
command->num_clusters = num_clusters;
if (utf8_len) {
command->utf8 = _cairo_malloc (utf8_len);
if (unlikely (command->utf8 == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_ARRAYS;
}
memcpy (command->utf8, utf8, utf8_len);
}
if (num_glyphs) {
command->glyphs = _cairo_malloc_ab (num_glyphs, sizeof (glyphs[0]));
if (unlikely (command->glyphs == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_ARRAYS;
}
memcpy (command->glyphs, glyphs, sizeof (glyphs[0]) * num_glyphs);
}
if (num_clusters) {
command->clusters = _cairo_malloc_ab (num_clusters, sizeof (clusters[0]));
if (unlikely (command->clusters == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_ARRAYS;
}
memcpy (command->clusters, clusters, sizeof (clusters[0]) * num_clusters);
}
command->cluster_flags = cluster_flags;
command->scaled_font = cairo_scaled_font_reference (scaled_font);
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto CLEANUP_SCALED_FONT;
_cairo_composite_rectangles_fini (&composite);
return CAIRO_STATUS_SUCCESS;
CLEANUP_SCALED_FONT:
cairo_scaled_font_destroy (command->scaled_font);
CLEANUP_ARRAYS:
free (command->utf8);
free (command->glyphs);
free (command->clusters);
_cairo_pattern_fini (&command->source.base);
CLEANUP_COMMAND:
_cairo_clip_destroy (command->header.clip);
free (command);
CLEANUP_COMPOSITE:
_cairo_composite_rectangles_fini (&composite);
return status;
}
static cairo_int_status_t
_cairo_recording_surface_tag (void *abstract_surface,
cairo_bool_t begin,
const char *tag_name,
const char *attributes,
const cairo_pattern_t *source,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
const cairo_clip_t *clip)
{
cairo_status_t status;
cairo_recording_surface_t *surface = abstract_surface;
cairo_command_tag_t *command;
cairo_composite_rectangles_t composite;
TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
status = _cairo_composite_rectangles_init_for_paint (&composite,
&surface->base,
CAIRO_OPERATOR_SOURCE,
source ? source : &_cairo_pattern_black.base,
clip);
if (unlikely (status))
return status;
command = calloc (1, sizeof (cairo_command_tag_t));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto CLEANUP_COMPOSITE;
}
status = _command_init (surface,
&command->header, CAIRO_COMMAND_TAG, CAIRO_OPERATOR_SOURCE,
&composite);
if (unlikely (status))
goto CLEANUP_COMMAND;
command->begin = begin;
command->tag_name = strdup (tag_name);
if (begin) {
if (attributes)
command->attributes = strdup (attributes);
status = _cairo_pattern_init_snapshot (&command->source.base, source);
if (unlikely (status))
goto CLEANUP_STRINGS;
status = _cairo_stroke_style_init_copy (&command->style, style);
if (unlikely (status))
goto CLEANUP_SOURCE;
command->ctm = *ctm;
command->ctm_inverse = *ctm_inverse;
}
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status)) {
if (begin)
goto CLEANUP_STRINGS;
else
goto CLEANUP_STYLE;
}
_cairo_recording_surface_destroy_bbtree (surface);
_cairo_composite_rectangles_fini (&composite);
return CAIRO_STATUS_SUCCESS;
CLEANUP_STYLE:
_cairo_stroke_style_fini (&command->style);
CLEANUP_SOURCE:
_cairo_pattern_fini (&command->source.base);
CLEANUP_STRINGS:
free (command->tag_name);
free (command->attributes);
CLEANUP_COMMAND:
_cairo_clip_destroy (command->header.clip);
free (command);
CLEANUP_COMPOSITE:
_cairo_composite_rectangles_fini (&composite);
return status;
}
static void
_command_init_copy (cairo_recording_surface_t *surface,
cairo_command_header_t *dst,
const cairo_command_header_t *src)
{
dst->type = src->type;
dst->op = src->op;
dst->region = CAIRO_RECORDING_REGION_ALL;
dst->extents = src->extents;
dst->chain = NULL;
dst->index = surface->commands.num_elements;
dst->clip = _cairo_clip_copy (src->clip);
}
static cairo_status_t
_cairo_recording_surface_copy__paint (cairo_recording_surface_t *surface,
const cairo_command_t *src)
{
cairo_command_paint_t *command;
cairo_status_t status;
command = _cairo_malloc (sizeof (*command));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err;
}
_command_init_copy (surface, &command->header, &src->header);
status = _cairo_pattern_init_copy (&command->source.base,
&src->paint.source.base);
if (unlikely (status))
goto err_command;
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto err_source;
return CAIRO_STATUS_SUCCESS;
err_source:
_cairo_pattern_fini (&command->source.base);
err_command:
free(command);
err:
return status;
}
static cairo_status_t
_cairo_recording_surface_copy__mask (cairo_recording_surface_t *surface,
const cairo_command_t *src)
{
cairo_command_mask_t *command;
cairo_status_t status;
command = _cairo_malloc (sizeof (*command));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err;
}
_command_init_copy (surface, &command->header, &src->header);
status = _cairo_pattern_init_copy (&command->source.base,
&src->mask.source.base);
if (unlikely (status))
goto err_command;
status = _cairo_pattern_init_copy (&command->mask.base,
&src->mask.mask.base);
if (unlikely (status))
goto err_source;
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto err_mask;
return CAIRO_STATUS_SUCCESS;
err_mask:
_cairo_pattern_fini (&command->mask.base);
err_source:
_cairo_pattern_fini (&command->source.base);
err_command:
free(command);
err:
return status;
}
static cairo_status_t
_cairo_recording_surface_copy__stroke (cairo_recording_surface_t *surface,
const cairo_command_t *src)
{
cairo_command_stroke_t *command;
cairo_status_t status;
command = _cairo_malloc (sizeof (*command));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err;
}
_command_init_copy (surface, &command->header, &src->header);
status = _cairo_pattern_init_copy (&command->source.base,
&src->stroke.source.base);
if (unlikely (status))
goto err_command;
status = _cairo_path_fixed_init_copy (&command->path, &src->stroke.path);
if (unlikely (status))
goto err_source;
status = _cairo_stroke_style_init_copy (&command->style,
&src->stroke.style);
if (unlikely (status))
goto err_path;
command->ctm = src->stroke.ctm;
command->ctm_inverse = src->stroke.ctm_inverse;
command->tolerance = src->stroke.tolerance;
command->antialias = src->stroke.antialias;
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto err_style;
return CAIRO_STATUS_SUCCESS;
err_style:
_cairo_stroke_style_fini (&command->style);
err_path:
_cairo_path_fixed_fini (&command->path);
err_source:
_cairo_pattern_fini (&command->source.base);
err_command:
free(command);
err:
return status;
}
static cairo_status_t
_cairo_recording_surface_copy__fill (cairo_recording_surface_t *surface,
const cairo_command_t *src)
{
cairo_command_fill_t *command;
cairo_status_t status;
command = _cairo_malloc (sizeof (*command));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err;
}
_command_init_copy (surface, &command->header, &src->header);
status = _cairo_pattern_init_copy (&command->source.base,
&src->fill.source.base);
if (unlikely (status))
goto err_command;
status = _cairo_path_fixed_init_copy (&command->path, &src->fill.path);
if (unlikely (status))
goto err_source;
command->fill_rule = src->fill.fill_rule;
command->tolerance = src->fill.tolerance;
command->antialias = src->fill.antialias;
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto err_path;
return CAIRO_STATUS_SUCCESS;
err_path:
_cairo_path_fixed_fini (&command->path);
err_source:
_cairo_pattern_fini (&command->source.base);
err_command:
free(command);
err:
return status;
}
static cairo_status_t
_cairo_recording_surface_copy__glyphs (cairo_recording_surface_t *surface,
const cairo_command_t *src)
{
cairo_command_show_text_glyphs_t *command;
cairo_status_t status;
command = _cairo_malloc (sizeof (*command));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err;
}
_command_init_copy (surface, &command->header, &src->header);
status = _cairo_pattern_init_copy (&command->source.base,
&src->show_text_glyphs.source.base);
if (unlikely (status))
goto err_command;
command->utf8 = NULL;
command->utf8_len = src->show_text_glyphs.utf8_len;
command->glyphs = NULL;
command->num_glyphs = src->show_text_glyphs.num_glyphs;
command->clusters = NULL;
command->num_clusters = src->show_text_glyphs.num_clusters;
if (command->utf8_len) {
command->utf8 = _cairo_malloc (command->utf8_len);
if (unlikely (command->utf8 == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err_arrays;
}
memcpy (command->utf8, src->show_text_glyphs.utf8, command->utf8_len);
}
if (command->num_glyphs) {
command->glyphs = _cairo_malloc_ab (command->num_glyphs,
sizeof (command->glyphs[0]));
if (unlikely (command->glyphs == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err_arrays;
}
memcpy (command->glyphs, src->show_text_glyphs.glyphs,
sizeof (command->glyphs[0]) * command->num_glyphs);
}
if (command->num_clusters) {
command->clusters = _cairo_malloc_ab (command->num_clusters,
sizeof (command->clusters[0]));
if (unlikely (command->clusters == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err_arrays;
}
memcpy (command->clusters, src->show_text_glyphs.clusters,
sizeof (command->clusters[0]) * command->num_clusters);
}
command->cluster_flags = src->show_text_glyphs.cluster_flags;
command->scaled_font =
cairo_scaled_font_reference (src->show_text_glyphs.scaled_font);
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status))
goto err_arrays;
return CAIRO_STATUS_SUCCESS;
err_arrays:
free (command->utf8);
free (command->glyphs);
free (command->clusters);
_cairo_pattern_fini (&command->source.base);
err_command:
free(command);
err:
return status;
}
static cairo_status_t
_cairo_recording_surface_copy__tag (cairo_recording_surface_t *surface,
const cairo_command_t *src)
{
cairo_command_tag_t *command;
cairo_status_t status;
command = calloc (1, sizeof (*command));
if (unlikely (command == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto err;
}
_command_init_copy (surface, &command->header, &src->header);
command->begin = src->tag.begin;
command->tag_name = strdup (src->tag.tag_name);
if (src->tag.begin) {
if (src->tag.attributes)
command->attributes = strdup (src->tag.attributes);
status = _cairo_pattern_init_copy (&command->source.base,
&src->stroke.source.base);
if (unlikely (status))
goto err_command;
status = _cairo_stroke_style_init_copy (&command->style,
&src->stroke.style);
if (unlikely (status))
goto err_source;
command->ctm = src->stroke.ctm;
command->ctm_inverse = src->stroke.ctm_inverse;
}
status = _cairo_recording_surface_commit (surface, &command->header);
if (unlikely (status)) {
if (src->tag.begin)
goto err_command;
else
goto err_style;
}
return CAIRO_STATUS_SUCCESS;
err_style:
_cairo_stroke_style_fini (&command->style);
err_source:
_cairo_pattern_fini (&command->source.base);
err_command:
free(command->tag_name);
free(command->attributes);
free(command);
err:
return status;
}
static cairo_status_t
_cairo_recording_surface_copy (cairo_recording_surface_t *dst,
cairo_recording_surface_t *src)
{
cairo_command_t **elements;
int i, num_elements;
cairo_status_t status;
elements = _cairo_array_index (&src->commands, 0);
num_elements = src->commands.num_elements;
for (i = 0; i < num_elements; i++) {
const cairo_command_t *command = elements[i];
switch (command->header.type) {
case CAIRO_COMMAND_PAINT:
status = _cairo_recording_surface_copy__paint (dst, command);
break;
case CAIRO_COMMAND_MASK:
status = _cairo_recording_surface_copy__mask (dst, command);
break;
case CAIRO_COMMAND_STROKE:
status = _cairo_recording_surface_copy__stroke (dst, command);
break;
case CAIRO_COMMAND_FILL:
status = _cairo_recording_surface_copy__fill (dst, command);
break;
case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
status = _cairo_recording_surface_copy__glyphs (dst, command);
break;
case CAIRO_COMMAND_TAG:
status = _cairo_recording_surface_copy__tag (dst, command);
break;
default:
ASSERT_NOT_REACHED;
}
if (unlikely (status))
return status;
}
return CAIRO_STATUS_SUCCESS;
}
/**
* _cairo_recording_surface_snapshot:
* @surface: a #cairo_surface_t which must be a recording surface
*
* Make an immutable copy of @surface. It is an error to call a
* surface-modifying function on the result of this function.
*
* The caller owns the return value and should call
* cairo_surface_destroy() when finished with it. This function will not
* return %NULL, but will return a nil surface instead.
*
* Return value: The snapshot surface.
**/
static cairo_surface_t *
_cairo_recording_surface_snapshot (void *abstract_other)
{
cairo_recording_surface_t *other = abstract_other;
cairo_recording_surface_t *surface;
cairo_status_t status;
surface = _cairo_malloc (sizeof (cairo_recording_surface_t));
if (unlikely (surface == NULL))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
_cairo_surface_init (&surface->base,
&cairo_recording_surface_backend,
NULL, /* device */
other->base.content,
other->base.is_vector);
surface->extents_pixels = other->extents_pixels;
surface->extents = other->extents;
surface->unbounded = other->unbounded;
surface->base.is_clear = other->base.is_clear;
surface->bbtree.left = surface->bbtree.right = NULL;
surface->bbtree.chain = INVALID_CHAIN;
surface->indices = NULL;
surface->num_indices = 0;
surface->optimize_clears = TRUE;
_cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
status = _cairo_recording_surface_copy (surface, other);
if (unlikely (status)) {
cairo_surface_destroy (&surface->base);
return _cairo_surface_create_in_error (status);
}
return &surface->base;
}
static cairo_bool_t
_cairo_recording_surface_get_extents (void *abstract_surface,
cairo_rectangle_int_t *rectangle)
{
cairo_recording_surface_t *surface = abstract_surface;
if (surface->unbounded)
return FALSE;
*rectangle = surface->extents;
return TRUE;
}
static const cairo_surface_backend_t cairo_recording_surface_backend = {
CAIRO_SURFACE_TYPE_RECORDING,
_cairo_recording_surface_finish,
_cairo_default_context_create,
_cairo_recording_surface_create_similar,
NULL, /* create similar image */
NULL, /* map to image */
NULL, /* unmap image */
_cairo_surface_default_source,
_cairo_recording_surface_acquire_source_image,
_cairo_recording_surface_release_source_image,
_cairo_recording_surface_snapshot,
NULL, /* copy_page */
NULL, /* show_page */
_cairo_recording_surface_get_extents,
NULL, /* get_font_options */
NULL, /* flush */
NULL, /* mark_dirty_rectangle */
/* Here are the 5 basic drawing operations, (which are in some
* sense the only things that cairo_recording_surface should need to
* implement). However, we implement the more generic show_text_glyphs
* instead of show_glyphs. One or the other is eough. */
_cairo_recording_surface_paint,
_cairo_recording_surface_mask,
_cairo_recording_surface_stroke,
_cairo_recording_surface_fill,
NULL, /* fill-stroke */
NULL,
_cairo_recording_surface_has_show_text_glyphs,
_cairo_recording_surface_show_text_glyphs,
NULL, /* get_supported_mime_types */
_cairo_recording_surface_tag,
};
cairo_int_status_t
_cairo_recording_surface_get_path (cairo_surface_t *abstract_surface,
cairo_path_fixed_t *path)
{
cairo_recording_surface_t *surface;
cairo_command_t **elements;
int i, num_elements;
cairo_int_status_t status;
if (unlikely (abstract_surface->status))
return abstract_surface->status;
surface = (cairo_recording_surface_t *) abstract_surface;
status = CAIRO_STATUS_SUCCESS;
num_elements = surface->commands.num_elements;
elements = _cairo_array_index (&surface->commands, 0);
for (i = 0; i < num_elements; i++) {
cairo_command_t *command = elements[i];
switch (command->header.type) {
case CAIRO_COMMAND_PAINT:
case CAIRO_COMMAND_MASK:
status = CAIRO_INT_STATUS_UNSUPPORTED;
break;
case CAIRO_COMMAND_STROKE:
{
cairo_traps_t traps;
_cairo_traps_init (&traps);
/* XXX call cairo_stroke_to_path() when that is implemented */
status = _cairo_path_fixed_stroke_polygon_to_traps (&command->stroke.path,
&command->stroke.style,
&command->stroke.ctm,
&command->stroke.ctm_inverse,
command->stroke.tolerance,
&traps);
if (status == CAIRO_INT_STATUS_SUCCESS)
status = _cairo_traps_path (&traps, path);
_cairo_traps_fini (&traps);
break;
}
case CAIRO_COMMAND_FILL:
{
status = _cairo_path_fixed_append (path,
&command->fill.path,
0, 0);
break;
}
case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
{
status = _cairo_scaled_font_glyph_path (command->show_text_glyphs.scaled_font,
command->show_text_glyphs.glyphs,
command->show_text_glyphs.num_glyphs,
path);
break;
}
case CAIRO_COMMAND_TAG:
break;
default:
ASSERT_NOT_REACHED;
}
if (unlikely (status))
break;
}
return status;
}
static int
_cairo_recording_surface_get_visible_commands (cairo_recording_surface_t *surface,
const cairo_rectangle_int_t *extents)
{
unsigned int num_visible, *indices;
cairo_box_t box;
if (surface->commands.num_elements == 0)
return 0;
_cairo_box_from_rectangle (&box, extents);
if (surface->bbtree.chain == INVALID_CHAIN)
_cairo_recording_surface_create_bbtree (surface);
indices = surface->indices;
bbtree_foreach_mark_visible (&surface->bbtree, &box, &indices);
num_visible = indices - surface->indices;
if (num_visible > 1)
sort_indices (surface->indices, num_visible);
return num_visible;
}
static void
_cairo_recording_surface_merge_source_attributes (cairo_recording_surface_t *surface,
cairo_operator_t op,
const cairo_pattern_t *source)
{
if (op != CAIRO_OPERATOR_OVER)
surface->has_only_op_over = FALSE;
if (source->type == CAIRO_PATTERN_TYPE_SURFACE) {
cairo_surface_pattern_t *surf_pat = (cairo_surface_pattern_t *) source;
cairo_surface_t *surf = surf_pat->surface;
cairo_surface_t *free_me = NULL;
if (_cairo_surface_is_snapshot (surf))
free_me = surf = _cairo_surface_snapshot_get_target (surf);
if (surf->type == CAIRO_SURFACE_TYPE_RECORDING) {
cairo_recording_surface_t *rec_surf = (cairo_recording_surface_t *) surf;
if (! _cairo_recording_surface_has_only_bilevel_alpha (rec_surf))
surface->has_bilevel_alpha = FALSE;
if (! _cairo_recording_surface_has_only_op_over (rec_surf))
surface->has_only_op_over = FALSE;
} else if (surf->type == CAIRO_SURFACE_TYPE_IMAGE) {
cairo_image_surface_t *img_surf = (cairo_image_surface_t *) surf;
if (_cairo_image_analyze_transparency (img_surf) == CAIRO_IMAGE_HAS_ALPHA)
surface->has_bilevel_alpha = FALSE;
} else {
if (!_cairo_pattern_is_clear (source) && !_cairo_pattern_is_opaque (source, NULL))
surface->has_bilevel_alpha = FALSE;
}
cairo_surface_destroy (free_me);
return;
} else if (source->type == CAIRO_PATTERN_TYPE_RASTER_SOURCE) {
cairo_surface_t *image;
cairo_surface_t *raster;
image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
raster = _cairo_raster_source_pattern_acquire (source, image, NULL);
cairo_surface_destroy (image);
if (raster) {
if (raster->type == CAIRO_SURFACE_TYPE_IMAGE) {
if (_cairo_image_analyze_transparency ((cairo_image_surface_t *)raster) == CAIRO_IMAGE_HAS_ALPHA)
surface->has_bilevel_alpha = FALSE;
}
_cairo_raster_source_pattern_release (source, raster);
if (raster->type == CAIRO_SURFACE_TYPE_IMAGE)
return;
}
}
if (!_cairo_pattern_is_clear (source) && !_cairo_pattern_is_opaque (source, NULL))
surface->has_bilevel_alpha = FALSE;
}
static cairo_status_t
_cairo_recording_surface_replay_internal (cairo_recording_surface_t *surface,
const cairo_rectangle_int_t *surface_extents,
const cairo_matrix_t *surface_transform,
cairo_surface_t *target,
const cairo_clip_t *target_clip,
cairo_bool_t surface_is_unbounded,
cairo_recording_replay_type_t type,
cairo_recording_region_type_t region)
{
cairo_surface_wrapper_t wrapper;
cairo_command_t **elements;
cairo_bool_t replay_all =
type == CAIRO_RECORDING_CREATE_REGIONS || region == CAIRO_RECORDING_REGION_ALL;
cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
cairo_rectangle_int_t extents;
cairo_bool_t use_indices = FALSE;
const cairo_rectangle_int_t *r;
unsigned int i, num_elements;
if (unlikely (surface->base.status))
return surface->base.status;
if (unlikely (target->status))
return target->status;
if (unlikely (surface->base.finished))
return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
if (surface->base.is_clear)
return CAIRO_STATUS_SUCCESS;
assert (_cairo_surface_is_recording (&surface->base));
_cairo_surface_wrapper_init (&wrapper, target);
if (surface_extents)
_cairo_surface_wrapper_intersect_extents (&wrapper, surface_extents);
r = &_cairo_unbounded_rectangle;
if (! surface->unbounded && !surface_is_unbounded) {
_cairo_surface_wrapper_intersect_extents (&wrapper, &surface->extents);
r = &surface->extents;
}
_cairo_surface_wrapper_set_inverse_transform (&wrapper, surface_transform);
_cairo_surface_wrapper_set_clip (&wrapper, target_clip);
/* Compute the extents of the target clip in recorded device space */
if (! _cairo_surface_wrapper_get_target_extents (&wrapper, surface_is_unbounded, &extents))
goto done;
surface->has_bilevel_alpha = TRUE;
surface->has_only_op_over = TRUE;
num_elements = surface->commands.num_elements;
elements = _cairo_array_index (&surface->commands, 0);
if (extents.width < r->width || extents.height < r->height) {
num_elements =
_cairo_recording_surface_get_visible_commands (surface, &extents);
use_indices = num_elements != surface->commands.num_elements;
}
for (i = 0; i < num_elements; i++) {
cairo_command_t *command = elements[use_indices ? surface->indices[i] : i];
if (! replay_all && command->header.region != region)
continue;
if (! _cairo_rectangle_intersects (&extents, &command->header.extents))
continue;
switch (command->header.type) {
case CAIRO_COMMAND_PAINT:
status = _cairo_surface_wrapper_paint (&wrapper,
command->header.op,
&command->paint.source.base,
command->header.clip);
if (type == CAIRO_RECORDING_CREATE_REGIONS) {
_cairo_recording_surface_merge_source_attributes (surface,
command->header.op,
&command->paint.source.base);
}
break;
case CAIRO_COMMAND_MASK:
status = _cairo_surface_wrapper_mask (&wrapper,
command->header.op,
&command->mask.source.base,
&command->mask.mask.base,
command->header.clip);
if (type == CAIRO_RECORDING_CREATE_REGIONS) {
_cairo_recording_surface_merge_source_attributes (surface,
command->header.op,
&command->mask.source.base);
_cairo_recording_surface_merge_source_attributes (surface,
command->header.op,
&command->mask.mask.base);
}
break;
case CAIRO_COMMAND_STROKE:
status = _cairo_surface_wrapper_stroke (&wrapper,
command->header.op,
&command->stroke.source.base,
&command->stroke.path,
&command->stroke.style,
&command->stroke.ctm,
&command->stroke.ctm_inverse,
command->stroke.tolerance,
command->stroke.antialias,
command->header.clip);
if (type == CAIRO_RECORDING_CREATE_REGIONS) {
_cairo_recording_surface_merge_source_attributes (surface,
command->header.op,
&command->stroke.source.base);
}
break;
case CAIRO_COMMAND_FILL:
status = CAIRO_INT_STATUS_UNSUPPORTED;
if (_cairo_surface_wrapper_has_fill_stroke (&wrapper)) {
cairo_command_t *stroke_command;
stroke_command = NULL;
if (type != CAIRO_RECORDING_CREATE_REGIONS && i < num_elements - 1)
stroke_command = elements[i + 1];
if (stroke_command != NULL &&
type == CAIRO_RECORDING_REPLAY &&
region != CAIRO_RECORDING_REGION_ALL)
{
if (stroke_command->header.region != region)
stroke_command = NULL;
}
if (stroke_command != NULL &&
stroke_command->header.type == CAIRO_COMMAND_STROKE &&
_cairo_path_fixed_equal (&command->fill.path,
&stroke_command->stroke.path) &&
_cairo_clip_equal (command->header.clip,
stroke_command->header.clip))
{
status = _cairo_surface_wrapper_fill_stroke (&wrapper,
command->header.op,
&command->fill.source.base,
command->fill.fill_rule,
command->fill.tolerance,
command->fill.antialias,
&command->fill.path,
stroke_command->header.op,
&stroke_command->stroke.source.base,
&stroke_command->stroke.style,
&stroke_command->stroke.ctm,
&stroke_command->stroke.ctm_inverse,
stroke_command->stroke.tolerance,
stroke_command->stroke.antialias,
command->header.clip);
if (type == CAIRO_RECORDING_CREATE_REGIONS) {
_cairo_recording_surface_merge_source_attributes (surface,
command->header.op,
&command->fill.source.base);
_cairo_recording_surface_merge_source_attributes (surface,
command->header.op,
&command->stroke.source.base);
}
i++;
}
}
if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
status = _cairo_surface_wrapper_fill (&wrapper,
command->header.op,
&command->fill.source.base,
&command->fill.path,
command->fill.fill_rule,
command->fill.tolerance,
command->fill.antialias,
command->header.clip);
if (type == CAIRO_RECORDING_CREATE_REGIONS) {
_cairo_recording_surface_merge_source_attributes (surface,
command->header.op,
&command->fill.source.base);
}
}
break;
case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
status = _cairo_surface_wrapper_show_text_glyphs (&wrapper,
command->header.op,
&command->show_text_glyphs.source.base,
command->show_text_glyphs.utf8, command->show_text_glyphs.utf8_len,
command->show_text_glyphs.glyphs, command->show_text_glyphs.num_glyphs,
command->show_text_glyphs.clusters, command->show_text_glyphs.num_clusters,
command->show_text_glyphs.cluster_flags,
command->show_text_glyphs.scaled_font,
command->header.clip);
if (type == CAIRO_RECORDING_CREATE_REGIONS) {
_cairo_recording_surface_merge_source_attributes (surface,
command->header.op,
&command->show_text_glyphs.source.base);
}
break;
case CAIRO_COMMAND_TAG:
status = _cairo_surface_wrapper_tag (&wrapper,
command->tag.begin,
command->tag.tag_name,
command->tag.attributes,
&command->tag.source.base,
&command->tag.style,
&command->tag.ctm,
&command->tag.ctm_inverse,
command->header.clip);
break;
default:
ASSERT_NOT_REACHED;
}
if (type == CAIRO_RECORDING_CREATE_REGIONS && command->header.region != CAIRO_RECORDING_REGION_NATIVE) {
if (status == CAIRO_INT_STATUS_SUCCESS) {
command->header.region = CAIRO_RECORDING_REGION_NATIVE;
} else if (status == CAIRO_INT_STATUS_IMAGE_FALLBACK) {
command->header.region = CAIRO_RECORDING_REGION_IMAGE_FALLBACK;
status = CAIRO_INT_STATUS_SUCCESS;
} else {
assert (_cairo_int_status_is_error (status));
}
}
if (unlikely (status))
break;
}
done:
_cairo_surface_wrapper_fini (&wrapper);
return _cairo_surface_set_error (&surface->base, status);
}
cairo_status_t
_cairo_recording_surface_replay_one (cairo_recording_surface_t *surface,
long unsigned index,
cairo_surface_t *target)
{
cairo_surface_wrapper_t wrapper;
cairo_command_t **elements, *command;
cairo_int_status_t status;
if (unlikely (surface->base.status))
return surface->base.status;
if (unlikely (target->status))
return target->status;
if (unlikely (surface->base.finished))
return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
assert (_cairo_surface_is_recording (&surface->base));
/* XXX
* Use a surface wrapper because we may want to do transformed
* replay in the future.
*/
_cairo_surface_wrapper_init (&wrapper, target);
if (index > surface->commands.num_elements)
return _cairo_error (CAIRO_STATUS_READ_ERROR);
elements = _cairo_array_index (&surface->commands, 0);
command = elements[index];
switch (command->header.type) {
case CAIRO_COMMAND_PAINT:
status = _cairo_surface_wrapper_paint (&wrapper,
command->header.op,
&command->paint.source.base,
command->header.clip);
break;
case CAIRO_COMMAND_MASK:
status = _cairo_surface_wrapper_mask (&wrapper,
command->header.op,
&command->mask.source.base,
&command->mask.mask.base,
command->header.clip);
break;
case CAIRO_COMMAND_STROKE:
status = _cairo_surface_wrapper_stroke (&wrapper,
command->header.op,
&command->stroke.source.base,
&command->stroke.path,
&command->stroke.style,
&command->stroke.ctm,
&command->stroke.ctm_inverse,
command->stroke.tolerance,
command->stroke.antialias,
command->header.clip);
break;
case CAIRO_COMMAND_FILL:
status = _cairo_surface_wrapper_fill (&wrapper,
command->header.op,
&command->fill.source.base,
&command->fill.path,
command->fill.fill_rule,
command->fill.tolerance,
command->fill.antialias,
command->header.clip);
break;
case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
status = _cairo_surface_wrapper_show_text_glyphs (&wrapper,
command->header.op,
&command->show_text_glyphs.source.base,
command->show_text_glyphs.utf8, command->show_text_glyphs.utf8_len,
command->show_text_glyphs.glyphs, command->show_text_glyphs.num_glyphs,
command->show_text_glyphs.clusters, command->show_text_glyphs.num_clusters,
command->show_text_glyphs.cluster_flags,
command->show_text_glyphs.scaled_font,
command->header.clip);
break;
case CAIRO_COMMAND_TAG:
status = _cairo_surface_wrapper_tag (&wrapper,
command->tag.begin,
command->tag.tag_name,
command->tag.attributes,
&command->tag.source.base,
&command->tag.style,
&command->tag.ctm,
&command->tag.ctm_inverse,
command->header.clip);
break;
default:
ASSERT_NOT_REACHED;
}
_cairo_surface_wrapper_fini (&wrapper);
return _cairo_surface_set_error (&surface->base, status);
}
/**
* _cairo_recording_surface_replay:
* @surface: the #cairo_recording_surface_t
* @target: a target #cairo_surface_t onto which to replay the operations
* @width_pixels: width of the surface, in pixels
* @height_pixels: height of the surface, in pixels
*
* A recording surface can be "replayed" against any target surface,
* after which the results in target will be identical to the results
* that would have been obtained if the original operations applied to
* the recording surface had instead been applied to the target surface.
**/
cairo_status_t
_cairo_recording_surface_replay (cairo_surface_t *surface,
cairo_surface_t *target)
{
return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, NULL, NULL,
target, NULL, FALSE,
CAIRO_RECORDING_REPLAY,
CAIRO_RECORDING_REGION_ALL);
}
cairo_status_t
_cairo_recording_surface_replay_with_clip (cairo_surface_t *surface,
const cairo_matrix_t *surface_transform,
cairo_surface_t *target,
const cairo_clip_t *target_clip)
{
return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, NULL, surface_transform,
target, target_clip, FALSE,
CAIRO_RECORDING_REPLAY,
CAIRO_RECORDING_REGION_ALL);
}
/* Replay recording to surface. When the return status of each operation is
* one of %CAIRO_STATUS_SUCCESS, %CAIRO_INT_STATUS_UNSUPPORTED, or
* %CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY the status of each operation
* will be stored in the recording surface. Any other status will abort the
* replay and return the status.
*/
cairo_status_t
_cairo_recording_surface_replay_and_create_regions (cairo_surface_t *surface,
const cairo_matrix_t *surface_transform,
cairo_surface_t *target,
cairo_bool_t surface_is_unbounded)
{
return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, NULL, surface_transform,
target, NULL,
surface_is_unbounded,
CAIRO_RECORDING_CREATE_REGIONS,
CAIRO_RECORDING_REGION_ALL);
}
cairo_status_t
_cairo_recording_surface_replay_region (cairo_surface_t *surface,
const cairo_rectangle_int_t *surface_extents,
cairo_surface_t *target,
cairo_recording_region_type_t region)
{
return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface,
surface_extents, NULL,
target, NULL, FALSE,
CAIRO_RECORDING_REPLAY,
region);
}
static cairo_status_t
_recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
cairo_box_t *bbox,
const cairo_matrix_t *transform)
{
cairo_surface_t *null_surface;
cairo_surface_t *analysis_surface;
cairo_status_t status;
null_surface = _cairo_null_surface_create (surface->base.content);
analysis_surface = _cairo_analysis_surface_create (null_surface);
cairo_surface_destroy (null_surface);
status = analysis_surface->status;
if (unlikely (status))
return status;
if (transform != NULL)
_cairo_analysis_surface_set_ctm (analysis_surface, transform);
status = _cairo_recording_surface_replay (&surface->base, analysis_surface);
_cairo_analysis_surface_get_bounding_box (analysis_surface, bbox);
cairo_surface_destroy (analysis_surface);
return status;
}
/**
* cairo_recording_surface_ink_extents:
* @surface: a #cairo_recording_surface_t
* @x0: the x-coordinate of the top-left of the ink bounding box
* @y0: the y-coordinate of the top-left of the ink bounding box
* @width: the width of the ink bounding box
* @height: the height of the ink bounding box
*
* Measures the extents of the operations stored within the recording-surface.
* This is useful to compute the required size of an image surface (or
* equivalent) into which to replay the full sequence of drawing operations.
*
* Since: 1.10
**/
void
cairo_recording_surface_ink_extents (cairo_surface_t *surface,
double *x0,
double *y0,
double *width,
double *height)
{
cairo_status_t status;
cairo_box_t bbox;
memset (&bbox, 0, sizeof (bbox));
if (surface->status || ! _cairo_surface_is_recording (surface)) {
_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
goto DONE;
}
status = _recording_surface_get_ink_bbox ((cairo_recording_surface_t *) surface,
&bbox,
NULL);
if (unlikely (status))
status = _cairo_surface_set_error (surface, status);
DONE:
if (x0)
*x0 = _cairo_fixed_to_double (bbox.p1.x);
if (y0)
*y0 = _cairo_fixed_to_double (bbox.p1.y);
if (width)
*width = _cairo_fixed_to_double (bbox.p2.x - bbox.p1.x);
if (height)
*height = _cairo_fixed_to_double (bbox.p2.y - bbox.p1.y);
}
cairo_status_t
_cairo_recording_surface_get_bbox (cairo_recording_surface_t *surface,
cairo_box_t *bbox,
const cairo_matrix_t *transform)
{
if (! surface->unbounded) {
_cairo_box_from_rectangle (bbox, &surface->extents);
if (transform != NULL)
_cairo_matrix_transform_bounding_box_fixed (transform, bbox, NULL);
return CAIRO_STATUS_SUCCESS;
}
return _recording_surface_get_ink_bbox (surface, bbox, transform);
}
cairo_status_t
_cairo_recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
cairo_box_t *bbox,
const cairo_matrix_t *transform)
{
return _recording_surface_get_ink_bbox (surface, bbox, transform);
}
/**
* cairo_recording_surface_get_extents:
* @surface: a #cairo_recording_surface_t
* @extents: the #cairo_rectangle_t to be assigned the extents
*
* Get the extents of the recording-surface.
*
* Return value: %TRUE if the surface is bounded, of recording type, and
* not in an error state, otherwise %FALSE
*
* Since: 1.12
**/
cairo_bool_t
cairo_recording_surface_get_extents (cairo_surface_t *surface,
cairo_rectangle_t *extents)
{
cairo_recording_surface_t *record;
if (surface->status || ! _cairo_surface_is_recording (surface)) {
_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
return FALSE;
}
record = (cairo_recording_surface_t *)surface;
if (record->unbounded)
return FALSE;
*extents = record->extents_pixels;
return TRUE;
}
cairo_bool_t
_cairo_recording_surface_has_only_bilevel_alpha (cairo_recording_surface_t *surface)
{
return surface->has_bilevel_alpha;
}
cairo_bool_t
_cairo_recording_surface_has_only_op_over (cairo_recording_surface_t *surface)
{
return surface->has_only_op_over;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-rectangle.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2006 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#include "cairoint.h"
#include "cairo-box-inline.h"
const cairo_rectangle_int_t _cairo_empty_rectangle = { 0, 0, 0, 0 };
const cairo_rectangle_int_t _cairo_unbounded_rectangle = {
CAIRO_RECT_INT_MIN, CAIRO_RECT_INT_MIN,
CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN,
CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN,
};
cairo_private void
_cairo_box_from_doubles (cairo_box_t *box,
double *x1, double *y1,
double *x2, double *y2)
{
box->p1.x = _cairo_fixed_from_double (*x1);
box->p1.y = _cairo_fixed_from_double (*y1);
box->p2.x = _cairo_fixed_from_double (*x2);
box->p2.y = _cairo_fixed_from_double (*y2);
}
cairo_private void
_cairo_box_to_doubles (const cairo_box_t *box,
double *x1, double *y1,
double *x2, double *y2)
{
*x1 = _cairo_fixed_to_double (box->p1.x);
*y1 = _cairo_fixed_to_double (box->p1.y);
*x2 = _cairo_fixed_to_double (box->p2.x);
*y2 = _cairo_fixed_to_double (box->p2.y);
}
void
_cairo_box_from_rectangle (cairo_box_t *box,
const cairo_rectangle_int_t *rect)
{
box->p1.x = _cairo_fixed_from_int (rect->x);
box->p1.y = _cairo_fixed_from_int (rect->y);
box->p2.x = _cairo_fixed_from_int (rect->x + rect->width);
box->p2.y = _cairo_fixed_from_int (rect->y + rect->height);
}
void
_cairo_boxes_get_extents (const cairo_box_t *boxes,
int num_boxes,
cairo_box_t *extents)
{
assert (num_boxes > 0);
*extents = *boxes;
while (--num_boxes)
_cairo_box_add_box (extents, ++boxes);
}
/* XXX We currently have a confusing mix of boxes and rectangles as
* exemplified by this function. A #cairo_box_t is a rectangular area
* represented by the coordinates of the upper left and lower right
* corners, expressed in fixed point numbers. A #cairo_rectangle_int_t is
* also a rectangular area, but represented by the upper left corner
* and the width and the height, as integer numbers.
*
* This function converts a #cairo_box_t to a #cairo_rectangle_int_t by
* increasing the area to the nearest integer coordinates. We should
* standardize on #cairo_rectangle_fixed_t and #cairo_rectangle_int_t, and
* this function could be renamed to the more reasonable
* _cairo_rectangle_fixed_round.
*/
void
_cairo_box_round_to_rectangle (const cairo_box_t *box,
cairo_rectangle_int_t *rectangle)
{
rectangle->x = _cairo_fixed_integer_floor (box->p1.x);
rectangle->y = _cairo_fixed_integer_floor (box->p1.y);
rectangle->width = _cairo_fixed_integer_ceil (box->p2.x) - rectangle->x;
rectangle->height = _cairo_fixed_integer_ceil (box->p2.y) - rectangle->y;
}
cairo_bool_t
_cairo_rectangle_intersect (cairo_rectangle_int_t *dst,
const cairo_rectangle_int_t *src)
{
int x1, y1, x2, y2;
x1 = MAX (dst->x, src->x);
y1 = MAX (dst->y, src->y);
/* Beware the unsigned promotion, fortunately we have bits to spare
* as (CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN) < UINT_MAX
*/
x2 = MIN (dst->x + (int) dst->width, src->x + (int) src->width);
y2 = MIN (dst->y + (int) dst->height, src->y + (int) src->height);
if (x1 >= x2 || y1 >= y2) {
dst->x = 0;
dst->y = 0;
dst->width = 0;
dst->height = 0;
return FALSE;
} else {
dst->x = x1;
dst->y = y1;
dst->width = x2 - x1;
dst->height = y2 - y1;
return TRUE;
}
}
/* Extends the dst rectangle to also contain src.
* If one of the rectangles is empty, the result is undefined
*/
void
_cairo_rectangle_union (cairo_rectangle_int_t *dst,
const cairo_rectangle_int_t *src)
{
int x1, y1, x2, y2;
x1 = MIN (dst->x, src->x);
y1 = MIN (dst->y, src->y);
/* Beware the unsigned promotion, fortunately we have bits to spare
* as (CAIRO_RECT_INT_MAX - CAIRO_RECT_INT_MIN) < UINT_MAX
*/
x2 = MAX (dst->x + (int) dst->width, src->x + (int) src->width);
y2 = MAX (dst->y + (int) dst->height, src->y + (int) src->height);
dst->x = x1;
dst->y = y1;
dst->width = x2 - x1;
dst->height = y2 - y1;
}
#define P1x (line->p1.x)
#define P1y (line->p1.y)
#define P2x (line->p2.x)
#define P2y (line->p2.y)
#define B1x (box->p1.x)
#define B1y (box->p1.y)
#define B2x (box->p2.x)
#define B2y (box->p2.y)
/*
* Check whether any part of line intersects box. This function essentially
* computes whether the ray starting at line->p1 in the direction of line->p2
* intersects the box before it reaches p2. Normally, this is done
* by dividing by the lengths of the line projected onto each axis. Because
* we're in fixed point, this function does a bit more work to avoid having to
* do the division -- we don't care about the actual intersection point, so
* it's of no interest to us.
*/
cairo_bool_t
_cairo_box_intersects_line_segment (const cairo_box_t *box, cairo_line_t *line)
{
cairo_fixed_t t1=0, t2=0, t3=0, t4=0;
cairo_int64_t t1y, t2y, t3x, t4x;
cairo_fixed_t xlen, ylen;
if (_cairo_box_contains_point (box, &line->p1) ||
_cairo_box_contains_point (box, &line->p2))
return TRUE;
xlen = P2x - P1x;
ylen = P2y - P1y;
if (xlen) {
if (xlen > 0) {
t1 = B1x - P1x;
t2 = B2x - P1x;
} else {
t1 = P1x - B2x;
t2 = P1x - B1x;
xlen = - xlen;
}
if ((t1 < 0 || t1 > xlen) &&
(t2 < 0 || t2 > xlen))
return FALSE;
} else {
/* Fully vertical line -- check that X is in bounds */
if (P1x < B1x || P1x > B2x)
return FALSE;
}
if (ylen) {
if (ylen > 0) {
t3 = B1y - P1y;
t4 = B2y - P1y;
} else {
t3 = P1y - B2y;
t4 = P1y - B1y;
ylen = - ylen;
}
if ((t3 < 0 || t3 > ylen) &&
(t4 < 0 || t4 > ylen))
return FALSE;
} else {
/* Fully horizontal line -- check Y */
if (P1y < B1y || P1y > B2y)
return FALSE;
}
/* If we had a horizontal or vertical line, then it's already been checked */
if (P1x == P2x || P1y == P2y)
return TRUE;
/* Check overlap. Note that t1 < t2 and t3 < t4 here. */
t1y = _cairo_int32x32_64_mul (t1, ylen);
t2y = _cairo_int32x32_64_mul (t2, ylen);
t3x = _cairo_int32x32_64_mul (t3, xlen);
t4x = _cairo_int32x32_64_mul (t4, xlen);
if (_cairo_int64_lt(t1y, t4x) &&
_cairo_int64_lt(t3x, t2y))
return TRUE;
return FALSE;
}
static cairo_status_t
_cairo_box_add_spline_point (void *closure,
const cairo_point_t *point,
const cairo_slope_t *tangent)
{
_cairo_box_add_point (closure, point);
return CAIRO_STATUS_SUCCESS;
}
/* assumes a has been previously added */
void
_cairo_box_add_curve_to (cairo_box_t *extents,
const cairo_point_t *a,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
_cairo_box_add_point (extents, d);
if (!_cairo_box_contains_point (extents, b) ||
!_cairo_box_contains_point (extents, c))
{
cairo_status_t status;
status = _cairo_spline_bound (_cairo_box_add_spline_point,
extents, a, b, c, d);
assert (status == CAIRO_STATUS_SUCCESS);
}
}
void
_cairo_rectangle_int_from_double (cairo_rectangle_int_t *recti,
const cairo_rectangle_t *rectf)
{
recti->x = floor (rectf->x);
recti->y = floor (rectf->y);
recti->width = ceil (rectf->x + rectf->width) - floor (rectf->x);
recti->height = ceil (rectf->y + rectf->height) - floor (rectf->y);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-rectangular-scan-converter.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-combsort-inline.h"
#include "cairo-error-private.h"
#include "cairo-freelist-private.h"
#include "cairo-list-private.h"
#include "cairo-spans-private.h"
#include <setjmp.h>
typedef struct _rectangle {
struct _rectangle *next, *prev;
cairo_fixed_t left, right;
cairo_fixed_t top, bottom;
int32_t top_y, bottom_y;
int dir;
} rectangle_t;
#define UNROLL3(x) x x x
/* the parent is always given by index/2 */
#define PQ_PARENT_INDEX(i) ((i) >> 1)
#define PQ_FIRST_ENTRY 1
/* left and right children are index * 2 and (index * 2) +1 respectively */
#define PQ_LEFT_CHILD_INDEX(i) ((i) << 1)
typedef struct _pqueue {
int size, max_size;
rectangle_t **elements;
rectangle_t *elements_embedded[1024];
} pqueue_t;
typedef struct {
rectangle_t **start;
pqueue_t stop;
rectangle_t head, tail;
rectangle_t *insert_cursor;
int32_t current_y;
int32_t xmin, xmax;
struct coverage {
struct cell {
struct cell *prev, *next;
int x, covered, uncovered;
} head, tail, *cursor;
unsigned int count;
cairo_freepool_t pool;
} coverage;
cairo_half_open_span_t spans_stack[CAIRO_STACK_ARRAY_LENGTH (cairo_half_open_span_t)];
cairo_half_open_span_t *spans;
unsigned int num_spans;
unsigned int size_spans;
jmp_buf jmpbuf;
} sweep_line_t;
static inline int
rectangle_compare_start (const rectangle_t *a,
const rectangle_t *b)
{
int cmp;
cmp = a->top_y - b->top_y;
if (cmp)
return cmp;
return a->left - b->left;
}
static inline int
rectangle_compare_stop (const rectangle_t *a,
const rectangle_t *b)
{
return a->bottom_y - b->bottom_y;
}
static inline void
pqueue_init (pqueue_t *pq)
{
pq->max_size = ARRAY_LENGTH (pq->elements_embedded);
pq->size = 0;
pq->elements = pq->elements_embedded;
pq->elements[PQ_FIRST_ENTRY] = NULL;
}
static inline void
pqueue_fini (pqueue_t *pq)
{
if (pq->elements != pq->elements_embedded)
free (pq->elements);
}
static cairo_bool_t
pqueue_grow (pqueue_t *pq)
{
rectangle_t **new_elements;
pq->max_size *= 2;
if (pq->elements == pq->elements_embedded) {
new_elements = _cairo_malloc_ab (pq->max_size,
sizeof (rectangle_t *));
if (unlikely (new_elements == NULL))
return FALSE;
memcpy (new_elements, pq->elements_embedded,
sizeof (pq->elements_embedded));
} else {
new_elements = _cairo_realloc_ab (pq->elements,
pq->max_size,
sizeof (rectangle_t *));
if (unlikely (new_elements == NULL))
return FALSE;
}
pq->elements = new_elements;
return TRUE;
}
static inline void
pqueue_push (sweep_line_t *sweep, rectangle_t *rectangle)
{
rectangle_t **elements;
int i, parent;
if (unlikely (sweep->stop.size + 1 == sweep->stop.max_size)) {
if (unlikely (! pqueue_grow (&sweep->stop)))
longjmp (sweep->jmpbuf,
_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
elements = sweep->stop.elements;
for (i = ++sweep->stop.size;
i != PQ_FIRST_ENTRY &&
rectangle_compare_stop (rectangle,
elements[parent = PQ_PARENT_INDEX (i)]) < 0;
i = parent)
{
elements[i] = elements[parent];
}
elements[i] = rectangle;
}
static inline void
pqueue_pop (pqueue_t *pq)
{
rectangle_t **elements = pq->elements;
rectangle_t *tail;
int child, i;
tail = elements[pq->size--];
if (pq->size == 0) {
elements[PQ_FIRST_ENTRY] = NULL;
return;
}
for (i = PQ_FIRST_ENTRY;
(child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size;
i = child)
{
if (child != pq->size &&
rectangle_compare_stop (elements[child+1],
elements[child]) < 0)
{
child++;
}
if (rectangle_compare_stop (elements[child], tail) >= 0)
break;
elements[i] = elements[child];
}
elements[i] = tail;
}
static inline rectangle_t *
peek_stop (sweep_line_t *sweep)
{
return sweep->stop.elements[PQ_FIRST_ENTRY];
}
CAIRO_COMBSORT_DECLARE (rectangle_sort, rectangle_t *, rectangle_compare_start)
static void
sweep_line_init (sweep_line_t *sweep)
{
sweep->head.left = INT_MIN;
sweep->head.next = &sweep->tail;
sweep->tail.left = INT_MAX;
sweep->tail.prev = &sweep->head;
sweep->insert_cursor = &sweep->tail;
_cairo_freepool_init (&sweep->coverage.pool, sizeof (struct cell));
sweep->spans = sweep->spans_stack;
sweep->size_spans = ARRAY_LENGTH (sweep->spans_stack);
sweep->coverage.head.prev = NULL;
sweep->coverage.head.x = INT_MIN;
sweep->coverage.tail.next = NULL;
sweep->coverage.tail.x = INT_MAX;
pqueue_init (&sweep->stop);
}
static void
sweep_line_fini (sweep_line_t *sweep)
{
_cairo_freepool_fini (&sweep->coverage.pool);
pqueue_fini (&sweep->stop);
if (sweep->spans != sweep->spans_stack)
free (sweep->spans);
}
static inline void
add_cell (sweep_line_t *sweep, int x, int covered, int uncovered)
{
struct cell *cell;
cell = sweep->coverage.cursor;
if (cell->x > x) {
do {
UNROLL3({
if (cell->prev->x < x)
break;
cell = cell->prev;
})
} while (TRUE);
} else {
if (cell->x == x)
goto found;
do {
UNROLL3({
cell = cell->next;
if (cell->x >= x)
break;
})
} while (TRUE);
}
if (x != cell->x) {
struct cell *c;
sweep->coverage.count++;
c = _cairo_freepool_alloc (&sweep->coverage.pool);
if (unlikely (c == NULL)) {
longjmp (sweep->jmpbuf,
_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
cell->prev->next = c;
c->prev = cell->prev;
c->next = cell;
cell->prev = c;
c->x = x;
c->covered = 0;
c->uncovered = 0;
cell = c;
}
found:
cell->covered += covered;
cell->uncovered += uncovered;
sweep->coverage.cursor = cell;
}
static inline void
_active_edges_to_spans (sweep_line_t *sweep)
{
int32_t y = sweep->current_y;
rectangle_t *rectangle;
int coverage, prev_coverage;
int prev_x;
struct cell *cell;
sweep->num_spans = 0;
if (sweep->head.next == &sweep->tail)
return;
sweep->coverage.head.next = &sweep->coverage.tail;
sweep->coverage.tail.prev = &sweep->coverage.head;
sweep->coverage.cursor = &sweep->coverage.tail;
sweep->coverage.count = 0;
/* XXX cell coverage only changes when a rectangle appears or
* disappears. Try only modifying coverage at such times.
*/
for (rectangle = sweep->head.next;
rectangle != &sweep->tail;
rectangle = rectangle->next)
{
int height;
int frac, i;
if (y == rectangle->bottom_y) {
height = rectangle->bottom & CAIRO_FIXED_FRAC_MASK;
if (height == 0)
continue;
} else
height = CAIRO_FIXED_ONE;
if (y == rectangle->top_y)
height -= rectangle->top & CAIRO_FIXED_FRAC_MASK;
height *= rectangle->dir;
i = _cairo_fixed_integer_part (rectangle->left),
frac = _cairo_fixed_fractional_part (rectangle->left);
add_cell (sweep, i,
(CAIRO_FIXED_ONE-frac) * height,
frac * height);
i = _cairo_fixed_integer_part (rectangle->right),
frac = _cairo_fixed_fractional_part (rectangle->right);
add_cell (sweep, i,
-(CAIRO_FIXED_ONE-frac) * height,
-frac * height);
}
if (2*sweep->coverage.count >= sweep->size_spans) {
unsigned size;
size = sweep->size_spans;
while (size <= 2*sweep->coverage.count)
size <<= 1;
if (sweep->spans != sweep->spans_stack)
free (sweep->spans);
sweep->spans = _cairo_malloc_ab (size, sizeof (cairo_half_open_span_t));
if (unlikely (sweep->spans == NULL))
longjmp (sweep->jmpbuf, _cairo_error (CAIRO_STATUS_NO_MEMORY));
sweep->size_spans = size;
}
prev_coverage = coverage = 0;
prev_x = INT_MIN;
for (cell = sweep->coverage.head.next; cell != &sweep->coverage.tail; cell = cell->next) {
if (cell->x != prev_x && coverage != prev_coverage) {
int n = sweep->num_spans++;
int c = coverage >> (CAIRO_FIXED_FRAC_BITS * 2 - 8);
sweep->spans[n].x = prev_x;
sweep->spans[n].inverse = 0;
sweep->spans[n].coverage = c - (c >> 8);
prev_coverage = coverage;
}
coverage += cell->covered;
if (coverage != prev_coverage) {
int n = sweep->num_spans++;
int c = coverage >> (CAIRO_FIXED_FRAC_BITS * 2 - 8);
sweep->spans[n].x = cell->x;
sweep->spans[n].inverse = 0;
sweep->spans[n].coverage = c - (c >> 8);
prev_coverage = coverage;
}
coverage += cell->uncovered;
prev_x = cell->x + 1;
}
_cairo_freepool_reset (&sweep->coverage.pool);
if (sweep->num_spans) {
if (prev_x <= sweep->xmax) {
int n = sweep->num_spans++;
int c = coverage >> (CAIRO_FIXED_FRAC_BITS * 2 - 8);
sweep->spans[n].x = prev_x;
sweep->spans[n].inverse = 0;
sweep->spans[n].coverage = c - (c >> 8);
}
if (coverage && prev_x < sweep->xmax) {
int n = sweep->num_spans++;
sweep->spans[n].x = sweep->xmax;
sweep->spans[n].inverse = 1;
sweep->spans[n].coverage = 0;
}
}
}
static inline void
sweep_line_delete (sweep_line_t *sweep,
rectangle_t *rectangle)
{
if (sweep->insert_cursor == rectangle)
sweep->insert_cursor = rectangle->next;
rectangle->prev->next = rectangle->next;
rectangle->next->prev = rectangle->prev;
pqueue_pop (&sweep->stop);
}
static inline void
sweep_line_insert (sweep_line_t *sweep,
rectangle_t *rectangle)
{
rectangle_t *pos;
pos = sweep->insert_cursor;
if (pos->left != rectangle->left) {
if (pos->left > rectangle->left) {
do {
UNROLL3({
if (pos->prev->left < rectangle->left)
break;
pos = pos->prev;
})
} while (TRUE);
} else {
do {
UNROLL3({
pos = pos->next;
if (pos->left >= rectangle->left)
break;
});
} while (TRUE);
}
}
pos->prev->next = rectangle;
rectangle->prev = pos->prev;
rectangle->next = pos;
pos->prev = rectangle;
sweep->insert_cursor = rectangle;
pqueue_push (sweep, rectangle);
}
static void
render_rows (sweep_line_t *sweep_line,
cairo_span_renderer_t *renderer,
int height)
{
cairo_status_t status;
_active_edges_to_spans (sweep_line);
status = renderer->render_rows (renderer,
sweep_line->current_y, height,
sweep_line->spans,
sweep_line->num_spans);
if (unlikely (status))
longjmp (sweep_line->jmpbuf, status);
}
static cairo_status_t
generate (cairo_rectangular_scan_converter_t *self,
cairo_span_renderer_t *renderer,
rectangle_t **rectangles)
{
sweep_line_t sweep_line;
rectangle_t *start, *stop;
cairo_status_t status;
sweep_line_init (&sweep_line);
sweep_line.xmin = _cairo_fixed_integer_part (self->extents.p1.x);
sweep_line.xmax = _cairo_fixed_integer_part (self->extents.p2.x);
sweep_line.start = rectangles;
if ((status = setjmp (sweep_line.jmpbuf)))
goto out;
sweep_line.current_y = _cairo_fixed_integer_part (self->extents.p1.y);
start = *sweep_line.start++;
do {
if (start->top_y != sweep_line.current_y) {
render_rows (&sweep_line, renderer,
start->top_y - sweep_line.current_y);
sweep_line.current_y = start->top_y;
}
do {
sweep_line_insert (&sweep_line, start);
start = *sweep_line.start++;
if (start == NULL)
goto end;
if (start->top_y != sweep_line.current_y)
break;
} while (TRUE);
render_rows (&sweep_line, renderer, 1);
stop = peek_stop (&sweep_line);
while (stop->bottom_y == sweep_line.current_y) {
sweep_line_delete (&sweep_line, stop);
stop = peek_stop (&sweep_line);
if (stop == NULL)
break;
}
sweep_line.current_y++;
while (stop != NULL && stop->bottom_y < start->top_y) {
if (stop->bottom_y != sweep_line.current_y) {
render_rows (&sweep_line, renderer,
stop->bottom_y - sweep_line.current_y);
sweep_line.current_y = stop->bottom_y;
}
render_rows (&sweep_line, renderer, 1);
do {
sweep_line_delete (&sweep_line, stop);
stop = peek_stop (&sweep_line);
} while (stop != NULL && stop->bottom_y == sweep_line.current_y);
sweep_line.current_y++;
}
} while (TRUE);
end:
render_rows (&sweep_line, renderer, 1);
stop = peek_stop (&sweep_line);
while (stop->bottom_y == sweep_line.current_y) {
sweep_line_delete (&sweep_line, stop);
stop = peek_stop (&sweep_line);
if (stop == NULL)
goto out;
}
while (++sweep_line.current_y < _cairo_fixed_integer_part (self->extents.p2.y)) {
if (stop->bottom_y != sweep_line.current_y) {
render_rows (&sweep_line, renderer,
stop->bottom_y - sweep_line.current_y);
sweep_line.current_y = stop->bottom_y;
}
render_rows (&sweep_line, renderer, 1);
do {
sweep_line_delete (&sweep_line, stop);
stop = peek_stop (&sweep_line);
if (stop == NULL)
goto out;
} while (stop->bottom_y == sweep_line.current_y);
}
out:
sweep_line_fini (&sweep_line);
return status;
}
static void generate_row(cairo_span_renderer_t *renderer,
const rectangle_t *r,
int y, int h,
uint16_t coverage)
{
cairo_half_open_span_t spans[4];
unsigned int num_spans = 0;
int x1 = _cairo_fixed_integer_part (r->left);
int x2 = _cairo_fixed_integer_part (r->right);
if (x2 > x1) {
if (! _cairo_fixed_is_integer (r->left)) {
spans[num_spans].x = x1;
spans[num_spans].coverage =
coverage * (256 - _cairo_fixed_fractional_part (r->left)) >> 8;
num_spans++;
x1++;
}
if (x2 > x1) {
spans[num_spans].x = x1;
spans[num_spans].coverage = coverage - (coverage >> 8);
num_spans++;
}
if (! _cairo_fixed_is_integer (r->right)) {
spans[num_spans].x = x2++;
spans[num_spans].coverage =
coverage * _cairo_fixed_fractional_part (r->right) >> 8;
num_spans++;
}
} else {
spans[num_spans].x = x2++;
spans[num_spans].coverage = coverage * (r->right - r->left) >> 8;
num_spans++;
}
spans[num_spans].x = x2;
spans[num_spans].coverage = 0;
num_spans++;
renderer->render_rows (renderer, y, h, spans, num_spans);
}
static cairo_status_t
generate_box (cairo_rectangular_scan_converter_t *self,
cairo_span_renderer_t *renderer)
{
const rectangle_t *r = self->chunks.base;
int y1 = _cairo_fixed_integer_part (r->top);
int y2 = _cairo_fixed_integer_part (r->bottom);
if (y2 > y1) {
if (! _cairo_fixed_is_integer (r->top)) {
generate_row(renderer, r, y1, 1,
256 - _cairo_fixed_fractional_part (r->top));
y1++;
}
if (y2 > y1)
generate_row(renderer, r, y1, y2-y1, 256);
if (! _cairo_fixed_is_integer (r->bottom))
generate_row(renderer, r, y2, 1,
_cairo_fixed_fractional_part (r->bottom));
} else
generate_row(renderer, r, y1, 1, r->bottom - r->top);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_rectangular_scan_converter_generate (void *converter,
cairo_span_renderer_t *renderer)
{
cairo_rectangular_scan_converter_t *self = converter;
rectangle_t *rectangles_stack[CAIRO_STACK_ARRAY_LENGTH (rectangle_t *)];
rectangle_t **rectangles;
struct _cairo_rectangular_scan_converter_chunk *chunk;
cairo_status_t status;
int i, j;
if (unlikely (self->num_rectangles == 0)) {
return renderer->render_rows (renderer,
_cairo_fixed_integer_part (self->extents.p1.y),
_cairo_fixed_integer_part (self->extents.p2.y - self->extents.p1.y),
NULL, 0);
}
if (self->num_rectangles == 1)
return generate_box (self, renderer);
rectangles = rectangles_stack;
if (unlikely (self->num_rectangles >= ARRAY_LENGTH (rectangles_stack))) {
rectangles = _cairo_malloc_ab (self->num_rectangles + 1,
sizeof (rectangle_t *));
if (unlikely (rectangles == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
j = 0;
for (chunk = &self->chunks; chunk != NULL; chunk = chunk->next) {
rectangle_t *rectangle;
rectangle = chunk->base;
for (i = 0; i < chunk->count; i++)
rectangles[j++] = &rectangle[i];
}
rectangle_sort (rectangles, j);
rectangles[j] = NULL;
status = generate (self, renderer, rectangles);
if (rectangles != rectangles_stack)
free (rectangles);
return status;
}
static rectangle_t *
_allocate_rectangle (cairo_rectangular_scan_converter_t *self)
{
rectangle_t *rectangle;
struct _cairo_rectangular_scan_converter_chunk *chunk;
chunk = self->tail;
if (chunk->count == chunk->size) {
int size;
size = chunk->size * 2;
chunk->next = _cairo_malloc_ab_plus_c (size,
sizeof (rectangle_t),
sizeof (struct _cairo_rectangular_scan_converter_chunk));
if (unlikely (chunk->next == NULL))
return NULL;
chunk = chunk->next;
chunk->next = NULL;
chunk->count = 0;
chunk->size = size;
chunk->base = chunk + 1;
self->tail = chunk;
}
rectangle = chunk->base;
return rectangle + chunk->count++;
}
cairo_status_t
_cairo_rectangular_scan_converter_add_box (cairo_rectangular_scan_converter_t *self,
const cairo_box_t *box,
int dir)
{
rectangle_t *rectangle;
rectangle = _allocate_rectangle (self);
if (unlikely (rectangle == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
rectangle->dir = dir;
rectangle->left = MAX (box->p1.x, self->extents.p1.x);
rectangle->right = MIN (box->p2.x, self->extents.p2.x);
if (unlikely (rectangle->right <= rectangle->left)) {
self->tail->count--;
return CAIRO_STATUS_SUCCESS;
}
rectangle->top = MAX (box->p1.y, self->extents.p1.y);
rectangle->top_y = _cairo_fixed_integer_floor (rectangle->top);
rectangle->bottom = MIN (box->p2.y, self->extents.p2.y);
rectangle->bottom_y = _cairo_fixed_integer_floor (rectangle->bottom);
if (likely (rectangle->bottom > rectangle->top))
self->num_rectangles++;
else
self->tail->count--;
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_rectangular_scan_converter_destroy (void *converter)
{
cairo_rectangular_scan_converter_t *self = converter;
struct _cairo_rectangular_scan_converter_chunk *chunk, *next;
for (chunk = self->chunks.next; chunk != NULL; chunk = next) {
next = chunk->next;
free (chunk);
}
}
void
_cairo_rectangular_scan_converter_init (cairo_rectangular_scan_converter_t *self,
const cairo_rectangle_int_t *extents)
{
self->base.destroy = _cairo_rectangular_scan_converter_destroy;
self->base.generate = _cairo_rectangular_scan_converter_generate;
_cairo_box_from_rectangle (&self->extents, extents);
self->chunks.base = self->buf;
self->chunks.next = NULL;
self->chunks.count = 0;
self->chunks.size = sizeof (self->buf) / sizeof (rectangle_t);
self->tail = &self->chunks;
self->num_rectangles = 0;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-reference-count-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2007 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#ifndef CAIRO_REFRENCE_COUNT_PRIVATE_H
#define CAIRO_REFRENCE_COUNT_PRIVATE_H
#include "cairo-atomic-private.h"
/* Encapsulate operations on the object's reference count */
typedef struct {
cairo_atomic_int_t ref_count;
} cairo_reference_count_t;
#define _cairo_reference_count_inc(RC) _cairo_atomic_int_inc (&(RC)->ref_count)
#define _cairo_reference_count_dec(RC) _cairo_atomic_int_dec (&(RC)->ref_count)
#define _cairo_reference_count_dec_and_test(RC) _cairo_atomic_int_dec_and_test (&(RC)->ref_count)
#define CAIRO_REFERENCE_COUNT_INIT(RC, VALUE) ((RC)->ref_count = (VALUE))
#define CAIRO_REFERENCE_COUNT_GET_VALUE(RC) _cairo_atomic_int_get (&(RC)->ref_count)
#define CAIRO_REFERENCE_COUNT_INVALID_VALUE ((cairo_atomic_int_t) -1)
#define CAIRO_REFERENCE_COUNT_INVALID {CAIRO_REFERENCE_COUNT_INVALID_VALUE}
#define CAIRO_REFERENCE_COUNT_IS_INVALID(RC) (CAIRO_REFERENCE_COUNT_GET_VALUE (RC) == CAIRO_REFERENCE_COUNT_INVALID_VALUE)
#define CAIRO_REFERENCE_COUNT_HAS_REFERENCE(RC) (CAIRO_REFERENCE_COUNT_GET_VALUE (RC) > 0)
#endif
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-region-private.h | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Owen Taylor <otaylor@redhat.com>
* Vladimir Vukicevic <vladimir@pobox.com>
* Søren Sandmann <sandmann@daimi.au.dk>
*/
#ifndef CAIRO_REGION_PRIVATE_H
#define CAIRO_REGION_PRIVATE_H
#include "cairo-types-private.h"
#include "cairo-reference-count-private.h"
#include <pixman/pixman.h>
CAIRO_BEGIN_DECLS
struct _cairo_region {
cairo_reference_count_t ref_count;
cairo_status_t status;
pixman_region32_t rgn;
};
cairo_private cairo_region_t *
_cairo_region_create_in_error (cairo_status_t status);
cairo_private void
_cairo_region_init (cairo_region_t *region);
cairo_private void
_cairo_region_init_rectangle (cairo_region_t *region,
const cairo_rectangle_int_t *rectangle);
cairo_private void
_cairo_region_fini (cairo_region_t *region);
cairo_private cairo_region_t *
_cairo_region_create_from_boxes (const cairo_box_t *boxes, int count);
cairo_private cairo_box_t *
_cairo_region_get_boxes (const cairo_region_t *region, int *nbox);
CAIRO_END_DECLS
#endif /* CAIRO_REGION_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-region.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Red Hat, Inc.
*
* Contributor(s):
* Owen Taylor <otaylor@redhat.com>
* Vladimir Vukicevic <vladimir@pobox.com>
* Søren Sandmann <sandmann@daimi.au.dk>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-region-private.h"
/* XXX need to update pixman headers to be const as appropriate */
#define CONST_CAST (pixman_region32_t *)
/**
* SECTION:cairo-region
* @Title: Regions
* @Short_Description: Representing a pixel-aligned area
*
* Regions are a simple graphical data type representing an area of
* integer-aligned rectangles. They are often used on raster surfaces
* to track areas of interest, such as change or clip areas.
**/
static const cairo_region_t _cairo_region_nil = {
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
CAIRO_STATUS_NO_MEMORY, /* status */
};
cairo_region_t *
_cairo_region_create_in_error (cairo_status_t status)
{
switch (status) {
case CAIRO_STATUS_NO_MEMORY:
return (cairo_region_t *) &_cairo_region_nil;
case CAIRO_STATUS_SUCCESS:
case CAIRO_STATUS_LAST_STATUS:
ASSERT_NOT_REACHED;
/* fall-through */
case CAIRO_STATUS_SURFACE_TYPE_MISMATCH:
case CAIRO_STATUS_INVALID_STATUS:
case CAIRO_STATUS_INVALID_CONTENT:
case CAIRO_STATUS_INVALID_FORMAT:
case CAIRO_STATUS_INVALID_VISUAL:
case CAIRO_STATUS_READ_ERROR:
case CAIRO_STATUS_WRITE_ERROR:
case CAIRO_STATUS_FILE_NOT_FOUND:
case CAIRO_STATUS_TEMP_FILE_ERROR:
case CAIRO_STATUS_INVALID_STRIDE:
case CAIRO_STATUS_INVALID_SIZE:
case CAIRO_STATUS_DEVICE_TYPE_MISMATCH:
case CAIRO_STATUS_DEVICE_ERROR:
case CAIRO_STATUS_INVALID_RESTORE:
case CAIRO_STATUS_INVALID_POP_GROUP:
case CAIRO_STATUS_NO_CURRENT_POINT:
case CAIRO_STATUS_INVALID_MATRIX:
case CAIRO_STATUS_NULL_POINTER:
case CAIRO_STATUS_INVALID_STRING:
case CAIRO_STATUS_INVALID_PATH_DATA:
case CAIRO_STATUS_SURFACE_FINISHED:
case CAIRO_STATUS_PATTERN_TYPE_MISMATCH:
case CAIRO_STATUS_INVALID_DASH:
case CAIRO_STATUS_INVALID_DSC_COMMENT:
case CAIRO_STATUS_INVALID_INDEX:
case CAIRO_STATUS_CLIP_NOT_REPRESENTABLE:
case CAIRO_STATUS_FONT_TYPE_MISMATCH:
case CAIRO_STATUS_USER_FONT_IMMUTABLE:
case CAIRO_STATUS_USER_FONT_ERROR:
case CAIRO_STATUS_NEGATIVE_COUNT:
case CAIRO_STATUS_INVALID_CLUSTERS:
case CAIRO_STATUS_INVALID_SLANT:
case CAIRO_STATUS_INVALID_WEIGHT:
case CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED:
case CAIRO_STATUS_INVALID_MESH_CONSTRUCTION:
case CAIRO_STATUS_DEVICE_FINISHED:
case CAIRO_STATUS_JBIG2_GLOBAL_MISSING:
case CAIRO_STATUS_PNG_ERROR:
case CAIRO_STATUS_FREETYPE_ERROR:
case CAIRO_STATUS_WIN32_GDI_ERROR:
case CAIRO_STATUS_TAG_ERROR:
default:
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_region_t *) &_cairo_region_nil;
}
}
/**
* _cairo_region_set_error:
* @region: a region
* @status: a status value indicating an error
*
* Atomically sets region->status to @status and calls _cairo_error;
* Does nothing if status is %CAIRO_STATUS_SUCCESS or any of the internal
* status values.
*
* All assignments of an error status to region->status should happen
* through _cairo_region_set_error(). Note that due to the nature of
* the atomic operation, it is not safe to call this function on the
* nil objects.
*
* The purpose of this function is to allow the user to set a
* breakpoint in _cairo_error() to generate a stack trace for when the
* user causes cairo to detect an error.
*
* Return value: the error status.
**/
static cairo_status_t
_cairo_region_set_error (cairo_region_t *region,
cairo_status_t status)
{
if (status == CAIRO_STATUS_SUCCESS)
return CAIRO_STATUS_SUCCESS;
/* Don't overwrite an existing error. This preserves the first
* error, which is the most significant. */
_cairo_status_set_error (®ion->status, status);
return _cairo_error (status);
}
void
_cairo_region_init (cairo_region_t *region)
{
VG (VALGRIND_MAKE_MEM_UNDEFINED (region, sizeof (cairo_region_t)));
region->status = CAIRO_STATUS_SUCCESS;
CAIRO_REFERENCE_COUNT_INIT (®ion->ref_count, 0);
pixman_region32_init (®ion->rgn);
}
void
_cairo_region_init_rectangle (cairo_region_t *region,
const cairo_rectangle_int_t *rectangle)
{
VG (VALGRIND_MAKE_MEM_UNDEFINED (region, sizeof (cairo_region_t)));
region->status = CAIRO_STATUS_SUCCESS;
CAIRO_REFERENCE_COUNT_INIT (®ion->ref_count, 0);
pixman_region32_init_rect (®ion->rgn,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
}
void
_cairo_region_fini (cairo_region_t *region)
{
assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (®ion->ref_count));
pixman_region32_fini (®ion->rgn);
VG (VALGRIND_MAKE_MEM_UNDEFINED (region, sizeof (cairo_region_t)));
}
/**
* cairo_region_create:
*
* Allocates a new empty region object.
*
* Return value: A newly allocated #cairo_region_t. Free with
* cairo_region_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_region_status().
*
* Since: 1.10
**/
cairo_region_t *
cairo_region_create (void)
{
cairo_region_t *region;
region = _cairo_malloc (sizeof (cairo_region_t));
if (region == NULL)
return (cairo_region_t *) &_cairo_region_nil;
region->status = CAIRO_STATUS_SUCCESS;
CAIRO_REFERENCE_COUNT_INIT (®ion->ref_count, 1);
pixman_region32_init (®ion->rgn);
return region;
}
slim_hidden_def (cairo_region_create);
/**
* cairo_region_create_rectangles:
* @rects: an array of @count rectangles
* @count: number of rectangles
*
* Allocates a new region object containing the union of all given @rects.
*
* Return value: A newly allocated #cairo_region_t. Free with
* cairo_region_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_region_status().
*
* Since: 1.10
**/
cairo_region_t *
cairo_region_create_rectangles (const cairo_rectangle_int_t *rects,
int count)
{
pixman_box32_t stack_pboxes[CAIRO_STACK_ARRAY_LENGTH (pixman_box32_t)];
pixman_box32_t *pboxes = stack_pboxes;
cairo_region_t *region;
int i;
region = _cairo_malloc (sizeof (cairo_region_t));
if (unlikely (region == NULL))
return _cairo_region_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
CAIRO_REFERENCE_COUNT_INIT (®ion->ref_count, 1);
region->status = CAIRO_STATUS_SUCCESS;
if (count == 1) {
pixman_region32_init_rect (®ion->rgn,
rects->x, rects->y,
rects->width, rects->height);
return region;
}
if (count > ARRAY_LENGTH (stack_pboxes)) {
pboxes = _cairo_malloc_ab (count, sizeof (pixman_box32_t));
if (unlikely (pboxes == NULL)) {
free (region);
return _cairo_region_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
}
for (i = 0; i < count; i++) {
pboxes[i].x1 = rects[i].x;
pboxes[i].y1 = rects[i].y;
pboxes[i].x2 = rects[i].x + rects[i].width;
pboxes[i].y2 = rects[i].y + rects[i].height;
}
i = pixman_region32_init_rects (®ion->rgn, pboxes, count);
if (pboxes != stack_pboxes)
free (pboxes);
if (unlikely (i == 0)) {
free (region);
return _cairo_region_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
return region;
}
slim_hidden_def (cairo_region_create_rectangles);
cairo_region_t *
_cairo_region_create_from_boxes (const cairo_box_t *boxes, int count)
{
cairo_region_t *region;
region = _cairo_malloc (sizeof (cairo_region_t));
if (unlikely (region == NULL))
return _cairo_region_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
CAIRO_REFERENCE_COUNT_INIT (®ion->ref_count, 1);
region->status = CAIRO_STATUS_SUCCESS;
if (! pixman_region32_init_rects (®ion->rgn,
(pixman_box32_t *)boxes, count)) {
free (region);
return _cairo_region_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
}
return region;
}
cairo_box_t *
_cairo_region_get_boxes (const cairo_region_t *region, int *nbox)
{
if (region->status) {
nbox = 0;
return NULL;
}
return (cairo_box_t *) pixman_region32_rectangles (CONST_CAST ®ion->rgn, nbox);
}
/**
* cairo_region_create_rectangle:
* @rectangle: a #cairo_rectangle_int_t
*
* Allocates a new region object containing @rectangle.
*
* Return value: A newly allocated #cairo_region_t. Free with
* cairo_region_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_region_status().
*
* Since: 1.10
**/
cairo_region_t *
cairo_region_create_rectangle (const cairo_rectangle_int_t *rectangle)
{
cairo_region_t *region;
region = _cairo_malloc (sizeof (cairo_region_t));
if (unlikely (region == NULL))
return (cairo_region_t *) &_cairo_region_nil;
region->status = CAIRO_STATUS_SUCCESS;
CAIRO_REFERENCE_COUNT_INIT (®ion->ref_count, 1);
pixman_region32_init_rect (®ion->rgn,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
return region;
}
slim_hidden_def (cairo_region_create_rectangle);
/**
* cairo_region_copy:
* @original: a #cairo_region_t
*
* Allocates a new region object copying the area from @original.
*
* Return value: A newly allocated #cairo_region_t. Free with
* cairo_region_destroy(). This function always returns a
* valid pointer; if memory cannot be allocated, then a special
* error object is returned where all operations on the object do nothing.
* You can check for this with cairo_region_status().
*
* Since: 1.10
**/
cairo_region_t *
cairo_region_copy (const cairo_region_t *original)
{
cairo_region_t *copy;
if (original != NULL && original->status)
return (cairo_region_t *) &_cairo_region_nil;
copy = cairo_region_create ();
if (unlikely (copy->status))
return copy;
if (original != NULL &&
! pixman_region32_copy (©->rgn, CONST_CAST &original->rgn))
{
cairo_region_destroy (copy);
return (cairo_region_t *) &_cairo_region_nil;
}
return copy;
}
slim_hidden_def (cairo_region_copy);
/**
* cairo_region_reference:
* @region: a #cairo_region_t
*
* Increases the reference count on @region by one. This prevents
* @region from being destroyed until a matching call to
* cairo_region_destroy() is made.
*
* Return value: the referenced #cairo_region_t.
*
* Since: 1.10
**/
cairo_region_t *
cairo_region_reference (cairo_region_t *region)
{
if (region == NULL || CAIRO_REFERENCE_COUNT_IS_INVALID (®ion->ref_count))
return NULL;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (®ion->ref_count));
_cairo_reference_count_inc (®ion->ref_count);
return region;
}
slim_hidden_def (cairo_region_reference);
/**
* cairo_region_destroy:
* @region: a #cairo_region_t
*
* Destroys a #cairo_region_t object created with
* cairo_region_create(), cairo_region_copy(), or
* or cairo_region_create_rectangle().
*
* Since: 1.10
**/
void
cairo_region_destroy (cairo_region_t *region)
{
if (region == NULL || CAIRO_REFERENCE_COUNT_IS_INVALID (®ion->ref_count))
return;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (®ion->ref_count));
if (! _cairo_reference_count_dec_and_test (®ion->ref_count))
return;
_cairo_region_fini (region);
free (region);
}
slim_hidden_def (cairo_region_destroy);
/**
* cairo_region_num_rectangles:
* @region: a #cairo_region_t
*
* Returns the number of rectangles contained in @region.
*
* Return value: The number of rectangles contained in @region.
*
* Since: 1.10
**/
int
cairo_region_num_rectangles (const cairo_region_t *region)
{
if (region->status)
return 0;
return pixman_region32_n_rects (CONST_CAST ®ion->rgn);
}
slim_hidden_def (cairo_region_num_rectangles);
/**
* cairo_region_get_rectangle:
* @region: a #cairo_region_t
* @nth: a number indicating which rectangle should be returned
* @rectangle: return location for a #cairo_rectangle_int_t
*
* Stores the @nth rectangle from the region in @rectangle.
*
* Since: 1.10
**/
void
cairo_region_get_rectangle (const cairo_region_t *region,
int nth,
cairo_rectangle_int_t *rectangle)
{
pixman_box32_t *pbox;
if (region->status) {
rectangle->x = rectangle->y = 0;
rectangle->width = rectangle->height = 0;
return;
}
pbox = pixman_region32_rectangles (CONST_CAST ®ion->rgn, NULL) + nth;
rectangle->x = pbox->x1;
rectangle->y = pbox->y1;
rectangle->width = pbox->x2 - pbox->x1;
rectangle->height = pbox->y2 - pbox->y1;
}
slim_hidden_def (cairo_region_get_rectangle);
/**
* cairo_region_get_extents:
* @region: a #cairo_region_t
* @extents: rectangle into which to store the extents
*
* Gets the bounding rectangle of @region as a #cairo_rectangle_int_t
*
* Since: 1.10
**/
void
cairo_region_get_extents (const cairo_region_t *region,
cairo_rectangle_int_t *extents)
{
pixman_box32_t *pextents;
if (region->status) {
extents->x = extents->y = 0;
extents->width = extents->height = 0;
return;
}
pextents = pixman_region32_extents (CONST_CAST ®ion->rgn);
extents->x = pextents->x1;
extents->y = pextents->y1;
extents->width = pextents->x2 - pextents->x1;
extents->height = pextents->y2 - pextents->y1;
}
slim_hidden_def (cairo_region_get_extents);
/**
* cairo_region_status:
* @region: a #cairo_region_t
*
* Checks whether an error has previous occurred for this
* region object.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_status (const cairo_region_t *region)
{
return region->status;
}
slim_hidden_def (cairo_region_status);
/**
* cairo_region_subtract:
* @dst: a #cairo_region_t
* @other: another #cairo_region_t
*
* Subtracts @other from @dst and places the result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_subtract (cairo_region_t *dst, const cairo_region_t *other)
{
if (dst->status)
return dst->status;
if (other->status)
return _cairo_region_set_error (dst, other->status);
if (! pixman_region32_subtract (&dst->rgn,
&dst->rgn,
CONST_CAST &other->rgn))
{
return _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
}
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_region_subtract);
/**
* cairo_region_subtract_rectangle:
* @dst: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Subtracts @rectangle from @dst and places the result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_subtract_rectangle (cairo_region_t *dst,
const cairo_rectangle_int_t *rectangle)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
pixman_region32_t region;
if (dst->status)
return dst->status;
pixman_region32_init_rect (®ion,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
if (! pixman_region32_subtract (&dst->rgn, &dst->rgn, ®ion))
status = _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
pixman_region32_fini (®ion);
return status;
}
slim_hidden_def (cairo_region_subtract_rectangle);
/**
* cairo_region_intersect:
* @dst: a #cairo_region_t
* @other: another #cairo_region_t
*
* Computes the intersection of @dst with @other and places the result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_intersect (cairo_region_t *dst, const cairo_region_t *other)
{
if (dst->status)
return dst->status;
if (other->status)
return _cairo_region_set_error (dst, other->status);
if (! pixman_region32_intersect (&dst->rgn, &dst->rgn, CONST_CAST &other->rgn))
return _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_region_intersect);
/**
* cairo_region_intersect_rectangle:
* @dst: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Computes the intersection of @dst with @rectangle and places the
* result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_intersect_rectangle (cairo_region_t *dst,
const cairo_rectangle_int_t *rectangle)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
pixman_region32_t region;
if (dst->status)
return dst->status;
pixman_region32_init_rect (®ion,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
if (! pixman_region32_intersect (&dst->rgn, &dst->rgn, ®ion))
status = _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
pixman_region32_fini (®ion);
return status;
}
slim_hidden_def (cairo_region_intersect_rectangle);
/**
* cairo_region_union:
* @dst: a #cairo_region_t
* @other: another #cairo_region_t
*
* Computes the union of @dst with @other and places the result in @dst
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_union (cairo_region_t *dst,
const cairo_region_t *other)
{
if (dst->status)
return dst->status;
if (other->status)
return _cairo_region_set_error (dst, other->status);
if (! pixman_region32_union (&dst->rgn, &dst->rgn, CONST_CAST &other->rgn))
return _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
slim_hidden_def (cairo_region_union);
/**
* cairo_region_union_rectangle:
* @dst: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Computes the union of @dst with @rectangle and places the result in @dst.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_union_rectangle (cairo_region_t *dst,
const cairo_rectangle_int_t *rectangle)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
pixman_region32_t region;
if (dst->status)
return dst->status;
pixman_region32_init_rect (®ion,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
if (! pixman_region32_union (&dst->rgn, &dst->rgn, ®ion))
status = _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
pixman_region32_fini (®ion);
return status;
}
slim_hidden_def (cairo_region_union_rectangle);
/**
* cairo_region_xor:
* @dst: a #cairo_region_t
* @other: another #cairo_region_t
*
* Computes the exclusive difference of @dst with @other and places the
* result in @dst. That is, @dst will be set to contain all areas that
* are either in @dst or in @other, but not in both.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_xor (cairo_region_t *dst, const cairo_region_t *other)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
pixman_region32_t tmp;
if (dst->status)
return dst->status;
if (other->status)
return _cairo_region_set_error (dst, other->status);
pixman_region32_init (&tmp);
/* XXX: get an xor function into pixman */
if (! pixman_region32_subtract (&tmp, CONST_CAST &other->rgn, &dst->rgn) ||
! pixman_region32_subtract (&dst->rgn, &dst->rgn, CONST_CAST &other->rgn) ||
! pixman_region32_union (&dst->rgn, &dst->rgn, &tmp))
status = _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
pixman_region32_fini (&tmp);
return status;
}
slim_hidden_def (cairo_region_xor);
/**
* cairo_region_xor_rectangle:
* @dst: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Computes the exclusive difference of @dst with @rectangle and places the
* result in @dst. That is, @dst will be set to contain all areas that are
* either in @dst or in @rectangle, but not in both.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
*
* Since: 1.10
**/
cairo_status_t
cairo_region_xor_rectangle (cairo_region_t *dst,
const cairo_rectangle_int_t *rectangle)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
pixman_region32_t region, tmp;
if (dst->status)
return dst->status;
pixman_region32_init_rect (®ion,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
pixman_region32_init (&tmp);
/* XXX: get an xor function into pixman */
if (! pixman_region32_subtract (&tmp, ®ion, &dst->rgn) ||
! pixman_region32_subtract (&dst->rgn, &dst->rgn, ®ion) ||
! pixman_region32_union (&dst->rgn, &dst->rgn, &tmp))
status = _cairo_region_set_error (dst, CAIRO_STATUS_NO_MEMORY);
pixman_region32_fini (&tmp);
pixman_region32_fini (®ion);
return status;
}
slim_hidden_def (cairo_region_xor_rectangle);
/**
* cairo_region_is_empty:
* @region: a #cairo_region_t
*
* Checks whether @region is empty.
*
* Return value: %TRUE if @region is empty, %FALSE if it isn't.
*
* Since: 1.10
**/
cairo_bool_t
cairo_region_is_empty (const cairo_region_t *region)
{
if (region->status)
return TRUE;
return ! pixman_region32_not_empty (CONST_CAST ®ion->rgn);
}
slim_hidden_def (cairo_region_is_empty);
/**
* cairo_region_translate:
* @region: a #cairo_region_t
* @dx: Amount to translate in the x direction
* @dy: Amount to translate in the y direction
*
* Translates @region by (@dx, @dy).
*
* Since: 1.10
**/
void
cairo_region_translate (cairo_region_t *region,
int dx, int dy)
{
if (region->status)
return;
pixman_region32_translate (®ion->rgn, dx, dy);
}
slim_hidden_def (cairo_region_translate);
/**
* cairo_region_contains_rectangle:
* @region: a #cairo_region_t
* @rectangle: a #cairo_rectangle_int_t
*
* Checks whether @rectangle is inside, outside or partially contained
* in @region
*
* Return value:
* %CAIRO_REGION_OVERLAP_IN if @rectangle is entirely inside @region,
* %CAIRO_REGION_OVERLAP_OUT if @rectangle is entirely outside @region, or
* %CAIRO_REGION_OVERLAP_PART if @rectangle is partially inside and partially outside @region.
*
* Since: 1.10
**/
cairo_region_overlap_t
cairo_region_contains_rectangle (const cairo_region_t *region,
const cairo_rectangle_int_t *rectangle)
{
pixman_box32_t pbox;
pixman_region_overlap_t poverlap;
if (region->status)
return CAIRO_REGION_OVERLAP_OUT;
pbox.x1 = rectangle->x;
pbox.y1 = rectangle->y;
pbox.x2 = rectangle->x + rectangle->width;
pbox.y2 = rectangle->y + rectangle->height;
poverlap = pixman_region32_contains_rectangle (CONST_CAST ®ion->rgn,
&pbox);
switch (poverlap) {
default:
case PIXMAN_REGION_OUT: return CAIRO_REGION_OVERLAP_OUT;
case PIXMAN_REGION_IN: return CAIRO_REGION_OVERLAP_IN;
case PIXMAN_REGION_PART: return CAIRO_REGION_OVERLAP_PART;
}
}
slim_hidden_def (cairo_region_contains_rectangle);
/**
* cairo_region_contains_point:
* @region: a #cairo_region_t
* @x: the x coordinate of a point
* @y: the y coordinate of a point
*
* Checks whether (@x, @y) is contained in @region.
*
* Return value: %TRUE if (@x, @y) is contained in @region, %FALSE if it is not.
*
* Since: 1.10
**/
cairo_bool_t
cairo_region_contains_point (const cairo_region_t *region,
int x, int y)
{
pixman_box32_t box;
if (region->status)
return FALSE;
return pixman_region32_contains_point (CONST_CAST ®ion->rgn, x, y, &box);
}
slim_hidden_def (cairo_region_contains_point);
/**
* cairo_region_equal:
* @a: a #cairo_region_t or %NULL
* @b: a #cairo_region_t or %NULL
*
* Compares whether region_a is equivalent to region_b. %NULL as an argument
* is equal to itself, but not to any non-%NULL region.
*
* Return value: %TRUE if both regions contained the same coverage,
* %FALSE if it is not or any region is in an error status.
*
* Since: 1.10
**/
cairo_bool_t
cairo_region_equal (const cairo_region_t *a,
const cairo_region_t *b)
{
/* error objects are never equal */
if ((a != NULL && a->status) || (b != NULL && b->status))
return FALSE;
if (a == b)
return TRUE;
if (a == NULL || b == NULL)
return FALSE;
return pixman_region32_equal (CONST_CAST &a->rgn, CONST_CAST &b->rgn);
}
slim_hidden_def (cairo_region_equal);
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-rtree-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Chris Wilson.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*
*/
#ifndef CAIRO_RTREE_PRIVATE_H
#define CAIRO_RTREE_PRIVATE_H
#include "cairo-compiler-private.h"
#include "cairo-error-private.h"
#include "cairo-types-private.h"
#include "cairo-freelist-private.h"
#include "cairo-list-inline.h"
enum {
CAIRO_RTREE_NODE_AVAILABLE,
CAIRO_RTREE_NODE_DIVIDED,
CAIRO_RTREE_NODE_OCCUPIED,
};
typedef struct _cairo_rtree_node {
struct _cairo_rtree_node *children[4], *parent;
cairo_list_t link;
uint16_t pinned;
uint16_t state;
uint16_t x, y;
uint16_t width, height;
} cairo_rtree_node_t;
typedef struct _cairo_rtree {
cairo_rtree_node_t root;
int min_size;
cairo_list_t pinned;
cairo_list_t available;
cairo_list_t evictable;
void (*destroy) (cairo_rtree_node_t *);
cairo_freepool_t node_freepool;
} cairo_rtree_t;
cairo_private cairo_rtree_node_t *
_cairo_rtree_node_create (cairo_rtree_t *rtree,
cairo_rtree_node_t *parent,
int x,
int y,
int width,
int height);
cairo_private cairo_status_t
_cairo_rtree_node_insert (cairo_rtree_t *rtree,
cairo_rtree_node_t *node,
int width,
int height,
cairo_rtree_node_t **out);
cairo_private void
_cairo_rtree_node_collapse (cairo_rtree_t *rtree, cairo_rtree_node_t *node);
cairo_private void
_cairo_rtree_node_remove (cairo_rtree_t *rtree, cairo_rtree_node_t *node);
cairo_private void
_cairo_rtree_node_destroy (cairo_rtree_t *rtree, cairo_rtree_node_t *node);
cairo_private void
_cairo_rtree_init (cairo_rtree_t *rtree,
int width,
int height,
int min_size,
int node_size,
void (*destroy)(cairo_rtree_node_t *));
cairo_private cairo_int_status_t
_cairo_rtree_insert (cairo_rtree_t *rtree,
int width,
int height,
cairo_rtree_node_t **out);
cairo_private cairo_int_status_t
_cairo_rtree_evict_random (cairo_rtree_t *rtree,
int width,
int height,
cairo_rtree_node_t **out);
cairo_private void
_cairo_rtree_foreach (cairo_rtree_t *rtree,
void (*func)(cairo_rtree_node_t *, void *data),
void *data);
static inline void *
_cairo_rtree_pin (cairo_rtree_t *rtree, cairo_rtree_node_t *node)
{
assert (node->state == CAIRO_RTREE_NODE_OCCUPIED);
if (! node->pinned) {
cairo_list_move (&node->link, &rtree->pinned);
node->pinned = 1;
}
return node;
}
cairo_private void
_cairo_rtree_unpin (cairo_rtree_t *rtree);
cairo_private void
_cairo_rtree_reset (cairo_rtree_t *rtree);
cairo_private void
_cairo_rtree_fini (cairo_rtree_t *rtree);
#endif /* CAIRO_RTREE_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-rtree.c | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2009 Chris Wilson
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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 Chris Wilson.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-rtree-private.h"
cairo_rtree_node_t *
_cairo_rtree_node_create (cairo_rtree_t *rtree,
cairo_rtree_node_t *parent,
int x,
int y,
int width,
int height)
{
cairo_rtree_node_t *node;
node = _cairo_freepool_alloc (&rtree->node_freepool);
if (node == NULL) {
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return NULL;
}
node->children[0] = NULL;
node->parent = parent;
node->state = CAIRO_RTREE_NODE_AVAILABLE;
node->pinned = FALSE;
node->x = x;
node->y = y;
node->width = width;
node->height = height;
cairo_list_add (&node->link, &rtree->available);
return node;
}
void
_cairo_rtree_node_destroy (cairo_rtree_t *rtree, cairo_rtree_node_t *node)
{
int i;
cairo_list_del (&node->link);
if (node->state == CAIRO_RTREE_NODE_OCCUPIED) {
rtree->destroy (node);
} else {
for (i = 0; i < 4 && node->children[i] != NULL; i++)
_cairo_rtree_node_destroy (rtree, node->children[i]);
}
_cairo_freepool_free (&rtree->node_freepool, node);
}
void
_cairo_rtree_node_collapse (cairo_rtree_t *rtree, cairo_rtree_node_t *node)
{
int i;
do {
assert (node->state == CAIRO_RTREE_NODE_DIVIDED);
for (i = 0; i < 4 && node->children[i] != NULL; i++)
if (node->children[i]->state != CAIRO_RTREE_NODE_AVAILABLE)
return;
for (i = 0; i < 4 && node->children[i] != NULL; i++)
_cairo_rtree_node_destroy (rtree, node->children[i]);
node->children[0] = NULL;
node->state = CAIRO_RTREE_NODE_AVAILABLE;
cairo_list_move (&node->link, &rtree->available);
} while ((node = node->parent) != NULL);
}
cairo_status_t
_cairo_rtree_node_insert (cairo_rtree_t *rtree,
cairo_rtree_node_t *node,
int width,
int height,
cairo_rtree_node_t **out)
{
int w, h, i;
assert (node->state == CAIRO_RTREE_NODE_AVAILABLE);
assert (node->pinned == FALSE);
if (node->width - width > rtree->min_size ||
node->height - height > rtree->min_size)
{
w = node->width - width;
h = node->height - height;
i = 0;
node->children[i] = _cairo_rtree_node_create (rtree, node,
node->x, node->y,
width, height);
if (unlikely (node->children[i] == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
i++;
if (w > rtree->min_size) {
node->children[i] = _cairo_rtree_node_create (rtree, node,
node->x + width,
node->y,
w, height);
if (unlikely (node->children[i] == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
i++;
}
if (h > rtree->min_size) {
node->children[i] = _cairo_rtree_node_create (rtree, node,
node->x,
node->y + height,
width, h);
if (unlikely (node->children[i] == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
i++;
if (w > rtree->min_size) {
node->children[i] = _cairo_rtree_node_create (rtree, node,
node->x + width,
node->y + height,
w, h);
if (unlikely (node->children[i] == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
i++;
}
}
if (i < 4)
node->children[i] = NULL;
node->state = CAIRO_RTREE_NODE_DIVIDED;
cairo_list_move (&node->link, &rtree->evictable);
node = node->children[0];
}
node->state = CAIRO_RTREE_NODE_OCCUPIED;
cairo_list_move (&node->link, &rtree->evictable);
*out = node;
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_rtree_node_remove (cairo_rtree_t *rtree, cairo_rtree_node_t *node)
{
assert (node->state == CAIRO_RTREE_NODE_OCCUPIED);
assert (node->pinned == FALSE);
rtree->destroy (node);
node->state = CAIRO_RTREE_NODE_AVAILABLE;
cairo_list_move (&node->link, &rtree->available);
_cairo_rtree_node_collapse (rtree, node->parent);
}
cairo_int_status_t
_cairo_rtree_insert (cairo_rtree_t *rtree,
int width,
int height,
cairo_rtree_node_t **out)
{
cairo_rtree_node_t *node;
cairo_list_foreach_entry (node, cairo_rtree_node_t,
&rtree->available, link)
{
if (node->width >= width && node->height >= height)
return _cairo_rtree_node_insert (rtree, node, width, height, out);
}
return CAIRO_INT_STATUS_UNSUPPORTED;
}
static uint32_t
hars_petruska_f54_1_random (void)
{
#define rol(x,k) ((x << k) | (x >> (32-k)))
static uint32_t x;
return x = (x ^ rol (x, 5) ^ rol (x, 24)) + 0x37798849;
#undef rol
}
cairo_int_status_t
_cairo_rtree_evict_random (cairo_rtree_t *rtree,
int width,
int height,
cairo_rtree_node_t **out)
{
cairo_int_status_t ret = CAIRO_INT_STATUS_UNSUPPORTED;
cairo_rtree_node_t *node, *next;
cairo_list_t tmp_pinned;
int i, cnt;
cairo_list_init (&tmp_pinned);
/* propagate pinned from children to root */
cairo_list_foreach_entry_safe (node, next,
cairo_rtree_node_t, &rtree->pinned, link) {
node = node->parent;
while (node && ! node->pinned) {
node->pinned = 1;
cairo_list_move (&node->link, &tmp_pinned);
node = node->parent;
}
}
cnt = 0;
cairo_list_foreach_entry (node, cairo_rtree_node_t,
&rtree->evictable, link)
{
if (node->width >= width && node->height >= height)
cnt++;
}
if (cnt == 0)
goto out;
cnt = hars_petruska_f54_1_random () % cnt;
cairo_list_foreach_entry (node, cairo_rtree_node_t,
&rtree->evictable, link)
{
if (node->width >= width && node->height >= height && cnt-- == 0) {
if (node->state == CAIRO_RTREE_NODE_OCCUPIED) {
rtree->destroy (node);
} else {
for (i = 0; i < 4 && node->children[i] != NULL; i++)
_cairo_rtree_node_destroy (rtree, node->children[i]);
node->children[0] = NULL;
}
node->state = CAIRO_RTREE_NODE_AVAILABLE;
cairo_list_move (&node->link, &rtree->available);
*out = node;
ret = CAIRO_STATUS_SUCCESS;
break;
}
}
out:
while (! cairo_list_is_empty (&tmp_pinned)) {
node = cairo_list_first_entry (&tmp_pinned, cairo_rtree_node_t, link);
node->pinned = 0;
cairo_list_move (&node->link, &rtree->evictable);
}
return ret;
}
void
_cairo_rtree_unpin (cairo_rtree_t *rtree)
{
while (! cairo_list_is_empty (&rtree->pinned)) {
cairo_rtree_node_t *node = cairo_list_first_entry (&rtree->pinned,
cairo_rtree_node_t,
link);
node->pinned = 0;
cairo_list_move (&node->link, &rtree->evictable);
}
}
void
_cairo_rtree_init (cairo_rtree_t *rtree,
int width,
int height,
int min_size,
int node_size,
void (*destroy) (cairo_rtree_node_t *))
{
assert (node_size >= (int) sizeof (cairo_rtree_node_t));
_cairo_freepool_init (&rtree->node_freepool, node_size);
cairo_list_init (&rtree->available);
cairo_list_init (&rtree->pinned);
cairo_list_init (&rtree->evictable);
rtree->min_size = min_size;
rtree->destroy = destroy;
memset (&rtree->root, 0, sizeof (rtree->root));
rtree->root.width = width;
rtree->root.height = height;
rtree->root.state = CAIRO_RTREE_NODE_AVAILABLE;
cairo_list_add (&rtree->root.link, &rtree->available);
}
void
_cairo_rtree_reset (cairo_rtree_t *rtree)
{
int i;
if (rtree->root.state == CAIRO_RTREE_NODE_OCCUPIED) {
rtree->destroy (&rtree->root);
} else {
for (i = 0; i < 4 && rtree->root.children[i] != NULL; i++)
_cairo_rtree_node_destroy (rtree, rtree->root.children[i]);
rtree->root.children[0] = NULL;
}
cairo_list_init (&rtree->available);
cairo_list_init (&rtree->evictable);
cairo_list_init (&rtree->pinned);
rtree->root.state = CAIRO_RTREE_NODE_AVAILABLE;
rtree->root.pinned = FALSE;
cairo_list_add (&rtree->root.link, &rtree->available);
}
static void
_cairo_rtree_node_foreach (cairo_rtree_node_t *node,
void (*func)(cairo_rtree_node_t *, void *data),
void *data)
{
int i;
for (i = 0; i < 4 && node->children[i] != NULL; i++)
_cairo_rtree_node_foreach(node->children[i], func, data);
func(node, data);
}
void
_cairo_rtree_foreach (cairo_rtree_t *rtree,
void (*func)(cairo_rtree_node_t *, void *data),
void *data)
{
int i;
if (rtree->root.state == CAIRO_RTREE_NODE_OCCUPIED) {
func(&rtree->root, data);
} else {
for (i = 0; i < 4 && rtree->root.children[i] != NULL; i++)
_cairo_rtree_node_foreach (rtree->root.children[i], func, data);
}
}
void
_cairo_rtree_fini (cairo_rtree_t *rtree)
{
int i;
if (rtree->root.state == CAIRO_RTREE_NODE_OCCUPIED) {
rtree->destroy (&rtree->root);
} else {
for (i = 0; i < 4 && rtree->root.children[i] != NULL; i++)
_cairo_rtree_node_destroy (rtree, rtree->root.children[i]);
}
_cairo_freepool_fini (&rtree->node_freepool);
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-scaled-font-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2002 University of Southern California
* Copyright © 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#ifndef CAIRO_SCALED_FONT_PRIVATE_H
#define CAIRO_SCALED_FONT_PRIVATE_H
#include "cairo.h"
#include "cairo-types-private.h"
#include "cairo-list-private.h"
#include "cairo-mutex-type-private.h"
#include "cairo-reference-count-private.h"
CAIRO_BEGIN_DECLS
typedef struct _cairo_scaled_glyph_page cairo_scaled_glyph_page_t;
struct _cairo_scaled_font {
/* For most cairo objects, the rule for multiple threads is that
* the user is responsible for any locking if the same object is
* manipulated from multiple threads simultaneously.
*
* However, with the caching that cairo does for scaled fonts, a
* user can easily end up with the same cairo_scaled_font object
* being manipulated from multiple threads without the user ever
* being aware of this, (and in fact, unable to control it).
*
* So, as a special exception, the cairo implementation takes care
* of all locking needed for cairo_scaled_font_t. Most of what is
* in the scaled font is immutable, (which is what allows for the
* sharing in the first place). The things that are modified and
* the locks protecting them are as follows:
*
* 1. The reference count (scaled_font->ref_count)
*
* Modifications to the reference count are protected by the
* _cairo_scaled_font_map_mutex. This is because the reference
* count of a scaled font is intimately related with the font
* map itself, (and the magic holdovers array).
*
* 2. The cache of glyphs (scaled_font->glyphs)
* 3. The backend private data (scaled_font->surface_backend,
* scaled_font->surface_private)
*
* Modifications to these fields are protected with locks on
* scaled_font->mutex in the generic scaled_font code.
*/
cairo_hash_entry_t hash_entry;
/* useful bits for _cairo_scaled_font_nil */
cairo_status_t status;
cairo_reference_count_t ref_count;
cairo_user_data_array_t user_data;
cairo_font_face_t *original_font_face; /* may be NULL */
/* hash key members */
cairo_font_face_t *font_face; /* may be NULL */
cairo_matrix_t font_matrix; /* font space => user space */
cairo_matrix_t ctm; /* user space => device space */
cairo_font_options_t options;
unsigned int placeholder : 1; /* protected by fontmap mutex */
unsigned int holdover : 1;
unsigned int finished : 1;
/* "live" scaled_font members */
cairo_matrix_t scale; /* font space => device space */
cairo_matrix_t scale_inverse; /* device space => font space */
double max_scale; /* maximum x/y expansion of scale */
cairo_font_extents_t extents; /* user space */
cairo_font_extents_t fs_extents; /* font space */
/* The mutex protects modification to all subsequent fields. */
cairo_mutex_t mutex;
cairo_hash_table_t *glyphs;
cairo_list_t glyph_pages;
cairo_bool_t cache_frozen;
cairo_bool_t global_cache_frozen;
cairo_list_t dev_privates;
/* font backend managing this scaled font */
const cairo_scaled_font_backend_t *backend;
cairo_list_t link;
};
struct _cairo_scaled_font_private {
cairo_list_t link;
const void *key;
void (*destroy) (cairo_scaled_font_private_t *,
cairo_scaled_font_t *);
};
struct _cairo_scaled_glyph {
cairo_hash_entry_t hash_entry;
cairo_text_extents_t metrics; /* user-space metrics */
cairo_text_extents_t fs_metrics; /* font-space metrics */
cairo_box_t bbox; /* device-space bounds */
int16_t x_advance; /* device-space rounded X advance */
int16_t y_advance; /* device-space rounded Y advance */
unsigned int has_info;
cairo_image_surface_t *surface; /* device-space image */
cairo_path_fixed_t *path; /* device-space outline */
cairo_surface_t *recording_surface; /* device-space recording-surface */
cairo_image_surface_t *color_surface; /* device-space color image */
const void *dev_private_key;
void *dev_private;
cairo_list_t dev_privates;
};
struct _cairo_scaled_glyph_private {
cairo_list_t link;
const void *key;
void (*destroy) (cairo_scaled_glyph_private_t *,
cairo_scaled_glyph_t *,
cairo_scaled_font_t *);
};
cairo_private cairo_scaled_font_private_t *
_cairo_scaled_font_find_private (cairo_scaled_font_t *scaled_font,
const void *key);
cairo_private void
_cairo_scaled_font_attach_private (cairo_scaled_font_t *scaled_font,
cairo_scaled_font_private_t *priv,
const void *key,
void (*destroy) (cairo_scaled_font_private_t *,
cairo_scaled_font_t *));
cairo_private cairo_scaled_glyph_private_t *
_cairo_scaled_glyph_find_private (cairo_scaled_glyph_t *scaled_glyph,
const void *key);
cairo_private void
_cairo_scaled_glyph_attach_private (cairo_scaled_glyph_t *scaled_glyph,
cairo_scaled_glyph_private_t *priv,
const void *key,
void (*destroy) (cairo_scaled_glyph_private_t *,
cairo_scaled_glyph_t *,
cairo_scaled_font_t *));
cairo_private cairo_bool_t
_cairo_scaled_font_has_color_glyphs (cairo_scaled_font_t *scaled_font);
CAIRO_END_DECLS
#endif /* CAIRO_SCALED_FONT_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-scaled-font-subsets-private.h | /* cairo - a vector graphics library with display and print output
*
* Copyright © 2006 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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@cworth.org>
*/
#ifndef CAIRO_SCALED_FONT_SUBSETS_PRIVATE_H
#define CAIRO_SCALED_FONT_SUBSETS_PRIVATE_H
#include "cairoint.h"
#if CAIRO_HAS_FONT_SUBSET
typedef struct _cairo_scaled_font_subsets_glyph {
unsigned int font_id;
unsigned int subset_id;
unsigned int subset_glyph_index;
cairo_bool_t is_scaled;
cairo_bool_t is_composite;
cairo_bool_t is_latin;
double x_advance;
double y_advance;
cairo_bool_t utf8_is_mapped;
uint32_t unicode;
} cairo_scaled_font_subsets_glyph_t;
/**
* _cairo_scaled_font_subsets_create_scaled:
*
* Create a new #cairo_scaled_font_subsets_t object which can be used
* to create subsets of any number of #cairo_scaled_font_t
* objects. This allows the (arbitrarily large and sparse) glyph
* indices of a #cairo_scaled_font_t to be mapped to one or more font
* subsets with glyph indices packed into the range
* [0 .. max_glyphs_per_subset).
*
* Return value: a pointer to the newly creates font subsets. The
* caller owns this object and should call
* _cairo_scaled_font_subsets_destroy() when done with it.
**/
cairo_private cairo_scaled_font_subsets_t *
_cairo_scaled_font_subsets_create_scaled (void);
/**
* _cairo_scaled_font_subsets_create_simple:
*
* Create a new #cairo_scaled_font_subsets_t object which can be used
* to create font subsets suitable for embedding as Postscript or PDF
* simple fonts.
*
* Glyphs with an outline path available will be mapped to one font
* subset for each font face. Glyphs from bitmap fonts will mapped to
* separate font subsets for each #cairo_scaled_font_t object.
*
* The maximum number of glyphs per subset is 256. Each subset
* reserves the first glyph for the .notdef glyph.
*
* Return value: a pointer to the newly creates font subsets. The
* caller owns this object and should call
* _cairo_scaled_font_subsets_destroy() when done with it.
**/
cairo_private cairo_scaled_font_subsets_t *
_cairo_scaled_font_subsets_create_simple (void);
/**
* _cairo_scaled_font_subsets_create_composite:
*
* Create a new #cairo_scaled_font_subsets_t object which can be used
* to create font subsets suitable for embedding as Postscript or PDF
* composite fonts.
*
* Glyphs with an outline path available will be mapped to one font
* subset for each font face. Each unscaled subset has a maximum of
* 65536 glyphs except for Type1 fonts which have a maximum of 256 glyphs.
*
* Glyphs from bitmap fonts will mapped to separate font subsets for
* each #cairo_scaled_font_t object. Each unscaled subset has a maximum
* of 256 glyphs.
*
* Each subset reserves the first glyph for the .notdef glyph.
*
* Return value: a pointer to the newly creates font subsets. The
* caller owns this object and should call
* _cairo_scaled_font_subsets_destroy() when done with it.
**/
cairo_private cairo_scaled_font_subsets_t *
_cairo_scaled_font_subsets_create_composite (void);
/**
* _cairo_scaled_font_subsets_destroy:
* @font_subsets: a #cairo_scaled_font_subsets_t object to be destroyed
*
* Destroys @font_subsets and all resources associated with it.
**/
cairo_private void
_cairo_scaled_font_subsets_destroy (cairo_scaled_font_subsets_t *font_subsets);
/**
* _cairo_scaled_font_subsets_enable_latin_subset:
* @font_subsets: a #cairo_scaled_font_subsets_t object to be destroyed
* @use_latin: a #cairo_bool_t indicating if a latin subset is to be used
*
* If enabled, all CP1252 characters will be placed in a separate
* 8-bit latin subset.
**/
cairo_private void
_cairo_scaled_font_subsets_enable_latin_subset (cairo_scaled_font_subsets_t *font_subsets,
cairo_bool_t use_latin);
/**
* _cairo_scaled_font_subsets_map_glyph:
* @font_subsets: a #cairo_scaled_font_subsets_t
* @scaled_font: the font of the glyph to be mapped
* @scaled_font_glyph_index: the index of the glyph to be mapped
* @utf8: a string of text encoded in UTF-8
* @utf8_len: length of @utf8 in bytes
* @subset_glyph_ret: return structure containing subset font and glyph id
*
* Map a glyph from a #cairo_scaled_font to a new index within a
* subset of that font. The mapping performed is from the tuple:
*
* (scaled_font, scaled_font_glyph_index)
*
* to the tuple:
*
* (font_id, subset_id, subset_glyph_index)
*
* This mapping is 1:1. If the input tuple has previously mapped, the
* the output tuple previously returned will be returned again.
*
* Otherwise, the return tuple will be constructed as follows:
*
* 1) There is a 1:1 correspondence between the input scaled_font
* value and the output font_id value. If no mapping has been
* previously performed with the scaled_font value then the
* smallest unused font_id value will be returned.
*
* 2) Within the set of output tuples of the same font_id value the
* smallest value of subset_id will be returned such that
* subset_glyph_index does not exceed max_glyphs_per_subset (as
* passed to _cairo_scaled_font_subsets_create()) and that the
* resulting tuple is unique.
*
* 3) The smallest value of subset_glyph_index is returned such that
* the resulting tuple is unique.
*
* The net result is that any #cairo_scaled_font_t will be represented
* by one or more font subsets. Each subset is effectively a tuple of
* (scaled_font, font_id, subset_id) and within each subset there
* exists a mapping of scaled_glyph_font_index to subset_glyph_index.
*
* This final description of a font subset is the same representation
* used by #cairo_scaled_font_subset_t as provided by
* _cairo_scaled_font_subsets_foreach.
*
* @utf8 and @utf8_len specify a string of unicode characters that the
* glyph @scaled_font_glyph_index maps to. If @utf8_is_mapped in
* @subset_glyph_ret is %TRUE, the font subsetting will (where index to
* unicode mapping is supported) ensure that @scaled_font_glyph_index
* maps to @utf8. If @utf8_is_mapped is %FALSE,
* @scaled_font_glyph_index has already been mapped to a different
* unicode string.
*
* The returned values in the #cairo_scaled_font_subsets_glyph_t struct are:
*
* @font_id: The font ID of the mapped glyph
* @subset_id : The subset ID of the mapped glyph within the @font_id
* @subset_glyph_index: The index of the mapped glyph within the @subset_id subset
* @is_scaled: If true, the mapped glyph is from a bitmap font, and separate font
* subset is created for each font scale used. If false, the outline of the mapped glyph
* is available. One font subset for each font face is created.
* @x_advance, @y_advance: When @is_scaled is true, @x_advance and @y_advance contain
* the x and y advance for the mapped glyph in device space.
* When @is_scaled is false, @x_advance and @y_advance contain the x and y advance for
* the the mapped glyph from an unhinted 1 point font.
* @utf8_is_mapped: If true the utf8 string provided to _cairo_scaled_font_subsets_map_glyph()
* is (or already was) the utf8 string mapped to this glyph. If false the glyph is already
* mapped to a different utf8 string.
* @unicode: the unicode character mapped to this glyph by the font backend.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful, or a non-zero
* value indicating an error. Possible errors include
* %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_scaled_font_subsets_map_glyph (cairo_scaled_font_subsets_t *font_subsets,
cairo_scaled_font_t *scaled_font,
unsigned long scaled_font_glyph_index,
const char * utf8,
int utf8_len,
cairo_scaled_font_subsets_glyph_t *subset_glyph_ret);
typedef cairo_int_status_t
(*cairo_scaled_font_subset_callback_func_t) (cairo_scaled_font_subset_t *font_subset,
void *closure);
/**
* _cairo_scaled_font_subsets_foreach_scaled:
* @font_subsets: a #cairo_scaled_font_subsets_t
* @font_subset_callback: a function to be called for each font subset
* @closure: closure data for the callback function
*
* Iterate over each unique scaled font subset as created by calls to
* _cairo_scaled_font_subsets_map_glyph(). A subset is determined by
* unique pairs of (font_id, subset_id) as returned by
* _cairo_scaled_font_subsets_map_glyph().
*
* For each subset, @font_subset_callback will be called and will be
* provided with both a #cairo_scaled_font_subset_t object containing
* all the glyphs in the subset as well as the value of @closure.
*
* The #cairo_scaled_font_subset_t object contains the scaled_font,
* the font_id, and the subset_id corresponding to all glyphs
* belonging to the subset. In addition, it contains an array providing
* a mapping between subset glyph indices and the original scaled font
* glyph indices.
*
* The index of the array corresponds to subset_glyph_index values
* returned by _cairo_scaled_font_subsets_map_glyph() while the
* values of the array correspond to the scaled_font_glyph_index
* values passed as input to the same function.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful, or a non-zero
* value indicating an error. Possible errors include
* %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_scaled_font_subsets_foreach_scaled (cairo_scaled_font_subsets_t *font_subsets,
cairo_scaled_font_subset_callback_func_t font_subset_callback,
void *closure);
/**
* _cairo_scaled_font_subsets_foreach_unscaled:
* @font_subsets: a #cairo_scaled_font_subsets_t
* @font_subset_callback: a function to be called for each font subset
* @closure: closure data for the callback function
*
* Iterate over each unique unscaled font subset as created by calls to
* _cairo_scaled_font_subsets_map_glyph(). A subset is determined by
* unique pairs of (font_id, subset_id) as returned by
* _cairo_scaled_font_subsets_map_glyph().
*
* For each subset, @font_subset_callback will be called and will be
* provided with both a #cairo_scaled_font_subset_t object containing
* all the glyphs in the subset as well as the value of @closure.
*
* The #cairo_scaled_font_subset_t object contains the scaled_font,
* the font_id, and the subset_id corresponding to all glyphs
* belonging to the subset. In addition, it contains an array providing
* a mapping between subset glyph indices and the original scaled font
* glyph indices.
*
* The index of the array corresponds to subset_glyph_index values
* returned by _cairo_scaled_font_subsets_map_glyph() while the
* values of the array correspond to the scaled_font_glyph_index
* values passed as input to the same function.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful, or a non-zero
* value indicating an error. Possible errors include
* %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_scaled_font_subsets_foreach_unscaled (cairo_scaled_font_subsets_t *font_subsets,
cairo_scaled_font_subset_callback_func_t font_subset_callback,
void *closure);
/**
* _cairo_scaled_font_subsets_foreach_user:
* @font_subsets: a #cairo_scaled_font_subsets_t
* @font_subset_callback: a function to be called for each font subset
* @closure: closure data for the callback function
*
* Iterate over each unique scaled font subset as created by calls to
* _cairo_scaled_font_subsets_map_glyph(). A subset is determined by
* unique pairs of (font_id, subset_id) as returned by
* _cairo_scaled_font_subsets_map_glyph().
*
* For each subset, @font_subset_callback will be called and will be
* provided with both a #cairo_scaled_font_subset_t object containing
* all the glyphs in the subset as well as the value of @closure.
*
* The #cairo_scaled_font_subset_t object contains the scaled_font,
* the font_id, and the subset_id corresponding to all glyphs
* belonging to the subset. In addition, it contains an array providing
* a mapping between subset glyph indices and the original scaled font
* glyph indices.
*
* The index of the array corresponds to subset_glyph_index values
* returned by _cairo_scaled_font_subsets_map_glyph() while the
* values of the array correspond to the scaled_font_glyph_index
* values passed as input to the same function.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful, or a non-zero
* value indicating an error. Possible errors include
* %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_scaled_font_subsets_foreach_user (cairo_scaled_font_subsets_t *font_subsets,
cairo_scaled_font_subset_callback_func_t font_subset_callback,
void *closure);
/**
* _cairo_scaled_font_subset_create_glyph_names:
* @font_subsets: a #cairo_scaled_font_subsets_t
*
* Create an array of strings containing the glyph name for each glyph
* in @font_subsets. The array as store in font_subsets->glyph_names.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font backend does not support
* mapping the glyph indices to unicode characters. Possible errors
* include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_int_status_t
_cairo_scaled_font_subset_create_glyph_names (cairo_scaled_font_subset_t *subset);
typedef struct _cairo_cff_subset {
char *family_name_utf8;
char *ps_name;
double *widths;
double x_min, y_min, x_max, y_max;
double ascent, descent;
char *data;
unsigned long data_length;
} cairo_cff_subset_t;
/**
* _cairo_cff_subset_init:
* @cff_subset: a #cairo_cff_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) generate a
* cff file corresponding to @font_subset and initialize
* @cff_subset with information about the subset and the cff
* data.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a
* cff file, or an non-zero value indicating an error. Possible
* errors include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_cff_subset_init (cairo_cff_subset_t *cff_subset,
const char *name,
cairo_scaled_font_subset_t *font_subset);
/**
* _cairo_cff_subset_fini:
* @cff_subset: a #cairo_cff_subset_t
*
* Free all resources associated with @cff_subset. After this
* call, @cff_subset should not be used again without a
* subsequent call to _cairo_cff_subset_init() again first.
**/
cairo_private void
_cairo_cff_subset_fini (cairo_cff_subset_t *cff_subset);
/**
* _cairo_cff_scaled_font_is_cid_cff:
* @scaled_font: a #cairo_scaled_font_t
*
* Return %TRUE if @scaled_font is a CID CFF font, otherwise return %FALSE.
**/
cairo_private cairo_bool_t
_cairo_cff_scaled_font_is_cid_cff (cairo_scaled_font_t *scaled_font);
/**
* _cairo_cff_fallback_init:
* @cff_subset: a #cairo_cff_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) generate a cff
* file corresponding to @font_subset and initialize @cff_subset
* with information about the subset and the cff data.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a
* cff file, or an non-zero value indicating an error. Possible
* errors include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_cff_fallback_init (cairo_cff_subset_t *cff_subset,
const char *name,
cairo_scaled_font_subset_t *font_subset);
/**
* _cairo_cff_fallback_fini:
* @cff_subset: a #cairo_cff_subset_t
*
* Free all resources associated with @cff_subset. After this
* call, @cff_subset should not be used again without a
* subsequent call to _cairo_cff_subset_init() again first.
**/
cairo_private void
_cairo_cff_fallback_fini (cairo_cff_subset_t *cff_subset);
typedef struct _cairo_truetype_subset {
char *family_name_utf8;
char *ps_name;
double *widths;
double x_min, y_min, x_max, y_max;
double ascent, descent;
unsigned char *data;
unsigned long data_length;
unsigned long *string_offsets;
unsigned long num_string_offsets;
} cairo_truetype_subset_t;
/**
* _cairo_truetype_subset_init_ps:
* @truetype_subset: a #cairo_truetype_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) generate a
* truetype file corresponding to @font_subset and initialize
* @truetype_subset with information about the subset and the truetype
* data. The generated font will be suitable for embedding in
* PostScript.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a
* truetype file, or an non-zero value indicating an error. Possible
* errors include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_truetype_subset_init_ps (cairo_truetype_subset_t *truetype_subset,
cairo_scaled_font_subset_t *font_subset);
/**
* _cairo_truetype_subset_init_pdf:
* @truetype_subset: a #cairo_truetype_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) generate a
* truetype file corresponding to @font_subset and initialize
* @truetype_subset with information about the subset and the truetype
* data. The generated font will be suitable for embedding in
* PDF.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a
* truetype file, or an non-zero value indicating an error. Possible
* errors include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_truetype_subset_init_pdf (cairo_truetype_subset_t *truetype_subset,
cairo_scaled_font_subset_t *font_subset);
/**
* _cairo_truetype_subset_fini:
* @truetype_subset: a #cairo_truetype_subset_t
*
* Free all resources associated with @truetype_subset. After this
* call, @truetype_subset should not be used again without a
* subsequent call to _cairo_truetype_subset_init() again first.
**/
cairo_private void
_cairo_truetype_subset_fini (cairo_truetype_subset_t *truetype_subset);
cairo_private const char *
_cairo_ps_standard_encoding_to_glyphname (int glyph);
cairo_private int
_cairo_unicode_to_winansi (unsigned long unicode);
cairo_private const char *
_cairo_winansi_to_glyphname (int glyph);
typedef struct _cairo_type1_subset {
char *base_font;
double *widths;
double x_min, y_min, x_max, y_max;
double ascent, descent;
char *data;
unsigned long header_length;
unsigned long data_length;
unsigned long trailer_length;
} cairo_type1_subset_t;
/**
* _cairo_type1_subset_init:
* @type1_subset: a #cairo_type1_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
* @hex_encode: if true the encrypted portion of the font is hex encoded
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) generate a type1
* file corresponding to @font_subset and initialize @type1_subset
* with information about the subset and the type1 data.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a type1
* file, or an non-zero value indicating an error. Possible errors
* include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_type1_subset_init (cairo_type1_subset_t *type_subset,
const char *name,
cairo_scaled_font_subset_t *font_subset,
cairo_bool_t hex_encode);
/**
* _cairo_type1_subset_fini:
* @type1_subset: a #cairo_type1_subset_t
*
* Free all resources associated with @type1_subset. After this call,
* @type1_subset should not be used again without a subsequent call to
* _cairo_truetype_type1_init() again first.
**/
cairo_private void
_cairo_type1_subset_fini (cairo_type1_subset_t *subset);
/**
* _cairo_type1_scaled_font_is_type1:
* @scaled_font: a #cairo_scaled_font_t
*
* Return %TRUE if @scaled_font is a Type 1 font, otherwise return %FALSE.
**/
cairo_private cairo_bool_t
_cairo_type1_scaled_font_is_type1 (cairo_scaled_font_t *scaled_font);
/**
* _cairo_type1_fallback_init_binary:
* @type1_subset: a #cairo_type1_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) generate a type1
* file corresponding to @font_subset and initialize @type1_subset
* with information about the subset and the type1 data. The encrypted
* part of the font is binary encoded.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a type1
* file, or an non-zero value indicating an error. Possible errors
* include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_type1_fallback_init_binary (cairo_type1_subset_t *type_subset,
const char *name,
cairo_scaled_font_subset_t *font_subset);
/**
* _cairo_type1_fallback_init_hex:
* @type1_subset: a #cairo_type1_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) generate a type1
* file corresponding to @font_subset and initialize @type1_subset
* with information about the subset and the type1 data. The encrypted
* part of the font is hex encoded.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a type1
* file, or an non-zero value indicating an error. Possible errors
* include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_type1_fallback_init_hex (cairo_type1_subset_t *type_subset,
const char *name,
cairo_scaled_font_subset_t *font_subset);
/**
* _cairo_type1_fallback_fini:
* @type1_subset: a #cairo_type1_subset_t
*
* Free all resources associated with @type1_subset. After this call,
* @type1_subset should not be used again without a subsequent call to
* _cairo_truetype_type1_init() again first.
**/
cairo_private void
_cairo_type1_fallback_fini (cairo_type1_subset_t *subset);
typedef struct _cairo_type2_charstrings {
int *widths;
long x_min, y_min, x_max, y_max;
long ascent, descent;
cairo_array_t charstrings;
} cairo_type2_charstrings_t;
/**
* _cairo_type2_charstrings_init:
* @type2_subset: a #cairo_type2_subset_t to initialize
* @font_subset: the #cairo_scaled_font_subset_t to initialize from
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) generate type2
* charstrings to @font_subset and initialize @type2_subset
* with information about the subset.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font can't be subset as a type2
* charstrings, or an non-zero value indicating an error. Possible errors
* include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_status_t
_cairo_type2_charstrings_init (cairo_type2_charstrings_t *charstrings,
cairo_scaled_font_subset_t *font_subset);
/**
* _cairo_type2_charstrings_fini:
* @subset: a #cairo_type2_charstrings_t
*
* Free all resources associated with @type2_charstring. After this call,
* @type2_charstring should not be used again without a subsequent call to
* _cairo_type2_charstring_init() again first.
**/
cairo_private void
_cairo_type2_charstrings_fini (cairo_type2_charstrings_t *charstrings);
/**
* _cairo_truetype_index_to_ucs4:
* @scaled_font: the #cairo_scaled_font_t
* @index: the glyph index
* @ucs4: return value for the unicode value of the glyph
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) assign
* the unicode character of the glyph to @ucs4.
*
* If mapping glyph indices to unicode is supported but the unicode
* value of the specified glyph is not available, @ucs4 is set to -1.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if mapping glyph indices to unicode
* is not supported. Possible errors include %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_int_status_t
_cairo_truetype_index_to_ucs4 (cairo_scaled_font_t *scaled_font,
unsigned long index,
uint32_t *ucs4);
/**
* _cairo_truetype_read_font_name:
* @scaled_font: the #cairo_scaled_font_t
* @ps_name: returns the PostScript name of the font
* or %NULL if the name could not be found.
* @font_name: returns the font name or %NULL if the name could not be found.
*
* If possible (depending on the format of the underlying
* #cairo_scaled_font_t and the font backend in use) read the
* PostScript and Font names from a TrueType/OpenType font.
*
* The font name is the full name of the font eg "DejaVu Sans Bold".
* The PostScript name is a shortened name with spaces removed
* suitable for use as the font name in a PS or PDF file eg
* "DejaVuSans-Bold".
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font is not TrueType/OpenType
* or the name table is not present. Possible errors include
* %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_int_status_t
_cairo_truetype_read_font_name (cairo_scaled_font_t *scaled_font,
char **ps_name,
char **font_name);
/**
* _cairo_truetype_get_style:
* @scaled_font: the #cairo_scaled_font_t
* @weight: returns the font weight from the OS/2 table
* @bold: returns true if font is bold
* @italic: returns true if font is italic
*
* If the font is a truetype/opentype font with an OS/2 table, get the
* weight, bold, and italic data from the OS/2 table. The weight
* values have the same meaning as the lfWeight field of the Windows
* LOGFONT structure. Refer to the TrueType Specification for
* definition of the weight values.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful,
* %CAIRO_INT_STATUS_UNSUPPORTED if the font is not TrueType/OpenType
* or the OS/2 table is not present.
**/
cairo_private cairo_int_status_t
_cairo_truetype_get_style (cairo_scaled_font_t *scaled_font,
int *weight,
cairo_bool_t *bold,
cairo_bool_t *italic);
/**
* _cairo_escape_ps_name:
* @ps_name: returns the PostScript name with all invalid characters escaped
*
* Ensure that PostSript name is a valid PDF/PostSript name object.
* In PDF names are treated as UTF8 and non ASCII bytes, ' ',
* and '#' are encoded as '#' followed by 2 hex digits that
* encode the byte.
*
* Return value: %CAIRO_STATUS_SUCCESS if successful. Possible errors include
* %CAIRO_STATUS_NO_MEMORY.
**/
cairo_private cairo_int_status_t
_cairo_escape_ps_name (char **ps_name);
#if DEBUG_SUBSETS
cairo_private void
dump_scaled_font_subsets (cairo_scaled_font_subsets_t *font_subsets);
#endif
#endif /* CAIRO_HAS_FONT_SUBSET */
#endif /* CAIRO_SCALED_FONT_SUBSETS_PRIVATE_H */
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-scaled-font.c | /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/*
* Copyright © 2005 Keith Packard
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is Keith Packard
*
* Contributor(s):
* Keith Packard <keithp@keithp.com>
* Carl D. Worth <cworth@cworth.org>
* Graydon Hoare <graydon@redhat.com>
* Owen Taylor <otaylor@redhat.com>
* Behdad Esfahbod <behdad@behdad.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-error-private.h"
#include "cairo-image-surface-private.h"
#include "cairo-list-inline.h"
#include "cairo-pattern-private.h"
#include "cairo-scaled-font-private.h"
#include "cairo-surface-backend-private.h"
/**
* SECTION:cairo-scaled-font
* @Title: cairo_scaled_font_t
* @Short_Description: Font face at particular size and options
* @See_Also: #cairo_font_face_t, #cairo_matrix_t, #cairo_font_options_t
*
* #cairo_scaled_font_t represents a realization of a font face at a particular
* size and transformation and a certain set of font options.
**/
static uint32_t
_cairo_scaled_font_compute_hash (cairo_scaled_font_t *scaled_font);
/* Global Glyph Cache
*
* We maintain a global pool of glyphs split between all active fonts. This
* allows a heavily used individual font to cache more glyphs than we could
* manage if we used per-font glyph caches, but at the same time maintains
* fairness across all fonts and provides a cap on the maximum number of
* global glyphs.
*
* The glyphs are allocated in pages, which are capped in the global pool.
* Using pages means we can reduce the frequency at which we have to probe the
* global pool and ameliorates the memory allocation pressure.
*/
/* XXX: This number is arbitrary---we've never done any measurement of this. */
#define MAX_GLYPH_PAGES_CACHED 512
static cairo_cache_t cairo_scaled_glyph_page_cache;
#define CAIRO_SCALED_GLYPH_PAGE_SIZE 32
struct _cairo_scaled_glyph_page {
cairo_cache_entry_t cache_entry;
cairo_scaled_font_t *scaled_font;
cairo_list_t link;
unsigned int num_glyphs;
cairo_scaled_glyph_t glyphs[CAIRO_SCALED_GLYPH_PAGE_SIZE];
};
/*
* Notes:
*
* To store rasterizations of glyphs, we use an image surface and the
* device offset to represent the glyph origin.
*
* A device_transform converts from device space (a conceptual space) to
* surface space. For simple cases of translation only, it's called a
* device_offset and is public API (cairo_surface_[gs]et_device_offset()).
* A possibly better name for those functions could have been
* cairo_surface_[gs]et_origin(). So, that's what they do: they set where
* the device-space origin (0,0) is in the surface. If the origin is inside
* the surface, device_offset values are positive. It may look like this:
*
* Device space:
* (-x,-y) <-- negative numbers
* +----------------+
* | . |
* | . |
* |......(0,0) <---|-- device-space origin
* | |
* | |
* +----------------+
* (width-x,height-y)
*
* Surface space:
* (0,0) <-- surface-space origin
* +---------------+
* | . |
* | . |
* |......(x,y) <--|-- device_offset
* | |
* | |
* +---------------+
* (width,height)
*
* In other words: device_offset is the coordinates of the device-space
* origin relative to the top-left of the surface.
*
* We use device offsets in a couple of places:
*
* - Public API: To let toolkits like Gtk+ give user a surface that
* only represents part of the final destination (say, the expose
* area), but has the same device space as the destination. In these
* cases device_offset is typically negative. Example:
*
* application window
* +---------------+
* | . |
* | (x,y). |
* |......+---+ |
* | | | <--|-- expose area
* | +---+ |
* +---------------+
*
* In this case, the user of cairo API can set the device_space on
* the expose area to (-x,-y) to move the device space origin to that
* of the application window, such that drawing in the expose area
* surface and painting it in the application window has the same
* effect as drawing in the application window directly. Gtk+ has
* been using this feature.
*
* - Glyph surfaces: In most font rendering systems, glyph surfaces
* have an origin at (0,0) and a bounding box that is typically
* represented as (x_bearing,y_bearing,width,height). Depending on
* which way y progresses in the system, y_bearing may typically be
* negative (for systems similar to cairo, with origin at top left),
* or be positive (in systems like PDF with origin at bottom left).
* No matter which is the case, it is important to note that
* (x_bearing,y_bearing) is the coordinates of top-left of the glyph
* relative to the glyph origin. That is, for example:
*
* Scaled-glyph space:
*
* (x_bearing,y_bearing) <-- negative numbers
* +----------------+
* | . |
* | . |
* |......(0,0) <---|-- glyph origin
* | |
* | |
* +----------------+
* (width+x_bearing,height+y_bearing)
*
* Note the similarity of the origin to the device space. That is
* exactly how we use the device_offset to represent scaled glyphs:
* to use the device-space origin as the glyph origin.
*
* Now compare the scaled-glyph space to device-space and surface-space
* and convince yourself that:
*
* (x_bearing,y_bearing) = (-x,-y) = - device_offset
*
* That's right. If you are not convinced yet, contrast the definition
* of the two:
*
* "(x_bearing,y_bearing) is the coordinates of top-left of the
* glyph relative to the glyph origin."
*
* "In other words: device_offset is the coordinates of the
* device-space origin relative to the top-left of the surface."
*
* and note that glyph origin = device-space origin.
*/
static void
_cairo_scaled_font_fini_internal (cairo_scaled_font_t *scaled_font);
static void
_cairo_scaled_glyph_fini (cairo_scaled_font_t *scaled_font,
cairo_scaled_glyph_t *scaled_glyph)
{
while (! cairo_list_is_empty (&scaled_glyph->dev_privates)) {
cairo_scaled_glyph_private_t *private =
cairo_list_first_entry (&scaled_glyph->dev_privates,
cairo_scaled_glyph_private_t,
link);
private->destroy (private, scaled_glyph, scaled_font);
}
_cairo_image_scaled_glyph_fini (scaled_font, scaled_glyph);
if (scaled_glyph->surface != NULL)
cairo_surface_destroy (&scaled_glyph->surface->base);
if (scaled_glyph->path != NULL)
_cairo_path_fixed_destroy (scaled_glyph->path);
if (scaled_glyph->recording_surface != NULL) {
cairo_surface_finish (scaled_glyph->recording_surface);
cairo_surface_destroy (scaled_glyph->recording_surface);
}
if (scaled_glyph->color_surface != NULL)
cairo_surface_destroy (&scaled_glyph->color_surface->base);
}
#define ZOMBIE 0
static const cairo_scaled_font_t _cairo_scaled_font_nil = {
{ ZOMBIE }, /* hash_entry */
CAIRO_STATUS_NO_MEMORY, /* status */
CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
{ 0, 0, 0, NULL }, /* user_data */
NULL, /* original_font_face */
NULL, /* font_face */
{ 1., 0., 0., 1., 0, 0}, /* font_matrix */
{ 1., 0., 0., 1., 0, 0}, /* ctm */
{ CAIRO_ANTIALIAS_DEFAULT, /* options */
CAIRO_SUBPIXEL_ORDER_DEFAULT,
CAIRO_HINT_STYLE_DEFAULT,
CAIRO_HINT_METRICS_DEFAULT} ,
FALSE, /* placeholder */
FALSE, /* holdover */
TRUE, /* finished */
{ 1., 0., 0., 1., 0, 0}, /* scale */
{ 1., 0., 0., 1., 0, 0}, /* scale_inverse */
1., /* max_scale */
{ 0., 0., 0., 0., 0. }, /* extents */
{ 0., 0., 0., 0., 0. }, /* fs_extents */
CAIRO_MUTEX_NIL_INITIALIZER,/* mutex */
NULL, /* glyphs */
{ NULL, NULL }, /* pages */
FALSE, /* cache_frozen */
FALSE, /* global_cache_frozen */
{ NULL, NULL }, /* privates */
NULL /* backend */
};
/**
* _cairo_scaled_font_set_error:
* @scaled_font: a scaled_font
* @status: a status value indicating an error
*
* Atomically sets scaled_font->status to @status and calls _cairo_error;
* Does nothing if status is %CAIRO_STATUS_SUCCESS.
*
* All assignments of an error status to scaled_font->status should happen
* through _cairo_scaled_font_set_error(). Note that due to the nature of
* the atomic operation, it is not safe to call this function on the nil
* objects.
*
* The purpose of this function is to allow the user to set a
* breakpoint in _cairo_error() to generate a stack trace for when the
* user causes cairo to detect an error.
*
* Return value: the error status.
**/
cairo_status_t
_cairo_scaled_font_set_error (cairo_scaled_font_t *scaled_font,
cairo_status_t status)
{
if (status == CAIRO_STATUS_SUCCESS)
return status;
/* Don't overwrite an existing error. This preserves the first
* error, which is the most significant. */
_cairo_status_set_error (&scaled_font->status, status);
return _cairo_error (status);
}
/**
* cairo_scaled_font_get_type:
* @scaled_font: a #cairo_scaled_font_t
*
* This function returns the type of the backend used to create
* a scaled font. See #cairo_font_type_t for available types.
* However, this function never returns %CAIRO_FONT_TYPE_TOY.
*
* Return value: The type of @scaled_font.
*
* Since: 1.2
**/
cairo_font_type_t
cairo_scaled_font_get_type (cairo_scaled_font_t *scaled_font)
{
if (CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
return CAIRO_FONT_TYPE_TOY;
return scaled_font->backend->type;
}
/**
* cairo_scaled_font_status:
* @scaled_font: a #cairo_scaled_font_t
*
* Checks whether an error has previously occurred for this
* scaled_font.
*
* Return value: %CAIRO_STATUS_SUCCESS or another error such as
* %CAIRO_STATUS_NO_MEMORY.
*
* Since: 1.0
**/
cairo_status_t
cairo_scaled_font_status (cairo_scaled_font_t *scaled_font)
{
return scaled_font->status;
}
slim_hidden_def (cairo_scaled_font_status);
/* Here we keep a unique mapping from
* font_face/matrix/ctm/font_options => #cairo_scaled_font_t.
*
* Here are the things that we want to map:
*
* a) All otherwise referenced #cairo_scaled_font_t's
* b) Some number of not otherwise referenced #cairo_scaled_font_t's
*
* The implementation uses a hash table which covers (a)
* completely. Then, for (b) we have an array of otherwise
* unreferenced fonts (holdovers) which are expired in
* least-recently-used order.
*
* The cairo_scaled_font_create() code gets to treat this like a regular
* hash table. All of the magic for the little holdover cache is in
* cairo_scaled_font_reference() and cairo_scaled_font_destroy().
*/
/* This defines the size of the holdover array ... that is, the number
* of scaled fonts we keep around even when not otherwise referenced
*/
#define CAIRO_SCALED_FONT_MAX_HOLDOVERS 256
typedef struct _cairo_scaled_font_map {
cairo_scaled_font_t *mru_scaled_font;
cairo_hash_table_t *hash_table;
cairo_scaled_font_t *holdovers[CAIRO_SCALED_FONT_MAX_HOLDOVERS];
int num_holdovers;
} cairo_scaled_font_map_t;
static cairo_scaled_font_map_t *cairo_scaled_font_map;
static int
_cairo_scaled_font_keys_equal (const void *abstract_key_a, const void *abstract_key_b);
static cairo_scaled_font_map_t *
_cairo_scaled_font_map_lock (void)
{
CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
if (cairo_scaled_font_map == NULL) {
cairo_scaled_font_map = _cairo_malloc (sizeof (cairo_scaled_font_map_t));
if (unlikely (cairo_scaled_font_map == NULL))
goto CLEANUP_MUTEX_LOCK;
cairo_scaled_font_map->mru_scaled_font = NULL;
cairo_scaled_font_map->hash_table =
_cairo_hash_table_create (_cairo_scaled_font_keys_equal);
if (unlikely (cairo_scaled_font_map->hash_table == NULL))
goto CLEANUP_SCALED_FONT_MAP;
cairo_scaled_font_map->num_holdovers = 0;
}
return cairo_scaled_font_map;
CLEANUP_SCALED_FONT_MAP:
free (cairo_scaled_font_map);
cairo_scaled_font_map = NULL;
CLEANUP_MUTEX_LOCK:
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return NULL;
}
static void
_cairo_scaled_font_map_unlock (void)
{
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
}
void
_cairo_scaled_font_map_destroy (void)
{
cairo_scaled_font_map_t *font_map;
cairo_scaled_font_t *scaled_font;
CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
font_map = cairo_scaled_font_map;
if (unlikely (font_map == NULL)) {
goto CLEANUP_MUTEX_LOCK;
}
scaled_font = font_map->mru_scaled_font;
if (scaled_font != NULL) {
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
cairo_scaled_font_destroy (scaled_font);
CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
}
/* remove scaled_fonts starting from the end so that font_map->holdovers
* is always in a consistent state when we release the mutex. */
while (font_map->num_holdovers) {
scaled_font = font_map->holdovers[font_map->num_holdovers-1];
assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
_cairo_hash_table_remove (font_map->hash_table,
&scaled_font->hash_entry);
font_map->num_holdovers--;
/* This releases the font_map lock to avoid the possibility of a
* recursive deadlock when the scaled font destroy closure gets
* called
*/
_cairo_scaled_font_fini (scaled_font);
free (scaled_font);
}
_cairo_hash_table_destroy (font_map->hash_table);
free (cairo_scaled_font_map);
cairo_scaled_font_map = NULL;
CLEANUP_MUTEX_LOCK:
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
}
static void
_cairo_scaled_glyph_page_destroy (cairo_scaled_font_t *scaled_font,
cairo_scaled_glyph_page_t *page)
{
unsigned int n;
assert (!scaled_font->cache_frozen);
assert (!scaled_font->global_cache_frozen);
for (n = 0; n < page->num_glyphs; n++) {
_cairo_hash_table_remove (scaled_font->glyphs,
&page->glyphs[n].hash_entry);
_cairo_scaled_glyph_fini (scaled_font, &page->glyphs[n]);
}
cairo_list_del (&page->link);
free (page);
}
static void
_cairo_scaled_glyph_page_pluck (void *closure)
{
cairo_scaled_glyph_page_t *page = closure;
cairo_scaled_font_t *scaled_font;
assert (! cairo_list_is_empty (&page->link));
scaled_font = page->scaled_font;
CAIRO_MUTEX_LOCK (scaled_font->mutex);
_cairo_scaled_glyph_page_destroy (scaled_font, page);
CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
}
/* If a scaled font wants to unlock the font map while still being
* created (needed for user-fonts), we need to take extra care not
* ending up with multiple identical scaled fonts being created.
*
* What we do is, we create a fake identical scaled font, and mark
* it as placeholder, lock its mutex, and insert that in the fontmap
* hash table. This makes other code trying to create an identical
* scaled font to just wait and retry.
*
* The reason we have to create a fake scaled font instead of just using
* scaled_font is for lifecycle management: we need to (or rather,
* other code needs to) reference the scaled_font in the hash table.
* We can't do that on the input scaled_font as it may be freed by
* font backend upon error.
*/
cairo_status_t
_cairo_scaled_font_register_placeholder_and_unlock_font_map (cairo_scaled_font_t *scaled_font)
{
cairo_status_t status;
cairo_scaled_font_t *placeholder_scaled_font;
assert (CAIRO_MUTEX_IS_LOCKED (_cairo_scaled_font_map_mutex));
status = scaled_font->status;
if (unlikely (status))
return status;
placeholder_scaled_font = _cairo_malloc (sizeof (cairo_scaled_font_t));
if (unlikely (placeholder_scaled_font == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
/* full initialization is wasteful, but who cares... */
status = _cairo_scaled_font_init (placeholder_scaled_font,
scaled_font->font_face,
&scaled_font->font_matrix,
&scaled_font->ctm,
&scaled_font->options,
NULL);
if (unlikely (status))
goto FREE_PLACEHOLDER;
placeholder_scaled_font->placeholder = TRUE;
placeholder_scaled_font->hash_entry.hash
= _cairo_scaled_font_compute_hash (placeholder_scaled_font);
status = _cairo_hash_table_insert (cairo_scaled_font_map->hash_table,
&placeholder_scaled_font->hash_entry);
if (unlikely (status))
goto FINI_PLACEHOLDER;
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
CAIRO_MUTEX_LOCK (placeholder_scaled_font->mutex);
return CAIRO_STATUS_SUCCESS;
FINI_PLACEHOLDER:
_cairo_scaled_font_fini_internal (placeholder_scaled_font);
FREE_PLACEHOLDER:
free (placeholder_scaled_font);
return _cairo_scaled_font_set_error (scaled_font, status);
}
void
_cairo_scaled_font_unregister_placeholder_and_lock_font_map (cairo_scaled_font_t *scaled_font)
{
cairo_scaled_font_t *placeholder_scaled_font;
CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
/* temporary hash value to match the placeholder */
scaled_font->hash_entry.hash
= _cairo_scaled_font_compute_hash (scaled_font);
placeholder_scaled_font =
_cairo_hash_table_lookup (cairo_scaled_font_map->hash_table,
&scaled_font->hash_entry);
assert (placeholder_scaled_font != NULL);
assert (placeholder_scaled_font->placeholder);
assert (CAIRO_MUTEX_IS_LOCKED (placeholder_scaled_font->mutex));
_cairo_hash_table_remove (cairo_scaled_font_map->hash_table,
&placeholder_scaled_font->hash_entry);
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
CAIRO_MUTEX_UNLOCK (placeholder_scaled_font->mutex);
cairo_scaled_font_destroy (placeholder_scaled_font);
CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
}
static void
_cairo_scaled_font_placeholder_wait_for_creation_to_finish (cairo_scaled_font_t *placeholder_scaled_font)
{
/* reference the place holder so it doesn't go away */
cairo_scaled_font_reference (placeholder_scaled_font);
/* now unlock the fontmap mutex so creation has a chance to finish */
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
/* wait on placeholder mutex until we are awaken */
CAIRO_MUTEX_LOCK (placeholder_scaled_font->mutex);
/* ok, creation done. just clean up and back out */
CAIRO_MUTEX_UNLOCK (placeholder_scaled_font->mutex);
cairo_scaled_font_destroy (placeholder_scaled_font);
CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
}
/* Fowler / Noll / Vo (FNV) Hash (http://www.isthe.com/chongo/tech/comp/fnv/)
*
* Not necessarily better than a lot of other hashes, but should be OK, and
* well tested with binary data.
*/
#define FNV_32_PRIME ((uint32_t)0x01000193)
#define FNV1_32_INIT ((uint32_t)0x811c9dc5)
static uint32_t
_hash_matrix_fnv (const cairo_matrix_t *matrix,
uint32_t hval)
{
const uint8_t *buffer = (const uint8_t *) matrix;
int len = sizeof (cairo_matrix_t);
do {
hval *= FNV_32_PRIME;
hval ^= *buffer++;
} while (--len);
return hval;
}
static uint32_t
_hash_mix_bits (uint32_t hash)
{
hash += hash << 12;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return hash;
}
static uint32_t
_cairo_scaled_font_compute_hash (cairo_scaled_font_t *scaled_font)
{
uint32_t hash = FNV1_32_INIT;
/* We do a bytewise hash on the font matrices */
hash = _hash_matrix_fnv (&scaled_font->font_matrix, hash);
hash = _hash_matrix_fnv (&scaled_font->ctm, hash);
hash = _hash_mix_bits (hash);
hash ^= (unsigned long) scaled_font->original_font_face;
hash ^= cairo_font_options_hash (&scaled_font->options);
/* final mixing of bits */
hash = _hash_mix_bits (hash);
assert (hash != ZOMBIE);
return hash;
}
static void
_cairo_scaled_font_init_key (cairo_scaled_font_t *scaled_font,
cairo_font_face_t *font_face,
const cairo_matrix_t *font_matrix,
const cairo_matrix_t *ctm,
const cairo_font_options_t *options)
{
scaled_font->status = CAIRO_STATUS_SUCCESS;
scaled_font->placeholder = FALSE;
scaled_font->font_face = font_face;
scaled_font->original_font_face = font_face;
scaled_font->font_matrix = *font_matrix;
scaled_font->ctm = *ctm;
/* ignore translation values in the ctm */
scaled_font->ctm.x0 = 0.;
scaled_font->ctm.y0 = 0.;
_cairo_font_options_init_copy (&scaled_font->options, options);
scaled_font->hash_entry.hash =
_cairo_scaled_font_compute_hash (scaled_font);
}
static cairo_bool_t
_cairo_scaled_font_keys_equal (const void *abstract_key_a,
const void *abstract_key_b)
{
const cairo_scaled_font_t *key_a = abstract_key_a;
const cairo_scaled_font_t *key_b = abstract_key_b;
return key_a->original_font_face == key_b->original_font_face &&
memcmp ((unsigned char *)(&key_a->font_matrix.xx),
(unsigned char *)(&key_b->font_matrix.xx),
sizeof(cairo_matrix_t)) == 0 &&
memcmp ((unsigned char *)(&key_a->ctm.xx),
(unsigned char *)(&key_b->ctm.xx),
sizeof(cairo_matrix_t)) == 0 &&
cairo_font_options_equal (&key_a->options, &key_b->options);
}
static cairo_bool_t
_cairo_scaled_font_matches (const cairo_scaled_font_t *scaled_font,
const cairo_font_face_t *font_face,
const cairo_matrix_t *font_matrix,
const cairo_matrix_t *ctm,
const cairo_font_options_t *options)
{
return scaled_font->original_font_face == font_face &&
memcmp ((unsigned char *)(&scaled_font->font_matrix.xx),
(unsigned char *)(&font_matrix->xx),
sizeof(cairo_matrix_t)) == 0 &&
memcmp ((unsigned char *)(&scaled_font->ctm.xx),
(unsigned char *)(&ctm->xx),
sizeof(cairo_matrix_t)) == 0 &&
cairo_font_options_equal (&scaled_font->options, options);
}
/*
* Basic #cairo_scaled_font_t object management
*/
cairo_status_t
_cairo_scaled_font_init (cairo_scaled_font_t *scaled_font,
cairo_font_face_t *font_face,
const cairo_matrix_t *font_matrix,
const cairo_matrix_t *ctm,
const cairo_font_options_t *options,
const cairo_scaled_font_backend_t *backend)
{
cairo_status_t status;
status = cairo_font_options_status ((cairo_font_options_t *) options);
if (unlikely (status))
return status;
scaled_font->status = CAIRO_STATUS_SUCCESS;
scaled_font->placeholder = FALSE;
scaled_font->font_face = font_face;
scaled_font->original_font_face = font_face;
scaled_font->font_matrix = *font_matrix;
scaled_font->ctm = *ctm;
/* ignore translation values in the ctm */
scaled_font->ctm.x0 = 0.;
scaled_font->ctm.y0 = 0.;
_cairo_font_options_init_copy (&scaled_font->options, options);
cairo_matrix_multiply (&scaled_font->scale,
&scaled_font->font_matrix,
&scaled_font->ctm);
scaled_font->max_scale = MAX (fabs (scaled_font->scale.xx) + fabs (scaled_font->scale.xy),
fabs (scaled_font->scale.yx) + fabs (scaled_font->scale.yy));
scaled_font->scale_inverse = scaled_font->scale;
status = cairo_matrix_invert (&scaled_font->scale_inverse);
if (unlikely (status)) {
/* If the font scale matrix is rank 0, just using an all-zero inverse matrix
* makes everything work correctly. This make font size 0 work without
* producing an error.
*
* FIXME: If the scale is rank 1, we still go into error mode. But then
* again, that's what we do everywhere in cairo.
*
* Also, the check for == 0. below may be too harsh...
*/
if (_cairo_matrix_is_scale_0 (&scaled_font->scale)) {
cairo_matrix_init (&scaled_font->scale_inverse,
0, 0, 0, 0,
-scaled_font->scale.x0,
-scaled_font->scale.y0);
} else
return status;
}
scaled_font->glyphs = _cairo_hash_table_create (NULL);
if (unlikely (scaled_font->glyphs == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
cairo_list_init (&scaled_font->glyph_pages);
scaled_font->cache_frozen = FALSE;
scaled_font->global_cache_frozen = FALSE;
scaled_font->holdover = FALSE;
scaled_font->finished = FALSE;
CAIRO_REFERENCE_COUNT_INIT (&scaled_font->ref_count, 1);
_cairo_user_data_array_init (&scaled_font->user_data);
cairo_font_face_reference (font_face);
scaled_font->original_font_face = NULL;
CAIRO_MUTEX_INIT (scaled_font->mutex);
cairo_list_init (&scaled_font->dev_privates);
scaled_font->backend = backend;
cairo_list_init (&scaled_font->link);
return CAIRO_STATUS_SUCCESS;
}
void
_cairo_scaled_font_freeze_cache (cairo_scaled_font_t *scaled_font)
{
/* ensure we do not modify an error object */
assert (scaled_font->status == CAIRO_STATUS_SUCCESS);
CAIRO_MUTEX_LOCK (scaled_font->mutex);
scaled_font->cache_frozen = TRUE;
}
void
_cairo_scaled_font_thaw_cache (cairo_scaled_font_t *scaled_font)
{
assert (scaled_font->cache_frozen);
if (scaled_font->global_cache_frozen) {
CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
_cairo_cache_thaw (&cairo_scaled_glyph_page_cache);
CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
scaled_font->global_cache_frozen = FALSE;
}
scaled_font->cache_frozen = FALSE;
CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
}
void
_cairo_scaled_font_reset_cache (cairo_scaled_font_t *scaled_font)
{
cairo_scaled_glyph_page_t *page;
CAIRO_MUTEX_LOCK (scaled_font->mutex);
assert (! scaled_font->cache_frozen);
assert (! scaled_font->global_cache_frozen);
CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
cairo_list_foreach_entry (page,
cairo_scaled_glyph_page_t,
&scaled_font->glyph_pages,
link) {
cairo_scaled_glyph_page_cache.size -= page->cache_entry.size;
_cairo_hash_table_remove (cairo_scaled_glyph_page_cache.hash_table,
(cairo_hash_entry_t *) &page->cache_entry);
}
CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
/* Destroy scaled_font's pages while holding its lock only, and not the
* global page cache lock. The destructor can cause us to recurse and
* end up back here for a different scaled_font. */
while (! cairo_list_is_empty (&scaled_font->glyph_pages)) {
page = cairo_list_first_entry (&scaled_font->glyph_pages,
cairo_scaled_glyph_page_t,
link);
_cairo_scaled_glyph_page_destroy (scaled_font, page);
}
CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
}
cairo_status_t
_cairo_scaled_font_set_metrics (cairo_scaled_font_t *scaled_font,
cairo_font_extents_t *fs_metrics)
{
cairo_status_t status;
double font_scale_x, font_scale_y;
scaled_font->fs_extents = *fs_metrics;
status = _cairo_matrix_compute_basis_scale_factors (&scaled_font->font_matrix,
&font_scale_x, &font_scale_y,
1);
if (unlikely (status))
return status;
/*
* The font responded in unscaled units, scale by the font
* matrix scale factors to get to user space
*/
scaled_font->extents.ascent = fs_metrics->ascent * font_scale_y;
scaled_font->extents.descent = fs_metrics->descent * font_scale_y;
scaled_font->extents.height = fs_metrics->height * font_scale_y;
scaled_font->extents.max_x_advance = fs_metrics->max_x_advance * font_scale_x;
scaled_font->extents.max_y_advance = fs_metrics->max_y_advance * font_scale_y;
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_scaled_font_fini_internal (cairo_scaled_font_t *scaled_font)
{
assert (! scaled_font->cache_frozen);
assert (! scaled_font->global_cache_frozen);
scaled_font->finished = TRUE;
_cairo_scaled_font_reset_cache (scaled_font);
_cairo_hash_table_destroy (scaled_font->glyphs);
cairo_font_face_destroy (scaled_font->font_face);
cairo_font_face_destroy (scaled_font->original_font_face);
CAIRO_MUTEX_FINI (scaled_font->mutex);
while (! cairo_list_is_empty (&scaled_font->dev_privates)) {
cairo_scaled_font_private_t *private =
cairo_list_first_entry (&scaled_font->dev_privates,
cairo_scaled_font_private_t,
link);
private->destroy (private, scaled_font);
}
if (scaled_font->backend != NULL && scaled_font->backend->fini != NULL)
scaled_font->backend->fini (scaled_font);
_cairo_user_data_array_fini (&scaled_font->user_data);
}
void
_cairo_scaled_font_fini (cairo_scaled_font_t *scaled_font)
{
/* Release the lock to avoid the possibility of a recursive
* deadlock when the scaled font destroy closure gets called. */
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
_cairo_scaled_font_fini_internal (scaled_font);
CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
}
void
_cairo_scaled_font_attach_private (cairo_scaled_font_t *scaled_font,
cairo_scaled_font_private_t *private,
const void *key,
void (*destroy) (cairo_scaled_font_private_t *,
cairo_scaled_font_t *))
{
private->key = key;
private->destroy = destroy;
cairo_list_add (&private->link, &scaled_font->dev_privates);
}
cairo_scaled_font_private_t *
_cairo_scaled_font_find_private (cairo_scaled_font_t *scaled_font,
const void *key)
{
cairo_scaled_font_private_t *priv;
cairo_list_foreach_entry (priv, cairo_scaled_font_private_t,
&scaled_font->dev_privates, link)
{
if (priv->key == key) {
if (priv->link.prev != &scaled_font->dev_privates)
cairo_list_move (&priv->link, &scaled_font->dev_privates);
return priv;
}
}
return NULL;
}
void
_cairo_scaled_glyph_attach_private (cairo_scaled_glyph_t *scaled_glyph,
cairo_scaled_glyph_private_t *private,
const void *key,
void (*destroy) (cairo_scaled_glyph_private_t *,
cairo_scaled_glyph_t *,
cairo_scaled_font_t *))
{
private->key = key;
private->destroy = destroy;
cairo_list_add (&private->link, &scaled_glyph->dev_privates);
}
cairo_scaled_glyph_private_t *
_cairo_scaled_glyph_find_private (cairo_scaled_glyph_t *scaled_glyph,
const void *key)
{
cairo_scaled_glyph_private_t *priv;
cairo_list_foreach_entry (priv, cairo_scaled_glyph_private_t,
&scaled_glyph->dev_privates, link)
{
if (priv->key == key) {
if (priv->link.prev != &scaled_glyph->dev_privates)
cairo_list_move (&priv->link, &scaled_glyph->dev_privates);
return priv;
}
}
return NULL;
}
/**
* cairo_scaled_font_create:
* @font_face: a #cairo_font_face_t
* @font_matrix: font space to user space transformation matrix for the
* font. In the simplest case of a N point font, this matrix is
* just a scale by N, but it can also be used to shear the font
* or stretch it unequally along the two axes. See
* cairo_set_font_matrix().
* @ctm: user to device transformation matrix with which the font will
* be used.
* @options: options to use when getting metrics for the font and
* rendering with it.
*
* Creates a #cairo_scaled_font_t object from a font face and matrices that
* describe the size of the font and the environment in which it will
* be used.
*
* Return value: a newly created #cairo_scaled_font_t. Destroy with
* cairo_scaled_font_destroy()
*
* Since: 1.0
**/
cairo_scaled_font_t *
cairo_scaled_font_create (cairo_font_face_t *font_face,
const cairo_matrix_t *font_matrix,
const cairo_matrix_t *ctm,
const cairo_font_options_t *options)
{
cairo_status_t status;
cairo_scaled_font_map_t *font_map;
cairo_font_face_t *original_font_face = font_face;
cairo_scaled_font_t key, *old = NULL, *scaled_font = NULL, *dead = NULL;
double det;
status = font_face->status;
if (unlikely (status))
return _cairo_scaled_font_create_in_error (status);
det = _cairo_matrix_compute_determinant (font_matrix);
if (! ISFINITE (det))
return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_MATRIX));
det = _cairo_matrix_compute_determinant (ctm);
if (! ISFINITE (det))
return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_MATRIX));
status = cairo_font_options_status ((cairo_font_options_t *) options);
if (unlikely (status))
return _cairo_scaled_font_create_in_error (status);
/* Note that degenerate ctm or font_matrix *are* allowed.
* We want to support a font size of 0. */
font_map = _cairo_scaled_font_map_lock ();
if (unlikely (font_map == NULL))
return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
scaled_font = font_map->mru_scaled_font;
if (scaled_font != NULL &&
_cairo_scaled_font_matches (scaled_font,
font_face, font_matrix, ctm, options))
{
assert (scaled_font->hash_entry.hash != ZOMBIE);
assert (! scaled_font->placeholder);
if (likely (scaled_font->status == CAIRO_STATUS_SUCCESS)) {
/* We increment the reference count manually here, (rather
* than calling into cairo_scaled_font_reference), since we
* must modify the reference count while our lock is still
* held. */
_cairo_reference_count_inc (&scaled_font->ref_count);
_cairo_scaled_font_map_unlock ();
return scaled_font;
}
/* the font has been put into an error status - abandon the cache */
_cairo_hash_table_remove (font_map->hash_table,
&scaled_font->hash_entry);
scaled_font->hash_entry.hash = ZOMBIE;
dead = scaled_font;
font_map->mru_scaled_font = NULL;
}
_cairo_scaled_font_init_key (&key, font_face, font_matrix, ctm, options);
while ((scaled_font = _cairo_hash_table_lookup (font_map->hash_table,
&key.hash_entry)))
{
if (! scaled_font->placeholder)
break;
/* If the scaled font is being created (happens for user-font),
* just wait until it's done, then retry */
_cairo_scaled_font_placeholder_wait_for_creation_to_finish (scaled_font);
}
if (scaled_font != NULL) {
/* If the original reference count is 0, then this font must have
* been found in font_map->holdovers, (which means this caching is
* actually working). So now we remove it from the holdovers
* array, unless we caught the font in the middle of destruction.
*/
if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count)) {
if (scaled_font->holdover) {
int i;
for (i = 0; i < font_map->num_holdovers; i++) {
if (font_map->holdovers[i] == scaled_font) {
font_map->num_holdovers--;
memmove (&font_map->holdovers[i],
&font_map->holdovers[i+1],
(font_map->num_holdovers - i) * sizeof (cairo_scaled_font_t*));
break;
}
}
scaled_font->holdover = FALSE;
}
/* reset any error status */
scaled_font->status = CAIRO_STATUS_SUCCESS;
}
if (likely (scaled_font->status == CAIRO_STATUS_SUCCESS)) {
/* We increment the reference count manually here, (rather
* than calling into cairo_scaled_font_reference), since we
* must modify the reference count while our lock is still
* held. */
old = font_map->mru_scaled_font;
font_map->mru_scaled_font = scaled_font;
/* increment reference count for the mru cache */
_cairo_reference_count_inc (&scaled_font->ref_count);
/* and increment for the returned reference */
_cairo_reference_count_inc (&scaled_font->ref_count);
_cairo_scaled_font_map_unlock ();
cairo_scaled_font_destroy (old);
if (font_face != original_font_face)
cairo_font_face_destroy (font_face);
return scaled_font;
}
/* the font has been put into an error status - abandon the cache */
_cairo_hash_table_remove (font_map->hash_table,
&scaled_font->hash_entry);
scaled_font->hash_entry.hash = ZOMBIE;
}
/* Otherwise create it and insert it into the hash table. */
if (font_face->backend->get_implementation != NULL) {
font_face = font_face->backend->get_implementation (font_face,
font_matrix,
ctm,
options);
if (unlikely (font_face->status)) {
_cairo_scaled_font_map_unlock ();
return _cairo_scaled_font_create_in_error (font_face->status);
}
}
status = font_face->backend->scaled_font_create (font_face, font_matrix,
ctm, options, &scaled_font);
/* Did we leave the backend in an error state? */
if (unlikely (status)) {
_cairo_scaled_font_map_unlock ();
if (font_face != original_font_face)
cairo_font_face_destroy (font_face);
if (dead != NULL)
cairo_scaled_font_destroy (dead);
status = _cairo_font_face_set_error (font_face, status);
return _cairo_scaled_font_create_in_error (status);
}
/* Or did we encounter an error whilst constructing the scaled font? */
if (unlikely (scaled_font->status)) {
_cairo_scaled_font_map_unlock ();
if (font_face != original_font_face)
cairo_font_face_destroy (font_face);
if (dead != NULL)
cairo_scaled_font_destroy (dead);
return scaled_font;
}
/* Our caching above is defeated if the backend switches fonts on us -
* e.g. old incarnations of toy-font-face and lazily resolved
* ft-font-faces
*/
assert (scaled_font->font_face == font_face);
assert (! scaled_font->cache_frozen);
assert (! scaled_font->global_cache_frozen);
scaled_font->original_font_face =
cairo_font_face_reference (original_font_face);
scaled_font->hash_entry.hash = _cairo_scaled_font_compute_hash(scaled_font);
status = _cairo_hash_table_insert (font_map->hash_table,
&scaled_font->hash_entry);
if (likely (status == CAIRO_STATUS_SUCCESS)) {
old = font_map->mru_scaled_font;
font_map->mru_scaled_font = scaled_font;
_cairo_reference_count_inc (&scaled_font->ref_count);
}
_cairo_scaled_font_map_unlock ();
cairo_scaled_font_destroy (old);
if (font_face != original_font_face)
cairo_font_face_destroy (font_face);
if (dead != NULL)
cairo_scaled_font_destroy (dead);
if (unlikely (status)) {
/* We can't call _cairo_scaled_font_destroy here since it expects
* that the font has already been successfully inserted into the
* hash table. */
_cairo_scaled_font_fini_internal (scaled_font);
free (scaled_font);
return _cairo_scaled_font_create_in_error (status);
}
return scaled_font;
}
slim_hidden_def (cairo_scaled_font_create);
static cairo_scaled_font_t *_cairo_scaled_font_nil_objects[CAIRO_STATUS_LAST_STATUS + 1];
/* XXX This should disappear in favour of a common pool of error objects. */
cairo_scaled_font_t *
_cairo_scaled_font_create_in_error (cairo_status_t status)
{
cairo_scaled_font_t *scaled_font;
assert (status != CAIRO_STATUS_SUCCESS);
if (status == CAIRO_STATUS_NO_MEMORY)
return (cairo_scaled_font_t *) &_cairo_scaled_font_nil;
CAIRO_MUTEX_LOCK (_cairo_scaled_font_error_mutex);
scaled_font = _cairo_scaled_font_nil_objects[status];
if (unlikely (scaled_font == NULL)) {
scaled_font = _cairo_malloc (sizeof (cairo_scaled_font_t));
if (unlikely (scaled_font == NULL)) {
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
return (cairo_scaled_font_t *) &_cairo_scaled_font_nil;
}
*scaled_font = _cairo_scaled_font_nil;
scaled_font->status = status;
_cairo_scaled_font_nil_objects[status] = scaled_font;
}
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
return scaled_font;
}
void
_cairo_scaled_font_reset_static_data (void)
{
int status;
CAIRO_MUTEX_LOCK (_cairo_scaled_font_error_mutex);
for (status = CAIRO_STATUS_SUCCESS;
status <= CAIRO_STATUS_LAST_STATUS;
status++)
{
free (_cairo_scaled_font_nil_objects[status]);
_cairo_scaled_font_nil_objects[status] = NULL;
}
CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
if (cairo_scaled_glyph_page_cache.hash_table != NULL) {
_cairo_cache_fini (&cairo_scaled_glyph_page_cache);
cairo_scaled_glyph_page_cache.hash_table = NULL;
}
CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
}
/**
* cairo_scaled_font_reference:
* @scaled_font: a #cairo_scaled_font_t, (may be %NULL in which case
* this function does nothing)
*
* Increases the reference count on @scaled_font by one. This prevents
* @scaled_font from being destroyed until a matching call to
* cairo_scaled_font_destroy() is made.
*
* Use cairo_scaled_font_get_reference_count() to get the number of
* references to a #cairo_scaled_font_t.
*
* Returns: the referenced #cairo_scaled_font_t
*
* Since: 1.0
**/
cairo_scaled_font_t *
cairo_scaled_font_reference (cairo_scaled_font_t *scaled_font)
{
if (scaled_font == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
return scaled_font;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
_cairo_reference_count_inc (&scaled_font->ref_count);
return scaled_font;
}
slim_hidden_def (cairo_scaled_font_reference);
/**
* cairo_scaled_font_destroy:
* @scaled_font: a #cairo_scaled_font_t
*
* Decreases the reference count on @font by one. If the result
* is zero, then @font and all associated resources are freed.
* See cairo_scaled_font_reference().
*
* Since: 1.0
**/
void
cairo_scaled_font_destroy (cairo_scaled_font_t *scaled_font)
{
cairo_scaled_font_t *lru = NULL;
cairo_scaled_font_map_t *font_map;
assert (CAIRO_MUTEX_IS_UNLOCKED (_cairo_scaled_font_map_mutex));
if (scaled_font == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
return;
assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
if (! _cairo_reference_count_dec_and_test (&scaled_font->ref_count))
return;
assert (! scaled_font->cache_frozen);
assert (! scaled_font->global_cache_frozen);
font_map = _cairo_scaled_font_map_lock ();
assert (font_map != NULL);
/* Another thread may have resurrected the font whilst we waited */
if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count)) {
if (! scaled_font->placeholder &&
scaled_font->hash_entry.hash != ZOMBIE)
{
/* Another thread may have already inserted us into the holdovers */
if (scaled_font->holdover)
goto unlock;
/* Rather than immediately destroying this object, we put it into
* the font_map->holdovers array in case it will get used again
* soon (and is why we must hold the lock over the atomic op on
* the reference count). To make room for it, we do actually
* destroy the least-recently-used holdover.
*/
if (font_map->num_holdovers == CAIRO_SCALED_FONT_MAX_HOLDOVERS) {
lru = font_map->holdovers[0];
assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&lru->ref_count));
_cairo_hash_table_remove (font_map->hash_table,
&lru->hash_entry);
font_map->num_holdovers--;
memmove (&font_map->holdovers[0],
&font_map->holdovers[1],
font_map->num_holdovers * sizeof (cairo_scaled_font_t*));
}
font_map->holdovers[font_map->num_holdovers++] = scaled_font;
scaled_font->holdover = TRUE;
} else
lru = scaled_font;
}
unlock:
_cairo_scaled_font_map_unlock ();
/* If we pulled an item from the holdovers array, (while the font
* map lock was held, of course), then there is no way that anyone
* else could have acquired a reference to it. So we can now
* safely call fini on it without any lock held. This is desirable
* as we never want to call into any backend function with a lock
* held. */
if (lru != NULL) {
_cairo_scaled_font_fini_internal (lru);
free (lru);
}
}
slim_hidden_def (cairo_scaled_font_destroy);
/**
* cairo_scaled_font_get_reference_count:
* @scaled_font: a #cairo_scaled_font_t
*
* Returns the current reference count of @scaled_font.
*
* Return value: the current reference count of @scaled_font. If the
* object is a nil object, 0 will be returned.
*
* Since: 1.4
**/
unsigned int
cairo_scaled_font_get_reference_count (cairo_scaled_font_t *scaled_font)
{
if (scaled_font == NULL ||
CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
return 0;
return CAIRO_REFERENCE_COUNT_GET_VALUE (&scaled_font->ref_count);
}
/**
* cairo_scaled_font_get_user_data:
* @scaled_font: a #cairo_scaled_font_t
* @key: the address of the #cairo_user_data_key_t the user data was
* attached to
*
* Return user data previously attached to @scaled_font using the
* specified key. If no user data has been attached with the given
* key this function returns %NULL.
*
* Return value: the user data previously attached or %NULL.
*
* Since: 1.4
**/
void *
cairo_scaled_font_get_user_data (cairo_scaled_font_t *scaled_font,
const cairo_user_data_key_t *key)
{
return _cairo_user_data_array_get_data (&scaled_font->user_data,
key);
}
slim_hidden_def (cairo_scaled_font_get_user_data);
/**
* cairo_scaled_font_set_user_data:
* @scaled_font: a #cairo_scaled_font_t
* @key: the address of a #cairo_user_data_key_t to attach the user data to
* @user_data: the user data to attach to the #cairo_scaled_font_t
* @destroy: a #cairo_destroy_func_t which will be called when the
* #cairo_t is destroyed or when new user data is attached using the
* same key.
*
* Attach user data to @scaled_font. To remove user data from a surface,
* call this function with the key that was used to set it and %NULL
* for @data.
*
* Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
* slot could not be allocated for the user data.
*
* Since: 1.4
**/
cairo_status_t
cairo_scaled_font_set_user_data (cairo_scaled_font_t *scaled_font,
const cairo_user_data_key_t *key,
void *user_data,
cairo_destroy_func_t destroy)
{
if (CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
return scaled_font->status;
return _cairo_user_data_array_set_data (&scaled_font->user_data,
key, user_data, destroy);
}
slim_hidden_def (cairo_scaled_font_set_user_data);
/* Public font API follows. */
/**
* cairo_scaled_font_extents:
* @scaled_font: a #cairo_scaled_font_t
* @extents: a #cairo_font_extents_t which to store the retrieved extents.
*
* Gets the metrics for a #cairo_scaled_font_t.
*
* Since: 1.0
**/
void
cairo_scaled_font_extents (cairo_scaled_font_t *scaled_font,
cairo_font_extents_t *extents)
{
if (scaled_font->status) {
extents->ascent = 0.0;
extents->descent = 0.0;
extents->height = 0.0;
extents->max_x_advance = 0.0;
extents->max_y_advance = 0.0;
return;
}
*extents = scaled_font->extents;
}
slim_hidden_def (cairo_scaled_font_extents);
/**
* cairo_scaled_font_text_extents:
* @scaled_font: a #cairo_scaled_font_t
* @utf8: a NUL-terminated string of text, encoded in UTF-8
* @extents: a #cairo_text_extents_t which to store the retrieved extents.
*
* Gets the extents for a string of text. The extents describe a
* user-space rectangle that encloses the "inked" portion of the text
* drawn at the origin (0,0) (as it would be drawn by cairo_show_text()
* if the cairo graphics state were set to the same font_face,
* font_matrix, ctm, and font_options as @scaled_font). Additionally,
* the x_advance and y_advance values indicate the amount by which the
* current point would be advanced by cairo_show_text().
*
* Note that whitespace characters do not directly contribute to the
* size of the rectangle (extents.width and extents.height). They do
* contribute indirectly by changing the position of non-whitespace
* characters. In particular, trailing whitespace characters are
* likely to not affect the size of the rectangle, though they will
* affect the x_advance and y_advance values.
*
* Since: 1.2
**/
void
cairo_scaled_font_text_extents (cairo_scaled_font_t *scaled_font,
const char *utf8,
cairo_text_extents_t *extents)
{
cairo_status_t status;
cairo_glyph_t *glyphs = NULL;
int num_glyphs;
if (scaled_font->status)
goto ZERO_EXTENTS;
if (utf8 == NULL)
goto ZERO_EXTENTS;
status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0.,
utf8, -1,
&glyphs, &num_glyphs,
NULL, NULL,
NULL);
if (unlikely (status)) {
status = _cairo_scaled_font_set_error (scaled_font, status);
goto ZERO_EXTENTS;
}
cairo_scaled_font_glyph_extents (scaled_font, glyphs, num_glyphs, extents);
free (glyphs);
return;
ZERO_EXTENTS:
extents->x_bearing = 0.0;
extents->y_bearing = 0.0;
extents->width = 0.0;
extents->height = 0.0;
extents->x_advance = 0.0;
extents->y_advance = 0.0;
}
/**
* cairo_scaled_font_glyph_extents:
* @scaled_font: a #cairo_scaled_font_t
* @glyphs: an array of glyph IDs with X and Y offsets.
* @num_glyphs: the number of glyphs in the @glyphs array
* @extents: a #cairo_text_extents_t which to store the retrieved extents.
*
* Gets the extents for an array of glyphs. The extents describe a
* user-space rectangle that encloses the "inked" portion of the
* glyphs, (as they would be drawn by cairo_show_glyphs() if the cairo
* graphics state were set to the same font_face, font_matrix, ctm,
* and font_options as @scaled_font). Additionally, the x_advance and
* y_advance values indicate the amount by which the current point
* would be advanced by cairo_show_glyphs().
*
* Note that whitespace glyphs do not contribute to the size of the
* rectangle (extents.width and extents.height).
*
* Since: 1.0
**/
void
cairo_scaled_font_glyph_extents (cairo_scaled_font_t *scaled_font,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_text_extents_t *extents)
{
cairo_status_t status;
int i;
double min_x = 0.0, min_y = 0.0, max_x = 0.0, max_y = 0.0;
cairo_bool_t visible = FALSE;
cairo_scaled_glyph_t *scaled_glyph = NULL;
extents->x_bearing = 0.0;
extents->y_bearing = 0.0;
extents->width = 0.0;
extents->height = 0.0;
extents->x_advance = 0.0;
extents->y_advance = 0.0;
if (unlikely (scaled_font->status))
goto ZERO_EXTENTS;
if (num_glyphs == 0)
goto ZERO_EXTENTS;
if (unlikely (num_glyphs < 0)) {
_cairo_error_throw (CAIRO_STATUS_NEGATIVE_COUNT);
/* XXX Can't propagate error */
goto ZERO_EXTENTS;
}
if (unlikely (glyphs == NULL)) {
_cairo_error_throw (CAIRO_STATUS_NULL_POINTER);
/* XXX Can't propagate error */
goto ZERO_EXTENTS;
}
_cairo_scaled_font_freeze_cache (scaled_font);
for (i = 0; i < num_glyphs; i++) {
double left, top, right, bottom;
status = _cairo_scaled_glyph_lookup (scaled_font,
glyphs[i].index,
CAIRO_SCALED_GLYPH_INFO_METRICS,
&scaled_glyph);
if (unlikely (status)) {
status = _cairo_scaled_font_set_error (scaled_font, status);
goto UNLOCK;
}
/* "Ink" extents should skip "invisible" glyphs */
if (scaled_glyph->metrics.width == 0 || scaled_glyph->metrics.height == 0)
continue;
left = scaled_glyph->metrics.x_bearing + glyphs[i].x;
right = left + scaled_glyph->metrics.width;
top = scaled_glyph->metrics.y_bearing + glyphs[i].y;
bottom = top + scaled_glyph->metrics.height;
if (!visible) {
visible = TRUE;
min_x = left;
max_x = right;
min_y = top;
max_y = bottom;
} else {
if (left < min_x) min_x = left;
if (right > max_x) max_x = right;
if (top < min_y) min_y = top;
if (bottom > max_y) max_y = bottom;
}
}
if (visible) {
extents->x_bearing = min_x - glyphs[0].x;
extents->y_bearing = min_y - glyphs[0].y;
extents->width = max_x - min_x;
extents->height = max_y - min_y;
} else {
extents->x_bearing = 0.0;
extents->y_bearing = 0.0;
extents->width = 0.0;
extents->height = 0.0;
}
if (num_glyphs) {
double x0, y0, x1, y1;
x0 = glyphs[0].x;
y0 = glyphs[0].y;
/* scaled_glyph contains the glyph for num_glyphs - 1 already. */
x1 = glyphs[num_glyphs - 1].x + scaled_glyph->metrics.x_advance;
y1 = glyphs[num_glyphs - 1].y + scaled_glyph->metrics.y_advance;
extents->x_advance = x1 - x0;
extents->y_advance = y1 - y0;
} else {
extents->x_advance = 0.0;
extents->y_advance = 0.0;
}
UNLOCK:
_cairo_scaled_font_thaw_cache (scaled_font);
return;
ZERO_EXTENTS:
extents->x_bearing = 0.0;
extents->y_bearing = 0.0;
extents->width = 0.0;
extents->height = 0.0;
extents->x_advance = 0.0;
extents->y_advance = 0.0;
}
slim_hidden_def (cairo_scaled_font_glyph_extents);
#define GLYPH_LUT_SIZE 64
static cairo_status_t
cairo_scaled_font_text_to_glyphs_internal_cached (cairo_scaled_font_t *scaled_font,
double x,
double y,
const char *utf8,
cairo_glyph_t *glyphs,
cairo_text_cluster_t **clusters,
int num_chars)
{
struct glyph_lut_elt {
unsigned long index;
double x_advance;
double y_advance;
} glyph_lut[GLYPH_LUT_SIZE];
uint32_t glyph_lut_unicode[GLYPH_LUT_SIZE];
cairo_status_t status;
const char *p;
int i;
for (i = 0; i < GLYPH_LUT_SIZE; i++)
glyph_lut_unicode[i] = ~0U;
p = utf8;
for (i = 0; i < num_chars; i++) {
int idx, num_bytes;
uint32_t unicode;
cairo_scaled_glyph_t *scaled_glyph;
struct glyph_lut_elt *glyph_slot;
num_bytes = _cairo_utf8_get_char_validated (p, &unicode);
p += num_bytes;
glyphs[i].x = x;
glyphs[i].y = y;
idx = unicode % ARRAY_LENGTH (glyph_lut);
glyph_slot = &glyph_lut[idx];
if (glyph_lut_unicode[idx] == unicode) {
glyphs[i].index = glyph_slot->index;
x += glyph_slot->x_advance;
y += glyph_slot->y_advance;
} else {
unsigned long g;
g = scaled_font->backend->ucs4_to_index (scaled_font, unicode);
status = _cairo_scaled_glyph_lookup (scaled_font,
g,
CAIRO_SCALED_GLYPH_INFO_METRICS,
&scaled_glyph);
if (unlikely (status))
return status;
x += scaled_glyph->metrics.x_advance;
y += scaled_glyph->metrics.y_advance;
glyph_lut_unicode[idx] = unicode;
glyph_slot->index = g;
glyph_slot->x_advance = scaled_glyph->metrics.x_advance;
glyph_slot->y_advance = scaled_glyph->metrics.y_advance;
glyphs[i].index = g;
}
if (clusters) {
(*clusters)[i].num_bytes = num_bytes;
(*clusters)[i].num_glyphs = 1;
}
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
cairo_scaled_font_text_to_glyphs_internal_uncached (cairo_scaled_font_t *scaled_font,
double x,
double y,
const char *utf8,
cairo_glyph_t *glyphs,
cairo_text_cluster_t **clusters,
int num_chars)
{
const char *p;
int i;
p = utf8;
for (i = 0; i < num_chars; i++) {
unsigned long g;
int num_bytes;
uint32_t unicode;
cairo_scaled_glyph_t *scaled_glyph;
cairo_status_t status;
num_bytes = _cairo_utf8_get_char_validated (p, &unicode);
p += num_bytes;
glyphs[i].x = x;
glyphs[i].y = y;
g = scaled_font->backend->ucs4_to_index (scaled_font, unicode);
/*
* No advance needed for a single character string. So, let's speed up
* one-character strings by skipping glyph lookup.
*/
if (num_chars > 1) {
status = _cairo_scaled_glyph_lookup (scaled_font,
g,
CAIRO_SCALED_GLYPH_INFO_METRICS,
&scaled_glyph);
if (unlikely (status))
return status;
x += scaled_glyph->metrics.x_advance;
y += scaled_glyph->metrics.y_advance;
}
glyphs[i].index = g;
if (clusters) {
(*clusters)[i].num_bytes = num_bytes;
(*clusters)[i].num_glyphs = 1;
}
}
return CAIRO_STATUS_SUCCESS;
}
/**
* cairo_scaled_font_text_to_glyphs:
* @x: X position to place first glyph
* @y: Y position to place first glyph
* @scaled_font: a #cairo_scaled_font_t
* @utf8: a string of text encoded in UTF-8
* @utf8_len: length of @utf8 in bytes, or -1 if it is NUL-terminated
* @glyphs: pointer to array of glyphs to fill
* @num_glyphs: pointer to number of glyphs
* @clusters: pointer to array of cluster mapping information to fill, or %NULL
* @num_clusters: pointer to number of clusters, or %NULL
* @cluster_flags: pointer to location to store cluster flags corresponding to the
* output @clusters, or %NULL
*
* Converts UTF-8 text to an array of glyphs, optionally with cluster
* mapping, that can be used to render later using @scaled_font.
*
* If @glyphs initially points to a non-%NULL value, that array is used
* as a glyph buffer, and @num_glyphs should point to the number of glyph
* entries available there. If the provided glyph array is too short for
* the conversion, a new glyph array is allocated using cairo_glyph_allocate()
* and placed in @glyphs. Upon return, @num_glyphs always contains the
* number of generated glyphs. If the value @glyphs points to has changed
* after the call, the user is responsible for freeing the allocated glyph
* array using cairo_glyph_free(). This may happen even if the provided
* array was large enough.
*
* If @clusters is not %NULL, @num_clusters and @cluster_flags should not be %NULL,
* and cluster mapping will be computed.
* The semantics of how cluster array allocation works is similar to the glyph
* array. That is,
* if @clusters initially points to a non-%NULL value, that array is used
* as a cluster buffer, and @num_clusters should point to the number of cluster
* entries available there. If the provided cluster array is too short for
* the conversion, a new cluster array is allocated using cairo_text_cluster_allocate()
* and placed in @clusters. Upon return, @num_clusters always contains the
* number of generated clusters. If the value @clusters points at has changed
* after the call, the user is responsible for freeing the allocated cluster
* array using cairo_text_cluster_free(). This may happen even if the provided
* array was large enough.
*
* In the simplest case, @glyphs and @clusters can point to %NULL initially
* and a suitable array will be allocated. In code:
* <informalexample><programlisting>
* cairo_status_t status;
*
* cairo_glyph_t *glyphs = NULL;
* int num_glyphs;
* cairo_text_cluster_t *clusters = NULL;
* int num_clusters;
* cairo_text_cluster_flags_t cluster_flags;
*
* status = cairo_scaled_font_text_to_glyphs (scaled_font,
* x, y,
* utf8, utf8_len,
* &glyphs, &num_glyphs,
* &clusters, &num_clusters, &cluster_flags);
*
* if (status == CAIRO_STATUS_SUCCESS) {
* cairo_show_text_glyphs (cr,
* utf8, utf8_len,
* glyphs, num_glyphs,
* clusters, num_clusters, cluster_flags);
*
* cairo_glyph_free (glyphs);
* cairo_text_cluster_free (clusters);
* }
* </programlisting></informalexample>
*
* If no cluster mapping is needed:
* <informalexample><programlisting>
* cairo_status_t status;
*
* cairo_glyph_t *glyphs = NULL;
* int num_glyphs;
*
* status = cairo_scaled_font_text_to_glyphs (scaled_font,
* x, y,
* utf8, utf8_len,
* &glyphs, &num_glyphs,
* NULL, NULL,
* NULL);
*
* if (status == CAIRO_STATUS_SUCCESS) {
* cairo_show_glyphs (cr, glyphs, num_glyphs);
* cairo_glyph_free (glyphs);
* }
* </programlisting></informalexample>
*
* If stack-based glyph and cluster arrays are to be used for small
* arrays:
* <informalexample><programlisting>
* cairo_status_t status;
*
* cairo_glyph_t stack_glyphs[40];
* cairo_glyph_t *glyphs = stack_glyphs;
* int num_glyphs = sizeof (stack_glyphs) / sizeof (stack_glyphs[0]);
* cairo_text_cluster_t stack_clusters[40];
* cairo_text_cluster_t *clusters = stack_clusters;
* int num_clusters = sizeof (stack_clusters) / sizeof (stack_clusters[0]);
* cairo_text_cluster_flags_t cluster_flags;
*
* status = cairo_scaled_font_text_to_glyphs (scaled_font,
* x, y,
* utf8, utf8_len,
* &glyphs, &num_glyphs,
* &clusters, &num_clusters, &cluster_flags);
*
* if (status == CAIRO_STATUS_SUCCESS) {
* cairo_show_text_glyphs (cr,
* utf8, utf8_len,
* glyphs, num_glyphs,
* clusters, num_clusters, cluster_flags);
*
* if (glyphs != stack_glyphs)
* cairo_glyph_free (glyphs);
* if (clusters != stack_clusters)
* cairo_text_cluster_free (clusters);
* }
* </programlisting></informalexample>
*
* For details of how @clusters, @num_clusters, and @cluster_flags map input
* UTF-8 text to the output glyphs see cairo_show_text_glyphs().
*
* The output values can be readily passed to cairo_show_text_glyphs()
* cairo_show_glyphs(), or related functions, assuming that the exact
* same @scaled_font is used for the operation.
*
* Return value: %CAIRO_STATUS_SUCCESS upon success, or an error status
* if the input values are wrong or if conversion failed. If the input
* values are correct but the conversion failed, the error status is also
* set on @scaled_font.
*
* Since: 1.8
**/
#define CACHING_THRESHOLD 16
cairo_status_t
cairo_scaled_font_text_to_glyphs (cairo_scaled_font_t *scaled_font,
double x,
double y,
const char *utf8,
int utf8_len,
cairo_glyph_t **glyphs,
int *num_glyphs,
cairo_text_cluster_t **clusters,
int *num_clusters,
cairo_text_cluster_flags_t *cluster_flags)
{
int num_chars = 0;
cairo_int_status_t status;
cairo_glyph_t *orig_glyphs;
cairo_text_cluster_t *orig_clusters;
status = scaled_font->status;
if (unlikely (status))
return status;
/* A slew of sanity checks */
/* glyphs and num_glyphs can't be NULL */
if (glyphs == NULL ||
num_glyphs == NULL) {
status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
goto BAIL;
}
/* Special case for NULL and -1 */
if (utf8 == NULL && utf8_len == -1)
utf8_len = 0;
/* No NULLs for non-NULLs! */
if ((utf8_len && utf8 == NULL) ||
(clusters && num_clusters == NULL) ||
(clusters && cluster_flags == NULL)) {
status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
goto BAIL;
}
/* A -1 for utf8_len means NUL-terminated */
if (utf8_len == -1)
utf8_len = strlen (utf8);
/* A NULL *glyphs means no prealloced glyphs array */
if (glyphs && *glyphs == NULL)
*num_glyphs = 0;
/* A NULL *clusters means no prealloced clusters array */
if (clusters && *clusters == NULL)
*num_clusters = 0;
if (!clusters && num_clusters) {
num_clusters = NULL;
}
if (cluster_flags) {
*cluster_flags = FALSE;
}
if (!clusters && cluster_flags) {
cluster_flags = NULL;
}
/* Apart from that, no negatives */
if (utf8_len < 0 ||
*num_glyphs < 0 ||
(num_clusters && *num_clusters < 0)) {
status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
goto BAIL;
}
if (utf8_len == 0) {
status = CAIRO_STATUS_SUCCESS;
goto BAIL;
}
/* validate input so backend does not have to */
status = _cairo_utf8_to_ucs4 (utf8, utf8_len, NULL, &num_chars);
if (unlikely (status))
goto BAIL;
_cairo_scaled_font_freeze_cache (scaled_font);
orig_glyphs = *glyphs;
orig_clusters = clusters ? *clusters : NULL;
if (scaled_font->backend->text_to_glyphs) {
status = scaled_font->backend->text_to_glyphs (scaled_font, x, y,
utf8, utf8_len,
glyphs, num_glyphs,
clusters, num_clusters,
cluster_flags);
if (status != CAIRO_INT_STATUS_UNSUPPORTED) {
if (status == CAIRO_INT_STATUS_SUCCESS) {
/* The checks here are crude; we only should do them in
* user-font backend, but they don't hurt here. This stuff
* can be hard to get right. */
if (*num_glyphs < 0) {
status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
goto DONE;
}
if (*num_glyphs != 0 && *glyphs == NULL) {
status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
goto DONE;
}
if (clusters) {
if (*num_clusters < 0) {
status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
goto DONE;
}
if (*num_clusters != 0 && *clusters == NULL) {
status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
goto DONE;
}
/* Don't trust the backend, validate clusters! */
status =
_cairo_validate_text_clusters (utf8, utf8_len,
*glyphs, *num_glyphs,
*clusters, *num_clusters,
*cluster_flags);
}
}
goto DONE;
}
}
if (*num_glyphs < num_chars) {
*glyphs = cairo_glyph_allocate (num_chars);
if (unlikely (*glyphs == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto DONE;
}
}
*num_glyphs = num_chars;
if (clusters) {
if (*num_clusters < num_chars) {
*clusters = cairo_text_cluster_allocate (num_chars);
if (unlikely (*clusters == NULL)) {
status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
goto DONE;
}
}
*num_clusters = num_chars;
}
if (num_chars > CACHING_THRESHOLD)
status = cairo_scaled_font_text_to_glyphs_internal_cached (scaled_font,
x, y,
utf8,
*glyphs,
clusters,
num_chars);
else
status = cairo_scaled_font_text_to_glyphs_internal_uncached (scaled_font,
x, y,
utf8,
*glyphs,
clusters,
num_chars);
DONE: /* error that should be logged on scaled_font happened */
_cairo_scaled_font_thaw_cache (scaled_font);
if (unlikely (status)) {
*num_glyphs = 0;
if (*glyphs != orig_glyphs) {
cairo_glyph_free (*glyphs);
*glyphs = orig_glyphs;
}
if (clusters) {
*num_clusters = 0;
if (*clusters != orig_clusters) {
cairo_text_cluster_free (*clusters);
*clusters = orig_clusters;
}
}
}
return _cairo_scaled_font_set_error (scaled_font, status);
BAIL: /* error with input arguments */
if (num_glyphs)
*num_glyphs = 0;
if (num_clusters)
*num_clusters = 0;
return status;
}
slim_hidden_def (cairo_scaled_font_text_to_glyphs);
static inline cairo_bool_t
_range_contains_glyph (const cairo_box_t *extents,
cairo_fixed_t left,
cairo_fixed_t top,
cairo_fixed_t right,
cairo_fixed_t bottom)
{
if (left == right || top == bottom)
return FALSE;
return right > extents->p1.x &&
left < extents->p2.x &&
bottom > extents->p1.y &&
top < extents->p2.y;
}
static cairo_status_t
_cairo_scaled_font_single_glyph_device_extents (cairo_scaled_font_t *scaled_font,
const cairo_glyph_t *glyph,
cairo_rectangle_int_t *extents)
{
cairo_scaled_glyph_t *scaled_glyph;
cairo_status_t status;
_cairo_scaled_font_freeze_cache (scaled_font);
status = _cairo_scaled_glyph_lookup (scaled_font,
glyph->index,
CAIRO_SCALED_GLYPH_INFO_METRICS,
&scaled_glyph);
if (likely (status == CAIRO_STATUS_SUCCESS)) {
cairo_bool_t round_xy = _cairo_font_options_get_round_glyph_positions (&scaled_font->options) == CAIRO_ROUND_GLYPH_POS_ON;
cairo_box_t box;
cairo_fixed_t v;
if (round_xy)
v = _cairo_fixed_from_int (_cairo_lround (glyph->x));
else
v = _cairo_fixed_from_double (glyph->x);
box.p1.x = v + scaled_glyph->bbox.p1.x;
box.p2.x = v + scaled_glyph->bbox.p2.x;
if (round_xy)
v = _cairo_fixed_from_int (_cairo_lround (glyph->y));
else
v = _cairo_fixed_from_double (glyph->y);
box.p1.y = v + scaled_glyph->bbox.p1.y;
box.p2.y = v + scaled_glyph->bbox.p2.y;
_cairo_box_round_to_rectangle (&box, extents);
}
_cairo_scaled_font_thaw_cache (scaled_font);
return status;
}
/*
* Compute a device-space bounding box for the glyphs.
*/
cairo_status_t
_cairo_scaled_font_glyph_device_extents (cairo_scaled_font_t *scaled_font,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_rectangle_int_t *extents,
cairo_bool_t *overlap_out)
{
cairo_status_t status = CAIRO_STATUS_SUCCESS;
cairo_box_t box = { { INT_MAX, INT_MAX }, { INT_MIN, INT_MIN }};
cairo_scaled_glyph_t *glyph_cache[64];
cairo_bool_t overlap = overlap_out ? FALSE : TRUE;
cairo_round_glyph_positions_t round_glyph_positions = _cairo_font_options_get_round_glyph_positions (&scaled_font->options);
int i;
if (unlikely (scaled_font->status))
return scaled_font->status;
if (num_glyphs == 1) {
if (overlap_out)
*overlap_out = FALSE;
return _cairo_scaled_font_single_glyph_device_extents (scaled_font,
glyphs,
extents);
}
_cairo_scaled_font_freeze_cache (scaled_font);
memset (glyph_cache, 0, sizeof (glyph_cache));
for (i = 0; i < num_glyphs; i++) {
cairo_scaled_glyph_t *scaled_glyph;
cairo_fixed_t x, y, x1, y1, x2, y2;
int cache_index = glyphs[i].index % ARRAY_LENGTH (glyph_cache);
scaled_glyph = glyph_cache[cache_index];
if (scaled_glyph == NULL ||
_cairo_scaled_glyph_index (scaled_glyph) != glyphs[i].index)
{
status = _cairo_scaled_glyph_lookup (scaled_font,
glyphs[i].index,
CAIRO_SCALED_GLYPH_INFO_METRICS,
&scaled_glyph);
if (unlikely (status))
break;
glyph_cache[cache_index] = scaled_glyph;
}
if (round_glyph_positions == CAIRO_ROUND_GLYPH_POS_ON)
x = _cairo_fixed_from_int (_cairo_lround (glyphs[i].x));
else
x = _cairo_fixed_from_double (glyphs[i].x);
x1 = x + scaled_glyph->bbox.p1.x;
x2 = x + scaled_glyph->bbox.p2.x;
if (round_glyph_positions == CAIRO_ROUND_GLYPH_POS_ON)
y = _cairo_fixed_from_int (_cairo_lround (glyphs[i].y));
else
y = _cairo_fixed_from_double (glyphs[i].y);
y1 = y + scaled_glyph->bbox.p1.y;
y2 = y + scaled_glyph->bbox.p2.y;
if (overlap == FALSE)
overlap = _range_contains_glyph (&box, x1, y1, x2, y2);
if (x1 < box.p1.x) box.p1.x = x1;
if (x2 > box.p2.x) box.p2.x = x2;
if (y1 < box.p1.y) box.p1.y = y1;
if (y2 > box.p2.y) box.p2.y = y2;
}
_cairo_scaled_font_thaw_cache (scaled_font);
if (unlikely (status))
return _cairo_scaled_font_set_error (scaled_font, status);
if (box.p1.x < box.p2.x) {
_cairo_box_round_to_rectangle (&box, extents);
} else {
extents->x = extents->y = 0;
extents->width = extents->height = 0;
}
if (overlap_out != NULL)
*overlap_out = overlap;
return CAIRO_STATUS_SUCCESS;
}
cairo_bool_t
_cairo_scaled_font_glyph_approximate_extents (cairo_scaled_font_t *scaled_font,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_rectangle_int_t *extents)
{
double x0, x1, y0, y1, pad;
int i;
/* If any of the factors are suspect (i.e. the font is broken), bail */
if (scaled_font->fs_extents.max_x_advance == 0 ||
scaled_font->fs_extents.height == 0 ||
scaled_font->max_scale == 0)
{
return FALSE;
}
assert (num_glyphs);
x0 = x1 = glyphs[0].x;
y0 = y1 = glyphs[0].y;
for (i = 1; i < num_glyphs; i++) {
double g;
g = glyphs[i].x;
if (g < x0) x0 = g;
if (g > x1) x1 = g;
g = glyphs[i].y;
if (g < y0) y0 = g;
if (g > y1) y1 = g;
}
pad = MAX(scaled_font->fs_extents.max_x_advance,
scaled_font->fs_extents.height);
pad *= scaled_font->max_scale;
extents->x = floor (x0 - pad);
extents->width = ceil (x1 + pad) - extents->x;
extents->y = floor (y0 - pad);
extents->height = ceil (y1 + pad) - extents->y;
return TRUE;
}
#if 0
/* XXX win32 */
cairo_status_t
_cairo_scaled_font_show_glyphs (cairo_scaled_font_t *scaled_font,
cairo_operator_t op,
const cairo_pattern_t *pattern,
cairo_surface_t *surface,
int source_x,
int source_y,
int dest_x,
int dest_y,
unsigned int width,
unsigned int height,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_region_t *clip_region)
{
cairo_int_status_t status;
cairo_surface_t *mask = NULL;
cairo_format_t mask_format = CAIRO_FORMAT_A1; /* shut gcc up */
cairo_surface_pattern_t mask_pattern;
int i;
/* These operators aren't interpreted the same way by the backends;
* they are implemented in terms of other operators in cairo-gstate.c
*/
assert (op != CAIRO_OPERATOR_SOURCE && op != CAIRO_OPERATOR_CLEAR);
if (scaled_font->status)
return scaled_font->status;
if (!num_glyphs)
return CAIRO_STATUS_SUCCESS;
if (scaled_font->backend->show_glyphs != NULL) {
int remaining_glyphs = num_glyphs;
status = scaled_font->backend->show_glyphs (scaled_font,
op, pattern,
surface,
source_x, source_y,
dest_x, dest_y,
width, height,
glyphs, num_glyphs,
clip_region,
&remaining_glyphs);
glyphs += num_glyphs - remaining_glyphs;
num_glyphs = remaining_glyphs;
if (remaining_glyphs == 0)
status = CAIRO_INT_STATUS_SUCCESS;
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
return _cairo_scaled_font_set_error (scaled_font, status);
}
/* Font display routine either does not exist or failed. */
_cairo_scaled_font_freeze_cache (scaled_font);
for (i = 0; i < num_glyphs; i++) {
int x, y;
cairo_image_surface_t *glyph_surface;
cairo_scaled_glyph_t *scaled_glyph;
status = _cairo_scaled_glyph_lookup (scaled_font,
glyphs[i].index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status))
goto CLEANUP_MASK;
glyph_surface = scaled_glyph->surface;
/* To start, create the mask using the format from the first
* glyph. Later we'll deal with different formats. */
if (mask == NULL) {
mask_format = glyph_surface->format;
mask = cairo_image_surface_create (mask_format, width, height);
status = mask->status;
if (unlikely (status))
goto CLEANUP_MASK;
}
/* If we have glyphs of different formats, we "upgrade" the mask
* to the wider of the formats. */
if (glyph_surface->format != mask_format &&
_cairo_format_bits_per_pixel (mask_format) <
_cairo_format_bits_per_pixel (glyph_surface->format) )
{
cairo_surface_t *new_mask;
switch (glyph_surface->format) {
case CAIRO_FORMAT_ARGB32:
case CAIRO_FORMAT_A8:
case CAIRO_FORMAT_A1:
mask_format = glyph_surface->format;
break;
case CAIRO_FORMAT_RGB16_565:
case CAIRO_FORMAT_RGB24:
case CAIRO_FORMAT_RGB30:
case CAIRO_FORMAT_INVALID:
default:
ASSERT_NOT_REACHED;
mask_format = CAIRO_FORMAT_ARGB32;
break;
}
new_mask = cairo_image_surface_create (mask_format, width, height);
status = new_mask->status;
if (unlikely (status)) {
cairo_surface_destroy (new_mask);
goto CLEANUP_MASK;
}
_cairo_pattern_init_for_surface (&mask_pattern, mask);
/* Note that we only upgrade masks, i.e. A1 -> A8 -> ARGB32, so there is
* never any component alpha here.
*/
status = _cairo_surface_composite (CAIRO_OPERATOR_ADD,
&_cairo_pattern_white.base,
&mask_pattern.base,
new_mask,
0, 0,
0, 0,
0, 0,
width, height,
NULL);
_cairo_pattern_fini (&mask_pattern.base);
if (unlikely (status)) {
cairo_surface_destroy (new_mask);
goto CLEANUP_MASK;
}
cairo_surface_destroy (mask);
mask = new_mask;
}
if (glyph_surface->width && glyph_surface->height) {
cairo_surface_pattern_t glyph_pattern;
/* round glyph locations to the nearest pixel */
/* XXX: FRAGILE: We're ignoring device_transform scaling here. A bug? */
x = _cairo_lround (glyphs[i].x -
glyph_surface->base.device_transform.x0);
y = _cairo_lround (glyphs[i].y -
glyph_surface->base.device_transform.y0);
_cairo_pattern_init_for_surface (&glyph_pattern,
&glyph_surface->base);
if (mask_format == CAIRO_FORMAT_ARGB32)
glyph_pattern.base.has_component_alpha = TRUE;
status = _cairo_surface_composite (CAIRO_OPERATOR_ADD,
&_cairo_pattern_white.base,
&glyph_pattern.base,
mask,
0, 0,
0, 0,
x - dest_x, y - dest_y,
glyph_surface->width,
glyph_surface->height,
NULL);
_cairo_pattern_fini (&glyph_pattern.base);
if (unlikely (status))
goto CLEANUP_MASK;
}
}
_cairo_pattern_init_for_surface (&mask_pattern, mask);
if (mask_format == CAIRO_FORMAT_ARGB32)
mask_pattern.base.has_component_alpha = TRUE;
status = _cairo_surface_composite (op, pattern, &mask_pattern.base,
surface,
source_x, source_y,
0, 0,
dest_x, dest_y,
width, height,
clip_region);
_cairo_pattern_fini (&mask_pattern.base);
CLEANUP_MASK:
_cairo_scaled_font_thaw_cache (scaled_font);
if (mask != NULL)
cairo_surface_destroy (mask);
return _cairo_scaled_font_set_error (scaled_font, status);
}
#endif
/* Add a single-device-unit rectangle to a path. */
static cairo_status_t
_add_unit_rectangle_to_path (cairo_path_fixed_t *path,
cairo_fixed_t x,
cairo_fixed_t y)
{
cairo_status_t status;
status = _cairo_path_fixed_move_to (path, x, y);
if (unlikely (status))
return status;
status = _cairo_path_fixed_rel_line_to (path,
_cairo_fixed_from_int (1),
_cairo_fixed_from_int (0));
if (unlikely (status))
return status;
status = _cairo_path_fixed_rel_line_to (path,
_cairo_fixed_from_int (0),
_cairo_fixed_from_int (1));
if (unlikely (status))
return status;
status = _cairo_path_fixed_rel_line_to (path,
_cairo_fixed_from_int (-1),
_cairo_fixed_from_int (0));
if (unlikely (status))
return status;
return _cairo_path_fixed_close_path (path);
}
/**
* _trace_mask_to_path:
* @bitmap: An alpha mask (either %CAIRO_FORMAT_A1 or %CAIRO_FORMAT_A8)
* @path: An initialized path to hold the result
*
* Given a mask surface, (an alpha image), fill out the provided path
* so that when filled it would result in something that approximates
* the mask.
*
* Note: The current tracing code here is extremely primitive. It
* operates only on an A1 surface, (converting an A8 surface to A1 if
* necessary), and performs the tracing by drawing a little square
* around each pixel that is on in the mask. We do not pretend that
* this is a high-quality result. But we are leaving it up to someone
* who cares enough about getting a better result to implement
* something more sophisticated.
**/
static cairo_status_t
_trace_mask_to_path (cairo_image_surface_t *mask,
cairo_path_fixed_t *path,
double tx, double ty)
{
const uint8_t *row;
int rows, cols, bytes_per_row;
int x, y, bit;
double xoff, yoff;
cairo_fixed_t x0, y0;
cairo_fixed_t px, py;
cairo_status_t status;
mask = _cairo_image_surface_coerce_to_format (mask, CAIRO_FORMAT_A1);
status = mask->base.status;
if (unlikely (status))
return status;
cairo_surface_get_device_offset (&mask->base, &xoff, &yoff);
x0 = _cairo_fixed_from_double (tx - xoff);
y0 = _cairo_fixed_from_double (ty - yoff);
bytes_per_row = (mask->width + 7) / 8;
row = mask->data;
for (y = 0, rows = mask->height; rows--; row += mask->stride, y++) {
const uint8_t *byte_ptr = row;
x = 0;
py = _cairo_fixed_from_int (y);
for (cols = bytes_per_row; cols--; ) {
uint8_t byte = *byte_ptr++;
if (byte == 0) {
x += 8;
continue;
}
byte = CAIRO_BITSWAP8_IF_LITTLE_ENDIAN (byte);
for (bit = 1 << 7; bit && x < mask->width; bit >>= 1, x++) {
if (byte & bit) {
px = _cairo_fixed_from_int (x);
status = _add_unit_rectangle_to_path (path,
px + x0,
py + y0);
if (unlikely (status))
goto BAIL;
}
}
}
}
BAIL:
cairo_surface_destroy (&mask->base);
return status;
}
cairo_status_t
_cairo_scaled_font_glyph_path (cairo_scaled_font_t *scaled_font,
const cairo_glyph_t *glyphs,
int num_glyphs,
cairo_path_fixed_t *path)
{
cairo_int_status_t status;
int i;
status = scaled_font->status;
if (unlikely (status))
return status;
_cairo_scaled_font_freeze_cache (scaled_font);
for (i = 0; i < num_glyphs; i++) {
cairo_scaled_glyph_t *scaled_glyph;
status = _cairo_scaled_glyph_lookup (scaled_font,
glyphs[i].index,
CAIRO_SCALED_GLYPH_INFO_PATH,
&scaled_glyph);
if (status == CAIRO_INT_STATUS_SUCCESS) {
status = _cairo_path_fixed_append (path,
scaled_glyph->path,
_cairo_fixed_from_double (glyphs[i].x),
_cairo_fixed_from_double (glyphs[i].y));
} else if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
/* If the font is incapable of providing a path, then we'll
* have to trace our own from a surface.
*/
status = _cairo_scaled_glyph_lookup (scaled_font,
glyphs[i].index,
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status))
goto BAIL;
status = _trace_mask_to_path (scaled_glyph->surface, path,
glyphs[i].x, glyphs[i].y);
}
if (unlikely (status))
goto BAIL;
}
BAIL:
_cairo_scaled_font_thaw_cache (scaled_font);
return _cairo_scaled_font_set_error (scaled_font, status);
}
/**
* _cairo_scaled_glyph_set_metrics:
* @scaled_glyph: a #cairo_scaled_glyph_t
* @scaled_font: a #cairo_scaled_font_t
* @fs_metrics: a #cairo_text_extents_t in font space
*
* _cairo_scaled_glyph_set_metrics() stores user space metrics
* for the specified glyph given font space metrics. It is
* called by the font backend when initializing a glyph with
* %CAIRO_SCALED_GLYPH_INFO_METRICS.
**/
void
_cairo_scaled_glyph_set_metrics (cairo_scaled_glyph_t *scaled_glyph,
cairo_scaled_font_t *scaled_font,
cairo_text_extents_t *fs_metrics)
{
cairo_bool_t first = TRUE;
double hm, wm;
double min_user_x = 0.0, max_user_x = 0.0, min_user_y = 0.0, max_user_y = 0.0;
double min_device_x = 0.0, max_device_x = 0.0, min_device_y = 0.0, max_device_y = 0.0;
double device_x_advance, device_y_advance;
scaled_glyph->fs_metrics = *fs_metrics;
for (hm = 0.0; hm <= 1.0; hm += 1.0)
for (wm = 0.0; wm <= 1.0; wm += 1.0) {
double x, y;
/* Transform this corner to user space */
x = fs_metrics->x_bearing + fs_metrics->width * wm;
y = fs_metrics->y_bearing + fs_metrics->height * hm;
cairo_matrix_transform_point (&scaled_font->font_matrix,
&x, &y);
if (first) {
min_user_x = max_user_x = x;
min_user_y = max_user_y = y;
} else {
if (x < min_user_x) min_user_x = x;
if (x > max_user_x) max_user_x = x;
if (y < min_user_y) min_user_y = y;
if (y > max_user_y) max_user_y = y;
}
/* Transform this corner to device space from glyph origin */
x = fs_metrics->x_bearing + fs_metrics->width * wm;
y = fs_metrics->y_bearing + fs_metrics->height * hm;
cairo_matrix_transform_distance (&scaled_font->scale,
&x, &y);
if (first) {
min_device_x = max_device_x = x;
min_device_y = max_device_y = y;
} else {
if (x < min_device_x) min_device_x = x;
if (x > max_device_x) max_device_x = x;
if (y < min_device_y) min_device_y = y;
if (y > max_device_y) max_device_y = y;
}
first = FALSE;
}
scaled_glyph->metrics.x_bearing = min_user_x;
scaled_glyph->metrics.y_bearing = min_user_y;
scaled_glyph->metrics.width = max_user_x - min_user_x;
scaled_glyph->metrics.height = max_user_y - min_user_y;
scaled_glyph->metrics.x_advance = fs_metrics->x_advance;
scaled_glyph->metrics.y_advance = fs_metrics->y_advance;
cairo_matrix_transform_distance (&scaled_font->font_matrix,
&scaled_glyph->metrics.x_advance,
&scaled_glyph->metrics.y_advance);
device_x_advance = fs_metrics->x_advance;
device_y_advance = fs_metrics->y_advance;
cairo_matrix_transform_distance (&scaled_font->scale,
&device_x_advance,
&device_y_advance);
scaled_glyph->bbox.p1.x = _cairo_fixed_from_double (min_device_x);
scaled_glyph->bbox.p1.y = _cairo_fixed_from_double (min_device_y);
scaled_glyph->bbox.p2.x = _cairo_fixed_from_double (max_device_x);
scaled_glyph->bbox.p2.y = _cairo_fixed_from_double (max_device_y);
scaled_glyph->x_advance = _cairo_lround (device_x_advance);
scaled_glyph->y_advance = _cairo_lround (device_y_advance);
scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_METRICS;
}
void
_cairo_scaled_glyph_set_surface (cairo_scaled_glyph_t *scaled_glyph,
cairo_scaled_font_t *scaled_font,
cairo_image_surface_t *surface)
{
if (scaled_glyph->surface != NULL)
cairo_surface_destroy (&scaled_glyph->surface->base);
/* sanity check the backend glyph contents */
_cairo_debug_check_image_surface_is_defined (&surface->base);
scaled_glyph->surface = surface;
if (surface != NULL)
scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_SURFACE;
else
scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_SURFACE;
}
void
_cairo_scaled_glyph_set_path (cairo_scaled_glyph_t *scaled_glyph,
cairo_scaled_font_t *scaled_font,
cairo_path_fixed_t *path)
{
if (scaled_glyph->path != NULL)
_cairo_path_fixed_destroy (scaled_glyph->path);
scaled_glyph->path = path;
if (path != NULL)
scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_PATH;
else
scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_PATH;
}
void
_cairo_scaled_glyph_set_recording_surface (cairo_scaled_glyph_t *scaled_glyph,
cairo_scaled_font_t *scaled_font,
cairo_surface_t *recording_surface)
{
if (scaled_glyph->recording_surface != NULL) {
cairo_surface_finish (scaled_glyph->recording_surface);
cairo_surface_destroy (scaled_glyph->recording_surface);
}
scaled_glyph->recording_surface = recording_surface;
if (recording_surface != NULL)
scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE;
else
scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE;
}
void
_cairo_scaled_glyph_set_color_surface (cairo_scaled_glyph_t *scaled_glyph,
cairo_scaled_font_t *scaled_font,
cairo_image_surface_t *surface)
{
if (scaled_glyph->color_surface != NULL)
cairo_surface_destroy (&scaled_glyph->color_surface->base);
/* sanity check the backend glyph contents */
_cairo_debug_check_image_surface_is_defined (&surface->base);
scaled_glyph->color_surface = surface;
if (surface != NULL)
scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE;
else
scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE;
}
static cairo_bool_t
_cairo_scaled_glyph_page_can_remove (const void *closure)
{
const cairo_scaled_glyph_page_t *page = closure;
const cairo_scaled_font_t *scaled_font;
scaled_font = page->scaled_font;
return scaled_font->cache_frozen == 0;
}
static cairo_status_t
_cairo_scaled_font_allocate_glyph (cairo_scaled_font_t *scaled_font,
cairo_scaled_glyph_t **scaled_glyph)
{
cairo_scaled_glyph_page_t *page;
cairo_status_t status;
assert (scaled_font->cache_frozen);
/* only the first page in the list may contain available slots */
if (! cairo_list_is_empty (&scaled_font->glyph_pages)) {
page = cairo_list_last_entry (&scaled_font->glyph_pages,
cairo_scaled_glyph_page_t,
link);
if (page->num_glyphs < CAIRO_SCALED_GLYPH_PAGE_SIZE) {
*scaled_glyph = &page->glyphs[page->num_glyphs++];
return CAIRO_STATUS_SUCCESS;
}
}
page = _cairo_malloc (sizeof (cairo_scaled_glyph_page_t));
if (unlikely (page == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
page->cache_entry.hash = (unsigned long) scaled_font;
page->scaled_font = scaled_font;
page->cache_entry.size = 1; /* XXX occupancy weighting? */
page->num_glyphs = 0;
CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
if (scaled_font->global_cache_frozen == FALSE) {
if (unlikely (cairo_scaled_glyph_page_cache.hash_table == NULL)) {
status = _cairo_cache_init (&cairo_scaled_glyph_page_cache,
NULL,
_cairo_scaled_glyph_page_can_remove,
_cairo_scaled_glyph_page_pluck,
MAX_GLYPH_PAGES_CACHED);
if (unlikely (status)) {
CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
free (page);
return status;
}
}
_cairo_cache_freeze (&cairo_scaled_glyph_page_cache);
scaled_font->global_cache_frozen = TRUE;
}
status = _cairo_cache_insert (&cairo_scaled_glyph_page_cache,
&page->cache_entry);
CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
if (unlikely (status)) {
free (page);
return status;
}
cairo_list_add_tail (&page->link, &scaled_font->glyph_pages);
*scaled_glyph = &page->glyphs[page->num_glyphs++];
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_scaled_font_free_last_glyph (cairo_scaled_font_t *scaled_font,
cairo_scaled_glyph_t *scaled_glyph)
{
cairo_scaled_glyph_page_t *page;
assert (scaled_font->cache_frozen);
assert (! cairo_list_is_empty (&scaled_font->glyph_pages));
page = cairo_list_last_entry (&scaled_font->glyph_pages,
cairo_scaled_glyph_page_t,
link);
assert (scaled_glyph == &page->glyphs[page->num_glyphs-1]);
_cairo_scaled_glyph_fini (scaled_font, scaled_glyph);
if (--page->num_glyphs == 0) {
_cairo_scaled_font_thaw_cache (scaled_font);
CAIRO_MUTEX_LOCK (scaled_font->mutex);
CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
/* Temporarily disconnect callback to avoid recursive locking */
cairo_scaled_glyph_page_cache.entry_destroy = NULL;
_cairo_cache_remove (&cairo_scaled_glyph_page_cache,
&page->cache_entry);
_cairo_scaled_glyph_page_destroy (scaled_font, page);
cairo_scaled_glyph_page_cache.entry_destroy = _cairo_scaled_glyph_page_pluck;
CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
_cairo_scaled_font_freeze_cache (scaled_font);
}
}
/**
* _cairo_scaled_glyph_lookup:
* @scaled_font: a #cairo_scaled_font_t
* @index: the glyph to create
* @info: a #cairo_scaled_glyph_info_t marking which portions of
* the glyph should be filled in.
* @scaled_glyph_ret: a #cairo_scaled_glyph_t where the glyph
* is returned.
*
* If the desired info is not available, (for example, when trying to
* get INFO_PATH with a bitmapped font), this function will return
* %CAIRO_INT_STATUS_UNSUPPORTED.
*
* Note: This function must be called with the scaled font frozen, and it must
* remain frozen for as long as the @scaled_glyph_ret is alive. (If the scaled
* font was not frozen, then there is no guarantee that the glyph would not be
* evicted before you tried to access it.) See
* _cairo_scaled_font_freeze_cache() and _cairo_scaled_font_thaw_cache().
*
* Returns: a glyph with the requested portions filled in. Glyph
* lookup is cached and glyph will be automatically freed along
* with the scaled_font so no explicit free is required.
* @info can be one or more of:
* %CAIRO_SCALED_GLYPH_INFO_METRICS - glyph metrics and bounding box
* %CAIRO_SCALED_GLYPH_INFO_SURFACE - surface holding glyph image
* %CAIRO_SCALED_GLYPH_INFO_PATH - path holding glyph outline in device space
**/
cairo_int_status_t
_cairo_scaled_glyph_lookup (cairo_scaled_font_t *scaled_font,
unsigned long index,
cairo_scaled_glyph_info_t info,
cairo_scaled_glyph_t **scaled_glyph_ret)
{
cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
cairo_scaled_glyph_t *scaled_glyph;
cairo_scaled_glyph_info_t need_info;
*scaled_glyph_ret = NULL;
if (unlikely (scaled_font->status))
return scaled_font->status;
assert (CAIRO_MUTEX_IS_LOCKED(scaled_font->mutex));
assert (scaled_font->cache_frozen);
if (CAIRO_INJECT_FAULT ())
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
/*
* Check cache for glyph
*/
scaled_glyph = _cairo_hash_table_lookup (scaled_font->glyphs,
(cairo_hash_entry_t *) &index);
if (scaled_glyph == NULL) {
status = _cairo_scaled_font_allocate_glyph (scaled_font, &scaled_glyph);
if (unlikely (status))
goto err;
memset (scaled_glyph, 0, sizeof (cairo_scaled_glyph_t));
_cairo_scaled_glyph_set_index (scaled_glyph, index);
cairo_list_init (&scaled_glyph->dev_privates);
/* ask backend to initialize metrics and shape fields */
status =
scaled_font->backend->scaled_glyph_init (scaled_font,
scaled_glyph,
info | CAIRO_SCALED_GLYPH_INFO_METRICS);
if (unlikely (status)) {
_cairo_scaled_font_free_last_glyph (scaled_font, scaled_glyph);
goto err;
}
status = _cairo_hash_table_insert (scaled_font->glyphs,
&scaled_glyph->hash_entry);
if (unlikely (status)) {
_cairo_scaled_font_free_last_glyph (scaled_font, scaled_glyph);
goto err;
}
}
/*
* Check and see if the glyph, as provided,
* already has the requested data and amend it if not
*/
need_info = info & ~scaled_glyph->has_info;
if (need_info) {
status = scaled_font->backend->scaled_glyph_init (scaled_font,
scaled_glyph,
need_info);
if (unlikely (status))
goto err;
/* Don't trust the scaled_glyph_init() return value, the font
* backend may not even know about some of the info. For example,
* no backend other than the user-fonts knows about recording-surface
* glyph info. */
if (info & ~scaled_glyph->has_info)
return CAIRO_INT_STATUS_UNSUPPORTED;
}
*scaled_glyph_ret = scaled_glyph;
return CAIRO_STATUS_SUCCESS;
err:
/* It's not an error for the backend to not support the info we want. */
if (status != CAIRO_INT_STATUS_UNSUPPORTED)
status = _cairo_scaled_font_set_error (scaled_font, status);
return status;
}
double
_cairo_scaled_font_get_max_scale (cairo_scaled_font_t *scaled_font)
{
return scaled_font->max_scale;
}
/**
* cairo_scaled_font_get_font_face:
* @scaled_font: a #cairo_scaled_font_t
*
* Gets the font face that this scaled font uses. This might be the
* font face passed to cairo_scaled_font_create(), but this does not
* hold true for all possible cases.
*
* Return value: The #cairo_font_face_t with which @scaled_font was
* created. This object is owned by cairo. To keep a reference to it,
* you must call cairo_scaled_font_reference().
*
* Since: 1.2
**/
cairo_font_face_t *
cairo_scaled_font_get_font_face (cairo_scaled_font_t *scaled_font)
{
if (scaled_font->status)
return (cairo_font_face_t*) &_cairo_font_face_nil;
if (scaled_font->original_font_face != NULL)
return scaled_font->original_font_face;
return scaled_font->font_face;
}
slim_hidden_def (cairo_scaled_font_get_font_face);
/**
* cairo_scaled_font_get_font_matrix:
* @scaled_font: a #cairo_scaled_font_t
* @font_matrix: return value for the matrix
*
* Stores the font matrix with which @scaled_font was created into
* @matrix.
*
* Since: 1.2
**/
void
cairo_scaled_font_get_font_matrix (cairo_scaled_font_t *scaled_font,
cairo_matrix_t *font_matrix)
{
if (scaled_font->status) {
cairo_matrix_init_identity (font_matrix);
return;
}
*font_matrix = scaled_font->font_matrix;
}
slim_hidden_def (cairo_scaled_font_get_font_matrix);
/**
* cairo_scaled_font_get_ctm:
* @scaled_font: a #cairo_scaled_font_t
* @ctm: return value for the CTM
*
* Stores the CTM with which @scaled_font was created into @ctm.
* Note that the translation offsets (x0, y0) of the CTM are ignored
* by cairo_scaled_font_create(). So, the matrix this
* function returns always has 0,0 as x0,y0.
*
* Since: 1.2
**/
void
cairo_scaled_font_get_ctm (cairo_scaled_font_t *scaled_font,
cairo_matrix_t *ctm)
{
if (scaled_font->status) {
cairo_matrix_init_identity (ctm);
return;
}
*ctm = scaled_font->ctm;
}
slim_hidden_def (cairo_scaled_font_get_ctm);
/**
* cairo_scaled_font_get_scale_matrix:
* @scaled_font: a #cairo_scaled_font_t
* @scale_matrix: return value for the matrix
*
* Stores the scale matrix of @scaled_font into @matrix.
* The scale matrix is product of the font matrix and the ctm
* associated with the scaled font, and hence is the matrix mapping from
* font space to device space.
*
* Since: 1.8
**/
void
cairo_scaled_font_get_scale_matrix (cairo_scaled_font_t *scaled_font,
cairo_matrix_t *scale_matrix)
{
if (scaled_font->status) {
cairo_matrix_init_identity (scale_matrix);
return;
}
*scale_matrix = scaled_font->scale;
}
/**
* cairo_scaled_font_get_font_options:
* @scaled_font: a #cairo_scaled_font_t
* @options: return value for the font options
*
* Stores the font options with which @scaled_font was created into
* @options.
*
* Since: 1.2
**/
void
cairo_scaled_font_get_font_options (cairo_scaled_font_t *scaled_font,
cairo_font_options_t *options)
{
if (cairo_font_options_status (options))
return;
if (scaled_font->status) {
_cairo_font_options_init_default (options);
return;
}
_cairo_font_options_init_copy (options, &scaled_font->options);
}
slim_hidden_def (cairo_scaled_font_get_font_options);
cairo_bool_t
_cairo_scaled_font_has_color_glyphs (cairo_scaled_font_t *scaled_font)
{
if (scaled_font->backend != NULL && scaled_font->backend->has_color_glyphs != NULL)
return scaled_font->backend->has_color_glyphs (scaled_font);
else
return FALSE;
}
| 0 |
D://workCode//uploadProject\awtk\3rd\cairo | D://workCode//uploadProject\awtk\3rd\cairo\cairo\cairo-shape-mask-compositor.c | /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2012 Intel Corporation
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairoint.h"
#include "cairo-compositor-private.h"
#include "cairo-clip-private.h"
#include "cairo-pattern-private.h"
#include "cairo-surface-private.h"
#include "cairo-surface-offset-private.h"
static cairo_int_status_t
_cairo_shape_mask_compositor_stroke (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
const cairo_path_fixed_t *path,
const cairo_stroke_style_t *style,
const cairo_matrix_t *ctm,
const cairo_matrix_t *ctm_inverse,
double tolerance,
cairo_antialias_t antialias)
{
cairo_surface_t *mask;
cairo_surface_pattern_t pattern;
cairo_int_status_t status;
cairo_clip_t *clip;
if (! extents->is_bounded)
return CAIRO_INT_STATUS_UNSUPPORTED;
TRACE ((stderr, "%s\n", __FUNCTION__));
mask = _cairo_surface_create_scratch (extents->surface,
CAIRO_CONTENT_ALPHA,
extents->bounded.width,
extents->bounded.height,
NULL);
if (unlikely (mask->status))
return mask->status;
clip = extents->clip;
if (! _cairo_clip_is_region (clip))
clip = _cairo_clip_copy_region (clip);
if (! mask->is_clear) {
status = _cairo_surface_offset_paint (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_CLEAR,
&_cairo_pattern_clear.base,
clip);
if (unlikely (status))
goto error;
}
status = _cairo_surface_offset_stroke (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_ADD,
&_cairo_pattern_white.base,
path, style, ctm, ctm_inverse,
tolerance, antialias,
clip);
if (unlikely (status))
goto error;
if (clip != extents->clip) {
status = _cairo_clip_combine_with_surface (extents->clip, mask,
extents->bounded.x,
extents->bounded.y);
if (unlikely (status))
goto error;
}
_cairo_pattern_init_for_surface (&pattern, mask);
cairo_matrix_init_translate (&pattern.base.matrix,
-extents->bounded.x,
-extents->bounded.y);
pattern.base.filter = CAIRO_FILTER_NEAREST;
pattern.base.extend = CAIRO_EXTEND_NONE;
if (extents->op == CAIRO_OPERATOR_SOURCE) {
status = _cairo_surface_mask (extents->surface,
CAIRO_OPERATOR_DEST_OUT,
&_cairo_pattern_white.base,
&pattern.base,
clip);
if ((status == CAIRO_INT_STATUS_SUCCESS)) {
status = _cairo_surface_mask (extents->surface,
CAIRO_OPERATOR_ADD,
&extents->source_pattern.base,
&pattern.base,
clip);
}
} else {
status = _cairo_surface_mask (extents->surface,
extents->op,
&extents->source_pattern.base,
&pattern.base,
clip);
}
_cairo_pattern_fini (&pattern.base);
error:
cairo_surface_destroy (mask);
if (clip != extents->clip)
_cairo_clip_destroy (clip);
return status;
}
static cairo_int_status_t
_cairo_shape_mask_compositor_fill (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
const cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias)
{
cairo_surface_t *mask;
cairo_surface_pattern_t pattern;
cairo_int_status_t status;
cairo_clip_t *clip;
TRACE ((stderr, "%s\n", __FUNCTION__));
if (! extents->is_bounded)
return CAIRO_INT_STATUS_UNSUPPORTED;
mask = _cairo_surface_create_scratch (extents->surface,
CAIRO_CONTENT_ALPHA,
extents->bounded.width,
extents->bounded.height,
NULL);
if (unlikely (mask->status))
return mask->status;
clip = extents->clip;
if (! _cairo_clip_is_region (clip))
clip = _cairo_clip_copy_region (clip);
if (! mask->is_clear) {
status = _cairo_surface_offset_paint (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_CLEAR,
&_cairo_pattern_clear.base,
clip);
if (unlikely (status))
goto error;
}
status = _cairo_surface_offset_fill (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_ADD,
&_cairo_pattern_white.base,
path, fill_rule, tolerance, antialias,
clip);
if (unlikely (status))
goto error;
if (clip != extents->clip) {
status = _cairo_clip_combine_with_surface (extents->clip, mask,
extents->bounded.x,
extents->bounded.y);
if (unlikely (status))
goto error;
}
_cairo_pattern_init_for_surface (&pattern, mask);
cairo_matrix_init_translate (&pattern.base.matrix,
-extents->bounded.x,
-extents->bounded.y);
pattern.base.filter = CAIRO_FILTER_NEAREST;
pattern.base.extend = CAIRO_EXTEND_NONE;
if (extents->op == CAIRO_OPERATOR_SOURCE) {
status = _cairo_surface_mask (extents->surface,
CAIRO_OPERATOR_DEST_OUT,
&_cairo_pattern_white.base,
&pattern.base,
clip);
if ((status == CAIRO_INT_STATUS_SUCCESS)) {
status = _cairo_surface_mask (extents->surface,
CAIRO_OPERATOR_ADD,
&extents->source_pattern.base,
&pattern.base,
clip);
}
} else {
status = _cairo_surface_mask (extents->surface,
extents->op,
&extents->source_pattern.base,
&pattern.base,
clip);
}
_cairo_pattern_fini (&pattern.base);
error:
if (clip != extents->clip)
_cairo_clip_destroy (clip);
cairo_surface_destroy (mask);
return status;
}
static cairo_int_status_t
_cairo_shape_mask_compositor_glyphs (const cairo_compositor_t *_compositor,
cairo_composite_rectangles_t *extents,
cairo_scaled_font_t *scaled_font,
cairo_glyph_t *glyphs,
int num_glyphs,
cairo_bool_t overlap)
{
cairo_surface_t *mask;
cairo_surface_pattern_t pattern;
cairo_int_status_t status;
cairo_clip_t *clip;
if (! extents->is_bounded)
return CAIRO_INT_STATUS_UNSUPPORTED;
TRACE ((stderr, "%s\n", __FUNCTION__));
mask = _cairo_surface_create_scratch (extents->surface,
CAIRO_CONTENT_ALPHA,
extents->bounded.width,
extents->bounded.height,
NULL);
if (unlikely (mask->status))
return mask->status;
clip = extents->clip;
if (! _cairo_clip_is_region (clip))
clip = _cairo_clip_copy_region (clip);
if (! mask->is_clear) {
status = _cairo_surface_offset_paint (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_CLEAR,
&_cairo_pattern_clear.base,
clip);
if (unlikely (status))
goto error;
}
status = _cairo_surface_offset_glyphs (mask,
extents->bounded.x,
extents->bounded.y,
CAIRO_OPERATOR_ADD,
&_cairo_pattern_white.base,
scaled_font, glyphs, num_glyphs,
clip);
if (unlikely (status))
goto error;
if (clip != extents->clip) {
status = _cairo_clip_combine_with_surface (extents->clip, mask,
extents->bounded.x,
extents->bounded.y);
if (unlikely (status))
goto error;
}
_cairo_pattern_init_for_surface (&pattern, mask);
cairo_matrix_init_translate (&pattern.base.matrix,
-extents->bounded.x,
-extents->bounded.y);
pattern.base.filter = CAIRO_FILTER_NEAREST;
pattern.base.extend = CAIRO_EXTEND_NONE;
if (extents->op == CAIRO_OPERATOR_SOURCE) {
status = _cairo_surface_mask (extents->surface,
CAIRO_OPERATOR_DEST_OUT,
&_cairo_pattern_white.base,
&pattern.base,
clip);
if ((status == CAIRO_INT_STATUS_SUCCESS)) {
status = _cairo_surface_mask (extents->surface,
CAIRO_OPERATOR_ADD,
&extents->source_pattern.base,
&pattern.base,
clip);
}
} else {
status = _cairo_surface_mask (extents->surface,
extents->op,
&extents->source_pattern.base,
&pattern.base,
clip);
}
_cairo_pattern_fini (&pattern.base);
error:
if (clip != extents->clip)
_cairo_clip_destroy (clip);
cairo_surface_destroy (mask);
return status;
}
void
_cairo_shape_mask_compositor_init (cairo_compositor_t *compositor,
const cairo_compositor_t *delegate)
{
compositor->delegate = delegate;
compositor->paint = NULL;
compositor->mask = NULL;
compositor->fill = _cairo_shape_mask_compositor_fill;
compositor->stroke = _cairo_shape_mask_compositor_stroke;
compositor->glyphs = _cairo_shape_mask_compositor_glyphs;
}
| 0 |