1 #include "sp-metrics.h"
2 #include "unit-constants.h"
4 /*
5 * SPMetric handling and stuff
6 * I hope this will be usefull :-)
7 */
9 gdouble
10 sp_absolute_metric_to_metric (gdouble length_src, const SPMetric metric_src, const SPMetric metric_dst)
11 {
12 gdouble src = 1;
13 gdouble dst = 1;
15 switch (metric_src) {
16 case SP_M:
17 src = M_PER_IN;
18 break;
19 case SP_MM:
20 src = MM_PER_IN;
21 break;
22 case SP_CM:
23 src = CM_PER_IN;
24 break;
25 case SP_IN:
26 src = IN_PER_IN;
27 break;
28 case SP_PT:
29 src = PT_PER_IN;
30 break;
31 case SP_PX:
32 src = PX_PER_IN;
33 break;
34 case NONE:
35 src = 1;
36 break;
37 }
39 switch (metric_dst) {
40 case SP_M:
41 dst = M_PER_IN;
42 break;
43 case SP_MM:
44 dst = MM_PER_IN;
45 break;
46 case SP_CM:
47 dst = CM_PER_IN;
48 break;
49 case SP_IN:
50 dst = IN_PER_IN;
51 break;
52 case SP_PT:
53 dst = PT_PER_IN;
54 break;
55 case SP_PX:
56 dst = PX_PER_IN;
57 break;
58 case NONE:
59 dst = 1;
60 break;
61 }
63 return length_src * (dst/src);
64 }
66 /**
67 * Create a human-readable string suitable for status-bar display.
68 */
69 GString *
70 sp_metric_to_metric_string(gdouble const length,
71 SPMetric const metric_src, SPMetric const metric_dst,
72 gboolean const m)
73 {
74 gdouble const len = sp_absolute_metric_to_metric(length, metric_src, metric_dst);
75 GString *str = g_string_new("");
76 g_string_printf(str, "%0.02f", len);
77 /* We need a fixed number of fractional digits, because otherwise the live statusbar display of
78 * lengths will be too jerky */
80 if (m) {
81 char const *unit_str;
82 switch (metric_dst) {
83 case SP_M: unit_str = " m"; break;
84 case SP_MM: unit_str = " mm"; break;
85 case SP_CM: unit_str = " cm"; break;
86 case SP_IN: unit_str = "\""; break;
87 case SP_PT: unit_str = " pt"; break;
88 case SP_PX: unit_str = " px"; break;
89 default: unit_str = NULL; break;
90 }
91 if (unit_str) {
92 g_string_append(str, unit_str);
93 }
94 }
95 return str;
96 }
99 /*
100 Local Variables:
101 mode:c++
102 c-file-style:"stroustrup"
103 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
104 indent-tabs-mode:nil
105 fill-column:99
106 End:
107 */
108 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :