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