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