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