1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation "vspace.h"
24 #include "BufferView.h"
25 #include "support/lstrings.h"
33 int const num_units = LyXLength::UNIT_NONE;
35 // I am not sure if "mu" should be possible to select (Lgb)
36 char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
37 "mm", "pc", "cc", "cm",
38 "in", "ex", "em", "mu",
39 "%", "c%", "p%", "l%" };
42 /* The following static items form a simple scanner for
43 * length strings, used by isValid[Glue]Length. See comments there.
45 double number[4] = { 0, 0, 0, 0 };
46 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
49 LyXLength::UNIT_NONE };
55 void lyx_advance(string & data, string::size_type n)
62 bool isEndOfData(string const & data)
64 return frontStrip(data).empty();
68 char nextToken(string & data)
70 data = frontStrip(data);
73 else if (data[0] == '+') {
76 } else if (prefixIs(data, "plus")) {
79 } else if (data[0] == '-') {
82 } else if (prefixIs(data, "minus")) {
86 string::size_type i = data.find_first_not_of("0123456789.");
89 if (number_index > 3) return 'E';
93 // we have found some number
94 if (i == string::npos) {
98 buffer = data.substr(0, i);
100 lyx_advance(data, i);
102 if (isStrDbl(buffer)) {
103 number[number_index] = strToDbl(buffer);
109 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
111 if (unit_index > 3) return 'E';
115 // we have found some alphabetical string
116 if (i == string::npos) {
120 buffer = data.substr(0, i);
122 // possibly we have "mmplus" string or similar
123 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
124 lyx_advance(data, 2);
125 unit[unit_index] = unitFromString(buffer.substr(0, 2));
127 lyx_advance(data, i);
128 unit[unit_index] = unitFromString(buffer);
131 if (unit[unit_index] != LyXLength::UNIT_NONE) {
134 } else return 'E'; // Error
142 char const * pattern;
150 LaTeXLength table[] = {
151 { "nu", 0, 0, 0, 0 },
152 { "nu+nu", 2, 0, 2, 0 },
153 { "nu+nu-nu", 2, 3, 2, 3 },
154 { "nu+-nu", 2, 2, 2, 2 },
155 { "nu-nu", 0, 2, 0, 2 },
156 { "nu-nu+nu", 3, 2, 3, 2 },
157 { "nu-+nu", 2, 2, 2, 2 },
158 { "n+nu", 2, 0, 1, 0 },
159 { "n+n-nu", 2, 3, 1, 1 },
160 { "n+-nu", 2, 2, 1, 1 },
161 { "n-nu", 0, 2, 0, 1 },
162 { "n-n+nu", 3, 2, 1, 1 },
163 { "n-+nu", 2, 2, 1, 1 },
164 { "", 0, 0, 0, 0 } // sentinel, must be empty
169 const char * stringFromUnit(int unit)
171 if (unit < 0 || unit >= num_units)
173 return unit_name[unit];
177 bool isValidGlueLength(string const & data, LyXGlueLength * result)
179 // This parser is table-driven. First, it constructs a "pattern"
180 // that describes the sequence of tokens in "data". For example,
181 // "n-nu" means: number, minus sign, number, unit. As we go along,
182 // numbers and units are stored into static arrays. Then, "pattern"
183 // is searched in the "table". If it is found, the associated
184 // table entries tell us which number and unit should go where
185 // in the LyXLength structure. Example: if "data" has the "pattern"
186 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
187 // That means, "plus_val" is the second number that was seen
188 // in the input, "minus_val" is the third number, and "plus_uni"
189 // and "minus_uni" are the second and third units, respectively.
190 // ("val" and "uni" are always the first items seen in "data".)
191 // This is the most elegant solution I could find -- a straight-
192 // forward approach leads to very long, tedious code that would be
193 // much harder to understand and maintain. (AS)
197 string buffer = frontStrip(data);
199 // To make isValidGlueLength recognize negative values as
200 // the first number this little hack is needed:
201 int val_sign = 1; // positive as default
204 lyx_advance(buffer, 1);
208 lyx_advance(buffer, 1);
216 int pattern_index = 0;
221 unit_index = 1; // entries at index 0 are sentinels
223 // construct "pattern" from "data"
224 while (!isEndOfData (buffer)) {
225 if (pattern_index > 20) return false;
226 pattern[pattern_index] = nextToken (buffer);
227 if (pattern[pattern_index] == 'E') return false;
230 pattern[pattern_index] = '\0';
232 // search "pattern" in "table"
234 while (compare(pattern, table[table_index].pattern)) {
236 if (!*table[table_index].pattern)
240 // Get the values from the appropriate places. If an index
241 // is zero, the corresponding array value is zero or UNIT_NONE,
242 // so we needn't check this.
244 result->val_ = number[1] * val_sign;
245 result->unit_ = unit[1];
246 result->plus_val_ = number[table[table_index].plus_val_index];
247 result->minus_val_ = number[table[table_index].minus_val_index];
248 result->plus_unit_ = unit [table[table_index].plus_uni_index];
249 result->minus_unit_ = unit [table[table_index].minus_uni_index];
255 bool isValidLength(string const & data, LyXLength * result)
257 /// This is a trimmed down version of isValidGlueLength.
258 /// The parser may seem overkill for lengths without
259 /// glue, but since we already have it, using it is
260 /// easier than writing something from scratch.
264 string buffer = data;
265 int pattern_index = 0;
268 // To make isValidLength recognize negative values
269 // this little hack is needed:
270 int val_sign = 1; // positive as default
273 lyx_advance(buffer, 1);
277 lyx_advance(buffer, 1);
285 number_index = unit_index = 1; // entries at index 0 are sentinels
287 // construct "pattern" from "data"
288 while (!isEndOfData (buffer)) {
289 if (pattern_index > 2)
291 pattern[pattern_index] = nextToken (buffer);
292 if (pattern[pattern_index] == 'E')
296 pattern[pattern_index] = '\0';
298 // only the most basic pattern is accepted here
299 if (compare(pattern, "nu") != 0) return false;
301 // It _was_ a correct length string.
302 // Store away the values we found.
304 result->val_ = number[1] * val_sign;
305 result->unit_ = unit[1];
317 LyXLength::LyXLength()
318 : val_(0), unit_(LyXLength::PT)
322 LyXLength::LyXLength(double v, LyXLength::UNIT u)
327 LyXLength::LyXLength(string const & data)
331 if (!isValidLength (data, &tmp))
332 return; // should raise an exception
339 string const LyXLength::asString() const
341 ostringstream buffer;
342 buffer << val_ << unit_name[unit_]; // setw?
343 return buffer.str().c_str();
347 string const LyXLength::asLatexString() const
349 ostringstream buffer;
353 buffer << abs(static_cast<int>(val_/100)) << "."
354 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
357 buffer << abs(static_cast<int>(val_/100)) << "."
358 << abs(static_cast<int>(val_)%100) << "\\pagewidth";
361 buffer << abs(static_cast<int>(val_/100)) << "."
362 << abs(static_cast<int>(val_)%100) << "\\linewidth";
365 buffer << val_ << unit_name[unit_]; // setw?
368 return buffer.str().c_str();
372 double LyXLength::value() const
378 LyXLength::UNIT LyXLength::unit() const
384 bool operator==(LyXLength const & l1, LyXLength const & l2)
386 return l1.value() == l2.value() && l1.unit() == l2.unit();
390 LyXLength::UNIT unitFromString (string const & data)
393 while (i < num_units && data != unit_name[i])
395 return static_cast<LyXLength::UNIT>(i);
405 LyXGlueLength::LyXGlueLength(
406 double v, LyXLength::UNIT u,
407 double pv, LyXLength::UNIT pu,
408 double mv, LyXLength::UNIT mu)
410 plus_val_(pv), minus_val_(mv),
411 plus_unit_(pu), minus_unit_(mu)
415 LyXGlueLength::LyXGlueLength(string const & data)
417 LyXGlueLength tmp(0.0, PT);
419 // we should really raise exception here
420 if (!isValidGlueLength(data, &tmp))
425 plus_val_ = tmp.plus_val_;
426 plus_unit_ = tmp.plus_unit_;
427 minus_val_ = tmp.minus_val_;
428 minus_unit_ = tmp.minus_unit_;
432 string const LyXGlueLength::asString() const
434 ostringstream buffer;
436 if (plus_val_ != 0.0)
437 if (minus_val_ != 0.0)
438 if (unit_ == plus_unit_ && unit_ == minus_unit_)
439 if (plus_val_ == minus_val_)
440 buffer << val_ << "+-"
441 << plus_val_ << unit_name[unit_];
448 if (plus_unit_ == minus_unit_
449 && plus_val_ == minus_val_)
450 buffer << val_ << unit_name[unit_]
452 << unit_name[plus_unit_];
455 buffer << val_ << unit_name[unit_]
457 << unit_name[plus_unit_]
459 << unit_name[minus_unit_];
461 if (unit_ == plus_unit_)
462 buffer << val_ << '+' << plus_val_
465 buffer << val_ << unit_name[unit_]
467 << unit_name[plus_unit_];
470 if (minus_val_ != 0.0)
471 if (unit_ == minus_unit_)
472 buffer << val_ << '-' << minus_val_
476 buffer << val_ << unit_name[unit_]
478 << unit_name[minus_unit_];
480 buffer << val_ << unit_name[unit_];
482 return buffer.str().c_str();
486 string const LyXGlueLength::asLatexString() const
488 ostringstream buffer;
490 if (plus_val_ != 0.0)
491 if (minus_val_ != 0.0)
492 buffer << val_ << unit_name[unit_]
494 << plus_val_ << unit_name[plus_unit_]
496 << minus_val_ << unit_name[minus_unit_];
498 buffer << val_ << unit_name[unit_]
500 << plus_val_ << unit_name[plus_unit_];
502 if (minus_val_ != 0.0)
503 buffer << val_ << unit_name[unit_]
505 << minus_val_ << unit_name[minus_unit_];
507 buffer << val_ << unit_name[unit_];
509 return buffer.str().c_str();
513 double LyXGlueLength::plusValue() const
519 LyXLength::UNIT LyXGlueLength::plusUnit() const
525 double LyXGlueLength::minusValue() const
531 LyXLength::UNIT LyXGlueLength::minusUnit() const
537 bool operator==(LyXGlueLength const & l1, LyXGlueLength const & l2)
539 return l1.value() == l2.value()
540 && l1.unit() == l2.unit()
541 && l1.plusValue() == l2.plusValue()
542 && l1.plusUnit() == l2.plusUnit()
543 && l1.minusValue() == l2.minusValue()
544 && l1.minusUnit() == l2.minusUnit();
555 : kind_(NONE), len_(0.0, LyXLength::PT), keep_(false)
559 VSpace::VSpace(vspace_kind k)
560 : kind_(k), len_(0.0, LyXLength::PT), keep_(false)
564 VSpace::VSpace(LyXGlueLength l)
565 : kind_(LENGTH), len_(l), keep_(false)
569 VSpace::VSpace(double v, LyXLength::UNIT u)
570 : kind_(LENGTH), len_(v, u), keep_(false)
574 VSpace::VSpace(string const & data)
575 : kind_(NONE), len_(0.0, LyXLength::PT), keep_(false)
580 string input = strip(data);
582 string::size_type const length = input.length();
584 if (length > 1 && input[length-1] == '*') {
586 input.erase(length - 1);
589 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
590 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
591 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
592 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
593 else if (prefixIs (input, "vfill")) kind_ = VFILL;
594 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
595 else if (sscanf(input.c_str(), "%lf", &value) == 1) {
596 // This last one is for reading old .lyx files
597 // without units in added_space_top/bottom.
598 // Let unit default to centimeters here.
600 len_ = LyXGlueLength(value, LyXLength::CM);
605 VSpace::vspace_kind VSpace::kind() const
611 LyXGlueLength VSpace::length() const
617 bool VSpace::keep() const
623 void VSpace::setKeep(bool val)
629 bool VSpace::operator==(VSpace const & other) const
631 if (kind_ != other.kind_)
635 return this->keep_ == other.keep_;
637 if (len_ != other.len_)
640 return keep_ == other.keep_;
644 string const VSpace::asLyXCommand() const
649 case DEFSKIP: result = "defskip"; break;
650 case SMALLSKIP: result = "smallskip"; break;
651 case MEDSKIP: result = "medskip"; break;
652 case BIGSKIP: result = "bigskip"; break;
653 case VFILL: result = "vfill"; break;
654 case LENGTH: result = len_.asString(); break;
656 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
662 string const VSpace::asLatexCommand(BufferParams const & params) const
665 case NONE: return string();
667 return params.getDefSkip().asLatexCommand(params);
668 case SMALLSKIP: return keep_ ? "\\vspace*{\\smallskipamount}"
670 case MEDSKIP: return keep_ ? "\\vspace*{\\medskipamount}"
672 case BIGSKIP: return keep_ ? "\\vspace*{\\bigskipamount}"
674 case VFILL: return keep_ ? "\\vspace*{\\fill}"
676 case LENGTH: return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
677 : "\\vspace{" + len_.asLatexString() + '}';
679 return string(); // should never be reached
683 int VSpace::inPixels(BufferView * bv) const
685 // Height of a normal line in pixels (zoom factor considered)
686 int height = bv->text->defaultHeight(); // [pixels]
688 int width = bv->workWidth();
690 if (kind_ == DEFSKIP)
691 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
693 return inPixels(height, skip, width);
697 int VSpace::inPixels(int default_height, int default_skip, int default_width)
700 // Height of a normal line in pixels (zoom factor considered)
701 int height = default_height; // [pixels]
703 // Zoom factor specified by user in percent
704 double const zoom = lyxrc.zoom / 100.0; // [percent]
706 // DPI setting for monitor: pixels/inch
707 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
709 // We want the result in pixels
720 // This is how the skips are normally defined by
721 // LateX. But there should be some way to change
722 // this per document.
723 case SMALLSKIP: return height / 4;
724 case MEDSKIP: return height / 2;
725 case BIGSKIP: return height;
726 case VFILL: return 3 * height;
727 // leave space for the vfill symbol
729 // Pixel values are scaled so that the ratio
730 // between lengths and font sizes on the screen
731 // is the same as on paper.
733 // we don't care about sign of value, we
734 // display negative space with text too
736 value = len_.value();
737 int val_sign = value < 0.0 ? -1 : 1;
739 switch (len_.unit()) {
741 // Scaled point: sp = 1/65536 pt
742 result = zoom * dpi * value
743 / (72.27 * 65536); // 4736286.7
746 // Point: 1 pt = 1/72.27 inch
747 result = zoom * dpi * value
751 // Big point: 1 bp = 1/72 inch
752 result = zoom * dpi * value
756 // Didot: 1157dd = 1238 pt?
757 result = zoom * dpi * value
758 / (72.27 / (0.376 * 2.845)); // 67.559735
761 // Millimeter: 1 mm = 1/25.4 inch
762 result = zoom * dpi * value
766 // Pica: 1 pc = 12 pt
767 result = zoom * dpi * value
768 / (72.27 / 12); // 6.0225
771 // Cicero: 1 cc = 12 dd
772 result = zoom * dpi * value
773 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
776 // Centimeter: 1 cm = 1/2.54 inch
777 result = zoom * dpi * value
782 result = zoom * dpi * value;
785 // Ex: The height of an "x"
786 result = zoom * value * height / 2; // what to / width?
788 case LyXLength::EM: // what to / width?
789 // Em: The width of an "m"
790 result = zoom * value * height / 2; // Why 2?
792 case LyXLength::MU: // This is probably only allowed in
794 result = zoom * value * height;
796 case LyXLength::PW: // Always % of workarea
800 result = value * default_width / 100;
802 case LyXLength::UNIT_NONE:
803 result = 0; // this cannot happen
806 return static_cast<int>(result * val_sign + 0.5);
808 return 0; // never reached