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