1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation "vspace.h"
24 #include "BufferView.h"
25 #include "support/lstrings.h"
33 int const num_units = LyXLength::UNIT_NONE;
35 // I am not sure if "mu" should be possible to select (Lgb)
36 char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
37 "mm", "pc", "cc", "cm",
38 "in", "ex", "em", "mu",
39 "%", "c%", "p%", "l%" };
42 /* The following static items form a simple scanner for
43 * length strings, used by isValid[Glue]Length. See comments there.
45 float number[4] = { 0, 0, 0, 0 };
46 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
49 LyXLength::UNIT_NONE };
55 void lyx_advance(string & data, unsigned int n)
62 bool isEndOfData(string const & data)
64 return frontStrip(data).empty();
68 char nextToken(string & data)
70 data = frontStrip(data);
73 else if (data[0] == '+') {
76 } else if (prefixIs(data, "plus")) {
79 } else if (data[0] == '-') {
82 } else if (prefixIs(data, "minus")) {
86 string::size_type i = data.find_first_not_of("0123456789.");
89 if (number_index > 3) return 'E';
93 // we have found some number
94 if (i == string::npos) {
98 buffer = data.substr(0, i);
100 lyx_advance(data, i);
102 if (isStrDbl(buffer)) {
103 number[number_index] = strToDbl(buffer);
109 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
111 if (unit_index > 3) return 'E';
115 // we have found some alphabetical string
116 if (i == string::npos) {
120 buffer = data.substr(0, i);
122 // possibly we have "mmplus" string or similar
123 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
124 lyx_advance(data, 2);
125 unit[unit_index] = unitFromString(buffer.substr(0, 2));
127 lyx_advance(data, i);
128 unit[unit_index] = unitFromString(buffer);
131 if (unit[unit_index] != LyXLength::UNIT_NONE) {
134 } else return 'E'; // Error
142 char const * pattern;
143 int plus_val_index, minus_val_index,
144 plus_uni_index, minus_uni_index;
148 LaTeXLength table[] = {
149 { "nu", 0, 0, 0, 0 },
150 { "nu+nu", 2, 0, 2, 0 },
151 { "nu+nu-nu", 2, 3, 2, 3 },
152 { "nu+-nu", 2, 2, 2, 2 },
153 { "nu-nu", 0, 2, 0, 2 },
154 { "nu-nu+nu", 3, 2, 3, 2 },
155 { "nu-+nu", 2, 2, 2, 2 },
156 { "n+nu", 2, 0, 1, 0 },
157 { "n+n-nu", 2, 3, 1, 1 },
158 { "n+-nu", 2, 2, 1, 1 },
159 { "n-nu", 0, 2, 0, 1 },
160 { "n-n+nu", 3, 2, 1, 1 },
161 { "n-+nu", 2, 2, 1, 1 },
162 { "", 0, 0, 0, 0 } // sentinel, must be empty
167 const char * stringFromUnit(int unit)
169 if (unit < 0 || unit >= num_units)
171 return unit_name[unit];
175 LyXLength::UNIT unitFromString (string const & data)
178 while ((i < num_units) && (data != unit_name[i])) ++i;
179 return static_cast<LyXLength::UNIT>(i);
183 bool isValidGlueLength (string const & data, LyXGlueLength * result)
185 // This parser is table-driven. First, it constructs a "pattern"
186 // that describes the sequence of tokens in "data". For example,
187 // "n-nu" means: number, minus sign, number, unit. As we go along,
188 // numbers and units are stored into static arrays. Then, "pattern"
189 // is searched in the "table". If it is found, the associated
190 // table entries tell us which number and unit should go where
191 // in the LyXLength structure. Example: if "data" has the "pattern"
192 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
193 // That means, "plus_val" is the second number that was seen
194 // in the input, "minus_val" is the third number, and "plus_uni"
195 // and "minus_uni" are the second and third units, respectively.
196 // ("val" and "uni" are always the first items seen in "data".)
197 // This is the most elegant solution I could find -- a straight-
198 // forward approach leads to very long, tedious code that would be
199 // much harder to understand and maintain. (AS)
203 string buffer = frontStrip(data);
205 // To make isValidGlueLength recognize negative values as
206 // the first number this little hack is needed:
207 short val_sign = 1; // positive as default
210 lyx_advance(buffer, 1);
214 lyx_advance(buffer, 1);
222 int pattern_index = 0, table_index = 0;
225 number_index = unit_index = 1; // entries at index 0 are sentinels
227 // construct "pattern" from "data"
228 while (!isEndOfData (buffer)) {
229 if (pattern_index > 20) return false;
230 pattern[pattern_index] = nextToken (buffer);
231 if (pattern[pattern_index] == 'E') return false;
234 pattern[pattern_index] = '\0';
236 // search "pattern" in "table"
238 while (compare(pattern, table[table_index].pattern)) {
240 if (!*table[table_index].pattern) return false;
243 // Get the values from the appropriate places. If an index
244 // is zero, the corresponding array value is zero or UNIT_NONE,
245 // so we needn't check this.
247 result->val = number[1] * val_sign;
248 result->uni = unit[1];
249 result->plus_val = number[table[table_index].plus_val_index];
250 result->minus_val = number[table[table_index].minus_val_index];
251 result->plus_uni = unit [table[table_index].plus_uni_index];
252 result->minus_uni = unit [table[table_index].minus_uni_index];
258 bool isValidLength(string const & data, LyXLength * result)
260 /// This is a trimmed down version of isValidGlueLength.
261 /// The parser may seem overkill for lengths without
262 /// glue, but since we already have it, using it is
263 /// easier than writing something from scratch.
268 int pattern_index = 0;
271 // To make isValidLength recognize negative values
272 // this little hack is needed:
273 short val_sign = 1; // positive as default
276 lyx_advance(buffer, 1);
280 lyx_advance(buffer, 1);
288 number_index = unit_index = 1; // entries at index 0 are sentinels
290 // construct "pattern" from "data"
291 while (!isEndOfData (buffer)) {
292 if (pattern_index > 2) return false;
293 pattern[pattern_index] = nextToken (buffer);
294 if (pattern[pattern_index] == 'E') return false;
297 pattern[pattern_index] = '\0';
299 // only the most basic pattern is accepted here
300 if (compare(pattern, "nu") != 0) return false;
302 // It _was_ a correct length string.
303 // Store away the values we found.
305 result->val = number[1] * val_sign;
306 result->uni = unit[1];
314 LyXLength::LyXLength(string const & data)
318 if (!isValidLength (data, &tmp))
319 return; // should raise an exception
327 string const LyXLength::asString() const
329 ostringstream buffer;
330 buffer << val << unit_name[uni]; // setw?
331 return buffer.str().c_str();
335 string const LyXLength::asLatexString() const
337 ostringstream buffer;
341 buffer << abs(static_cast<int>(val/100)) << "." << abs(static_cast<int>(val)%100) << "\\columnwidth";
344 buffer << "." << abs(static_cast<int>(val/100)) << "." << abs(static_cast<int>(val)%100) << "\\pagewidth";
347 buffer << "." << abs(static_cast<int>(val/100)) << "." << abs(static_cast<int>(val)%100) << "\\linewidth";
350 buffer << val << unit_name[uni]; // setw?
353 return buffer.str().c_str();
357 /* LyXGlueLength class
360 LyXGlueLength::LyXGlueLength (string const & data)
362 LyXGlueLength tmp(0.0, PT);
364 if (!isValidGlueLength (data, &tmp))
365 return; // should raise an exception
369 plus_val = tmp.plus_val;
370 plus_uni = tmp.plus_uni;
371 minus_val = tmp.minus_val;
372 minus_uni = tmp.minus_uni;
377 string const LyXGlueLength::asString() const
379 ostringstream buffer;
382 if (minus_val != 0.0)
383 if ((uni == plus_uni) && (uni == minus_uni))
384 if (plus_val == minus_val)
385 buffer << val << "+-"
386 << plus_val << unit_name[uni];
393 if (plus_uni == minus_uni
394 && plus_val == minus_val)
395 buffer << val << unit_name[uni]
397 << unit_name[plus_uni];
400 buffer << val << unit_name[uni]
402 << unit_name[plus_uni]
404 << unit_name[minus_uni];
407 buffer << val << '+' << plus_val
410 buffer << val << unit_name[uni]
412 << unit_name[plus_uni];
415 if (minus_val != 0.0)
416 if (uni == minus_uni)
417 buffer << val << '-' << minus_val
421 buffer << val << unit_name[uni]
423 << unit_name[minus_uni];
425 buffer << val << unit_name[uni];
427 return buffer.str().c_str();
431 string const LyXGlueLength::asLatexString() const
433 ostringstream buffer;
436 if (minus_val != 0.0)
437 buffer << val << unit_name[uni]
439 << plus_val << unit_name[plus_uni]
441 << minus_val << unit_name[minus_uni];
443 buffer << val << unit_name[uni]
445 << plus_val << unit_name[plus_uni];
447 if (minus_val != 0.0)
448 buffer << val << unit_name[uni]
450 << minus_val << unit_name[minus_uni];
452 buffer << val << unit_name[uni];
454 return buffer.str().c_str();
461 VSpace::VSpace (string const & data)
462 : kin (NONE), len (0.0, LyXLength::PT)
468 string input = strip(data);
470 int length = input.length();
472 if (length > 1 && input[length-1] == '*') {
474 input.erase(length - 1);
477 if (prefixIs (input, "defskip")) kin = DEFSKIP;
478 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
479 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
480 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
481 else if (prefixIs (input, "vfill")) kin = VFILL;
482 else if (isValidGlueLength (input, &len))
484 else if (sscanf(input.c_str(), "%f", &value) == 1) {
485 // This last one is for reading old .lyx files
486 // without units in added_space_top/bottom.
487 // Let unit default to centimeters here.
489 len = LyXGlueLength (value, LyXLength::CM);
494 bool VSpace::operator==(VSpace const & other) const
496 if (this->kin != other.kin)
499 if (this->kin != LENGTH)
500 return this->kp == other.kp;
502 if (!(this->len == other.len))
505 return this->kp == other.kp;
509 string const VSpace::asLyXCommand() const
515 case DEFSKIP: result = "defskip"; break;
516 case SMALLSKIP: result = "smallskip"; break;
517 case MEDSKIP: result = "medskip"; break;
518 case BIGSKIP: result = "bigskip"; break;
519 case VFILL: result = "vfill"; break;
520 case LENGTH: result = len.asString(); break;
522 if (kp && (kin != NONE) && (kin != DEFSKIP))
523 return result += '*';
529 string const VSpace::asLatexCommand(BufferParams const & params) const
532 case NONE: return string();
534 return params.getDefSkip().asLatexCommand(params);
535 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
537 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
539 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
541 case VFILL: return kp ? "\\vspace*{\\fill}"
543 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
544 : "\\vspace{" + len.asLatexString() + '}';
546 return string(); // should never be reached
550 int VSpace::inPixels(BufferView * bv) const
552 // Height of a normal line in pixels (zoom factor considered)
553 int height = bv->text->defaultHeight(); // [pixels]
555 int width = bv->workWidth();
558 skip = bv->buffer()->params.getDefSkip().inPixels(bv);
560 return inPixels(height, skip, width);
563 int VSpace::inPixels(int default_height, int default_skip, int default_width) const
565 // Height of a normal line in pixels (zoom factor considered)
566 int height = default_height; // [pixels]
568 // Zoom factor specified by user in percent
569 float const zoom = lyxrc.zoom / 100.0; // [percent]
571 // DPI setting for monitor: pixels/inch
572 float const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
574 // We want the result in pixels
583 // This is how the skips are normally defined by
584 // LateX. But there should be some way to change
585 // this per document.
586 case SMALLSKIP: return height / 4;
587 case MEDSKIP: return height / 2;
588 case BIGSKIP: return height;
589 case VFILL: return 3 * height;
590 // leave space for the vfill symbol
592 // Pixel values are scaled so that the ratio
593 // between lengths and font sizes on the screen
594 // is the same as on paper.
596 // we don't care about sign of value, we
597 // can't display negative anyway
600 short val_sign = value < 0.0 ? -1 : 1;
602 switch (len.unit()) {
604 // Scaled point: sp = 1/65536 pt
605 result = zoom * dpi * value
606 / (72.27 * 65536); // 4736286.7
609 // Point: 1 pt = 1/72.27 inch
610 result = zoom * dpi * value
614 // Big point: 1 bp = 1/72 inch
615 result = zoom * dpi * value
619 // Didot: 1157dd = 1238 pt?
620 result = zoom * dpi * value
621 / (72.27 / (0.376 * 2.845)); // 67.559735
624 // Millimeter: 1 mm = 1/25.4 inch
625 result = zoom * dpi * value
629 // Pica: 1 pc = 12 pt
630 result = zoom * dpi * value
631 / (72.27 / 12); // 6.0225
634 // Cicero: 1 cc = 12 dd
635 result = zoom * dpi * value
636 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
639 // Centimeter: 1 cm = 1/2.54 inch
640 result = zoom * dpi * value
645 result = zoom * dpi * value;
648 // Ex: The height of an "x"
649 result = zoom * value * height / 2; // what to / width?
651 case LyXLength::EM: // what to / width?
652 // Em: The width of an "m"
653 result = zoom * value * height / 2; // Why 2?
655 case LyXLength::MU: // This is probably only allowed in
657 result = zoom * value * height;
659 case LyXLength::PW: // Always % of workarea
663 result = value * default_width / 100;
665 case LyXLength::UNIT_NONE:
666 result = 0; // this cannot happen
669 return int (result * val_sign + 0.5);
671 return 0; // never reached