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 if (isStrDbl(buffer)) {
97 number[number_index] = strToDbl(buffer);
98 lyx_advance(data, i + 1);
103 } else if ((i = data.find_last_of("abcdefghijklmnopqrstuvwxyz"))
105 if (unit_index > 3) return 'E'; // Error
106 string buffer = data.substr(0, i + 1);
107 unit[unit_index] = unitFromString(buffer);
108 if (unit[unit_index] != LyXLength::UNIT_NONE) {
109 lyx_advance(data, i + 1);
121 char const * pattern;
122 int plus_val_index, minus_val_index,
123 plus_uni_index, minus_uni_index;
128 LaTeXLength table[] = {
129 { "nu", 0, 0, 0, 0 },
130 { "nu+nu", 2, 0, 2, 0 },
131 { "nu+nu-nu", 2, 3, 2, 3 },
132 { "nu+-nu", 2, 2, 2, 2 },
133 { "nu-nu", 0, 2, 0, 2 },
134 { "nu-nu+nu", 3, 2, 3, 2 },
135 { "nu-+nu", 2, 2, 2, 2 },
136 { "n+nu", 2, 0, 1, 0 },
137 { "n+n-nu", 2, 3, 1, 1 },
138 { "n+-nu", 2, 2, 1, 1 },
139 { "n-nu", 0, 2, 0, 1 },
140 { "n-n+nu", 3, 2, 1, 1 },
141 { "n-+nu", 2, 2, 1, 1 },
142 { "", 0, 0, 0, 0 } // sentinel, must be empty
146 bool isValidGlueLength (string const & data, LyXGlueLength * result)
148 // This parser is table-driven. First, it constructs a "pattern"
149 // that describes the sequence of tokens in "data". For example,
150 // "n-nu" means: number, minus sign, number, unit. As we go along,
151 // numbers and units are stored into static arrays. Then, "pattern"
152 // is searched in the "table". If it is found, the associated
153 // table entries tell us which number and unit should go where
154 // in the LyXLength structure. Example: if "data" has the "pattern"
155 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
156 // That means, "plus_val" is the second number that was seen
157 // in the input, "minus_val" is the third number, and "plus_uni"
158 // and "minus_uni" are the second and third units, respectively.
159 // ("val" and "uni" are always the first items seen in "data".)
160 // This is the most elegant solution I could find -- a straight-
161 // forward approach leads to very long, tedious code that would be
162 // much harder to understand and maintain. (AS)
166 string buffer = frontStrip(data);
168 // To make isValidGlueLength recognize negative values as
169 // the first number this little hack is needed:
170 short val_sign = 1; // positive as default
173 lyx_advance(buffer, 1);
177 lyx_advance(buffer, 1);
185 int pattern_index = 0, table_index = 0;
188 number_index = unit_index = 1; // entries at index 0 are sentinels
190 // construct "pattern" from "data"
191 while (!isEndOfData (buffer)) {
192 if (pattern_index > 20) return false;
193 pattern[pattern_index] = nextToken (buffer);
194 if (pattern[pattern_index] == 'E') return false;
197 pattern[pattern_index] = '\0';
199 // search "pattern" in "table"
201 while (compare(pattern, table[table_index].pattern)) {
203 if (!*table[table_index].pattern) return false;
206 // Get the values from the appropriate places. If an index
207 // is zero, the corresponding array value is zero or UNIT_NONE,
208 // so we needn't check this.
210 result->val = number[1] * val_sign;
211 result->uni = unit[1];
212 result->plus_val = number[table[table_index].plus_val_index];
213 result->minus_val = number[table[table_index].minus_val_index];
214 result->plus_uni = unit [table[table_index].plus_uni_index];
215 result->minus_uni = unit [table[table_index].minus_uni_index];
221 bool isValidLength(string const & data, LyXLength * result)
223 /// This is a trimmed down version of isValidGlueLength.
224 /// The parser may seem overkill for lengths without
225 /// glue, but since we already have it, using it is
226 /// easier than writing something from scratch.
229 int pattern_index = 0;
232 // To make isValidLength recognize negative values
233 // this little hack is needed:
234 short val_sign = 1; // positive as default
237 lyx_advance(buffer, 1);
241 lyx_advance(buffer, 1);
249 number_index = unit_index = 1; // entries at index 0 are sentinels
251 // construct "pattern" from "data"
252 while (!isEndOfData (buffer)) {
253 if (pattern_index > 2) return false;
254 pattern[pattern_index] = nextToken (buffer);
255 if (pattern[pattern_index] == 'E') return false;
258 pattern[pattern_index] = '\0';
260 // only the most basic pattern is accepted here
261 if (compare(pattern, "nu") != 0) return false;
263 // It _was_ a correct length string.
264 // Store away the values we found.
266 result->val = number[1] * val_sign;
267 result->uni = unit[1];
275 LyXLength::LyXLength(string const & data)
279 if (!isValidLength (data, &tmp))
280 return; // should raise an exception
288 string const LyXLength::asString() const
290 std::ostringstream buffer;
291 buffer << val << unit_name[uni]; // setw?
292 return buffer.str().c_str();
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 string const LyXGlueLength::asString() const
318 std::ostringstream buffer;
321 if (minus_val != 0.0)
322 if ((uni == plus_uni) && (uni == minus_uni))
323 if (plus_val == minus_val)
324 buffer << val << "+-"
325 << plus_val << unit_name[uni];
332 if (plus_uni == minus_uni
333 && plus_val == minus_val)
334 buffer << val << unit_name[uni]
336 << unit_name[plus_uni];
339 buffer << val << unit_name[uni]
341 << unit_name[plus_uni]
343 << unit_name[minus_uni];
346 buffer << val << '+' << plus_val
349 buffer << val << unit_name[uni]
351 << unit_name[plus_uni];
354 if (minus_val != 0.0)
355 if (uni == minus_uni)
356 buffer << val << '-' << minus_val
360 buffer << val << unit_name[uni]
362 << unit_name[minus_uni];
364 buffer << val << unit_name[uni];
366 return buffer.str().c_str();
370 string const LyXGlueLength::asLatexString() const
372 std::ostringstream buffer;
375 if (minus_val != 0.0)
376 buffer << val << unit_name[uni]
378 << plus_val << unit_name[plus_uni]
380 << minus_val << unit_name[minus_uni];
382 buffer << val << unit_name[uni]
384 << plus_val << unit_name[plus_uni];
386 if (minus_val != 0.0)
387 buffer << val << unit_name[uni]
389 << minus_val << unit_name[minus_uni];
391 buffer << val << unit_name[uni];
393 return buffer.str().c_str();
400 VSpace::VSpace (string const & data)
401 : kin (NONE), len (0.0, LyXLength::PT)
407 string input = strip(data);
409 int length = input.length();
411 if (length > 1 && input[length-1] == '*') {
413 input.erase(length - 1);
416 if (prefixIs (input, "defskip")) kin = DEFSKIP;
417 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
418 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
419 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
420 else if (prefixIs (input, "vfill")) kin = VFILL;
421 else if (isValidGlueLength (input, &len))
423 else if (sscanf(input.c_str(), "%f", &value) == 1) {
424 // This last one is for reading old .lyx files
425 // without units in added_space_top/bottom.
426 // Let unit default to centimeters here.
428 len = LyXGlueLength (value, LyXLength::CM);
433 bool VSpace::operator==(VSpace const & other) const
435 if (this->kin == other.kin)
436 if (this->kin == LENGTH)
437 if (this->len == other.len)
438 return this->kp == other.kp;
442 return this->kp == other.kp;
448 string const VSpace::asLyXCommand() const
454 case DEFSKIP: result = "defskip"; break;
455 case SMALLSKIP: result = "smallskip"; break;
456 case MEDSKIP: result = "medskip"; break;
457 case BIGSKIP: result = "bigskip"; break;
458 case VFILL: result = "vfill"; break;
459 case LENGTH: result = len.asString(); break;
461 if (kp && (kin != NONE) && (kin != DEFSKIP))
462 return result += '*';
468 string const VSpace::asLatexCommand(BufferParams const & params) const
471 case NONE: return string();
473 return params.getDefSkip().asLatexCommand(params);
474 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
476 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
478 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
480 case VFILL: return kp ? "\\vspace*{\\fill}"
482 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
483 : "\\vspace{" + len.asLatexString() + '}';
485 return string(); // should never be reached
489 int VSpace::inPixels(BufferView * bv) const
491 // Height of a normal line in pixels (zoom factor considered)
492 int height = bv->text->DefaultHeight(); // [pixels]
495 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
497 return inPixels(height, skip);
500 int VSpace::inPixels(int default_height, int default_skip) const
502 // Height of a normal line in pixels (zoom factor considered)
503 int height = default_height; // [pixels]
505 // Zoom factor specified by user in percent
506 float const zoom = lyxrc.zoom / 100.0; // [percent]
508 // DPI setting for monitor: pixels/inch
509 float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
511 // We want the result in pixels
520 // This is how the skips are normally defined by
521 // LateX. But there should be some way to change
522 // this per document.
523 case SMALLSKIP: return height / 4;
524 case MEDSKIP: return height / 2;
525 case BIGSKIP: return height;
526 case VFILL: return 3 * height;
527 // leave space for the vfill symbol
529 // Pixel values are scaled so that the ratio
530 // between lengths and font sizes on the screen
531 // is the same as on paper.
533 // we don't care about sign of value, we
534 // can't display negative anyway
537 short val_sign = value < 0.0 ? -1 : 1;
539 switch (len.unit()) {
541 // Scaled point: sp = 1/65536 pt
542 result = zoom * dpi * value
543 / (72.27 * 65536); // 4736286.7
546 // Point: 1 pt = 1/72.27 inch
547 result = zoom * dpi * value
551 // Big point: 1 bp = 1/72 inch
552 result = zoom * dpi * value
556 // Didot: 1157dd = 1238 pt?
557 result = zoom * dpi * value
558 / (72.27 / (0.376 * 2.845)); // 67.559735
561 // Millimeter: 1 mm = 1/25.4 inch
562 result = zoom * dpi * value
566 // Pica: 1 pc = 12 pt
567 result = zoom * dpi * value
568 / (72.27 / 12); // 6.0225
571 // Cicero: 1 cc = 12 dd
572 result = zoom * dpi * value
573 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
576 // Centimeter: 1 cm = 1/2.54 inch
577 result = zoom * dpi * value
582 result = zoom * dpi * value;
585 // Ex: The height of an "x"
586 result = zoom * value * height / 2; // what to / width?
588 case LyXLength::EM: // what to / width?
589 // Em: The width of an "m"
590 result = zoom * value * height / 2; // Why 2?
592 case LyXLength::MU: // This is probably only allowed in
594 result = zoom * value * height;
596 case LyXLength::UNIT_NONE:
597 result = 0; // this cannot happen
600 return int (result * val_sign + 0.5);
602 return 0; // never reached