34 #include "builtin-defun-decls.h"
49 return (
'"' + str +
'"');
60 : m_fontsize (10.0), m_fontname (
"cmr"), m_tmp_dir (),
61 m_color (
dim_vector (1, 3), 0), m_latex_binary (
"latex"),
62 m_dvipng_binary (
"dvipng"), m_dvisvg_binary (
"dvisvgm"),
63 m_debug (false), m_testing (true)
65 std::string bin = sys::env::getenv (
"OCTAVE_LATEX_BINARY");
69 bin = sys::env::getenv (
"OCTAVE_DVIPNG_BINARY");
73 bin = sys::env::getenv (
"OCTAVE_DVISVG_BINARY");
77 m_debug = ! sys::env::getenv (
"OCTAVE_LATEX_DEBUG_FLAG").empty ();
82 if (! m_tmp_dir.empty () && ! m_debug)
86 void set_font (
const std::string& ,
const std::string& ,
87 const std::string& ,
double size)
96 m_color(0) =
static_cast<uint8_t
> (c (0) * 255);
97 m_color(1) =
static_cast<uint8_t
> (c (1) * 255);
98 m_color(2) =
static_cast<uint8_t
> (c (2) * 255);
104 return Matrix (1, 2, 0.0);
113 text_to_pixels (txt, pixels, bbox, 0, 0, rotation,
interpreter,
false);
119 std::list<text_renderer::string>& lst,
120 Matrix& bbox,
int halign,
int valign,
double rotation,
124 text_to_pixels (txt, pixels, bbox, halign, valign, rotation,
133 gh_manager::latex_data ldata = gh_mgr.get_latex_data (key (txt, halign));
140 void text_to_pixels (
const std::string& txt,
uint8NDArray& pxls,
141 Matrix& bbox,
int halign,
int valign,
double rotation,
143 bool handle_rotation);
153 std::string
key (
const std::string& txt,
int halign)
156 + std::to_string (m_fontsize) +
":"
157 + std::to_string (halign) +
":"
158 + std::to_string (m_color(0)) +
":"
159 + std::to_string (m_color(1)) +
":"
160 + std::to_string (m_color(2)));
163 void warn_helper (std::string caller, std::string txt, std::string cmd,
166 uint8NDArray render (
const std::string& txt,
int halign = 0);
168 bool read_image (
const std::string& png_file,
uint8NDArray& data)
const;
170 std::string write_tex_file (
const std::string& txt,
int halign);
189 static bool tested =
false;
191 static bool isok =
false;
204 "latex_renderer: a run-time test failed and the 'latex' interpreter has been disabled.");
218 #if defined (OCTAVE_USE_WINDOWS_API)
219 static std::string base_tmp_dir;
221 if (base_tmp_dir.empty ())
228 if (base_tmp_dir.find (
'~') != std::string::npos)
240 "latex_renderer: unable to create temp directory");
241 return std::string ();
245 std::string base_file_name
250 std::string latex_txt (txt);
255 pos = txt.find_first_of (
"\n", pos);
257 if (pos == std::string::npos)
260 latex_txt.replace (pos, 1,
"\n\n");
265 std::string
env (
"flushleft");
268 else if (halign == 2)
271 latex_txt = std::string (
"\\begin{" ) +
env +
"}\n"
273 +
"\\end{" +
env +
"}\n";
277 file.open (base_file_name +
".tex");
278 file <<
"\\documentclass[10pt, varwidth]{standalone}\n"
279 <<
"\\usepackage{amsmath}\n"
280 <<
"\\usepackage[utf8]{inputenc}\n"
281 <<
"\\begin{document}\n"
283 <<
"\\end{document}";
286 return base_file_name;
304 = retval(0).xscalar_map_value (
"latex_renderer::read_image: "
305 "Wrong type for info");
309 region(0) = range<double> (1.0, height);
310 region(1) = range<double> (1.0, width);
317 alpha = retval(2).xuint8_array_value (
"latex_renderer::read_image: "
318 "Wrong type for alpha");
323 "latex_renderer:: failed to read png data. %s",
324 ee.message ().c_str ());
334 static_cast<uint8_t
> (0));
336 for (
int i = 0; i < height; i++)
338 for (
int j = 0; j < width; j++)
343 data(3, j, i) = alpha(height-i-1, j);
359 "latex_renderer: unable to compile \"%s\"",
363 "latex_renderer: %s failed for string \"%s\"\n\
364 * Command:\n\t%s\n\n* Error:\n%s\n\n* Stdout:\n%s",
365 caller.c_str (), txt.c_str (), cmd.c_str (),
376 gh_manager::latex_data ldata = gh_mgr.get_latex_data (
key (txt, halign));
378 if (! ldata.first.isempty ())
386 if (base_file_name.empty ())
390 std::string tex_file =
quote_string (base_file_name +
".tex");
391 std::string dvi_file =
quote_string (base_file_name +
".dvi");
392 std::string log_file =
quote_string (base_file_name +
".log");
399 #if defined (OCTAVE_USE_WINDOWS_API)
426 std::string svg_file = base_file_name +
".svg";
429 +
"-TS" + std::to_string (size_factor) +
" "
433 #if defined (OCTAVE_USE_WINDOWS_API)
446 std::string svg_string;
447 svg_string.assign (std::istreambuf_iterator<char> (svg_stream),
448 std::istreambuf_iterator<char> ());
452 std::string png_file = base_file_name +
".png";
456 +
"-bg Transparent -D "
457 + std::to_string (
std::floor (72.0 * size_factor)));
459 #if defined (OCTAVE_USE_WINDOWS_API)
476 ldata.second = svg_string;
478 gh_mgr.set_latex_data (
key (txt, halign), ldata);
481 std::cout <<
"* Caching " <<
key (txt, halign) << std::endl;
488 Matrix& bbox,
int halign,
int valign,
491 bool handle_rotation)
496 bbox =
Matrix (1, 4, 0.0);
501 pixels =
render (txt, halign);
509 bbox =
Matrix (1, 4, 0.0);
510 bbox (2) = pixels.
dim2 ();
511 bbox (3) = pixels.
dim3 ();
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
OCTAVE_EXPORT octave_value_list F__magick_read__(const octave_value_list &args, int nargout)
OCTAVE_EXPORT octave_value_list F__magick_ping__(const octave_value_list &args, int)
OCTARRAY_OVERRIDABLE_FUNC_API bool isempty(void) const
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type dim2(void) const
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type dim3(void) const
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API int ndims(void) const
Size of the specified dimension.
OCTAVE_API Matrix extract_n(octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const
void fix_bbox_anchor(Matrix &bbox, int halign, int valign, int rot_mode, bool handle_rotation) const
void rotate_pixels(uint8NDArray &pixels, int rot_mode) const
int rotation_to_mode(double rotation) const
Vector representing the dimensions (size) of an Array.
void recover_from_exception(void)
std::string m_dvipng_binary
std::string key(const std::string &txt, int halign)
octave_map get_system_fonts(void)
bool read_image(const std::string &png_file, uint8NDArray &data) const
void text_to_strlist(const std::string &txt, std::list< text_renderer::string > &lst, Matrix &bbox, int halign, int valign, double rotation, const caseless_str &interp)
Matrix get_extent(text_element *, double)
std::string m_latex_binary
void text_to_pixels(const std::string &txt, uint8NDArray &pxls, Matrix &bbox, int halign, int valign, double rotation, const caseless_str &interpreter, bool handle_rotation)
Matrix get_extent(const std::string &txt, double rotation, const caseless_str &interpreter)
std::string m_dvisvg_binary
void set_font(const std::string &, const std::string &, const std::string &, double size)
void warn_helper(std::string caller, std::string txt, std::string cmd, process_execution_result result)
void set_anti_aliasing(bool)
std::string write_tex_file(const std::string &txt, int halign)
void set_color(const Matrix &c)
uint8NDArray render(const std::string &txt, int halign=0)
void setfield(const std::string &key, const octave_value &val)
octave_value getfield(const std::string &key) const
int int_value(bool req_int=false, bool frc_str_conv=false) const
int exit_status(void) const
std::string stdout_output(void) const
std::string err_msg(void) const
void set_svg_element(const std::string &svg)
void set_color(const uint8NDArray &c)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning_with_id(const char *id, const char *fmt,...)
std::string canonicalize_file_name(const std::string &name)
std::string tempnam(const std::string &dir, const std::string &pfx)
int recursive_rmdir(const std::string &name)
int mkdir(const std::string &nm, mode_t md)
interpreter & __get_interpreter__(void)
gh_manager & __get_gh_manager__(void)
base_text_renderer * make_latex_text_renderer(void)
std::string quote_string(std::string str)
std::complex< T > floor(const std::complex< T > &x)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
static std::string get_temp_directory(void)
process_execution_result run_command_and_return_output(const std::string &cmd_str)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
intNDArray< octave_uint8 > uint8NDArray