summaryrefslogtreecommitdiffstats
path: root/ksvg/impl/SVGAnimationElementImpl.cc
blob: b87a982954263920c5a1aae53d14538af64bfbae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
    Copyright (C) 2001-2003 KSVG Team
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <math.h>

#include <kdebug.h>

#include <tqtimer.h>

#include "CanvasItem.h"
#include "SVGHelperImpl.h"
#include "SVGDocumentImpl.h"
#include "SVGSVGElementImpl.h"
#include "SVGStringListImpl.h"
#include "SVGURIReferenceImpl.h"
#include "SVGAnimationElementImpl.h"

using namespace KSVG;

#include "SVGAnimationElementImpl.lut.h"
#include "ksvg_bridge.h"
#include "ksvg_ecma.h"

SVGAnimationElementImpl::SVGAnimationElementImpl(DOM::ElementImpl *impl) : SVGElementImpl(impl), SVGTestsImpl(), SVGExternalResourcesRequiredImpl()
{
	KSVG_EMPTY_FLAGS

	m_connected = false;
	m_targetElement = 0;

	m_values = new SVGStringListImpl();
	m_keyTimes= new SVGStringListImpl();
	m_keySplines = new SVGStringListImpl();

	m_fill = REMOVE;
	m_additive = REPLACE;
	m_accumulate = ACCUMULATE_NONE;
}

SVGAnimationElementImpl::~SVGAnimationElementImpl()
{
	if(m_targetElement)
		m_targetElement->deref();
}

SVGElementImpl *SVGAnimationElementImpl::targetElement() const
{
	if(!m_targetElement)
	{
		SVGAnimationElementImpl *modify = const_cast<SVGAnimationElementImpl *>(this);
		if(!m_href.isEmpty())
			modify->setTargetElement(ownerDoc()->getElementByIdRecursive(ownerSVGElement(), SVGURIReferenceImpl::getTarget(m_href)));
		else if(!parentNode().isNull())
			modify->setTargetElement(ownerDoc()->getElementFromHandle(parentNode().handle()));
	}

	return m_targetElement;
}

double SVGAnimationElementImpl::parseClockValue(const TQString &data) const
{
	TQString parse = data.stripWhiteSpace();
	TQString debugOutput = "parseClockValue(" + parse + ") -> ";
	
	if(parse == "indefinite") // Saves some time...
		return -1;

	double result;

	int doublePointOne = parse.find(':');
	int doublePointTwo = parse.find(':', doublePointOne + 1);

	if(doublePointOne != -1 && doublePointTwo != -1) // Spec: "Full clock values"
	{
		unsigned int hours = parse.mid(0, 2).toUInt();
		unsigned int minutes = parse.mid(3, 2).toUInt();
		unsigned int seconds = parse.mid(6, 2).toUInt();
		unsigned int milliseconds = 0;

		result = (3600 * hours) + (60 * minutes) + seconds;

		if(parse.find('.') != -1)
		{
			TQString temp = parse.mid(9, 2);
			milliseconds = temp.toUInt();
			result += (milliseconds * (1 / pow(10.0, temp.length())));
		}
	}
	else if(doublePointOne != -1 && doublePointTwo == -1) // Spec: "Partial clock values"
	{
		unsigned int minutes = parse.mid(0, 2).toUInt();
		unsigned int seconds = parse.mid(3, 2).toUInt();
		unsigned int milliseconds = 0;

		result = (60 * minutes) + seconds;

		if(parse.find('.') != -1)
		{
			TQString temp = parse.mid(6, 2);
			milliseconds = temp.toUInt();
			result += (milliseconds * (1 / pow(10.0, temp.length())));
		}
	}
	else // Spec: "Timecount values"
	{
		int dotPosition = parse.find('.');

		if(parse.endsWith("h"))
		{
			if(dotPosition == -1)
				result = parse.mid(0, parse.length() - 1).toUInt() * 3600;
			else
			{
				result = parse.mid(0, dotPosition).toUInt() * 3600;
				TQString temp = parse.mid(dotPosition + 1, parse.length() - dotPosition - 2);
				result += (3600.0 * temp.toUInt()) * (1 / pow(10.0, temp.length()));
			}
		}
		else if(parse.endsWith("min"))
		{
			if(dotPosition == -1)
				result = parse.mid(0, parse.length() - 3).toUInt() * 60;
			else
			{
				result = parse.mid(0, dotPosition).toUInt() * 60;
				TQString temp = parse.mid(dotPosition + 1, parse.length() - dotPosition - 4);
				result += (60.0 * temp.toUInt()) * (1 / pow(10.0, temp.length()));
			}
		}
		else if(parse.endsWith("ms"))
		{
			if(dotPosition == -1)
				result = parse.mid(0, parse.length() - 2).toUInt() / 1000.0;
			else
			{
				result = parse.mid(0, dotPosition).toUInt() / 1000.0;
				TQString temp = parse.mid(dotPosition + 1, parse.length() - dotPosition - 3);
				result += (temp.toUInt() / 1000.0) * (1 / pow(10.0, temp.length()));
			}
		}
		else if(parse.endsWith("s"))
		{
			if(dotPosition == -1)
				result = parse.mid(0, parse.length() - 1).toUInt();
			else
			{
				result = parse.mid(0, dotPosition).toUInt();
				TQString temp = parse.mid(dotPosition + 1, parse.length() - dotPosition - 2);
				result += temp.toUInt() * (1 / pow(10.0, temp.length()));
			}
		}
		else
			result = parse.toDouble();
	}

	kdDebug() << debugOutput << result << endl;
	return result;
}

