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/convert.h"
21 #include "support/lstrings.h"
23 using lyx::support::compare;
24 using lyx::support::isStrDbl;
25 using lyx::support::ltrim;
26 using lyx::support::prefixIs;
27 using lyx::support::rtrim;
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)
55 /// return true when the input is at the end
57 bool isEndOfData(string const & data)
59 return ltrim(data).empty();
64 * nextToken - return the next token in the input
65 * @param data input string
66 * @return a char representing the type of token returned
68 * The possible return values are :
69 * + stretch indicator for glue length
70 * - shrink indicator for glue length
71 * n a numeric value (stored in number array)
72 * u a unit type (stored in unit array)
75 char nextToken(string & data)
87 if (prefixIs(data, "plus")) {
97 if (prefixIs(data, "minus")) {
102 string::size_type i = data.find_first_not_of("0123456789.");
105 if (number_index > 3)
110 // we have found some number
111 if (i == string::npos) {
115 buffer = data.substr(0, i);
117 lyx_advance(data, i);
119 if (isStrDbl(buffer)) {
120 number[number_index] = convert<double>(buffer);
127 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
134 // we have found some alphabetical string
135 if (i == string::npos) {
139 buffer = data.substr(0, i);
141 // possibly we have "mmplus" string or similar
142 if (buffer.size() > 5 &&
143 (buffer.substr(2, 4) == string("plus") ||
144 buffer.substr(2, 5) == string("minus")))
146 lyx_advance(data, 2);
147 unit[unit_index] = unitFromString(buffer.substr(0, 2));
149 lyx_advance(data, i);
150 unit[unit_index] = unitFromString(buffer);
153 if (unit[unit_index] != LyXLength::UNIT_NONE) {
163 /// latex representation of a vspace
165 char const * pattern;
173 /// the possible formats for a vspace string
174 LaTeXLength table[] = {
175 { "nu", 0, 0, 0, 0 },
176 { "nu+nu", 2, 0, 2, 0 },
177 { "nu+nu-nu", 2, 3, 2, 3 },
178 { "nu+-nu", 2, 2, 2, 2 },
179 { "nu-nu", 0, 2, 0, 2 },
180 { "nu-nu+nu", 3, 2, 3, 2 },
181 { "nu-+nu", 2, 2, 2, 2 },
182 { "n+nu", 2, 0, 1, 0 },
183 { "n+n-nu", 2, 3, 1, 1 },
184 { "n+-nu", 2, 2, 1, 1 },
185 { "n-nu", 0, 2, 0, 1 },
186 { "n-n+nu", 3, 2, 1, 1 },
187 { "n-+nu", 2, 2, 1, 1 },
188 { "", 0, 0, 0, 0 } // sentinel, must be empty
194 const char * stringFromUnit(int unit)
196 if (unit < 0 || unit > num_units)
198 return unit_name[unit];
202 bool isValidGlueLength(string const & data, LyXGlueLength * result)
204 // This parser is table-driven. First, it constructs a "pattern"
205 // that describes the sequence of tokens in "data". For example,
206 // "n-nu" means: number, minus sign, number, unit. As we go along,
207 // numbers and units are stored into static arrays. Then, "pattern"
208 // is searched in the "table". If it is found, the associated
209 // table entries tell us which number and unit should go where
210 // in the LyXLength structure. Example: if "data" has the "pattern"
211 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
212 // That means, "plus_val" is the second number that was seen
213 // in the input, "minus_val" is the third number, and "plus_uni"
214 // and "minus_uni" are the second and third units, respectively.
215 // ("val" and "uni" are always the first items seen in "data".)
216 // This is the most elegant solution I could find -- a straight-
217 // forward approach leads to very long, tedious code that would be
218 // much harder to understand and maintain. (AS)
222 string buffer = ltrim(data);
224 // To make isValidGlueLength recognize negative values as
225 // the first number this little hack is needed:
226 int val_sign = 1; // positive as default
229 lyx_advance(buffer, 1);
233 lyx_advance(buffer, 1);
240 int pattern_index = 0;
245 unit_index = 1; // entries at index 0 are sentinels
247 // construct "pattern" from "data"
248 while (!isEndOfData (buffer)) {
249 if (pattern_index > 20) return false;
250 pattern[pattern_index] = nextToken (buffer);
251 if (pattern[pattern_index] == 'E') return false;
254 pattern[pattern_index] = '\0';
256 // search "pattern" in "table"
258 while (compare(pattern, table[table_index].pattern)) {
260 if (!*table[table_index].pattern)
264 // Get the values from the appropriate places. If an index
265 // is zero, the corresponding array value is zero or UNIT_NONE,
266 // so we needn't check this.
268 result->len_.value (number[1] * val_sign);
269 result->len_.unit (unit[1]);
270 result->plus_.value (number[table[table_index].plus_val_index]);
271 result->plus_.unit (unit [table[table_index].plus_uni_index]);
272 result->minus_.value(number[table[table_index].minus_val_index]);
273 result->minus_.unit (unit [table[table_index].minus_uni_index]);
279 bool isValidLength(string const & data, LyXLength * result)
281 // This is a trimmed down version of isValidGlueLength.
282 // The parser may seem overkill for lengths without
283 // glue, but since we already have it, using it is
284 // easier than writing something from scratch.
288 string buffer = data;
289 int pattern_index = 0;
292 // To make isValidLength recognize negative values
293 // this little hack is needed:
294 int val_sign = 1; // positive as default
297 lyx_advance(buffer, 1);
301 lyx_advance(buffer, 1);
309 number_index = unit_index = 1; // entries at index 0 are sentinels
311 // construct "pattern" from "data"
312 while (!isEndOfData(buffer)) {
313 if (pattern_index > 2)
315 pattern[pattern_index] = nextToken(buffer);
316 if (pattern[pattern_index] == 'E')
320 pattern[pattern_index] = '\0';
322 // only the most basic pattern is accepted here
323 if (compare(pattern, "nu") != 0) return false;
325 // It _was_ a correct length string.
326 // Store away the values we found.
328 result->val_ = number[1] * val_sign;
329 result->unit_ = unit[1];
340 : kind_(DEFSKIP), len_(), keep_(false)
344 VSpace::VSpace(vspace_kind k)
345 : kind_(k), len_(), keep_(false)
349 VSpace::VSpace(LyXLength const & l)
350 : kind_(LENGTH), len_(l), keep_(false)
354 VSpace::VSpace(LyXGlueLength const & l)
355 : kind_(LENGTH), len_(l), keep_(false)
359 VSpace::VSpace(string const & data)
360 : kind_(DEFSKIP), len_(), keep_(false)
365 string input = rtrim(data);
367 string::size_type const length = input.length();
369 if (length > 1 && input[length - 1] == '*') {
371 input.erase(length - 1);
374 if (prefixIs(input, "defskip"))
376 else if (prefixIs(input, "smallskip"))
378 else if (prefixIs(input, "medskip"))
380 else if (prefixIs(input, "bigskip"))
382 else if (prefixIs(input, "vfill"))
384 else if (isValidGlueLength(input, &len_))
386 else if (isStrDbl(input)) {
387 // This last one is for reading old .lyx files
388 // without units in added_space_top/bottom.
389 // Let unit default to centimeters here.
391 len_ = LyXGlueLength(LyXLength(convert<double>(input), LyXLength::CM));
396 VSpace::vspace_kind VSpace::kind() const
402 LyXGlueLength const & VSpace::length() const
408 bool VSpace::keep() const
414 void VSpace::setKeep(bool val)
420 bool VSpace::operator==(VSpace const & other) const
422 if (kind_ != other.kind_)
426 return this->keep_ == other.keep_;
428 if (len_ != other.len_)
431 return keep_ == other.keep_;
435 string const VSpace::asLyXCommand() const
439 case DEFSKIP: result = "defskip"; break;
440 case SMALLSKIP: result = "smallskip"; break;
441 case MEDSKIP: result = "medskip"; break;
442 case BIGSKIP: result = "bigskip"; break;
443 case VFILL: result = "vfill"; break;
444 case LENGTH: result = len_.asString(); break;
452 string const VSpace::asLatexCommand(BufferParams const & params) const
456 return params.getDefSkip().asLatexCommand(params);
459 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
462 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
465 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
468 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
471 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
472 : "\\vspace{" + len_.asLatexString() + '}';
482 int VSpace::inPixels(BufferView const & bv) const
484 // Height of a normal line in pixels (zoom factor considered)
485 int const default_height = defaultRowHeight();
490 return bv.buffer()->params().getDefSkip().inPixels(bv);
492 // This is how the skips are normally defined by LateX.
493 // But there should be some way to change this per document.
495 return default_height / 4;
498 return default_height / 2;
501 return default_height;
504 // leave space for the vfill symbol
505 return 3 * default_height;
508 return len_.len().inPixels(bv.workWidth());