33
37
static GType guideline_type = 0;
35
39
if (!guideline_type) {
36
static const GTypeInfo guideline_info =
38
sizeof (SPGuideLineClass),
40
(GClassInitFunc) sp_guideline_class_init,
44
(GInstanceInitFunc) sp_guideline_init,
40
static GTypeInfo const guideline_info = {
41
sizeof (SPGuideLineClass),
43
(GClassInitFunc) sp_guideline_class_init,
47
(GInstanceInitFunc) sp_guideline_init,
48
51
guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
51
54
return guideline_type;
54
57
static void sp_guideline_class_init(SPGuideLineClass *c)
56
parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
58
GtkObjectClass *object_class = (GtkObjectClass *) c;
59
object_class->destroy = sp_guideline_destroy;
61
SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
62
item_class->update = sp_guideline_update;
63
item_class->render = sp_guideline_render;
64
item_class->point = sp_guideline_point;
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;
67
70
static void sp_guideline_init(SPGuideLine *gl)
69
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);
80
85
static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
82
87
SPGuideLine const *gl = SP_GUIDELINE (item);
84
89
sp_canvas_prepare_buffer(buf);
86
91
unsigned int const r = NR_RGBA32_R (gl->rgba);
87
92
unsigned int const g = NR_RGBA32_G (gl->rgba);
88
93
unsigned int const b = NR_RGBA32_B (gl->rgba);
89
94
unsigned int const a = NR_RGBA32_A (gl->rgba);
96
if (gl->position < buf->rect.x0 || gl->position >= buf->rect.x1) {
102
step = buf->buf_rowstride;
103
d = buf->buf + 3 * (gl->position - buf->rect.x0);
96
if (gl->is_vertical()) {
97
int position = gl->point_on_line[Geom::X];
98
if (position < buf->rect.x0 || position >= buf->rect.x1) {
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]);
113
} else if (gl->is_horizontal()) {
114
int position = gl->point_on_line[Geom::Y];
115
if (position < buf->rect.y0 || position >= buf->rect.y1) {
119
int p0 = buf->rect.x0;
120
int p1 = buf->rect.x1;
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]);
107
if (gl->position < buf->rect.y0 || gl->position >= buf->rect.y1) {
114
d = buf->buf + (gl->position - buf->rect.y0) * buf->buf_rowstride;
117
for (int p = p0; p < p1; p++) {
118
d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
119
d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
120
d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
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);
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);
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);
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);
130
176
((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
134
gl->position = (int) (affine[4] + 0.5);
135
sp_canvas_update_bbox (item, gl->position, -1000000, gl->position + 1, 1000000);
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->is_horizontal()) {
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->is_vertical()) {
185
sp_canvas_update_bbox (item, gl->point_on_line[Geom::X], -1000000, gl->point_on_line[Geom::X]+1, 1000000);
137
gl->position = (int) (affine[5] - 0.5);
138
sp_canvas_update_bbox (item, -1000000, gl->position, 1000000, gl->position + 1);
187
sp_canvas_update_bbox (item, -1000000, -1000000, 1000000, 1000000);
191
// Returns 0.0 if point is on the guideline
142
192
static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
144
194
SPGuideLine *gl = SP_GUIDELINE (item);
150
200
*actual_item = item;
153
return MAX(fabs(gl->position - p[NR::X])-1, 0);
155
return MAX(fabs(gl->position - p[NR::Y])-1, 0);
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);
159
SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, double position, unsigned int vertical)
207
SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, Geom::Point point_on_line, Geom::Point normal)
161
209
SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
163
211
SPGuideLine *gl = SP_GUIDELINE(item);
165
gl->vertical = vertical;
166
sp_guideline_set_position(gl, position);
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);
171
void sp_guideline_set_position(SPGuideLine *gl, double position)
173
sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl),
174
NR::Matrix(NR::translate(position, position)));
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));
177
235
void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
186
244
gl->sensitive = sensitive;
247
//##########################################################
249
#define SAFE_SETPIXEL //undefine this when it is certain that setpixel is never called with invalid params
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
262
sp_guideline_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
265
if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
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]);
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
288
sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
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);
301
int fraction = dy - (dx >> 1); // same as 2*dy - dx
305
fraction -= dx; // same as fraction -= 2*dx
308
fraction += dy; // same as fraction -= 2*dy
309
sp_guideline_setpixel(buf, x0, y0, rgba);
312
int fraction = dx - (dy >> 1);
320
sp_guideline_setpixel(buf, x0, y0, rgba);