1 #ifndef _Painter_Painter_h_ 2 #define _Painter_Painter_h_ 3 4 #include <Draw/Draw.h> 5 6 #ifdef flagTIMING 7 #define PAINTER_TIMING(x) RTIMING(x) 8 #else 9 #define PAINTER_TIMING(x) // RTIMING(x) 10 #endif 11 12 namespace Upp { 13 14 INITIALIZE(PaintPainting) 15 16 enum XformClass { // classification of Xform (simpler forms can be optimized) 17 XFORM_REGULAR = 32, // same scale in X and Y, does not skew line width 18 19 XFORM_IDENTITY = 0|XFORM_REGULAR, // not transformation 20 XFORM_TRANSLATION = 1|XFORM_REGULAR, 21 XFORM_REGULAR_SCALE = 2|XFORM_REGULAR, // just scale, same in X and Y 22 XFORM_SCALE = 2, // just scale, but X scale != Y scale 23 XFORM_ANY = 0, 24 }; 25 26 struct Xform2D { 27 Pointf x, y, t; 28 TransformXform2D29 Pointf Transform(double px, double py) const { return Pointf(px * x.x + py * x.y + t.x, px * y.x + py * y.y + t.y); } TransformXform2D30 Pointf Transform(const Pointf& f) const { return Transform(f.x, f.y); } 31 32 Pointf GetScaleXY() const; 33 double GetScale() const; 34 bool IsRegular() const; 35 36 byte GetClass() const; 37 38 static Xform2D Identity(); 39 static Xform2D Translation(double x, double y); 40 static Xform2D Scale(double sx, double sy); 41 static Xform2D Scale(double scale); 42 static Xform2D Rotation(double fi); 43 static Xform2D Sheer(double fi); 44 static Xform2D Map(Pointf s1, Pointf s2, Pointf s3); // maps 0,0 -> s3, 1,0 -> s1, 0,1 -> s2 45 static Xform2D Map(Pointf s1, Pointf s2, Pointf s3, Pointf t1, Pointf t2, Pointf t3); 46 47 Xform2D(); 48 }; 49 50 Xform2D operator*(const Xform2D& a, const Xform2D& b); 51 Xform2D Inverse(const Xform2D& m); 52 53 enum PainterOptions { 54 LINECAP_BUTT, 55 LINECAP_SQUARE, 56 LINECAP_ROUND, 57 58 LINEJOIN_MITER, 59 LINEJOIN_ROUND, 60 LINEJOIN_BEVEL, 61 62 FILL_EXACT = 0, 63 64 FILL_HPAD = 1, 65 FILL_HREPEAT = 2, 66 FILL_HREFLECT = 3, 67 68 FILL_VPAD = 4, 69 FILL_VREPEAT = 8, 70 FILL_VREFLECT = 12, 71 72 FILL_PAD = FILL_HPAD|FILL_VPAD, 73 FILL_REPEAT = FILL_HREPEAT|FILL_VREPEAT, 74 FILL_REFLECT = FILL_HREFLECT|FILL_VREFLECT, 75 76 FILL_FAST = 128, 77 78 GRADIENT_PAD = 0, 79 GRADIENT_REPEAT = 1, 80 GRADIENT_REFLECT = 2, 81 }; 82 83 class Painter : public Draw { 84 public: 85 virtual dword GetInfo() const; 86 87 virtual void OffsetOp(Point p); 88 virtual bool ClipOp(const Rect& r); 89 virtual bool ClipoffOp(const Rect& r); 90 virtual bool ExcludeClipOp(const Rect& r); 91 virtual bool IntersectClipOp(const Rect& r); 92 virtual bool IsPaintingOp(const Rect& r) const; 93 94 virtual void DrawRectOp(int x, int y, int cx, int cy, Color color); 95 virtual void DrawImageOp(int x, int y, int cx, int cy, const Image& img, const Rect& src, Color color); 96 virtual void DrawLineOp(int x1, int y1, int x2, int y2, int width, Color color); 97 virtual void DrawPolyPolylineOp(const Point *vertices, int vertex_count, const int *counts, 98 int count_count, int width, Color color, Color doxor); 99 virtual void DrawPolyPolyPolygonOp(const Point *vertices, int vertex_count, 100 const int *subpolygon_counts, int scc, 101 const int *disjunct_polygon_counts, int dpcc, Color color, 102 int width, Color outline, uint64 pattern, Color doxor); 103 virtual void DrawArcOp(const Rect& rc, Point start, Point end, int width, Color color); 104 virtual void DrawEllipseOp(const Rect& r, Color color, int pen, Color pencolor); 105 virtual void DrawTextOp(int x, int y, int angle, const wchar *text, Font font, Color ink, int n, const int *dx); 106 virtual void DrawPaintingOp(const Rect& target, const Painting& p); 107 108 protected: 109 virtual void ClearOp(const RGBA& color) = 0; 110 111 virtual void MoveOp(const Pointf& p, bool rel) = 0; 112 virtual void LineOp(const Pointf& p, bool rel) = 0; 113 virtual void QuadraticOp(const Pointf& p1, const Pointf& p, bool rel) = 0; 114 virtual void QuadraticOp(const Pointf& p, bool rel) = 0; 115 virtual void CubicOp(const Pointf& p1, const Pointf& p2, const Pointf& p, bool rel) = 0; 116 virtual void CubicOp(const Pointf& p2, const Pointf& p, bool rel) = 0; 117 virtual void ArcOp(const Pointf& c, const Pointf& r, double angle, double sweep, bool rel) = 0; 118 virtual void SvgArcOp(const Pointf& r, double xangle, bool large, bool sweep, 119 const Pointf& p, bool rel) = 0; 120 virtual void CloseOp() = 0; 121 virtual void DivOp() = 0; 122 123 virtual void FillOp(const RGBA& color) = 0; 124 virtual void FillOp(const Image& image, const Xform2D& transsrc, dword flags) = 0; 125 virtual void FillOp(const Pointf& p1, const RGBA& color1, 126 const Pointf& p2, const RGBA& color2, 127 int style) = 0; 128 virtual void FillOp(const Pointf& f, const RGBA& color1, 129 const Pointf& c, double r, const RGBA& color2, 130 int style) = 0; 131 virtual void FillOp(const RGBA& color1, const RGBA& color2, const Xform2D& transsrc, 132 int style) = 0; 133 virtual void FillOp(const Pointf& f, const RGBA& color1, const RGBA& color2, 134 const Xform2D& transsrc, int style) = 0; 135 136 virtual void StrokeOp(double width, const RGBA& rgba) = 0; 137 virtual void StrokeOp(double width, const Image& image, const Xform2D& transsrc, 138 dword flags) = 0; 139 virtual void StrokeOp(double width, const Pointf& p1, const RGBA& color1, 140 const Pointf& p2, const RGBA& color2, 141 int style) = 0; 142 virtual void StrokeOp(double width, const RGBA& color1, const RGBA& color2, 143 const Xform2D& transsrc, int style) = 0; 144 virtual void StrokeOp(double width, const Pointf& f, const RGBA& color1, 145 const Pointf& c, double r, const RGBA& color2, 146 int style) = 0; 147 virtual void StrokeOp(double width, const Pointf& f, 148 const RGBA& color1, const RGBA& color2, 149 const Xform2D& transsrc, int style) = 0; 150 151 virtual void ClipOp() = 0; 152 153 virtual void CharacterOp(const Pointf& p, int ch, Font fnt) = 0; 154 virtual void TextOp(const Pointf& p, const wchar *text, Font fnt, int n = -1, 155 const double *dx = NULL); 156 157 virtual void ColorStopOp(double pos, const RGBA& color) = 0; 158 virtual void ClearStopsOp() = 0; 159 160 virtual void OpacityOp(double o) = 0; 161 virtual void LineCapOp(int linecap) = 0; 162 virtual void LineJoinOp(int linejoin) = 0; 163 virtual void MiterLimitOp(double l) = 0; 164 virtual void EvenOddOp(bool evenodd) = 0; 165 virtual void DashOp(const Vector<double>& dash, double start = 0) = 0; 166 virtual void DashOp(const String& dash, double start = 0); 167 virtual void InvertOp(bool invert) = 0; 168 169 virtual void TransformOp(const Xform2D& m) = 0; 170 171 virtual void BeginOp() = 0; 172 virtual void EndOp() = 0; 173 174 virtual void BeginMaskOp() = 0; 175 virtual void BeginOnPathOp(double q, bool absolute) = 0; 176 177 protected: 178 void DoArc0(double theta, double th_sweep, const Xform2D& m); 179 void DoArc(const Pointf& c, const Pointf& r, double angle, double sweep, double xangle); 180 void DoSvgArc(const Pointf& rr, double xangle, int large, int sweep, 181 const Pointf& p, const Pointf& p0); 182 void DrawLineStroke(int width, Color color); 183 184 public: 185 void Clear(const RGBA& color); 186 187 Painter& Move(const Pointf& p, bool rel); 188 Painter& Move(const Pointf& p); 189 Painter& Move(double x, double y, bool rel); 190 Painter& Move(double x, double y); 191 Painter& RelMove(const Pointf& p); 192 Painter& RelMove(double x, double y); 193 194 Painter& Line(const Pointf& p, bool rel); 195 Painter& Line(const Pointf& p); 196 Painter& Line(double x, double y, bool rel); 197 Painter& Line(double x, double y); 198 Painter& RelLine(const Pointf& p); 199 Painter& RelLine(double x, double y); 200 201 Painter& Quadratic(const Pointf& p1, const Pointf& p, bool rel); 202 Painter& Quadratic(const Pointf& p1, const Pointf& p); 203 Painter& Quadratic(const Pointf& p); 204 Painter& Quadratic(double x, double y, bool rel); 205 Painter& Quadratic(double x1, double y1, double x, double y, bool rel); 206 Painter& Quadratic(const Pointf& p, bool rel); 207 Painter& Quadratic(double x1, double y1, double x, double y); 208 Painter& Quadratic(double x, double y); 209 Painter& RelQuadratic(const Pointf& p1, const Pointf& p); 210 Painter& RelQuadratic(double x1, double y1, double x, double y); 211 Painter& RelQuadratic(double x, double y); 212 Painter& RelQuadratic(const Pointf& p); 213 214 Painter& Cubic(const Pointf& p1, const Pointf& p2, const Pointf& p, bool rel); 215 Painter& Cubic(const Pointf& p2, const Pointf& p, bool rel); 216 Painter& Cubic(const Pointf& p1, const Pointf& p2, const Pointf& p); 217 Painter& Cubic(const Pointf& p2, const Pointf& p); 218 Painter& Cubic(double x1, double y1, double x2, double y2, double x, double y, bool rel); 219 Painter& Cubic(double x2, double y2, double x, double y, bool rel); 220 Painter& Cubic(double x1, double y1, double x2, double y2, double x, double y); 221 Painter& Cubic(double x2, double y2, double x, double y); 222 Painter& RelCubic(const Pointf& p1, const Pointf& p2, const Pointf& p); 223 Painter& RelCubic(const Pointf& p2, const Pointf& p); 224 Painter& RelCubic(double x1, double y1, double x2, double y2, double x, double y); 225 Painter& RelCubic(double x2, double y2, double x, double y); 226 227 Painter& Arc(const Pointf& c, const Pointf& r, double angle, double sweep, bool rel); 228 Painter& Arc(const Pointf& c, double rx, double ry, double angle, double sweep, bool rel); 229 Painter& Arc(const Pointf& c, double r, double angle, double sweep, bool rel); 230 Painter& Arc(double x, double y, double rx, double ry, double angle, double sweep, bool rel); 231 Painter& Arc(double x, double y, double r, double angle, double sweep, bool rel); 232 Painter& Arc(const Pointf& c, const Pointf& r, double angle, double sweep); 233 Painter& Arc(const Pointf& c, double rx, double ry, double angle, double sweep); 234 Painter& Arc(const Pointf& c, double r, double angle, double sweep); 235 Painter& Arc(double x, double y, double rx, double ry, double angle, double sweep); 236 Painter& Arc(double x, double y, double r, double angle, double sweep); 237 Painter& RelArc(const Pointf& c, const Pointf& r, double angle, double sweep); 238 Painter& RelArc(const Pointf& c, double rx, double ry, double angle, double sweep); 239 Painter& RelArc(const Pointf& c, double r, double angle, double sweep); 240 Painter& RelArc(double x, double y, double rx, double ry, double angle, double sweep); 241 Painter& RelArc(double x, double y, double r, double angle, double sweep); 242 243 Painter& SvgArc(const Pointf& r, double xangle, bool large, bool sweep, const Pointf& p, bool rel); 244 Painter& SvgArc(double rx, double ry, double xangle, bool large, bool sweep, const Pointf& p, bool rel); 245 Painter& SvgArc(double rx, double ry, double xangle, bool large, bool sweep, double x, double y, bool rel); 246 Painter& SvgArc(const Pointf& r, double xangle, bool large, bool sweep, const Pointf& p); 247 Painter& SvgArc(double rx, double ry, double xangle, bool large, bool sweep, const Pointf& p); 248 Painter& SvgArc(double rx, double ry, double xangle, bool large, bool sweep, double x, double y); 249 Painter& RelSvgArc(const Pointf& r, double xangle, bool large, bool sweep, const Pointf& p); 250 Painter& RelSvgArc(double rx, double ry, double xangle, bool large, bool sweep, const Pointf& p); 251 Painter& RelSvgArc(double rx, double ry, double xangle, bool large, bool sweep, double x, double y); 252 253 Painter& Close(); 254 Painter& Div(); 255 256 Painter& Path(CParser& p); 257 Painter& Path(const char *path); 258 259 Painter& Fill(const RGBA& color); 260 Painter& Fill(const Image& image, const Xform2D& transsrc = Xform2D::Identity(), dword flags = 0); 261 Painter& Fill(const Image& image, Pointf p1, Pointf p2, dword flags = 0); 262 Painter& Fill(const Image& image, double x1, double y1, double x2, double y2, 263 dword flags = 0); 264 Painter& Fill(const Pointf& p1, const RGBA& color1, 265 const Pointf& p2, const RGBA& color2, int style = GRADIENT_PAD); 266 Painter& Fill(double x1, double y1, const RGBA& color1, 267 double x2, double y2, const RGBA& color2, int style = GRADIENT_PAD); 268 Painter& Fill(const RGBA& color1, const RGBA& color2, const Xform2D& transsrc, 269 dword flags = 0); 270 Painter& Fill(const Pointf& f, const RGBA& color1, 271 const Pointf& c, double r, const RGBA& color2, int style = GRADIENT_PAD); 272 Painter& Fill(double fx, double fy, const RGBA& color1, 273 double cx, double cy, double r, const RGBA& color2, int style = GRADIENT_PAD); 274 Painter& Fill(const Pointf& c, const RGBA& color1, 275 double r, const RGBA& color2, int style = GRADIENT_PAD); 276 Painter& Fill(double x, double y, const RGBA& color1, 277 double r, const RGBA& color2, int style = GRADIENT_PAD); 278 Painter& Fill(const Pointf& f, const RGBA& color1, const RGBA& color2, 279 const Xform2D& transsrc, int style = GRADIENT_PAD); 280 281 Painter& Stroke(double width, const RGBA& color); 282 Painter& Stroke(double width, const Image& image, const Xform2D& transsrc, dword flags = 0); 283 Painter& Stroke(double width, const Image& image, const Pointf& p1, const Pointf& p2, 284 dword flags = 0); 285 Painter& Stroke(double width, const Image& image, double x1, double y1, double x2, double y2, 286 dword flags = 0); 287 Painter& Stroke(double width, const Pointf& p1, const RGBA& color1, 288 const Pointf& p2, const RGBA& color2, int style = GRADIENT_PAD); 289 Painter& Stroke(double width, double x1, double y1, const RGBA& color1, 290 double x2, double y2, const RGBA& color2, int style = GRADIENT_PAD); 291 Painter& Stroke(double width, const RGBA& color1, const RGBA& color2, const Xform2D& transsrc, 292 dword flags = 0); 293 Painter& Stroke(double width, const Pointf& f, const RGBA& color1, 294 const Pointf& c, double r, const RGBA& color2, int style = GRADIENT_PAD); 295 Painter& Stroke(double width, double fx, double fy, const RGBA& color1, 296 double cx, double cy, double r, const RGBA& color2, int style = GRADIENT_PAD); 297 Painter& Stroke(double width, const Pointf& c, const RGBA& color1, 298 double r, const RGBA& color2, int style = GRADIENT_PAD); 299 Painter& Stroke(double width, double x, double y, const RGBA& color1, 300 double r, const RGBA& color2, int style = GRADIENT_PAD); 301 Painter& Stroke(double width, const Pointf& f, 302 const RGBA& color1, const RGBA& color2, 303 const Xform2D& transsrc, int style = GRADIENT_PAD); 304 305 Painter& Clip(); 306 307 Painter& Character(const Pointf& p, int ch, Font fnt); 308 Painter& Character(double x, double y, int ch, Font fnt); 309 Painter& Text(const Pointf& p, const wchar *text, Font fnt, int n = -1, const double *dx = NULL); 310 Painter& Text(double x, double y, const wchar *text, Font fnt, int n = -1, const double *dx = NULL); 311 Painter& Text(const Pointf& p, const WString& s, Font fnt, const double *dx = NULL); 312 Painter& Text(double x, double y, const WString& s, Font fnt, const double *dx = NULL); 313 Painter& Text(const Pointf& p, const String& s, Font fnt, const double *dx = NULL); 314 Painter& Text(double x, double y, const String& s, Font fnt, const double *dx = NULL); 315 Painter& Text(const Pointf& p, const char *text, Font fnt, int n = -1, const double *dx = NULL); 316 Painter& Text(double x, double y, const char *text, Font fnt, int n = -1, const double *dx = NULL); 317 EndPath()318 void EndPath() { Stroke(0, RGBAZero()); } 319 320 void Begin(); 321 void End(); 322 323 void BeginMask(); 324 void BeginOnPath(double q, bool absolute = false); 325 326 Painter& ColorStop(double pos, const RGBA& color); 327 Painter& ClearStops(); 328 Painter& Opacity(double o); 329 Painter& LineCap(int linecap); 330 Painter& LineJoin(int linejoin); 331 Painter& MiterLimit(double l); 332 Painter& EvenOdd(bool evenodd = true); 333 Painter& Dash(const Vector<double>& dash, double start); 334 Painter& Dash(const char *dash, double start = 0); 335 Painter& Invert(bool b = true); 336 337 Painter& Transform(const Xform2D& m); 338 Painter& Translate(double x, double y); 339 Painter& Translate(const Pointf& p); 340 Painter& Rotate(double a); 341 Painter& Scale(double scalex, double scaley); 342 Painter& Scale(double scale); 343 344 void Paint(const Painting& p); 345 346 Painter& Rectangle(double x, double y, double cx, double cy); 347 Painter& RoundedRectangle(double x, double y, double cx, double cy, double r); 348 Painter& RoundedRectangle(double x, double y, double cx, double cy, double r1, double r2); 349 Painter& Ellipse(double x, double y, double rx, double ry); 350 Painter& Circle(double x, double y, double r); 351 352 Painter& RectPath(int x, int y, int cx, int cy); 353 Painter& RectPath(const Rect& r); 354 Painter& RectPath(double x, double y, double cx, double cy); 355 Painter& RectPath(const Rectf& r); 356 }; 357 358 void PaintCharacter(Painter& sw, const Pointf& p, int ch, Font fnt); 359 360 #include "Painter.hpp" 361 #include "Painting.h" 362 #include "LinearPath.h" 363 #include "BufferPainter.h" 364 365 class ImageBuffer__ { 366 protected: 367 ImageBuffer ib; 368 369 public: ImageBuffer__(Size sz)370 ImageBuffer__(Size sz) : ib(sz) {} 371 }; 372 373 class ImagePainter : private ImageBuffer__, public BufferPainter { 374 public: 375 ImagePainter(Size sz, int mode = MODE_ANTIALIASED); 376 ImagePainter(int cx, int cy, int mode = MODE_ANTIALIASED); 377 GetResult()378 Image GetResult() { Finish(); return ImageBuffer__::ib; } Image()379 operator Image() { return GetResult(); } 380 }; 381 382 class DrawPainter : public ImagePainter { 383 Draw& w; 384 385 public: 386 DrawPainter(Draw& w, Size sz, int mode = MODE_ANTIALIASED); DrawPainter(w,Size (cx,cy),mode)387 DrawPainter(Draw& w, int cx, int cy, int mode = MODE_ANTIALIASED) : DrawPainter(w, Size(cx, cy), mode) {} 388 ~DrawPainter(); 389 }; 390 391 class NilPainter : public Painter { 392 protected: 393 virtual void ClearOp(const RGBA& color); 394 395 virtual void MoveOp(const Pointf& p, bool rel); 396 virtual void LineOp(const Pointf& p, bool rel); 397 virtual void QuadraticOp(const Pointf& p1, const Pointf& p, bool rel); 398 virtual void QuadraticOp(const Pointf& p, bool rel); 399 virtual void CubicOp(const Pointf& p1, const Pointf& p2, const Pointf& p, bool rel); 400 virtual void CubicOp(const Pointf& p2, const Pointf& p, bool rel); 401 virtual void ArcOp(const Pointf& c, const Pointf& r, double angle, double sweep, bool rel); 402 virtual void SvgArcOp(const Pointf& r, double xangle, bool large, bool sweep, 403 const Pointf& p, bool rel); 404 virtual void CloseOp(); 405 virtual void DivOp(); 406 407 virtual void FillOp(const RGBA& color); 408 virtual void FillOp(const Image& image, const Xform2D& transsrc, dword flags); 409 virtual void FillOp(const Pointf& p1, const RGBA& color1, 410 const Pointf& p2, const RGBA& color2, 411 int style); 412 virtual void FillOp(const RGBA& color1, const RGBA& color2, const Xform2D& transsrc, 413 int style); 414 virtual void FillOp(const Pointf& f, const RGBA& color1, 415 const Pointf& c, double r, const RGBA& color2, 416 int style); 417 virtual void FillOp(const Pointf& f, const RGBA& color1, const RGBA& color2, 418 const Xform2D& transsrc, int style); 419 420 virtual void StrokeOp(double width, const RGBA& rgba); 421 virtual void StrokeOp(double width, const Image& image, const Xform2D& transsrc, 422 dword flags); 423 virtual void StrokeOp(double width, const Pointf& p1, const RGBA& color1, 424 const Pointf& p2, const RGBA& color2, 425 int style); 426 virtual void StrokeOp(double width, const RGBA& color1, const RGBA& color2, 427 const Xform2D& transsrc, 428 int style); 429 virtual void StrokeOp(double width, const Pointf& f, const RGBA& color1, 430 const Pointf& c, double r, const RGBA& color2, 431 int style); 432 virtual void StrokeOp(double width, const Pointf& f, 433 const RGBA& color1, const RGBA& color2, 434 const Xform2D& transsrc, int style); 435 436 virtual void ClipOp(); 437 438 virtual void CharacterOp(const Pointf& p, int ch, Font fnt); 439 virtual void TextOp(const Pointf& p, const wchar *text, Font fnt, int n = -1, 440 const double *dx = NULL); 441 442 virtual void ColorStopOp(double pos, const RGBA& color); 443 virtual void ClearStopsOp(); 444 445 virtual void OpacityOp(double o); 446 virtual void LineCapOp(int linecap); 447 virtual void LineJoinOp(int linejoin); 448 virtual void MiterLimitOp(double l); 449 virtual void EvenOddOp(bool evenodd); 450 virtual void DashOp(const Vector<double>& dash, double start); 451 virtual void InvertOp(bool invert); 452 453 virtual void TransformOp(const Xform2D& m); 454 455 virtual void BeginOp(); 456 virtual void EndOp(); 457 458 virtual void BeginMaskOp(); 459 virtual void BeginOnPathOp(double q, bool abs); 460 }; 461 462 bool RenderSVG(Painter& p, const char *svg, Event<String, String&> resloader); 463 bool RenderSVG(Painter& p, const char *svg); 464 465 void GetSVGDimensions(const char *svg, Sizef& sz, Rectf& viewbox); 466 Rectf GetSVGBoundingBox(const char *svg); 467 Rectf GetSVGPathBoundingBox(const char *path); 468 469 Image RenderSVGImage(Size sz, const char *svg, Event<String, String&> resloader); 470 Image RenderSVGImage(Size sz, const char *svg); 471 472 bool IsSVG(const char *svg); 473 474 } 475 476 #endif 477