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.
17 #include "support/convert.h"
18 #include "support/gettext.h"
19 #include "support/lassert.h"
20 #include "support/lstrings.h"
26 using namespace lyx::support;
31 // I am not sure if "mu" should be possible to select (Lgb)
34 char const * const unit_name[] = {
35 "bp", "cc", "cm", "dd", "em", "ex", "in", "mm", "mu",
37 "text%", "col%", "page%", "line%",
38 "theight%", "pheight%", "" };
40 int const num_units = int(sizeof(unit_name) / sizeof(unit_name[0]) - 1);
43 char const * const unit_name_gui[] = {
44 N_("bp"), N_("cc[[unit of measure]]"), N_("cm"), N_("dd"), N_("em"),
45 N_("ex"), N_("in[[unit of measure]]"), N_("mm"), N_("mu[[unit of measure]]"), N_("pc"),
46 N_("pt"), N_("sp"), N_("Text Width %"),
47 N_("Column Width %"), N_("Page Width %"), N_("Line Width %"),
48 N_("Text Height %"), N_("Page Height %"), "" };
50 Length::UNIT unitFromString(string const & data)
53 while (i < num_units && data != unit_name[i])
55 return static_cast<Length::UNIT>(i);
61 /// used to return numeric values in parsing vspace
62 double number[4] = { 0, 0, 0, 0 };
64 /// used to return unit types in parsing vspace
65 Length::UNIT unit[4] = {
72 /// the current position in the number array
74 /// the current position in the unit array
77 /// skip n characters of input
78 inline void lyx_advance(string & data, size_t n)
84 /// return true when the input is at the end
85 inline bool isEndOfData(string const & data)
87 return ltrim(data).empty();
92 * nextToken - return the next token in the input
93 * @param data input string
94 * @return a char representing the type of token returned
96 * The possible return values are :
97 * + stretch indicator for glue length
98 * - shrink indicator for glue length
99 * n a numeric value (stored in number array)
100 * u a unit type (stored in unit array)
103 char nextToken(string & data)
110 if (data[0] == '+') {
111 lyx_advance(data, 1);
115 if (prefixIs(data, "plus")) {
116 lyx_advance(data, 4);
120 if (data[0] == '-') {
121 lyx_advance(data, 1);
125 if (prefixIs(data, "minus")) {
126 lyx_advance(data, 5);
130 size_t i = data.find_first_not_of("0123456789.");
133 if (number_index > 3)
138 // we have found some number
139 if (i == string::npos) {
143 buffer = data.substr(0, i);
146 lyx_advance(data, i);
148 if (isStrDbl(buffer)) {
149 number[number_index] = convert<double>(buffer);
156 i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
163 // we have found some alphabetical string
164 if (i == string::npos) {
168 buffer = data.substr(0, i);
171 // possibly we have "mmplus" string or similar
172 if (buffer.size() > 5 &&
173 (buffer.substr(2, 4) == string("plus") ||
174 buffer.substr(2, 5) == string("minus")))
176 lyx_advance(data, 2);
177 unit[unit_index] = unitFromString(buffer.substr(0, 2));
179 lyx_advance(data, i);
180 unit[unit_index] = unitFromString(buffer);
183 if (unit[unit_index] != Length::UNIT_NONE) {
193 /// latex representation of a vspace
195 char const * pattern;
203 /// the possible formats for a vspace string
204 LaTeXLength table[] = {
205 { "nu", 0, 0, 0, 0 },
206 { "nu+nu", 2, 0, 2, 0 },
207 { "nu+nu-nu", 2, 3, 2, 3 },
208 { "nu+-nu", 2, 2, 2, 2 },
209 { "nu-nu", 0, 2, 0, 2 },
210 { "nu-nu+nu", 3, 2, 3, 2 },
211 { "nu-+nu", 2, 2, 2, 2 },
212 { "n+nu", 2, 0, 1, 0 },
213 { "n+n-nu", 2, 3, 1, 1 },
214 { "n+-nu", 2, 2, 1, 1 },
215 { "n-nu", 0, 2, 0, 1 },
216 { "n-n+nu", 3, 2, 1, 1 },
217 { "n-+nu", 2, 2, 1, 1 },
218 { "", 0, 0, 0, 0 } // sentinel, must be empty
224 const char * stringFromUnit(int unit)
226 if (unit < 0 || unit > num_units)
228 return unit_name[unit];
232 bool isValidGlueLength(string const & data, GlueLength * result)
234 // This parser is table-driven. First, it constructs a "pattern"
235 // that describes the sequence of tokens in "data". For example,
236 // "n-nu" means: number, minus sign, number, unit. As we go along,
237 // numbers and units are stored into static arrays. Then, "pattern"
238 // is searched in the "table". If it is found, the associated
239 // table entries tell us which number and unit should go where
240 // in the Length structure. Example: if "data" has the "pattern"
241 // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
242 // That means, "plus_val" is the second number that was seen
243 // in the input, "minus_val" is the third number, and "plus_uni"
244 // and "minus_uni" are the second and third units, respectively.
245 // ("val" and "uni" are always the first items seen in "data".)
246 // This is the most elegant solution I could find -- a straight-
247 // forward approach leads to very long, tedious code that would be
248 // much harder to understand and maintain. (AS)
252 string buffer = ltrim(data);
254 // To make isValidGlueLength recognize negative values as
255 // the first number this little hack is needed:
256 int val_sign = 1; // positive as default
259 lyx_advance(buffer, 1);
263 lyx_advance(buffer, 1);
270 number_index = unit_index = 1; // entries at index 0 are sentinels
272 // construct "pattern" from "data"
273 size_t const pattern_max_size = 20;
275 while (!isEndOfData(buffer)) {
276 if (pattern.size() > pattern_max_size)
278 char const c = nextToken(buffer);
281 pattern.push_back(c);
284 // search "pattern" in "table"
285 size_t table_index = 0;
286 while (pattern != table[table_index].pattern) {
288 if (!*table[table_index].pattern)
292 // Get the values from the appropriate places. If an index
293 // is zero, the corresponding array value is zero or UNIT_NONE,
294 // so we needn't check this.
296 result->len_.value (number[1] * val_sign);
297 result->len_.unit (unit[1]);
298 result->plus_.value (number[table[table_index].plus_val_index]);
299 result->plus_.unit (unit [table[table_index].plus_uni_index]);
300 result->minus_.value(number[table[table_index].minus_val_index]);
301 result->minus_.unit (unit [table[table_index].minus_uni_index]);
307 bool isValidLength(string const & data, Length * result)
309 // This is a trimmed down version of isValidGlueLength.
310 // The parser may seem overkill for lengths without
311 // glue, but since we already have it, using it is
312 // easier than writing something from scratch.
316 string buffer = data;
317 int pattern_index = 0;
320 // To make isValidLength recognize negative values
321 // this little hack is needed:
322 int val_sign = 1; // positive as default
325 lyx_advance(buffer, 1);
329 lyx_advance(buffer, 1);
337 number_index = unit_index = 1; // entries at index 0 are sentinels
339 // construct "pattern" from "data"
340 while (!isEndOfData(buffer)) {
341 if (pattern_index > 2)
343 pattern[pattern_index] = nextToken(buffer);
344 if (pattern[pattern_index] == 'E')
348 pattern[pattern_index] = '\0';
350 // only the most basic pattern is accepted here
351 if (strcmp(pattern, "nu") != 0)
354 // It _was_ a correct length string.
355 // Store away the values we found.
357 result->val_ = number[1] * val_sign;
358 result->unit_ = unit[1];