]> git.lyx.org Git - lyx.git/blob - src/support/lengthcommon.cpp
Correct comment
[lyx.git] / src / support / lengthcommon.cpp
1 /**
2  * \file lengthcommon.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Matthias Ettrich
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
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"
20
21 using namespace std;
22 using namespace lyx::support;
23
24
25 namespace lyx {
26
27 // I am not sure if "mu" should be possible to select (Lgb)
28
29 // the latex units
30 char const * const unit_name[] = {
31         "bp", "cc", "cm", "dd", "em", "ex", "in", "mm", "mu",
32         "pc", "pt", "sp",
33         "text%",  "col%", "page%", "line%",
34         "theight%", "pheight%", "baselineskip%", "" };
35
36 int const num_units = int(sizeof(unit_name) / sizeof(unit_name[0]) - 1);
37
38 // the LyX gui units
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 %"), "" };
45
46
47 Length::UNIT unitFromString(string const & data)
48 {
49         int i = 0;
50         while (i < num_units && data != unit_name[i])
51                 ++i;
52         return static_cast<Length::UNIT>(i);
53 }
54
55
56 namespace {
57
58 /// skip n characters of input
59 inline void lyx_advance(string & data, size_t n)
60 {
61         data.erase(0, n);
62 }
63
64
65 /// return true when the input is at the end
66 inline bool isEndOfData(string const & data)
67 {
68         return ltrim(data).empty();
69 }
70
71
72 /**
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
78  *
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)
84  *      E       parse error
85  */
86 char nextToken(string & data, double * number, int & number_index,
87                Length::UNIT * unit, int & unit_index)
88 {
89         data = ltrim(data);
90
91         if (data.empty())
92                 return '\0';
93
94         if (data[0] == '+') {
95                 lyx_advance(data, 1);
96                 return '+';
97         }
98
99         if (prefixIs(data, "plus")) {
100                 lyx_advance(data, 4);
101                 return '+';
102         }
103
104         if (data[0] == '-') {
105                 lyx_advance(data, 1);
106                 return '-';
107         }
108
109         if (prefixIs(data, "minus")) {
110                 lyx_advance(data, 5);
111                 return '-';
112         }
113
114         size_t i = data.find_first_not_of("0123456789.");
115
116         if (i != 0) {
117                 if (number_index > 3)
118                         return 'E';
119
120                 string buffer;
121
122                 // we have found some number
123                 if (i == string::npos) {
124                         buffer = data;
125                         i = data.size() + 1;
126                 } else {
127                         buffer = data.substr(0, i);
128                 }
129
130                 lyx_advance(data, i);
131
132                 if (isStrDbl(buffer)) {
133                         number[number_index] = convert<double>(buffer);
134                         ++number_index;
135                         return 'n';
136                 }
137                 return 'E';
138         }
139
140         i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
141         if (i != 0) {
142                 if (unit_index > 3)
143                         return 'E';
144
145                 string buffer;
146
147                 // we have found some alphabetical string
148                 if (i == string::npos) {
149                         buffer = data;
150                         i = data.size() + 1;
151                 } else {
152                         buffer = data.substr(0, i);
153                 }
154
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")))
159                 {
160                         lyx_advance(data, 2);
161                         unit[unit_index] = unitFromString(buffer.substr(0, 2));
162                 } else {
163                         lyx_advance(data, i);
164                         unit[unit_index] = unitFromString(buffer);
165                 }
166
167                 if (unit[unit_index] != Length::UNIT_NONE) {
168                         ++unit_index;
169                         return 'u';
170                 }
171                 return 'E';  // Error
172         }
173         return 'E';  // Error
174 }
175
176
177 /// latex representation of a vspace
178 struct LaTeXLength {
179         char const * pattern;
180         int  plus_val_index;
181         int  minus_val_index;
182         int  plus_uni_index;
183         int  minus_uni_index;
184 };
185
186
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
203 };
204
205
206 } // namespace
207
208
209 const char * stringFromUnit(int unit)
210 {
211         if (unit < 0 || unit > num_units)
212                 return nullptr;
213         return unit_name[unit];
214 }
215
216
217 bool isValidGlueLength(string const & data, GlueLength * result)
218 {
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)
234
235         if (data.empty()) {
236                 if (result)
237                         *result = GlueLength();
238                 return true;
239         }
240         string buffer = ltrim(data);
241
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
245         switch (buffer[0]) {
246         case '-':
247                 lyx_advance(buffer, 1);
248                 val_sign = -1;
249                 break;
250         case '+':
251                 lyx_advance(buffer, 1);
252                 break;
253         default:
254                 break;
255         }
256         // end of hack
257
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
265
266         // construct "pattern" from "data"
267         size_t const pattern_max_size = 20;
268         string pattern;
269         while (!isEndOfData(buffer)) {
270                 if (pattern.size() > pattern_max_size)
271                         return false;
272                 char const c = nextToken(buffer, number, number_index, unit,
273                                 unit_index);
274                 if (c == 'E')
275                         return false;
276                 pattern.push_back(c);
277         }
278
279         // search "pattern" in "table"
280         size_t table_index = 0;
281         while (pattern != table[table_index].pattern) {
282                 ++table_index;
283                 if (!*table[table_index].pattern)
284                         return false;
285         }
286
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.
290         if (result) {
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]);
297         }
298         return true;
299 }
300
301
302 bool isValidLength(string const & data, Length * result)
303 {
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.
308         if (data.empty()) {
309                 if (result)
310                         *result = Length();
311                 return true;
312         }
313
314         string buffer = data;
315
316         // To make isValidLength recognize negative values
317         // this little hack is needed:
318         int val_sign = 1; // positive as default
319         switch (buffer[0]) {
320         case '-':
321                 lyx_advance(buffer, 1);
322                 val_sign = -1;
323                 break;
324         case '+':
325                 lyx_advance(buffer, 1);
326                 // fall through
327         default:
328                 // no action
329                 break;
330         }
331         // end of hack
332
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
340
341         // construct "pattern" from "data"
342         string pattern;
343         while (!isEndOfData(buffer)) {
344                 if (pattern.size() > 2)
345                         return false;
346                 char const token = nextToken(buffer, number,
347                                 number_index, unit, unit_index);
348                 if (token == 'E')
349                         return false;
350                 pattern += token;
351         }
352
353         // only the most basic pattern is accepted here
354         if (pattern != "nu")
355                 return false;
356
357         // It _was_ a correct length string.
358         // Store away the values we found.
359         if (result) {
360                 result->val_  = number[1] * val_sign;
361                 result->unit_ = unit[1];
362         }
363         return true;
364 }
365
366 } // namespace lyx