xref: /haiku/src/tests/kits/interface/picture/SVGViewView.h (revision 6d7838a16b939b9009c9831faab32786354de2a7)
1 #ifndef _SVGVIEW_VIEW_H
2 #define _SVGVIEW_VIEW_H
3 
4 // Standard Includes -----------------------------------------------------------
5 #include <expat.h>
6 
7 // System Includes -------------------------------------------------------------
8 #include <InterfaceKit.h>
9 #include <SupportKit.h>
10 #include <TranslationUtils.h>
11 
12 // Project Includes ------------------------------------------------------------
13 #include "Matrix.h"
14 
15 // Local Includes --------------------------------------------------------------
16 
17 // Local Defines ---------------------------------------------------------------
18 struct named_color {
19 	const char  *name;
20 	rgb_color   color;
21 };
22 
23 struct named_gradient {
24 	const char  *name;
25 	rgb_color   color;
26 	bool        started;
27 };
28 
29 enum {
30 	STROKE_FLAG			= 0x01,
31 	FILL_FLAG			= 0x02,
32 	STROKE_WIDTH_FLAG	= 0x04,
33 	LINE_MODE_FLAG		= 0x08,
34 	FONT_SIZE_FLAG		= 0x10,
35 	MATRIX_FLAG			= 0x20,
36 
37 };
38 
39 struct _state_ {
40 
_state__state_41 	_state_() { set_default_values(); }
_state__state_42 	_state_(_state_ &state) { *this = state; }
set_default_values_state_43 	void set_default_values()
44 	{
45 		fFlags = 0;
46 		fCurrentColor.red = 0; fCurrentColor.green = 0;
47 		fCurrentColor.blue = 0; fCurrentColor.alpha = 255;
48 		fStrokeColor.red = 0; fStrokeColor.green = 0;
49 		fStrokeColor.blue = 0; fStrokeColor.alpha = 255;
50 		fStroke = false;
51 		fFillColor.red = 0; fFillColor.green = 0;
52 		fFillColor.blue = 0; fFillColor.alpha = 255;
53 		fFill = true;
54 		fStrokeWidth = 1.0f;
55 		fLineCap = B_BUTT_CAP;
56 		fLineJoin = B_MITER_JOIN;
57 		fLineMiterLimit = B_DEFAULT_MITER_LIMIT;
58 		fFontSize = 9.0f;
59 	}
60 
61 	uint32		fFlags;
62 	rgb_color	fCurrentColor;
63 	rgb_color	fStrokeColor;
64 	bool		fStroke;
65 	rgb_color	fFillColor;
66 	bool		fFill;
67 	float		fStrokeWidth;
68 	cap_mode	fLineCap;
69 	join_mode	fLineJoin;
70 	float		fLineMiterLimit;
71 	float		fFontSize;
72 	BMatrix		fMatrix;
73 };
74 
75 // Globals ---------------------------------------------------------------------
76 
77 // Svg2PictureView class -------------------------------------------------------
78 class Svg2PictureView : public BView {
79 
80 public:
81 				Svg2PictureView(BRect frame, const char *fileName);
82 				~Svg2PictureView();
83 
84 	virtual void		AttachedToWindow();
85         virtual void    	Draw(BRect updateRect);
86 
87 private:
88         bool    HasAttribute(const XML_Char **attributes, const char *name);
89         float   GetFloatAttribute(const XML_Char **attributes, const char *name);
90         const char  *GetStringAttribute(const XML_Char **attributes, const char *name);
91         rgb_color   GetColorAttribute(const XML_Char **attributes, const char *name, uint8 alpha);
92         void    GetPolygonAttribute(const XML_Char **attributes, const char *name, BShape &shape);
93         void    GetMatrixAttribute(const XML_Char **attributes, const char *name, BMatrix *matrix);
94         void    GetShapeAttribute(const XML_Char **attributes, const char *name, BShape &shape);
95         void    CheckAttributes(const XML_Char **attributes);
96         void    StartElement(const XML_Char *name, const XML_Char **attributes);
97         void    EndElement(const XML_Char *name);
98         void    CharacterDataHandler(const XML_Char *s, int len);
99 
100         void    Push();
101         void    Pop();
102 
103 static  void    _StartElement(Svg2PictureView *view, const XML_Char *name, const XML_Char **attributes);
104 static  void    _EndElement(Svg2PictureView *view, const XML_Char *name);
105 static  void    _CharacterDataHandler(Svg2PictureView *view, const XML_Char *s, int len);
106 
107         _state_ fState;
108         BList   fStack;
109         named_gradient *fGradient;
110         BList   fGradients;
111         BPoint  fTextPosition;
112         BString fText;
113 
114 	BString fFileName;
115 	BPicture *fPicture;
116 };
117 //------------------------------------------------------------------------------
118 
119 #endif // _SVGVIEW_VIEW_H
120