1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
39 /* === U S I N G =========================================================== */
43 using namespace synfig;
45 /* === M A C R O S ========================================================= */
47 #define POINTS_PER_INCH (72.0)
48 #define INCHES_PER_METER (39.3700787402)
49 #define POINTS_PER_METER (POINTS_PER_INCH*INCHES_PER_METER)
50 #define CENTIMETERS_PER_METER (100.0)
51 #define MILLIMETERS_PER_METER (1000.0)
53 #define METERS_PER_UNIT (rend_desc.get_physical_w()/abs(rend_desc.get_tl()[0]-rend_desc.get_br()[0]))
55 /* === G L O B A L S ======================================================= */
57 /* === P R O C E D U R E S ================================================= */
59 /* === M E T H O D S ======================================================= */
61 Distance::Distance(const synfig::String& str)
66 int ret(strscanf(str,"%f%n",&val,&i));
67 synfig::info("Distance::Distance(): ret=%d, val=%f",ret,val);
72 synfig::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
78 synfig::info("Distance::Distance(): system=\"%s\"",String(str.begin()+i,str.end()).c_str());
79 system_=ident_system(String(str.begin()+i,str.end()));
84 Distance::operator=(const synfig::String& str)
88 int ret(strscanf(str,"%f%n",&val,&i));
89 // synfig::info("Distance::Distance(): ret=%d, val=%f",ret,val);
94 synfig::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
100 synfig::String sys(str.begin()+i,str.end());
103 system_=ident_system(sys);
108 Distance::get_string(int digits)const
110 digits=min(9,max(0,digits));
111 String fmt(strprintf("%%.%01df%%s",digits));
112 return strprintf(fmt.c_str(),value_,system_name(system_).c_str());
116 Distance::convert(Distance::System target, const RendDesc& rend_desc)
118 value_=get(target,rend_desc);
123 Distance::get(Distance::System target, const RendDesc& rend_desc)const
125 if(target==SYSTEM_UNITS)
126 return units(rend_desc);
127 if(target==SYSTEM_PIXELS)
128 return units(rend_desc)*METERS_PER_UNIT*rend_desc.get_x_res();
130 return meters_to_system(meters(rend_desc),target);
134 Distance::meters()const
138 case SYSTEM_INCHES: return value_/INCHES_PER_METER;
139 case SYSTEM_POINTS: return value_/POINTS_PER_METER;
140 case SYSTEM_METERS: return value_;
141 case SYSTEM_CENTIMETERS: return value_/CENTIMETERS_PER_METER;
142 case SYSTEM_MILLIMETERS: return value_/MILLIMETERS_PER_METER;
143 default: throw BadSystem();
148 Distance::meters(const RendDesc& rend_desc)const
150 if(system_>SYSTEM_PIXELS)
152 if(system_==SYSTEM_UNITS)
153 return value_*METERS_PER_UNIT;
154 if(system_==SYSTEM_PIXELS)
155 return value_/rend_desc.get_x_res();
161 Distance::units(const RendDesc& rend_desc)const
163 if(system_==SYSTEM_UNITS)
168 if(system_>SYSTEM_PIXELS)
171 ret=value_/rend_desc.get_x_res();
173 return ret/METERS_PER_UNIT;
178 Distance::meters_to_system(Real x,System target_system)
180 switch(target_system)
182 case SYSTEM_INCHES: return x*INCHES_PER_METER;
183 case SYSTEM_POINTS: return x*POINTS_PER_METER;
184 case SYSTEM_METERS: return x;
185 case SYSTEM_CENTIMETERS: return x*CENTIMETERS_PER_METER;
186 case SYSTEM_MILLIMETERS: return x*MILLIMETERS_PER_METER;
187 default: throw BadSystem();
191 Distance::System // (static)
192 Distance::ident_system(const synfig::String& x)
196 // Make it all upper case, and remove white space
197 for(unsigned int i=0;i<x.size();i++)if(x[i]!=' ' && x[i]!='\t')str+=toupper(x[i]);
199 // If it is plural, make it singular
200 if(str[str.size()-1]=='S')
201 str=synfig::String(str.begin(),str.end()-1);
203 if(str.empty() || str=="U" || str=="UNIT")
205 if(str=="PX" || str=="PIXEL")
206 return SYSTEM_PIXELS;
207 if(str=="PT" || str=="POINT")
208 return SYSTEM_POINTS;
209 if(str=="IN" || str=="\"" || str=="INCHE" || str=="INCH")
210 return SYSTEM_INCHES;
211 if(str=="M" || str=="METER")
212 return SYSTEM_METERS;
213 if(str=="CM" || str=="CENTIMETER")
214 return SYSTEM_CENTIMETERS;
215 if(str=="MM" || str=="MILLIMETER")
216 return SYSTEM_MILLIMETERS;
218 synfig::warning("Distance::ident_system(): Unknown distance system \"%s\"",x.c_str());
223 synfig::String // (static)
224 Distance::system_name(Distance::System system)
228 case SYSTEM_UNITS: return "u";
229 case SYSTEM_PIXELS: return "px";
230 case SYSTEM_POINTS: return "pt";
231 case SYSTEM_INCHES: return "in";
232 case SYSTEM_METERS: return "m";
233 case SYSTEM_MILLIMETERS: return "mm";
234 case SYSTEM_CENTIMETERS: return "cm";
236 default: throw BadSystem();
238 return synfig::String();
241 synfig::String // (static)
242 Distance::system_local_name(Distance::System system)
246 case SYSTEM_UNITS: return _("Units");
247 case SYSTEM_PIXELS: return _("Pixels");
248 case SYSTEM_POINTS: return _("Points");
249 case SYSTEM_INCHES: return _("Inches");
250 case SYSTEM_METERS: return _("Meters");
251 case SYSTEM_MILLIMETERS:return _("Millimeters");
252 case SYSTEM_CENTIMETERS:return _("Centimeters");
254 default: throw BadSystem();
256 return synfig::String();