]> git.lyx.org Git - lyx.git/blob - src/vspace.C
my changes during the holidyas...i expect some compilers to have some problems, but...
[lyx.git] / src / vspace.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 1995 Matthias Ettrich
8  *          Copyright 1995-1999 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation "vspace.h"
16 #endif
17
18 #include "lyx_main.h"
19 #include "buffer.h"
20 #include "vspace.h"
21 #include "lyxrc.h"
22 #include "lyxtext.h"
23 #include <cstring>
24 #include "BufferView.h"
25 #include "support/lstrings.h"
26
27 extern LyXRC * lyxrc;
28 extern BufferView * current_view;
29
30 /*  length units
31  */
32
33 static const int num_units = LyXLength::UNIT_NONE;
34
35 // I am not sure if "mu" should be possible to select (Lgb)
36 static char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
37                                              "mm", "pc", "cc", "cm",
38                                              "in", "ex", "em", "mu" }; 
39
40
41 LyXLength::UNIT unitFromString (string const & data)
42 {
43         int i = 0;
44         while ((i < num_units) && (data != unit_name[i])) ++i;
45         return static_cast<LyXLength::UNIT>(i);
46 }
47
48 /*  The following static items form a simple scanner for
49  *  length strings, used by isValid[Glue]Length.  See comments there.
50  */
51 static float           number[4] = { 0, 0, 0, 0 };
52 static LyXLength::UNIT unit[4]   = { LyXLength::UNIT_NONE,
53                                      LyXLength::UNIT_NONE,
54                                      LyXLength::UNIT_NONE,
55                                      LyXLength::UNIT_NONE };
56 //static int number_index, unit_index;
57 int number_index, unit_index;
58
59 static inline void lyx_advance (string & data, unsigned int n)
60 {
61         data.erase(0, n);
62 }
63
64 static inline bool isEndOfData (string const & data)
65 {
66         return frontStrip (data).empty();
67 }
68
69 static char nextToken (string & data)
70 {
71         data = frontStrip(data);
72         if (data.empty())
73                 return '\0';
74         else if (data[0] == '+') {
75                 lyx_advance (data, 1);
76                 return '+';
77         }
78         else if (prefixIs(data, "plus")) {
79                 lyx_advance (data, 4);
80                 return '+';
81         }
82         else if (data[0] == '-') {
83                 lyx_advance (data, 1);
84                 return '-';
85         }
86         else if (prefixIs(data, "minus")) {
87                 lyx_advance (data, 5);
88                 return '-';
89         }
90         else {
91                 string::size_type i;
92
93                 // I really mean assignment ("=") below, not equality!
94                 if ((i = data.find_last_of("0123456789.")) != string::npos) {
95                         if (number_index > 3) return 'E';  // Error
96                         string buffer = data.substr(0, i + 1);
97                         if (sscanf (buffer.c_str(),
98                                     "%f", &number[number_index]) == 1) {
99                                 lyx_advance (data, i + 1);
100                                 ++number_index;
101                                 return 'n';
102                         } else 
103                                 return 'E';  // Error
104                 } else if ((i = data.find_last_of("abcdefghijklmnopqrstuvwxyz"))
105                            != string::npos) {
106                         if (unit_index > 3) return 'E';  // Error
107                         string buffer = data.substr(0, i + 1);
108                         unit[unit_index] = unitFromString (buffer);
109                         if (unit[unit_index] != LyXLength::UNIT_NONE) {
110                                 lyx_advance (data, i + 1);
111                                 ++unit_index;
112                                 return 'u';
113                         } else
114                                 return 'E';  // Error
115                 } else
116                         return 'E';  // Error
117         }
118 }
119
120 static struct {
121         char const * pattern;
122         int   plus_val_index, minus_val_index,
123                 plus_uni_index, minus_uni_index;
124 } table[] = { { "nu",       0, 0, 0, 0 },
125               { "nu+nu",    2, 0, 2, 0 },
126               { "nu+nu-nu", 2, 3, 2, 3 },
127               { "nu+-nu",   2, 2, 2, 2 },
128               { "nu-nu",    0, 2, 0, 2 },
129               { "nu-nu+nu", 3, 2, 3, 2 },
130               { "nu-+nu",   2, 2, 2, 2 },
131               { "n+nu",     2, 0, 1, 0 },
132               { "n+n-nu",   2, 3, 1, 1 },
133               { "n+-nu",    2, 2, 1, 1 },
134               { "n-nu",     0, 2, 0, 1 },
135               { "n-n+nu",   3, 2, 1, 1 },
136               { "n-+nu",    2, 2, 1, 1 },
137               { "",         0, 0, 0, 0 }   // sentinel, must be empty
138 };
139
140 bool isValidGlueLength (string const & data, LyXGlueLength * result)
141 {
142         // This parser is table-driven.  First, it constructs a "pattern"
143         // that describes the sequence of tokens in "data".  For example,
144         // "n-nu" means: number, minus sign, number, unit.  As we go along,
145         // numbers and units are stored into static arrays.  Then, "pattern"
146         // is searched in the "table".  If it is found, the associated
147         // table entries tell us which number and unit should go where
148         // in the LyXLength structure.  Example: if "data" has the "pattern"
149         // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".  
150         // That means, "plus_val" is the second number that was seen
151         // in the input, "minus_val" is the third number, and "plus_uni"
152         // and "minus_uni" are the second and third units, respectively.
153         // ("val" and "uni" are always the first items seen in "data".)
154         //     This is the most elegant solution I could find -- a straight-
155         // forward approach leads to very long, tedious code that would be
156         // much harder to understand and maintain. (AS)
157
158         if (data.empty())
159                 return true;
160         string buffer = frontStrip(data);
161
162         // To make isValidGlueLength recognize negative values as
163         // the first number this little hack is needed:
164         short val_sign = 1; // positive as default
165         switch(buffer[0]) {
166         case '-':
167                 lyx_advance(buffer, 1);
168                 val_sign = -1;
169                 break;
170         case '+':
171                 lyx_advance(buffer, 1);
172                 // fall through
173         default:
174                 // no action
175                 break;
176         }
177         // end of hack
178         
179         int       pattern_index = 0, table_index = 0;
180         char      pattern[20];
181
182         number_index = unit_index = 1;  // entries at index 0 are sentinels
183
184         // construct "pattern" from "data"
185         while (!isEndOfData (buffer)) {
186                 if (pattern_index > 20) return false;
187                 pattern[pattern_index] = nextToken (buffer);
188                 if (pattern[pattern_index] == 'E') return false;
189                 ++pattern_index;
190         }
191         pattern[pattern_index] = '\0';
192
193         // search "pattern" in "table"
194         table_index = 0;
195         while (compare(pattern, table[table_index].pattern)) {
196                 ++table_index;
197                 if (!*table[table_index].pattern) return false;
198         }
199         
200         // Get the values from the appropriate places.  If an index
201         // is zero, the corresponding array value is zero or UNIT_NONE,
202         // so we needn't check this.
203         if (result) {
204                 result->val = number[1] * val_sign;
205                 result->uni = unit[1];
206                 result->plus_val  = number[table[table_index].plus_val_index];
207                 result->minus_val = number[table[table_index].minus_val_index];
208                 result->plus_uni  = unit  [table[table_index].plus_uni_index];
209                 result->minus_uni = unit  [table[table_index].minus_uni_index];
210         }
211         return true;
212 }
213
214
215 bool isValidLength(string const & data, LyXLength * result)
216 {
217         /// This is a trimmed down version of isValidGlueLength.
218         /// The parser may seem overkill for lengths without 
219         /// glue, but since we already have it, using it is
220         /// easier than writing something from scratch.
221
222         string   buffer = data;
223         int       pattern_index = 0;
224         char      pattern[3];
225
226         // To make isValidLength recognize negative values
227         // this little hack is needed:
228         short val_sign = 1; // positive as default
229         switch(buffer[0]) {
230         case '-':
231                 lyx_advance(buffer, 1);
232                 val_sign = -1;
233                 break;
234         case '+':
235                 lyx_advance(buffer, 1);
236                 // fall through
237         default:
238                 // no action
239                 break;
240         }
241         // end of hack
242         
243         number_index = unit_index = 1;  // entries at index 0 are sentinels
244
245         // construct "pattern" from "data"
246         while (!isEndOfData (buffer)) {
247                 if (pattern_index > 2) return false;
248                 pattern[pattern_index] = nextToken (buffer);
249                 if (pattern[pattern_index] == 'E') return false;
250                 ++pattern_index;
251         }
252         pattern[pattern_index] = '\0';
253
254         // only the most basic pattern is accepted here
255         if (compare(pattern, "nu") != 0) return false;          
256         
257         // It _was_ a correct length string.  
258         // Store away the values we found.
259         if (result) {
260                 result->val = number[1] * val_sign;
261                 result->uni = unit[1];
262         }
263         return true;
264 }
265
266 /// LyXLength class
267
268 LyXLength::LyXLength(string const & data)
269 {
270         LyXLength tmp;
271         
272         if (!isValidLength (data, &tmp))
273                 return; // should raise an exception
274         else {
275                 val = tmp.val;
276                 uni = tmp.uni;
277         }
278 }
279
280 bool LyXLength::operator== (LyXLength const & other) const
281 {
282         return (val == other.val &&
283                 uni == other.uni);
284 }
285
286 string LyXLength::asString() const
287 {
288         char buffer[20];
289
290         sprintf (buffer, "%g%s", val, unit_name[uni]);
291         return string (buffer);
292 }
293
294
295 /*  LyXGlueLength class
296  */
297
298 LyXGlueLength::LyXGlueLength (string const & data)
299 {
300         LyXGlueLength tmp(0.0, PT);
301
302         if (!isValidGlueLength (data, &tmp))
303                 return; // should raise an exception
304         else {
305                 val = tmp.val;
306                 uni = tmp.uni;
307                 plus_val = tmp.plus_val;
308                 plus_uni = tmp.plus_uni;
309                 minus_val = tmp.minus_val;
310                 minus_uni = tmp.minus_uni;
311         }
312 }
313
314
315 bool LyXGlueLength::operator== (LyXGlueLength const & other) const
316 {
317         return (val == other.val && 
318                 uni == other.uni &&
319                 plus_val  == other.plus_val &&
320                 plus_uni  == other.plus_uni &&
321                 minus_val == other.minus_val &&
322                 minus_uni == other.minus_uni);
323 }
324
325
326 string LyXGlueLength::asString() const
327 {
328         char buffer[20];
329         
330         if (plus_val != 0.0)
331                 if (minus_val != 0.0)
332                         if ((uni == plus_uni) && (uni == minus_uni))
333                                 if (plus_val == minus_val)
334                                         sprintf (buffer, "%g+-%g%s",
335                                                  val, plus_val, unit_name[uni]);
336                                 else
337                                         sprintf (buffer, "%g+%g-%g%s",
338                                                  val, plus_val, minus_val, 
339                                                  unit_name[uni]);
340                         else
341                                 if ((plus_uni == minus_uni) && (plus_val == minus_val))
342                                         sprintf (buffer, "%g%s+-%g%s",
343                                                  val, unit_name[uni],
344                                                  plus_val, unit_name[plus_uni]);
345                                 else
346                                         sprintf (buffer, "%g%s+%g%s-%g%s",
347                                                  val,       unit_name[uni],
348                                                  plus_val,  unit_name[plus_uni],
349                                                  minus_val, unit_name[minus_uni]);
350                 else 
351                         if (uni == plus_uni)
352                                 sprintf (buffer, "%g+%g%s",
353                                          val, plus_val, unit_name[uni]);
354                         else
355                                 sprintf (buffer, "%g%s+%g%s",
356                                          val,      unit_name[uni],
357                                          plus_val, unit_name[plus_uni]);
358         else
359                 if (minus_val != 0.0)
360                         if (uni == minus_uni)
361                                 sprintf (buffer, "%g-%g%s",
362                                          val, minus_val, unit_name[uni]);
363                         else
364                                 sprintf (buffer, "%g%s-%g%s",
365                                          val,       unit_name[uni],
366                                          minus_val, unit_name[minus_uni]);
367                 else
368                         sprintf (buffer, "%g%s", val, unit_name[uni]);
369         return string (buffer);
370 }
371
372
373 string LyXGlueLength::asLatexString() const
374 {
375         char buffer[40];
376
377         if (plus_val != 0.0)
378                 if (minus_val != 0.0)
379                         sprintf (buffer, "%g%s plus %g%s minus %g%s",
380                                  val, unit_name[uni],
381                                  plus_val,  unit_name[plus_uni],
382                                  minus_val, unit_name[minus_uni]);
383                 else
384                         sprintf (buffer, "%g%s plus %g%s",
385                                  val,      unit_name[uni],
386                                  plus_val, unit_name[plus_uni]);
387         else
388                 if (minus_val != 0.0)
389                         sprintf (buffer, "%g%s minus %g%s",
390                                  val,       unit_name[uni],
391                                  minus_val, unit_name[minus_uni]);
392                 else
393                         sprintf (buffer, "%g%s",
394                                  val, unit_name[uni]);
395         return string (buffer);
396 }
397
398
399 /*  VSpace class
400  */
401
402 VSpace::VSpace (string const & data)
403         : kin (NONE), len (0.0, LyXLength::PT) 
404 {
405         kp = false;
406         if (data.empty())
407                 return;
408         float   value;
409         string input  = strip(data);
410
411         int length = input.length();
412
413         if (length > 1 && input[length-1] == '*') {
414                 kp = true;
415                 input.erase(length - 1);
416         }
417
418         if      (prefixIs (input, "defskip"))   kin = DEFSKIP;
419         else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
420         else if (prefixIs (input, "medskip"))   kin = MEDSKIP;
421         else if (prefixIs (input, "bigskip"))   kin = BIGSKIP;
422         else if (prefixIs (input, "vfill"))     kin = VFILL;
423         else if (isValidGlueLength (input, &len))
424                 kin = LENGTH;
425         else if (sscanf (input.c_str(), "%f", &value) == 1) {
426                 // This last one is for reading old .lyx files
427                 // without units in added_space_top/bottom.
428                 // Let unit default to centimeters here.
429                 kin = LENGTH;
430                 len = LyXGlueLength (value, LyXLength::CM);
431         }
432 }
433
434
435 bool VSpace::operator==(VSpace const & other) const
436 {
437         if (this->kin == other.kin) 
438                 if (this->kin == LENGTH)
439                         if (this->len == other.len)
440                                 return this->kp == other.kp;
441                         else
442                                 return false;
443                 else
444                         return this->kp == other.kp;
445         else
446                 return false;
447 }
448
449
450 string VSpace::asLyXCommand() const
451 {
452         string result;
453
454         switch (kin) {
455         case NONE:      break;
456         case DEFSKIP:   result = "defskip";      break;
457         case SMALLSKIP: result = "smallskip";    break;
458         case MEDSKIP:   result = "medskip";      break;
459         case BIGSKIP:   result = "bigskip";      break;
460         case VFILL:     result = "vfill";        break;
461         case LENGTH:    result = len.asString(); break;
462         }
463         if (kp && (kin != NONE) && (kin != DEFSKIP))
464                 return result += '*';
465         else
466                 return result;
467 }
468
469
470 string VSpace::asLatexCommand() const
471 {
472         switch (kin) {
473         case NONE:      return string();
474         case DEFSKIP:   
475                 return current_view->buffer()->params.getDefSkip().asLatexCommand();
476         case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
477                                 : "\\smallskip{}";
478         case MEDSKIP:   return kp ? "\\vspace*{\\medskipamount}"
479                                 : "\\medskip{}";
480         case BIGSKIP:   return kp ? "\\vspace*{\\bigskipamount}"
481                                 : "\\bigskip{}";
482         case VFILL:     return kp ? "\\vspace*{\\fill}"
483                                 : "\\vfill{}";
484         case LENGTH:    return kp ? "\\vspace*{" + len.asLatexString() + '}'
485                                 : "\\vspace{" + len.asLatexString() + '}';
486         }
487         return string();  // should never be reached
488 }
489
490
491 int VSpace::inPixels() const
492 {
493         // Height of a normal line in pixels (zoom factor considered)
494         int height = current_view->text->DefaultHeight(); // [pixels]
495
496         // Zoom factor specified by user in percent
497         float const zoom = lyxrc->zoom / 100.0; // [percent]
498
499         // DPI setting for monitor: pixels/inch
500         float const dpi = lyxrc->dpi; // screen resolution [pixels/inch]
501
502         // We want the result in pixels
503         float result, value;
504
505         switch (kin) {
506         case NONE: return 0;
507
508         case DEFSKIP:
509                 return current_view->buffer()->params.getDefSkip().inPixels();
510
511                 // This is how the skips are normally defined by
512                 // LateX.  But there should be some way to change
513                 // this per document.
514         case SMALLSKIP: return height/4;
515         case MEDSKIP:   return height/2;
516         case BIGSKIP:   return height;
517         case VFILL:     return 3*height;
518                 // leave space for the vfill symbol
519         case LENGTH:
520                 // Pixel values are scaled so that the ratio
521                 // between lengths and font sizes on the screen
522                 // is the same as on paper.
523
524                 // we don't care about sign of value, we
525                 // can't display negative anyway
526                 result = 0.0;
527                 value = len.value();
528                 short val_sign = value < 0.0 ? -1 : 1;
529                 
530                 switch (len.unit()) {
531                 case LyXLength::SP:
532                         // Scaled point: sp = 1/65536 pt
533                         result = zoom * dpi * value
534                                 / (72.27 * 65536); // 4736286.7
535                         break;
536                 case LyXLength::PT:
537                         // Point: 1 pt = 1/72.27 inch
538                         result = zoom * dpi * value
539                                 / 72.27; // 72.27
540                         break;
541                 case LyXLength::BP:
542                         // Big point: 1 bp = 1/72 inch
543                         result = zoom * dpi * value
544                                 / 72; // 72
545                         break;
546                 case LyXLength::DD:
547                         // Didot: 1157dd = 1238 pt?
548                         result = zoom * dpi * value
549                                 / (72.27 / (0.376 * 2.845)); // 67.559735
550                         break;
551                 case LyXLength::MM:
552                         // Millimeter: 1 mm = 1/25.4 inch
553                         result = zoom * dpi * value
554                                 / 25.4; // 25.4
555                         break;
556                 case LyXLength::PC:
557                         // Pica: 1 pc = 12 pt
558                         result = zoom * dpi * value
559                                 / (72.27 / 12); // 6.0225
560                         break;
561                 case LyXLength::CC:
562                         // Cicero: 1 cc = 12 dd
563                         result = zoom * dpi * value
564                                 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
565                         break;
566                 case LyXLength::CM:
567                         // Centimeter: 1 cm = 1/2.54 inch
568                         result = zoom * dpi * value
569                                 / 2.54; // 2.54
570                         break;
571                 case LyXLength::IN:
572                         // Inch
573                         result = zoom * dpi * value;
574                         break;
575                 case LyXLength::EX:
576                         // Ex: The height of an "x"
577                         result = zoom * value * height / 2; // what to / width?
578                         break;
579                 case LyXLength::EM: // what to / width?
580                         // Em: The width of an "m"
581                         result = zoom * value * height / 2; // Why 2?
582                         break;
583                 case LyXLength::MU: // This is probably only allowed in
584                         // math mode
585                         result = zoom * value * height;
586                         break;
587                 case LyXLength::UNIT_NONE:
588                         result = 0;  // this cannot happen
589                         break;  
590                 }
591                 return int (result * val_sign + 0.5);
592         }
593         return 0; // never reached
594 }