xref: /haiku/src/servers/app/drawing/Painter/Painter.h (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
1 /*
2  * Copyright 2005-2007, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2008, Andrej Spielmann <andrej.spielmann@seh.ox.ac.uk>.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  *
6  * API to the Anti-Grain Geometry based "Painter" drawing backend. Manages
7  * rendering pipe-lines for stroke, fills, bitmap and text rendering.
8  *
9  */
10 
11 #ifndef PAINTER_H
12 #define PAINTER_H
13 
14 #include "AGGTextRenderer.h"
15 #include "FontManager.h"
16 #include "PatternHandler.h"
17 #include "ServerFont.h"
18 
19 #include "defines.h"
20 
21 #include <agg_conv_curve.h>
22 #include <agg_path_storage.h>
23 
24 #include <Font.h>
25 #include <Rect.h>
26 
27 class BBitmap;
28 class BRegion;
29 class DrawState;
30 class FontCacheReference;
31 class RenderingBuffer;
32 class ServerBitmap;
33 class ServerFont;
34 class Transformable;
35 
36 
37 class Painter {
38  public:
39 								Painter();
40 	virtual						~Painter();
41 
42 								// frame buffer stuff
43 			void				AttachToBuffer(RenderingBuffer* buffer);
44 			void				DetachFromBuffer();
45 			BRect				Bounds() const;
46 
47 			void				ConstrainClipping(const BRegion* region);
48 			const BRegion*		ClippingRegion() const
49 									{ return fClippingRegion; }
50 
51 			void				SetDrawState(const DrawState* data,
52 											 int32 xOffset = 0,
53 											 int32 yOffset = 0);
54 
55 								// object settings
56 			void				SetHighColor(const rgb_color& color);
57 	inline	rgb_color			HighColor() const
58 									{ return fPatternHandler.HighColor(); }
59 
60 			void				SetLowColor(const rgb_color& color);
61 	inline	rgb_color			LowColor() const
62 									{ return fPatternHandler.LowColor(); }
63 
64 			void				SetPenSize(float size);
65 	inline	float				PenSize() const
66 									{ return fPenSize; }
67 			void				SetStrokeMode(cap_mode lineCap,
68 									join_mode joinMode, float miterLimit);
69 			void				SetPattern(const pattern& p,
70 										   bool drawingText = false);
71 	inline	pattern				Pattern() const
72 									{ return *fPatternHandler.GetR5Pattern(); }
73 			void				SetDrawingMode(drawing_mode mode);
74 	inline	drawing_mode		DrawingMode() const
75 									{ return fDrawingMode; }
76 			void				SetBlendingMode(source_alpha srcAlpha,
77 									alpha_function alphaFunc);
78 
79 			void				SetFont(const ServerFont& font);
80 			void				SetFont(const DrawState* state);
81 	inline	const ServerFont&	Font() const
82 									{ return fTextRenderer.Font(); }
83 
84 								// painting functions
85 
86 								// lines
87 			void				StrokeLine(		BPoint a,
88 												BPoint b);
89 
90 			// returns true if the line was either vertical or horizontal
91 			// draws a solid one pixel wide line of color c, no blending
92 			bool				StraightLine(	BPoint a,
93 												BPoint b,
94 												const rgb_color& c) const;
95 
96 								// triangles
97 			BRect				StrokeTriangle(	BPoint pt1,
98 												BPoint pt2,
99 												BPoint pt3) const;
100 
101 			BRect				FillTriangle(	BPoint pt1,
102 												BPoint pt2,
103 												BPoint pt3) const;
104 
105 								// polygons
106 			BRect				DrawPolygon(	BPoint* ptArray,
107 												int32 numPts,
108 											    bool filled,
109 											    bool closed) const;
110 
111 								// bezier curves
112 			BRect				DrawBezier(		BPoint* controlPoints,
113 												bool filled) const;
114 
115 								// shapes
116 			BRect				DrawShape(		const int32& opCount,
117 												const uint32* opList,
118 												const int32& ptCount,
119 												const BPoint* ptList,
120 												bool filled) const;
121 
122 								// rects
123 			BRect				StrokeRect(		const BRect& r) const;
124 
125 			// strokes a one pixel wide solid rect, no blending
126 			void				StrokeRect(		const BRect& r,
127 												const rgb_color& c) const;
128 
129 			BRect				FillRect(		const BRect& r) const;
130 
131 			// fills a solid rect with color c, no blending
132 			void				FillRect(		const BRect& r,
133 												const rgb_color& c) const;
134 			// fills a solid rect with color c, no blending, no clipping
135 			void				FillRectNoClipping(const clipping_rect& r,
136 												const rgb_color& c) const;
137 
138 								// round rects
139 			BRect				StrokeRoundRect(const BRect& r,
140 												float xRadius,
141 												float yRadius) const;
142 
143 			BRect				FillRoundRect(	const BRect& r,
144 												float xRadius,
145 												float yRadius) const;
146 
147 								// ellipses
148 			void				AlignEllipseRect(BRect* rect,
149 												 bool filled) const;
150 
151 			BRect				DrawEllipse(	BRect r,
152 												bool filled) const;
153 
154 								// arcs
155 			BRect				StrokeArc(		BPoint center,
156 												float xRadius,
157 												float yRadius,
158 												float angle,
159 												float span) const;
160 
161 			BRect				FillArc(		BPoint center,
162 												float xRadius,
163 												float yRadius,
164 												float angle,
165 												float span) const;
166 
167 								// strings
168 			BRect				DrawString(		const char* utf8String,
169 												uint32 length,
170 												BPoint baseLine,
171 												const escapement_delta* delta,
172 												FontCacheReference* cacheReference = NULL);
173 
174 			BRect				BoundingBox(	const char* utf8String,
175 												uint32 length,
176 												BPoint baseLine,
177 												BPoint* penLocation,
178 												const escapement_delta* delta,
179 												FontCacheReference* cacheReference = NULL) const;
180 
181 			float				StringWidth(	const char* utf8String,
182 												uint32 length,
183 												const escapement_delta* delta = NULL);
184 
185 
186 								// bitmaps
187 			BRect				DrawBitmap(		const ServerBitmap* bitmap,
188 												BRect bitmapRect,
189 												BRect viewRect,
190 												uint32 options) const;
191 
192 								// some convenience stuff
193 			BRect				FillRegion(		const BRegion* region) const;
194 
195 			BRect				InvertRect(		const BRect& r) const;
196 
197 	inline	BRect				ClipRect(		BRect rect) const;
198 	inline	BRect				AlignAndClipRect(BRect rect) const;
199 
200 
201  private:
202 			void				_Transform(BPoint* point,
203 										   bool centerOffset = true) const;
204 			BPoint				_Transform(const BPoint& point,
205 										   bool centerOffset = true) const;
206 			BRect				_Clipped(const BRect& rect) const;
207 
208 			void				_UpdateFont() const;
209 			void				_UpdateLineWidth();
210 			void				_UpdateDrawingMode(bool drawingText = false);
211 			void				_SetRendererColor(const rgb_color& color) const;
212 
213 								// drawing functions stroke/fill
214 			BRect				_DrawTriangle(	BPoint pt1,
215 												BPoint pt2,
216 												BPoint pt3,
217 												bool fill) const;
218 
219 			template<typename sourcePixel>
220 			void				_TransparentMagicToAlpha(sourcePixel *buffer,
221 									uint32 width, uint32 height,
222 									uint32 sourceBytesPerRow,
223 									sourcePixel transparentMagic,
224 									BBitmap *output) const;
225 
226 			void				_DrawBitmap(	agg::rendering_buffer& srcBuffer,
227 												color_space format,
228 												BRect actualBitmapRect,
229 												BRect bitmapRect,
230 												BRect viewRect,
231 												uint32 bitmapFlags) const;
232 			template <class F>
233 			void				_DrawBitmapNoScale32(
234 												F copyRowFunction,
235 												uint32 bytesPerSourcePixel,
236 												agg::rendering_buffer& srcBuffer,
237 												int32 xOffset, int32 yOffset,
238 												BRect viewRect) const;
239 			void				_DrawBitmapNearestNeighborCopy32(
240 												agg::rendering_buffer& srcBuffer,
241 												double xOffset, double yOffset,
242 												double xScale, double yScale,
243 												BRect viewRect) const;
244 			void				_DrawBitmapBilinearCopy32(
245 												agg::rendering_buffer& srcBuffer,
246 												double xOffset, double yOffset,
247 												double xScale, double yScale,
248 												BRect viewRect) const;
249 			void				_DrawBitmapGeneric32(
250 												agg::rendering_buffer& srcBuffer,
251 												double xOffset, double yOffset,
252 												double xScale, double yScale,
253 												BRect viewRect,
254 												uint32 bitmapFlags) const;
255 
256 			void				_InvertRect32(	BRect r) const;
257 			void				_BlendRect32(	const BRect& r,
258 												const rgb_color& c) const;
259 
260 
261 			template<class VertexSource>
262 			BRect				_BoundingBox(VertexSource& path) const;
263 
264 			template<class VertexSource>
265 			BRect				_StrokePath(VertexSource& path) const;
266 			template<class VertexSource>
267 			BRect				_FillPath(VertexSource& path) const;
268 
269 mutable agg::rendering_buffer	fBuffer;
270 
271 	// AGG rendering and rasterization classes
272 	pixfmt						fPixelFormat;
273 mutable renderer_base			fBaseRenderer;
274 
275 mutable scanline_unpacked_type	fUnpackedScanline;
276 mutable scanline_packed_type	fPackedScanline;
277 mutable scanline_packed_subpix_type fSubpixPackedScanline;
278 mutable scanline_unpacked_subpix_type fSubpixUnpackedScanline;
279 mutable rasterizer_subpix_type	fSubpixRasterizer;
280 mutable rasterizer_type			fRasterizer;
281 mutable renderer_subpix_type	fSubpixRenderer;
282 mutable renderer_type			fRenderer;
283 mutable renderer_bin_type		fRendererBin;
284 
285 mutable agg::path_storage		fPath;
286 mutable agg::conv_curve<agg::path_storage> fCurve;
287 
288 	// for internal coordinate rounding/transformation
289 	bool						fSubpixelPrecise : 1;
290 	bool						fValidClipping : 1;
291 	bool						fDrawingText : 1;
292 	bool						fAttached : 1;
293 
294 	float						fPenSize;
295 	const BRegion*				fClippingRegion;
296 	drawing_mode				fDrawingMode;
297 	source_alpha				fAlphaSrcMode;
298 	alpha_function				fAlphaFncMode;
299 	cap_mode					fLineCapMode;
300 	join_mode					fLineJoinMode;
301 	float						fMiterLimit;
302 
303 	PatternHandler				fPatternHandler;
304 
305 	// a class handling rendering and caching of glyphs
306 	// it is setup to load from a specific Freetype supported
307 	// font file which it gets from ServerFont
308 mutable AGGTextRenderer			fTextRenderer;
309 };
310 
311 
312 inline BRect
313 Painter::ClipRect(BRect rect) const
314 {
315 	rect.left = floorf(rect.left);
316 	rect.top = floorf(rect.top);
317 	rect.right = ceilf(rect.right);
318 	rect.bottom = ceilf(rect.bottom);
319 	return _Clipped(rect);
320 }
321 
322 inline BRect
323 Painter::AlignAndClipRect(BRect rect) const
324 {
325 	rect.left = floorf(rect.left);
326 	rect.top = floorf(rect.top);
327 	if (fSubpixelPrecise) {
328 		rect.right = ceilf(rect.right);
329 		rect.bottom = ceilf(rect.bottom);
330 	} else {
331 		rect.right = floorf(rect.right);
332 		rect.bottom = floorf(rect.bottom);
333 	}
334 	return _Clipped(rect);
335 }
336 
337 
338 #endif // PAINTER_H
339 
340 
341