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