/*
@namespace KSVG
@begin SVGAnimationElementImpl::s_hashTable 23 
 targetElement	SVGAnimationElementImpl::TargetElement	DontDelete|ReadOnly
 href			SVGAnimationElementImpl::Href			DontDelete|ReadOnly
 additive		SVGAnimationElementImpl::Additive		DontDelete|ReadOnly
 accumulate		SVGAnimationElementImpl::Accumulate		DontDelete|ReadOnly
 attributeName	SVGAnimationElementImpl::AttributeName	DontDelete|ReadOnly
 attributeType	SVGAnimationElementImpl::AttributeType	DontDelete|ReadOnly
 calcMode		SVGAnimationElementImpl::CalcMode		DontDelete|ReadOnly
 values			SVGAnimationElementImpl::Values			DontDelete|ReadOnly
 keyTimes		SVGAnimationElementImpl::KeyTimes		DontDelete|ReadOnly
 keySplines		SVGAnimationElementImpl::KeySplines		DontDelete|ReadOnly
 from			SVGAnimationElementImpl::From			DontDelete|ReadOnly
 to				SVGAnimationElementImpl::To				DontDelete|ReadOnly
 by				SVGAnimationElementImpl::By				DontDelete|ReadOnly
 begin			SVGAnimationElementImpl::Begin			DontDelete|ReadOnly
 dur			SVGAnimationElementImpl::Dur			DontDelete|ReadOnly
 end			SVGAnimationElementImpl::End			DontDelete|ReadOnly
 min			SVGAnimationElementImpl::Min			DontDelete|ReadOnly
 max			SVGAnimationElementImpl::Max			DontDelete|ReadOnly
 restart		SVGAnimationElementImpl::Restart		DontDelete|ReadOnly
 repeatCount	SVGAnimationElementImpl::RepeatCount	DontDelete|ReadOnly
 repeatDur		SVGAnimationElementImpl::RepeatDur		DontDelete|ReadOnly
 fill			SVGAnimationElementImpl::Fill			DontDelete|ReadOnly
@end
@namespace KSVG
@begin SVGAnimationElementImplProto::s_hashTable 5
 getStartTime		SVGAnimationElementImpl::GetStartTime		DontDelete|Function 0
 getCurrentTime		SVGAnimationElementImpl::GetCurrentTime		DontDelete|Function 0
 getSimpleDuration	SVGAnimationElementImpl::GetSimpleDuration	DontDelete|Function 0
@end
*/

KSVG_IMPLEMENT_PROTOTYPE("SVGAnimationElement",SVGAnimationElementImplProto,SVGAnimationElementImplProtoFunc)

Value SVGAnimationElementImpl::getValueProperty(ExecState *exec, int token) const
{
	switch(token)
	{
		case TargetElement:
			return m_targetElement->cache(exec);
		default:
			kdWarning() << "Unhandled token in " << k_funcinfo << " : " << token << endl;
			return Undefined();
	}
}

