Code

optimize guide canvasupdating
[inkscape.git] / src / display / guideline.cpp
1 #define __SP_GUIDELINE_C__
3 /*
4  * Horizontal/vertical but can also be angled line
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Johan Engelen
9  *
10  * Copyright (C) 2000-2002 Lauris Kaplinski
11  * Copyright (C) 2007 Johan Engelen
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
17 #include <libnr/nr-pixops.h>
18 #include "display-forward.h"
19 #include "sp-canvas-util.h"
20 #include "guideline.h"
22 static void sp_guideline_class_init(SPGuideLineClass *c);
23 static void sp_guideline_init(SPGuideLine *guideline);
24 static void sp_guideline_destroy(GtkObject *object);
26 static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
27 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf);
29 static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
31 static SPCanvasItemClass *parent_class;
33 GType sp_guideline_get_type()
34 {
35     static GType guideline_type = 0;
37     if (!guideline_type) {
38         static GTypeInfo const guideline_info = {
39             sizeof (SPGuideLineClass),
40             NULL, NULL,
41             (GClassInitFunc) sp_guideline_class_init,
42             NULL, NULL,
43             sizeof (SPGuideLine),
44             16,
45             (GInstanceInitFunc) sp_guideline_init,
46             NULL,
47         };
49         guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
50     }
52     return guideline_type;
53 }
55 static void sp_guideline_class_init(SPGuideLineClass *c)
56 {
57     parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
59     GtkObjectClass *object_class = (GtkObjectClass *) c;
60     object_class->destroy = sp_guideline_destroy;
62     SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
63     item_class->update = sp_guideline_update;
64     item_class->render = sp_guideline_render;
65     item_class->point = sp_guideline_point;
66 }
68 static void sp_guideline_init(SPGuideLine *gl)
69 {
70     gl->rgba = 0x0000ff7f;
72     gl->normal_to_line = Geom::Point(0,1);
73     gl->point_on_line = Geom::Point(0,0);
74     gl->sensitive = 0;
75 }
77 static void sp_guideline_destroy(GtkObject *object)
78 {
79     GTK_OBJECT_CLASS(parent_class)->destroy(object);
80 }
82 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
83 {
84     SPGuideLine const *gl = SP_GUIDELINE (item);
86     sp_canvas_prepare_buffer(buf);
88     unsigned int const r = NR_RGBA32_R (gl->rgba);
89     unsigned int const g = NR_RGBA32_G (gl->rgba);
90     unsigned int const b = NR_RGBA32_B (gl->rgba);
91     unsigned int const a = NR_RGBA32_A (gl->rgba);
93     if (gl->normal_to_line[Geom::Y] == 0.) {
94         int position = gl->point_on_line[Geom::X];
95         if (position < buf->rect.x0 || position >= buf->rect.x1) {
96             return;
97         }
99         int p0 = buf->rect.y0;
100         int p1 = buf->rect.y1;
101         int step = buf->buf_rowstride;
102         unsigned char *d = buf->buf + 3 * (position - buf->rect.x0);
104         for (int p = p0; p < p1; p++) {
105             d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
106             d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
107             d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
108             d += step;
109         }
110     } else if (gl->normal_to_line[Geom::X] == 0.) {
111         int position = gl->point_on_line[Geom::Y];
112         if (position < buf->rect.y0 || position >= buf->rect.y1) {
113             return;
114         }
116         int p0 = buf->rect.x0;
117         int p1 = buf->rect.x1;
118         int step = 3;
119         unsigned char *d = buf->buf + (position - buf->rect.y0) * buf->buf_rowstride;
121         for (int p = p0; p < p1; p++) {
122             d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
123             d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
124             d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
125             d += step;
126         }
127     } else {
128         // render angled line
129     }
132 static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
134     SPGuideLine *gl = SP_GUIDELINE(item);
136     if (((SPCanvasItemClass *) parent_class)->update) {
137         ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
138     }
140     gl->point_on_line[Geom::X] = affine[4];
141     gl->point_on_line[Geom::Y] = affine[5];
143     if (gl->normal_to_line[Geom::Y] == 1.) {
144         sp_canvas_update_bbox (item, -1000000, gl->point_on_line[Geom::Y], 1000000, gl->point_on_line[Geom::Y] + 1);
145     } else if (gl->normal_to_line[Geom::X] == 1.) {
146         sp_canvas_update_bbox (item, gl->point_on_line[Geom::X], -1000000, gl->point_on_line[Geom::X]+1, 1000000);
147     } else {
148         sp_canvas_update_bbox (item, -1000000, -1000000, 1000000, 1000000);
149     }
152 // Returns 0.0 if point is on the guideline
153 static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
155     SPGuideLine *gl = SP_GUIDELINE (item);
157     if (!gl->sensitive) {
158         return NR_HUGE;
159     }
161     *actual_item = item;
163     double distance = Geom::dot((p.to_2geom() - gl->point_on_line), gl->normal_to_line);
164     return MAX(fabs(distance)-1, 0);
167 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, Geom::Point point_on_line, Geom::Point normal)
169     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
171     SPGuideLine *gl = SP_GUIDELINE(item);
173     normal.normalize();
174     gl->normal_to_line = normal;
175     sp_guideline_set_position(gl, point_on_line);
177     return item;
180 void sp_guideline_set_position(SPGuideLine *gl, Geom::Point point_on_line)
182     sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl),
183                                    NR::Matrix(NR::translate(point_on_line)));
186 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
188     gl->rgba = rgba;
190     sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
193 void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
195     gl->sensitive = sensitive;
199 /*
200   Local Variables:
201   mode:c++
202   c-file-style:"stroustrup"
203   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
204   indent-tabs-mode:nil
205   fill-column:99
206   End:
207 */
208 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :