File: pst.cc

package info (click to toggle)
epix 1.2.18-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 3,264 kB
  • ctags: 2,685
  • sloc: cpp: 16,837; sh: 5,075; makefile: 158
file content (359 lines) | stat: -rw-r--r-- 8,063 bytes parent folder | download | duplicates (3)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * pst.cc -- ePiX's pstricks output format
 *
 * This file is part of ePiX, a C++ library for creating high-quality 
 * figures in LaTeX 
 *
 * Version 1.1.20
 * Last Change: September 19, 2007
 */

/* 
 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
 * Andrew D. Hwang <rot 13 nujnat at zngupf dot ubylpebff dot rqh>
 * Department of Mathematics and Computer Science
 * College of the Holy Cross
 * Worcester, MA, 01610-2395, USA
 */
 
/*
 * ePiX is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ePiX is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ePiX; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <list>

#include <string>
#include <sstream>

#include <set>

#include "constants.h"

#include "functions.h"

#include "pairs.h"
#include "edge_data.h"

#include "Color.h"

#include "path_style.h"
#include "pen_data.h"

#include "format.h"
#include "pst.h"

namespace ePiX {

  pst::pst()
    : m_fill(Neutral()), m_line(Black()), m_base(Neutral()),
      m_lwidth(PLAIN_WIDTH), m_bwidth("0pt") { }

  pst* pst::clone() const
  {
    return new pst(*this);
  }

  std::string pst::print_fill(const std::list<edge2d>& edges,
			      const pair& offset,
			      const Color& fc,
			      const pen_data& line,
			      const std::string& len) const
  {
    std::stringstream obuf;

    // compute attribute string
    std::string attribs("[fillstyle=");

    if (fc.is_unset())
      attribs += "none";
    else
      attribs += "solid";

    attribs += "]";

    obuf << set_fill_state(fc) << set_pen_state(line); // side effects
    obuf << print_paths(edges, offset, attribs, len);

    return obuf.str();
  }


  std::string pst::print_line(const std::list<edge2d>& edges,
			      const pair& offset,
			      const pen_data& line,
			      const pen_data& base,
			      const path_state& style,
			      const std::string& len) const
  {
    std::stringstream value, obuf;

    obuf << set_pen_state(line);

    // update base
    m_base = base.color();
    m_bwidth = base.width();

    std::string attribs;

    // Generate string to pass as third argument to print_paths if need border
    if (m_lwidth < m_bwidth && !m_base.is_unset())
      {
	length tmp_b(m_bwidth), tmp_l(m_lwidth);
	tmp_b += (tmp_l *= -1);
	tmp_b *= 0.5;

	std::stringstream bordbuf;
	bordbuf << "[border=" << print(tmp_b)
		<< ",bordercolor=" << m_base.name() << "]";

	attribs = bordbuf.str();
      }

    if (style.is_solid())
      obuf << print_paths(edges, offset, attribs, len);

    else // not solid
      {
	// Use base to draw solid underlayer, line to draw top layer.
	// Attempt to adjust PST pen alignment; m_bwidth too large.
	if (m_lwidth < m_bwidth && !base.color().is_unset())
	  obuf << set_pen_state(pen_data(base.color(),
					 0.5*(m_bwidth + m_lwidth)))
	       << print_paths(edges, offset, attribs, len);

	obuf << format::print_line(edges, offset, line, style, "", len);
      }

    return obuf.str();
  }


  std::string pst::print_color(const std::string& model,
			       const std::string& name,
			       double d1, double d2,
			       double d3) const
  {
    // PSTricks declaration
    std::stringstream obuf;
    obuf << "\\new" << model;
    if (model == "cmy")
      obuf << "k";

    obuf << "color{" << name << "}";

    if (model == "cmy")
      {
	double bk(min(min(d1,d2),d3));

	obuf << "{" 
	     << d1-bk << " " << d2-bk << " " << d3-bk << " " << bk
	     << "}%"  << std::endl;
      }

    else
      obuf << "{"
	   << d1 << " " << d2 << " " << d3
	   << "}%" << std::endl;

    return obuf.str();
  }

  std::string pst::print_color(const std::string& model,
			       const std::string& name,
			       double d1, double d2,
			       double d3, double d4) const
  {
    // model presumably "cmyk"
    std::stringstream obuf;
    obuf << "\\newcmykcolor{" << name << "}{"
	 << d1 << " " << d2 << " " << d3 << " " << d4 << "}%"
	 << std::endl;

    return obuf.str();
  }


  std::string pst::print_comment(const std::string& msg) const
  {
    std::stringstream obuf;
    obuf << "%% " << msg << std::endl;

    return obuf.str();
  }

  // verbatim text, %-protected newline
  std::string pst::print_verbatim(const std::string& msg) const
  {
    std::stringstream obuf;
    obuf << msg << "%" << std::endl;

    return obuf.str();
  }

  void pst::reset_state() const
  {
    m_fill = Neutral();
    m_line = Neutral();
    m_base = Neutral();

    m_lwidth = length(0);
    m_bwidth = length(0);
  }


  //// private member functions ////
  std::string pst::start_picture(const pair& sz, const pair& offset) const
  {
    std::stringstream obuf;
    obuf << "\\begin{pspicture}"
	 << format::print(sz) << format::print(-offset) << "%" << std::endl;

    return obuf.str();
  }

  std::string pst::end_picture() const
  {
    std::stringstream obuf;
    obuf << "\\end{pspicture}%" << std::endl;
    return obuf.str();
  }

  std::string pst::set_unitlength(const std::string& len) const
  {
    std::stringstream obuf;

    obuf << "\\psset{unit=1" << len
	 << ",linewidth=" << print(length(PLAIN_WIDTH)) << "}%"
	 << std::endl;

    return obuf.str();
  }


  std::string pst::usepackages() const
  {
    return "usepackages pstricks";
  }

  std::string pst::start_open_path(const std::string& attribs) const
  {
    return "\\psline" + attribs;
  }

  std::string pst::end_open_path(const std::string& attribs) const
  {
    std::stringstream obuf;
    obuf << std::endl;
    return obuf.str();
  }

  std::string pst::start_closed_path(const std::string& attribs) const
  {
    return "\\pspolygon" + attribs;
  }

  std::string pst::end_closed_path(const std::string& attribs) const
  {
    std::stringstream obuf;
    obuf << std::endl;
    return obuf.str();
  }

  std::string pst::set_fill_state(const Color& col) const
  {
    if (m_fill == col)
      return "";

    // else
    m_fill = col;

    std::stringstream obuf;
    obuf << "\\psset{";

    // filled paths set fillstyle
    if (m_fill.is_unset())
      obuf << "fillstyle=none";

    else
      obuf << "fillcolor=" << m_fill.name();

    obuf << "}%" << std::endl;

    return obuf.str();
  }

  std::string pst::set_pen_state(const pen_data& pen) const
  {
    std::stringstream obuf;

    if (pen.color().is_unset())
      {
	m_lwidth=length(0);
	return "\\psset{linewidth=0pt}";
      }

    // else
    bool change_width(m_lwidth != pen.width());
    bool change_color(m_line != pen.color());

    if (change_width || change_color)
      {
	obuf << "\\psset{";

	if (change_width)
	  {
	    m_lwidth = pen.width();
	    obuf << "linewidth=" << m_lwidth.name();

	    if (change_color) // both true, need separator
	      obuf << ",";
	  }

	if (change_color)
	  {
	    m_line = pen.color();
	    obuf << "linecolor=" << m_line.name();
	  }

	obuf << "}%" << std::endl;
      }

    return obuf.str();
  }

  // place a LaTeX box of width zero (containing string) at location (pair)
  std::string pst::put_box(const pair& loc, const std::string& msg) const
  {
    std::stringstream obuf;
    obuf << "\\rput" << print(loc) << "{" << msg << "}" << std::endl;

    return obuf.str();
  }

  std::string pst::print_circle_marker(const pair& here, double diam,
				       bool fill, const Color& color,
				       const std::string& len) const
  {
    std::stringstream obuf;
    obuf << "\\pscircle";

    if (fill)
      obuf << "*";

    obuf << "[linecolor=" << print(color) << "]" << print(here)
	 << "{" << 0.5*diam << "}%" << std::endl;

    return obuf.str();
  }
} // end of namespace