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 using namespace lyx::support;
25 #ifndef CXX_GLOBAL_CSTD
31 /// used to return numeric values in parsing vspace
32 double number[4] = { 0, 0, 0, 0 };
33 /// used to return unit types in parsing vspace
34 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
37 LyXLength::UNIT_NONE };
39 /// the current position in the number array
41 /// the current position in the unit array
44 /// skip n characters of input
46 void lyx_advance(string & data, string::size_type n)
51 /// return true when the input is at the end
53 bool isEndOfData(string const & data)
55 return ltrim(data).empty();
59 * nextToken - return the next token in the input
60 * @param data input string
61 * @return a char representing the type of token returned
63 * The possible return values are :
64 * + stretch indicator for glue length
65 * - shrink indicator for glue length
66 * n a numeric value (stored in number array)
67 * u a unit type (stored in unit array)
70 char nextToken(string & data)
75 else if (data[0] == '+') {
78 } else if (prefixIs(data, "plus")) {
81 } else if (data[0] == '-') {
84 } else if (prefixIs(data, "minus")) {
88 string::size_type i = data.find_first_not_of("0123456789.");
91 if (number_index > 3) return 'E';
95 // we have found some number
96 if (i == string::npos) {
100 buffer = data.substr(0, i);
102 lyx_advance(data, i);
104 if (isStrDbl(buffer)) {
105 number[number_index] = strToDbl(buffer);
111 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
113 if (unit_index > 3) return 'E';
117 // we have found some alphabetical string
118 if (i == string::npos) {
122 buffer = data.substr(0, i);
124 // possibly we have "mmplus" string or similar
125 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
126 lyx_advance(data, 2);
127 unit[unit_index] = unitFromString(buffer.substr(0, 2));
129 lyx_advance(data, i);
130 unit[unit_index] = unitFromString(buffer);
133 if (unit[unit_index] != LyXLength::UNIT_NONE) {
136 } else return 'E'; // Error
143 /// latex representation of a vspace
145 char const * pattern;
153 /// the possible formats for a vspace string
154 LaTeXLength table[] = {
155 { "nu", 0, 0, 0, 0 },
156 { "nu+nu", 2, 0, 2, 0 },
157 { "nu+nu-nu", 2, 3, 2, 3 },
158 { "nu+-nu", 2, 2, 2, 2 },
159 { "nu-nu", 0, 2, 0, 2 },
160 { "nu-nu+nu", 3, 2, 3, 2 },
161 { "nu-+nu", 2, 2, 2, 2 },
162 { "n+nu", 2, 0, 1, 0 },
163 { "n+n-nu", 2, 3, 1, 1 },
164 { "n+-nu", 2, 2, 1, 1 },
165 { "n-nu", 0, 2, 0, 1 },
166 { "n-n+nu", 3, 2, 1, 1 },
167 { "n-+nu", 2, 2, 1, 1 },
168 { "", 0, 0, 0, 0 } // sentinel, must be empty
174 const char * stringFromUnit(int unit)
176 if (unit < 0 || unit >= num_units)
178 return unit_name[unit];
182 bool isValidGlueLength(string const & data, LyXGlueLength * result)
184 // This parser is table-driven. First, it constructs a "pattern"
185 // that describes the sequence of tokens in "data". For example,
186 // "n-nu" means: number, minus sign, number, unit. As we go along,
187 // numbers and units are stored into static arrays. Then, "pattern"
188 // is searched in the "table". If it is found, the associated
189 // table entries tell us which number and unit should go where
190 // in the LyXLength structure. Example: if "data" has the "pattern"
191 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
192 // That means, "plus_val" is the second number that was seen
193 // in the input, "minus_val" is the third number, and "plus_uni"
194 // and "minus_uni" are the second and third units, respectively.
195 // ("val" and "uni" are always the first items seen in "data".)
196 // This is the most elegant solution I could find -- a straight-
197 // forward approach leads to very long, tedious code that would be
198 // much harder to understand and maintain. (AS)
202 string buffer = ltrim(data);
204 // To make isValidGlueLength recognize negative values as
205 // the first number this little hack is needed:
206 int val_sign = 1; // positive as default
209 lyx_advance(buffer, 1);
213 lyx_advance(buffer, 1);
221 int pattern_index = 0;
226 unit_index = 1; // entries at index 0 are sentinels
228 // construct "pattern" from "data"
229 while (!isEndOfData (buffer)) {
230 if (pattern_index > 20) return false;
231 pattern[pattern_index] = nextToken (buffer);
232 if (pattern[pattern_index] == 'E') return false;
235 pattern[pattern_index] = '\0';
237 // search "pattern" in "table"
239 while (compare(pattern, table[table_index].pattern)) {
241 if (!*table[table_index].pattern)
245 // Get the values from the appropriate places. If an index
246 // is zero, the corresponding array value is zero or UNIT_NONE,
247 // so we needn't check this.
249 result->len_.value (number[1] * val_sign);
250 result->len_.unit (unit[1]);
251 result->plus_.value (number[table[table_index].plus_val_index]);
252 result->plus_.unit (unit [table[table_index].plus_uni_index]);
253 result->minus_.value(number[table[table_index].minus_val_index]);
254 result->minus_.unit (unit [table[table_index].minus_uni_index]);
260 bool isValidLength(string const & data, LyXLength * result)
262 // This is a trimmed down version of isValidGlueLength.
263 // The parser may seem overkill for lengths without
264 // glue, but since we already have it, using it is
265 // easier than writing something from scratch.
269 string buffer = data;
270 int pattern_index = 0;
273 // To make isValidLength recognize negative values
274 // this little hack is needed:
275 int val_sign = 1; // positive as default
278 lyx_advance(buffer, 1);
282 lyx_advance(buffer, 1);
290 number_index = unit_index = 1; // entries at index 0 are sentinels
292 // construct "pattern" from "data"
293 while (!isEndOfData (buffer)) {
294 if (pattern_index > 2)
296 pattern[pattern_index] = nextToken (buffer);
297 if (pattern[pattern_index] == 'E')
301 pattern[pattern_index] = '\0';
303 // only the most basic pattern is accepted here
304 if (compare(pattern, "nu") != 0) return false;
306 // It _was_ a correct length string.
307 // Store away the values we found.
309 result->val_ = number[1] * val_sign;
310 result->unit_ = unit[1];
321 : kind_(NONE), len_(), keep_(false)
325 VSpace::VSpace(vspace_kind k)
326 : kind_(k), len_(), keep_(false)
330 VSpace::VSpace(LyXLength const & l)
331 : kind_(LENGTH), len_(l), keep_(false)
335 VSpace::VSpace(LyXGlueLength const & l)
336 : kind_(LENGTH), len_(l), keep_(false)
340 VSpace::VSpace(string const & data)
341 : kind_(NONE), len_(), keep_(false)
346 string input = rtrim(data);
348 string::size_type const length = input.length();
350 if (length > 1 && input[length - 1] == '*') {
352 input.erase(length - 1);
355 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
356 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
357 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
358 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
359 else if (prefixIs (input, "vfill")) kind_ = VFILL;
360 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
361 else if (sscanf(input.c_str(), "%lf", &value) == 1) {
362 // This last one is for reading old .lyx files
363 // without units in added_space_top/bottom.
364 // Let unit default to centimeters here.
366 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
371 VSpace::vspace_kind VSpace::kind() const
377 LyXGlueLength VSpace::length() const
383 bool VSpace::keep() const
389 void VSpace::setKeep(bool val)
395 bool VSpace::operator==(VSpace const & other) const
397 if (kind_ != other.kind_)
401 return this->keep_ == other.keep_;
403 if (len_ != other.len_)
406 return keep_ == other.keep_;
410 string const VSpace::asLyXCommand() const
415 case DEFSKIP: result = "defskip"; break;
416 case SMALLSKIP: result = "smallskip"; break;
417 case MEDSKIP: result = "medskip"; break;
418 case BIGSKIP: result = "bigskip"; break;
419 case VFILL: result = "vfill"; break;
420 case LENGTH: result = len_.asString(); break;
422 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
428 string const VSpace::asLatexCommand(BufferParams const & params) const
436 ret = params.getDefSkip().asLatexCommand(params);
439 ret = keep_ ? "\\vspace*{\\smallskipamount}"
443 ret = keep_ ? "\\vspace*{\\medskipamount}"
447 ret = keep_ ? "\\vspace*{\\bigskipamount}"
451 ret = keep_ ? "\\vspace*{\\fill}"
456 string const lenstr = len_.asLatexString();
458 ret = keep_ ? "\\vspace*{" + lenstr + '}'
459 : "\\vspace{" + lenstr + '}';
470 int VSpace::inPixels(BufferView const & bv) const
472 // Height of a normal line in pixels (zoom factor considered)
473 int const default_height = defaultRowHeight(); // [pixels]
480 // value for this is already set
484 retval = bv.buffer()->params.getDefSkip().inPixels(bv);
487 // This is how the skips are normally defined by LateX.
488 // But there should be some way to change this per document.
490 retval = default_height / 4;
494 retval = default_height / 2;
498 retval = default_height;
502 // leave space for the vfill symbol
503 retval = 3 * default_height;
507 retval = len_.len().inPixels(bv.workWidth());