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