]> git.lyx.org Git - lyx.git/blob - src/vspace.C
312753288bf73ca47aaf28d23e8d74ff60a5635f
[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).c_str();
100                         double x = strToDbl(buffer);
101                         if (x || (buffer[0] == '0')) {
102                                 number[number_index] = x;
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 string LyXLength::asString() const
294 {
295 #ifdef HAVE_SSTREAM
296         std::ostringstream buffer;
297         buffer << val << unit_name[uni]; // setw?
298         return buffer.str().c_str();
299 #else
300         char tbuf[20];
301         ostrstream buffer(tbuf, 20);
302         buffer << val << unit_name[uni] << '\0'; // setw?
303         return buffer.str();
304 #endif
305 }
306
307
308 /*  LyXGlueLength class
309  */
310
311 LyXGlueLength::LyXGlueLength (string const & data)
312 {
313         LyXGlueLength tmp(0.0, PT);
314
315         if (!isValidGlueLength (data, &tmp))
316                 return; // should raise an exception
317         else {
318                 val = tmp.val;
319                 uni = tmp.uni;
320                 plus_val = tmp.plus_val;
321                 plus_uni = tmp.plus_uni;
322                 minus_val = tmp.minus_val;
323                 minus_uni = tmp.minus_uni;
324         }
325 }
326
327
328 string LyXGlueLength::asString() const
329 {
330 #ifdef HAVE_SSTREAM
331         std::ostringstream buffer;
332 #else
333         char tbuf[20];
334         ostrstream buffer(tbuf, 20);
335 #endif
336         if (plus_val != 0.0)
337                 if (minus_val != 0.0)
338                         if ((uni == plus_uni) && (uni == minus_uni))
339                                 if (plus_val == minus_val)
340                                         buffer << val << "+-"
341                                                << plus_val << unit_name[uni];
342                                 else
343                                         buffer << val
344                                                << '+' << plus_val
345                                                << '-' << minus_val
346                                                << unit_name[uni];
347                         else
348                                 if (plus_uni == minus_uni
349                                     && plus_val == minus_val)
350                                         buffer << val << unit_name[uni]
351                                                << "+-" << plus_val
352                                                << unit_name[plus_uni];
353         
354                                 else
355                                         buffer << val << unit_name[uni]
356                                                << '+' << plus_val
357                                                << unit_name[plus_uni]
358                                                << '-' << minus_val
359                                                << unit_name[minus_uni];
360                 else 
361                         if (uni == plus_uni)
362                                 buffer << val << '+' << plus_val
363                                        << unit_name[uni];
364                         else
365                                 buffer << val << unit_name[uni]
366                                        << '+' << plus_val
367                                        << unit_name[plus_uni];
368         
369         else
370                 if (minus_val != 0.0)
371                         if (uni == minus_uni)
372                                 buffer << val << '-' << minus_val
373                                        << unit_name[uni];
374         
375                         else
376                                 buffer << val << unit_name[uni]
377                                        << '-' << minus_val
378                                        << unit_name[minus_uni];
379                 else
380                         buffer << val << unit_name[uni];
381 #ifdef HAVE_SSTREAM
382         return buffer.str().c_str();
383 #else
384         buffer << '\0';
385         return buffer.str();
386 #endif
387 }
388
389
390 string LyXGlueLength::asLatexString() const
391 {
392 #ifdef HAVE_SSTREAM
393         std::ostringstream buffer;
394 #else
395         char tbuf[40];
396         ostrstream buffer(tbuf, 40);
397 #endif
398
399         if (plus_val != 0.0)
400                 if (minus_val != 0.0)
401                         buffer << val << unit_name[uni]
402                                << " plus "
403                                << plus_val << unit_name[plus_uni]
404                                << " minus "
405                                << minus_val << unit_name[minus_uni];
406                 else
407                         buffer << val << unit_name[uni]
408                                << " plus "
409                                << plus_val << unit_name[plus_uni];
410         else
411                 if (minus_val != 0.0)
412                         buffer << val << unit_name[uni]
413                                << " minus "
414                                << minus_val << unit_name[minus_uni];
415                 else
416                         buffer << val << unit_name[uni];
417 #ifdef HAVE_SSTREAM
418         return buffer.str().c_str();
419 #else
420         buffer << '\0';
421         return buffer.str();
422 #endif
423 }
424
425
426 /*  VSpace class
427  */
428
429 VSpace::VSpace (string const & data)
430         : kin (NONE), len (0.0, LyXLength::PT) 
431 {
432         kp = false;
433         if (data.empty())
434                 return;
435         float   value;
436         string input  = strip(data);
437
438         int length = input.length();
439
440         if (length > 1 && input[length-1] == '*') {
441                 kp = true;
442                 input.erase(length - 1);
443         }
444
445         if      (prefixIs (input, "defskip"))   kin = DEFSKIP;
446         else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
447         else if (prefixIs (input, "medskip"))   kin = MEDSKIP;
448         else if (prefixIs (input, "bigskip"))   kin = BIGSKIP;
449         else if (prefixIs (input, "vfill"))     kin = VFILL;
450         else if (isValidGlueLength (input, &len))
451                 kin = LENGTH;
452         else if (sscanf (input.c_str(), "%f", &value) == 1) {
453                 // This last one is for reading old .lyx files
454                 // without units in added_space_top/bottom.
455                 // Let unit default to centimeters here.
456                 kin = LENGTH;
457                 len = LyXGlueLength (value, LyXLength::CM);
458         }
459 }
460
461
462 bool VSpace::operator==(VSpace const & other) const
463 {
464         if (this->kin == other.kin) 
465                 if (this->kin == LENGTH)
466                         if (this->len == other.len)
467                                 return this->kp == other.kp;
468                         else
469                                 return false;
470                 else
471                         return this->kp == other.kp;
472         else
473                 return false;
474 }
475
476
477 string VSpace::asLyXCommand() const
478 {
479         string result;
480
481         switch (kin) {
482         case NONE:      break;
483         case DEFSKIP:   result = "defskip";      break;
484         case SMALLSKIP: result = "smallskip";    break;
485         case MEDSKIP:   result = "medskip";      break;
486         case BIGSKIP:   result = "bigskip";      break;
487         case VFILL:     result = "vfill";        break;
488         case LENGTH:    result = len.asString(); break;
489         }
490         if (kp && (kin != NONE) && (kin != DEFSKIP))
491                 return result += '*';
492         else
493                 return result;
494 }
495
496
497 string VSpace::asLatexCommand(BufferParams const & params) const
498 {
499         switch (kin) {
500         case NONE:      return string();
501         case DEFSKIP:   
502                 return params.getDefSkip().asLatexCommand(params);
503         case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
504                                 : "\\smallskip{}";
505         case MEDSKIP:   return kp ? "\\vspace*{\\medskipamount}"
506                                 : "\\medskip{}";
507         case BIGSKIP:   return kp ? "\\vspace*{\\bigskipamount}"
508                                 : "\\bigskip{}";
509         case VFILL:     return kp ? "\\vspace*{\\fill}"
510                                 : "\\vfill{}";
511         case LENGTH:    return kp ? "\\vspace*{" + len.asLatexString() + '}'
512                                 : "\\vspace{" + len.asLatexString() + '}';
513         }
514         return string();  // should never be reached
515 }
516
517
518 int VSpace::inPixels(BufferView * bv) const
519 {
520         // Height of a normal line in pixels (zoom factor considered)
521         int height = bv->text->DefaultHeight(); // [pixels]
522         int skip = 0;
523         if (kin == DEFSKIP)
524             skip = bv->buffer()->params.getDefSkip().inPixels(bv);
525
526         return inPixels(height, skip);
527 }
528
529 int VSpace::inPixels(int default_height, int default_skip) const
530 {
531         // Height of a normal line in pixels (zoom factor considered)
532         int height = default_height; // [pixels]
533         
534         // Zoom factor specified by user in percent
535         float const zoom = lyxrc.zoom / 100.0; // [percent]
536
537         // DPI setting for monitor: pixels/inch
538         float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
539
540         // We want the result in pixels
541         float result, value;
542
543         switch (kin) {
544         case NONE: return 0;
545
546         case DEFSKIP:
547                 return default_skip;
548
549                 // This is how the skips are normally defined by
550                 // LateX.  But there should be some way to change
551                 // this per document.
552         case SMALLSKIP: return height / 4;
553         case MEDSKIP:   return height / 2;
554         case BIGSKIP:   return height;
555         case VFILL:     return 3 * height;
556                 // leave space for the vfill symbol
557         case LENGTH:
558                 // Pixel values are scaled so that the ratio
559                 // between lengths and font sizes on the screen
560                 // is the same as on paper.
561
562                 // we don't care about sign of value, we
563                 // can't display negative anyway
564                 result = 0.0;
565                 value = len.value();
566                 short val_sign = value < 0.0 ? -1 : 1;
567                 
568                 switch (len.unit()) {
569                 case LyXLength::SP:
570                         // Scaled point: sp = 1/65536 pt
571                         result = zoom * dpi * value
572                                 / (72.27 * 65536); // 4736286.7
573                         break;
574                 case LyXLength::PT:
575                         // Point: 1 pt = 1/72.27 inch
576                         result = zoom * dpi * value
577                                 / 72.27; // 72.27
578                         break;
579                 case LyXLength::BP:
580                         // Big point: 1 bp = 1/72 inch
581                         result = zoom * dpi * value
582                                 / 72; // 72
583                         break;
584                 case LyXLength::DD:
585                         // Didot: 1157dd = 1238 pt?
586                         result = zoom * dpi * value
587                                 / (72.27 / (0.376 * 2.845)); // 67.559735
588                         break;
589                 case LyXLength::MM:
590                         // Millimeter: 1 mm = 1/25.4 inch
591                         result = zoom * dpi * value
592                                 / 25.4; // 25.4
593                         break;
594                 case LyXLength::PC:
595                         // Pica: 1 pc = 12 pt
596                         result = zoom * dpi * value
597                                 / (72.27 / 12); // 6.0225
598                         break;
599                 case LyXLength::CC:
600                         // Cicero: 1 cc = 12 dd
601                         result = zoom * dpi * value
602                                 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
603                         break;
604                 case LyXLength::CM:
605                         // Centimeter: 1 cm = 1/2.54 inch
606                         result = zoom * dpi * value
607                                 / 2.54; // 2.54
608                         break;
609                 case LyXLength::IN:
610                         // Inch
611                         result = zoom * dpi * value;
612                         break;
613                 case LyXLength::EX:
614                         // Ex: The height of an "x"
615                         result = zoom * value * height / 2; // what to / width?
616                         break;
617                 case LyXLength::EM: // what to / width?
618                         // Em: The width of an "m"
619                         result = zoom * value * height / 2; // Why 2?
620                         break;
621                 case LyXLength::MU: // This is probably only allowed in
622                         // math mode
623                         result = zoom * value * height;
624                         break;
625                 case LyXLength::UNIT_NONE:
626                         result = 0;  // this cannot happen
627                         break;  
628                 }
629                 return int (result * val_sign + 0.5);
630         }
631         return 0; // never reached
632 }