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