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