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