summaryrefslogtreecommitdiffstats
path: root/ksvg/impl/SVGUnitConverter.h
blob: 78eb2528cb724b9c139ace46ca879aa2afa52907 (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
/*
    Copyright (C) 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.
*/

#ifndef SVGUnitConverter_H
#define SVGUnitConverter_H

#include <tqptrdict.h>

#include <dom/dom_string.h>

#include "SVGUnitTypes.h"
#include "SVGShapeImpl.h"
#include "SVGAnimatedLengthImpl.h"

namespace KSVG
{

class SVGUnitConverter
{
public:
	SVGUnitConverter() { m_dict.setAutoDelete(true); }
	~SVGUnitConverter() { }

	void add(SVGAnimatedLengthImpl *obj)
	{
		UnitData *data = new UnitData();
		data->valueAsString = TQString::null;

		m_dict.insert(obj, data);
	}

	void modify(SVGAnimatedLengthImpl *obj, const TQString &value)
	{
		UnitData *data = m_dict.find(obj);

		if(data)
			data->valueAsString = value;
	}

	void finalize(SVGShapeImpl *bboxContext, SVGShapeImpl *userContext, unsigned short unitType)
	{
		bool user = (unitType == SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
		bool bbox = (unitType == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);

		if(!user && !bbox) // Invalid unit type
			return;

		TQPtrDictIterator<UnitData> it(m_dict);
		for(; it.current(); ++it)
		{
			UnitData *data = it.current();

			if(!data)
				continue;

			SVGAnimatedLengthImpl *obj = static_cast<SVGAnimatedLengthImpl *>(it.currentKey());

			if(bbox)
				obj->baseVal()->setBBoxContext(bboxContext);
			else
				obj->baseVal()->setBBoxContext(userContext);

			if(user) // Just assign value, no conversion needed!
				obj->baseVal()->setValueAsString(data->valueAsString);
			else // Convert to objectBoundingBox
				obj->baseVal()->setValueAsString(SVGLengthImpl::convertValToPercentage(data->valueAsString));
		}
	}

private:
	typedef struct
	{
		TQString valueAsString; // Original value
	} UnitData;

	TQPtrDict<UnitData> m_dict;
};

}

#endif

// vim:ts=4:noet