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"
22 #include "BufferView.h"
24 #include "support/lstrings.h"
34 int const num_units = LyXLength::UNIT_NONE;
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%" };
43 /* The following static items form a simple scanner for
44 * length strings, used by isValid[Glue]Length. See comments there.
46 double number[4] = { 0, 0, 0, 0 };
47 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
50 LyXLength::UNIT_NONE };
56 void lyx_advance(string & data, string::size_type n)
63 bool isEndOfData(string const & data)
65 return frontStrip(data).empty();
69 char nextToken(string & data)
71 data = frontStrip(data);
74 else if (data[0] == '+') {
77 } else if (prefixIs(data, "plus")) {
80 } else if (data[0] == '-') {
83 } else if (prefixIs(data, "minus")) {
87 string::size_type i = data.find_first_not_of("0123456789.");
90 if (number_index > 3) return 'E';
94 // we have found some number
95 if (i == string::npos) {
99 buffer = data.substr(0, i);
101 lyx_advance(data, i);
103 if (isStrDbl(buffer)) {
104 number[number_index] = strToDbl(buffer);
110 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
112 if (unit_index > 3) return 'E';
116 // we have found some alphabetical string
117 if (i == string::npos) {
121 buffer = data.substr(0, i);
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));
128 lyx_advance(data, i);
129 unit[unit_index] = unitFromString(buffer);
132 if (unit[unit_index] != LyXLength::UNIT_NONE) {
135 } else return 'E'; // Error
143 char const * pattern;
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
170 const char * stringFromUnit(int unit)
172 if (unit < 0 || unit >= num_units)
174 return unit_name[unit];
178 bool isValidGlueLength(string const & data, LyXGlueLength * result)
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)
198 string buffer = frontStrip(data);
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
205 lyx_advance(buffer, 1);
209 lyx_advance(buffer, 1);
217 int pattern_index = 0;
222 unit_index = 1; // entries at index 0 are sentinels
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;
231 pattern[pattern_index] = '\0';
233 // search "pattern" in "table"
235 while (compare(pattern, table[table_index].pattern)) {
237 if (!*table[table_index].pattern)
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.
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];
256 bool isValidLength(string const & data, LyXLength * result)
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.
265 string buffer = data;
266 int pattern_index = 0;
269 // To make isValidLength recognize negative values
270 // this little hack is needed:
271 int val_sign = 1; // positive as default
274 lyx_advance(buffer, 1);
278 lyx_advance(buffer, 1);
286 number_index = unit_index = 1; // entries at index 0 are sentinels
288 // construct "pattern" from "data"
289 while (!isEndOfData (buffer)) {
290 if (pattern_index > 2)
292 pattern[pattern_index] = nextToken (buffer);
293 if (pattern[pattern_index] == 'E')
297 pattern[pattern_index] = '\0';
299 // only the most basic pattern is accepted here
300 if (compare(pattern, "nu") != 0) return false;
302 // It _was_ a correct length string.
303 // Store away the values we found.
305 result->val_ = number[1] * val_sign;
306 result->unit_ = unit[1];
318 LyXLength::LyXLength()
319 : val_(0), unit_(LyXLength::PT)
323 LyXLength::LyXLength(double v, LyXLength::UNIT u)
328 LyXLength::LyXLength(string const & data)
332 if (!isValidLength (data, &tmp))
333 return; // should raise an exception
340 string const LyXLength::asString() const
342 ostringstream buffer;
343 buffer << val_ << unit_name[unit_]; // setw?
344 return buffer.str().c_str();
348 string const LyXLength::asLatexString() const
350 ostringstream buffer;
354 buffer << abs(static_cast<int>(val_/100)) << "."
355 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
358 buffer << abs(static_cast<int>(val_/100)) << "."
359 << abs(static_cast<int>(val_)%100) << "\\pagewidth";
362 buffer << abs(static_cast<int>(val_/100)) << "."
363 << abs(static_cast<int>(val_)%100) << "\\linewidth";
366 buffer << val_ << unit_name[unit_]; // setw?
369 return buffer.str().c_str();
373 double LyXLength::value() const
379 LyXLength::UNIT LyXLength::unit() const
385 bool operator==(LyXLength const & l1, LyXLength const & l2)
387 return l1.value() == l2.value() && l1.unit() == l2.unit();
391 LyXLength::UNIT unitFromString (string const & data)
394 while (i < num_units && data != unit_name[i])
396 return static_cast<LyXLength::UNIT>(i);
406 LyXGlueLength::LyXGlueLength(
407 double v, LyXLength::UNIT u,
408 double pv, LyXLength::UNIT pu,
409 double mv, LyXLength::UNIT mu)
411 plus_val_(pv), minus_val_(mv),
412 plus_unit_(pu), minus_unit_(mu)
416 LyXGlueLength::LyXGlueLength(string const & data)
418 LyXGlueLength tmp(0.0, PT);
420 // we should really raise exception here
421 if (!isValidGlueLength(data, &tmp))
426 plus_val_ = tmp.plus_val_;
427 plus_unit_ = tmp.plus_unit_;
428 minus_val_ = tmp.minus_val_;
429 minus_unit_ = tmp.minus_unit_;
433 string const LyXGlueLength::asString() const
435 ostringstream buffer;
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_];
449 if (plus_unit_ == minus_unit_
450 && plus_val_ == minus_val_)
451 buffer << val_ << unit_name[unit_]
453 << unit_name[plus_unit_];
456 buffer << val_ << unit_name[unit_]
458 << unit_name[plus_unit_]
460 << unit_name[minus_unit_];
462 if (unit_ == plus_unit_)
463 buffer << val_ << '+' << plus_val_
466 buffer << val_ << unit_name[unit_]
468 << unit_name[plus_unit_];
471 if (minus_val_ != 0.0)
472 if (unit_ == minus_unit_)
473 buffer << val_ << '-' << minus_val_
477 buffer << val_ << unit_name[unit_]
479 << unit_name[minus_unit_];
481 buffer << val_ << unit_name[unit_];
483 return buffer.str().c_str();
487 string const LyXGlueLength::asLatexString() const
489 ostringstream buffer;
491 if (plus_val_ != 0.0)
492 if (minus_val_ != 0.0)
493 buffer << val_ << unit_name[unit_]
495 << plus_val_ << unit_name[plus_unit_]
497 << minus_val_ << unit_name[minus_unit_];
499 buffer << val_ << unit_name[unit_]
501 << plus_val_ << unit_name[plus_unit_];
503 if (minus_val_ != 0.0)
504 buffer << val_ << unit_name[unit_]
506 << minus_val_ << unit_name[minus_unit_];
508 buffer << val_ << unit_name[unit_];
510 return buffer.str().c_str();
514 double LyXGlueLength::plusValue() const
520 LyXLength::UNIT LyXGlueLength::plusUnit() const
526 double LyXGlueLength::minusValue() const
532 LyXLength::UNIT LyXGlueLength::minusUnit() const
538 bool operator==(LyXGlueLength const & l1, LyXGlueLength const & l2)
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();
549 bool operator!=(LyXGlueLength const & l1, LyXGlueLength const & l2)
561 : kind_(NONE), len_(0.0, LyXLength::PT), keep_(false)
565 VSpace::VSpace(vspace_kind k)
566 : kind_(k), len_(0.0, LyXLength::PT), keep_(false)
570 VSpace::VSpace(LyXGlueLength l)
571 : kind_(LENGTH), len_(l), keep_(false)
575 VSpace::VSpace(double v, LyXLength::UNIT u)
576 : kind_(LENGTH), len_(v, u), keep_(false)
580 VSpace::VSpace(string const & data)
581 : kind_(NONE), len_(0.0, LyXLength::PT), keep_(false)
586 string input = strip(data);
588 string::size_type const length = input.length();
590 if (length > 1 && input[length-1] == '*') {
592 input.erase(length - 1);
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.
606 len_ = LyXGlueLength(value, LyXLength::CM);
611 VSpace::vspace_kind VSpace::kind() const
617 LyXGlueLength VSpace::length() const
623 bool VSpace::keep() const
629 void VSpace::setKeep(bool val)
635 bool VSpace::operator==(VSpace const & other) const
637 if (kind_ != other.kind_)
641 return this->keep_ == other.keep_;
643 if (len_ != other.len_)
646 return keep_ == other.keep_;
650 string const VSpace::asLyXCommand() const
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;
662 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
668 string const VSpace::asLatexCommand(BufferParams const & params) const
671 case NONE: return string();
673 return params.getDefSkip().asLatexCommand(params);
674 case SMALLSKIP: return keep_ ? "\\vspace*{\\smallskipamount}"
676 case MEDSKIP: return keep_ ? "\\vspace*{\\medskipamount}"
678 case BIGSKIP: return keep_ ? "\\vspace*{\\bigskipamount}"
680 case VFILL: return keep_ ? "\\vspace*{\\fill}"
682 case LENGTH: return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
683 : "\\vspace{" + len_.asLatexString() + '}';
685 return string(); // should never be reached
689 int VSpace::inPixels(BufferView * bv) const
691 // Height of a normal line in pixels (zoom factor considered)
692 int height = bv->text->defaultHeight(); // [pixels]
694 int width = bv->workWidth();
696 if (kind_ == DEFSKIP)
697 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
699 return inPixels(height, skip, width);
703 int VSpace::inPixels(int default_height, int default_skip, int default_width)
706 // Height of a normal line in pixels (zoom factor considered)
707 int height = default_height; // [pixels]
709 // Zoom factor specified by user in percent
710 double const zoom = lyxrc.zoom / 100.0; // [percent]
712 // DPI setting for monitor: pixels/inch
713 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
715 // We want the result in pixels
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
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.
739 // we don't care about sign of value, we
740 // display negative space with text too
742 value = len_.value();
743 int val_sign = value < 0.0 ? -1 : 1;
745 switch (len_.unit()) {
747 // Scaled point: sp = 1/65536 pt
748 result = zoom * dpi * value
749 / (72.27 * 65536); // 4736286.7
752 // Point: 1 pt = 1/72.27 inch
753 result = zoom * dpi * value
757 // Big point: 1 bp = 1/72 inch
758 result = zoom * dpi * value
762 // Didot: 1157dd = 1238 pt?
763 result = zoom * dpi * value
764 / (72.27 / (0.376 * 2.845)); // 67.559735
767 // Millimeter: 1 mm = 1/25.4 inch
768 result = zoom * dpi * value
772 // Pica: 1 pc = 12 pt
773 result = zoom * dpi * value
774 / (72.27 / 12); // 6.0225
777 // Cicero: 1 cc = 12 dd
778 result = zoom * dpi * value
779 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
782 // Centimeter: 1 cm = 1/2.54 inch
783 result = zoom * dpi * value
788 result = zoom * dpi * value;
791 // Ex: The height of an "x"
792 result = zoom * value * height / 2; // what to / width?
794 case LyXLength::EM: // what to / width?
795 // Em: The width of an "m"
796 result = zoom * value * height / 2; // Why 2?
798 case LyXLength::MU: // This is probably only allowed in
800 result = zoom * value * height;
802 case LyXLength::PW: // Always % of workarea
806 result = value * default_width / 100;
808 case LyXLength::UNIT_NONE:
809 result = 0; // this cannot happen
812 return static_cast<int>(result * val_sign + 0.5);
814 return 0; // never reached