1 #include "Painter.h"
2 
3 namespace Upp {
4 
GetInfo() const5 dword Painter::GetInfo() const
6 {
7 	return DOTS;
8 }
9 
OffsetOp(Point p)10 void Painter::OffsetOp(Point p)
11 {
12 	Begin();
13 	Translate(p.x, p.y);
14 }
15 
RectPath(int x,int y,int cx,int cy)16 Painter& Painter::RectPath(int x, int y, int cx, int cy)
17 {
18 	Move(x, y).Line(x + cx, y).Line(x + cx, y + cy).Line(x, y + cy).Close();
19 	return *this;
20 }
21 
RectPath(const Rect & r)22 Painter& Painter::RectPath(const Rect& r)
23 {
24 	RectPath(r.left, r.top, r.GetWidth(), r.GetHeight());
25 	return *this;
26 }
27 
RectPath(double x,double y,double cx,double cy)28 Painter& Painter::RectPath(double x, double y, double cx, double cy)
29 {
30 	Move(x, y).Line(x + cx, y).Line(x + cx, y + cy).Line(x, y + cy).Close();
31 	return *this;
32 }
33 
RectPath(const Rectf & r)34 Painter& Painter::RectPath(const Rectf& r)
35 {
36 	RectPath(r.left, r.top, r.GetWidth(), r.GetHeight());
37 	return *this;
38 }
39 
ClipOp(const Rect & r)40 bool Painter::ClipOp(const Rect& r)
41 {
42 	Begin();
43 	RectPath(r);
44 	Clip();
45 	return true;
46 }
47 
ClipoffOp(const Rect & r)48 bool Painter::ClipoffOp(const Rect& r)
49 {
50 	Begin();
51 	RectPath(r);
52 	Clip();
53 	Translate(r.left, r.top);
54 	return true;
55 }
56 
ExcludeClipOp(const Rect & r)57 bool Painter::ExcludeClipOp(const Rect& r)
58 {
59 	RectPath(Rect(-99999, -99999, 99999, r.top));
60 	RectPath(Rect(-99999, r.top, r.left, 99999));
61 	RectPath(Rect(r.right, r.top, 99999, 99999));
62 	RectPath(Rect(r.left, r.bottom, r.right, 99999));
63 	Clip();
64 	return true;
65 }
66 
IntersectClipOp(const Rect & r)67 bool Painter::IntersectClipOp(const Rect& r)
68 {
69 	RectPath(r);
70 	Clip();
71 	return true;
72 }
73 
IsPaintingOp(const Rect & r) const74 bool Painter::IsPaintingOp(const Rect& r) const
75 {
76 	return true;
77 }
78 
DrawRectOp(int x,int y,int cx,int cy,Color color)79 void Painter::DrawRectOp(int x, int y, int cx, int cy, Color color)
80 {
81 	RectPath(x, y, cx, cy);
82 	if(color == InvertColor)
83 		Invert();
84 	Fill(color);
85 }
86 
DrawImageOp(int x,int y,int cx,int cy,const Image & image,const Rect & src,Color color)87 void Painter::DrawImageOp(int x, int y, int cx, int cy, const Image& image, const Rect& src, Color color)
88 {
89 	if(src.GetWidth() * src.GetHeight() == 0)
90 		return;
91 	Image img = IsNull(color) ? image : SetColorKeepAlpha(image, color);
92 	RectPath(x, y, cx, cy);
93 	double sw = (double)cx / src.GetWidth();
94 	double sh = (double)cy / src.GetHeight();
95 	Fill(img, Xform2D::Scale(sw, sh) * Xform2D::Translation(x - sw * src.left, y - sh * src.top));
96 }
97 
DrawLineStroke(int width,Color color)98 void Painter::DrawLineStroke(int width, Color color)
99 {
100 	if(IsNull(width) || IsNull(color))
101 		return;
102 	Begin();
103 	LineCap(LINECAP_ROUND);
104 	switch(width) {
105 	case PEN_NULL:
106 		Stroke(0, color);
107 		End();
108 		return;
109 	case PEN_SOLID:
110 		Stroke(1, color);
111 		break;
112 	case PEN_DASH:
113 		Dash("18 6");
114 		break;
115 	case PEN_DOT:
116 		Dash("3 3");
117 		break;
118 	case PEN_DASHDOT:
119 		Dash("9 6 3 6");
120 		break;
121 	case PEN_DASHDOTDOT:
122 		Dash("9 3 3 3 3 3");
123 		break;
124 	default:
125 		Stroke(width == 0 ? 1 : width, color);
126 		End();
127 		return;
128 	}
129 	Stroke(1, color);
130 	End();
131 }
132 
DrawLineOp(int x1,int y1,int x2,int y2,int width,Color color)133 void Painter::DrawLineOp(int x1, int y1, int x2, int y2, int width, Color color)
134 {
135 	Move(x1, y1);
136 	Line(x2, y2);
137 	DrawLineStroke(width, color);
138 }
139 
DrawPolyPolylineOp(const Point * vertices,int vertex_count,const int * counts,int count_count,int width,Color color,Color doxor)140 void Painter::DrawPolyPolylineOp(const Point *vertices, int vertex_count, const int *counts,
141                                  int count_count, int width, Color color, Color doxor)
142 {
143 	while(--count_count >= 0) {
144 		const Point *lp = vertices;
145 		vertices += *counts++;
146 		Move(*lp);
147 		while(++lp < vertices)
148 			Line(*lp);
149 		DrawLineStroke(width, color);
150 	}
151 }
152 
DrawPolyPolyPolygonOp(const Point * vertices,int vertex_count,const int * subpolygon_counts,int scc,const int * disjunct_polygon_counts,int dpcc,Color color,int width,Color outline,uint64 pattern,Color doxor)153 void Painter::DrawPolyPolyPolygonOp(const Point *vertices, int vertex_count,
154                                   const int *subpolygon_counts, int scc, const int *disjunct_polygon_counts,
155                                   int dpcc, Color color, int width, Color outline, uint64 pattern, Color doxor)
156 {
157 	Image fill_img;
158 	if(pattern && !IsNull(color)) {
159 		ImageBuffer ibuf(8, 8);
160 		RGBA r[2] = { color, RGBAZero() };
161 		for(RGBA *out = ibuf, *end = out + 64; out < end; pattern >>= 1)
162 			*out++ = r[(byte)pattern & 1];
163 		fill_img = ibuf;
164 	}
165 
166 	while(--dpcc >= 0) {
167 		const Point *sp = vertices;
168 		vertices += *disjunct_polygon_counts++;
169 		while(sp < vertices) {
170 			const Point *pp = sp;
171 			sp += *subpolygon_counts++;
172 			Move(*pp);
173 			while(++pp < sp)
174 				Line(*pp);
175 			Close();
176 		}
177 		if(!IsNull(fill_img))
178 			Fill(fill_img, Xform2D::Identity(), FILL_HREPEAT | FILL_VREPEAT);
179 		else if(!IsNull(color))
180 			Fill(color);
181 		if(!IsNull(outline))
182 			DrawLineStroke(width, outline);
183 	}
184 }
185 
DrawArcOp(const Rect & rc,Point start,Point end,int width,Color color)186 void Painter::DrawArcOp(const Rect& rc, Point start, Point end, int width, Color color)
187 {
188 	if(rc.Width() <= 0 || rc.Height() <= 0)
189 		return;
190 	Sizef radius = Sizef(rc.Size()) / 2.0;
191 	Pointf center = Pointf(rc.TopLeft()) + radius;
192 	double ang1 = Bearing((Pointf(start) - center) / radius);
193 	double ang2 = Bearing((Pointf(end) - center) / radius);
194 	double sweep = ang1 - ang2;
195 	if(sweep <= 0)
196 		sweep += 2 * M_PI;
197 	Move(center.x + radius.cx * cos(ang1), center.y + radius.cy * sin(ang1));
198 	Arc(center, radius.cx, radius.cy, ang1, -sweep);
199 	DrawLineStroke(width, color);
200 }
201 
DrawEllipseOp(const Rect & r,Color color,int pen,Color pencolor)202 void Painter::DrawEllipseOp(const Rect& r, Color color, int pen, Color pencolor)
203 {
204 	Sizef sz = r.GetSize();
205 	Ellipse(r.left + sz.cx / 2, r.top + sz.cy / 2, sz.cx / 2, sz.cy / 2);
206 	Fill(color);
207 	DrawLineStroke(pen, pencolor);
208 }
209 
DrawTextOp(int x,int y,int angle,const wchar * text,Font font,Color ink,int n,const int * dx)210 void Painter::DrawTextOp(int x, int y, int angle, const wchar *text, Font font, Color ink, int n, const int *dx)
211 {
212 	Begin();
213 	EvenOdd(true);
214 	Translate(x, y);
215 	if(angle)
216 		Rotate(-angle * M_2PI / 3600);
217 	if(n < 0)
218 		n = wstrlen(text);
219 	double *ddx = NULL;
220 	Buffer<double> h;
221 	if(dx) {
222 		h.Alloc(n);
223 		ddx = h;
224 		for(int i = 0; i < n; i++)
225 			ddx[i] = dx[i];
226 	}
227 	Text(0, 0, text, font, n, ddx);
228 	Fill(ink);
229 	End();
230 }
231 
DrawPaintingOp(const Rect & target,const Painting & p)232 void Painter::DrawPaintingOp(const Rect& target, const Painting& p)
233 {
234 	Size sz = target.GetSize();
235 	Sizef psz = p.GetSize();
236 	Begin();
237 	Translate(target.left, target.top);
238 	Scale(sz.cx / psz.cx, sz.cy / psz.cy);
239 	Paint(p);
240 	End();
241 }
242 
243 }
244