GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
text-engine.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2009-2025 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 <list>
32#include <memory>
33#include <string>
34
35#include "caseless-str.h"
36#include "dMatrix.h"
37
39
40class text_element;
51
52class text_processor;
53
54class OCTINTERP_API text_element
55{
56public:
57
58 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (text_element)
59
60 virtual ~text_element () = default;
61
62 virtual void accept (text_processor& p) = 0;
63};
64
65class OCTINTERP_API text_element_string : public text_element
66{
67public:
68
69 text_element_string (const std::string& s = "")
70 : text_element (), m_str (s) { }
71
72 OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_string)
73
74 std::string string_value () const { return m_str; }
75
76 void accept (text_processor& p);
77
78private:
79
80 std::string m_str;
81};
82
83class OCTINTERP_API text_element_symbol : public text_element
84{
85public:
86
87 enum { invalid_code = 0xFFFFFFFFU };
88
90
92 : text_element (), m_symbol (sym) { }
93
94 OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_symbol)
95
96 int get_symbol () const { return m_symbol; }
97
98 uint32_t get_symbol_code () const;
99
100 void accept (text_processor& p);
101
102private:
103 int m_symbol;
104};
105
106class OCTINTERP_API text_element_list
107 : public text_element, public std::list<text_element *>
108{
109public:
110
111 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (text_element_list)
112
114 : text_element (), std::list<text_element*> ()
115 {
116 push_back (e);
117 }
118
120 {
121 while (! empty ())
122 {
123 auto it = begin ();
124 delete (*it);
125 erase (it);
126 }
127 }
128
129 void accept (text_processor& p);
130};
131
132class OCTINTERP_API text_element_subscript : public text_element
133{
134public:
135
136 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (text_element_subscript)
137
139 : text_element (), m_elem (e) { }
140
142 : text_element ()
143 { m_elem = new text_element_string (std::string (1, c)); }
144
146 { delete m_elem; }
147
148 void accept (text_processor& p);
149
150 text_element * get_element () { return m_elem; }
151
152private:
153
154 text_element *m_elem;
155};
156
157class OCTINTERP_API text_element_superscript : public text_element
158{
159public:
160
161 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (text_element_superscript)
162
165
167 : text_element ()
168 { m_elem = new text_element_string (std::string (1, c)); }
169
171 { delete m_elem; }
172
173 void accept (text_processor& p);
174
175 text_element * get_element () { return m_elem; }
176
177private:
178
179 text_element *m_elem;
180};
181
182class OCTINTERP_API text_element_combined : public text_element_list
183{
184public:
185
186 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE_DELETE (text_element_combined)
187
190
193 { push_back (e2); }
194
195 void accept (text_processor& p);
196};
197
198class OCTINTERP_API text_element_fontstyle : public text_element
199{
200public:
201
203 {
207 oblique
208 };
209
211
213 : text_element (), m_style (st) { }
214
215 OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_fontstyle)
216
217 fontstyle get_fontstyle () const { return m_style; }
218
219 void accept (text_processor& p);
220
221private:
222
223 fontstyle m_style;
224};
225
226class OCTINTERP_API text_element_fontname : public text_element
227{
228public:
229
231
232 text_element_fontname (const std::string& fname)
233 : text_element (), m_name (fname) { }
234
235 OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_fontname)
236
237 const std::string& get_fontname () const { return m_name; }
238
239 void accept (text_processor& p);
240
241private:
242
243 std::string m_name;
244};
245
246class OCTINTERP_API text_element_fontsize : public text_element
247{
248public:
249
251
253 : text_element (), m_size (fsize) { }
254
255 OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_fontsize)
256
257 double get_fontsize () const { return m_size; }
258
259 void accept (text_processor& p);
260
261private:
262
263 double m_size;
264};
265
266class OCTINTERP_API text_element_color : public text_element
267{
268public:
269
271
272 text_element_color (double r, double g, double b)
273 : text_element (), m_rgb (1, 3, 0.0)
274 {
275 m_rgb(0) = r;
276 m_rgb(1) = g;
277 m_rgb(2) = b;
278 }
279
280 text_element_color (const std::string& cname)
281 : text_element (), m_rgb (1, 3, 0.0)
282 {
283#define ASSIGN_COLOR(r,g,b) { m_rgb(0) = r; m_rgb(1) = g; m_rgb(2) = b; }
284 if (cname == "red") ASSIGN_COLOR(1, 0, 0)
285 else if (cname == "green") ASSIGN_COLOR(0, 1, 0)
286 else if (cname == "yellow") ASSIGN_COLOR(1, 1, 0)
287 else if (cname == "magenta") ASSIGN_COLOR(1, 0, 1)
288 else if (cname == "blue") ASSIGN_COLOR(0, 0, 1)
289 else if (cname == "black") ASSIGN_COLOR(0, 0, 0)
290 else if (cname == "white") ASSIGN_COLOR(1, 1, 1)
291 else if (cname == "gray") ASSIGN_COLOR(.5, .5, .5)
292 else if (cname == "darkGreen") ASSIGN_COLOR(0, .5, 0)
293 else if (cname == "orange") ASSIGN_COLOR(1, .65, 0)
294 else if (cname == "lightBlue") ASSIGN_COLOR(0.68, .85, .9)
295#undef ASSIGN_COLOR
296 }
297
298 OCTAVE_DEFAULT_COPY_MOVE_DELETE (text_element_color)
299
300 Matrix get_color () { return m_rgb; }
301
302 void accept (text_processor& p);
303
304private:
305
306 Matrix m_rgb;
307};
308
309class OCTINTERP_API text_processor
310{
311public:
312
313 virtual void visit (text_element_string&) { }
314
315 virtual void visit (text_element_symbol&) { }
316
317 virtual void visit (text_element_list& e)
318 {
319 for (auto& el_p : e)
320 {
321 el_p->accept (*this);
322 }
323 }
324
326 { e.get_element ()->accept (*this); }
327
329 { e.get_element ()->accept (*this); }
330
331 virtual void visit (text_element_combined&) { }
332
333 virtual void visit (text_element_fontstyle&) { }
334
335 virtual void visit (text_element_fontname&) { }
336
337 virtual void visit (text_element_fontsize&) { }
338
339 virtual void visit (text_element_color&) { }
340
341 virtual void reset () { }
342
343protected:
344
345 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (text_processor)
346
347 virtual ~text_processor () = default;
348};
349
350#define TEXT_ELEMENT_ACCEPT(cls) \
351 inline void \
352 cls::accept (text_processor& p) \
353 { \
354 p.visit (*this); \
355 }
356
367
368class OCTINTERP_API text_parser
369{
370public:
371
372 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE (text_parser)
373
374 virtual ~text_parser () = default;
375
376 virtual text_element * parse (const std::string& s) = 0;
377
378public:
379 static text_element * parse (const std::string& s,
381};
382
383class OCTINTERP_API text_parser_none : public text_parser
384{
385public:
386
387 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE_DELETE (text_parser_none)
388
389 // FIXME: is it possible to use reference counting to manage the
390 // memory for the object returned by the text parser? That would be
391 // preferable to having to know when and where to delete the object it
392 // creates...
393
394 text_element * parse (const std::string& s)
395 {
396 return new text_element_string (s);
397 }
398};
399
400class OCTINTERP_API text_parser_tex : public text_parser
401{
402public:
403
405 : text_parser (), m_scanner (nullptr), m_buffer_state (nullptr),
406 m_result (nullptr)
407 { }
408
409 OCTAVE_DISABLE_COPY_MOVE (text_parser_tex)
410
412 { destroy_lexer (); }
413
414 text_element * parse (const std::string& s);
415
416 void * get_scanner () { return m_scanner; }
417
418 void set_parse_result (text_element *e) { m_result = e; }
419
420 text_element * get_parse_result () { return m_result; }
421
422private:
423 bool init_lexer (const std::string& s);
424
425 void destroy_lexer ();
426
427 //--------
428
429 void *m_scanner;
430
431 void *m_buffer_state;
432
433 text_element *m_result;
434};
435
436inline text_element *
437text_parser::parse (const std::string& s, const caseless_str& interpreter)
438{
439 std::unique_ptr<text_parser> parser;
440
441 if (interpreter.compare ("tex"))
443 else
445
446 return parser->parse (s);
447}
448
449OCTAVE_END_NAMESPACE(octave)
450
451#endif
text_element_color(double r, double g, double b)
text_element_color()=delete
text_element_color(const std::string &cname)
text_element_combined(text_element *e1, text_element *e2)
text_element_fontname(const std::string &fname)
text_element_fontname()=delete
text_element_fontsize(double fsize)
text_element_fontsize()=delete
text_element_fontstyle()=delete
text_element_fontstyle(fontstyle st)
text_element_string(const std::string &s="")
Definition text-engine.h:69
text_element * get_element()
text_element * get_element()
text_element_symbol()=delete
text_element_symbol(int sym)
Definition text-engine.h:91
virtual void accept(text_processor &p)=0
void set_parse_result(text_element *e)
void * get_scanner()
text_element * get_parse_result()
virtual text_element * parse(const std::string &s)=0
virtual void reset()
virtual void visit(text_element_combined &)
virtual void visit(text_element_string &)
virtual void visit(text_element_fontsize &)
virtual void visit(text_element_list &e)
virtual void visit(text_element_superscript &e)
virtual void visit(text_element_fontstyle &)
virtual void visit(text_element_fontname &)
virtual void visit(text_element_color &)
virtual void visit(text_element_symbol &)
virtual void visit(text_element_subscript &e)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define m_scanner
#define TEXT_ELEMENT_ACCEPT(cls)
#define ASSIGN_COLOR(r, g, b)