3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
6 * \author Matthias Ettrich
12 #include "lengthcommon.h"
15 #include "BufferView.h"
16 #include "support/LAssert.h"
18 #include "support/lstrings.h"
22 #ifndef CXX_GLOBAL_CSTD
28 /// used to return numeric values in parsing vspace
29 double number[4] = { 0, 0, 0, 0 };
30 /// used to return unit types in parsing vspace
31 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
34 LyXLength::UNIT_NONE };
36 /// the current position in the number array
38 /// the current position in the unit array
41 /// skip n characters of input
43 void lyx_advance(string & data, string::size_type n)
48 /// return true when the input is at the end
50 bool isEndOfData(string const & data)
52 return ltrim(data).empty();
56 * nextToken - return the next token in the input
57 * @param data input string
58 * @return a char representing the type of token returned
60 * The possible return values are :
61 * + stretch indicator for glue length
62 * - shrink indicator for glue length
63 * n a numeric value (stored in number array)
64 * u a unit type (stored in unit array)
67 char nextToken(string & data)
72 else if (data[0] == '+') {
75 } else if (prefixIs(data, "plus")) {
78 } else if (data[0] == '-') {
81 } else if (prefixIs(data, "minus")) {
85 string::size_type i = data.find_first_not_of("0123456789.");
88 if (number_index > 3) return 'E';
92 // we have found some number
93 if (i == string::npos) {
97 buffer = data.substr(0, i);
101 if (isStrDbl(buffer)) {
102 number[number_index] = strToDbl(buffer);
108 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
110 if (unit_index > 3) return 'E';
114 // we have found some alphabetical string
115 if (i == string::npos) {
119 buffer = data.substr(0, i);
121 // possibly we have "mmplus" string or similar
122 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
123 lyx_advance(data, 2);
124 unit[unit_index] = unitFromString(buffer.substr(0, 2));
126 lyx_advance(data, i);
127 unit[unit_index] = unitFromString(buffer);
130 if (unit[unit_index] != LyXLength::UNIT_NONE) {
133 } else return 'E'; // Error
140 /// latex representation of a vspace
142 char const * pattern;
150 /// the possible formats for a vspace string
151 LaTeXLength table[] = {
152 { "nu", 0, 0, 0, 0 },
153 { "nu+nu", 2, 0, 2, 0 },
154 { "nu+nu-nu", 2, 3, 2, 3 },
155 { "nu+-nu", 2, 2, 2, 2 },
156 { "nu-nu", 0, 2, 0, 2 },
157 { "nu-nu+nu", 3, 2, 3, 2 },
158 { "nu-+nu", 2, 2, 2, 2 },
159 { "n+nu", 2, 0, 1, 0 },
160 { "n+n-nu", 2, 3, 1, 1 },
161 { "n+-nu", 2, 2, 1, 1 },
162 { "n-nu", 0, 2, 0, 1 },
163 { "n-n+nu", 3, 2, 1, 1 },
164 { "n-+nu", 2, 2, 1, 1 },
165 { "", 0, 0, 0, 0 } // sentinel, must be empty
171 const char * stringFromUnit(int unit)
173 if (unit < 0 || unit >= num_units)
175 return unit_name[unit];
179 bool isValidGlueLength(string const & data, LyXGlueLength * result)
181 // This parser is table-driven. First, it constructs a "pattern"
182 // that describes the sequence of tokens in "data". For example,
183 // "n-nu" means: number, minus sign, number, unit. As we go along,
184 // numbers and units are stored into static arrays. Then, "pattern"
185 // is searched in the "table". If it is found, the associated
186 // table entries tell us which number and unit should go where
187 // in the LyXLength structure. Example: if "data" has the "pattern"
188 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
189 // That means, "plus_val" is the second number that was seen
190 // in the input, "minus_val" is the third number, and "plus_uni"
191 // and "minus_uni" are the second and third units, respectively.
192 // ("val" and "uni" are always the first items seen in "data".)
193 // This is the most elegant solution I could find -- a straight-
194 // forward approach leads to very long, tedious code that would be
195 // much harder to understand and maintain. (AS)
199 string buffer = ltrim(data);
201 // To make isValidGlueLength recognize negative values as
202 // the first number this little hack is needed:
203 int val_sign = 1; // positive as default
206 lyx_advance(buffer, 1);
210 lyx_advance(buffer, 1);
218 int pattern_index = 0;
223 unit_index = 1; // entries at index 0 are sentinels
225 // construct "pattern" from "data"
226 while (!isEndOfData (buffer)) {
227 if (pattern_index > 20) return false;
228 pattern[pattern_index] = nextToken (buffer);
229 if (pattern[pattern_index] == 'E') return false;
232 pattern[pattern_index] = '\0';
234 // search "pattern" in "table"
236 while (compare(pattern, table[table_index].pattern)) {
238 if (!*table[table_index].pattern)
242 // Get the values from the appropriate places. If an index
243 // is zero, the corresponding array value is zero or UNIT_NONE,
244 // so we needn't check this.
246 result->len_.value (number[1] * val_sign);
247 result->len_.unit (unit[1]);
248 result->plus_.value (number[table[table_index].plus_val_index]);
249 result->plus_.unit (unit [table[table_index].plus_uni_index]);
250 result->minus_.value(number[table[table_index].minus_val_index]);
251 result->minus_.unit (unit [table[table_index].minus_uni_index]);
257 bool isValidLength(string const & data, LyXLength * result)
259 // This is a trimmed down version of isValidGlueLength.
260 // The parser may seem overkill for lengths without
261 // glue, but since we already have it, using it is
262 // easier than writing something from scratch.
266 string buffer = data;
267 int pattern_index = 0;
270 // To make isValidLength recognize negative values
271 // this little hack is needed:
272 int val_sign = 1; // positive as default
275 lyx_advance(buffer, 1);
279 lyx_advance(buffer, 1);
287 number_index = unit_index = 1; // entries at index 0 are sentinels
289 // construct "pattern" from "data"
290 while (!isEndOfData (buffer)) {
291 if (pattern_index > 2)
293 pattern[pattern_index] = nextToken (buffer);
294 if (pattern[pattern_index] == 'E')
298 pattern[pattern_index] = '\0';
300 // only the most basic pattern is accepted here
301 if (compare(pattern, "nu") != 0) return false;
303 // It _was_ a correct length string.
304 // Store away the values we found.
306 result->val_ = number[1] * val_sign;
307 result->unit_ = unit[1];
318 : kind_(NONE), len_(), keep_(false)
322 VSpace::VSpace(vspace_kind k)
323 : kind_(k), len_(), keep_(false)
327 VSpace::VSpace(LyXLength const & l)
328 : kind_(LENGTH), len_(l), keep_(false)
332 VSpace::VSpace(LyXGlueLength const & l)
333 : kind_(LENGTH), len_(l), keep_(false)
337 VSpace::VSpace(string const & data)
338 : kind_(NONE), len_(), keep_(false)
343 string input = rtrim(data);
345 string::size_type const length = input.length();
347 if (length > 1 && input[length - 1] == '*') {
349 input.erase(length - 1);
352 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
353 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
354 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
355 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
356 else if (prefixIs (input, "vfill")) kind_ = VFILL;
357 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
358 else if (sscanf(input.c_str(), "%lf", &value) == 1) {
359 // This last one is for reading old .lyx files
360 // without units in added_space_top/bottom.
361 // Let unit default to centimeters here.
363 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
368 VSpace::vspace_kind VSpace::kind() const
374 LyXGlueLength VSpace::length() const
380 bool VSpace::keep() const
386 void VSpace::setKeep(bool val)
392 bool VSpace::operator==(VSpace const & other) const
394 if (kind_ != other.kind_)
398 return this->keep_ == other.keep_;
400 if (len_ != other.len_)
403 return keep_ == other.keep_;
407 string const VSpace::asLyXCommand() const
412 case DEFSKIP: result = "defskip"; break;
413 case SMALLSKIP: result = "smallskip"; break;
414 case MEDSKIP: result = "medskip"; break;
415 case BIGSKIP: result = "bigskip"; break;
416 case VFILL: result = "vfill"; break;
417 case LENGTH: result = len_.asString(); break;
419 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
425 string const VSpace::asLatexCommand(BufferParams const & params) const
433 ret = params.getDefSkip().asLatexCommand(params);
436 ret = keep_ ? "\\vspace*{\\smallskipamount}"
440 ret = keep_ ? "\\vspace*{\\medskipamount}"
444 ret = keep_ ? "\\vspace*{\\bigskipamount}"
448 ret = keep_ ? "\\vspace*{\\fill}"
453 string const lenstr = len_.asLatexString();
455 ret = keep_ ? "\\vspace*{" + lenstr + '}'
456 : "\\vspace{" + lenstr + '}';
467 int VSpace::inPixels(BufferView const * bv) const
469 // Height of a normal line in pixels (zoom factor considered)
470 int const default_height = bv->defaultHeight(); // [pixels]
477 // value for this is already set
481 retval = bv->buffer()->params.getDefSkip().inPixels(bv);
484 // This is how the skips are normally defined by LateX.
485 // But there should be some way to change this per document.
487 retval = default_height / 4;
491 retval = default_height / 2;
495 retval = default_height;
499 // leave space for the vfill symbol
500 retval = 3 * default_height;
504 retval = len_.len().inPixels(bv->workWidth());