1 ///
2 module plot2d.backend.base;
3 
4 public import plot2d.types;
5 
6 ///
7 interface Ctx
8 {
9     ///
10     void save();
11     ///
12     void restore();
13     ///
14     void stroke();
15     ///
16     void fill();
17     ///
18     void moveTo(double x, double y);
19     ///
20     void lineTo(double x, double y);
21     ///
22     void setLineWidth(double lw);
23     ///
24     void showText(string str);
25     ///
26     void setDash(double[] dash, double offset);
27     ///
28     void setColor(double r, double g, double b, double a=1);
29     ///
30     void getTextSize(string str, out double w, out double h);
31     ///
32     void setFont(string name, double size);
33     ///
34     void clipViewport(Viewport vp);
35 
36 final:
37     ///
38     void moveToP(P)(P p) { moveTo(p.x, p.y); }
39     ///
40     void lineToP(P)(P p) { lineTo(p.x, p.y); }
41     ///
42     void lineP2P()(auto ref const Point p0, Point[] ps...)
43     {
44         moveToP(p0);
45         foreach (p; ps) lineToP(p);
46     }
47     ///
48     void lineP2P()(double x1, double y1, double x2, double y2)
49     { lineP2P(Point(x1, y1), Point(x2, y2)); }
50     ///
51     void setColor(C)(C c) { setColor(c.r, c.g, c.b, c.a); }
52     ///
53     void setColor(C)(C c, double a) { setColor(c.r, c.g, c.b, a); }
54 }
55 
56 /++ mixin string for saving Context
57     and restore on exit from scope
58 +/
59 string scopeSave(alias cr)()
60     if (is(typeof(cr) == Ctx))
61 {
62     import std..string : format;
63     return format(q{
64         %1$s.save();
65         scope(exit)
66             %1$s.restore();
67     }, __traits(identifier, cr));
68 }