1 #define CANVAS_AXONOMGRID_C
3 /*
4 * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
5 */
7 /*
8 * Current limits are: one axis (y-axis) is always vertical. The other two
9 * axes are bound to a certain range of angles. The z-axis always has an angle
10 * smaller than 90 degrees (measured from horizontal, 0 degrees being a line extending
11 * to the right). The x-axis will always have an angle between 0 and 90 degrees.
12 * When I quickly think about it: all possibilities are probably covered this way. Eg.
13 * a z-axis with negative angle can be replaced with an x-axis, etc.
14 */
16 /*
17 * TODO: LOTS LOTS LOTS. Clean up code. dirty as hell
18 * THIS FILE AND THE HEADER FILE NEED HUGE CLEANING UP. PLEASE DO NOT HESISTATE TO DO SO.
19 * For example: the line drawing code should not be here. There _must_ be a function somewhere else that can provide this functionality...
20 */
22 #include "sp-canvas-util.h"
23 #include "canvas-axonomgrid.h"
24 #include "display-forward.h"
25 #include <libnr/nr-pixops.h>
28 #include "canvas-grid.h"
29 #include "desktop-handles.h"
30 #include "helper/units.h"
31 #include "svg/svg-color.h"
32 #include "xml/node-event-vector.h"
33 #include "sp-object.h"
35 #include "sp-namedview.h"
36 #include "inkscape.h"
37 #include "desktop.h"
39 #include "../document.h"
41 #define SAFE_SETPIXEL //undefine this when it is certain that setpixel is never called with invalid params
43 enum Dim3 { X=0, Y, Z };
45 #ifndef M_PI
46 #define M_PI 3.14159265358979323846
47 #endif
49 static double deg_to_rad(double deg) { return deg*M_PI/180.0;}
52 /**
53 \brief This function renders a pixel on a particular buffer.
55 The topleft of the buffer equals
56 ( rect.x0 , rect.y0 ) in screen coordinates
57 ( 0 , 0 ) in setpixel coordinates
58 The bottomright of the buffer equals
59 ( rect.x1 , rect,y1 ) in screen coordinates
60 ( rect.x1 - rect.x0 , rect.y1 - rect.y0 ) in setpixel coordinates
61 */
62 static void
63 sp_caxonomgrid_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba) {
64 #ifdef SAFE_SETPIXEL
65 if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
66 #endif
67 guint r, g, b, a;
68 r = NR_RGBA32_R (rgba);
69 g = NR_RGBA32_G (rgba);
70 b = NR_RGBA32_B (rgba);
71 a = NR_RGBA32_A (rgba);
72 guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
73 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
74 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
75 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
76 #ifdef SAFE_SETPIXEL
77 }
78 #endif
79 }
81 /**
82 \brief This function renders a line on a particular canvas buffer,
83 using Bresenham's line drawing function.
84 http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
85 Coordinates are interpreted as SCREENcoordinates
86 */
87 static void
88 sp_caxonomgrid_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba) {
89 int dy = y1 - y0;
90 int dx = x1 - x0;
91 int stepx, stepy;
93 if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
94 if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
95 dy <<= 1; // dy is now 2*dy
96 dx <<= 1; // dx is now 2*dx
98 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
99 if (dx > dy) {
100 int fraction = dy - (dx >> 1); // same as 2*dy - dx
101 while (x0 != x1) {
102 if (fraction >= 0) {
103 y0 += stepy;
104 fraction -= dx; // same as fraction -= 2*dx
105 }
106 x0 += stepx;
107 fraction += dy; // same as fraction -= 2*dy
108 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
109 }
110 } else {
111 int fraction = dx - (dy >> 1);
112 while (y0 != y1) {
113 if (fraction >= 0) {
114 x0 += stepx;
115 fraction -= dy;
116 }
117 y0 += stepy;
118 fraction += dx;
119 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
120 }
121 }
123 }
125 static void
126 sp_grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
127 {
128 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
129 guint r, g, b, a;
130 gint y0, y1, y;
131 guchar *p;
132 r = NR_RGBA32_R(rgba);
133 g = NR_RGBA32_G (rgba);
134 b = NR_RGBA32_B (rgba);
135 a = NR_RGBA32_A (rgba);
136 y0 = MAX (buf->rect.y0, ys);
137 y1 = MIN (buf->rect.y1, ye + 1);
138 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
139 for (y = y0; y < y1; y++) {
140 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
141 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
142 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
143 p += buf->buf_rowstride;
144 }
145 }
146 }
148 namespace Inkscape {
151 /**
152 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
153 *
154 * Helper function that attachs widgets in a 3xn table. The widgets come in an
155 * array that has two entries per table row. The two entries code for four
156 * possible cases: (0,0) means insert space in first column; (0, non-0) means
157 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
158 * (non-0, non-0) means two widgets in columns 2 and 3.
159 **/
160 #define SPACE_SIZE_X 15
161 #define SPACE_SIZE_Y 10
162 static inline void
163 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
164 {
165 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
166 {
167 if (arr[i] && arr[i+1])
168 {
169 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
170 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
171 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
172 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
173 }
174 else
175 {
176 if (arr[i+1])
177 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
178 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
179 else if (arr[i])
180 {
181 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
182 label.set_alignment (0.0);
183 table.attach (label, 0, 3, r, r+1,
184 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
185 }
186 else
187 {
188 Gtk::HBox *space = manage (new Gtk::HBox);
189 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
190 table.attach (*space, 0, 1, r, r+1,
191 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
192 }
193 }
194 ++r;
195 }
196 }
198 CanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
199 : CanvasGrid(nv, in_repr, in_doc), table(1, 1)
200 {
202 origin[NR::X] = origin[NR::Y] = 0.0;
203 // nv->gridcolor = (nv->gridcolor & 0xff) | (DEFAULTGRIDCOLOR & 0xffffff00);
204 // case SP_ATTR_GRIDOPACITY:
205 // nv->gridcolor = (nv->gridcolor & 0xffffff00) | (DEFAULTGRIDCOLOR & 0xff);
206 color = 0xff3f3f20;
207 empcolor = 0xFF3F3F40;
208 empspacing = 5;
209 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
210 angle_deg[X] = angle_deg[Z] = 30;
211 angle_deg[Y] =0;
212 lengthy = 1;
214 angle_rad[X] = deg_to_rad(angle_deg[X]);
215 tan_angle[X] = tan(angle_rad[X]);
216 angle_rad[Z] = deg_to_rad(angle_deg[Z]);
217 tan_angle[Z] = tan(angle_rad[Z]);
219 snapper = new CanvasAxonomGridSnapper(this, namedview, 0);
221 // initialize widgets:
222 vbox.set_border_width(2);
223 table.set_spacings(2);
224 vbox.pack_start(table, false, false, 0);
226 _rumg.init (_("Grid _units:"), "units", _wr, repr, doc);
227 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
228 "originx", _rumg, _wr, repr, doc);
229 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
230 "originy", _rumg, _wr, repr, doc);
231 _rsu_sy.init (_("Spacing _Y:"), _("Base length of z-axis"),
232 "spacingy", _rumg, _wr, repr, doc);
233 _rsu_ax.init (_("Angle X:"), _("Angle of x-axis"),
234 "gridanglex", _rumg, _wr, repr, doc);
235 _rsu_az.init (_("Angle Z:"), _("Angle of z-axis"),
236 "gridanglez", _rumg, _wr, repr, doc);
237 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
238 _("Color of grid lines"), "color", "opacity", _wr, repr, doc);
239 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
240 _("Color of the major (highlighted) grid lines"),
241 "empcolor", "empopacity", _wr, repr, doc);
242 _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr, doc);
244 const Gtk::Widget* widget_array[] =
245 {
246 0, _rcbgrid._button,
247 _rumg._label, _rumg._sel,
248 0, _rsu_ox.getSU(),
249 0, _rsu_oy.getSU(),
250 0, _rsu_sy.getSU(),
251 0, _rsu_ax.getSU(),
252 0, _rsu_az.getSU(),
253 _rcp_gcol._label, _rcp_gcol._cp,
254 0, 0,
255 _rcp_gmcol._label, _rcp_gmcol._cp,
256 _rsi._label, &_rsi._hbox,
257 };
259 attach_all (table, widget_array, sizeof(widget_array));
261 vbox.show();
263 if (repr) readRepr();
264 updateWidgets();
265 }
267 CanvasAxonomGrid::~CanvasAxonomGrid ()
268 {
269 if (snapper) delete snapper;
270 }
273 /* fixme: Collect all these length parsing methods and think common sane API */
275 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
276 {
277 if (!str) {
278 return FALSE;
279 }
281 gchar *u;
282 gdouble v = g_ascii_strtod(str, &u);
283 if (!u) {
284 return FALSE;
285 }
286 while (isspace(*u)) {
287 u += 1;
288 }
290 if (!*u) {
291 /* No unit specified - keep default */
292 *val = v;
293 return TRUE;
294 }
296 if (base & SP_UNIT_DEVICE) {
297 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
298 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
299 *val = v;
300 return TRUE;
301 }
302 }
304 if (base & SP_UNIT_ABSOLUTE) {
305 if (!strncmp(u, "pt", 2)) {
306 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
307 } else if (!strncmp(u, "mm", 2)) {
308 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
309 } else if (!strncmp(u, "cm", 2)) {
310 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
311 } else if (!strncmp(u, "m", 1)) {
312 *unit = &sp_unit_get_by_id(SP_UNIT_M);
313 } else if (!strncmp(u, "in", 2)) {
314 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
315 } else {
316 return FALSE;
317 }
318 *val = v;
319 return TRUE;
320 }
322 return FALSE;
323 }
325 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
326 {
327 if (!str) {
328 return FALSE;
329 }
331 gchar *u;
332 gdouble v = g_ascii_strtod(str, &u);
333 if (!u) {
334 return FALSE;
335 }
336 v = CLAMP(v, 0.0, 1.0);
338 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
340 return TRUE;
341 }
345 void
346 CanvasAxonomGrid::readRepr()
347 {
348 gchar const* value;
349 if ( (value = repr->attribute("originx")) ) {
350 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
351 origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
352 }
353 if ( (value = repr->attribute("originy")) ) {
354 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
355 origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
356 }
358 if ( (value = repr->attribute("spacingy")) ) {
359 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &lengthy, &gridunit);
360 lengthy = sp_units_get_pixels(lengthy, *(gridunit));
361 if (lengthy < 1.0) lengthy = 1.0;
362 }
364 if ( (value = repr->attribute("gridanglex")) ) {
365 angle_deg[X] = g_ascii_strtod(value, NULL);
366 if (angle_deg[X] < 1.0) angle_deg[X] = 1.0;
367 if (angle_deg[X] > 89.0) angle_deg[X] = 89.0;
368 angle_rad[X] = deg_to_rad(angle_deg[X]);
369 tan_angle[X] = tan(angle_rad[X]);
370 }
372 if ( (value = repr->attribute("gridanglez")) ) {
373 angle_deg[Z] = g_ascii_strtod(value, NULL);
374 if (angle_deg[Z] < 1.0) angle_deg[Z] = 1.0;
375 if (angle_deg[Z] > 89.0) angle_deg[Z] = 89.0;
376 angle_rad[Z] = deg_to_rad(angle_deg[Z]);
377 tan_angle[Z] = tan(angle_rad[Z]);
378 }
380 if ( (value = repr->attribute("color")) ) {
381 color = (color & 0xff) | sp_svg_read_color(value, color);
382 }
384 if ( (value = repr->attribute("empcolor")) ) {
385 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
386 }
388 if ( (value = repr->attribute("opacity")) ) {
389 sp_nv_read_opacity(value, &color);
390 }
391 if ( (value = repr->attribute("empopacity")) ) {
392 sp_nv_read_opacity(value, &empcolor);
393 }
395 if ( (value = repr->attribute("empspacing")) ) {
396 empspacing = atoi(value);
397 }
399 for (GSList *l = canvasitems; l != NULL; l = l->next) {
400 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
401 }
402 return;
403 }
405 /**
406 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
407 */
408 void
409 CanvasAxonomGrid::onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive)
410 {
411 readRepr();
413 if ( ! (_wr.isUpdating()) )
414 updateWidgets();
415 }
420 Gtk::Widget &
421 CanvasAxonomGrid::getWidget()
422 {
423 return vbox;
424 }
427 /**
428 * Update dialog widgets from object's values.
429 */
430 void
431 CanvasAxonomGrid::updateWidgets()
432 {
433 if (_wr.isUpdating()) return;
435 _wr.setUpdating (true);
437 // _rrb_gridtype.setValue (nv->gridtype);
438 _rumg.setUnit (gridunit);
440 gdouble val;
441 val = origin[NR::X];
442 val = sp_pixels_get_units (val, *(gridunit));
443 _rsu_ox.setValue (val);
444 val = origin[NR::Y];
445 val = sp_pixels_get_units (val, *(gridunit));
446 _rsu_oy.setValue (val);
447 val = lengthy;
448 double gridy = sp_pixels_get_units (val, *(gridunit));
449 _rsu_sy.setValue (gridy);
451 _rsu_ax.setValue(angle_deg[X]);
452 _rsu_az.setValue(angle_deg[Z]);
454 _rcp_gcol.setRgba32 (color);
455 _rcp_gmcol.setRgba32 (empcolor);
456 _rsi.setValue (empspacing);
458 _wr.setUpdating (false);
460 return;
461 }
465 void
466 CanvasAxonomGrid::Update (NR::Matrix const &affine, unsigned int flags)
467 {
468 ow = origin * affine;
469 sw = NR::Point(fabs(affine[0]),fabs(affine[3]));
471 for(int dim = 0; dim < 2; dim++) {
472 gint scaling_factor = empspacing;
474 if (scaling_factor <= 1)
475 scaling_factor = 5;
477 scaled = FALSE;
478 int watchdog = 0;
479 while ( (sw[dim] < 8.0) & (watchdog < 100) ) {
480 scaled = TRUE;
481 sw[dim] *= scaling_factor;
482 // First pass, go up to the major line spacing, then
483 // keep increasing by two.
484 scaling_factor = 2;
485 watchdog++;
486 }
488 }
490 spacing_ylines = sw[NR::X] * lengthy /(tan_angle[X] + tan_angle[Z]);
491 lyw = lengthy * sw[NR::Y];
492 lxw_x = (lengthy / tan_angle[X]) * sw[NR::X];
493 lxw_z = (lengthy / tan_angle[Z]) * sw[NR::X];
495 if (empspacing == 0) {
496 scaled = TRUE;
497 }
499 }
501 void
502 CanvasAxonomGrid::Render (SPCanvasBuf *buf)
503 {
504 // gc = gridcoordinates (the coordinates calculated from the grids origin 'grid->ow'.
505 // sc = screencoordinates ( for example "buf->rect.x0" is in screencoordinates )
506 // bc = buffer patch coordinates
508 // tl = topleft ; br = bottomright
509 NR::Point buf_tl_gc;
510 NR::Point buf_br_gc;
511 buf_tl_gc[NR::X] = buf->rect.x0 - ow[NR::X];
512 buf_tl_gc[NR::Y] = buf->rect.y0 - ow[NR::Y];
513 buf_br_gc[NR::X] = buf->rect.x1 - ow[NR::X];
514 buf_br_gc[NR::Y] = buf->rect.y1 - ow[NR::Y];
516 gdouble x;
517 gdouble y;
519 // render the three separate line groups representing the main-axes:
520 // x-axis always goes from topleft to bottomright. (0,0) - (1,1)
521 const gdouble xintercept_y_bc = (buf_tl_gc[NR::X] * tan_angle[X]) - buf_tl_gc[NR::Y] ;
522 const gdouble xstart_y_sc = ( xintercept_y_bc - floor(xintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
523 const gint xlinestart = (gint) Inkscape::round( (xstart_y_sc - ow[NR::Y]) / lyw );
524 gint xlinenum;
525 // lijnen vanaf linker zijkant.
526 for (y = xstart_y_sc, xlinenum = xlinestart; y < buf->rect.y1; y += lyw, xlinenum++) {
527 const gint x0 = buf->rect.x0;
528 const gint y0 = (gint) Inkscape::round(y);
529 const gint x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - y) / tan_angle[X] );
530 const gint y1 = buf->rect.y1;
532 if (!scaled && (xlinenum % empspacing) == 0) {
533 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
534 } else {
535 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
536 }
537 }
538 // lijnen vanaf bovenkant.
539 const gdouble xstart_x_sc = buf->rect.x0 + (lxw_x - (xstart_y_sc - buf->rect.y0) / tan_angle[X]) ;
540 for (x = xstart_x_sc, xlinenum = xlinestart; x < buf->rect.x1; x += lxw_x, xlinenum--) {
541 const gint y0 = buf->rect.y0;
542 const gint y1 = buf->rect.y1;
543 const gint x0 = (gint) Inkscape::round(x);
544 const gint x1 = x0 + (gint) Inkscape::round( (y1 - y0) / tan_angle[X] );
546 if (!scaled && (xlinenum % empspacing) == 0) {
547 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
548 } else {
549 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
550 }
551 }
554 // y-axis lines (vertical)
555 const gdouble ystart_x_sc = floor (buf_tl_gc[NR::X] / spacing_ylines) * spacing_ylines + ow[NR::X];
556 const gint ylinestart = (gint) Inkscape::round((ystart_x_sc - ow[NR::X]) / spacing_ylines);
557 gint ylinenum;
558 for (x = ystart_x_sc, ylinenum = ylinestart; x < buf->rect.x1; x += spacing_ylines, ylinenum++) {
559 const gint x0 = (gint) Inkscape::round(x);
561 if (!scaled && (ylinenum % empspacing) == 0) {
562 sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, empcolor);
563 } else {
564 sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, color);
565 }
566 }
568 // z-axis always goes from bottomleft to topright. (0,1) - (1,0)
569 const gdouble zintercept_y_bc = (buf_tl_gc[NR::X] * -tan_angle[Z]) - buf_tl_gc[NR::Y] ;
570 const gdouble zstart_y_sc = ( zintercept_y_bc - floor(zintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
571 const gint zlinestart = (gint) Inkscape::round( (zstart_y_sc - ow[NR::Y]) / lyw );
572 gint zlinenum;
573 // lijnen vanaf linker zijkant.
574 for (y = zstart_y_sc, zlinenum = zlinestart; y < buf->rect.y1; y += lyw, zlinenum++) {
575 const gint x0 = buf->rect.x0;
576 const gint y0 = (gint) Inkscape::round(y);
577 const gint x1 = x0 + (gint) Inkscape::round( (y - buf->rect.y0 ) / tan_angle[Z] );
578 const gint y1 = buf->rect.y0;
580 if (!scaled && (zlinenum % empspacing) == 0) {
581 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
582 } else {
583 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
584 }
585 }
586 // draw lines from bottom-up
587 const gdouble zstart_x_sc = buf->rect.x0 + (y - buf->rect.y1) / tan_angle[Z] ;
588 for (x = zstart_x_sc; x < buf->rect.x1; x += lxw_z, zlinenum--) {
589 const gint y0 = buf->rect.y1;
590 const gint y1 = buf->rect.y0;
591 const gint x0 = (gint) Inkscape::round(x);
592 const gint x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - buf->rect.y0) / tan_angle[Z] );
594 if (!scaled && (zlinenum % empspacing) == 0) {
595 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
596 } else {
597 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
598 }
599 }
600 }
613 /**
614 * \return x rounded to the nearest multiple of c1 plus c0.
615 *
616 * \note
617 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
618 * mean "ignore the grid in this dimention". We're currently discussing "good" semantics
619 * for guide/grid snapping.
620 */
622 /* FIXME: move this somewhere else, perhaps */
623 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
624 {
625 return floor((x - c0) / c1 + .5) * c1 + c0;
626 }
628 CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
629 {
630 this->grid = grid;
631 }
633 LineSnapper::LineList
634 CanvasAxonomGridSnapper::_getSnapLines(NR::Point const &p) const
635 {
636 LineList s;
638 if ( grid == NULL ) {
639 return s;
640 }
642 for (unsigned int i = 0; i < 2; ++i) {
644 /* This is to make sure we snap to only visible grid lines */
645 double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
647 // convert screen pixels to px
648 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
649 if (SP_ACTIVE_DESKTOP) {
650 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
651 }
653 NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
654 scaled_spacing,
655 grid->origin[i]);
657 s.push_back(std::make_pair(NR::Dim2(i), rounded));
658 }
660 return s;
661 }
664 }; // namespace Inkscape
667 /*
668 Local Variables:
669 mode:c++
670 c-file-style:"stroustrup"
671 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
672 indent-tabs-mode:nil
673 fill-column:99
674 End:
675 */
676 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :