3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
6 * \author Matthias Ettrich
12 #include "lengthcommon.h"
15 #include "BufferView.h"
17 #include "support/LAssert.h"
19 #include "support/lstrings.h"
23 #ifndef CXX_GLOBAL_CSTD
29 /// used to return numeric values in parsing vspace
30 double number[4] = { 0, 0, 0, 0 };
31 /// used to return unit types in parsing vspace
32 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
35 LyXLength::UNIT_NONE };
37 /// the current position in the number array
39 /// the current position in the unit array
42 /// skip n characters of input
44 void lyx_advance(string & data, string::size_type n)
49 /// return true when the input is at the end
51 bool isEndOfData(string const & data)
53 return ltrim(data).empty();
57 * nextToken - return the next token in the input
58 * @param data input string
59 * @return a char representing the type of token returned
61 * The possible return values are :
62 * + stretch indicator for glue length
63 * - shrink indicator for glue length
64 * n a numeric value (stored in number array)
65 * u a unit type (stored in unit array)
68 char nextToken(string & data)
73 else if (data[0] == '+') {
76 } else if (prefixIs(data, "plus")) {
79 } else if (data[0] == '-') {
82 } else if (prefixIs(data, "minus")) {
86 string::size_type i = data.find_first_not_of("0123456789.");
89 if (number_index > 3) return 'E';
93 // we have found some number
94 if (i == string::npos) {
98 buffer = data.substr(0, i);
100 lyx_advance(data, i);
102 if (isStrDbl(buffer)) {
103 number[number_index] = strToDbl(buffer);
109 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
111 if (unit_index > 3) return 'E';
115 // we have found some alphabetical string
116 if (i == string::npos) {
120 buffer = data.substr(0, i);
122 // possibly we have "mmplus" string or similar
123 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
124 lyx_advance(data, 2);
125 unit[unit_index] = unitFromString(buffer.substr(0, 2));
127 lyx_advance(data, i);
128 unit[unit_index] = unitFromString(buffer);
131 if (unit[unit_index] != LyXLength::UNIT_NONE) {
134 } else return 'E'; // Error
141 /// latex representation of a vspace
143 char const * pattern;
151 /// the possible formats for a vspace string
152 LaTeXLength table[] = {
153 { "nu", 0, 0, 0, 0 },
154 { "nu+nu", 2, 0, 2, 0 },
155 { "nu+nu-nu", 2, 3, 2, 3 },
156 { "nu+-nu", 2, 2, 2, 2 },
157 { "nu-nu", 0, 2, 0, 2 },
158 { "nu-nu+nu", 3, 2, 3, 2 },
159 { "nu-+nu", 2, 2, 2, 2 },
160 { "n+nu", 2, 0, 1, 0 },
161 { "n+n-nu", 2, 3, 1, 1 },
162 { "n+-nu", 2, 2, 1, 1 },
163 { "n-nu", 0, 2, 0, 1 },
164 { "n-n+nu", 3, 2, 1, 1 },
165 { "n-+nu", 2, 2, 1, 1 },
166 { "", 0, 0, 0, 0 } // sentinel, must be empty
172 const char * stringFromUnit(int unit)
174 if (unit < 0 || unit >= num_units)
176 return unit_name[unit];
180 bool isValidGlueLength(string const & data, LyXGlueLength * result)
182 // This parser is table-driven. First, it constructs a "pattern"
183 // that describes the sequence of tokens in "data". For example,
184 // "n-nu" means: number, minus sign, number, unit. As we go along,
185 // numbers and units are stored into static arrays. Then, "pattern"
186 // is searched in the "table". If it is found, the associated
187 // table entries tell us which number and unit should go where
188 // in the LyXLength structure. Example: if "data" has the "pattern"
189 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
190 // That means, "plus_val" is the second number that was seen
191 // in the input, "minus_val" is the third number, and "plus_uni"
192 // and "minus_uni" are the second and third units, respectively.
193 // ("val" and "uni" are always the first items seen in "data".)
194 // This is the most elegant solution I could find -- a straight-
195 // forward approach leads to very long, tedious code that would be
196 // much harder to understand and maintain. (AS)
200 string buffer = ltrim(data);
202 // To make isValidGlueLength recognize negative values as
203 // the first number this little hack is needed:
204 int val_sign = 1; // positive as default
207 lyx_advance(buffer, 1);
211 lyx_advance(buffer, 1);
219 int pattern_index = 0;
224 unit_index = 1; // entries at index 0 are sentinels
226 // construct "pattern" from "data"
227 while (!isEndOfData (buffer)) {
228 if (pattern_index > 20) return false;
229 pattern[pattern_index] = nextToken (buffer);
230 if (pattern[pattern_index] == 'E') return false;
233 pattern[pattern_index] = '\0';
235 // search "pattern" in "table"
237 while (compare(pattern, table[table_index].pattern)) {
239 if (!*table[table_index].pattern)
243 // Get the values from the appropriate places. If an index
244 // is zero, the corresponding array value is zero or UNIT_NONE,
245 // so we needn't check this.
247 result->len_.value (number[1] * val_sign);
248 result->len_.unit (unit[1]);
249 result->plus_.value (number[table[table_index].plus_val_index]);
250 result->plus_.unit (unit [table[table_index].plus_uni_index]);
251 result->minus_.value(number[table[table_index].minus_val_index]);
252 result->minus_.unit (unit [table[table_index].minus_uni_index]);
258 bool isValidLength(string const & data, LyXLength * result)
260 // This is a trimmed down version of isValidGlueLength.
261 // The parser may seem overkill for lengths without
262 // glue, but since we already have it, using it is
263 // easier than writing something from scratch.
267 string buffer = data;
268 int pattern_index = 0;
271 // To make isValidLength recognize negative values
272 // this little hack is needed:
273 int val_sign = 1; // positive as default
276 lyx_advance(buffer, 1);
280 lyx_advance(buffer, 1);
288 number_index = unit_index = 1; // entries at index 0 are sentinels
290 // construct "pattern" from "data"
291 while (!isEndOfData (buffer)) {
292 if (pattern_index > 2)
294 pattern[pattern_index] = nextToken (buffer);
295 if (pattern[pattern_index] == 'E')
299 pattern[pattern_index] = '\0';
301 // only the most basic pattern is accepted here
302 if (compare(pattern, "nu") != 0) return false;
304 // It _was_ a correct length string.
305 // Store away the values we found.
307 result->val_ = number[1] * val_sign;
308 result->unit_ = unit[1];
319 : kind_(NONE), len_(), keep_(false)
323 VSpace::VSpace(vspace_kind k)
324 : kind_(k), len_(), keep_(false)
328 VSpace::VSpace(LyXLength const & l)
329 : kind_(LENGTH), len_(l), keep_(false)
333 VSpace::VSpace(LyXGlueLength const & l)
334 : kind_(LENGTH), len_(l), keep_(false)
338 VSpace::VSpace(string const & data)
339 : kind_(NONE), len_(), keep_(false)
344 string input = rtrim(data);
346 string::size_type const length = input.length();
348 if (length > 1 && input[length - 1] == '*') {
350 input.erase(length - 1);
353 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
354 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
355 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
356 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
357 else if (prefixIs (input, "vfill")) kind_ = VFILL;
358 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
359 else if (sscanf(input.c_str(), "%lf", &value) == 1) {
360 // This last one is for reading old .lyx files
361 // without units in added_space_top/bottom.
362 // Let unit default to centimeters here.
364 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
369 VSpace::vspace_kind VSpace::kind() const
375 LyXGlueLength VSpace::length() const
381 bool VSpace::keep() const
387 void VSpace::setKeep(bool val)
393 bool VSpace::operator==(VSpace const & other) const
395 if (kind_ != other.kind_)
399 return this->keep_ == other.keep_;
401 if (len_ != other.len_)
404 return keep_ == other.keep_;
408 string const VSpace::asLyXCommand() const
413 case DEFSKIP: result = "defskip"; break;
414 case SMALLSKIP: result = "smallskip"; break;
415 case MEDSKIP: result = "medskip"; break;
416 case BIGSKIP: result = "bigskip"; break;
417 case VFILL: result = "vfill"; break;
418 case LENGTH: result = len_.asString(); break;
420 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
426 string const VSpace::asLatexCommand(BufferParams const & params) const
434 ret = params.getDefSkip().asLatexCommand(params);
437 ret = keep_ ? "\\vspace*{\\smallskipamount}"
441 ret = keep_ ? "\\vspace*{\\medskipamount}"
445 ret = keep_ ? "\\vspace*{\\bigskipamount}"
449 ret = keep_ ? "\\vspace*{\\fill}"
454 string const lenstr = len_.asLatexString();
456 ret = keep_ ? "\\vspace*{" + lenstr + '}'
457 : "\\vspace{" + lenstr + '}';
468 int VSpace::inPixels(BufferView const & bv) const
470 // Height of a normal line in pixels (zoom factor considered)
471 int const default_height = defaultRowHeight(); // [pixels]
478 // value for this is already set
482 retval = bv.buffer()->params.getDefSkip().inPixels(bv);
485 // This is how the skips are normally defined by LateX.
486 // But there should be some way to change this per document.
488 retval = default_height / 4;
492 retval = default_height / 2;
496 retval = default_height;
500 // leave space for the vfill symbol
501 retval = 3 * default_height;
505 retval = len_.len().inPixels(bv.workWidth());