File: utils.cpp

package info (click to toggle)
qtcurve 1.9-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,460 kB
  • sloc: cpp: 70,269; perl: 123; sh: 122; xml: 46; makefile: 15
file content (217 lines) | stat: -rw-r--r-- 7,232 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*****************************************************************************
 *   Copyright 2003 - 2010 Craig Drummond <craig.p.drummond@gmail.com>       *
 *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
 *                                                                           *
 *   This program is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU Lesser General Public License as          *
 *   published by the Free Software Foundation; either version 2.1 of the    *
 *   License, or (at your option) version 3, or any later version accepted   *
 *   by the membership of KDE e.V. (or its successor approved by the         *
 *   membership of KDE e.V.), which shall act as a proxy defined in          *
 *   Section 6 of version 3 of the license.                                  *
 *                                                                           *
 *   This program 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       *
 *   Lesser General Public License for more details.                         *
 *                                                                           *
 *   You should have received a copy of the GNU Lesser General Public        *
 *   License along with this library. If not,                                *
 *   see <http://www.gnu.org/licenses/>.                                     *
 *****************************************************************************/

#include "utils_p.h"
#include <qtcurve-utils/number.h>

namespace QtCurve {
namespace Rect {

QTC_EXPORT void
union_(const QtcRect *src1, const QtcRect *src2, QtcRect *dest)
{
    // Copied from gdk_rectangle_union
    int dest_x = qtcMin(src1->x, src2->x);
    int dest_y = qtcMin(src1->y, src2->y);
    dest->width = qtcMax(src1->x + src1->width,
                         src2->x + src2->width) - dest_x;
    dest->height = qtcMax(src1->y + src1->height,
                          src2->y + src2->height) - dest_y;
    dest->x = dest_x;
    dest->y = dest_y;
}

QTC_EXPORT bool
intersect(const QtcRect *src1, const QtcRect *src2, QtcRect *dest)
{
    // Copied from gdk_rectangle_intersect
    int dest_x = qtcMax(src1->x, src2->x);
    int dest_y = qtcMax(src1->y, src2->y);
    int dest_x2 = qtcMin(src1->x + src1->width, src2->x + src2->width);
    int dest_y2 = qtcMin(src1->y + src1->height, src2->y + src2->height);

    if (dest_x2 > dest_x && dest_y2 > dest_y) {
        if (dest) {
            dest->x = dest_x;
            dest->y = dest_y;
            dest->width = dest_x2 - dest_x;
            dest->height = dest_y2 - dest_y;
        }
        return true;
    } else if (dest) {
        dest->width = 0;
        dest->height = 0;
    }
    return false;
}

}

namespace Cairo {

QTC_EXPORT void
pathPoints(cairo_t *cr, const GdkPoint *pts, int count)
{
    // The (0.5, 0.5) offset here is for moving the path to the center of the
    // pixel.
    cairo_move_to(cr, pts[0].x + 0.5, pts[0].y + 0.5);
    for (int i = 1;i < count;i++) {
        cairo_line_to(cr, pts[i].x + 0.5, pts[i].y + 0.5);
    }
}

static void
pathRegion(cairo_t *cr, const cairo_region_t *region)
{
    // Copied from gdk_cairo_region.
    int n_boxes = cairo_region_num_rectangles(region);
    QtcRect box;
    for (int i = 0;i < n_boxes;i++) {
        cairo_region_get_rectangle(region, i, &box);
        cairo_rectangle(cr, box.x, box.y, box.width, box.height);
    }
}

QTC_EXPORT void
clipRegion(cairo_t *cr, const cairo_region_t *region)
{
    cairo_new_path(cr);
    if (qtcLikely(region)) {
        pathRegion(cr, region);
        cairo_clip(cr);
    }
}

QTC_EXPORT void
clipRect(cairo_t *cr, const QtcRect *_rect)
{
    cairo_new_path(cr);
    if (qtcLikely(_rect)) {
        cairo_rectangle(cr, _rect->x, _rect->y, _rect->width, _rect->height);
        cairo_clip(cr);
    }
}

QTC_EXPORT void
setColor(cairo_t *cr, const GdkColor *col, double a)
{
    cairo_set_source_rgba(cr, col->red / 65535.0, col->green / 65535.0,
                          col->blue / 65535.0, a);
}

QTC_EXPORT void
patternAddColorStop(cairo_pattern_t *pt, double offset,
                    const GdkColor *col, double a)
{
    cairo_pattern_add_color_stop_rgba(pt, offset, col->red / 65535.0,
                                      col->green / 65535.0,
                                      col->blue / 65535.0, a);
}

QTC_EXPORT void
pathTopLeft(cairo_t *cr, double xd, double yd, double width,
            double height, double radius, ECornerBits round)
{
    bool rounded = radius > 0.0;

    if (rounded && round & CORNER_BL) {
        cairo_arc(cr, xd + radius, yd + height - radius, radius,
                  M_PI * 0.75, M_PI);
    } else {
        cairo_move_to(cr, xd, yd + height);
    }
    if (rounded && round & CORNER_TL) {
        cairo_arc(cr, xd + radius, yd + radius, radius, M_PI, M_PI * 1.5);
    } else {
        cairo_line_to(cr, xd, yd);
    }
    if (rounded && round & CORNER_TR) {
        cairo_arc(cr, xd + width - radius, yd + radius,
                  radius, M_PI * 1.5, M_PI * 1.75);
    } else {
        cairo_line_to(cr, xd + width, yd);
    }
}

QTC_EXPORT void
pathBottomRight(cairo_t *cr, double xd, double yd, double width,
                double height, double radius, ECornerBits round)
{
    bool rounded = radius > 0.0;

    if (rounded && round & CORNER_TR) {
        cairo_arc(cr, xd + width - radius, yd + radius, radius, M_PI * 1.75, 0);
    } else {
        cairo_move_to(cr, xd + width, yd);
    }
    if (rounded && round & CORNER_BR) {
        cairo_arc(cr, xd + width - radius, yd + height - radius,
                  radius, 0, M_PI * 0.5);
    } else {
        cairo_line_to(cr, xd + width, yd + height);
    }
    if (rounded && round & CORNER_BL) {
        cairo_arc(cr, xd + radius, yd + height - radius,
                  radius, M_PI * 0.5, M_PI * 0.75);
    } else {
        cairo_line_to(cr, xd, yd + height);
    }
}

QTC_EXPORT void
pathWhole(cairo_t *cr, double xd, double yd, double width,
          double height, double radius, ECornerBits round)
{
    bool rounded = radius > 0.0;

    if (rounded && round & CORNER_TL) {
        cairo_move_to(cr, xd + radius, yd);
    } else {
        cairo_move_to(cr, xd, yd);
    }
    if (rounded && round & CORNER_TR) {
        cairo_arc(cr, xd + width - radius, yd + radius, radius,
                  M_PI * 1.5, M_PI * 2);
    } else {
        cairo_line_to(cr, xd + width, yd);
    }
    if (rounded && round & CORNER_BR) {
        cairo_arc(cr, xd + width - radius, yd + height - radius,
                  radius, 0, M_PI * 0.5);
    } else {
        cairo_line_to(cr, xd + width, yd + height);
    }
    if (rounded && round & CORNER_BL) {
        cairo_arc(cr, xd + radius, yd + height - radius,
                  radius, M_PI * 0.5, M_PI);
    } else {
        cairo_line_to(cr, xd, yd + height);
    }
    if (rounded && round & CORNER_TL) {
        cairo_arc(cr, xd + radius, yd + radius, radius, M_PI, M_PI * 1.5);
    } else {
        cairo_line_to(cr, xd, yd);
    }
}

}
}