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.
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "support/gettext.h"
21 #include "TextMetrics.h" // for defaultRowHeight()
23 #include "support/convert.h"
24 #include "support/lstrings.h"
29 using support::compare;
30 using support::isStrDbl;
32 using support::prefixIs;
40 /// used to return numeric values in parsing vspace
41 double number[4] = { 0, 0, 0, 0 };
42 /// used to return unit types in parsing vspace
43 Length::UNIT unit[4] = { Length::UNIT_NONE,
48 /// the current position in the number array
50 /// the current position in the unit array
53 /// skip n characters of input
55 void lyx_advance(string & data, string::size_type n)
61 /// return true when the input is at the end
63 bool isEndOfData(string const & data)
65 return ltrim(data).empty();
70 * nextToken - return the next token in the input
71 * @param data input string
72 * @return a char representing the type of token returned
74 * The possible return values are :
75 * + stretch indicator for glue length
76 * - shrink indicator for glue length
77 * n a numeric value (stored in number array)
78 * u a unit type (stored in unit array)
81 char nextToken(string & data)
93 if (prefixIs(data, "plus")) {
103 if (prefixIs(data, "minus")) {
104 lyx_advance(data, 5);
108 string::size_type i = data.find_first_not_of("0123456789.");
111 if (number_index > 3)
116 // we have found some number
117 if (i == string::npos) {
121 buffer = data.substr(0, i);
123 lyx_advance(data, i);
125 if (isStrDbl(buffer)) {
126 number[number_index] = convert<double>(buffer);
133 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
140 // we have found some alphabetical string
141 if (i == string::npos) {
145 buffer = data.substr(0, i);
147 // possibly we have "mmplus" string or similar
148 if (buffer.size() > 5 &&
149 (buffer.substr(2, 4) == string("plus") ||
150 buffer.substr(2, 5) == string("minus")))
152 lyx_advance(data, 2);
153 unit[unit_index] = unitFromString(buffer.substr(0, 2));
155 lyx_advance(data, i);
156 unit[unit_index] = unitFromString(buffer);
159 if (unit[unit_index] != Length::UNIT_NONE) {
169 /// latex representation of a vspace
171 char const * pattern;
179 /// the possible formats for a vspace string
180 LaTeXLength table[] = {
181 { "nu", 0, 0, 0, 0 },
182 { "nu+nu", 2, 0, 2, 0 },
183 { "nu+nu-nu", 2, 3, 2, 3 },
184 { "nu+-nu", 2, 2, 2, 2 },
185 { "nu-nu", 0, 2, 0, 2 },
186 { "nu-nu+nu", 3, 2, 3, 2 },
187 { "nu-+nu", 2, 2, 2, 2 },
188 { "n+nu", 2, 0, 1, 0 },
189 { "n+n-nu", 2, 3, 1, 1 },
190 { "n+-nu", 2, 2, 1, 1 },
191 { "n-nu", 0, 2, 0, 1 },
192 { "n-n+nu", 3, 2, 1, 1 },
193 { "n-+nu", 2, 2, 1, 1 },
194 { "", 0, 0, 0, 0 } // sentinel, must be empty
200 const char * stringFromUnit(int unit)
202 if (unit < 0 || unit > num_units)
204 return unit_name[unit];
208 bool isValidGlueLength(string const & data, GlueLength * result)
210 // This parser is table-driven. First, it constructs a "pattern"
211 // that describes the sequence of tokens in "data". For example,
212 // "n-nu" means: number, minus sign, number, unit. As we go along,
213 // numbers and units are stored into static arrays. Then, "pattern"
214 // is searched in the "table". If it is found, the associated
215 // table entries tell us which number and unit should go where
216 // in the Length structure. Example: if "data" has the "pattern"
217 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
218 // That means, "plus_val" is the second number that was seen
219 // in the input, "minus_val" is the third number, and "plus_uni"
220 // and "minus_uni" are the second and third units, respectively.
221 // ("val" and "uni" are always the first items seen in "data".)
222 // This is the most elegant solution I could find -- a straight-
223 // forward approach leads to very long, tedious code that would be
224 // much harder to understand and maintain. (AS)
228 string buffer = ltrim(data);
230 // To make isValidGlueLength recognize negative values as
231 // the first number this little hack is needed:
232 int val_sign = 1; // positive as default
235 lyx_advance(buffer, 1);
239 lyx_advance(buffer, 1);
246 int pattern_index = 0;
251 unit_index = 1; // entries at index 0 are sentinels
253 // construct "pattern" from "data"
254 while (!isEndOfData(buffer)) {
255 if (pattern_index > 20)
257 pattern[pattern_index] = nextToken(buffer);
258 if (pattern[pattern_index] == 'E')
262 pattern[pattern_index] = '\0';
264 // search "pattern" in "table"
266 while (compare(pattern, table[table_index].pattern)) {
268 if (!*table[table_index].pattern)
272 // Get the values from the appropriate places. If an index
273 // is zero, the corresponding array value is zero or UNIT_NONE,
274 // so we needn't check this.
276 result->len_.value (number[1] * val_sign);
277 result->len_.unit (unit[1]);
278 result->plus_.value (number[table[table_index].plus_val_index]);
279 result->plus_.unit (unit [table[table_index].plus_uni_index]);
280 result->minus_.value(number[table[table_index].minus_val_index]);
281 result->minus_.unit (unit [table[table_index].minus_uni_index]);
287 bool isValidLength(string const & data, Length * result)
289 // This is a trimmed down version of isValidGlueLength.
290 // The parser may seem overkill for lengths without
291 // glue, but since we already have it, using it is
292 // easier than writing something from scratch.
296 string buffer = data;
297 int pattern_index = 0;
300 // To make isValidLength recognize negative values
301 // this little hack is needed:
302 int val_sign = 1; // positive as default
305 lyx_advance(buffer, 1);
309 lyx_advance(buffer, 1);
317 number_index = unit_index = 1; // entries at index 0 are sentinels
319 // construct "pattern" from "data"
320 while (!isEndOfData(buffer)) {
321 if (pattern_index > 2)
323 pattern[pattern_index] = nextToken(buffer);
324 if (pattern[pattern_index] == 'E')
328 pattern[pattern_index] = '\0';
330 // only the most basic pattern is accepted here
331 if (compare(pattern, "nu") != 0)
334 // It _was_ a correct length string.
335 // Store away the values we found.
337 result->val_ = number[1] * val_sign;
338 result->unit_ = unit[1];
349 : kind_(DEFSKIP), len_(), keep_(false)
353 VSpace::VSpace(VSpaceKind k)
354 : kind_(k), len_(), keep_(false)
358 VSpace::VSpace(Length const & l)
359 : kind_(LENGTH), len_(l), keep_(false)
363 VSpace::VSpace(GlueLength const & l)
364 : kind_(LENGTH), len_(l), keep_(false)
368 VSpace::VSpace(string const & data)
369 : kind_(DEFSKIP), len_(), keep_(false)
374 string input = rtrim(data);
376 string::size_type const length = input.length();
378 if (length > 1 && input[length - 1] == '*') {
380 input.erase(length - 1);
383 if (prefixIs(input, "defskip"))
385 else if (prefixIs(input, "smallskip"))
387 else if (prefixIs(input, "medskip"))
389 else if (prefixIs(input, "bigskip"))
391 else if (prefixIs(input, "vfill"))
393 else if (isValidGlueLength(input, &len_))
395 else if (isStrDbl(input)) {
396 // This last one is for reading old .lyx files
397 // without units in added_space_top/bottom.
398 // Let unit default to centimeters here.
400 len_ = GlueLength(Length(convert<double>(input), Length::CM));
405 bool VSpace::operator==(VSpace const & other) const
407 if (kind_ != other.kind_)
411 return this->keep_ == other.keep_;
413 if (len_ != other.len_)
416 return keep_ == other.keep_;
420 string const VSpace::asLyXCommand() const
424 case DEFSKIP: result = "defskip"; break;
425 case SMALLSKIP: result = "smallskip"; break;
426 case MEDSKIP: result = "medskip"; break;
427 case BIGSKIP: result = "bigskip"; break;
428 case VFILL: result = "vfill"; break;
429 case LENGTH: result = len_.asString(); break;
437 string const VSpace::asLatexCommand(BufferParams const & params) const
441 return params.getDefSkip().asLatexCommand(params);
444 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
447 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
450 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
453 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
456 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
457 : "\\vspace{" + len_.asLatexString() + '}';
466 docstring const VSpace::asGUIName() const
471 result = _("Default skip");
474 result = _("Small skip");
477 result = _("Medium skip");
480 result = _("Big skip");
483 result = _("Vertical fill");
486 result = from_ascii(len_.asString());
490 result += ", " + _("protected");
495 int VSpace::inPixels(BufferView const & bv) const
497 // Height of a normal line in pixels (zoom factor considered)
498 int const default_height = defaultRowHeight();
503 return bv.buffer().params().getDefSkip().inPixels(bv);
505 // This is how the skips are normally defined by LateX.
506 // But there should be some way to change this per document.
508 return default_height / 4;
511 return default_height / 2;
514 return default_height;
517 // leave space for the vfill symbol
518 return 3 * default_height;
521 return len_.len().inPixels(bv.workWidth());