Code

Now users can design a font within inkscape, save it and then open the
[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 <2geom/transforms.h>
19 #include "display-forward.h"
20 #include "sp-canvas-util.h"
21 #include "guideline.h"
23 static void sp_guideline_class_init(SPGuideLineClass *c);
24 static void sp_guideline_init(SPGuideLine *guideline);
25 static void sp_guideline_destroy(GtkObject *object);
27 static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
28 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf);
30 static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
32 static void sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba);
34 static SPCanvasItemClass *parent_class;
36 GType sp_guideline_get_type()
37 {
38     static GType guideline_type = 0;
40     if (!guideline_type) {
41         static GTypeInfo const guideline_info = {
42             sizeof (SPGuideLineClass),
43             NULL, NULL,
44             (GClassInitFunc) sp_guideline_class_init,
45             NULL, NULL,
46             sizeof (SPGuideLine),
47             16,
48             (GInstanceInitFunc) sp_guideline_init,
49             NULL,
50         };
52         guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
53     }
55     return guideline_type;
56 }
58 static void sp_guideline_class_init(SPGuideLineClass *c)
59 {
60     parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
62     GtkObjectClass *object_class = (GtkObjectClass *) c;
63     object_class->destroy = sp_guideline_destroy;
65     SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
66     item_class->update = sp_guideline_update;
67     item_class->render = sp_guideline_render;
68     item_class->point = sp_guideline_point;
69 }
71 static void sp_guideline_init(SPGuideLine *gl)
72 {
73     gl->rgba = 0x0000ff7f;
75     gl->normal_to_line = Geom::Point(0,1);
76     gl->angle = 3.14159265358979323846/2;
77     gl->point_on_line = Geom::Point(0,0);
78     gl->sensitive = 0;
79 }
81 static void sp_guideline_destroy(GtkObject *object)
82 {
83     GTK_OBJECT_CLASS(parent_class)->destroy(object);
84 }
86 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
87 {
88     SPGuideLine const *gl = SP_GUIDELINE (item);
90     sp_canvas_prepare_buffer(buf);
92     unsigned int const r = NR_RGBA32_R (gl->rgba);
93     unsigned int const g = NR_RGBA32_G (gl->rgba);
94     unsigned int const b = NR_RGBA32_B (gl->rgba);
95     unsigned int const a = NR_RGBA32_A (gl->rgba);
97     if (gl->is_vertical()) {
98         int position = (int) Inkscape::round(gl->point_on_line[Geom::X]);
99         if (position < buf->rect.x0 || position >= buf->rect.x1) {
100             return;
101         }
103         int p0 = buf->rect.y0;
104         int p1 = buf->rect.y1;
105         int step = buf->buf_rowstride;
106         unsigned char *d = buf->buf + 4 * (position - buf->rect.x0);
108         for (int p = p0; p < p1; p++) {
109             d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
110             d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
111             d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
112             d += step;
113         }
114     } else if (gl->is_horizontal()) {
115         int position = (int) Inkscape::round(gl->point_on_line[Geom::Y]);
116         if (position < buf->rect.y0 || position >= buf->rect.y1) {
117             return;
118         }
120         int p0 = buf->rect.x0;
121         int p1 = buf->rect.x1;
122         int step = 4;
123         unsigned char *d = buf->buf + (position - buf->rect.y0) * buf->buf_rowstride;
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 {
132         // render angled line, once intersection has been detected, draw from there.
133         Geom::Point parallel_to_line( gl->normal_to_line[Geom::Y],
134                                       /*should be minus, but inverted y axis*/ gl->normal_to_line[Geom::X]);
136         //try to intersect with left vertical of rect
137         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];
138         if ( (y_intersect_left >= buf->rect.y0) && (y_intersect_left <= buf->rect.y1) ) {
139             // intersects with left vertical!
140             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];
141             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);
142             return;
143         }
145         //try to intersect with right vertical of rect
146         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];
147         if ( (y_intersect_right >= buf->rect.y0) && (y_intersect_right <= buf->rect.y1) ) {
148             // intersects with right vertical!
149             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);
150             return;
151         }
153         //try to intersect with top horizontal of rect
154         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];
155         if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
156             // intersects with top horizontal!
157             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];
158             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);
159             return;
160         }
162         //try to intersect with bottom horizontal of rect
163         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];
164         if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
165             // intersects with bottom horizontal!
166             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);
167             return;
168         }
169     }
172 static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
174     SPGuideLine *gl = SP_GUIDELINE(item);
176     if (((SPCanvasItemClass *) parent_class)->update) {
177         ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
178     }
180     gl->point_on_line[Geom::X] = affine[4];
181     gl->point_on_line[Geom::Y] = affine[5];
183     if (gl->is_horizontal()) {
184         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));
185     } else if (gl->is_vertical()) {
186         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);
187     } else {
188         sp_canvas_update_bbox (item, -1000000, -1000000, 1000000, 1000000);
189     }
192 // Returns 0.0 if point is on the guideline
193 static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
195     SPGuideLine *gl = SP_GUIDELINE (item);
197     if (!gl->sensitive) {
198         return NR_HUGE;
199     }
201     *actual_item = item;
203     Geom::Point vec(gl->normal_to_line[Geom::X], - gl->normal_to_line[Geom::Y]);
204     double distance = Geom::dot((p - gl->point_on_line), vec);
205     return MAX(fabs(distance)-1, 0);
208 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, Geom::Point point_on_line, Geom::Point normal)
210     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
212     SPGuideLine *gl = SP_GUIDELINE(item);
214     normal.normalize();
215     gl->normal_to_line = normal;
216     gl->angle = tan( -gl->normal_to_line[Geom::X] / gl->normal_to_line[Geom::Y]);
217     sp_guideline_set_position(gl, point_on_line);
219     return item;
222 void sp_guideline_set_position(SPGuideLine *gl, Geom::Point point_on_line)
224     sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl),
225                                    Geom::Matrix(Geom::Translate(point_on_line)));
228 void sp_guideline_set_normal(SPGuideLine *gl, Geom::Point normal_to_line)
230     gl->normal_to_line = normal_to_line;
231     gl->angle = tan( -normal_to_line[Geom::X] / normal_to_line[Geom::Y]);
233     sp_canvas_item_request_update(SP_CANVAS_ITEM (gl));
236 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
238     gl->rgba = rgba;
240     sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
243 void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
245     gl->sensitive = sensitive;
248 //##########################################################
249 // Line rendering
250 #define SAFE_SETPIXEL   //undefine this when it is certain that setpixel is never called with invalid params
252 /**
253     \brief  This function renders a pixel on a particular buffer.
255     The topleft of the buffer equals
256                         ( rect.x0 , rect.y0 )  in screen coordinates
257                         ( 0 , 0 )  in setpixel coordinates
258     The bottomright of the buffer equals
259                         ( rect.x1 , rect,y1 )  in screen coordinates
260                         ( rect.x1 - rect.x0 , rect.y1 - rect.y0 )  in setpixel coordinates
261 */
262 static void
263 sp_guideline_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
265 #ifdef SAFE_SETPIXEL
266     if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
267 #endif
268         guint r, g, b, a;
269         r = NR_RGBA32_R (rgba);
270         g = NR_RGBA32_G (rgba);
271         b = NR_RGBA32_B (rgba);
272         a = NR_RGBA32_A (rgba);
273         guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
274         p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
275         p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
276         p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
277 #ifdef SAFE_SETPIXEL
278     }
279 #endif
282 /**
283     \brief  This function renders a line on a particular canvas buffer,
284             using Bresenham's line drawing function.
285             http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
286             Coordinates are interpreted as SCREENcoordinates
287 */
288 static void
289 sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
291     int dy = y1 - y0;
292     int dx = x1 - x0;
293     int stepx, stepy;
295     if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
296     if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
297     dy <<= 1;                                                  // dy is now 2*dy
298     dx <<= 1;                                                  // dx is now 2*dx
300     sp_guideline_setpixel(buf, x0, y0, rgba);
301     if (dx > dy) {
302         int fraction = dy - (dx >> 1);                         // same as 2*dy - dx
303         while (x0 != x1) {
304             if (fraction >= 0) {
305                 y0 += stepy;
306                 fraction -= dx;                                // same as fraction -= 2*dx
307             }
308             x0 += stepx;
309             fraction += dy;                                    // same as fraction -= 2*dy
310             sp_guideline_setpixel(buf, x0, y0, rgba);
311         }
312     } else {
313         int fraction = dx - (dy >> 1);
314         while (y0 != y1) {
315             if (fraction >= 0) {
316                 x0 += stepx;
317                 fraction -= dy;
318             }
319             y0 += stepy;
320             fraction += dx;
321             sp_guideline_setpixel(buf, x0, y0, rgba);
322         }
323     }
326 /*
327   Local Variables:
328   mode:c++
329   c-file-style:"stroustrup"
330   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
331   indent-tabs-mode:nil
332   fill-column:99
333   End:
334 */
335 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :