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