26#if defined (HAVE_CONFIG_H)
34#if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
61 gl2ps_renderer :
public opengl_renderer
65 gl2ps_renderer (opengl_functions& glfcns, FILE *_fp,
66 const std::string& _term)
67 : opengl_renderer (glfcns), m_fp (_fp), m_term (_term), m_fontsize (),
68 m_fontname (), m_buffer_overflow (false), m_svg_def_index (0)
71 ~gl2ps_renderer (
void) =
default;
80 void draw (
const graphics_object& go,
const std::string& print_cmd);
84 Matrix render_text (
const std::string& txt,
85 double x,
double y,
double z,
86 int halign,
int valign,
double rotation = 0.0);
88 void set_font (
const base_properties& props);
96 graphics_object go = gh_mgr.get_object (h);
98 if (! go.valid_object ())
101 if (go.isa (
"axes") || go.isa (
"hggroup"))
103 Matrix children = go.get (
"children").matrix_value ();
111 else if (go.isa (
"patch") || go.isa (
"surface"))
118 else if (go.isa (
"scatter"))
133 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
134 gl2psBeginViewport (vp);
139 gl2psGetOptions (&opts);
140 if (has_alpha (props.get___myhandle__ ()))
142 opts &= ~GL2PS_OCCLUSION_CULL;
146 gl2psEnable (GL2PS_BLEND);
150 opts |= GL2PS_OCCLUSION_CULL;
151 gl2psDisable (GL2PS_BLEND);
154 gl2psSetOptions (opts);
162 GLint
state = gl2psEndViewport ();
163 if (
state == GL2PS_NO_FEEDBACK && props.is_visible ())
164 warning (
"gl2ps_renderer::draw_axes: empty feedback buffer and/or nothing else to print");
165 else if (
state == GL2PS_ERROR)
166 error (
"gl2ps_renderer::draw_axes: gl2psEndPage returned GL2PS_ERROR");
168 m_buffer_overflow |= (
state == GL2PS_OVERFLOW);
172 gl2psGetOptions (&opts);
173 opts &= ~GL2PS_DRAW_BACKGROUND;
174 gl2psSetOptions (opts);
180 void draw_pixels (
int w,
int h,
const float *data);
181 void draw_pixels (
int w,
int h,
const uint8_t *data);
182 void draw_pixels (
int w,
int h,
const uint16_t *data);
184 void init_marker (
const std::string& m,
double size,
float width)
190 if (m ==
"o" || m ==
"v" || m ==
"^" || m ==
">" || m ==
"<" || m ==
"h"
191 || m ==
"hexagram" || m ==
"p" || m ==
"pentagram")
193 set_linejoin (
"round");
194 set_linecap (
"round");
198 set_linejoin (
"miter");
199 set_linecap (
"square");
203 void set_linestyle (
const std::string& s,
bool use_stipple =
false,
204 double linewidth = 0.5)
208 if (s ==
"-" && ! use_stipple)
209 gl2psDisable (GL2PS_LINE_STIPPLE);
211 gl2psEnable (GL2PS_LINE_STIPPLE);
214 void set_linecap (
const std::string& s)
218#if defined (HAVE_GL2PSLINEJOIN)
220 gl2psLineCap (GL2PS_LINE_CAP_BUTT);
221 else if (s ==
"square")
222 gl2psLineCap (GL2PS_LINE_CAP_SQUARE);
223 else if (s ==
"round")
224 gl2psLineCap (GL2PS_LINE_CAP_ROUND);
228 void set_linejoin (
const std::string& s)
232#if defined (HAVE_GL2PSLINEJOIN)
234 gl2psLineJoin (GL2PS_LINE_JOIN_ROUND);
235 else if (s ==
"miter")
236 gl2psLineJoin (GL2PS_LINE_JOIN_MITER);
237 else if (s ==
"chamfer")
238 gl2psLineJoin (GL2PS_LINE_JOIN_BEVEL);
242 void set_polygon_offset (
bool on,
float offset = 0.0f)
247 gl2psEnable (GL2PS_POLYGON_OFFSET_FILL);
251 gl2psDisable (GL2PS_POLYGON_OFFSET_FILL);
256 void set_linewidth (
float w)
265 void fix_strlist_position (
double x,
double y,
double z,
267 std::list<text_renderer::string>& lst);
270 std::string format_svg_element (std::string str,
Matrix bbox,
274 std::string strlist_to_svg (
double x,
double y,
double z,
Matrix box,
276 std::list<text_renderer::string>& lst);
279 std::string strlist_to_ps (
double x,
double y,
double z,
Matrix box,
281 std::list<text_renderer::string>& lst);
283 int alignment_to_mode (
int ha,
int va)
const;
288 std::string m_fontname;
289 bool m_buffer_overflow;
290 std::size_t m_svg_def_index;
300 graphics_object go = gh_mgr.get_object (h);
302 if (! go.valid_object ())
305 if (go.isa (
"figure") || go.isa (
"uipanel"))
307 Matrix children = go.get (
"children").matrix_value ();
315 else if (go.isa (
"axes"))
319 retval = ap.get_is2D (
true);
332 graphics_object go = gh_mgr.get_object (h);
334 if (! go.valid_object ())
337 if (go.isa (
"figure"))
342 retval = fp.get_title ();
351 static bool in_draw =
false;
352 static std::string old_print_cmd;
353 static GLint buffsize;
357 unwind_protect frame;
359 frame.protect_var (in_draw);
363 GLint gl2ps_term = GL2PS_PS;
364 if (m_term.find (
"eps") != std::string::npos)
365 gl2ps_term = GL2PS_EPS;
366 else if (m_term.find (
"pdf") != std::string::npos)
367 gl2ps_term = GL2PS_PDF;
368 else if (m_term.find (
"ps") != std::string::npos)
369 gl2ps_term = GL2PS_PS;
370 else if (m_term.find (
"svg") != std::string::npos)
371 gl2ps_term = GL2PS_SVG;
372 else if (m_term.find (
"pgf") != std::string::npos)
373 gl2ps_term = GL2PS_PGF;
374 else if (m_term.find (
"tex") != std::string::npos)
375 gl2ps_term = GL2PS_TEX;
377 warning (
"gl2ps_renderer::draw: Unknown terminal %s, using 'ps'",
380 GLint gl2ps_text = 0;
381 if (m_term.find (
"notxt") != std::string::npos)
382 gl2ps_text = GL2PS_NO_TEXT;
386 std::string plot_title = get_title (myhandle);
387 if (plot_title.empty ())
388 plot_title =
"Octave plot";
391 GLint gl2ps_sort = GL2PS_BSP_SORT;
395 if (has_2D_axes (myhandle))
396 gl2ps_sort = GL2PS_NO_SORT;
402 error (
"gl2ps_renderer::draw: couldn't open temporary file for printing");
404 frame.add ([=] () { std::fclose (tmpf); });
407 if (m_term.find (
"tex") == std::string::npos)
408 buffsize = 2*1024*1024;
412 m_buffer_overflow =
true;
414 while (m_buffer_overflow)
416 m_buffer_overflow =
false;
428 std::string include_graph;
430 std::size_t found_redirect = old_print_cmd.find (
'>');
432 if (found_redirect != std::string::npos)
433 include_graph = old_print_cmd.substr (found_redirect + 1);
435 include_graph = old_print_cmd;
437 std::size_t n_begin = include_graph.find_first_not_of (R
"( "')");
439 if (n_begin != std::string::npos)
442 std::size_t n_end = include_graph.find_last_not_of (R
"( "')");
443 include_graph = include_graph.substr (n_begin,
444 n_end - n_begin + 1);
447 include_graph = include_graph.substr (n_begin + 1);
450 include_graph =
"foobar-inc";
456 Matrix c = fprop.get_color_rgb ();
457 m_glfcns.glClearColor (c(0), c(1), c(2), 1);
460 set_device_pixel_ratio (fprop.get___device_pixel_ratio__ ());
463 GLint ret = gl2psBeginPage (plot_title.c_str (),
"Octave",
464 nullptr, gl2ps_term, gl2ps_sort,
467 | GL2PS_DRAW_BACKGROUND
468 | GL2PS_NO_PS3_SHADING
469 | GL2PS_USE_CURRENT_VIEWPORT),
470 GL_RGBA, 0,
nullptr, 0, 0, 0,
471 buffsize, tmpf, include_graph.c_str ());
472 if (ret == GL2PS_ERROR)
474 old_print_cmd.clear ();
475 error (
"gl2ps_renderer::draw: gl2psBeginPage returned GL2PS_ERROR");
480 if (m_buffer_overflow)
481 warning (
"gl2ps_renderer::draw: retrying with buffer size: %.1E B\n",
double (2*buffsize));
483 if (! m_buffer_overflow)
484 old_print_cmd = print_cmd;
495 std::size_t nread, nwrite;
500 const char *fcn =
"/SRX { gsave FCT moveto rotate xshow grestore } BD\n";
501 bool header_found = ! (m_term.find (
"eps") != std::string::npos
502 || m_term.find (
"svg") != std::string::npos);
504 while (! feof (tmpf) && nread)
506 if (! header_found &&
std::fgets (str, 8192, tmpf))
509 nread = std::fread (str, 1, 8192, tmpf);
513 if (! header_found &&
std::strncmp (str,
"/SBCR", 5) == 0)
516 nwrite = std::fwrite (fcn, 1,
strlen (fcn), m_fp);
517 if (nwrite !=
strlen (fcn))
521 error (
"gl2ps_renderer::draw: internal pipe error");
524 else if (m_term.find (
"svg") != std::string::npos)
534 std::string srchstr (str, nread);
535 std::size_t pos = srchstr.find (
"<svg ");
536 if (! header_found && pos != std::string::npos)
539 pos = srchstr.find (
"px");
540 if (pos != std::string::npos)
542 srchstr[pos+1] =
't';
544 pos = srchstr.find (
"px", pos);
545 srchstr[pos+1] =
't';
546 std::strcpy (str, srchstr.c_str ());
551 nwrite = std::fwrite (str, 1, nread, m_fp);
556 error (
"gl2ps_renderer::draw: internal pipe error");
566 gl2ps_renderer::alignment_to_mode (
int ha,
int va)
const
568 int gl2psa = GL2PS_TEXT_BL;
572 if (va == 0 || va == 3)
573 gl2psa=GL2PS_TEXT_BL;
575 gl2psa=GL2PS_TEXT_TL;
577 gl2psa=GL2PS_TEXT_CL;
581 if (va == 0 || va == 3)
582 gl2psa=GL2PS_TEXT_BR;
584 gl2psa=GL2PS_TEXT_TR;
586 gl2psa=GL2PS_TEXT_CR;
590 if (va == 0 || va == 3)
602 gl2ps_renderer::fix_strlist_position (
double x,
double y,
double z,
604 std::list<text_renderer::string>& lst)
606 for (
auto& txtobj : lst)
609 ColumnVector coord_pix = get_transform ().transform (
x, y, z,
false);
612 double rot = rotation * 4.0 *
atan (1.0) / 180;
613 coord_pix(0) += (txtobj.get_x () +
box(0))*cos (rot)
614 - (txtobj.get_y () +
box(1))*sin (rot);
615 coord_pix(1) -= (txtobj.get_y () +
box(1))*cos (rot)
616 + (txtobj.get_x () +
box(0))*sin (rot);
619 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
621 txtobj.set_x (coord_pix(0));
622 txtobj.set_y (vp[3] - coord_pix(1));
623 txtobj.set_z (coord_pix(2));
628 code_to_symbol (uint32_t code)
632 uint32_t idx = code - 945;
635 std::string characters (
"abgdezhqiklmnxoprVstufcyw");
636 retval = characters[idx];
643 std::string characters (
"ABGDEZHQIKLMNXOPRVSTUFCYW");
644 retval = characters[idx];
646 else if (code == 978)
648 else if (code == 215)
650 else if (code == 177)
652 else if (code == 8501)
654 else if (code == 8465)
656 else if (code == 8242)
658 else if (code == 8736)
660 else if (code == 172)
662 else if (code == 9829)
664 else if (code == 8472)
666 else if (code == 8706)
668 else if (code == 8704)
670 else if (code == 9827)
672 else if (code == 9824)
674 else if (code == 8476)
676 else if (code == 8734)
678 else if (code == 8730)
680 else if (code == 8707)
682 else if (code == 9830)
684 else if (code == 8747)
686 else if (code == 8727)
688 else if (code == 8744)
690 else if (code == 8855)
692 else if (code == 8901)
694 else if (code == 8728)
696 else if (code == 8745)
698 else if (code == 8743)
700 else if (code == 8856)
702 else if (code == 8729)
704 else if (code == 8746)
706 else if (code == 8853)
708 else if (code == 8804)
710 else if (code == 8712)
712 else if (code == 8839)
714 else if (code == 8801)
716 else if (code == 8773)
718 else if (code == 8834)
720 else if (code == 8805)
722 else if (code == 8715)
724 else if (code == 8764)
726 else if (code == 8733)
728 else if (code == 8838)
730 else if (code == 8835)
732 else if (code == 8739)
734 else if (code == 8776)
736 else if (code == 8869)
738 else if (code == 8656)
740 else if (code == 8592)
742 else if (code == 8658)
744 else if (code == 8594)
746 else if (code == 8596)
748 else if (code == 8593)
750 else if (code == 8595)
752 else if (code == 8970)
754 else if (code == 8971)
756 else if (code == 10216)
758 else if (code == 10217)
760 else if (code == 8968)
762 else if (code == 8969)
764 else if (code == 8800)
766 else if (code == 8230)
768 else if (code == 176)
770 else if (code == 8709)
772 else if (code == 169)
776 warning (
"print: unhandled symbol %d", code);
782 select_font (
caseless_str fn,
bool isbold,
bool isitalic)
785 std::string fontname;
786 if (fn ==
"times" || fn ==
"times-roman")
788 if (isitalic && isbold)
789 fontname =
"Times-BoldItalic";
791 fontname =
"Times-Italic";
793 fontname =
"Times-Bold";
795 fontname =
"Times-Roman";
797 else if (fn ==
"courier")
799 if (isitalic && isbold)
800 fontname =
"Courier-BoldOblique";
802 fontname =
"Courier-Oblique";
804 fontname =
"Courier-Bold";
806 fontname =
"Courier";
808 else if (fn ==
"symbol")
810 else if (fn ==
"zapfdingbats")
811 fontname =
"ZapfDingbats";
814 if (isitalic && isbold)
815 fontname =
"Helvetica-BoldOblique";
817 fontname =
"Helvetica-Oblique";
819 fontname =
"Helvetica-Bold";
821 fontname =
"Helvetica";
827 escape_character (
const std::string chr, std::string& str)
829 std::size_t idx = str.find (chr);
830 while (idx != std::string::npos)
832 str.insert (idx, 1,
'\\');
833 idx = str.find (chr, idx + 2);
838 gl2ps_renderer::format_svg_element (std::string str,
Matrix box,
844 std::string::size_type n1 = str.find (
"<defs>");
845 if (n1 == std::string::npos)
846 return std::string ();
848 std::string id, new_id;
849 n1 = str.find (
"<path", ++n1);
850 std::string::size_type n2;
852 while (n1 != std::string::npos)
855 n1 = str.find (
"id='", n1) + 4;
856 n2 = str.find (
"'", n1);
857 id = str.substr (n1, n2-n1);
859 new_id = std::to_string (m_svg_def_index) +
"-" + id ;
861 str.replace (n1, n2-n1, new_id);
863 std::string::size_type n_ref = str.find (
"#" +
id);
865 while (n_ref != std::string::npos)
867 str.replace (n_ref + 1,
id.length (), new_id);
868 n_ref = str.find (
"#" +
id);
871 n1 = str.find (
"<path", n1);
876 n1 = str.find (
"<defs>");
877 n2 = str.find (
"</defs>") + 7;
879 std::string defs = str.substr (n1, n2-n1);
885 n1 = str.find (
"viewBox='") + 9;
886 if (n1 == std::string::npos)
887 return std::string ();
889 n2 = str.find (
" ", n1);
890 double original_x0 = std::stod (str.substr (n1, n2-n1));
893 n2 = str.find (
" ", n1);
894 double original_y0 = std::stod (str.substr (n1, n2-n1));
897 std::string orig_trans;
898 n1 = str.find (
"<g id='page1' transform='");
899 if (n1 != std::string::npos)
902 n2 = str.find (
"'", n1);
903 orig_trans = str.substr (n1, n2-n1);
908 n1 = str.find (
"<g id='page1'");
912 n2 = str.find (
"</g>", n1) + 4;
916 std::string tform = orig_trans;
919 tform = std::string (
"translate")
920 +
"(" + std::to_string (
box(0) - original_x0 + coord_pix(0))
921 +
"," + std::to_string (-(
box(3) +
box(1)) - original_y0 + coord_pix(1))
926 tform = std::string (
"rotate")
927 +
"(" + std::to_string (-rotation)
928 +
"," + std::to_string (coord_pix(0))
929 +
"," + std::to_string (coord_pix(1))
933 std::string fill =
"fill='rgb("
934 + std::to_string (
static_cast<uint8_t
> (color(0) * 255.0)) +
","
935 + std::to_string (
static_cast<uint8_t
> (color(1) * 255.0)) +
","
936 + std::to_string (
static_cast<uint8_t
> (color(2) * 255.0)) +
")' ";
938 std::string use_group =
"<g "
940 +
"transform='" + tform +
"'"
941 + str.substr (n1, n2-n1);
943 return defs +
"\n" + use_group;
947 gl2ps_renderer::strlist_to_svg (
double x,
double y,
double z,
949 std::list<text_renderer::string>& lst)
952 ColumnVector coord_pix = get_transform ().transform (
x, y, z,
false);
958 std::string svg = lst.front ().get_svg_element ();
960 return format_svg_element (svg,
box, rotation, coord_pix,
961 lst.front ().get_color ());
964 std::ostringstream os;
965 os << R
"(<g xml:space="preserve" )";
967 <<
"translate(" << coord_pix(0) +
box(0) <<
"," << coord_pix(1) -
box(1)
968 <<
") rotate(" << -rotation <<
"," << -
box(0) <<
"," <<
box(1)
972 auto p = lst.begin ();
973 std::string
name = p->get_family ();
974 std::string weight = p->get_weight ();
975 std::string angle = p->get_angle ();
976 double size = p->get_size ();
978 os <<
"font-family=\"" <<
name <<
"\" "
979 <<
"font-weight=\"" << weight <<
"\" "
980 <<
"font-style=\"" << angle <<
"\" "
981 <<
"font-size=\"" << size <<
"\">";
985 for (p = lst.begin (); p != lst.end (); p++)
989 if (
name.compare (p->get_family ()))
990 os <<
"font-family=\"" << p->get_family () <<
"\" ";
992 if (weight.compare (p->get_weight ()))
993 os <<
"font-weight=\"" << p->get_weight () <<
"\" ";
995 if (angle.compare (p->get_angle ()))
996 os <<
"font-style=\"" << p->get_angle () <<
"\" ";
998 if (size != p->get_size ())
999 os <<
"font-size=\"" << p->get_size () <<
"\" ";
1001 os <<
"y=\"" << - p->get_y () <<
"\" ";
1003 Matrix col = p->get_color ();
1004 os <<
"fill=\"rgb(" << col(0)*255 <<
","
1005 << col(1)*255 <<
"," << col(2)*255 <<
")\" ";
1009 std::vector<double> xdata = p->get_xdata ();
1010 for (
auto q = xdata.begin (); q != xdata.end (); q++)
1016 // translate unicode and special xml characters
1018 os << "&#" << p->get_code () << ";";
1021 const std::string str = p->get_string ();
1022 for (auto q = str.begin (); q != str.end (); q++)
1024 std::stringstream chr;
1026 if (chr.str () == "\"")
1028 else if (chr.str () == "'")
1030 else if (chr.str () == "&
")
1032 else if (chr.str () == "<
")
1034 else if (chr.str () == ">
")
1048 gl2ps_renderer::strlist_to_ps (double x, double y, double z,
1049 Matrix box, double rotation,
1050 std::list<text_renderer::string>& lst)
1054 else if (lst.size () == 1)
1056 static bool warned = false;
1057 // This may be an svg image, not handled in native eps format.
1058 if (! lst.front ().get_svg_element ().empty ())
1063 warning_with_id ("Octave:print:unhandled-svg-content
",
1064 "print: unhandled LaTeX strings.
"
1065 "Use -svgconvert option or -
d*latex* output
"
1072 // Translate and rotate coordinates in order to use bottom-left alignment
1073 fix_strlist_position (x, y, z, box, rotation, lst);
1074 Matrix prev_color (1, 3, -1);
1076 std::ostringstream ss;
1079 static bool warned = false;
1081 for (const auto& txtobj : lst)
1084 if (txtobj.get_color () != prev_color)
1086 prev_color = txtobj.get_color ();
1087 for (int i = 0; i < 3; i++)
1088 ss << prev_color(i) << " ";
1095 if (txtobj.get_code ())
1097 m_fontname = "Symbol
";
1098 str = code_to_symbol (txtobj.get_code ());
1102 m_fontname = select_font (txtobj.get_name (),
1103 txtobj.get_weight () == "bold
",
1104 txtobj.get_angle () == "italic
");
1106 // Check that the string is composed of single byte characters
1107 const std::string tmpstr = txtobj.get_string ();
1109 = reinterpret_cast<const uint8_t *> (tmpstr.c_str ());
1111 for (std::size_t i = 0; i < tmpstr.size ();)
1113 int mblen = octave_u8_strmblen_wrapper (c + i);
1115 // Replace multibyte or non ascii characters by a question mark
1121 warning_with_id ("Octave:print:unsupported-multibyte
",
1122 "print: only ASCII characters are
"
1123 "supported
for EPS and derived
"
1124 "formats. Use the
'-svgconvert' "
1125 "option
for better font support.
");
1135 warning_with_id ("Octave:print:unhandled-character
",
1136 "print: only ASCII characters are
"
1137 "supported
for EPS and derived
"
1138 "formats. Use the
'-svgconvert' "
1139 "option
for better font support.
");
1144 str += tmpstr.at (i);
1150 escape_character ("\\
", str);
1151 escape_character ("(
", str);
1152 escape_character (")
", str);
1154 ss << "(
" << str << ") [
";
1156 std::vector<double> xdata = txtobj.get_xdata ();
1157 for (std::size_t i = 1; i < xdata.size (); i++)
1158 ss << xdata[i] - xdata[i-1] << " ";
1160 ss << "10]
" << rotation << " " << txtobj.get_x ()
1161 << " " << txtobj.get_y () << " " << txtobj.get_size ()
1162 << " /
" << m_fontname << " SRX\n
";
1171 gl2ps_renderer::render_text (const std::string& txt,
1172 double x, double y, double z,
1173 int ha, int va, double rotation)
1175 std::string saved_font = m_fontname;
1178 return Matrix (1, 4, 0.0);
1181 std::string str = txt;
1182 std::list<text_renderer::string> lst;
1184 text_to_strlist (str, lst, bbox, ha, va, rotation);
1185 m_glfcns.glRasterPos3d (x, y, z);
1187 // For svg/eps directly dump a preformated text element into gl2ps output
1188 if (m_term.find ("svg
") != std::string::npos)
1190 std::string elt = strlist_to_svg (x, y, z, bbox, rotation, lst);
1192 gl2psSpecial (GL2PS_SVG, elt.c_str ());
1194 else if (m_term.find ("eps") != std::string::npos)
1196 std::string elt = strlist_to_ps (x, y, z, bbox, rotation, lst);
1198 gl2psSpecial (GL2PS_EPS, elt.c_str ());
1202 gl2psTextOpt (str.c_str (), m_fontname.c_str (), m_fontsize,
1203 alignment_to_mode (ha, va), rotation);
1205 m_fontname = saved_font;
1211 gl2ps_renderer::set_font (const base_properties& props)
1213 opengl_renderer::set_font (props);
1215 // Set the interpreter so that text_to_pixels can parse strings properly
1217 set_interpreter (props.get ("interpreter").string_value ());
1219 m_fontsize = props.get ("__fontsize_points__
").double_value ();
1221 caseless_str fn = props.get ("fontname
").xtolower ().string_value ();
1223 =(props.get ("fontweight
").xtolower ().string_value () == "bold
");
1225 = (props.get ("fontangle
").xtolower ().string_value () == "italic
");
1227 m_fontname = select_font (fn, isbold, isitalic);
1231 gl2ps_renderer::draw_image (const image::properties& props)
1233 octave_value cdata = props.get_color_data ();
1234 dim_vector dv (cdata.dims ());
1238 Matrix x = props.get_xdata ().matrix_value ();
1239 Matrix y = props.get_ydata ().matrix_value ();
1241 // Someone wants us to draw an empty image? No way.
1242 if (x.isempty () || y.isempty ())
1245 // Sort x/ydata and mark flipped dimensions
1249 std::swap (x(0), x(1));
1252 else if (w > 1 && x(1) == x(0))
1253 x(1) = x(1) + (w-1);
1258 std::swap (y(0), y(1));
1261 else if (h > 1 && y(1) == y(0))
1262 y(1) = y(1) + (h-1);
1265 const ColumnVector p0 = m_xform.transform (x(0), y(0), 0);
1266 const ColumnVector p1 = m_xform.transform (x(1), y(1), 0);
1268 if (math::isnan (p0(0)) || math::isnan (p0(1))
1269 || math::isnan (p1(0)) || math::isnan (p1(1)))
1271 warning ("opengl_renderer: image X,Y data too large to
draw");
1275 // image pixel size in screen pixel units
1276 float pix_dx, pix_dy;
1277 // image pixel size in normalized units
1278 float nor_dx, nor_dy;
1282 pix_dx = (p1(0) - p0(0)) / (w-1);
1283 nor_dx = (x(1) - x(0)) / (w-1);
1287 const ColumnVector p1w = m_xform.transform (x(1) + 1, y(1), 0);
1288 pix_dx = p1w(0) - p0(0);
1294 pix_dy = (p1(1) - p0(1)) / (h-1);
1295 nor_dy = (y(1) - y(0)) / (h-1);
1299 const ColumnVector p1h = m_xform.transform (x(1), y(1) + 1, 0);
1300 pix_dy = p1h(1) - p0(1);
1304 // OpenGL won't draw any of the image if its origin is outside the
1305 // viewport/clipping plane so we must do the clipping ourselves.
1307 int j0, j1, jj, i0, i1, ii;
1311 float im_xmin = x(0) - nor_dx/2;
1312 float im_xmax = x(1) + nor_dx/2;
1313 float im_ymin = y(0) - nor_dy/2;
1314 float im_ymax = y(1) + nor_dy/2;
1316 // Clip to axes or viewport
1317 bool do_clip = props.is_clipping ();
1318 Matrix vp = get_viewport_scaled ();
1320 ColumnVector vp_lim_min
1321 = m_xform.untransform (std::numeric_limits <float>::epsilon (),
1322 std::numeric_limits <float>::epsilon ());
1323 ColumnVector vp_lim_max = m_xform.untransform (vp(2), vp(3));
1325 if (vp_lim_min(0) > vp_lim_max(0))
1326 std::swap (vp_lim_min(0), vp_lim_max(0));
1328 if (vp_lim_min(1) > vp_lim_max(1))
1329 std::swap (vp_lim_min(1), vp_lim_max(1));
1332 = do_clip ? (vp_lim_min(0) > m_xmin ? vp_lim_min(0) : m_xmin) : vp_lim_min(0);
1335 = do_clip ? (vp_lim_min(1) > m_ymin ? vp_lim_min(1) : m_ymin) : vp_lim_min(1);
1338 = do_clip ? (vp_lim_max(0) < m_xmax ? vp_lim_max(0) : m_xmax) : vp_lim_max(0);
1341 = do_clip ? (vp_lim_max(1) < m_ymax ? vp_lim_max(1) : m_ymax) : vp_lim_max(1);
1343 if (im_xmin < clip_xmin)
1344 j0 += (clip_xmin - im_xmin)/nor_dx + 1;
1346 if (im_xmax > clip_xmax)
1347 j1 -= (im_xmax - clip_xmax)/nor_dx;
1349 if (im_ymin < clip_ymin)
1350 i0 += (clip_ymin - im_ymin)/nor_dy + 1;
1352 if (im_ymax > clip_ymax)
1353 i1 -= (im_ymax - clip_ymax)/nor_dy;
1355 if (i0 >= i1 || j0 >= j1)
1359 m_glfcns.glGetFloatv (GL_ZOOM_X, &zoom_x);
1361 m_glfcns.glGetFloatv (GL_ZOOM_Y, &zoom_y);
1363 m_glfcns.glPixelZoom (m_devpixratio * pix_dx, - m_devpixratio * pix_dy);
1364 m_glfcns.glRasterPos3d (im_xmin + nor_dx*j0, im_ymin + nor_dy*i0, 0);
1367 if (dv.ndims () == 3 && dv(2) == 3)
1369 if (cdata.is_double_type ())
1371 const NDArray xcdata = cdata.array_value ();
1373 OCTAVE_LOCAL_BUFFER (GLfloat, a,
1374 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1376 for (int i = i0; i < i1; i++)
1378 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1390 a[idx] = xcdata(ii, jj, 0);
1391 a[idx+1] = xcdata(ii, jj, 1);
1392 a[idx+2] = xcdata(ii, jj, 2);
1396 draw_pixels (j1-j0, i1-i0, a);
1399 else if (cdata.is_single_type ())
1401 const FloatNDArray xcdata = cdata.float_array_value ();
1403 OCTAVE_LOCAL_BUFFER (GLfloat, a,
1404 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1406 for (int i = i0; i < i1; i++)
1408 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1420 a[idx] = xcdata(ii, jj, 0);
1421 a[idx+1] = xcdata(ii, jj, 1);
1422 a[idx+2] = xcdata(ii, jj, 2);
1426 draw_pixels (j1-j0, i1-i0, a);
1429 else if (cdata.is_uint8_type ())
1431 const uint8NDArray xcdata = cdata.uint8_array_value ();
1433 OCTAVE_LOCAL_BUFFER (GLubyte, a,
1434 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1436 for (int i = i0; i < i1; i++)
1438 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1450 a[idx] = xcdata(ii, jj, 0);
1451 a[idx+1] = xcdata(ii, jj, 1);
1452 a[idx+2] = xcdata(ii, jj, 2);
1456 draw_pixels (j1-j0, i1-i0, a);
1459 else if (cdata.is_uint16_type ())
1461 const uint16NDArray xcdata = cdata.uint16_array_value ();
1463 OCTAVE_LOCAL_BUFFER (GLushort, a,
1464 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1466 for (int i = i0; i < i1; i++)
1468 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1480 a[idx] = xcdata(ii, jj, 0);
1481 a[idx+1] = xcdata(ii, jj, 1);
1482 a[idx+2] = xcdata(ii, jj, 2);
1486 draw_pixels (j1-j0, i1-i0, a);
1490 warning ("opengl_renderer: invalid image data type (expected
double, single, uint8, or uint16)
");
1492 m_glfcns.glPixelZoom (zoom_x, zoom_y);
1498 gl2ps_renderer::draw_pixels (int w, int h, const float *data)
1500 // Clip data between 0 and 1 for float values
1501 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1503 for (int i = 0; i < 3*h*w; i++)
1504 tmp_data[i] = (data[i] < 0.0f ? 0.0f : (data[i] > 1.0f ? 1.0f : data[i]));
1506 gl2psDrawPixels (w, h, 0, 0, GL_RGB, GL_FLOAT, tmp_data);
1510 gl2ps_renderer::draw_pixels (int w, int h, const uint8_t *data)
1512 // gl2psDrawPixels only supports the GL_FLOAT type.
1514 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1516 static const float maxval = std::numeric_limits<uint8_t>::max ();
1518 for (int i = 0; i < 3*w*h; i++)
1519 tmp_data[i] = data[i] / maxval;
1521 draw_pixels (w, h, tmp_data);
1525 gl2ps_renderer::draw_pixels (int w, int h, const uint16_t *data)
1527 // gl2psDrawPixels only supports the GL_FLOAT type.
1529 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1531 static const float maxval = std::numeric_limits<uint16_t>::max ();
1533 for (int i = 0; i < 3*w*h; i++)
1534 tmp_data[i] = data[i] / maxval;
1536 draw_pixels (w, h, tmp_data);
1540 gl2ps_renderer::draw_text (const text::properties& props)
1542 if (props.get_string ().isempty ())
1545 draw_text_background (props, true);
1547 // First set font properties: freetype will use them to compute
1548 // coordinates and gl2ps will retrieve the color directly from the
1551 set_color (props.get_color_rgb ());
1553 std::string saved_font = m_fontname;
1559 if (props.horizontalalignment_is ("center
"))
1561 else if (props.horizontalalignment_is ("right
"))
1564 if (props.verticalalignment_is ("top
"))
1566 else if (props.verticalalignment_is ("baseline
"))
1568 else if (props.verticalalignment_is ("middle
"))
1571 // FIXME: handle margin and surrounding box
1574 const Matrix pos = get_transform ().scale (props.get_data_position ());
1575 std::string str = props.get_string ().string_vector_value ().join ("\n
");
1577 render_text (str, pos(0), pos(1), pos.numel () > 2 ? pos(2) : 0.0,
1578 halign, valign, props.get_rotation ());
1586 // If the name of the stream begins with '|', open a pipe to the command
1587 // named by the rest of the string. Otherwise, write to the named file.
1590 gl2ps_print (opengl_functions& glfcns, const graphics_object& fig,
1591 const std::string& stream, const std::string& term)
1593#if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
1595 // FIXME: should we have a way to create a file that begins with the
1598 bool have_cmd = stream.length () > 1 && stream[0] == '|';
1600 FILE *m_fp = nullptr;
1602 unwind_protect frame;
1606 // Create process and pipe gl2ps output to it.
1608 std::string cmd = stream.substr (1);
1610 m_fp = popen (cmd.c_str (), "w");
1613 error (R"(print: failed to open
pipe "%s")
", stream.c_str ());
1615 // Need octave:: qualifier here to avoid ambiguity.
1616 frame.add ([=] () { octave::pclose (m_fp); });
1620 // Write gl2ps output directly to file.
1622 m_fp = sys::fopen (stream.c_str (), "w");
1625 error (R"(
gl2ps_print: failed to create file
"%s")
", stream.c_str ());
1627 frame.add ([=] () { std::fclose (m_fp); });
1630 gl2ps_renderer rend (glfcns, m_fp, term);
1632 Matrix pos = fig.get ("position
").matrix_value ();
1633 rend.set_viewport (pos(2), pos(3));
1634 rend.draw (fig, stream);
1636 // Make sure buffered commands are finished!!!
1641 octave_unused_parameter (glfcns);
1642 octave_unused_parameter (fig);
1643 octave_unused_parameter (stream);
1644 octave_unused_parameter (term);
octave_idx_type numel(void) const
Number of elements in the array.
virtual void set_linejoin(const std::string &)
virtual void draw_axes(const axes::properties &props)
virtual void draw(const graphics_object &go, bool toplevel=true)
virtual void set_polygon_offset(bool on, float offset=0.0f)
virtual void init_marker(const std::string &m, double size, float width)
virtual void set_linestyle(const std::string &s, bool stipple=false, double linewidth=0.5)
bool is_scalar_type(void) const
bool is_double_type(void) const
double double_value(bool frc_str_conv=false) const
void warning(const char *fmt,...)
void error(const char *fmt,...)
ColumnVector transform(const Matrix &m, double x, double y, double z)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
std::complex< double > w(std::complex< double > z, double relerr=0)
T::properties & properties(graphics_object obj)
Complex atan(const Complex &x)
OCTAVE_API bool strncmp(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same.
std::string dir_sep_chars(void)
std::string fgets(FILE *f)
static uint32_t state[624]
void gl2ps_print(opengl_functions &glfcns, const graphics_object &fig, const std::string &stream, const std::string &term)
gh_manager & __get_gh_manager__(const std::string &who)
T::size_type strlen(const typename T::value_type *str)
void draw(QDomElement &parent_elt, pdfpainter &painter)
static octave_value box(JNIEnv *jni_env, void *jobj, void *jcls_arg=nullptr)
Convert the Java object pointed to by jobj_arg with class jcls_arg to an Octave value.
void respond_to_pending_signals(void)
FILE * octave_tmpfile_wrapper(void)
int octave_ftruncate_wrapper(int fd, off_t sz)