]> git.lyx.org Git - lyx.git/blob - src/lyxlength.C
Get rid of lyxstring, remove usage of STRCONV.
[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
23 #include "support/std_sstream.h"
24
25 using std::abs;
26 using std::ostringstream;
27
28
29 LyXLength::LyXLength()
30         : val_(0), unit_(LyXLength::UNIT_NONE)
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();
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();
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 bool LyXLength::empty() const
127 {
128         return unit_ == LyXLength::UNIT_NONE;
129 }
130
131
132 int LyXLength::inPixels(int text_width, int em_width_base) const
133 {
134         // Zoom factor specified by user in percent
135         double const zoom = lyxrc.zoom / 100.0; // [percent]
136
137         // DPI setting for monitor: pixels/inch
138         double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
139
140         double const em_width = (em_width_base > 0)
141                 ? em_width_base
142                 : 10*(dpi/72.27)*zoom;
143         // A different estimate for em_width is
144         // font_metrics::width('M', LyXFont(LyXFont::ALL_SANE))
145         // but this estimate might not be more accurate as the screen font
146         // is different then the latex font.
147
148         // Pixel values are scaled so that the ratio
149         // between lengths and font sizes on the screen
150         // is the same as on paper.
151
152 #ifdef WITH_WARNINGS
153 #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')
154 #endif
155
156         double result = 0.0;
157
158         switch (unit_) {
159         case LyXLength::SP:
160                 // Scaled point: sp = 1/65536 pt
161                 result = zoom * dpi * val_
162                         / (72.27 * 65536); // 4736286.7
163                 break;
164         case LyXLength::PT:
165                 // Point: 1 pt = 1/72.27 inch
166                 result = zoom * dpi * val_
167                         / 72.27; // 72.27
168                 break;
169         case LyXLength::BP:
170                 // Big point: 1 bp = 1/72 inch
171                 result = zoom * dpi * val_
172                         / 72; // 72
173                 break;
174         case LyXLength::DD:
175                 // Didot: 1157dd = 1238 pt?
176                 result = zoom * dpi * val_
177                         / (72.27 / (0.376 * 2.845)); // 67.559735
178                 break;
179         case LyXLength::MM:
180                 // Millimeter: 1 mm = 1/25.4 inch
181                 result = zoom * dpi * val_
182                         / 25.4; // 25.4
183                 break;
184         case LyXLength::PC:
185                 // Pica: 1 pc = 12 pt
186                 result = zoom * dpi * val_
187                         / (72.27 / 12); // 6.0225
188                 break;
189         case LyXLength::CC:
190                 // Cicero: 1 cc = 12 dd
191                 result = zoom * dpi * val_
192                         / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
193                 break;
194         case LyXLength::CM:
195                 // Centimeter: 1 cm = 1/2.54 inch
196                 result = zoom * dpi * val_
197                         / 2.54; // 2.54
198                 break;
199         case LyXLength::IN:
200                 // Inch
201                 result = zoom * dpi * val_;
202                 break;
203         case LyXLength::EX:
204                 // Ex: The height of an "x"
205                 // 0.4305 is the ration between 1ex and 1em in cmr10
206                 result = val_ * em_width * 0.4305;
207                 break;
208         case LyXLength::EM:
209                 // Em: The width of an "m"
210                 result = val_ * em_width;
211                 break;
212         case LyXLength::MU:
213                 // math unit = 1/18em
214                 result = val_ * em_width / 18;
215                 break;
216         case LyXLength::PCW: // Always % of workarea
217         case LyXLength::PTW:
218         case LyXLength::PLW:
219                 result = val_ * text_width / 100;
220                 break;
221         case LyXLength::PPW:
222                 // paperwidth/textwidth is 1.7 for A4 paper with default margins
223                 result = val_ * text_width * 1.7 / 100;
224                 break;
225         case LyXLength::PTH:
226                 result = val_ * text_width * 1.787 / 100;
227                 break;
228         case LyXLength::PPH:
229                 result = val_ * text_width * 2.2 / 100;
230                 break;
231         case LyXLength::UNIT_NONE:
232                 result = 0;  // this cannot happen
233                 break;
234         }
235         return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
236 }
237
238
239 int LyXLength::inBP() const
240 {
241         // return any LyXLength value as a one with
242         // the PostScript point, called bp (big points)
243         double result = 0.0;
244         switch (unit_) {
245         case LyXLength::CM:
246                 // 1bp = 0.2835cm
247                 result = val_ * 28.346;
248                 break;
249         case LyXLength::MM:
250                 // 1bp = 0.02835mm
251                 result = val_ * 2.8346;
252                 break;
253         case LyXLength::IN:
254                 // 1pt = 1/72in
255                 result = val_ * 72.0;
256                 break;
257         default:
258                 // no other than bp possible
259                 result = val_;
260                 break;
261         }
262         return static_cast<int>(result + 0.5);
263 }
264
265
266 bool operator==(LyXLength const & l1, LyXLength const & l2)
267 {
268         return l1.value() == l2.value() && l1.unit() == l2.unit();
269 }
270
271
272 bool operator!=(LyXLength const & l1, LyXLength const & l2)
273 {
274         return !(l1 == l2);
275 }