File: draw.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 (336 lines) | stat: -rw-r--r-- 11,642 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*****************************************************************************
 *   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 "draw.h"
#include "utils_p.h"
#include <pango/pangocairo.h>

namespace QtCurve {
namespace Cairo {

QTC_EXPORT void
hLine(cairo_t *cr, int x, int y, int w, const GdkColor *col, double a)
{
    Saver saver(cr);
    cairo_new_path(cr);
    setColor(cr, col, a);
    cairo_move_to(cr, x, y + 0.5);
    cairo_line_to(cr, x + w, y + 0.5);
    cairo_stroke(cr);
}

QTC_EXPORT void
vLine(cairo_t *cr, int x, int y, int h, const GdkColor *col, double a)
{
    Saver saver(cr);
    cairo_new_path(cr);
    setColor(cr, col, a);
    cairo_move_to(cr, x + 0.5, y);
    cairo_line_to(cr, x + 0.5, y + h);
    cairo_stroke(cr);
}

QTC_EXPORT void
polygon(cairo_t *cr, const GdkColor *col, const QtcRect *area,
        const GdkPoint *points, int npoints, bool fill)
{
    Saver saver(cr);
    cairo_set_line_width(cr, 1);
    clipRect(cr, area);
    setColor(cr, col);
    cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
    pathPoints(cr, points, npoints);
    cairo_close_path(cr);
    cairo_stroke_preserve(cr);
    if (fill) {
        cairo_fill(cr);
    }
}

QTC_EXPORT void
rect(cairo_t *cr, const QtcRect *area, int x, int y, int width, int height,
     const GdkColor *col, double alpha)
{
    Saver saver(cr);
    clipRect(cr, area);
    cairo_rectangle(cr, x, y, width, height);
    setColor(cr, col, alpha);
    cairo_fill(cr);
}

QTC_EXPORT void
fadedLine(cairo_t *cr, int x, int y, int width, int height,
          const QtcRect *area, const QtcRect *gap, bool fadeStart,
          bool fadeEnd, double fadeSize, bool horiz,
          const GdkColor *col, double alpha)
{
    double rx = x + 0.5;
    double ry = y + 0.5;
    cairo_pattern_t *pt =
        cairo_pattern_create_linear(rx, ry, horiz ? rx + width - 1 : rx + 1,
                                    horiz ? ry + 1 : ry + height - 1);

    Saver saver(cr);
    if (gap) {
        QtcRect r = {x, y, width, height};
        cairo_region_t *region =
            cairo_region_create_rectangle(area ? area : &r);
        cairo_region_xor_rectangle(region, gap);
        clipRegion(cr, region);
        cairo_region_destroy(region);
    } else {
        clipRect(cr, area);
    }
    patternAddColorStop(pt, 0, col, fadeStart ? 0.0 : alpha);
    patternAddColorStop(pt, fadeSize, col, alpha);
    patternAddColorStop(pt, 1 - fadeSize, col, alpha);
    patternAddColorStop(pt, 1, col, fadeEnd ? 0.0 : alpha);
    cairo_set_source(cr, pt);
    if (horiz) {
        cairo_move_to(cr, x, ry);
        cairo_line_to(cr, x + width - 1, ry);
    } else {
        cairo_move_to(cr, rx, y);
        cairo_line_to(cr, rx, y + height - 1);
    }
    cairo_stroke(cr);
    cairo_pattern_destroy(pt);
}

QTC_EXPORT void
stripes(cairo_t *cr, int x, int y, int w, int h,
        bool horizontal, int stripeWidth)
{
    int endx = horizontal ? stripeWidth : 0;
    int endy = horizontal ? 0 : stripeWidth;
    cairo_pattern_t *pat =
        cairo_pattern_create_linear(x, y, x + endx, y + endy);
    cairo_pattern_add_color_stop_rgba(pat, 0.0, 1.0, 1.0, 1.0, 0.0);
    cairo_pattern_add_color_stop_rgba(pat, 1, 1.0, 1.0, 1.0, 0.15);
    cairo_pattern_set_extend(pat, CAIRO_EXTEND_REFLECT);

    Saver saver(cr);
    cairo_set_source(cr, pat);
    cairo_rectangle(cr, x, y, w, h);
    cairo_fill(cr);

    cairo_pattern_destroy(pat);
}

QTC_EXPORT void
dot(cairo_t *cr, int x, int y, int w, int h, const GdkColor *col)
{
    double dx = x + (w - 5) / 2;
    double dy = y + (h - 5) / 2;
    cairo_pattern_t *p1 = cairo_pattern_create_linear(dx, dy, dx + 4, dy + 4);
    cairo_pattern_t *p2 = cairo_pattern_create_linear(dx + 2, dy + 2,
                                                      dx + 4, dx + 4);

    patternAddColorStop(p1, 0, col, 1);
    patternAddColorStop(p1, 1, col, 0.4);
    cairo_pattern_add_color_stop_rgba(p2, 1, 1, 1, 1, 0.9);
    cairo_pattern_add_color_stop_rgba(p2, 0, 1, 1, 1, 0.7);

    Saver saver(cr);
    cairo_new_path(cr);
    cairo_arc(cr, dx + 2.5, dy + 2.5, 2.5, 0, 2 * M_PI);
    cairo_clip(cr);
    cairo_set_source(cr, p1);
    cairo_rectangle(cr, dx, dy, 5, 5);
    cairo_fill(cr);

    cairo_new_path(cr);
    cairo_arc(cr, dx + 3, dy + 3, 2, 0, 2 * M_PI);
    cairo_clip(cr);
    cairo_set_source(cr, p2);
    cairo_rectangle(cr, dx + 1, dy + 1, 4, 4);
    cairo_fill(cr);

    cairo_pattern_destroy(p1);
    cairo_pattern_destroy(p2);
}

QTC_EXPORT void
dots(cairo_t *cr, int rx, int ry, int rwidth, int rheight, bool horiz,
     int nLines, int offset, const QtcRect *area, int startOffset,
     const GdkColor *col1, const GdkColor *col2)
{
    int space = nLines * 2 + nLines - 1;
    int x = horiz ? rx : rx + (rwidth - space) / 2;
    int y = horiz ? ry + (rheight - space) / 2 : ry;
    int numDots = ((horiz ? rwidth : rheight) - 2 * offset) / 3 + 1;

    Saver saver(cr);
    clipRect(cr, area);
    if (horiz) {
        if (startOffset && y + startOffset > 0) {
            y += startOffset;
        }
        cairo_new_path(cr);
        setColor(cr, col1);
        for (int i = 0;i < space;i += 3) {
            for (int j = 0;j < numDots;j++) {
                cairo_rectangle(cr, x + offset + 3 * j, y + i, 1, 1);
            }
        }
        cairo_fill(cr);

        cairo_new_path(cr);
        setColor(cr, col2);
        for (int i = 1;i < space;i += 3) {
            for (int j = 0;j < numDots;j++) {
                cairo_rectangle(cr, x + offset + 1 + 3 * j, y + i, 1, 1);
            }
        }
        cairo_fill(cr);
    } else {
        if (startOffset && x + startOffset > 0) {
            x += startOffset;
        }
        cairo_new_path(cr);
        setColor(cr, col1);
        for (int i = 0;i < space;i += 3) {
            for (int j = 0;j < numDots;j++) {
                cairo_rectangle(cr, x + i, y + offset + 3 * j, 1, 1);
            }
        }
        cairo_fill(cr);

        cairo_new_path(cr);
        setColor(cr, col2);
        for (int i = 1;i < space;i += 3) {
            for(int j = 0;j < numDots;j++) {
                cairo_rectangle(cr, x + i, y + offset + 1 + 3 * j, 1, 1);
            }
        }
        cairo_fill(cr);
    }
}

static void
ge_transform_for_layout(cairo_t *cr, PangoLayout *layout, int x, int y)
{
    const PangoMatrix *matrix =
        pango_context_get_matrix(pango_layout_get_context(layout));
    if (matrix) {
        cairo_matrix_t cairo_matrix;
        PangoRectangle _rect;

        cairo_matrix_init(&cairo_matrix, matrix->xx, matrix->yx,
                          matrix->xy, matrix->yy, matrix->x0, matrix->y0);
        pango_layout_get_extents(layout, nullptr, &_rect);
        pango_matrix_transform_rectangle(matrix, &_rect);
        pango_extents_to_pixels(&_rect, nullptr);

        cairo_matrix.x0 += x - _rect.x;
        cairo_matrix.y0 += y - _rect.y;

        cairo_set_matrix(cr, &cairo_matrix);
    } else {
        cairo_translate(cr, x, y);
    }
}

QTC_EXPORT void
layout(cairo_t *cr, const QtcRect *area, int x, int y, PangoLayout *_layout,
       const GdkColor *col)
{
    Saver saver(cr);
    clipRect(cr, area);
    cairo_set_line_width(cr, 1);
    setColor(cr, col);
    ge_transform_for_layout(cr, _layout, x, y);
    pango_cairo_show_layout(cr, _layout);
}

QTC_EXPORT void
arrow(cairo_t *cr, const GdkColor *col, const QtcRect *area,
      ArrowType arrow_type, int x, int y, bool small, bool fill, bool varrow)
{
    if (small) {
        switch (arrow_type) {
        case ArrowType::Up: {
            const GdkPoint a[] = {{x + 2, y}, {x, y - 2}, {x - 2, y},
                                  {x - 2, y + 1}, {x, y - 1}, {x + 2, y + 1}};
            polygon(cr, col, area, a, varrow ? 6 : 3, fill);
            break;
        }
        case ArrowType::Down: {
            const GdkPoint a[] = {{x + 2, y}, {x, y + 2}, {x - 2, y},
                                  {x - 2, y - 1}, {x, y + 1}, {x + 2, y - 1}};
            polygon(cr, col, area, a, varrow ? 6 : 3, fill);
            break;
        }
        case ArrowType::Right: {
            const GdkPoint a[] = {{x, y - 2}, {x + 2, y}, {x, y + 2},
                                  {x - 1, y + 2}, {x + 1, y}, {x - 1, y - 2}};
            polygon(cr, col, area, a, varrow ? 6 : 3, fill);
            break;
        }
        case ArrowType::Left: {
            const GdkPoint a[] = {{x, y - 2}, {x - 2, y}, {x, y + 2},
                                  {x + 1, y + 2}, {x - 1, y}, {x + 1, y - 2}};
            polygon(cr, col, area, a, varrow ? 6 : 3, fill);
            break;
        }
        default:
            break;
        }
    } else {
        /* Large arrows... */
        switch (arrow_type) {
        case ArrowType::Up: {
            const GdkPoint a[] = {{x + 3, y + 1}, {x, y - 2}, {x - 3, y + 1},
                                  {x - 3, y + 2}, {x - 2, y + 2}, {x, y},
                                  {x + 2, y + 2}, {x + 3, y + 2}};
            polygon(cr, col, area, a, varrow ? 8 : 3, fill);
            break;
        }
        case ArrowType::Down: {
            const GdkPoint a[] = {{x + 3, y - 1}, {x, y + 2}, {x - 3, y - 1},
                                  {x - 3, y - 2}, {x - 2, y - 2}, {x, y},
                                  {x + 2, y - 2}, {x + 3,y - 2}};
            polygon(cr, col, area, a, varrow ? 8 : 3, fill);
            break;
        }
        case ArrowType::Right: {
            const GdkPoint a[] = {{x - 1, y + 3}, {x + 2, y}, {x - 1, y - 3},
                                  {x - 2, y - 3}, {x - 2, y - 2}, {x, y},
                                  {x - 2, y + 2}, {x - 2, y + 3}};
            polygon(cr, col, area, a, varrow ? 8 : 3, fill);
            break;
        }
        case ArrowType::Left: {
            const GdkPoint a[] = {{x + 1, y - 3}, {x - 2, y}, {x + 1, y + 3},
                                  {x + 2, y + 3}, {x + 2, y + 2}, {x, y},
                                  {x + 2, y - 2}, {x + 2, y - 3}};
            polygon(cr, col, area, a, varrow ? 8 : 3, fill);
            break;
        }
        default:
            break;
        }
    }
}

}
}