xref: /haiku/src/servers/app/drawing/Painter/Painter.h (revision 323b65468e5836bb27a5e373b14027d902349437)
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 #ifndef PAINTER_H
11 #define PAINTER_H
12 
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 
28 class BBitmap;
29 class BRegion;
30 class BGradient;
31 class BGradientLinear;
32 class BGradientRadial;
33 class BGradientRadialFocus;
34 class BGradientDiamond;
35 class BGradientConic;
36 class DrawState;
37 class FontCacheReference;
38 class RenderingBuffer;
39 class ServerBitmap;
40 class ServerFont;
41 class Transformable;
42 
43 
44 class Painter {
45 public:
46 								Painter();
47 	virtual						~Painter();
48 
49 								// frame buffer stuff
50 			void				AttachToBuffer(RenderingBuffer* buffer);
51 			void				DetachFromBuffer();
52 			BRect				Bounds() const;
53 
54 			void				ConstrainClipping(const BRegion* region);
55 			const BRegion*		ClippingRegion() const
56 									{ return fClippingRegion; }
57 
58 			void				SetDrawState(const DrawState* data,
59 									int32 xOffset = 0,
60 									int32 yOffset = 0);
61 
62 								// object settings
63 			void				SetHighColor(const rgb_color& color);
64 	inline	rgb_color			HighColor() const
65 									{ return fPatternHandler.HighColor(); }
66 
67 			void				SetLowColor(const rgb_color& color);
68 	inline	rgb_color			LowColor() const
69 									{ return fPatternHandler.LowColor(); }
70 
71 			void				SetPenSize(float size);
72 	inline	float				PenSize() const
73 									{ return fPenSize; }
74 			void				SetStrokeMode(cap_mode lineCap,
75 									join_mode joinMode, float miterLimit);
76 			void				SetPattern(const pattern& p,
77 									bool drawingText = false);
78 	inline	pattern				Pattern() const
79 									{ return *fPatternHandler.GetR5Pattern(); }
80 			void				SetDrawingMode(drawing_mode mode);
81 	inline	drawing_mode		DrawingMode() const
82 									{ return fDrawingMode; }
83 			void				SetBlendingMode(source_alpha srcAlpha,
84 									alpha_function alphaFunc);
85 
86 			void				SetFont(const ServerFont& font);
87 			void				SetFont(const DrawState* state);
88 	inline	const ServerFont&	Font() const
89 									{ return fTextRenderer.Font(); }
90 
91 								// painting functions
92 
93 								// lines
94 			void				StrokeLine(BPoint a, BPoint b);
95 
96 			// returns true if the line was either vertical or horizontal
97 			// draws a solid one pixel wide line of color c, no blending
98 			bool				StraightLine(BPoint a, BPoint b,
99 									const rgb_color& c) const;
100 
101 								// triangles
102 			BRect				StrokeTriangle(BPoint pt1, BPoint pt2,
103 									BPoint pt3) const;
104 
105 			BRect				FillTriangle(BPoint pt1, BPoint pt2,
106 									BPoint pt3) const;
107 			BRect				FillTriangle(BPoint pt1, BPoint pt2,
108 									BPoint pt3,
109 									const BGradient& gradient) const;
110 
111 								// polygons
112 			BRect				DrawPolygon(BPoint* ptArray, int32 numPts,
113 									bool filled, bool closed) const;
114 			BRect				FillPolygon(BPoint* ptArray, int32 numPts,
115 									const BGradient& gradient,
116 									bool closed) const;
117 
118 								// bezier curves
119 			BRect				DrawBezier(BPoint* controlPoints,
120 									bool filled) const;
121 			BRect				FillBezier(BPoint* controlPoints,
122 									const BGradient& gradient) const;
123 
124 								// shapes
125 			BRect				DrawShape(const int32& opCount,
126 									const uint32* opList, const int32& ptCount,
127 									const BPoint* ptList, bool filled,
128 									const BPoint& viewToScreenOffset,
129 									float viewScale) const;
130 			BRect				FillShape(const int32& opCount,
131 									const uint32* opList, const int32& ptCount,
132 									const BPoint* ptList,
133 									const BGradient& gradient,
134 									const BPoint& viewToScreenOffset,
135 									float viewScale) const;
136 
137 								// rects
138 			BRect				StrokeRect(const BRect& r) const;
139 
140 			// strokes a one pixel wide solid rect, no blending
141 			void				StrokeRect(const BRect& r,
142 									const rgb_color& c) const;
143 
144 			BRect				FillRect(const BRect& r) const;
145 			BRect				FillRect(const BRect& r,
146 									const BGradient& gradient) const;
147 
148 			// fills a solid rect with color c, no blending
149 			void				FillRect(const BRect& r,
150 									const rgb_color& c) const;
151 
152 			// fills a rect with a linear gradient, the caller should be
153 			// sure that the gradient is indeed vertical. The start point of
154 			// the gradient should be above the end point, or this function
155 			// will not draw anything.
156 			void				FillRectVerticalGradient(BRect r,
157 									const BGradientLinear& gradient) const;
158 
159 			// fills a solid rect with color c, no blending, no clipping
160 			void				FillRectNoClipping(const clipping_rect& r,
161 									const rgb_color& c) const;
162 
163 								// round rects
164 			BRect				StrokeRoundRect(const BRect& r, float xRadius,
165 									float yRadius) const;
166 
167 			BRect				FillRoundRect(const BRect& r, float xRadius,
168 									float yRadius) const;
169 			BRect				FillRoundRect(const BRect& r, float xRadius,
170 									float yRadius,
171 									const BGradient& gradient) const;
172 
173 								// ellipses
174 			void				AlignEllipseRect(BRect* rect,
175 									bool filled) const;
176 
177 			BRect				DrawEllipse(BRect r, bool filled) const;
178 			BRect				FillEllipse(BRect r,
179 									const BGradient& gradient) const;
180 
181 								// arcs
182 			BRect				StrokeArc(BPoint center, float xRadius,
183 									float yRadius, float angle,
184 									float span) const;
185 
186 			BRect				FillArc(BPoint center, float xRadius,
187 									float yRadius, float angle,
188 									float span) const;
189 			BRect				FillArc(BPoint center, float xRadius,
190 									float yRadius, float angle, float span,
191 									const BGradient& gradient) const;
192 
193 								// strings
194 			BRect				DrawString(const char* utf8String,
195 									uint32 length, BPoint baseLine,
196 									const escapement_delta* delta,
197 									FontCacheReference* cacheReference = NULL);
198 			BRect				DrawString(const char* utf8String,
199 									uint32 length, const BPoint* offsets,
200 									FontCacheReference* cacheReference = NULL);
201 
202 			BRect				BoundingBox(const char* utf8String,
203 									uint32 length, BPoint baseLine,
204 									BPoint* penLocation,
205 									const escapement_delta* delta,
206 									FontCacheReference* cacheReference
207 										= NULL) const;
208 			BRect				BoundingBox(const char* utf8String,
209 									uint32 length, const BPoint* offsets,
210 									BPoint* penLocation,
211 									FontCacheReference* cacheReference
212 										= NULL) const;
213 
214 			float				StringWidth(const char* utf8String,
215 									uint32 length,
216 									const escapement_delta* delta = NULL);
217 
218 
219 								// bitmaps
220 			BRect				DrawBitmap(const ServerBitmap* bitmap,
221 									BRect bitmapRect, BRect viewRect,
222 									uint32 options) const;
223 
224 								// some convenience stuff
225 			BRect				FillRegion(const BRegion* region) const;
226 			BRect				FillRegion(const BRegion* region,
227 									const BGradient& gradient) const;
228 
229 			BRect				InvertRect(const BRect& r) const;
230 
231 	inline	BRect				ClipRect(BRect rect) const;
232 	inline	BRect				AlignAndClipRect(BRect rect) const;
233 
234 
235 private:
236 			void				_Transform(BPoint* point,
237 									bool centerOffset = true) const;
238 			BPoint				_Transform(const BPoint& point,
239 									bool centerOffset = true) const;
240 			BRect				_Clipped(const BRect& rect) const;
241 
242 			void				_UpdateFont() const;
243 			void				_UpdateLineWidth();
244 			void				_UpdateDrawingMode(bool drawingText = false);
245 			void				_SetRendererColor(const rgb_color& color) const;
246 
247 								// drawing functions stroke/fill
248 			BRect				_DrawTriangle(BPoint pt1, BPoint pt2,
249 									BPoint pt3, bool fill) const;
250 
251 			template<typename sourcePixel>
252 			void				_TransparentMagicToAlpha(sourcePixel *buffer,
253 									uint32 width, uint32 height,
254 									uint32 sourceBytesPerRow,
255 									sourcePixel transparentMagic,
256 									BBitmap *output) const;
257 
258 			void				_DrawBitmap(agg::rendering_buffer& srcBuffer,
259 									color_space format,
260 									BRect actualBitmapRect,
261 									BRect bitmapRect, BRect viewRect,
262 									uint32 bitmapFlags) const;
263 			template <class F>
264 			void				_DrawBitmapNoScale32( F copyRowFunction,
265 									uint32 bytesPerSourcePixel,
266 									agg::rendering_buffer& srcBuffer,
267 									int32 xOffset, int32 yOffset,
268 									BRect viewRect) const;
269 			void				_DrawBitmapNearestNeighborCopy32(
270 									agg::rendering_buffer& srcBuffer,
271 									double xOffset, double yOffset,
272 									double xScale, double yScale,
273 									BRect viewRect) const;
274 			void				_DrawBitmapBilinearCopy32(
275 									agg::rendering_buffer& srcBuffer,
276 									double xOffset, double yOffset,
277 									double xScale, double yScale,
278 									BRect viewRect) const;
279 			void				_DrawBitmapGeneric32(
280 									agg::rendering_buffer& srcBuffer,
281 									double xOffset, double yOffset,
282 									double xScale, double yScale,
283 									BRect viewRect,
284 									uint32 bitmapFlags) const;
285 
286 			void				_InvertRect32(BRect r) const;
287 			void				_BlendRect32(const BRect& r,
288 									const rgb_color& c) const;
289 
290 
291 			template<class VertexSource>
292 			BRect				_BoundingBox(VertexSource& path) const;
293 
294 			template<class VertexSource>
295 			BRect				_StrokePath(VertexSource& path) const;
296 			template<class VertexSource>
297 			BRect				_FillPath(VertexSource& path) const;
298 			void				_CalcLinearGradientTransform(BPoint startPoint,
299 									BPoint endPoint, agg::trans_affine& mtx,
300 									float gradient_d2 = 100.0f) const;
301 
302 			void				_MakeGradient(const BGradient& gradient,
303 									int32 colorCount, uint32* colors,
304 									int32 arrayOffset, int32 arraySize) const;
305 
306 			template<class Array>
307 			void				_MakeGradient(Array& array,
308 									const BGradient& gradient) const;
309 			template<class VertexSource>
310 			BRect				_FillPath(VertexSource& path,
311 									const BGradient& gradient) const;
312 			template<class VertexSource>
313 			void				_FillPathGradientLinear(VertexSource& path,
314 									const BGradientLinear& linear) const;
315 			template<class VertexSource>
316 			void				_FillPathGradientRadial(VertexSource& path,
317 									const BGradientRadial& radial) const;
318 			template<class VertexSource>
319 			void				_FillPathGradientRadialFocus(VertexSource& path,
320 									const BGradientRadialFocus& focus) const;
321 			template<class VertexSource>
322 			void				_FillPathGradientDiamond(VertexSource& path,
323 									const BGradientDiamond& diamond) const;
324 			template<class VertexSource>
325 			void				_FillPathGradientConic(VertexSource& path,
326 									const BGradientConic& conic) const;
327 
328 	mutable	agg::rendering_buffer fBuffer;
329 
330 	// AGG rendering and rasterization classes
331 			pixfmt				fPixelFormat;
332 	mutable	renderer_base		fBaseRenderer;
333 
334 	mutable	scanline_unpacked_type fUnpackedScanline;
335 	mutable	scanline_packed_type fPackedScanline;
336 	mutable	scanline_packed_subpix_type fSubpixPackedScanline;
337 	mutable	scanline_unpacked_subpix_type fSubpixUnpackedScanline;
338 	mutable	rasterizer_subpix_type fSubpixRasterizer;
339 	mutable	rasterizer_type		fRasterizer;
340 	mutable	renderer_subpix_type fSubpixRenderer;
341 	mutable	renderer_type		fRenderer;
342 	mutable	renderer_bin_type	fRendererBin;
343 
344 	mutable	agg::path_storage	fPath;
345 	mutable	agg::conv_curve<agg::path_storage> fCurve;
346 
347 	// for internal coordinate rounding/transformation
348 			bool				fSubpixelPrecise : 1;
349 			bool				fValidClipping : 1;
350 			bool				fDrawingText : 1;
351 			bool				fAttached : 1;
352 
353 			float				fPenSize;
354 			const BRegion*		fClippingRegion;
355 			drawing_mode		fDrawingMode;
356 			source_alpha		fAlphaSrcMode;
357 			alpha_function		fAlphaFncMode;
358 			cap_mode			fLineCapMode;
359 			join_mode			fLineJoinMode;
360 			float				fMiterLimit;
361 
362 			PatternHandler		fPatternHandler;
363 
364 	// a class handling rendering and caching of glyphs
365 	// it is setup to load from a specific Freetype supported
366 	// font file which it gets from ServerFont
367 	mutable	AGGTextRenderer		fTextRenderer;
368 };
369 
370 
371 inline BRect
372 Painter::ClipRect(BRect rect) const
373 {
374 	rect.left = floorf(rect.left);
375 	rect.top = floorf(rect.top);
376 	rect.right = ceilf(rect.right);
377 	rect.bottom = ceilf(rect.bottom);
378 	return _Clipped(rect);
379 }
380 
381 
382 inline BRect
383 Painter::AlignAndClipRect(BRect rect) const
384 {
385 	rect.left = floorf(rect.left);
386 	rect.top = floorf(rect.top);
387 	if (fSubpixelPrecise) {
388 		rect.right = ceilf(rect.right);
389 		rect.bottom = ceilf(rect.bottom);
390 	} else {
391 		rect.right = floorf(rect.right);
392 		rect.bottom = floorf(rect.bottom);
393 	}
394 	return _Clipped(rect);
395 }
396 
397 
398 #endif // PAINTER_H
399