3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Matthias Ettrich
8 * Full author contact details are available in file CREDITS.
15 #include "bufferparams.h"
16 #include "BufferView.h"
17 #include "lengthcommon.h"
20 #include "support/lstrings.h"
22 using namespace lyx::support;
27 /// used to return numeric values in parsing vspace
28 double number[4] = { 0, 0, 0, 0 };
29 /// used to return unit types in parsing vspace
30 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
33 LyXLength::UNIT_NONE };
35 /// the current position in the number array
37 /// the current position in the unit array
40 /// skip n characters of input
42 void lyx_advance(string & data, string::size_type n)
47 /// return true when the input is at the end
49 bool isEndOfData(string const & data)
51 return ltrim(data).empty();
55 * nextToken - return the next token in the input
56 * @param data input string
57 * @return a char representing the type of token returned
59 * The possible return values are :
60 * + stretch indicator for glue length
61 * - shrink indicator for glue length
62 * n a numeric value (stored in number array)
63 * u a unit type (stored in unit array)
66 char nextToken(string & data)
71 else if (data[0] == '+') {
74 } else if (prefixIs(data, "plus")) {
77 } else if (data[0] == '-') {
80 } else if (prefixIs(data, "minus")) {
84 string::size_type i = data.find_first_not_of("0123456789.");
87 if (number_index > 3) return 'E';
91 // we have found some number
92 if (i == string::npos) {
96 buffer = data.substr(0, i);
100 if (isStrDbl(buffer)) {
101 number[number_index] = strToDbl(buffer);
107 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
109 if (unit_index > 3) return 'E';
113 // we have found some alphabetical string
114 if (i == string::npos) {
118 buffer = data.substr(0, i);
120 // possibly we have "mmplus" string or similar
121 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
122 lyx_advance(data, 2);
123 unit[unit_index] = unitFromString(buffer.substr(0, 2));
125 lyx_advance(data, i);
126 unit[unit_index] = unitFromString(buffer);
129 if (unit[unit_index] != LyXLength::UNIT_NONE) {
132 } else return 'E'; // Error
139 /// latex representation of a vspace
141 char const * pattern;
149 /// the possible formats for a vspace string
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
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 = ltrim(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->len_.value (number[1] * val_sign);
246 result->len_.unit (unit[1]);
247 result->plus_.value (number[table[table_index].plus_val_index]);
248 result->plus_.unit (unit [table[table_index].plus_uni_index]);
249 result->minus_.value(number[table[table_index].minus_val_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];
317 : kind_(NONE), len_(), keep_(false)
321 VSpace::VSpace(vspace_kind k)
322 : kind_(k), len_(), keep_(false)
326 VSpace::VSpace(LyXLength const & l)
327 : kind_(LENGTH), len_(l), keep_(false)
331 VSpace::VSpace(LyXGlueLength const & l)
332 : kind_(LENGTH), len_(l), keep_(false)
336 VSpace::VSpace(string const & data)
337 : kind_(NONE), len_(), keep_(false)
342 string input = rtrim(data);
344 string::size_type const length = input.length();
346 if (length > 1 && input[length - 1] == '*') {
348 input.erase(length - 1);
351 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
352 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
353 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
354 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
355 else if (prefixIs (input, "vfill")) kind_ = VFILL;
356 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
357 else if (isStrDbl(input)) {
358 value = strToDbl(input);
359 // This last one is for reading old .lyx files
360 // without units in added_space_top/bottom.
361 // Let unit default to centimeters here.
363 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
368 VSpace::vspace_kind VSpace::kind() const
374 LyXGlueLength const & VSpace::length() const
380 bool VSpace::keep() const
386 void VSpace::setKeep(bool val)
392 bool VSpace::operator==(VSpace const & other) const
394 if (kind_ != other.kind_)
398 return this->keep_ == other.keep_;
400 if (len_ != other.len_)
403 return keep_ == other.keep_;
407 string const VSpace::asLyXCommand() const
412 case DEFSKIP: result = "defskip"; break;
413 case SMALLSKIP: result = "smallskip"; break;
414 case MEDSKIP: result = "medskip"; break;
415 case BIGSKIP: result = "bigskip"; break;
416 case VFILL: result = "vfill"; break;
417 case LENGTH: result = len_.asString(); break;
419 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
425 string const VSpace::asLatexCommand(BufferParams const & params) const
433 ret = params.getDefSkip().asLatexCommand(params);
436 ret = keep_ ? "\\vspace*{\\smallskipamount}"
440 ret = keep_ ? "\\vspace*{\\medskipamount}"
444 ret = keep_ ? "\\vspace*{\\bigskipamount}"
448 ret = keep_ ? "\\vspace*{\\fill}"
453 string const lenstr = len_.asLatexString();
455 ret = keep_ ? "\\vspace*{" + lenstr + '}'
456 : "\\vspace{" + lenstr + '}';
467 int VSpace::inPixels(BufferView const & bv) const
469 // Height of a normal line in pixels (zoom factor considered)
470 int const default_height = defaultRowHeight(); // [pixels]
477 // value for this is already set
481 retval = bv.buffer()->params().getDefSkip().inPixels(bv);
484 // This is how the skips are normally defined by LateX.
485 // But there should be some way to change this per document.
487 retval = default_height / 4;
491 retval = default_height / 2;
495 retval = default_height;
499 // leave space for the vfill symbol
500 retval = 3 * default_height;
504 retval = len_.len().inPixels(bv.workWidth());