]> git.lyx.org Git - lyx.git/blob - src/lyxlength.C
typos
[lyx.git] / src / lyxlength.C
1 /**
2  * \file lyxlength.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Matthias Ettrich
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author Dekel Tsur
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "lyxlength.h"
19 #include "lengthcommon.h"
20 #include "lyxrc.h"
21
22 #include "support/docstream.h"
23 #include <sstream>
24 #include <iomanip>
25
26
27 namespace lyx {
28
29
30 using std::ostringstream;
31 using std::string;
32
33
34 LyXLength::LyXLength()
35         : val_(0), unit_(LyXLength::UNIT_NONE)
36 {}
37
38
39 LyXLength::LyXLength(double v, LyXLength::UNIT u)
40         : val_(v), unit_(u)
41 {}
42
43
44 LyXLength::LyXLength(string const & data)
45         : val_(0), unit_(LyXLength::PT)
46 {
47         LyXLength tmp;
48
49         if (!isValidLength(data, &tmp))
50                 return; // should raise an exception
51
52         val_  = tmp.val_;
53         unit_ = tmp.unit_;
54 }
55
56
57 string const LyXLength::asString() const
58 {
59         ostringstream os;
60         os << val_ << unit_name[unit_]; // setw?
61         return os.str();
62 }
63
64
65 docstring const LyXLength::asDocstring() const
66 {
67         odocstringstream os;
68         os << val_ << unit_name[unit_]; // setw?
69         return os.str();
70 }
71
72
73 string const LyXLength::asLatexString() const
74 {
75         ostringstream os;
76         switch (unit_) {
77         case PTW:
78                 os << val_ / 100.0 << "\\textwidth";
79                 break;
80         case PCW:
81                 os << val_ / 100.0 << "\\columnwidth";
82                 break;
83         case PPW:
84                 os << val_ / 100.0 << "\\paperwidth";
85                 break;
86         case PLW:
87                 os << val_ / 100.0 << "\\linewidth";
88                 break;
89         case PPH:
90                 os << val_ / 100.0 << "\\paperheight";
91                 break;
92         case PTH:
93                 os << val_ / 100.0 << "\\textheight";
94                 break;
95         default:
96                 os << val_ << unit_name[unit_];
97           break;
98         }
99         return os.str();
100 }
101
102
103 double LyXLength::value() const
104 {
105         return val_;
106 }
107
108
109 LyXLength::UNIT LyXLength::unit() const
110 {
111         return unit_;
112 }
113
114
115 void LyXLength::value(double v)
116 {
117         val_ = v;
118 }
119
120
121 void LyXLength::unit(LyXLength::UNIT u)
122 {
123         unit_ = u;
124 }
125
126
127 bool LyXLength::zero() const
128 {
129         return val_ == 0.0;
130 }
131
132
133 bool LyXLength::empty() const
134 {
135         return unit_ == LyXLength::UNIT_NONE;
136 }
137
138
139 int LyXLength::inPixels(int text_width, int em_width_base) const
140 {
141         // Zoom factor specified by user in percent
142         double const zoom = lyxrc.zoom / 100.0; // [percent]
143
144         // DPI setting for monitor: pixels/inch
145         double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
146
147         double const em_width = (em_width_base > 0)
148                 ? em_width_base
149                 : 10*(dpi/72.27)*zoom;
150         // A different estimate for em_width is
151         // theFontMetrics(LyXFont(LyXFont::ALL_SANE)).width('M')
152         // but this estimate might not be more accurate as the screen font
153         // is different then the latex font.
154
155         // Pixel values are scaled so that the ratio
156         // between lengths and font sizes on the screen
157         // is the same as on paper.
158
159         double result = 0.0;
160
161         switch (unit_) {
162         case LyXLength::SP:
163                 // Scaled point: sp = 1/65536 pt
164                 result = zoom * dpi * val_
165                         / (72.27 * 65536); // 4736286.7
166                 break;
167         case LyXLength::PT:
168                 // Point: 1 pt = 1/72.27 inch
169                 result = zoom * dpi * val_
170                         / 72.27; // 72.27
171                 break;
172         case LyXLength::BP:
173                 // Big point: 1 bp = 1/72 inch
174                 result = zoom * dpi * val_
175                         / 72; // 72
176                 break;
177         case LyXLength::DD:
178                 // Didot: 1157dd = 1238 pt?
179                 result = zoom * dpi * val_
180                         / (72.27 / (0.376 * 2.845)); // 67.559735
181                 break;
182         case LyXLength::MM:
183                 // Millimeter: 1 mm = 1/25.4 inch
184                 result = zoom * dpi * val_
185                         / 25.4; // 25.4
186                 break;
187         case LyXLength::PC:
188                 // Pica: 1 pc = 12 pt
189                 result = zoom * dpi * val_
190                         / (72.27 / 12); // 6.0225
191                 break;
192         case LyXLength::CC:
193                 // Cicero: 1 cc = 12 dd
194                 result = zoom * dpi * val_
195                         / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
196                 break;
197         case LyXLength::CM:
198                 // Centimeter: 1 cm = 1/2.54 inch
199                 result = zoom * dpi * val_
200                         / 2.54; // 2.54
201                 break;
202         case LyXLength::IN:
203                 // Inch
204                 result = zoom * dpi * val_;
205                 break;
206         case LyXLength::EX:
207                 // Ex: The height of an "x"
208                 // 0.4305 is the ration between 1ex and 1em in cmr10
209                 result = val_ * em_width * 0.4305;
210                 break;
211         case LyXLength::EM:
212                 // Em: The width of an "m"
213                 result = val_ * em_width;
214                 break;
215         case LyXLength::MU:
216                 // math unit = 1/18em
217                 result = val_ * em_width / 18;
218                 break;
219         case LyXLength::PCW: // Always % of workarea
220         case LyXLength::PTW:
221         case LyXLength::PLW:
222                 result = val_ * text_width / 100;
223                 break;
224         case LyXLength::PPW:
225                 // paperwidth/textwidth is 1.7 for A4 paper with default margins
226                 result = val_ * text_width * 1.7 / 100;
227                 break;
228         case LyXLength::PTH:
229                 result = val_ * text_width * 1.787 / 100;
230                 break;
231         case LyXLength::PPH:
232                 result = val_ * text_width * 2.2 / 100;
233                 break;
234         case LyXLength::UNIT_NONE:
235                 result = 0;  // this cannot happen
236                 break;
237         }
238         return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
239 }
240
241
242 int LyXLength::inBP() const
243 {
244         // return any LyXLength value as a one with
245         // the PostScript point, called bp (big points)
246         double result = 0.0;
247         switch (unit_) {
248         case LyXLength::CM:
249                 // 1bp = 0.2835cm
250                 result = val_ * 28.346;
251                 break;
252         case LyXLength::MM:
253                 // 1bp = 0.02835mm
254                 result = val_ * 2.8346;
255                 break;
256         case LyXLength::IN:
257                 // 1pt = 1/72in
258                 result = val_ * 72.0;
259                 break;
260         default:
261                 // no other than bp possible
262                 result = val_;
263                 break;
264         }
265         return static_cast<int>(result + 0.5);
266 }
267
268
269 bool operator==(LyXLength const & l1, LyXLength const & l2)
270 {
271         return l1.value() == l2.value() && l1.unit() == l2.unit();
272 }
273
274
275 bool operator!=(LyXLength const & l1, LyXLength const & l2)
276 {
277         return !(l1 == l2);
278 }
279
280
281 } // namespace lyx