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