summaryrefslogtreecommitdiff
path: root/graphics/asymptote/drawfill.h
blob: 4f7df49f96704f024b8b76f461e0998f18079d39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*****
 * drawfill.h
 * Andy Hammerlindl 2002/06/06
 *
 * Stores a cyclic path that will outline a filled shape in a picture.
 *****/

#ifndef DRAWFILL_H
#define DRAWFILL_H

#include "drawelement.h"
#include "path.h"

namespace camp {

class drawFill : public drawSuperPathPenBase {
protected:  
  bool stroke;
public:
  void noncyclic() {
    reportError("non-cyclic path cannot be filled");
  }
  
  drawFill(const vm::array& src, bool stroke, pen pentype,
           const string& key="") : 
    drawElement(key), drawSuperPathPenBase(src,pentype), stroke(stroke) {
    if(!stroke && !cyclic()) noncyclic();
  }

  bool svg() {return true;}
  
  // dvisvgm doesn't yet support SVG patterns.
  bool svgpng() {return pentype.fillpattern() != "";}
  
  virtual ~drawFill() {}

  virtual bool draw(psfile *out);
  
  virtual void palette(psfile *out) {
    penSave(out);
    penTranslate(out);
  }
  virtual void fill(psfile *out) {
    out->setpen(pentype);
    if(stroke) out->strokepath();
    out->fill(pentype);
    penRestore(out);
  };

  drawElement *transformed(const transform& t);
};
  
class drawShade : public drawFill {
public:
  drawShade(const vm::array& src, bool stroke, pen pentype,
            const string& key="")
    : drawFill(src,stroke,pentype,key) {}

  void bounds(bbox& b, iopipestream& iopipe, boxvector& vbox,
              bboxlist& bboxstack) {
    if(stroke) strokebounds(b);
    else drawSuperPathPenBase::bounds(b,iopipe,vbox,bboxstack);
  }
  
  // Shading in SVG is incomplete and not supported at all by dvisvgm.
  bool svgpng() {return true;}
      
  virtual void beginshade(psfile *out)=0;
  virtual void shade(psfile *out)=0;
  
  bool draw(psfile *out) {
    if(pentype.invisible() || empty()) return true;
  
    palette(out);
    beginshade(out);
    writeclippath(out);
    if(stroke) strokepath(out);
    out->endpsclip(pentype.Fillrule());
    shade(out);
    out->grestore();
    return true;
  }
};
  
class drawLatticeShade : public drawShade {
protected:
  vm::array pens;
  const transform T;
public:  
  drawLatticeShade(const vm::array& src, bool stroke,
                   pen pentype, const vm::array& pens,
                   const camp::transform& T=identity, const string& key="")
    : drawShade(src,stroke,pentype,key), pens(pens), T(T) {}
  
  void palette(psfile *out) {
    out->gsave();
  }
  
  void beginshade(psfile *out) {
    out->beginlatticeshade(pens,bpath);
  }
  
  void shade(psfile *out) {
    bbox b;
    for(size_t i=0; i < size; i++) {
      path p=vm::read<path>(P,i).transformed(inverse(T));
      if(stroke)
        drawPathPenBase::strokebounds(b,p);
      else 
        b += p.bounds();
    }
    out->latticeshade(pens,T*matrix(b.Min(),b.Max()));
  }
  
  drawElement *transformed(const transform& t);
};
  
class drawAxialShade : public drawShade {
protected:
  pair a;
  bool extenda;
  pen penb;
  pair b;
  bool extendb;
  ColorSpace colorspace;
public:  
  drawAxialShade(const vm::array& src, bool stroke,
                 pen pentype, pair a, bool extenda, pen penb, pair b,
                 bool extendb, const string& key="") 
    : drawShade(src,stroke,pentype,key), a(a), extenda(extenda),
      penb(penb), b(b), extendb(extendb) {}
  
  bool svgpng() {return false;}
  
  void palette(psfile *out);
  
  void beginshade(psfile *out) {
    out->begingradientshade(true,colorspace,pentype,a,0,penb,b,0);
  }
  
  void shade(psfile *out) {
    out->gradientshade(true,colorspace,pentype,a,0,extenda,penb,b,0,extendb);
  }
  
  drawElement *transformed(const transform& t);
};
  
class drawRadialShade : public drawAxialShade {
protected:
  double ra;
  double rb;
public:
  drawRadialShade(const vm::array& src, bool stroke,
                  pen pentype, pair a, double ra, bool extenda, pen penb,
                  pair b, double rb, bool extendb, const string& key="")
    : drawAxialShade(src,stroke,pentype,a,extenda,penb,b,
                     extendb,key), ra(ra), rb(rb) {}
  
  bool svgpng() {return ra > 0.0;}
  
  void beginshade(psfile *out) {
    out->begingradientshade(false,colorspace,pentype,a,ra,penb,b,rb);
  }
  
  void shade(psfile *out) {
    out->gradientshade(false,colorspace,pentype,a,ra,extenda,penb,b,rb,extendb);
  }
  
  drawElement *transformed(const transform& t);
};
  
class drawGouraudShade : public drawShade {
protected:
  vm::array pens,vertices,edges;
public:  
  drawGouraudShade(const vm::array& src, bool stroke,
                   pen pentype, const vm::array& pens,
                   const vm::array& vertices, const vm::array& edges,
                   const string& key="")
    : drawElement(key), drawShade(src,stroke,pentype,key), pens(pens),
      vertices(vertices), edges(edges) {}
  
  bool svgpng() {return !settings::getSetting<bool>("svgemulation");}
  
  void palette(psfile *out) {
    out->gsave();
  }
  
  void beginshade(psfile *out) {
    out->begingouraudshade(pens,vertices,edges);
  }
  
  void shade(psfile *out) {
    out->gouraudshade(pentype,pens,vertices,edges);
  }
  
  drawElement *transformed(const transform& t);
};
  
class drawTensorShade : public drawShade {
protected:
  vm::array pens,boundaries,z;
public:  
  drawTensorShade(const vm::array& src, bool stroke,
                  pen pentype, const vm::array& pens,
                  const vm::array& boundaries, const vm::array& z,
                  const string& key="") : 
    drawShade(src,stroke,pentype,key), pens(pens), boundaries(boundaries),
    z(z) {}
  
  bool svgpng() {
    return pens.size() > 1 || !settings::getSetting<bool>("svgemulation");
  }
  
  void palette(psfile *out) {
    out->gsave();
  }
  
  void beginshade(psfile *out) {
    out->begintensorshade(pens,boundaries,z);
  }
  
  void shade(psfile *out) {
    out->tensorshade(pentype,pens,boundaries,z);
  }
  
  drawElement *transformed(const transform& t);
};
  
class drawFunctionShade : public drawFill {
protected:  
  string shader;
public:
  drawFunctionShade(const vm::array& src, bool stroke,
                    pen pentype, const string& shader, const string& key="")
    : drawFill(src,stroke,pentype,key), shader(shader) {
    string texengine=settings::getSetting<string>("tex");
    if(!settings::pdf(texengine))
      reportError("functionshade is not implemented for the '"+texengine+
                  "' tex engine");
  }

  virtual ~drawFunctionShade() {}

  bool draw(psfile *out) {return false;}
  
  bool write(texfile *, const bbox&);
  
  bool islabel() {return true;}
  
  drawElement *transformed(const transform& t);
};
  
}

#endif