]> git.lyx.org Git - lyx.git/blob - src/lyxlength.h
e45d0f4a110680311d68907b652d4d4d21d25d0a
[lyx.git] / src / lyxlength.h
1 // -*- C++ -*-
2 /**
3  * \file lyxlength.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 <string>
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  * LyXLength - Represents latex length measurement
30  */
31 class LyXLength {
32 public:
33         /// length units
34         enum UNIT {
35                 SP, ///< Scaled point (65536sp = 1pt) TeX's smallest unit.
36                 PT, ///< Point = 1/72.27in = 0.351mm
37                 BP, ///< Big point (72bp = 1in), also PostScript point
38                 DD, ///< Didot point = 1/72 of a French inch, = 0.376mm
39                 MM, ///< Millimeter = 2.845pt
40                 PC, ///< Pica = 12pt = 4.218mm
41                 CC, ///< Cicero = 12dd = 4.531mm
42                 CM, ///< Centimeter = 10mm = 2.371pc
43                 IN, ///< Inch = 25.4mm = 72.27pt = 6.022pc
44                 EX, ///< Height of a small "x" for the current font.
45                 EM, ///< Width of capital "M" in current font.
46                 MU, ///< Math unit (18mu = 1em) for positioning in math mode
47                 PTW, //< Percent of TextWidth
48                 PCW, //< Percent of ColumnWidth
49                 PPW, //< Percent of PageWidth
50                 PLW, //< Percent of LineWidth
51                 PTH, //< Percent of TextHeight          // Herbert 2002-05-16
52                 PPH, //< Percent of PaperHeight         // Herbert 2002-05-16
53                 UNIT_NONE ///< no unit
54         };
55
56         ///
57         LyXLength();
58         ///
59         LyXLength(double v, LyXLength::UNIT u);
60
61         /// "data" must be a decimal number, followed by a unit
62         explicit LyXLength(std::string const & data);
63
64         void swap(LyXLength & rhs)
65         {
66                 std::swap(val_, rhs.val_);
67                 std::swap(unit_, rhs.unit_);
68         }
69
70         ///
71         double value() const;
72         ///
73         LyXLength::UNIT unit() const;
74         ///
75         void value(double);
76         ///
77         void unit(LyXLength::UNIT unit);
78         ///
79         bool zero() const;
80         ///
81         bool empty() const;
82         /// return string representation
83         std::string const asString() const;
84         /// return string representation for LaTeX
85         std::string const asLatexString() const;
86         /// return the on-screen size of this length
87         int inPixels(int text_width, int em_width = 0) const;
88         /// return the on-screen size of this length of an image
89         int inBP() const;
90
91         friend bool isValidLength(std::string const & data, LyXLength * result);
92
93 private:
94         ///
95         double          val_;
96         ///
97         LyXLength::UNIT unit_;
98 };
99
100 ///
101 bool operator==(LyXLength const & l1, LyXLength const & l2);
102 ///
103 bool operator!=(LyXLength const & l1, LyXLength const & l2);
104 /** Test whether \p data represents a valid length.
105  * 
106  * \returns whether \p data is a valid length
107  * \param data Length in LyX format. Since the only difference between LyX
108  * and LaTeX format is the representation of length variables as units (e.g.
109  * \c text% vs. \c \\textwidth) you can actually use this function as well
110  * for testing LaTeX lengths as long as they only contain real units like pt.
111  * \param result Pointer to a LyXLength variable. If \p result is not 0 and
112  * \p data is valid, the length represented by it is stored into \p result.
113  */
114 bool isValidLength(std::string const & data, LyXLength * result = 0);
115 /// return the LyX name of the given unit number
116 char const * stringFromUnit(int unit);
117
118
119 } // namespace lyx
120
121 #endif // LYXLENGTH_H