]> git.lyx.org Git - lyx.git/blob - src/lyxlength.C
77218b3e7b4b862a8253fa1ed0807c2d90fb2b74
[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 LyXLength::LyXLength(string const & data)
40         : val_(0), unit_(LyXLength::PT)
41 {
42         LyXLength tmp;
43
44         if (!isValidLength(data, &tmp))
45                 return; // should raise an exception
46
47         val_  = tmp.val_;
48         unit_ = tmp.unit_;
49 }
50
51
52 string const LyXLength::asString() const
53 {
54         ostringstream buffer;
55         buffer << val_ << unit_name[unit_]; // setw?
56         return buffer.str().c_str();
57 }
58
59
60 string const LyXLength::asLatexString() const
61 {
62         ostringstream buffer;
63         switch (unit_) {
64         case PTW:
65             buffer << abs(static_cast<int>(val_/100)) << "."
66                    << abs(static_cast<int>(val_)%100) << "\\textwidth";
67             break;
68         case PCW:
69             buffer << abs(static_cast<int>(val_/100)) << "."
70                    << abs(static_cast<int>(val_)%100) << "\\columnwidth";
71             break;
72         case PPW:
73             buffer << abs(static_cast<int>(val_/100)) << "."
74                    << abs(static_cast<int>(val_)%100) << "\\paperwidth";
75             break;
76         case PLW:
77             buffer << abs(static_cast<int>(val_/100)) << "."
78                    << abs(static_cast<int>(val_)%100) << "\\linewidth";
79             break;
80         case PPH:
81             buffer << abs(static_cast<int>(val_/100)) << "."
82                    << abs(static_cast<int>(val_)%100) << "\\paperheight";
83             break;
84         case PTH:
85             buffer << abs(static_cast<int>(val_/100)) << "."
86                    << abs(static_cast<int>(val_)%100) << "\\textheight";
87             break;
88         default:
89             buffer << val_ << unit_name[unit_]; // setw?
90             break;
91         }
92         return buffer.str().c_str();
93 }
94
95
96 double LyXLength::value() const
97 {
98         return val_;
99 }
100
101
102 LyXLength::UNIT LyXLength::unit() const
103 {
104         return unit_;
105 }
106
107
108 void LyXLength::value(double v)
109 {
110         val_ = v;
111 }
112
113
114 void LyXLength::unit(LyXLength::UNIT u)
115 {
116         unit_ = u;
117 }
118
119
120 bool LyXLength::zero() const
121 {
122         return val_ == 0.0;
123 }
124
125
126 int LyXLength::inPixels(int default_width, int default_height) const
127 {
128         // Zoom factor specified by user in percent
129         double const zoom = lyxrc.zoom / 100.0; // [percent]
130
131         // DPI setting for monitor: pixels/inch
132         double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
133
134         // Pixel values are scaled so that the ratio
135         // between lengths and font sizes on the screen
136         // is the same as on paper.
137
138         // we don't care about sign of value, we
139         // display negative space with text too
140 #ifdef WITH_WARNINGS
141 #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')
142 #endif
143
144         double result = 0.0;
145
146         switch (unit_) {
147         case LyXLength::SP:
148                 // Scaled point: sp = 1/65536 pt
149                 result = zoom * dpi * val_
150                         / (72.27 * 65536); // 4736286.7
151                 break;
152         case LyXLength::PT:
153                 // Point: 1 pt = 1/72.27 inch
154                 result = zoom * dpi * val_
155                         / 72.27; // 72.27
156                 break;
157         case LyXLength::BP:
158                 // Big point: 1 bp = 1/72 inch
159                 result = zoom * dpi * val_
160                         / 72; // 72
161                 break;
162         case LyXLength::DD:
163                 // Didot: 1157dd = 1238 pt?
164                 result = zoom * dpi * val_
165                         / (72.27 / (0.376 * 2.845)); // 67.559735
166                 break;
167         case LyXLength::MM:
168                 // Millimeter: 1 mm = 1/25.4 inch
169                 result = zoom * dpi * val_
170                         / 25.4; // 25.4
171                 break;
172         case LyXLength::PC:
173                 // Pica: 1 pc = 12 pt
174                 result = zoom * dpi * val_
175                         / (72.27 / 12); // 6.0225
176                 break;
177         case LyXLength::CC:
178                 // Cicero: 1 cc = 12 dd
179                 result = zoom * dpi * val_
180                         / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
181                 break;
182         case LyXLength::CM:
183                 // Centimeter: 1 cm = 1/2.54 inch
184                 result = zoom * dpi * val_
185                         / 2.54; // 2.54
186                 break;
187         case LyXLength::IN:
188                 // Inch
189                 result = zoom * dpi * val_;
190                 break;
191         case LyXLength::EX:
192                 // Ex: The height of an "x"
193                 // 0.4305 is the ration between 1ex and 1em in cmr10
194                 result = zoom * val_ * default_height * 0.4305;
195                 break;
196         case LyXLength::EM:
197                 // Em: The width of an "m"
198                 // 1em is approx 10points in cmr10
199                 result = zoom * val_ * default_height;
200                 break;
201         case LyXLength::MU:
202                 // math unit = 1/18em
203                 result = val_ * default_height / 18;
204                 break;
205         case LyXLength::PCW: // Always % of workarea
206         case LyXLength::PTW:
207         case LyXLength::PPW:
208         case LyXLength::PLW:
209                 result = val_ * default_width / 100;
210                 break;
211         case LyXLength::PTH:
212         case LyXLength::PPH:
213                 result = val_ * default_height / 100;
214                 break;
215         case LyXLength::UNIT_NONE:
216                 result = 0;  // this cannot happen
217                 break;
218         }
219         return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
220 }
221
222
223 int LyXLength::inBP() const
224 {
225         // return any LyXLength value as a one with
226         // the PostScript point, called bp (big points)
227         double result = 0.0;
228         switch (unit_) {
229         case LyXLength::CM:
230                 // 1bp = 0.2835cm
231                 result = val_ * 28.346;
232                 break;
233         case LyXLength::MM:
234                 // 1bp = 0.02835mm
235                 result = val_ * 2.8346;
236                 break;
237         case LyXLength::IN:
238                 // 1pt = 1/72in
239                 result = val_ * 72.0;
240                 break;
241         default:
242                 // no other than bp possible
243                 result = val_;
244                 break;
245         }
246         return static_cast<int>(result + 0.5);
247 }
248
249
250 bool operator==(LyXLength const & l1, LyXLength const & l2)
251 {
252         return l1.value() == l2.value() && l1.unit() == l2.unit();
253 }
254
255
256 bool operator!=(LyXLength const & l1, LyXLength const & l2)
257 {
258         return !(l1 == l2);
259 }