Code

Fix emphasized gridlines bug when zoomed out.
[inkscape.git] / src / display / canvas-grid.cpp
1 #define INKSCAPE_CANVAS_GRID_C
3 /*
4  *
5  * Copyright (C) Johan Engelen 2006-2007 <johan@shouraizou.nl>
6  * Copyright (C) Lauris Kaplinski 2000
7  *
8  */
11 #include "sp-canvas-util.h"
12 #include "display-forward.h"
13 #include <libnr/nr-pixops.h>
14 #include "desktop-handles.h"
15 #include "helper/units.h"
16 #include "svg/svg-color.h"
17 #include "xml/node-event-vector.h"
18 #include "sp-object.h"
20 #include "sp-namedview.h"
21 #include "inkscape.h"
22 #include "desktop.h"
24 #include "../document.h"
26 #include "canvas-grid.h"
27 #include "canvas-axonomgrid.h"
29 namespace Inkscape {
31 #define DEFAULTGRIDCOLOR    0x0000FF20
32 #define DEFAULTGRIDEMPCOLOR 0x0000FF40
35 static void grid_canvasitem_class_init (GridCanvasItemClass *klass);
36 static void grid_canvasitem_init (GridCanvasItem *grid);
37 static void grid_canvasitem_destroy (GtkObject *object);
39 static void grid_canvasitem_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
40 static void grid_canvasitem_render (SPCanvasItem *item, SPCanvasBuf *buf);
42 static SPCanvasItemClass * parent_class;
44 GtkType
45 grid_canvasitem_get_type (void)
46 {
47     static GtkType grid_canvasitem_type = 0;
49     if (!grid_canvasitem_type) {
50         GtkTypeInfo grid_canvasitem_info = {
51             "GridCanvasItem",
52             sizeof (GridCanvasItem),
53             sizeof (GridCanvasItemClass),
54             (GtkClassInitFunc) grid_canvasitem_class_init,
55             (GtkObjectInitFunc) grid_canvasitem_init,
56             NULL, NULL,
57             (GtkClassInitFunc) NULL
58         };
59         grid_canvasitem_type = gtk_type_unique (sp_canvas_item_get_type (), &grid_canvasitem_info);
60     }
61     return grid_canvasitem_type;
62 }
64 static void
65 grid_canvasitem_class_init (GridCanvasItemClass *klass)
66 {
67     GtkObjectClass *object_class;
68     SPCanvasItemClass *item_class;
70     object_class = (GtkObjectClass *) klass;
71     item_class = (SPCanvasItemClass *) klass;
73     parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
75     object_class->destroy = grid_canvasitem_destroy;
77     item_class->update = grid_canvasitem_update;
78     item_class->render = grid_canvasitem_render;
79 }
81 static void
82 grid_canvasitem_init (GridCanvasItem *griditem)
83 {
84     griditem->grid = NULL;
85 }
87 static void
88 grid_canvasitem_destroy (GtkObject *object)
89 {
90     g_return_if_fail (object != NULL);
91     g_return_if_fail (INKSCAPE_IS_GRID_CANVASITEM (object));
93     if (GTK_OBJECT_CLASS (parent_class)->destroy)
94         (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
95 }
97 /**
98 */
99 static void
100 grid_canvasitem_render (SPCanvasItem * item, SPCanvasBuf * buf)
102     GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
104     sp_canvas_prepare_buffer (buf);
106     if (gridcanvasitem->grid) gridcanvasitem->grid->Render(buf);
109 static void
110 grid_canvasitem_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
112     GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
114     if (parent_class->update)
115         (* parent_class->update) (item, affine, flags);
117     if (gridcanvasitem->grid) {
118         gridcanvasitem->grid->Update(affine, flags);
120         sp_canvas_request_redraw (item->canvas,
121                          -1000000, -1000000,
122                          1000000, 1000000);
124         item->x1 = item->y1 = -1000000;
125         item->x2 = item->y2 = 1000000;
126     }
131 // ##########################################################
132 //   CanvasGrid
134     static Inkscape::XML::NodeEventVector const _repr_events = {
135         NULL, /* child_added */
136         NULL, /* child_removed */
137         CanvasGrid::on_repr_attr_changed,
138         NULL, /* content_changed */
139         NULL  /* order_changed */
140     };
142 CanvasGrid::CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr)
144     repr = in_repr;
145     if (repr) {
146         repr->addListener (&_repr_events, this);
147     }
149     namedview = nv;
150     canvasitems = NULL;
153 CanvasGrid::~CanvasGrid()
155     if (repr) {
156         repr->removeListenerByData (this);
157     }
159     while (canvasitems) {
160         gtk_object_destroy(GTK_OBJECT(canvasitems->data));
161         canvasitems = g_slist_remove(canvasitems, canvasitems->data);
162     }
165 /*
166 *  writes an <inkscape:grid> child to repr.
167 */
168 void
169 CanvasGrid::writeNewGridToRepr(Inkscape::XML::Node * repr, const char * gridtype)
171     if (!repr) return;
172     if (!gridtype) return;
174     // first create the child xml node, then hook it to repr. This order is important, to not set off listeners to repr before the new node is complete.
176     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(sp_desktop_document(SP_ACTIVE_DESKTOP));
177     Inkscape::XML::Node *newnode;
178     newnode = xml_doc->createElement("inkscape:grid");
179     newnode->setAttribute("type",gridtype);
181     repr->appendChild(newnode);
183     // FIXME: add this to history?
184 //    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
185 //                     _("Create new element node"));
188 /*
189 * Creates a new CanvasGrid object of type gridtype
190 */
191 CanvasGrid*
192 CanvasGrid::NewGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, const char * gridtype)
194     if (!in_repr) return NULL;
195     if (!gridtype) return NULL;
197     if (!strcmp(gridtype,"xygrid")) {
198         return (CanvasGrid*) new CanvasXYGrid(nv, in_repr);
199     } else if (!strcmp(gridtype,"axonometric")) {
200         return (CanvasGrid*) new CanvasAxonomGrid(nv, in_repr);
201     }
203     return NULL;
207 /**
208 *  creates a new grid canvasitem for the SPDesktop given as parameter. Keeps a link to this canvasitem in the canvasitems list.
209 */
210 GridCanvasItem *
211 CanvasGrid::createCanvasItem(SPDesktop * desktop)
213     if (!desktop) return NULL;
214     //Johan: I think for multiple desktops it is best if each has their own canvasitem, but share the same CanvasGrid object; that is what this function is for.
216     // check if there is already a canvasitem on this desktop linking to this grid
217     for (GSList *l = canvasitems; l != NULL; l = l->next) {
218         if ( sp_desktop_gridgroup(desktop) == SP_CANVAS_GROUP(SP_CANVAS_ITEM(l->data)->parent) ) {
219             return NULL;
220         }
221     }
223     GridCanvasItem * item = INKSCAPE_GRID_CANVASITEM( sp_canvas_item_new(sp_desktop_gridgroup(desktop), INKSCAPE_TYPE_GRID_CANVASITEM, NULL) );
224     item->grid = this;
225     sp_canvas_item_show(SP_CANVAS_ITEM(item));
227     gtk_object_ref(GTK_OBJECT(item));    // since we're keeping a link to this item, we need to bump up the ref count
228     canvasitems = g_slist_prepend(canvasitems, item);
230     return item;
233 void
234 CanvasGrid::on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data)
236     if (!data)
237         return;
239     ((CanvasGrid*) data)->onReprAttrChanged(repr, key, oldval, newval, is_interactive);
244 // ##########################################################
245 //   CanvasXYGrid
248 /**
249 * "attach_all" function
250 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP  TO QUICKLY GET RESULTS
252  * Helper function that attachs widgets in a 3xn table. The widgets come in an
253  * array that has two entries per table row. The two entries code for four
254  * possible cases: (0,0) means insert space in first column; (0, non-0) means
255  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
256  * (non-0, non-0) means two widgets in columns 2 and 3.
257 **/
258 #define SPACE_SIZE_X 15
259 #define SPACE_SIZE_Y 10
260 static inline void
261 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
263     for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
264     {
265         if (arr[i] && arr[i+1])
266         {
267             table.attach (const_cast<Gtk::Widget&>(*arr[i]),   1, 2, r, r+1,
268                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
269             table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
270                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
271         }
272         else
273         {
274             if (arr[i+1])
275                 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
276                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
277             else if (arr[i])
278             {
279                 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
280                 label.set_alignment (0.0);
281                 table.attach (label, 0, 3, r, r+1,
282                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
283             }
284             else
285             {
286                 Gtk::HBox *space = manage (new Gtk::HBox);
287                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
288                 table.attach (*space, 0, 1, r, r+1,
289                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
290             }
291         }
292         ++r;
293     }
296 CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr)
297     : CanvasGrid(nv, in_repr), table(1, 1)
299     origin[NR::X] = origin[NR::Y] = 0.0;
300     color = DEFAULTGRIDCOLOR;
301     empcolor = DEFAULTGRIDEMPCOLOR;
302     empspacing = 5;
303     spacing[NR::X] = spacing[NR::Y] = 1.0;
304     gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
305     render_dotted = false;
307     snapper = new CanvasXYGridSnapper(this, namedview, 0);
309     // initialize widgets:
310     vbox.set_border_width(2);
311     table.set_spacings(2);
312     vbox.pack_start(table, false, false, 0);
314     _rumg.init (_("Grid _units:"), "units", _wr, repr);
315     _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
316                   "originx", _rumg, _wr, repr);
317     _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
318                   "originy", _rumg, _wr, repr);
319     _rsu_sx.init (_("Spacing _X:"), _("Distance between vertical grid lines"),
320                   "spacingx", _rumg, _wr, repr);
321     _rsu_sy.init (_("Spacing _Y:"), _("Distance between horizontal grid lines"),
322                   "spacingy", _rumg, _wr, repr);
323     _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
324                     _("Color of grid lines"), "color", "opacity", _wr, repr);
325     _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
326                      _("Color of the major (highlighted) grid lines"),
327                      "empcolor", "empopacity", _wr, repr);
328     _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr);
329     _rcb_dotted.init ( _("_Show dots instead of lines"), 
330                        _("If set, displays dots at gridpoints instead of gridlines"),
331                         "dotted", _wr, false, repr);
333     const Gtk::Widget* widget_array[] =
334     {
335         _rumg._label,       _rumg._sel,
336         0,                  _rsu_ox.getSU(),
337         0,                  _rsu_oy.getSU(),
338         0,                  _rsu_sx.getSU(),
339         0,                  _rsu_sy.getSU(),
340         _rcp_gcol._label,   _rcp_gcol._cp,
341         0,                  0,
342         _rcp_gmcol._label,  _rcp_gmcol._cp,
343         _rsi._label,        &_rsi._hbox,
344         0,                  _rcb_dotted._button,
345     };
347     attach_all (table, widget_array, sizeof(widget_array));
349     vbox.show();
351     if (repr) readRepr();
352     updateWidgets();
355 CanvasXYGrid::~CanvasXYGrid ()
357    if (snapper) delete snapper;
361 /* fixme: Collect all these length parsing methods and think common sane API */
363 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
365     if (!str) {
366         return FALSE;
367     }
369     gchar *u;
370     gdouble v = g_ascii_strtod(str, &u);
371     if (!u) {
372         return FALSE;
373     }
374     while (isspace(*u)) {
375         u += 1;
376     }
378     if (!*u) {
379         /* No unit specified - keep default */
380         *val = v;
381         return TRUE;
382     }
384     if (base & SP_UNIT_DEVICE) {
385         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
386             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
387             *val = v;
388             return TRUE;
389         }
390     }
392     if (base & SP_UNIT_ABSOLUTE) {
393         if (!strncmp(u, "pt", 2)) {
394             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
395         } else if (!strncmp(u, "mm", 2)) {
396             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
397         } else if (!strncmp(u, "cm", 2)) {
398             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
399         } else if (!strncmp(u, "m", 1)) {
400             *unit = &sp_unit_get_by_id(SP_UNIT_M);
401         } else if (!strncmp(u, "in", 2)) {
402             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
403         } else {
404             return FALSE;
405         }
406         *val = v;
407         return TRUE;
408     }
410     return FALSE;
413 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
415     if (!str) {
416         return FALSE;
417     }
419     gchar *u;
420     gdouble v = g_ascii_strtod(str, &u);
421     if (!u) {
422         return FALSE;
423     }
424     v = CLAMP(v, 0.0, 1.0);
426     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
428     return TRUE;
433 void
434 CanvasXYGrid::readRepr()
436     gchar const* value;
437     if ( (value = repr->attribute("originx")) ) {
438         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
439         origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
440     }
441     if ( (value = repr->attribute("originy")) ) {
442         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
443         origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
444     }
446     if ( (value = repr->attribute("spacingx")) ) {
447         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::X], &gridunit);
448         spacing[NR::X] = sp_units_get_pixels(spacing[NR::X], *(gridunit));
449     }
450     if ( (value = repr->attribute("spacingy")) ) {
451         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::Y], &gridunit);
452         spacing[NR::Y] = sp_units_get_pixels(spacing[NR::Y], *(gridunit));
453     }
455     if ( (value = repr->attribute("color")) ) {
456         color = (color & 0xff) | sp_svg_read_color(value, color);
457     }
459     if ( (value = repr->attribute("empcolor")) ) {
460         empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
461     }
463     if ( (value = repr->attribute("opacity")) ) {
464         sp_nv_read_opacity(value, &color);
465     }
466     if ( (value = repr->attribute("empopacity")) ) {
467         sp_nv_read_opacity(value, &empcolor);
468     }
470     if ( (value = repr->attribute("empspacing")) ) {
471         empspacing = atoi(value);
472     }
473     
474     if ( (value = repr->attribute("dotted")) ) {
475         render_dotted = (strcmp(value,"true") == 0);
476     }    
478     for (GSList *l = canvasitems; l != NULL; l = l->next) {
479         sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
480     }
482     return;
485 /**
486  * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
487  */
488 void
489 CanvasXYGrid::onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive)
491     readRepr();
493     if ( ! (_wr.isUpdating()) )
494         updateWidgets();
500 Gtk::Widget &
501 CanvasXYGrid::getWidget()
503     return vbox;
507 /**
508  * Update dialog widgets from object's values.
509  */
510 void
511 CanvasXYGrid::updateWidgets()
513     if (_wr.isUpdating()) return;
515     _wr.setUpdating (true);
517     _rumg.setUnit (gridunit);
519     gdouble val;
520     val = origin[NR::X];
521     val = sp_pixels_get_units (val, *(gridunit));
522     _rsu_ox.setValue (val);
523     val = origin[NR::Y];
524     val = sp_pixels_get_units (val, *(gridunit));
525     _rsu_oy.setValue (val);
526     val = spacing[NR::X];
527     double gridx = sp_pixels_get_units (val, *(gridunit));
528     _rsu_sx.setValue (gridx);
529     val = spacing[NR::Y];
530     double gridy = sp_pixels_get_units (val, *(gridunit));
531     _rsu_sy.setValue (gridy);
533     _rcp_gcol.setRgba32 (color);
534     _rcp_gmcol.setRgba32 (empcolor);
535     _rsi.setValue (empspacing);
537     _wr.setUpdating (false);
539     return;
544 void
545 CanvasXYGrid::Update (NR::Matrix const &affine, unsigned int flags)
547     ow = origin * affine;
548     sw = spacing * affine;
549     sw -= NR::Point(affine[4], affine[5]);
551     for(int dim = 0; dim < 2; dim++) {
552         gint scaling_factor = empspacing;
554         if (scaling_factor <= 1)
555             scaling_factor = 5;
557         scaled[dim] = FALSE;
558         sw[dim] = fabs (sw[dim]);
559         while (sw[dim] < 8.0) {
560             scaled[dim] = TRUE;
561             sw[dim] *= scaling_factor;
562             /* First pass, go up to the major line spacing, then
563                keep increasing by two. */
564             scaling_factor = 2;
565         }
566     }
570 static void
571 grid_hline (SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba)
573     if ((y >= buf->rect.y0) && (y < buf->rect.y1)) {
574         guint r, g, b, a;
575         gint x0, x1, x;
576         guchar *p;
577         r = NR_RGBA32_R (rgba);
578         g = NR_RGBA32_G (rgba);
579         b = NR_RGBA32_B (rgba);
580         a = NR_RGBA32_A (rgba);
581         x0 = MAX (buf->rect.x0, xs);
582         x1 = MIN (buf->rect.x1, xe + 1);
583         p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
584         for (x = x0; x < x1; x++) {
585             p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
586             p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
587             p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
588             p += 3;
589         }
590     }
593 static void
594 grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
596     if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
597         guint r, g, b, a;
598         gint y0, y1, y;
599         guchar *p;
600         r = NR_RGBA32_R(rgba);
601         g = NR_RGBA32_G (rgba);
602         b = NR_RGBA32_B (rgba);
603         a = NR_RGBA32_A (rgba);
604         y0 = MAX (buf->rect.y0, ys);
605         y1 = MIN (buf->rect.y1, ye + 1);
606         p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
607         for (y = y0; y < y1; y++) {
608             p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
609             p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
610             p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
611             p += buf->buf_rowstride;
612         }
613     }
616 static void
617 grid_dot (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
619     if ( (y >= buf->rect.y0) && (y < buf->rect.y1) 
620          && (x >= buf->rect.x0) && (x < buf->rect.x1) ) {
621         guint r, g, b, a;
622         guchar *p;
623         r = NR_RGBA32_R (rgba);
624         g = NR_RGBA32_G (rgba);
625         b = NR_RGBA32_B (rgba);
626         a = NR_RGBA32_A (rgba);
627         p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
628         p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
629         p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
630         p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);   
631     }
634 void
635 CanvasXYGrid::Render (SPCanvasBuf *buf)
637     const gdouble sxg = floor ((buf->rect.x0 - ow[NR::X]) / sw[NR::X]) * sw[NR::X] + ow[NR::X];
638     const gint  xlinestart = (gint) Inkscape::round((sxg - ow[NR::X]) / sw[NR::X]);
639     const gdouble syg = floor ((buf->rect.y0 - ow[NR::Y]) / sw[NR::Y]) * sw[NR::Y] + ow[NR::Y];
640     const gint  ylinestart = (gint) Inkscape::round((syg - ow[NR::Y]) / sw[NR::Y]);
642     if (!render_dotted) {
643         gint ylinenum;
644         gdouble y;
645         for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
646             const gint y0 = (gint) Inkscape::round(y);
647     
648             if (!scaled[NR::Y] && (ylinenum % empspacing) == 0) {
649                 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, empcolor);
650             } else {
651                 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, color);
652             }
653         }
654     
655         gint xlinenum;
656         gdouble x;
657         for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
658             const gint ix = (gint) Inkscape::round(x);
659             if (!scaled[NR::X] && (xlinenum % empspacing) == 0) {
660                 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, empcolor);
661             } else {
662                 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, color);
663             }
664         }
665     } else {
666         gint ylinenum;
667         gdouble y;
668         for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
669             const gint iy = (gint) Inkscape::round(y);
671             gint xlinenum;
672             gdouble x;
673             for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
674                 const gint ix = (gint) Inkscape::round(x);
675                 if ( (!scaled[NR::X] && (xlinenum % empspacing) == 0) 
676                      || (!scaled[NR::Y] && (ylinenum % empspacing) == 0) ) 
677                 {
678                     grid_dot (buf, ix, iy, empcolor | (guint32)0x000000FF); // put alpha to max value
679                 } 
680                 else 
681                 {
682                     grid_dot (buf, ix, iy, color | (guint32)0x000000FF);  // put alpha to max value
683                 }
684             }
686         }
687     }
693 /**
694  * \return x rounded to the nearest multiple of c1 plus c0.
695  *
696  * \note
697  * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero
698  * mean "ignore the grid in this dimention".  We're currently discussing "good" semantics
699  * for guide/grid snapping.
700  */
702 /* FIXME: move this somewhere else, perhaps */
703 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
705     return floor((x - c0) / c1 + .5) * c1 + c0;
708 CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
710     this->grid = grid;
713 LineSnapper::LineList
714 CanvasXYGridSnapper::_getSnapLines(NR::Point const &p) const
716     LineList s;
718     if ( grid == NULL ) {
719         return s;
720     }
722     for (unsigned int i = 0; i < 2; ++i) {
724         /* This is to make sure we snap to only visible grid lines */
725         double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
727         // convert screen pixels to px
728         // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
729         if (SP_ACTIVE_DESKTOP) {
730             scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
731         }
733         NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
734                                                                  scaled_spacing,
735                                                                  grid->origin[i]);
737         s.push_back(std::make_pair(NR::Dim2(i), rounded));
738     }
740     return s;
747 }; /* namespace Inkscape */
749 /*
750   Local Variables:
751   mode:c++
752   c-file-style:"stroustrup"
753   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
754   indent-tabs-mode:nil
755   fill-column:99
756   End:
757 */
758 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :