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