~vaifrax/inkscape/bugfix170049

« back to all changes in this revision

Viewing changes to src/helper/units.h

  • Committer: mental
  • Date: 2006-01-16 02:36:01 UTC
  • Revision ID: mental@users.sourceforge.net-20060116023601-wkr0h7edl5veyudq
moving trunk for module inkscape

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __SP_UNIT_H__
 
2
#define __SP_UNIT_H__
 
3
 
 
4
/*
 
5
 * SPUnit
 
6
 *
 
7
 * Ported from libgnomeprint
 
8
 *
 
9
 * Authors:
 
10
 *   Dirk Luetjens <dirk@luedi.oche.de>
 
11
 *   Yves Arrouye <Yves.Arrouye@marin.fdn.fr>
 
12
 *   Lauris Kaplinski <lauris@ximian.com>
 
13
 *
 
14
 * Copyright 1999-2001 Ximian, Inc. and authors
 
15
 *
 
16
 */
 
17
 
 
18
#include <glib/gmessages.h>
 
19
#include <glib/gslist.h>
 
20
#include <glib/gtypes.h>
 
21
#include "sp-metric.h"
 
22
 
 
23
 
 
24
/*
 
25
 * Units and conversion methods used by libgnomeprint.
 
26
 *
 
27
 * You need those for certain config keys (like paper size), if you are
 
28
 * interested in using these (look at gnome-print-config.h for discussion,
 
29
 * why you may NOT be interested in paper size).
 
30
 *
 
31
 * Unit bases define set of mutually unrelated measuring systems (numbers,
 
32
 * paper, screen and dimesionless user coordinates). Still, you can convert
 
33
 * between those, specifying scaling factors explicitly.
 
34
 *
 
35
 * Paper (i.e. output) coordinates are taken as absolute real world units.
 
36
 * It has some justification, because screen unit (pixel) size changes,
 
37
 * if you change screen resolution, while you cannot change output on paper
 
38
 * as easily (unless you have thermally contracting paper, of course).
 
39
 *
 
40
 */
 
41
 
 
42
struct SPUnit;
 
43
struct SPDistance;
 
44
 
 
45
/*
 
46
 * The base linear ("absolute") unit is 1/72th of an inch, i.e. the base unit of postscript.
 
47
 */
 
48
 
 
49
/*
 
50
 * Unit bases
 
51
 */
 
52
enum SPUnitBase {
 
53
        SP_UNIT_DIMENSIONLESS = (1 << 0), /* For percentages and like */
 
54
        SP_UNIT_ABSOLUTE = (1 << 1), /* Real world distances - i.e. mm, cm... */
 
55
        SP_UNIT_DEVICE = (1 << 2), /* Pixels in the SVG/CSS sense. */
 
56
        SP_UNIT_VOLATILE = (1 << 3) /* em and ex */
 
57
};
 
58
 
 
59
/*
 
60
 * Units: indexes into sp_units.
 
61
 */
 
62
enum SPUnitId {
 
63
        SP_UNIT_SCALE,  // 1.0 == 100%
 
64
        SP_UNIT_PT,     // Postscript points: exactly 72 per inch
 
65
        SP_UNIT_PX,     // "Pixels" in the CSS sense; though Inkscape assumes a constant 90 per inch.
 
66
        SP_UNIT_PERCENT,  /* Note: In Inkscape this often means "relative to current value" (for
 
67
                             users to edit a value), rather than the SVG/CSS use of percentages. */
 
68
        SP_UNIT_MM,     // millimetres
 
69
        SP_UNIT_CM,     // centimetres
 
70
        SP_UNIT_M,      // metres
 
71
        SP_UNIT_IN,     // inches
 
72
        SP_UNIT_EM,     // font-size of relevant font
 
73
        SP_UNIT_EX,     // x-height of relevant font
 
74
        sp_max_unit_id = SP_UNIT_EX     // For bounds-checking in sp_unit_get_by_id.
 
75
};
 
76
 
 
77
/*
 
78
 * Notice, that for correct menus etc. you have to use
 
79
 * ngettext method family yourself. For that reason we
 
80
 * do not provide translations in unit names.
 
81
 * I also do not know, whether to allow user-created units,
 
82
 * because this would certainly confuse textdomain.
 
83
 */
 
84
 
 
85
struct SPUnit {
 
86
        SPUnitId unit_id; /* used as sanity check */
 
87
        SPUnitBase base;
 
88
        gdouble unittobase; /* how many base units in this unit */
 
89
        SPMetric metric; // the corresponding SPMetric from sp-metrics.h
 
90
        guint svg_unit; // the corresponding SVGLengthUnit
 
91
 
 
92
        /* When using, you must call "gettext" on them so they're translated */
 
93
        gchar const *name;
 
94
        gchar const *abbr;
 
95
        gchar const *plural;
 
96
        gchar const *abbr_plural;
 
97
};
 
98
 
 
99
const SPUnit *sp_unit_get_by_abbreviation (const gchar *abbreviation);
 
100
/* When using, you must call "gettext" on them so they're translated */
 
101
const gchar *sp_unit_get_abbreviation (const SPUnit *unit);
 
102
gchar const *sp_unit_get_plural (SPUnit const *unit);
 
103
 
 
104
SPMetric sp_unit_get_metric(SPUnit const *unit);
 
105
guint sp_unit_get_svg_unit(SPUnit const *unit);
 
106
 
 
107
extern SPUnit const sp_units[];
 
108
 
 
109
inline SPUnit const &
 
110
sp_unit_get_by_id(SPUnitId const id)
 
111
{
 
112
        /* inline because the compiler should optimize away the g_return_val_if_fail test in the
 
113
           usual case that the argument value is known at compile-time, leaving just
 
114
           "return sp_units[constant]". */
 
115
        unsigned const ix = unsigned(id);
 
116
        g_return_val_if_fail(ix <= sp_max_unit_id, sp_units[SP_UNIT_PX]);
 
117
        return sp_units[ix];
 
118
}
 
119
 
 
120
#define SP_PS_UNIT (&sp_unit_get_by_id(SP_UNIT_PT))
 
121
 
 
122
 
 
123
/** Used solely by units-test.cpp. */
 
124
bool sp_units_table_sane();
 
125
 
 
126
#define SP_UNITS_ALL (SP_UNIT_DIMENSIONLESS | SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE | SP_UNIT_VOLATILE)
 
127
 
 
128
GSList *sp_unit_get_list (guint bases);
 
129
void sp_unit_free_list (GSList *units);
 
130
 
 
131
/* These are pure utility */
 
132
/* Return TRUE if conversion is possible, FALSE if unit bases differ */
 
133
gboolean sp_convert_distance (gdouble *distance, const SPUnit *from, const SPUnit *to);
 
134
 
 
135
/* If either one is NULL, transconverting to/from that base fails */
 
136
/* Generic conversion between volatile units would be useless anyways */
 
137
gdouble sp_convert_distance_full(gdouble const from_dist, SPUnit const &from, SPUnit const &to);
 
138
 
 
139
/* Some more convenience */
 
140
gdouble sp_units_get_pixels(gdouble const units, SPUnit const &unit);
 
141
gdouble sp_pixels_get_units(gdouble const pixels, SPUnit const &unit);
 
142
 
 
143
double angle_to_compass(double angle);
 
144
double angle_from_compass(double angle);
 
145
 
 
146
#endif