2 * \file lengthcommon.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
10 * Full author contact details are available in file CREDITS.
15 #include "support/convert.h"
16 #include "support/gettext.h"
17 #include "support/lassert.h"
18 #include "support/Length.h"
19 #include "support/lstrings.h"
22 using namespace lyx::support;
27 // I am not sure if "mu" should be possible to select (Lgb)
30 char const * const unit_name[] = {
31 "bp", "cc", "cm", "dd", "em", "ex", "in", "mm", "mu",
33 "text%", "col%", "page%", "line%",
34 "theight%", "pheight%", "baselineskip%", "" };
36 int const num_units = int(sizeof(unit_name) / sizeof(unit_name[0]) - 1);
39 char const * const unit_name_gui[] = {
40 N_("bp"), N_("cc[[unit of measure]]"), N_("cm"), N_("dd"), N_("em"),
41 N_("ex"), N_("in[[unit of measure]]"), N_("mm"), N_("mu[[unit of measure]]"), N_("pc"),
42 N_("pt"), N_("sp"), N_("Text Width %"),
43 N_("Column Width %"), N_("Page Width %"), N_("Line Width %"),
44 N_("Text Height %"), N_("Page Height %"), N_("Line Distance %"), "" };
47 Length::UNIT unitFromString(string const & data)
50 while (i < num_units && data != unit_name[i])
52 return static_cast<Length::UNIT>(i);
58 /// skip n characters of input
59 inline void lyx_advance(string & data, size_t n)
65 /// return true when the input is at the end
66 inline bool isEndOfData(string const & data)
68 return ltrim(data).empty();
73 * nextToken - return the next token in the input
74 * @param data input string
75 * @param number_index the current position in the number array
76 * @param unit_index the current position in the unit array
77 * @return a char representing the type of token returned
79 * The possible return values are :
80 * + stretch indicator for glue length
81 * - shrink indicator for glue length
82 * n a numeric value (stored in number array)
83 * u a unit type (stored in unit array)
86 char nextToken(string & data, double * number, int & number_index,
87 Length::UNIT * unit, int & unit_index)
99 if (prefixIs(data, "plus")) {
100 lyx_advance(data, 4);
104 if (data[0] == '-') {
105 lyx_advance(data, 1);
109 if (prefixIs(data, "minus")) {
110 lyx_advance(data, 5);
114 size_t i = data.find_first_not_of("0123456789.");
117 if (number_index > 3)
122 // we have found some number
123 if (i == string::npos) {
127 buffer = data.substr(0, i);
130 lyx_advance(data, i);
132 if (isStrDbl(buffer)) {
133 number[number_index] = convert<double>(buffer);
140 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
147 // we have found some alphabetical string
148 if (i == string::npos) {
152 buffer = data.substr(0, i);
155 // possibly we have "mmplus" string or similar
156 if (buffer.size() > 5 &&
157 (buffer.substr(2, 4) == string("plus") ||
158 buffer.substr(2, 5) == string("minus")))
160 lyx_advance(data, 2);
161 unit[unit_index] = unitFromString(buffer.substr(0, 2));
163 lyx_advance(data, i);
164 unit[unit_index] = unitFromString(buffer);
167 if (unit[unit_index] != Length::UNIT_NONE) {
177 /// latex representation of a vspace
179 char const * pattern;
187 /// the possible formats for a vspace string
188 LaTeXLength table[] = {
189 { "nu", 0, 0, 0, 0 },
190 { "nu+nu", 2, 0, 2, 0 },
191 { "nu+nu-nu", 2, 3, 2, 3 },
192 { "nu+-nu", 2, 2, 2, 2 },
193 { "nu-nu", 0, 2, 0, 2 },
194 { "nu-nu+nu", 3, 2, 3, 2 },
195 { "nu-+nu", 2, 2, 2, 2 },
196 { "n+nu", 2, 0, 1, 0 },
197 { "n+n-nu", 2, 3, 1, 1 },
198 { "n+-nu", 2, 2, 1, 1 },
199 { "n-nu", 0, 2, 0, 1 },
200 { "n-n+nu", 3, 2, 1, 1 },
201 { "n-+nu", 2, 2, 1, 1 },
202 { "", 0, 0, 0, 0 } // sentinel, must be empty
209 const char * stringFromUnit(int unit)
211 if (unit < 0 || unit > num_units)
213 return unit_name[unit];
217 bool isValidGlueLength(string const & data, GlueLength * result)
219 // This parser is table-driven. First, it constructs a "pattern"
220 // that describes the sequence of tokens in "data". For example,
221 // "n-nu" means: number, minus sign, number, unit. As we go along,
222 // numbers and units are stored into static arrays. Then, "pattern"
223 // is searched in the "table". If it is found, the associated
224 // table entries tell us which number and unit should go where
225 // in the Length structure. Example: if "data" has the "pattern"
226 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
227 // That means, "plus_val" is the second number that was seen
228 // in the input, "minus_val" is the third number, and "plus_uni"
229 // and "minus_uni" are the second and third units, respectively.
230 // ("val" and "uni" are always the first items seen in "data".)
231 // This is the most elegant solution I could find -- a straight-
232 // forward approach leads to very long, tedious code that would be
233 // much harder to understand and maintain. (AS)
237 *result = GlueLength();
240 string buffer = ltrim(data);
242 // To make isValidGlueLength recognize negative values as
243 // the first number this little hack is needed:
244 int val_sign = 1; // positive as default
247 lyx_advance(buffer, 1);
251 lyx_advance(buffer, 1);
258 // used to return numeric values in parsing vspace
259 double number[4] = { 0, 0, 0, 0 };
260 // used to return unit types in parsing vspace
261 Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
262 Length::UNIT_NONE, Length::UNIT_NONE};
263 int number_index = 1; // entries at index 0 are sentinels
264 int unit_index = 1; // entries at index 0 are sentinels
266 // construct "pattern" from "data"
267 size_t const pattern_max_size = 20;
269 while (!isEndOfData(buffer)) {
270 if (pattern.size() > pattern_max_size)
272 char const c = nextToken(buffer, number, number_index, unit,
276 pattern.push_back(c);
279 // search "pattern" in "table"
280 size_t table_index = 0;
281 while (pattern != table[table_index].pattern) {
283 if (!*table[table_index].pattern)
287 // Get the values from the appropriate places. If an index
288 // is zero, the corresponding array value is zero or UNIT_NONE,
289 // so we needn't check this.
291 result->len_.value (number[1] * val_sign);
292 result->len_.unit (unit[1]);
293 result->plus_.value (number[table[table_index].plus_val_index]);
294 result->plus_.unit (unit [table[table_index].plus_uni_index]);
295 result->minus_.value(number[table[table_index].minus_val_index]);
296 result->minus_.unit (unit [table[table_index].minus_uni_index]);
302 bool isValidLength(string const & data, Length * result)
304 // This is a trimmed down version of isValidGlueLength.
305 // The parser may seem overkill for lengths without
306 // glue, but since we already have it, using it is
307 // easier than writing something from scratch.
314 string buffer = data;
316 // To make isValidLength recognize negative values
317 // this little hack is needed:
318 int val_sign = 1; // positive as default
321 lyx_advance(buffer, 1);
325 lyx_advance(buffer, 1);
333 // used to return numeric values in parsing vspace
334 double number[4] = { 0, 0, 0, 0 };
335 // used to return unit types in parsing vspace
336 Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
337 Length::UNIT_NONE, Length::UNIT_NONE};
338 int number_index = 1; // entries at index 0 are sentinels
339 int unit_index = 1; // entries at index 0 are sentinels
341 // construct "pattern" from "data"
343 while (!isEndOfData(buffer)) {
344 if (pattern.size() > 2)
346 char const token = nextToken(buffer, number,
347 number_index, unit, unit_index);
353 // only the most basic pattern is accepted here
357 // It _was_ a correct length string.
358 // Store away the values we found.
360 result->val_ = number[1] * val_sign;
361 result->unit_ = unit[1];