b6086db270f64bee8d93b2ce19b50f4ece48573e
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 static gchar const *const grid_name[] = {
35 N_("Rectangular grid"),
36 N_("Axonometric grid")
37 };
38 static gchar const *const 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, SPDocument *in_doc)
154 {
155 repr = in_repr;
156 doc = in_doc;
157 if (repr) {
158 repr->addListener (&_repr_events, this);
159 }
161 namedview = nv;
162 canvasitems = NULL;
163 }
165 CanvasGrid::~CanvasGrid()
166 {
167 if (repr) {
168 repr->removeListenerByData (this);
169 }
171 while (canvasitems) {
172 gtk_object_destroy(GTK_OBJECT(canvasitems->data));
173 canvasitems = g_slist_remove(canvasitems, canvasitems->data);
174 }
175 }
179 char const *
180 CanvasGrid::getName(GridType type)
181 {
182 return _(grid_name[type]);
183 }
185 char const *
186 CanvasGrid::getSVGName(GridType type)
187 {
188 return grid_svgname[type];
189 }
191 GridType
192 CanvasGrid::getGridTypeFromSVGName(char const *typestr)
193 {
194 if (!typestr) return GRID_RECTANGULAR;
196 gint t = 0;
197 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
198 if (!strcmp(typestr, grid_svgname[t])) break;
199 }
200 return (GridType) t;
201 }
203 GridType
204 CanvasGrid::getGridTypeFromName(char const *typestr)
205 {
206 if (!typestr) return GRID_RECTANGULAR;
208 gint t = 0;
209 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
210 if (!strcmp(typestr, _(grid_name[t]))) break;
211 }
212 return (GridType) t;
213 }
216 /*
217 * writes an <inkscape:grid> child to repr.
218 */
219 void
220 CanvasGrid::writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype)
221 {
222 if (!repr) return;
223 if (gridtype > GRID_MAXTYPENR) return;
225 // 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.
227 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
228 Inkscape::XML::Node *newnode;
229 newnode = xml_doc->createElement("inkscape:grid");
230 newnode->setAttribute("type", getSVGName(gridtype));
232 repr->appendChild(newnode);
233 // Inkscape::GC::release(repr); FIX THIS. THIS SHOULD BE HERE!!!
235 sp_document_done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid"));
236 }
238 /*
239 * Creates a new CanvasGrid object of type gridtype
240 */
241 CanvasGrid*
242 CanvasGrid::NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype)
243 {
244 if (!repr) return NULL;
245 if (!doc) {
246 g_error("CanvasGrid::NewGrid - doc==NULL");
247 return NULL;
248 }
250 switch (gridtype) {
251 case GRID_RECTANGULAR:
252 return (CanvasGrid*) new CanvasXYGrid(nv, repr, doc);
253 case GRID_AXONOMETRIC:
254 return (CanvasGrid*) new CanvasAxonomGrid(nv, repr, doc);
255 }
257 return NULL;
258 }
261 /**
262 * creates a new grid canvasitem for the SPDesktop given as parameter. Keeps a link to this canvasitem in the canvasitems list.
263 */
264 GridCanvasItem *
265 CanvasGrid::createCanvasItem(SPDesktop * desktop)
266 {
267 if (!desktop) return NULL;
268 //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.
270 // check if there is already a canvasitem on this desktop linking to this grid
271 for (GSList *l = canvasitems; l != NULL; l = l->next) {
272 if ( sp_desktop_gridgroup(desktop) == SP_CANVAS_GROUP(SP_CANVAS_ITEM(l->data)->parent) ) {
273 return NULL;
274 }
275 }
277 GridCanvasItem * item = INKSCAPE_GRID_CANVASITEM( sp_canvas_item_new(sp_desktop_gridgroup(desktop), INKSCAPE_TYPE_GRID_CANVASITEM, NULL) );
278 item->grid = this;
279 sp_canvas_item_show(SP_CANVAS_ITEM(item));
281 gtk_object_ref(GTK_OBJECT(item)); // since we're keeping a link to this item, we need to bump up the ref count
282 canvasitems = g_slist_prepend(canvasitems, item);
284 return item;
285 }
287 void
288 CanvasGrid::on_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, void *data)
289 {
290 if (!data)
291 return;
293 ((CanvasGrid*) data)->onReprAttrChanged(repr, key, oldval, newval, is_interactive);
294 }
298 // ##########################################################
299 // CanvasXYGrid
302 /**
303 * "attach_all" function
304 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
305 *
306 * Helper function that attachs widgets in a 3xn table. The widgets come in an
307 * array that has two entries per table row. The two entries code for four
308 * possible cases: (0,0) means insert space in first column; (0, non-0) means
309 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
310 * (non-0, non-0) means two widgets in columns 2 and 3.
311 **/
312 #define SPACE_SIZE_X 15
313 #define SPACE_SIZE_Y 10
314 static inline void
315 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
316 {
317 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
318 if (arr[i] && arr[i+1]) {
319 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
320 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
321 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
322 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
323 } else {
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 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 } else {
333 Gtk::HBox *space = manage (new Gtk::HBox);
334 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
335 table.attach (*space, 0, 1, r, r+1,
336 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
337 }
338 }
339 ++r;
340 }
341 }
343 CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
344 : CanvasGrid(nv, in_repr, in_doc), table(1, 1)
345 {
346 origin[NR::X] = origin[NR::Y] = 0.0;
347 color = DEFAULTGRIDCOLOR;
348 empcolor = DEFAULTGRIDEMPCOLOR;
349 empspacing = 5;
350 spacing[NR::X] = spacing[NR::Y] = 1.0;
351 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
352 render_dotted = false;
354 snapper = new CanvasXYGridSnapper(this, namedview, 0);
356 // initialize widgets:
357 vbox.set_border_width(2);
358 table.set_spacings(2);
359 vbox.pack_start(table, false, false, 0);
361 _rumg.init (_("Grid _units:"), "units", _wr, repr, doc);
362 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
363 "originx", _rumg, _wr, repr, doc);
364 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
365 "originy", _rumg, _wr, repr, doc);
366 _rsu_sx.init (_("Spacing _X:"), _("Distance between vertical grid lines"),
367 "spacingx", _rumg, _wr, repr, doc);
368 _rsu_sy.init (_("Spacing _Y:"), _("Distance between horizontal grid lines"),
369 "spacingy", _rumg, _wr, repr, doc);
370 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
371 _("Color of grid lines"), "color", "opacity", _wr, repr, doc);
372 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
373 _("Color of the major (highlighted) grid lines"),
374 "empcolor", "empopacity", _wr, repr, doc);
375 _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr, doc);
376 _rcb_dotted.init ( _("_Show dots instead of lines"),
377 _("If set, displays dots at gridpoints instead of gridlines"),
378 "dotted", _wr, false, repr, doc);
380 Gtk::Widget const *const widget_array[] = {
381 _rumg._label, _rumg._sel,
382 0, _rsu_ox.getSU(),
383 0, _rsu_oy.getSU(),
384 0, _rsu_sx.getSU(),
385 0, _rsu_sy.getSU(),
386 _rcp_gcol._label, _rcp_gcol._cp,
387 0, 0,
388 _rcp_gmcol._label, _rcp_gmcol._cp,
389 _rsi._label, &_rsi._hbox,
390 0, _rcb_dotted._button,
391 };
393 attach_all (table, widget_array, sizeof(widget_array));
395 vbox.show();
397 if (repr) readRepr();
398 updateWidgets();
399 }
401 CanvasXYGrid::~CanvasXYGrid ()
402 {
403 if (snapper) delete snapper;
404 }
407 /* fixme: Collect all these length parsing methods and think common sane API */
409 static gboolean
410 sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
411 {
412 if (!str) {
413 return FALSE;
414 }
416 gchar *u;
417 gdouble v = g_ascii_strtod(str, &u);
418 if (!u) {
419 return FALSE;
420 }
421 while (isspace(*u)) {
422 u += 1;
423 }
425 if (!*u) {
426 /* No unit specified - keep default */
427 *val = v;
428 return TRUE;
429 }
431 if (base & SP_UNIT_DEVICE) {
432 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
433 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
434 *val = v;
435 return TRUE;
436 }
437 }
439 if (base & SP_UNIT_ABSOLUTE) {
440 if (!strncmp(u, "pt", 2)) {
441 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
442 } else if (!strncmp(u, "mm", 2)) {
443 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
444 } else if (!strncmp(u, "cm", 2)) {
445 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
446 } else if (!strncmp(u, "m", 1)) {
447 *unit = &sp_unit_get_by_id(SP_UNIT_M);
448 } else if (!strncmp(u, "in", 2)) {
449 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
450 } else {
451 return FALSE;
452 }
453 *val = v;
454 return TRUE;
455 }
457 return FALSE;
458 }
460 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
461 {
462 if (!str) {
463 return FALSE;
464 }
466 gchar *u;
467 gdouble v = g_ascii_strtod(str, &u);
468 if (!u) {
469 return FALSE;
470 }
471 v = CLAMP(v, 0.0, 1.0);
473 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
475 return TRUE;
476 }
478 /** If the passed scalar is invalid (<=0), then set the widget and the scalar
479 to use the given old value.
481 @param oldVal Old value to use if the new one is invalid.
482 @param pTarget The scalar to validate.
483 @param widget Widget associated with the scalar.
484 */
485 static void validateScalar(double oldVal,
486 double* pTarget,
487 Inkscape::UI::Widget::RegisteredScalarUnit& widget)
488 {
489 // Avoid nullness.
490 if ( pTarget == NULL )
491 return;
493 // Invalid new value?
494 if ( *pTarget <= 0 ) {
495 // If the old value is somehow invalid as well, then default to 1.
496 if ( oldVal <= 0 )
497 oldVal = 1;
499 // Reset the scalar and associated widget to the old value.
500 *pTarget = oldVal;
501 widget.setValue( *pTarget);
502 } //if
504 } //validateScalar
507 /** If the passed int is invalid (<=0), then set the widget and the int
508 to use the given old value.
510 @param oldVal Old value to use if the new one is invalid.
511 @param pTarget The int to validate.
512 @param widget Widget associated with the int.
513 */
514 static void validateInt(gint oldVal,
515 gint* pTarget,
516 Inkscape::UI::Widget::RegisteredSuffixedInteger& widget)
517 {
518 // Avoid nullness.
519 if ( pTarget == NULL )
520 return;
522 // Invalid new value?
523 if ( *pTarget <= 0 ) {
524 // If the old value is somehow invalid as well, then default to 1.
525 if ( oldVal <= 0 )
526 oldVal = 1;
528 // Reset the int and associated widget to the old value.
529 *pTarget = oldVal;
530 widget.setValue( *pTarget);
531 } //if
533 } //validateInt
535 void
536 CanvasXYGrid::readRepr()
537 {
538 char buff[100];
539 gchar const *value;
540 if ( (value = repr->attribute("originx")) ) {
541 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
542 origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
544 }
545 if ( (value = repr->attribute("originy")) ) {
546 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
547 origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
548 }
550 if ( (value = repr->attribute("spacingx")) ) {
551 double oldVal = spacing[NR::X];
552 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::X], &gridunit);
553 validateScalar( oldVal, &spacing[NR::X], _rsu_sx );
554 spacing[NR::X] = sp_units_get_pixels(spacing[NR::X], *(gridunit));
556 }
557 if ( (value = repr->attribute("spacingy")) ) {
558 double oldVal = spacing[NR::Y];
559 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::Y], &gridunit);
560 validateScalar( oldVal, &spacing[NR::Y], _rsu_sy );
561 spacing[NR::Y] = sp_units_get_pixels(spacing[NR::Y], *(gridunit));
563 }
565 if ( (value = repr->attribute("color")) ) {
566 color = (color & 0xff) | sp_svg_read_color(value, color);
567 }
569 if ( (value = repr->attribute("empcolor")) ) {
570 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
571 }
573 if ( (value = repr->attribute("opacity")) ) {
574 sp_nv_read_opacity(value, &color);
575 }
576 if ( (value = repr->attribute("empopacity")) ) {
577 sp_nv_read_opacity(value, &empcolor);
578 }
580 if ( (value = repr->attribute("empspacing")) ) {
581 gint oldVal = empspacing;
582 empspacing = atoi(value);
583 validateInt( oldVal, &empspacing, _rsi );
584 }
586 if ( (value = repr->attribute("dotted")) ) {
587 render_dotted = (strcmp(value,"true") == 0);
588 }
590 for (GSList *l = canvasitems; l != NULL; l = l->next) {
591 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
592 }
594 return;
595 }
597 /**
598 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
599 */
600 void
601 CanvasXYGrid::onReprAttrChanged(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive)
602 {
603 readRepr();
605 if ( ! (_wr.isUpdating()) )
606 updateWidgets();
607 }
612 Gtk::Widget &
613 CanvasXYGrid::getWidget()
614 {
615 return vbox;
616 }
619 /**
620 * Update dialog widgets from object's values.
621 */
622 void
623 CanvasXYGrid::updateWidgets()
624 {
625 if (_wr.isUpdating()) return;
627 _wr.setUpdating (true);
629 _rumg.setUnit (gridunit);
631 gdouble val;
632 val = origin[NR::X];
633 val = sp_pixels_get_units (val, *(gridunit));
634 _rsu_ox.setValue (val);
635 val = origin[NR::Y];
636 val = sp_pixels_get_units (val, *(gridunit));
637 _rsu_oy.setValue (val);
638 val = spacing[NR::X];
639 double gridx = sp_pixels_get_units (val, *(gridunit));
640 _rsu_sx.setValue (gridx);
641 val = spacing[NR::Y];
642 double gridy = sp_pixels_get_units (val, *(gridunit));
643 _rsu_sy.setValue (gridy);
645 _rcp_gcol.setRgba32 (color);
646 _rcp_gmcol.setRgba32 (empcolor);
647 _rsi.setValue (empspacing);
649 _wr.setUpdating (false);
651 return;
652 }
656 void
657 CanvasXYGrid::Update (NR::Matrix const &affine, unsigned int flags)
658 {
659 ow = origin * affine;
660 sw = spacing * affine;
661 sw -= NR::Point(affine[4], affine[5]);
663 for(int dim = 0; dim < 2; dim++) {
664 gint scaling_factor = empspacing;
666 if (scaling_factor <= 1)
667 scaling_factor = 5;
669 scaled[dim] = FALSE;
670 sw[dim] = fabs (sw[dim]);
671 while (sw[dim] < 8.0) {
672 scaled[dim] = TRUE;
673 sw[dim] *= scaling_factor;
674 /* First pass, go up to the major line spacing, then
675 keep increasing by two. */
676 scaling_factor = 2;
677 }
678 }
679 }
682 static void
683 grid_hline (SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba)
684 {
685 if ((y >= buf->rect.y0) && (y < buf->rect.y1)) {
686 guint r, g, b, a;
687 gint x0, x1, x;
688 guchar *p;
689 r = NR_RGBA32_R (rgba);
690 g = NR_RGBA32_G (rgba);
691 b = NR_RGBA32_B (rgba);
692 a = NR_RGBA32_A (rgba);
693 x0 = MAX (buf->rect.x0, xs);
694 x1 = MIN (buf->rect.x1, xe + 1);
695 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
696 for (x = x0; x < x1; x++) {
697 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
698 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
699 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
700 p += 3;
701 }
702 }
703 }
705 static void
706 grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
707 {
708 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
709 guint r, g, b, a;
710 gint y0, y1, y;
711 guchar *p;
712 r = NR_RGBA32_R(rgba);
713 g = NR_RGBA32_G (rgba);
714 b = NR_RGBA32_B (rgba);
715 a = NR_RGBA32_A (rgba);
716 y0 = MAX (buf->rect.y0, ys);
717 y1 = MIN (buf->rect.y1, ye + 1);
718 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
719 for (y = y0; y < y1; y++) {
720 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
721 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
722 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
723 p += buf->buf_rowstride;
724 }
725 }
726 }
728 static void
729 grid_dot (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
730 {
731 if ( (y >= buf->rect.y0) && (y < buf->rect.y1)
732 && (x >= buf->rect.x0) && (x < buf->rect.x1) ) {
733 guint r, g, b, a;
734 guchar *p;
735 r = NR_RGBA32_R (rgba);
736 g = NR_RGBA32_G (rgba);
737 b = NR_RGBA32_B (rgba);
738 a = NR_RGBA32_A (rgba);
739 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
740 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
741 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
742 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
743 }
744 }
746 void
747 CanvasXYGrid::Render (SPCanvasBuf *buf)
748 {
749 gdouble const sxg = floor ((buf->rect.x0 - ow[NR::X]) / sw[NR::X]) * sw[NR::X] + ow[NR::X];
750 gint const xlinestart = (gint) Inkscape::round((sxg - ow[NR::X]) / sw[NR::X]);
751 gdouble const syg = floor ((buf->rect.y0 - ow[NR::Y]) / sw[NR::Y]) * sw[NR::Y] + ow[NR::Y];
752 gint const ylinestart = (gint) Inkscape::round((syg - ow[NR::Y]) / sw[NR::Y]);
754 if (!render_dotted) {
755 gint ylinenum;
756 gdouble y;
757 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
758 gint const y0 = (gint) Inkscape::round(y);
760 if (!scaled[NR::Y] && (ylinenum % empspacing) == 0) {
761 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, empcolor);
762 } else {
763 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, color);
764 }
765 }
767 gint xlinenum;
768 gdouble x;
769 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
770 gint const ix = (gint) Inkscape::round(x);
771 if (!scaled[NR::X] && (xlinenum % empspacing) == 0) {
772 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, empcolor);
773 } else {
774 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, color);
775 }
776 }
777 } else {
778 gint ylinenum;
779 gdouble y;
780 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
781 gint const iy = (gint) Inkscape::round(y);
783 gint xlinenum;
784 gdouble x;
785 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
786 gint const ix = (gint) Inkscape::round(x);
787 if ( (!scaled[NR::X] && (xlinenum % empspacing) == 0)
788 || (!scaled[NR::Y] && (ylinenum % empspacing) == 0) )
789 {
790 grid_dot (buf, ix, iy, empcolor | (guint32)0x000000FF); // put alpha to max value
791 } else {
792 grid_dot (buf, ix, iy, color | (guint32)0x000000FF); // put alpha to max value
793 }
794 }
796 }
797 }
798 }
803 /**
804 * \return x rounded to the nearest multiple of c1 plus c0.
805 *
806 * \note
807 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
808 * mean "ignore the grid in this dimention". We're currently discussing "good" semantics
809 * for guide/grid snapping.
810 */
812 /* FIXME: move this somewhere else, perhaps */
813 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
814 {
815 return floor((x - c0) / c1 + .5) * c1 + c0;
816 }
818 CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
819 {
820 this->grid = grid;
821 }
823 LineSnapper::LineList
824 CanvasXYGridSnapper::_getSnapLines(NR::Point const &p) const
825 {
826 LineList s;
828 if ( grid == NULL ) {
829 return s;
830 }
832 for (unsigned int i = 0; i < 2; ++i) {
834 /* This is to make sure we snap to only visible grid lines */
835 double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
837 // convert screen pixels to px
838 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
839 if (SP_ACTIVE_DESKTOP) {
840 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
841 }
843 NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
844 scaled_spacing,
845 grid->origin[i]);
847 s.push_back(std::make_pair(NR::Dim2(i), rounded));
848 }
850 return s;
851 }
857 }; /* namespace Inkscape */
860 /*
861 Local Variables:
862 mode:c++
863 c-file-style:"stroustrup"
864 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
865 indent-tabs-mode:nil
866 fill-column:99
867 End:
868 */
869 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :