4746fcccaaa2dbfe6182140a889c3ee3c256b075
1 #ifndef __SP_UNIT_H__
2 #define __SP_UNIT_H__
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 */
18 #include <glib/gmessages.h>
19 #include <glib/gslist.h>
20 #include <glib/gtypes.h>
21 #include "sp-metric.h"
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 */
42 struct SPUnit;
43 struct SPDistance;
45 /*
46 * The base linear ("absolute") unit is 1/72th of an inch, i.e. the base unit of postscript.
47 */
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 };
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 };
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 */
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
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 };
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);
104 SPMetric sp_unit_get_metric(SPUnit const *unit);
105 guint sp_unit_get_svg_unit(SPUnit const *unit);
107 extern SPUnit const sp_units[];
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 }
120 #define SP_PS_UNIT (&sp_unit_get_by_id(SP_UNIT_PT))
123 /** Used solely by units-test.cpp. */
124 bool sp_units_table_sane();
126 #define SP_UNITS_ALL (SP_UNIT_DIMENSIONLESS | SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE | SP_UNIT_VOLATILE)
128 GSList *sp_unit_get_list (guint bases);
129 void sp_unit_free_list (GSList *units);
131 /* These are pure utility */
132 /* Return TRUE if conversion is possible, FALSE if unit bases differ */
133 bool sp_convert_distance (gdouble *distance, const SPUnit *from, const SPUnit *to);
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);
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);
143 double angle_to_compass(double angle);
144 double angle_from_compass(double angle);
146 #endif