GNU Octave  9.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-2024 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 
60  OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (text_element)
61 
62  virtual ~text_element () = default;
63 
64  virtual void accept (text_processor& p) = 0;
65 };
66 
67 class
68 OCTINTERP_API
70 {
71 public:
72 
73  text_element_string (const std::string& s = "")
74  : text_element (), m_str (s) { }
75 
76  OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_string)
77 
78  std::string string_value () const { return m_str; }
79 
80  void accept (text_processor& p);
81 
82 private:
83 
84  std::string m_str;
85 };
86 
87 class
88 OCTINTERP_API
90 {
91 public:
92 
93  enum { invalid_code = 0xFFFFFFFFU };
94 
95  text_element_symbol () = delete;
96 
98  : text_element (), m_symbol (sym) { }
99 
100  OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_symbol)
101 
102  int get_symbol () const { return m_symbol; }
103 
104  uint32_t get_symbol_code () const;
105 
106  void accept (text_processor& p);
107 
108 private:
109  int m_symbol;
110 };
111 
112 class
113 OCTINTERP_API
115  : public text_element, public base_list<text_element *>
116 {
117 public:
118 
119  OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (text_element_list)
120 
123  {
124  push_back (e);
125  }
126 
128  {
129  while (! empty ())
130  {
131  auto it = begin ();
132  delete (*it);
133  erase (it);
134  }
135  }
136 
137  void accept (text_processor& p);
138 };
139 
140 class
141 OCTINTERP_API
143 {
144 public:
145 
146  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (text_element_subscript)
147 
149  : text_element (), m_elem (e) { }
150 
152  : text_element ()
153  { m_elem = new text_element_string (std::string (1, c)); }
154 
156  { delete m_elem; }
157 
158  void accept (text_processor& p);
159 
160  text_element * get_element () { return m_elem; }
161 
162 private:
163 
164  text_element *m_elem;
165 };
166 
167 class
168 OCTINTERP_API
170 {
171 public:
172 
173  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (text_element_superscript)
174 
176  : text_element (), m_elem (e) { }
177 
179  : text_element ()
180  { m_elem = new text_element_string (std::string (1, c)); }
181 
183  { delete m_elem; }
184 
185  void accept (text_processor& p);
186 
187  text_element * get_element () { return m_elem; }
188 
189 private:
190 
191  text_element *m_elem;
192 };
193 
194 class
195 OCTINTERP_API
197 {
198 public:
199 
200  OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE_DELETE (text_element_combined)
201 
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:
217 
219  {
223  oblique
224  };
225 
227 
229  : text_element (), m_style (st) { }
230 
231  OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_fontstyle)
232 
233  fontstyle get_fontstyle () const { return m_style; }
234 
235  void accept (text_processor& p);
236 
237 private:
238 
239  fontstyle m_style;
240 };
241 
242 class
243 OCTINTERP_API
245 {
246 public:
247 
249 
250  text_element_fontname (const std::string& fname)
251  : text_element (), m_name (fname) { }
252 
253  OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_fontname)
254 
255  const std::string& get_fontname () const { return m_name; }
256 
257  void accept (text_processor& p);
258 
259 private:
260 
261  std::string m_name;
262 };
263 
264 class
265 OCTINTERP_API
267 {
268 public:
269 
271 
272  text_element_fontsize (double fsize)
273  : text_element (), m_size (fsize) { }
274 
275  OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_fontsize)
276 
277  double get_fontsize () const { return m_size; }
278 
279  void accept (text_processor& p);
280 
281 private:
282 
283  double m_size;
284 };
285 
286 class
287 OCTINTERP_API
289 {
290 public:
291 
292  text_element_color () = delete;
293 
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  OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_color)
321 
322  Matrix get_color () { return m_rgb; }
323 
324  void accept (text_processor& p);
325 
326 private:
327 
328  Matrix m_rgb;
329 };
330 
331 class
332 OCTINTERP_API
334 {
335 public:
336 
337  virtual void visit (text_element_string&) { }
338 
339  virtual void visit (text_element_symbol&) { }
340 
341  virtual void visit (text_element_list& e)
342  {
343  for (auto& el_p : e)
344  {
345  el_p->accept (*this);
346  }
347  }
348 
349  virtual void visit (text_element_subscript& e)
350  { e.get_element ()->accept (*this); }
351 
352  virtual void visit (text_element_superscript& e)
353  { e.get_element ()->accept (*this); }
354 
355  virtual void visit (text_element_combined&) { }
356 
357  virtual void visit (text_element_fontstyle&) { }
358 
359  virtual void visit (text_element_fontname&) { }
360 
361  virtual void visit (text_element_fontsize&) { }
362 
363  virtual void visit (text_element_color&) { }
364 
365  virtual void reset () { }
366 
367 protected:
368 
369  OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (text_processor)
370 
371  virtual ~text_processor () = default;
372 };
373 
374 #define TEXT_ELEMENT_ACCEPT(cls) \
375  inline void \
376  cls::accept (text_processor& p) \
377  { \
378  p.visit (*this); \
379  }
380 
391 
392 class
393 OCTINTERP_API
395 {
396 public:
397 
398  OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (text_parser)
399 
400  virtual ~text_parser () = default;
401 
402  virtual text_element * parse (const std::string& s) = 0;
403 
404 public:
405  static text_element * parse (const std::string& s,
406  const caseless_str& interpreter);
407 };
408 
409 class
410 OCTINTERP_API
412 {
413 public:
414 
415  OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE_DELETE (text_parser_none)
416 
417  // FIXME: is it possible to use reference counting to manage the
418  // memory for the object returned by the text parser? That would be
419  // preferable to having to know when and where to delete the object it
420  // creates...
421 
422  text_element * parse (const std::string& s)
423  {
424  return new text_element_string (s);
425  }
426 };
427 
428 class
429 OCTINTERP_API
431 {
432 public:
433 
435  : text_parser (), m_scanner (nullptr), m_buffer_state (nullptr),
436  m_result (nullptr)
437  { }
438 
439  OCTAVE_DISABLE_COPY_MOVE (text_parser_tex)
440 
442  { destroy_lexer (); }
443 
444  text_element * parse (const std::string& s);
445 
446  void * get_scanner () { return m_scanner; }
447 
448  void set_parse_result (text_element *e) { m_result = e; }
449 
450  text_element * get_parse_result () { return m_result; }
451 
452 private:
453  bool init_lexer (const std::string& s);
454 
455  void destroy_lexer ();
456 
457  //--------
458 
459  void *m_scanner;
460 
461  void *m_buffer_state;
462 
463  text_element *m_result;
464 };
465 
466 inline text_element *
467 text_parser::parse (const std::string& s, const caseless_str& interpreter)
468 {
469  std::unique_ptr<text_parser> parser;
470 
471  if (interpreter.compare ("tex"))
472  parser.reset (new text_parser_tex ());
473  else
474  parser.reset (new text_parser_none ());
475 
476  return parser->parse (s);
477 }
478 
479 OCTAVE_END_NAMESPACE(octave)
480 
481 #endif
Definition: dMatrix.h:42
void reset()
Definition: oct-parse.cc:6723
Definition: parse.h:822
text_element_color(double r, double g, double b)
Definition: text-engine.h:294
text_element_color()=delete
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_fontname(const std::string &fname)
Definition: text-engine.h:250
text_element_fontname()=delete
text_element_fontsize(double fsize)
Definition: text-engine.h:272
text_element_fontsize()=delete
text_element_fontstyle()=delete
text_element_fontstyle(fontstyle st)
Definition: text-engine.h:228
text_element_string(const std::string &s="")
Definition: text-engine.h:73
text_element_subscript(char c)
Definition: text-engine.h:151
text_element * get_element()
Definition: text-engine.h:160
text_element * get_element()
Definition: text-engine.h:187
text_element_symbol()=delete
text_element_symbol(int sym)
Definition: text-engine.h:97
virtual void accept(text_processor &p)=0
void * get_scanner()
Definition: text-engine.h:446
void set_parse_result(text_element *e)
Definition: text-engine.h:448
text_element * get_parse_result()
Definition: text-engine.h:450
virtual text_element * parse(const std::string &s)=0
virtual void reset()
Definition: text-engine.h:365
virtual void visit(text_element_combined &)
Definition: text-engine.h:355
virtual void visit(text_element_string &)
Definition: text-engine.h:337
virtual void visit(text_element_fontsize &)
Definition: text-engine.h:361
virtual void visit(text_element_list &e)
Definition: text-engine.h:341
virtual void visit(text_element_superscript &e)
Definition: text-engine.h:352
virtual void visit(text_element_fontstyle &)
Definition: text-engine.h:357
virtual void visit(text_element_fontname &)
Definition: text-engine.h:359
virtual void visit(text_element_color &)
Definition: text-engine.h:363
virtual void visit(text_element_symbol &)
Definition: text-engine.h:339
virtual void visit(text_element_subscript &e)
Definition: text-engine.h:349
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
T * r
Definition: mx-inlines.cc:781
#define m_scanner
#define TEXT_ELEMENT_ACCEPT(cls)
Definition: text-engine.h:374
#define ASSIGN_COLOR(r, g, b)