1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
38 /* === U S I N G =========================================================== */
42 using namespace synfig;
44 /* === M A C R O S ========================================================= */
46 #define POINTS_PER_INCH (72.0)
48 /* === G L O B A L S ======================================================= */
50 #define METERS_PER_UNIT (rend_desc.get_physical_w()/abs(rend_desc.get_tl()[0]-rend_desc.get_br()[0]))
52 /* === P R O C E D U R E S ================================================= */
54 /* === M E T H O D S ======================================================= */
56 Distance::Distance(const synfig::String& str)
61 int ret(strscanf(str,"%f%n",&val,&i));
62 synfig::info("Distance::Distance(): ret=%d, val=%f",ret,val);
67 synfig::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
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()));
79 Distance::operator=(const synfig::String& str)
83 int ret(strscanf(str,"%f%n",&val,&i));
84 synfig::info("Distance::Distance(): ret=%d, val=%f",ret,val);
89 synfig::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
95 synfig::String sys(str.begin()+i,str.end());
98 system_=ident_system(sys);
103 Distance::get_string(int digits)const
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());
111 Distance::convert(Distance::System target, const RendDesc& rend_desc)
113 value_=get(target,rend_desc);
118 Distance::get(Distance::System target, const RendDesc& rend_desc)const
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();
125 return meters_to_system(meters(rend_desc),target);
129 Distance::meters()const
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();
143 Distance::meters(const RendDesc& rend_desc)const
145 if(system_>SYSTEM_PIXELS)
147 if(system_==SYSTEM_UNITS)
148 return value_*METERS_PER_UNIT;
149 if(system_==SYSTEM_PIXELS)
150 return value_/rend_desc.get_x_res();
156 Distance::units(const RendDesc& rend_desc)const
158 if(system_==SYSTEM_UNITS)
163 if(system_>SYSTEM_PIXELS)
166 ret=value_/rend_desc.get_x_res();
168 return ret/METERS_PER_UNIT;
173 Distance::meters_to_system(Real x,System target_system)
175 switch(target_system)
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();
186 Distance::System // (static)
187 Distance::ident_system(const synfig::String& x)
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]);
194 // If it is plural, make it singular
195 if(str[str.size()-1]=='S')
196 str=synfig::String(str.begin(),str.end()-1);
198 if(str.empty() || str=="U" || str=="UNIT")
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;
213 synfig::warning("Distance::ident_system(): Unknown distance system \"%s\"",x.c_str());
218 synfig::String // (static)
219 Distance::system_name(Distance::System system)
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";
231 default: throw BadSystem();
233 return synfig::String();
236 synfig::String // (static)
237 Distance::system_local_name(Distance::System system)
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");
249 default: throw BadSystem();
251 return synfig::String();