GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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;
36 class text_element_list;
43 class text_element_color;
44 
45 class text_processor;
46 
47 class
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
65 {
66 public:
67  text_element_string (const std::string& s = "")
68  : text_element (), str (s) { }
69 
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:
81 };
82 
83 class
86 {
87 public:
88  enum { invalid_code = 0xFFFFFFFFU };
89 
90 public:
92  : text_element (), symbol (sym) { }
93 
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
110 {
111 public:
114 
117  { push_back (e); }
118 
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
135 {
136 public:
138  : text_element (), elem (e) { }
139 
141  : text_element ()
142  { elem = new text_element_string (std::string (1, c)); }
143 
145  { delete elem; }
146 
147  void accept (text_processor& p);
148 
149  text_element* get_element (void) { return elem; }
150 
151 private:
153 
154 private:
155  text_element_subscript (void);
156 };
157 
158 class
161 {
162 public:
164  : text_element (), elem (e) { }
165 
167  : text_element ()
168  { elem = new text_element_string (std::string (1, c)); }
169 
171  { delete elem; }
172 
173  void accept (text_processor& p);
174 
175  text_element* get_element (void) { return elem; }
176 
177 private:
179 
180 private:
182 };
183 
184 class
187 {
188 public:
190  : text_element_list (e) { }
191 
193  : text_element_list(e1)
194  { push_back (e2); }
195 
196  void accept (text_processor& p);
197 };
198 
199 class
202 {
203 public:
205  {
209  oblique
210  };
211 
213  : text_element (), style (st) { }
214 
216 
217  fontstyle get_fontstyle (void) const { return style; }
218 
219  void accept (text_processor& p);
220 
221 private:
223 
224 private:
225  text_element_fontstyle (void);
226 };
227 
228 class
231 {
232 public:
233  text_element_fontname (const std::string& fname)
234  : text_element (), name (fname) { }
235 
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
252 {
253 public:
254  text_element_fontsize (double fsize)
255  : text_element (), size (fsize) { }
256 
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
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 
302 
303  Matrix get_color (void) { return rgb; }
304 
305  void accept (text_processor& p);
306 
307 private:
309 };
310 
311 class
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 
368 
369 class
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
388 {
389 public:
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
408 {
409 public:
411  : text_parser (), scanner (0), buffer_state (0), result (0)
412  { }
413 
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 
434 
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