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