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 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
44 LyXLength::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] != LyXLength::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, LyXGlueLength * 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 LyXLength 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) return false;
254 pattern[pattern_index] = nextToken (buffer);
255 if (pattern[pattern_index] == 'E') return false;
258 pattern[pattern_index] = '\0';
260 // search "pattern" in "table"
262 while (compare(pattern, table[table_index].pattern)) {
264 if (!*table[table_index].pattern)
268 // Get the values from the appropriate places. If an index
269 // is zero, the corresponding array value is zero or UNIT_NONE,
270 // so we needn't check this.
272 result->len_.value (number[1] * val_sign);
273 result->len_.unit (unit[1]);
274 result->plus_.value (number[table[table_index].plus_val_index]);
275 result->plus_.unit (unit [table[table_index].plus_uni_index]);
276 result->minus_.value(number[table[table_index].minus_val_index]);
277 result->minus_.unit (unit [table[table_index].minus_uni_index]);
283 bool isValidLength(string const & data, LyXLength * result)
285 // This is a trimmed down version of isValidGlueLength.
286 // The parser may seem overkill for lengths without
287 // glue, but since we already have it, using it is
288 // easier than writing something from scratch.
292 string buffer = data;
293 int pattern_index = 0;
296 // To make isValidLength recognize negative values
297 // this little hack is needed:
298 int val_sign = 1; // positive as default
301 lyx_advance(buffer, 1);
305 lyx_advance(buffer, 1);
313 number_index = unit_index = 1; // entries at index 0 are sentinels
315 // construct "pattern" from "data"
316 while (!isEndOfData(buffer)) {
317 if (pattern_index > 2)
319 pattern[pattern_index] = nextToken(buffer);
320 if (pattern[pattern_index] == 'E')
324 pattern[pattern_index] = '\0';
326 // only the most basic pattern is accepted here
327 if (compare(pattern, "nu") != 0) return false;
329 // It _was_ a correct length string.
330 // Store away the values we found.
332 result->val_ = number[1] * val_sign;
333 result->unit_ = unit[1];
344 : kind_(DEFSKIP), len_(), keep_(false)
348 VSpace::VSpace(vspace_kind k)
349 : kind_(k), len_(), keep_(false)
353 VSpace::VSpace(LyXLength const & l)
354 : kind_(LENGTH), len_(l), keep_(false)
358 VSpace::VSpace(LyXGlueLength const & l)
359 : kind_(LENGTH), len_(l), keep_(false)
363 VSpace::VSpace(string const & data)
364 : kind_(DEFSKIP), len_(), keep_(false)
369 string input = rtrim(data);
371 string::size_type const length = input.length();
373 if (length > 1 && input[length - 1] == '*') {
375 input.erase(length - 1);
378 if (prefixIs(input, "defskip"))
380 else if (prefixIs(input, "smallskip"))
382 else if (prefixIs(input, "medskip"))
384 else if (prefixIs(input, "bigskip"))
386 else if (prefixIs(input, "vfill"))
388 else if (isValidGlueLength(input, &len_))
390 else if (isStrDbl(input)) {
391 // This last one is for reading old .lyx files
392 // without units in added_space_top/bottom.
393 // Let unit default to centimeters here.
395 len_ = LyXGlueLength(LyXLength(convert<double>(input), LyXLength::CM));
400 VSpace::vspace_kind VSpace::kind() const
406 LyXGlueLength const & VSpace::length() const
412 bool VSpace::keep() const
418 void VSpace::setKeep(bool val)
424 bool VSpace::operator==(VSpace const & other) const
426 if (kind_ != other.kind_)
430 return this->keep_ == other.keep_;
432 if (len_ != other.len_)
435 return keep_ == other.keep_;
439 string const VSpace::asLyXCommand() const
443 case DEFSKIP: result = "defskip"; break;
444 case SMALLSKIP: result = "smallskip"; break;
445 case MEDSKIP: result = "medskip"; break;
446 case BIGSKIP: result = "bigskip"; break;
447 case VFILL: result = "vfill"; break;
448 case LENGTH: result = len_.asString(); break;
456 string const VSpace::asLatexCommand(BufferParams const & params) const
460 return params.getDefSkip().asLatexCommand(params);
463 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
466 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
469 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
472 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
475 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
476 : "\\vspace{" + len_.asLatexString() + '}';
485 docstring const VSpace::asGUIName() const
490 result = _("Default skip");
493 result = _("Small skip");
496 result = _("Medium skip");
499 result = _("Big skip");
502 result = _("Vertical fill");
505 result = from_ascii(len_.asString());
509 result += ", " + _("protected");
514 int VSpace::inPixels(BufferView const & bv) const
516 // Height of a normal line in pixels (zoom factor considered)
517 int const default_height = defaultRowHeight();
522 return bv.buffer()->params().getDefSkip().inPixels(bv);
524 // This is how the skips are normally defined by LateX.
525 // But there should be some way to change this per document.
527 return default_height / 4;
530 return default_height / 2;
533 return default_height;
536 // leave space for the vfill symbol
537 return 3 * default_height;
540 return len_.len().inPixels(bv.workWidth());