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
18 #include "lengthcommon.h"
23 #include "BufferView.h"
25 #include "support/lstrings.h"
36 int const num_units = LyXLength::UNIT_NONE;
38 // I am not sure if "mu" should be possible to select (Lgb)
39 char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
40 "mm", "pc", "cc", "cm",
41 "in", "ex", "em", "mu",
42 "%", "c%", "p%", "l%" };
45 /* The following static items form a simple scanner for
46 * length strings, used by isValid[Glue]Length. See comments there.
48 double number[4] = { 0, 0, 0, 0 };
49 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
52 LyXLength::UNIT_NONE };
56 LyXLength::UNIT unitFromString(string const & data)
59 while (i < num_units && data != unit_name[i])
61 return static_cast<LyXLength::UNIT>(i);
65 double number[4] = { 0, 0, 0, 0 };
66 LyXLength::UNIT unit[4] = { LyXLength::UNIT_NONE,
69 LyXLength::UNIT_NONE };
75 void lyx_advance(string & data, string::size_type n)
82 bool isEndOfData(string const & data)
84 return frontStrip(data).empty();
88 char nextToken(string & data)
90 data = frontStrip(data);
93 else if (data[0] == '+') {
96 } else if (prefixIs(data, "plus")) {
99 } else if (data[0] == '-') {
100 lyx_advance(data, 1);
102 } else if (prefixIs(data, "minus")) {
103 lyx_advance(data, 5);
106 string::size_type i = data.find_first_not_of("0123456789.");
109 if (number_index > 3) return 'E';
113 // we have found some number
114 if (i == string::npos) {
118 buffer = data.substr(0, i);
120 lyx_advance(data, i);
122 if (isStrDbl(buffer)) {
123 number[number_index] = strToDbl(buffer);
129 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
131 if (unit_index > 3) return 'E';
135 // we have found some alphabetical string
136 if (i == string::npos) {
140 buffer = data.substr(0, i);
142 // possibly we have "mmplus" string or similar
143 if (buffer.size() > 5 && (buffer.substr(2,4) == string("plus") || buffer.substr(2,5) == string("minus"))) {
144 lyx_advance(data, 2);
145 unit[unit_index] = unitFromString(buffer.substr(0, 2));
147 lyx_advance(data, i);
148 unit[unit_index] = unitFromString(buffer);
151 if (unit[unit_index] != LyXLength::UNIT_NONE) {
154 } else return 'E'; // Error
162 char const * pattern;
170 LaTeXLength table[] = {
171 { "nu", 0, 0, 0, 0 },
172 { "nu+nu", 2, 0, 2, 0 },
173 { "nu+nu-nu", 2, 3, 2, 3 },
174 { "nu+-nu", 2, 2, 2, 2 },
175 { "nu-nu", 0, 2, 0, 2 },
176 { "nu-nu+nu", 3, 2, 3, 2 },
177 { "nu-+nu", 2, 2, 2, 2 },
178 { "n+nu", 2, 0, 1, 0 },
179 { "n+n-nu", 2, 3, 1, 1 },
180 { "n+-nu", 2, 2, 1, 1 },
181 { "n-nu", 0, 2, 0, 1 },
182 { "n-n+nu", 3, 2, 1, 1 },
183 { "n-+nu", 2, 2, 1, 1 },
184 { "", 0, 0, 0, 0 } // sentinel, must be empty
190 const char * stringFromUnit(int unit)
192 if (unit < 0 || unit >= num_units)
194 return unit_name[unit];
198 bool isValidGlueLength(string const & data, LyXGlueLength * result)
200 // This parser is table-driven. First, it constructs a "pattern"
201 // that describes the sequence of tokens in "data". For example,
202 // "n-nu" means: number, minus sign, number, unit. As we go along,
203 // numbers and units are stored into static arrays. Then, "pattern"
204 // is searched in the "table". If it is found, the associated
205 // table entries tell us which number and unit should go where
206 // in the LyXLength structure. Example: if "data" has the "pattern"
207 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
208 // That means, "plus_val" is the second number that was seen
209 // in the input, "minus_val" is the third number, and "plus_uni"
210 // and "minus_uni" are the second and third units, respectively.
211 // ("val" and "uni" are always the first items seen in "data".)
212 // This is the most elegant solution I could find -- a straight-
213 // forward approach leads to very long, tedious code that would be
214 // much harder to understand and maintain. (AS)
218 string buffer = frontStrip(data);
220 // To make isValidGlueLength recognize negative values as
221 // the first number this little hack is needed:
222 int val_sign = 1; // positive as default
225 lyx_advance(buffer, 1);
229 lyx_advance(buffer, 1);
237 int pattern_index = 0;
242 unit_index = 1; // entries at index 0 are sentinels
244 // construct "pattern" from "data"
245 while (!isEndOfData (buffer)) {
246 if (pattern_index > 20) return false;
247 pattern[pattern_index] = nextToken (buffer);
248 if (pattern[pattern_index] == 'E') return false;
251 pattern[pattern_index] = '\0';
253 // search "pattern" in "table"
255 while (compare(pattern, table[table_index].pattern)) {
257 if (!*table[table_index].pattern)
261 // Get the values from the appropriate places. If an index
262 // is zero, the corresponding array value is zero or UNIT_NONE,
263 // so we needn't check this.
265 result->len_.value (number[1] * val_sign);
266 result->len_.unit (unit[1]);
267 result->plus_.value (number[table[table_index].plus_val_index]);
268 result->plus_.unit (unit [table[table_index].plus_uni_index]);
269 result->minus_.value(number[table[table_index].minus_val_index]);
270 result->minus_.unit (unit [table[table_index].minus_uni_index]);
276 bool isValidLength(string const & data, LyXLength * result)
278 /// This is a trimmed down version of isValidGlueLength.
279 /// The parser may seem overkill for lengths without
280 /// glue, but since we already have it, using it is
281 /// easier than writing something from scratch.
285 string buffer = data;
286 int pattern_index = 0;
289 // To make isValidLength recognize negative values
290 // this little hack is needed:
291 int val_sign = 1; // positive as default
294 lyx_advance(buffer, 1);
298 lyx_advance(buffer, 1);
306 number_index = unit_index = 1; // entries at index 0 are sentinels
308 // construct "pattern" from "data"
309 while (!isEndOfData (buffer)) {
310 if (pattern_index > 2)
312 pattern[pattern_index] = nextToken (buffer);
313 if (pattern[pattern_index] == 'E')
317 pattern[pattern_index] = '\0';
319 // only the most basic pattern is accepted here
320 if (compare(pattern, "nu") != 0) return false;
322 // It _was_ a correct length string.
323 // Store away the values we found.
325 result->val_ = number[1] * val_sign;
326 result->unit_ = unit[1];
337 : kind_(NONE), len_(), keep_(false)
341 VSpace::VSpace(vspace_kind k)
342 : kind_(k), len_(), keep_(false)
346 VSpace::VSpace(LyXLength const & l)
347 : kind_(LENGTH), len_(l), keep_(false)
351 VSpace::VSpace(LyXGlueLength const & l)
352 : kind_(LENGTH), len_(l), keep_(false)
356 VSpace::VSpace(string const & data)
357 : kind_(NONE), len_(), keep_(false)
362 string input = strip(data);
364 string::size_type const length = input.length();
366 if (length > 1 && input[length-1] == '*') {
368 input.erase(length - 1);
371 if (prefixIs (input, "defskip")) kind_ = DEFSKIP;
372 else if (prefixIs (input, "smallskip")) kind_ = SMALLSKIP;
373 else if (prefixIs (input, "medskip")) kind_ = MEDSKIP;
374 else if (prefixIs (input, "bigskip")) kind_ = BIGSKIP;
375 else if (prefixIs (input, "vfill")) kind_ = VFILL;
376 else if (isValidGlueLength(input, &len_)) kind_ = LENGTH;
377 else if (sscanf(input.c_str(), "%lf", &value) == 1) {
378 // This last one is for reading old .lyx files
379 // without units in added_space_top/bottom.
380 // Let unit default to centimeters here.
382 len_ = LyXGlueLength(LyXLength(value, LyXLength::CM));
387 VSpace::vspace_kind VSpace::kind() const
393 LyXGlueLength VSpace::length() const
399 bool VSpace::keep() const
405 void VSpace::setKeep(bool val)
411 bool VSpace::operator==(VSpace const & other) const
413 if (kind_ != other.kind_)
417 return this->keep_ == other.keep_;
419 if (len_ != other.len_)
422 return keep_ == other.keep_;
426 string const VSpace::asLyXCommand() const
431 case DEFSKIP: result = "defskip"; break;
432 case SMALLSKIP: result = "smallskip"; break;
433 case MEDSKIP: result = "medskip"; break;
434 case BIGSKIP: result = "bigskip"; break;
435 case VFILL: result = "vfill"; break;
436 case LENGTH: result = len_.asString(); break;
438 if (keep_ && kind_ != NONE && kind_ != DEFSKIP)
444 string const VSpace::asLatexCommand(BufferParams const & params) const
447 case NONE: return string();
449 return params.getDefSkip().asLatexCommand(params);
450 case SMALLSKIP: return keep_ ? "\\vspace*{\\smallskipamount}"
452 case MEDSKIP: return keep_ ? "\\vspace*{\\medskipamount}"
454 case BIGSKIP: return keep_ ? "\\vspace*{\\bigskipamount}"
456 case VFILL: return keep_ ? "\\vspace*{\\fill}"
458 case LENGTH: return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
459 : "\\vspace{" + len_.asLatexString() + '}';
461 return string(); // should never be reached
465 int VSpace::inPixels(BufferView * bv) const
467 // Height of a normal line in pixels (zoom factor considered)
468 int default_height = bv->text->defaultHeight(); // [pixels]
469 int default_skip = 0;
470 int default_width = bv->workWidth();
472 if (kind_ == DEFSKIP)
473 default_skip = bv->buffer()->params.getDefSkip().inPixels(bv);
475 // Height of a normal line in pixels (zoom factor considered)
476 int height = default_height; // [pixels]
478 // Zoom factor specified by user in percent
479 double const zoom = lyxrc.zoom / 100.0; // [percent]
481 // DPI setting for monitor: pixels/inch
482 double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
484 // We want the result in pixels
495 // This is how the skips are normally defined by
496 // LateX. But there should be some way to change
497 // this per document.
498 case SMALLSKIP: return height / 4;
499 case MEDSKIP: return height / 2;
500 case BIGSKIP: return height;
501 case VFILL: return 3 * height;
502 // leave space for the vfill symbol
504 // Pixel values are scaled so that the ratio
505 // between lengths and font sizes on the screen
506 // is the same as on paper.
508 // we don't care about sign of value, we
509 // display negative space with text too
511 value = len_.len().value();
512 int val_sign = value < 0.0 ? -1 : 1;
514 switch (len_.len().unit()) {
516 // Scaled point: sp = 1/65536 pt
517 result = zoom * dpi * value
518 / (72.27 * 65536); // 4736286.7
521 // Point: 1 pt = 1/72.27 inch
522 result = zoom * dpi * value
526 // Big point: 1 bp = 1/72 inch
527 result = zoom * dpi * value
531 // Didot: 1157dd = 1238 pt?
532 result = zoom * dpi * value
533 / (72.27 / (0.376 * 2.845)); // 67.559735
536 // Millimeter: 1 mm = 1/25.4 inch
537 result = zoom * dpi * value
541 // Pica: 1 pc = 12 pt
542 result = zoom * dpi * value
543 / (72.27 / 12); // 6.0225
546 // Cicero: 1 cc = 12 dd
547 result = zoom * dpi * value
548 / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
551 // Centimeter: 1 cm = 1/2.54 inch
552 result = zoom * dpi * value
557 result = zoom * dpi * value;
560 // Ex: The height of an "x"
561 result = zoom * value * height / 2; // what to / width?
563 case LyXLength::EM: // what to / width?
564 // Em: The width of an "m"
565 result = zoom * value * height / 2; // Why 2?
567 case LyXLength::MU: // This is probably only allowed in
569 result = zoom * value * height;
571 case LyXLength::PW: // Always % of workarea
575 result = value * default_width / 100;
577 case LyXLength::UNIT_NONE:
578 result = 0; // this cannot happen
581 return static_cast<int>(result * val_sign + 0.5);
583 return 0; // never reached