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