summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthew Petroff <matthew@mpetroff.net>2013-07-02 03:40:52 +0000
committerMatthew Petroff <matthew@mpetroff.net>2013-07-02 03:40:52 +0000
commitbd77ef25e9161acb007323f851ba77e106036939 (patch)
tree4c38776379d63e26de436a85476d73566205476d /src
parentRenamed Length class to Quantity class, fixed bugs, and added functions. (diff)
downloadinkscape-bd77ef25e9161acb007323f851ba77e106036939.tar.gz
inkscape-bd77ef25e9161acb007323f851ba77e106036939.zip
Ported "ui/widget/page-sizer.cpp" and "document.cpp" to "Util::Unit" class.
(bzr r12380.1.4)
Diffstat (limited to 'src')
-rw-r--r--src/document.cpp32
-rw-r--r--src/document.h8
-rw-r--r--src/measure-context.cpp2
-rw-r--r--src/sp-namedview.cpp11
-rw-r--r--src/sp-namedview.h3
-rw-r--r--src/ui/widget/page-sizer.cpp159
-rw-r--r--src/ui/widget/page-sizer.h9
7 files changed, 114 insertions, 110 deletions
diff --git a/src/document.cpp b/src/document.cpp
index 706710cfc..cc1c519fc 100644
--- a/src/document.cpp
+++ b/src/document.cpp
@@ -49,7 +49,6 @@
#include "display/drawing-item.h"
#include "document-private.h"
#include "document-undo.h"
-#include "helper/units.h"
#include "id-clash.h"
#include "inkscape-private.h"
#include "inkscape-version.h"
@@ -87,6 +86,8 @@ static gint doc_count = 0;
static unsigned long next_serial = 0;
+static Inkscape::Util::UnitTable unit_table;
+
SPDocument::SPDocument() :
keepalive(FALSE),
virgin(TRUE),
@@ -546,21 +547,22 @@ gdouble SPDocument::getWidth() const
return result;
}
-void SPDocument::setWidth(gdouble width, const SPUnit *unit)
+void SPDocument::setWidth(gdouble width, const Inkscape::Util::Unit *unit)
{
+ Inkscape::Util::Unit px = unit_table.getUnit("px");
if (root->width.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
- root->viewBox.setMax(Geom::Point(root->viewBox.left() + sp_units_get_pixels (width, *unit), root->viewBox.bottom()));
+ root->viewBox.setMax(Geom::Point(root->viewBox.left() + Inkscape::Util::Quantity::convert(width, unit, &px), root->viewBox.bottom()));
} else { // set to width=
gdouble old_computed = root->width.computed;
- root->width.computed = sp_units_get_pixels (width, *unit);
+ root->width.computed = Inkscape::Util::Quantity::convert(width, unit, &px);
/* SVG does not support meters as a unit, so we must translate meters to
* cm when writing */
- if (!strcmp(unit->abbr, "m")) {
+ if (*unit == unit_table.getUnit("m")) {
root->width.value = 100*width;
root->width.unit = SVGLength::CM;
} else {
root->width.value = width;
- root->width.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
+ root->width.unit = (SVGLength::Unit) unit->svgUnit();
}
if (root->viewBox_set)
@@ -582,21 +584,22 @@ gdouble SPDocument::getHeight() const
return result;
}
-void SPDocument::setHeight(gdouble height, const SPUnit *unit)
+void SPDocument::setHeight(gdouble height, const Inkscape::Util::Unit *unit)
{
+ Inkscape::Util::Unit px = unit_table.getUnit("px");
if (root->height.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
- root->viewBox.setMax(Geom::Point(root->viewBox.right(), root->viewBox.top() + sp_units_get_pixels (height, *unit)));
+ root->viewBox.setMax(Geom::Point(root->viewBox.right(), root->viewBox.top() + Inkscape::Util::Quantity::convert(height, unit, &px)));
} else { // set to height=
gdouble old_computed = root->height.computed;
- root->height.computed = sp_units_get_pixels (height, *unit);
+ root->height.computed = Inkscape::Util::Quantity::convert(height, unit, &px);
/* SVG does not support meters as a unit, so we must translate meters to
* cm when writing */
- if (!strcmp(unit->abbr, "m")) {
+ if (*unit == unit_table.getUnit("m")) {
root->height.value = 100*height;
root->height.unit = SVGLength::CM;
} else {
root->height.value = height;
- root->height.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
+ root->height.unit = (SVGLength::Unit) unit->svgUnit();
}
if (root->viewBox_set)
@@ -626,7 +629,7 @@ void SPDocument::fitToRect(Geom::Rect const &rect, bool with_margins)
double const h = rect.height();
double const old_height = getHeight();
- SPUnit const &px(sp_unit_get_by_id(SP_UNIT_PX));
+ Inkscape::Util::Unit const px = unit_table.getUnit("px");
/* in px */
double margin_top = 0.0;
@@ -639,9 +642,10 @@ void SPDocument::fitToRect(Geom::Rect const &rect, bool with_margins)
if (with_margins && nv) {
if (nv != NULL) {
gchar const * const units_abbr = nv->getAttribute("units");
- SPUnit const *margin_units = NULL;
+ Inkscape::Util::Unit const *margin_units = NULL;
if (units_abbr != NULL) {
- margin_units = sp_unit_get_by_abbreviation(units_abbr);
+ Inkscape::Util::Unit mu = unit_table.getUnit(units_abbr);
+ margin_units = &mu;
}
if (margin_units == NULL) {
margin_units = &px;
diff --git a/src/document.h b/src/document.h
index 606a83be8..c2334bbc3 100644
--- a/src/document.h
+++ b/src/document.h
@@ -26,6 +26,7 @@
#include "gc-anchored.h"
#include <glibmm/ustring.h>
#include <vector>
+#include "util/units.h"
namespace Avoid {
class Router;
@@ -35,7 +36,6 @@ class SPItem;
class SPObject;
struct SPGroup;
struct SPRoot;
-struct SPUnit;
namespace Inkscape {
struct Application;
@@ -228,8 +228,8 @@ public:
gdouble getWidth() const;
gdouble getHeight() const;
Geom::Point getDimensions() const;
- void setWidth(gdouble width, const SPUnit *unit);
- void setHeight(gdouble height, const SPUnit *unit);
+ void setWidth(gdouble width, const Inkscape::Util::Unit *unit);
+ void setHeight(gdouble height, const Inkscape::Util::Unit *unit);
void requestModified();
gint ensureUpToDate();
bool addResource(const gchar *key, SPObject *object);
@@ -253,8 +253,6 @@ private:
void setupViewport(SPItemCtx *ctx);
};
-struct SPUnit;
-
/*
* Ideas: How to overcome style invalidation nightmare
*
diff --git a/src/measure-context.cpp b/src/measure-context.cpp
index dc23cf5c6..9d60d38e8 100644
--- a/src/measure-context.cpp
+++ b/src/measure-context.cpp
@@ -13,7 +13,7 @@
#include <gdk/gdkkeysyms.h>
#include <boost/none_t.hpp>
-#include "helper/units.h"
+#include "util/units.h"
#include "macros.h"
#include "display/curve.h"
#include "sp-shape.h"
diff --git a/src/sp-namedview.cpp b/src/sp-namedview.cpp
index 452f57425..5184d37a9 100644
--- a/src/sp-namedview.cpp
+++ b/src/sp-namedview.cpp
@@ -1101,20 +1101,23 @@ bool SPNamedView::getGuides()
* \return the margin size in px, else 0.0 if anything is invalid.
*/
double SPNamedView::getMarginLength(gchar const * const key,
- SPUnit const * const margin_units,
- SPUnit const * const return_units,
+ Inkscape::Util::Unit const * const margin_units,
+ Inkscape::Util::Unit const * const return_units,
double const width,
double const height,
bool const use_width)
{
double value;
+ static Inkscape::Util::UnitTable unit_table;
+ Inkscape::Util::Unit percent = unit_table.getUnit("%");
if(!this->storeAsDouble(key,&value)) {
return 0.0;
}
- if (margin_units == &sp_unit_get_by_id (SP_UNIT_PERCENT)) {
+ if (*margin_units == percent) {
return (use_width)? width * value : height * value;
}
- if (!sp_convert_distance (&value, margin_units, return_units)) {
+// if (!sp_convert_distance (&value, margin_units, return_units)) {
+ if (!margin_units->compatibleWith(return_units)) {
return 0.0;
}
return value;
diff --git a/src/sp-namedview.h b/src/sp-namedview.h
index 8191ef6d6..a84368c86 100644
--- a/src/sp-namedview.h
+++ b/src/sp-namedview.h
@@ -24,6 +24,7 @@
#include "sp-metric.h"
#include "snap.h"
#include "document.h"
+#include "util/units.h"
G_BEGIN_DECLS
@@ -94,7 +95,7 @@ struct SPNamedView : public SPObjectGroup {
bool getGuides();
private:
- double getMarginLength(gchar const * const key,SPUnit const * const margin_units,SPUnit const * const return_units,double const width,double const height,bool const use_width);
+ double getMarginLength(gchar const * const key,Inkscape::Util::Unit const * const margin_units,Inkscape::Util::Unit const * const return_units,double const width,double const height,bool const use_width);
friend class SPDocument;
};
diff --git a/src/ui/widget/page-sizer.cpp b/src/ui/widget/page-sizer.cpp
index fa3f8e3a1..cceb3f600 100644
--- a/src/ui/widget/page-sizer.cpp
+++ b/src/ui/widget/page-sizer.cpp
@@ -36,7 +36,7 @@
#include "document.h"
#include "desktop.h"
#include "helper/action.h"
-#include "helper/units.h"
+#include "util/units.h"
#include "inkscape.h"
#include "sp-namedview.h"
#include "sp-root.h"
@@ -46,6 +46,8 @@
#include "xml/node.h"
#include "xml/repr.h"
+static Inkscape::Util::UnitTable unit_table;
+
using std::pair;
namespace Inkscape {
@@ -95,7 +97,7 @@ struct PaperSizeRec {
char const * const name; //name
double const smaller; //lesser dimension
double const larger; //greater dimension
- SPUnitId const unit; //units
+ Inkscape::Util::Unit const unit; //units
};
// list of page formats that should be in landscape automatically
@@ -113,31 +115,31 @@ fill_landscape_papers() {
}
static PaperSizeRec const inkscape_papers[] = {
- { "A4", 210, 297, SP_UNIT_MM },
- { "US Letter", 8.5, 11, SP_UNIT_IN },
- { "US Legal", 8.5, 14, SP_UNIT_IN },
- { "US Executive", 7.25, 10.5, SP_UNIT_IN },
- { "A0", 841, 1189, SP_UNIT_MM },
- { "A1", 594, 841, SP_UNIT_MM },
- { "A2", 420, 594, SP_UNIT_MM },
- { "A3", 297, 420, SP_UNIT_MM },
- { "A5", 148, 210, SP_UNIT_MM },
- { "A6", 105, 148, SP_UNIT_MM },
- { "A7", 74, 105, SP_UNIT_MM },
- { "A8", 52, 74, SP_UNIT_MM },
- { "A9", 37, 52, SP_UNIT_MM },
- { "A10", 26, 37, SP_UNIT_MM },
- { "B0", 1000, 1414, SP_UNIT_MM },
- { "B1", 707, 1000, SP_UNIT_MM },
- { "B2", 500, 707, SP_UNIT_MM },
- { "B3", 353, 500, SP_UNIT_MM },
- { "B4", 250, 353, SP_UNIT_MM },
- { "B5", 176, 250, SP_UNIT_MM },
- { "B6", 125, 176, SP_UNIT_MM },
- { "B7", 88, 125, SP_UNIT_MM },
- { "B8", 62, 88, SP_UNIT_MM },
- { "B9", 44, 62, SP_UNIT_MM },
- { "B10", 31, 44, SP_UNIT_MM },
+ { "A4", 210, 297, unit_table.getUnit("mm") },
+ { "US Letter", 8.5, 11, unit_table.getUnit("in") },
+ { "US Legal", 8.5, 14, unit_table.getUnit("in") },
+ { "US Executive", 7.25, 10.5, unit_table.getUnit("in") },
+ { "A0", 841, 1189, unit_table.getUnit("mm") },
+ { "A1", 594, 841, unit_table.getUnit("mm") },
+ { "A2", 420, 594, unit_table.getUnit("mm") },
+ { "A3", 297, 420, unit_table.getUnit("mm") },
+ { "A5", 148, 210, unit_table.getUnit("mm") },
+ { "A6", 105, 148, unit_table.getUnit("mm") },
+ { "A7", 74, 105, unit_table.getUnit("mm") },
+ { "A8", 52, 74, unit_table.getUnit("mm") },
+ { "A9", 37, 52, unit_table.getUnit("mm") },
+ { "A10", 26, 37, unit_table.getUnit("mm") },
+ { "B0", 1000, 1414, unit_table.getUnit("mm") },
+ { "B1", 707, 1000, unit_table.getUnit("mm") },
+ { "B2", 500, 707, unit_table.getUnit("mm") },
+ { "B3", 353, 500, unit_table.getUnit("mm") },
+ { "B4", 250, 353, unit_table.getUnit("mm") },
+ { "B5", 176, 250, unit_table.getUnit("mm") },
+ { "B6", 125, 176, unit_table.getUnit("mm") },
+ { "B7", 88, 125, unit_table.getUnit("mm") },
+ { "B8", 62, 88, unit_table.getUnit("mm") },
+ { "B9", 44, 62, unit_table.getUnit("mm") },
+ { "B10", 31, 44, unit_table.getUnit("mm") },
@@ -149,63 +151,63 @@ static PaperSizeRec const inkscape_papers[] = {
don't know what D and E series are used for.
*/
- { "C0", 917, 1297, SP_UNIT_MM },
- { "C1", 648, 917, SP_UNIT_MM },
- { "C2", 458, 648, SP_UNIT_MM },
- { "C3", 324, 458, SP_UNIT_MM },
- { "C4", 229, 324, SP_UNIT_MM },
- { "C5", 162, 229, SP_UNIT_MM },
- { "C6", 114, 162, SP_UNIT_MM },
- { "C7", 81, 114, SP_UNIT_MM },
- { "C8", 57, 81, SP_UNIT_MM },
- { "C9", 40, 57, SP_UNIT_MM },
- { "C10", 28, 40, SP_UNIT_MM },
- { "D1", 545, 771, SP_UNIT_MM },
- { "D2", 385, 545, SP_UNIT_MM },
- { "D3", 272, 385, SP_UNIT_MM },
- { "D4", 192, 272, SP_UNIT_MM },
- { "D5", 136, 192, SP_UNIT_MM },
- { "D6", 96, 136, SP_UNIT_MM },
- { "D7", 68, 96, SP_UNIT_MM },
- { "E3", 400, 560, SP_UNIT_MM },
- { "E4", 280, 400, SP_UNIT_MM },
- { "E5", 200, 280, SP_UNIT_MM },
- { "E6", 140, 200, SP_UNIT_MM },
+ { "C0", 917, 1297, unit_table.getUnit("mm") },
+ { "C1", 648, 917, unit_table.getUnit("mm") },
+ { "C2", 458, 648, unit_table.getUnit("mm") },
+ { "C3", 324, 458, unit_table.getUnit("mm") },
+ { "C4", 229, 324, unit_table.getUnit("mm") },
+ { "C5", 162, 229, unit_table.getUnit("mm") },
+ { "C6", 114, 162, unit_table.getUnit("mm") },
+ { "C7", 81, 114, unit_table.getUnit("mm") },
+ { "C8", 57, 81, unit_table.getUnit("mm") },
+ { "C9", 40, 57, unit_table.getUnit("mm") },
+ { "C10", 28, 40, unit_table.getUnit("mm") },
+ { "D1", 545, 771, unit_table.getUnit("mm") },
+ { "D2", 385, 545, unit_table.getUnit("mm") },
+ { "D3", 272, 385, unit_table.getUnit("mm") },
+ { "D4", 192, 272, unit_table.getUnit("mm") },
+ { "D5", 136, 192, unit_table.getUnit("mm") },
+ { "D6", 96, 136, unit_table.getUnit("mm") },
+ { "D7", 68, 96, unit_table.getUnit("mm") },
+ { "E3", 400, 560, unit_table.getUnit("mm") },
+ { "E4", 280, 400, unit_table.getUnit("mm") },
+ { "E5", 200, 280, unit_table.getUnit("mm") },
+ { "E6", 140, 200, unit_table.getUnit("mm") },
//#endif
- { "CSE", 462, 649, SP_UNIT_PT },
- { "US #10 Envelope", 4.125, 9.5, SP_UNIT_IN },
+ { "CSE", 462, 649, unit_table.getUnit("pt") },
+ { "US #10 Envelope", 4.125, 9.5, unit_table.getUnit("in") },
/* See http://www.hbp.com/content/PCR_envelopes.cfm for a much larger list of US envelope
sizes. */
- { "DL Envelope", 110, 220, SP_UNIT_MM },
- { "Ledger/Tabloid", 11, 17, SP_UNIT_IN },
+ { "DL Envelope", 110, 220, unit_table.getUnit("mm") },
+ { "Ledger/Tabloid", 11, 17, unit_table.getUnit("in") },
/* Note that `Folio' (used in QPrinter/KPrinter) is deliberately absent from this list, as it
means different sizes to different people: different people may expect the width to be
either 8, 8.25 or 8.5 inches, and the height to be either 13 or 13.5 inches, even
restricting our interpretation to foolscap folio. If you wish to introduce a folio-like
page size to the list, then please consider using a name more specific than just `Folio' or
`Foolscap Folio'. */
- { "Banner 468x60", 60, 468, SP_UNIT_PX },
- { "Icon 16x16", 16, 16, SP_UNIT_PX },
- { "Icon 32x32", 32, 32, SP_UNIT_PX },
- { "Icon 48x48", 48, 48, SP_UNIT_PX },
+ { "Banner 468x60", 60, 468, unit_table.getUnit("px") },
+ { "Icon 16x16", 16, 16, unit_table.getUnit("px") },
+ { "Icon 32x32", 32, 32, unit_table.getUnit("px") },
+ { "Icon 48x48", 48, 48, unit_table.getUnit("px") },
/* business cards */
- { "Business Card (ISO 7810)", 53.98, 85.60, SP_UNIT_MM },
- { "Business Card (US)", 2, 3.5, SP_UNIT_IN },
- { "Business Card (Europe)", 55, 85, SP_UNIT_MM },
- { "Business Card (Aus/NZ)", 55, 90, SP_UNIT_MM },
+ { "Business Card (ISO 7810)", 53.98, 85.60, unit_table.getUnit("mm") },
+ { "Business Card (US)", 2, 3.5, unit_table.getUnit("in") },
+ { "Business Card (Europe)", 55, 85, unit_table.getUnit("mm") },
+ { "Business Card (Aus/NZ)", 55, 90, unit_table.getUnit("mm") },
// Start Arch Series List
- { "Arch A", 9, 12, SP_UNIT_IN }, // 229 x 305 mm
- { "Arch B", 12, 18, SP_UNIT_IN }, // 305 x 457 mm
- { "Arch C", 18, 24, SP_UNIT_IN }, // 457 x 610 mm
- { "Arch D", 24, 36, SP_UNIT_IN }, // 610 x 914 mm
- { "Arch E", 36, 48, SP_UNIT_IN }, // 914 x 1219 mm
- { "Arch E1", 30, 42, SP_UNIT_IN }, // 762 x 1067 mm
+ { "Arch A", 9, 12, unit_table.getUnit("in") }, // 229 x 305 mm
+ { "Arch B", 12, 18, unit_table.getUnit("in") }, // 305 x 457 mm
+ { "Arch C", 18, 24, unit_table.getUnit("in") }, // 457 x 610 mm
+ { "Arch D", 24, 36, unit_table.getUnit("in") }, // 610 x 914 mm
+ { "Arch E", 36, 48, unit_table.getUnit("in") }, // 914 x 1219 mm
+ { "Arch E1", 30, 42, unit_table.getUnit("in") }, // 762 x 1067 mm
/*
* The above list of Arch sizes were taken from the following site:
@@ -216,7 +218,7 @@ static PaperSizeRec const inkscape_papers[] = {
* September 2009 - DAK
*/
- { NULL, 0, 0, SP_UNIT_PX },
+ { NULL, 0, 0, unit_table.getUnit("px") },
};
@@ -226,7 +228,7 @@ static PaperSizeRec const inkscape_papers[] = {
//########################################################################
//The default unit for this widget and its calculations
-static const SPUnit _px_unit = sp_unit_get_by_id (SP_UNIT_PX);
+static Inkscape::Util::Unit _px_unit = unit_table.getUnit("px");
/**
@@ -279,12 +281,7 @@ PageSizer::PageSizer(Registry & _wr)
char formatBuf[80];
snprintf(formatBuf, 79, "%0.1f x %0.1f", p->smaller, p->larger);
Glib::ustring desc = formatBuf;
- if (p->unit == SP_UNIT_IN)
- desc.append(" in");
- else if (p->unit == SP_UNIT_MM)
- desc.append(" mm");
- else if (p->unit == SP_UNIT_PX)
- desc.append(" px");
+ desc.append(" " + p->unit.abbr);
PaperSize paper(name, p->smaller, p->larger, p->unit);
_paperSizeTable[name] = paper;
Gtk::TreeModel::Row row = *(_paperSizeListStore->append());
@@ -568,9 +565,9 @@ PageSizer::find_paper_size (double w, double h) const
for (iter = _paperSizeTable.begin() ;
iter != _paperSizeTable.end() ; ++iter) {
PaperSize paper = iter->second;
- SPUnit const &i_unit = sp_unit_get_by_id(paper.unit);
- double smallX = sp_units_get_pixels(paper.smaller, i_unit);
- double largeX = sp_units_get_pixels(paper.larger, i_unit);
+ Inkscape::Util::Unit const &i_unit = paper.unit;
+ double smallX = Inkscape::Util::Quantity::convert(paper.smaller, &i_unit, &_px_unit);
+ double largeX = Inkscape::Util::Quantity::convert(paper.larger, &i_unit, &_px_unit);
g_return_val_if_fail(smallX <= largeX, _paperSizeListStore->children().end());
@@ -661,9 +658,9 @@ PageSizer::on_paper_size_list_changed()
_landscape = _landscapeButton.get_active();
}
- SPUnit const &src_unit = sp_unit_get_by_id (paper.unit);
- sp_convert_distance (&w, &src_unit, &_px_unit);
- sp_convert_distance (&h, &src_unit, &_px_unit);
+ Inkscape::Util::Unit const &src_unit = paper.unit;
+ w = Inkscape::Util::Quantity::convert(w, &src_unit, &_px_unit);
+ h = Inkscape::Util::Quantity::convert(h, &src_unit, &_px_unit);
if (_landscape)
setDim (h, w, false);
diff --git a/src/ui/widget/page-sizer.h b/src/ui/widget/page-sizer.h
index d1fbb56e0..fc8edeeac 100644
--- a/src/ui/widget/page-sizer.h
+++ b/src/ui/widget/page-sizer.h
@@ -18,7 +18,7 @@
#include "ui/widget/registered-widget.h"
#include <sigc++/sigc++.h>
-#include "helper/units.h"
+#include "util/units.h"
#include <gtkmm/alignment.h>
#include <gtkmm/expander.h>
@@ -64,7 +64,7 @@ public:
PaperSize(const Glib::ustring &nameArg,
double smallerArg,
double largerArg,
- SPUnitId unitArg)
+ Inkscape::Util::Unit unitArg)
{
name = nameArg;
smaller = smallerArg;
@@ -108,7 +108,7 @@ public:
/**
* The units (px, pt, mm, etc) of this specification
*/
- SPUnitId unit;
+ Inkscape::Util::Unit unit;
private:
@@ -117,7 +117,8 @@ private:
name = "";
smaller = 0.0;
larger = 0.0;
- unit = SP_UNIT_PX;
+ static Inkscape::Util::UnitTable unit_table;
+ unit = unit_table.getUnit("px");
}
void assign(const PaperSize &other)