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"
32 static const int num_units = LyXLength::UNIT_NONE;
34 // I am not sure if "mu" should be possible to select (Lgb)
35 static char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
36 "mm", "pc", "cc", "cm",
37 "in", "ex", "em", "mu" };
40 LyXLength::UNIT unitFromString (string const & data)
43 while ((i < num_units) && (data != unit_name[i])) ++i;
44 return static_cast<LyXLength::UNIT>(i);
47 /* The following static items form a simple scanner for
48 * length strings, used by isValid[Glue]Length. See comments there.
50 static float number[4] = { 0, 0, 0, 0 };
51 static LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
54 LyXLength::UNIT_NONE };
55 //static int number_index, unit_index;
56 int number_index, unit_index;
59 void lyx_advance (string & data, unsigned int n)
65 bool isEndOfData (string const & data)
67 return frontStrip (data).empty();
71 char nextToken (string & data)
73 data = frontStrip(data);
76 else if (data[0] == '+') {
77 lyx_advance (data, 1);
80 else if (prefixIs(data, "plus")) {
81 lyx_advance (data, 4);
84 else if (data[0] == '-') {
85 lyx_advance (data, 1);
88 else if (prefixIs(data, "minus")) {
89 lyx_advance (data, 5);
95 // I really mean assignment ("=") below, not equality!
96 if ((i = data.find_last_of("0123456789.")) != string::npos) {
97 if (number_index > 3) return 'E'; // Error
98 string buffer = data.substr(0, i + 1);
99 if (sscanf (buffer.c_str(),
100 "%f", &number[number_index]) == 1) {
101 lyx_advance (data, i + 1);
106 } else if ((i = data.find_last_of("abcdefghijklmnopqrstuvwxyz"))
108 if (unit_index > 3) return 'E'; // Error
109 string buffer = data.substr(0, i + 1);
110 unit[unit_index] = unitFromString (buffer);
111 if (unit[unit_index] != LyXLength::UNIT_NONE) {
112 lyx_advance (data, i + 1);
123 char const * pattern;
124 int plus_val_index, minus_val_index,
125 plus_uni_index, minus_uni_index;
126 } table[] = { { "nu", 0, 0, 0, 0 },
127 { "nu+nu", 2, 0, 2, 0 },
128 { "nu+nu-nu", 2, 3, 2, 3 },
129 { "nu+-nu", 2, 2, 2, 2 },
130 { "nu-nu", 0, 2, 0, 2 },
131 { "nu-nu+nu", 3, 2, 3, 2 },
132 { "nu-+nu", 2, 2, 2, 2 },
133 { "n+nu", 2, 0, 1, 0 },
134 { "n+n-nu", 2, 3, 1, 1 },
135 { "n+-nu", 2, 2, 1, 1 },
136 { "n-nu", 0, 2, 0, 1 },
137 { "n-n+nu", 3, 2, 1, 1 },
138 { "n-+nu", 2, 2, 1, 1 },
139 { "", 0, 0, 0, 0 } // sentinel, must be empty
142 bool isValidGlueLength (string const & data, LyXGlueLength * result)
144 // This parser is table-driven. First, it constructs a "pattern"
145 // that describes the sequence of tokens in "data". For example,
146 // "n-nu" means: number, minus sign, number, unit. As we go along,
147 // numbers and units are stored into static arrays. Then, "pattern"
148 // is searched in the "table". If it is found, the associated
149 // table entries tell us which number and unit should go where
150 // in the LyXLength structure. Example: if "data" has the "pattern"
151 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
152 // That means, "plus_val" is the second number that was seen
153 // in the input, "minus_val" is the third number, and "plus_uni"
154 // and "minus_uni" are the second and third units, respectively.
155 // ("val" and "uni" are always the first items seen in "data".)
156 // This is the most elegant solution I could find -- a straight-
157 // forward approach leads to very long, tedious code that would be
158 // much harder to understand and maintain. (AS)
162 string buffer = frontStrip(data);
164 // To make isValidGlueLength recognize negative values as
165 // the first number this little hack is needed:
166 short val_sign = 1; // positive as default
169 lyx_advance(buffer, 1);
173 lyx_advance(buffer, 1);
181 int pattern_index = 0, table_index = 0;
184 number_index = unit_index = 1; // entries at index 0 are sentinels
186 // construct "pattern" from "data"
187 while (!isEndOfData (buffer)) {
188 if (pattern_index > 20) return false;
189 pattern[pattern_index] = nextToken (buffer);
190 if (pattern[pattern_index] == 'E') return false;
193 pattern[pattern_index] = '\0';
195 // search "pattern" in "table"
197 while (compare(pattern, table[table_index].pattern)) {
199 if (!*table[table_index].pattern) return false;
202 // Get the values from the appropriate places. If an index
203 // is zero, the corresponding array value is zero or UNIT_NONE,
204 // so we needn't check this.
206 result->val = number[1] * val_sign;
207 result->uni = unit[1];
208 result->plus_val = number[table[table_index].plus_val_index];
209 result->minus_val = number[table[table_index].minus_val_index];
210 result->plus_uni = unit [table[table_index].plus_uni_index];
211 result->minus_uni = unit [table[table_index].minus_uni_index];
217 bool isValidLength(string const & data, LyXLength * result)
219 /// This is a trimmed down version of isValidGlueLength.
220 /// The parser may seem overkill for lengths without
221 /// glue, but since we already have it, using it is
222 /// easier than writing something from scratch.
224 string buffer = data;
225 int pattern_index = 0;
228 // To make isValidLength recognize negative values
229 // this little hack is needed:
230 short val_sign = 1; // positive as default
233 lyx_advance(buffer, 1);
237 lyx_advance(buffer, 1);
245 number_index = unit_index = 1; // entries at index 0 are sentinels
247 // construct "pattern" from "data"
248 while (!isEndOfData (buffer)) {
249 if (pattern_index > 2) return false;
250 pattern[pattern_index] = nextToken (buffer);
251 if (pattern[pattern_index] == 'E') return false;
254 pattern[pattern_index] = '\0';
256 // only the most basic pattern is accepted here
257 if (compare(pattern, "nu") != 0) return false;
259 // It _was_ a correct length string.
260 // Store away the values we found.
262 result->val = number[1] * val_sign;
263 result->uni = unit[1];
270 LyXLength::LyXLength(string const & data)
274 if (!isValidLength (data, &tmp))
275 return; // should raise an exception
282 bool LyXLength::operator== (LyXLength const & other) const
284 return (val == other.val &&
288 string LyXLength::asString() const
292 sprintf (buffer, "%g%s", val, unit_name[uni]);
293 return string (buffer);
297 /* LyXGlueLength class
300 LyXGlueLength::LyXGlueLength (string const & data)
302 LyXGlueLength tmp(0.0, PT);
304 if (!isValidGlueLength (data, &tmp))
305 return; // should raise an exception
309 plus_val = tmp.plus_val;
310 plus_uni = tmp.plus_uni;
311 minus_val = tmp.minus_val;
312 minus_uni = tmp.minus_uni;
317 bool LyXGlueLength::operator== (LyXGlueLength const & other) const
319 return (val == other.val &&
321 plus_val == other.plus_val &&
322 plus_uni == other.plus_uni &&
323 minus_val == other.minus_val &&
324 minus_uni == other.minus_uni);
328 string LyXGlueLength::asString() const
333 if (minus_val != 0.0)
334 if ((uni == plus_uni) && (uni == minus_uni))
335 if (plus_val == minus_val)
336 sprintf (buffer, "%g+-%g%s",
337 val, plus_val, unit_name[uni]);
339 sprintf (buffer, "%g+%g-%g%s",
340 val, plus_val, minus_val,
343 if ((plus_uni == minus_uni) && (plus_val == minus_val))
344 sprintf (buffer, "%g%s+-%g%s",
346 plus_val, unit_name[plus_uni]);
348 sprintf (buffer, "%g%s+%g%s-%g%s",
350 plus_val, unit_name[plus_uni],
351 minus_val, unit_name[minus_uni]);
354 sprintf (buffer, "%g+%g%s",
355 val, plus_val, unit_name[uni]);
357 sprintf (buffer, "%g%s+%g%s",
359 plus_val, unit_name[plus_uni]);
361 if (minus_val != 0.0)
362 if (uni == minus_uni)
363 sprintf (buffer, "%g-%g%s",
364 val, minus_val, unit_name[uni]);
366 sprintf (buffer, "%g%s-%g%s",
368 minus_val, unit_name[minus_uni]);
370 sprintf (buffer, "%g%s", val, unit_name[uni]);
371 return string (buffer);
375 string LyXGlueLength::asLatexString() const
380 if (minus_val != 0.0)
381 sprintf (buffer, "%g%s plus %g%s minus %g%s",
383 plus_val, unit_name[plus_uni],
384 minus_val, unit_name[minus_uni]);
386 sprintf (buffer, "%g%s plus %g%s",
388 plus_val, unit_name[plus_uni]);
390 if (minus_val != 0.0)
391 sprintf (buffer, "%g%s minus %g%s",
393 minus_val, unit_name[minus_uni]);
395 sprintf (buffer, "%g%s",
396 val, unit_name[uni]);
397 return string (buffer);
404 VSpace::VSpace (string const & data)
405 : kin (NONE), len (0.0, LyXLength::PT)
411 string input = strip(data);
413 int length = input.length();
415 if (length > 1 && input[length-1] == '*') {
417 input.erase(length - 1);
420 if (prefixIs (input, "defskip")) kin = DEFSKIP;
421 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
422 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
423 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
424 else if (prefixIs (input, "vfill")) kin = VFILL;
425 else if (isValidGlueLength (input, &len))
427 else if (sscanf (input.c_str(), "%f", &value) == 1) {
428 // This last one is for reading old .lyx files
429 // without units in added_space_top/bottom.
430 // Let unit default to centimeters here.
432 len = LyXGlueLength (value, LyXLength::CM);
437 bool VSpace::operator==(VSpace const & other) const
439 if (this->kin == other.kin)
440 if (this->kin == LENGTH)
441 if (this->len == other.len)
442 return this->kp == other.kp;
446 return this->kp == other.kp;
452 string VSpace::asLyXCommand() const
458 case DEFSKIP: result = "defskip"; break;
459 case SMALLSKIP: result = "smallskip"; break;
460 case MEDSKIP: result = "medskip"; break;
461 case BIGSKIP: result = "bigskip"; break;
462 case VFILL: result = "vfill"; break;
463 case LENGTH: result = len.asString(); break;
465 if (kp && (kin != NONE) && (kin != DEFSKIP))
466 return result += '*';
472 string VSpace::asLatexCommand(BufferParams const & params) const
475 case NONE: return string();
477 return params.getDefSkip().asLatexCommand(params);
478 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
480 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
482 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
484 case VFILL: return kp ? "\\vspace*{\\fill}"
486 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
487 : "\\vspace{" + len.asLatexString() + '}';
489 return string(); // should never be reached
493 int VSpace::inPixels(BufferView * bv) const
495 // Height of a normal line in pixels (zoom factor considered)
496 int height = bv->text->DefaultHeight(); // [pixels]
498 // Zoom factor specified by user in percent
499 float const zoom = lyxrc->zoom / 100.0; // [percent]
501 // DPI setting for monitor: pixels/inch
502 float const dpi = lyxrc->dpi; // screen resolution [pixels/inch]
504 // We want the result in pixels
511 return bv->buffer()->params.getDefSkip().inPixels(bv);
513 // This is how the skips are normally defined by
514 // LateX. But there should be some way to change
515 // this per document.
516 case SMALLSKIP: return height/4;
517 case MEDSKIP: return height/2;
518 case BIGSKIP: return height;
519 case VFILL: return 3*height;
520 // leave space for the vfill symbol
522 // Pixel values are scaled so that the ratio
523 // between lengths and font sizes on the screen
524 // is the same as on paper.
526 // we don't care about sign of value, we
527 // can't display negative anyway
530 short val_sign = value < 0.0 ? -1 : 1;
532 switch (len.unit()) {
534 // Scaled point: sp = 1/65536 pt
535 result = zoom * dpi * value
536 / (72.27 * 65536); // 4736286.7
539 // Point: 1 pt = 1/72.27 inch
540 result = zoom * dpi * value
544 // Big point: 1 bp = 1/72 inch
545 result = zoom * dpi * value
549 // Didot: 1157dd = 1238 pt?
550 result = zoom * dpi * value
551 / (72.27 / (0.376 * 2.845)); // 67.559735
554 // Millimeter: 1 mm = 1/25.4 inch
555 result = zoom * dpi * value
559 // Pica: 1 pc = 12 pt
560 result = zoom * dpi * value
561 / (72.27 / 12); // 6.0225
564 // Cicero: 1 cc = 12 dd
565 result = zoom * dpi * value
566 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
569 // Centimeter: 1 cm = 1/2.54 inch
570 result = zoom * dpi * value
575 result = zoom * dpi * value;
578 // Ex: The height of an "x"
579 result = zoom * value * height / 2; // what to / width?
581 case LyXLength::EM: // what to / width?
582 // Em: The width of an "m"
583 result = zoom * value * height / 2; // Why 2?
585 case LyXLength::MU: // This is probably only allowed in
587 result = zoom * value * height;
589 case LyXLength::UNIT_NONE:
590 result = 0; // this cannot happen
593 return int (result * val_sign + 0.5);
595 return 0; // never reached