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