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"
22 using namespace lyx::support;
24 #ifndef CXX_GLOBAL_CSTD
30 /// used to return numeric values in parsing vspace
31 double number[4] = { 0, 0, 0, 0 };
32 /// used to return unit types in parsing vspace
33 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
36 LyXLength::UNIT_NONE };
38 /// the current position in the number array
40 /// the current position in the unit array
43 /// skip n characters of input
45 void lyx_advance(string & data, string::size_type n)
50 /// return true when the input is at the end
52 bool isEndOfData(string const & data)
54 return ltrim(data).empty();
58 * nextToken - return the next token in the input
59 * @param data input string
60 * @return a char representing the type of token returned
62 * The possible return values are :
63 * + stretch indicator for glue length
64 * - shrink indicator for glue length
65 * n a numeric value (stored in number array)
66 * u a unit type (stored in unit array)
69 char nextToken(string & 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
142 /// latex representation of a vspace
144 char const * pattern;
152 /// the possible formats for a vspace string
153 LaTeXLength table[] = {
154 { "nu", 0, 0, 0, 0 },
155 { "nu+nu", 2, 0, 2, 0 },
156 { "nu+nu-nu", 2, 3, 2, 3 },
157 { "nu+-nu", 2, 2, 2, 2 },
158 { "nu-nu", 0, 2, 0, 2 },
159 { "nu-nu+nu", 3, 2, 3, 2 },
160 { "nu-+nu", 2, 2, 2, 2 },
161 { "n+nu", 2, 0, 1, 0 },
162 { "n+n-nu", 2, 3, 1, 1 },
163 { "n+-nu", 2, 2, 1, 1 },
164 { "n-nu", 0, 2, 0, 1 },
165 { "n-n+nu", 3, 2, 1, 1 },
166 { "n-+nu", 2, 2, 1, 1 },
167 { "", 0, 0, 0, 0 } // sentinel, must be empty
173 const char * stringFromUnit(int unit)
175 if (unit < 0 || unit >= num_units)
177 return unit_name[unit];
181 bool isValidGlueLength(string const & data, LyXGlueLength * result)
183 // This parser is table-driven. First, it constructs a "pattern"
184 // that describes the sequence of tokens in "data". For example,
185 // "n-nu" means: number, minus sign, number, unit. As we go along,
186 // numbers and units are stored into static arrays. Then, "pattern"
187 // is searched in the "table". If it is found, the associated
188 // table entries tell us which number and unit should go where
189 // in the LyXLength structure. Example: if "data" has the "pattern"
190 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
191 // That means, "plus_val" is the second number that was seen
192 // in the input, "minus_val" is the third number, and "plus_uni"
193 // and "minus_uni" are the second and third units, respectively.
194 // ("val" and "uni" are always the first items seen in "data".)
195 // This is the most elegant solution I could find -- a straight-
196 // forward approach leads to very long, tedious code that would be
197 // much harder to understand and maintain. (AS)
201 string buffer = ltrim(data);
203 // To make isValidGlueLength recognize negative values as
204 // the first number this little hack is needed:
205 int val_sign = 1; // positive as default
208 lyx_advance(buffer, 1);
212 lyx_advance(buffer, 1);
220 int pattern_index = 0;
225 unit_index = 1; // entries at index 0 are sentinels
227 // construct "pattern" from "data"
228 while (!isEndOfData (buffer)) {
229 if (pattern_index > 20) return false;
230 pattern[pattern_index] = nextToken (buffer);
231 if (pattern[pattern_index] == 'E') return false;
234 pattern[pattern_index] = '\0';
236 // search "pattern" in "table"
238 while (compare(pattern, table[table_index].pattern)) {
240 if (!*table[table_index].pattern)
244 // Get the values from the appropriate places. If an index
245 // is zero, the corresponding array value is zero or UNIT_NONE,
246 // so we needn't check this.
248 result->len_.value (number[1] * val_sign);
249 result->len_.unit (unit[1]);
250 result->plus_.value (number[table[table_index].plus_val_index]);
251 result->plus_.unit (unit [table[table_index].plus_uni_index]);
252 result->minus_.value(number[table[table_index].minus_val_index]);
253 result->minus_.unit (unit [table[table_index].minus_uni_index]);
259 bool isValidLength(string const & data, LyXLength * result)
261 // This is a trimmed down version of isValidGlueLength.
262 // The parser may seem overkill for lengths without
263 // glue, but since we already have it, using it is
264 // easier than writing something from scratch.
268 string buffer = data;
269 int pattern_index = 0;
272 // To make isValidLength recognize negative values
273 // this little hack is needed:
274 int val_sign = 1; // positive as default
277 lyx_advance(buffer, 1);
281 lyx_advance(buffer, 1);
289 number_index = unit_index = 1; // entries at index 0 are sentinels
291 // construct "pattern" from "data"
292 while (!isEndOfData (buffer)) {
293 if (pattern_index > 2)
295 pattern[pattern_index] = nextToken (buffer);
296 if (pattern[pattern_index] == 'E')
300 pattern[pattern_index] = '\0';
302 // only the most basic pattern is accepted here
303 if (compare(pattern, "nu") != 0) return false;
305 // It _was_ a correct length string.
306 // Store away the values we found.
308 result->val_ = number[1] * val_sign;
309 result->unit_ = unit[1];
320 : kind_(NONE), len_(), keep_(false)
324 VSpace::VSpace(vspace_kind k)
325 : kind_(k), len_(), keep_(false)
329 VSpace::VSpace(LyXLength const & l)
330 : kind_(LENGTH), len_(l), keep_(false)
334 VSpace::VSpace(LyXGlueLength const & l)
335 : kind_(LENGTH), len_(l), keep_(false)
339 VSpace::VSpace(string const & data)
340 : kind_(NONE), len_(), keep_(false)
345 string input = rtrim(data);
347 string::size_type const length = input.length();
349 if (length > 1 && input[length - 1] == '*') {
351 input.erase(length - 1);
354 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
355 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
356 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
357 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
358 else if (prefixIs (input, "vfill")) kind_ = VFILL;
359 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
360 else if (sscanf(input.c_str(), "%lf", &value) == 1) {
361 // This last one is for reading old .lyx files
362 // without units in added_space_top/bottom.
363 // Let unit default to centimeters here.
365 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
370 VSpace::vspace_kind VSpace::kind() const
376 LyXGlueLength VSpace::length() const
382 bool VSpace::keep() const
388 void VSpace::setKeep(bool val)
394 bool VSpace::operator==(VSpace const & other) const
396 if (kind_ != other.kind_)
400 return this->keep_ == other.keep_;
402 if (len_ != other.len_)
405 return keep_ == other.keep_;
409 string const VSpace::asLyXCommand() const
414 case DEFSKIP: result = "defskip"; break;
415 case SMALLSKIP: result = "smallskip"; break;
416 case MEDSKIP: result = "medskip"; break;
417 case BIGSKIP: result = "bigskip"; break;
418 case VFILL: result = "vfill"; break;
419 case LENGTH: result = len_.asString(); break;
421 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
427 string const VSpace::asLatexCommand(BufferParams const & params) const
435 ret = params.getDefSkip().asLatexCommand(params);
438 ret = keep_ ? "\\vspace*{\\smallskipamount}"
442 ret = keep_ ? "\\vspace*{\\medskipamount}"
446 ret = keep_ ? "\\vspace*{\\bigskipamount}"
450 ret = keep_ ? "\\vspace*{\\fill}"
455 string const lenstr = len_.asLatexString();
457 ret = keep_ ? "\\vspace*{" + lenstr + '}'
458 : "\\vspace{" + lenstr + '}';
469 int VSpace::inPixels(BufferView const & bv) const
471 // Height of a normal line in pixels (zoom factor considered)
472 int const default_height = defaultRowHeight(); // [pixels]
479 // value for this is already set
483 retval = bv.buffer()->params.getDefSkip().inPixels(bv);
486 // This is how the skips are normally defined by LateX.
487 // But there should be some way to change this per document.
489 retval = default_height / 4;
493 retval = default_height / 2;
497 retval = default_height;
501 // leave space for the vfill symbol
502 retval = 3 * default_height;
506 retval = len_.len().inPixels(bv.workWidth());