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;
57 void lyx_advance(string & data, unsigned int n)
64 bool isEndOfData(string const & data)
66 return frontStrip(data).empty();
71 char nextToken(string & data)
73 data = frontStrip(data);
76 else if (data[0] == '+') {
79 } else if (prefixIs(data, "plus")) {
82 } else if (data[0] == '-') {
85 } else if (prefixIs(data, "minus")) {
89 string::size_type i = data.find_first_not_of("0123456789.");
92 if (number_index > 3) return 'E';
96 // we have found some number
97 if (i == string::npos) {
101 buffer = data.substr(0, i);
103 lyx_advance(data, i);
105 if (isStrDbl(buffer)) {
106 number[number_index] = strToDbl(buffer);
112 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz");
114 if (unit_index > 3) return 'E';
118 // we have found some alphabetical string
119 if (i == string::npos) {
123 buffer = data.substr(0, i);
125 // possibly we have "mmplus" string or similar
126 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
127 lyx_advance(data, 2);
128 unit[unit_index] = unitFromString(buffer.substr(0, 2));
130 lyx_advance(data, i);
131 unit[unit_index] = unitFromString(buffer);
134 if (unit[unit_index] != LyXLength::UNIT_NONE) {
137 } else return 'E'; // Error
145 char const * pattern;
146 int plus_val_index, minus_val_index,
147 plus_uni_index, minus_uni_index;
152 LaTeXLength table[] = {
153 { "nu", 0, 0, 0, 0 },
154 { "nu+nu", 2, 0, 2, 0 },
155 { "nu+nu-nu", 2, 3, 2, 3 },
156 { "nu+-nu", 2, 2, 2, 2 },
157 { "nu-nu", 0, 2, 0, 2 },
158 { "nu-nu+nu", 3, 2, 3, 2 },
159 { "nu-+nu", 2, 2, 2, 2 },
160 { "n+nu", 2, 0, 1, 0 },
161 { "n+n-nu", 2, 3, 1, 1 },
162 { "n+-nu", 2, 2, 1, 1 },
163 { "n-nu", 0, 2, 0, 1 },
164 { "n-n+nu", 3, 2, 1, 1 },
165 { "n-+nu", 2, 2, 1, 1 },
166 { "", 0, 0, 0, 0 } // sentinel, must be empty
170 bool isValidGlueLength (string const & data, LyXGlueLength * result)
172 // This parser is table-driven. First, it constructs a "pattern"
173 // that describes the sequence of tokens in "data". For example,
174 // "n-nu" means: number, minus sign, number, unit. As we go along,
175 // numbers and units are stored into static arrays. Then, "pattern"
176 // is searched in the "table". If it is found, the associated
177 // table entries tell us which number and unit should go where
178 // in the LyXLength structure. Example: if "data" has the "pattern"
179 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
180 // That means, "plus_val" is the second number that was seen
181 // in the input, "minus_val" is the third number, and "plus_uni"
182 // and "minus_uni" are the second and third units, respectively.
183 // ("val" and "uni" are always the first items seen in "data".)
184 // This is the most elegant solution I could find -- a straight-
185 // forward approach leads to very long, tedious code that would be
186 // much harder to understand and maintain. (AS)
190 string buffer = frontStrip(data);
192 // To make isValidGlueLength recognize negative values as
193 // the first number this little hack is needed:
194 short val_sign = 1; // positive as default
197 lyx_advance(buffer, 1);
201 lyx_advance(buffer, 1);
209 int pattern_index = 0, table_index = 0;
212 number_index = unit_index = 1; // entries at index 0 are sentinels
214 // construct "pattern" from "data"
215 while (!isEndOfData (buffer)) {
216 if (pattern_index > 20) return false;
217 pattern[pattern_index] = nextToken (buffer);
218 if (pattern[pattern_index] == 'E') return false;
221 pattern[pattern_index] = '\0';
223 // search "pattern" in "table"
225 while (compare(pattern, table[table_index].pattern)) {
227 if (!*table[table_index].pattern) return false;
230 // Get the values from the appropriate places. If an index
231 // is zero, the corresponding array value is zero or UNIT_NONE,
232 // so we needn't check this.
234 result->val = number[1] * val_sign;
235 result->uni = unit[1];
236 result->plus_val = number[table[table_index].plus_val_index];
237 result->minus_val = number[table[table_index].minus_val_index];
238 result->plus_uni = unit [table[table_index].plus_uni_index];
239 result->minus_uni = unit [table[table_index].minus_uni_index];
245 bool isValidLength(string const & data, LyXLength * result)
247 /// This is a trimmed down version of isValidGlueLength.
248 /// The parser may seem overkill for lengths without
249 /// glue, but since we already have it, using it is
250 /// easier than writing something from scratch.
255 int pattern_index = 0;
258 // To make isValidLength recognize negative values
259 // this little hack is needed:
260 short val_sign = 1; // positive as default
263 lyx_advance(buffer, 1);
267 lyx_advance(buffer, 1);
275 number_index = unit_index = 1; // entries at index 0 are sentinels
277 // construct "pattern" from "data"
278 while (!isEndOfData (buffer)) {
279 if (pattern_index > 2) return false;
280 pattern[pattern_index] = nextToken (buffer);
281 if (pattern[pattern_index] == 'E') return false;
284 pattern[pattern_index] = '\0';
286 // only the most basic pattern is accepted here
287 if (compare(pattern, "nu") != 0) return false;
289 // It _was_ a correct length string.
290 // Store away the values we found.
292 result->val = number[1] * val_sign;
293 result->uni = unit[1];
301 LyXLength::LyXLength(string const & data)
305 if (!isValidLength (data, &tmp))
306 return; // should raise an exception
314 string const LyXLength::asString() const
316 std::ostringstream buffer;
317 buffer << val << unit_name[uni]; // setw?
318 return buffer.str().c_str();
322 /* LyXGlueLength class
325 LyXGlueLength::LyXGlueLength (string const & data)
327 LyXGlueLength tmp(0.0, PT);
329 if (!isValidGlueLength (data, &tmp))
330 return; // should raise an exception
334 plus_val = tmp.plus_val;
335 plus_uni = tmp.plus_uni;
336 minus_val = tmp.minus_val;
337 minus_uni = tmp.minus_uni;
342 string const LyXGlueLength::asString() const
344 std::ostringstream buffer;
347 if (minus_val != 0.0)
348 if ((uni == plus_uni) && (uni == minus_uni))
349 if (plus_val == minus_val)
350 buffer << val << "+-"
351 << plus_val << unit_name[uni];
358 if (plus_uni == minus_uni
359 && plus_val == minus_val)
360 buffer << val << unit_name[uni]
362 << unit_name[plus_uni];
365 buffer << val << unit_name[uni]
367 << unit_name[plus_uni]
369 << unit_name[minus_uni];
372 buffer << val << '+' << plus_val
375 buffer << val << unit_name[uni]
377 << unit_name[plus_uni];
380 if (minus_val != 0.0)
381 if (uni == minus_uni)
382 buffer << val << '-' << minus_val
386 buffer << val << unit_name[uni]
388 << unit_name[minus_uni];
390 buffer << val << unit_name[uni];
392 return buffer.str().c_str();
396 string const LyXGlueLength::asLatexString() const
398 std::ostringstream buffer;
401 if (minus_val != 0.0)
402 buffer << val << unit_name[uni]
404 << plus_val << unit_name[plus_uni]
406 << minus_val << unit_name[minus_uni];
408 buffer << val << unit_name[uni]
410 << plus_val << unit_name[plus_uni];
412 if (minus_val != 0.0)
413 buffer << val << unit_name[uni]
415 << minus_val << unit_name[minus_uni];
417 buffer << val << unit_name[uni];
419 return buffer.str().c_str();
426 VSpace::VSpace (string const & data)
427 : kin (NONE), len (0.0, LyXLength::PT)
433 string input = strip(data);
435 int length = input.length();
437 if (length > 1 && input[length-1] == '*') {
439 input.erase(length - 1);
442 if (prefixIs (input, "defskip")) kin = DEFSKIP;
443 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
444 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
445 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
446 else if (prefixIs (input, "vfill")) kin = VFILL;
447 else if (isValidGlueLength (input, &len))
449 else if (sscanf(input.c_str(), "%f", &value) == 1) {
450 // This last one is for reading old .lyx files
451 // without units in added_space_top/bottom.
452 // Let unit default to centimeters here.
454 len = LyXGlueLength (value, LyXLength::CM);
459 bool VSpace::operator==(VSpace const & other) const
461 if (this->kin != other.kin)
464 if (this->kin != LENGTH)
465 return this->kp == other.kp;
467 if (!(this->len == other.len))
470 return this->kp == other.kp;
474 string const VSpace::asLyXCommand() const
480 case DEFSKIP: result = "defskip"; break;
481 case SMALLSKIP: result = "smallskip"; break;
482 case MEDSKIP: result = "medskip"; break;
483 case BIGSKIP: result = "bigskip"; break;
484 case VFILL: result = "vfill"; break;
485 case LENGTH: result = len.asString(); break;
487 if (kp && (kin != NONE) && (kin != DEFSKIP))
488 return result += '*';
494 string const VSpace::asLatexCommand(BufferParams const & params) const
497 case NONE: return string();
499 return params.getDefSkip().asLatexCommand(params);
500 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
502 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
504 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
506 case VFILL: return kp ? "\\vspace*{\\fill}"
508 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
509 : "\\vspace{" + len.asLatexString() + '}';
511 return string(); // should never be reached
515 int VSpace::inPixels(BufferView * bv) const
517 // Height of a normal line in pixels (zoom factor considered)
518 int height = bv->text->DefaultHeight(); // [pixels]
521 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
523 return inPixels(height, skip);
526 int VSpace::inPixels(int default_height, int default_skip) const
528 // Height of a normal line in pixels (zoom factor considered)
529 int height = default_height; // [pixels]
531 // Zoom factor specified by user in percent
532 float const zoom = lyxrc.zoom / 100.0; // [percent]
534 // DPI setting for monitor: pixels/inch
535 float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
537 // We want the result in pixels
546 // This is how the skips are normally defined by
547 // LateX. But there should be some way to change
548 // this per document.
549 case SMALLSKIP: return height / 4;
550 case MEDSKIP: return height / 2;
551 case BIGSKIP: return height;
552 case VFILL: return 3 * height;
553 // leave space for the vfill symbol
555 // Pixel values are scaled so that the ratio
556 // between lengths and font sizes on the screen
557 // is the same as on paper.
559 // we don't care about sign of value, we
560 // can't display negative anyway
563 short val_sign = value < 0.0 ? -1 : 1;
565 switch (len.unit()) {
567 // Scaled point: sp = 1/65536 pt
568 result = zoom * dpi * value
569 / (72.27 * 65536); // 4736286.7
572 // Point: 1 pt = 1/72.27 inch
573 result = zoom * dpi * value
577 // Big point: 1 bp = 1/72 inch
578 result = zoom * dpi * value
582 // Didot: 1157dd = 1238 pt?
583 result = zoom * dpi * value
584 / (72.27 / (0.376 * 2.845)); // 67.559735
587 // Millimeter: 1 mm = 1/25.4 inch
588 result = zoom * dpi * value
592 // Pica: 1 pc = 12 pt
593 result = zoom * dpi * value
594 / (72.27 / 12); // 6.0225
597 // Cicero: 1 cc = 12 dd
598 result = zoom * dpi * value
599 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
602 // Centimeter: 1 cm = 1/2.54 inch
603 result = zoom * dpi * value
608 result = zoom * dpi * value;
611 // Ex: The height of an "x"
612 result = zoom * value * height / 2; // what to / width?
614 case LyXLength::EM: // what to / width?
615 // Em: The width of an "m"
616 result = zoom * value * height / 2; // Why 2?
618 case LyXLength::MU: // This is probably only allowed in
620 result = zoom * value * height;
622 case LyXLength::UNIT_NONE:
623 result = 0; // this cannot happen
626 return int (result * val_sign + 0.5);
628 return 0; // never reached