2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
15 #pragma implementation "vspace.h"
23 #include "BufferView.h"
24 #include "support/lstrings.h"
30 static const int num_units = LyXLength::UNIT_NONE;
32 // I am not sure if "mu" should be possible to select (Lgb)
33 static char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
34 "mm", "pc", "cc", "cm",
35 "in", "ex", "em", "mu" };
38 LyXLength::UNIT unitFromString (string const & data)
41 while ((i < num_units) && (data != unit_name[i])) ++i;
42 return static_cast<LyXLength::UNIT>(i);
45 /* The following static items form a simple scanner for
46 * length strings, used by isValid[Glue]Length. See comments there.
48 static float number[4] = { 0, 0, 0, 0 };
49 static LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
52 LyXLength::UNIT_NONE };
53 //static int number_index, unit_index;
54 int number_index, unit_index;
58 void lyx_advance(string & data, unsigned int n)
65 bool isEndOfData(string const & data)
67 return frontStrip(data).empty();
72 char nextToken(string & data)
74 data = frontStrip(data);
77 else if (data[0] == '+') {
80 } else if (prefixIs(data, "plus")) {
83 } else if (data[0] == '-') {
86 } else if (prefixIs(data, "minus")) {
92 // I really mean assignment ("=") below, not equality!
93 if ((i = data.find_last_of("0123456789.")) != string::npos) {
94 if (number_index > 3) return 'E'; // Error
95 string buffer = data.substr(0, i + 1);
96 double x = strToDbl(buffer);
97 if (x || (buffer[0] == '0')) {
98 number[number_index] = x;
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);
122 char const * pattern;
123 int plus_val_index, minus_val_index,
124 plus_uni_index, minus_uni_index;
129 LaTeXLength table[] = {
130 { "nu", 0, 0, 0, 0 },
131 { "nu+nu", 2, 0, 2, 0 },
132 { "nu+nu-nu", 2, 3, 2, 3 },
133 { "nu+-nu", 2, 2, 2, 2 },
134 { "nu-nu", 0, 2, 0, 2 },
135 { "nu-nu+nu", 3, 2, 3, 2 },
136 { "nu-+nu", 2, 2, 2, 2 },
137 { "n+nu", 2, 0, 1, 0 },
138 { "n+n-nu", 2, 3, 1, 1 },
139 { "n+-nu", 2, 2, 1, 1 },
140 { "n-nu", 0, 2, 0, 1 },
141 { "n-n+nu", 3, 2, 1, 1 },
142 { "n-+nu", 2, 2, 1, 1 },
143 { "", 0, 0, 0, 0 } // sentinel, must be empty
147 bool isValidGlueLength (string const & data, LyXGlueLength * result)
149 // This parser is table-driven. First, it constructs a "pattern"
150 // that describes the sequence of tokens in "data". For example,
151 // "n-nu" means: number, minus sign, number, unit. As we go along,
152 // numbers and units are stored into static arrays. Then, "pattern"
153 // is searched in the "table". If it is found, the associated
154 // table entries tell us which number and unit should go where
155 // in the LyXLength structure. Example: if "data" has the "pattern"
156 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
157 // That means, "plus_val" is the second number that was seen
158 // in the input, "minus_val" is the third number, and "plus_uni"
159 // and "minus_uni" are the second and third units, respectively.
160 // ("val" and "uni" are always the first items seen in "data".)
161 // This is the most elegant solution I could find -- a straight-
162 // forward approach leads to very long, tedious code that would be
163 // much harder to understand and maintain. (AS)
167 string buffer = frontStrip(data);
169 // To make isValidGlueLength recognize negative values as
170 // the first number this little hack is needed:
171 short val_sign = 1; // positive as default
174 lyx_advance(buffer, 1);
178 lyx_advance(buffer, 1);
186 int pattern_index = 0, table_index = 0;
189 number_index = unit_index = 1; // entries at index 0 are sentinels
191 // construct "pattern" from "data"
192 while (!isEndOfData (buffer)) {
193 if (pattern_index > 20) return false;
194 pattern[pattern_index] = nextToken (buffer);
195 if (pattern[pattern_index] == 'E') return false;
198 pattern[pattern_index] = '\0';
200 // search "pattern" in "table"
202 while (compare(pattern, table[table_index].pattern)) {
204 if (!*table[table_index].pattern) return false;
207 // Get the values from the appropriate places. If an index
208 // is zero, the corresponding array value is zero or UNIT_NONE,
209 // so we needn't check this.
211 result->val = number[1] * val_sign;
212 result->uni = unit[1];
213 result->plus_val = number[table[table_index].plus_val_index];
214 result->minus_val = number[table[table_index].minus_val_index];
215 result->plus_uni = unit [table[table_index].plus_uni_index];
216 result->minus_uni = unit [table[table_index].minus_uni_index];
222 bool isValidLength(string const & data, LyXLength * result)
224 /// This is a trimmed down version of isValidGlueLength.
225 /// The parser may seem overkill for lengths without
226 /// glue, but since we already have it, using it is
227 /// easier than writing something from scratch.
230 int pattern_index = 0;
233 // To make isValidLength recognize negative values
234 // this little hack is needed:
235 short val_sign = 1; // positive as default
238 lyx_advance(buffer, 1);
242 lyx_advance(buffer, 1);
250 number_index = unit_index = 1; // entries at index 0 are sentinels
252 // construct "pattern" from "data"
253 while (!isEndOfData (buffer)) {
254 if (pattern_index > 2) return false;
255 pattern[pattern_index] = nextToken (buffer);
256 if (pattern[pattern_index] == 'E') return false;
259 pattern[pattern_index] = '\0';
261 // only the most basic pattern is accepted here
262 if (compare(pattern, "nu") != 0) return false;
264 // It _was_ a correct length string.
265 // Store away the values we found.
267 result->val = number[1] * val_sign;
268 result->uni = unit[1];
276 LyXLength::LyXLength(string const & data)
280 if (!isValidLength (data, &tmp))
281 return; // should raise an exception
289 string const LyXLength::asString() const
291 std::ostringstream buffer;
292 buffer << val << unit_name[uni]; // setw?
293 return buffer.str().c_str();
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 string const LyXGlueLength::asString() const
319 std::ostringstream buffer;
322 if (minus_val != 0.0)
323 if ((uni == plus_uni) && (uni == minus_uni))
324 if (plus_val == minus_val)
325 buffer << val << "+-"
326 << plus_val << unit_name[uni];
333 if (plus_uni == minus_uni
334 && plus_val == minus_val)
335 buffer << val << unit_name[uni]
337 << unit_name[plus_uni];
340 buffer << val << unit_name[uni]
342 << unit_name[plus_uni]
344 << unit_name[minus_uni];
347 buffer << val << '+' << plus_val
350 buffer << val << unit_name[uni]
352 << unit_name[plus_uni];
355 if (minus_val != 0.0)
356 if (uni == minus_uni)
357 buffer << val << '-' << minus_val
361 buffer << val << unit_name[uni]
363 << unit_name[minus_uni];
365 buffer << val << unit_name[uni];
367 return buffer.str().c_str();
371 string const LyXGlueLength::asLatexString() const
373 std::ostringstream buffer;
376 if (minus_val != 0.0)
377 buffer << val << unit_name[uni]
379 << plus_val << unit_name[plus_uni]
381 << minus_val << unit_name[minus_uni];
383 buffer << val << unit_name[uni]
385 << plus_val << unit_name[plus_uni];
387 if (minus_val != 0.0)
388 buffer << val << unit_name[uni]
390 << minus_val << unit_name[minus_uni];
392 buffer << val << unit_name[uni];
394 return buffer.str().c_str();
401 VSpace::VSpace (string const & data)
402 : kin (NONE), len (0.0, LyXLength::PT)
408 string input = strip(data);
410 int length = input.length();
412 if (length > 1 && input[length-1] == '*') {
414 input.erase(length - 1);
417 if (prefixIs (input, "defskip")) kin = DEFSKIP;
418 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
419 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
420 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
421 else if (prefixIs (input, "vfill")) kin = VFILL;
422 else if (isValidGlueLength (input, &len))
424 else if (sscanf (input.c_str(), "%f", &value) == 1) {
425 // This last one is for reading old .lyx files
426 // without units in added_space_top/bottom.
427 // Let unit default to centimeters here.
429 len = LyXGlueLength (value, LyXLength::CM);
434 bool VSpace::operator==(VSpace const & other) const
436 if (this->kin == other.kin)
437 if (this->kin == LENGTH)
438 if (this->len == other.len)
439 return this->kp == other.kp;
443 return this->kp == other.kp;
449 string const VSpace::asLyXCommand() const
455 case DEFSKIP: result = "defskip"; break;
456 case SMALLSKIP: result = "smallskip"; break;
457 case MEDSKIP: result = "medskip"; break;
458 case BIGSKIP: result = "bigskip"; break;
459 case VFILL: result = "vfill"; break;
460 case LENGTH: result = len.asString(); break;
462 if (kp && (kin != NONE) && (kin != DEFSKIP))
463 return result += '*';
469 string const VSpace::asLatexCommand(BufferParams const & params) const
472 case NONE: return string();
474 return params.getDefSkip().asLatexCommand(params);
475 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
477 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
479 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
481 case VFILL: return kp ? "\\vspace*{\\fill}"
483 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
484 : "\\vspace{" + len.asLatexString() + '}';
486 return string(); // should never be reached
490 int VSpace::inPixels(BufferView * bv) const
492 // Height of a normal line in pixels (zoom factor considered)
493 int height = bv->text->DefaultHeight(); // [pixels]
496 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
498 return inPixels(height, skip);
501 int VSpace::inPixels(int default_height, int default_skip) const
503 // Height of a normal line in pixels (zoom factor considered)
504 int height = default_height; // [pixels]
506 // Zoom factor specified by user in percent
507 float const zoom = lyxrc.zoom / 100.0; // [percent]
509 // DPI setting for monitor: pixels/inch
510 float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
512 // We want the result in pixels
521 // This is how the skips are normally defined by
522 // LateX. But there should be some way to change
523 // this per document.
524 case SMALLSKIP: return height / 4;
525 case MEDSKIP: return height / 2;
526 case BIGSKIP: return height;
527 case VFILL: return 3 * height;
528 // leave space for the vfill symbol
530 // Pixel values are scaled so that the ratio
531 // between lengths and font sizes on the screen
532 // is the same as on paper.
534 // we don't care about sign of value, we
535 // can't display negative anyway
538 short val_sign = value < 0.0 ? -1 : 1;
540 switch (len.unit()) {
542 // Scaled point: sp = 1/65536 pt
543 result = zoom * dpi * value
544 / (72.27 * 65536); // 4736286.7
547 // Point: 1 pt = 1/72.27 inch
548 result = zoom * dpi * value
552 // Big point: 1 bp = 1/72 inch
553 result = zoom * dpi * value
557 // Didot: 1157dd = 1238 pt?
558 result = zoom * dpi * value
559 / (72.27 / (0.376 * 2.845)); // 67.559735
562 // Millimeter: 1 mm = 1/25.4 inch
563 result = zoom * dpi * value
567 // Pica: 1 pc = 12 pt
568 result = zoom * dpi * value
569 / (72.27 / 12); // 6.0225
572 // Cicero: 1 cc = 12 dd
573 result = zoom * dpi * value
574 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
577 // Centimeter: 1 cm = 1/2.54 inch
578 result = zoom * dpi * value
583 result = zoom * dpi * value;
586 // Ex: The height of an "x"
587 result = zoom * value * height / 2; // what to / width?
589 case LyXLength::EM: // what to / width?
590 // Em: The width of an "m"
591 result = zoom * value * height / 2; // Why 2?
593 case LyXLength::MU: // This is probably only allowed in
595 result = zoom * value * height;
597 case LyXLength::UNIT_NONE:
598 result = 0; // this cannot happen
601 return int (result * val_sign + 0.5);
603 return 0; // never reached