Code

guidelines: implement rendering of angled lines.
[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 void sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba);
33 static SPCanvasItemClass *parent_class;
35 GType sp_guideline_get_type()
36 {
37     static GType guideline_type = 0;
39     if (!guideline_type) {
40         static GTypeInfo const guideline_info = {
41             sizeof (SPGuideLineClass),
42             NULL, NULL,
43             (GClassInitFunc) sp_guideline_class_init,
44             NULL, NULL,
45             sizeof (SPGuideLine),
46             16,
47             (GInstanceInitFunc) sp_guideline_init,
48             NULL,
49         };
51         guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
52     }
54     return guideline_type;
55 }
57 static void sp_guideline_class_init(SPGuideLineClass *c)
58 {
59     parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
61     GtkObjectClass *object_class = (GtkObjectClass *) c;
62     object_class->destroy = sp_guideline_destroy;
64     SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
65     item_class->update = sp_guideline_update;
66     item_class->render = sp_guideline_render;
67     item_class->point = sp_guideline_point;
68 }
70 static void sp_guideline_init(SPGuideLine *gl)
71 {
72     gl->rgba = 0x0000ff7f;
74     gl->normal_to_line = Geom::Point(0,1);
75     gl->angle = 3.14159265358979323846/2;
76     gl->point_on_line = Geom::Point(0,0);
77     gl->sensitive = 0;
78 }
80 static void sp_guideline_destroy(GtkObject *object)
81 {
82     GTK_OBJECT_CLASS(parent_class)->destroy(object);
83 }
85 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
86 {
87     SPGuideLine const *gl = SP_GUIDELINE (item);
89     sp_canvas_prepare_buffer(buf);
91     unsigned int const r = NR_RGBA32_R (gl->rgba);
92     unsigned int const g = NR_RGBA32_G (gl->rgba);
93     unsigned int const b = NR_RGBA32_B (gl->rgba);
94     unsigned int const a = NR_RGBA32_A (gl->rgba);
96     if (gl->normal_to_line[Geom::Y] == 0.) {
97         int position = gl->point_on_line[Geom::X];
98         if (position < buf->rect.x0 || position >= buf->rect.x1) {
99             return;
100         }
102         int p0 = buf->rect.y0;
103         int p1 = buf->rect.y1;
104         int step = buf->buf_rowstride;
105         unsigned char *d = buf->buf + 3 * (position - buf->rect.x0);
107         for (int p = p0; p < p1; p++) {
108             d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
109             d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
110             d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
111             d += step;
112         }
113     } else if (gl->normal_to_line[Geom::X] == 0.) {
114         int position = gl->point_on_line[Geom::Y];
115         if (position < buf->rect.y0 || position >= buf->rect.y1) {
116             return;
117         }
119         int p0 = buf->rect.x0;
120         int p1 = buf->rect.x1;
121         int step = 3;
122         unsigned char *d = buf->buf + (position - buf->rect.y0) * buf->buf_rowstride;
124         for (int p = p0; p < p1; p++) {
125             d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
126             d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
127             d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
128             d += step;
129         }
130     } else {
131         // render angled line, once intersection has been detected, draw from there.
132         Geom::Point parallel_to_line( gl->normal_to_line[Geom::Y],
133                                       /*should be minus, but inverted y axis*/ gl->normal_to_line[Geom::X]); 
135         //try to intersect with left vertical of rect
136         double y_intersect_left = (buf->rect.x0 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
137         if ( (y_intersect_left >= buf->rect.y0) && (y_intersect_left <= buf->rect.y1) ) {
138             // intersects with left vertical!
139             double y_intersect_right = (buf->rect.x1 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
140             sp_guideline_drawline (buf, buf->rect.x0, round(y_intersect_left), buf->rect.x1, round(y_intersect_right), gl->rgba);
141             return;
142         }
144         //try to intersect with right vertical of rect
145         double y_intersect_right = (buf->rect.x1 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
146         if ( (y_intersect_right >= buf->rect.y0) && (y_intersect_right <= buf->rect.y1) ) {
147             // intersects with right vertical!
148             sp_guideline_drawline (buf, buf->rect.x1, round(y_intersect_right), buf->rect.x0, round(y_intersect_left), gl->rgba);
149             return;
150         }
152         //try to intersect with top horizontal of rect
153         double x_intersect_top = (buf->rect.y0 - gl->point_on_line[Geom::Y]) * parallel_to_line[Geom::X] / parallel_to_line[Geom::Y] + gl->point_on_line[Geom::X];
154         if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
155             // intersects with top horizontal!
156             double x_intersect_bottom = (buf->rect.y1 - gl->point_on_line[Geom::Y]) * parallel_to_line[Geom::X] / parallel_to_line[Geom::Y] + gl->point_on_line[Geom::X];
157             sp_guideline_drawline (buf, round(x_intersect_top), buf->rect.y0, round(x_intersect_bottom), buf->rect.y1, gl->rgba);
158             return;
159         }
161         //try to intersect with bottom horizontal of rect
162         double x_intersect_bottom = (buf->rect.y1 - gl->point_on_line[Geom::Y]) * parallel_to_line[Geom::X] / parallel_to_line[Geom::Y] + gl->point_on_line[Geom::X];
163         if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
164             // intersects with bottom horizontal!
165             sp_guideline_drawline (buf, round(x_intersect_bottom), buf->rect.y1, round(x_intersect_top), buf->rect.y0, gl->rgba);
166             return;
167         }
168     }
171 static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
173     SPGuideLine *gl = SP_GUIDELINE(item);
175     if (((SPCanvasItemClass *) parent_class)->update) {
176         ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
177     }
179     gl->point_on_line[Geom::X] = affine[4] +0.5;
180     gl->point_on_line[Geom::Y] = affine[5] -0.5;
182     if (gl->normal_to_line[Geom::X] == 0.) {
183         sp_canvas_update_bbox (item, -1000000, gl->point_on_line[Geom::Y], 1000000, gl->point_on_line[Geom::Y] + 1);
184     } else if (gl->normal_to_line[Geom::Y] == 0.) {
185         sp_canvas_update_bbox (item, gl->point_on_line[Geom::X], -1000000, gl->point_on_line[Geom::X]+1, 1000000);
186     } else {
187         sp_canvas_update_bbox (item, -1000000, -1000000, 1000000, 1000000);
188     }
191 // Returns 0.0 if point is on the guideline
192 static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
194     SPGuideLine *gl = SP_GUIDELINE (item);
196     if (!gl->sensitive) {
197         return NR_HUGE;
198     }
200     *actual_item = item;
202     Geom::Point vec(gl->normal_to_line[Geom::X], - gl->normal_to_line[Geom::Y]);
203     double distance = Geom::dot((p.to_2geom() - gl->point_on_line), vec);
204     return MAX(fabs(distance)-1, 0);
207 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, Geom::Point point_on_line, Geom::Point normal)
209     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
211     SPGuideLine *gl = SP_GUIDELINE(item);
213     normal.normalize();
214     gl->normal_to_line = normal;
215     gl->angle = tan( -gl->normal_to_line[Geom::X] / gl->normal_to_line[Geom::Y]);
216     sp_guideline_set_position(gl, point_on_line);
218     return item;
221 void sp_guideline_set_position(SPGuideLine *gl, Geom::Point point_on_line)
223     sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl),
224                                    NR::Matrix(NR::translate(point_on_line)));
227 void sp_guideline_set_normal(SPGuideLine *gl, Geom::Point normal_to_line)
229     gl->normal_to_line = normal_to_line;
230     gl->angle = tan( -normal_to_line[Geom::X] / normal_to_line[Geom::Y]);
232     sp_canvas_item_request_update(SP_CANVAS_ITEM (gl));
235 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
237     gl->rgba = rgba;
239     sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
242 void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
244     gl->sensitive = sensitive;
247 //##########################################################
248 // Line rendering
249 #define SAFE_SETPIXEL   //undefine this when it is certain that setpixel is never called with invalid params
251 /**
252     \brief  This function renders a pixel on a particular buffer.
254     The topleft of the buffer equals
255                         ( rect.x0 , rect.y0 )  in screen coordinates
256                         ( 0 , 0 )  in setpixel coordinates
257     The bottomright of the buffer equals
258                         ( rect.x1 , rect,y1 )  in screen coordinates
259                         ( rect.x1 - rect.x0 , rect.y1 - rect.y0 )  in setpixel coordinates
260 */
261 static void
262 sp_guideline_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
264 #ifdef SAFE_SETPIXEL
265     if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
266 #endif
267         guint r, g, b, a;
268         r = NR_RGBA32_R (rgba);
269         g = NR_RGBA32_G (rgba);
270         b = NR_RGBA32_B (rgba);
271         a = NR_RGBA32_A (rgba);
272         guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
273         p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
274         p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
275         p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
276 #ifdef SAFE_SETPIXEL
277     }
278 #endif
281 /**
282     \brief  This function renders a line on a particular canvas buffer,
283             using Bresenham's line drawing function.
284             http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
285             Coordinates are interpreted as SCREENcoordinates
286 */
287 static void
288 sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
290     int dy = y1 - y0;
291     int dx = x1 - x0;
292     int stepx, stepy;
294     if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
295     if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
296     dy <<= 1;                                                  // dy is now 2*dy
297     dx <<= 1;                                                  // dx is now 2*dx
299     sp_guideline_setpixel(buf, x0, y0, rgba);
300     if (dx > dy) {
301         int fraction = dy - (dx >> 1);                         // same as 2*dy - dx
302         while (x0 != x1) {
303             if (fraction >= 0) {
304                 y0 += stepy;
305                 fraction -= dx;                                // same as fraction -= 2*dx
306             }
307             x0 += stepx;
308             fraction += dy;                                    // same as fraction -= 2*dy
309             sp_guideline_setpixel(buf, x0, y0, rgba);
310         }
311     } else {
312         int fraction = dx - (dy >> 1);
313         while (y0 != y1) {
314             if (fraction >= 0) {
315                 x0 += stepx;
316                 fraction -= dy;
317             }
318             y0 += stepy;
319             fraction += dx;
320             sp_guideline_setpixel(buf, x0, y0, rgba);
321         }
322     }
325 /*
326   Local Variables:
327   mode:c++
328   c-file-style:"stroustrup"
329   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
330   indent-tabs-mode:nil
331   fill-column:99
332   End:
333 */
334 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :