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" };
40 /* The following static items form a simple scanner for
41 * length strings, used by isValid[Glue]Length. See comments there.
43 float number[4] = { 0, 0, 0, 0 };
44 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
47 LyXLength::UNIT_NONE };
53 void lyx_advance(string & data, unsigned int n)
60 bool isEndOfData(string const & data)
62 return frontStrip(data).empty();
66 char nextToken(string & data)
68 data = frontStrip(data);
71 else if (data[0] == '+') {
74 } else if (prefixIs(data, "plus")) {
77 } else if (data[0] == '-') {
80 } else if (prefixIs(data, "minus")) {
84 string::size_type i = data.find_first_not_of("0123456789.");
87 if (number_index > 3) return 'E';
91 // we have found some number
92 if (i == string::npos) {
96 buffer = data.substr(0, i);
100 if (isStrDbl(buffer)) {
101 number[number_index] = strToDbl(buffer);
107 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz");
109 if (unit_index > 3) return 'E';
113 // we have found some alphabetical string
114 if (i == string::npos) {
118 buffer = data.substr(0, i);
120 // possibly we have "mmplus" string or similar
121 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
122 lyx_advance(data, 2);
123 unit[unit_index] = unitFromString(buffer.substr(0, 2));
125 lyx_advance(data, i);
126 unit[unit_index] = unitFromString(buffer);
129 if (unit[unit_index] != LyXLength::UNIT_NONE) {
132 } else return 'E'; // Error
140 char const * pattern;
141 int plus_val_index, minus_val_index,
142 plus_uni_index, minus_uni_index;
146 LaTeXLength table[] = {
147 { "nu", 0, 0, 0, 0 },
148 { "nu+nu", 2, 0, 2, 0 },
149 { "nu+nu-nu", 2, 3, 2, 3 },
150 { "nu+-nu", 2, 2, 2, 2 },
151 { "nu-nu", 0, 2, 0, 2 },
152 { "nu-nu+nu", 3, 2, 3, 2 },
153 { "nu-+nu", 2, 2, 2, 2 },
154 { "n+nu", 2, 0, 1, 0 },
155 { "n+n-nu", 2, 3, 1, 1 },
156 { "n+-nu", 2, 2, 1, 1 },
157 { "n-nu", 0, 2, 0, 1 },
158 { "n-n+nu", 3, 2, 1, 1 },
159 { "n-+nu", 2, 2, 1, 1 },
160 { "", 0, 0, 0, 0 } // sentinel, must be empty
166 LyXLength::UNIT unitFromString (string const & data)
169 while ((i < num_units) && (data != unit_name[i])) ++i;
170 return static_cast<LyXLength::UNIT>(i);
174 bool isValidGlueLength (string const & data, LyXGlueLength * result)
176 // This parser is table-driven. First, it constructs a "pattern"
177 // that describes the sequence of tokens in "data". For example,
178 // "n-nu" means: number, minus sign, number, unit. As we go along,
179 // numbers and units are stored into static arrays. Then, "pattern"
180 // is searched in the "table". If it is found, the associated
181 // table entries tell us which number and unit should go where
182 // in the LyXLength structure. Example: if "data" has the "pattern"
183 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
184 // That means, "plus_val" is the second number that was seen
185 // in the input, "minus_val" is the third number, and "plus_uni"
186 // and "minus_uni" are the second and third units, respectively.
187 // ("val" and "uni" are always the first items seen in "data".)
188 // This is the most elegant solution I could find -- a straight-
189 // forward approach leads to very long, tedious code that would be
190 // much harder to understand and maintain. (AS)
194 string buffer = frontStrip(data);
196 // To make isValidGlueLength recognize negative values as
197 // the first number this little hack is needed:
198 short val_sign = 1; // positive as default
201 lyx_advance(buffer, 1);
205 lyx_advance(buffer, 1);
213 int pattern_index = 0, table_index = 0;
216 number_index = unit_index = 1; // entries at index 0 are sentinels
218 // construct "pattern" from "data"
219 while (!isEndOfData (buffer)) {
220 if (pattern_index > 20) return false;
221 pattern[pattern_index] = nextToken (buffer);
222 if (pattern[pattern_index] == 'E') return false;
225 pattern[pattern_index] = '\0';
227 // search "pattern" in "table"
229 while (compare(pattern, table[table_index].pattern)) {
231 if (!*table[table_index].pattern) return false;
234 // Get the values from the appropriate places. If an index
235 // is zero, the corresponding array value is zero or UNIT_NONE,
236 // so we needn't check this.
238 result->val = number[1] * val_sign;
239 result->uni = unit[1];
240 result->plus_val = number[table[table_index].plus_val_index];
241 result->minus_val = number[table[table_index].minus_val_index];
242 result->plus_uni = unit [table[table_index].plus_uni_index];
243 result->minus_uni = unit [table[table_index].minus_uni_index];
249 bool isValidLength(string const & data, LyXLength * result)
251 /// This is a trimmed down version of isValidGlueLength.
252 /// The parser may seem overkill for lengths without
253 /// glue, but since we already have it, using it is
254 /// easier than writing something from scratch.
259 int pattern_index = 0;
262 // To make isValidLength recognize negative values
263 // this little hack is needed:
264 short val_sign = 1; // positive as default
267 lyx_advance(buffer, 1);
271 lyx_advance(buffer, 1);
279 number_index = unit_index = 1; // entries at index 0 are sentinels
281 // construct "pattern" from "data"
282 while (!isEndOfData (buffer)) {
283 if (pattern_index > 2) return false;
284 pattern[pattern_index] = nextToken (buffer);
285 if (pattern[pattern_index] == 'E') return false;
288 pattern[pattern_index] = '\0';
290 // only the most basic pattern is accepted here
291 if (compare(pattern, "nu") != 0) return false;
293 // It _was_ a correct length string.
294 // Store away the values we found.
296 result->val = number[1] * val_sign;
297 result->uni = unit[1];
305 LyXLength::LyXLength(string const & data)
309 if (!isValidLength (data, &tmp))
310 return; // should raise an exception
318 string const LyXLength::asString() const
320 std::ostringstream buffer;
321 buffer << val << unit_name[uni]; // setw?
322 return buffer.str().c_str();
326 /* LyXGlueLength class
329 LyXGlueLength::LyXGlueLength (string const & data)
331 LyXGlueLength tmp(0.0, PT);
333 if (!isValidGlueLength (data, &tmp))
334 return; // should raise an exception
338 plus_val = tmp.plus_val;
339 plus_uni = tmp.plus_uni;
340 minus_val = tmp.minus_val;
341 minus_uni = tmp.minus_uni;
346 string const LyXGlueLength::asString() const
348 std::ostringstream buffer;
351 if (minus_val != 0.0)
352 if ((uni == plus_uni) && (uni == minus_uni))
353 if (plus_val == minus_val)
354 buffer << val << "+-"
355 << plus_val << unit_name[uni];
362 if (plus_uni == minus_uni
363 && plus_val == minus_val)
364 buffer << val << unit_name[uni]
366 << unit_name[plus_uni];
369 buffer << val << unit_name[uni]
371 << unit_name[plus_uni]
373 << unit_name[minus_uni];
376 buffer << val << '+' << plus_val
379 buffer << val << unit_name[uni]
381 << unit_name[plus_uni];
384 if (minus_val != 0.0)
385 if (uni == minus_uni)
386 buffer << val << '-' << minus_val
390 buffer << val << unit_name[uni]
392 << unit_name[minus_uni];
394 buffer << val << unit_name[uni];
396 return buffer.str().c_str();
400 string const LyXGlueLength::asLatexString() const
402 std::ostringstream buffer;
405 if (minus_val != 0.0)
406 buffer << val << unit_name[uni]
408 << plus_val << unit_name[plus_uni]
410 << minus_val << unit_name[minus_uni];
412 buffer << val << unit_name[uni]
414 << plus_val << unit_name[plus_uni];
416 if (minus_val != 0.0)
417 buffer << val << unit_name[uni]
419 << minus_val << unit_name[minus_uni];
421 buffer << val << unit_name[uni];
423 return buffer.str().c_str();
430 VSpace::VSpace (string const & data)
431 : kin (NONE), len (0.0, LyXLength::PT)
437 string input = strip(data);
439 int length = input.length();
441 if (length > 1 && input[length-1] == '*') {
443 input.erase(length - 1);
446 if (prefixIs (input, "defskip")) kin = DEFSKIP;
447 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
448 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
449 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
450 else if (prefixIs (input, "vfill")) kin = VFILL;
451 else if (isValidGlueLength (input, &len))
453 else if (sscanf(input.c_str(), "%f", &value) == 1) {
454 // This last one is for reading old .lyx files
455 // without units in added_space_top/bottom.
456 // Let unit default to centimeters here.
458 len = LyXGlueLength (value, LyXLength::CM);
463 bool VSpace::operator==(VSpace const & other) const
465 if (this->kin != other.kin)
468 if (this->kin != LENGTH)
469 return this->kp == other.kp;
471 if (!(this->len == other.len))
474 return this->kp == other.kp;
478 string const VSpace::asLyXCommand() const
484 case DEFSKIP: result = "defskip"; break;
485 case SMALLSKIP: result = "smallskip"; break;
486 case MEDSKIP: result = "medskip"; break;
487 case BIGSKIP: result = "bigskip"; break;
488 case VFILL: result = "vfill"; break;
489 case LENGTH: result = len.asString(); break;
491 if (kp && (kin != NONE) && (kin != DEFSKIP))
492 return result += '*';
498 string const VSpace::asLatexCommand(BufferParams const & params) const
501 case NONE: return string();
503 return params.getDefSkip().asLatexCommand(params);
504 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
506 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
508 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
510 case VFILL: return kp ? "\\vspace*{\\fill}"
512 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
513 : "\\vspace{" + len.asLatexString() + '}';
515 return string(); // should never be reached
519 int VSpace::inPixels(BufferView * bv) const
521 // Height of a normal line in pixels (zoom factor considered)
522 int height = bv->text->DefaultHeight(); // [pixels]
525 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
527 return inPixels(height, skip);
530 int VSpace::inPixels(int default_height, int default_skip) const
532 // Height of a normal line in pixels (zoom factor considered)
533 int height = default_height; // [pixels]
535 // Zoom factor specified by user in percent
536 float const zoom = lyxrc.zoom / 100.0; // [percent]
538 // DPI setting for monitor: pixels/inch
539 float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
541 // We want the result in pixels
550 // This is how the skips are normally defined by
551 // LateX. But there should be some way to change
552 // this per document.
553 case SMALLSKIP: return height / 4;
554 case MEDSKIP: return height / 2;
555 case BIGSKIP: return height;
556 case VFILL: return 3 * height;
557 // leave space for the vfill symbol
559 // Pixel values are scaled so that the ratio
560 // between lengths and font sizes on the screen
561 // is the same as on paper.
563 // we don't care about sign of value, we
564 // can't display negative anyway
567 short val_sign = value < 0.0 ? -1 : 1;
569 switch (len.unit()) {
571 // Scaled point: sp = 1/65536 pt
572 result = zoom * dpi * value
573 / (72.27 * 65536); // 4736286.7
576 // Point: 1 pt = 1/72.27 inch
577 result = zoom * dpi * value
581 // Big point: 1 bp = 1/72 inch
582 result = zoom * dpi * value
586 // Didot: 1157dd = 1238 pt?
587 result = zoom * dpi * value
588 / (72.27 / (0.376 * 2.845)); // 67.559735
591 // Millimeter: 1 mm = 1/25.4 inch
592 result = zoom * dpi * value
596 // Pica: 1 pc = 12 pt
597 result = zoom * dpi * value
598 / (72.27 / 12); // 6.0225
601 // Cicero: 1 cc = 12 dd
602 result = zoom * dpi * value
603 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
606 // Centimeter: 1 cm = 1/2.54 inch
607 result = zoom * dpi * value
612 result = zoom * dpi * value;
615 // Ex: The height of an "x"
616 result = zoom * value * height / 2; // what to / width?
618 case LyXLength::EM: // what to / width?
619 // Em: The width of an "m"
620 result = zoom * value * height / 2; // Why 2?
622 case LyXLength::MU: // This is probably only allowed in
624 result = zoom * value * height;
626 case LyXLength::UNIT_NONE:
627 result = 0; // this cannot happen
630 return int (result * val_sign + 0.5);
632 return 0; // never reached