]> git.lyx.org Git - features.git/blob - src/Length.cpp
The way this was done here is inconsistent with how it is done
[features.git] / src / Length.cpp
1 /**
2  * \file Length.cpp
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 "Length.h"
19 #include "LyXRC.h"
20 #include "MetricsInfo.h"
21
22 #include "frontends/FontMetrics.h"
23
24 #include "support/docstream.h"
25 #include "support/lstrings.h"
26
27 #include <sstream>
28 #include <iomanip>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35
36 /////////////////////////////////////////////////////////////////////
37 //
38 // Length
39 //
40 /////////////////////////////////////////////////////////////////////
41
42 Length::Length()
43         : val_(0), unit_(Length::UNIT_NONE)
44 {}
45
46
47 Length::Length(double v, Length::UNIT u)
48         : val_(v), unit_(u)
49 {}
50
51
52 Length::Length(string const & data)
53         : val_(0), unit_(Length::PT)
54 {
55         Length tmp;
56
57         if (!isValidLength(data, &tmp))
58                 return; // should raise an exception
59
60         val_  = tmp.val_;
61         unit_ = tmp.unit_;
62 }
63
64
65 string const Length::asString() const
66 {
67         ostringstream os;
68         if (unit_ != UNIT_NONE)
69                 os << formatFPNumber(val_) << unit_name[unit_]; // setw?
70         return os.str();
71 }
72
73
74 docstring const Length::asDocstring() const
75 {
76         odocstringstream os;
77         if (unit_ != UNIT_NONE)
78                 os << formatFPNumber(val_) << unit_name[unit_]; // setw?
79         return os.str();
80 }
81
82
83 string const Length::asLatexString() const
84 {
85         ostringstream os;
86         // Do not allow scientific notation (e.g. 1.2e+03), since this is not valid
87         // LaTeX (bug 9416)
88         switch (unit_) {
89         case PTW:
90                 os << formatFPNumber(val_ / 100.0) << "\\textwidth";
91                 break;
92         case PCW:
93                 os << formatFPNumber(val_ / 100.0) << "\\columnwidth";
94                 break;
95         case PPW:
96                 os << formatFPNumber(val_ / 100.0) << "\\paperwidth";
97                 break;
98         case PLW:
99                 os << formatFPNumber(val_ / 100.0) << "\\linewidth";
100                 break;
101         case PTH:
102                 os << formatFPNumber(val_ / 100.0) << "\\textheight";
103                 break;
104         case PPH:
105                 os << formatFPNumber(val_ / 100.0) << "\\paperheight";
106                 break;
107         case UNIT_NONE:
108                 break;
109         default:
110                 os << formatFPNumber(val_) << unit_name[unit_];
111           break;
112         }
113         return os.str();
114 }
115
116
117 string const Length::asHTMLString() const
118 {
119         ostringstream os;
120         switch (unit_) {
121         case PT:
122         case BP:
123         case DD:
124                 // close enough
125                 os << formatFPNumber(val_) << "pt";
126                 break;
127         case MM:
128         case CM:
129         case PC:
130         case IN:
131         case EX:
132         case EM:
133                 os << formatFPNumber(val_) << unit_name[unit_];
134                 break;
135         case CC:
136                 os << formatFPNumber(val_ / 12.0) << "pt";
137                 break;
138         case MU:
139                 os << formatFPNumber(val_ / 18.0) << "em";
140                 break;
141         case PTW:
142         case PPW:
143         case PLW:
144         case PCW:
145         case PTH:
146         case PPH:
147                 // what it's a percentage of probably won't make sense for HTML,
148                 // so we'll assume people have chosen these appropriately
149                 os << formatFPNumber(val_) << '%';
150                 break;
151         case SP:
152         case UNIT_NONE:
153                 break;
154         }
155         return os.str();
156 }
157
158
159 double Length::value() const
160 {
161         return val_;
162 }
163
164
165 Length::UNIT Length::unit() const
166 {
167         return unit_;
168 }
169
170
171 void Length::value(double v)
172 {
173         val_ = v;
174 }
175
176
177 void Length::unit(Length::UNIT u)
178 {
179         unit_ = u;
180 }
181
182
183 bool Length::zero() const
184 {
185         return val_ == 0.0;
186 }
187
188
189 bool Length::empty() const
190 {
191         return unit_ == Length::UNIT_NONE;
192 }
193
194
195 int Length::inPixels(int text_width, int em_width_base) const
196 {
197         // Zoom factor specified by user in percent
198         double const zoom = lyxrc.zoom / 100.0; // [percent]
199
200         // DPI setting for monitor: pixels/inch
201         double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
202
203         double const em_width = (em_width_base > 0)
204                 ? em_width_base
205                 : 10*(dpi/72.27)*zoom;
206         // A different estimate for em_width is
207         // theFontMetrics(FontInfo(sane_font)).em()
208         // but this estimate might not be more accurate as the screen font
209         // is different then the latex font.
210
211         // Pixel values are scaled so that the ratio
212         // between lengths and font sizes on the screen
213         // is the same as on paper.
214
215         double result = 0.0;
216
217         switch (unit_) {
218         case Length::SP:
219                 // Scaled point: sp = 1/65536 pt
220                 result = zoom * dpi * val_
221                         / (72.27 * 65536); // 4736286.7
222                 break;
223         case Length::PT:
224                 // Point: 1 pt = 1/72.27 inch
225                 result = zoom * dpi * val_
226                         / 72.27; // 72.27
227                 break;
228         case Length::BP:
229                 // Big point: 1 bp = 1/72 inch
230                 result = zoom * dpi * val_
231                         / 72; // 72
232                 break;
233         case Length::DD:
234                 // Didot: 1157dd = 1238 pt?
235                 result = zoom * dpi * val_
236                         / (72.27 / (0.376 * 2.845)); // 67.559735
237                 break;
238         case Length::MM:
239                 // Millimeter: 1 mm = 1/25.4 inch
240                 result = zoom * dpi * val_
241                         / 25.4; // 25.4
242                 break;
243         case Length::PC:
244                 // Pica: 1 pc = 12 pt
245                 result = zoom * dpi * val_
246                         / (72.27 / 12); // 6.0225
247                 break;
248         case Length::CC:
249                 // Cicero: 1 cc = 12 dd
250                 result = zoom * dpi * val_
251                         / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
252                 break;
253         case Length::CM:
254                 // Centimeter: 1 cm = 1/2.54 inch
255                 result = zoom * dpi * val_
256                         / 2.54; // 2.54
257                 break;
258         case Length::IN:
259                 // Inch
260                 result = zoom * dpi * val_;
261                 break;
262         case Length::EX:
263                 // Ex: The height of an "x"
264                 // 0.4305 is the ration between 1ex and 1em in cmr10
265                 result = val_ * em_width * 0.4305;
266                 break;
267         case Length::EM:
268                 // Em: The width of an "m"
269                 result = val_ * em_width;
270                 break;
271         case Length::MU:
272                 // math unit = 1/18em
273                 result = val_ * em_width / 18;
274                 break;
275         case Length::PCW: // Always % of workarea
276         case Length::PTW:
277         case Length::PLW:
278                 result = val_ * text_width / 100;
279                 break;
280         case Length::PPW:
281                 // paperwidth/textwidth is 1.7 for A4 paper with default margins
282                 result = val_ * text_width * 1.7 / 100;
283                 break;
284         case Length::PTH:
285                 result = val_ * text_width * 1.787 / 100;
286                 break;
287         case Length::PPH:
288                 result = val_ * text_width * 2.2 / 100;
289                 break;
290         case Length::UNIT_NONE:
291                 result = 0;  // this cannot happen
292                 break;
293         }
294         return static_cast<int>(result + ((result >= 0) ? 0.5 : -0.5));
295 }
296
297
298 int Length::inPixels(MetricsBase const & base) const
299 {
300         return inPixels(base.textwidth, theFontMetrics(base.font).em());
301 }
302
303
304 int Length::inBP() const
305 {
306         // return any Length value as a one with
307         // the PostScript point, called bp (big points)
308         double result = 0.0;
309         switch (unit_) {
310         case Length::CM:
311                 // 1bp = 0.2835cm
312                 result = val_ * 28.346;
313                 break;
314         case Length::MM:
315                 // 1bp = 0.02835mm
316                 result = val_ * 2.8346;
317                 break;
318         case Length::IN:
319                 // 1pt = 1/72in
320                 result = val_ * 72.0;
321                 break;
322         default:
323                 // no other than bp possible
324                 result = val_;
325                 break;
326         }
327         return static_cast<int>(result + 0.5);
328 }
329
330
331 Length::UNIT Length::defaultUnit()
332 {
333         return lyxrc.default_length_unit;
334 }
335
336
337
338 bool operator==(Length const & l1, Length const & l2)
339 {
340         return l1.value() == l2.value() && l1.unit() == l2.unit();
341 }
342
343
344 bool operator!=(Length const & l1, Length const & l2)
345 {
346         return !(l1 == l2);
347 }
348
349
350 /////////////////////////////////////////////////////////////////////
351 //
352 // GlueLength
353 //
354 /////////////////////////////////////////////////////////////////////
355
356
357 GlueLength::GlueLength(Length const & len)
358         : len_(len)
359 {}
360
361
362 GlueLength::GlueLength(Length const & len, Length const & plus,
363                 Length const & minus)
364         : len_(len), plus_(plus), minus_(minus)
365 {}
366
367
368 GlueLength::GlueLength(string const & data)
369 {
370         isValidGlueLength(data, this);
371 }
372
373
374 string const GlueLength::asString() const
375 {
376         if (len_.empty())
377                 return string();
378
379         ostringstream buffer;
380
381         buffer << formatFPNumber(len_.value());
382
383         if (plus_.zero() && minus_.zero()) {
384                 buffer << unit_name[len_.unit()];
385                 return buffer.str();
386         }
387
388         // just len and plus
389         if (minus_.zero()) {
390                 if (len_.unit() != plus_.unit())
391                         buffer << unit_name[len_.unit()];
392                 buffer << '+' << formatFPNumber(plus_.value());
393                 buffer << unit_name[plus_.unit()];
394                 return buffer.str();
395         }
396
397         // just len and minus
398         if (plus_.zero()) {
399                 if (len_.unit() != minus_.unit())
400                         buffer << unit_name[len_.unit()];
401                 buffer << '-' << formatFPNumber(minus_.value());
402                 buffer << unit_name[minus_.unit()];
403                 return buffer.str();
404         }
405
406         // ok, len, plus AND minus
407
408         // len+-
409         if (minus_ == plus_) {
410                 if (len_.unit() != minus_.unit())
411                         buffer << unit_name[len_.unit()];
412                 buffer << "+-" << formatFPNumber(minus_.value());
413                 buffer << unit_name[minus_.unit()];
414                 return buffer.str();
415         }
416
417         // this is so rare a case, why bother minimising units ?
418
419         buffer << unit_name[len_.unit()];
420         buffer << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
421         buffer << '-' << formatFPNumber(minus_.value()) << unit_name[minus_.unit()];
422
423         return buffer.str();
424 }
425
426
427 string const GlueLength::asLatexString() const
428 {
429         ostringstream buffer;
430         // use Length::asLatexString() to handle also the percent lengths
431         buffer << len_.Length::asLatexString();
432         if (!plus_.zero())
433                 buffer << " plus " << plus_.Length::asLatexString();
434         if (!minus_.zero())
435                 buffer << " minus " << minus_.Length::asLatexString();
436         return buffer.str();
437 }
438
439
440 Length const & GlueLength::len() const
441 {
442         return len_;
443 }
444
445
446 Length const & GlueLength::plus() const
447 {
448         return plus_;
449 }
450
451
452 Length const & GlueLength::minus() const
453 {
454         return minus_;
455 }
456
457
458 bool operator==(GlueLength const & l1, GlueLength const & l2)
459 {
460         return l1.len() == l2.len()
461                  && l1.plus() == l2.plus()
462                  && l1.minus() == l2.minus();
463 }
464
465
466 bool operator!=(GlueLength const & l1, GlueLength const & l2)
467 {
468         return !(l1 == l2);
469 }
470
471
472 } // namespace lyx