summaryrefslogtreecommitdiff
path: root/graphics/epix/length.cc
blob: 431874ee92901e063256a42f0b0df033876b1ddf (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
/*
 * length.cc -- the ePiX::length class
 *
 * 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 "errors.h"
#include "constants.h"
#include "length.h"

namespace ePiX {

  // Inviolable "private" constants: number of pts per unit
  const double IN(72.27);               // the gold standard
  const double CM(28.452755905512);     // 2.54 cm = 72.27 pt
  const double MM(2.8452755905512);     // 25.4 mm = 72.27 pt
  const double PC(6.0225);              // 12 picas = 1 inch
  const double BP(1.00375);             // 72 big pt = 72.27 pt
  // const double SP(0.0000152587890625);  // 65536 scaled pt = 1 pt

  // extract length from "double [spaces] length unit"
  length::length(std::string arg)
  {
    char* tmp_units;
    m_mag = strtod(arg.c_str(), &tmp_units);
    std::string putative_units(tmp_units);
      
    // skip spaces, get first two non-space characters
    std::string::size_type i=putative_units.find_first_not_of(" ");

    if (i == std::string::npos)
      {
	epix_warning("Missing units, using pt");
	m_units = "pt";
      }
    else
      m_units = putative_units.substr(i, 2);

    // See what we found; ugly, but not going to change...
    if (m_units == "pt")
      pts_per_unit = 1.0;

    else if (m_units == "in")
      pts_per_unit = IN;

    else if (m_units == "cm")
      pts_per_unit = CM;

    else if (m_units == "mm")
      pts_per_unit = MM;

    else if (m_units == "pc")
      pts_per_unit = PC;

    else if (m_units == "bp")
      pts_per_unit = BP;
    /*
    else if (m_units == "sp")
      pts_per_unit = SP;
    */
    else
      {
	std::string warn="Unrecognized units \""+m_units+"\", assuming pt";
        epix_warning(warn);
	pts_per_unit = 1.0;
	m_units = "pt";
      }
  } // end of string constructor

  length::length(double lgth)
    : m_mag(lgth), pts_per_unit(1.0), m_units("pt") { }

  length& length::operator+= (const length& increment)
  {
    m_mag += increment.m_mag*increment.pts_per_unit/pts_per_unit;
    return *this;
  }

  length& length::operator*= (double scale)
  {
    m_mag *= scale;
    return *this;
  }

  // convert to specified units
  length& length::to(std::string units)
  {
    units = units.substr(0,2);

    if (m_units == units)
      return *this; // null conversion

    if (units == "pt")
      {
	m_mag *= pts_per_unit;
	pts_per_unit = 1.0;
	m_units = units;
      }

    else if (units == "in")
      {
	m_mag *= pts_per_unit/IN;
	pts_per_unit = IN;
	m_units = units;
      }

    else if (units == "cm")
      {
	m_mag *= pts_per_unit/CM;
	pts_per_unit = CM;
	m_units = units;
      }

    else if (units == "mm")
      {
	m_mag *= pts_per_unit/MM;
	pts_per_unit = MM;
	m_units = units;
      }

    else if (units == "pc")
      {
	m_mag *= pts_per_unit/PC;
	pts_per_unit = PC;
	m_units = units;
      }

    else if (units == "bp")
      {
	m_mag *= pts_per_unit/BP;
	pts_per_unit = BP;
	m_units = units;
      }

    /*
    else if (units == "sp")
      {
	m_mag *= pts_per_unit/SP;
	pts_per_unit = SP;
	m_units = units;
      }
    */
    else
      epix_warning("Ignoring conversion to unrecognized units \""+units+"\"");

    return *this;
  }

  bool length::operator== (const length& L) const
  {
    return (fabs(m_mag*pts_per_unit - L.m_mag*L.pts_per_unit) < EPIX_EPSILON);
  }

  double length::magnitude() const
  {
    return m_mag;
  }

  std::string length::units() const
  {
    return m_units;
  }

  std::string length::name() const
  {
    std::stringstream buf;
    buf << m_mag << m_units;
    return buf.str();
  }


  // value-returning operators
  length operator+ (length len1, const length& len2)
  {
    return len1 += len2;
  }

  length operator* (double sc, length len)
  {
    return len *= sc;
  }


  bool operator< (length arg1, length arg2)
  {
    return (arg1.to("pt").magnitude()) < (arg2.to("pt").magnitude());
  }

  // Parse a string into two lengths.
  void get_lengths(std::string sz, length& length1, length& length2)
  {
    std::string units1;
    std::string units2;

    double len1(0), len2(0);

    // find first digit
    std::string::size_type i=sz.find_first_of("-0123456789.");

    if (i == std::string::npos)
      epix_warning("Invalid width in size string");

    else
      {
	sz.erase(0, i); // remove initial garbage
	const char* tmp = sz.c_str();
	char* arg;
	len1 = strtod(tmp, &arg);
	sz=arg;
      }

    // expect either unitlength or multiplication character
    i=sz.find_first_not_of(" ");
    std::string::size_type j=sz.find_first_of("Xx*");
    if (i != j)
      units1 = sz.substr(i, 2);

    // next digit
    i=sz.find_first_of("-0123456789.");

    if (i == std::string::npos)
      epix_warning("Invalid height in size string");

    else
      {
	sz.erase(0, i);
	const char* tmp = sz.c_str();
	char* arg;
	len2 = strtod(tmp, &arg);
	sz=arg;
	i=sz.find_first_not_of(" ");
	units2 = sz.substr(i, 2);
      }

    if (units1 == "") // e.g. sz = "4x6in"
      units1 = units2;

    // assign values
    std::stringstream buf1;
    buf1 << len1 << units1;
    length1=length(buf1.str());

    std::stringstream buf2;
    buf2 << len2 << units2;
    length2=length(buf2.str());
  }
} // end of namespace