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