]> git.lyx.org Git - lyx.git/blob - src/VSpace.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / VSpace.cpp
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.
5  *
6  * \author Matthias Ettrich
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "VSpace.h"
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "gettext.h"
18 #include "lengthcommon.h"
19 #include "Text.h"
20
21 #include "support/convert.h"
22 #include "support/lstrings.h"
23
24
25 namespace lyx {
26
27 using support::compare;
28 using support::isStrDbl;
29 using support::ltrim;
30 using support::prefixIs;
31 using support::rtrim;
32
33 using std::string;
34
35
36 namespace {
37
38 /// used to return numeric values in parsing vspace
39 double number[4] = { 0, 0, 0, 0 };
40 /// used to return unit types in parsing vspace
41 Length::UNIT unit[4] = { Length::UNIT_NONE,
42                             Length::UNIT_NONE,
43                             Length::UNIT_NONE,
44                             Length::UNIT_NONE };
45
46 /// the current position in the number array
47 int number_index;
48 /// the current position in the unit array
49 int unit_index;
50
51 /// skip n characters of input
52 inline
53 void lyx_advance(string & data, string::size_type n)
54 {
55         data.erase(0, n);
56 }
57
58
59 /// return true when the input is at the end
60 inline
61 bool isEndOfData(string const & data)
62 {
63         return ltrim(data).empty();
64 }
65
66
67 /**
68  * nextToken -  return the next token in the input
69  * @param data input string
70  * @return a char representing the type of token returned
71  *
72  * The possible return values are :
73  *      +       stretch indicator for glue length
74  *      -       shrink indicator for glue length
75  *      n       a numeric value (stored in number array)
76  *      u       a unit type (stored in unit array)
77  *      E       parse error
78  */
79 char nextToken(string & data)
80 {
81         data = ltrim(data);
82
83         if (data.empty())
84                 return '\0';
85
86         if (data[0] == '+') {
87                 lyx_advance(data, 1);
88                 return '+';
89         }
90
91         if (prefixIs(data, "plus")) {
92                 lyx_advance(data, 4);
93                 return '+';
94         }
95
96         if (data[0] == '-') {
97                 lyx_advance(data, 1);
98                 return '-';
99         }
100
101         if (prefixIs(data, "minus")) {
102                 lyx_advance(data, 5);
103                 return '-';
104         }
105
106         string::size_type i = data.find_first_not_of("0123456789.");
107
108         if (i != 0) {
109                 if (number_index > 3)
110                         return 'E';
111
112                 string buffer;
113
114                 // we have found some number
115                 if (i == string::npos) {
116                         buffer = data;
117                         i = data.size() + 1;
118                 } else
119                         buffer = data.substr(0, i);
120
121                 lyx_advance(data, i);
122
123                 if (isStrDbl(buffer)) {
124                         number[number_index] = convert<double>(buffer);
125                         ++number_index;
126                         return 'n';
127                 }
128                 return 'E';
129         }
130
131         i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
132         if (i != 0) {
133                 if (unit_index > 3)
134                         return 'E';
135
136                 string buffer;
137
138                 // we have found some alphabetical string
139                 if (i == string::npos) {
140                         buffer = data;
141                         i = data.size() + 1;
142                 } else
143                         buffer = data.substr(0, i);
144
145                 // possibly we have "mmplus" string or similar
146                 if (buffer.size() > 5 &&
147                                 (buffer.substr(2, 4) == string("plus") ||
148                                  buffer.substr(2, 5) == string("minus")))
149                 {
150                         lyx_advance(data, 2);
151                         unit[unit_index] = unitFromString(buffer.substr(0, 2));
152                 } else {
153                         lyx_advance(data, i);
154                         unit[unit_index] = unitFromString(buffer);
155                 }
156
157                 if (unit[unit_index] != Length::UNIT_NONE) {
158                         ++unit_index;
159                         return 'u';
160                 }
161                 return 'E';  // Error
162         }
163         return 'E';  // Error
164 }
165
166
167 /// latex representation of a vspace
168 struct LaTeXLength {
169         char const * pattern;
170         int  plus_val_index;
171         int  minus_val_index;
172         int  plus_uni_index;
173         int  minus_uni_index;
174 };
175
176
177 /// the possible formats for a vspace string
178 LaTeXLength table[] = {
179         { "nu",       0, 0, 0, 0 },
180         { "nu+nu",    2, 0, 2, 0 },
181         { "nu+nu-nu", 2, 3, 2, 3 },
182         { "nu+-nu",   2, 2, 2, 2 },
183         { "nu-nu",    0, 2, 0, 2 },
184         { "nu-nu+nu", 3, 2, 3, 2 },
185         { "nu-+nu",   2, 2, 2, 2 },
186         { "n+nu",     2, 0, 1, 0 },
187         { "n+n-nu",   2, 3, 1, 1 },
188         { "n+-nu",    2, 2, 1, 1 },
189         { "n-nu",     0, 2, 0, 1 },
190         { "n-n+nu",   3, 2, 1, 1 },
191         { "n-+nu",    2, 2, 1, 1 },
192         { "",         0, 0, 0, 0 }   // sentinel, must be empty
193 };
194
195
196 } // namespace anon
197
198 const char * stringFromUnit(int unit)
199 {
200         if (unit < 0 || unit > num_units)
201                 return 0;
202         return unit_name[unit];
203 }
204
205
206 bool isValidGlueLength(string const & data, GlueLength * result)
207 {
208         // This parser is table-driven.  First, it constructs a "pattern"
209         // that describes the sequence of tokens in "data".  For example,
210         // "n-nu" means: number, minus sign, number, unit.  As we go along,
211         // numbers and units are stored into static arrays.  Then, "pattern"
212         // is searched in the "table".  If it is found, the associated
213         // table entries tell us which number and unit should go where
214         // in the Length structure.  Example: if "data" has the "pattern"
215         // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
216         // That means, "plus_val" is the second number that was seen
217         // in the input, "minus_val" is the third number, and "plus_uni"
218         // and "minus_uni" are the second and third units, respectively.
219         // ("val" and "uni" are always the first items seen in "data".)
220         // This is the most elegant solution I could find -- a straight-
221         // forward approach leads to very long, tedious code that would be
222         // much harder to understand and maintain. (AS)
223
224         if (data.empty())
225                 return true;
226         string buffer = ltrim(data);
227
228         // To make isValidGlueLength recognize negative values as
229         // the first number this little hack is needed:
230         int val_sign = 1; // positive as default
231         switch (buffer[0]) {
232         case '-':
233                 lyx_advance(buffer, 1);
234                 val_sign = -1;
235                 break;
236         case '+':
237                 lyx_advance(buffer, 1);
238                 break;
239         default:
240                 break;
241         }
242         // end of hack
243
244         int  pattern_index = 0;
245         int  table_index = 0;
246         char pattern[20];
247
248         number_index = 1;
249         unit_index = 1;  // entries at index 0 are sentinels
250
251         // construct "pattern" from "data"
252         while (!isEndOfData (buffer)) {
253                 if (pattern_index > 20) return false;
254                 pattern[pattern_index] = nextToken (buffer);
255                 if (pattern[pattern_index] == 'E') return false;
256                 ++pattern_index;
257         }
258         pattern[pattern_index] = '\0';
259
260         // search "pattern" in "table"
261         table_index = 0;
262         while (compare(pattern, table[table_index].pattern)) {
263                 ++table_index;
264                 if (!*table[table_index].pattern)
265                         return false;
266         }
267
268         // Get the values from the appropriate places.  If an index
269         // is zero, the corresponding array value is zero or UNIT_NONE,
270         // so we needn't check this.
271         if (result) {
272                 result->len_.value  (number[1] * val_sign);
273                 result->len_.unit   (unit[1]);
274                 result->plus_.value (number[table[table_index].plus_val_index]);
275                 result->plus_.unit  (unit  [table[table_index].plus_uni_index]);
276                 result->minus_.value(number[table[table_index].minus_val_index]);
277                 result->minus_.unit (unit  [table[table_index].minus_uni_index]);
278         }
279         return true;
280 }
281
282
283 bool isValidLength(string const & data, Length * result)
284 {
285         // This is a trimmed down version of isValidGlueLength.
286         // The parser may seem overkill for lengths without
287         // glue, but since we already have it, using it is
288         // easier than writing something from scratch.
289         if (data.empty())
290                 return true;
291
292         string   buffer = data;
293         int      pattern_index = 0;
294         char     pattern[3];
295
296         // To make isValidLength recognize negative values
297         // this little hack is needed:
298         int val_sign = 1; // positive as default
299         switch (buffer[0]) {
300         case '-':
301                 lyx_advance(buffer, 1);
302                 val_sign = -1;
303                 break;
304         case '+':
305                 lyx_advance(buffer, 1);
306                 // fall through
307         default:
308                 // no action
309                 break;
310         }
311         // end of hack
312
313         number_index = unit_index = 1;  // entries at index 0 are sentinels
314
315         // construct "pattern" from "data"
316         while (!isEndOfData(buffer)) {
317                 if (pattern_index > 2)
318                         return false;
319                 pattern[pattern_index] = nextToken(buffer);
320                 if (pattern[pattern_index] == 'E')
321                         return false;
322                 ++pattern_index;
323         }
324         pattern[pattern_index] = '\0';
325
326         // only the most basic pattern is accepted here
327         if (compare(pattern, "nu") != 0) return false;
328
329         // It _was_ a correct length string.
330         // Store away the values we found.
331         if (result) {
332                 result->val_  = number[1] * val_sign;
333                 result->unit_ = unit[1];
334         }
335         return true;
336 }
337
338
339 //
340 //  VSpace class
341 //
342
343 VSpace::VSpace()
344         : kind_(DEFSKIP), len_(), keep_(false)
345 {}
346
347
348 VSpace::VSpace(vspace_kind k)
349         : kind_(k), len_(), keep_(false)
350 {}
351
352
353 VSpace::VSpace(Length const & l)
354         : kind_(LENGTH), len_(l), keep_(false)
355 {}
356
357
358 VSpace::VSpace(GlueLength const & l)
359         : kind_(LENGTH), len_(l), keep_(false)
360 {}
361
362
363 VSpace::VSpace(string const & data)
364         : kind_(DEFSKIP), len_(), keep_(false)
365 {
366         if (data.empty())
367                 return;
368
369         string input = rtrim(data);
370
371         string::size_type const length = input.length();
372
373         if (length > 1 && input[length - 1] == '*') {
374                 keep_ = true;
375                 input.erase(length - 1);
376         }
377
378         if (prefixIs(input, "defskip"))
379                 kind_ = DEFSKIP;
380         else if (prefixIs(input, "smallskip"))
381                 kind_ = SMALLSKIP;
382         else if (prefixIs(input, "medskip"))
383                 kind_ = MEDSKIP;
384         else if (prefixIs(input, "bigskip"))
385                 kind_ = BIGSKIP;
386         else if (prefixIs(input, "vfill"))
387                 kind_ = VFILL;
388         else if (isValidGlueLength(input, &len_))
389                 kind_ = LENGTH;
390         else if (isStrDbl(input)) {
391                 // This last one is for reading old .lyx files
392                 // without units in added_space_top/bottom.
393                 // Let unit default to centimeters here.
394                 kind_ = LENGTH;
395                 len_  = GlueLength(Length(convert<double>(input), Length::CM));
396         }
397 }
398
399
400 VSpace::vspace_kind VSpace::kind() const
401 {
402         return kind_;
403 }
404
405
406 GlueLength const & VSpace::length() const
407 {
408         return len_;
409 }
410
411
412 bool VSpace::keep() const
413 {
414         return keep_;
415 }
416
417
418 void VSpace::setKeep(bool val)
419 {
420         keep_ = val;
421 }
422
423
424 bool VSpace::operator==(VSpace const & other) const
425 {
426         if (kind_ != other.kind_)
427                 return false;
428
429         if (kind_ != LENGTH)
430                 return this->keep_ == other.keep_;
431
432         if (len_ != other.len_)
433                 return false;
434
435         return keep_ == other.keep_;
436 }
437
438
439 string const VSpace::asLyXCommand() const
440 {
441         string result;
442         switch (kind_) {
443         case DEFSKIP:   result = "defskip";      break;
444         case SMALLSKIP: result = "smallskip";    break;
445         case MEDSKIP:   result = "medskip";      break;
446         case BIGSKIP:   result = "bigskip";      break;
447         case VFILL:     result = "vfill";        break;
448         case LENGTH:    result = len_.asString(); break;
449         }
450         if (keep_)
451                 result += '*';
452         return result;
453 }
454
455
456 string const VSpace::asLatexCommand(BufferParams const & params) const
457 {
458         switch (kind_) {
459         case DEFSKIP:
460                 return params.getDefSkip().asLatexCommand(params);
461
462         case SMALLSKIP:
463                 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
464
465         case MEDSKIP:
466                 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
467
468         case BIGSKIP:
469                 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
470
471         case VFILL:
472                 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
473
474         case LENGTH:
475                 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
476                         : "\\vspace{" + len_.asLatexString() + '}';
477
478         default:
479                 BOOST_ASSERT(false);
480                 return string();
481         }
482 }
483
484
485 docstring const VSpace::asGUIName() const
486 {
487         docstring result;
488         switch (kind_) {
489         case DEFSKIP:
490                 result = _("Default skip");
491                 break;
492         case SMALLSKIP:
493                 result = _("Small skip");
494                 break;
495         case MEDSKIP:
496                 result = _("Medium skip");
497                 break;
498         case BIGSKIP:
499                 result = _("Big skip");
500                 break;
501         case VFILL:
502                 result = _("Vertical fill");
503                 break;
504         case LENGTH:
505                 result = from_ascii(len_.asString());
506                 break;
507         }
508         if (keep_)
509                 result += ", " + _("protected");
510         return result;
511 }
512
513
514 int VSpace::inPixels(BufferView const & bv) const
515 {
516         // Height of a normal line in pixels (zoom factor considered)
517         int const default_height = defaultRowHeight();
518
519         switch (kind_) {
520
521         case DEFSKIP:
522                 return bv.buffer()->params().getDefSkip().inPixels(bv);
523
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;
528
529         case MEDSKIP:
530                 return default_height / 2;
531
532         case BIGSKIP:
533                 return default_height;
534
535         case VFILL:
536                 // leave space for the vfill symbol
537                 return 3 * default_height;
538
539         case LENGTH:
540                 return len_.len().inPixels(bv.workWidth());
541
542         default:
543                 BOOST_ASSERT(false);
544                 return 0;
545         }
546 }
547
548
549 } // namespace lyx