GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
txt-eng.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_txt_eng_h)
24 #define octave_txt_eng_h 1
25 
26 #include "octave-config.h"
27 
28 #include <memory>
29 #include <string>
30 
31 #include "base-list.h"
32 #include "caseless-str.h"
33 #include "dMatrix.h"
34 
35 class text_element;
38 class text_element_list;
45 class text_element_color;
46 
47 class text_processor;
48 
49 class
50 OCTINTERP_API
52 {
53 public:
54  text_element (void) { }
55 
56  virtual ~text_element (void) = default;
57 
58  virtual void accept (text_processor& p) = 0;
59 
60 private:
61  text_element (const text_element&);
62 };
63 
64 class
65 OCTINTERP_API
67 {
68 public:
70  : text_element (), str (s) { }
71 
72  ~text_element_string (void) = default;
73 
74  std::string string_value (void) const { return str; }
75 
76  void accept (text_processor& p);
77 
78 private:
80 
81 private:
83 };
84 
85 class
86 OCTINTERP_API
88 {
89 public:
90  enum { invalid_code = 0xFFFFFFFFU };
91 
92 public:
94  : text_element (), symbol (sym) { }
95 
96  ~text_element_symbol (void) = default;
97 
98  int get_symbol (void) const { return symbol; }
99 
100  uint32_t get_symbol_code (void) const;
101 
102  void accept (text_processor& p);
103 
104 private:
105  int symbol;
106 };
107 
108 class
109 OCTINTERP_API
112 {
113 public:
115  : text_element (), octave::base_list<text_element*> () { }
116 
118  : text_element (), octave::base_list<text_element*> ()
119  { push_back (e); }
120 
122  {
123  while (! empty ())
124  {
125  iterator it = begin ();
126  delete (*it);
127  erase (it);
128  }
129  }
130 
131  void accept (text_processor& p);
132 };
133 
134 class
135 OCTINTERP_API
137 {
138 public:
140  : text_element (), elem (e) { }
141 
143  : text_element ()
144  { elem = new text_element_string (std::string (1, c)); }
145 
147  { delete elem; }
148 
149  void accept (text_processor& p);
150 
151  text_element * get_element (void) { return elem; }
152 
153 private:
155 
156 private:
157  text_element_subscript (void);
158 };
159 
160 class
161 OCTINTERP_API
163 {
164 public:
166  : text_element (), elem (e) { }
167 
169  : text_element ()
170  { elem = new text_element_string (std::string (1, c)); }
171 
173  { delete elem; }
174 
175  void accept (text_processor& p);
176 
177  text_element * get_element (void) { return elem; }
178 
179 private:
181 
182 private:
184 };
185 
186 class
187 OCTINTERP_API
189 {
190 public:
192  : text_element_list (e) { }
193 
195  : text_element_list(e1)
196  { push_back (e2); }
197 
198  void accept (text_processor& p);
199 };
200 
201 class
202 OCTINTERP_API
204 {
205 public:
207  {
211  oblique
212  };
213 
215  : text_element (), style (st) { }
216 
217  ~text_element_fontstyle (void) = default;
218 
219  fontstyle get_fontstyle (void) const { return style; }
220 
221  void accept (text_processor& p);
222 
223 private:
225 
226 private:
227  text_element_fontstyle (void);
228 };
229 
230 class
231 OCTINTERP_API
233 {
234 public:
236  : text_element (), name (fname) { }
237 
238  ~text_element_fontname (void) = default;
239 
240  const std::string& get_fontname (void) const { return name; }
241 
242  void accept (text_processor& p);
243 
244 private:
246 
247 private:
248  text_element_fontname (void);
249 };
250 
251 class
252 OCTINTERP_API
254 {
255 public:
256  text_element_fontsize (double fsize)
257  : text_element (), size (fsize) { }
258 
259  ~text_element_fontsize (void) = default;
260 
261  double get_fontsize (void) const { return size; }
262 
263  void accept (text_processor& p);
264 
265 private:
266  double size;
267 
268 private:
269  text_element_fontsize (void);
270 };
271 
272 class
273 OCTINTERP_API
275 {
276 public:
277  text_element_color (double r, double g, double b)
278  : text_element (), rgb (1, 3, 0.0)
279  {
280  rgb(0) = r;
281  rgb(1) = g;
282  rgb(2) = b;
283  }
284 
286  : text_element (), rgb (1, 3, 0.0)
287  {
288 #define ASSIGN_COLOR(r,g,b) { rgb(0) = r; rgb(1) = g; rgb(2) = b; }
289  if (cname == "red") ASSIGN_COLOR(1, 0, 0)
290  else if (cname == "green") ASSIGN_COLOR(0, 1, 0)
291  else if (cname == "yellow") ASSIGN_COLOR(1, 1, 0)
292  else if (cname == "magenta") ASSIGN_COLOR(1, 0, 1)
293  else if (cname == "blue") ASSIGN_COLOR(0, 0, 1)
294  else if (cname == "black") ASSIGN_COLOR(0, 0, 0)
295  else if (cname == "white") ASSIGN_COLOR(1, 1, 1)
296  else if (cname == "gray") ASSIGN_COLOR(.5, .5, .5)
297  else if (cname == "darkGreen") ASSIGN_COLOR(0, .5, 0)
298  else if (cname == "orange") ASSIGN_COLOR(1, .65, 0)
299  else if (cname == "lightBlue") ASSIGN_COLOR(0.68, .85, .9)
300 #undef ASSIGN_COLOR
301  }
302 
303  ~text_element_color (void) = default;
304 
305  Matrix get_color (void) { return rgb; }
306 
307  void accept (text_processor& p);
308 
309 private:
311 };
312 
313 class
314 OCTINTERP_API
316 {
317 public:
318  virtual void visit (text_element_string& e) = 0;
319 
320  virtual void visit (text_element_symbol&) { }
321 
322  virtual void visit (text_element_list& e)
323  {
324  for (auto& el_p : e)
325  {
326  el_p->accept (*this);
327  }
328  }
329 
331  { e.get_element ()->accept (*this); }
332 
334  { e.get_element ()->accept (*this); }
335 
336  virtual void visit (text_element_combined&) { }
337 
338  virtual void visit (text_element_fontstyle&) { }
339 
340  virtual void visit (text_element_fontname&) { }
341 
342  virtual void visit (text_element_fontsize&) { }
343 
344  virtual void visit (text_element_color&) { }
345 
346  virtual void reset (void) { }
347 
348 protected:
349  text_processor (void) { }
350 
351  virtual ~text_processor (void) = default;
352 };
353 
354 #define TEXT_ELEMENT_ACCEPT(cls) \
355  inline void \
356  cls::accept (text_processor& p) \
357  { \
358  p.visit (*this); \
359  }
360 
371 
372 class
373 OCTINTERP_API
375 {
376 public:
377  text_parser (void) { }
378 
379  virtual ~text_parser (void) = default;
380 
381  virtual text_element * parse (const std::string& s) = 0;
382 
383 public:
384  static text_element * parse (const std::string& s,
385  const caseless_str& interpreter);
386 };
387 
388 class
389 OCTINTERP_API
391 {
392 public:
394 
395  ~text_parser_none (void) = default;
396 
397  // FIXME: is it possible to use reference counting to manage the
398  // memory for the object returned by the text parser? That would be
399  // preferable to having to know when and where to delete the object it
400  // creates...
401 
403  {
404  return new text_element_string (s);
405  }
406 };
407 
408 class
409 OCTINTERP_API
411 {
412 public:
414  : text_parser (), scanner (nullptr), buffer_state (nullptr), result (nullptr)
415  { }
416 
418  { destroy_lexer (); }
419 
420  text_element * parse (const std::string& s);
421 
422  void * get_scanner (void) { return scanner; }
423 
425 
426  text_element * get_parse_result (void) { return result; }
427 
428 private:
429  bool init_lexer (const std::string& s);
430 
431  void destroy_lexer (void);
432 
433 private:
434  void *scanner;
435 
437 
439 };
440 
441 inline text_element*
442 text_parser::parse (const std::string& s, const caseless_str& interpreter)
443 {
444  std::unique_ptr<text_parser> parser;
445 
446  if (interpreter.compare ("tex"))
447  parser.reset (new text_parser_tex ());
448  else
449  parser.reset (new text_parser_none ());
450 
451  return parser->parse (s);
452 }
453 
454 #endif
double get_fontsize(void) const
Definition: txt-eng.h:261
text_element * get_element(void)
Definition: txt-eng.h:177
text_element_string(const std::string &s="")
Definition: txt-eng.h:69
bool compare(const std::string &s, size_t limit=std::string::npos) const
Definition: caseless-str.h:76
~text_element_superscript(void)
Definition: txt-eng.h:172
fname
Definition: load-save.cc:767
text_element_subscript(char c)
Definition: txt-eng.h:142
void * buffer_state
Definition: txt-eng.h:436
void * scanner
Definition: txt-eng.h:434
text_element_list(text_element *e)
Definition: txt-eng.h:117
virtual void visit(text_element_list &e)
Definition: txt-eng.h:322
void set_parse_result(text_element *e)
Definition: txt-eng.h:424
text_element_superscript(char c)
Definition: txt-eng.h:168
std::list< text_element * >::iterator iterator
Definition: base-list.h:40
~text_parser_tex(void)
Definition: txt-eng.h:417
text_processor(void)
Definition: txt-eng.h:349
virtual void visit(text_element_fontsize &)
Definition: txt-eng.h:342
virtual ~text_processor(void)=default
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
s
Definition: file-io.cc:2729
Matrix get_color(void)
Definition: txt-eng.h:305
i e
Definition: data.cc:2591
text_element_color(double r, double g, double b)
Definition: txt-eng.h:277
text_element * parse(const std::string &s)
Definition: txt-eng.h:402
text_element * get_parse_result(void)
Definition: txt-eng.h:426
text_element_fontstyle(fontstyle st)
Definition: txt-eng.h:214
~text_element_subscript(void)
Definition: txt-eng.h:146
text_element(void)
Definition: txt-eng.h:54
virtual void visit(text_element_superscript &e)
Definition: txt-eng.h:333
#define ASSIGN_COLOR(r, g, b)
text_element_fontname(const std::string &fname)
Definition: txt-eng.h:235
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are empty
Definition: ov-struct.cc:1736
nd deftypefn *std::string name
Definition: sysdep.cc:647
~text_element_list(void)
Definition: txt-eng.h:121
text_element_superscript(text_element *e)
Definition: txt-eng.h:165
virtual void reset(void)
Definition: txt-eng.h:346
std::string name
Definition: txt-eng.h:245
text_element * get_element(void)
Definition: txt-eng.h:151
fontstyle get_fontstyle(void) const
Definition: txt-eng.h:219
text_element_list(void)
Definition: txt-eng.h:114
virtual void visit(text_element_fontname &)
Definition: txt-eng.h:340
text_element_subscript(text_element *e)
Definition: txt-eng.h:139
std::string str
Definition: hash.cc:118
#define TEXT_ELEMENT_ACCEPT(cls)
Definition: txt-eng.h:354
static int elem
Definition: __contourc__.cc:47
virtual void visit(text_element_subscript &e)
Definition: txt-eng.h:330
text_parser_none(void)
Definition: txt-eng.h:393
virtual text_element * parse(const std::string &s)=0
virtual void visit(text_element_color &)
Definition: txt-eng.h:344
Definition: dMatrix.h:36
text_element_fontsize(double fsize)
Definition: txt-eng.h:256
text_element_combined(text_element *e1, text_element *e2)
Definition: txt-eng.h:194
text_element_symbol(int sym)
Definition: txt-eng.h:93
text_element * result
Definition: txt-eng.h:438
With real return the complex result
Definition: data.cc:3260
text_element_combined(text_element *e)
Definition: txt-eng.h:191
std::string str
Definition: txt-eng.h:79
const std::string & get_fontname(void) const
Definition: txt-eng.h:240
text_parser_tex(void)
Definition: txt-eng.h:413
virtual void visit(text_element_fontstyle &)
Definition: txt-eng.h:338
text_element * elem
Definition: txt-eng.h:154
void * get_scanner(void)
Definition: txt-eng.h:422
std::string string_value(void) const
Definition: txt-eng.h:74
int get_symbol(void) const
Definition: txt-eng.h:98
p
Definition: lu.cc:138
virtual void visit(text_element_symbol &)
Definition: txt-eng.h:320
b
Definition: cellfun.cc:400
virtual void visit(text_element_combined &)
Definition: txt-eng.h:336
virtual void visit(text_element_string &e)=0
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
text_parser(void)
Definition: txt-eng.h:377
text_element_color(const std::string &cname)
Definition: txt-eng.h:285
text_element * elem
Definition: txt-eng.h:180