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 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] == '+') {
79 else if (prefixIs(data, "plus")) {
83 else if (data[0] == '-') {
87 else if (prefixIs(data, "minus")) {
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
98 string buffer = data.substr(0, i);
99 if (sscanf (buffer.c_str(),
100 "%f", &number[number_index]) == 1) {
106 } else if ((i=data.find_last_of("abcdefghijklmnopqrstuvwxyz"))
108 if (unit_index > 3) return 'E'; // Error
110 string buffer = data.substr(0, i);
111 unit[unit_index] = unitFromString (buffer);
112 if (unit[unit_index] != LyXLength::UNIT_NONE) {
124 char const * pattern;
125 int plus_val_index, minus_val_index,
126 plus_uni_index, minus_uni_index;
127 } table[] = { { "nu", 0, 0, 0, 0 },
128 { "nu+nu", 2, 0, 2, 0 },
129 { "nu+nu-nu", 2, 3, 2, 3 },
130 { "nu+-nu", 2, 2, 2, 2 },
131 { "nu-nu", 0, 2, 0, 2 },
132 { "nu-nu+nu", 3, 2, 3, 2 },
133 { "nu-+nu", 2, 2, 2, 2 },
134 { "n+nu", 2, 0, 1, 0 },
135 { "n+n-nu", 2, 3, 1, 1 },
136 { "n+-nu", 2, 2, 1, 1 },
137 { "n-nu", 0, 2, 0, 1 },
138 { "n-n+nu", 3, 2, 1, 1 },
139 { "n-+nu", 2, 2, 1, 1 },
140 { "", 0, 0, 0, 0 } // sentinel, must be empty
143 bool isValidGlueLength (string const & data, LyXGlueLength * result)
145 // This parser is table-driven. First, it constructs a "pattern"
146 // that describes the sequence of tokens in "data". For example,
147 // "n-nu" means: number, minus sign, number, unit. As we go along,
148 // numbers and units are stored into static arrays. Then, "pattern"
149 // is searched in the "table". If it is found, the associated
150 // table entries tell us which number and unit should go where
151 // in the LyXLength structure. Example: if "data" has the "pattern"
152 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
153 // That means, "plus_val" is the second number that was seen
154 // in the input, "minus_val" is the third number, and "plus_uni"
155 // and "minus_uni" are the second and third units, respectively.
156 // ("val" and "uni" are always the first items seen in "data".)
157 // This is the most elegant solution I could find -- a straight-
158 // forward approach leads to very long, tedious code that would be
159 // much harder to understand and maintain. (AS)
163 string buffer = frontStrip(data);
165 // To make isValidGlueLength recognize negative values as
166 // the first number this little hack is needed:
167 short val_sign = 1; // positive as default
182 int pattern_index = 0, table_index = 0;
185 number_index = unit_index = 1; // entries at index 0 are sentinels
187 // construct "pattern" from "data"
188 while (!isEndOfData (buffer)) {
189 if (pattern_index > 20) return false;
190 pattern[pattern_index] = nextToken (buffer);
191 if (pattern[pattern_index] == 'E') return false;
194 pattern[pattern_index] = '\0';
196 // search "pattern" in "table"
198 while (compare(pattern, table[table_index].pattern)) {
200 if (!*table[table_index].pattern) return false;
203 // Get the values from the appropriate places. If an index
204 // is zero, the corresponding array value is zero or UNIT_NONE,
205 // so we needn't check this.
207 result->val = number[1] * val_sign;
208 result->uni = unit[1];
209 result->plus_val = number[table[table_index].plus_val_index];
210 result->minus_val = number[table[table_index].minus_val_index];
211 result->plus_uni = unit [table[table_index].plus_uni_index];
212 result->minus_uni = unit [table[table_index].minus_uni_index];
218 bool isValidLength(string const & data, LyXLength * result)
220 /// This is a trimmed down version of isValidGlueLength.
221 /// The parser may seem overkill for lengths without
222 /// glue, but since we already have it, using it is
223 /// easier than writing something from scratch.
225 string buffer = data;
226 int pattern_index = 0;
229 // To make isValidLength recognize negative values
230 // this little hack is needed:
231 short val_sign = 1; // positive as default
246 number_index = unit_index = 1; // entries at index 0 are sentinels
248 // construct "pattern" from "data"
249 while (!isEndOfData (buffer)) {
250 if (pattern_index > 2) return false;
251 pattern[pattern_index] = nextToken (buffer);
252 if (pattern[pattern_index] == 'E') return false;
255 pattern[pattern_index] = '\0';
257 // only the most basic pattern is accepted here
258 if (compare(pattern, "nu") != 0) return false;
260 // It _was_ a correct length string.
261 // Store away the values we found.
263 result->val = number[1]*val_sign;
264 result->uni = unit[1];
271 LyXLength::LyXLength(string const & data)
275 if (!isValidLength (data, &tmp))
276 return; // should raise an exception
283 bool LyXLength::operator== (LyXLength const & other) const
285 return (val == other.val &&
289 string LyXLength::asString() const
293 sprintf (buffer, "%g%s", val, unit_name[uni]);
294 return string (buffer);
298 /* LyXGlueLength class
301 LyXGlueLength::LyXGlueLength (string const & data)
303 LyXGlueLength tmp(0.0, PT);
305 if (!isValidGlueLength (data, &tmp))
306 return; // should raise an exception
310 plus_val =tmp.plus_val;
311 plus_uni =tmp.plus_uni;
312 minus_val=tmp.minus_val;
313 minus_uni=tmp.minus_uni;
318 bool LyXGlueLength::operator== (LyXGlueLength const & other) const
320 return (val == other.val &&
322 plus_val == other.plus_val &&
323 plus_uni == other.plus_uni &&
324 minus_val == other.minus_val &&
325 minus_uni == other.minus_uni);
329 string LyXGlueLength::asString() const
334 if (minus_val != 0.0)
335 if ((uni == plus_uni) && (uni == minus_uni))
336 if (plus_val == minus_val)
337 sprintf (buffer, "%g+-%g%s",
338 val, plus_val, unit_name[uni]);
340 sprintf (buffer, "%g+%g-%g%s",
341 val, plus_val, minus_val,
344 if ((plus_uni == minus_uni) && (plus_val == minus_val))
345 sprintf (buffer, "%g%s+-%g%s",
347 plus_val, unit_name[plus_uni]);
349 sprintf (buffer, "%g%s+%g%s-%g%s",
351 plus_val, unit_name[plus_uni],
352 minus_val, unit_name[minus_uni]);
355 sprintf (buffer, "%g+%g%s",
356 val, plus_val, unit_name[uni]);
358 sprintf (buffer, "%g%s+%g%s",
360 plus_val, unit_name[plus_uni]);
362 if (minus_val != 0.0)
363 if (uni == minus_uni)
364 sprintf (buffer, "%g-%g%s",
365 val, minus_val, unit_name[uni]);
367 sprintf (buffer, "%g%s-%g%s",
369 minus_val, unit_name[minus_uni]);
371 sprintf (buffer, "%g%s", val, unit_name[uni]);
372 return string (buffer);
376 string LyXGlueLength::asLatexString() const
381 if (minus_val != 0.0)
382 sprintf (buffer, "%g%s plus %g%s minus %g%s",
384 plus_val, unit_name[plus_uni],
385 minus_val, unit_name[minus_uni]);
387 sprintf (buffer, "%g%s plus %g%s",
389 plus_val, unit_name[plus_uni]);
391 if (minus_val != 0.0)
392 sprintf (buffer, "%g%s minus %g%s",
394 minus_val, unit_name[minus_uni]);
396 sprintf (buffer, "%g%s",
397 val, unit_name[uni]);
398 return string (buffer);
405 VSpace::VSpace (string const & data)
406 : kin (NONE), len (0.0, LyXLength::PT)
412 string input = strip(data);
414 int length = input.length();
416 if (length > 1 && input[length-1] == '*') {
418 input.erase(length - 1);
421 if (prefixIs (input, "defskip")) kin = DEFSKIP;
422 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
423 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
424 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
425 else if (prefixIs (input, "vfill")) kin = VFILL;
426 else if (isValidGlueLength (input, &len))
428 else if (sscanf (input.c_str(), "%f", &value) == 1) {
429 // This last one is for reading old .lyx files
430 // without units in added_space_top/bottom.
431 // Let unit default to centimeters here.
433 len = LyXGlueLength (value, LyXLength::CM);
438 bool VSpace::operator== (VSpace const & other) const
440 if (this->kin == other.kin)
441 if (this->kin == LENGTH)
442 if (this->len == other.len)
443 return this->kp == other.kp;
447 return this->kp == other.kp;
453 string VSpace::asLyXCommand() const
459 case DEFSKIP: result = "defskip"; break;
460 case SMALLSKIP: result = "smallskip"; break;
461 case MEDSKIP: result = "medskip"; break;
462 case BIGSKIP: result = "bigskip"; break;
463 case VFILL: result = "vfill"; break;
464 case LENGTH: result = len.asString(); break;
466 if (kp && (kin != NONE) && (kin != DEFSKIP))
467 return result += '*';
473 string VSpace::asLatexCommand() const
476 case NONE: return string();
478 return current_view->currentBuffer()->params.getDefSkip().asLatexCommand();
479 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
481 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
483 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
485 case VFILL: return kp ? "\\vspace*{\\fill}"
487 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
488 : "\\vspace{" + len.asLatexString() + '}';
490 return string(); // should never be reached
494 int VSpace::inPixels() const
496 // Height of a normal line in pixels (zoom factor considered)
497 int height = current_view->currentBuffer()->text->DefaultHeight(); // [pixels]
499 // Zoom factor specified by user in percent
500 float const zoom = lyxrc->zoom / 100.0; // [percent]
502 // DPI setting for monitor: pixels/inch
503 float const dpi = lyxrc->dpi; // screen resolution [pixels/inch]
505 // We want the result in pixels
512 return current_view->currentBuffer()->params.getDefSkip().inPixels();
514 // This is how the skips are normally defined by
515 // LateX. But there should be some way to change
516 // this per document.
517 case SMALLSKIP: return height/4;
518 case MEDSKIP: return height/2;
519 case BIGSKIP: return height;
520 case VFILL: return 3*height;
521 // leave space for the vfill symbol
523 // Pixel values are scaled so that the ratio
524 // between lengths and font sizes on the screen
525 // is the same as on paper.
527 // we don't care about sign of value, we
528 // can't display negative anyway
531 short val_sign = value < 0.0 ? -1 : 1;
533 switch (len.unit()) {
535 // Scaled point: sp = 1/65536 pt
536 result = zoom * dpi * value
537 / (72.27 * 65536); // 4736286.7
540 // Point: 1 pt = 1/72.27 inch
541 result = zoom * dpi * value
545 // Big point: 1 bp = 1/72 inch
546 result = zoom * dpi * value
550 // Didot: 1157dd = 1238 pt?
551 result = zoom * dpi * value
552 / (72.27/(0.376 * 2.845)); // 67.559735
555 // Millimeter: 1 mm = 1/25.4 inch
556 result = zoom * dpi * value
560 // Pica: 1 pc = 12 pt
561 result = zoom * dpi * value
562 / (72.27/12); // 6.0225
565 // Cicero: 1 cc = 12 dd
566 result = zoom * dpi * value
567 / (72.27/ (12*0.376*2.845)); // 5.6299779
570 // Centimeter: 1 cm = 1/2.54 inch
571 result = zoom * dpi * value
576 result = zoom * dpi * value;
579 // Ex: The height of an "x"
580 result = zoom * value * height / 2; // what to / width?
582 case LyXLength::EM: // what to / width?
583 // Em: The width of an "m"
584 result = zoom * value * height / 2; // Why 2?
586 case LyXLength::MU: // This is probably only allowed in
588 result = zoom * value * height;
590 case LyXLength::UNIT_NONE:
591 result = 0; // this cannot happen
594 return int (result * val_sign + 0.5);
596 return 0; // never reached