* de/Tutorial.lyx: minor corrections reported on lyx-docs.
[lyx.git] / src / VSpace.cpp
blob19f6af41a1f638d282263e6f6be1ef01ccdf19a3
1 /**
2 * \file VSpace.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Matthias Ettrich
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "VSpace.h"
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "support/gettext.h"
19 #include "Length.h"
20 #include "Text.h"
21 #include "TextMetrics.h" // for defaultRowHeight()
23 #include "support/convert.h"
24 #include "support/lstrings.h"
26 #include "support/lassert.h"
28 #include <cstring>
30 using namespace std;
31 using namespace lyx::support;
34 namespace lyx {
36 namespace {
38 /// used to return numeric values in parsing vspace
39 double number[4] = { 0, 0, 0, 0 };
41 /// used to return unit types in parsing vspace
42 Length::UNIT unit[4] = {
43 Length::UNIT_NONE,
44 Length::UNIT_NONE,
45 Length::UNIT_NONE,
46 Length::UNIT_NONE
49 /// the current position in the number array
50 int number_index;
51 /// the current position in the unit array
52 int unit_index;
54 /// skip n characters of input
55 inline void lyx_advance(string & data, size_t n)
57 data.erase(0, n);
61 /// return true when the input is at the end
62 inline bool isEndOfData(string const & data)
64 return ltrim(data).empty();
68 /**
69 * nextToken - return the next token in the input
70 * @param data input string
71 * @return a char representing the type of token returned
73 * The possible return values are :
74 * + stretch indicator for glue length
75 * - shrink indicator for glue length
76 * n a numeric value (stored in number array)
77 * u a unit type (stored in unit array)
78 * E parse error
80 char nextToken(string & data)
82 data = ltrim(data);
84 if (data.empty())
85 return '\0';
87 if (data[0] == '+') {
88 lyx_advance(data, 1);
89 return '+';
92 if (prefixIs(data, "plus")) {
93 lyx_advance(data, 4);
94 return '+';
97 if (data[0] == '-') {
98 lyx_advance(data, 1);
99 return '-';
102 if (prefixIs(data, "minus")) {
103 lyx_advance(data, 5);
104 return '-';
107 size_t i = data.find_first_not_of("0123456789.");
109 if (i != 0) {
110 if (number_index > 3)
111 return 'E';
113 string buffer;
115 // we have found some number
116 if (i == string::npos) {
117 buffer = data;
118 i = data.size() + 1;
119 } else {
120 buffer = data.substr(0, i);
123 lyx_advance(data, i);
125 if (isStrDbl(buffer)) {
126 number[number_index] = convert<double>(buffer);
127 ++number_index;
128 return 'n';
130 return 'E';
133 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
134 if (i != 0) {
135 if (unit_index > 3)
136 return 'E';
138 string buffer;
140 // we have found some alphabetical string
141 if (i == string::npos) {
142 buffer = data;
143 i = data.size() + 1;
144 } else {
145 buffer = data.substr(0, i);
148 // possibly we have "mmplus" string or similar
149 if (buffer.size() > 5 &&
150 (buffer.substr(2, 4) == string("plus") ||
151 buffer.substr(2, 5) == string("minus")))
153 lyx_advance(data, 2);
154 unit[unit_index] = unitFromString(buffer.substr(0, 2));
155 } else {
156 lyx_advance(data, i);
157 unit[unit_index] = unitFromString(buffer);
160 if (unit[unit_index] != Length::UNIT_NONE) {
161 ++unit_index;
162 return 'u';
164 return 'E'; // Error
166 return 'E'; // Error
170 /// latex representation of a vspace
171 struct LaTeXLength {
172 char const * pattern;
173 int plus_val_index;
174 int minus_val_index;
175 int plus_uni_index;
176 int minus_uni_index;
180 /// the possible formats for a vspace string
181 LaTeXLength table[] = {
182 { "nu", 0, 0, 0, 0 },
183 { "nu+nu", 2, 0, 2, 0 },
184 { "nu+nu-nu", 2, 3, 2, 3 },
185 { "nu+-nu", 2, 2, 2, 2 },
186 { "nu-nu", 0, 2, 0, 2 },
187 { "nu-nu+nu", 3, 2, 3, 2 },
188 { "nu-+nu", 2, 2, 2, 2 },
189 { "n+nu", 2, 0, 1, 0 },
190 { "n+n-nu", 2, 3, 1, 1 },
191 { "n+-nu", 2, 2, 1, 1 },
192 { "n-nu", 0, 2, 0, 1 },
193 { "n-n+nu", 3, 2, 1, 1 },
194 { "n-+nu", 2, 2, 1, 1 },
195 { "", 0, 0, 0, 0 } // sentinel, must be empty
199 } // namespace anon
201 const char * stringFromUnit(int unit)
203 if (unit < 0 || unit > num_units)
204 return 0;
205 return unit_name[unit];
209 bool isValidGlueLength(string const & data, GlueLength * result)
211 // This parser is table-driven. First, it constructs a "pattern"
212 // that describes the sequence of tokens in "data". For example,
213 // "n-nu" means: number, minus sign, number, unit. As we go along,
214 // numbers and units are stored into static arrays. Then, "pattern"
215 // is searched in the "table". If it is found, the associated
216 // table entries tell us which number and unit should go where
217 // in the Length structure. Example: if "data" has the "pattern"
218 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
219 // That means, "plus_val" is the second number that was seen
220 // in the input, "minus_val" is the third number, and "plus_uni"
221 // and "minus_uni" are the second and third units, respectively.
222 // ("val" and "uni" are always the first items seen in "data".)
223 // This is the most elegant solution I could find -- a straight-
224 // forward approach leads to very long, tedious code that would be
225 // much harder to understand and maintain. (AS)
227 if (data.empty())
228 return true;
229 string buffer = ltrim(data);
231 // To make isValidGlueLength recognize negative values as
232 // the first number this little hack is needed:
233 int val_sign = 1; // positive as default
234 switch (buffer[0]) {
235 case '-':
236 lyx_advance(buffer, 1);
237 val_sign = -1;
238 break;
239 case '+':
240 lyx_advance(buffer, 1);
241 break;
242 default:
243 break;
245 // end of hack
247 int pattern_index = 0;
248 int table_index = 0;
249 char pattern[20];
251 number_index = 1;
252 unit_index = 1; // entries at index 0 are sentinels
254 // construct "pattern" from "data"
255 while (!isEndOfData(buffer)) {
256 if (pattern_index > 20)
257 return false;
258 pattern[pattern_index] = nextToken(buffer);
259 if (pattern[pattern_index] == 'E')
260 return false;
261 ++pattern_index;
263 pattern[pattern_index] = '\0';
265 // search "pattern" in "table"
266 table_index = 0;
267 while (strcmp(pattern, table[table_index].pattern)) {
268 ++table_index;
269 if (!*table[table_index].pattern)
270 return false;
273 // Get the values from the appropriate places. If an index
274 // is zero, the corresponding array value is zero or UNIT_NONE,
275 // so we needn't check this.
276 if (result) {
277 result->len_.value (number[1] * val_sign);
278 result->len_.unit (unit[1]);
279 result->plus_.value (number[table[table_index].plus_val_index]);
280 result->plus_.unit (unit [table[table_index].plus_uni_index]);
281 result->minus_.value(number[table[table_index].minus_val_index]);
282 result->minus_.unit (unit [table[table_index].minus_uni_index]);
284 return true;
288 bool isValidLength(string const & data, Length * result)
290 // This is a trimmed down version of isValidGlueLength.
291 // The parser may seem overkill for lengths without
292 // glue, but since we already have it, using it is
293 // easier than writing something from scratch.
294 if (data.empty())
295 return true;
297 string buffer = data;
298 int pattern_index = 0;
299 char pattern[3];
301 // To make isValidLength recognize negative values
302 // this little hack is needed:
303 int val_sign = 1; // positive as default
304 switch (buffer[0]) {
305 case '-':
306 lyx_advance(buffer, 1);
307 val_sign = -1;
308 break;
309 case '+':
310 lyx_advance(buffer, 1);
311 // fall through
312 default:
313 // no action
314 break;
316 // end of hack
318 number_index = unit_index = 1; // entries at index 0 are sentinels
320 // construct "pattern" from "data"
321 while (!isEndOfData(buffer)) {
322 if (pattern_index > 2)
323 return false;
324 pattern[pattern_index] = nextToken(buffer);
325 if (pattern[pattern_index] == 'E')
326 return false;
327 ++pattern_index;
329 pattern[pattern_index] = '\0';
331 // only the most basic pattern is accepted here
332 if (strcmp(pattern, "nu") != 0)
333 return false;
335 // It _was_ a correct length string.
336 // Store away the values we found.
337 if (result) {
338 result->val_ = number[1] * val_sign;
339 result->unit_ = unit[1];
341 return true;
346 // VSpace class
349 VSpace::VSpace()
350 : kind_(DEFSKIP), len_(), keep_(false)
354 VSpace::VSpace(VSpaceKind k)
355 : kind_(k), len_(), keep_(false)
359 VSpace::VSpace(Length const & l)
360 : kind_(LENGTH), len_(l), keep_(false)
364 VSpace::VSpace(GlueLength const & l)
365 : kind_(LENGTH), len_(l), keep_(false)
369 VSpace::VSpace(string const & data)
370 : kind_(DEFSKIP), len_(), keep_(false)
372 if (data.empty())
373 return;
375 string input = rtrim(data);
377 size_t const length = input.length();
379 if (length > 1 && input[length - 1] == '*') {
380 keep_ = true;
381 input.erase(length - 1);
384 if (prefixIs(input, "defskip"))
385 kind_ = DEFSKIP;
386 else if (prefixIs(input, "smallskip"))
387 kind_ = SMALLSKIP;
388 else if (prefixIs(input, "medskip"))
389 kind_ = MEDSKIP;
390 else if (prefixIs(input, "bigskip"))
391 kind_ = BIGSKIP;
392 else if (prefixIs(input, "vfill"))
393 kind_ = VFILL;
394 else if (isValidGlueLength(input, &len_))
395 kind_ = LENGTH;
396 else if (isStrDbl(input)) {
397 // This last one is for reading old .lyx files
398 // without units in added_space_top/bottom.
399 // Let unit default to centimeters here.
400 kind_ = LENGTH;
401 len_ = GlueLength(Length(convert<double>(input), Length::CM));
406 bool VSpace::operator==(VSpace const & other) const
408 if (kind_ != other.kind_)
409 return false;
411 if (kind_ != LENGTH)
412 return this->keep_ == other.keep_;
414 if (len_ != other.len_)
415 return false;
417 return keep_ == other.keep_;
421 string const VSpace::asLyXCommand() const
423 string result;
424 switch (kind_) {
425 case DEFSKIP: result = "defskip"; break;
426 case SMALLSKIP: result = "smallskip"; break;
427 case MEDSKIP: result = "medskip"; break;
428 case BIGSKIP: result = "bigskip"; break;
429 case VFILL: result = "vfill"; break;
430 case LENGTH: result = len_.asString(); break;
432 if (keep_)
433 result += '*';
434 return result;
438 string const VSpace::asLatexCommand(BufferParams const & params) const
440 switch (kind_) {
441 case DEFSKIP:
442 return params.getDefSkip().asLatexCommand(params);
444 case SMALLSKIP:
445 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
447 case MEDSKIP:
448 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
450 case BIGSKIP:
451 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
453 case VFILL:
454 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
456 case LENGTH:
457 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
458 : "\\vspace{" + len_.asLatexString() + '}';
460 default:
461 LASSERT(false, /**/);
462 return string();
467 docstring const VSpace::asGUIName() const
469 docstring result;
470 switch (kind_) {
471 case DEFSKIP:
472 result = _("Default skip");
473 break;
474 case SMALLSKIP:
475 result = _("Small skip");
476 break;
477 case MEDSKIP:
478 result = _("Medium skip");
479 break;
480 case BIGSKIP:
481 result = _("Big skip");
482 break;
483 case VFILL:
484 result = _("Vertical fill");
485 break;
486 case LENGTH:
487 result = from_ascii(len_.asString());
488 break;
490 if (keep_)
491 result += ", " + _("protected");
492 return result;
496 string VSpace::asHTMLLength() const
498 string result;
499 switch (kind_) {
500 case DEFSKIP: result = "2ex"; break;
501 case SMALLSKIP: result = "1ex"; break;
502 case MEDSKIP: result = "3ex"; break;
503 case BIGSKIP: result = "5ex"; break;
504 case LENGTH: {
505 Length tmp = len_.len();
506 if (tmp.value() > 0)
507 result = tmp.asHTMLString();
509 case VFILL: break;
511 return result;
514 int VSpace::inPixels(BufferView const & bv) const
516 // Height of a normal line in pixels (zoom factor considered)
517 int const default_height = defaultRowHeight();
519 switch (kind_) {
521 case DEFSKIP:
522 return bv.buffer().params().getDefSkip().inPixels(bv);
524 // This is how the skips are normally defined by LateX.
525 // But there should be some way to change this per document.
526 case SMALLSKIP:
527 return default_height / 4;
529 case MEDSKIP:
530 return default_height / 2;
532 case BIGSKIP:
533 return default_height;
535 case VFILL:
536 // leave space for the vfill symbol
537 return 3 * default_height;
539 case LENGTH:
540 return len_.len().inPixels(bv.workWidth());
542 default:
543 LASSERT(false, /**/);
544 return 0;
549 } // namespace lyx