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