GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
text-engine.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2009-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_text_engine_h)
27 #define octave_text_engine_h 1
28 
29 #include "octave-config.h"
30 
31 #include <memory>
32 #include <string>
33 
34 #include "base-list.h"
35 #include "caseless-str.h"
36 #include "dMatrix.h"
37 
39 
40 class text_element;
43 class text_element_list;
50 class text_element_color;
51 
52 class text_processor;
53 
54 class
55 OCTINTERP_API
57 {
58 public:
59  text_element (void) { }
60 
61  virtual ~text_element (void) = default;
62 
63  virtual void accept (text_processor& p) = 0;
64 
65 private:
67 };
68 
69 class
70 OCTINTERP_API
72 {
73 public:
74  text_element_string (const std::string& s = "")
75  : text_element (), m_str (s) { }
76 
77  ~text_element_string (void) = default;
78 
79  std::string string_value (void) const { return m_str; }
80 
81  void accept (text_processor& p);
82 
83 private:
85 
86  //--------
87 
88  std::string m_str;
89 };
90 
91 class
92 OCTINTERP_API
94 {
95 public:
96  enum { invalid_code = 0xFFFFFFFFU };
97 
99  : text_element (), m_symbol (sym) { }
100 
101  ~text_element_symbol (void) = default;
102 
103  int get_symbol (void) const { return m_symbol; }
104 
105  uint32_t get_symbol_code (void) const;
106 
107  void accept (text_processor& p);
108 
109 private:
110  int m_symbol;
111 };
112 
113 class
114 OCTINTERP_API
116  : public text_element, public base_list<text_element *>
117 {
118 public:
120  : text_element (), base_list<text_element*> () { }
121 
124  {
125  push_back (e);
126  }
127 
129  {
130  while (! empty ())
131  {
132  auto it = begin ();
133  delete (*it);
134  erase (it);
135  }
136  }
137 
138  void accept (text_processor& p);
139 };
140 
141 class
142 OCTINTERP_API
144 {
145 public:
147  : text_element (), m_elem (e) { }
148 
150  : text_element ()
151  { m_elem = new text_element_string (std::string (1, c)); }
152 
154  { delete m_elem; }
155 
156  void accept (text_processor& p);
157 
158  text_element * get_element (void) { return m_elem; }
159 
160 private:
162 
163  //--------
164 
166 
167 };
168 
169 class
170 OCTINTERP_API
172 {
173 public:
175  : text_element (), m_elem (e) { }
176 
178  : text_element ()
179  { m_elem = new text_element_string (std::string (1, c)); }
180 
182  { delete m_elem; }
183 
184  void accept (text_processor& p);
185 
186  text_element * get_element (void) { return m_elem; }
187 
188 private:
190 
191  //--------
192 
194 
195 };
196 
197 class
198 OCTINTERP_API
200 {
201 public:
203  : text_element_list (e) { }
204 
206  : text_element_list(e1)
207  { push_back (e2); }
208 
209  void accept (text_processor& p);
210 };
211 
212 class
213 OCTINTERP_API
215 {
216 public:
218  {
222  oblique
223  };
224 
226  : text_element (), m_style (st) { }
227 
228  ~text_element_fontstyle (void) = default;
229 
230  fontstyle get_fontstyle (void) const { return m_style; }
231 
232  void accept (text_processor& p);
233 
234 private:
236 
237  //--------
238 
240 
241 };
242 
243 class
244 OCTINTERP_API
246 {
247 public:
248  text_element_fontname (const std::string& fname)
249  : text_element (), m_name (fname) { }
250 
251  ~text_element_fontname (void) = default;
252 
253  const std::string& get_fontname (void) const { return m_name; }
254 
255  void accept (text_processor& p);
256 
257 private:
259 
260  //--------
261 
262  std::string m_name;
263 
264 };
265 
266 class
267 OCTINTERP_API
269 {
270 public:
271  text_element_fontsize (double fsize)
272  : text_element (), m_size (fsize) { }
273 
274  ~text_element_fontsize (void) = default;
275 
276  double get_fontsize (void) const { return m_size; }
277 
278  void accept (text_processor& p);
279 
280 private:
282 
283  //--------
284 
285  double m_size;
286 
287 };
288 
289 class
290 OCTINTERP_API
292 {
293 public:
294  text_element_color (double r, double g, double b)
295  : text_element (), m_rgb (1, 3, 0.0)
296  {
297  m_rgb(0) = r;
298  m_rgb(1) = g;
299  m_rgb(2) = b;
300  }
301 
302  text_element_color (const std::string& cname)
303  : text_element (), m_rgb (1, 3, 0.0)
304  {
305 #define ASSIGN_COLOR(r,g,b) { m_rgb(0) = r; m_rgb(1) = g; m_rgb(2) = b; }
306  if (cname == "red") ASSIGN_COLOR(1, 0, 0)
307  else if (cname == "green") ASSIGN_COLOR(0, 1, 0)
308  else if (cname == "yellow") ASSIGN_COLOR(1, 1, 0)
309  else if (cname == "magenta") ASSIGN_COLOR(1, 0, 1)
310  else if (cname == "blue") ASSIGN_COLOR(0, 0, 1)
311  else if (cname == "black") ASSIGN_COLOR(0, 0, 0)
312  else if (cname == "white") ASSIGN_COLOR(1, 1, 1)
313  else if (cname == "gray") ASSIGN_COLOR(.5, .5, .5)
314  else if (cname == "darkGreen") ASSIGN_COLOR(0, .5, 0)
315  else if (cname == "orange") ASSIGN_COLOR(1, .65, 0)
316  else if (cname == "lightBlue") ASSIGN_COLOR(0.68, .85, .9)
317 #undef ASSIGN_COLOR
318  }
319 
320  ~text_element_color (void) = default;
321 
322  Matrix get_color (void) { return m_rgb; }
323 
324  void accept (text_processor& p);
325 
326 private:
328 };
329 
330 class
331 OCTINTERP_API
333 {
334 public:
335  virtual void visit (text_element_string&) { }
336 
337  virtual void visit (text_element_symbol&) { }
338 
339  virtual void visit (text_element_list& e)
340  {
341  for (auto& el_p : e)
342  {
343  el_p->accept (*this);
344  }
345  }
346 
347  virtual void visit (text_element_subscript& e)
348  { e.get_element ()->accept (*this); }
349 
350  virtual void visit (text_element_superscript& e)
351  { e.get_element ()->accept (*this); }
352 
353  virtual void visit (text_element_combined&) { }
354 
355  virtual void visit (text_element_fontstyle&) { }
356 
357  virtual void visit (text_element_fontname&) { }
358 
359  virtual void visit (text_element_fontsize&) { }
360 
361  virtual void visit (text_element_color&) { }
362 
363  virtual void reset (void) { }
364 
365 protected:
366  text_processor (void) { }
367 
368  virtual ~text_processor (void) = default;
369 };
370 
371 #define TEXT_ELEMENT_ACCEPT(cls) \
372  inline void \
373  cls::accept (text_processor& p) \
374  { \
375  p.visit (*this); \
376  }
377 
388 
389 class
390 OCTINTERP_API
392 {
393 public:
394  text_parser (void) { }
395 
396  virtual ~text_parser (void) = default;
397 
398  virtual text_element * parse (const std::string& s) = 0;
399 
400 public:
401  static text_element * parse (const std::string& s,
402  const caseless_str& interpreter);
403 };
404 
405 class
406 OCTINTERP_API
408 {
409 public:
411 
412  ~text_parser_none (void) = default;
413 
414  // FIXME: is it possible to use reference counting to manage the
415  // memory for the object returned by the text parser? That would be
416  // preferable to having to know when and where to delete the object it
417  // creates...
418 
419  text_element * parse (const std::string& s)
420  {
421  return new text_element_string (s);
422  }
423 };
424 
425 class
426 OCTINTERP_API
428 {
429 public:
431  : text_parser (), m_scanner (nullptr), m_buffer_state (nullptr),
432  m_result (nullptr)
433  { }
434 
436  { destroy_lexer (); }
437 
438  text_element * parse (const std::string& s);
439 
440  void * get_scanner (void) { return m_scanner; }
441 
442  void set_parse_result (text_element *e) { m_result = e; }
443 
444  text_element * get_parse_result (void) { return m_result; }
445 
446 private:
447  bool init_lexer (const std::string& s);
448 
449  void destroy_lexer (void);
450 
451  //--------
452 
453  void *m_scanner;
454 
456 
458 };
459 
460 inline text_element *
461 text_parser::parse (const std::string& s, const caseless_str& interpreter)
462 {
463  std::unique_ptr<text_parser> parser;
464 
465  if (interpreter.compare ("tex"))
466  parser.reset (new text_parser_tex ());
467  else
468  parser.reset (new text_parser_none ());
469 
470  return parser->parse (s);
471 }
472 
474 
475 #endif
OCTAVE_END_NAMESPACE(octave)
Definition: dMatrix.h:42
OCTINTERP_API void reset(void)
Definition: oct-parse.cc:6747
Definition: parse.h:829
text_element_color(double r, double g, double b)
Definition: text-engine.h:294
~text_element_color(void)=default
Matrix get_color(void)
Definition: text-engine.h:322
text_element_color(const std::string &cname)
Definition: text-engine.h:302
text_element_combined(text_element *e1, text_element *e2)
Definition: text-engine.h:205
text_element_combined(text_element *e)
Definition: text-engine.h:202
text_element_fontname(const std::string &fname)
Definition: text-engine.h:248
~text_element_fontname(void)=default
const std::string & get_fontname(void) const
Definition: text-engine.h:253
~text_element_fontsize(void)=default
text_element_fontsize(double fsize)
Definition: text-engine.h:271
double get_fontsize(void) const
Definition: text-engine.h:276
fontstyle get_fontstyle(void) const
Definition: text-engine.h:230
~text_element_fontstyle(void)=default
text_element_fontstyle(fontstyle st)
Definition: text-engine.h:225
~text_element_list(void)
Definition: text-engine.h:128
text_element_list(text_element *e)
Definition: text-engine.h:122
~text_element_string(void)=default
text_element_string(const text_element_string &)
text_element_string(const std::string &s="")
Definition: text-engine.h:74
std::string m_str
Definition: text-engine.h:88
std::string string_value(void) const
Definition: text-engine.h:79
text_element_subscript(char c)
Definition: text-engine.h:149
text_element * m_elem
Definition: text-engine.h:165
text_element * get_element(void)
Definition: text-engine.h:158
text_element_subscript(text_element *e)
Definition: text-engine.h:146
text_element_superscript(text_element *e)
Definition: text-engine.h:174
text_element * get_element(void)
Definition: text-engine.h:186
text_element * m_elem
Definition: text-engine.h:193
int get_symbol(void) const
Definition: text-engine.h:103
text_element_symbol(int sym)
Definition: text-engine.h:98
~text_element_symbol(void)=default
text_element(const text_element &)
virtual void accept(text_processor &p)=0
virtual ~text_element(void)=default
text_element(void)
Definition: text-engine.h:59
text_element * parse(const std::string &s)
Definition: text-engine.h:419
~text_parser_none(void)=default
text_parser_none(void)
Definition: text-engine.h:410
text_element * m_result
Definition: text-engine.h:457
void * get_scanner(void)
Definition: text-engine.h:440
text_parser_tex(void)
Definition: text-engine.h:430
void set_parse_result(text_element *e)
Definition: text-engine.h:442
void * m_buffer_state
Definition: text-engine.h:455
~text_parser_tex(void)
Definition: text-engine.h:435
text_element * get_parse_result(void)
Definition: text-engine.h:444
virtual text_element * parse(const std::string &s)=0
text_parser(void)
Definition: text-engine.h:394
virtual ~text_parser(void)=default
virtual void visit(text_element_combined &)
Definition: text-engine.h:353
virtual void visit(text_element_string &)
Definition: text-engine.h:335
virtual ~text_processor(void)=default
virtual void reset(void)
Definition: text-engine.h:363
virtual void visit(text_element_fontsize &)
Definition: text-engine.h:359
text_processor(void)
Definition: text-engine.h:366
virtual void visit(text_element_list &e)
Definition: text-engine.h:339
virtual void visit(text_element_superscript &e)
Definition: text-engine.h:350
virtual void visit(text_element_fontstyle &)
Definition: text-engine.h:355
virtual void visit(text_element_fontname &)
Definition: text-engine.h:357
virtual void visit(text_element_color &)
Definition: text-engine.h:361
virtual void visit(text_element_symbol &)
Definition: text-engine.h:337
virtual void visit(text_element_subscript &e)
Definition: text-engine.h:347
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
T * r
Definition: mx-inlines.cc:773
#define m_scanner
#define TEXT_ELEMENT_ACCEPT(cls)
Definition: text-engine.h:371
#define ASSIGN_COLOR(r, g, b)