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"
24 using lyx::support::compare;
25 using lyx::support::isStrDbl;
26 using lyx::support::ltrim;
27 using lyx::support::prefixIs;
28 using lyx::support::rtrim;
35 /// used to return numeric values in parsing vspace
36 double number[4] = { 0, 0, 0, 0 };
37 /// used to return unit types in parsing vspace
38 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
41 LyXLength::UNIT_NONE };
43 /// the current position in the number array
45 /// the current position in the unit array
48 /// skip n characters of input
50 void lyx_advance(string & data, string::size_type n)
56 /// return true when the input is at the end
58 bool isEndOfData(string const & data)
60 return ltrim(data).empty();
65 * nextToken - return the next token in the input
66 * @param data input string
67 * @return a char representing the type of token returned
69 * The possible return values are :
70 * + stretch indicator for glue length
71 * - shrink indicator for glue length
72 * n a numeric value (stored in number array)
73 * u a unit type (stored in unit array)
76 char nextToken(string & data)
88 if (prefixIs(data, "plus")) {
98 if (prefixIs(data, "minus")) {
103 string::size_type i = data.find_first_not_of("0123456789.");
106 if (number_index > 3)
111 // we have found some number
112 if (i == string::npos) {
116 buffer = data.substr(0, i);
118 lyx_advance(data, i);
120 if (isStrDbl(buffer)) {
121 number[number_index] = convert<double>(buffer);
128 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
135 // we have found some alphabetical string
136 if (i == string::npos) {
140 buffer = data.substr(0, i);
142 // possibly we have "mmplus" string or similar
143 if (buffer.size() > 5 &&
144 (buffer.substr(2, 4) == string("plus") ||
145 buffer.substr(2, 5) == string("minus")))
147 lyx_advance(data, 2);
148 unit[unit_index] = unitFromString(buffer.substr(0, 2));
150 lyx_advance(data, i);
151 unit[unit_index] = unitFromString(buffer);
154 if (unit[unit_index] != LyXLength::UNIT_NONE) {
164 /// latex representation of a vspace
166 char const * pattern;
174 /// the possible formats for a vspace string
175 LaTeXLength table[] = {
176 { "nu", 0, 0, 0, 0 },
177 { "nu+nu", 2, 0, 2, 0 },
178 { "nu+nu-nu", 2, 3, 2, 3 },
179 { "nu+-nu", 2, 2, 2, 2 },
180 { "nu-nu", 0, 2, 0, 2 },
181 { "nu-nu+nu", 3, 2, 3, 2 },
182 { "nu-+nu", 2, 2, 2, 2 },
183 { "n+nu", 2, 0, 1, 0 },
184 { "n+n-nu", 2, 3, 1, 1 },
185 { "n+-nu", 2, 2, 1, 1 },
186 { "n-nu", 0, 2, 0, 1 },
187 { "n-n+nu", 3, 2, 1, 1 },
188 { "n-+nu", 2, 2, 1, 1 },
189 { "", 0, 0, 0, 0 } // sentinel, must be empty
195 const char * stringFromUnit(int unit)
197 if (unit < 0 || unit > num_units)
199 return unit_name[unit];
203 bool isValidGlueLength(string const & data, LyXGlueLength * result)
205 // This parser is table-driven. First, it constructs a "pattern"
206 // that describes the sequence of tokens in "data". For example,
207 // "n-nu" means: number, minus sign, number, unit. As we go along,
208 // numbers and units are stored into static arrays. Then, "pattern"
209 // is searched in the "table". If it is found, the associated
210 // table entries tell us which number and unit should go where
211 // in the LyXLength structure. Example: if "data" has the "pattern"
212 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
213 // That means, "plus_val" is the second number that was seen
214 // in the input, "minus_val" is the third number, and "plus_uni"
215 // and "minus_uni" are the second and third units, respectively.
216 // ("val" and "uni" are always the first items seen in "data".)
217 // This is the most elegant solution I could find -- a straight-
218 // forward approach leads to very long, tedious code that would be
219 // much harder to understand and maintain. (AS)
223 string buffer = ltrim(data);
225 // To make isValidGlueLength recognize negative values as
226 // the first number this little hack is needed:
227 int val_sign = 1; // positive as default
230 lyx_advance(buffer, 1);
234 lyx_advance(buffer, 1);
241 int pattern_index = 0;
246 unit_index = 1; // entries at index 0 are sentinels
248 // construct "pattern" from "data"
249 while (!isEndOfData (buffer)) {
250 if (pattern_index > 20) return false;
251 pattern[pattern_index] = nextToken (buffer);
252 if (pattern[pattern_index] == 'E') return false;
255 pattern[pattern_index] = '\0';
257 // search "pattern" in "table"
259 while (compare(pattern, table[table_index].pattern)) {
261 if (!*table[table_index].pattern)
265 // Get the values from the appropriate places. If an index
266 // is zero, the corresponding array value is zero or UNIT_NONE,
267 // so we needn't check this.
269 result->len_.value (number[1] * val_sign);
270 result->len_.unit (unit[1]);
271 result->plus_.value (number[table[table_index].plus_val_index]);
272 result->plus_.unit (unit [table[table_index].plus_uni_index]);
273 result->minus_.value(number[table[table_index].minus_val_index]);
274 result->minus_.unit (unit [table[table_index].minus_uni_index]);
280 bool isValidLength(string const & data, LyXLength * result)
282 // This is a trimmed down version of isValidGlueLength.
283 // The parser may seem overkill for lengths without
284 // glue, but since we already have it, using it is
285 // easier than writing something from scratch.
289 string buffer = data;
290 int pattern_index = 0;
293 // To make isValidLength recognize negative values
294 // this little hack is needed:
295 int val_sign = 1; // positive as default
298 lyx_advance(buffer, 1);
302 lyx_advance(buffer, 1);
310 number_index = unit_index = 1; // entries at index 0 are sentinels
312 // construct "pattern" from "data"
313 while (!isEndOfData(buffer)) {
314 if (pattern_index > 2)
316 pattern[pattern_index] = nextToken(buffer);
317 if (pattern[pattern_index] == 'E')
321 pattern[pattern_index] = '\0';
323 // only the most basic pattern is accepted here
324 if (compare(pattern, "nu") != 0) return false;
326 // It _was_ a correct length string.
327 // Store away the values we found.
329 result->val_ = number[1] * val_sign;
330 result->unit_ = unit[1];
341 : kind_(DEFSKIP), len_(), keep_(false)
345 VSpace::VSpace(vspace_kind k)
346 : kind_(k), len_(), keep_(false)
350 VSpace::VSpace(LyXLength const & l)
351 : kind_(LENGTH), len_(l), keep_(false)
355 VSpace::VSpace(LyXGlueLength const & l)
356 : kind_(LENGTH), len_(l), keep_(false)
360 VSpace::VSpace(string const & data)
361 : kind_(DEFSKIP), len_(), keep_(false)
366 string input = rtrim(data);
368 string::size_type const length = input.length();
370 if (length > 1 && input[length - 1] == '*') {
372 input.erase(length - 1);
375 if (prefixIs(input, "defskip"))
377 else if (prefixIs(input, "smallskip"))
379 else if (prefixIs(input, "medskip"))
381 else if (prefixIs(input, "bigskip"))
383 else if (prefixIs(input, "vfill"))
385 else if (isValidGlueLength(input, &len_))
387 else if (isStrDbl(input)) {
388 // This last one is for reading old .lyx files
389 // without units in added_space_top/bottom.
390 // Let unit default to centimeters here.
392 len_ = LyXGlueLength(LyXLength(convert<double>(input), LyXLength::CM));
397 VSpace::vspace_kind VSpace::kind() const
403 LyXGlueLength const & VSpace::length() const
409 bool VSpace::keep() const
415 void VSpace::setKeep(bool val)
421 bool VSpace::operator==(VSpace const & other) const
423 if (kind_ != other.kind_)
427 return this->keep_ == other.keep_;
429 if (len_ != other.len_)
432 return keep_ == other.keep_;
436 string const VSpace::asLyXCommand() const
440 case DEFSKIP: result = "defskip"; break;
441 case SMALLSKIP: result = "smallskip"; break;
442 case MEDSKIP: result = "medskip"; break;
443 case BIGSKIP: result = "bigskip"; break;
444 case VFILL: result = "vfill"; break;
445 case LENGTH: result = len_.asString(); break;
453 string const VSpace::asLatexCommand(BufferParams const & params) const
457 return params.getDefSkip().asLatexCommand(params);
460 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
463 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
466 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
469 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
472 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
473 : "\\vspace{" + len_.asLatexString() + '}';
482 string const VSpace::asGUIName() const
487 result = _("Default skip");
490 result = _("Small skip");
493 result = _("Medium skip");
496 result = _("Big skip");
499 result = _("Vertical fill");
502 result = len_.asString();
506 result += ", " + _("protected");
511 int VSpace::inPixels(BufferView const & bv) const
513 // Height of a normal line in pixels (zoom factor considered)
514 int const default_height = defaultRowHeight();
519 return bv.buffer()->params().getDefSkip().inPixels(bv);
521 // This is how the skips are normally defined by LateX.
522 // But there should be some way to change this per document.
524 return default_height / 4;
527 return default_height / 2;
530 return default_height;
533 // leave space for the vfill symbol
534 return 3 * default_height;
537 return len_.len().inPixels(bv.workWidth());