]> git.lyx.org Git - lyx.git/blob - src/Length.h
start using FileName::exists()
[lyx.git] / src / Length.h
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.
6  *
7  * \author Matthias Ettrich
8  * \author Lars Gullik Bjønnes
9  * \author Jean-Marc Lasgouttes
10  * \author John Levon
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef LYX_LENGTH_H
16 #define LYX_LENGTH_H
17
18 #include "support/docstring.h"
19
20
21 namespace lyx {
22
23 // Solaris/x86 version 9 and earlier define these
24 #undef PC
25 #undef SP
26
27 /////////////////////////////////////////////////////////////////////
28 //
29 // Length
30 //
31 /////////////////////////////////////////////////////////////////////
32
33
34 /**
35  * Length - Represents latex length measurement
36  */
37 class Length {
38 public:
39         /// length units
40         enum UNIT {
41                 SP, ///< Scaled point (65536sp = 1pt) TeX's smallest unit.
42                 PT, ///< Point = 1/72.27in = 0.351mm
43                 BP, ///< Big point (72bp = 1in), also PostScript point
44                 DD, ///< Didot point = 1/72 of a French inch, = 0.376mm
45                 MM, ///< Millimeter = 2.845pt
46                 PC, ///< Pica = 12pt = 4.218mm
47                 CC, ///< Cicero = 12dd = 4.531mm
48                 CM, ///< Centimeter = 10mm = 2.371pc
49                 IN, ///< Inch = 25.4mm = 72.27pt = 6.022pc
50                 EX, ///< Height of a small "x" for the current font.
51                 EM, ///< Width of capital "M" in current font.
52                 MU, ///< Math unit (18mu = 1em) for positioning in math mode
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
60         };
61
62         ///
63         Length();
64         ///
65         Length(double v, Length::UNIT u);
66
67         /// "data" must be a decimal number, followed by a unit
68         explicit Length(std::string const & data);
69
70         void swap(Length & rhs)
71         {
72                 std::swap(val_, rhs.val_);
73                 std::swap(unit_, rhs.unit_);
74         }
75
76         ///
77         double value() const;
78         ///
79         Length::UNIT unit() const;
80         ///
81         void value(double);
82         ///
83         void unit(Length::UNIT unit);
84         ///
85         bool zero() const;
86         ///
87         bool empty() const;
88         /// return string representation
89         std::string const asString() const;
90         /// return string representation
91         docstring const asDocstring() const;
92         /// return string representation for LaTeX
93         std::string const asLatexString() const;
94         /// return the on-screen size of this length
95         int inPixels(int text_width, int em_width = 0) const;
96         /// return the on-screen size of this length of an image
97         int inBP() const;
98
99         friend bool isValidLength(std::string const & data, Length * result);
100
101 private:
102         ///
103         double          val_;
104         ///
105         Length::UNIT unit_;
106 };
107
108 ///
109 bool operator==(Length const & l1, Length const & l2);
110 ///
111 bool operator!=(Length const & l1, Length const & l2);
112 /** Test whether \p data represents a valid length.
113  *
114  * \returns whether \p data is a valid length
115  * \param data Length in LyX format. Since the only difference between LyX
116  * and LaTeX format is the representation of length variables as units (e.g.
117  * \c text% vs. \c \\textwidth) you can actually use this function as well
118  * for testing LaTeX lengths as long as they only contain real units like pt.
119  * \param result Pointer to a Length variable. If \p result is not 0 and
120  * \p data is valid, the length represented by it is stored into \p result.
121  */
122 bool isValidLength(std::string const & data, Length * result = 0);
123 /// return the LyX name of the given unit number
124 char const * stringFromUnit(int unit);
125
126
127 /////////////////////////////////////////////////////////////////////
128 //
129 // GlueLength
130 //
131 /////////////////////////////////////////////////////////////////////
132
133 class GlueLength {
134 public:
135         ///
136         GlueLength() {}
137         ///
138         explicit GlueLength(Length const & len);
139         ///
140         GlueLength(Length const & len,
141                       Length const & plus,
142                       Length const & minus);
143
144         /** "data" must be a decimal number, followed by a unit, and
145           optional "glue" indicated by "+" and "-".  You may abbreviate
146           reasonably.  Examples:
147           1.2 cm  //  4mm +2pt  //  2cm -4mm +2mm  //  4+0.1-0.2cm
148           The traditional Latex format is also accepted, like
149           4cm plus 10pt minus 10pt */
150         explicit GlueLength(std::string const & data);
151
152         ///
153         Length const & len() const;
154         ///
155         Length const & plus() const;
156         ///
157         Length const & minus() const;
158
159
160         /// conversion
161         std::string const asString() const;
162         ///
163         std::string const asLatexString() const;
164
165         friend bool isValidGlueLength(std::string const & data,
166                                       GlueLength* result);
167
168 private:
169         /// the normal vlaue
170         Length len_;
171         /// extra stretch
172         Length plus_;
173         /// extra shrink
174         Length minus_;
175 };
176
177 ///
178 bool operator==(GlueLength const & l1, GlueLength const & l2);
179 ///
180 bool operator!=(GlueLength const & l1, GlueLength const & l2);
181 /** If "data" is valid, the length represented by it is
182     stored into "result", if that is not 0. */
183 bool isValidGlueLength(std::string const & data, GlueLength * result = 0);
184
185 } // namespace lyx
186
187 #endif // LYXLENGTH_H