1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
18 #include "lengthcommon.h"
23 #include "BufferView.h"
25 #include "support/lstrings.h"
32 double number[4] = { 0, 0, 0, 0 };
33 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
36 LyXLength::UNIT_NONE };
42 void lyx_advance(string & data, string::size_type n)
49 bool isEndOfData(string const & data)
51 return frontStrip(data).empty();
55 char nextToken(string & data)
57 data = frontStrip(data);
60 else if (data[0] == '+') {
63 } else if (prefixIs(data, "plus")) {
66 } else if (data[0] == '-') {
69 } else if (prefixIs(data, "minus")) {
73 string::size_type i = data.find_first_not_of("0123456789.");
76 if (number_index > 3) return 'E';
80 // we have found some number
81 if (i == string::npos) {
85 buffer = data.substr(0, i);
89 if (isStrDbl(buffer)) {
90 number[number_index] = strToDbl(buffer);
96 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
98 if (unit_index > 3) return 'E';
102 // we have found some alphabetical string
103 if (i == string::npos) {
107 buffer = data.substr(0, i);
109 // possibly we have "mmplus" string or similar
110 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
111 lyx_advance(data, 2);
112 unit[unit_index] = unitFromString(buffer.substr(0, 2));
114 lyx_advance(data, i);
115 unit[unit_index] = unitFromString(buffer);
118 if (unit[unit_index] != LyXLength::UNIT_NONE) {
121 } else return 'E'; // Error
129 char const * pattern;
137 LaTeXLength table[] = {
138 { "nu", 0, 0, 0, 0 },
139 { "nu+nu", 2, 0, 2, 0 },
140 { "nu+nu-nu", 2, 3, 2, 3 },
141 { "nu+-nu", 2, 2, 2, 2 },
142 { "nu-nu", 0, 2, 0, 2 },
143 { "nu-nu+nu", 3, 2, 3, 2 },
144 { "nu-+nu", 2, 2, 2, 2 },
145 { "n+nu", 2, 0, 1, 0 },
146 { "n+n-nu", 2, 3, 1, 1 },
147 { "n+-nu", 2, 2, 1, 1 },
148 { "n-nu", 0, 2, 0, 1 },
149 { "n-n+nu", 3, 2, 1, 1 },
150 { "n-+nu", 2, 2, 1, 1 },
151 { "", 0, 0, 0, 0 } // sentinel, must be empty
157 const char * stringFromUnit(int unit)
159 if (unit < 0 || unit >= num_units)
161 return unit_name[unit];
165 bool isValidGlueLength(string const & data, LyXGlueLength * result)
167 // This parser is table-driven. First, it constructs a "pattern"
168 // that describes the sequence of tokens in "data". For example,
169 // "n-nu" means: number, minus sign, number, unit. As we go along,
170 // numbers and units are stored into static arrays. Then, "pattern"
171 // is searched in the "table". If it is found, the associated
172 // table entries tell us which number and unit should go where
173 // in the LyXLength structure. Example: if "data" has the "pattern"
174 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
175 // That means, "plus_val" is the second number that was seen
176 // in the input, "minus_val" is the third number, and "plus_uni"
177 // and "minus_uni" are the second and third units, respectively.
178 // ("val" and "uni" are always the first items seen in "data".)
179 // This is the most elegant solution I could find -- a straight-
180 // forward approach leads to very long, tedious code that would be
181 // much harder to understand and maintain. (AS)
185 string buffer = frontStrip(data);
187 // To make isValidGlueLength recognize negative values as
188 // the first number this little hack is needed:
189 int val_sign = 1; // positive as default
192 lyx_advance(buffer, 1);
196 lyx_advance(buffer, 1);
204 int pattern_index = 0;
209 unit_index = 1; // entries at index 0 are sentinels
211 // construct "pattern" from "data"
212 while (!isEndOfData (buffer)) {
213 if (pattern_index > 20) return false;
214 pattern[pattern_index] = nextToken (buffer);
215 if (pattern[pattern_index] == 'E') return false;
218 pattern[pattern_index] = '\0';
220 // search "pattern" in "table"
222 while (compare(pattern, table[table_index].pattern)) {
224 if (!*table[table_index].pattern)
228 // Get the values from the appropriate places. If an index
229 // is zero, the corresponding array value is zero or UNIT_NONE,
230 // so we needn't check this.
232 result->len_.value (number[1] * val_sign);
233 result->len_.unit (unit[1]);
234 result->plus_.value (number[table[table_index].plus_val_index]);
235 result->plus_.unit (unit [table[table_index].plus_uni_index]);
236 result->minus_.value(number[table[table_index].minus_val_index]);
237 result->minus_.unit (unit [table[table_index].minus_uni_index]);
243 bool isValidLength(string const & data, LyXLength * result)
245 /// This is a trimmed down version of isValidGlueLength.
246 /// The parser may seem overkill for lengths without
247 /// glue, but since we already have it, using it is
248 /// easier than writing something from scratch.
252 string buffer = data;
253 int pattern_index = 0;
256 // To make isValidLength recognize negative values
257 // this little hack is needed:
258 int val_sign = 1; // positive as default
261 lyx_advance(buffer, 1);
265 lyx_advance(buffer, 1);
273 number_index = unit_index = 1; // entries at index 0 are sentinels
275 // construct "pattern" from "data"
276 while (!isEndOfData (buffer)) {
277 if (pattern_index > 2)
279 pattern[pattern_index] = nextToken (buffer);
280 if (pattern[pattern_index] == 'E')
284 pattern[pattern_index] = '\0';
286 // only the most basic pattern is accepted here
287 if (compare(pattern, "nu") != 0) return false;
289 // It _was_ a correct length string.
290 // Store away the values we found.
292 result->val_ = number[1] * val_sign;
293 result->unit_ = unit[1];
304 : kind_(NONE), len_(), keep_(false)
308 VSpace::VSpace(vspace_kind k)
309 : kind_(k), len_(), keep_(false)
313 VSpace::VSpace(LyXLength const & l)
314 : kind_(LENGTH), len_(l), keep_(false)
318 VSpace::VSpace(LyXGlueLength const & l)
319 : kind_(LENGTH), len_(l), keep_(false)
323 VSpace::VSpace(string const & data)
324 : kind_(NONE), len_(), keep_(false)
329 string input = strip(data);
331 string::size_type const length = input.length();
333 if (length > 1 && input[length-1] == '*') {
335 input.erase(length - 1);
338 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
339 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
340 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
341 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
342 else if (prefixIs (input, "vfill")) kind_ = VFILL;
343 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
344 else if (sscanf(input.c_str(), "%lf", &value) == 1) {
345 // This last one is for reading old .lyx files
346 // without units in added_space_top/bottom.
347 // Let unit default to centimeters here.
349 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
354 VSpace::vspace_kind VSpace::kind() const
360 LyXGlueLength VSpace::length() const
366 bool VSpace::keep() const
372 void VSpace::setKeep(bool val)
378 bool VSpace::operator==(VSpace const & other) const
380 if (kind_ != other.kind_)
384 return this->keep_ == other.keep_;
386 if (len_ != other.len_)
389 return keep_ == other.keep_;
393 string const VSpace::asLyXCommand() const
398 case DEFSKIP: result = "defskip"; break;
399 case SMALLSKIP: result = "smallskip"; break;
400 case MEDSKIP: result = "medskip"; break;
401 case BIGSKIP: result = "bigskip"; break;
402 case VFILL: result = "vfill"; break;
403 case LENGTH: result = len_.asString(); break;
405 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
411 string const VSpace::asLatexCommand(BufferParams const & params) const
414 case NONE: return string();
416 return params.getDefSkip().asLatexCommand(params);
417 case SMALLSKIP: return keep_ ? "\\vspace*{\\smallskipamount}"
419 case MEDSKIP: return keep_ ? "\\vspace*{\\medskipamount}"
421 case BIGSKIP: return keep_ ? "\\vspace*{\\bigskipamount}"
423 case VFILL: return keep_ ? "\\vspace*{\\fill}"
425 case LENGTH: return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
426 : "\\vspace{" + len_.asLatexString() + '}';
428 return string(); // should never be reached
432 int VSpace::inPixels(BufferView * bv) const
434 // Height of a normal line in pixels (zoom factor considered)
435 int default_height = bv->text->defaultHeight(); // [pixels]
436 int default_width = bv->workWidth();
442 return bv->buffer()->params.getDefSkip().inPixels(bv);
444 // This is how the skips are normally defined by
445 // LateX. But there should be some way to change
446 // this per document.
448 return default_height / 4;
450 return default_height / 2;
452 return default_height;
455 // leave space for the vfill symbol
456 return 3 * default_height;
458 return len_.len().inPixels(default_width, default_height);