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 SPDocument *current_document = sp_desktop_document(SP_ACTIVE_DESKTOP);
227 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(current_document);
228 Inkscape::XML::Node *newnode;
229 newnode = xml_doc->createElement("inkscape:grid");
230 newnode->setAttribute("type", getSVGName(gridtype));
232 repr->appendChild(newnode);
234 sp_document_done(current_document, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid"));
235 }
237 /*
238 * Creates a new CanvasGrid object of type gridtype
239 */
240 CanvasGrid*
241 CanvasGrid::NewGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, GridType gridtype)
242 {
243 if (!in_repr) return NULL;
245 switch (gridtype) {
246 case GRID_RECTANGULAR:
247 return (CanvasGrid*) new CanvasXYGrid(nv, in_repr);
248 case GRID_AXONOMETRIC:
249 return (CanvasGrid*) new CanvasAxonomGrid(nv, in_repr);
250 }
252 return NULL;
253 }
256 /**
257 * creates a new grid canvasitem for the SPDesktop given as parameter. Keeps a link to this canvasitem in the canvasitems list.
258 */
259 GridCanvasItem *
260 CanvasGrid::createCanvasItem(SPDesktop * desktop)
261 {
262 if (!desktop) return NULL;
263 //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.
265 // check if there is already a canvasitem on this desktop linking to this grid
266 for (GSList *l = canvasitems; l != NULL; l = l->next) {
267 if ( sp_desktop_gridgroup(desktop) == SP_CANVAS_GROUP(SP_CANVAS_ITEM(l->data)->parent) ) {
268 return NULL;
269 }
270 }
272 GridCanvasItem * item = INKSCAPE_GRID_CANVASITEM( sp_canvas_item_new(sp_desktop_gridgroup(desktop), INKSCAPE_TYPE_GRID_CANVASITEM, NULL) );
273 item->grid = this;
274 sp_canvas_item_show(SP_CANVAS_ITEM(item));
276 gtk_object_ref(GTK_OBJECT(item)); // since we're keeping a link to this item, we need to bump up the ref count
277 canvasitems = g_slist_prepend(canvasitems, item);
279 return item;
280 }
282 void
283 CanvasGrid::on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data)
284 {
285 if (!data)
286 return;
288 ((CanvasGrid*) data)->onReprAttrChanged(repr, key, oldval, newval, is_interactive);
289 }
293 // ##########################################################
294 // CanvasXYGrid
297 /**
298 * "attach_all" function
299 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
300 *
301 * Helper function that attachs widgets in a 3xn table. The widgets come in an
302 * array that has two entries per table row. The two entries code for four
303 * possible cases: (0,0) means insert space in first column; (0, non-0) means
304 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
305 * (non-0, non-0) means two widgets in columns 2 and 3.
306 **/
307 #define SPACE_SIZE_X 15
308 #define SPACE_SIZE_Y 10
309 static inline void
310 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
311 {
312 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
313 {
314 if (arr[i] && arr[i+1])
315 {
316 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
317 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
318 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
319 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
320 }
321 else
322 {
323 if (arr[i+1])
324 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
325 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
326 else if (arr[i])
327 {
328 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
329 label.set_alignment (0.0);
330 table.attach (label, 0, 3, r, r+1,
331 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
332 }
333 else
334 {
335 Gtk::HBox *space = manage (new Gtk::HBox);
336 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
337 table.attach (*space, 0, 1, r, r+1,
338 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
339 }
340 }
341 ++r;
342 }
343 }
345 CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr)
346 : CanvasGrid(nv, in_repr), table(1, 1)
347 {
348 origin[NR::X] = origin[NR::Y] = 0.0;
349 color = DEFAULTGRIDCOLOR;
350 empcolor = DEFAULTGRIDEMPCOLOR;
351 empspacing = 5;
352 spacing[NR::X] = spacing[NR::Y] = 1.0;
353 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
354 render_dotted = false;
356 snapper = new CanvasXYGridSnapper(this, namedview, 0);
358 // initialize widgets:
359 vbox.set_border_width(2);
360 table.set_spacings(2);
361 vbox.pack_start(table, false, false, 0);
363 _rumg.init (_("Grid _units:"), "units", _wr, repr);
364 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
365 "originx", _rumg, _wr, repr);
366 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
367 "originy", _rumg, _wr, repr);
368 _rsu_sx.init (_("Spacing _X:"), _("Distance between vertical grid lines"),
369 "spacingx", _rumg, _wr, repr);
370 _rsu_sy.init (_("Spacing _Y:"), _("Distance between horizontal grid lines"),
371 "spacingy", _rumg, _wr, repr);
372 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
373 _("Color of grid lines"), "color", "opacity", _wr, repr);
374 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
375 _("Color of the major (highlighted) grid lines"),
376 "empcolor", "empopacity", _wr, repr);
377 _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr);
378 _rcb_dotted.init ( _("_Show dots instead of lines"),
379 _("If set, displays dots at gridpoints instead of gridlines"),
380 "dotted", _wr, false, repr);
382 const Gtk::Widget* widget_array[] =
383 {
384 _rumg._label, _rumg._sel,
385 0, _rsu_ox.getSU(),
386 0, _rsu_oy.getSU(),
387 0, _rsu_sx.getSU(),
388 0, _rsu_sy.getSU(),
389 _rcp_gcol._label, _rcp_gcol._cp,
390 0, 0,
391 _rcp_gmcol._label, _rcp_gmcol._cp,
392 _rsi._label, &_rsi._hbox,
393 0, _rcb_dotted._button,
394 };
396 attach_all (table, widget_array, sizeof(widget_array));
398 vbox.show();
400 if (repr) readRepr();
401 updateWidgets();
402 }
404 CanvasXYGrid::~CanvasXYGrid ()
405 {
406 if (snapper) delete snapper;
407 }
410 /* fixme: Collect all these length parsing methods and think common sane API */
412 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
413 {
414 if (!str) {
415 return FALSE;
416 }
418 gchar *u;
419 gdouble v = g_ascii_strtod(str, &u);
420 if (!u) {
421 return FALSE;
422 }
423 while (isspace(*u)) {
424 u += 1;
425 }
427 if (!*u) {
428 /* No unit specified - keep default */
429 *val = v;
430 return TRUE;
431 }
433 if (base & SP_UNIT_DEVICE) {
434 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
435 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
436 *val = v;
437 return TRUE;
438 }
439 }
441 if (base & SP_UNIT_ABSOLUTE) {
442 if (!strncmp(u, "pt", 2)) {
443 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
444 } else if (!strncmp(u, "mm", 2)) {
445 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
446 } else if (!strncmp(u, "cm", 2)) {
447 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
448 } else if (!strncmp(u, "m", 1)) {
449 *unit = &sp_unit_get_by_id(SP_UNIT_M);
450 } else if (!strncmp(u, "in", 2)) {
451 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
452 } else {
453 return FALSE;
454 }
455 *val = v;
456 return TRUE;
457 }
459 return FALSE;
460 }
462 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
463 {
464 if (!str) {
465 return FALSE;
466 }
468 gchar *u;
469 gdouble v = g_ascii_strtod(str, &u);
470 if (!u) {
471 return FALSE;
472 }
473 v = CLAMP(v, 0.0, 1.0);
475 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
477 return TRUE;
478 }
480 /** If the passed scalar is invalid (<=0), then set the widget and the scalar
481 to use the given old value.
482 @param oldVal old value to use if the new one is invalid
483 @param pTarget the scalar to validate
484 @param widget widget associated with the scalar
485 */
486 static void validateScalar(double oldVal,
487 double* pTarget,
488 Inkscape::UI::Widget::RegisteredScalarUnit& widget)
489 {
490 // Avoid nullness.
491 if ( pTarget == NULL )
492 return;
494 // Invalid new value?
495 if ( *pTarget <= 0 )
496 {
497 // If the old value is somehow invalid as well, then default to 1.
498 if ( oldVal <= 0 )
499 oldVal = 1;
501 // Reset the scalar and associated widget to the old value.
502 *pTarget = oldVal;
503 widget.setValue( *pTarget);
504 } //if
506 } //validateScalar
509 /** If the passed int is invalid (<=0), then set the widget and the int
510 to use the given old value.
511 @param oldVal old value to use if the new one is invalid
512 @param pTarget the int to validate
513 @param widget widget associated with the int
514 */
515 static void validateInt(gint oldVal,
516 gint* pTarget,
517 Inkscape::UI::Widget::RegisteredSuffixedInteger& widget)
518 {
519 // Avoid nullness.
520 if ( pTarget == NULL )
521 return;
523 // Invalid new value?
524 if ( *pTarget <= 0 )
525 {
526 // If the old value is somehow invalid as well, then default to 1.
527 if ( oldVal <= 0 )
528 oldVal = 1;
530 // Reset the int and associated widget to the old value.
531 *pTarget = oldVal;
532 widget.setValue( *pTarget);
533 } //if
535 } //validateInt
537 void
538 CanvasXYGrid::readRepr()
539 {
540 char buff[100];
541 gchar const* value;
542 if ( (value = repr->attribute("originx")) ) {
543 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
544 origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
546 }
547 if ( (value = repr->attribute("originy")) ) {
548 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
549 origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
550 }
552 if ( (value = repr->attribute("spacingx")) ) {
553 double oldVal = spacing[NR::X];
554 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::X], &gridunit);
555 validateScalar( oldVal, &spacing[NR::X], _rsu_sx );
556 spacing[NR::X] = sp_units_get_pixels(spacing[NR::X], *(gridunit));
558 }
559 if ( (value = repr->attribute("spacingy")) ) {
560 double oldVal = spacing[NR::Y];
561 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::Y], &gridunit);
562 validateScalar( oldVal, &spacing[NR::Y], _rsu_sy );
563 spacing[NR::Y] = sp_units_get_pixels(spacing[NR::Y], *(gridunit));
565 }
567 if ( (value = repr->attribute("color")) ) {
568 color = (color & 0xff) | sp_svg_read_color(value, color);
569 }
571 if ( (value = repr->attribute("empcolor")) ) {
572 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
573 }
575 if ( (value = repr->attribute("opacity")) ) {
576 sp_nv_read_opacity(value, &color);
577 }
578 if ( (value = repr->attribute("empopacity")) ) {
579 sp_nv_read_opacity(value, &empcolor);
580 }
582 if ( (value = repr->attribute("empspacing")) ) {
583 gint oldVal = empspacing;
584 empspacing = atoi(value);
585 validateInt( oldVal, &empspacing, _rsi );
586 }
588 if ( (value = repr->attribute("dotted")) ) {
589 render_dotted = (strcmp(value,"true") == 0);
590 }
592 for (GSList *l = canvasitems; l != NULL; l = l->next) {
593 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
594 }
596 return;
597 }
599 /**
600 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
601 */
602 void
603 CanvasXYGrid::onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive)
604 {
605 readRepr();
607 if ( ! (_wr.isUpdating()) )
608 updateWidgets();
609 }
614 Gtk::Widget &
615 CanvasXYGrid::getWidget()
616 {
617 return vbox;
618 }
621 /**
622 * Update dialog widgets from object's values.
623 */
624 void
625 CanvasXYGrid::updateWidgets()
626 {
627 if (_wr.isUpdating()) return;
629 _wr.setUpdating (true);
631 _rumg.setUnit (gridunit);
633 gdouble val;
634 val = origin[NR::X];
635 val = sp_pixels_get_units (val, *(gridunit));
636 _rsu_ox.setValue (val);
637 val = origin[NR::Y];
638 val = sp_pixels_get_units (val, *(gridunit));
639 _rsu_oy.setValue (val);
640 val = spacing[NR::X];
641 double gridx = sp_pixels_get_units (val, *(gridunit));
642 _rsu_sx.setValue (gridx);
643 val = spacing[NR::Y];
644 double gridy = sp_pixels_get_units (val, *(gridunit));
645 _rsu_sy.setValue (gridy);
647 _rcp_gcol.setRgba32 (color);
648 _rcp_gmcol.setRgba32 (empcolor);
649 _rsi.setValue (empspacing);
651 _wr.setUpdating (false);
653 return;
654 }
658 void
659 CanvasXYGrid::Update (NR::Matrix const &affine, unsigned int flags)
660 {
661 ow = origin * affine;
662 sw = spacing * affine;
663 sw -= NR::Point(affine[4], affine[5]);
665 for(int dim = 0; dim < 2; dim++) {
666 gint scaling_factor = empspacing;
668 if (scaling_factor <= 1)
669 scaling_factor = 5;
671 scaled[dim] = FALSE;
672 sw[dim] = fabs (sw[dim]);
673 while (sw[dim] < 8.0) {
674 scaled[dim] = TRUE;
675 sw[dim] *= scaling_factor;
676 /* First pass, go up to the major line spacing, then
677 keep increasing by two. */
678 scaling_factor = 2;
679 }
680 }
681 }
684 static void
685 grid_hline (SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba)
686 {
687 if ((y >= buf->rect.y0) && (y < buf->rect.y1)) {
688 guint r, g, b, a;
689 gint x0, x1, x;
690 guchar *p;
691 r = NR_RGBA32_R (rgba);
692 g = NR_RGBA32_G (rgba);
693 b = NR_RGBA32_B (rgba);
694 a = NR_RGBA32_A (rgba);
695 x0 = MAX (buf->rect.x0, xs);
696 x1 = MIN (buf->rect.x1, xe + 1);
697 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
698 for (x = x0; x < x1; x++) {
699 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
700 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
701 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
702 p += 3;
703 }
704 }
705 }
707 static void
708 grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
709 {
710 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
711 guint r, g, b, a;
712 gint y0, y1, y;
713 guchar *p;
714 r = NR_RGBA32_R(rgba);
715 g = NR_RGBA32_G (rgba);
716 b = NR_RGBA32_B (rgba);
717 a = NR_RGBA32_A (rgba);
718 y0 = MAX (buf->rect.y0, ys);
719 y1 = MIN (buf->rect.y1, ye + 1);
720 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
721 for (y = y0; y < y1; y++) {
722 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
723 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
724 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
725 p += buf->buf_rowstride;
726 }
727 }
728 }
730 static void
731 grid_dot (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
732 {
733 if ( (y >= buf->rect.y0) && (y < buf->rect.y1)
734 && (x >= buf->rect.x0) && (x < buf->rect.x1) ) {
735 guint r, g, b, a;
736 guchar *p;
737 r = NR_RGBA32_R (rgba);
738 g = NR_RGBA32_G (rgba);
739 b = NR_RGBA32_B (rgba);
740 a = NR_RGBA32_A (rgba);
741 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
742 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
743 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
744 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
745 }
746 }
748 void
749 CanvasXYGrid::Render (SPCanvasBuf *buf)
750 {
751 const gdouble sxg = floor ((buf->rect.x0 - ow[NR::X]) / sw[NR::X]) * sw[NR::X] + ow[NR::X];
752 const gint xlinestart = (gint) Inkscape::round((sxg - ow[NR::X]) / sw[NR::X]);
753 const gdouble syg = floor ((buf->rect.y0 - ow[NR::Y]) / sw[NR::Y]) * sw[NR::Y] + ow[NR::Y];
754 const gint ylinestart = (gint) Inkscape::round((syg - ow[NR::Y]) / sw[NR::Y]);
756 if (!render_dotted) {
757 gint ylinenum;
758 gdouble y;
759 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
760 const gint y0 = (gint) Inkscape::round(y);
762 if (!scaled[NR::Y] && (ylinenum % empspacing) == 0) {
763 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, empcolor);
764 } else {
765 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, color);
766 }
767 }
769 gint xlinenum;
770 gdouble x;
771 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
772 const gint ix = (gint) Inkscape::round(x);
773 if (!scaled[NR::X] && (xlinenum % empspacing) == 0) {
774 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, empcolor);
775 } else {
776 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, color);
777 }
778 }
779 } else {
780 gint ylinenum;
781 gdouble y;
782 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
783 const gint iy = (gint) Inkscape::round(y);
785 gint xlinenum;
786 gdouble x;
787 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
788 const gint ix = (gint) Inkscape::round(x);
789 if ( (!scaled[NR::X] && (xlinenum % empspacing) == 0)
790 || (!scaled[NR::Y] && (ylinenum % empspacing) == 0) )
791 {
792 grid_dot (buf, ix, iy, empcolor | (guint32)0x000000FF); // put alpha to max value
793 }
794 else
795 {
796 grid_dot (buf, ix, iy, color | (guint32)0x000000FF); // put alpha to max value
797 }
798 }
800 }
801 }
802 }
807 /**
808 * \return x rounded to the nearest multiple of c1 plus c0.
809 *
810 * \note
811 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
812 * mean "ignore the grid in this dimention". We're currently discussing "good" semantics
813 * for guide/grid snapping.
814 */
816 /* FIXME: move this somewhere else, perhaps */
817 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
818 {
819 return floor((x - c0) / c1 + .5) * c1 + c0;
820 }
822 CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
823 {
824 this->grid = grid;
825 }
827 LineSnapper::LineList
828 CanvasXYGridSnapper::_getSnapLines(NR::Point const &p) const
829 {
830 LineList s;
832 if ( grid == NULL ) {
833 return s;
834 }
836 for (unsigned int i = 0; i < 2; ++i) {
838 /* This is to make sure we snap to only visible grid lines */
839 double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
841 // convert screen pixels to px
842 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
843 if (SP_ACTIVE_DESKTOP) {
844 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
845 }
847 NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
848 scaled_spacing,
849 grid->origin[i]);
851 s.push_back(std::make_pair(NR::Dim2(i), rounded));
852 }
854 return s;
855 }
861 }; /* namespace Inkscape */
863 /*
864 Local Variables:
865 mode:c++
866 c-file-style:"stroustrup"
867 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
868 indent-tabs-mode:nil
869 fill-column:99
870 End:
871 */
872 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :