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