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