1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        SVGCanvasItem.h
3 // Purpose:     Canvas items
4 // Author:      Alex Thuering
5 // Created:     2005/05/09
6 // RCS-ID:      $Id: SVGCanvasItem.h,v 1.29 2016/07/27 08:54:21 ntalex Exp $
7 // Copyright:   (c) 2005 Alex Thuering
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef WX_SVG_CANVAS_ITEM_H
12 #define WX_SVG_CANVAS_ITEM_H
13 
14 #include "svg.h"
15 #include <wx/dynarray.h>
16 #include <vector>
17 
18 using std::vector;
19 
20 enum wxSVGCanvasItemType {
21 	wxSVG_CANVAS_ITEM_PATH,
22 	wxSVG_CANVAS_ITEM_TEXT,
23 	wxSVG_CANVAS_ITEM_IMAGE,
24 	wxSVG_CANVAS_ITEM_VIDEO
25 };
26 
27 
28 struct wxSVGMark {
29 	enum Type {
30 		START, MID, END
31 	};
32 
33 	double x, y, angle;
34 	Type type;
35 
wxSVGMarkwxSVGMark36 	wxSVGMark(double aX, double aY, double aAngle, Type aType): x(aX), y(aY), angle(aAngle), type(aType) {}
37 };
38 
39 /** Base class for canvas items */
40 class wxSVGCanvasItem {
41   public:
wxSVGCanvasItem(wxSVGCanvasItemType type)42 	wxSVGCanvasItem(wxSVGCanvasItemType type) { m_type = type; }
~wxSVGCanvasItem()43 	virtual ~wxSVGCanvasItem() {}
GetType()44 	wxSVGCanvasItemType GetType() { return m_type; }
45 
46     /** returns the bounding box of the item */
47     virtual wxSVGRect GetBBox(const wxSVGMatrix* matrix = NULL) { return wxSVGRect(); }
48     virtual wxSVGRect GetResultBBox(const wxCSSStyleDeclaration& style,
49       const wxSVGMatrix* matrix = NULL) { return GetBBox(matrix); }
50 
51   protected:
52 	wxSVGCanvasItemType m_type;
53 };
54 
55 /** Canvas item, that saves a graphic path (SVGPathElement) and
56   * and other elements that can be converted to a path (SVGRectElement, etc.)
57   */
58 class wxSVGCanvasPath: public wxSVGCanvasItem
59 {
60   public:
61 	wxSVGCanvasPath();
~wxSVGCanvasPath()62 	virtual ~wxSVGCanvasPath() {}
63 
64 	void Init(wxSVGLineElement& element);
65 	void Init(wxSVGPolylineElement& element);
66 	void Init(wxSVGPolygonElement& element);
67 	void Init(wxSVGRectElement& element);
68 	void Init(wxSVGCircleElement& element);
69 	void Init(wxSVGEllipseElement& element);
70 	void Init(wxSVGPathElement& element);
71 
72 	void MoveTo(double x, double y, bool relative = false);
73 	void LineTo(double x, double y, bool relative = false);
74 	void LineToHorizontal(double x, bool relative = false);
75 	void LineToVertical(double y, bool relative = false);
76 	void CurveToCubic(double x1, double y1, double x2, double y2, double x, double y, bool relative = false);
77 	void CurveToCubicSmooth(double x2, double y2, double x, double y, bool relative = false);
78 	void CurveToQuadratic(double x1, double y1, double x, double y, bool relative = false);
79 	void CurveToQuadraticSmooth(double x, double y, bool relative = false);
80 	void Arc(double x, double y, double r1, double r2, double angle,
81 	  bool largeArcFlag, bool sweepFlag, bool relative = false);
82 	bool ClosePath();
83 
84 	virtual void End() = 0;
85 
86 	inline void SetFill(bool fill = true) { m_fill = fill; }
GetFill()87 	inline bool GetFill() { return m_fill; }
88 
89     /** returns the marker points */
90     virtual vector<wxSVGMark> GetMarkPoints();
91 
92   protected:
93     wxSVGElement* m_element;
94 	bool m_fill; /* define, if a path can be filled (disabled for line) */
95 	double m_curx, m_cury, m_cubicx, m_cubicy, m_quadx, m_quady, m_begx, m_begy;
96 	virtual void MoveToImpl(double x, double y) = 0;
97 	virtual void LineToImpl(double x, double y) = 0;
98 	virtual void CurveToCubicImpl(double x1, double y1, double x2, double y2, double x, double y) = 0;
99 	virtual bool ClosePathImpl() = 0;
100 };
101 
102 /** character */
103 struct wxSVGCanvasTextChar {
104 	wxSVGCanvasPath* path;
105 	wxSVGRect bbox;
106 };
107 WX_DECLARE_OBJARRAY(wxSVGCanvasTextChar, wxSVGCanvasTextCharList);
108 
109 /** text-chunk */
110 struct wxSVGCanvasTextChunk {
111   double x;
112   double y;
113   wxString text;
114   wxSVGCanvasTextCharList chars;
115   wxCSSStyleDeclaration style;
116   wxSVGMatrix matrix;
117   wxSVGRect GetBBox(const wxSVGMatrix* matrix);
GetBBoxwxSVGCanvasTextChunk118   wxSVGRect GetBBox() { return GetBBox(NULL); }
119 };
120 
121 WX_DECLARE_OBJARRAY(wxSVGCanvasTextChunk, wxSVGCanvasTextChunkList);
122 
123 /** Canvas item, that saves text (SVGTextElement) as list of chunks */
124 class wxSVGCanvasText: public wxSVGCanvasItem
125 {
126   public:
127 	wxSVGCanvasText(wxSVGCanvas* canvas);
128 	virtual ~wxSVGCanvasText();
129 
130 	virtual void Init(wxSVGTextElement& element, const wxCSSStyleDeclaration& style, wxSVGMatrix* matrix);
131     virtual wxSVGRect GetBBox(const wxSVGMatrix* matrix = NULL);
132 	virtual long GetNumberOfChars();
133     virtual double GetComputedTextLength();
134     virtual double GetSubStringLength(unsigned long charnum, unsigned long nchars);
135     virtual wxSVGPoint GetStartPositionOfChar(unsigned long charnum);
136     virtual wxSVGPoint GetEndPositionOfChar(unsigned long charnum);
137     virtual wxSVGRect GetExtentOfChar(unsigned long charnum);
138     virtual double GetRotationOfChar(unsigned long charnum);
139     virtual long GetCharNumAtPosition(const wxSVGPoint& point);
140 
141   public:
142     wxSVGCanvasTextChunkList m_chunks; /** list of text-chunks */
143 	wxSVGCanvasTextChar* m_char; /** current char */
144 
145   protected:
146     wxSVGCanvas* m_canvas;
147     double m_tx, m_ty; /** current text position */
148     wxCSS_VALUE m_textAnchor; /** current text anchor */
149 	int m_textAnchorBeginIndex; /** index of first chunk with current text anchor */
150 	double m_textAnchorBeginPos; /** x-coordinate of text with current text anchor */
151 	wxCSS_VALUE m_dominantBaseline; /** current dominant baseline */
152     int m_dominantBaselineBeginIndex; /** index of first chunk with current baseline */
153 	virtual void Init(wxSVGTSpanElement& element, const wxCSSStyleDeclaration& style, wxSVGMatrix* matrix);
154 	virtual void InitChildren(wxSVGTextPositioningElement& element, const wxCSSStyleDeclaration& style,
155 			wxSVGMatrix* matrix);
156 	virtual void AddChunk(const wxString& text, const wxCSSStyleDeclaration& style, wxSVGMatrix* matrix);
157 	virtual void BeginChar(wxSVGMatrix* matrix);
158 	virtual void EndChar();
159 	virtual void EndTextAnchor();
160 	wxSVGCanvasTextChunk* GetChunk(unsigned long& charnum);
161     /** Converts text in path and saves in current chunk (m_chunk->path) */
162     virtual void InitText(const wxString& text, const wxCSSStyleDeclaration& style, wxSVGMatrix* matrix) = 0;
163 };
164 
165 class wxSVGCanvasSvgImageData {
166 public:
167 	wxSVGCanvasSvgImageData(const wxString& filename, wxSVGDocument* doc);
168 	wxSVGCanvasSvgImageData(wxSVGSVGElement* svgImage, wxSVGDocument* doc);
169 	~wxSVGCanvasSvgImageData();
170 
IncRef()171 	void IncRef() { m_count++; }
DecRef()172 	int DecRef() { return (--m_count); }
173 
GetSvgImage()174 	inline wxSVGSVGElement* GetSvgImage() { return m_svgImage; }
175 
176 private:
177     int m_count;
178     wxSVGSVGElement* m_svgImage;
179 };
180 
181 /** Canvas item, that saves image (SVGImageElement) */
182 class wxSVGCanvasImage: public wxSVGCanvasItem {
183 public:
wxSVGCanvasImage()184 	wxSVGCanvasImage(): wxSVGCanvasItem(wxSVG_CANVAS_ITEM_IMAGE), m_x(0), m_y(0), m_width(0), m_height(0),
185 		m_defHeightScale(1), m_svgImageData(NULL) {}
wxSVGCanvasImage(wxSVGCanvasItemType type)186 	wxSVGCanvasImage(wxSVGCanvasItemType type): wxSVGCanvasItem(type), m_x(0), m_y(0), m_width(0), m_height(0),
187 		m_defHeightScale(1), m_svgImageData(NULL) {}
188 	virtual ~wxSVGCanvasImage();
189 	virtual void Init(wxSVGImageElement& element, const wxCSSStyleDeclaration& style, wxProgressDialog* progressDlg);
190 	virtual int GetDefaultWidth();
191 	virtual int GetDefaultHeight();
GetPreserveAspectRatio()192 	const wxSVGPreserveAspectRatio& GetPreserveAspectRatio() { return m_preserveAspectRatio; }
193 	wxSVGSVGElement* GetSvgImage(wxSVGDocument* doc = NULL);
194 
195 public:
196 	double m_x, m_y, m_width, m_height; /** position and size of image */
197     wxString m_href; /** link to the image (filename) */
198 	wxImage m_image; /** image data */
199 	double m_defHeightScale;
200 	wxSVGPreserveAspectRatio m_preserveAspectRatio;
201 	wxSVGCanvasSvgImageData* m_svgImageData;
202 };
203 
204 class wxFfmpegMediaDecoder;
205 
206 /** CanvasVideoData */
207 class wxSVGCanvasVideoData {
208 public:
209 	wxSVGCanvasVideoData(wxFfmpegMediaDecoder* mediaDecoder);
210 	~wxSVGCanvasVideoData();
211 
IncRef()212 	void IncRef() { m_count++; }
DecRef()213 	int DecRef() { return (--m_count); }
214 
GetMediaDecoder()215 	wxFfmpegMediaDecoder* GetMediaDecoder() { return m_mediaDecoder; }
216 	wxImage GetImage(double time);
217 
218 private:
219 	int m_count;
220 	wxFfmpegMediaDecoder* m_mediaDecoder;
221 	wxImage m_image;
222 };
223 
224 /** Canvas item, that saves video (wxSVGVideoElement) */
225 class wxSVGCanvasVideo: public wxSVGCanvasImage {
226 public:
227 	wxSVGCanvasVideo();
228 	virtual ~wxSVGCanvasVideo();
229 	virtual void Init(wxSVGVideoElement& element, const wxCSSStyleDeclaration& style, wxProgressDialog* progressDlg);
GetDuration()230 	double GetDuration() { return m_duration; }
231 
232 public:
233 	double m_time; /** time of the loaded frame */
234 	double m_duration;
235 	wxSVGCanvasVideoData* m_videoData;
236 };
237 
238 #endif // WX_SVG_CANVAS_ITEM_H
239