GNU Octave
3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Pages
libinterp
corefcn
txt-eng.h
Go to the documentation of this file.
1
/*
2
3
Copyright (C) 2009-2013 Michael Goffioul
4
5
This file is part of Octave.
6
7
Octave is free software; you can redistribute it and/or modify it
8
under the terms of the GNU General Public License as published by the
9
Free Software Foundation; either version 3 of the License, or (at your
10
option) any later version.
11
12
Octave is distributed in the hope that it will be useful, but WITHOUT
13
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15
for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with Octave; see the file COPYING. If not, see
19
<http://www.gnu.org/licenses/>.
20
21
*/
22
23
#if ! defined (txt_eng_h)
24
#define txt_eng_h 1
25
26
#include <memory>
27
#include <string>
28
29
#include "
base-list.h
"
30
#include "
caseless-str.h
"
31
#include "
dMatrix.h
"
32
33
class
text_element
;
34
class
text_element_string
;
35
class
text_element_symbol
;
36
class
text_element_list
;
37
class
text_element_subscript
;
38
class
text_element_superscript
;
39
class
text_element_combined
;
40
class
text_element_fontname
;
41
class
text_element_fontsize
;
42
class
text_element_fontstyle
;
43
class
text_element_color
;
44
45
class
text_processor
;
46
47
class
48
OCTINTERP_API
49
text_element
50
{
51
public
:
52
text_element
(
void
) { }
53
54
virtual
~text_element
(
void
) { }
55
56
virtual
void
accept (
text_processor
& p) = 0;
57
58
private
:
59
text_element
(
const
text_element
&);
60
};
61
62
class
63
OCTINTERP_API
64
text_element_string
:
public
text_element
65
{
66
public
:
67
text_element_string
(
const
std::string& s =
""
)
68
:
text_element
(), str (s) { }
69
70
~text_element_string
(
void
) { }
71
72
std::string
string_value
(
void
)
const
{
return
str; }
73
74
void
accept (
text_processor
& p);
75
76
private
:
77
std::string
str
;
78
79
private
:
80
text_element_string
(
const
text_element_string
&);
81
};
82
83
class
84
OCTINTERP_API
85
text_element_symbol
:
public
text_element
86
{
87
public
:
88
enum
{ invalid_code = 0xFFFFFFFFU };
89
90
public
:
91
text_element_symbol
(
int
sym)
92
:
text_element
(), symbol (sym) { }
93
94
~text_element_symbol
(
void
) { }
95
96
int
get_symbol
(
void
)
const
{
return
symbol; }
97
98
uint32_t get_symbol_code (
void
)
const
;
99
100
void
accept (
text_processor
& p);
101
102
private
:
103
int
symbol
;
104
};
105
106
class
107
OCTINTERP_API
108
text_element_list
109
:
public
text_element
,
public
octave_base_list<text_element *>
110
{
111
public
:
112
text_element_list
(
void
)
113
:
text_element
(),
octave_base_list
<
text_element
*> () { }
114
115
text_element_list
(
text_element
* e)
116
:
text_element
(),
octave_base_list
<
text_element
*> ()
117
{ push_back (e); }
118
119
~
text_element_list
(
void
)
120
{
121
while
(! empty ())
122
{
123
iterator
it = begin ();
124
delete
(*it);
125
erase (it);
126
}
127
}
128
129
void
accept (
text_processor
& p);
130
};
131
132
class
133
OCTINTERP_API
134
text_element_subscript
:
public
text_element
135
{
136
public
:
137
text_element_subscript
(
text_element
* e)
138
:
text_element
(),
elem
(e) { }
139
140
text_element_subscript
(
char
c)
141
:
text_element
()
142
{
elem
=
new
text_element_string
(std::string (1, c)); }
143
144
~
text_element_subscript
(
void
)
145
{
delete
elem
; }
146
147
void
accept (
text_processor
& p);
148
149
text_element
*
get_element
(
void
) {
return
elem
; }
150
151
private
:
152
text_element
*
elem
;
153
154
private
:
155
text_element_subscript
(
void
);
156
};
157
158
class
159
OCTINTERP_API
160
text_element_superscript
:
public
text_element
161
{
162
public
:
163
text_element_superscript
(
text_element
* e)
164
:
text_element
(),
elem
(e) { }
165
166
text_element_superscript
(
char
c)
167
:
text_element
()
168
{
elem
=
new
text_element_string
(std::string (1, c)); }
169
170
~
text_element_superscript
(
void
)
171
{
delete
elem
; }
172
173
void
accept (
text_processor
& p);
174
175
text_element
*
get_element
(
void
) {
return
elem
; }
176
177
private
:
178
text_element
*
elem
;
179
180
private
:
181
text_element_superscript
(
void
);
182
};
183
184
class
185
OCTINTERP_API
186
text_element_combined
:
public
text_element_list
187
{
188
public
:
189
text_element_combined
(
text_element
* e)
190
:
text_element_list
(e) { }
191
192
text_element_combined
(
text_element
* e1,
text_element
* e2)
193
:
text_element_list
(e1)
194
{ push_back (e2); }
195
196
void
accept (
text_processor
& p);
197
};
198
199
class
200
OCTINTERP_API
201
text_element_fontstyle
:
public
text_element
202
{
203
public
:
204
enum
fontstyle
205
{
206
normal
,
207
bold
,
208
italic
,
209
oblique
210
};
211
212
text_element_fontstyle
(
fontstyle
st)
213
:
text_element
(), style (st) { }
214
215
~text_element_fontstyle
(
void
) { }
216
217
fontstyle
get_fontstyle
(
void
)
const
{
return
style; }
218
219
void
accept (
text_processor
& p);
220
221
private
:
222
fontstyle
style
;
223
224
private
:
225
text_element_fontstyle
(
void
);
226
};
227
228
class
229
OCTINTERP_API
230
text_element_fontname
:
public
text_element
231
{
232
public
:
233
text_element_fontname
(
const
std::string& fname)
234
:
text_element
(), name (fname) { }
235
236
~text_element_fontname
(
void
) { }
237
238
const
std::string&
get_fontname
(
void
)
const
{
return
name; }
239
240
void
accept (
text_processor
& p);
241
242
private
:
243
std::string
name
;
244
245
private
:
246
text_element_fontname
(
void
);
247
};
248
249
class
250
OCTINTERP_API
251
text_element_fontsize
:
public
text_element
252
{
253
public
:
254
text_element_fontsize
(
double
fsize)
255
:
text_element
(),
size
(fsize) { }
256
257
~text_element_fontsize
(
void
) { }
258
259
double
get_fontsize
(
void
)
const
{
return
size
; }
260
261
void
accept (
text_processor
& p);
262
263
private
:
264
double
size
;
265
266
private
:
267
text_element_fontsize
(
void
);
268
};
269
270
class
271
OCTINTERP_API
272
text_element_color
:
public
text_element
273
{
274
public
:
275
text_element_color
(
double
r,
double
g,
double
b)
276
:
text_element
(), rgb (1, 3, 0.0)
277
{
278
rgb(0) = r;
279
rgb(1) = g;
280
rgb(2) = b;
281
}
282
283
text_element_color
(
const
std::string& cname)
284
:
text_element
(), rgb (1, 3, 0.0)
285
{
286
#define ASSIGN_COLOR(r,g,b) { rgb(0) = r; rgb(1) = g; rgb(2) = b; }
287
if
(cname ==
"red"
)
ASSIGN_COLOR
(1, 0, 0)
288
else
if
(cname ==
"green"
)
ASSIGN_COLOR
(0, 1, 0)
289
else
if
(cname ==
"yellow"
)
ASSIGN_COLOR
(1, 1, 0)
290
else
if
(cname ==
"magenta"
)
ASSIGN_COLOR
(1, 0, 1)
291
else
if
(cname ==
"blue"
)
ASSIGN_COLOR
(0, 0, 1)
292
else
if
(cname ==
"black"
)
ASSIGN_COLOR
(0, 0, 0)
293
else
if
(cname ==
"white"
)
ASSIGN_COLOR
(1, 1, 1)
294
else
if
(cname ==
"gray"
)
ASSIGN_COLOR
(.5, .5, .5)
295
else
if
(cname ==
"darkGreen"
)
ASSIGN_COLOR
(0, .5, 0)
296
else
if
(cname ==
"orange"
)
ASSIGN_COLOR
(1, .65, 0)
297
else
if
(cname ==
"lightBlue"
)
ASSIGN_COLOR
(0.68, .85, .9)
298
#undef ASSIGN_COLOR
299
}
300
301
~text_element_color
(
void
) { }
302
303
Matrix
get_color
(
void
) {
return
rgb; }
304
305
void
accept (
text_processor
& p);
306
307
private
:
308
Matrix
rgb
;
309
};
310
311
class
312
OCTINTERP_API
313
text_processor
314
{
315
public
:
316
virtual
void
visit
(
text_element_string
& e) = 0;
317
318
virtual
void
visit
(
text_element_symbol
&) { }
319
320
virtual
void
visit
(
text_element_list
& e)
321
{
322
for
(
text_element_list::iterator
it = e.
begin
();
323
it != e.
end
(); ++it)
324
{
325
(*it)->accept (*
this
);
326
}
327
}
328
329
virtual
void
visit
(
text_element_subscript
& e)
330
{ e.
get_element
()->
accept
(*
this
); }
331
332
virtual
void
visit
(
text_element_superscript
& e)
333
{ e.
get_element
()->
accept
(*
this
); }
334
335
virtual
void
visit
(
text_element_combined
&) { }
336
337
virtual
void
visit
(
text_element_fontstyle
&) { }
338
339
virtual
void
visit
(
text_element_fontname
&) { }
340
341
virtual
void
visit
(
text_element_fontsize
&) { }
342
343
virtual
void
visit
(
text_element_color
&) { }
344
345
virtual
void
reset
(
void
) { }
346
347
protected
:
348
text_processor
(
void
) { }
349
350
virtual
~text_processor
(
void
) { }
351
};
352
353
#define TEXT_ELEMENT_ACCEPT(cls) \
354
inline void \
355
cls::accept (text_processor& p) \
356
{ p.visit (*this); }
357
358
TEXT_ELEMENT_ACCEPT
(
text_element_string
)
359
TEXT_ELEMENT_ACCEPT
(
text_element_symbol
)
360
TEXT_ELEMENT_ACCEPT
(
text_element_list
)
361
TEXT_ELEMENT_ACCEPT
(
text_element_subscript
)
362
TEXT_ELEMENT_ACCEPT
(
text_element_superscript
)
363
TEXT_ELEMENT_ACCEPT
(
text_element_combined
)
364
TEXT_ELEMENT_ACCEPT
(
text_element_fontstyle
)
365
TEXT_ELEMENT_ACCEPT
(
text_element_fontname
)
366
TEXT_ELEMENT_ACCEPT
(
text_element_fontsize
)
367
TEXT_ELEMENT_ACCEPT
(
text_element_color
)
368
369
class
370
OCTINTERP_API
371
text_parser
372
{
373
public
:
374
text_parser
(
void
) { }
375
376
virtual
~text_parser
(
void
) { }
377
378
virtual
text_element
* parse (
const
std::string& s) = 0;
379
380
public
:
381
static
text_element
* parse (
const
std::string& s,
382
const
caseless_str
& interpreter);
383
};
384
385
class
386
OCTINTERP_API
387
text_parser_none
:
public
text_parser
388
{
389
public
:
390
text_parser_none
(
void
) :
text_parser
() { }
391
392
~text_parser_none
(
void
) { }
393
394
// FIXME: is it possible to use reference counting to manage the
395
// memory for the object returned by the text parser? That would be
396
// preferable to having to know when and where to delete the object it
397
// creates...
398
399
text_element
* parse (
const
std::string& s)
400
{
401
return
new
text_element_string
(s);
402
}
403
};
404
405
class
406
OCTINTERP_API
407
text_parser_tex
:
public
text_parser
408
{
409
public
:
410
text_parser_tex
(
void
)
411
:
text_parser
(),
scanner
(0), buffer_state (0), result (0)
412
{ }
413
414
~
text_parser_tex
(
void
)
415
{ destroy_lexer (); }
416
417
text_element
* parse (
const
std::string& s);
418
419
void
*
get_scanner
(
void
) {
return
scanner
; }
420
421
void
set_parse_result
(
text_element
* e) { result = e; }
422
423
text_element
*
get_parse_result
(
void
) {
return
result; }
424
425
private
:
426
bool
init_lexer (
const
std::string& s);
427
428
void
destroy_lexer (
void
);
429
430
private
:
431
void
*
scanner
;
432
433
void
*
buffer_state
;
434
435
text_element
*
result
;
436
};
437
438
inline
text_element
*
439
text_parser::parse
(
const
std::string& s,
const
caseless_str
& interpreter)
440
{
441
std::auto_ptr<text_parser>
parser
;
442
443
if
(interpreter.
compare
(
"tex"
))
444
parser.reset (
new
text_parser_tex
());
445
else
446
parser.reset (
new
text_parser_none
());
447
448
return
parser->
parse
(s);
449
}
450
451
#endif
Generated on Mon Dec 30 2013 03:04:30 for GNU Octave by
1.8.1.2