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) 2007, 2008 Chris Moore
10 ** Copyright (c) 2008 Gerco Ballintijn
11 ** Copyright (c) 2008 Carlos López
13 ** This package is free software; you can redistribute it and/or
14 ** modify it under the terms of the GNU General Public License as
15 ** published by the Free Software Foundation; either version 2 of
16 ** the License, or (at your option) any later version.
18 ** This package is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ** General Public License for more details.
24 /* ========================================================================= */
26 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
50 extern "C" { int _isnan(double x); }
55 // For some reason isnan() isn't working on macosx any more.
56 // This is a quick fix.
57 #if defined(__APPLE__) && !defined(SYNFIG_ISNAN_FIX)
61 inline bool isnan(double x) { return x != x; }
62 inline bool isnan(float x) { return x != x; }
63 #define SYNFIG_ISNAN_FIX 1
69 /* === U S I N G =========================================================== */
73 using namespace synfig;
75 #define tolower ::tolower
77 /* === M A C R O S ========================================================= */
79 /* === G L O B A L S ======================================================= */
81 /* === M E T H O D S ======================================================= */
83 Time::Time(const String &str_, float fps):
87 std::transform(str.begin(),str.end(),str.begin(),&tolower);
89 // Start/Begin Of Time
90 if(str=="sot" || str=="bot")
107 // Now try to read it in the letter-abbreviated format
108 while(pos<str.size() && sscanf(String(str,pos).c_str(),"%f%n",&amount,&read))
111 if(pos>=str.size() || read==0)
113 // Throw up a warning if there are no units
114 // and the amount isn't zero. There is no need
115 // to warn about units if the value is zero
116 // it is the only case where units are irrelevant.
118 synfig::warning("Time(): No unit provided in time code, assuming SECONDS (\"%s\")",str.c_str());
141 synfig::warning("Time(): Individual frames referenced, but frame rate is unknown");
144 // try to read it in as a traditional time format
146 int hour,minute,second;
148 if(fps && sscanf(str.c_str(),"%d:%d:%d.%f",&hour,&minute,&second,&frame)==4)
150 value_=frame/fps+(hour*3600+minute*60+second);
154 if(sscanf(str.c_str(),"%d:%d:%d",&hour,&minute,&second)==3)
156 value_=hour*3600+minute*60+second;
160 synfig::warning("Time(): Bad time format");
165 synfig::warning("Time(): Unexpected character '%c' when parsing time string \"%s\"",str[pos],str.c_str());
174 Time::get_string(float fps, Time::Format format)const
179 return "SOT"; // Start Of Time
181 return "EOT"; // End Of Time
185 if(ceil(time.value_)-time.value_<epsilon_())
186 time.value_=ceil(time.value_);
188 int hour = 0, minute = 0;
189 if(!(format<=FORMAT_FRAMES))
191 hour=time/3600;time-=hour*3600;
192 minute=time/60;time-=minute*60;
194 // <= is redefined, so this means "is the FORMAT_VIDEO bit set in the format?"
195 if(format<=FORMAT_VIDEO)
198 second=time;time-=second;
203 frame=round_to_int(time*fps);
205 return strprintf("%02d:%02d:%02d.%02d",hour,minute,second,frame);
208 return strprintf("%02d:%02d:%02d",hour,minute,second);
211 if (format <= FORMAT_FRAMES)
214 return strprintf("%df", round_to_int(time * fps));
216 return strprintf("%ds", round_to_int(time * 1));
220 bool started = false;
222 if(format<=FORMAT_FULL || hour)
224 ret+=strprintf("%dh",hour);
228 if(format<=FORMAT_FULL || minute)
230 if (!(format<=FORMAT_NOSPACES) && started)
233 ret += strprintf("%dm", minute);
241 second=time;time-=second;
244 if(format<=FORMAT_FULL || second)
246 if (!(format<=FORMAT_NOSPACES) && started)
249 ret += strprintf("%ds", (int)second);
253 if(format<=FORMAT_FULL || abs(frame) > epsilon_() || !started)
255 if (!(format<=FORMAT_NOSPACES) && started)
258 if(abs(frame-floor(frame) >= epsilon_()))
259 ret += strprintf("%0.3ff", frame);
261 ret += strprintf("%0.0ff", frame);
268 if(format<=FORMAT_FULL || second || !started)
270 if (!(format<=FORMAT_NOSPACES) && started)
273 if(abs(second-floor(second))>=epsilon_())
275 String seconds(strprintf("%0.8f",second));
277 // skip trailing zeros
279 String::reverse_iterator i = seconds.rbegin();
280 for ( ; (*i) == '0'; i++)
283 // if we removed too many, go back one place, leaving one zero
284 if (*i < '0' || *i > '9') count--;
286 ret += seconds.substr(0, seconds.size()-count) + "s";
289 ret+=strprintf("%0.0fs",second);
297 Time::round(float fps)const
301 value_type time(*this);
305 if(abs(time-floor(time))<0.5)
306 return floor(time)/fps;
308 return ceil(time)/fps;
315 return get_string().c_str();
321 Time::is_valid()const
323 return !isnan(value_);