Another minor change, but this should almost get us to the point that we
[lyx.git] / src / Length.h
blob470c7e1a5382b73238f5f0a2785c7adfceba678d
1 // -*- C++ -*-
2 /**
3 * \file Length.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Matthias Ettrich
8 * \author Lars Gullik Bjønnes
9 * \author Jean-Marc Lasgouttes
10 * \author John Levon
12 * Full author contact details are available in file CREDITS.
15 #ifndef LENGTH_H
16 #define LENGTH_H
18 #include "support/strfwd.h"
21 namespace lyx {
23 // Solaris/x86 version 9 and earlier define these
24 #undef PC
25 #undef SP
27 /////////////////////////////////////////////////////////////////////
29 // Length
31 /////////////////////////////////////////////////////////////////////
34 /**
35 * Length - Represents latex length measurement
37 class Length {
38 public:
39 /// length units
40 enum UNIT {
41 BP, ///< Big point (72bp = 1in), also PostScript point
42 CC, ///< Cicero = 12dd = 4.531mm
43 CM, ///< Centimeter = 10mm = 2.371pc
44 DD, ///< Didot point = 1/72 of a French inch, = 0.376mm
45 EM, ///< Width of capital "M" in current font.
46 EX, ///< Height of a small "x" for the current font.
47 IN, ///< Inch = 25.4mm = 72.27pt = 6.022pc
48 MM, ///< Millimeter = 2.845pt
49 MU, ///< Math unit (18mu = 1em) for positioning in math mode
50 PC, ///< Pica = 12pt = 4.218mm
51 PT, ///< Point = 1/72.27in = 0.351mm
52 SP, ///< Scaled point (65536sp = 1pt) TeX's smallest unit.
53 PTW, //< Percent of TextWidth
54 PCW, //< Percent of ColumnWidth
55 PPW, //< Percent of PageWidth
56 PLW, //< Percent of LineWidth
57 PTH, //< Percent of TextHeight // Herbert 2002-05-16
58 PPH, //< Percent of PaperHeight // Herbert 2002-05-16
59 UNIT_NONE ///< no unit
62 ///
63 Length();
64 ///
65 Length(double v, Length::UNIT u);
67 /// "data" must be a decimal number, followed by a unit
68 explicit Length(std::string const & data);
70 ///
71 void swap(Length & rhs);
72 ///
73 double value() const;
74 ///
75 Length::UNIT unit() const;
76 ///
77 void value(double);
78 ///
79 void unit(Length::UNIT unit);
80 ///
81 bool zero() const;
82 ///
83 bool empty() const;
84 /// return string representation
85 std::string const asString() const;
86 /// return string representation
87 docstring const asDocstring() const;
88 /// return string representation for LaTeX
89 std::string const asLatexString() const;
90 /// return string representation for HTML
91 std::string const asHTMLString() const;
92 /// return the on-screen size of this length
93 int inPixels(int text_width, int em_width = 0) const;
94 /// return the value in Big Postscript points.
95 int inBP() const;
97 /// return the default unit (centimeter or inch)
98 static UNIT defaultUnit();
100 friend bool isValidLength(std::string const & data, Length * result);
102 private:
104 double val_;
106 Length::UNIT unit_;
110 bool operator==(Length const & l1, Length const & l2);
112 bool operator!=(Length const & l1, Length const & l2);
113 /** Test whether \p data represents a valid length.
115 * \returns whether \p data is a valid length
116 * \param data Length in LyX format. Since the only difference between LyX
117 * and LaTeX format is the representation of length variables as units (e.g.
118 * \c text% vs. \c \\textwidth) you can actually use this function as well
119 * for testing LaTeX lengths as long as they only contain real units like pt.
120 * \param result Pointer to a Length variable. If \p result is not 0 and
121 * \p data is valid, the length represented by it is stored into \p result.
123 bool isValidLength(std::string const & data, Length * result = 0);
124 /// return the LyX name of the given unit number
125 char const * stringFromUnit(int unit);
128 /////////////////////////////////////////////////////////////////////
130 // GlueLength
132 /////////////////////////////////////////////////////////////////////
134 class GlueLength {
135 public:
137 GlueLength() {}
139 explicit GlueLength(Length const & len);
141 GlueLength(Length const & len,
142 Length const & plus,
143 Length const & minus);
145 /** "data" must be a decimal number, followed by a unit, and
146 optional "glue" indicated by "+" and "-". You may abbreviate
147 reasonably. Examples:
148 1.2 cm // 4mm +2pt // 2cm -4mm +2mm // 4+0.1-0.2cm
149 The traditional Latex format is also accepted, like
150 4cm plus 10pt minus 10pt */
151 explicit GlueLength(std::string const & data);
154 Length const & len() const;
156 Length const & plus() const;
158 Length const & minus() const;
161 /// conversion
162 std::string const asString() const;
164 std::string const asLatexString() const;
166 friend bool isValidGlueLength(std::string const & data,
167 GlueLength* result);
169 private:
170 /// the normal vlaue
171 Length len_;
172 /// extra stretch
173 Length plus_;
174 /// extra shrink
175 Length minus_;
179 bool operator==(GlueLength const & l1, GlueLength const & l2);
181 bool operator!=(GlueLength const & l1, GlueLength const & l2);
182 /** If "data" is valid, the length represented by it is
183 stored into "result", if that is not 0. */
184 bool isValidGlueLength(std::string const & data, GlueLength * result = 0);
186 /// the number of units possible
187 extern int const num_units;
190 * array of unit names
192 * FIXME: I am not sure if "mu" should be possible to select (Lgb)
194 extern char const * const unit_name[];
195 extern char const * const unit_name_gui[];
197 /// return the unit given a string representation such as "cm"
198 Length::UNIT unitFromString(std::string const & data);
201 } // namespace lyx
203 #endif // LENGTH_H