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