26 #if defined (HAVE_CONFIG_H)
34 #if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
66 const std::string& _term)
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;
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;
404 error (
"gl2ps_renderer::draw: couldn't open temporary file for printing");
406 frame.
add ([=] () { std::fclose (tmpf); });
409 if (m_term.find (
"tex") == std::string::npos)
410 buffsize = 2*1024*1024;
414 m_buffer_overflow =
true;
416 while (m_buffer_overflow)
418 m_buffer_overflow =
false;
430 std::string include_graph;
432 std::size_t found_redirect = old_print_cmd.find (
'>');
434 if (found_redirect != std::string::npos)
435 include_graph = old_print_cmd.substr (found_redirect + 1);
437 include_graph = old_print_cmd;
439 std::size_t n_begin = include_graph.find_first_not_of (R
"( "')");
441 if (n_begin != std::string::npos)
444 std::size_t n_end = include_graph.find_last_not_of (R
"( "')");
445 include_graph = include_graph.substr (n_begin,
446 n_end - n_begin + 1);
449 include_graph = include_graph.substr (n_begin + 1);
452 include_graph =
"foobar-inc";
458 Matrix c = fprop.get_color_rgb ();
459 m_glfcns.glClearColor (c(0), c(1), c(2), 1);
462 set_device_pixel_ratio (fprop.get___device_pixel_ratio__ ());
465 GLint ret = gl2psBeginPage (plot_title.c_str (),
"Octave",
466 nullptr, gl2ps_term, gl2ps_sort,
469 | GL2PS_DRAW_BACKGROUND
470 | GL2PS_NO_PS3_SHADING
471 | GL2PS_USE_CURRENT_VIEWPORT),
472 GL_RGBA, 0,
nullptr, 0, 0, 0,
473 buffsize, tmpf, include_graph.c_str ());
474 if (ret == GL2PS_ERROR)
476 old_print_cmd.clear ();
477 error (
"gl2ps_renderer::draw: gl2psBeginPage returned GL2PS_ERROR");
482 if (m_buffer_overflow)
483 warning (
"gl2ps_renderer::draw: retrying with buffer size: %.1E B\n",
double (2*buffsize));
485 if (! m_buffer_overflow)
486 old_print_cmd = print_cmd;
497 std::size_t nread, nwrite;
502 const char *fcn =
"/SRX { gsave FCT moveto rotate xshow grestore } BD\n";
503 bool header_found = ! (m_term.find (
"eps") != std::string::npos
504 || m_term.find (
"svg") != std::string::npos);
506 while (! feof (tmpf) && nread)
508 if (! header_found &&
std::fgets (str, 8192, tmpf))
511 nread = std::fread (str, 1, 8192, tmpf);
515 if (! header_found &&
std::strncmp (str,
"/SBCR", 5) == 0)
518 nwrite = std::fwrite (fcn, 1,
strlen (fcn), m_fp);
519 if (nwrite !=
strlen (fcn))
523 error (
"gl2ps_renderer::draw: internal pipe error");
526 else if (m_term.find (
"svg") != std::string::npos)
536 std::string srchstr (str, nread);
537 std::size_t pos = srchstr.find (
"<svg ");
538 if (! header_found && pos != std::string::npos)
541 pos = srchstr.find (
"px");
542 if (pos != std::string::npos)
544 srchstr[pos+1] =
't';
546 pos = srchstr.find (
"px", pos);
547 srchstr[pos+1] =
't';
548 std::strcpy (str, srchstr.c_str ());
553 nwrite = std::fwrite (str, 1, nread, m_fp);
558 error (
"gl2ps_renderer::draw: internal pipe error");
568 gl2ps_renderer::alignment_to_mode (
int ha,
int va)
const
570 int gl2psa = GL2PS_TEXT_BL;
574 if (va == 0 || va == 3)
575 gl2psa=GL2PS_TEXT_BL;
577 gl2psa=GL2PS_TEXT_TL;
579 gl2psa=GL2PS_TEXT_CL;
583 if (va == 0 || va == 3)
584 gl2psa=GL2PS_TEXT_BR;
586 gl2psa=GL2PS_TEXT_TR;
588 gl2psa=GL2PS_TEXT_CR;
592 if (va == 0 || va == 3)
604 gl2ps_renderer::fix_strlist_position (
double x,
double y,
double z,
606 std::list<text_renderer::string>& lst)
608 for (
auto& txtobj : lst)
611 ColumnVector coord_pix = get_transform ().transform (
x, y, z,
false);
614 double rot = rotation * 4.0 *
atan (1.0) / 180;
615 coord_pix(0) += (txtobj.get_x () +
box(0))* cos (rot)
616 - (txtobj.get_y () +
box(1))* sin (rot);
617 coord_pix(1) -= (txtobj.get_y () +
box(1))* cos (rot)
618 + (txtobj.get_x () +
box(0))* sin (rot);
621 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
623 txtobj.set_x (coord_pix(0));
624 txtobj.set_y (vp[3] - coord_pix(1));
625 txtobj.set_z (coord_pix(2));
630 code_to_symbol (uint32_t code)
634 uint32_t idx = code - 945;
637 std::string characters (
"abgdezhqiklmnxoprVstufcyw");
638 retval = characters[idx];
645 std::string characters (
"ABGDEZHQIKLMNXOPRVSTUFCYW");
646 retval = characters[idx];
648 else if (code == 978)
650 else if (code == 215)
652 else if (code == 177)
654 else if (code == 8501)
656 else if (code == 8465)
658 else if (code == 8242)
660 else if (code == 8736)
662 else if (code == 172)
664 else if (code == 9829)
666 else if (code == 8472)
668 else if (code == 8706)
670 else if (code == 8704)
672 else if (code == 9827)
674 else if (code == 9824)
676 else if (code == 8476)
678 else if (code == 8734)
680 else if (code == 8730)
682 else if (code == 8707)
684 else if (code == 9830)
686 else if (code == 8747)
688 else if (code == 8727)
690 else if (code == 8744)
692 else if (code == 8855)
694 else if (code == 8901)
696 else if (code == 8728)
698 else if (code == 8745)
700 else if (code == 8743)
702 else if (code == 8856)
704 else if (code == 8729)
706 else if (code == 8746)
708 else if (code == 8853)
710 else if (code == 8804)
712 else if (code == 8712)
714 else if (code == 8839)
716 else if (code == 8801)
718 else if (code == 8773)
720 else if (code == 8834)
722 else if (code == 8805)
724 else if (code == 8715)
726 else if (code == 8764)
728 else if (code == 8733)
730 else if (code == 8838)
732 else if (code == 8835)
734 else if (code == 8739)
736 else if (code == 8776)
738 else if (code == 8869)
740 else if (code == 8656)
742 else if (code == 8592)
744 else if (code == 8658)
746 else if (code == 8594)
748 else if (code == 8596)
750 else if (code == 8593)
752 else if (code == 8595)
754 else if (code == 8970)
756 else if (code == 8971)
758 else if (code == 10216)
760 else if (code == 10217)
762 else if (code == 8968)
764 else if (code == 8969)
766 else if (code == 8800)
768 else if (code == 8230)
770 else if (code == 176)
772 else if (code == 8709)
774 else if (code == 169)
778 warning (
"print: unhandled symbol %d", code);
784 select_font (
caseless_str fn,
bool isbold,
bool isitalic)
787 std::string fontname;
788 if (fn ==
"times" || fn ==
"times-roman")
790 if (isitalic && isbold)
791 fontname =
"Times-BoldItalic";
793 fontname =
"Times-Italic";
795 fontname =
"Times-Bold";
797 fontname =
"Times-Roman";
799 else if (fn ==
"courier")
801 if (isitalic && isbold)
802 fontname =
"Courier-BoldOblique";
804 fontname =
"Courier-Oblique";
806 fontname =
"Courier-Bold";
808 fontname =
"Courier";
810 else if (fn ==
"symbol")
812 else if (fn ==
"zapfdingbats")
813 fontname =
"ZapfDingbats";
816 if (isitalic && isbold)
817 fontname =
"Helvetica-BoldOblique";
819 fontname =
"Helvetica-Oblique";
821 fontname =
"Helvetica-Bold";
823 fontname =
"Helvetica";
829 escape_character (
const std::string chr, std::string& str)
831 std::size_t idx = str.find (chr);
832 while (idx != std::string::npos)
834 str.insert (idx, 1,
'\\');
835 idx = str.find (chr, idx + 2);
840 gl2ps_renderer::format_svg_element (std::string str,
Matrix box,
846 std::string::size_type n1 = str.find (
"<defs>");
847 if (n1 == std::string::npos)
848 return std::string ();
850 std::string id, new_id;
851 n1 = str.find (
"<path", ++n1);
852 std::string::size_type n2;
854 while (n1 != std::string::npos)
857 n1 = str.find (
"id='", n1) + 4;
858 n2 = str.find (
"'", n1);
859 id = str.substr (n1, n2-n1);
861 new_id = std::to_string (m_svg_def_index) +
"-" + id ;
863 str.replace (n1, n2-n1, new_id);
865 std::string::size_type n_ref = str.find (
"#" +
id);
867 while (n_ref != std::string::npos)
869 str.replace (n_ref + 1,
id.length (), new_id);
870 n_ref = str.find (
"#" +
id);
873 n1 = str.find (
"<path", n1);
878 n1 = str.find (
"<defs>");
879 n2 = str.find (
"</defs>") + 7;
881 std::string defs = str.substr (n1, n2-n1);
887 n1 = str.find (
"viewBox='") + 9;
888 if (n1 == std::string::npos)
889 return std::string ();
891 n2 = str.find (
" ", n1);
892 double original_x0 = std::stod (str.substr (n1, n2-n1));
895 n2 = str.find (
" ", n1);
896 double original_y0 = std::stod (str.substr (n1, n2-n1));
899 std::string orig_trans;
900 n1 = str.find (
"<g id='page1' transform='");
901 if (n1 != std::string::npos)
904 n2 = str.find (
"'", n1);
905 orig_trans = str.substr (n1, n2-n1);
910 n1 = str.find (
"<g id='page1'");
914 n2 = str.find (
"</g>", n1) + 4;
918 std::string tform = orig_trans;
921 tform = std::string (
"translate")
922 +
"(" + std::to_string (
box(0) - original_x0 + coord_pix(0))
923 +
"," + std::to_string (-(
box(3) +
box(1)) - original_y0 + coord_pix(1))
928 tform = std::string (
"rotate")
929 +
"(" + std::to_string (-rotation)
930 +
"," + std::to_string (coord_pix(0))
931 +
"," + std::to_string (coord_pix(1))
935 std::string fill =
"fill='rgb("
936 + std::to_string (
static_cast<uint8_t
> (color(0) * 255.0)) +
","
937 + std::to_string (
static_cast<uint8_t
> (color(1) * 255.0)) +
","
938 + std::to_string (
static_cast<uint8_t
> (color(2) * 255.0)) +
")' ";
940 std::string use_group =
"<g "
942 +
"transform='" + tform +
"'"
943 + str.substr (n1, n2-n1);
945 return defs +
"\n" + use_group;
949 gl2ps_renderer::strlist_to_svg (
double x,
double y,
double z,
951 std::list<text_renderer::string>& lst)
954 ColumnVector coord_pix = get_transform ().transform (
x, y, z,
false);
960 std::string svg = lst.front ().get_svg_element ();
962 return format_svg_element (svg,
box, rotation, coord_pix,
963 lst.front ().get_color ());
966 std::ostringstream os;
967 os << R
"(<g xml:space="preserve" )";
969 <<
"translate(" << coord_pix(0) +
box(0) <<
"," << coord_pix(1) -
box(1)
970 <<
") rotate(" << -rotation <<
"," << -
box(0) <<
"," <<
box(1)
974 auto p = lst.begin ();
975 std::string name = p->get_family ();
976 std::string weight = p->get_weight ();
977 std::string angle = p->get_angle ();
978 double size = p->get_size ();
980 os <<
"font-family=\"" << name <<
"\" "
981 <<
"font-weight=\"" << weight <<
"\" "
982 <<
"font-style=\"" << angle <<
"\" "
983 <<
"font-size=\"" << size <<
"\">";
987 for (p = lst.begin (); p != lst.end (); p++)
991 if (name.compare (p->get_family ()))
992 os <<
"font-family=\"" << p->get_family () <<
"\" ";
994 if (weight.compare (p->get_weight ()))
995 os <<
"font-weight=\"" << p->get_weight () <<
"\" ";
997 if (angle.compare (p->get_angle ()))
998 os <<
"font-style=\"" << p->get_angle () <<
"\" ";
1000 if (size != p->get_size ())
1001 os <<
"font-size=\"" << p->get_size () <<
"\" ";
1003 os <<
"y=\"" << - p->get_y () <<
"\" ";
1005 Matrix col = p->get_color ();
1006 os <<
"fill=\"rgb(" << col(0)*255 <<
","
1007 << col(1)*255 <<
"," << col(2)*255 <<
")\" ";
1011 std::vector<double> xdata = p->get_xdata ();
1012 for (
auto q = xdata.begin (); q != xdata.end (); q++)
1018 // translate unicode and special xml characters
1020 os << "&#" << p->get_code () << ";";
1023 const std::string str = p->get_string ();
1024 for (auto q = str.begin (); q != str.end (); q++)
1026 std::stringstream chr;
1028 if (chr.str () == "\"")
1030 else if (chr.str () == "'")
1032 else if (chr.str () == "&
")
1034 else if (chr.str () == "<
")
1036 else if (chr.str () == ">
")
1050 gl2ps_renderer::strlist_to_ps (double x, double y, double z,
1051 Matrix box, double rotation,
1052 std::list<text_renderer::string>& lst)
1056 else if (lst.size () == 1)
1058 static bool warned = false;
1059 // This may be an svg image, not handled in native eps format.
1060 if (! lst.front ().get_svg_element ().empty ())
1065 warning_with_id ("Octave:print:unhandled-svg-content
",
1066 "print: unhandled LaTeX strings.
"
1067 "Use -svgconvert option or -
d*latex* output
"
1074 // Translate and rotate coordinates in order to use bottom-left alignment
1075 fix_strlist_position (x, y, z, box, rotation, lst);
1076 Matrix prev_color (1, 3, -1);
1078 std::ostringstream ss;
1081 static bool warned = false;
1083 for (const auto& txtobj : lst)
1086 if (txtobj.get_color () != prev_color)
1088 prev_color = txtobj.get_color ();
1089 for (int i = 0; i < 3; i++)
1090 ss << prev_color(i) << " ";
1097 if (txtobj.get_code ())
1099 m_fontname = "Symbol
";
1100 str = code_to_symbol (txtobj.get_code ());
1104 m_fontname = select_font (txtobj.get_name (),
1105 txtobj.get_weight () == "bold
",
1106 txtobj.get_angle () == "italic
");
1108 // Check that the string is composed of single byte characters
1109 const std::string tmpstr = txtobj.get_string ();
1111 = reinterpret_cast<const uint8_t *> (tmpstr.c_str ());
1113 for (std::size_t i = 0; i < tmpstr.size ();)
1115 int mblen = octave_u8_strmblen_wrapper (c + i);
1117 // Replace multibyte or non ascii characters by a question mark
1123 warning_with_id ("Octave:print:unsupported-multibyte
",
1124 "print: only ASCII characters are
"
1125 "supported
for EPS and derived
"
1126 "formats. Use the
'-svgconvert' "
1127 "option
for better font support.
");
1137 warning_with_id ("Octave:print:unhandled-character
",
1138 "print: only ASCII characters are
"
1139 "supported
for EPS and derived
"
1140 "formats. Use the
'-svgconvert' "
1141 "option
for better font support.
");
1146 str += tmpstr.at (i);
1152 escape_character ("\\
", str);
1153 escape_character ("(
", str);
1154 escape_character (")
", str);
1156 ss << "(
" << str << ") [
";
1158 std::vector<double> xdata = txtobj.get_xdata ();
1159 for (std::size_t i = 1; i < xdata.size (); i++)
1160 ss << xdata[i] - xdata[i-1] << " ";
1162 ss << "10]
" << rotation << " " << txtobj.get_x ()
1163 << " " << txtobj.get_y () << " " << txtobj.get_size ()
1164 << " /
" << m_fontname << " SRX\n";
1173 gl2ps_renderer::render_text (const std::string& txt,
1174 double x, double y, double z,
1175 int ha, int va, double rotation)
1177 std::string saved_font = m_fontname;
1180 return Matrix (1, 4, 0.0);
1183 std::string str = txt;
1184 std::list<text_renderer::string> lst;
1186 text_to_strlist (str, lst, bbox, ha, va, rotation);
1187 m_glfcns.glRasterPos3d (x, y, z);
1189 // For svg/eps directly dump a preformated text element into gl2ps output
1190 if (m_term.find ("svg
") != std::string::npos)
1192 std::string elt = strlist_to_svg (x, y, z, bbox, rotation, lst);
1194 gl2psSpecial (GL2PS_SVG, elt.c_str ());
1196 else if (m_term.find ("eps") != std::string::npos)
1198 std::string elt = strlist_to_ps (x, y, z, bbox, rotation, lst);
1200 gl2psSpecial (GL2PS_EPS, elt.c_str ());
1204 gl2psTextOpt (str.c_str (), m_fontname.c_str (), m_fontsize,
1205 alignment_to_mode (ha, va), rotation);
1207 m_fontname = saved_font;
1213 gl2ps_renderer::set_font (const base_properties& props)
1215 opengl_renderer::set_font (props);
1217 // Set the interpreter so that text_to_pixels can parse strings properly
1219 set_interpreter (props.get ("interpreter").string_value ());
1221 m_fontsize = props.get ("__fontsize_points__
").double_value ();
1223 caseless_str fn = props.get ("fontname
").xtolower ().string_value ();
1225 =(props.get ("fontweight
").xtolower ().string_value () == "bold
");
1227 = (props.get ("fontangle
").xtolower ().string_value () == "italic
");
1229 m_fontname = select_font (fn, isbold, isitalic);
1233 gl2ps_renderer::draw_image (const image::properties& props)
1235 octave_value cdata = props.get_color_data ();
1236 dim_vector dv (cdata.dims ());
1240 Matrix x = props.get_xdata ().matrix_value ();
1241 Matrix y = props.get_ydata ().matrix_value ();
1243 // Someone wants us to draw an empty image? No way.
1244 if (x.isempty () || y.isempty ())
1247 // Sort x/ydata and mark flipped dimensions
1251 std::swap (x(0), x(1));
1254 else if (w > 1 && x(1) == x(0))
1255 x(1) = x(1) + (w-1);
1260 std::swap (y(0), y(1));
1263 else if (h > 1 && y(1) == y(0))
1264 y(1) = y(1) + (h-1);
1267 const ColumnVector p0 = m_xform.transform (x(0), y(0), 0);
1268 const ColumnVector p1 = m_xform.transform (x(1), y(1), 0);
1270 if (math::isnan (p0(0)) || math::isnan (p0(1))
1271 || math::isnan (p1(0)) || math::isnan (p1(1)))
1277 // image pixel size in screen pixel units
1278 float pix_dx, pix_dy;
1279 // image pixel size in normalized units
1280 float nor_dx, nor_dy;
1284 pix_dx = (p1(0) - p0(0)) / (w-1);
1285 nor_dx = (x(1) - x(0)) / (w-1);
1289 const ColumnVector p1w = m_xform.transform (x(1) + 1, y(1), 0);
1290 pix_dx = p1w(0) - p0(0);
1296 pix_dy = (p1(1) - p0(1)) / (h-1);
1297 nor_dy = (y(1) - y(0)) / (h-1);
1301 const ColumnVector p1h = m_xform.transform (x(1), y(1) + 1, 0);
1302 pix_dy = p1h(1) - p0(1);
1306 // OpenGL won't draw any of the image if its origin is outside the
1307 // viewport/clipping plane so we must do the clipping ourselves.
1309 int j0, j1, jj, i0, i1, ii;
1313 float im_xmin = x(0) - nor_dx/2;
1314 float im_xmax = x(1) + nor_dx/2;
1315 float im_ymin = y(0) - nor_dy/2;
1316 float im_ymax = y(1) + nor_dy/2;
1318 // Clip to axes or viewport
1319 bool do_clip = props.is_clipping ();
1320 Matrix vp = get_viewport_scaled ();
1322 ColumnVector vp_lim_min
1323 = m_xform.untransform (std::numeric_limits <float>::epsilon (),
1324 std::numeric_limits <float>::epsilon ());
1325 ColumnVector vp_lim_max = m_xform.untransform (vp(2), vp(3));
1327 if (vp_lim_min(0) > vp_lim_max(0))
1328 std::swap (vp_lim_min(0), vp_lim_max(0));
1330 if (vp_lim_min(1) > vp_lim_max(1))
1331 std::swap (vp_lim_min(1), vp_lim_max(1));
1334 = do_clip ? (vp_lim_min(0) > m_xmin ? vp_lim_min(0) : m_xmin)
1338 = do_clip ? (vp_lim_min(1) > m_ymin ? vp_lim_min(1) : m_ymin)
1342 = do_clip ? (vp_lim_max(0) < m_xmax ? vp_lim_max(0) : m_xmax)
1346 = do_clip ? (vp_lim_max(1) < m_ymax ? vp_lim_max(1) : m_ymax)
1349 if (im_xmin < clip_xmin)
1350 j0 += (clip_xmin - im_xmin)/nor_dx + 1;
1352 if (im_xmax > clip_xmax)
1353 j1 -= (im_xmax - clip_xmax)/nor_dx;
1355 if (im_ymin < clip_ymin)
1356 i0 += (clip_ymin - im_ymin)/nor_dy + 1;
1358 if (im_ymax > clip_ymax)
1359 i1 -= (im_ymax - clip_ymax)/nor_dy;
1361 if (i0 >= i1 || j0 >= j1)
1365 m_glfcns.glGetFloatv (GL_ZOOM_X, &zoom_x);
1367 m_glfcns.glGetFloatv (GL_ZOOM_Y, &zoom_y);
1369 m_glfcns.glPixelZoom (m_devpixratio * pix_dx, - m_devpixratio * pix_dy);
1370 m_glfcns.glRasterPos3d (im_xmin + nor_dx*j0, im_ymin + nor_dy*i0, 0);
1373 if (dv.ndims () == 3 && dv(2) == 3)
1375 if (cdata.is_double_type ())
1377 const NDArray xcdata = cdata.array_value ();
1379 OCTAVE_LOCAL_BUFFER (GLfloat, a,
1380 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1382 for (int i = i0; i < i1; i++)
1384 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1396 a[idx] = xcdata(ii, jj, 0);
1397 a[idx+1] = xcdata(ii, jj, 1);
1398 a[idx+2] = xcdata(ii, jj, 2);
1402 draw_pixels (j1-j0, i1-i0, a);
1405 else if (cdata.is_single_type ())
1407 const FloatNDArray xcdata = cdata.float_array_value ();
1409 OCTAVE_LOCAL_BUFFER (GLfloat, a,
1410 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1412 for (int i = i0; i < i1; i++)
1414 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1426 a[idx] = xcdata(ii, jj, 0);
1427 a[idx+1] = xcdata(ii, jj, 1);
1428 a[idx+2] = xcdata(ii, jj, 2);
1432 draw_pixels (j1-j0, i1-i0, a);
1435 else if (cdata.is_uint8_type ())
1437 const uint8NDArray xcdata = cdata.uint8_array_value ();
1439 OCTAVE_LOCAL_BUFFER (GLubyte, a,
1440 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1442 for (int i = i0; i < i1; i++)
1444 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1456 a[idx] = xcdata(ii, jj, 0);
1457 a[idx+1] = xcdata(ii, jj, 1);
1458 a[idx+2] = xcdata(ii, jj, 2);
1462 draw_pixels (j1-j0, i1-i0, a);
1465 else if (cdata.is_uint16_type ())
1467 const uint16NDArray xcdata = cdata.uint16_array_value ();
1469 OCTAVE_LOCAL_BUFFER (GLushort, a,
1470 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1472 for (int i = i0; i < i1; i++)
1474 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1486 a[idx] = xcdata(ii, jj, 0);
1487 a[idx+1] = xcdata(ii, jj, 1);
1488 a[idx+2] = xcdata(ii, jj, 2);
1492 draw_pixels (j1-j0, i1-i0, a);
1496 warning ("opengl_renderer: invalid image data type (expected
double, single, uint8, or uint16)
");
1498 m_glfcns.glPixelZoom (zoom_x, zoom_y);
1504 gl2ps_renderer::draw_pixels (int w, int h, const float *data)
1506 // Clip data between 0 and 1 for float values
1507 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1509 for (int i = 0; i < 3*h*w; i++)
1510 tmp_data[i] = (data[i] < 0.0f ? 0.0f : (data[i] > 1.0f ? 1.0f : data[i]));
1512 gl2psDrawPixels (w, h, 0, 0, GL_RGB, GL_FLOAT, tmp_data);
1516 gl2ps_renderer::draw_pixels (int w, int h, const uint8_t *data)
1518 // gl2psDrawPixels only supports the GL_FLOAT type.
1520 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1522 static const float maxval = std::numeric_limits<uint8_t>::max ();
1524 for (int i = 0; i < 3*w*h; i++)
1525 tmp_data[i] = data[i] / maxval;
1527 draw_pixels (w, h, tmp_data);
1531 gl2ps_renderer::draw_pixels (int w, int h, const uint16_t *data)
1533 // gl2psDrawPixels only supports the GL_FLOAT type.
1535 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1537 static const float maxval = std::numeric_limits<uint16_t>::max ();
1539 for (int i = 0; i < 3*w*h; i++)
1540 tmp_data[i] = data[i] / maxval;
1542 draw_pixels (w, h, tmp_data);
1546 gl2ps_renderer::draw_text (const text::properties& props)
1548 if (props.get_string ().isempty ())
1551 draw_text_background (props, true);
1553 // First set font properties: freetype will use them to compute
1554 // coordinates and gl2ps will retrieve the color directly from the
1557 set_color (props.get_color_rgb ());
1559 std::string saved_font = m_fontname;
1565 if (props.horizontalalignment_is ("center
"))
1567 else if (props.horizontalalignment_is ("right
"))
1570 if (props.verticalalignment_is ("top
"))
1572 else if (props.verticalalignment_is ("baseline
"))
1574 else if (props.verticalalignment_is ("middle
"))
1577 // FIXME: handle margin and surrounding box
1580 const Matrix pos = get_transform ().scale (props.get_data_position ());
1581 std::string str = props.get_string ().string_vector_value ().join ("\n");
1583 render_text (str, pos(0), pos(1), pos.numel () > 2 ? pos(2) : 0.0,
1584 halign, valign, props.get_rotation ());
1587 OCTAVE_END_NAMESPACE(octave)
1591 OCTAVE_BEGIN_NAMESPACE(octave)
1593 // If the name of the stream begins with '|', open a pipe to the command
1594 // named by the rest of the string. Otherwise, write to the named file.
1597 gl2ps_print (opengl_functions& glfcns, const graphics_object& fig,
1598 const std::string& stream, const std::string& term)
1600 #if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
1602 // FIXME: should we have a way to create a file that begins with the
1605 bool have_cmd = stream.length () > 1 && stream[0] == '|';
1607 FILE *m_fp = nullptr;
1609 unwind_protect frame;
1613 // Create process and pipe gl2ps output to it.
1615 std::string cmd = stream.substr (1);
1617 m_fp = popen (cmd.c_str (), "w");
1620 error (R"(print: failed to open
pipe "%s")
", stream.c_str ());
1622 // Need octave:: qualifier here to avoid ambiguity.
1623 frame.add ([=] () { octave::pclose (m_fp); });
1627 // Write gl2ps output directly to file.
1629 m_fp = sys::fopen (stream.c_str (), "w");
1632 error (R"(
gl2ps_print: failed to create file
"%s")
", stream.c_str ());
1634 frame.add ([=] () { std::fclose (m_fp); });
1637 gl2ps_renderer rend (glfcns, m_fp, term);
1639 Matrix pos = fig.get ("position
").matrix_value ();
1640 rend.set_viewport (pos(2), pos(3));
1641 rend.draw (fig, stream);
1643 // Make sure buffered commands are finished!!!
1648 octave_unused_parameter (glfcns);
1649 octave_unused_parameter (fig);
1650 octave_unused_parameter (stream);
1651 octave_unused_parameter (term);
1658 OCTAVE_END_NAMESPACE(octave)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
void add(F &&fcn, Args &&... args)
bool is_scalar_type(void) const
bool is_double_type(void) const
double double_value(bool frc_str_conv=false) const
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)
virtual void draw(const graphics_object &go, bool toplevel=true)
virtual void draw_axes(const axes::properties &props)
virtual void set_polygon_offset(bool on, float offset=0.0f)
virtual void set_linejoin(const std::string &)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning(const char *fmt,...)
void error(const char *fmt,...)
std::string tempnam(const std::string &dir, const std::string &pfx)
void gl2ps_print(opengl_functions &glfcns, const graphics_object &fig, const std::string &stream, const std::string &term)
ColumnVector transform(const Matrix &m, double x, double y, double z)
gh_manager & __get_gh_manager__(void)
Complex atan(const Complex &x)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
std::FILE * fopen_tmp(const std::string &name, const std::string &mode)
std::string fgets(FILE *f)
static std::string get_temp_directory(void)
std::complex< double > w(std::complex< double > z, double relerr=0)
T::properties & properties(graphics_object obj)
T::size_type strlen(const typename T::value_type *str)
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.
void draw(QDomElement &parent_elt, pdfpainter &painter)
static octave_value box(JNIEnv *jni_env, void *jobj, void *jcls_arg=nullptr)
static uint32_t state[624]
static std::string dir_sep_chars
void respond_to_pending_signals(void)
int octave_ftruncate_wrapper(int fd, off_t sz)