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"
31 static const int num_units = LyXLength::UNIT_NONE;
33 // I am not sure if "mu" should be possible to select (Lgb)
34 static char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
35 "mm", "pc", "cc", "cm",
36 "in", "ex", "em", "mu" };
39 LyXLength::UNIT unitFromString (string const & data)
42 while ((i < num_units) && (data != unit_name[i])) ++i;
43 return static_cast<LyXLength::UNIT>(i);
46 /* The following static items form a simple scanner for
47 * length strings, used by isValid[Glue]Length. See comments there.
49 static float number[4] = { 0, 0, 0, 0 };
50 static LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
53 LyXLength::UNIT_NONE };
54 //static int number_index, unit_index;
55 int number_index, unit_index;
59 void lyx_advance (string & data, unsigned int n)
66 bool isEndOfData (string const & data)
68 return frontStrip (data).empty();
73 char nextToken (string & data)
75 data = frontStrip(data);
78 else if (data[0] == '+') {
79 lyx_advance (data, 1);
82 else if (prefixIs(data, "plus")) {
83 lyx_advance (data, 4);
86 else if (data[0] == '-') {
87 lyx_advance (data, 1);
90 else if (prefixIs(data, "minus")) {
91 lyx_advance (data, 5);
97 // I really mean assignment ("=") below, not equality!
98 if ((i = data.find_last_of("0123456789.")) != string::npos) {
99 if (number_index > 3) return 'E'; // Error
100 string buffer = data.substr(0, i + 1);
101 if (sscanf (buffer.c_str(),
102 "%f", &number[number_index]) == 1) {
103 lyx_advance (data, i + 1);
108 } else if ((i = data.find_last_of("abcdefghijklmnopqrstuvwxyz"))
110 if (unit_index > 3) return 'E'; // Error
111 string buffer = data.substr(0, i + 1);
112 unit[unit_index] = unitFromString (buffer);
113 if (unit[unit_index] != LyXLength::UNIT_NONE) {
114 lyx_advance (data, i + 1);
126 char const * pattern;
127 int plus_val_index, minus_val_index,
128 plus_uni_index, minus_uni_index;
133 LaTeXLength table[] = {
134 { "nu", 0, 0, 0, 0 },
135 { "nu+nu", 2, 0, 2, 0 },
136 { "nu+nu-nu", 2, 3, 2, 3 },
137 { "nu+-nu", 2, 2, 2, 2 },
138 { "nu-nu", 0, 2, 0, 2 },
139 { "nu-nu+nu", 3, 2, 3, 2 },
140 { "nu-+nu", 2, 2, 2, 2 },
141 { "n+nu", 2, 0, 1, 0 },
142 { "n+n-nu", 2, 3, 1, 1 },
143 { "n+-nu", 2, 2, 1, 1 },
144 { "n-nu", 0, 2, 0, 1 },
145 { "n-n+nu", 3, 2, 1, 1 },
146 { "n-+nu", 2, 2, 1, 1 },
147 { "", 0, 0, 0, 0 } // sentinel, must be empty
151 bool isValidGlueLength (string const & data, LyXGlueLength * result)
153 // This parser is table-driven. First, it constructs a "pattern"
154 // that describes the sequence of tokens in "data". For example,
155 // "n-nu" means: number, minus sign, number, unit. As we go along,
156 // numbers and units are stored into static arrays. Then, "pattern"
157 // is searched in the "table". If it is found, the associated
158 // table entries tell us which number and unit should go where
159 // in the LyXLength structure. Example: if "data" has the "pattern"
160 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
161 // That means, "plus_val" is the second number that was seen
162 // in the input, "minus_val" is the third number, and "plus_uni"
163 // and "minus_uni" are the second and third units, respectively.
164 // ("val" and "uni" are always the first items seen in "data".)
165 // This is the most elegant solution I could find -- a straight-
166 // forward approach leads to very long, tedious code that would be
167 // much harder to understand and maintain. (AS)
171 string buffer = frontStrip(data);
173 // To make isValidGlueLength recognize negative values as
174 // the first number this little hack is needed:
175 short val_sign = 1; // positive as default
178 lyx_advance(buffer, 1);
182 lyx_advance(buffer, 1);
190 int pattern_index = 0, table_index = 0;
193 number_index = unit_index = 1; // entries at index 0 are sentinels
195 // construct "pattern" from "data"
196 while (!isEndOfData (buffer)) {
197 if (pattern_index > 20) return false;
198 pattern[pattern_index] = nextToken (buffer);
199 if (pattern[pattern_index] == 'E') return false;
202 pattern[pattern_index] = '\0';
204 // search "pattern" in "table"
206 while (compare(pattern, table[table_index].pattern)) {
208 if (!*table[table_index].pattern) return false;
211 // Get the values from the appropriate places. If an index
212 // is zero, the corresponding array value is zero or UNIT_NONE,
213 // so we needn't check this.
215 result->val = number[1] * val_sign;
216 result->uni = unit[1];
217 result->plus_val = number[table[table_index].plus_val_index];
218 result->minus_val = number[table[table_index].minus_val_index];
219 result->plus_uni = unit [table[table_index].plus_uni_index];
220 result->minus_uni = unit [table[table_index].minus_uni_index];
226 bool isValidLength(string const & data, LyXLength * result)
228 /// This is a trimmed down version of isValidGlueLength.
229 /// The parser may seem overkill for lengths without
230 /// glue, but since we already have it, using it is
231 /// easier than writing something from scratch.
233 string buffer = data;
234 int pattern_index = 0;
237 // To make isValidLength recognize negative values
238 // this little hack is needed:
239 short val_sign = 1; // positive as default
242 lyx_advance(buffer, 1);
246 lyx_advance(buffer, 1);
254 number_index = unit_index = 1; // entries at index 0 are sentinels
256 // construct "pattern" from "data"
257 while (!isEndOfData (buffer)) {
258 if (pattern_index > 2) return false;
259 pattern[pattern_index] = nextToken (buffer);
260 if (pattern[pattern_index] == 'E') return false;
263 pattern[pattern_index] = '\0';
265 // only the most basic pattern is accepted here
266 if (compare(pattern, "nu") != 0) return false;
268 // It _was_ a correct length string.
269 // Store away the values we found.
271 result->val = number[1] * val_sign;
272 result->uni = unit[1];
280 LyXLength::LyXLength(string const & data)
284 if (!isValidLength (data, &tmp))
285 return; // should raise an exception
293 bool LyXLength::operator== (LyXLength const & other) const
295 return (val == other.val &&
300 string LyXLength::asString() const
303 ostringstream buffer;
304 buffer << val << unit_name[uni]; // setw?
305 return buffer.str().c_str();
308 ostrstream buffer(tbuf, 20);
309 buffer << val << unit_name[uni] << '\0'; // setw?
315 /* LyXGlueLength class
318 LyXGlueLength::LyXGlueLength (string const & data)
320 LyXGlueLength tmp(0.0, PT);
322 if (!isValidGlueLength (data, &tmp))
323 return; // should raise an exception
327 plus_val = tmp.plus_val;
328 plus_uni = tmp.plus_uni;
329 minus_val = tmp.minus_val;
330 minus_uni = tmp.minus_uni;
335 bool LyXGlueLength::operator== (LyXGlueLength const & other) const
337 return (val == other.val &&
339 plus_val == other.plus_val &&
340 plus_uni == other.plus_uni &&
341 minus_val == other.minus_val &&
342 minus_uni == other.minus_uni);
346 string LyXGlueLength::asString() const
349 ostringstream buffer;
352 ostrstream buffer(tbuf, 20);
355 if (minus_val != 0.0)
356 if ((uni == plus_uni) && (uni == minus_uni))
357 if (plus_val == minus_val)
358 buffer << val << "+-"
359 << plus_val << unit_name[uni];
366 if (plus_uni == minus_uni
367 && plus_val == minus_val)
368 buffer << val << unit_name[uni]
370 << unit_name[plus_uni];
373 buffer << val << unit_name[uni]
375 << unit_name[plus_uni]
377 << unit_name[minus_uni];
380 buffer << val << '+' << plus_val
383 buffer << val << unit_name[uni]
385 << unit_name[plus_uni];
388 if (minus_val != 0.0)
389 if (uni == minus_uni)
390 buffer << val << '-' << minus_val
394 buffer << val << unit_name[uni]
396 << unit_name[minus_uni];
398 buffer << val << unit_name[uni];
400 return buffer.str().c_str();
408 string LyXGlueLength::asLatexString() const
411 ostringstream buffer;
414 ostrstream buffer(tbuf, 40);
418 if (minus_val != 0.0)
419 buffer << val << unit_name[uni]
421 << plus_val << unit_name[plus_uni]
423 << minus_val << unit_name[minus_uni];
425 buffer << val << unit_name[uni]
427 << plus_val << unit_name[plus_uni];
429 if (minus_val != 0.0)
430 buffer << val << unit_name[uni]
432 << minus_val << unit_name[minus_uni];
434 buffer << val << unit_name[uni];
436 return buffer.str().c_str();
447 VSpace::VSpace (string const & data)
448 : kin (NONE), len (0.0, LyXLength::PT)
454 string input = strip(data);
456 int length = input.length();
458 if (length > 1 && input[length-1] == '*') {
460 input.erase(length - 1);
463 if (prefixIs (input, "defskip")) kin = DEFSKIP;
464 else if (prefixIs (input, "smallskip")) kin = SMALLSKIP;
465 else if (prefixIs (input, "medskip")) kin = MEDSKIP;
466 else if (prefixIs (input, "bigskip")) kin = BIGSKIP;
467 else if (prefixIs (input, "vfill")) kin = VFILL;
468 else if (isValidGlueLength (input, &len))
470 else if (sscanf (input.c_str(), "%f", &value) == 1) {
471 // This last one is for reading old .lyx files
472 // without units in added_space_top/bottom.
473 // Let unit default to centimeters here.
475 len = LyXGlueLength (value, LyXLength::CM);
480 bool VSpace::operator==(VSpace const & other) const
482 if (this->kin == other.kin)
483 if (this->kin == LENGTH)
484 if (this->len == other.len)
485 return this->kp == other.kp;
489 return this->kp == other.kp;
495 string VSpace::asLyXCommand() const
501 case DEFSKIP: result = "defskip"; break;
502 case SMALLSKIP: result = "smallskip"; break;
503 case MEDSKIP: result = "medskip"; break;
504 case BIGSKIP: result = "bigskip"; break;
505 case VFILL: result = "vfill"; break;
506 case LENGTH: result = len.asString(); break;
508 if (kp && (kin != NONE) && (kin != DEFSKIP))
509 return result += '*';
515 string VSpace::asLatexCommand(BufferParams const & params) const
518 case NONE: return string();
520 return params.getDefSkip().asLatexCommand(params);
521 case SMALLSKIP: return kp ? "\\vspace*{\\smallskipamount}"
523 case MEDSKIP: return kp ? "\\vspace*{\\medskipamount}"
525 case BIGSKIP: return kp ? "\\vspace*{\\bigskipamount}"
527 case VFILL: return kp ? "\\vspace*{\\fill}"
529 case LENGTH: return kp ? "\\vspace*{" + len.asLatexString() + '}'
530 : "\\vspace{" + len.asLatexString() + '}';
532 return string(); // should never be reached
536 int VSpace::inPixels(BufferView * bv) const
538 // Height of a normal line in pixels (zoom factor considered)
539 int height = bv->text->DefaultHeight(); // [pixels]
541 // Zoom factor specified by user in percent
542 float const zoom = lyxrc->zoom / 100.0; // [percent]
544 // DPI setting for monitor: pixels/inch
545 float const dpi = lyxrc->dpi; // screen resolution [pixels/inch]
547 // We want the result in pixels
554 return bv->buffer()->params.getDefSkip().inPixels(bv);
556 // This is how the skips are normally defined by
557 // LateX. But there should be some way to change
558 // this per document.
559 case SMALLSKIP: return height/4;
560 case MEDSKIP: return height/2;
561 case BIGSKIP: return height;
562 case VFILL: return 3*height;
563 // leave space for the vfill symbol
565 // Pixel values are scaled so that the ratio
566 // between lengths and font sizes on the screen
567 // is the same as on paper.
569 // we don't care about sign of value, we
570 // can't display negative anyway
573 short val_sign = value < 0.0 ? -1 : 1;
575 switch (len.unit()) {
577 // Scaled point: sp = 1/65536 pt
578 result = zoom * dpi * value
579 / (72.27 * 65536); // 4736286.7
582 // Point: 1 pt = 1/72.27 inch
583 result = zoom * dpi * value
587 // Big point: 1 bp = 1/72 inch
588 result = zoom * dpi * value
592 // Didot: 1157dd = 1238 pt?
593 result = zoom * dpi * value
594 / (72.27 / (0.376 * 2.845)); // 67.559735
597 // Millimeter: 1 mm = 1/25.4 inch
598 result = zoom * dpi * value
602 // Pica: 1 pc = 12 pt
603 result = zoom * dpi * value
604 / (72.27 / 12); // 6.0225
607 // Cicero: 1 cc = 12 dd
608 result = zoom * dpi * value
609 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
612 // Centimeter: 1 cm = 1/2.54 inch
613 result = zoom * dpi * value
618 result = zoom * dpi * value;
621 // Ex: The height of an "x"
622 result = zoom * value * height / 2; // what to / width?
624 case LyXLength::EM: // what to / width?
625 // Em: The width of an "m"
626 result = zoom * value * height / 2; // Why 2?
628 case LyXLength::MU: // This is probably only allowed in
630 result = zoom * value * height;
632 case LyXLength::UNIT_NONE:
633 result = 0; // this cannot happen
636 return int (result * val_sign + 0.5);
638 return 0; // never reached