aboutsummaryrefslogtreecommitdiffstats
path: root/libgnomecanvas/gnome-canvas-util.c
blob: d05171b4e4b888c4986a123f94310f126b8a7c52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
 * All rights reserved.
 *
 * This file is part of the Gnome Library.
 *
 * The Gnome Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Gnome Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/*
  @NOTATION@
 */
/* Miscellaneous utility functions for the GnomeCanvas widget
 *
 * GnomeCanvas is basically a port of the Tk toolkit's most excellent canvas
 * widget.  Tk is copyrighted by the Regents of the University of California,
 * Sun Microsystems, and other parties.
 *
 *
 * Author: Federico Mena <federico@nuclecu.unam.mx>
 */

#include <config.h>

/* needed for M_PI_2 under 'gcc -ansi -predantic' on GNU/Linux */
#ifndef _BSD_SOURCE
#  define _BSD_SOURCE 1
#endif
#include <sys/types.h>

#include <glib.h>
#include <math.h>
#include "gnome-canvas.h"
#include "gnome-canvas-util.h"

/* Here are some helper functions for aa rendering: */

/**
 * gnome_canvas_item_reset_bounds:
 * @item: A canvas item
 *
 * Resets the bounding box of a canvas item to an empty rectangle.
 **/
void
gnome_canvas_item_reset_bounds (GnomeCanvasItem *item)
{
    item->x1 = 0.0;
    item->y1 = 0.0;
    item->x2 = 0.0;
    item->y2 = 0.0;
}

/**
 * gnome_canvas_update_bbox:
 * @item: the canvas item needing update
 * @x1: Left coordinate of the new bounding box
 * @y1: Top coordinate of the new bounding box
 * @x2: Right coordinate of the new bounding box
 * @y2: Bottom coordinate of the new bounding box
 *
 * Sets the bbox to the new value, requesting full repaint.
 **/
void
gnome_canvas_update_bbox (GnomeCanvasItem *item, gint x1, gint y1, gint x2, gint y2)
{
    gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
    item->x1 = x1;
    item->y1 = y1;
    item->x2 = x2;
    item->y2 = y2;
    gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
}

/**
 * gnome_canvas_cairo_create_scratch:
 *
 * Create a scratch #cairo_t. This is useful for measuring purposes or
 * calling functions like cairo_in_fill().
 *
 * Returns: A new cairo_t. Destroy with cairo_destroy() after use.
 **/
cairo_t *
gnome_canvas_cairo_create_scratch (void)
{
        cairo_surface_t *surface;
        cairo_t *cr;

        surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
        cr = cairo_create (surface);
        cairo_surface_destroy (surface);

        return cr;
}

/**
 * gnome_canvas_matrix_transform_rect:
 * @matrix: a cairo matrix
 * @x1: x coordinate of top left position of rectangle (in-out)
 * @y1: y coordinate of top left position of rectangle (in-out)
 * @x2: x coordinate of bottom right position of rectangle (in-out)
 * @y2: y coordinate of bottom right position of rectangle (in-out)
 *
 * Computes the smallest rectangle containing the whole area of the given
 * rectangle after applying the transformation given in @matrix.
 **/
void
gnome_canvas_matrix_transform_rect (const cairo_matrix_t *matrix,
                                    double *x1, double *y1, double *x2, double *y2)
{
        double maxx, maxy, minx, miny;
        double tmpx, tmpy;

        tmpx = *x1;
        tmpy = *y1;
        cairo_matrix_transform_point (matrix, &tmpx, &tmpy);
        minx = maxx = tmpx;
        miny = maxy = tmpy;

        tmpx = *x2;
        tmpy = *y1;
        cairo_matrix_transform_point (matrix, &tmpx, &tmpy);
        minx = MIN (minx, tmpx);
        maxx = MAX (maxx, tmpx);
        miny = MIN (miny, tmpy);
        maxy = MAX (maxy, tmpy);

        tmpx = *x2;
        tmpy = *y2;
        cairo_matrix_transform_point (matrix, &tmpx, &tmpy);
        minx = MIN (minx, tmpx);
        maxx = MAX (maxx, tmpx);
        miny = MIN (miny, tmpy);
        maxy = MAX (maxy, tmpy);

        tmpx = *x1;
        tmpy = *y2;
        cairo_matrix_transform_point (matrix, &tmpx, &tmpy);
        minx = MIN (minx, tmpx);
        maxx = MAX (maxx, tmpx);
        miny = MIN (miny, tmpy);
        maxy = MAX (maxy, tmpy);

        *x1 = minx;
        *x2 = maxx;
        *y1 = miny;
        *y2 = maxy;
}