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_FT:
29 src = FT_PER_IN;
30 break;
31 case SP_PT:
32 src = PT_PER_IN;
33 break;
34 case SP_PC:
35 src = PC_PER_IN;
36 break;
37 case SP_PX:
38 src = PX_PER_IN;
39 break;
40 case NONE:
41 src = 1;
42 break;
43 }
45 switch (metric_dst) {
46 case SP_M:
47 dst = M_PER_IN;
48 break;
49 case SP_MM:
50 dst = MM_PER_IN;
51 break;
52 case SP_CM:
53 dst = CM_PER_IN;
54 break;
55 case SP_IN:
56 dst = IN_PER_IN;
57 break;
58 case SP_FT:
59 dst = FT_PER_IN;
60 break;
61 case SP_PT:
62 dst = PT_PER_IN;
63 break;
64 case SP_PC:
65 dst = PC_PER_IN;
66 break;
67 case SP_PX:
68 dst = PX_PER_IN;
69 break;
70 case NONE:
71 dst = 1;
72 break;
73 }
75 return length_src * (dst/src);
76 }
78 /**
79 * Create a human-readable string suitable for status-bar display.
80 */
81 GString *
82 sp_metric_to_metric_string(gdouble const length,
83 SPMetric const metric_src, SPMetric const metric_dst,
84 gboolean const m)
85 {
86 gdouble const len = sp_absolute_metric_to_metric(length, metric_src, metric_dst);
87 GString *str = g_string_new("");
88 g_string_printf(str, "%0.02f", len);
89 /* We need a fixed number of fractional digits, because otherwise the live statusbar display of
90 * lengths will be too jerky */
92 if (m) {
93 char const *unit_str;
94 switch (metric_dst) {
95 case SP_M: unit_str = " m"; break;
96 case SP_MM: unit_str = " mm"; break;
97 case SP_CM: unit_str = " cm"; break;
98 case SP_IN: unit_str = "\""; break;
99 case SP_PT: unit_str = " pt"; break;
100 case SP_PX: unit_str = " px"; break;
101 default: unit_str = NULL; break;
102 }
103 if (unit_str) {
104 g_string_append(str, unit_str);
105 }
106 }
107 return str;
108 }
111 /*
112 Local Variables:
113 mode:c++
114 c-file-style:"stroustrup"
115 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
116 indent-tabs-mode:nil
117 fill-column:99
118 End:
119 */
120 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :