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