1 ///
2 module plot2d.backend.cairo;
3 
4 import plot2d.backend.base;
5 
6 version (cairo):
7 
8 import cairo.Context;
9 
10 ///
11 class CairoCtx : Ctx
12 {
13     /// set this before draw
14     Context cr;
15 
16 override:
17     void save() { cr.save(); }
18     void restore() { cr.restore(); }
19     void stroke() { cr.stroke(); }
20     void fill() { cr.fill(); }
21     void moveTo(double x, double y) { cr.moveTo(x, y); }
22     void lineTo(double x, double y) { cr.lineTo(x, y); }
23     void setLineWidth(double lw) { cr.setLineWidth(lw); }
24     void showText(string str) { cr.showText(str); }
25     void setDash(double[] dash, double offset) { cr.setDash(dash, offset); }
26     void setColor(double r, double g, double b, double a=1)
27     { cr.setSourceRgba(r, g, b, a); }
28 
29     void getTextSize(string str, out double w, out double h)
30     {
31         cairo_text_extents_t te;
32         cr.textExtents(str, &te);
33         w = te.width;
34         h = te.height;
35     }
36 
37     void setFont(string name, double size)
38     {
39         cr.selectFontFace(name,
40             cairo_font_slant_t.NORMAL,
41             cairo_font_weight_t.NORMAL);
42         cr.setFontSize(size);
43     }
44 
45     void clipViewport(Viewport vp)
46     {
47         cr.moveTo(vp.w.min, vp.h.min);
48         cr.lineTo(vp.w.max, vp.h.min);
49         cr.lineTo(vp.w.max, vp.h.max);
50         cr.lineTo(vp.w.min, vp.h.max);
51         cr.lineTo(vp.w.min, vp.h.min);
52         cr.clip();
53         cr.newPath();
54     }
55 }