]> git.lyx.org Git - features.git/blob - src/lengthcommon.cpp
do not hardcode packages loaded by external insets
[features.git] / src / 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 "Length.h"
16
17 #include "support/convert.h"
18 #include "support/gettext.h"
19 #include "support/lassert.h"
20 #include "support/lstrings.h"
21
22 #include <cstring>
23 #include <string>
24
25 using namespace std;
26 using namespace lyx::support;
27
28
29 namespace lyx {
30
31 // I am not sure if "mu" should be possible to select (Lgb)
32
33 // the latex units
34 char const * const unit_name[] = {
35         "bp", "cc", "cm", "dd", "em", "ex", "in", "mm", "mu",
36         "pc", "pt", "sp",
37         "text%",  "col%", "page%", "line%",
38         "theight%", "pheight%", "" };
39
40 int const num_units = int(sizeof(unit_name) / sizeof(unit_name[0]) - 1);
41
42 // the LyX gui units
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 %"), "" };
49
50 Length::UNIT unitFromString(string const & data)
51 {
52         int i = 0;
53         while (i < num_units && data != unit_name[i])
54                 ++i;
55         return static_cast<Length::UNIT>(i);
56 }
57
58
59 namespace {
60
61 /// used to return numeric values in parsing vspace
62 double number[4] = { 0, 0, 0, 0 };
63
64 /// used to return unit types in parsing vspace
65 Length::UNIT unit[4] = {
66         Length::UNIT_NONE,
67         Length::UNIT_NONE,
68         Length::UNIT_NONE,
69         Length::UNIT_NONE
70 };
71
72 /// the current position in the number array
73 int number_index;
74 /// the current position in the unit array
75 int unit_index;
76
77 /// skip n characters of input
78 inline void lyx_advance(string & data, size_t n)
79 {
80         data.erase(0, n);
81 }
82
83
84 /// return true when the input is at the end
85 inline bool isEndOfData(string const & data)
86 {
87         return ltrim(data).empty();
88 }
89
90
91 /**
92  * nextToken -  return the next token in the input
93  * @param data input string
94  * @return a char representing the type of token returned
95  *
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)
101  *      E       parse error
102  */
103 char nextToken(string & data)
104 {
105         data = ltrim(data);
106
107         if (data.empty())
108                 return '\0';
109
110         if (data[0] == '+') {
111                 lyx_advance(data, 1);
112                 return '+';
113         }
114
115         if (prefixIs(data, "plus")) {
116                 lyx_advance(data, 4);
117                 return '+';
118         }
119
120         if (data[0] == '-') {
121                 lyx_advance(data, 1);
122                 return '-';
123         }
124
125         if (prefixIs(data, "minus")) {
126                 lyx_advance(data, 5);
127                 return '-';
128         }
129
130         size_t i = data.find_first_not_of("0123456789.");
131
132         if (i != 0) {
133                 if (number_index > 3)
134                         return 'E';
135
136                 string buffer;
137
138                 // we have found some number
139                 if (i == string::npos) {
140                         buffer = data;
141                         i = data.size() + 1;
142                 } else {
143                         buffer = data.substr(0, i);
144                 }
145
146                 lyx_advance(data, i);
147
148                 if (isStrDbl(buffer)) {
149                         number[number_index] = convert<double>(buffer);
150                         ++number_index;
151                         return 'n';
152                 }
153                 return 'E';
154         }
155
156         i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
157         if (i != 0) {
158                 if (unit_index > 3)
159                         return 'E';
160
161                 string buffer;
162
163                 // we have found some alphabetical string
164                 if (i == string::npos) {
165                         buffer = data;
166                         i = data.size() + 1;
167                 } else {
168                         buffer = data.substr(0, i);
169                 }
170
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")))
175                 {
176                         lyx_advance(data, 2);
177                         unit[unit_index] = unitFromString(buffer.substr(0, 2));
178                 } else {
179                         lyx_advance(data, i);
180                         unit[unit_index] = unitFromString(buffer);
181                 }
182
183                 if (unit[unit_index] != Length::UNIT_NONE) {
184                         ++unit_index;
185                         return 'u';
186                 }
187                 return 'E';  // Error
188         }
189         return 'E';  // Error
190 }
191
192
193 /// latex representation of a vspace
194 struct LaTeXLength {
195         char const * pattern;
196         int  plus_val_index;
197         int  minus_val_index;
198         int  plus_uni_index;
199         int  minus_uni_index;
200 };
201
202
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
219 };
220
221
222 } // namespace anon
223
224 const char * stringFromUnit(int unit)
225 {
226         if (unit < 0 || unit > num_units)
227                 return 0;
228         return unit_name[unit];
229 }
230
231
232 bool isValidGlueLength(string const & data, GlueLength * result)
233 {
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)
249
250         if (data.empty())
251                 return true;
252         string buffer = ltrim(data);
253
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
257         switch (buffer[0]) {
258         case '-':
259                 lyx_advance(buffer, 1);
260                 val_sign = -1;
261                 break;
262         case '+':
263                 lyx_advance(buffer, 1);
264                 break;
265         default:
266                 break;
267         }
268         // end of hack
269
270         number_index = unit_index = 1;  // entries at index 0 are sentinels
271
272         // construct "pattern" from "data"
273         size_t const pattern_max_size = 20;
274         string pattern;
275         while (!isEndOfData(buffer)) {
276                 if (pattern.size() > pattern_max_size)
277                         return false;
278                 char const c = nextToken(buffer);
279                 if (c == 'E')
280                         return false;
281                 pattern.push_back(c);
282         }
283
284         // search "pattern" in "table"
285         size_t table_index = 0;
286         while (pattern != table[table_index].pattern) {
287                 ++table_index;
288                 if (!*table[table_index].pattern)
289                         return false;
290         }
291
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.
295         if (result) {
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]);
302         }
303         return true;
304 }
305
306
307 bool isValidLength(string const & data, Length * result)
308 {
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.
313         if (data.empty())
314                 return true;
315
316         string   buffer = data;
317         int      pattern_index = 0;
318         char     pattern[3];
319
320         // To make isValidLength recognize negative values
321         // this little hack is needed:
322         int val_sign = 1; // positive as default
323         switch (buffer[0]) {
324         case '-':
325                 lyx_advance(buffer, 1);
326                 val_sign = -1;
327                 break;
328         case '+':
329                 lyx_advance(buffer, 1);
330                 // fall through
331         default:
332                 // no action
333                 break;
334         }
335         // end of hack
336
337         number_index = unit_index = 1;  // entries at index 0 are sentinels
338
339         // construct "pattern" from "data"
340         while (!isEndOfData(buffer)) {
341                 if (pattern_index > 2)
342                         return false;
343                 pattern[pattern_index] = nextToken(buffer);
344                 if (pattern[pattern_index] == 'E')
345                         return false;
346                 ++pattern_index;
347         }
348         pattern[pattern_index] = '\0';
349
350         // only the most basic pattern is accepted here
351         if (strcmp(pattern, "nu") != 0)
352                 return false;
353
354         // It _was_ a correct length string.
355         // Store away the values we found.
356         if (result) {
357                 result->val_  = number[1] * val_sign;
358                 result->unit_ = unit[1];
359         }
360         return true;
361 }
362
363 } // namespace lyx