]> git.lyx.org Git - lyx.git/blob - src/vspace.C
Fix natbib bug spotted by JMarc.
[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 "buffer.h"
18 #include "lyxrc.h"
19 #include "BufferView.h"
20 #include "support/LAssert.h"
21
22 #include "support/lstrings.h"
23
24 #include <cstdio>
25
26 #ifndef CXX_GLOBAL_CSTD
27 using std::sscanf;
28 #endif
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 (sscanf(input.c_str(), "%lf", &value) == 1) {
363                 // This last one is for reading old .lyx files
364                 // without units in added_space_top/bottom.
365                 // Let unit default to centimeters here.
366                 kind_ = LENGTH;
367                 len_  = LyXGlueLength(LyXLength(value, LyXLength::CM));
368         }
369 }
370
371
372 VSpace::vspace_kind VSpace::kind() const
373 {
374         return kind_;
375 }
376
377
378 LyXGlueLength VSpace::length() const
379 {
380         return len_;
381 }
382
383
384 bool VSpace::keep() const
385 {
386         return keep_;
387 }
388
389
390 void VSpace::setKeep(bool val)
391 {
392         keep_ = val;
393 }
394
395
396 bool VSpace::operator==(VSpace const & other) const
397 {
398         if (kind_ != other.kind_)
399                 return false;
400
401         if (kind_ != LENGTH)
402                 return this->keep_ == other.keep_;
403
404         if (len_ != other.len_)
405                 return false;
406
407         return keep_ == other.keep_;
408 }
409
410
411 string const VSpace::asLyXCommand() const
412 {
413         string result;
414         switch (kind_) {
415         case NONE:      break;
416         case DEFSKIP:   result = "defskip";      break;
417         case SMALLSKIP: result = "smallskip";    break;
418         case MEDSKIP:   result = "medskip";      break;
419         case BIGSKIP:   result = "bigskip";      break;
420         case VFILL:     result = "vfill";        break;
421         case LENGTH:    result = len_.asString(); break;
422         }
423         if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
424                 result += '*';
425         return result;
426 }
427
428
429 string const VSpace::asLatexCommand(BufferParams const & params) const
430 {
431         string ret;
432
433         switch (kind_) {
434         case NONE:
435                 break;
436         case DEFSKIP:
437                 ret = params.getDefSkip().asLatexCommand(params);
438                 break;
439         case SMALLSKIP:
440                 ret = keep_ ? "\\vspace*{\\smallskipamount}"
441                         : "\\smallskip{}";
442                 break;
443         case MEDSKIP:
444                 ret = keep_ ? "\\vspace*{\\medskipamount}"
445                         : "\\medskip{}";
446                 break;
447         case BIGSKIP:
448                 ret = keep_ ? "\\vspace*{\\bigskipamount}"
449                         : "\\bigskip{}";
450                 break;
451         case VFILL:
452                 ret = keep_ ? "\\vspace*{\\fill}"
453                         : "\\vfill{}";
454                 break;
455         case LENGTH:
456         {
457                 string const lenstr = len_.asLatexString();
458
459                 ret = keep_ ? "\\vspace*{" + lenstr + '}'
460                         : "\\vspace{" + lenstr + '}';
461         }
462         break;
463
464         }
465
466         return ret;
467 }
468
469
470
471 int VSpace::inPixels(BufferView const * bv) const
472 {
473         // Height of a normal line in pixels (zoom factor considered)
474         int const default_height = bv->defaultHeight(); // [pixels]
475
476         int retval = 0;
477
478         switch (kind_) {
479
480         case NONE:
481                 // value for this is already set
482                 break;
483
484         case DEFSKIP:
485                 retval = bv->buffer()->params.getDefSkip().inPixels(bv);
486                 break;
487
488         // This is how the skips are normally defined by LateX.
489         // But there should be some way to change this per document.
490         case SMALLSKIP:
491                 retval = default_height / 4;
492                 break;
493
494         case MEDSKIP:
495                 retval = default_height / 2;
496                 break;
497
498         case BIGSKIP:
499                 retval = default_height;
500                 break;
501
502         case VFILL:
503                 // leave space for the vfill symbol
504                 retval = 3 * default_height;
505                 break;
506
507         case LENGTH:
508                 retval = len_.len().inPixels(bv->workWidth());
509                 break;
510
511         }
512         return retval;
513 }