summaryrefslogtreecommitdiffstats
path: root/src/display/sp-ctrlcurve.cpp
blob: 41572cdc6537f8a05a996657489e26674aeeb32c (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Simple bezier curve used for Mesh Gradients
 *
 * Author:
 *   Tavmjong Bah <tavmjong@free.fr>
 *
 * Copyright (C) 2011 Tavmjong Bah
 * Copyright (C) 2007 Johan Engelen
 * Copyright (C) 1999-2002 Lauris Kaplinski
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "display/sp-ctrlcurve.h"
#include "display/sp-canvas-util.h"
#include "display/cairo-utils.h"
#include "color.h"
#include "display/sp-canvas.h"

namespace {

static void sp_ctrlcurve_destroy(SPCanvasItem *object);

static void sp_ctrlcurve_update(SPCanvasItem *item, Geom::Affine const &affine, unsigned int flags);
static void sp_ctrlcurve_render(SPCanvasItem *item, SPCanvasBuf *buf);

} // namespace

G_DEFINE_TYPE(SPCtrlCurve, sp_ctrlcurve, SP_TYPE_CANVAS_ITEM);

static void
sp_ctrlcurve_class_init(SPCtrlCurveClass *klass)
{
    klass->destroy = sp_ctrlcurve_destroy;

    klass->update = sp_ctrlcurve_update;
    klass->render = sp_ctrlcurve_render;
}

static void
sp_ctrlcurve_init(SPCtrlCurve *ctrlcurve)
{
    // Points are initialized to 0,0
    ctrlcurve->rgba = 0x0000ff7f;
    ctrlcurve->item=nullptr;
    ctrlcurve->corner0 = -1;
    ctrlcurve->corner1 = -1;
}

namespace {
static void
sp_ctrlcurve_destroy(SPCanvasItem *object)
{
    g_return_if_fail (object != nullptr);
    g_return_if_fail (SP_IS_CTRLCURVE (object));

    SPCtrlCurve *ctrlcurve = SP_CTRLCURVE (object);

    ctrlcurve->item=nullptr;

    if (SP_CANVAS_ITEM_CLASS(sp_ctrlcurve_parent_class)->destroy)
        SP_CANVAS_ITEM_CLASS(sp_ctrlcurve_parent_class)->destroy(object);
}

static void
sp_ctrlcurve_render(SPCanvasItem *item, SPCanvasBuf *buf)
{
    SPCtrlCurve *cl = SP_CTRLCURVE (item);

    if (!buf->ct)
        return;

    if ( cl->p0 == cl->p1 &&
         cl->p1 == cl->p2 &&
         cl->p2 == cl->p3 )
        return;

    ink_cairo_set_source_rgba32(buf->ct, cl->rgba);
    cairo_set_line_width(buf->ct, 1);
    cairo_new_path(buf->ct);

    Geom::Point p0 = cl->p0 * cl->affine;
    Geom::Point p1 = cl->p1 * cl->affine;
    Geom::Point p2 = cl->p2 * cl->affine;
    Geom::Point p3 = cl->p3 * cl->affine;

    cairo_move_to (buf->ct, p0[Geom::X] - buf->rect.left(), p0[Geom::Y] - buf->rect.top());
    cairo_curve_to (buf->ct,
                    p1[Geom::X] - buf->rect.left(), p1[Geom::Y] - buf->rect.top(),
                    p2[Geom::X] - buf->rect.left(), p2[Geom::Y] - buf->rect.top(),
                    p3[Geom::X] - buf->rect.left(), p3[Geom::Y] - buf->rect.top() );

    cairo_stroke(buf->ct);
}

static void
sp_ctrlcurve_update(SPCanvasItem *item, Geom::Affine const &affine, unsigned int flags)
{
    SPCtrlCurve *cl = SP_CTRLCURVE (item);

    item->canvas->requestRedraw(item->x1, item->y1, item->x2, item->y2);

    if (SP_CANVAS_ITEM_CLASS(sp_ctrlcurve_parent_class)->update)
        SP_CANVAS_ITEM_CLASS(sp_ctrlcurve_parent_class)->update(item, affine, flags);

    sp_canvas_item_reset_bounds (item);

    cl->affine = affine;

    if (cl->p0 == cl->p1 && cl->p1 == cl->p2 && cl->p2 == cl->p3 ) {
        item->x1 = item->x2 = item->y1 = item->y2 = 0;
    } else {

        Geom::Point p0 = cl->p0 * affine;
        Geom::Point p1 = cl->p1 * affine;
        Geom::Point p2 = cl->p2 * affine;
        Geom::Point p3 = cl->p3 * affine;

        double min_x = p0[Geom::X];
        double min_y = p0[Geom::Y];
        double max_x = p0[Geom::X];
        double max_y = p0[Geom::Y];

        min_x = MIN( min_x, p1[Geom::X] );
        min_y = MIN( min_y, p1[Geom::Y] );
        max_x = MAX( max_x, p1[Geom::X] );
        max_y = MAX( max_y, p1[Geom::Y] );

        min_x = MIN( min_x, p2[Geom::X] );
        min_y = MIN( min_y, p2[Geom::Y] );
        max_x = MAX( max_x, p2[Geom::X] );
        max_y = MAX( max_y, p2[Geom::Y] );

        min_x = MIN( min_x, p3[Geom::X] );
        min_y = MIN( min_y, p3[Geom::Y] );
        max_x = MAX( max_x, p3[Geom::X] );
        max_y = MAX( max_y, p3[Geom::Y] );

        item->x1 = round( min_x - 1 );
        item->y1 = round( min_y - 1 );
        item->x2 = round( max_x + 1 );
        item->y2 = round( max_y + 1 );

        item->canvas->requestRedraw(item->x1, item->y1, item->x2, item->y2);

    }
}

} // namespace

#define EPSILON 1e-6
#define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)

void SPCtrlCurve::setCoords( gdouble x0, gdouble y0, gdouble x1, gdouble y1,
                             gdouble x2, gdouble y2, gdouble x3, gdouble y3 )
{

    Geom::Point q0( x0, y0 );
    Geom::Point q1( x1, y1 );
    Geom::Point q2( x2, y2 );
    Geom::Point q3( x3, y3 );

    setCoords( q0, q1, q2, q3 );

}

void SPCtrlCurve::setCoords( Geom::Point const &q0, Geom::Point const &q1,
                             Geom::Point const &q2, Geom::Point const &q3)
{
    if (DIFFER(p0[Geom::X], q0[Geom::X]) || DIFFER(p0[Geom::Y], q0[Geom::Y]) ||
        DIFFER(p1[Geom::X], q1[Geom::X]) || DIFFER(p1[Geom::Y], q1[Geom::Y]) ||
        DIFFER(p2[Geom::X], q2[Geom::X]) || DIFFER(p2[Geom::Y], q2[Geom::Y]) ||
        DIFFER(p3[Geom::X], q3[Geom::X]) || DIFFER(p3[Geom::Y], q3[Geom::Y]) ) {
        p0 = q0;
        p1 = q1;
        p2 = q2;
        p3 = q3;
        sp_canvas_item_request_update( this );
    }
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :