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"
27 using namespace lyx::support;
33 /// used to return numeric values in parsing vspace
34 double number[4] = { 0, 0, 0, 0 };
35 /// used to return unit types in parsing vspace
36 Length::UNIT unit[4] = { Length::UNIT_NONE,
41 /// the current position in the number array
43 /// the current position in the unit array
46 /// skip n characters of input
48 void lyx_advance(string & data, string::size_type n)
54 /// return true when the input is at the end
56 bool isEndOfData(string const & data)
58 return ltrim(data).empty();
63 * nextToken - return the next token in the input
64 * @param data input string
65 * @return a char representing the type of token returned
67 * The possible return values are :
68 * + stretch indicator for glue length
69 * - shrink indicator for glue length
70 * n a numeric value (stored in number array)
71 * u a unit type (stored in unit array)
74 char nextToken(string & data)
86 if (prefixIs(data, "plus")) {
96 if (prefixIs(data, "minus")) {
101 string::size_type i = data.find_first_not_of("0123456789.");
104 if (number_index > 3)
109 // we have found some number
110 if (i == string::npos) {
114 buffer = data.substr(0, i);
116 lyx_advance(data, i);
118 if (isStrDbl(buffer)) {
119 number[number_index] = convert<double>(buffer);
126 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
133 // we have found some alphabetical string
134 if (i == string::npos) {
138 buffer = data.substr(0, i);
140 // possibly we have "mmplus" string or similar
141 if (buffer.size() > 5 &&
142 (buffer.substr(2, 4) == string("plus") ||
143 buffer.substr(2, 5) == string("minus")))
145 lyx_advance(data, 2);
146 unit[unit_index] = unitFromString(buffer.substr(0, 2));
148 lyx_advance(data, i);
149 unit[unit_index] = unitFromString(buffer);
152 if (unit[unit_index] != Length::UNIT_NONE) {
162 /// latex representation of a vspace
164 char const * pattern;
172 /// the possible formats for a vspace string
173 LaTeXLength table[] = {
174 { "nu", 0, 0, 0, 0 },
175 { "nu+nu", 2, 0, 2, 0 },
176 { "nu+nu-nu", 2, 3, 2, 3 },
177 { "nu+-nu", 2, 2, 2, 2 },
178 { "nu-nu", 0, 2, 0, 2 },
179 { "nu-nu+nu", 3, 2, 3, 2 },
180 { "nu-+nu", 2, 2, 2, 2 },
181 { "n+nu", 2, 0, 1, 0 },
182 { "n+n-nu", 2, 3, 1, 1 },
183 { "n+-nu", 2, 2, 1, 1 },
184 { "n-nu", 0, 2, 0, 1 },
185 { "n-n+nu", 3, 2, 1, 1 },
186 { "n-+nu", 2, 2, 1, 1 },
187 { "", 0, 0, 0, 0 } // sentinel, must be empty
193 const char * stringFromUnit(int unit)
195 if (unit < 0 || unit > num_units)
197 return unit_name[unit];
201 bool isValidGlueLength(string const & data, GlueLength * result)
203 // This parser is table-driven. First, it constructs a "pattern"
204 // that describes the sequence of tokens in "data". For example,
205 // "n-nu" means: number, minus sign, number, unit. As we go along,
206 // numbers and units are stored into static arrays. Then, "pattern"
207 // is searched in the "table". If it is found, the associated
208 // table entries tell us which number and unit should go where
209 // in the Length structure. Example: if "data" has the "pattern"
210 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
211 // That means, "plus_val" is the second number that was seen
212 // in the input, "minus_val" is the third number, and "plus_uni"
213 // and "minus_uni" are the second and third units, respectively.
214 // ("val" and "uni" are always the first items seen in "data".)
215 // This is the most elegant solution I could find -- a straight-
216 // forward approach leads to very long, tedious code that would be
217 // much harder to understand and maintain. (AS)
221 string buffer = ltrim(data);
223 // To make isValidGlueLength recognize negative values as
224 // the first number this little hack is needed:
225 int val_sign = 1; // positive as default
228 lyx_advance(buffer, 1);
232 lyx_advance(buffer, 1);
239 int pattern_index = 0;
244 unit_index = 1; // entries at index 0 are sentinels
246 // construct "pattern" from "data"
247 while (!isEndOfData(buffer)) {
248 if (pattern_index > 20)
250 pattern[pattern_index] = nextToken(buffer);
251 if (pattern[pattern_index] == 'E')
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, Length * 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)
327 // It _was_ a correct length string.
328 // Store away the values we found.
330 result->val_ = number[1] * val_sign;
331 result->unit_ = unit[1];
342 : kind_(DEFSKIP), len_(), keep_(false)
346 VSpace::VSpace(VSpaceKind k)
347 : kind_(k), len_(), keep_(false)
351 VSpace::VSpace(Length const & l)
352 : kind_(LENGTH), len_(l), keep_(false)
356 VSpace::VSpace(GlueLength const & l)
357 : kind_(LENGTH), len_(l), keep_(false)
361 VSpace::VSpace(string const & data)
362 : kind_(DEFSKIP), len_(), keep_(false)
367 string input = rtrim(data);
369 string::size_type const length = input.length();
371 if (length > 1 && input[length - 1] == '*') {
373 input.erase(length - 1);
376 if (prefixIs(input, "defskip"))
378 else if (prefixIs(input, "smallskip"))
380 else if (prefixIs(input, "medskip"))
382 else if (prefixIs(input, "bigskip"))
384 else if (prefixIs(input, "vfill"))
386 else if (isValidGlueLength(input, &len_))
388 else if (isStrDbl(input)) {
389 // This last one is for reading old .lyx files
390 // without units in added_space_top/bottom.
391 // Let unit default to centimeters here.
393 len_ = GlueLength(Length(convert<double>(input), Length::CM));
398 bool VSpace::operator==(VSpace const & other) const
400 if (kind_ != other.kind_)
404 return this->keep_ == other.keep_;
406 if (len_ != other.len_)
409 return keep_ == other.keep_;
413 string const VSpace::asLyXCommand() const
417 case DEFSKIP: result = "defskip"; break;
418 case SMALLSKIP: result = "smallskip"; break;
419 case MEDSKIP: result = "medskip"; break;
420 case BIGSKIP: result = "bigskip"; break;
421 case VFILL: result = "vfill"; break;
422 case LENGTH: result = len_.asString(); break;
430 string const VSpace::asLatexCommand(BufferParams const & params) const
434 return params.getDefSkip().asLatexCommand(params);
437 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
440 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
443 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
446 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
449 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
450 : "\\vspace{" + len_.asLatexString() + '}';
459 docstring const VSpace::asGUIName() const
464 result = _("Default skip");
467 result = _("Small skip");
470 result = _("Medium skip");
473 result = _("Big skip");
476 result = _("Vertical fill");
479 result = from_ascii(len_.asString());
483 result += ", " + _("protected");
488 int VSpace::inPixels(BufferView const & bv) const
490 // Height of a normal line in pixels (zoom factor considered)
491 int const default_height = defaultRowHeight();
496 return bv.buffer().params().getDefSkip().inPixels(bv);
498 // This is how the skips are normally defined by LateX.
499 // But there should be some way to change this per document.
501 return default_height / 4;
504 return default_height / 2;
507 return default_height;
510 // leave space for the vfill symbol
511 return 3 * default_height;
514 return len_.len().inPixels(bv.workWidth());