summaryrefslogtreecommitdiff
path: root/graphics/epix/path_style.cc
blob: 43b93681a4e95cec3e1c488f0300876e46e34026 (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
/* 
 * path_style.cc -- ePiX's class for path styles.
 *
 * This file is part of ePiX, a C++ library for creating high-quality 
 * figures in LaTeX 
 *
 * Version 1.1.18
 * Last Change: September 15, 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 <vector>

#include "constants.h"

#include "functions.h" // for snip_to
#include "utils.h" // for get_chars
#include "path_style.h"

namespace ePiX {

  // true if arg is a string of ch
  static bool __epix_all_one_char(const std::string& arg, const char ch)
  {
    return arg.find_first_not_of(ch) == std::string::npos;
  }

  // convert string of dashes, spaces, and periods to a vector of breakpoints
  static std::vector<double> __epix_get_breakpts(const std::string& pattern)
  {
    std::vector<double> value;

    // Assume solid on anomalous input
    if (pattern.size() == 0
	|| __epix_all_one_char(pattern, '-')
	|| __epix_all_one_char(pattern, ' '))
      {
	value.push_back(0);
	value.push_back(1);

	return value;
      }

    // else
    const unsigned int sz(pattern.size()); // not zero
    const double dt(1.0/sz);

    for (unsigned int i=0; i<sz; ++i)
      {
	if (pattern.at(i) == '-')
	  {
	    // previous element was a dash; extend instead of adding breaks
	    if (0 < value.size()
		&& fabs(value.at(value.size()-1) - i*dt) < 0.25*dt)
	      value.at(value.size()-1) = (i+1)*dt;

	    else
	      {
		value.push_back(i*dt);
		value.push_back((i+1)*dt);
	      }
	  }

	else if (pattern.at(i) == '.')
	  {
	    value.push_back((i+0.5)*dt);
	    value.push_back((i+0.5)*dt); // same value twice
	  }
	// spaces have no breakpoints, just increment i
      }

    return value;
  } // end of __epix_get_breakpts


  path_state::path_state()
    : m_separation(AVG_SEP), m_natural_length(1), m_solid(true)
  {
    m_breakpoints.push_back(0);
    m_breakpoints.push_back(1);
  }

  path_state::path_state(std::string arg)
    : m_separation(AVG_SEP), m_solid(false)
  {
    // strip out everything but dash, space, period.
    const std::string pattern(get_chars(arg, "- ."));
    m_breakpoints = __epix_get_breakpts(pattern); // ok even if pattern=""

    m_natural_length = pattern.size();

    if (m_breakpoints.at(0) == 0 && m_breakpoints.at(1) == 1)
      {
	m_solid = true;
	m_natural_length = 1;
      }
  }

  path_state& path_state::separation(double len)
  {
    if (0 < len)
      m_separation = snip_to(len, MIN_SEP, MAX_SEP);

    else
      m_separation = AVG_SEP;

    return *this;
  }

  double path_state::separation() const
  {
    return m_separation;
  }

  unsigned int path_state::natural_length() const
  {
    return m_natural_length;
  }

  std::vector<double> path_state::breakpts() const
  {
    return m_breakpoints;
  }

  bool path_state::is_solid() const
  {
    return m_solid;
  }


  path_state& the_path_style()
  {
    static path_state* the_path_state(new path_state());
    return *the_path_state;
  }
} // end of namespace