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