]> git.lyx.org Git - lyx.git/blob - src/support/Length.h
Let paragraph::requestSpellcheck() consider contained insets
[lyx.git] / src / support / 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 LENGTH_H
16 #define LENGTH_H
17
18 #include "support/strfwd.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                 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                 BLS, //< Percent of BaselineSkip        // uwestoehr 2017-04-01
60                 UNIT_NONE ///< no unit
61         };
62
63         ///
64         Length() = default;
65         ///
66         Length(double v, Length::UNIT u) : val_(v), unit_(u) {}
67
68         /// "data" must be a decimal number, followed by a unit
69         explicit Length(std::string const & data);
70
71         ///
72         double value() const { return val_; }
73         ///
74         Length::UNIT unit() const { return unit_; }
75         ///
76         void value(double val) { val_ = val; }
77         ///
78         void unit(Length::UNIT unit) { unit_ = unit; }
79         ///
80         bool zero() const { return val_ == 0.0; }
81         ///
82         bool empty() const { return unit_ == Length::UNIT_NONE; }
83         /// return string representation
84         std::string const asString() const;
85         /// return string representation
86         docstring const asDocstring() const;
87         /// return string representation for LaTeX
88         std::string const asLatexString() const;
89         /// return string representation for HTML
90         std::string const asHTMLString() const;
91         /** return the on-screen size of this length.
92          *
93          *      If the second argument is not provided, then the unit EM will
94          *      only be approximated. It is better if possible to use
95          *      FontMetrics::em() to get this value.
96          */
97         int inPixels(int text_width, int em_width = 0) const;
98
99         /// return the value in Big Postscript points.
100         /// Caution: Inaccurate for em, ex, mu and percent units.
101         int inBP() const;
102
103         /// return the default unit (centimeter or inch)
104         static UNIT defaultUnit();
105
106         friend bool isValidLength(std::string const & data, Length * result);
107
108 private:
109         /// Convert value to inch for text width and em width given in inch
110         double inInch(double text_width, double em_width) const;
111         ///
112         double val_ = 0;
113         ///
114         Length::UNIT unit_ = UNIT_NONE;
115 };
116
117 ///
118 inline bool operator==(Length const & l1, Length const & l2)
119         { return l1.value() == l2.value() && l1.unit() == l2.unit(); }
120 ///
121 inline bool operator!=(Length const & l1, Length const & l2)
122         { return !(l1 == l2); }
123
124 /** Test whether \p data represents a valid length.
125  *
126  * \returns whether \p data is a valid length
127  * \param data Length in LyX format. Since the only difference between LyX
128  * and LaTeX format is the representation of length variables as units (e.g.
129  * \c text% vs. \c \\textwidth) you can actually use this function as well
130  * for testing LaTeX lengths as long as they only contain real units like pt.
131  * \param result Pointer to a Length variable. If \p result is not 0 and
132  * \p data is valid, the length represented by it is stored into \p result.
133  */
134 bool isValidLength(std::string const & data, Length * result = 0);
135 /// return the LyX name of the given unit number
136 char const * stringFromUnit(int unit);
137
138
139 /////////////////////////////////////////////////////////////////////
140 //
141 // GlueLength
142 //
143 /////////////////////////////////////////////////////////////////////
144
145 class GlueLength {
146 public:
147         ///
148         GlueLength() = default;
149         ///
150         explicit GlueLength(Length const & len) : len_(len) {}
151         ///
152         GlueLength(Length const & len, Length const & plus, Length const & minus)
153                  : len_(len), plus_(plus), minus_(minus) {}
154
155         /** "data" must be a decimal number, followed by a unit, and
156           optional "glue" indicated by "+" and "-".  You may abbreviate
157           reasonably.  Examples:
158           1.2 cm  //  4mm +2pt  //  2cm -4mm +2mm  //  4+0.1-0.2cm
159           The traditional Latex format is also accepted, like
160           4cm plus 10pt minus 10pt */
161         explicit GlueLength(std::string const & data);
162
163         ///
164         Length const & len() const { return len_; }
165         ///
166         Length const & plus() const { return plus_; }
167         ///
168         Length const & minus() const { return minus_; }
169
170
171         /// conversion
172         std::string const asString() const;
173         ///
174         std::string const asLatexString() const;
175
176         friend bool isValidGlueLength(std::string const & data,
177                                       GlueLength* result);
178
179 private:
180         /// the normal vlaue
181         Length len_;
182         /// extra stretch
183         Length plus_;
184         /// extra shrink
185         Length minus_;
186 };
187
188 ///
189 bool operator==(GlueLength const & l1, GlueLength const & l2);
190 ///
191 inline bool operator!=(GlueLength const & l1, GlueLength const & l2)
192         { return !(l1 == l2); }
193
194 /** If "data" is valid, the length represented by it is
195     stored into "result", if that is not 0. */
196 bool isValidGlueLength(std::string const & data, GlueLength * result = 0);
197
198 /// the number of units possible
199 extern int const num_units;
200
201 /**
202  * array of unit names
203  *
204  * FIXME: I am not sure if "mu" should be possible to select (Lgb)
205  */
206 extern char const * const unit_name[];
207 extern char const * const unit_name_gui[];
208
209 /// return the unit given a string representation such as "cm"
210 Length::UNIT unitFromString(std::string const & data);
211
212
213 } // namespace lyx
214
215 #endif // LENGTH_H