void SVGAnimationElementImpl::putValueProperty(ExecState *exec, int token, const Value &value, int attr)
{
	// This class has just ReadOnly properties, only with the Internal flag set
	// it's allowed to modify those.
	if(!(attr & KJS::Internal))
		return;

	TQString val = value.toString(exec).qstring();
	switch(token)
	{
		case Href:
			m_href = val;
			break;
		case Additive:
			m_additive = (val == "sum") ? SUM : REPLACE;
			break;
		case Accumulate:
			m_accumulate = (val == "sum") ? ACCUMULATE_SUM : ACCUMULATE_NONE;
			break;
		case AttributeName:
			m_attributeName = val;
			break;
		case AttributeType:
			if(val == "css")
				m_attributeType = CSS;
			else if(val == "xml")
				m_attributeType = XML;
			else
				m_attributeType = AUTO;
			break;
		case CalcMode: // FIXME: See spec for default values!!!
			if(val == "discrete")
				m_calcMode = DISCRETE;
			else if(val == "linear")
				m_calcMode = LINEAR;
			else if(val == "spline")
				m_calcMode = SPLINE;
			else if(val == "paced")
				m_calcMode = PACED;
			break;
		case Values:
			SVGHelperImpl::parseSemicolonSeperatedList(m_values, val);
			break;
		case KeyTimes:
			SVGHelperImpl::parseSemicolonSeperatedList(m_keyTimes, val);
			break;
		case KeySplines:
			SVGHelperImpl::parseSemicolonSeperatedList(m_keySplines, val);
			break;
		case From:
			m_from = val;
			break;
		case To:
			m_to = val;
			break;
		case By:
			m_by = val;
			break;
		case Begin:
		case End:
		{
			// Create list
			SVGStringListImpl *temp = new SVGStringListImpl();
			temp->ref();
			
			// Feed data into list
			SVGHelperImpl::parseSemicolonSeperatedList(temp, val);

			// Parse data
			for(unsigned int i = 0; i < temp->numberOfItems(); i++)
			{
				TQString current = temp->getItem(i)->string();
				
				if(current.startsWith("accessKey"))
				{
					// Register keyDownEventListener for the character
					TQString character = current.mid(current.length() - 2, 1);

					kdDebug() << "ACCESSKEY CHARACTER " << character << endl;
				}
				else if(current.startsWith("wallclock"))
				{
					int firstBrace = current.find("(");
					int secondBrace = current.find(")");
					
					TQString wallclockValue = current.mid(firstBrace + 1, secondBrace - firstBrace - 2);

					kdDebug() << "WALLCLOCK VALUE " << wallclockValue << endl;
				}	
				else if(current.contains("."))
				{
					int dotPosition = current.find(".");
					
					TQString element = current.mid(0, dotPosition);
					TQString clockValue;

					if(current.contains("begin"))
						clockValue = current.mid(dotPosition + 6);
					else if(current.contains("end"))
						clockValue = current.mid(dotPosition + 4);
					else if(current.contains("repeat"))
						clockValue = current.mid(dotPosition + 7);
					else // DOM2 Event Reference
					{
						int plusMinusPosition = -1;
						
						if(current.contains("+"))
							plusMinusPosition = current.find("+");
						else if(current.contains("-"))
							plusMinusPosition = current.find("-");

						TQString event = current.mid(dotPosition + 1, plusMinusPosition - dotPosition - 1);

						clockValue = current.mid(dotPosition + event.length() + 1);
						kdDebug() << "EVENT " << event << endl;
					}

					kdDebug() << "ELEMENT " << element << " CLOCKVALUE " << clockValue << endl;
				}
				else
				{
					if(token == Begin)
						m_begin = parseClockValue(current);
					else
						m_end = parseClockValue(current);
				}
			}

			temp->deref();
			
			break;
		}
		case Dur:
			m_duration = parseClockValue(val);
			break;
//		case Min:
//		case Max:
		case Restart:
			if(val == "whenNotActive")
				m_restart = WHENNOTACTIVE;
			else if(val == "never")
				m_restart = NEVER;
			else
				m_restart = ALWAYS;
			break;
		case RepeatCount:
			m_repeatCount = val;
			break;
		case RepeatDur:
			m_repeatDur = val;
			break;
		case Fill:
			m_fill = (val == "freeze") ? FREEZE : REMOVE;
			break;
		default:
			kdWarning() << "Unhandled token in " << k_funcinfo << " : " << token << endl;
	}
}

Value SVGAnimationElementImplProtoFunc::call(ExecState *exec, Object &thisObj, const List &)
{
	KSVG_CHECK_THIS(SVGAnimationElementImpl)

	switch(id)
	{
		case SVGAnimationElementImpl::GetStartTime:
			return Number(obj->getStartTime());
		case SVGAnimationElementImpl::GetCurrentTime:
			return Number(obj->getCurrentTime());
		case SVGAnimationElementImpl::GetSimpleDuration:
			return Number(obj->getSimpleDuration());
		default:
			kdWarning() << "Unhandled function id in " << k_funcinfo << " : " << id << endl;
			break;
	}

	return Undefined();
}

void SVGAnimationElementImpl::setAttributes()
{
	SVGElementImpl::setAttributes();

	// Spec: Default value is "replace"
	if(KSVG_TOKEN_NOT_PARSED(Additive))
		KSVG_SET_ALT_ATTRIBUTE(Additive, "replace")
	
	// Spec: Default value is "none"
	if(KSVG_TOKEN_NOT_PARSED(Accumulate))
		KSVG_SET_ALT_ATTRIBUTE(Accumulate, "none")

	// Spec: Default value is "always"
	if(KSVG_TOKEN_NOT_PARSED(Restart))
		KSVG_SET_ALT_ATTRIBUTE(Restart, "always")
}

void SVGAnimationElementImpl::setTargetElement(SVGElementImpl *target)
{
	if(m_targetElement)
		m_targetElement->deref();

	m_targetElement = target;
	m_targetElement->ref();
}

void SVGAnimationElementImpl::applyAttribute(const TQString &name, const TQString &value)
{
	SVGElementImpl *target = targetElement();
	if(!target)
	{
		kdDebug() << k_funcinfo << " name: " << name << " value: " << value << " NO TARGET ELEMENT!" << endl;
		return;
	}
	
	// The only two cases I can imagine, where combining is needed (Niko)
	bool combine = (name == "style" || name == "transform");

	if(!combine)
		target->setAttributeInternal(name, value);
	else
	{
		kdDebug() << "TODO COMBINE: " << value << " NAME " << name << endl;
	}
}

double SVGAnimationElementImpl::getStartTime() const
{
	return m_begin;
}

double SVGAnimationElementImpl::getCurrentTime() const
{
	return 0.0;
}

double SVGAnimationElementImpl::getSimpleDuration() const
{
	return m_duration;
}