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