ruler.cpp revision c830d132ea7f29e73b55f87badff602b065f8eac
#define __SP_RULER_C__
/*
* Customized ruler class for inkscape
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Frank Felfe <innerspace@iname.com>
* bulia byak <buliabyak@users.sf.net>
* Diederik van Lierop <mail@diedenrezi.nl>
*
* Copyright (C) 1999-2008 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <cstring>
#include <cmath>
#include <cstdio>
#include "widget-sizes.h"
#include "desktop-widget.h"
#include "ruler.h"
#include "unit-constants.h"
#include "round.h"
#define MINIMUM_INCR 5
#define MAXIMUM_SUBDIVIDE 5
#define MAXIMUM_SCALES 10
#define UNUSED_PIXELS 2 // There appear to be two pixels that are not being used at each end of the ruler
static GtkWidgetClass *hruler_parent_class;
sp_hruler_get_type (void)
{
//TODO: switch to GObject
// GtkType and such calls were deprecated a while back with the
// introduction of GObject as a separate layer, with GType instead. --JonCruz
static GtkType hruler_type = 0;
if (!hruler_type)
{
static const GtkTypeInfo hruler_info =
{
(gchar*) "SPHRuler",
sizeof (SPHRuler),
sizeof (SPHRulerClass),
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
};
}
return hruler_type;
}
static void
{
}
static void
{
}
sp_hruler_new (void)
{
}
static gint
{
double x = event->x; //Although event->x is double according to the docs, it only appears to return integers
ruler->position = ruler->lower + (ruler->upper - ruler->lower) * (x + UNUSED_PIXELS) / (widget->allocation.width + 2*UNUSED_PIXELS);
/* Make sure the ruler has been allocated already */
return FALSE;
}
static void
{
gint i, tick_index;
double increment; /* Number of pixels per unit */
double subd_incr;
if (!GTK_WIDGET_DRAWABLE (ruler))
return;
// Create the pango layout
width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
0, 0,
upper = ruler->upper / ruler->metric->pixels_per_unit; // upper and lower are expressed in ruler units
/* "pixels_per_unit" should be "points_per_unit". This is the size of the unit
* in 1/72nd's of an inch and has nothing to do with screen pixels */
return;
/* determine the scale
* We calculate the text size as for the vruler instead of using
* text_width = gdk_string_width(font, unit_str), so that the result
* for the scale looks consistent with an accompanying vruler
*/
break;
if (scale == MAXIMUM_SCALES)
/* drawing starts here */
length = 0;
for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--)
{
continue;
/* Calculate the length of the tickmarks. Make sure that
* this length increases for each set of ticks
*/
if (ideal_length > ++length)
{
}
else
{
}
tick_index = 0;
cur = start; // location (in ruler units) of the first invisible tick at the left side of the canvas
{
// due to the typical values for cur, lower and increment, pos will often end up to
// be e.g. 641.50000000000; rounding behaviour is not defined in such a case (see round.h)
// and jitter will be apparent (upon redrawing some of the lines on the ruler might jump a
// by a pixel, and jump back on the next redraw). This is suppressed by adding 1e-9 (that's only one nanopixel ;-))
/* draw label */
double label_spacing_px = (increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i];
if (i == 0 &&
{
else
}
/* Calculate cur from start rather than incrementing by subd_incr
* in each iteration. This is to avoid propagation of floating point
* errors in subd_incr.
*/
++tick_index;
}
}
}
static void
{
int i;
gint x, y;
if (GTK_WIDGET_DRAWABLE (ruler))
{
width = widget->allocation.width; // in pixels; is apparently 2 pixels shorter than the canvas at each end
{
/* If a backing store exists, restore the ruler */
// Calculate the coordinates (x, y, in pixels) of the tip of the triangle
x = int(Inkscape::round((ruler->position - ruler->lower) * increment + double(xthickness - bs_width) / 2.0) - UNUSED_PIXELS);
for (i = 0; i < bs_height; i++)
x + i, y + i,
x + bs_width - 1 - i, y + i);
}
}
}
/**
* The hruler widget's size_allocate callback.
*/
static void
{
// First call the default gtk_widget_size_allocate() method (which is being overridden here)
// Now the size of the ruler has changed, the ruler bounds (upper & lower) need to be updated
// For this we first need to obtain a pointer to the desktop, by walking up the tree of ancestors
do {
if (SP_IS_DESKTOP_WIDGET(parent)) {
// Now we've found the desktop widget we can have the ruler boundaries updated
// If the size of the ruler has increased, then a blank part is uncovered; therefore
// it must be redrawn
break;
}
}
// vruler
static GtkWidgetClass *vruler_parent_class;
sp_vruler_get_type (void)
{
//TODO: switch to GObject
// GtkType and such calls were deprecated a while back with the
// introduction of GObject as a separate layer, with GType instead. --JonCruz
static GtkType vruler_type = 0;
if (!vruler_type)
{
static const GtkTypeInfo vruler_info =
{
(gchar*) "SPVRuler",
sizeof (SPVRuler),
sizeof (SPVRulerClass),
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
};
}
return vruler_type;
}
static void
{
}
static void
{
}
sp_vruler_new (void)
{
}
static gint
{
double y = event->y; //Although event->y is double according to the docs, it only appears to return integers
ruler->position = ruler->lower + (ruler->upper - ruler->lower) * (y + UNUSED_PIXELS) / (widget->allocation.height + 2*UNUSED_PIXELS);
/* Make sure the ruler has been allocated already */
return FALSE;
}
static void
{
gint i, j, tick_index;
double increment; /* Number of pixels per unit */
double subd_incr;
if (!GTK_WIDGET_DRAWABLE (ruler))
return;
// Create the pango layout
width = widget->allocation.height; //in pixels; is apparently 2 pixels shorter than the canvas at each end
0, 0,
upper = ruler->upper / ruler->metric->pixels_per_unit; // upper and lower are expressed in ruler units
/* "pixels_per_unit" should be "points_per_unit". This is the size of the unit
* in 1/72nd's of an inch and has nothing to do with screen pixels */
return;
/* determine the scale
* use the maximum extents of the ruler to determine the largest
* possible number to be displayed. Calculate the height in pixels
* of this displayed text. Use this height to find a scale which
* leaves sufficient room for drawing the ruler.
*/
break;
if (scale == MAXIMUM_SCALES)
/* drawing starts here */
length = 0;
for (i = MAXIMUM_SUBDIVIDE - 1; i >= 0; i--) {
continue;
/* Calculate the length of the tickmarks. Make sure that
* this length increases for each set of ticks
*/
if (ideal_length > ++length)
{
}
else
{
}
tick_index = 0;
// due to the typical values for cur, lower and increment, pos will often end up to
// be e.g. 641.50000000000; rounding behaviour is not defined in such a case (see round.h)
// and jitter will be apparent (upon redrawing some of the lines on the ruler might jump a
// by a pixel, and jump back on the next redraw). This is suppressed by adding 1e-9 (that's only one nanopixel ;-))
/* draw label */
double label_spacing_px = fabs((increment*(double)ruler->metric->ruler_scale[scale])/ruler->metric->subdivide[i]);
if (i == 0 &&
{
else
{
xthickness + 1,
}
}
/* Calculate cur from start rather than incrementing by subd_incr
* in each iteration. This is to avoid propagation of floating point
* errors in subd_incr.
*/
++tick_index;
}
}
}
static void
{
int i;
gint x, y;
if (GTK_WIDGET_DRAWABLE (ruler))
{
height = widget->allocation.height; // in pixels; is apparently 2 pixels shorter than the canvas at each end
{
/* If a backing store exists, restore the ruler */
// Calculate the coordinates (x, y, in pixels) of the tip of the triangle
y = int(Inkscape::round((ruler->position - ruler->lower) * increment + double(ythickness - bs_height) / 2.0) - UNUSED_PIXELS);
for (i = 0; i < bs_width; i++)
x + i, y + i,
x + i, y + bs_height - 1 - i);
}
}
}
/**
* The vruler widget's size_allocate callback.
*/
static void
{
// First call the default gtk_widget_size_allocate() method (which is being overridden here)
// Now the size of the ruler has changed, the ruler bounds (upper & lower) need to be updated
// For this we first need to obtain a pointer to the desktop, by walking up the tree of ancestors
do {
if (SP_IS_DESKTOP_WIDGET(parent)) {
// Now we've found the desktop widget we can have the ruler boundaries updated
// If the size of the ruler has increased, then a blank part is uncovered; therefore
// it must be redrawn
break;
}
}
//TODO: warning: deprecated conversion from string constant to ‘gchar*’
//
//Turn out to be warnings that we should probably leave in place. The
// code, those warnings are actually desired. They say "Hey! Fix this". We
/// Ruler metrics.
static GtkRulerMetric const sp_ruler_metrics[] = {
// NOTE: the order of records in this struct must correspond to the SPMetric enum.
{"NONE", "", 1, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
{"millimeters", "mm", PX_PER_MM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
{"centimeters", "cm", PX_PER_CM, { 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000 }, { 1, 5, 10, 50, 100 }},
};
void
{
if (metric == 0)
return;
if (GTK_WIDGET_DRAWABLE (ruler))
}