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, 136 const BPoint& viewToScreenOffset, 137 float viewScale) const; 138 BRect FillShape(const int32& opCount, 139 const uint32* opList, const int32& ptCount, 140 const BPoint* ptList, 141 const BGradient& gradient, 142 const BPoint& viewToScreenOffset, 143 float viewScale) const; 144 145 // rects 146 BRect StrokeRect(const BRect& r) const; 147 148 // strokes a one pixel wide solid rect, no blending 149 void StrokeRect(const BRect& r, 150 const rgb_color& c) const; 151 152 BRect FillRect(const BRect& r) const; 153 BRect FillRect(const BRect& r, 154 const BGradient& gradient) const; 155 156 // fills a solid rect with color c, no blending 157 void FillRect(const BRect& r, 158 const rgb_color& c) const; 159 160 // fills a rect with a linear gradient, the caller should be 161 // sure that the gradient is indeed vertical. The start point of 162 // the gradient should be above the end point, or this function 163 // will not draw anything. 164 void FillRectVerticalGradient(BRect r, 165 const BGradientLinear& gradient) const; 166 167 // fills a solid rect with color c, no blending, no clipping 168 void FillRectNoClipping(const clipping_rect& r, 169 const rgb_color& c) const; 170 171 // round rects 172 BRect StrokeRoundRect(const BRect& r, float xRadius, 173 float yRadius) const; 174 175 BRect FillRoundRect(const BRect& r, float xRadius, 176 float yRadius) const; 177 BRect FillRoundRect(const BRect& r, float xRadius, 178 float yRadius, 179 const BGradient& gradient) const; 180 181 // ellipses 182 void AlignEllipseRect(BRect* rect, 183 bool filled) const; 184 185 BRect DrawEllipse(BRect r, bool filled) const; 186 BRect FillEllipse(BRect r, 187 const BGradient& gradient) const; 188 189 // arcs 190 BRect StrokeArc(BPoint center, float xRadius, 191 float yRadius, float angle, 192 float span) const; 193 194 BRect FillArc(BPoint center, float xRadius, 195 float yRadius, float angle, 196 float span) const; 197 BRect FillArc(BPoint center, float xRadius, 198 float yRadius, float angle, float span, 199 const BGradient& gradient) const; 200 201 // strings 202 BRect DrawString(const char* utf8String, 203 uint32 length, BPoint baseLine, 204 const escapement_delta* delta, 205 FontCacheReference* cacheReference = NULL); 206 BRect DrawString(const char* utf8String, 207 uint32 length, const BPoint* offsets, 208 FontCacheReference* cacheReference = NULL); 209 210 BRect BoundingBox(const char* utf8String, 211 uint32 length, BPoint baseLine, 212 BPoint* penLocation, 213 const escapement_delta* delta, 214 FontCacheReference* cacheReference 215 = NULL) const; 216 BRect BoundingBox(const char* utf8String, 217 uint32 length, const BPoint* offsets, 218 BPoint* penLocation, 219 FontCacheReference* cacheReference 220 = NULL) const; 221 222 float StringWidth(const char* utf8String, 223 uint32 length, 224 const escapement_delta* delta = NULL); 225 226 227 // bitmaps 228 BRect DrawBitmap(const ServerBitmap* bitmap, 229 BRect bitmapRect, BRect viewRect, 230 uint32 options) const; 231 232 // some convenience stuff 233 BRect FillRegion(const BRegion* region) const; 234 BRect FillRegion(const BRegion* region, 235 const BGradient& gradient) const; 236 237 BRect InvertRect(const BRect& r) const; 238 239 inline BRect ClipRect(BRect rect) const; 240 inline BRect AlignAndClipRect(BRect rect) const; 241 242 243 private: 244 void _Transform(BPoint* point, 245 bool centerOffset = true) const; 246 BPoint _Transform(const BPoint& point, 247 bool centerOffset = true) const; 248 BRect _Clipped(const BRect& rect) const; 249 250 void _UpdateFont() const; 251 void _UpdateLineWidth(); 252 void _UpdateDrawingMode(bool drawingText = false); 253 void _SetRendererColor(const rgb_color& color) const; 254 255 // drawing functions stroke/fill 256 BRect _DrawTriangle(BPoint pt1, BPoint pt2, 257 BPoint pt3, bool fill) const; 258 259 template<typename sourcePixel> 260 void _TransparentMagicToAlpha(sourcePixel *buffer, 261 uint32 width, uint32 height, 262 uint32 sourceBytesPerRow, 263 sourcePixel transparentMagic, 264 BBitmap *output) const; 265 266 void _DrawBitmap(agg::rendering_buffer& srcBuffer, 267 color_space format, 268 BRect actualBitmapRect, 269 BRect bitmapRect, BRect viewRect, 270 uint32 bitmapFlags) const; 271 template <class F> 272 void _DrawBitmapNoScale32( F copyRowFunction, 273 uint32 bytesPerSourcePixel, 274 agg::rendering_buffer& srcBuffer, 275 int32 xOffset, int32 yOffset, 276 BRect viewRect) const; 277 void _DrawBitmapNearestNeighborCopy32( 278 agg::rendering_buffer& srcBuffer, 279 double xOffset, double yOffset, 280 double xScale, double yScale, 281 BRect viewRect) const; 282 void _DrawBitmapBilinearCopy32( 283 agg::rendering_buffer& srcBuffer, 284 double xOffset, double yOffset, 285 double xScale, double yScale, 286 BRect viewRect) const; 287 void _DrawBitmapGeneric32( 288 agg::rendering_buffer& srcBuffer, 289 double xOffset, double yOffset, 290 double xScale, double yScale, 291 BRect viewRect, 292 uint32 bitmapFlags) const; 293 294 void _InvertRect32(BRect r) const; 295 void _BlendRect32(const BRect& r, 296 const rgb_color& c) const; 297 298 299 template<class VertexSource> 300 BRect _BoundingBox(VertexSource& path) const; 301 302 template<class VertexSource> 303 BRect _StrokePath(VertexSource& path) const; 304 template<class VertexSource> 305 BRect _FillPath(VertexSource& path) const; 306 void _CalcLinearGradientTransform(BPoint startPoint, 307 BPoint endPoint, agg::trans_affine& mtx, 308 float gradient_d2 = 100.0f) const; 309 310 void _MakeGradient(const BGradient& gradient, 311 int32 colorCount, uint32* colors, 312 int32 arrayOffset, int32 arraySize) const; 313 314 template<class Array> 315 void _MakeGradient(Array& array, 316 const BGradient& gradient) const; 317 template<class VertexSource> 318 BRect _FillPath(VertexSource& path, 319 const BGradient& gradient) const; 320 template<class VertexSource> 321 void _FillPathGradientLinear(VertexSource& path, 322 const BGradientLinear& linear) const; 323 template<class VertexSource> 324 void _FillPathGradientRadial(VertexSource& path, 325 const BGradientRadial& radial) const; 326 template<class VertexSource> 327 void _FillPathGradientRadialFocus(VertexSource& path, 328 const BGradientRadialFocus& focus) const; 329 template<class VertexSource> 330 void _FillPathGradientDiamond(VertexSource& path, 331 const BGradientDiamond& diamond) const; 332 template<class VertexSource> 333 void _FillPathGradientConic(VertexSource& path, 334 const BGradientConic& conic) const; 335 336 mutable agg::rendering_buffer fBuffer; 337 338 // AGG rendering and rasterization classes 339 pixfmt fPixelFormat; 340 mutable renderer_base fBaseRenderer; 341 342 mutable scanline_unpacked_type fUnpackedScanline; 343 mutable scanline_packed_type fPackedScanline; 344 mutable scanline_packed_subpix_type fSubpixPackedScanline; 345 mutable scanline_unpacked_subpix_type fSubpixUnpackedScanline; 346 mutable rasterizer_subpix_type fSubpixRasterizer; 347 mutable rasterizer_type fRasterizer; 348 mutable renderer_subpix_type fSubpixRenderer; 349 mutable renderer_type fRenderer; 350 mutable renderer_bin_type fRendererBin; 351 352 mutable agg::path_storage fPath; 353 mutable agg::conv_curve<agg::path_storage> fCurve; 354 355 // for internal coordinate rounding/transformation 356 bool fSubpixelPrecise : 1; 357 bool fValidClipping : 1; 358 bool fDrawingText : 1; 359 bool fAttached : 1; 360 361 float fPenSize; 362 const BRegion* fClippingRegion; 363 drawing_mode fDrawingMode; 364 source_alpha fAlphaSrcMode; 365 alpha_function fAlphaFncMode; 366 cap_mode fLineCapMode; 367 join_mode fLineJoinMode; 368 float fMiterLimit; 369 370 PatternHandler fPatternHandler; 371 372 // a class handling rendering and caching of glyphs 373 // it is setup to load from a specific Freetype supported 374 // font file which it gets from ServerFont 375 mutable AGGTextRenderer fTextRenderer; 376 }; 377 378 379 inline BRect 380 Painter::ClipRect(BRect rect) const 381 { 382 rect.left = floorf(rect.left); 383 rect.top = floorf(rect.top); 384 rect.right = ceilf(rect.right); 385 rect.bottom = ceilf(rect.bottom); 386 return _Clipped(rect); 387 } 388 389 390 inline BRect 391 Painter::AlignAndClipRect(BRect rect) const 392 { 393 rect.left = floorf(rect.left); 394 rect.top = floorf(rect.top); 395 if (fSubpixelPrecise) { 396 rect.right = ceilf(rect.right); 397 rect.bottom = ceilf(rect.bottom); 398 } else { 399 rect.right = floorf(rect.right); 400 rect.bottom = floorf(rect.bottom); 401 } 402 return _Clipped(rect); 403 } 404 405 406 #endif // PAINTER_H 407 408 409