summaryrefslogtreecommitdiffstats
path: root/ksvg/core/KSVGCanvas.h
blob: aa41e6c25c16f4571d9985004ac2b0ccc7df86fc (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
/*
    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
    aint 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 KSVGCANVAS_H
#define KSVGCANVAS_H

#include <tqmap.h>
#include <tqdict.h>
#include <tqcolor.h>
#include <tqobject.h>
#include <tqptrlist.h>
#include <tqdict.h>

#include <Converter.h>

namespace KSVG
{

class SVGShapeImpl;
class SVGMatrixImpl;
class SVGStylableImpl;
class SVGElementImpl;
class SVGTextElementImpl;
class SVGRectElementImpl;
class SVGLineElementImpl;
class SVGPathElementImpl;
class SVGImageElementImpl;
class SVGCircleElementImpl;
class SVGMarkerElementImpl;
class SVGEllipseElementImpl;
class SVGPolygonElementImpl;
class SVGPolylineElementImpl;
class SVGClipPathElementImpl;

class CanvasItem;
class CanvasChunk;
class CanvasItemList;
class CanvasClipPath;
class CanvasPaintServer;

// Must be a TQObject to be able to be loaded by KLibLoader...
class KSVGCanvas : public QObject
{
Q_OBJECT
public:
	KSVGCanvas(unsigned int width, unsigned int height);
	virtual ~KSVGCanvas();

	void setViewportDimension(unsigned int w, unsigned int h);

	void setup(TQPaintDevice *drawWidget, TQPaintDevice *directWindow);
	void setup(unsigned char *buffer, unsigned int width = 0, unsigned int height = 0);

	void reset();
	void update();
	void update(float zoomFactor);
	void update(const TQPoint &panPoint, bool erase = true);
	void resize(unsigned int w, unsigned int h);
	void retune(unsigned int csh, unsigned int csv);
	void invalidate(CanvasItem *item, bool recalc = true);
	CanvasItemList collisions(const TQPoint &p, bool exact = false) const;

	void setBackgroundColor(const TQColor &c) { m_backgroundColor = c; }
	void blit();
	void blit(const TQRect &rect, bool direct);

	float zoom() const { return m_zoom; }
	TQPoint pan() const { return m_pan; }
	void setPan(const TQPoint &pan) { m_pan = pan; }

	int width() const { return m_width; }
	int height() const { return m_height; }

	virtual void setRenderBufferSize(int w, int h);
	void clipToBuffer(int &x0, int &y0, int &x1, int &y1) const;

	// creating canvas items
	virtual CanvasItem *createRectangle(SVGRectElementImpl *rect) = 0;
	virtual CanvasItem *createEllipse(SVGEllipseElementImpl *ellipse) = 0;
	virtual CanvasItem *createCircle(SVGCircleElementImpl *circle) = 0;
	virtual CanvasItem *createLine(SVGLineElementImpl *line) = 0;
	virtual CanvasItem *createPolyline(SVGPolylineElementImpl *poly) = 0;
	virtual CanvasItem *createPolygon(SVGPolygonElementImpl *poly) = 0;
	virtual CanvasItem *createPath(SVGPathElementImpl *path) = 0;
	virtual CanvasItem *createClipPath(SVGClipPathElementImpl *clippath) = 0;
	virtual CanvasItem *createImage(SVGImageElementImpl *image) = 0;
	virtual CanvasItem *createCanvasMarker(SVGMarkerElementImpl *marker) = 0;
	virtual CanvasItem *createText(SVGTextElementImpl *text) = 0;
	virtual CanvasPaintServer *createPaintServer(SVGElementImpl *pserver) = 0;

	void insert(CanvasItem *item, int z = -1);
	void removeItem(CanvasItem *);

	// Enable to have the canvas updated and blitted on item insertion.
	void setImmediateUpdate(bool immediateUpdate) { m_immediateUpdate = immediateUpdate; }
	bool immediateUpdate() const { return m_immediateUpdate; }

	TQPtrList<CanvasItem> allItems() const { return m_items; }

	unsigned char *renderingBuffer() const { return m_buffer; }
	unsigned int nrChannels() const { return m_nrChannels; }
	unsigned int rowStride() const { return m_nrChannels * m_width; }

	T2P::Converter *fontContext() { return m_fontContext; }
	TQPaintDevice *drawWindow() { return m_drawWindow; }
	TQPaintDevice *directWindow() { return m_directWindow; }

	T2P::FontVisualParams *fontVisualParams(SVGStylableImpl *style) const;
	virtual T2P::BezierPath *toBezierPath(CanvasItem *item) const { Q_UNUSED(item); return 0; }

	// Assign z indices to the element and its children, starting with z, and 
	// return the next z value to be used.
	unsigned int setElementItemZIndexRecursive(SVGElementImpl *element, unsigned int z);

protected:
	void addToChunks(CanvasItem *item);
	void removeFromChunks(CanvasItem *item);

	void initVars();

	void fill();
	void clear(const TQRect &r);

	virtual void setBuffer(unsigned char *buffer);

protected:
	class ChunkManager
	{
	public:
		ChunkManager() { m_chunks.setAutoDelete(true); }
		void addChunk(CanvasChunk *chunk);
		CanvasChunk *getChunk(short x, short y) const;

		void clear();

	private:
		TQDict<CanvasChunk> m_chunks;
	} m_chunkManager;

	TQValueList<CanvasChunk *> m_dirtyChunks;

	TQMap<CanvasItem *, TQPtrList<CanvasChunk> > m_chunksByItem;
	TQPtrList<CanvasItem> m_items;

	TQDict<CanvasClipPath> m_clipPaths;

	unsigned int m_viewportWidth, m_viewportHeight;
	int m_width, m_height;
	int m_chunkSizeHor, m_chunkSizeVer;

	TQPaintDevice *m_drawWindow;
	TQPaintDevice *m_directWindow;

	float m_zoom;
	TQPoint m_pan;

	GC m_gc;

	T2P::Converter *m_fontContext;

	unsigned char *m_buffer;
	unsigned int m_nrChannels;

	TQColor m_backgroundColor;

	bool m_immediateUpdate;
};

}

#endif

// vim:ts=4:noet