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