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