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 lyx::support::compare;
23 using lyx::support::isStrDbl;
24 using lyx::support::ltrim;
25 using lyx::support::prefixIs;
26 using lyx::support::rtrim;
27 using lyx::support::strToDbl;
34 /// used to return numeric values in parsing vspace
35 double number[4] = { 0, 0, 0, 0 };
36 /// used to return unit types in parsing vspace
37 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
40 LyXLength::UNIT_NONE };
42 /// the current position in the number array
44 /// the current position in the unit array
47 /// skip n characters of input
49 void lyx_advance(string & data, string::size_type n)
54 /// return true when the input is at the end
56 bool isEndOfData(string const & data)
58 return ltrim(data).empty();
62 * nextToken - return the next token in the input
63 * @param data input string
64 * @return a char representing the type of token returned
66 * The possible return values are :
67 * + stretch indicator for glue length
68 * - shrink indicator for glue length
69 * n a numeric value (stored in number array)
70 * u a unit type (stored in unit array)
73 char nextToken(string & data)
78 else if (data[0] == '+') {
81 } else if (prefixIs(data, "plus")) {
84 } else if (data[0] == '-') {
87 } else if (prefixIs(data, "minus")) {
91 string::size_type i = data.find_first_not_of("0123456789.");
94 if (number_index > 3) return 'E';
98 // we have found some number
99 if (i == string::npos) {
103 buffer = data.substr(0, i);
105 lyx_advance(data, i);
107 if (isStrDbl(buffer)) {
108 number[number_index] = strToDbl(buffer);
114 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
116 if (unit_index > 3) return 'E';
120 // we have found some alphabetical string
121 if (i == string::npos) {
125 buffer = data.substr(0, i);
127 // possibly we have "mmplus" string or similar
128 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
129 lyx_advance(data, 2);
130 unit[unit_index] = unitFromString(buffer.substr(0, 2));
132 lyx_advance(data, i);
133 unit[unit_index] = unitFromString(buffer);
136 if (unit[unit_index] != LyXLength::UNIT_NONE) {
139 } else return 'E'; // Error
146 /// latex representation of a vspace
148 char const * pattern;
156 /// the possible formats for a vspace string
157 LaTeXLength table[] = {
158 { "nu", 0, 0, 0, 0 },
159 { "nu+nu", 2, 0, 2, 0 },
160 { "nu+nu-nu", 2, 3, 2, 3 },
161 { "nu+-nu", 2, 2, 2, 2 },
162 { "nu-nu", 0, 2, 0, 2 },
163 { "nu-nu+nu", 3, 2, 3, 2 },
164 { "nu-+nu", 2, 2, 2, 2 },
165 { "n+nu", 2, 0, 1, 0 },
166 { "n+n-nu", 2, 3, 1, 1 },
167 { "n+-nu", 2, 2, 1, 1 },
168 { "n-nu", 0, 2, 0, 1 },
169 { "n-n+nu", 3, 2, 1, 1 },
170 { "n-+nu", 2, 2, 1, 1 },
171 { "", 0, 0, 0, 0 } // sentinel, must be empty
177 const char * stringFromUnit(int unit)
179 if (unit < 0 || unit >= num_units)
181 return unit_name[unit];
185 bool isValidGlueLength(string const & data, LyXGlueLength * result)
187 // This parser is table-driven. First, it constructs a "pattern"
188 // that describes the sequence of tokens in "data". For example,
189 // "n-nu" means: number, minus sign, number, unit. As we go along,
190 // numbers and units are stored into static arrays. Then, "pattern"
191 // is searched in the "table". If it is found, the associated
192 // table entries tell us which number and unit should go where
193 // in the LyXLength structure. Example: if "data" has the "pattern"
194 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
195 // That means, "plus_val" is the second number that was seen
196 // in the input, "minus_val" is the third number, and "plus_uni"
197 // and "minus_uni" are the second and third units, respectively.
198 // ("val" and "uni" are always the first items seen in "data".)
199 // This is the most elegant solution I could find -- a straight-
200 // forward approach leads to very long, tedious code that would be
201 // much harder to understand and maintain. (AS)
205 string buffer = ltrim(data);
207 // To make isValidGlueLength recognize negative values as
208 // the first number this little hack is needed:
209 int val_sign = 1; // positive as default
212 lyx_advance(buffer, 1);
216 lyx_advance(buffer, 1);
224 int pattern_index = 0;
229 unit_index = 1; // entries at index 0 are sentinels
231 // construct "pattern" from "data"
232 while (!isEndOfData (buffer)) {
233 if (pattern_index > 20) return false;
234 pattern[pattern_index] = nextToken (buffer);
235 if (pattern[pattern_index] == 'E') return false;
238 pattern[pattern_index] = '\0';
240 // search "pattern" in "table"
242 while (compare(pattern, table[table_index].pattern)) {
244 if (!*table[table_index].pattern)
248 // Get the values from the appropriate places. If an index
249 // is zero, the corresponding array value is zero or UNIT_NONE,
250 // so we needn't check this.
252 result->len_.value (number[1] * val_sign);
253 result->len_.unit (unit[1]);
254 result->plus_.value (number[table[table_index].plus_val_index]);
255 result->plus_.unit (unit [table[table_index].plus_uni_index]);
256 result->minus_.value(number[table[table_index].minus_val_index]);
257 result->minus_.unit (unit [table[table_index].minus_uni_index]);
263 bool isValidLength(string const & data, LyXLength * result)
265 // This is a trimmed down version of isValidGlueLength.
266 // The parser may seem overkill for lengths without
267 // glue, but since we already have it, using it is
268 // easier than writing something from scratch.
272 string buffer = data;
273 int pattern_index = 0;
276 // To make isValidLength recognize negative values
277 // this little hack is needed:
278 int val_sign = 1; // positive as default
281 lyx_advance(buffer, 1);
285 lyx_advance(buffer, 1);
293 number_index = unit_index = 1; // entries at index 0 are sentinels
295 // construct "pattern" from "data"
296 while (!isEndOfData (buffer)) {
297 if (pattern_index > 2)
299 pattern[pattern_index] = nextToken (buffer);
300 if (pattern[pattern_index] == 'E')
304 pattern[pattern_index] = '\0';
306 // only the most basic pattern is accepted here
307 if (compare(pattern, "nu") != 0) return false;
309 // It _was_ a correct length string.
310 // Store away the values we found.
312 result->val_ = number[1] * val_sign;
313 result->unit_ = unit[1];
324 : kind_(NONE), len_(), keep_(false)
328 VSpace::VSpace(vspace_kind k)
329 : kind_(k), len_(), keep_(false)
333 VSpace::VSpace(LyXLength const & l)
334 : kind_(LENGTH), len_(l), keep_(false)
338 VSpace::VSpace(LyXGlueLength const & l)
339 : kind_(LENGTH), len_(l), keep_(false)
343 VSpace::VSpace(string const & data)
344 : kind_(NONE), len_(), keep_(false)
349 string input = rtrim(data);
351 string::size_type const length = input.length();
353 if (length > 1 && input[length - 1] == '*') {
355 input.erase(length - 1);
358 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
359 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
360 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
361 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
362 else if (prefixIs (input, "vfill")) kind_ = VFILL;
363 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
364 else if (isStrDbl(input)) {
365 value = strToDbl(input);
366 // This last one is for reading old .lyx files
367 // without units in added_space_top/bottom.
368 // Let unit default to centimeters here.
370 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
375 VSpace::vspace_kind VSpace::kind() const
381 LyXGlueLength const & VSpace::length() const
387 bool VSpace::keep() const
393 void VSpace::setKeep(bool val)
399 bool VSpace::operator==(VSpace const & other) const
401 if (kind_ != other.kind_)
405 return this->keep_ == other.keep_;
407 if (len_ != other.len_)
410 return keep_ == other.keep_;
414 string const VSpace::asLyXCommand() const
419 case DEFSKIP: result = "defskip"; break;
420 case SMALLSKIP: result = "smallskip"; break;
421 case MEDSKIP: result = "medskip"; break;
422 case BIGSKIP: result = "bigskip"; break;
423 case VFILL: result = "vfill"; break;
424 case LENGTH: result = len_.asString(); break;
426 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
432 string const VSpace::asLatexCommand(BufferParams const & params) const
440 ret = params.getDefSkip().asLatexCommand(params);
443 ret = keep_ ? "\\vspace*{\\smallskipamount}"
447 ret = keep_ ? "\\vspace*{\\medskipamount}"
451 ret = keep_ ? "\\vspace*{\\bigskipamount}"
455 ret = keep_ ? "\\vspace*{\\fill}"
460 string const lenstr = len_.asLatexString();
462 ret = keep_ ? "\\vspace*{" + lenstr + '}'
463 : "\\vspace{" + lenstr + '}';
474 int VSpace::inPixels(BufferView const & bv) const
476 // Height of a normal line in pixels (zoom factor considered)
477 int const default_height = defaultRowHeight(); // [pixels]
484 // value for this is already set
488 retval = bv.buffer()->params().getDefSkip().inPixels(bv);
491 // This is how the skips are normally defined by LateX.
492 // But there should be some way to change this per document.
494 retval = default_height / 4;
498 retval = default_height / 2;
502 retval = default_height;
506 // leave space for the vfill symbol
507 retval = 3 * default_height;
511 retval = len_.len().inPixels(bv.workWidth());