cd79e00bd538db52a788a378af98ec080a4065b2
1 #define CANVAS_AXONOMGRID_C
3 /*
4 * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
5 */
7 /*
8 * Current limits are: one axis (y-axis) is always vertical. The other two
9 * axes are bound to a certain range of angles. The z-axis always has an angle
10 * smaller than 90 degrees (measured from horizontal, 0 degrees being a line extending
11 * to the right). The x-axis will always have an angle between 0 and 90 degrees.
12 * When I quickly think about it: all possibilities are probably covered this way. Eg.
13 * a z-axis with negative angle can be replaced with an x-axis, etc.
14 */
16 /*
17 * TODO: LOTS LOTS LOTS. Clean up code. dirty as hell
18 * THIS FILE AND THE HEADER FILE NEED HUGE CLEANING UP. PLEASE DO NOT HESISTATE TO DO SO.
19 * For example: the line drawing code should not be here. There _must_ be a function somewhere else that can provide this functionality...
20 */
22 #include "sp-canvas-util.h"
23 #include "canvas-axonomgrid.h"
24 #include "display-forward.h"
25 #include <libnr/nr-pixops.h>
28 #include "canvas-grid.h"
29 #include "desktop-handles.h"
30 #include "helper/units.h"
31 #include "svg/svg-color.h"
32 #include "xml/node-event-vector.h"
33 #include "sp-object.h"
35 #include "sp-namedview.h"
36 #include "inkscape.h"
37 #include "desktop.h"
39 #include "document.h"
41 #define SAFE_SETPIXEL //undefine this when it is certain that setpixel is never called with invalid params
43 enum Dim3 { X=0, Y, Z };
45 #ifndef M_PI
46 # define M_PI 3.14159265358979323846
47 #endif
49 static double deg_to_rad(double deg) { return deg*M_PI/180.0;}
52 /**
53 \brief This function renders a pixel on a particular buffer.
55 The topleft of the buffer equals
56 ( rect.x0 , rect.y0 ) in screen coordinates
57 ( 0 , 0 ) in setpixel coordinates
58 The bottomright of the buffer equals
59 ( rect.x1 , rect,y1 ) in screen coordinates
60 ( rect.x1 - rect.x0 , rect.y1 - rect.y0 ) in setpixel coordinates
61 */
62 static void
63 sp_caxonomgrid_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
64 {
65 #ifdef SAFE_SETPIXEL
66 if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
67 #endif
68 guint r, g, b, a;
69 r = NR_RGBA32_R (rgba);
70 g = NR_RGBA32_G (rgba);
71 b = NR_RGBA32_B (rgba);
72 a = NR_RGBA32_A (rgba);
73 guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
74 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
75 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
76 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
77 #ifdef SAFE_SETPIXEL
78 }
79 #endif
80 }
82 /**
83 \brief This function renders a line on a particular canvas buffer,
84 using Bresenham's line drawing function.
85 http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
86 Coordinates are interpreted as SCREENcoordinates
87 */
88 static void
89 sp_caxonomgrid_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
90 {
91 int dy = y1 - y0;
92 int dx = x1 - x0;
93 int stepx, stepy;
95 if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
96 if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
97 dy <<= 1; // dy is now 2*dy
98 dx <<= 1; // dx is now 2*dx
100 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
101 if (dx > dy) {
102 int fraction = dy - (dx >> 1); // same as 2*dy - dx
103 while (x0 != x1) {
104 if (fraction >= 0) {
105 y0 += stepy;
106 fraction -= dx; // same as fraction -= 2*dx
107 }
108 x0 += stepx;
109 fraction += dy; // same as fraction -= 2*dy
110 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
111 }
112 } else {
113 int fraction = dx - (dy >> 1);
114 while (y0 != y1) {
115 if (fraction >= 0) {
116 x0 += stepx;
117 fraction -= dy;
118 }
119 y0 += stepy;
120 fraction += dx;
121 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
122 }
123 }
125 }
127 static void
128 sp_grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
129 {
130 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
131 guint r, g, b, a;
132 gint y0, y1, y;
133 guchar *p;
134 r = NR_RGBA32_R(rgba);
135 g = NR_RGBA32_G (rgba);
136 b = NR_RGBA32_B (rgba);
137 a = NR_RGBA32_A (rgba);
138 y0 = MAX (buf->rect.y0, ys);
139 y1 = MIN (buf->rect.y1, ye + 1);
140 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
141 for (y = y0; y < y1; y++) {
142 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
143 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
144 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
145 p += buf->buf_rowstride;
146 }
147 }
148 }
150 namespace Inkscape {
153 /**
154 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
155 *
156 * Helper function that attachs widgets in a 3xn table. The widgets come in an
157 * array that has two entries per table row. The two entries code for four
158 * possible cases: (0,0) means insert space in first column; (0, non-0) means
159 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
160 * (non-0, non-0) means two widgets in columns 2 and 3.
161 **/
162 #define SPACE_SIZE_X 15
163 #define SPACE_SIZE_Y 10
164 static inline void
165 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
166 {
167 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
168 if (arr[i] && arr[i+1]) {
169 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
170 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
171 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
172 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
173 } else {
174 if (arr[i+1]) {
175 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
176 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
177 } else if (arr[i]) {
178 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
179 label.set_alignment (0.0);
180 table.attach (label, 0, 3, r, r+1,
181 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
182 } else {
183 Gtk::HBox *space = manage (new Gtk::HBox);
184 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
185 table.attach (*space, 0, 1, r, r+1,
186 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
187 }
188 }
189 ++r;
190 }
191 }
193 CanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
194 : CanvasGrid(nv, in_repr, in_doc, GRID_AXONOMETRIC), table(1, 1)
195 {
196 origin[NR::X] = origin[NR::Y] = 0.0;
197 color = 0xff3f3f20;
198 empcolor = 0xFF3F3F40;
199 empspacing = 5;
200 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
201 angle_deg[X] = angle_deg[Z] = 30;
202 angle_deg[Y] =0;
203 lengthy = 1;
205 angle_rad[X] = deg_to_rad(angle_deg[X]);
206 tan_angle[X] = tan(angle_rad[X]);
207 angle_rad[Z] = deg_to_rad(angle_deg[Z]);
208 tan_angle[Z] = tan(angle_rad[Z]);
210 snapper = new CanvasAxonomGridSnapper(this, namedview, 0);
212 // initialize widgets:
213 vbox.set_border_width(2);
214 table.set_spacings(2);
215 vbox.pack_start(table, false, false, 0);
217 _rumg.init (_("Grid _units:"), "units", _wr, repr, doc);
218 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
219 "originx", _rumg, _wr, repr, doc);
220 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
221 "originy", _rumg, _wr, repr, doc);
222 _rsu_sy.init (_("Spacing _Y:"), _("Base length of z-axis"),
223 "spacingy", _rumg, _wr, repr, doc);
224 _rsu_ax.init (_("Angle X:"), _("Angle of x-axis"),
225 "gridanglex", _rumg, _wr, repr, doc);
226 _rsu_az.init (_("Angle Z:"), _("Angle of z-axis"),
227 "gridanglez", _rumg, _wr, repr, doc);
228 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
229 _("Color of grid lines"), "color", "opacity", _wr, repr, doc);
230 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
231 _("Color of the major (highlighted) grid lines"),
232 "empcolor", "empopacity", _wr, repr, doc);
233 _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr, doc);
235 Gtk::Widget const *const widget_array[] = {
236 0, _rcbgrid._button,
237 _rumg._label, _rumg._sel,
238 0, _rsu_ox.getSU(),
239 0, _rsu_oy.getSU(),
240 0, _rsu_sy.getSU(),
241 0, _rsu_ax.getSU(),
242 0, _rsu_az.getSU(),
243 _rcp_gcol._label, _rcp_gcol._cp,
244 0, 0,
245 _rcp_gmcol._label, _rcp_gmcol._cp,
246 _rsi._label, &_rsi._hbox,
247 };
249 attach_all (table, widget_array, sizeof(widget_array));
251 vbox.show();
253 if (repr) readRepr();
254 updateWidgets();
255 }
257 CanvasAxonomGrid::~CanvasAxonomGrid ()
258 {
259 if (snapper) delete snapper;
260 }
263 /* fixme: Collect all these length parsing methods and think common sane API */
265 static gboolean sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
266 {
267 if (!str) {
268 return FALSE;
269 }
271 gchar *u;
272 gdouble v = g_ascii_strtod(str, &u);
273 if (!u) {
274 return FALSE;
275 }
276 while (isspace(*u)) {
277 u += 1;
278 }
280 if (!*u) {
281 /* No unit specified - keep default */
282 *val = v;
283 return TRUE;
284 }
286 if (base & SP_UNIT_DEVICE) {
287 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
288 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
289 *val = v;
290 return TRUE;
291 }
292 }
294 if (base & SP_UNIT_ABSOLUTE) {
295 if (!strncmp(u, "pt", 2)) {
296 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
297 } else if (!strncmp(u, "mm", 2)) {
298 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
299 } else if (!strncmp(u, "cm", 2)) {
300 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
301 } else if (!strncmp(u, "m", 1)) {
302 *unit = &sp_unit_get_by_id(SP_UNIT_M);
303 } else if (!strncmp(u, "in", 2)) {
304 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
305 } else {
306 return FALSE;
307 }
308 *val = v;
309 return TRUE;
310 }
312 return FALSE;
313 }
315 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
316 {
317 if (!str) {
318 return FALSE;
319 }
321 gchar *u;
322 gdouble v = g_ascii_strtod(str, &u);
323 if (!u) {
324 return FALSE;
325 }
326 v = CLAMP(v, 0.0, 1.0);
328 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
330 return TRUE;
331 }
335 void
336 CanvasAxonomGrid::readRepr()
337 {
338 gchar const *value;
339 if ( (value = repr->attribute("originx")) ) {
340 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
341 origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
342 }
343 if ( (value = repr->attribute("originy")) ) {
344 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
345 origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
346 }
348 if ( (value = repr->attribute("spacingy")) ) {
349 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &lengthy, &gridunit);
350 lengthy = sp_units_get_pixels(lengthy, *(gridunit));
351 if (lengthy < 1.0) lengthy = 1.0;
352 }
354 if ( (value = repr->attribute("gridanglex")) ) {
355 angle_deg[X] = g_ascii_strtod(value, NULL);
356 if (angle_deg[X] < 1.0) angle_deg[X] = 1.0;
357 if (angle_deg[X] > 89.0) angle_deg[X] = 89.0;
358 angle_rad[X] = deg_to_rad(angle_deg[X]);
359 tan_angle[X] = tan(angle_rad[X]);
360 }
362 if ( (value = repr->attribute("gridanglez")) ) {
363 angle_deg[Z] = g_ascii_strtod(value, NULL);
364 if (angle_deg[Z] < 1.0) angle_deg[Z] = 1.0;
365 if (angle_deg[Z] > 89.0) angle_deg[Z] = 89.0;
366 angle_rad[Z] = deg_to_rad(angle_deg[Z]);
367 tan_angle[Z] = tan(angle_rad[Z]);
368 }
370 if ( (value = repr->attribute("color")) ) {
371 color = (color & 0xff) | sp_svg_read_color(value, color);
372 }
374 if ( (value = repr->attribute("empcolor")) ) {
375 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
376 }
378 if ( (value = repr->attribute("opacity")) ) {
379 sp_nv_read_opacity(value, &color);
380 }
381 if ( (value = repr->attribute("empopacity")) ) {
382 sp_nv_read_opacity(value, &empcolor);
383 }
385 if ( (value = repr->attribute("empspacing")) ) {
386 empspacing = atoi(value);
387 }
389 if ( (value = repr->attribute("visible")) ) {
390 visible = (strcmp(value,"true") == 0);
391 }
393 if ( (value = repr->attribute("snap_enabled")) ) {
394 snap_enabled = (strcmp(value,"true") == 0);
395 }
397 for (GSList *l = canvasitems; l != NULL; l = l->next) {
398 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
399 }
400 return;
401 }
403 /**
404 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
405 */
406 void
407 CanvasAxonomGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
408 {
409 readRepr();
411 if ( ! (_wr.isUpdating()) )
412 updateWidgets();
413 }
418 Gtk::Widget &
419 CanvasAxonomGrid::getWidget()
420 {
421 return vbox;
422 }
425 /**
426 * Update dialog widgets from object's values.
427 */
428 void
429 CanvasAxonomGrid::updateWidgets()
430 {
431 if (_wr.isUpdating()) return;
433 _wr.setUpdating (true);
435 _rcb_visible.setActive(visible);
436 _rcb_snap_enabled.setActive(snap_enabled);
438 _rumg.setUnit (gridunit);
440 gdouble val;
441 val = origin[NR::X];
442 val = sp_pixels_get_units (val, *(gridunit));
443 _rsu_ox.setValue (val);
444 val = origin[NR::Y];
445 val = sp_pixels_get_units (val, *(gridunit));
446 _rsu_oy.setValue (val);
447 val = lengthy;
448 double gridy = sp_pixels_get_units (val, *(gridunit));
449 _rsu_sy.setValue (gridy);
451 _rsu_ax.setValue(angle_deg[X]);
452 _rsu_az.setValue(angle_deg[Z]);
454 _rcp_gcol.setRgba32 (color);
455 _rcp_gmcol.setRgba32 (empcolor);
456 _rsi.setValue (empspacing);
458 _wr.setUpdating (false);
460 return;
461 }
465 void
466 CanvasAxonomGrid::Update (NR::Matrix const &affine, unsigned int /*flags*/)
467 {
468 ow = origin * affine;
469 sw = NR::Point(fabs(affine[0]),fabs(affine[3]));
471 for(int dim = 0; dim < 2; dim++) {
472 gint scaling_factor = empspacing;
474 if (scaling_factor <= 1)
475 scaling_factor = 5;
477 scaled = FALSE;
478 int watchdog = 0;
479 while ( (sw[dim] < 8.0) & (watchdog < 100) ) {
480 scaled = TRUE;
481 sw[dim] *= scaling_factor;
482 // First pass, go up to the major line spacing, then
483 // keep increasing by two.
484 scaling_factor = 2;
485 watchdog++;
486 }
488 }
490 spacing_ylines = sw[NR::X] * lengthy /(tan_angle[X] + tan_angle[Z]);
491 lyw = sw[NR::Y] * lengthy;
492 lxw_x = (lengthy / tan_angle[X]) * sw[NR::X];
493 lxw_z = (lengthy / tan_angle[Z]) * sw[NR::X];
495 if (empspacing == 0) {
496 scaled = TRUE;
497 }
499 }
501 void
502 CanvasAxonomGrid::Render (SPCanvasBuf *buf)
503 {
504 // gc = gridcoordinates (the coordinates calculated from the grids origin 'grid->ow'.
505 // sc = screencoordinates ( for example "buf->rect.x0" is in screencoordinates )
506 // bc = buffer patch coordinates
508 // tl = topleft ; br = bottomright
509 NR::Point buf_tl_gc;
510 NR::Point buf_br_gc;
511 buf_tl_gc[NR::X] = buf->rect.x0 - ow[NR::X];
512 buf_tl_gc[NR::Y] = buf->rect.y0 - ow[NR::Y];
513 buf_br_gc[NR::X] = buf->rect.x1 - ow[NR::X];
514 buf_br_gc[NR::Y] = buf->rect.y1 - ow[NR::Y];
516 gdouble x;
517 gdouble y;
519 // render the three separate line groups representing the main-axes
521 // x-axis always goes from topleft to bottomright. (0,0) - (1,1)
522 gdouble const xintercept_y_bc = (buf_tl_gc[NR::X] * tan_angle[X]) - buf_tl_gc[NR::Y] ;
523 gdouble const xstart_y_sc = ( xintercept_y_bc - floor(xintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
524 gint const xlinestart = (gint) Inkscape::round( (xstart_y_sc - buf->rect.x0*tan_angle[X] -ow[NR::Y]) / lyw );
525 gint xlinenum = xlinestart;
526 // lines starting on left side.
527 for (y = xstart_y_sc; y < buf->rect.y1; y += lyw, xlinenum++) {
528 gint const x0 = buf->rect.x0;
529 gint const y0 = (gint) Inkscape::round(y);
530 gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - y) / tan_angle[X] );
531 gint const y1 = buf->rect.y1;
533 if (!scaled && (xlinenum % empspacing) == 0) {
534 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
535 } else {
536 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
537 }
538 }
539 // lines starting from top side
540 gdouble const xstart_x_sc = buf->rect.x0 + (lxw_x - (xstart_y_sc - buf->rect.y0) / tan_angle[X]) ;
541 xlinenum = xlinestart-1;
542 for (x = xstart_x_sc; x < buf->rect.x1; x += lxw_x, xlinenum--) {
543 gint const y0 = buf->rect.y0;
544 gint const y1 = buf->rect.y1;
545 gint const x0 = (gint) Inkscape::round(x);
546 gint const x1 = x0 + (gint) Inkscape::round( (y1 - y0) / tan_angle[X] );
548 if (!scaled && (xlinenum % empspacing) == 0) {
549 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
550 } else {
551 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
552 }
553 }
555 // y-axis lines (vertical)
556 gdouble const ystart_x_sc = floor (buf_tl_gc[NR::X] / spacing_ylines) * spacing_ylines + ow[NR::X];
557 gint const ylinestart = (gint) Inkscape::round((ystart_x_sc - ow[NR::X]) / spacing_ylines);
558 gint ylinenum = ylinestart;
559 for (x = ystart_x_sc; x < buf->rect.x1; x += spacing_ylines, ylinenum++) {
560 gint const x0 = (gint) Inkscape::round(x);
562 if (!scaled && (ylinenum % empspacing) == 0) {
563 sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, empcolor);
564 } else {
565 sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, color);
566 }
567 }
569 // z-axis always goes from bottomleft to topright. (0,1) - (1,0)
570 gdouble const zintercept_y_bc = (buf_tl_gc[NR::X] * -tan_angle[Z]) - buf_tl_gc[NR::Y] ;
571 gdouble const zstart_y_sc = ( zintercept_y_bc - floor(zintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
572 gint const zlinestart = (gint) Inkscape::round( (zstart_y_sc + buf->rect.x0*tan_angle[X] - ow[NR::Y]) / lyw );
573 gint zlinenum = zlinestart;
574 // lines starting from left side
575 for (y = zstart_y_sc; y < buf->rect.y1; y += lyw, zlinenum++) {
576 gint const x0 = buf->rect.x0;
577 gint const y0 = (gint) Inkscape::round(y);
578 gint const x1 = x0 + (gint) Inkscape::round( (y - buf->rect.y0 ) / tan_angle[Z] );
579 gint const y1 = buf->rect.y0;
581 if (!scaled && (zlinenum % empspacing) == 0) {
582 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
583 } else {
584 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
585 }
586 }
587 // draw lines from bottom-up
588 gdouble const zstart_x_sc = buf->rect.x0 + (y - buf->rect.y1) / tan_angle[Z] ;
589 for (x = zstart_x_sc; x < buf->rect.x1; x += lxw_z, zlinenum++) {
590 gint const y0 = buf->rect.y1;
591 gint const y1 = buf->rect.y0;
592 gint const x0 = (gint) Inkscape::round(x);
593 gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - buf->rect.y0) / tan_angle[Z] );
595 if (!scaled && (zlinenum % empspacing) == 0) {
596 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
597 } else {
598 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
599 }
600 }
601 }
614 /**
615 * \return x rounded to the nearest multiple of c1 plus c0.
616 *
617 * \note
618 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
619 * mean "ignore the grid in this dimention". We're currently discussing "good" semantics
620 * for guide/grid snapping.
621 */
623 /* FIXME: move this somewhere else, perhaps */
624 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
625 {
626 return floor((x - c0) / c1 + .5) * c1 + c0;
627 }
629 CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
630 {
631 this->grid = grid;
632 }
634 LineSnapper::LineList
635 CanvasAxonomGridSnapper::_getSnapLines(NR::Point const &p) const
636 {
637 LineList s;
639 if ( grid == NULL ) {
640 return s;
641 }
643 /* This is to make sure we snap to only visible grid lines */
644 double scaled_spacing = grid->spacing_ylines; // this is spacing of visible lines if screen pixels
646 // convert screen pixels to px
647 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
648 if (SP_ACTIVE_DESKTOP) {
649 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
650 }
652 NR::Coord const rounded = round_to_nearest_multiple_plus(p[0],
653 scaled_spacing,
654 grid->origin[0]);
656 s.push_back(std::make_pair(NR::Dim2(0), rounded));
658 return s;
659 }
661 void CanvasAxonomGridSnapper::_addSnappedLine(SnappedConstraints &sc, NR::Point const snapped_point, NR::Coord const snapped_distance, NR::Point const normal_to_line, NR::Point const point_on_line) const
662 {
663 SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, normal_to_line, point_on_line);
664 sc.grid_lines.push_back(dummy);
665 }
669 }; // namespace Inkscape
672 /*
673 Local Variables:
674 mode:c++
675 c-file-style:"stroustrup"
676 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
677 indent-tabs-mode:nil
678 fill-column:99
679 End:
680 */
681 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :