X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=synfig-core%2Ftrunk%2Fsrc%2Fsynfig%2Fdistance.cpp;fp=synfig-core%2Ftrunk%2Fsrc%2Fsynfig%2Fdistance.cpp;h=c1a7e65d15dd5816ab1e0d65cefb45731b3109f6;hb=e3acc0b267b14fda5db3c7bbb2f218b993ef84b3;hp=0000000000000000000000000000000000000000;hpb=ced68168d8518aac837f20e73bcd1e84a6bd6178;p=synfig.git diff --git a/synfig-core/trunk/src/synfig/distance.cpp b/synfig-core/trunk/src/synfig/distance.cpp new file mode 100644 index 0000000..c1a7e65 --- /dev/null +++ b/synfig-core/trunk/src/synfig/distance.cpp @@ -0,0 +1,251 @@ +/* === S I N F G =========================================================== */ +/*! \file distance.cpp +** \brief Template File +** +** $Id: distance.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $ +** +** \legal +** Copyright (c) 2002 Robert B. Quattlebaum Jr. +** +** This software and associated documentation +** are CONFIDENTIAL and PROPRIETARY property of +** the above-mentioned copyright holder. +** +** You may not copy, print, publish, or in any +** other way distribute this software without +** a prior written agreement with +** the copyright holder. +** \endlegal +*/ +/* ========================================================================= */ + +/* === H E A D E R S ======================================================= */ + +#ifdef USING_PCH +# include "pch.h" +#else +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "distance.h" +#include "renddesc.h" +#include "general.h" +#include +#endif + +/* === U S I N G =========================================================== */ + +using namespace std; +using namespace etl; +using namespace sinfg; + +/* === M A C R O S ========================================================= */ + +#define POINTS_PER_INCH (72.0) + +/* === G L O B A L S ======================================================= */ + +#define METERS_PER_UNIT (rend_desc.get_physical_w()/abs(rend_desc.get_tl()[0]-rend_desc.get_br()[0])) + +/* === P R O C E D U R E S ================================================= */ + +/* === M E T H O D S ======================================================= */ + +Distance::Distance(const sinfg::String& str) +{ + (*this)=str; +/* int i(0); + float val; + int ret(strscanf(str,"%f%n",&val,&i)); + sinfg::info("Distance::Distance(): ret=%d, val=%f",ret,val); + + if(ret<=0) + { + // Error + sinfg::error("Distance::Distance(): Bad value \"%s\"",str.c_str()); + value_=0; + } + else + value_=val; + + sinfg::info("Distance::Distance(): system=\"%s\"",String(str.begin()+i,str.end()).c_str()); + system_=ident_system(String(str.begin()+i,str.end())); +*/ +} + +Distance& +Distance::operator=(const sinfg::String& str) +{ + int i(0); + float val; + int ret(strscanf(str,"%f%n",&val,&i)); + sinfg::info("Distance::Distance(): ret=%d, val=%f",ret,val); + + if(ret<=0) + { + // Error + sinfg::error("Distance::Distance(): Bad value \"%s\"",str.c_str()); + return *this; + } + else + value_=val; + + sinfg::String sys(str.begin()+i,str.end()); + + if(sys.size()) + system_=ident_system(sys); + return *this; +} + +sinfg::String +Distance::get_string(int digits)const +{ + digits=min(9,max(0,digits)); + String fmt(strprintf("%%.%01df%%s",digits)); + return strprintf(fmt.c_str(),value_,system_name(system_).c_str()); +} + +void +Distance::convert(Distance::System target, const RendDesc& rend_desc) +{ + value_=get(target,rend_desc); + system_=target; +} + +Real +Distance::get(Distance::System target, const RendDesc& rend_desc)const +{ + if(target==SYSTEM_UNITS) + return units(rend_desc); + if(target==SYSTEM_PIXELS) + return units(rend_desc)*METERS_PER_UNIT*rend_desc.get_x_res(); + + return meters_to_system(meters(rend_desc),target); +} + +Real +Distance::meters()const +{ + switch(system_) + { + case SYSTEM_INCHES: return value_/39.3700787402; + case SYSTEM_POINTS: return value_/POINTS_PER_INCH/39.3700787402; + case SYSTEM_METERS: return value_; + case SYSTEM_CENTIMETERS: return value_/100.0; + case SYSTEM_MILLIMETERS: return value_/1000.0; + default: throw BadSystem(); + } +} + +Real +Distance::meters(const RendDesc& rend_desc)const +{ + if(system_>SYSTEM_PIXELS) + return meters(); + if(system_==SYSTEM_UNITS) + return value_*METERS_PER_UNIT; + if(system_==SYSTEM_PIXELS) + return value_/rend_desc.get_x_res(); + + throw BadSystem(); +} + +Real +Distance::units(const RendDesc& rend_desc)const +{ + if(system_==SYSTEM_UNITS) + return value_; + + Real ret; + + if(system_>SYSTEM_PIXELS) + ret=meters(); + else + ret=value_/rend_desc.get_x_res(); + + return ret/METERS_PER_UNIT; +} + + +Real // (static) +Distance::meters_to_system(Real x,System target_system) +{ + switch(target_system) + { + case SYSTEM_INCHES: return x*39.3700787402; + case SYSTEM_POINTS: return x*39.3700787402*POINTS_PER_INCH; + case SYSTEM_METERS: return x; + case SYSTEM_CENTIMETERS: return x*100.0; + case SYSTEM_MILLIMETERS: return x*1000.0; + default: throw BadSystem(); + } +} + +Distance::System // (static) +Distance::ident_system(const sinfg::String& x) +{ + sinfg::String str; + + // Make it all upper case, and remove white space + for(unsigned int i=0;i