]> git.lyx.org Git - lyx.git/blob - src/vspace.C
247bb6d3e46b1ed3b43c381629b352412539095e
[lyx.git] / src / vspace.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *      
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation "vspace.h"
15 #endif
16
17 #include "vspace.h"
18 #include "lyx_main.h"
19 #include "buffer.h"
20 #include "lyxrc.h"
21 #include "lyxtext.h"
22 #include "BufferView.h"
23
24 #include "support/lstrings.h"
25
26 #include <cstdio>
27
28
29 namespace {
30
31 /*  length units
32  */
33
34 int const num_units = LyXLength::UNIT_NONE;
35
36 // I am not sure if "mu" should be possible to select (Lgb)
37 char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
38                                       "mm", "pc", "cc", "cm",
39                                       "in", "ex", "em", "mu",
40                                       "%",  "c%", "p%", "l%" };
41
42
43 /*  The following static items form a simple scanner for
44  *  length strings, used by isValid[Glue]Length.  See comments there.
45  */
46 double           number[4] = { 0, 0, 0, 0 };
47 LyXLength::UNIT unit[4]   = { LyXLength::UNIT_NONE,
48                               LyXLength::UNIT_NONE,
49                               LyXLength::UNIT_NONE,
50                               LyXLength::UNIT_NONE };
51 int number_index;
52 int unit_index;
53
54
55 inline
56 void lyx_advance(string & data, string::size_type n)
57 {
58         data.erase(0, n);
59 }
60
61
62 inline
63 bool isEndOfData(string const & data)
64 {
65         return frontStrip(data).empty();
66 }
67
68
69 char nextToken(string & data)
70 {
71         data = frontStrip(data);
72         if (data.empty())
73                 return '\0';
74         else if (data[0] == '+') {
75                 lyx_advance(data, 1);
76                 return '+';
77         } else if (prefixIs(data, "plus")) {
78                 lyx_advance(data, 4);
79                 return '+';
80         } else if (data[0] == '-') {
81                 lyx_advance(data, 1);
82                 return '-';
83         } else if (prefixIs(data, "minus")) {
84                 lyx_advance(data, 5);
85                 return '-';
86         } else {
87                 string::size_type i = data.find_first_not_of("0123456789.");
88
89                 if (i != 0) {
90                         if (number_index > 3) return 'E';
91
92                         string buffer;
93                 
94                         // we have found some number
95                         if (i == string::npos) {
96                                 buffer = data;
97                                 i = data.size() + 1;
98                         } else
99                                 buffer = data.substr(0, i);
100
101                         lyx_advance(data, i);
102
103                         if (isStrDbl(buffer)) {
104                                 number[number_index] = strToDbl(buffer);
105                                 ++number_index;
106                                 return 'n';
107                         } else return 'E';
108                 }
109                 
110                 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
111                 if (i != 0) {
112                         if (unit_index > 3) return 'E';
113
114                         string buffer;
115                 
116                         // we have found some alphabetical string
117                         if (i == string::npos) {
118                                 buffer = data;
119                                 i = data.size() + 1;
120                         } else
121                                 buffer = data.substr(0, i);
122
123                         // possibly we have "mmplus" string or similar
124                         if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
125                                 lyx_advance(data, 2);
126                                 unit[unit_index] = unitFromString(buffer.substr(0, 2));
127                         } else {
128                                 lyx_advance(data, i);
129                                 unit[unit_index] = unitFromString(buffer);
130                         }
131
132                         if (unit[unit_index] != LyXLength::UNIT_NONE) {
133                                 ++unit_index;
134                                 return 'u';
135                         } else return 'E';  // Error
136                 }
137                 return 'E';  // Error
138         }
139 }
140
141
142 struct LaTeXLength {
143         char const * pattern;
144         int  plus_val_index;
145         int  minus_val_index;
146         int  plus_uni_index;
147         int  minus_uni_index;
148 };
149
150
151 LaTeXLength table[] = {
152         { "nu",       0, 0, 0, 0 },
153         { "nu+nu",    2, 0, 2, 0 },
154         { "nu+nu-nu", 2, 3, 2, 3 },
155         { "nu+-nu",   2, 2, 2, 2 },
156         { "nu-nu",    0, 2, 0, 2 },
157         { "nu-nu+nu", 3, 2, 3, 2 },
158         { "nu-+nu",   2, 2, 2, 2 },
159         { "n+nu",     2, 0, 1, 0 },
160         { "n+n-nu",   2, 3, 1, 1 },
161         { "n+-nu",    2, 2, 1, 1 },
162         { "n-nu",     0, 2, 0, 1 },
163         { "n-n+nu",   3, 2, 1, 1 },
164         { "n-+nu",    2, 2, 1, 1 },
165         { "",         0, 0, 0, 0 }   // sentinel, must be empty
166 };
167
168 } // namespace anon
169
170 const char * stringFromUnit(int unit)
171 {
172         if (unit < 0 || unit >= num_units)
173                 return 0;
174         return unit_name[unit];
175 }
176
177
178 bool isValidGlueLength(string const & data, LyXGlueLength * result)
179 {
180         // This parser is table-driven.  First, it constructs a "pattern"
181         // that describes the sequence of tokens in "data".  For example,
182         // "n-nu" means: number, minus sign, number, unit.  As we go along,
183         // numbers and units are stored into static arrays.  Then, "pattern"
184         // is searched in the "table".  If it is found, the associated
185         // table entries tell us which number and unit should go where
186         // in the LyXLength structure.  Example: if "data" has the "pattern"
187         // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
188         // That means, "plus_val" is the second number that was seen
189         // in the input, "minus_val" is the third number, and "plus_uni"
190         // and "minus_uni" are the second and third units, respectively.
191         // ("val" and "uni" are always the first items seen in "data".)
192         // This is the most elegant solution I could find -- a straight-
193         // forward approach leads to very long, tedious code that would be
194         // much harder to understand and maintain. (AS)
195
196         if (data.empty())
197                 return true;
198         string buffer = frontStrip(data);
199
200         // To make isValidGlueLength recognize negative values as
201         // the first number this little hack is needed:
202         int val_sign = 1; // positive as default
203         switch (buffer[0]) {
204         case '-':
205                 lyx_advance(buffer, 1);
206                 val_sign = -1;
207                 break;
208         case '+':
209                 lyx_advance(buffer, 1);
210                 // fall through
211         default:
212                 // no action
213                 break;
214         }
215         // end of hack
216         
217         int  pattern_index = 0;
218         int  table_index = 0;
219         char pattern[20];
220
221         number_index = 1;
222         unit_index = 1;  // entries at index 0 are sentinels
223
224         // construct "pattern" from "data"
225         while (!isEndOfData (buffer)) {
226                 if (pattern_index > 20) return false;
227                 pattern[pattern_index] = nextToken (buffer);
228                 if (pattern[pattern_index] == 'E') return false;
229                 ++pattern_index;
230         }
231         pattern[pattern_index] = '\0';
232
233         // search "pattern" in "table"
234         table_index = 0;
235         while (compare(pattern, table[table_index].pattern)) {
236                 ++table_index;
237                 if (!*table[table_index].pattern)
238                         return false;
239         }
240         
241         // Get the values from the appropriate places.  If an index
242         // is zero, the corresponding array value is zero or UNIT_NONE,
243         // so we needn't check this.
244         if (result) {
245                 result->val_        = number[1] * val_sign;
246                 result->unit_       = unit[1];
247                 result->plus_val_   = number[table[table_index].plus_val_index];
248                 result->minus_val_  = number[table[table_index].minus_val_index];
249                 result->plus_unit_  = unit  [table[table_index].plus_uni_index];
250                 result->minus_unit_ = unit  [table[table_index].minus_uni_index];
251         }
252         return true;
253 }
254
255
256 bool isValidLength(string const & data, LyXLength * result)
257 {
258         /// This is a trimmed down version of isValidGlueLength.
259         /// The parser may seem overkill for lengths without
260         /// glue, but since we already have it, using it is
261         /// easier than writing something from scratch.
262         if (data.empty())
263                 return true;
264
265         string   buffer = data;
266         int      pattern_index = 0;
267         char     pattern[3];
268
269         // To make isValidLength recognize negative values
270         // this little hack is needed:
271         int val_sign = 1; // positive as default
272         switch (buffer[0]) {
273         case '-':
274                 lyx_advance(buffer, 1);
275                 val_sign = -1;
276                 break;
277         case '+':
278                 lyx_advance(buffer, 1);
279                 // fall through
280         default:
281                 // no action
282                 break;
283         }
284         // end of hack
285         
286         number_index = unit_index = 1;  // entries at index 0 are sentinels
287
288         // construct "pattern" from "data"
289         while (!isEndOfData (buffer)) {
290                 if (pattern_index > 2)
291                         return false;
292                 pattern[pattern_index] = nextToken (buffer);
293                 if (pattern[pattern_index] == 'E')
294                         return false;
295                 ++pattern_index;
296         }
297         pattern[pattern_index] = '\0';
298
299         // only the most basic pattern is accepted here
300         if (compare(pattern, "nu") != 0) return false;          
301         
302         // It _was_ a correct length string.
303         // Store away the values we found.
304         if (result) {
305                 result->val_  = number[1] * val_sign;
306                 result->unit_ = unit[1];
307         }
308         return true;
309 }
310
311
312
313
314 //
315 // LyXLength
316 //
317
318 LyXLength::LyXLength()
319         : val_(0), unit_(LyXLength::PT)
320 {}
321
322
323 LyXLength::LyXLength(double v, LyXLength::UNIT u)
324         : val_(v), unit_(u)
325 {}
326
327
328 LyXLength::LyXLength(string const & data)
329 {
330         LyXLength tmp;
331         
332         if (!isValidLength (data, &tmp))
333                 return; // should raise an exception
334
335         val_  = tmp.val_;
336         unit_ = tmp.unit_;
337 }
338
339
340 string const LyXLength::asString() const
341 {
342         ostringstream buffer;
343         buffer << val_ << unit_name[unit_]; // setw?
344         return buffer.str().c_str();
345 }
346
347
348 string const LyXLength::asLatexString() const
349 {
350         ostringstream buffer;
351         switch(unit_) {
352         case PW:
353         case PE:
354             buffer << abs(static_cast<int>(val_/100)) << "."
355                                 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
356             break;
357         case PP:
358             buffer << abs(static_cast<int>(val_/100)) << "."
359                                 << abs(static_cast<int>(val_)%100) << "\\pagewidth";
360             break;
361         case PL:
362             buffer << abs(static_cast<int>(val_/100)) << "."
363                                 << abs(static_cast<int>(val_)%100) << "\\linewidth";
364             break;
365         default:
366             buffer << val_ << unit_name[unit_]; // setw?
367             break;
368         }
369         return buffer.str().c_str();
370 }
371
372
373 double LyXLength::value() const
374 {
375         return val_;
376 }
377
378
379 LyXLength::UNIT LyXLength::unit() const
380 {
381         return unit_;
382 }
383
384
385 bool operator==(LyXLength const & l1, LyXLength const & l2)
386 {
387         return l1.value() == l2.value() && l1.unit() == l2.unit();
388 }
389         
390
391 LyXLength::UNIT unitFromString (string const & data)
392 {
393         int i = 0;
394         while (i < num_units && data != unit_name[i])
395                 ++i;
396         return static_cast<LyXLength::UNIT>(i);
397 }
398
399
400
401 //
402 // LyXGlueLength
403 //
404
405
406 LyXGlueLength::LyXGlueLength(
407                         double v,  LyXLength::UNIT u,
408                         double pv, LyXLength::UNIT pu,
409                         double mv, LyXLength::UNIT mu)
410         : LyXLength(v, u),
411           plus_val_(pv),  minus_val_(mv),
412           plus_unit_(pu), minus_unit_(mu)
413 {}
414
415
416 LyXGlueLength::LyXGlueLength(string const & data)
417 {
418         LyXGlueLength tmp(0.0, PT);
419
420         // we should really raise exception here
421         if (!isValidGlueLength(data, &tmp))
422                 ;
423
424         val_        = tmp.val_;
425         unit_       = tmp.unit_;
426         plus_val_   = tmp.plus_val_;
427         plus_unit_  = tmp.plus_unit_;
428         minus_val_  = tmp.minus_val_;
429         minus_unit_ = tmp.minus_unit_;
430 }
431
432
433 string const LyXGlueLength::asString() const
434 {
435         ostringstream buffer;
436
437         if (plus_val_ != 0.0)
438                 if (minus_val_ != 0.0)
439                         if (unit_ == plus_unit_ && unit_ == minus_unit_)
440                                 if (plus_val_ == minus_val_)
441                                         buffer << val_ << "+-"
442                                                << plus_val_ << unit_name[unit_];
443                                 else
444                                         buffer << val_
445                                                << '+' << plus_val_
446                                                << '-' << minus_val_
447                                                << unit_name[unit_];
448                         else
449                                 if (plus_unit_ == minus_unit_
450                                     && plus_val_ == minus_val_)
451                                         buffer << val_ << unit_name[unit_]
452                                                << "+-" << plus_val_
453                                                << unit_name[plus_unit_];
454         
455                                 else
456                                         buffer << val_ << unit_name[unit_]
457                                                << '+' << plus_val_
458                                                << unit_name[plus_unit_]
459                                                << '-' << minus_val_
460                                                << unit_name[minus_unit_];
461                 else
462                         if (unit_ == plus_unit_)
463                                 buffer << val_ << '+' << plus_val_
464                                        << unit_name[unit_];
465                         else
466                                 buffer << val_ << unit_name[unit_]
467                                        << '+' << plus_val_
468                                        << unit_name[plus_unit_];
469         
470         else
471                 if (minus_val_ != 0.0)
472                         if (unit_ == minus_unit_)
473                                 buffer << val_ << '-' << minus_val_
474                                        << unit_name[unit_];
475         
476                         else
477                                 buffer << val_ << unit_name[unit_]
478                                        << '-' << minus_val_
479                                        << unit_name[minus_unit_];
480                 else
481                         buffer << val_ << unit_name[unit_];
482
483         return buffer.str().c_str();
484 }
485
486
487 string const LyXGlueLength::asLatexString() const
488 {
489         ostringstream buffer;
490
491         if (plus_val_ != 0.0)
492                 if (minus_val_ != 0.0)
493                         buffer << val_ << unit_name[unit_]
494                                << " plus "
495                                << plus_val_ << unit_name[plus_unit_]
496                                << " minus "
497                                << minus_val_ << unit_name[minus_unit_];
498                 else
499                         buffer << val_ << unit_name[unit_]
500                                << " plus "
501                                << plus_val_ << unit_name[plus_unit_];
502         else
503                 if (minus_val_ != 0.0)
504                         buffer << val_ << unit_name[unit_]
505                                << " minus "
506                                << minus_val_ << unit_name[minus_unit_];
507                 else
508                         buffer << val_ << unit_name[unit_];
509
510         return buffer.str().c_str();
511 }
512
513
514 double LyXGlueLength::plusValue() const
515 {
516         return plus_val_;
517 }
518
519
520 LyXLength::UNIT LyXGlueLength::plusUnit() const
521 {
522         return plus_unit_;
523 }
524
525
526 double LyXGlueLength::minusValue() const
527 {
528         return minus_val_;
529 }
530
531
532 LyXLength::UNIT LyXGlueLength::minusUnit() const
533 {
534         return minus_unit_;
535 }
536
537
538 bool operator==(LyXGlueLength const & l1, LyXGlueLength const & l2)
539 {
540         return l1.value() == l2.value()
541                 && l1.unit() == l2.unit()
542                 && l1.plusValue() == l2.plusValue()
543                 && l1.plusUnit() == l2.plusUnit()
544                 && l1.minusValue() == l2.minusValue()
545                 && l1.minusUnit() == l2.minusUnit();
546 }
547
548
549 bool operator!=(LyXGlueLength const & l1, LyXGlueLength const & l2)
550 {
551         return !(l1 == l2);
552 }
553
554
555
556 //
557 //  VSpace class
558 //
559
560 VSpace::VSpace()
561         : kind_(NONE), len_(0.0, LyXLength::PT), keep_(false)
562 {}
563
564
565 VSpace::VSpace(vspace_kind k)
566         : kind_(k), len_(0.0, LyXLength::PT), keep_(false)
567 {}
568
569
570 VSpace::VSpace(LyXGlueLength l)
571         : kind_(LENGTH), len_(l), keep_(false)
572 {}
573
574
575 VSpace::VSpace(double v, LyXLength::UNIT u)
576         : kind_(LENGTH), len_(v, u), keep_(false)
577 {}
578
579
580 VSpace::VSpace(string const & data)
581         : kind_(NONE), len_(0.0, LyXLength::PT), keep_(false)
582 {
583         if (data.empty())
584                 return;
585         double value;
586         string input  = strip(data);
587
588         string::size_type const length = input.length();
589
590         if (length > 1 && input[length-1] == '*') {
591                 keep_ = true;
592                 input.erase(length - 1);
593         }
594
595         if      (prefixIs (input, "defskip"))    kind_ = DEFSKIP;
596         else if (prefixIs (input, "smallskip"))  kind_ = SMALLSKIP;
597         else if (prefixIs (input, "medskip"))    kind_ = MEDSKIP;
598         else if (prefixIs (input, "bigskip"))    kind_ = BIGSKIP;
599         else if (prefixIs (input, "vfill"))      kind_ = VFILL;
600         else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
601         else if (sscanf(input.c_str(), "%lf", &value) == 1) {
602                 // This last one is for reading old .lyx files
603                 // without units in added_space_top/bottom.
604                 // Let unit default to centimeters here.
605                 kind_ = LENGTH;
606                 len_  = LyXGlueLength(value, LyXLength::CM);
607         }
608 }
609
610
611 VSpace::vspace_kind VSpace::kind() const
612 {
613         return kind_;
614 }
615
616
617 LyXGlueLength VSpace::length() const
618 {
619         return len_;
620 }
621
622
623 bool VSpace::keep() const
624 {
625         return keep_;
626 }
627
628
629 void VSpace::setKeep(bool val)
630 {
631         keep_ = val;
632 }
633
634
635 bool VSpace::operator==(VSpace const & other) const
636 {
637         if (kind_ != other.kind_)
638                 return false;
639
640         if (kind_ != LENGTH)
641                 return this->keep_ == other.keep_;
642
643         if (len_ != other.len_)
644                 return false;
645
646         return keep_ == other.keep_;
647 }
648
649
650 string const VSpace::asLyXCommand() const
651 {
652         string result;
653         switch (kind_) {
654         case NONE:      break;
655         case DEFSKIP:   result = "defskip";      break;
656         case SMALLSKIP: result = "smallskip";    break;
657         case MEDSKIP:   result = "medskip";      break;
658         case BIGSKIP:   result = "bigskip";      break;
659         case VFILL:     result = "vfill";        break;
660         case LENGTH:    result = len_.asString(); break;
661         }
662         if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
663                 result += '*';
664         return result;
665 }
666
667
668 string const VSpace::asLatexCommand(BufferParams const & params) const
669 {
670         switch (kind_) {
671         case NONE:      return string();
672         case DEFSKIP:
673                 return params.getDefSkip().asLatexCommand(params);
674         case SMALLSKIP: return keep_ ? "\\vspace*{\\smallskipamount}"
675                                 : "\\smallskip{}";
676         case MEDSKIP:   return keep_ ? "\\vspace*{\\medskipamount}"
677                                 : "\\medskip{}";
678         case BIGSKIP:   return keep_ ? "\\vspace*{\\bigskipamount}"
679                                 : "\\bigskip{}";
680         case VFILL:     return keep_ ? "\\vspace*{\\fill}"
681                                 : "\\vfill{}";
682         case LENGTH:    return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
683                                 : "\\vspace{" + len_.asLatexString() + '}';
684         }
685         return string();  // should never be reached
686 }
687
688
689 int VSpace::inPixels(BufferView * bv) const
690 {
691         // Height of a normal line in pixels (zoom factor considered)
692         int height = bv->text->defaultHeight(); // [pixels]
693         int skip  = 0;
694         int width = bv->workWidth();
695
696         if (kind_ == DEFSKIP)
697                 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
698
699         return inPixels(height, skip, width);
700 }
701
702
703 int VSpace::inPixels(int default_height, int default_skip, int default_width)
704         const
705 {
706         // Height of a normal line in pixels (zoom factor considered)
707         int height = default_height; // [pixels]
708         
709         // Zoom factor specified by user in percent
710         double const zoom = lyxrc.zoom / 100.0; // [percent]
711
712         // DPI setting for monitor: pixels/inch
713         double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
714
715         // We want the result in pixels
716         double result;
717         double value;
718
719         switch (kind_) {
720         case NONE:
721                 return 0;
722
723         case DEFSKIP:
724                 return default_skip;
725
726                 // This is how the skips are normally defined by
727                 // LateX.  But there should be some way to change
728                 // this per document.
729         case SMALLSKIP: return height / 4;
730         case MEDSKIP:   return height / 2;
731         case BIGSKIP:   return height;
732         case VFILL:     return 3 * height;
733                 // leave space for the vfill symbol
734         case LENGTH:
735                 // Pixel values are scaled so that the ratio
736                 // between lengths and font sizes on the screen
737                 // is the same as on paper.
738
739                 // we don't care about sign of value, we
740                 // display negative space with text too
741                 result = 0.0;
742                 value  = len_.value();
743                 int val_sign = value < 0.0 ? -1 : 1;
744                 
745                 switch (len_.unit()) {
746                 case LyXLength::SP:
747                         // Scaled point: sp = 1/65536 pt
748                         result = zoom * dpi * value
749                                 / (72.27 * 65536); // 4736286.7
750                         break;
751                 case LyXLength::PT:
752                         // Point: 1 pt = 1/72.27 inch
753                         result = zoom * dpi * value
754                                 / 72.27; // 72.27
755                         break;
756                 case LyXLength::BP:
757                         // Big point: 1 bp = 1/72 inch
758                         result = zoom * dpi * value
759                                 / 72; // 72
760                         break;
761                 case LyXLength::DD:
762                         // Didot: 1157dd = 1238 pt?
763                         result = zoom * dpi * value
764                                 / (72.27 / (0.376 * 2.845)); // 67.559735
765                         break;
766                 case LyXLength::MM:
767                         // Millimeter: 1 mm = 1/25.4 inch
768                         result = zoom * dpi * value
769                                 / 25.4; // 25.4
770                         break;
771                 case LyXLength::PC:
772                         // Pica: 1 pc = 12 pt
773                         result = zoom * dpi * value
774                                 / (72.27 / 12); // 6.0225
775                         break;
776                 case LyXLength::CC:
777                         // Cicero: 1 cc = 12 dd
778                         result = zoom * dpi * value
779                                 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
780                         break;
781                 case LyXLength::CM:
782                         // Centimeter: 1 cm = 1/2.54 inch
783                         result = zoom * dpi * value
784                                 / 2.54; // 2.54
785                         break;
786                 case LyXLength::IN:
787                         // Inch
788                         result = zoom * dpi * value;
789                         break;
790                 case LyXLength::EX:
791                         // Ex: The height of an "x"
792                         result = zoom * value * height / 2; // what to / width?
793                         break;
794                 case LyXLength::EM: // what to / width?
795                         // Em: The width of an "m"
796                         result = zoom * value * height / 2; // Why 2?
797                         break;
798                 case LyXLength::MU: // This is probably only allowed in
799                         // math mode
800                         result = zoom * value * height;
801                         break;
802                 case LyXLength::PW: // Always % of workarea
803                 case LyXLength::PE:
804                 case LyXLength::PP:
805                 case LyXLength::PL:
806                         result = value * default_width / 100;
807                         break;
808                 case LyXLength::UNIT_NONE:
809                         result = 0;  // this cannot happen
810                         break;
811                 }
812                 return static_cast<int>(result * val_sign + 0.5);
813         }
814         return 0; // never reached
815 }