summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/drawfill.cc
blob: 0353ae2c4050c9c57e8b2a91ab5cd80f76b8c709 (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
/*****
 * drawfill.cc
 * Andy Hammerlindl 2002/06/06
 *
 * Stores a cyclic path that will outline a filled shape in a picture.
 *****/

#include "drawfill.h"

namespace camp {

void drawAxialShade::palette(psfile *out)
{
  pentype.convert();
  penb.convert();
  
  colorspace=(ColorSpace) max(pentype.colorspace(),penb.colorspace());
  
  switch(colorspace) {
    case RGB:
    {
      if (pentype.grayscale()) pentype.greytorgb();
      else if (penb.grayscale()) penb.greytorgb();
      break;
    }
      
    case CMYK:
    {
      if (pentype.grayscale()) pentype.greytocmyk();
      else if (penb.grayscale()) penb.greytocmyk();
        
      if (pentype.rgb()) pentype.rgbtocmyk();
      else if (penb.rgb()) penb.rgbtocmyk();
      break;
    }
    default:
      break;
  }
  
  out->gsave();
}  
  
bool drawFill::draw(psfile *out)
{
  if(pentype.invisible() || empty()) return true;
  
  palette(out);
  writepath(out);
  fill(out);
  return true;
}
  
drawElement *drawFill::transformed(const transform& t)
{
  return new drawFill(transpath(t),stroke,transpen(t));
}
  
drawElement *drawLatticeShade::transformed(const transform& t)
{
  return new drawLatticeShade(transpath(t),stroke,pentype,pens,t*T);
}

drawElement *drawAxialShade::transformed(const transform& t)
{
  pair A=t*a, B=t*b;
  return new drawAxialShade(transpath(t),stroke,pentype,A,penb,B);
}
  
drawElement *drawRadialShade::transformed(const transform& t)
{
  pair A=t*a, B=t*b;
  double RA=length(t*(a+ra)-A);
  double RB=length(t*(b+rb)-B);
  return new drawRadialShade(transpath(t),stroke,pentype,A,RA,penb,B,RB);
}

drawElement *drawGouraudShade::transformed(const transform& t)
{
  size_t size=vertices.size();
  vm::array *Vertices=new vm::array(size);
  for(size_t i=0; i < size; i++)
    (*Vertices)[i]=t*vm::read<pair>(vertices,i);

  return new drawGouraudShade(transpath(t),stroke,pentype,pens,*Vertices,edges);
}

drawElement *drawTensorShade::transformed(const transform& t)
{
  size_t size=boundaries.size();
  size_t zsize=z.size();
  vm::array *Boundaries=new vm::array(size);
  vm::array *Z=new vm::array(zsize);
  
  for(size_t i=0; i < size; i++)
    (*Boundaries)[i]=vm::read<path>(boundaries,i).transformed(t);
  
  for(size_t i=0; i < zsize; i++) {
    vm::array *zi=vm::read<vm::array *>(z,i);
    size_t zisize=checkArray(zi);
    vm::array *Zi=new vm::array(zisize);
    (*Z)[i]=Zi;
    for(size_t j=0; j < zisize; j++)
      (*Zi)[j]=t*vm::read<pair>(zi,j);
  }

  return new drawTensorShade(transpath(t),stroke,pentype,pens,*Boundaries,*Z);
}

bool drawFunctionShade::write(texfile *out, const bbox& box)
{
  if(empty()) return true;
  
  ColorSpace colorspace=pentype.colorspace();
  size_t ncomponents=ColorComponents[colorspace];
  
  out->verbatim("\\pdfobj stream attr {/FunctionType 4");
  out->verbatim("/Domain [0 1 0 1]");
  out->verbatim("/Range [");
  for(size_t i=0; i < ncomponents; ++i)
    out->verbatim("0 1 ");
  out->verbatim("]}{{");
  out->verbatimline(shader);
  out->verbatimline("}}%");
  out->verbatimline("\\edef\\lastobj{\\the\\pdflastobj}\\pdfrefobj\\lastobj");
   
  out->verbatim("\\setbox\\ASYbox=\\hbox to ");
  double Hoffset=out->hoffset();
  double hoffset=(bpath.Max().getx()-Hoffset)*ps2tex;
  out->write(hoffset);
  out->verbatim("pt {");
  out->verbatim("\\vbox to ");
  out->write((box.top-box.bottom)*ps2tex);
  out->verbatimline("pt {\\vfil%");
  out->gsave();
  out->beginspecial();
  out->beginraw();
  writeshiftedpath(out);
  if(stroke) strokepath(out);
  out->endclip(pentype);
  out->verbatimline("/Sh sh");
  out->endraw();
  out->endspecial();
  out->grestore();
  out->verbatimline("}\\hfil}%");
  
  out->verbatimline("\\pdfxform resources {");
  out->verbatimline("/Shading << /Sh << /ShadingType 1");
  out->verbatim("/Matrix [");

  out->write(shift(pair(-Hoffset,-box.bottom))*matrix(bpath.Min(),bpath.Max()));
  out->verbatimline("]");
  out->verbatim("/Domain [0 1 0 1]");
  out->verbatimline("/ColorSpace /Device"+ColorDeviceSuffix[colorspace]);
  out->verbatimline("/Function \\lastobj\\space 0 R >> >>}\\ASYbox");
  
  out->verbatimline("\\pdfrefxform\\the\\pdflastxform");
  out->verbatim("\\kern");
  out->write(-hoffset);
  out->verbatimline("pt%");
  return true;
}

drawElement *drawFunctionShade::transformed(const transform& t)
{
  return new drawFunctionShade(transpath(t),stroke,pentype,shader);
}

} // namespace camp