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"
30 using support::compare;
31 using support::isStrDbl;
33 using support::prefixIs;
39 /// used to return numeric values in parsing vspace
40 double number[4] = { 0, 0, 0, 0 };
41 /// used to return unit types in parsing vspace
42 Length::UNIT unit[4] = { Length::UNIT_NONE,
47 /// the current position in the number array
49 /// the current position in the unit array
52 /// skip n characters of input
54 void lyx_advance(string & data, string::size_type n)
60 /// return true when the input is at the end
62 bool isEndOfData(string const & data)
64 return ltrim(data).empty();
69 * nextToken - return the next token in the input
70 * @param data input string
71 * @return a char representing the type of token returned
73 * The possible return values are :
74 * + stretch indicator for glue length
75 * - shrink indicator for glue length
76 * n a numeric value (stored in number array)
77 * u a unit type (stored in unit array)
80 char nextToken(string & data)
92 if (prefixIs(data, "plus")) {
102 if (prefixIs(data, "minus")) {
103 lyx_advance(data, 5);
107 string::size_type i = data.find_first_not_of("0123456789.");
110 if (number_index > 3)
115 // we have found some number
116 if (i == string::npos) {
120 buffer = data.substr(0, i);
122 lyx_advance(data, i);
124 if (isStrDbl(buffer)) {
125 number[number_index] = convert<double>(buffer);
132 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
139 // we have found some alphabetical string
140 if (i == string::npos) {
144 buffer = data.substr(0, i);
146 // possibly we have "mmplus" string or similar
147 if (buffer.size() > 5 &&
148 (buffer.substr(2, 4) == string("plus") ||
149 buffer.substr(2, 5) == string("minus")))
151 lyx_advance(data, 2);
152 unit[unit_index] = unitFromString(buffer.substr(0, 2));
154 lyx_advance(data, i);
155 unit[unit_index] = unitFromString(buffer);
158 if (unit[unit_index] != Length::UNIT_NONE) {
168 /// latex representation of a vspace
170 char const * pattern;
178 /// the possible formats for a vspace string
179 LaTeXLength table[] = {
180 { "nu", 0, 0, 0, 0 },
181 { "nu+nu", 2, 0, 2, 0 },
182 { "nu+nu-nu", 2, 3, 2, 3 },
183 { "nu+-nu", 2, 2, 2, 2 },
184 { "nu-nu", 0, 2, 0, 2 },
185 { "nu-nu+nu", 3, 2, 3, 2 },
186 { "nu-+nu", 2, 2, 2, 2 },
187 { "n+nu", 2, 0, 1, 0 },
188 { "n+n-nu", 2, 3, 1, 1 },
189 { "n+-nu", 2, 2, 1, 1 },
190 { "n-nu", 0, 2, 0, 1 },
191 { "n-n+nu", 3, 2, 1, 1 },
192 { "n-+nu", 2, 2, 1, 1 },
193 { "", 0, 0, 0, 0 } // sentinel, must be empty
199 const char * stringFromUnit(int unit)
201 if (unit < 0 || unit > num_units)
203 return unit_name[unit];
207 bool isValidGlueLength(string const & data, GlueLength * result)
209 // This parser is table-driven. First, it constructs a "pattern"
210 // that describes the sequence of tokens in "data". For example,
211 // "n-nu" means: number, minus sign, number, unit. As we go along,
212 // numbers and units are stored into static arrays. Then, "pattern"
213 // is searched in the "table". If it is found, the associated
214 // table entries tell us which number and unit should go where
215 // in the Length structure. Example: if "data" has the "pattern"
216 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
217 // That means, "plus_val" is the second number that was seen
218 // in the input, "minus_val" is the third number, and "plus_uni"
219 // and "minus_uni" are the second and third units, respectively.
220 // ("val" and "uni" are always the first items seen in "data".)
221 // This is the most elegant solution I could find -- a straight-
222 // forward approach leads to very long, tedious code that would be
223 // much harder to understand and maintain. (AS)
227 string buffer = ltrim(data);
229 // To make isValidGlueLength recognize negative values as
230 // the first number this little hack is needed:
231 int val_sign = 1; // positive as default
234 lyx_advance(buffer, 1);
238 lyx_advance(buffer, 1);
245 int pattern_index = 0;
250 unit_index = 1; // entries at index 0 are sentinels
252 // construct "pattern" from "data"
253 while (!isEndOfData(buffer)) {
254 if (pattern_index > 20)
256 pattern[pattern_index] = nextToken(buffer);
257 if (pattern[pattern_index] == 'E')
261 pattern[pattern_index] = '\0';
263 // search "pattern" in "table"
265 while (compare(pattern, table[table_index].pattern)) {
267 if (!*table[table_index].pattern)
271 // Get the values from the appropriate places. If an index
272 // is zero, the corresponding array value is zero or UNIT_NONE,
273 // so we needn't check this.
275 result->len_.value (number[1] * val_sign);
276 result->len_.unit (unit[1]);
277 result->plus_.value (number[table[table_index].plus_val_index]);
278 result->plus_.unit (unit [table[table_index].plus_uni_index]);
279 result->minus_.value(number[table[table_index].minus_val_index]);
280 result->minus_.unit (unit [table[table_index].minus_uni_index]);
286 bool isValidLength(string const & data, Length * result)
288 // This is a trimmed down version of isValidGlueLength.
289 // The parser may seem overkill for lengths without
290 // glue, but since we already have it, using it is
291 // easier than writing something from scratch.
295 string buffer = data;
296 int pattern_index = 0;
299 // To make isValidLength recognize negative values
300 // this little hack is needed:
301 int val_sign = 1; // positive as default
304 lyx_advance(buffer, 1);
308 lyx_advance(buffer, 1);
316 number_index = unit_index = 1; // entries at index 0 are sentinels
318 // construct "pattern" from "data"
319 while (!isEndOfData(buffer)) {
320 if (pattern_index > 2)
322 pattern[pattern_index] = nextToken(buffer);
323 if (pattern[pattern_index] == 'E')
327 pattern[pattern_index] = '\0';
329 // only the most basic pattern is accepted here
330 if (compare(pattern, "nu") != 0)
333 // It _was_ a correct length string.
334 // Store away the values we found.
336 result->val_ = number[1] * val_sign;
337 result->unit_ = unit[1];
348 : kind_(DEFSKIP), len_(), keep_(false)
352 VSpace::VSpace(VSpaceKind k)
353 : kind_(k), len_(), keep_(false)
357 VSpace::VSpace(Length const & l)
358 : kind_(LENGTH), len_(l), keep_(false)
362 VSpace::VSpace(GlueLength const & l)
363 : kind_(LENGTH), len_(l), keep_(false)
367 VSpace::VSpace(string const & data)
368 : kind_(DEFSKIP), len_(), keep_(false)
373 string input = rtrim(data);
375 string::size_type const length = input.length();
377 if (length > 1 && input[length - 1] == '*') {
379 input.erase(length - 1);
382 if (prefixIs(input, "defskip"))
384 else if (prefixIs(input, "smallskip"))
386 else if (prefixIs(input, "medskip"))
388 else if (prefixIs(input, "bigskip"))
390 else if (prefixIs(input, "vfill"))
392 else if (isValidGlueLength(input, &len_))
394 else if (isStrDbl(input)) {
395 // This last one is for reading old .lyx files
396 // without units in added_space_top/bottom.
397 // Let unit default to centimeters here.
399 len_ = GlueLength(Length(convert<double>(input), Length::CM));
404 bool VSpace::operator==(VSpace const & other) const
406 if (kind_ != other.kind_)
410 return this->keep_ == other.keep_;
412 if (len_ != other.len_)
415 return keep_ == other.keep_;
419 string const VSpace::asLyXCommand() const
423 case DEFSKIP: result = "defskip"; break;
424 case SMALLSKIP: result = "smallskip"; break;
425 case MEDSKIP: result = "medskip"; break;
426 case BIGSKIP: result = "bigskip"; break;
427 case VFILL: result = "vfill"; break;
428 case LENGTH: result = len_.asString(); break;
436 string const VSpace::asLatexCommand(BufferParams const & params) const
440 return params.getDefSkip().asLatexCommand(params);
443 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
446 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
449 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
452 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
455 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
456 : "\\vspace{" + len_.asLatexString() + '}';
465 docstring const VSpace::asGUIName() const
470 result = _("Default skip");
473 result = _("Small skip");
476 result = _("Medium skip");
479 result = _("Big skip");
482 result = _("Vertical fill");
485 result = from_ascii(len_.asString());
489 result += ", " + _("protected");
494 int VSpace::inPixels(BufferView const & bv) const
496 // Height of a normal line in pixels (zoom factor considered)
497 int const default_height = defaultRowHeight();
502 return bv.buffer().params().getDefSkip().inPixels(bv);
504 // This is how the skips are normally defined by LateX.
505 // But there should be some way to change this per document.
507 return default_height / 4;
510 return default_height / 2;
513 return default_height;
516 // leave space for the vfill symbol
517 return 3 * default_height;
520 return len_.len().inPixels(bv.workWidth());