Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc1 / src / synfig / distance.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file distance.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "distance.h"
33 #include "renddesc.h"
34 #include "general.h"
35 #include <ctype.h>
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43
44 /* === M A C R O S ========================================================= */
45
46 #define POINTS_PER_INCH (72.0)
47
48 /* === G L O B A L S ======================================================= */
49
50 #define METERS_PER_UNIT  (rend_desc.get_physical_w()/abs(rend_desc.get_tl()[0]-rend_desc.get_br()[0]))
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 Distance::Distance(const synfig::String& str)
57 {
58         (*this)=str;
59 /*      int i(0);
60         float val;
61         int ret(strscanf(str,"%f%n",&val,&i));
62         synfig::info("Distance::Distance(): ret=%d, val=%f",ret,val);
63
64         if(ret<=0)
65         {
66                 // Error
67                 synfig::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
68                 value_=0;
69         }
70         else
71                 value_=val;
72
73         synfig::info("Distance::Distance(): system=\"%s\"",String(str.begin()+i,str.end()).c_str());
74         system_=ident_system(String(str.begin()+i,str.end()));
75 */
76 }
77
78 Distance&
79 Distance::operator=(const synfig::String& str)
80 {
81         int i(0);
82         float val;
83         int ret(strscanf(str,"%f%n",&val,&i));
84         // synfig::info("Distance::Distance(): ret=%d, val=%f",ret,val);
85
86         if(ret<=0)
87         {
88                 // Error
89                 synfig::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
90                 return *this;
91         }
92         else
93                 value_=val;
94
95         synfig::String sys(str.begin()+i,str.end());
96
97         if(sys.size())
98                 system_=ident_system(sys);
99         return *this;
100 }
101
102 synfig::String
103 Distance::get_string(int digits)const
104 {
105         digits=min(9,max(0,digits));
106         String fmt(strprintf("%%.%01df%%s",digits));
107         return strprintf(fmt.c_str(),value_,system_name(system_).c_str());
108 }
109
110 void
111 Distance::convert(Distance::System target, const RendDesc& rend_desc)
112 {
113         value_=get(target,rend_desc);
114         system_=target;
115 }
116
117 Real
118 Distance::get(Distance::System target, const RendDesc& rend_desc)const
119 {
120         if(target==SYSTEM_UNITS)
121                 return units(rend_desc);
122         if(target==SYSTEM_PIXELS)
123                 return units(rend_desc)*METERS_PER_UNIT*rend_desc.get_x_res();
124
125         return meters_to_system(meters(rend_desc),target);
126 }
127
128 Real
129 Distance::meters()const
130 {
131         switch(system_)
132         {
133                 case SYSTEM_INCHES: return value_/39.3700787402;
134                 case SYSTEM_POINTS: return value_/POINTS_PER_INCH/39.3700787402;
135                 case SYSTEM_METERS: return value_;
136                 case SYSTEM_CENTIMETERS: return value_/100.0;
137                 case SYSTEM_MILLIMETERS: return value_/1000.0;
138                 default: throw BadSystem();
139         }
140 }
141
142 Real
143 Distance::meters(const RendDesc& rend_desc)const
144 {
145         if(system_>SYSTEM_PIXELS)
146                 return meters();
147         if(system_==SYSTEM_UNITS)
148                 return value_*METERS_PER_UNIT;
149         if(system_==SYSTEM_PIXELS)
150                 return value_/rend_desc.get_x_res();
151
152         throw BadSystem();
153 }
154
155 Real
156 Distance::units(const RendDesc& rend_desc)const
157 {
158         if(system_==SYSTEM_UNITS)
159                 return value_;
160
161         Real ret;
162
163         if(system_>SYSTEM_PIXELS)
164                 ret=meters();
165         else
166                 ret=value_/rend_desc.get_x_res();
167
168         return ret/METERS_PER_UNIT;
169 }
170
171
172 Real // (static)
173 Distance::meters_to_system(Real x,System target_system)
174 {
175         switch(target_system)
176         {
177                 case SYSTEM_INCHES: return x*39.3700787402;
178                 case SYSTEM_POINTS: return x*39.3700787402*POINTS_PER_INCH;
179                 case SYSTEM_METERS: return x;
180                 case SYSTEM_CENTIMETERS: return x*100.0;
181                 case SYSTEM_MILLIMETERS: return x*1000.0;
182                 default: throw BadSystem();
183         }
184 }
185
186 Distance::System // (static)
187 Distance::ident_system(const synfig::String& x)
188 {
189         synfig::String str;
190
191         // Make it all upper case, and remove white space
192         for(unsigned int i=0;i<x.size();i++)if(x[i]!=' ' && x[i]!='\t')str+=toupper(x[i]);
193
194         // If it is plural, make it singular
195         if(str[str.size()-1]=='S')
196                 str=synfig::String(str.begin(),str.end()-1);
197
198         if(str.empty() || str=="U" || str=="UNIT")
199                 return SYSTEM_UNITS;
200         if(str=="PX" || str=="PIXEL")
201                 return SYSTEM_PIXELS;
202         if(str=="PT" || str=="POINT")
203                 return SYSTEM_POINTS;
204         if(str=="IN" || str=="\"" || str=="INCHE" || str=="INCH")
205                 return SYSTEM_INCHES;
206         if(str=="M" || str=="METER")
207                 return SYSTEM_METERS;
208         if(str=="CM" || str=="CENTIMETER")
209                 return SYSTEM_CENTIMETERS;
210         if(str=="MM" || str=="MILLIMETER")
211                 return SYSTEM_MILLIMETERS;
212
213         synfig::warning("Distance::ident_system(): Unknown distance system \"%s\"",x.c_str());
214
215         return SYSTEM_UNITS;
216 }
217
218 synfig::String  // (static)
219 Distance::system_name(Distance::System system)
220 {
221         switch(system)
222         {
223                 case SYSTEM_UNITS:              return "u";
224                 case SYSTEM_PIXELS:             return "px";
225                 case SYSTEM_POINTS:             return "pt";
226                 case SYSTEM_INCHES:             return "in";
227                 case SYSTEM_METERS:             return "m";
228                 case SYSTEM_MILLIMETERS:        return "mm";
229                 case SYSTEM_CENTIMETERS:        return "cm";
230
231                 default:                                throw BadSystem();
232         }
233         return synfig::String();
234 }
235
236 synfig::String  // (static)
237 Distance::system_local_name(Distance::System system)
238 {
239         switch(system)
240         {
241                 case SYSTEM_UNITS:              return _("Units");
242                 case SYSTEM_PIXELS:             return _("Pixels");
243                 case SYSTEM_POINTS:             return _("Points");
244                 case SYSTEM_INCHES:             return _("Inches");
245                 case SYSTEM_METERS:             return _("Meters");
246                 case SYSTEM_MILLIMETERS:return _("Millimeters");
247                 case SYSTEM_CENTIMETERS:return _("Centimeters");
248
249                 default:                                throw BadSystem();
250         }
251         return synfig::String();
252 }