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 * Maximilian Albert <maximilian.albert@gmail.com>
10 *
11 * Copyright (C) 2000-2002 Lauris Kaplinski
12 * Copyright (C) 2007 Johan Engelen
13 * Copyright (C) 2009 Maximilian Albert
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
19 #include <libnr/nr-pixops.h>
20 #include <2geom/transforms.h>
21 #include "display-forward.h"
22 #include "sp-canvas-util.h"
23 #include "sp-ctrlpoint.h"
24 #include "guideline.h"
26 static void sp_guideline_class_init(SPGuideLineClass *c);
27 static void sp_guideline_init(SPGuideLine *guideline);
28 static void sp_guideline_destroy(GtkObject *object);
30 static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
31 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf);
33 static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
35 static void sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba);
37 static SPCanvasItemClass *parent_class;
39 GType sp_guideline_get_type()
40 {
41 static GType guideline_type = 0;
43 if (!guideline_type) {
44 static GTypeInfo const guideline_info = {
45 sizeof (SPGuideLineClass),
46 NULL, NULL,
47 (GClassInitFunc) sp_guideline_class_init,
48 NULL, NULL,
49 sizeof (SPGuideLine),
50 16,
51 (GInstanceInitFunc) sp_guideline_init,
52 NULL,
53 };
55 guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
56 }
58 return guideline_type;
59 }
61 static void sp_guideline_class_init(SPGuideLineClass *c)
62 {
63 parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
65 GtkObjectClass *object_class = (GtkObjectClass *) c;
66 object_class->destroy = sp_guideline_destroy;
68 SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
69 item_class->update = sp_guideline_update;
70 item_class->render = sp_guideline_render;
71 item_class->point = sp_guideline_point;
72 }
74 static void sp_guideline_init(SPGuideLine *gl)
75 {
76 gl->rgba = 0x0000ff7f;
78 gl->normal_to_line = Geom::Point(0,1);
79 gl->angle = 3.14159265358979323846/2;
80 gl->point_on_line = Geom::Point(0,0);
81 gl->sensitive = 0;
83 gl->origin = NULL;
84 }
86 static void sp_guideline_destroy(GtkObject *object)
87 {
88 g_return_if_fail (object != NULL);
89 g_return_if_fail (SP_IS_GUIDELINE (object));
90 //g_return_if_fail (SP_GUIDELINE(object)->origin != NULL);
91 //g_return_if_fail (SP_IS_CTRLPOINT(SP_GUIDELINE(object)->origin));
93 if (SP_GUIDELINE(object)->origin != NULL && SP_IS_CTRLPOINT(SP_GUIDELINE(object)->origin)) {
94 gtk_object_destroy(GTK_OBJECT(SP_GUIDELINE(object)->origin));
95 } else {
96 // FIXME: This branch shouldn't be reached (although it seems to be harmless).
97 //g_error("Why can it be that gl->origin is not a valid SPCtrlPoint?\n");
98 }
100 GTK_OBJECT_CLASS(parent_class)->destroy(object);
101 }
103 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
104 {
105 SPGuideLine const *gl = SP_GUIDELINE (item);
107 sp_canvas_prepare_buffer(buf);
109 unsigned int const r = NR_RGBA32_R (gl->rgba);
110 unsigned int const g = NR_RGBA32_G (gl->rgba);
111 unsigned int const b = NR_RGBA32_B (gl->rgba);
112 unsigned int const a = NR_RGBA32_A (gl->rgba);
114 if (gl->is_vertical()) {
115 int position = (int) Inkscape::round(gl->point_on_line[Geom::X]);
116 if (position < buf->rect.x0 || position >= buf->rect.x1) {
117 return;
118 }
120 int p0 = buf->rect.y0;
121 int p1 = buf->rect.y1;
122 int step = buf->buf_rowstride;
123 unsigned char *d = buf->buf + 4 * (position - buf->rect.x0);
125 for (int p = p0; p < p1; p++) {
126 d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
127 d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
128 d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
129 d += step;
130 }
131 } else if (gl->is_horizontal()) {
132 int position = (int) Inkscape::round(gl->point_on_line[Geom::Y]);
133 if (position < buf->rect.y0 || position >= buf->rect.y1) {
134 return;
135 }
137 int p0 = buf->rect.x0;
138 int p1 = buf->rect.x1;
139 int step = 4;
140 unsigned char *d = buf->buf + (position - buf->rect.y0) * buf->buf_rowstride;
142 for (int p = p0; p < p1; p++) {
143 d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
144 d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
145 d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
146 d += step;
147 }
148 } else {
149 // render angled line, once intersection has been detected, draw from there.
150 Geom::Point parallel_to_line( gl->normal_to_line[Geom::Y],
151 /*should be minus, but inverted y axis*/ gl->normal_to_line[Geom::X]);
153 //try to intersect with left vertical of rect
154 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];
155 if ( (y_intersect_left >= buf->rect.y0) && (y_intersect_left <= buf->rect.y1) ) {
156 // intersects with left vertical!
157 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];
158 sp_guideline_drawline (buf, buf->rect.x0, static_cast<gint>(round(y_intersect_left)), buf->rect.x1, static_cast<gint>(round(y_intersect_right)), gl->rgba);
159 return;
160 }
162 //try to intersect with right vertical of rect
163 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];
164 if ( (y_intersect_right >= buf->rect.y0) && (y_intersect_right <= buf->rect.y1) ) {
165 // intersects with right vertical!
166 sp_guideline_drawline (buf, buf->rect.x1, static_cast<gint>(round(y_intersect_right)), buf->rect.x0, static_cast<gint>(round(y_intersect_left)), gl->rgba);
167 return;
168 }
170 //try to intersect with top horizontal of rect
171 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];
172 if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
173 // intersects with top horizontal!
174 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];
175 sp_guideline_drawline (buf, static_cast<gint>(round(x_intersect_top)), buf->rect.y0, static_cast<gint>(round(x_intersect_bottom)), buf->rect.y1, gl->rgba);
176 return;
177 }
179 //try to intersect with bottom horizontal of rect
180 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];
181 if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
182 // intersects with bottom horizontal!
183 sp_guideline_drawline (buf, static_cast<gint>(round(x_intersect_bottom)), buf->rect.y1, static_cast<gint>(round(x_intersect_top)), buf->rect.y0, gl->rgba);
184 return;
185 }
186 }
187 }
189 static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
190 {
191 SPGuideLine *gl = SP_GUIDELINE(item);
193 if (((SPCanvasItemClass *) parent_class)->update) {
194 ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
195 }
197 gl->point_on_line[Geom::X] = affine[4];
198 gl->point_on_line[Geom::Y] = affine[5];
200 sp_ctrlpoint_set_coords(gl->origin, gl->point_on_line * affine.inverse());
201 sp_canvas_item_request_update(SP_CANVAS_ITEM (gl->origin));
203 if (gl->is_horizontal()) {
204 sp_canvas_update_bbox (item, -1000000, (int) Inkscape::round(gl->point_on_line[Geom::Y]), 1000000, (int) Inkscape::round(gl->point_on_line[Geom::Y] + 1));
205 } else if (gl->is_vertical()) {
206 sp_canvas_update_bbox (item, (int) Inkscape::round(gl->point_on_line[Geom::X]), -1000000, (int) Inkscape::round(gl->point_on_line[Geom::X] + 1), 1000000);
207 } else {
208 sp_canvas_update_bbox (item, -1000000, -1000000, 1000000, 1000000);
209 }
210 }
212 // Returns 0.0 if point is on the guideline
213 static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
214 {
215 SPGuideLine *gl = SP_GUIDELINE (item);
217 if (!gl->sensitive) {
218 return NR_HUGE;
219 }
221 *actual_item = item;
223 Geom::Point vec(gl->normal_to_line[Geom::X], - gl->normal_to_line[Geom::Y]);
224 double distance = Geom::dot((p - gl->point_on_line), vec);
225 return MAX(fabs(distance)-1, 0);
226 }
228 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, Geom::Point point_on_line, Geom::Point normal)
229 {
230 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
231 SPCanvasItem *origin = sp_canvas_item_new(parent, SP_TYPE_CTRLPOINT, NULL);
233 SPGuideLine *gl = SP_GUIDELINE(item);
234 SPCtrlPoint *cp = SP_CTRLPOINT(origin);
235 gl->origin = cp;
237 normal.normalize();
238 gl->normal_to_line = normal;
239 gl->angle = tan( -gl->normal_to_line[Geom::X] / gl->normal_to_line[Geom::Y]);
240 sp_guideline_set_position(gl, point_on_line);
242 sp_ctrlpoint_set_coords(cp, point_on_line);
244 return item;
245 }
247 void sp_guideline_set_position(SPGuideLine *gl, Geom::Point point_on_line)
248 {
249 sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl), Geom::Matrix(Geom::Translate(point_on_line)));
250 sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl->origin), Geom::Matrix(Geom::Translate(point_on_line)));
251 }
253 void sp_guideline_set_normal(SPGuideLine *gl, Geom::Point normal_to_line)
254 {
255 gl->normal_to_line = normal_to_line;
256 gl->angle = tan( -normal_to_line[Geom::X] / normal_to_line[Geom::Y]);
258 sp_canvas_item_request_update(SP_CANVAS_ITEM (gl));
259 }
261 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
262 {
263 gl->rgba = rgba;
264 sp_ctrlpoint_set_color(gl->origin, rgba);
266 sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
267 }
269 void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
270 {
271 gl->sensitive = sensitive;
272 }
274 void sp_guideline_delete(SPGuideLine *gl)
275 {
276 //gtk_object_destroy(GTK_OBJECT(gl->origin));
277 gtk_object_destroy(GTK_OBJECT(gl));
278 }
280 //##########################################################
281 // Line rendering
282 #define SAFE_SETPIXEL //undefine this when it is certain that setpixel is never called with invalid params
284 /**
285 \brief This function renders a pixel on a particular buffer.
287 The topleft of the buffer equals
288 ( rect.x0 , rect.y0 ) in screen coordinates
289 ( 0 , 0 ) in setpixel coordinates
290 The bottomright of the buffer equals
291 ( rect.x1 , rect,y1 ) in screen coordinates
292 ( rect.x1 - rect.x0 , rect.y1 - rect.y0 ) in setpixel coordinates
293 */
294 static void
295 sp_guideline_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
296 {
297 #ifdef SAFE_SETPIXEL
298 if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
299 #endif
300 guint r, g, b, a;
301 r = NR_RGBA32_R (rgba);
302 g = NR_RGBA32_G (rgba);
303 b = NR_RGBA32_B (rgba);
304 a = NR_RGBA32_A (rgba);
305 guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
306 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
307 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
308 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
309 #ifdef SAFE_SETPIXEL
310 }
311 #endif
312 }
314 /**
315 \brief This function renders a line on a particular canvas buffer,
316 using Bresenham's line drawing function.
317 http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
318 Coordinates are interpreted as SCREENcoordinates
319 */
320 static void
321 sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
322 {
323 int dy = y1 - y0;
324 int dx = x1 - x0;
325 int stepx, stepy;
327 if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
328 if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
329 dy <<= 1; // dy is now 2*dy
330 dx <<= 1; // dx is now 2*dx
332 sp_guideline_setpixel(buf, x0, y0, rgba);
333 if (dx > dy) {
334 int fraction = dy - (dx >> 1); // same as 2*dy - dx
335 while (x0 != x1) {
336 if (fraction >= 0) {
337 y0 += stepy;
338 fraction -= dx; // same as fraction -= 2*dx
339 }
340 x0 += stepx;
341 fraction += dy; // same as fraction -= 2*dy
342 sp_guideline_setpixel(buf, x0, y0, rgba);
343 }
344 } else {
345 int fraction = dx - (dy >> 1);
346 while (y0 != y1) {
347 if (fraction >= 0) {
348 x0 += stepx;
349 fraction -= dy;
350 }
351 y0 += stepy;
352 fraction += dx;
353 sp_guideline_setpixel(buf, x0, y0, rgba);
354 }
355 }
356 }
358 /*
359 Local Variables:
360 mode:c++
361 c-file-style:"stroustrup"
362 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
363 indent-tabs-mode:nil
364 fill-column:99
365 End:
366 */
367 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :