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"
25 #include "BufferView.h"
26 #include "support/lstrings.h"
29 extern BufferView *current_view;
34 static const int num_units = LyXLength::UNIT_NONE;
36 // I am not sure if "mu" should be possible to select (Lgb)
37 static char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
38 "mm", "pc", "cc", "cm",
39 "in", "ex", "em", "mu" };
42 LyXLength::UNIT unitFromString (string const & data)
45 while ((i<num_units) && (data != unit_name[i])) ++i;
46 return (LyXLength::UNIT)i;
49 /* The following static items form a simple scanner for
50 * length strings, used by isValid[Glue]Length. See comments there.
52 static float number[4] = { 0, 0, 0, 0 };
53 static LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
56 LyXLength::UNIT_NONE };
57 //static int number_index, unit_index;
58 int number_index, unit_index;
60 static inline void lyx_advance (string & data, unsigned int n)
65 static inline bool isEndOfData (string const & data)
67 return frontStrip (data).empty();
70 static char nextToken (string & data)
72 data = frontStrip(data);
75 else if (data[0] == '+') {
76 lyx_advance (data, 1);
79 else if (prefixIs(data, "plus")) {
80 lyx_advance (data, 4);
83 else if (data[0] == '-') {
84 lyx_advance (data, 1);
87 else if (prefixIs(data, "minus")) {
88 lyx_advance (data, 5);
94 // I really mean assignment ("=") below, not equality!
95 if ((i = data.find_last_of("0123456789.")) != string::npos) {
96 if (number_index > 3) return 'E'; // Error
97 string buffer = data.substr(0, i + 1);
98 if (sscanf (buffer.c_str(),
99 "%f", &number[number_index]) == 1) {
100 lyx_advance (data, i + 1);
105 } else if ((i = data.find_last_of("abcdefghijklmnopqrstuvwxyz"))
107 if (unit_index > 3) return 'E'; // Error
108 string buffer = data.substr(0, i + 1);
109 unit[unit_index] = unitFromString (buffer);
110 if (unit[unit_index] != LyXLength::UNIT_NONE) {
111 lyx_advance (data, i + 1);
122 char const * pattern;
123 int plus_val_index, minus_val_index,
124 plus_uni_index, minus_uni_index;
125 } table[] = { { "nu", 0, 0, 0, 0 },
126 { "nu+nu", 2, 0, 2, 0 },
127 { "nu+nu-nu", 2, 3, 2, 3 },
128 { "nu+-nu", 2, 2, 2, 2 },
129 { "nu-nu", 0, 2, 0, 2 },
130 { "nu-nu+nu", 3, 2, 3, 2 },
131 { "nu-+nu", 2, 2, 2, 2 },
132 { "n+nu", 2, 0, 1, 0 },
133 { "n+n-nu", 2, 3, 1, 1 },
134 { "n+-nu", 2, 2, 1, 1 },
135 { "n-nu", 0, 2, 0, 1 },
136 { "n-n+nu", 3, 2, 1, 1 },
137 { "n-+nu", 2, 2, 1, 1 },
138 { "", 0, 0, 0, 0 } // sentinel, must be empty
141 bool isValidGlueLength (string const & data, LyXGlueLength * result)
143 // This parser is table-driven. First, it constructs a "pattern"
144 // that describes the sequence of tokens in "data". For example,
145 // "n-nu" means: number, minus sign, number, unit. As we go along,
146 // numbers and units are stored into static arrays. Then, "pattern"
147 // is searched in the "table". If it is found, the associated
148 // table entries tell us which number and unit should go where
149 // in the LyXLength structure. Example: if "data" has the "pattern"
150 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
151 // That means, "plus_val" is the second number that was seen
152 // in the input, "minus_val" is the third number, and "plus_uni"
153 // and "minus_uni" are the second and third units, respectively.
154 // ("val" and "uni" are always the first items seen in "data".)
155 // This is the most elegant solution I could find -- a straight-
156 // forward approach leads to very long, tedious code that would be
157 // much harder to understand and maintain. (AS)
161 string buffer = frontStrip(data);
163 // To make isValidGlueLength recognize negative values as
164 // the first number this little hack is needed:
165 short val_sign = 1; // positive as default
168 lyx_advance(buffer, 1);
172 lyx_advance(buffer, 1);
180 int pattern_index = 0, table_index = 0;
183 number_index = unit_index = 1; // entries at index 0 are sentinels
185 // construct "pattern" from "data"
186 while (!isEndOfData (buffer)) {
187 if (pattern_index > 20) return false;
188 pattern[pattern_index] = nextToken (buffer);
189 if (pattern[pattern_index] == 'E') return false;
192 pattern[pattern_index] = '\0';
194 // search "pattern" in "table"
196 while (compare(pattern, table[table_index].pattern)) {
198 if (!*table[table_index].pattern) return false;
201 // Get the values from the appropriate places. If an index
202 // is zero, the corresponding array value is zero or UNIT_NONE,
203 // so we needn't check this.
205 result->val = number[1] * val_sign;
206 result->uni = unit[1];
207 result->plus_val = number[table[table_index].plus_val_index];
208 result->minus_val = number[table[table_index].minus_val_index];
209 result->plus_uni = unit [table[table_index].plus_uni_index];
210 result->minus_uni = unit [table[table_index].minus_uni_index];
216 bool isValidLength(string const & data, LyXLength * result)
218 /// This is a trimmed down version of isValidGlueLength.
219 /// The parser may seem overkill for lengths without
220 /// glue, but since we already have it, using it is
221 /// easier than writing something from scratch.
223 string buffer = data;
224 int pattern_index = 0;
227 // To make isValidLength recognize negative values
228 // this little hack is needed:
229 short val_sign = 1; // positive as default
232 lyx_advance(buffer, 1);
236 lyx_advance(buffer, 1);
244 number_index = unit_index = 1; // entries at index 0 are sentinels
246 // construct "pattern" from "data"
247 while (!isEndOfData (buffer)) {
248 if (pattern_index > 2) return false;
249 pattern[pattern_index] = nextToken (buffer);
250 if (pattern[pattern_index] == 'E') return false;
253 pattern[pattern_index] = '\0';
255 // only the most basic pattern is accepted here
256 if (compare(pattern, "nu") != 0) return false;
258 // It _was_ a correct length string.
259 // Store away the values we found.
261 result->val = number[1]*val_sign;
262 result->uni = unit[1];
269 LyXLength::LyXLength(string const & data)
273 if (!isValidLength (data, &tmp))
274 return; // should raise an exception
281 bool LyXLength::operator== (LyXLength const & other) const
283 return (val == other.val &&
287 string LyXLength::asString() const
291 sprintf (buffer, "%g%s", val, unit_name[uni]);
292 return string (buffer);
296 /* LyXGlueLength class
299 LyXGlueLength::LyXGlueLength (string const & data)
301 LyXGlueLength tmp(0.0, PT);
303 if (!isValidGlueLength (data, &tmp))
304 return; // should raise an exception
308 plus_val = tmp.plus_val;
309 plus_uni = tmp.plus_uni;
310 minus_val = tmp.minus_val;
311 minus_uni = tmp.minus_uni;
316 bool LyXGlueLength::operator== (LyXGlueLength const & other) const
318 return (val == other.val &&
320 plus_val == other.plus_val &&
321 plus_uni == other.plus_uni &&
322 minus_val == other.minus_val &&
323 minus_uni == other.minus_uni);
327 string LyXGlueLength::asString() const
332 if (minus_val != 0.0)
333 if ((uni == plus_uni) && (uni == minus_uni))
334 if (plus_val == minus_val)
335 sprintf (buffer, "%g+-%g%s",
336 val, plus_val, unit_name[uni]);
338 sprintf (buffer, "%g+%g-%g%s",
339 val, plus_val, minus_val,
342 if ((plus_uni == minus_uni) && (plus_val == minus_val))
343 sprintf (buffer, "%g%s+-%g%s",
345 plus_val, unit_name[plus_uni]);
347 sprintf (buffer, "%g%s+%g%s-%g%s",
349 plus_val, unit_name[plus_uni],
350 minus_val, unit_name[minus_uni]);
353 sprintf (buffer, "%g+%g%s",
354 val, plus_val, unit_name[uni]);
356 sprintf (buffer, "%g%s+%g%s",
358 plus_val, unit_name[plus_uni]);
360 if (minus_val != 0.0)
361 if (uni == minus_uni)
362 sprintf (buffer, "%g-%g%s",
363 val, minus_val, unit_name[uni]);
365 sprintf (buffer, "%g%s-%g%s",
367 minus_val, unit_name[minus_uni]);
369 sprintf (buffer, "%g%s", val, unit_name[uni]);
370 return string (buffer);
374 string LyXGlueLength::asLatexString() const
379 if (minus_val != 0.0)
380 sprintf (buffer, "%g%s plus %g%s minus %g%s",
382 plus_val, unit_name[plus_uni],
383 minus_val, unit_name[minus_uni]);
385 sprintf (buffer, "%g%s plus %g%s",
387 plus_val, unit_name[plus_uni]);
389 if (minus_val != 0.0)
390 sprintf (buffer, "%g%s minus %g%s",
392 minus_val, unit_name[minus_uni]);
394 sprintf (buffer, "%g%s",
395 val, unit_name[uni]);
396 return string (buffer);
403 VSpace::VSpace (string const & data)
404 : kin (NONE), len (0.0, LyXLength::PT)
410 string input = strip(data);
412 int length = input.length();
414 if (length > 1 && input[length-1] == '*') {
416 input.erase(length - 1);
419 if (prefixIs (input, "defskip")) kin = DEFSKIP;
420 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
421 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
422 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
423 else if (prefixIs (input, "vfill")) kin = VFILL;
424 else if (isValidGlueLength (input, &len))
426 else if (sscanf (input.c_str(), "%f", &value) == 1) {
427 // This last one is for reading old .lyx files
428 // without units in added_space_top/bottom.
429 // Let unit default to centimeters here.
431 len = LyXGlueLength (value, LyXLength::CM);
436 bool VSpace::operator==(VSpace const & other) const
438 if (this->kin == other.kin)
439 if (this->kin == LENGTH)
440 if (this->len == other.len)
441 return this->kp == other.kp;
445 return this->kp == other.kp;
451 string VSpace::asLyXCommand() const
457 case DEFSKIP: result = "defskip"; break;
458 case SMALLSKIP: result = "smallskip"; break;
459 case MEDSKIP: result = "medskip"; break;
460 case BIGSKIP: result = "bigskip"; break;
461 case VFILL: result = "vfill"; break;
462 case LENGTH: result = len.asString(); break;
464 if (kp && (kin != NONE) && (kin != DEFSKIP))
465 return result += '*';
471 string VSpace::asLatexCommand() const
474 case NONE: return string();
476 return current_view->buffer()->params.getDefSkip().asLatexCommand();
477 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
479 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
481 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
483 case VFILL: return kp ? "\\vspace*{\\fill}"
485 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
486 : "\\vspace{" + len.asLatexString() + '}';
488 return string(); // should never be reached
492 int VSpace::inPixels() const
494 // Height of a normal line in pixels (zoom factor considered)
496 int height = current_view->text->DefaultHeight(); // [pixels]
498 int height = current_view->buffer()->text->DefaultHeight(); // [pixels]
501 // Zoom factor specified by user in percent
502 float const zoom = lyxrc->zoom / 100.0; // [percent]
504 // DPI setting for monitor: pixels/inch
505 float const dpi = lyxrc->dpi; // screen resolution [pixels/inch]
507 // We want the result in pixels
514 return current_view->buffer()->params.getDefSkip().inPixels();
516 // This is how the skips are normally defined by
517 // LateX. But there should be some way to change
518 // this per document.
519 case SMALLSKIP: return height/4;
520 case MEDSKIP: return height/2;
521 case BIGSKIP: return height;
522 case VFILL: return 3*height;
523 // leave space for the vfill symbol
525 // Pixel values are scaled so that the ratio
526 // between lengths and font sizes on the screen
527 // is the same as on paper.
529 // we don't care about sign of value, we
530 // can't display negative anyway
533 short val_sign = value < 0.0 ? -1 : 1;
535 switch (len.unit()) {
537 // Scaled point: sp = 1/65536 pt
538 result = zoom * dpi * value
539 / (72.27 * 65536); // 4736286.7
542 // Point: 1 pt = 1/72.27 inch
543 result = zoom * dpi * value
547 // Big point: 1 bp = 1/72 inch
548 result = zoom * dpi * value
552 // Didot: 1157dd = 1238 pt?
553 result = zoom * dpi * value
554 / (72.27/(0.376 * 2.845)); // 67.559735
557 // Millimeter: 1 mm = 1/25.4 inch
558 result = zoom * dpi * value
562 // Pica: 1 pc = 12 pt
563 result = zoom * dpi * value
564 / (72.27/12); // 6.0225
567 // Cicero: 1 cc = 12 dd
568 result = zoom * dpi * value
569 / (72.27/ (12*0.376*2.845)); // 5.6299779
572 // Centimeter: 1 cm = 1/2.54 inch
573 result = zoom * dpi * value
578 result = zoom * dpi * value;
581 // Ex: The height of an "x"
582 result = zoom * value * height / 2; // what to / width?
584 case LyXLength::EM: // what to / width?
585 // Em: The width of an "m"
586 result = zoom * value * height / 2; // Why 2?
588 case LyXLength::MU: // This is probably only allowed in
590 result = zoom * value * height;
592 case LyXLength::UNIT_NONE:
593 result = 0; // this cannot happen
596 return int (result * val_sign + 0.5);
598 return 0; // never reached