2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-1999 The LyX Team.
10 * ====================================================== */
15 #pragma implementation "vspace.h"
24 #include "BufferView.h"
25 #include "support/lstrings.h"
28 extern BufferView * current_view;
33 static const int num_units = LyXLength::UNIT_NONE;
35 // I am not sure if "mu" should be possible to select (Lgb)
36 static char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
37 "mm", "pc", "cc", "cm",
38 "in", "ex", "em", "mu" };
41 LyXLength::UNIT unitFromString (string const & data)
44 while ((i < num_units) && (data != unit_name[i])) ++i;
45 return static_cast<LyXLength::UNIT>(i);
48 /* The following static items form a simple scanner for
49 * length strings, used by isValid[Glue]Length. See comments there.
51 static float number[4] = { 0, 0, 0, 0 };
52 static LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
55 LyXLength::UNIT_NONE };
56 //static int number_index, unit_index;
57 int number_index, unit_index;
59 static inline void lyx_advance (string & data, unsigned int n)
64 static inline bool isEndOfData (string const & data)
66 return frontStrip (data).empty();
69 static char nextToken (string & data)
71 data = frontStrip(data);
74 else if (data[0] == '+') {
75 lyx_advance (data, 1);
78 else if (prefixIs(data, "plus")) {
79 lyx_advance (data, 4);
82 else if (data[0] == '-') {
83 lyx_advance (data, 1);
86 else if (prefixIs(data, "minus")) {
87 lyx_advance (data, 5);
93 // I really mean assignment ("=") below, not equality!
94 if ((i = data.find_last_of("0123456789.")) != string::npos) {
95 if (number_index > 3) return 'E'; // Error
96 string buffer = data.substr(0, i + 1);
97 if (sscanf (buffer.c_str(),
98 "%f", &number[number_index]) == 1) {
99 lyx_advance (data, i + 1);
104 } else if ((i = data.find_last_of("abcdefghijklmnopqrstuvwxyz"))
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);
121 char const * pattern;
122 int plus_val_index, minus_val_index,
123 plus_uni_index, minus_uni_index;
124 } table[] = { { "nu", 0, 0, 0, 0 },
125 { "nu+nu", 2, 0, 2, 0 },
126 { "nu+nu-nu", 2, 3, 2, 3 },
127 { "nu+-nu", 2, 2, 2, 2 },
128 { "nu-nu", 0, 2, 0, 2 },
129 { "nu-nu+nu", 3, 2, 3, 2 },
130 { "nu-+nu", 2, 2, 2, 2 },
131 { "n+nu", 2, 0, 1, 0 },
132 { "n+n-nu", 2, 3, 1, 1 },
133 { "n+-nu", 2, 2, 1, 1 },
134 { "n-nu", 0, 2, 0, 1 },
135 { "n-n+nu", 3, 2, 1, 1 },
136 { "n-+nu", 2, 2, 1, 1 },
137 { "", 0, 0, 0, 0 } // sentinel, must be empty
140 bool isValidGlueLength (string const & data, LyXGlueLength * result)
142 // This parser is table-driven. First, it constructs a "pattern"
143 // that describes the sequence of tokens in "data". For example,
144 // "n-nu" means: number, minus sign, number, unit. As we go along,
145 // numbers and units are stored into static arrays. Then, "pattern"
146 // is searched in the "table". If it is found, the associated
147 // table entries tell us which number and unit should go where
148 // in the LyXLength structure. Example: if "data" has the "pattern"
149 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
150 // That means, "plus_val" is the second number that was seen
151 // in the input, "minus_val" is the third number, and "plus_uni"
152 // and "minus_uni" are the second and third units, respectively.
153 // ("val" and "uni" are always the first items seen in "data".)
154 // This is the most elegant solution I could find -- a straight-
155 // forward approach leads to very long, tedious code that would be
156 // much harder to understand and maintain. (AS)
160 string buffer = frontStrip(data);
162 // To make isValidGlueLength recognize negative values as
163 // the first number this little hack is needed:
164 short val_sign = 1; // positive as default
167 lyx_advance(buffer, 1);
171 lyx_advance(buffer, 1);
179 int pattern_index = 0, table_index = 0;
182 number_index = unit_index = 1; // entries at index 0 are sentinels
184 // construct "pattern" from "data"
185 while (!isEndOfData (buffer)) {
186 if (pattern_index > 20) return false;
187 pattern[pattern_index] = nextToken (buffer);
188 if (pattern[pattern_index] == 'E') return false;
191 pattern[pattern_index] = '\0';
193 // search "pattern" in "table"
195 while (compare(pattern, table[table_index].pattern)) {
197 if (!*table[table_index].pattern) return false;
200 // Get the values from the appropriate places. If an index
201 // is zero, the corresponding array value is zero or UNIT_NONE,
202 // so we needn't check this.
204 result->val = number[1] * val_sign;
205 result->uni = unit[1];
206 result->plus_val = number[table[table_index].plus_val_index];
207 result->minus_val = number[table[table_index].minus_val_index];
208 result->plus_uni = unit [table[table_index].plus_uni_index];
209 result->minus_uni = unit [table[table_index].minus_uni_index];
215 bool isValidLength(string const & data, LyXLength * result)
217 /// This is a trimmed down version of isValidGlueLength.
218 /// The parser may seem overkill for lengths without
219 /// glue, but since we already have it, using it is
220 /// easier than writing something from scratch.
222 string buffer = data;
223 int pattern_index = 0;
226 // To make isValidLength recognize negative values
227 // this little hack is needed:
228 short val_sign = 1; // positive as default
231 lyx_advance(buffer, 1);
235 lyx_advance(buffer, 1);
243 number_index = unit_index = 1; // entries at index 0 are sentinels
245 // construct "pattern" from "data"
246 while (!isEndOfData (buffer)) {
247 if (pattern_index > 2) return false;
248 pattern[pattern_index] = nextToken (buffer);
249 if (pattern[pattern_index] == 'E') return false;
252 pattern[pattern_index] = '\0';
254 // only the most basic pattern is accepted here
255 if (compare(pattern, "nu") != 0) return false;
257 // It _was_ a correct length string.
258 // Store away the values we found.
260 result->val = number[1] * val_sign;
261 result->uni = unit[1];
268 LyXLength::LyXLength(string const & data)
272 if (!isValidLength (data, &tmp))
273 return; // should raise an exception
280 bool LyXLength::operator== (LyXLength const & other) const
282 return (val == other.val &&
286 string LyXLength::asString() const
290 sprintf (buffer, "%g%s", val, unit_name[uni]);
291 return string (buffer);
295 /* LyXGlueLength class
298 LyXGlueLength::LyXGlueLength (string const & data)
300 LyXGlueLength tmp(0.0, PT);
302 if (!isValidGlueLength (data, &tmp))
303 return; // should raise an exception
307 plus_val = tmp.plus_val;
308 plus_uni = tmp.plus_uni;
309 minus_val = tmp.minus_val;
310 minus_uni = tmp.minus_uni;
315 bool LyXGlueLength::operator== (LyXGlueLength const & other) const
317 return (val == other.val &&
319 plus_val == other.plus_val &&
320 plus_uni == other.plus_uni &&
321 minus_val == other.minus_val &&
322 minus_uni == other.minus_uni);
326 string LyXGlueLength::asString() const
331 if (minus_val != 0.0)
332 if ((uni == plus_uni) && (uni == minus_uni))
333 if (plus_val == minus_val)
334 sprintf (buffer, "%g+-%g%s",
335 val, plus_val, unit_name[uni]);
337 sprintf (buffer, "%g+%g-%g%s",
338 val, plus_val, minus_val,
341 if ((plus_uni == minus_uni) && (plus_val == minus_val))
342 sprintf (buffer, "%g%s+-%g%s",
344 plus_val, unit_name[plus_uni]);
346 sprintf (buffer, "%g%s+%g%s-%g%s",
348 plus_val, unit_name[plus_uni],
349 minus_val, unit_name[minus_uni]);
352 sprintf (buffer, "%g+%g%s",
353 val, plus_val, unit_name[uni]);
355 sprintf (buffer, "%g%s+%g%s",
357 plus_val, unit_name[plus_uni]);
359 if (minus_val != 0.0)
360 if (uni == minus_uni)
361 sprintf (buffer, "%g-%g%s",
362 val, minus_val, unit_name[uni]);
364 sprintf (buffer, "%g%s-%g%s",
366 minus_val, unit_name[minus_uni]);
368 sprintf (buffer, "%g%s", val, unit_name[uni]);
369 return string (buffer);
373 string LyXGlueLength::asLatexString() const
378 if (minus_val != 0.0)
379 sprintf (buffer, "%g%s plus %g%s minus %g%s",
381 plus_val, unit_name[plus_uni],
382 minus_val, unit_name[minus_uni]);
384 sprintf (buffer, "%g%s plus %g%s",
386 plus_val, unit_name[plus_uni]);
388 if (minus_val != 0.0)
389 sprintf (buffer, "%g%s minus %g%s",
391 minus_val, unit_name[minus_uni]);
393 sprintf (buffer, "%g%s",
394 val, unit_name[uni]);
395 return string (buffer);
402 VSpace::VSpace (string const & data)
403 : kin (NONE), len (0.0, LyXLength::PT)
409 string input = strip(data);
411 int length = input.length();
413 if (length > 1 && input[length-1] == '*') {
415 input.erase(length - 1);
418 if (prefixIs (input, "defskip")) kin = DEFSKIP;
419 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
420 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
421 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
422 else if (prefixIs (input, "vfill")) kin = VFILL;
423 else if (isValidGlueLength (input, &len))
425 else if (sscanf (input.c_str(), "%f", &value) == 1) {
426 // This last one is for reading old .lyx files
427 // without units in added_space_top/bottom.
428 // Let unit default to centimeters here.
430 len = LyXGlueLength (value, LyXLength::CM);
435 bool VSpace::operator==(VSpace const & other) const
437 if (this->kin == other.kin)
438 if (this->kin == LENGTH)
439 if (this->len == other.len)
440 return this->kp == other.kp;
444 return this->kp == other.kp;
450 string VSpace::asLyXCommand() const
456 case DEFSKIP: result = "defskip"; break;
457 case SMALLSKIP: result = "smallskip"; break;
458 case MEDSKIP: result = "medskip"; break;
459 case BIGSKIP: result = "bigskip"; break;
460 case VFILL: result = "vfill"; break;
461 case LENGTH: result = len.asString(); break;
463 if (kp && (kin != NONE) && (kin != DEFSKIP))
464 return result += '*';
470 string VSpace::asLatexCommand() const
473 case NONE: return string();
475 return current_view->buffer()->params.getDefSkip().asLatexCommand();
476 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
478 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
480 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
482 case VFILL: return kp ? "\\vspace*{\\fill}"
484 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
485 : "\\vspace{" + len.asLatexString() + '}';
487 return string(); // should never be reached
491 int VSpace::inPixels() const
493 // Height of a normal line in pixels (zoom factor considered)
494 int height = current_view->text->DefaultHeight(); // [pixels]
496 // Zoom factor specified by user in percent
497 float const zoom = lyxrc->zoom / 100.0; // [percent]
499 // DPI setting for monitor: pixels/inch
500 float const dpi = lyxrc->dpi; // screen resolution [pixels/inch]
502 // We want the result in pixels
509 return current_view->buffer()->params.getDefSkip().inPixels();
511 // This is how the skips are normally defined by
512 // LateX. But there should be some way to change
513 // this per document.
514 case SMALLSKIP: return height/4;
515 case MEDSKIP: return height/2;
516 case BIGSKIP: return height;
517 case VFILL: return 3*height;
518 // leave space for the vfill symbol
520 // Pixel values are scaled so that the ratio
521 // between lengths and font sizes on the screen
522 // is the same as on paper.
524 // we don't care about sign of value, we
525 // can't display negative anyway
528 short val_sign = value < 0.0 ? -1 : 1;
530 switch (len.unit()) {
532 // Scaled point: sp = 1/65536 pt
533 result = zoom * dpi * value
534 / (72.27 * 65536); // 4736286.7
537 // Point: 1 pt = 1/72.27 inch
538 result = zoom * dpi * value
542 // Big point: 1 bp = 1/72 inch
543 result = zoom * dpi * value
547 // Didot: 1157dd = 1238 pt?
548 result = zoom * dpi * value
549 / (72.27 / (0.376 * 2.845)); // 67.559735
552 // Millimeter: 1 mm = 1/25.4 inch
553 result = zoom * dpi * value
557 // Pica: 1 pc = 12 pt
558 result = zoom * dpi * value
559 / (72.27 / 12); // 6.0225
562 // Cicero: 1 cc = 12 dd
563 result = zoom * dpi * value
564 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
567 // Centimeter: 1 cm = 1/2.54 inch
568 result = zoom * dpi * value
573 result = zoom * dpi * value;
576 // Ex: The height of an "x"
577 result = zoom * value * height / 2; // what to / width?
579 case LyXLength::EM: // what to / width?
580 // Em: The width of an "m"
581 result = zoom * value * height / 2; // Why 2?
583 case LyXLength::MU: // This is probably only allowed in
585 result = zoom * value * height;
587 case LyXLength::UNIT_NONE:
588 result = 0; // this cannot happen
591 return int (result * val_sign + 0.5);
593 return 0; // never reached