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"
25 #include "BufferView.h"
26 #include "support/lstrings.h"
34 int const num_units = LyXLength::UNIT_NONE;
36 // I am not sure if "mu" should be possible to select (Lgb)
37 char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
38 "mm", "pc", "cc", "cm",
39 "in", "ex", "em", "mu",
40 "%", "c%", "p%", "l%" };
43 /* The following static items form a simple scanner for
44 * length strings, used by isValid[Glue]Length. See comments there.
46 float number[4] = { 0, 0, 0, 0 };
47 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
50 LyXLength::UNIT_NONE };
56 void lyx_advance(string & data, unsigned int n)
63 bool isEndOfData(string const & data)
65 return frontStrip(data).empty();
69 char nextToken(string & data)
71 data = frontStrip(data);
74 else if (data[0] == '+') {
77 } else if (prefixIs(data, "plus")) {
80 } else if (data[0] == '-') {
83 } else if (prefixIs(data, "minus")) {
87 string::size_type i = data.find_first_not_of("0123456789.");
90 if (number_index > 3) return 'E';
94 // we have found some number
95 if (i == string::npos) {
99 buffer = data.substr(0, i);
101 lyx_advance(data, i);
103 if (isStrDbl(buffer)) {
104 number[number_index] = strToDbl(buffer);
110 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
112 if (unit_index > 3) return 'E';
116 // we have found some alphabetical string
117 if (i == string::npos) {
121 buffer = data.substr(0, i);
123 // possibly we have "mmplus" string or similar
124 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
125 lyx_advance(data, 2);
126 unit[unit_index] = unitFromString(buffer.substr(0, 2));
128 lyx_advance(data, i);
129 unit[unit_index] = unitFromString(buffer);
132 if (unit[unit_index] != LyXLength::UNIT_NONE) {
135 } else return 'E'; // Error
143 char const * pattern;
144 int plus_val_index, minus_val_index,
145 plus_uni_index, minus_uni_index;
149 LaTeXLength table[] = {
150 { "nu", 0, 0, 0, 0 },
151 { "nu+nu", 2, 0, 2, 0 },
152 { "nu+nu-nu", 2, 3, 2, 3 },
153 { "nu+-nu", 2, 2, 2, 2 },
154 { "nu-nu", 0, 2, 0, 2 },
155 { "nu-nu+nu", 3, 2, 3, 2 },
156 { "nu-+nu", 2, 2, 2, 2 },
157 { "n+nu", 2, 0, 1, 0 },
158 { "n+n-nu", 2, 3, 1, 1 },
159 { "n+-nu", 2, 2, 1, 1 },
160 { "n-nu", 0, 2, 0, 1 },
161 { "n-n+nu", 3, 2, 1, 1 },
162 { "n-+nu", 2, 2, 1, 1 },
163 { "", 0, 0, 0, 0 } // sentinel, must be empty
168 const char * stringFromUnit(int unit)
170 if (unit < 0 || unit >= num_units)
172 return unit_name[unit];
176 LyXLength::UNIT unitFromString (string const & data)
179 while ((i < num_units) && (data != unit_name[i])) ++i;
180 return static_cast<LyXLength::UNIT>(i);
184 bool isValidGlueLength (string const & data, LyXGlueLength * result)
186 // This parser is table-driven. First, it constructs a "pattern"
187 // that describes the sequence of tokens in "data". For example,
188 // "n-nu" means: number, minus sign, number, unit. As we go along,
189 // numbers and units are stored into static arrays. Then, "pattern"
190 // is searched in the "table". If it is found, the associated
191 // table entries tell us which number and unit should go where
192 // in the LyXLength structure. Example: if "data" has the "pattern"
193 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
194 // That means, "plus_val" is the second number that was seen
195 // in the input, "minus_val" is the third number, and "plus_uni"
196 // and "minus_uni" are the second and third units, respectively.
197 // ("val" and "uni" are always the first items seen in "data".)
198 // This is the most elegant solution I could find -- a straight-
199 // forward approach leads to very long, tedious code that would be
200 // much harder to understand and maintain. (AS)
204 string buffer = frontStrip(data);
206 // To make isValidGlueLength recognize negative values as
207 // the first number this little hack is needed:
208 short val_sign = 1; // positive as default
211 lyx_advance(buffer, 1);
215 lyx_advance(buffer, 1);
223 int pattern_index = 0, table_index = 0;
226 number_index = unit_index = 1; // entries at index 0 are sentinels
228 // construct "pattern" from "data"
229 while (!isEndOfData (buffer)) {
230 if (pattern_index > 20) return false;
231 pattern[pattern_index] = nextToken (buffer);
232 if (pattern[pattern_index] == 'E') return false;
235 pattern[pattern_index] = '\0';
237 // search "pattern" in "table"
239 while (compare(pattern, table[table_index].pattern)) {
241 if (!*table[table_index].pattern) return false;
244 // Get the values from the appropriate places. If an index
245 // is zero, the corresponding array value is zero or UNIT_NONE,
246 // so we needn't check this.
248 result->val = number[1] * val_sign;
249 result->uni = unit[1];
250 result->plus_val = number[table[table_index].plus_val_index];
251 result->minus_val = number[table[table_index].minus_val_index];
252 result->plus_uni = unit [table[table_index].plus_uni_index];
253 result->minus_uni = unit [table[table_index].minus_uni_index];
259 bool isValidLength(string const & data, LyXLength * result)
261 /// This is a trimmed down version of isValidGlueLength.
262 /// The parser may seem overkill for lengths without
263 /// glue, but since we already have it, using it is
264 /// easier than writing something from scratch.
269 int pattern_index = 0;
272 // To make isValidLength recognize negative values
273 // this little hack is needed:
274 short val_sign = 1; // positive as default
277 lyx_advance(buffer, 1);
281 lyx_advance(buffer, 1);
289 number_index = unit_index = 1; // entries at index 0 are sentinels
291 // construct "pattern" from "data"
292 while (!isEndOfData (buffer)) {
293 if (pattern_index > 2) return false;
294 pattern[pattern_index] = nextToken (buffer);
295 if (pattern[pattern_index] == 'E') return false;
298 pattern[pattern_index] = '\0';
300 // only the most basic pattern is accepted here
301 if (compare(pattern, "nu") != 0) return false;
303 // It _was_ a correct length string.
304 // Store away the values we found.
306 result->val = number[1] * val_sign;
307 result->uni = unit[1];
315 LyXLength::LyXLength(string const & data)
319 if (!isValidLength (data, &tmp))
320 return; // should raise an exception
328 string const LyXLength::asString() const
330 std::ostringstream buffer;
331 buffer << val << unit_name[uni]; // setw?
332 return buffer.str().c_str();
336 string const LyXLength::asLatexString() const
338 std::ostringstream buffer;
342 buffer << abs(static_cast<int>(val/100)) << "." << abs(static_cast<int>(val)%100) << "\\columnwidth";
345 buffer << "." << abs(static_cast<int>(val/100)) << "." << abs(static_cast<int>(val)%100) << "\\pagewidth";
348 buffer << "." << abs(static_cast<int>(val/100)) << "." << abs(static_cast<int>(val)%100) << "\\linewidth";
351 buffer << val << unit_name[uni]; // setw?
354 return buffer.str().c_str();
358 /* LyXGlueLength class
361 LyXGlueLength::LyXGlueLength (string const & data)
363 LyXGlueLength tmp(0.0, PT);
365 if (!isValidGlueLength (data, &tmp))
366 return; // should raise an exception
370 plus_val = tmp.plus_val;
371 plus_uni = tmp.plus_uni;
372 minus_val = tmp.minus_val;
373 minus_uni = tmp.minus_uni;
378 string const LyXGlueLength::asString() const
380 std::ostringstream buffer;
383 if (minus_val != 0.0)
384 if ((uni == plus_uni) && (uni == minus_uni))
385 if (plus_val == minus_val)
386 buffer << val << "+-"
387 << plus_val << unit_name[uni];
394 if (plus_uni == minus_uni
395 && plus_val == minus_val)
396 buffer << val << unit_name[uni]
398 << unit_name[plus_uni];
401 buffer << val << unit_name[uni]
403 << unit_name[plus_uni]
405 << unit_name[minus_uni];
408 buffer << val << '+' << plus_val
411 buffer << val << unit_name[uni]
413 << unit_name[plus_uni];
416 if (minus_val != 0.0)
417 if (uni == minus_uni)
418 buffer << val << '-' << minus_val
422 buffer << val << unit_name[uni]
424 << unit_name[minus_uni];
426 buffer << val << unit_name[uni];
428 return buffer.str().c_str();
432 string const LyXGlueLength::asLatexString() const
434 std::ostringstream buffer;
437 if (minus_val != 0.0)
438 buffer << val << unit_name[uni]
440 << plus_val << unit_name[plus_uni]
442 << minus_val << unit_name[minus_uni];
444 buffer << val << unit_name[uni]
446 << plus_val << unit_name[plus_uni];
448 if (minus_val != 0.0)
449 buffer << val << unit_name[uni]
451 << minus_val << unit_name[minus_uni];
453 buffer << val << unit_name[uni];
455 return buffer.str().c_str();
462 VSpace::VSpace (string const & data)
463 : kin (NONE), len (0.0, LyXLength::PT)
469 string input = strip(data);
471 int length = input.length();
473 if (length > 1 && input[length-1] == '*') {
475 input.erase(length - 1);
478 if (prefixIs (input, "defskip")) kin = DEFSKIP;
479 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
480 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
481 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
482 else if (prefixIs (input, "vfill")) kin = VFILL;
483 else if (isValidGlueLength (input, &len))
485 else if (sscanf(input.c_str(), "%f", &value) == 1) {
486 // This last one is for reading old .lyx files
487 // without units in added_space_top/bottom.
488 // Let unit default to centimeters here.
490 len = LyXGlueLength (value, LyXLength::CM);
495 bool VSpace::operator==(VSpace const & other) const
497 if (this->kin != other.kin)
500 if (this->kin != LENGTH)
501 return this->kp == other.kp;
503 if (!(this->len == other.len))
506 return this->kp == other.kp;
510 string const VSpace::asLyXCommand() const
516 case DEFSKIP: result = "defskip"; break;
517 case SMALLSKIP: result = "smallskip"; break;
518 case MEDSKIP: result = "medskip"; break;
519 case BIGSKIP: result = "bigskip"; break;
520 case VFILL: result = "vfill"; break;
521 case LENGTH: result = len.asString(); break;
523 if (kp && (kin != NONE) && (kin != DEFSKIP))
524 return result += '*';
530 string const VSpace::asLatexCommand(BufferParams const & params) const
533 case NONE: return string();
535 return params.getDefSkip().asLatexCommand(params);
536 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
538 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
540 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
542 case VFILL: return kp ? "\\vspace*{\\fill}"
544 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
545 : "\\vspace{" + len.asLatexString() + '}';
547 return string(); // should never be reached
551 int VSpace::inPixels(BufferView * bv) const
553 // Height of a normal line in pixels (zoom factor considered)
554 int height = bv->text->DefaultHeight(); // [pixels]
556 int width = bv->workWidth();
559 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
561 return inPixels(height, skip, width);
564 int VSpace::inPixels(int default_height, int default_skip, int default_width) const
566 // Height of a normal line in pixels (zoom factor considered)
567 int height = default_height; // [pixels]
569 // Zoom factor specified by user in percent
570 float const zoom = lyxrc.zoom / 100.0; // [percent]
572 // DPI setting for monitor: pixels/inch
573 float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
575 // We want the result in pixels
584 // This is how the skips are normally defined by
585 // LateX. But there should be some way to change
586 // this per document.
587 case SMALLSKIP: return height / 4;
588 case MEDSKIP: return height / 2;
589 case BIGSKIP: return height;
590 case VFILL: return 3 * height;
591 // leave space for the vfill symbol
593 // Pixel values are scaled so that the ratio
594 // between lengths and font sizes on the screen
595 // is the same as on paper.
597 // we don't care about sign of value, we
598 // can't display negative anyway
601 short val_sign = value < 0.0 ? -1 : 1;
603 switch (len.unit()) {
605 // Scaled point: sp = 1/65536 pt
606 result = zoom * dpi * value
607 / (72.27 * 65536); // 4736286.7
610 // Point: 1 pt = 1/72.27 inch
611 result = zoom * dpi * value
615 // Big point: 1 bp = 1/72 inch
616 result = zoom * dpi * value
620 // Didot: 1157dd = 1238 pt?
621 result = zoom * dpi * value
622 / (72.27 / (0.376 * 2.845)); // 67.559735
625 // Millimeter: 1 mm = 1/25.4 inch
626 result = zoom * dpi * value
630 // Pica: 1 pc = 12 pt
631 result = zoom * dpi * value
632 / (72.27 / 12); // 6.0225
635 // Cicero: 1 cc = 12 dd
636 result = zoom * dpi * value
637 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
640 // Centimeter: 1 cm = 1/2.54 inch
641 result = zoom * dpi * value
646 result = zoom * dpi * value;
649 // Ex: The height of an "x"
650 result = zoom * value * height / 2; // what to / width?
652 case LyXLength::EM: // what to / width?
653 // Em: The width of an "m"
654 result = zoom * value * height / 2; // Why 2?
656 case LyXLength::MU: // This is probably only allowed in
658 result = zoom * value * height;
660 case LyXLength::PW: // Always % of workarea
664 result = value * default_width / 100;
666 case LyXLength::UNIT_NONE:
667 result = 0; // this cannot happen
670 return int (result * val_sign + 0.5);
672 return 0; // never reached