3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
6 * \author Matthias Ettrich
12 #pragma implementation
16 #include "lengthcommon.h"
20 #include "BufferView.h"
21 #include "support/LAssert.h"
23 #include "support/lstrings.h"
27 #ifndef CXX_GLOBAL_CSTD
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 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
39 LyXLength::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)
53 /// return true when the input is at the end
55 bool isEndOfData(string const & data)
57 return ltrim(data).empty();
61 * nextToken - return the next token in the input
62 * @param data input string
63 * @return a char representing the type of token returned
65 * The possible return values are :
66 * + stretch indicator for glue length
67 * - shrink indicator for glue length
68 * n a numeric value (stored in number array)
69 * u a unit type (stored in unit array)
72 char nextToken(string & data)
77 else if (data[0] == '+') {
80 } else if (prefixIs(data, "plus")) {
83 } else if (data[0] == '-') {
86 } else if (prefixIs(data, "minus")) {
90 string::size_type i = data.find_first_not_of("0123456789.");
93 if (number_index > 3) return 'E';
97 // we have found some number
98 if (i == string::npos) {
102 buffer = data.substr(0, i);
104 lyx_advance(data, i);
106 if (isStrDbl(buffer)) {
107 number[number_index] = strToDbl(buffer);
113 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
115 if (unit_index > 3) return 'E';
119 // we have found some alphabetical string
120 if (i == string::npos) {
124 buffer = data.substr(0, i);
126 // possibly we have "mmplus" string or similar
127 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
128 lyx_advance(data, 2);
129 unit[unit_index] = unitFromString(buffer.substr(0, 2));
131 lyx_advance(data, i);
132 unit[unit_index] = unitFromString(buffer);
135 if (unit[unit_index] != LyXLength::UNIT_NONE) {
138 } else return 'E'; // Error
145 /// latex representation of a vspace
147 char const * pattern;
155 /// the possible formats for a vspace string
156 LaTeXLength table[] = {
157 { "nu", 0, 0, 0, 0 },
158 { "nu+nu", 2, 0, 2, 0 },
159 { "nu+nu-nu", 2, 3, 2, 3 },
160 { "nu+-nu", 2, 2, 2, 2 },
161 { "nu-nu", 0, 2, 0, 2 },
162 { "nu-nu+nu", 3, 2, 3, 2 },
163 { "nu-+nu", 2, 2, 2, 2 },
164 { "n+nu", 2, 0, 1, 0 },
165 { "n+n-nu", 2, 3, 1, 1 },
166 { "n+-nu", 2, 2, 1, 1 },
167 { "n-nu", 0, 2, 0, 1 },
168 { "n-n+nu", 3, 2, 1, 1 },
169 { "n-+nu", 2, 2, 1, 1 },
170 { "", 0, 0, 0, 0 } // sentinel, must be empty
176 const char * stringFromUnit(int unit)
178 if (unit < 0 || unit >= num_units)
180 return unit_name[unit];
184 bool isValidGlueLength(string const & data, LyXGlueLength * result)
186 // This parser is table-driven. First, it constructs a "pattern"
187 // that describes the sequence of tokens in "data". For example,
188 // "n-nu" means: number, minus sign, number, unit. As we go along,
189 // numbers and units are stored into static arrays. Then, "pattern"
190 // is searched in the "table". If it is found, the associated
191 // table entries tell us which number and unit should go where
192 // in the LyXLength structure. Example: if "data" has the "pattern"
193 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
194 // That means, "plus_val" is the second number that was seen
195 // in the input, "minus_val" is the third number, and "plus_uni"
196 // and "minus_uni" are the second and third units, respectively.
197 // ("val" and "uni" are always the first items seen in "data".)
198 // This is the most elegant solution I could find -- a straight-
199 // forward approach leads to very long, tedious code that would be
200 // much harder to understand and maintain. (AS)
204 string buffer = ltrim(data);
206 // To make isValidGlueLength recognize negative values as
207 // the first number this little hack is needed:
208 int val_sign = 1; // positive as default
211 lyx_advance(buffer, 1);
215 lyx_advance(buffer, 1);
223 int pattern_index = 0;
228 unit_index = 1; // entries at index 0 are sentinels
230 // construct "pattern" from "data"
231 while (!isEndOfData (buffer)) {
232 if (pattern_index > 20) return false;
233 pattern[pattern_index] = nextToken (buffer);
234 if (pattern[pattern_index] == 'E') return false;
237 pattern[pattern_index] = '\0';
239 // search "pattern" in "table"
241 while (compare(pattern, table[table_index].pattern)) {
243 if (!*table[table_index].pattern)
247 // Get the values from the appropriate places. If an index
248 // is zero, the corresponding array value is zero or UNIT_NONE,
249 // so we needn't check this.
251 result->len_.value (number[1] * val_sign);
252 result->len_.unit (unit[1]);
253 result->plus_.value (number[table[table_index].plus_val_index]);
254 result->plus_.unit (unit [table[table_index].plus_uni_index]);
255 result->minus_.value(number[table[table_index].minus_val_index]);
256 result->minus_.unit (unit [table[table_index].minus_uni_index]);
262 bool isValidLength(string const & data, LyXLength * result)
264 // This is a trimmed down version of isValidGlueLength.
265 // The parser may seem overkill for lengths without
266 // glue, but since we already have it, using it is
267 // easier than writing something from scratch.
271 string buffer = data;
272 int pattern_index = 0;
275 // To make isValidLength recognize negative values
276 // this little hack is needed:
277 int val_sign = 1; // positive as default
280 lyx_advance(buffer, 1);
284 lyx_advance(buffer, 1);
292 number_index = unit_index = 1; // entries at index 0 are sentinels
294 // construct "pattern" from "data"
295 while (!isEndOfData (buffer)) {
296 if (pattern_index > 2)
298 pattern[pattern_index] = nextToken (buffer);
299 if (pattern[pattern_index] == 'E')
303 pattern[pattern_index] = '\0';
305 // only the most basic pattern is accepted here
306 if (compare(pattern, "nu") != 0) return false;
308 // It _was_ a correct length string.
309 // Store away the values we found.
311 result->val_ = number[1] * val_sign;
312 result->unit_ = unit[1];
323 : kind_(NONE), len_(), keep_(false)
327 VSpace::VSpace(vspace_kind k)
328 : kind_(k), len_(), keep_(false)
332 VSpace::VSpace(LyXLength const & l)
333 : kind_(LENGTH), len_(l), keep_(false)
337 VSpace::VSpace(LyXGlueLength const & l)
338 : kind_(LENGTH), len_(l), keep_(false)
342 VSpace::VSpace(string const & data)
343 : kind_(NONE), len_(), keep_(false)
348 string input = rtrim(data);
350 string::size_type const length = input.length();
352 if (length > 1 && input[length - 1] == '*') {
354 input.erase(length - 1);
357 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
358 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
359 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
360 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
361 else if (prefixIs (input, "vfill")) kind_ = VFILL;
362 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
363 else if (sscanf(input.c_str(), "%lf", &value) == 1) {
364 // This last one is for reading old .lyx files
365 // without units in added_space_top/bottom.
366 // Let unit default to centimeters here.
368 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
373 VSpace::vspace_kind VSpace::kind() const
379 LyXGlueLength VSpace::length() const
385 bool VSpace::keep() const
391 void VSpace::setKeep(bool val)
397 bool VSpace::operator==(VSpace const & other) const
399 if (kind_ != other.kind_)
403 return this->keep_ == other.keep_;
405 if (len_ != other.len_)
408 return keep_ == other.keep_;
412 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;
424 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
430 string const VSpace::asLatexCommand(BufferParams const & params) const
438 ret = params.getDefSkip().asLatexCommand(params);
441 ret = keep_ ? "\\vspace*{\\smallskipamount}"
445 ret = keep_ ? "\\vspace*{\\medskipamount}"
449 ret = keep_ ? "\\vspace*{\\bigskipamount}"
453 ret = keep_ ? "\\vspace*{\\fill}"
458 string const lenstr = len_.asLatexString();
460 ret = keep_ ? "\\vspace*{" + lenstr + '}'
461 : "\\vspace{" + lenstr + '}';
472 int VSpace::inPixels(BufferView const * bv) const
474 // Height of a normal line in pixels (zoom factor considered)
475 int const default_height = bv->text->defaultHeight(); // [pixels]
481 // Value for this is already set
484 retval = bv->buffer()->params.getDefSkip().inPixels(bv);
487 // This is how the skips are normally defined by
488 // LateX. But there should be some way to change
489 // this per document.
491 retval = default_height / 4;
495 retval = default_height / 2;
499 retval = default_height;
503 // leave space for the vfill symbol
504 retval = 3 * default_height;
508 int const default_width = bv->workWidth();
509 retval = len_.len().inPixels(default_width, default_height);