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] == '+') {
78 lyx_advance (data, 1);
81 else if (prefixIs(data, "plus")) {
82 lyx_advance (data, 4);
85 else if (data[0] == '-') {
86 lyx_advance (data, 1);
89 else if (prefixIs(data, "minus")) {
90 lyx_advance (data, 5);
96 // I really mean assignment ("=") below, not equality!
97 if ((i = data.find_last_of("0123456789.")) != string::npos) {
98 if (number_index > 3) return 'E'; // Error
99 string buffer = data.substr(0, i + 1);
100 if (sscanf (buffer.c_str(),
101 "%f", &number[number_index]) == 1) {
102 lyx_advance (data, i + 1);
107 } else if ((i = data.find_last_of("abcdefghijklmnopqrstuvwxyz"))
109 if (unit_index > 3) return 'E'; // Error
110 string buffer = data.substr(0, i + 1);
111 unit[unit_index] = unitFromString (buffer);
112 if (unit[unit_index] != LyXLength::UNIT_NONE) {
113 lyx_advance (data, i + 1);
125 char const * pattern;
126 int plus_val_index, minus_val_index,
127 plus_uni_index, minus_uni_index;
132 LaTeXLength table[] = {
133 { "nu", 0, 0, 0, 0 },
134 { "nu+nu", 2, 0, 2, 0 },
135 { "nu+nu-nu", 2, 3, 2, 3 },
136 { "nu+-nu", 2, 2, 2, 2 },
137 { "nu-nu", 0, 2, 0, 2 },
138 { "nu-nu+nu", 3, 2, 3, 2 },
139 { "nu-+nu", 2, 2, 2, 2 },
140 { "n+nu", 2, 0, 1, 0 },
141 { "n+n-nu", 2, 3, 1, 1 },
142 { "n+-nu", 2, 2, 1, 1 },
143 { "n-nu", 0, 2, 0, 1 },
144 { "n-n+nu", 3, 2, 1, 1 },
145 { "n-+nu", 2, 2, 1, 1 },
146 { "", 0, 0, 0, 0 } // sentinel, must be empty
150 bool isValidGlueLength (string const & data, LyXGlueLength * result)
152 // This parser is table-driven. First, it constructs a "pattern"
153 // that describes the sequence of tokens in "data". For example,
154 // "n-nu" means: number, minus sign, number, unit. As we go along,
155 // numbers and units are stored into static arrays. Then, "pattern"
156 // is searched in the "table". If it is found, the associated
157 // table entries tell us which number and unit should go where
158 // in the LyXLength structure. Example: if "data" has the "pattern"
159 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
160 // That means, "plus_val" is the second number that was seen
161 // in the input, "minus_val" is the third number, and "plus_uni"
162 // and "minus_uni" are the second and third units, respectively.
163 // ("val" and "uni" are always the first items seen in "data".)
164 // This is the most elegant solution I could find -- a straight-
165 // forward approach leads to very long, tedious code that would be
166 // much harder to understand and maintain. (AS)
170 string buffer = frontStrip(data);
172 // To make isValidGlueLength recognize negative values as
173 // the first number this little hack is needed:
174 short val_sign = 1; // positive as default
177 lyx_advance(buffer, 1);
181 lyx_advance(buffer, 1);
189 int pattern_index = 0, table_index = 0;
192 number_index = unit_index = 1; // entries at index 0 are sentinels
194 // construct "pattern" from "data"
195 while (!isEndOfData (buffer)) {
196 if (pattern_index > 20) return false;
197 pattern[pattern_index] = nextToken (buffer);
198 if (pattern[pattern_index] == 'E') return false;
201 pattern[pattern_index] = '\0';
203 // search "pattern" in "table"
205 while (compare(pattern, table[table_index].pattern)) {
207 if (!*table[table_index].pattern) return false;
210 // Get the values from the appropriate places. If an index
211 // is zero, the corresponding array value is zero or UNIT_NONE,
212 // so we needn't check this.
214 result->val = number[1] * val_sign;
215 result->uni = unit[1];
216 result->plus_val = number[table[table_index].plus_val_index];
217 result->minus_val = number[table[table_index].minus_val_index];
218 result->plus_uni = unit [table[table_index].plus_uni_index];
219 result->minus_uni = unit [table[table_index].minus_uni_index];
225 bool isValidLength(string const & data, LyXLength * result)
227 /// This is a trimmed down version of isValidGlueLength.
228 /// The parser may seem overkill for lengths without
229 /// glue, but since we already have it, using it is
230 /// easier than writing something from scratch.
232 string buffer = data;
233 int pattern_index = 0;
236 // To make isValidLength recognize negative values
237 // this little hack is needed:
238 short val_sign = 1; // positive as default
241 lyx_advance(buffer, 1);
245 lyx_advance(buffer, 1);
253 number_index = unit_index = 1; // entries at index 0 are sentinels
255 // construct "pattern" from "data"
256 while (!isEndOfData (buffer)) {
257 if (pattern_index > 2) return false;
258 pattern[pattern_index] = nextToken (buffer);
259 if (pattern[pattern_index] == 'E') return false;
262 pattern[pattern_index] = '\0';
264 // only the most basic pattern is accepted here
265 if (compare(pattern, "nu") != 0) return false;
267 // It _was_ a correct length string.
268 // Store away the values we found.
270 result->val = number[1] * val_sign;
271 result->uni = unit[1];
279 LyXLength::LyXLength(string const & data)
283 if (!isValidLength (data, &tmp))
284 return; // should raise an exception
292 string LyXLength::asString() const
295 std::ostringstream buffer;
296 buffer << val << unit_name[uni]; // setw?
297 return buffer.str().c_str();
300 ostrstream buffer(tbuf, 20);
301 buffer << val << unit_name[uni] << '\0'; // setw?
307 /* LyXGlueLength class
310 LyXGlueLength::LyXGlueLength (string const & data)
312 LyXGlueLength tmp(0.0, PT);
314 if (!isValidGlueLength (data, &tmp))
315 return; // should raise an exception
319 plus_val = tmp.plus_val;
320 plus_uni = tmp.plus_uni;
321 minus_val = tmp.minus_val;
322 minus_uni = tmp.minus_uni;
327 string LyXGlueLength::asString() const
330 std::ostringstream buffer;
333 ostrstream buffer(tbuf, 20);
336 if (minus_val != 0.0)
337 if ((uni == plus_uni) && (uni == minus_uni))
338 if (plus_val == minus_val)
339 buffer << val << "+-"
340 << plus_val << unit_name[uni];
347 if (plus_uni == minus_uni
348 && plus_val == minus_val)
349 buffer << val << unit_name[uni]
351 << unit_name[plus_uni];
354 buffer << val << unit_name[uni]
356 << unit_name[plus_uni]
358 << unit_name[minus_uni];
361 buffer << val << '+' << plus_val
364 buffer << val << unit_name[uni]
366 << unit_name[plus_uni];
369 if (minus_val != 0.0)
370 if (uni == minus_uni)
371 buffer << val << '-' << minus_val
375 buffer << val << unit_name[uni]
377 << unit_name[minus_uni];
379 buffer << val << unit_name[uni];
381 return buffer.str().c_str();
389 string LyXGlueLength::asLatexString() const
392 std::ostringstream buffer;
395 ostrstream buffer(tbuf, 40);
399 if (minus_val != 0.0)
400 buffer << val << unit_name[uni]
402 << plus_val << unit_name[plus_uni]
404 << minus_val << unit_name[minus_uni];
406 buffer << val << unit_name[uni]
408 << plus_val << unit_name[plus_uni];
410 if (minus_val != 0.0)
411 buffer << val << unit_name[uni]
413 << minus_val << unit_name[minus_uni];
415 buffer << val << unit_name[uni];
417 return buffer.str().c_str();
428 VSpace::VSpace (string const & data)
429 : kin (NONE), len (0.0, LyXLength::PT)
435 string input = strip(data);
437 int length = input.length();
439 if (length > 1 && input[length-1] == '*') {
441 input.erase(length - 1);
444 if (prefixIs (input, "defskip")) kin = DEFSKIP;
445 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
446 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
447 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
448 else if (prefixIs (input, "vfill")) kin = VFILL;
449 else if (isValidGlueLength (input, &len))
451 else if (sscanf (input.c_str(), "%f", &value) == 1) {
452 // This last one is for reading old .lyx files
453 // without units in added_space_top/bottom.
454 // Let unit default to centimeters here.
456 len = LyXGlueLength (value, LyXLength::CM);
461 bool VSpace::operator==(VSpace const & other) const
463 if (this->kin == other.kin)
464 if (this->kin == LENGTH)
465 if (this->len == other.len)
466 return this->kp == other.kp;
470 return this->kp == other.kp;
476 string VSpace::asLyXCommand() const
482 case DEFSKIP: result = "defskip"; break;
483 case SMALLSKIP: result = "smallskip"; break;
484 case MEDSKIP: result = "medskip"; break;
485 case BIGSKIP: result = "bigskip"; break;
486 case VFILL: result = "vfill"; break;
487 case LENGTH: result = len.asString(); break;
489 if (kp && (kin != NONE) && (kin != DEFSKIP))
490 return result += '*';
496 string VSpace::asLatexCommand(BufferParams const & params) const
499 case NONE: return string();
501 return params.getDefSkip().asLatexCommand(params);
502 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
504 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
506 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
508 case VFILL: return kp ? "\\vspace*{\\fill}"
510 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
511 : "\\vspace{" + len.asLatexString() + '}';
513 return string(); // should never be reached
517 int VSpace::inPixels(BufferView * bv) const
519 // Height of a normal line in pixels (zoom factor considered)
520 int height = bv->text->DefaultHeight(); // [pixels]
523 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
525 return inPixels(height, skip);
528 int VSpace::inPixels(int default_height, int default_skip) const
530 // Height of a normal line in pixels (zoom factor considered)
531 int height = default_height; // [pixels]
533 // Zoom factor specified by user in percent
534 float const zoom = lyxrc.zoom / 100.0; // [percent]
536 // DPI setting for monitor: pixels/inch
537 float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
539 // We want the result in pixels
548 // This is how the skips are normally defined by
549 // LateX. But there should be some way to change
550 // this per document.
551 case SMALLSKIP: return height / 4;
552 case MEDSKIP: return height / 2;
553 case BIGSKIP: return height;
554 case VFILL: return 3 * height;
555 // leave space for the vfill symbol
557 // Pixel values are scaled so that the ratio
558 // between lengths and font sizes on the screen
559 // is the same as on paper.
561 // we don't care about sign of value, we
562 // can't display negative anyway
565 short val_sign = value < 0.0 ? -1 : 1;
567 switch (len.unit()) {
569 // Scaled point: sp = 1/65536 pt
570 result = zoom * dpi * value
571 / (72.27 * 65536); // 4736286.7
574 // Point: 1 pt = 1/72.27 inch
575 result = zoom * dpi * value
579 // Big point: 1 bp = 1/72 inch
580 result = zoom * dpi * value
584 // Didot: 1157dd = 1238 pt?
585 result = zoom * dpi * value
586 / (72.27 / (0.376 * 2.845)); // 67.559735
589 // Millimeter: 1 mm = 1/25.4 inch
590 result = zoom * dpi * value
594 // Pica: 1 pc = 12 pt
595 result = zoom * dpi * value
596 / (72.27 / 12); // 6.0225
599 // Cicero: 1 cc = 12 dd
600 result = zoom * dpi * value
601 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
604 // Centimeter: 1 cm = 1/2.54 inch
605 result = zoom * dpi * value
610 result = zoom * dpi * value;
613 // Ex: The height of an "x"
614 result = zoom * value * height / 2; // what to / width?
616 case LyXLength::EM: // what to / width?
617 // Em: The width of an "m"
618 result = zoom * value * height / 2; // Why 2?
620 case LyXLength::MU: // This is probably only allowed in
622 result = zoom * value * height;
624 case LyXLength::UNIT_NONE:
625 result = 0; // this cannot happen
628 return int (result * val_sign + 0.5);
630 return 0; // never reached