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
34 const gchar *grid_name [] = {
35 N_("Rectangular grid"),
36 N_("Axonometric grid")
37 };
38 const gchar *grid_svgname [] = {
39 "xygrid",
40 "axonomgrid"
41 };
44 // ##########################################################
45 // Grid CanvasItem
46 static void grid_canvasitem_class_init (GridCanvasItemClass *klass);
47 static void grid_canvasitem_init (GridCanvasItem *grid);
48 static void grid_canvasitem_destroy (GtkObject *object);
50 static void grid_canvasitem_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
51 static void grid_canvasitem_render (SPCanvasItem *item, SPCanvasBuf *buf);
53 static SPCanvasItemClass * parent_class;
55 GtkType
56 grid_canvasitem_get_type (void)
57 {
58 static GtkType grid_canvasitem_type = 0;
60 if (!grid_canvasitem_type) {
61 GtkTypeInfo grid_canvasitem_info = {
62 "GridCanvasItem",
63 sizeof (GridCanvasItem),
64 sizeof (GridCanvasItemClass),
65 (GtkClassInitFunc) grid_canvasitem_class_init,
66 (GtkObjectInitFunc) grid_canvasitem_init,
67 NULL, NULL,
68 (GtkClassInitFunc) NULL
69 };
70 grid_canvasitem_type = gtk_type_unique (sp_canvas_item_get_type (), &grid_canvasitem_info);
71 }
72 return grid_canvasitem_type;
73 }
75 static void
76 grid_canvasitem_class_init (GridCanvasItemClass *klass)
77 {
78 GtkObjectClass *object_class;
79 SPCanvasItemClass *item_class;
81 object_class = (GtkObjectClass *) klass;
82 item_class = (SPCanvasItemClass *) klass;
84 parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
86 object_class->destroy = grid_canvasitem_destroy;
88 item_class->update = grid_canvasitem_update;
89 item_class->render = grid_canvasitem_render;
90 }
92 static void
93 grid_canvasitem_init (GridCanvasItem *griditem)
94 {
95 griditem->grid = NULL;
96 }
98 static void
99 grid_canvasitem_destroy (GtkObject *object)
100 {
101 g_return_if_fail (object != NULL);
102 g_return_if_fail (INKSCAPE_IS_GRID_CANVASITEM (object));
104 if (GTK_OBJECT_CLASS (parent_class)->destroy)
105 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
106 }
108 /**
109 */
110 static void
111 grid_canvasitem_render (SPCanvasItem * item, SPCanvasBuf * buf)
112 {
113 GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
115 sp_canvas_prepare_buffer (buf);
117 if (gridcanvasitem->grid) gridcanvasitem->grid->Render(buf);
118 }
120 static void
121 grid_canvasitem_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
122 {
123 GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
125 if (parent_class->update)
126 (* parent_class->update) (item, affine, flags);
128 if (gridcanvasitem->grid) {
129 gridcanvasitem->grid->Update(affine, flags);
131 sp_canvas_request_redraw (item->canvas,
132 -1000000, -1000000,
133 1000000, 1000000);
135 item->x1 = item->y1 = -1000000;
136 item->x2 = item->y2 = 1000000;
137 }
138 }
142 // ##########################################################
143 // CanvasGrid
145 static Inkscape::XML::NodeEventVector const _repr_events = {
146 NULL, /* child_added */
147 NULL, /* child_removed */
148 CanvasGrid::on_repr_attr_changed,
149 NULL, /* content_changed */
150 NULL /* order_changed */
151 };
153 CanvasGrid::CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr)
154 {
155 repr = in_repr;
156 if (repr) {
157 repr->addListener (&_repr_events, this);
158 }
160 namedview = nv;
161 canvasitems = NULL;
162 }
164 CanvasGrid::~CanvasGrid()
165 {
166 if (repr) {
167 repr->removeListenerByData (this);
168 }
170 while (canvasitems) {
171 gtk_object_destroy(GTK_OBJECT(canvasitems->data));
172 canvasitems = g_slist_remove(canvasitems, canvasitems->data);
173 }
174 }
178 const char *
179 CanvasGrid::getName(GridType type)
180 {
181 return _(grid_name[type]);
182 }
184 const char *
185 CanvasGrid::getSVGName(GridType type)
186 {
187 return grid_svgname[type];
188 }
190 GridType
191 CanvasGrid::getGridTypeFromSVGName(const char * typestr)
192 {
193 if (!typestr) return GRID_RECTANGULAR;
195 gint t = 0;
196 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
197 if (!strcmp(typestr, grid_svgname[t])) break;
198 }
199 return (GridType) t;
200 }
202 GridType
203 CanvasGrid::getGridTypeFromName(const char * typestr)
204 {
205 if (!typestr) return GRID_RECTANGULAR;
207 gint t = 0;
208 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
209 if (!strcmp(typestr, _(grid_name[t]))) break;
210 }
211 return (GridType) t;
212 }
215 /*
216 * writes an <inkscape:grid> child to repr.
217 */
218 void
219 CanvasGrid::writeNewGridToRepr(Inkscape::XML::Node * repr, GridType gridtype)
220 {
221 if (!repr) return;
222 if (gridtype > GRID_MAXTYPENR) return;
224 // 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.
226 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(sp_desktop_document(SP_ACTIVE_DESKTOP));
227 Inkscape::XML::Node *newnode;
228 newnode = xml_doc->createElement("inkscape:grid");
229 newnode->setAttribute("type", getSVGName(gridtype));
231 repr->appendChild(newnode);
233 // FIXME: add this to history?
234 // sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
235 // _("Create new element node"));
236 }
238 /*
239 * Creates a new CanvasGrid object of type gridtype
240 */
241 CanvasGrid*
242 CanvasGrid::NewGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, GridType gridtype)
243 {
244 if (!in_repr) return NULL;
246 switch (gridtype) {
247 case GRID_RECTANGULAR:
248 return (CanvasGrid*) new CanvasXYGrid(nv, in_repr);
249 case GRID_AXONOMETRIC:
250 return (CanvasGrid*) new CanvasAxonomGrid(nv, in_repr);
251 }
253 return NULL;
254 }
257 /**
258 * creates a new grid canvasitem for the SPDesktop given as parameter. Keeps a link to this canvasitem in the canvasitems list.
259 */
260 GridCanvasItem *
261 CanvasGrid::createCanvasItem(SPDesktop * desktop)
262 {
263 if (!desktop) return NULL;
264 //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.
266 // check if there is already a canvasitem on this desktop linking to this grid
267 for (GSList *l = canvasitems; l != NULL; l = l->next) {
268 if ( sp_desktop_gridgroup(desktop) == SP_CANVAS_GROUP(SP_CANVAS_ITEM(l->data)->parent) ) {
269 return NULL;
270 }
271 }
273 GridCanvasItem * item = INKSCAPE_GRID_CANVASITEM( sp_canvas_item_new(sp_desktop_gridgroup(desktop), INKSCAPE_TYPE_GRID_CANVASITEM, NULL) );
274 item->grid = this;
275 sp_canvas_item_show(SP_CANVAS_ITEM(item));
277 gtk_object_ref(GTK_OBJECT(item)); // since we're keeping a link to this item, we need to bump up the ref count
278 canvasitems = g_slist_prepend(canvasitems, item);
280 return item;
281 }
283 void
284 CanvasGrid::on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data)
285 {
286 if (!data)
287 return;
289 ((CanvasGrid*) data)->onReprAttrChanged(repr, key, oldval, newval, is_interactive);
290 }
294 // ##########################################################
295 // CanvasXYGrid
298 /**
299 * "attach_all" function
300 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
301 *
302 * Helper function that attachs widgets in a 3xn table. The widgets come in an
303 * array that has two entries per table row. The two entries code for four
304 * possible cases: (0,0) means insert space in first column; (0, non-0) means
305 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
306 * (non-0, non-0) means two widgets in columns 2 and 3.
307 **/
308 #define SPACE_SIZE_X 15
309 #define SPACE_SIZE_Y 10
310 static inline void
311 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
312 {
313 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
314 {
315 if (arr[i] && arr[i+1])
316 {
317 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
318 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
319 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
320 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
321 }
322 else
323 {
324 if (arr[i+1])
325 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
326 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
327 else if (arr[i])
328 {
329 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
330 label.set_alignment (0.0);
331 table.attach (label, 0, 3, r, r+1,
332 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
333 }
334 else
335 {
336 Gtk::HBox *space = manage (new Gtk::HBox);
337 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
338 table.attach (*space, 0, 1, r, r+1,
339 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
340 }
341 }
342 ++r;
343 }
344 }
346 CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr)
347 : CanvasGrid(nv, in_repr), table(1, 1)
348 {
349 origin[NR::X] = origin[NR::Y] = 0.0;
350 color = DEFAULTGRIDCOLOR;
351 empcolor = DEFAULTGRIDEMPCOLOR;
352 empspacing = 5;
353 spacing[NR::X] = spacing[NR::Y] = 1.0;
354 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
355 render_dotted = false;
357 snapper = new CanvasXYGridSnapper(this, namedview, 0);
359 // initialize widgets:
360 vbox.set_border_width(2);
361 table.set_spacings(2);
362 vbox.pack_start(table, false, false, 0);
364 _rumg.init (_("Grid _units:"), "units", _wr, repr);
365 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
366 "originx", _rumg, _wr, repr);
367 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
368 "originy", _rumg, _wr, repr);
369 _rsu_sx.init (_("Spacing _X:"), _("Distance between vertical grid lines"),
370 "spacingx", _rumg, _wr, repr);
371 _rsu_sy.init (_("Spacing _Y:"), _("Distance between horizontal grid lines"),
372 "spacingy", _rumg, _wr, repr);
373 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
374 _("Color of grid lines"), "color", "opacity", _wr, repr);
375 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
376 _("Color of the major (highlighted) grid lines"),
377 "empcolor", "empopacity", _wr, repr);
378 _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr);
379 _rcb_dotted.init ( _("_Show dots instead of lines"),
380 _("If set, displays dots at gridpoints instead of gridlines"),
381 "dotted", _wr, false, repr);
383 const Gtk::Widget* widget_array[] =
384 {
385 _rumg._label, _rumg._sel,
386 0, _rsu_ox.getSU(),
387 0, _rsu_oy.getSU(),
388 0, _rsu_sx.getSU(),
389 0, _rsu_sy.getSU(),
390 _rcp_gcol._label, _rcp_gcol._cp,
391 0, 0,
392 _rcp_gmcol._label, _rcp_gmcol._cp,
393 _rsi._label, &_rsi._hbox,
394 0, _rcb_dotted._button,
395 };
397 attach_all (table, widget_array, sizeof(widget_array));
399 vbox.show();
401 if (repr) readRepr();
402 updateWidgets();
403 }
405 CanvasXYGrid::~CanvasXYGrid ()
406 {
407 if (snapper) delete snapper;
408 }
411 /* fixme: Collect all these length parsing methods and think common sane API */
413 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
414 {
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 while (isspace(*u)) {
425 u += 1;
426 }
428 if (!*u) {
429 /* No unit specified - keep default */
430 *val = v;
431 return TRUE;
432 }
434 if (base & SP_UNIT_DEVICE) {
435 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
436 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
437 *val = v;
438 return TRUE;
439 }
440 }
442 if (base & SP_UNIT_ABSOLUTE) {
443 if (!strncmp(u, "pt", 2)) {
444 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
445 } else if (!strncmp(u, "mm", 2)) {
446 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
447 } else if (!strncmp(u, "cm", 2)) {
448 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
449 } else if (!strncmp(u, "m", 1)) {
450 *unit = &sp_unit_get_by_id(SP_UNIT_M);
451 } else if (!strncmp(u, "in", 2)) {
452 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
453 } else {
454 return FALSE;
455 }
456 *val = v;
457 return TRUE;
458 }
460 return FALSE;
461 }
463 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
464 {
465 if (!str) {
466 return FALSE;
467 }
469 gchar *u;
470 gdouble v = g_ascii_strtod(str, &u);
471 if (!u) {
472 return FALSE;
473 }
474 v = CLAMP(v, 0.0, 1.0);
476 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
478 return TRUE;
479 }
483 void
484 CanvasXYGrid::readRepr()
485 {
486 gchar const* value;
487 if ( (value = repr->attribute("originx")) ) {
488 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
489 origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
490 }
491 if ( (value = repr->attribute("originy")) ) {
492 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
493 origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
494 }
496 if ( (value = repr->attribute("spacingx")) ) {
497 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::X], &gridunit);
498 spacing[NR::X] = sp_units_get_pixels(spacing[NR::X], *(gridunit));
499 }
500 if ( (value = repr->attribute("spacingy")) ) {
501 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::Y], &gridunit);
502 spacing[NR::Y] = sp_units_get_pixels(spacing[NR::Y], *(gridunit));
503 }
505 if ( (value = repr->attribute("color")) ) {
506 color = (color & 0xff) | sp_svg_read_color(value, color);
507 }
509 if ( (value = repr->attribute("empcolor")) ) {
510 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
511 }
513 if ( (value = repr->attribute("opacity")) ) {
514 sp_nv_read_opacity(value, &color);
515 }
516 if ( (value = repr->attribute("empopacity")) ) {
517 sp_nv_read_opacity(value, &empcolor);
518 }
520 if ( (value = repr->attribute("empspacing")) ) {
521 empspacing = atoi(value);
522 }
524 if ( (value = repr->attribute("dotted")) ) {
525 render_dotted = (strcmp(value,"true") == 0);
526 }
528 for (GSList *l = canvasitems; l != NULL; l = l->next) {
529 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
530 }
532 return;
533 }
535 /**
536 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
537 */
538 void
539 CanvasXYGrid::onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive)
540 {
541 readRepr();
543 if ( ! (_wr.isUpdating()) )
544 updateWidgets();
545 }
550 Gtk::Widget &
551 CanvasXYGrid::getWidget()
552 {
553 return vbox;
554 }
557 /**
558 * Update dialog widgets from object's values.
559 */
560 void
561 CanvasXYGrid::updateWidgets()
562 {
563 if (_wr.isUpdating()) return;
565 _wr.setUpdating (true);
567 _rumg.setUnit (gridunit);
569 gdouble val;
570 val = origin[NR::X];
571 val = sp_pixels_get_units (val, *(gridunit));
572 _rsu_ox.setValue (val);
573 val = origin[NR::Y];
574 val = sp_pixels_get_units (val, *(gridunit));
575 _rsu_oy.setValue (val);
576 val = spacing[NR::X];
577 double gridx = sp_pixels_get_units (val, *(gridunit));
578 _rsu_sx.setValue (gridx);
579 val = spacing[NR::Y];
580 double gridy = sp_pixels_get_units (val, *(gridunit));
581 _rsu_sy.setValue (gridy);
583 _rcp_gcol.setRgba32 (color);
584 _rcp_gmcol.setRgba32 (empcolor);
585 _rsi.setValue (empspacing);
587 _wr.setUpdating (false);
589 return;
590 }
594 void
595 CanvasXYGrid::Update (NR::Matrix const &affine, unsigned int flags)
596 {
597 ow = origin * affine;
598 sw = spacing * affine;
599 sw -= NR::Point(affine[4], affine[5]);
601 for(int dim = 0; dim < 2; dim++) {
602 gint scaling_factor = empspacing;
604 if (scaling_factor <= 1)
605 scaling_factor = 5;
607 scaled[dim] = FALSE;
608 sw[dim] = fabs (sw[dim]);
609 while (sw[dim] < 8.0) {
610 scaled[dim] = TRUE;
611 sw[dim] *= scaling_factor;
612 /* First pass, go up to the major line spacing, then
613 keep increasing by two. */
614 scaling_factor = 2;
615 }
616 }
617 }
620 static void
621 grid_hline (SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba)
622 {
623 if ((y >= buf->rect.y0) && (y < buf->rect.y1)) {
624 guint r, g, b, a;
625 gint x0, x1, x;
626 guchar *p;
627 r = NR_RGBA32_R (rgba);
628 g = NR_RGBA32_G (rgba);
629 b = NR_RGBA32_B (rgba);
630 a = NR_RGBA32_A (rgba);
631 x0 = MAX (buf->rect.x0, xs);
632 x1 = MIN (buf->rect.x1, xe + 1);
633 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
634 for (x = x0; x < x1; x++) {
635 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
636 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
637 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
638 p += 3;
639 }
640 }
641 }
643 static void
644 grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
645 {
646 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
647 guint r, g, b, a;
648 gint y0, y1, y;
649 guchar *p;
650 r = NR_RGBA32_R(rgba);
651 g = NR_RGBA32_G (rgba);
652 b = NR_RGBA32_B (rgba);
653 a = NR_RGBA32_A (rgba);
654 y0 = MAX (buf->rect.y0, ys);
655 y1 = MIN (buf->rect.y1, ye + 1);
656 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
657 for (y = y0; y < y1; y++) {
658 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
659 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
660 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
661 p += buf->buf_rowstride;
662 }
663 }
664 }
666 static void
667 grid_dot (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
668 {
669 if ( (y >= buf->rect.y0) && (y < buf->rect.y1)
670 && (x >= buf->rect.x0) && (x < buf->rect.x1) ) {
671 guint r, g, b, a;
672 guchar *p;
673 r = NR_RGBA32_R (rgba);
674 g = NR_RGBA32_G (rgba);
675 b = NR_RGBA32_B (rgba);
676 a = NR_RGBA32_A (rgba);
677 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
678 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
679 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
680 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
681 }
682 }
684 void
685 CanvasXYGrid::Render (SPCanvasBuf *buf)
686 {
687 const gdouble sxg = floor ((buf->rect.x0 - ow[NR::X]) / sw[NR::X]) * sw[NR::X] + ow[NR::X];
688 const gint xlinestart = (gint) Inkscape::round((sxg - ow[NR::X]) / sw[NR::X]);
689 const gdouble syg = floor ((buf->rect.y0 - ow[NR::Y]) / sw[NR::Y]) * sw[NR::Y] + ow[NR::Y];
690 const gint ylinestart = (gint) Inkscape::round((syg - ow[NR::Y]) / sw[NR::Y]);
692 if (!render_dotted) {
693 gint ylinenum;
694 gdouble y;
695 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
696 const gint y0 = (gint) Inkscape::round(y);
698 if (!scaled[NR::Y] && (ylinenum % empspacing) == 0) {
699 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, empcolor);
700 } else {
701 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, color);
702 }
703 }
705 gint xlinenum;
706 gdouble x;
707 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
708 const gint ix = (gint) Inkscape::round(x);
709 if (!scaled[NR::X] && (xlinenum % empspacing) == 0) {
710 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, empcolor);
711 } else {
712 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, color);
713 }
714 }
715 } else {
716 gint ylinenum;
717 gdouble y;
718 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
719 const gint iy = (gint) Inkscape::round(y);
721 gint xlinenum;
722 gdouble x;
723 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
724 const gint ix = (gint) Inkscape::round(x);
725 if ( (!scaled[NR::X] && (xlinenum % empspacing) == 0)
726 || (!scaled[NR::Y] && (ylinenum % empspacing) == 0) )
727 {
728 grid_dot (buf, ix, iy, empcolor | (guint32)0x000000FF); // put alpha to max value
729 }
730 else
731 {
732 grid_dot (buf, ix, iy, color | (guint32)0x000000FF); // put alpha to max value
733 }
734 }
736 }
737 }
738 }
743 /**
744 * \return x rounded to the nearest multiple of c1 plus c0.
745 *
746 * \note
747 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
748 * mean "ignore the grid in this dimention". We're currently discussing "good" semantics
749 * for guide/grid snapping.
750 */
752 /* FIXME: move this somewhere else, perhaps */
753 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
754 {
755 return floor((x - c0) / c1 + .5) * c1 + c0;
756 }
758 CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
759 {
760 this->grid = grid;
761 }
763 LineSnapper::LineList
764 CanvasXYGridSnapper::_getSnapLines(NR::Point const &p) const
765 {
766 LineList s;
768 if ( grid == NULL ) {
769 return s;
770 }
772 for (unsigned int i = 0; i < 2; ++i) {
774 /* This is to make sure we snap to only visible grid lines */
775 double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
777 // convert screen pixels to px
778 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
779 if (SP_ACTIVE_DESKTOP) {
780 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
781 }
783 NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
784 scaled_spacing,
785 grid->origin[i]);
787 s.push_back(std::make_pair(NR::Dim2(i), rounded));
788 }
790 return s;
791 }
797 }; /* namespace Inkscape */
799 /*
800 Local Variables:
801 mode:c++
802 c-file-style:"stroustrup"
803 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
804 indent-tabs-mode:nil
805 fill-column:99
806 End:
807 */
808 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :