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)
47 #define INCHES_PER_METER (39.3700787402)
48 #define POINTS_PER_METER (POINTS_PER_INCH*INCHES_PER_METER)
49 #define CENTIMETERS_PER_METER (100.0)
50 #define MILLIMETERS_PER_METER (1000.0)
52 #define METERS_PER_UNIT (rend_desc.get_physical_w()/abs(rend_desc.get_tl()[0]-rend_desc.get_br()[0]))
54 /* === G L O B A L S ======================================================= */
56 /* === P R O C E D U R E S ================================================= */
58 /* === M E T H O D S ======================================================= */
60 Distance::Distance(const synfig::String& str)
65 int ret(strscanf(str,"%f%n",&val,&i));
66 synfig::info("Distance::Distance(): ret=%d, val=%f",ret,val);
71 synfig::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
77 synfig::info("Distance::Distance(): system=\"%s\"",String(str.begin()+i,str.end()).c_str());
78 system_=ident_system(String(str.begin()+i,str.end()));
83 Distance::operator=(const synfig::String& str)
87 int ret(strscanf(str,"%f%n",&val,&i));
88 // synfig::info("Distance::Distance(): ret=%d, val=%f",ret,val);
93 synfig::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
99 synfig::String sys(str.begin()+i,str.end());
102 system_=ident_system(sys);
107 Distance::get_string(int digits)const
109 digits=min(9,max(0,digits));
110 String fmt(strprintf("%%.%01df%%s",digits));
111 return strprintf(fmt.c_str(),value_,system_name(system_).c_str());
115 Distance::convert(Distance::System target, const RendDesc& rend_desc)
117 value_=get(target,rend_desc);
122 Distance::get(Distance::System target, const RendDesc& rend_desc)const
124 if(target==SYSTEM_UNITS)
125 return units(rend_desc);
126 if(target==SYSTEM_PIXELS)
127 return units(rend_desc)*METERS_PER_UNIT*rend_desc.get_x_res();
129 return meters_to_system(meters(rend_desc),target);
133 Distance::meters()const
137 case SYSTEM_INCHES: return value_/INCHES_PER_METER;
138 case SYSTEM_POINTS: return value_/POINTS_PER_METER;
139 case SYSTEM_METERS: return value_;
140 case SYSTEM_CENTIMETERS: return value_/CENTIMETERS_PER_METER;
141 case SYSTEM_MILLIMETERS: return value_/MILLIMETERS_PER_METER;
142 default: throw BadSystem();
147 Distance::meters(const RendDesc& rend_desc)const
149 if(system_>SYSTEM_PIXELS)
151 if(system_==SYSTEM_UNITS)
152 return value_*METERS_PER_UNIT;
153 if(system_==SYSTEM_PIXELS)
154 return value_/rend_desc.get_x_res();
160 Distance::units(const RendDesc& rend_desc)const
162 if(system_==SYSTEM_UNITS)
167 if(system_>SYSTEM_PIXELS)
170 ret=value_/rend_desc.get_x_res();
172 return ret/METERS_PER_UNIT;
177 Distance::meters_to_system(Real x,System target_system)
179 switch(target_system)
181 case SYSTEM_INCHES: return x*INCHES_PER_METER;
182 case SYSTEM_POINTS: return x*POINTS_PER_METER;
183 case SYSTEM_METERS: return x;
184 case SYSTEM_CENTIMETERS: return x*CENTIMETERS_PER_METER;
185 case SYSTEM_MILLIMETERS: return x*MILLIMETERS_PER_METER;
186 default: throw BadSystem();
190 Distance::System // (static)
191 Distance::ident_system(const synfig::String& x)
195 // Make it all upper case, and remove white space
196 for(unsigned int i=0;i<x.size();i++)if(x[i]!=' ' && x[i]!='\t')str+=toupper(x[i]);
198 // If it is plural, make it singular
199 if(str[str.size()-1]=='S')
200 str=synfig::String(str.begin(),str.end()-1);
202 if(str.empty() || str=="U" || str=="UNIT")
204 if(str=="PX" || str=="PIXEL")
205 return SYSTEM_PIXELS;
206 if(str=="PT" || str=="POINT")
207 return SYSTEM_POINTS;
208 if(str=="IN" || str=="\"" || str=="INCHE" || str=="INCH")
209 return SYSTEM_INCHES;
210 if(str=="M" || str=="METER")
211 return SYSTEM_METERS;
212 if(str=="CM" || str=="CENTIMETER")
213 return SYSTEM_CENTIMETERS;
214 if(str=="MM" || str=="MILLIMETER")
215 return SYSTEM_MILLIMETERS;
217 synfig::warning("Distance::ident_system(): Unknown distance system \"%s\"",x.c_str());
222 synfig::String // (static)
223 Distance::system_name(Distance::System system)
227 case SYSTEM_UNITS: return "u";
228 case SYSTEM_PIXELS: return "px";
229 case SYSTEM_POINTS: return "pt";
230 case SYSTEM_INCHES: return "in";
231 case SYSTEM_METERS: return "m";
232 case SYSTEM_MILLIMETERS: return "mm";
233 case SYSTEM_CENTIMETERS: return "cm";
235 default: throw BadSystem();
237 return synfig::String();
240 synfig::String // (static)
241 Distance::system_local_name(Distance::System system)
245 case SYSTEM_UNITS: return _("Units");
246 case SYSTEM_PIXELS: return _("Pixels");
247 case SYSTEM_POINTS: return _("Points");
248 case SYSTEM_INCHES: return _("Inches");
249 case SYSTEM_METERS: return _("Meters");
250 case SYSTEM_MILLIMETERS:return _("Millimeters");
251 case SYSTEM_CENTIMETERS:return _("Centimeters");
253 default: throw BadSystem();
255 return synfig::String();