1// Painter.h
2
3#ifndef PAINTER_H
4#define PAINTER_H
5
6#include <Font.h>
7#include <Rect.h>
8#include <FontServer.h>
9#include <ServerFont.h>
10
11#include "defines.h"
12#include "forwarding_pixfmt.h"
13
14#include "RGBColor.h"
15
16class AGGTextRenderer;
17class BBitmap;
18class BRegion;
19class DrawData;
20class PatternHandler;
21class RenderingBuffer;
22class ServerBitmap;
23class ServerFont;
24
25class Painter {
26 public:
27								Painter();
28	virtual						~Painter();
29
30								// frame buffer stuff
31			void				AttachToBuffer(RenderingBuffer* buffer);
32			void				DetachFromBuffer();
33
34			void				ConstrainClipping(const BRegion& region);
35			void				SetDrawData(const DrawData* data);
36
37								// object settings
38			void				SetHighColor(const rgb_color& color);
39	inline	void				SetHighColor(uint8 r, uint8 g, uint8 b, uint8 a = 255);
40	inline	void				SetHighColor(const RGBColor& color)
41									{ SetHighColor(color.GetColor32()); }
42			void				SetLowColor(const rgb_color& color);
43	inline	void				SetLowColor(uint8 r, uint8 g, uint8 b, uint8 a = 255);
44	inline	void				SetLowColor(const RGBColor& color)
45									{ SetLowColor(color.GetColor32()); }
46
47			void				SetScale(float scale);
48			void				SetPenSize(float size);
49			void				SetOrigin(const BPoint& origin);
50			void				SetDrawingMode(drawing_mode mode);
51			void				SetBlendingMode(source_alpha alphaSrcMode,
52												alpha_function alphaFncMode);
53			void				SetPenLocation(const BPoint& location);
54			void				SetFont(const BFont& font);
55			void				SetFont(const ServerFont& font);
56
57								// BView API compatibility (for easier testing)
58			void				Sync() {}
59	inline	void				MovePenTo(const BPoint& location)
60									{ SetPenLocation(location); }
61	inline	void				SetFont(const BFont* font)
62									{ if (font) SetFont(*font); }
63
64								// painting functions
65
66								// lines
67			BRect				StrokeLine(		BPoint a,
68												BPoint b,
69												const pattern& p = B_SOLID_HIGH);
70
71			BRect				StrokeLine(		BPoint b,
72												const pattern& p = B_SOLID_HIGH);
73
74			// return true if the line was either vertical or horizontal
75			// draws a solid one pixel wide line of color c, no blending
76			bool				StraightLine(	BPoint a,
77												BPoint b,
78												const rgb_color& c) const;
79
80								// triangles
81			void				StrokeTriangle(	BPoint pt1,
82												BPoint pt2,
83												BPoint pt3,
84												const pattern& p = B_SOLID_HIGH) const;
85
86			void				FillTriangle(	BPoint pt1,
87												BPoint pt2,
88												BPoint pt3,
89												const pattern& p = B_SOLID_HIGH) const;
90
91								// polygons
92			void				StrokePolygon(	const BPoint* ptArray,
93												int32 numPts,
94											    bool  closed = true,
95												const pattern& p = B_SOLID_HIGH) const;
96
97			void				FillPolygon(	const BPoint* ptArray,
98												int32 numPts,
99											    bool  closed = true,
100												const pattern& p = B_SOLID_HIGH) const;
101
102								// bezier curves
103			void				StrokeBezier(	const BPoint* controlPoints,
104												const pattern& p = B_SOLID_HIGH) const;
105
106			void				FillBezier(		const BPoint* controlPoints,
107												const pattern& p = B_SOLID_HIGH) const;
108
109								// shapes
110			void				StrokeShape(	/*const */BShape* shape,
111												const pattern& p = B_SOLID_HIGH) const;
112
113			void				FillShape(		/*const */BShape* shape,
114												const pattern& p = B_SOLID_HIGH) const;
115
116
117								// rects
118			BRect				StrokeRect(		const BRect& r,
119												const pattern& p = B_SOLID_HIGH) const;
120
121			// strokes a one pixel wide solid rect, no blending
122			void				StrokeRect(		const BRect& r,
123												const rgb_color& c) const;
124
125			BRect				FillRect(		const BRect& r,
126												const pattern& p = B_SOLID_HIGH) const;
127
128			// fills a solid rect with color c, no blending
129			void				FillRect(		const BRect& r,
130												const rgb_color& c) const;
131
132								// round rects
133			void				StrokeRoundRect(const BRect& r,
134												float xRadius,
135												float yRadius,
136												const pattern& p = B_SOLID_HIGH) const;
137
138			void				FillRoundRect(	const BRect& r,
139												float xRadius,
140												float yRadius,
141												const pattern& p = B_SOLID_HIGH) const;
142
143								// ellipses
144			void				StrokeEllipse(	BPoint center,
145												float xRadius,
146												float yRadius,
147												const pattern& p = B_SOLID_HIGH) const;
148
149			void				FillEllipse(	BPoint center,
150												float xRadius,
151												float yRadius,
152												const pattern& p = B_SOLID_HIGH) const;
153
154								// arcs
155			void				StrokeArc(		BPoint center,
156												float xRadius,
157												float yRadius,
158												float angle,
159												float span,
160												const pattern& p = B_SOLID_HIGH) const;
161
162			void				FillArc(		BPoint center,
163												float xRadius,
164												float yRadius,
165												float angle,
166												float span,
167												const pattern& p = B_SOLID_HIGH) const;
168
169								// strings
170			BRect				DrawChar(		char aChar);
171
172			BRect				DrawChar(		char aChar,
173												BPoint baseLine);
174
175			BRect				DrawString(		const char* utf8String,
176												uint32 length,
177												const escapement_delta* delta = NULL);
178
179			BRect				DrawString(		const char* utf8String,
180												uint32 length,
181												BPoint baseLine,
182												const escapement_delta* delta = NULL);
183
184			BRect				DrawString(		const char* utf8String,
185												const escapement_delta* delta = NULL);
186
187			BRect				DrawString(		const char* utf8String,
188												BPoint baseLine,
189												const escapement_delta* delta = NULL);
190
191								// bitmaps
192			void				DrawBitmap(		const BBitmap* bitmap,
193												BRect bitmapRect,
194												BRect viewRect) const;
195
196			void				DrawBitmap(		const ServerBitmap* bitmap,
197												BRect bitmapRect,
198												BRect viewRect) const;
199
200								// some convenience stuff
201			void				FillRegion(		const BRegion* region,
202												const pattern& p = B_SOLID_HIGH) const;
203
204			void				InvertRect(		const BRect& r) const;
205
206			BRect				BoundingBox(	const char* utf8String,
207												uint32 length,
208												const BPoint& baseLine) const;
209
210	inline	BRect				ClipRect(const BRect& rect) const
211									{ return _Clipped(rect); }
212
213 private:
214			void				_MakeEmpty();
215
216			void				_Transform(BPoint* point,
217										   bool centerOffset = true) const;
218			BPoint				_Transform(const BPoint& point,
219										   bool centerOffset = true) const;
220			void				_Transform(float* width) const;
221			float				_Transform(const float& width) const;
222			void				_Transform(BRect* rect) const;
223			BRect				_Transform(const BRect& rect) const;
224			BRect				_Clipped(const BRect& rect) const;
225
226			void				_RebuildClipping();
227
228			void				_UpdateFont();
229			void				_UpdateLineWidth();
230
231								// drawing functions stroke/fill
232			void				_DrawTriangle(	BPoint pt1,
233												BPoint pt2,
234												BPoint pt3,
235												const pattern& p,
236												bool fill) const;
237			void				_DrawEllipse(	BPoint center,
238												float xRadius,
239												float yRadius,
240												const pattern& p,
241												bool fill) const;
242			void				_DrawShape(		/*const */BShape* shape,
243												const pattern& p,
244												bool fill) const;
245			void				_DrawPolygon(	const BPoint* ptArray,
246												int32 numPts,
247											    bool  closed,
248												const pattern& p,
249												bool fill) const;
250
251			void				_DrawBitmap(	const agg::rendering_buffer& srcBuffer,
252												color_space format,
253												BRect actualBitmapRect,
254												BRect bitmapRect,
255												BRect viewRect) const;
256			void				_DrawBitmap32(	const agg::rendering_buffer& srcBuffer,
257												BRect actualBitmapRect,
258												BRect bitmapRect,
259												BRect viewRect) const;
260
261			void				_InvertRect32(BRect r) const;
262
263
264			template<class VertexSource>
265			BRect				_BoundingBox(VertexSource& path) const;
266
267			template<class VertexSource>
268			BRect				_StrokePath(VertexSource& path,
269											const pattern& p) const;
270			template<class VertexSource>
271			BRect				_FillPath(VertexSource& path,
272
273										  const pattern& p) const;
274
275			void				_SetPattern(const pattern& p) const;
276			void				_SetRendererColor(const rgb_color& color) const;
277
278	agg::rendering_buffer*		fBuffer;
279
280	// AGG rendering and rasterization classes
281	pixfmt*						fPixelFormat;
282	renderer_base*				fBaseRenderer;
283
284	outline_renderer_type*		fOutlineRenderer;
285	outline_rasterizer_type*	fOutlineRasterizer;
286
287	scanline_type*				fScanline;
288	rasterizer_type*			fRasterizer;
289	renderer_type*				fRenderer;
290
291	font_renderer_solid_type*	fFontRendererSolid;
292	font_renderer_bin_type*		fFontRendererBin;
293
294	agg::line_profile_aa		fLineProfile;
295
296	// for internal coordinate rounding/transformation,
297	// does not concern rendering
298	bool						fSubpixelPrecise;
299
300	float						fScale;
301	float						fPenSize;
302	BPoint						fOrigin;
303	BRegion*					fClippingRegion; // NULL indicates no clipping at all
304	drawing_mode				fDrawingMode;
305	source_alpha				fAlphaSrcMode;
306	alpha_function				fAlphaFncMode;
307	BPoint						fPenLocation;
308	PatternHandler*				fPatternHandler;
309
310	ServerFont					fFont;
311	// a class handling rendering and caching of glyphs
312	// it is setup to load from a specific Freetype supported
313	// font file, it uses the FontManager to locate a file
314	// by Family and Style
315	AGGTextRenderer*			fTextRenderer;
316	uint32						fLastFamilyAndStyle;
317};
318
319// SetHighColor
320inline void
321Painter::SetHighColor(uint8 r, uint8 g, uint8 b, uint8 a)
322{
323	rgb_color color;
324	color.red = r;
325	color.green = g;
326	color.blue = b;
327	color.alpha = a;
328	SetHighColor(color);
329}
330
331// SetLowColor
332inline void
333Painter::SetLowColor(uint8 r, uint8 g, uint8 b, uint8 a)
334{
335	rgb_color color;
336	color.red = r;
337	color.green = g;
338	color.blue = b;
339	color.alpha = a;
340	SetLowColor(color);
341}
342
343
344#endif // PAINTER_H
345
346
347