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.
14 #include "lengthcommon.h"
16 #include "BufferView.h"
19 #include "support/lstrings.h"
21 using namespace lyx::support;
26 /// used to return numeric values in parsing vspace
27 double number[4] = { 0, 0, 0, 0 };
28 /// used to return unit types in parsing vspace
29 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
32 LyXLength::UNIT_NONE };
34 /// the current position in the number array
36 /// the current position in the unit array
39 /// skip n characters of input
41 void lyx_advance(string & data, string::size_type n)
46 /// return true when the input is at the end
48 bool isEndOfData(string const & data)
50 return ltrim(data).empty();
54 * nextToken - return the next token in the input
55 * @param data input string
56 * @return a char representing the type of token returned
58 * The possible return values are :
59 * + stretch indicator for glue length
60 * - shrink indicator for glue length
61 * n a numeric value (stored in number array)
62 * u a unit type (stored in unit array)
65 char nextToken(string & data)
70 else if (data[0] == '+') {
73 } else if (prefixIs(data, "plus")) {
76 } else if (data[0] == '-') {
79 } else if (prefixIs(data, "minus")) {
83 string::size_type i = data.find_first_not_of("0123456789.");
86 if (number_index > 3) return 'E';
90 // we have found some number
91 if (i == string::npos) {
95 buffer = data.substr(0, i);
99 if (isStrDbl(buffer)) {
100 number[number_index] = strToDbl(buffer);
106 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
108 if (unit_index > 3) return 'E';
112 // we have found some alphabetical string
113 if (i == string::npos) {
117 buffer = data.substr(0, i);
119 // possibly we have "mmplus" string or similar
120 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
121 lyx_advance(data, 2);
122 unit[unit_index] = unitFromString(buffer.substr(0, 2));
124 lyx_advance(data, i);
125 unit[unit_index] = unitFromString(buffer);
128 if (unit[unit_index] != LyXLength::UNIT_NONE) {
131 } else return 'E'; // Error
138 /// latex representation of a vspace
140 char const * pattern;
148 /// the possible formats for a vspace string
149 LaTeXLength table[] = {
150 { "nu", 0, 0, 0, 0 },
151 { "nu+nu", 2, 0, 2, 0 },
152 { "nu+nu-nu", 2, 3, 2, 3 },
153 { "nu+-nu", 2, 2, 2, 2 },
154 { "nu-nu", 0, 2, 0, 2 },
155 { "nu-nu+nu", 3, 2, 3, 2 },
156 { "nu-+nu", 2, 2, 2, 2 },
157 { "n+nu", 2, 0, 1, 0 },
158 { "n+n-nu", 2, 3, 1, 1 },
159 { "n+-nu", 2, 2, 1, 1 },
160 { "n-nu", 0, 2, 0, 1 },
161 { "n-n+nu", 3, 2, 1, 1 },
162 { "n-+nu", 2, 2, 1, 1 },
163 { "", 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 = ltrim(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->len_.value (number[1] * val_sign);
245 result->len_.unit (unit[1]);
246 result->plus_.value (number[table[table_index].plus_val_index]);
247 result->plus_.unit (unit [table[table_index].plus_uni_index]);
248 result->minus_.value(number[table[table_index].minus_val_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];
316 : kind_(NONE), len_(), keep_(false)
320 VSpace::VSpace(vspace_kind k)
321 : kind_(k), len_(), keep_(false)
325 VSpace::VSpace(LyXLength const & l)
326 : kind_(LENGTH), len_(l), keep_(false)
330 VSpace::VSpace(LyXGlueLength const & l)
331 : kind_(LENGTH), len_(l), keep_(false)
335 VSpace::VSpace(string const & data)
336 : kind_(NONE), len_(), keep_(false)
341 string input = rtrim(data);
343 string::size_type const length = input.length();
345 if (length > 1 && input[length - 1] == '*') {
347 input.erase(length - 1);
350 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
351 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
352 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
353 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
354 else if (prefixIs (input, "vfill")) kind_ = VFILL;
355 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
356 else if (isStrDbl(input)) {
357 value = strToDbl(input);
358 // This last one is for reading old .lyx files
359 // without units in added_space_top/bottom.
360 // Let unit default to centimeters here.
362 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
367 VSpace::vspace_kind VSpace::kind() const
373 LyXGlueLength VSpace::length() const
379 bool VSpace::keep() const
385 void VSpace::setKeep(bool val)
391 bool VSpace::operator==(VSpace const & other) const
393 if (kind_ != other.kind_)
397 return this->keep_ == other.keep_;
399 if (len_ != other.len_)
402 return keep_ == other.keep_;
406 string const VSpace::asLyXCommand() const
411 case DEFSKIP: result = "defskip"; break;
412 case SMALLSKIP: result = "smallskip"; break;
413 case MEDSKIP: result = "medskip"; break;
414 case BIGSKIP: result = "bigskip"; break;
415 case VFILL: result = "vfill"; break;
416 case LENGTH: result = len_.asString(); break;
418 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
424 string const VSpace::asLatexCommand(BufferParams const & params) const
432 ret = params.getDefSkip().asLatexCommand(params);
435 ret = keep_ ? "\\vspace*{\\smallskipamount}"
439 ret = keep_ ? "\\vspace*{\\medskipamount}"
443 ret = keep_ ? "\\vspace*{\\bigskipamount}"
447 ret = keep_ ? "\\vspace*{\\fill}"
452 string const lenstr = len_.asLatexString();
454 ret = keep_ ? "\\vspace*{" + lenstr + '}'
455 : "\\vspace{" + lenstr + '}';
466 int VSpace::inPixels(BufferView const & bv) const
468 // Height of a normal line in pixels (zoom factor considered)
469 int const default_height = defaultRowHeight(); // [pixels]
476 // value for this is already set
480 retval = bv.buffer()->params.getDefSkip().inPixels(bv);
483 // This is how the skips are normally defined by LateX.
484 // But there should be some way to change this per document.
486 retval = default_height / 4;
490 retval = default_height / 2;
494 retval = default_height;
498 // leave space for the vfill symbol
499 retval = 3 * default_height;
503 retval = len_.len().inPixels(bv.workWidth());