]> git.lyx.org Git - lyx.git/blob - src/lyxlength.C
fix lookup problem for \vert.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "lyxlength.h"
18 #include "lengthcommon.h"
19 #include "lyxrc.h"
20
21 #include "support/lstrings.h"
22
23 #include "Lsstream.h"
24
25 #include <cstdlib>
26
27
28 LyXLength::LyXLength()
29         : val_(0), unit_(LyXLength::PT)
30 {}
31
32
33 LyXLength::LyXLength(double v, LyXLength::UNIT u)
34         : val_(v), unit_(u)
35 {}
36
37
38 #ifndef NO_PEXTRA_REALLY
39 // compatibility stuff < version 1.2.0pre and for 
40 // "old" 1.2.0 files before the pre
41 namespace {
42 string const convertOldRelLength(string const & oldLength) 
43 {
44         // we can have only one or none of the following
45         if (oldLength.find("c%") != string::npos) {
46                 return subst(oldLength,"c%","col%");
47                     
48         } else if (oldLength.find("t%") != string::npos) {
49                 if (oldLength.find("text%") != string::npos)
50                     return oldLength;
51                 else
52                     return subst(oldLength,"t%","text%");
53
54         } else if (oldLength.find("l%") != string::npos) {
55                 if (oldLength.find("col%") != string::npos)
56                     return oldLength;
57                 else
58                     return subst(oldLength,"l%","line%");
59
60         } else if (oldLength.find("p%") != string::npos)
61                 return subst(oldLength,"p%","page%");
62
63         return oldLength;
64 }
65 } // end anon
66 #endif
67
68 LyXLength::LyXLength(string const & data)
69         : val_(0), unit_(LyXLength::PT)
70 {
71         LyXLength tmp;
72
73 #ifndef NO_PEXTRA_REALLY
74         // this is needed for 1.1.x minipages with width like %t
75         if (!isValidLength (convertOldRelLength(data), &tmp))
76 #else
77         if (!isValidLength (data, &tmp))
78 #endif
79                 if (!isValidLength (convertOldRelLength(data), &tmp))
80                 return; // should raise an exception
81
82         val_  = tmp.val_;
83         unit_ = tmp.unit_;
84 }
85
86
87 string const LyXLength::asString() const
88 {
89         ostringstream buffer;
90         buffer << val_ << unit_name[unit_]; // setw?
91         return buffer.str().c_str();
92 }
93
94
95 string const LyXLength::asLatexString() const
96 {
97         ostringstream buffer;
98         switch (unit_) {
99         case PW:
100             buffer << abs(static_cast<int>(val_/100)) << "."
101                    << abs(static_cast<int>(val_)%100) << "\\textwidth";
102             break;
103         case PE:
104             buffer << abs(static_cast<int>(val_/100)) << "."
105                    << abs(static_cast<int>(val_)%100) << "\\columnwidth";
106             break;
107         case PP:
108             buffer << abs(static_cast<int>(val_/100)) << "."
109                    << abs(static_cast<int>(val_)%100) << "\\paperwidth";
110             break;
111         case PL:
112             buffer << abs(static_cast<int>(val_/100)) << "."
113                    << abs(static_cast<int>(val_)%100) << "\\linewidth";
114             break;
115         default:
116             buffer << val_ << unit_name[unit_]; // setw?
117             break;
118         }
119         return buffer.str().c_str();
120 }
121
122
123 double LyXLength::value() const
124 {
125         return val_;
126 }
127
128
129 LyXLength::UNIT LyXLength::unit() const
130 {
131         return unit_;
132 }
133
134
135 void LyXLength::value(double v)
136 {
137         val_ = v;
138 }
139
140
141 void LyXLength::unit(LyXLength::UNIT u)
142 {
143         unit_ = u;
144 }
145
146
147 bool LyXLength::zero() const
148 {
149         return val_ == 0.0;
150 }
151
152
153 int LyXLength::inPixels(int default_width, int default_height) const
154 {
155         // Zoom factor specified by user in percent
156         double const zoom = lyxrc.zoom / 100.0; // [percent]
157
158         // DPI setting for monitor: pixels/inch
159         double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
160
161         // Pixel values are scaled so that the ratio
162         // between lengths and font sizes on the screen
163         // is the same as on paper.
164
165         // we don't care about sign of value, we
166         // display negative space with text too
167         double result = 0.0;
168         int val_sign = val_ < 0.0 ? -1 : 1;
169
170         switch (unit_) {
171         case LyXLength::SP:
172                 // Scaled point: sp = 1/65536 pt
173                 result = zoom * dpi * val_
174                         / (72.27 * 65536); // 4736286.7
175                 break;
176         case LyXLength::PT:
177                 // Point: 1 pt = 1/72.27 inch
178                 result = zoom * dpi * val_
179                         / 72.27; // 72.27
180                 break;
181         case LyXLength::BP:
182                 // Big point: 1 bp = 1/72 inch
183                 result = zoom * dpi * val_
184                         / 72; // 72
185                 break;
186         case LyXLength::DD:
187                 // Didot: 1157dd = 1238 pt?
188                 result = zoom * dpi * val_
189                         / (72.27 / (0.376 * 2.845)); // 67.559735
190                 break;
191         case LyXLength::MM:
192                 // Millimeter: 1 mm = 1/25.4 inch
193                 result = zoom * dpi * val_
194                         / 25.4; // 25.4
195                 break;
196         case LyXLength::PC:
197                 // Pica: 1 pc = 12 pt
198                 result = zoom * dpi * val_
199                         / (72.27 / 12); // 6.0225
200                 break;
201         case LyXLength::CC:
202                 // Cicero: 1 cc = 12 dd
203                 result = zoom * dpi * val_
204                         / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
205                 break;
206         case LyXLength::CM:
207                 // Centimeter: 1 cm = 1/2.54 inch
208                 result = zoom * dpi * val_
209                         / 2.54; // 2.54
210                 break;
211         case LyXLength::IN:
212                 // Inch
213                 result = zoom * dpi * val_;
214                 break;
215         case LyXLength::EX:
216                 // Ex: The height of an "x"
217                 result = zoom * val_ * default_height / 2; // what to / width?
218                 break;
219         case LyXLength::EM: // what to / width?
220                 // Em: The width of an "m"
221                 result = zoom * val_ * default_height / 2; // Why 2?
222                 break;
223         case LyXLength::MU: // This is probably only allowed in
224                 // math mode
225                 result = zoom * val_ * default_height;
226                 break;
227         case LyXLength::PW: // Always % of workarea
228         case LyXLength::PE:
229         case LyXLength::PP:
230         case LyXLength::PL:
231                 result = val_ * default_width / 100;
232                 break;
233         case LyXLength::UNIT_NONE:
234                 result = 0;  // this cannot happen
235                 break;
236         }
237         return static_cast<int>(result * val_sign + 0.5);
238 }
239
240
241 bool operator==(LyXLength const & l1, LyXLength const & l2)
242 {
243         return l1.value() == l2.value() && l1.unit() == l2.unit();
244 }
245
246
247 bool operator!=(LyXLength const & l1, LyXLength const & l2)
248 {
249         return !(l1 == l2);
250 }