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"
18 #include "lengthcommon.h"
21 #include "support/convert.h"
22 #include "support/lstrings.h"
27 using support::compare;
28 using support::isStrDbl;
30 using support::prefixIs;
38 /// used to return numeric values in parsing vspace
39 double number[4] = { 0, 0, 0, 0 };
40 /// used to return unit types in parsing vspace
41 Length::UNIT unit[4] = { Length::UNIT_NONE,
46 /// the current position in the number array
48 /// the current position in the unit array
51 /// skip n characters of input
53 void lyx_advance(string & data, string::size_type n)
59 /// return true when the input is at the end
61 bool isEndOfData(string const & data)
63 return ltrim(data).empty();
68 * nextToken - return the next token in the input
69 * @param data input string
70 * @return a char representing the type of token returned
72 * The possible return values are :
73 * + stretch indicator for glue length
74 * - shrink indicator for glue length
75 * n a numeric value (stored in number array)
76 * u a unit type (stored in unit array)
79 char nextToken(string & data)
91 if (prefixIs(data, "plus")) {
101 if (prefixIs(data, "minus")) {
102 lyx_advance(data, 5);
106 string::size_type i = data.find_first_not_of("0123456789.");
109 if (number_index > 3)
114 // we have found some number
115 if (i == string::npos) {
119 buffer = data.substr(0, i);
121 lyx_advance(data, i);
123 if (isStrDbl(buffer)) {
124 number[number_index] = convert<double>(buffer);
131 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
138 // we have found some alphabetical string
139 if (i == string::npos) {
143 buffer = data.substr(0, i);
145 // possibly we have "mmplus" string or similar
146 if (buffer.size() > 5 &&
147 (buffer.substr(2, 4) == string("plus") ||
148 buffer.substr(2, 5) == string("minus")))
150 lyx_advance(data, 2);
151 unit[unit_index] = unitFromString(buffer.substr(0, 2));
153 lyx_advance(data, i);
154 unit[unit_index] = unitFromString(buffer);
157 if (unit[unit_index] != Length::UNIT_NONE) {
167 /// latex representation of a vspace
169 char const * pattern;
177 /// the possible formats for a vspace string
178 LaTeXLength table[] = {
179 { "nu", 0, 0, 0, 0 },
180 { "nu+nu", 2, 0, 2, 0 },
181 { "nu+nu-nu", 2, 3, 2, 3 },
182 { "nu+-nu", 2, 2, 2, 2 },
183 { "nu-nu", 0, 2, 0, 2 },
184 { "nu-nu+nu", 3, 2, 3, 2 },
185 { "nu-+nu", 2, 2, 2, 2 },
186 { "n+nu", 2, 0, 1, 0 },
187 { "n+n-nu", 2, 3, 1, 1 },
188 { "n+-nu", 2, 2, 1, 1 },
189 { "n-nu", 0, 2, 0, 1 },
190 { "n-n+nu", 3, 2, 1, 1 },
191 { "n-+nu", 2, 2, 1, 1 },
192 { "", 0, 0, 0, 0 } // sentinel, must be empty
198 const char * stringFromUnit(int unit)
200 if (unit < 0 || unit > num_units)
202 return unit_name[unit];
206 bool isValidGlueLength(string const & data, GlueLength * result)
208 // This parser is table-driven. First, it constructs a "pattern"
209 // that describes the sequence of tokens in "data". For example,
210 // "n-nu" means: number, minus sign, number, unit. As we go along,
211 // numbers and units are stored into static arrays. Then, "pattern"
212 // is searched in the "table". If it is found, the associated
213 // table entries tell us which number and unit should go where
214 // in the Length structure. Example: if "data" has the "pattern"
215 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
216 // That means, "plus_val" is the second number that was seen
217 // in the input, "minus_val" is the third number, and "plus_uni"
218 // and "minus_uni" are the second and third units, respectively.
219 // ("val" and "uni" are always the first items seen in "data".)
220 // This is the most elegant solution I could find -- a straight-
221 // forward approach leads to very long, tedious code that would be
222 // much harder to understand and maintain. (AS)
226 string buffer = ltrim(data);
228 // To make isValidGlueLength recognize negative values as
229 // the first number this little hack is needed:
230 int val_sign = 1; // positive as default
233 lyx_advance(buffer, 1);
237 lyx_advance(buffer, 1);
244 int pattern_index = 0;
249 unit_index = 1; // entries at index 0 are sentinels
251 // construct "pattern" from "data"
252 while (!isEndOfData(buffer)) {
253 if (pattern_index > 20)
255 pattern[pattern_index] = nextToken(buffer);
256 if (pattern[pattern_index] == 'E')
260 pattern[pattern_index] = '\0';
262 // search "pattern" in "table"
264 while (compare(pattern, table[table_index].pattern)) {
266 if (!*table[table_index].pattern)
270 // Get the values from the appropriate places. If an index
271 // is zero, the corresponding array value is zero or UNIT_NONE,
272 // so we needn't check this.
274 result->len_.value (number[1] * val_sign);
275 result->len_.unit (unit[1]);
276 result->plus_.value (number[table[table_index].plus_val_index]);
277 result->plus_.unit (unit [table[table_index].plus_uni_index]);
278 result->minus_.value(number[table[table_index].minus_val_index]);
279 result->minus_.unit (unit [table[table_index].minus_uni_index]);
285 bool isValidLength(string const & data, Length * result)
287 // This is a trimmed down version of isValidGlueLength.
288 // The parser may seem overkill for lengths without
289 // glue, but since we already have it, using it is
290 // easier than writing something from scratch.
294 string buffer = data;
295 int pattern_index = 0;
298 // To make isValidLength recognize negative values
299 // this little hack is needed:
300 int val_sign = 1; // positive as default
303 lyx_advance(buffer, 1);
307 lyx_advance(buffer, 1);
315 number_index = unit_index = 1; // entries at index 0 are sentinels
317 // construct "pattern" from "data"
318 while (!isEndOfData(buffer)) {
319 if (pattern_index > 2)
321 pattern[pattern_index] = nextToken(buffer);
322 if (pattern[pattern_index] == 'E')
326 pattern[pattern_index] = '\0';
328 // only the most basic pattern is accepted here
329 if (compare(pattern, "nu") != 0)
332 // It _was_ a correct length string.
333 // Store away the values we found.
335 result->val_ = number[1] * val_sign;
336 result->unit_ = unit[1];
347 : kind_(DEFSKIP), len_(), keep_(false)
351 VSpace::VSpace(VSpaceKind k)
352 : kind_(k), len_(), keep_(false)
356 VSpace::VSpace(Length const & l)
357 : kind_(LENGTH), len_(l), keep_(false)
361 VSpace::VSpace(GlueLength const & l)
362 : kind_(LENGTH), len_(l), keep_(false)
366 VSpace::VSpace(string const & data)
367 : kind_(DEFSKIP), len_(), keep_(false)
372 string input = rtrim(data);
374 string::size_type const length = input.length();
376 if (length > 1 && input[length - 1] == '*') {
378 input.erase(length - 1);
381 if (prefixIs(input, "defskip"))
383 else if (prefixIs(input, "smallskip"))
385 else if (prefixIs(input, "medskip"))
387 else if (prefixIs(input, "bigskip"))
389 else if (prefixIs(input, "vfill"))
391 else if (isValidGlueLength(input, &len_))
393 else if (isStrDbl(input)) {
394 // This last one is for reading old .lyx files
395 // without units in added_space_top/bottom.
396 // Let unit default to centimeters here.
398 len_ = GlueLength(Length(convert<double>(input), Length::CM));
403 bool VSpace::operator==(VSpace const & other) const
405 if (kind_ != other.kind_)
409 return this->keep_ == other.keep_;
411 if (len_ != other.len_)
414 return keep_ == other.keep_;
418 string const VSpace::asLyXCommand() const
422 case DEFSKIP: result = "defskip"; break;
423 case SMALLSKIP: result = "smallskip"; break;
424 case MEDSKIP: result = "medskip"; break;
425 case BIGSKIP: result = "bigskip"; break;
426 case VFILL: result = "vfill"; break;
427 case LENGTH: result = len_.asString(); break;
435 string const VSpace::asLatexCommand(BufferParams const & params) const
439 return params.getDefSkip().asLatexCommand(params);
442 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
445 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
448 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
451 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
454 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
455 : "\\vspace{" + len_.asLatexString() + '}';
464 docstring const VSpace::asGUIName() const
469 result = _("Default skip");
472 result = _("Small skip");
475 result = _("Medium skip");
478 result = _("Big skip");
481 result = _("Vertical fill");
484 result = from_ascii(len_.asString());
488 result += ", " + _("protected");
493 int VSpace::inPixels(BufferView const & bv) const
495 // Height of a normal line in pixels (zoom factor considered)
496 int const default_height = defaultRowHeight();
501 return bv.buffer().params().getDefSkip().inPixels(bv);
503 // This is how the skips are normally defined by LateX.
504 // But there should be some way to change this per document.
506 return default_height / 4;
509 return default_height / 2;
512 return default_height;
515 // leave space for the vfill symbol
516 return 3 * default_height;
519 return len_.len().inPixels(bv.workWidth());