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