1 /* === S I N F G =========================================================== */
3 ** \brief Template File
5 ** $Id: distance.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
37 /* === U S I N G =========================================================== */
41 using namespace sinfg;
43 /* === M A C R O S ========================================================= */
45 #define POINTS_PER_INCH (72.0)
47 /* === G L O B A L S ======================================================= */
49 #define METERS_PER_UNIT (rend_desc.get_physical_w()/abs(rend_desc.get_tl()[0]-rend_desc.get_br()[0]))
51 /* === P R O C E D U R E S ================================================= */
53 /* === M E T H O D S ======================================================= */
55 Distance::Distance(const sinfg::String& str)
60 int ret(strscanf(str,"%f%n",&val,&i));
61 sinfg::info("Distance::Distance(): ret=%d, val=%f",ret,val);
66 sinfg::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
72 sinfg::info("Distance::Distance(): system=\"%s\"",String(str.begin()+i,str.end()).c_str());
73 system_=ident_system(String(str.begin()+i,str.end()));
78 Distance::operator=(const sinfg::String& str)
82 int ret(strscanf(str,"%f%n",&val,&i));
83 sinfg::info("Distance::Distance(): ret=%d, val=%f",ret,val);
88 sinfg::error("Distance::Distance(): Bad value \"%s\"",str.c_str());
94 sinfg::String sys(str.begin()+i,str.end());
97 system_=ident_system(sys);
102 Distance::get_string(int digits)const
104 digits=min(9,max(0,digits));
105 String fmt(strprintf("%%.%01df%%s",digits));
106 return strprintf(fmt.c_str(),value_,system_name(system_).c_str());
110 Distance::convert(Distance::System target, const RendDesc& rend_desc)
112 value_=get(target,rend_desc);
117 Distance::get(Distance::System target, const RendDesc& rend_desc)const
119 if(target==SYSTEM_UNITS)
120 return units(rend_desc);
121 if(target==SYSTEM_PIXELS)
122 return units(rend_desc)*METERS_PER_UNIT*rend_desc.get_x_res();
124 return meters_to_system(meters(rend_desc),target);
128 Distance::meters()const
132 case SYSTEM_INCHES: return value_/39.3700787402;
133 case SYSTEM_POINTS: return value_/POINTS_PER_INCH/39.3700787402;
134 case SYSTEM_METERS: return value_;
135 case SYSTEM_CENTIMETERS: return value_/100.0;
136 case SYSTEM_MILLIMETERS: return value_/1000.0;
137 default: throw BadSystem();
142 Distance::meters(const RendDesc& rend_desc)const
144 if(system_>SYSTEM_PIXELS)
146 if(system_==SYSTEM_UNITS)
147 return value_*METERS_PER_UNIT;
148 if(system_==SYSTEM_PIXELS)
149 return value_/rend_desc.get_x_res();
155 Distance::units(const RendDesc& rend_desc)const
157 if(system_==SYSTEM_UNITS)
162 if(system_>SYSTEM_PIXELS)
165 ret=value_/rend_desc.get_x_res();
167 return ret/METERS_PER_UNIT;
172 Distance::meters_to_system(Real x,System target_system)
174 switch(target_system)
176 case SYSTEM_INCHES: return x*39.3700787402;
177 case SYSTEM_POINTS: return x*39.3700787402*POINTS_PER_INCH;
178 case SYSTEM_METERS: return x;
179 case SYSTEM_CENTIMETERS: return x*100.0;
180 case SYSTEM_MILLIMETERS: return x*1000.0;
181 default: throw BadSystem();
185 Distance::System // (static)
186 Distance::ident_system(const sinfg::String& x)
190 // Make it all upper case, and remove white space
191 for(unsigned int i=0;i<x.size();i++)if(x[i]!=' ' && x[i]!='\t')str+=toupper(x[i]);
193 // If it is plural, make it singular
194 if(str[str.size()-1]=='S')
195 str=sinfg::String(str.begin(),str.end()-1);
197 if(str.empty() || str=="U" || str=="UNIT")
199 if(str=="PX" || str=="PIXEL")
200 return SYSTEM_PIXELS;
201 if(str=="PT" || str=="POINT")
202 return SYSTEM_POINTS;
203 if(str=="IN" || str=="\"" || str=="INCHE" || str=="INCH")
204 return SYSTEM_INCHES;
205 if(str=="M" || str=="METER")
206 return SYSTEM_METERS;
207 if(str=="CM" || str=="CENTIMETER")
208 return SYSTEM_CENTIMETERS;
209 if(str=="MM" || str=="MILLIMETER")
210 return SYSTEM_MILLIMETERS;
212 sinfg::warning("Distance::ident_system(): Unknown distance system \"%s\"",x.c_str());
217 sinfg::String // (static)
218 Distance::system_name(Distance::System system)
222 case SYSTEM_UNITS: return "u";
223 case SYSTEM_PIXELS: return "px";
224 case SYSTEM_POINTS: return "pt";
225 case SYSTEM_INCHES: return "in";
226 case SYSTEM_METERS: return "m";
227 case SYSTEM_MILLIMETERS: return "mm";
228 case SYSTEM_CENTIMETERS: return "cm";
230 default: throw BadSystem();
232 return sinfg::String();
235 sinfg::String // (static)
236 Distance::system_local_name(Distance::System system)
240 case SYSTEM_UNITS: return _("Units");
241 case SYSTEM_PIXELS: return _("Pixels");
242 case SYSTEM_POINTS: return _("Points");
243 case SYSTEM_INCHES: return _("Inches");
244 case SYSTEM_METERS: return _("Meters");
245 case SYSTEM_MILLIMETERS:return _("Millimeters");
246 case SYSTEM_CENTIMETERS:return _("Centimeters");
248 default: throw BadSystem();
250 return sinfg::String();