]> git.lyx.org Git - lyx.git/blob - src/lengthcommon.cpp
Fix bug #8105: Crash when deleting math macro from the inside
[lyx.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 /// skip n characters of input
62 inline void lyx_advance(string & data, size_t n)
63 {
64         data.erase(0, n);
65 }
66
67
68 /// return true when the input is at the end
69 inline bool isEndOfData(string const & data)
70 {
71         return ltrim(data).empty();
72 }
73
74
75 /**
76  * nextToken -  return the next token in the input
77  * @param data input string
78  * @param number_index the current position in the number array
79  * @param unit_index the current position in the unit array
80  * @return a char representing the type of token returned
81  *
82  * The possible return values are :
83  *      +       stretch indicator for glue length
84  *      -       shrink indicator for glue length
85  *      n       a numeric value (stored in number array)
86  *      u       a unit type (stored in unit array)
87  *      E       parse error
88  */
89 char nextToken(string & data, double * number, int & number_index,
90                Length::UNIT * unit, int & unit_index)
91 {
92         data = ltrim(data);
93
94         if (data.empty())
95                 return '\0';
96
97         if (data[0] == '+') {
98                 lyx_advance(data, 1);
99                 return '+';
100         }
101
102         if (prefixIs(data, "plus")) {
103                 lyx_advance(data, 4);
104                 return '+';
105         }
106
107         if (data[0] == '-') {
108                 lyx_advance(data, 1);
109                 return '-';
110         }
111
112         if (prefixIs(data, "minus")) {
113                 lyx_advance(data, 5);
114                 return '-';
115         }
116
117         size_t i = data.find_first_not_of("0123456789.");
118
119         if (i != 0) {
120                 if (number_index > 3)
121                         return 'E';
122
123                 string buffer;
124
125                 // we have found some number
126                 if (i == string::npos) {
127                         buffer = data;
128                         i = data.size() + 1;
129                 } else {
130                         buffer = data.substr(0, i);
131                 }
132
133                 lyx_advance(data, i);
134
135                 if (isStrDbl(buffer)) {
136                         number[number_index] = convert<double>(buffer);
137                         ++number_index;
138                         return 'n';
139                 }
140                 return 'E';
141         }
142
143         i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
144         if (i != 0) {
145                 if (unit_index > 3)
146                         return 'E';
147
148                 string buffer;
149
150                 // we have found some alphabetical string
151                 if (i == string::npos) {
152                         buffer = data;
153                         i = data.size() + 1;
154                 } else {
155                         buffer = data.substr(0, i);
156                 }
157
158                 // possibly we have "mmplus" string or similar
159                 if (buffer.size() > 5 &&
160                                 (buffer.substr(2, 4) == string("plus") ||
161                                  buffer.substr(2, 5) == string("minus")))
162                 {
163                         lyx_advance(data, 2);
164                         unit[unit_index] = unitFromString(buffer.substr(0, 2));
165                 } else {
166                         lyx_advance(data, i);
167                         unit[unit_index] = unitFromString(buffer);
168                 }
169
170                 if (unit[unit_index] != Length::UNIT_NONE) {
171                         ++unit_index;
172                         return 'u';
173                 }
174                 return 'E';  // Error
175         }
176         return 'E';  // Error
177 }
178
179
180 /// latex representation of a vspace
181 struct LaTeXLength {
182         char const * pattern;
183         int  plus_val_index;
184         int  minus_val_index;
185         int  plus_uni_index;
186         int  minus_uni_index;
187 };
188
189
190 /// the possible formats for a vspace string
191 LaTeXLength table[] = {
192         { "nu",       0, 0, 0, 0 },
193         { "nu+nu",    2, 0, 2, 0 },
194         { "nu+nu-nu", 2, 3, 2, 3 },
195         { "nu+-nu",   2, 2, 2, 2 },
196         { "nu-nu",    0, 2, 0, 2 },
197         { "nu-nu+nu", 3, 2, 3, 2 },
198         { "nu-+nu",   2, 2, 2, 2 },
199         { "n+nu",     2, 0, 1, 0 },
200         { "n+n-nu",   2, 3, 1, 1 },
201         { "n+-nu",    2, 2, 1, 1 },
202         { "n-nu",     0, 2, 0, 1 },
203         { "n-n+nu",   3, 2, 1, 1 },
204         { "n-+nu",    2, 2, 1, 1 },
205         { "",         0, 0, 0, 0 }   // sentinel, must be empty
206 };
207
208
209 } // namespace anon
210
211 const char * stringFromUnit(int unit)
212 {
213         if (unit < 0 || unit > num_units)
214                 return 0;
215         return unit_name[unit];
216 }
217
218
219 bool isValidGlueLength(string const & data, GlueLength * result)
220 {
221         // This parser is table-driven.  First, it constructs a "pattern"
222         // that describes the sequence of tokens in "data".  For example,
223         // "n-nu" means: number, minus sign, number, unit.  As we go along,
224         // numbers and units are stored into static arrays.  Then, "pattern"
225         // is searched in the "table".  If it is found, the associated
226         // table entries tell us which number and unit should go where
227         // in the Length structure.  Example: if "data" has the "pattern"
228         // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
229         // That means, "plus_val" is the second number that was seen
230         // in the input, "minus_val" is the third number, and "plus_uni"
231         // and "minus_uni" are the second and third units, respectively.
232         // ("val" and "uni" are always the first items seen in "data".)
233         // This is the most elegant solution I could find -- a straight-
234         // forward approach leads to very long, tedious code that would be
235         // much harder to understand and maintain. (AS)
236
237         if (data.empty())
238                 return true;
239         string buffer = ltrim(data);
240
241         // To make isValidGlueLength recognize negative values as
242         // the first number this little hack is needed:
243         int val_sign = 1; // positive as default
244         switch (buffer[0]) {
245         case '-':
246                 lyx_advance(buffer, 1);
247                 val_sign = -1;
248                 break;
249         case '+':
250                 lyx_advance(buffer, 1);
251                 break;
252         default:
253                 break;
254         }
255         // end of hack
256
257         // used to return numeric values in parsing vspace
258         double number[4] = { 0, 0, 0, 0 };
259         // used to return unit types in parsing vspace
260         Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
261                                 Length::UNIT_NONE, Length::UNIT_NONE};
262         int number_index = 1; // entries at index 0 are sentinels
263         int unit_index = 1;   // entries at index 0 are sentinels
264
265         // construct "pattern" from "data"
266         size_t const pattern_max_size = 20;
267         string pattern;
268         while (!isEndOfData(buffer)) {
269                 if (pattern.size() > pattern_max_size)
270                         return false;
271                 char const c = nextToken(buffer, number, number_index, unit,
272                                 unit_index);
273                 if (c == 'E')
274                         return false;
275                 pattern.push_back(c);
276         }
277
278         // search "pattern" in "table"
279         size_t table_index = 0;
280         while (pattern != table[table_index].pattern) {
281                 ++table_index;
282                 if (!*table[table_index].pattern)
283                         return false;
284         }
285
286         // Get the values from the appropriate places.  If an index
287         // is zero, the corresponding array value is zero or UNIT_NONE,
288         // so we needn't check this.
289         if (result) {
290                 result->len_.value  (number[1] * val_sign);
291                 result->len_.unit   (unit[1]);
292                 result->plus_.value (number[table[table_index].plus_val_index]);
293                 result->plus_.unit  (unit  [table[table_index].plus_uni_index]);
294                 result->minus_.value(number[table[table_index].minus_val_index]);
295                 result->minus_.unit (unit  [table[table_index].minus_uni_index]);
296         }
297         return true;
298 }
299
300
301 bool isValidLength(string const & data, Length * result)
302 {
303         // This is a trimmed down version of isValidGlueLength.
304         // The parser may seem overkill for lengths without
305         // glue, but since we already have it, using it is
306         // easier than writing something from scratch.
307         if (data.empty())
308                 return true;
309
310         string   buffer = data;
311         int      pattern_index = 0;
312         char     pattern[3];
313
314         // To make isValidLength recognize negative values
315         // this little hack is needed:
316         int val_sign = 1; // positive as default
317         switch (buffer[0]) {
318         case '-':
319                 lyx_advance(buffer, 1);
320                 val_sign = -1;
321                 break;
322         case '+':
323                 lyx_advance(buffer, 1);
324                 // fall through
325         default:
326                 // no action
327                 break;
328         }
329         // end of hack
330
331         // used to return numeric values in parsing vspace
332         double number[4] = { 0, 0, 0, 0 };
333         // used to return unit types in parsing vspace
334         Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
335                                 Length::UNIT_NONE, Length::UNIT_NONE};
336         int number_index = 1; // entries at index 0 are sentinels
337         int 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, number,
344                                 number_index, unit, unit_index);
345                 if (pattern[pattern_index] == 'E')
346                         return false;
347                 ++pattern_index;
348         }
349         pattern[pattern_index] = '\0';
350
351         // only the most basic pattern is accepted here
352         if (strcmp(pattern, "nu") != 0)
353                 return false;
354
355         // It _was_ a correct length string.
356         // Store away the values we found.
357         if (result) {
358                 result->val_  = number[1] * val_sign;
359                 result->unit_ = unit[1];
360         }
361         return true;
362 }
363
364 } // namespace lyx