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