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"
32 int const num_units = LyXLength::UNIT_NONE;
34 // I am not sure if "mu" should be possible to select (Lgb)
35 char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
36 "mm", "pc", "cc", "cm",
37 "in", "ex", "em", "mu",
38 "%", "c%", "p%", "l%" };
41 /* The following static items form a simple scanner for
42 * length strings, used by isValid[Glue]Length. See comments there.
44 float number[4] = { 0, 0, 0, 0 };
45 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
48 LyXLength::UNIT_NONE };
54 void lyx_advance(string & data, unsigned int n)
61 bool isEndOfData(string const & data)
63 return frontStrip(data).empty();
67 char nextToken(string & data)
69 data = frontStrip(data);
72 else if (data[0] == '+') {
75 } else if (prefixIs(data, "plus")) {
78 } else if (data[0] == '-') {
81 } else if (prefixIs(data, "minus")) {
85 string::size_type i = data.find_first_not_of("0123456789.");
88 if (number_index > 3) return 'E';
92 // we have found some number
93 if (i == string::npos) {
97 buffer = data.substr(0, i);
101 if (isStrDbl(buffer)) {
102 number[number_index] = strToDbl(buffer);
108 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
110 if (unit_index > 3) return 'E';
114 // we have found some alphabetical string
115 if (i == string::npos) {
119 buffer = data.substr(0, i);
121 // possibly we have "mmplus" string or similar
122 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
123 lyx_advance(data, 2);
124 unit[unit_index] = unitFromString(buffer.substr(0, 2));
126 lyx_advance(data, i);
127 unit[unit_index] = unitFromString(buffer);
130 if (unit[unit_index] != LyXLength::UNIT_NONE) {
133 } else return 'E'; // Error
141 char const * pattern;
142 int plus_val_index, minus_val_index,
143 plus_uni_index, minus_uni_index;
147 LaTeXLength table[] = {
148 { "nu", 0, 0, 0, 0 },
149 { "nu+nu", 2, 0, 2, 0 },
150 { "nu+nu-nu", 2, 3, 2, 3 },
151 { "nu+-nu", 2, 2, 2, 2 },
152 { "nu-nu", 0, 2, 0, 2 },
153 { "nu-nu+nu", 3, 2, 3, 2 },
154 { "nu-+nu", 2, 2, 2, 2 },
155 { "n+nu", 2, 0, 1, 0 },
156 { "n+n-nu", 2, 3, 1, 1 },
157 { "n+-nu", 2, 2, 1, 1 },
158 { "n-nu", 0, 2, 0, 1 },
159 { "n-n+nu", 3, 2, 1, 1 },
160 { "n-+nu", 2, 2, 1, 1 },
161 { "", 0, 0, 0, 0 } // sentinel, must be empty
166 const char * stringFromUnit(int unit)
168 if (unit < 0 || unit >= num_units)
170 return unit_name[unit];
174 LyXLength::UNIT unitFromString (string const & data)
177 while ((i < num_units) && (data != unit_name[i])) ++i;
178 return static_cast<LyXLength::UNIT>(i);
182 bool isValidGlueLength (string const & data, LyXGlueLength * result)
184 // This parser is table-driven. First, it constructs a "pattern"
185 // that describes the sequence of tokens in "data". For example,
186 // "n-nu" means: number, minus sign, number, unit. As we go along,
187 // numbers and units are stored into static arrays. Then, "pattern"
188 // is searched in the "table". If it is found, the associated
189 // table entries tell us which number and unit should go where
190 // in the LyXLength structure. Example: if "data" has the "pattern"
191 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
192 // That means, "plus_val" is the second number that was seen
193 // in the input, "minus_val" is the third number, and "plus_uni"
194 // and "minus_uni" are the second and third units, respectively.
195 // ("val" and "uni" are always the first items seen in "data".)
196 // This is the most elegant solution I could find -- a straight-
197 // forward approach leads to very long, tedious code that would be
198 // much harder to understand and maintain. (AS)
202 string buffer = frontStrip(data);
204 // To make isValidGlueLength recognize negative values as
205 // the first number this little hack is needed:
206 short val_sign = 1; // positive as default
209 lyx_advance(buffer, 1);
213 lyx_advance(buffer, 1);
221 int pattern_index = 0, table_index = 0;
224 number_index = unit_index = 1; // entries at index 0 are sentinels
226 // construct "pattern" from "data"
227 while (!isEndOfData (buffer)) {
228 if (pattern_index > 20) return false;
229 pattern[pattern_index] = nextToken (buffer);
230 if (pattern[pattern_index] == 'E') return false;
233 pattern[pattern_index] = '\0';
235 // search "pattern" in "table"
237 while (compare(pattern, table[table_index].pattern)) {
239 if (!*table[table_index].pattern) return false;
242 // Get the values from the appropriate places. If an index
243 // is zero, the corresponding array value is zero or UNIT_NONE,
244 // so we needn't check this.
246 result->val = number[1] * val_sign;
247 result->uni = unit[1];
248 result->plus_val = number[table[table_index].plus_val_index];
249 result->minus_val = number[table[table_index].minus_val_index];
250 result->plus_uni = unit [table[table_index].plus_uni_index];
251 result->minus_uni = unit [table[table_index].minus_uni_index];
257 bool isValidLength(string const & data, LyXLength * result)
259 /// This is a trimmed down version of isValidGlueLength.
260 /// The parser may seem overkill for lengths without
261 /// glue, but since we already have it, using it is
262 /// easier than writing something from scratch.
267 int pattern_index = 0;
270 // To make isValidLength recognize negative values
271 // this little hack is needed:
272 short val_sign = 1; // positive as default
275 lyx_advance(buffer, 1);
279 lyx_advance(buffer, 1);
287 number_index = unit_index = 1; // entries at index 0 are sentinels
289 // construct "pattern" from "data"
290 while (!isEndOfData (buffer)) {
291 if (pattern_index > 2) return false;
292 pattern[pattern_index] = nextToken (buffer);
293 if (pattern[pattern_index] == 'E') return false;
296 pattern[pattern_index] = '\0';
298 // only the most basic pattern is accepted here
299 if (compare(pattern, "nu") != 0) return false;
301 // It _was_ a correct length string.
302 // Store away the values we found.
304 result->val = number[1] * val_sign;
305 result->uni = unit[1];
313 LyXLength::LyXLength(string const & data)
317 if (!isValidLength (data, &tmp))
318 return; // should raise an exception
326 string const LyXLength::asString() const
328 std::ostringstream buffer;
329 buffer << val << unit_name[uni]; // setw?
330 return buffer.str().c_str();
334 string const LyXLength::asLatexString() const
336 std::ostringstream buffer;
340 buffer << "." << abs(static_cast<int>(val)) << "\\columnwidth";
343 buffer << "." << abs(static_cast<int>(val)) << "\\pagewidth";
346 buffer << "." << abs(static_cast<int>(val)) << "\\linewidth";
349 buffer << val << unit_name[uni]; // setw?
352 return buffer.str().c_str();
356 /* LyXGlueLength class
359 LyXGlueLength::LyXGlueLength (string const & data)
361 LyXGlueLength tmp(0.0, PT);
363 if (!isValidGlueLength (data, &tmp))
364 return; // should raise an exception
368 plus_val = tmp.plus_val;
369 plus_uni = tmp.plus_uni;
370 minus_val = tmp.minus_val;
371 minus_uni = tmp.minus_uni;
376 string const LyXGlueLength::asString() const
378 std::ostringstream buffer;
381 if (minus_val != 0.0)
382 if ((uni == plus_uni) && (uni == minus_uni))
383 if (plus_val == minus_val)
384 buffer << val << "+-"
385 << plus_val << unit_name[uni];
392 if (plus_uni == minus_uni
393 && plus_val == minus_val)
394 buffer << val << unit_name[uni]
396 << unit_name[plus_uni];
399 buffer << val << unit_name[uni]
401 << unit_name[plus_uni]
403 << unit_name[minus_uni];
406 buffer << val << '+' << plus_val
409 buffer << val << unit_name[uni]
411 << unit_name[plus_uni];
414 if (minus_val != 0.0)
415 if (uni == minus_uni)
416 buffer << val << '-' << minus_val
420 buffer << val << unit_name[uni]
422 << unit_name[minus_uni];
424 buffer << val << unit_name[uni];
426 return buffer.str().c_str();
430 string const LyXGlueLength::asLatexString() const
432 std::ostringstream buffer;
435 if (minus_val != 0.0)
436 buffer << val << unit_name[uni]
438 << plus_val << unit_name[plus_uni]
440 << minus_val << unit_name[minus_uni];
442 buffer << val << unit_name[uni]
444 << plus_val << unit_name[plus_uni];
446 if (minus_val != 0.0)
447 buffer << val << unit_name[uni]
449 << minus_val << unit_name[minus_uni];
451 buffer << val << unit_name[uni];
453 return buffer.str().c_str();
460 VSpace::VSpace (string const & data)
461 : kin (NONE), len (0.0, LyXLength::PT)
467 string input = strip(data);
469 int length = input.length();
471 if (length > 1 && input[length-1] == '*') {
473 input.erase(length - 1);
476 if (prefixIs (input, "defskip")) kin = DEFSKIP;
477 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
478 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
479 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
480 else if (prefixIs (input, "vfill")) kin = VFILL;
481 else if (isValidGlueLength (input, &len))
483 else if (sscanf(input.c_str(), "%f", &value) == 1) {
484 // This last one is for reading old .lyx files
485 // without units in added_space_top/bottom.
486 // Let unit default to centimeters here.
488 len = LyXGlueLength (value, LyXLength::CM);
493 bool VSpace::operator==(VSpace const & other) const
495 if (this->kin != other.kin)
498 if (this->kin != LENGTH)
499 return this->kp == other.kp;
501 if (!(this->len == other.len))
504 return this->kp == other.kp;
508 string const VSpace::asLyXCommand() const
514 case DEFSKIP: result = "defskip"; break;
515 case SMALLSKIP: result = "smallskip"; break;
516 case MEDSKIP: result = "medskip"; break;
517 case BIGSKIP: result = "bigskip"; break;
518 case VFILL: result = "vfill"; break;
519 case LENGTH: result = len.asString(); break;
521 if (kp && (kin != NONE) && (kin != DEFSKIP))
522 return result += '*';
528 string const VSpace::asLatexCommand(BufferParams const & params) const
531 case NONE: return string();
533 return params.getDefSkip().asLatexCommand(params);
534 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
536 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
538 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
540 case VFILL: return kp ? "\\vspace*{\\fill}"
542 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
543 : "\\vspace{" + len.asLatexString() + '}';
545 return string(); // should never be reached
549 int VSpace::inPixels(BufferView * bv) const
551 // Height of a normal line in pixels (zoom factor considered)
552 int height = bv->text->DefaultHeight(); // [pixels]
554 int width = bv->workWidth();
557 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
559 return inPixels(height, skip, width);
562 int VSpace::inPixels(int default_height, int default_skip, int default_width) const
564 // Height of a normal line in pixels (zoom factor considered)
565 int height = default_height; // [pixels]
567 // Zoom factor specified by user in percent
568 float const zoom = lyxrc.zoom / 100.0; // [percent]
570 // DPI setting for monitor: pixels/inch
571 float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
573 // We want the result in pixels
582 // This is how the skips are normally defined by
583 // LateX. But there should be some way to change
584 // this per document.
585 case SMALLSKIP: return height / 4;
586 case MEDSKIP: return height / 2;
587 case BIGSKIP: return height;
588 case VFILL: return 3 * height;
589 // leave space for the vfill symbol
591 // Pixel values are scaled so that the ratio
592 // between lengths and font sizes on the screen
593 // is the same as on paper.
595 // we don't care about sign of value, we
596 // can't display negative anyway
599 short val_sign = value < 0.0 ? -1 : 1;
601 switch (len.unit()) {
603 // Scaled point: sp = 1/65536 pt
604 result = zoom * dpi * value
605 / (72.27 * 65536); // 4736286.7
608 // Point: 1 pt = 1/72.27 inch
609 result = zoom * dpi * value
613 // Big point: 1 bp = 1/72 inch
614 result = zoom * dpi * value
618 // Didot: 1157dd = 1238 pt?
619 result = zoom * dpi * value
620 / (72.27 / (0.376 * 2.845)); // 67.559735
623 // Millimeter: 1 mm = 1/25.4 inch
624 result = zoom * dpi * value
628 // Pica: 1 pc = 12 pt
629 result = zoom * dpi * value
630 / (72.27 / 12); // 6.0225
633 // Cicero: 1 cc = 12 dd
634 result = zoom * dpi * value
635 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
638 // Centimeter: 1 cm = 1/2.54 inch
639 result = zoom * dpi * value
644 result = zoom * dpi * value;
647 // Ex: The height of an "x"
648 result = zoom * value * height / 2; // what to / width?
650 case LyXLength::EM: // what to / width?
651 // Em: The width of an "m"
652 result = zoom * value * height / 2; // Why 2?
654 case LyXLength::MU: // This is probably only allowed in
656 result = zoom * value * height;
658 case LyXLength::PW: // Always % of workarea
662 result = value * default_width / 100;
664 case LyXLength::UNIT_NONE:
665 result = 0; // this cannot happen
668 return int (result * val_sign + 0.5);
670 return 0; // never reached