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-ctrlquadr.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;
38 static const double radius = 7.0;
40 GType sp_guideline_get_type()
41 {
42 static GType guideline_type = 0;
44 if (!guideline_type) {
45 static GTypeInfo const guideline_info = {
46 sizeof (SPGuideLineClass),
47 NULL, NULL,
48 (GClassInitFunc) sp_guideline_class_init,
49 NULL, NULL,
50 sizeof (SPGuideLine),
51 16,
52 (GInstanceInitFunc) sp_guideline_init,
53 NULL,
54 };
56 guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
57 }
59 return guideline_type;
60 }
62 static void sp_guideline_class_init(SPGuideLineClass *c)
63 {
64 parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
66 GtkObjectClass *object_class = (GtkObjectClass *) c;
67 object_class->destroy = sp_guideline_destroy;
69 SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
70 item_class->update = sp_guideline_update;
71 item_class->render = sp_guideline_render;
72 item_class->point = sp_guideline_point;
73 }
75 static void sp_guideline_init(SPGuideLine *gl)
76 {
77 gl->rgba = 0x0000ff7f;
79 gl->normal_to_line = Geom::Point(0,1);
80 gl->angle = 3.14159265358979323846/2;
81 gl->point_on_line = Geom::Point(0,0);
82 gl->sensitive = 0;
84 gl->origin = NULL;
85 }
87 static void sp_guideline_destroy(GtkObject *object)
88 {
89 g_return_if_fail (object != NULL);
90 g_return_if_fail (SP_IS_GUIDELINE (object));
91 //g_return_if_fail (SP_GUIDELINE(object)->origin != NULL);
92 //g_return_if_fail (SP_IS_CTRLQUADR(SP_GUIDELINE(object)->origin));
94 if (SP_GUIDELINE(object)->origin != NULL && SP_IS_CTRLQUADR(SP_GUIDELINE(object)->origin)) {
95 gtk_object_destroy(GTK_OBJECT(SP_GUIDELINE(object)->origin));
96 } else {
97 // FIXME: This branch shouldn't be reached (although it seems to be harmless).
98 //g_error("Why can it be that gl->origin is not a valid SPCtrlQuadr?\n");
99 }
101 GTK_OBJECT_CLASS(parent_class)->destroy(object);
102 }
104 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
105 {
106 SPGuideLine const *gl = SP_GUIDELINE (item);
108 sp_canvas_prepare_buffer(buf);
110 unsigned int const r = NR_RGBA32_R (gl->rgba);
111 unsigned int const g = NR_RGBA32_G (gl->rgba);
112 unsigned int const b = NR_RGBA32_B (gl->rgba);
113 unsigned int const a = NR_RGBA32_A (gl->rgba);
115 if (gl->is_vertical()) {
116 int position = (int) Inkscape::round(gl->point_on_line[Geom::X]);
117 if (position < buf->rect.x0 || position >= buf->rect.x1) {
118 return;
119 }
121 int p0 = buf->rect.y0;
122 int p1 = buf->rect.y1;
123 int step = buf->buf_rowstride;
124 unsigned char *d = buf->buf + 4 * (position - buf->rect.x0);
126 for (int p = p0; p < p1; p++) {
127 d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
128 d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
129 d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
130 d += step;
131 }
132 } else if (gl->is_horizontal()) {
133 int position = (int) Inkscape::round(gl->point_on_line[Geom::Y]);
134 if (position < buf->rect.y0 || position >= buf->rect.y1) {
135 return;
136 }
138 int p0 = buf->rect.x0;
139 int p1 = buf->rect.x1;
140 int step = 4;
141 unsigned char *d = buf->buf + (position - buf->rect.y0) * buf->buf_rowstride;
143 for (int p = p0; p < p1; p++) {
144 d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
145 d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
146 d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
147 d += step;
148 }
149 } else {
150 // render angled line, once intersection has been detected, draw from there.
151 Geom::Point parallel_to_line( gl->normal_to_line[Geom::Y],
152 /*should be minus, but inverted y axis*/ gl->normal_to_line[Geom::X]);
154 //try to intersect with left vertical of rect
155 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];
156 if ( (y_intersect_left >= buf->rect.y0) && (y_intersect_left <= buf->rect.y1) ) {
157 // intersects with left vertical!
158 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];
159 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);
160 return;
161 }
163 //try to intersect with right vertical of rect
164 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];
165 if ( (y_intersect_right >= buf->rect.y0) && (y_intersect_right <= buf->rect.y1) ) {
166 // intersects with right vertical!
167 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);
168 return;
169 }
171 //try to intersect with top horizontal of rect
172 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];
173 if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
174 // intersects with top horizontal!
175 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];
176 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);
177 return;
178 }
180 //try to intersect with bottom horizontal of rect
181 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];
182 if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
183 // intersects with bottom horizontal!
184 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);
185 return;
186 }
187 }
188 }
190 static void set_origin_coords(SPCtrlQuadr *quadr, Geom::Point const ¢er, double const r)
191 {
192 sp_ctrlquadr_set_coords(quadr,
193 center + Geom::Point(-r,-r),
194 center + Geom::Point(-r, r),
195 center + Geom::Point( r, r),
196 center + Geom::Point( r,-r));
198 }
200 static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
201 {
202 SPGuideLine *gl = SP_GUIDELINE(item);
204 if (((SPCanvasItemClass *) parent_class)->update) {
205 ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
206 }
208 gl->point_on_line[Geom::X] = affine[4];
209 gl->point_on_line[Geom::Y] = affine[5];
211 set_origin_coords(gl->origin, gl->point_on_line * affine.inverse(), radius);
212 sp_canvas_item_request_update(SP_CANVAS_ITEM (gl->origin));
214 if (gl->is_horizontal()) {
215 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));
216 } else if (gl->is_vertical()) {
217 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);
218 } else {
219 sp_canvas_update_bbox (item, -1000000, -1000000, 1000000, 1000000);
220 }
221 }
223 // Returns 0.0 if point is on the guideline
224 static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
225 {
226 SPGuideLine *gl = SP_GUIDELINE (item);
228 if (!gl->sensitive) {
229 return NR_HUGE;
230 }
232 *actual_item = item;
234 Geom::Point vec(gl->normal_to_line[Geom::X], - gl->normal_to_line[Geom::Y]);
235 double distance = Geom::dot((p - gl->point_on_line), vec);
236 return MAX(fabs(distance)-1, 0);
237 }
239 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, Geom::Point point_on_line, Geom::Point normal)
240 {
241 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
242 SPCanvasItem *origin = sp_canvas_item_new(parent, SP_TYPE_CTRLQUADR, NULL);
244 SPGuideLine *gl = SP_GUIDELINE(item);
245 SPCtrlQuadr *cp = SP_CTRLQUADR(origin);
246 gl->origin = cp;
248 normal.normalize();
249 gl->normal_to_line = normal;
250 gl->angle = tan( -gl->normal_to_line[Geom::X] / gl->normal_to_line[Geom::Y]);
251 sp_guideline_set_position(gl, point_on_line);
253 set_origin_coords(cp, point_on_line, radius);
255 return item;
256 }
258 void sp_guideline_set_position(SPGuideLine *gl, Geom::Point point_on_line)
259 {
260 sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl), Geom::Matrix(Geom::Translate(point_on_line)));
261 sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl->origin), Geom::Matrix(Geom::Translate(point_on_line)));
262 }
264 void sp_guideline_set_normal(SPGuideLine *gl, Geom::Point normal_to_line)
265 {
266 gl->normal_to_line = normal_to_line;
267 gl->angle = tan( -normal_to_line[Geom::X] / normal_to_line[Geom::Y]);
269 sp_canvas_item_request_update(SP_CANVAS_ITEM (gl));
270 }
272 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
273 {
274 gl->rgba = rgba;
275 sp_ctrlquadr_set_rgba32(gl->origin, rgba);
277 sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
278 }
280 void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
281 {
282 gl->sensitive = sensitive;
283 }
285 void sp_guideline_delete(SPGuideLine *gl)
286 {
287 //gtk_object_destroy(GTK_OBJECT(gl->origin));
288 gtk_object_destroy(GTK_OBJECT(gl));
289 }
291 //##########################################################
292 // Line rendering
293 #define SAFE_SETPIXEL //undefine this when it is certain that setpixel is never called with invalid params
295 /**
296 \brief This function renders a pixel on a particular buffer.
298 The topleft of the buffer equals
299 ( rect.x0 , rect.y0 ) in screen coordinates
300 ( 0 , 0 ) in setpixel coordinates
301 The bottomright of the buffer equals
302 ( rect.x1 , rect,y1 ) in screen coordinates
303 ( rect.x1 - rect.x0 , rect.y1 - rect.y0 ) in setpixel coordinates
304 */
305 static void
306 sp_guideline_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
307 {
308 #ifdef SAFE_SETPIXEL
309 if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
310 #endif
311 guint r, g, b, a;
312 r = NR_RGBA32_R (rgba);
313 g = NR_RGBA32_G (rgba);
314 b = NR_RGBA32_B (rgba);
315 a = NR_RGBA32_A (rgba);
316 guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
317 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
318 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
319 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
320 #ifdef SAFE_SETPIXEL
321 }
322 #endif
323 }
325 /**
326 \brief This function renders a line on a particular canvas buffer,
327 using Bresenham's line drawing function.
328 http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
329 Coordinates are interpreted as SCREENcoordinates
330 */
331 static void
332 sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
333 {
334 int dy = y1 - y0;
335 int dx = x1 - x0;
336 int stepx, stepy;
338 if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
339 if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
340 dy <<= 1; // dy is now 2*dy
341 dx <<= 1; // dx is now 2*dx
343 sp_guideline_setpixel(buf, x0, y0, rgba);
344 if (dx > dy) {
345 int fraction = dy - (dx >> 1); // same as 2*dy - dx
346 while (x0 != x1) {
347 if (fraction >= 0) {
348 y0 += stepy;
349 fraction -= dx; // same as fraction -= 2*dx
350 }
351 x0 += stepx;
352 fraction += dy; // same as fraction -= 2*dy
353 sp_guideline_setpixel(buf, x0, y0, rgba);
354 }
355 } else {
356 int fraction = dx - (dy >> 1);
357 while (y0 != y1) {
358 if (fraction >= 0) {
359 x0 += stepx;
360 fraction -= dy;
361 }
362 y0 += stepy;
363 fraction += dx;
364 sp_guideline_setpixel(buf, x0, y0, rgba);
365 }
366 }
367 }
369 /*
370 Local Variables:
371 mode:c++
372 c-file-style:"stroustrup"
373 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
374 indent-tabs-mode:nil
375 fill-column:99
376 End:
377 */
378 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :