annotate libinterp/corefcn/latex-text-renderer.cc @ 33625:d213a148b3f1 default tip

ensure exp. terminal widget has focus at startup * main-window.cc (main_window): call focus_command_window only if event loop is idle by using a single shot timer * main-window.h: make focus_command_window a public slot
author Torsten Lilge <ttl-octave@mailbox.org>
date Sun, 26 May 2024 02:29:44 +0200
parents 8f8bb94a1b83
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
1 ////////////////////////////////////////////////////////////////////////
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
2 //
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 32256
diff changeset
3 // Copyright (C) 2021-2024 The Octave Project Developers
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
4 //
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
6 // distribution or <https://octave.org/copyright/>.
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
7 //
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
8 // This file is part of Octave.
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
9 //
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
11 // under the terms of the GNU General Public License as published by
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
13 // (at your option) any later version.
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
14 //
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
18 // GNU General Public License for more details.
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
19 //
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
20 // You should have received a copy of the GNU General Public License
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
21 // along with Octave; see the file COPYING. If not, see
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
22 // <https://www.gnu.org/licenses/>.
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
23 //
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
24 ////////////////////////////////////////////////////////////////////////
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
25
33167
8f8bb94a1b83 for consistent coding style, use #if defined, not #ifdef or #ifndef
John W. Eaton <jwe@octave.org>
parents: 32633
diff changeset
26 #if defined (HAVE_CONFIG_H)
32256
3701ac59d931 Consistently use double quotes to include "config.h".
Markus Mützel <markus.muetzel@gmx.de>
parents: 31855
diff changeset
27 # include "config.h"
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
28 #endif
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
29
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
30 #include <iostream>
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
31 #include <fstream>
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
32
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
33 #include "base-text-renderer.h"
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
34 #include "builtin-defun-decls.h"
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
35 #include "dim-vector.h"
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
36 #include "error.h"
30299
f306fe9bfa0d Limit the size of the cache of latex parsed strings (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 29950
diff changeset
37 #include "graphics.h"
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
38 #include "file-ops.h"
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
39 #include "interpreter.h"
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
40 #include "interpreter-private.h"
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
41 #include "oct-env.h"
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
42 #include "oct-process.h"
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
43
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 31211
diff changeset
44 OCTAVE_BEGIN_NAMESPACE(octave)
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 31211
diff changeset
45
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
46 std::string
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
47 quote_string (std::string str)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
48 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
49 return ('"' + str + '"');
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
50 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
51
32628
ae4e19c0a2b1 maint: Place class name and class keyword on one line.
Rik <rik@octave.org>
parents: 32256
diff changeset
52 class OCTINTERP_API latex_renderer : public base_text_renderer
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
53 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
54 public:
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
55
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
56 latex_renderer ()
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
57 : m_fontsize (10.0), m_fontname ("cmr"), m_tmp_dir (),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
58 m_color (dim_vector (1, 3), 0), m_latex_binary ("latex"),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
59 m_dvipng_binary ("dvipng"), m_dvisvg_binary ("dvisvgm"),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
60 m_debug (false), m_testing (true)
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
61 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
62 std::string bin = sys::env::getenv ("OCTAVE_LATEX_BINARY");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
63 if (! bin.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
64 m_latex_binary = quote_string (bin);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
65
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
66 bin = sys::env::getenv ("OCTAVE_DVIPNG_BINARY");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
67 if (! bin.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
68 m_dvipng_binary = quote_string (bin);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
69
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
70 bin = sys::env::getenv ("OCTAVE_DVISVG_BINARY");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
71 if (! bin.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
72 m_dvisvg_binary = quote_string (bin);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
73
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
74 m_debug = ! sys::env::getenv ("OCTAVE_LATEX_DEBUG_FLAG").empty ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
75 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
76
31855
1daf8bfceac3 default or disable more ctors, dtors, and assignment ops
John W. Eaton <jwe@octave.org>
parents: 31771
diff changeset
77 OCTAVE_DISABLE_COPY_MOVE (latex_renderer)
1daf8bfceac3 default or disable more ctors, dtors, and assignment ops
John W. Eaton <jwe@octave.org>
parents: 31771
diff changeset
78
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
79 ~latex_renderer ()
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
80 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
81 if (! m_tmp_dir.empty () && ! m_debug)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
82 sys::recursive_rmdir (m_tmp_dir);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
83 }
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
84
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
85 void set_font (const std::string& /*name*/, const std::string& /*weight*/,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
86 const std::string& /*angle*/, double size)
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30299
diff changeset
87 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
88 m_fontsize = size;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
89 }
30299
f306fe9bfa0d Limit the size of the cache of latex parsed strings (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 29950
diff changeset
90
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
91 void set_color (const Matrix& c)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
92 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
93 if (c.numel () == 3)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
94 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
95 m_color(0) = static_cast<uint8_t> (c (0) * 255);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
96 m_color(1) = static_cast<uint8_t> (c (1) * 255);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
97 m_color(2) = static_cast<uint8_t> (c (2) * 255);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
98 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
99 }
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
100
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
101 Matrix get_extent (text_element * /*elt*/, double /*rotation*/)
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
102 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
103 return Matrix (1, 2, 0.0);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
104 }
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
105
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
106 Matrix get_extent (const std::string& txt, double rotation,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
107 const caseless_str& interpreter)
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
108 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
109 Matrix bbox;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
110 uint8NDArray pixels;
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
111
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
112 text_to_pixels (txt, pixels, bbox, 0, 0, rotation, interpreter, false);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
113
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
114 return bbox.extract_n (0, 2, 1, 2);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
115 }
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
116
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
117 void text_to_strlist (const std::string& txt,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
118 std::list<text_renderer::string>& lst,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
119 Matrix& bbox, int halign, int valign, double rotation,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
120 const caseless_str& interp)
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
121 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
122 uint8NDArray pixels;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
123 text_to_pixels (txt, pixels, bbox, halign, valign, rotation,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
124 interp, false);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
125
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
126 text_renderer::font fnt;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
127 text_renderer::string str ("", fnt, 0.0, 0.0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
128 str.set_color (m_color);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
129
31105
670a0d878af1 eliminate WHO arguments from interpreter-private functions
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
130 gh_manager& gh_mgr = octave::__get_gh_manager__ ();
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
131
30299
f306fe9bfa0d Limit the size of the cache of latex parsed strings (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 29950
diff changeset
132 gh_manager::latex_data ldata = gh_mgr.get_latex_data (key (txt, halign));
f306fe9bfa0d Limit the size of the cache of latex parsed strings (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 29950
diff changeset
133
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
134 str.set_svg_element (ldata.second);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
135
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
136 lst.push_back (str);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
137 }
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
138
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
139 void text_to_pixels (const std::string& txt, uint8NDArray& pxls,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
140 Matrix& bbox, int halign, int valign, double rotation,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
141 const caseless_str& interpreter,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
142 bool handle_rotation);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
143
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
144 void set_anti_aliasing (bool /*val*/) { }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
145
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
146 octave_map get_system_fonts () { return octave_map (); }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
147
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
148 bool ok ();
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
149
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
150 private:
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
151
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
152 std::string key (const std::string& txt, int halign)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
153 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
154 return (txt + ":"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
155 + std::to_string (m_fontsize) + ":"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
156 + std::to_string (halign) + ":"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
157 + std::to_string (m_color(0)) + ":"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
158 + std::to_string (m_color(1)) + ":"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
159 + std::to_string (m_color(2)));
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
160 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
161
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
162 void warn_helper (std::string caller, std::string txt, std::string cmd,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
163 process_execution_result result);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
164
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
165 uint8NDArray render (const std::string& txt, int halign = 0);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
166
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
167 bool read_image (const std::string& png_file, uint8NDArray& data) const;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
168
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
169 std::string write_tex_file (const std::string& txt, int halign);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
170
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
171 private:
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
172 double m_fontsize;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
173 std::string m_fontname;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
174 std::string m_tmp_dir;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
175 uint8NDArray m_color;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
176 std::string m_latex_binary;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
177 std::string m_dvipng_binary;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
178 std::string m_dvisvg_binary;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
179 bool m_debug;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
180 bool m_testing;
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
181
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
182 };
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
183
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
184 bool
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
185 latex_renderer::ok ()
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
186 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
187 // Only run the test once in a session
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
188 static bool tested = false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
189
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
190 static bool isok = false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
191
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
192 if (! tested)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
193 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
194 tested = true;
29483
af41ebf3d1b3 Differ latex test until actually needed (bug #59546)
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 29470
diff changeset
195
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
196 // For testing, render a questoin mark
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
197 uint8NDArray pixels = render ("?");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
198
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
199 if (! pixels.isempty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
200 isok = true;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
201 else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
202 warning_with_id ("Octave:LaTeX:internal-error",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
203 "latex_renderer: a run-time test failed and the 'latex' interpreter has been disabled.");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
204 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
205
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
206 m_testing = false;
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
207
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
208 return isok;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
209 }
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
210
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
211 std::string
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
212 latex_renderer::write_tex_file (const std::string& txt, int halign)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
213 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
214 if (m_tmp_dir.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
215 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
216 //Create the temporary directory
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
217 #if defined (OCTAVE_USE_WINDOWS_API)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
218 static std::string base_tmp_dir;
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
219
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
220 if (base_tmp_dir.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
221 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
222 base_tmp_dir = sys::env::get_temp_directory ();
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
223
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
224 // Make sure we don't get short 8.3 path on Windows since some
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
225 // versions of latex on that platform don't support them
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
226 // (see bug #62779)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
227 if (base_tmp_dir.find ('~') != std::string::npos)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
228 base_tmp_dir = sys::canonicalize_file_name (base_tmp_dir);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
229 }
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
230
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
231 m_tmp_dir = sys::tempnam (base_tmp_dir, "latex");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
232 #else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
233 m_tmp_dir = sys::tempnam ("", "latex");
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
234 #endif
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
235
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
236 if (sys::mkdir (m_tmp_dir, 0700) != 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
237 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
238 warning_with_id ("Octave:LaTeX:internal-error",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
239 "latex_renderer: unable to create temp directory");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
240 return std::string ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
241 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
242 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
243
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
244 std::string base_file_name
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
245 = sys::file_ops::concat (m_tmp_dir, "default");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
246
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
247 // Duplicate \n characters and align multi-line strings based on
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
248 // horizontalalignment
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
249 std::string latex_txt (txt);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
250 std::size_t pos = 0;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
251
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
252 while (true)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
253 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
254 pos = txt.find_first_of ("\n", pos);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
255
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
256 if (pos == std::string::npos)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
257 break;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
258
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
259 latex_txt.replace (pos, 1, "\n\n");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
260
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
261 pos += 1;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
262 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
263
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
264 std::string env ("flushleft");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
265 if (halign == 1)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
266 env = "center";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
267 else if (halign == 2)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
268 env = "flushright";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
269
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
270 latex_txt = std::string ("\\begin{" ) + env + "}\n"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
271 + latex_txt + "\n"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
272 + "\\end{" + env + "}\n";
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
273
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
274 // Write to temporary .tex file
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
275 std::ofstream file;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
276 file.open (base_file_name + ".tex");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
277 file << "\\documentclass[10pt, varwidth]{standalone}\n"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
278 << "\\usepackage{amsmath}\n"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
279 << "\\usepackage[utf8]{inputenc}\n"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
280 << "\\begin{document}\n"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
281 << latex_txt << "\n"
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
282 << "\\end{document}";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
283 file.close ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
284
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
285 return base_file_name;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
286 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
287
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
288 bool
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
289 latex_renderer::read_image (const std::string& png_file,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
290 uint8NDArray& data) const
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
291 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
292 uint8NDArray alpha;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
293 uint8NDArray rgb;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
294 int height;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
295 int width;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
296
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
297 try
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
298 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
299 // First get the image size to build the argument to __magick_read__
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
300 octave_value_list retval = F__magick_ping__ (ovl (png_file), 1);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
301
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
302 octave_scalar_map info
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
303 = retval(0).xscalar_map_value ("latex_renderer::read_image: "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
304 "Wrong type for info");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
305 height = info.getfield ("rows").int_value ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
306 width = info.getfield ("columns").int_value ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
307 Cell region (dim_vector(1, 2));
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
308 region(0) = range<double> (1.0, height);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
309 region(1) = range<double> (1.0, width);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
310 info.setfield ("region", region);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
311 info.setfield ("index", octave_value (1));
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
312
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
313 // Retrieve the alpha map
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
314 retval = F__magick_read__ (ovl (png_file, info), 3);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
315
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
316 alpha = retval(2).xuint8_array_value ("latex_renderer::read_image: "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
317 "Wrong type for alpha");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
318 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
319 catch (const execution_exception& ee)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
320 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
321 warning_with_id ("Octave:LaTeX:internal-error",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
322 "latex_renderer:: failed to read png data. %s",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
323 ee.message ().c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
324
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
325 interpreter& interp = __get_interpreter__ ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
326
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
327 interp.recover_from_exception ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
328
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
329 return false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
330 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
331
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
332 data = uint8NDArray (dim_vector (4, width, height),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
333 static_cast<uint8_t> (0));
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
334
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
335 for (int i = 0; i < height; i++)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
336 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
337 for (int j = 0; j < width; j++)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
338 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
339 data(0, j, i) = m_color(0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
340 data(1, j, i) = m_color(1);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
341 data(2, j, i) = m_color(2);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
342 data(3, j, i) = alpha(height-i-1, j);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
343 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
344 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
345
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
346 return true;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
347 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
348
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
349 void
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
350 latex_renderer::warn_helper (std::string caller, std::string txt,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
351 std::string cmd, process_execution_result result)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
352 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
353 if (m_testing && ! m_debug)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
354 return;
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
355
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
356 if (! m_debug)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
357 warning_with_id ("Octave:LaTeX:internal-error",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
358 "latex_renderer: unable to compile \"%s\"",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
359 txt.c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
360 else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
361 warning_with_id ("Octave:LaTeX:internal-error",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
362 "latex_renderer: %s failed for string \"%s\"\n\
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
363 * Command:\n\t%s\n\n* Error:\n%s\n\n* Stdout:\n%s",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
364 caller.c_str (), txt.c_str (), cmd.c_str (),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
365 result.err_msg ().c_str (),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
366 result.stdout_output ().c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
367 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
368
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
369 uint8NDArray
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
370 latex_renderer::render (const std::string& txt, int halign)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
371 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
372 // Render if it was not already done
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
373 gh_manager& gh_mgr = octave::__get_gh_manager__ ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
374
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
375 gh_manager::latex_data ldata = gh_mgr.get_latex_data (key (txt, halign));
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
376
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
377 if (! ldata.first.isempty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
378 return ldata.first;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
379
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
380 uint8NDArray data;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
381
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
382 // First write the base .tex file
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
383 std::string base_file_name = write_tex_file (txt, halign);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
384
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
385 if (base_file_name.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
386 return data;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
387
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
388 // Generate DVI file
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
389 std::string tex_file = quote_string (base_file_name + ".tex");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
390 std::string dvi_file = quote_string (base_file_name + ".dvi");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
391 std::string log_file = quote_string (base_file_name + ".log");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
392
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
393 process_execution_result result;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
394 std::string cmd = (m_latex_binary + " -interaction=nonstopmode "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
395 + "-output-directory=" + quote_string (m_tmp_dir) + " "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
396 + tex_file);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
397
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
398 #if defined (OCTAVE_USE_WINDOWS_API)
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
399 cmd = quote_string (cmd);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
400 #endif
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
401
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
402 result = run_command_and_return_output (cmd);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
403
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
404 if (result.exit_status () != 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
405 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
406 warn_helper ("latex", txt, cmd, result);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
407
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
408 if (txt != "?")
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
409 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
410 write_tex_file ("?", halign);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
411
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
412 result = run_command_and_return_output (cmd);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
413 if (result.exit_status () != 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
414 return data;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
415 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
416 else
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
417 return data;
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
418 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
419
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
420 double size_factor = m_fontsize / 10.0;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
421
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
422
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
423 // Convert DVI to SVG, read file and store its content for later use in
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
424 // gl2ps_print
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
425 std::string svg_file = base_file_name + ".svg";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
426
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
427 cmd = (m_dvisvg_binary + " -n "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
428 + "-TS" + std::to_string (size_factor) + " "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
429 + "-v1 -o " + quote_string (svg_file) + " "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
430 + dvi_file);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
431
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
432 #if defined (OCTAVE_USE_WINDOWS_API)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
433 cmd = quote_string (cmd);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
434 #endif
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
435
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
436 result = run_command_and_return_output (cmd);
30299
f306fe9bfa0d Limit the size of the cache of latex parsed strings (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 29950
diff changeset
437
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
438 if (result.exit_status () != 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
439 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
440 warn_helper ("dvisvg", txt, cmd, result);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
441 return data;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
442 }
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
443
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
444 std::ifstream svg_stream (svg_file);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
445 std::string svg_string;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
446 svg_string.assign (std::istreambuf_iterator<char> (svg_stream),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
447 std::istreambuf_iterator<char> ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
448
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
449 // Convert DVI to PNG, read file and format pixel data for later use in
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
450 // OpenGL
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
451 std::string png_file = base_file_name + ".png";
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
452
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
453 cmd = (m_dvipng_binary + " " + dvi_file + " "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
454 + "-q -o " + quote_string (png_file) + " "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
455 + "-bg Transparent -D "
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
456 + std::to_string (std::floor (72.0 * size_factor)));
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
457
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
458 #if defined (OCTAVE_USE_WINDOWS_API)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
459 cmd = quote_string (cmd);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
460 #endif
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
461
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
462 result = run_command_and_return_output (cmd);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
463
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
464 if (result.exit_status () != 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
465 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
466 warn_helper ("dvipng", txt, cmd, result);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
467 return data;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
468 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
469
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
470 if (! read_image (png_file, data))
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
471 return data;
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
472
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
473 // Cache pixel and svg data for this string
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
474 ldata.first = data;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
475 ldata.second = svg_string;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
476
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
477 gh_mgr.set_latex_data (key (txt, halign), ldata);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
478
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
479 if (m_debug)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
480 std::cout << "* Caching " << key (txt, halign) << std::endl;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
481
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
482 return data;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
483 }
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
484
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
485 void
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
486 latex_renderer::text_to_pixels (const std::string& txt, uint8NDArray& pixels,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
487 Matrix& bbox, int halign, int valign,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
488 double rotation,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
489 const caseless_str& /*interpreter*/,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
490 bool handle_rotation)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
491 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
492 // Return early for empty strings
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
493 if (txt.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
494 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
495 bbox = Matrix (1, 4, 0.0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
496 return;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
497 }
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
498
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
499 if (ok ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
500 pixels = render (txt, halign);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
501 else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
502 pixels = uint8NDArray (dim_vector (4, 1, 1), static_cast<uint8_t> (0));
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
503
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
504 if (pixels.ndims () < 3 || pixels.isempty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
505 return; // nothing to render
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
506
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
507 // Store unrotated bbox size
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
508 bbox = Matrix (1, 4, 0.0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
509 bbox (2) = pixels.dim2 ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
510 bbox (3) = pixels.dim3 ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
511
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
512 // Now rotate pixels if necessary
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
513 int rot_mode = rotation_to_mode (rotation);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
514
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
515 if (! pixels.isempty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
516 rotate_pixels (pixels, rot_mode);
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
517
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
518 // Move X0 and Y0 depending on alignments and eventually swap values
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
519 // for text rotated 90° 180° or 270°
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
520 fix_bbox_anchor (bbox, halign, valign, rot_mode, handle_rotation);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
521 }
29470
2ae4764180c6 Initial implementation of a LaTeX interpreter (bug #59546).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
diff changeset
522
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
523 base_text_renderer *
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
524 make_latex_text_renderer ()
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
525 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
526 latex_renderer *renderer = new latex_renderer ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
527
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
528 return renderer;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
529 }
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 31211
diff changeset
530
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 31211
diff changeset
531 OCTAVE_END_NAMESPACE(octave)