Code

noop: CodingStyle: const placement
[inkscape.git] / src / display / canvas-axonomgrid.cpp
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     }
125 static void
126 sp_grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
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     }
148 namespace Inkscape {
151 /**
152 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP  TO QUICKLY GET RESULTS
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, Gtk::Widget const *const arr[], unsigned size, int start = 0)
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     }
198 CanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
199     : CanvasGrid(nv, in_repr, in_doc), table(1, 1)
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     Gtk::Widget const *const widget_array[] = {
245         0,                  _rcbgrid._button,
246         _rumg._label,       _rumg._sel,
247         0,                  _rsu_ox.getSU(),
248         0,                  _rsu_oy.getSU(),
249         0,                  _rsu_sy.getSU(),
250         0,                  _rsu_ax.getSU(),
251         0,                  _rsu_az.getSU(),
252         _rcp_gcol._label,   _rcp_gcol._cp,
253         0,                  0,
254         _rcp_gmcol._label,  _rcp_gmcol._cp,
255         _rsi._label,        &_rsi._hbox,
256     };
258     attach_all (table, widget_array, sizeof(widget_array));
260     vbox.show();
262     if (repr) readRepr();
263     updateWidgets();
266 CanvasAxonomGrid::~CanvasAxonomGrid ()
268    if (snapper) delete snapper;
272 /* fixme: Collect all these length parsing methods and think common sane API */
274 static gboolean sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
276     if (!str) {
277         return FALSE;
278     }
280     gchar *u;
281     gdouble v = g_ascii_strtod(str, &u);
282     if (!u) {
283         return FALSE;
284     }
285     while (isspace(*u)) {
286         u += 1;
287     }
289     if (!*u) {
290         /* No unit specified - keep default */
291         *val = v;
292         return TRUE;
293     }
295     if (base & SP_UNIT_DEVICE) {
296         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
297             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
298             *val = v;
299             return TRUE;
300         }
301     }
303     if (base & SP_UNIT_ABSOLUTE) {
304         if (!strncmp(u, "pt", 2)) {
305             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
306         } else if (!strncmp(u, "mm", 2)) {
307             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
308         } else if (!strncmp(u, "cm", 2)) {
309             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
310         } else if (!strncmp(u, "m", 1)) {
311             *unit = &sp_unit_get_by_id(SP_UNIT_M);
312         } else if (!strncmp(u, "in", 2)) {
313             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
314         } else {
315             return FALSE;
316         }
317         *val = v;
318         return TRUE;
319     }
321     return FALSE;
324 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
326     if (!str) {
327         return FALSE;
328     }
330     gchar *u;
331     gdouble v = g_ascii_strtod(str, &u);
332     if (!u) {
333         return FALSE;
334     }
335     v = CLAMP(v, 0.0, 1.0);
337     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
339     return TRUE;
344 void
345 CanvasAxonomGrid::readRepr()
347     gchar const *value;
348     if ( (value = repr->attribute("originx")) ) {
349         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
350         origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
351     }
352     if ( (value = repr->attribute("originy")) ) {
353         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
354         origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
355     }
357     if ( (value = repr->attribute("spacingy")) ) {
358         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &lengthy, &gridunit);
359         lengthy = sp_units_get_pixels(lengthy, *(gridunit));
360         if (lengthy < 1.0) lengthy = 1.0;
361     }
363     if ( (value = repr->attribute("gridanglex")) ) {
364         angle_deg[X] = g_ascii_strtod(value, NULL);
365         if (angle_deg[X] < 1.0) angle_deg[X] = 1.0;
366         if (angle_deg[X] > 89.0) angle_deg[X] = 89.0;
367         angle_rad[X] = deg_to_rad(angle_deg[X]);
368         tan_angle[X] = tan(angle_rad[X]);
369     }
371     if ( (value = repr->attribute("gridanglez")) ) {
372         angle_deg[Z] = g_ascii_strtod(value, NULL);
373         if (angle_deg[Z] < 1.0) angle_deg[Z] = 1.0;
374         if (angle_deg[Z] > 89.0) angle_deg[Z] = 89.0;
375         angle_rad[Z] = deg_to_rad(angle_deg[Z]);
376         tan_angle[Z] = tan(angle_rad[Z]);
377     }
379     if ( (value = repr->attribute("color")) ) {
380         color = (color & 0xff) | sp_svg_read_color(value, color);
381     }
383     if ( (value = repr->attribute("empcolor")) ) {
384         empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
385     }
387     if ( (value = repr->attribute("opacity")) ) {
388         sp_nv_read_opacity(value, &color);
389     }
390     if ( (value = repr->attribute("empopacity")) ) {
391         sp_nv_read_opacity(value, &empcolor);
392     }
394     if ( (value = repr->attribute("empspacing")) ) {
395         empspacing = atoi(value);
396     }
398     for (GSList *l = canvasitems; l != NULL; l = l->next) {
399         sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
400     }
401     return;
404 /**
405  * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
406  */
407 void
408 CanvasAxonomGrid::onReprAttrChanged(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive)
410     readRepr();
412     if ( ! (_wr.isUpdating()) )
413         updateWidgets();
419 Gtk::Widget &
420 CanvasAxonomGrid::getWidget()
422     return vbox;
426 /**
427  * Update dialog widgets from object's values.
428  */
429 void
430 CanvasAxonomGrid::updateWidgets()
432     if (_wr.isUpdating()) return;
434     _wr.setUpdating (true);
436 //    _rrb_gridtype.setValue (nv->gridtype);
437     _rumg.setUnit (gridunit);
439     gdouble val;
440     val = origin[NR::X];
441     val = sp_pixels_get_units (val, *(gridunit));
442     _rsu_ox.setValue (val);
443     val = origin[NR::Y];
444     val = sp_pixels_get_units (val, *(gridunit));
445     _rsu_oy.setValue (val);
446     val = lengthy;
447     double gridy = sp_pixels_get_units (val, *(gridunit));
448     _rsu_sy.setValue (gridy);
450     _rsu_ax.setValue(angle_deg[X]);
451     _rsu_az.setValue(angle_deg[Z]);
453     _rcp_gcol.setRgba32 (color);
454     _rcp_gmcol.setRgba32 (empcolor);
455     _rsi.setValue (empspacing);
457     _wr.setUpdating (false);
459     return;
464 void
465 CanvasAxonomGrid::Update (NR::Matrix const &affine, unsigned int flags)
467     ow = origin * affine;
468     sw = NR::Point(fabs(affine[0]),fabs(affine[3]));
469     
470     for(int dim = 0; dim < 2; dim++) {
471         gint scaling_factor = empspacing;
473         if (scaling_factor <= 1)
474             scaling_factor = 5;
476         scaled = FALSE;
477         int watchdog = 0;
478         while (  (sw[dim] < 8.0) & (watchdog < 100) ) {
479             scaled = TRUE;
480             sw[dim] *= scaling_factor;
481             // First pass, go up to the major line spacing, then
482             // keep increasing by two.
483             scaling_factor = 2;
484             watchdog++;
485         }
487     }
489     spacing_ylines = sw[NR::X] * lengthy  /(tan_angle[X] + tan_angle[Z]);
490     lyw            = lengthy * sw[NR::Y];
491     lxw_x          = (lengthy / tan_angle[X]) * sw[NR::X];
492     lxw_z          = (lengthy / tan_angle[Z]) * sw[NR::X];
494     if (empspacing == 0) {
495         scaled = TRUE;
496     }
500 void
501 CanvasAxonomGrid::Render (SPCanvasBuf *buf)
503      // gc = gridcoordinates (the coordinates calculated from the grids origin 'grid->ow'.
504      // sc = screencoordinates ( for example "buf->rect.x0" is in screencoordinates )
505      // bc = buffer patch coordinates
507      // tl = topleft ; br = bottomright
508     NR::Point buf_tl_gc;
509     NR::Point buf_br_gc;
510     buf_tl_gc[NR::X] = buf->rect.x0 - ow[NR::X];
511     buf_tl_gc[NR::Y] = buf->rect.y0 - ow[NR::Y];
512     buf_br_gc[NR::X] = buf->rect.x1 - ow[NR::X];
513     buf_br_gc[NR::Y] = buf->rect.y1 - ow[NR::Y];
515     gdouble x;
516     gdouble y;
518     // render the three separate line groups representing the main-axes:
519     // x-axis always goes from topleft to bottomright. (0,0) - (1,1)
520     gdouble const xintercept_y_bc = (buf_tl_gc[NR::X] * tan_angle[X]) - buf_tl_gc[NR::Y] ;
521     gdouble const xstart_y_sc = ( xintercept_y_bc - floor(xintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
522     gint const  xlinestart = (gint) Inkscape::round( (xstart_y_sc - ow[NR::Y]) / lyw );
523     gint xlinenum;
524     // lijnen vanaf linker zijkant.
525     for (y = xstart_y_sc, xlinenum = xlinestart; y < buf->rect.y1; y += lyw, xlinenum++) {
526         gint const x0 = buf->rect.x0;
527         gint const y0 = (gint) Inkscape::round(y);
528         gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - y) / tan_angle[X] );
529         gint const y1 = buf->rect.y1;
531         if (!scaled && (xlinenum % empspacing) == 0) {
532             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
533         } else {
534             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
535         }
536     }
537     // lijnen vanaf bovenkant.
538     gdouble const xstart_x_sc = buf->rect.x0 + (lxw_x - (xstart_y_sc - buf->rect.y0) / tan_angle[X]) ;
539     for (x = xstart_x_sc, xlinenum = xlinestart; x < buf->rect.x1; x += lxw_x, xlinenum--) {
540         gint const y0 = buf->rect.y0;
541         gint const y1 = buf->rect.y1;
542         gint const x0 = (gint) Inkscape::round(x);
543         gint const x1 = x0 + (gint) Inkscape::round( (y1 - y0) / tan_angle[X] );
545         if (!scaled && (xlinenum % empspacing) == 0) {
546             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
547         } else {
548             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
549         }
550     }
553     // y-axis lines (vertical)
554     gdouble const ystart_x_sc = floor (buf_tl_gc[NR::X] / spacing_ylines) * spacing_ylines + ow[NR::X];
555     gint const  ylinestart = (gint) Inkscape::round((ystart_x_sc - ow[NR::X]) / spacing_ylines);
556     gint ylinenum;
557     for (x = ystart_x_sc, ylinenum = ylinestart; x < buf->rect.x1; x += spacing_ylines, ylinenum++) {
558         gint const x0 = (gint) Inkscape::round(x);
560         if (!scaled && (ylinenum % empspacing) == 0) {
561             sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, empcolor);
562         } else {
563             sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, color);
564         }
565     }
567     // z-axis always goes from bottomleft to topright. (0,1) - (1,0)
568     gdouble const zintercept_y_bc = (buf_tl_gc[NR::X] * -tan_angle[Z]) - buf_tl_gc[NR::Y] ;
569     gdouble const zstart_y_sc = ( zintercept_y_bc - floor(zintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
570     gint const  zlinestart = (gint) Inkscape::round( (zstart_y_sc - ow[NR::Y]) / lyw );
571     gint zlinenum;
572     // lijnen vanaf linker zijkant.
573     for (y = zstart_y_sc, zlinenum = zlinestart; y < buf->rect.y1; y += lyw, zlinenum++) {
574         gint const x0 = buf->rect.x0;
575         gint const y0 = (gint) Inkscape::round(y);
576         gint const x1 = x0 + (gint) Inkscape::round( (y - buf->rect.y0 ) / tan_angle[Z] );
577         gint const y1 = buf->rect.y0;
579         if (!scaled && (zlinenum % empspacing) == 0) {
580             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
581         } else {
582             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
583         }
584     }
585     // draw lines from bottom-up
586     gdouble const zstart_x_sc = buf->rect.x0 + (y - buf->rect.y1) / tan_angle[Z] ;
587     for (x = zstart_x_sc; x < buf->rect.x1; x += lxw_z, zlinenum--) {
588         gint const y0 = buf->rect.y1;
589         gint const y1 = buf->rect.y0;
590         gint const x0 = (gint) Inkscape::round(x);
591         gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - buf->rect.y0) / tan_angle[Z] );
593         if (!scaled && (zlinenum % empspacing) == 0) {
594             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
595         } else {
596             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
597         }
598     }
612 /**
613  * \return x rounded to the nearest multiple of c1 plus c0.
614  *
615  * \note
616  * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero
617  * mean "ignore the grid in this dimention".  We're currently discussing "good" semantics
618  * for guide/grid snapping.
619  */
621 /* FIXME: move this somewhere else, perhaps */
622 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
624     return floor((x - c0) / c1 + .5) * c1 + c0;
627 CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
629     this->grid = grid;
632 LineSnapper::LineList
633 CanvasAxonomGridSnapper::_getSnapLines(NR::Point const &p) const
635     LineList s;
637     if ( grid == NULL ) {
638         return s;
639     }
641     for (unsigned int i = 0; i < 2; ++i) {
643         /* This is to make sure we snap to only visible grid lines */
644         double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
646         // convert screen pixels to px
647         // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
648         if (SP_ACTIVE_DESKTOP) {
649             scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
650         }
652         NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
653                                                                  scaled_spacing,
654                                                                  grid->origin[i]);
656         s.push_back(std::make_pair(NR::Dim2(i), rounded));
657     }
659     return s;
663 }; // namespace Inkscape
666 /*
667   Local Variables:
668   mode:c++
669   c-file-style:"stroustrup"
670   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
671   indent-tabs-mode:nil
672   fill-column:99
673   End:
674 */
675 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :