summaryrefslogtreecommitdiff
path: root/graphics/epix/format.cc
blob: f799baa7a45679e193c7b130dc100432631f814c (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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
 * format.cc -- ePiX output format interface and label-writing functions
 *
 * This file is part of ePiX, a c++ library for creating high-quality
 * figures in LaTeX
 *
 * Version 1.2.0-2
 * Last Change: September 26, 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.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <list>

#include "enums.h"
#include "constants.h"

#include "errors.h"
#include "utils.h"

#include "functions.h"

#include "marker_style.h"
#include "label_style.h"
#include "path_style.h"
#include "Color.h"

#include "pen_data.h"

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

#include "length.h"

#include "picture.h" // circ may need to add White to pallet
#include "picture_data.h"

#include "format.h"

namespace ePiX {
  // public, non-virtual
  // LaTeX label-generating commands written in terms of protected interface
  std::string format::print_mark(const pair& here, const pair& offset,
				 const epix_mark_type mk,
				 const marker_sizes& sz,
				 const label_state& style,
				 const std::string& len) const
  {
    // convert dotsize to len
    length true_diam(sz.dot_size());
    double diam(true_diam.to(len).magnitude());

    // set diameter
    if (mk == DOT || mk == CIRC)
      diam *= 0.75;
    else if (mk == DDOT)
      diam *= 0.5;

    // and print
    if (mk == SPOT || mk == DOT || mk == DDOT)
      return print_circle_marker(here + pt_to_len(offset, len), diam, true,
				 style.text_color(), len);

    else if (mk == CIRC) // Draw filled dot, then ring
      {
	Color bk(style.mask_color());
	// use White for unset mask
	if (bk.is_unset())
	  {
	    bk = White();
	    the_picture().add_to_pallet(bk);
	  }

	return print_circle_marker(here + pt_to_len(offset, len), diam, true,
				   bk, len)
	  + print_circle_marker(here + pt_to_len(offset, len), diam, false,
				style.text_color(), len);
      }

    else if (mk == RING)
      return print_circle_marker(here + pt_to_len(offset, len), diam, false,
				 style.text_color(), len);

    else
      return put_box(here + pt_to_len(offset, len),
		     print_latex_label(style, print_latex_marker(mk, sz, len),
				       false)); // no mask or box
  }

  // public, non-virtual
  std::string format::print_text(const pair& here, const pair& offset,
				 const std::string& label,
				 const label_state& style,
				 const std::string& len) const
  {
    return put_box(here + pt_to_len(offset, len),
		   print_latex_label(style, label, true));
  }


  // public, non-virtual
  std::string format::print_line(const edge2d& e,
				 const pair& offset,
				 const pen_data& line,
				 const pen_data& base,
				 const path_state& style,
				 const std::string& len) const
  {
    if ((line.width() < length(EPIX_EPSILON))
	|| (line.color().is_unset())
	|| !e.is_seen() )
      return "";

    // else
    std::list<edge2d> edges;
    edges.push_back(e);
    return print_line(edges, offset, line, base, style, len);
  }


  // public, non-virtual (file header comment block)
  std::string format::file_comments(const std::string& units,
				    const length& hoffset,
				    const length& voffset,
				    double Xmin, double Xmax,
				    double Ymin, double Ymax,
				    double Hsize, double Vsize) const
  {
    std::ostringstream obuf;

    const std::string hoff_dir(length(0) < hoffset ? "right" : "left");
    const std::string voff_dir(length(0) < voffset ? "up" : "down");

    const double hoff(hoffset.magnitude());
    const double voff(voffset.magnitude());

    std::stringstream bd_box, act_sz, fg_off;
    bd_box << "  Cartesian bounding box: ["
	   << Xmin << "," << Xmax << "] x [" << Ymin << "," << Ymax<< "]";

    act_sz << "  Actual size: " << Hsize << " x " << Vsize << units;

    fg_off << "  Figure offset: "
	   << hoff_dir << " by " << hoff << units << ", "
	   << voff_dir << " by " << voff << units;

    obuf << print_comment("ePiX-" + epix_version())
	 << print_comment("")
	 << print_comment(bd_box.str())
	 << print_comment(act_sz.str())
	 << print_comment(fg_off.str())
	 << print_comment("")
	 << print_comment(usepackages())
	 << print_comment("");

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


  // public, virtual
  std::string format::pic_header(const pair& sizes,
				 const pair& offsets,
				 const std::set<Color>& pallet,
				 const std::string& len) const
  {
    std::stringstream obuf;
    obuf << print_pallet(pallet)
	 << set_unitlength(len)
	 << start_picture(sizes, offsets);

    return obuf.str();
  }

  // public, virtual
  std::string format::pic_footer() const
  {
    return end_picture();
  }

  // public, non-virtual
  std::string format::print_color(const std::string& model,
				  const std::string& name,
				  const std::vector<double>& dens) const
  {
    if (dens.size() == 3)
      return print_color(model, name, dens.at(0), dens.at(1), dens.at(2));

    else if (dens.size() == 4)
      return print_color(model, name,
			 dens.at(0), dens.at(1), dens.at(2), dens.at(3));

    else if (dens.size() == 0) // Neutral()
      return "";

    else
      {
	epix_warning("format::print_color(): Internal error");
	return print_color("rgb", "rgb_000000", 0, 0, 0);
      }
  }

  // protected, non-virtual (one-pen version)
  std::string format::print_line(const std::list<edge2d>& edges,
				 const pair& offset, const pen_data& line,
				 const path_state& style,
				 const std::string& attribs,
				 const std::string& len) const
  {
    if ( (line.width() < length(EPIX_EPSILON))
	 || (line.color().is_unset())
	 || edges.begin() == edges.end() )
      return "";

    // else
    std::stringstream value, obuf;

    obuf << set_pen_state(line);

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

    else // not solid
      {
	// compute diameter of dots
	length lw(line.width());
	lw.to(len);

	// compute unit length in pt
	length units("1"+len);
	units.to("pt");
	double in_pt(units.magnitude());

	double pattern_size(style.separation()); // in pt

	// split each edge into pattern_size chunks if necessary
	for (std::list<edge2d>::const_iterator ep = edges.begin();
	     ep != edges.end(); ++ep)
	  {
	    double curr_len(((*ep).length())*in_pt);

	    // number of dash patterns *ep can hold
	    unsigned int N((unsigned int) ceil(curr_len/pattern_size));
	    if (N == 1)
	      obuf << print_edge((*ep), offset, style.breakpts(),
				 lw.magnitude(), line.color(), attribs, len);

	    if (N > 1)
	      {
		pair T((*ep).tail()), dir((1.0/N)*((*ep).head() - T));
		for (unsigned int i=0; i<N; ++i)
		  obuf << print_edge(edge2d(T+i*dir, T+(i+1)*dir,
					    (*ep).is_seen()),
				     offset, style.breakpts(),
				     lw.magnitude(), line.color(),
				     attribs, len);
	      }
	  }
      } // end of !solid

    return obuf.str();
  } // end of print_line(edges, offset, line, style, attribs, len)


  std::string format::print_line(const edge2d& e,
				 const pair& offset,
				 const pen_data& line,
				 const path_state& style,
				 const std::string& attribs,
				 const std::string& len) const
  {
    if ( (line.width() < length(EPIX_EPSILON)) ||
         (line.color().is_unset()) ||
         !e.is_seen() )
      return "";

    std::list<edge2d> edges;
    edges.push_back(e);
    return print_line(edges, offset, line, style, attribs, len);
  }


  // local to this file
  std::string __epix_rule(double x, double y)
  {
    std::stringstream buf;
    buf << "\\rule{" << x << "pt}{" << y << "pt}";

    return buf.str();
  }


  // protected, non-virtual
  std::string format::xdefinecolor(const std::string& model,
				   const std::string& name,
				   double d1, double d2, double d3) const
  {
    std::stringstream obuf;
    obuf << "\\xdefinecolor{" << name << "}";

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

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

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

    return obuf.str();
  }

  // protected, non-virtual
  std::string format::xdefinecolor(const std::string& model,
				   const std::string& name,
				   double d1, double d2,
				   double d3, double d4) const
  {
    std::stringstream obuf;
    obuf << "\\xdefinecolor{" << name << "}{cmyk}{"
	 << d1 << "," << d2 << "," << d3 << "," << d4 << "}%"
	 << std::endl;

    return obuf.str();
  }

  // protected, virtual
  std::string format::print_pallet(const std::set<Color>& pallet) const
  {
    std::stringstream obuf;

    for (std::set<Color>::const_iterator cp=pallet.begin();
	 cp!=pallet.end(); ++cp)
      obuf << print_color((*cp).model(), (*cp).name(), (*cp).densities());

    return obuf.str();
  }

  // protected, virtual
  std::string format::print(const pair& arg) const
  {
    std::stringstream o;
    o << "(" << truncate(arg.x1()) << "," << truncate(arg.x2()) << ")";
    return o.str();
  }

  // protected, virtual
  std::string format::print(const length& len) const
  {
    return len.name();
  }

  // protected, virtual
  std::string format::print(const Color& col) const
  {
    return col.name();
  }

  // protected, non-virtual (print non-solid edge as segments and dots)
  std::string format::print_edge(const edge2d& edge, const pair& offset,
				 const std::vector<double> bkpts,
				 double diam, const Color& col,
				 const std::string& attribs,
				 const std::string& len) const
  {
    if (!edge.is_seen())
      return "";

    if (bkpts.size() % 2 != 0)
      {
        epix_warning("Internal error in path pattern");
        return "";
      }

    // else
    std::stringstream obuf;

    const pair T(edge.tail() + pt_to_len(offset, len));
    const pair dir(edge.head() - edge.tail());

    for (unsigned int i=0; i < bkpts.size(); i += 2)
      {
        double curr(bkpts.at(i)), next(bkpts.at(i+1));

        if (EPIX_EPSILON < next - curr) // dash
	  obuf << start_open_path(attribs)
	       << print(T + curr*dir)
	       << path_connector()
	       << print(T + next*dir)
	       << end_open_path(attribs);

        else // dot
	  obuf << print_circle_marker(T + curr*dir, diam, true, col, len);
      }

    return obuf.str();
  }


  // protected, non-virtual (solid path, handle (offset) data only)
  std::string format::print_paths(const std::list<edge2d>& edges,
				  const pair& offset,
				  const std::string& attribs,
				  const std::string& len) const
  {
    std::stringstream obuf;

    std::list<edge2d>::const_iterator ep(edges.begin());

    const pair shift(pt_to_len(offset, len));

    bool all_visible(true); // all edges visible?

    // Assume not for now
    const std::string pname(start_open_path(attribs));

    // print contiguous lists of visible edges
    while (ep != edges.end())
      {
	if ((*ep).is_seen()) // print contiguous visible path
	  {
	    obuf << pname
		 << print((*ep).tail() + shift)
		 << path_connector()
		 << print((*ep).head() + shift);
	    ++ep;

	    while ((*ep).is_seen() && ep != edges.end())
	      obuf << path_connector() << print((*ep++).head() + shift);

	    obuf << end_open_path(attribs);
	  }

	else // invisible edge
	  {
	    ++ep;
	    all_visible = false;
	  }
      }

    std::string value(obuf.str());

    --ep; // last edge

    // if we're a closed loop of visible edges, adjust open/close path
    if (all_visible && (*edges.begin()).tail() == (*ep).head())
      {
	value.replace(0,
		      start_open_path(attribs).size(),
		      start_closed_path(attribs));
    
	value.replace(value.size()-end_open_path(attribs).size(),
		      end_open_path(attribs).size(),
		      end_closed_path(attribs));
      }

    return break_lines(value, path_break());
  }


  //// Label and marker functions ////
  // protected, non-virtual (print LaTeX command for marker of specified size)
  std::string format::print_latex_marker(const epix_mark_type mark,
					 const marker_sizes& sz,
					 const std::string& len) const
  {
    std::stringstream obuf;

    obuf << "$";

    switch(mark)
      {
      case PLUS:
	obuf << "+";
	break;
      case OPLUS:
	obuf << "\\oplus";
	break;
      case TIMES:
	obuf << "\\times";
	break;
      case OTIMES:
	obuf << "\\otimes";
	break;
      case DIAMOND:
	obuf << "\\diamond";
	break;
      case UP:
	obuf << "\\bigtriangleup";
	break;
      case DOWN:
	obuf << "\\bigtriangledown";
	break;
      case BOX:
	obuf << __epix_rule(0.75*sz.dot_size(), 0.75*sz.dot_size());
	break;
      case BBOX:
	obuf << __epix_rule(0.5*sz.dot_size(), 0.5*sz.dot_size());
	break;
      case HTICK:
	obuf << __epix_rule(0.5, 2*sz.tick_size());
	break;
      case VTICK:
	obuf << __epix_rule(2*sz.tick_size(), 0.5);
	break;
	// should never see these cases
      case SPOT:
      case DOT:
      case DDOT:
      case CIRC:
      case RING:
	epix_warning("Mark type internal error"); // fall through
      case PATH: // fall through
      case TEXT:
      default:
	return ""; // N.B. return
      }

    obuf << "$";

    return obuf.str();
  }


  // public, non-virtual
  // handles font face, size, and color
  std::string format::print_latex_hbox(const label_state& style,
				       const std::string& label) const
  {
    std::stringstream text_buf;

    bool need_ff(style.fontface() != "rm");  // roman
    bool need_fs(style.fontsize() != "nsz"); // normalsize

    text_buf << "\\hbox{\\color{" << print(style.text_color()) << "}";

    if (need_ff)
      text_buf << "\\text" << style.fontface() << "{";

    if (need_fs)
      text_buf << "\\" << style.fontsize() << " ";

    text_buf << label;

    if (need_ff)
      text_buf << "}";

    text_buf << "}";

    return text_buf.str();
  }


  // protected, virtual
  std::string format::path_break() const
  {
    std::stringstream buf;
    buf << std::endl << "  ";

    return buf.str();
  }

  // inserted between points of a path
  std::string format::path_connector() const
  {
    return "";
  }

  // protected, non-virtual
  // handles rotation, masking, border, and alignment
  std::string format::print_latex_label(const label_state& style,
					const std::string& label,
					bool boxed) const
  {
    std::stringstream obuf;

    // need rotatebox?
    bool rotated(EPIX_EPSILON < fabs(style.label_angle()));

    // need colorbox?
    bool masked(boxed && style.is_masked());

    // need extra fcolorbox?
    bool padded(style.label_padding() != length(0));

    obuf << "\\makebox(0,0)"
	 << print(style.align_to()); // "none" -> ""

    obuf << "{";

    if (rotated)
      obuf << "\\rotatebox{" << style.label_angle() << "}{";

    if (masked)
      {
	pen_data bpen(style.label_border());

	// draw border?
	if (!bpen.color().is_unset() && length(EPIX_EPSILON) < bpen.width())
	  obuf << "\\fboxrule" << print(bpen.width()) << "%" << std::endl
	       << "\\fcolorbox{" << print(bpen.color()) << "}{"
	       << print(style.mask_color()) << "}";

	else
	  obuf << "\\colorbox{" << print(style.mask_color()) << "}";

	if (padded)
	  {
	    // cancel off the xcolor default
	    length extra_pad(style.label_padding() + length(-3));

	    obuf << "{\\fboxrule" << print(extra_pad) << "%" << std::endl
		 << "\\fcolorbox{" << print(style.mask_color()) << "}{"
		 << print(style.mask_color()) << "}";
	  }

	obuf << "{";
      }

    // finally, the payload
    obuf << print_latex_hbox(style, label);

    // close wrappers
    if (masked)
      {
	obuf << "}";

	if (padded)
	  obuf << "}";
      }

    if (rotated)
      obuf << "}";
  
    obuf << "}";

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

  // private (convert an epix_label_posn to a string)
  std::string format::print(const epix_label_posn arg) const
  {
    std::string val;
    switch(arg) 
      {
      case none:
	return ""; // other cases return at bottom

      case c:
	val="c";
	break;
      case r:
	val="l"; // opposite of LaTeX
	break;
      case tr:
      case rt:
	val="bl";
	break;
      case t:
	val="b";
	break;
      case tl:
      case lt:
	val="br";
	break;
      case l:
	val="r";
	break;
      case bl:
      case lb:
	val="tr";
	break;
      case b:
	val="t";
	break;
      case br:
      case rb:
	val="tl";
	break;
      }

    return "[" + val + "]";
  } // end of format::print(align_type)


  // private (true pt offset to len)
  pair format::pt_to_len(pair offset, const std::string& len) const
  {
    return offset *= length("1pt").to(len).magnitude();
  }
} // end of namespace