26 #if defined (HAVE_CONFIG_H)
34 #if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
67 const std::string& _term)
69 m_fontname (), m_buffer_overflow (false), m_svg_def_index (0)
72 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (gl2ps_renderer)
74 ~gl2ps_renderer () =
default;
87 Matrix render_text (
const std::string& txt,
88 double x,
double y,
double z,
89 int halign,
int valign,
double rotation = 0.0);
104 if (go.
isa (
"axes") || go.
isa (
"hggroup"))
114 else if (go.
isa (
"patch") || go.
isa (
"surface"))
121 else if (go.
isa (
"scatter"))
136 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
137 gl2psBeginViewport (vp);
142 gl2psGetOptions (&opts);
145 opts &= ~GL2PS_OCCLUSION_CULL;
149 gl2psEnable (GL2PS_BLEND);
153 opts |= GL2PS_OCCLUSION_CULL;
154 gl2psDisable (GL2PS_BLEND);
157 gl2psSetOptions (opts);
165 GLint state = gl2psEndViewport ();
166 if (state == GL2PS_NO_FEEDBACK && props.
is_visible ())
167 warning (
"gl2ps_renderer::draw_axes: empty feedback buffer and/or nothing else to print");
168 else if (state == GL2PS_ERROR)
169 error (
"gl2ps_renderer::draw_axes: gl2psEndPage returned GL2PS_ERROR");
171 m_buffer_overflow |= (state == GL2PS_OVERFLOW);
175 gl2psGetOptions (&opts);
176 opts &= ~GL2PS_DRAW_BACKGROUND;
177 gl2psSetOptions (opts);
183 void draw_pixels (
int w,
int h,
const float *data);
184 void draw_pixels (
int w,
int h,
const uint8_t *data);
185 void draw_pixels (
int w,
int h,
const uint16_t *data);
187 void init_marker (
const std::string&
m,
double size,
float width)
193 if (
m ==
"o" ||
m ==
"v" ||
m ==
"^" ||
m ==
">" ||
m ==
"<" ||
m ==
"h"
194 ||
m ==
"hexagram" ||
m ==
"p" ||
m ==
"pentagram")
196 set_linejoin (
"round");
197 set_linecap (
"round");
201 set_linejoin (
"miter");
202 set_linecap (
"square");
206 void set_linestyle (
const std::string& s,
bool use_stipple =
false,
207 double linewidth = 0.5)
211 if (s ==
"-" && ! use_stipple)
212 gl2psDisable (GL2PS_LINE_STIPPLE);
214 gl2psEnable (GL2PS_LINE_STIPPLE);
217 void set_linecap (
const std::string& s)
221 #if defined (HAVE_GL2PSLINEJOIN)
223 gl2psLineCap (GL2PS_LINE_CAP_BUTT);
224 else if (s ==
"square")
225 gl2psLineCap (GL2PS_LINE_CAP_SQUARE);
226 else if (s ==
"round")
227 gl2psLineCap (GL2PS_LINE_CAP_ROUND);
231 void set_linejoin (
const std::string& s)
235 #if defined (HAVE_GL2PSLINEJOIN)
237 gl2psLineJoin (GL2PS_LINE_JOIN_ROUND);
238 else if (s ==
"miter")
239 gl2psLineJoin (GL2PS_LINE_JOIN_MITER);
240 else if (s ==
"chamfer")
241 gl2psLineJoin (GL2PS_LINE_JOIN_BEVEL);
245 void set_polygon_offset (
bool on,
float offset = 0.0f)
250 gl2psEnable (GL2PS_POLYGON_OFFSET_FILL);
254 gl2psDisable (GL2PS_POLYGON_OFFSET_FILL);
259 void set_linewidth (
float w)
268 void fix_strlist_position (
double x,
double y,
double z,
269 Matrix box,
double rotation,
270 std::list<text_renderer::string>& lst);
273 std::string format_svg_element (std::string str,
Matrix bbox,
277 std::string strlist_to_svg (
double x,
double y,
double z,
Matrix box,
279 std::list<text_renderer::string>& lst);
282 std::string strlist_to_ps (
double x,
double y,
double z,
Matrix box,
284 std::list<text_renderer::string>& lst);
286 int alignment_to_mode (
int ha,
int va)
const;
291 std::string m_fontname;
292 bool m_buffer_overflow;
293 std::size_t m_svg_def_index;
308 if (go.
isa (
"figure") || go.
isa (
"uipanel"))
318 else if (go.
isa (
"axes"))
340 if (go.
isa (
"figure"))
354 static bool in_draw =
false;
355 static std::string old_print_cmd;
356 static GLint buffsize;
366 GLint gl2ps_term = GL2PS_PS;
367 if (m_term.find (
"eps") != std::string::npos)
368 gl2ps_term = GL2PS_EPS;
369 else if (m_term.find (
"pdf") != std::string::npos)
370 gl2ps_term = GL2PS_PDF;
371 else if (m_term.find (
"ps") != std::string::npos)
372 gl2ps_term = GL2PS_PS;
373 else if (m_term.find (
"svg") != std::string::npos)
374 gl2ps_term = GL2PS_SVG;
375 else if (m_term.find (
"pgf") != std::string::npos)
376 gl2ps_term = GL2PS_PGF;
377 else if (m_term.find (
"tex") != std::string::npos)
378 gl2ps_term = GL2PS_TEX;
380 warning (
"gl2ps_renderer::draw: Unknown terminal %s, using 'ps'",
383 GLint gl2ps_text = 0;
384 if (m_term.find (
"notxt") != std::string::npos)
385 gl2ps_text = GL2PS_NO_TEXT;
389 std::string plot_title = get_title (myhandle);
390 if (plot_title.empty ())
391 plot_title =
"Octave plot";
394 GLint gl2ps_sort = GL2PS_BSP_SORT;
398 if (has_2D_axes (myhandle))
399 gl2ps_sort = GL2PS_NO_SORT;
402 std::string tmpfile (
sys::tempnam (sys::env::get_temp_directory (),
407 error (
"gl2ps_renderer::draw: couldn't open temporary file for printing");
409 frame.
add ([=] () { std::fclose (tmpf); });
412 if (m_term.find (
"tex") == std::string::npos)
413 buffsize = 2*1024*1024;
417 m_buffer_overflow =
true;
419 while (m_buffer_overflow)
421 m_buffer_overflow =
false;
433 std::string include_graph;
435 std::size_t found_redirect = old_print_cmd.find (
'>');
437 if (found_redirect != std::string::npos)
438 include_graph = old_print_cmd.substr (found_redirect + 1);
440 include_graph = old_print_cmd;
442 std::size_t n_begin = include_graph.find_first_not_of (R
"( "')");
444 if (n_begin != std::string::npos)
447 std::size_t n_end = include_graph.find_last_not_of (R
"( "')");
448 include_graph = include_graph.substr (n_begin,
449 n_end - n_begin + 1);
452 include_graph = include_graph.substr (n_begin + 1);
455 include_graph =
"foobar-inc";
462 m_glfcns.glClearColor (c(0), c(1), c(2), 1);
468 GLint ret = gl2psBeginPage (plot_title.c_str (),
"Octave",
469 nullptr, gl2ps_term, gl2ps_sort,
472 | GL2PS_DRAW_BACKGROUND
473 | GL2PS_NO_PS3_SHADING
474 | GL2PS_USE_CURRENT_VIEWPORT),
475 GL_RGBA, 0,
nullptr, 0, 0, 0,
476 buffsize, tmpf, include_graph.c_str ());
477 if (ret == GL2PS_ERROR)
479 old_print_cmd.clear ();
480 error (
"gl2ps_renderer::draw: gl2psBeginPage returned GL2PS_ERROR");
485 if (m_buffer_overflow)
486 warning (
"gl2ps_renderer::draw: retrying with buffer size: %.1E B\n",
double (2*buffsize));
488 if (! m_buffer_overflow)
489 old_print_cmd = print_cmd;
500 std::size_t nread, nwrite;
505 const char *fcn =
"/SRX { gsave FCT moveto rotate xshow grestore } BD\n";
506 bool header_found = ! (m_term.find (
"eps") != std::string::npos
507 || m_term.find (
"svg") != std::string::npos);
509 while (! feof (tmpf) && nread)
511 if (! header_found &&
std::fgets (str, 8192, tmpf))
514 nread = std::fread (str, 1, 8192, tmpf);
518 if (! header_found &&
std::strncmp (str,
"/SBCR", 5) == 0)
521 nwrite = std::fwrite (fcn, 1,
strlen (fcn), m_fp);
522 if (nwrite !=
strlen (fcn))
526 error (
"gl2ps_renderer::draw: internal pipe error");
529 else if (m_term.find (
"svg") != std::string::npos)
539 std::string srchstr (str, nread);
540 std::size_t pos = srchstr.find (
"<svg ");
541 if (! header_found && pos != std::string::npos)
544 pos = srchstr.find (
"px");
545 if (pos != std::string::npos)
547 srchstr[pos+1] =
't';
549 pos = srchstr.find (
"px", pos);
550 srchstr[pos+1] =
't';
551 std::strcpy (str, srchstr.c_str ());
556 nwrite = std::fwrite (str, 1, nread, m_fp);
561 error (
"gl2ps_renderer::draw: internal pipe error");
571 gl2ps_renderer::alignment_to_mode (
int ha,
int va)
const
573 int gl2psa = GL2PS_TEXT_BL;
577 if (va == 0 || va == 3)
578 gl2psa=GL2PS_TEXT_BL;
580 gl2psa=GL2PS_TEXT_TL;
582 gl2psa=GL2PS_TEXT_CL;
586 if (va == 0 || va == 3)
587 gl2psa=GL2PS_TEXT_BR;
589 gl2psa=GL2PS_TEXT_TR;
591 gl2psa=GL2PS_TEXT_CR;
595 if (va == 0 || va == 3)
607 gl2ps_renderer::fix_strlist_position (
double x,
double y,
double z,
608 Matrix box,
double rotation,
609 std::list<text_renderer::string>& lst)
611 for (
auto& txtobj : lst)
614 ColumnVector coord_pix = get_transform ().transform (
x, y, z,
false);
617 double rot = rotation * 4.0 *
atan (1.0) / 180;
618 coord_pix(0) += (txtobj.get_x () + box(0))* cos (rot)
619 - (txtobj.get_y () + box(1))* sin (rot);
620 coord_pix(1) -= (txtobj.get_y () + box(1))* cos (rot)
621 + (txtobj.get_x () + box(0))* sin (rot);
624 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
626 txtobj.set_x (coord_pix(0));
627 txtobj.set_y (vp[3] - coord_pix(1));
628 txtobj.set_z (coord_pix(2));
633 code_to_symbol (uint32_t code)
637 uint32_t idx = code - 945;
640 std::string characters (
"abgdezhqiklmnxoprVstufcyw");
641 retval = characters[idx];
648 std::string characters (
"ABGDEZHQIKLMNXOPRVSTUFCYW");
649 retval = characters[idx];
651 else if (code == 978)
653 else if (code == 215)
655 else if (code == 177)
657 else if (code == 8501)
659 else if (code == 8465)
661 else if (code == 8242)
663 else if (code == 8736)
665 else if (code == 172)
667 else if (code == 9829)
669 else if (code == 8472)
671 else if (code == 8706)
673 else if (code == 8704)
675 else if (code == 9827)
677 else if (code == 9824)
679 else if (code == 8476)
681 else if (code == 8734)
683 else if (code == 8730)
685 else if (code == 8707)
687 else if (code == 9830)
689 else if (code == 8747)
691 else if (code == 8727)
693 else if (code == 8744)
695 else if (code == 8855)
697 else if (code == 8901)
699 else if (code == 8728)
701 else if (code == 8745)
703 else if (code == 8743)
705 else if (code == 8856)
707 else if (code == 8729)
709 else if (code == 8746)
711 else if (code == 8853)
713 else if (code == 8804)
715 else if (code == 8712)
717 else if (code == 8839)
719 else if (code == 8801)
721 else if (code == 8773)
723 else if (code == 8834)
725 else if (code == 8805)
727 else if (code == 8715)
729 else if (code == 8764)
731 else if (code == 8733)
733 else if (code == 8838)
735 else if (code == 8835)
737 else if (code == 8739)
739 else if (code == 8776)
741 else if (code == 8869)
743 else if (code == 8656)
745 else if (code == 8592)
747 else if (code == 8658)
749 else if (code == 8594)
751 else if (code == 8596)
753 else if (code == 8593)
755 else if (code == 8595)
757 else if (code == 8970)
759 else if (code == 8971)
761 else if (code == 10216)
763 else if (code == 10217)
765 else if (code == 8968)
767 else if (code == 8969)
769 else if (code == 8800)
771 else if (code == 8230)
773 else if (code == 176)
775 else if (code == 8709)
777 else if (code == 169)
781 warning (
"print: unhandled symbol %d", code);
787 select_font (
caseless_str fn,
bool isbold,
bool isitalic)
790 std::string fontname;
791 if (fn ==
"times" || fn ==
"times-roman")
793 if (isitalic && isbold)
794 fontname =
"Times-BoldItalic";
796 fontname =
"Times-Italic";
798 fontname =
"Times-Bold";
800 fontname =
"Times-Roman";
802 else if (fn ==
"courier")
804 if (isitalic && isbold)
805 fontname =
"Courier-BoldOblique";
807 fontname =
"Courier-Oblique";
809 fontname =
"Courier-Bold";
811 fontname =
"Courier";
813 else if (fn ==
"symbol")
815 else if (fn ==
"zapfdingbats")
816 fontname =
"ZapfDingbats";
819 if (isitalic && isbold)
820 fontname =
"Helvetica-BoldOblique";
822 fontname =
"Helvetica-Oblique";
824 fontname =
"Helvetica-Bold";
826 fontname =
"Helvetica";
832 escape_character (
const std::string chr, std::string& str)
834 std::size_t idx = str.find (chr);
835 while (idx != std::string::npos)
837 str.insert (idx, 1,
'\\');
838 idx = str.find (chr, idx + 2);
843 gl2ps_renderer::format_svg_element (std::string str,
Matrix box,
849 std::string::size_type n1 = str.find (
"<defs>");
850 if (n1 == std::string::npos)
851 return std::string ();
853 std::string id, new_id;
854 n1 = str.find (
"<path", ++n1);
855 std::string::size_type n2;
857 while (n1 != std::string::npos)
860 n1 = str.find (
"id='", n1) + 4;
861 n2 = str.find (
"'", n1);
862 id = str.substr (n1, n2-n1);
864 new_id = std::to_string (m_svg_def_index) +
"-" + id ;
866 str.replace (n1, n2-n1, new_id);
868 std::string::size_type n_ref = str.find (
"#" +
id);
870 while (n_ref != std::string::npos)
872 str.replace (n_ref + 1,
id.length (), new_id);
873 n_ref = str.find (
"#" +
id);
876 n1 = str.find (
"<path", n1);
881 n1 = str.find (
"<defs>");
882 n2 = str.find (
"</defs>") + 7;
884 std::string defs = str.substr (n1, n2-n1);
890 n1 = str.find (
"viewBox='") + 9;
891 if (n1 == std::string::npos)
892 return std::string ();
894 n2 = str.find (
" ", n1);
895 double original_x0 = std::stod (str.substr (n1, n2-n1));
898 n2 = str.find (
" ", n1);
899 double original_y0 = std::stod (str.substr (n1, n2-n1));
902 std::string orig_trans;
903 n1 = str.find (
"<g id='page1' transform='");
904 if (n1 != std::string::npos)
907 n2 = str.find (
"'", n1);
908 orig_trans = str.substr (n1, n2-n1);
913 n1 = str.find (
"<g id='page1'");
917 n2 = str.find (
"</g>", n1) + 4;
921 std::string tform = orig_trans;
924 tform = std::string (
"translate")
925 +
"(" + std::to_string (box(0) - original_x0 + coord_pix(0))
926 +
"," + std::to_string (-(box(3) + box(1)) - original_y0 + coord_pix(1))
931 tform = std::string (
"rotate")
932 +
"(" + std::to_string (-rotation)
933 +
"," + std::to_string (coord_pix(0))
934 +
"," + std::to_string (coord_pix(1))
938 std::string fill =
"fill='rgb("
939 + std::to_string (
static_cast<uint8_t
> (color(0) * 255.0)) +
","
940 + std::to_string (
static_cast<uint8_t
> (color(1) * 255.0)) +
","
941 + std::to_string (
static_cast<uint8_t
> (color(2) * 255.0)) +
")' ";
943 std::string use_group =
"<g "
945 +
"transform='" + tform +
"'"
946 + str.substr (n1, n2-n1);
948 return defs +
"\n" + use_group;
952 gl2ps_renderer::strlist_to_svg (
double x,
double y,
double z,
953 Matrix box,
double rotation,
954 std::list<text_renderer::string>& lst)
957 ColumnVector coord_pix = get_transform ().transform (
x, y, z,
false);
963 std::string svg = lst.front ().get_svg_element ();
965 return format_svg_element (svg, box, rotation, coord_pix,
966 lst.front ().get_color ());
969 std::ostringstream os;
970 os << R
"(<g xml:space="preserve" )";
972 <<
"translate(" << coord_pix(0) + box(0) <<
"," << coord_pix(1) - box(1)
973 <<
") rotate(" << -rotation <<
"," << -box(0) <<
"," << box(1)
977 auto p = lst.begin ();
978 std::string name = p->get_family ();
979 std::string weight = p->get_weight ();
980 std::string angle = p->get_angle ();
981 double size = p->get_size ();
983 os <<
"font-family=\"" << name <<
"\" "
984 <<
"font-weight=\"" << weight <<
"\" "
985 <<
"font-style=\"" << angle <<
"\" "
986 <<
"font-size=\"" << size <<
"\">";
990 for (p = lst.begin (); p != lst.end (); p++)
994 if (name.compare (p->get_family ()))
995 os <<
"font-family=\"" << p->get_family () <<
"\" ";
997 if (weight.compare (p->get_weight ()))
998 os <<
"font-weight=\"" << p->get_weight () <<
"\" ";
1000 if (angle.compare (p->get_angle ()))
1001 os <<
"font-style=\"" << p->get_angle () <<
"\" ";
1003 if (size != p->get_size ())
1004 os <<
"font-size=\"" << p->get_size () <<
"\" ";
1006 os <<
"y=\"" << - p->get_y () <<
"\" ";
1008 Matrix col = p->get_color ();
1009 os <<
"fill=\"rgb(" << col(0)*255 <<
","
1010 << col(1)*255 <<
"," << col(2)*255 <<
")\" ";
1014 std::vector<double> xdata = p->get_xdata ();
1015 for (
const auto& q : xdata)
1021 // translate unicode and special xml characters
1023 os << "&#" << p->get_code () << ";";
1026 const std::string str = p->get_string ();
1027 for (const auto& q : str)
1029 std::stringstream chr;
1031 if (chr.str () == "\"")
1033 else if (chr.str () == "'")
1035 else if (chr.str () == "&
")
1037 else if (chr.str () == "<
")
1039 else if (chr.str () == ">
")
1053 gl2ps_renderer::strlist_to_ps (double x, double y, double z,
1054 Matrix box, double rotation,
1055 std::list<text_renderer::string>& lst)
1059 else if (lst.size () == 1)
1061 static bool warned = false;
1062 // This may be an svg image, not handled in native eps format.
1063 if (! lst.front ().get_svg_element ().empty ())
1068 warning_with_id ("Octave:print:unhandled-svg-content
",
1069 "print: unhandled LaTeX strings.
"
1070 "Use -svgconvert option or -
d*latex* output
"
1077 // Translate and rotate coordinates in order to use bottom-left alignment
1078 fix_strlist_position (x, y, z, box, rotation, lst);
1079 Matrix prev_color (1, 3, -1);
1081 std::ostringstream ss;
1084 static bool warned = false;
1086 for (const auto& txtobj : lst)
1089 if (txtobj.get_color () != prev_color)
1091 prev_color = txtobj.get_color ();
1092 for (int i = 0; i < 3; i++)
1093 ss << prev_color(i) << " ";
1100 if (txtobj.get_code ())
1102 m_fontname = "Symbol
";
1103 str = code_to_symbol (txtobj.get_code ());
1107 m_fontname = select_font (txtobj.get_name (),
1108 txtobj.get_weight () == "bold
",
1109 txtobj.get_angle () == "italic
");
1111 // Check that the string is composed of single byte characters
1112 const std::string tmpstr = txtobj.get_string ();
1114 = reinterpret_cast<const uint8_t *> (tmpstr.c_str ());
1116 for (std::size_t i = 0; i < tmpstr.size ();)
1118 int mblen = octave_u8_strmblen_wrapper (c + i);
1120 // Replace multibyte or non ascii characters by a question mark
1126 warning_with_id ("Octave:print:unsupported-multibyte
",
1127 "print: only ASCII characters are
"
1128 "supported
for EPS and derived
"
1129 "formats. Use the
'-svgconvert' "
1130 "option
for better font support.
");
1140 warning_with_id ("Octave:print:unhandled-character
",
1141 "print: only ASCII characters are
"
1142 "supported
for EPS and derived
"
1143 "formats. Use the
'-svgconvert' "
1144 "option
for better font support.
");
1149 str += tmpstr.at (i);
1155 escape_character ("\\
", str);
1156 escape_character ("(
", str);
1157 escape_character (")
", str);
1159 ss << "(
" << str << ") [
";
1161 std::vector<double> xdata = txtobj.get_xdata ();
1162 for (std::size_t i = 1; i < xdata.size (); i++)
1163 ss << xdata[i] - xdata[i-1] << " ";
1165 ss << "10]
" << rotation << " " << txtobj.get_x ()
1166 << " " << txtobj.get_y () << " " << txtobj.get_size ()
1167 << " /
" << m_fontname << " SRX\n";
1176 gl2ps_renderer::render_text (const std::string& txt,
1177 double x, double y, double z,
1178 int ha, int va, double rotation)
1180 std::string saved_font = m_fontname;
1183 return Matrix (1, 4, 0.0);
1186 std::string str = txt;
1187 std::list<text_renderer::string> lst;
1189 text_to_strlist (str, lst, bbox, ha, va, rotation);
1190 m_glfcns.glRasterPos3d (x, y, z);
1192 // For svg/eps directly dump a preformated text element into gl2ps output
1193 if (m_term.find ("svg
") != std::string::npos)
1195 std::string elt = strlist_to_svg (x, y, z, bbox, rotation, lst);
1197 gl2psSpecial (GL2PS_SVG, elt.c_str ());
1199 else if (m_term.find ("eps") != std::string::npos)
1201 std::string elt = strlist_to_ps (x, y, z, bbox, rotation, lst);
1203 gl2psSpecial (GL2PS_EPS, elt.c_str ());
1207 gl2psTextOpt (str.c_str (), m_fontname.c_str (), m_fontsize,
1208 alignment_to_mode (ha, va), rotation);
1210 m_fontname = saved_font;
1216 gl2ps_renderer::set_font (const base_properties& props)
1218 opengl_renderer::set_font (props);
1220 // Set the interpreter so that text_to_pixels can parse strings properly
1222 set_interpreter (props.get ("interpreter").string_value ());
1224 m_fontsize = props.get ("__fontsize_points__
").double_value ();
1226 caseless_str fn = props.get ("fontname
").xtolower ().string_value ();
1228 =(props.get ("fontweight
").xtolower ().string_value () == "bold
");
1230 = (props.get ("fontangle
").xtolower ().string_value () == "italic
");
1232 m_fontname = select_font (fn, isbold, isitalic);
1236 gl2ps_renderer::draw_image (const image::properties& props)
1238 octave_value cdata = props.get_color_data ();
1239 dim_vector dv (cdata.dims ());
1243 Matrix x = props.get_xdata ().matrix_value ();
1244 Matrix y = props.get_ydata ().matrix_value ();
1246 // Someone wants us to draw an empty image? No way.
1247 if (x.isempty () || y.isempty ())
1250 // Sort x/ydata and mark flipped dimensions
1254 std::swap (x(0), x(1));
1257 else if (w > 1 && x(1) == x(0))
1258 x(1) = x(1) + (w-1);
1263 std::swap (y(0), y(1));
1266 else if (h > 1 && y(1) == y(0))
1267 y(1) = y(1) + (h-1);
1270 const ColumnVector p0 = m_xform.transform (x(0), y(0), 0);
1271 const ColumnVector p1 = m_xform.transform (x(1), y(1), 0);
1273 if (math::isnan (p0(0)) || math::isnan (p0(1))
1274 || math::isnan (p1(0)) || math::isnan (p1(1)))
1280 // image pixel size in screen pixel units
1281 float pix_dx, pix_dy;
1282 // image pixel size in normalized units
1283 float nor_dx, nor_dy;
1287 pix_dx = (p1(0) - p0(0)) / (w-1);
1288 nor_dx = (x(1) - x(0)) / (w-1);
1292 const ColumnVector p1w = m_xform.transform (x(1) + 1, y(1), 0);
1293 pix_dx = p1w(0) - p0(0);
1299 pix_dy = (p1(1) - p0(1)) / (h-1);
1300 nor_dy = (y(1) - y(0)) / (h-1);
1304 const ColumnVector p1h = m_xform.transform (x(1), y(1) + 1, 0);
1305 pix_dy = p1h(1) - p0(1);
1309 // OpenGL won't draw any of the image if its origin is outside the
1310 // viewport/clipping plane so we must do the clipping ourselves.
1312 int j0, j1, jj, i0, i1, ii;
1316 float im_xmin = x(0) - nor_dx/2;
1317 float im_xmax = x(1) + nor_dx/2;
1318 float im_ymin = y(0) - nor_dy/2;
1319 float im_ymax = y(1) + nor_dy/2;
1321 // Clip to axes or viewport
1322 bool do_clip = props.is_clipping ();
1323 Matrix vp = get_viewport_scaled ();
1325 ColumnVector vp_lim_min
1326 = m_xform.untransform (std::numeric_limits <float>::epsilon (),
1327 std::numeric_limits <float>::epsilon ());
1328 ColumnVector vp_lim_max = m_xform.untransform (vp(2), vp(3));
1330 if (vp_lim_min(0) > vp_lim_max(0))
1331 std::swap (vp_lim_min(0), vp_lim_max(0));
1333 if (vp_lim_min(1) > vp_lim_max(1))
1334 std::swap (vp_lim_min(1), vp_lim_max(1));
1337 = do_clip ? (vp_lim_min(0) > m_xmin ? vp_lim_min(0) : m_xmin)
1341 = do_clip ? (vp_lim_min(1) > m_ymin ? vp_lim_min(1) : m_ymin)
1345 = do_clip ? (vp_lim_max(0) < m_xmax ? vp_lim_max(0) : m_xmax)
1349 = do_clip ? (vp_lim_max(1) < m_ymax ? vp_lim_max(1) : m_ymax)
1352 if (im_xmin < clip_xmin)
1353 j0 += (clip_xmin - im_xmin)/nor_dx + 1;
1355 if (im_xmax > clip_xmax)
1356 j1 -= (im_xmax - clip_xmax)/nor_dx;
1358 if (im_ymin < clip_ymin)
1359 i0 += (clip_ymin - im_ymin)/nor_dy + 1;
1361 if (im_ymax > clip_ymax)
1362 i1 -= (im_ymax - clip_ymax)/nor_dy;
1364 if (i0 >= i1 || j0 >= j1)
1368 m_glfcns.glGetFloatv (GL_ZOOM_X, &zoom_x);
1370 m_glfcns.glGetFloatv (GL_ZOOM_Y, &zoom_y);
1372 m_glfcns.glPixelZoom (m_devpixratio * pix_dx, - m_devpixratio * pix_dy);
1373 m_glfcns.glRasterPos3d (im_xmin + nor_dx*j0, im_ymin + nor_dy*i0, 0);
1376 if (dv.ndims () == 3 && dv(2) == 3)
1378 if (cdata.is_double_type ())
1380 const NDArray xcdata = cdata.array_value ();
1382 OCTAVE_LOCAL_BUFFER (GLfloat, a,
1383 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1385 for (int i = i0; i < i1; i++)
1387 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1399 a[idx] = xcdata(ii, jj, 0);
1400 a[idx+1] = xcdata(ii, jj, 1);
1401 a[idx+2] = xcdata(ii, jj, 2);
1405 draw_pixels (j1-j0, i1-i0, a);
1408 else if (cdata.is_single_type ())
1410 const FloatNDArray xcdata = cdata.float_array_value ();
1412 OCTAVE_LOCAL_BUFFER (GLfloat, a,
1413 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1415 for (int i = i0; i < i1; i++)
1417 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1429 a[idx] = xcdata(ii, jj, 0);
1430 a[idx+1] = xcdata(ii, jj, 1);
1431 a[idx+2] = xcdata(ii, jj, 2);
1435 draw_pixels (j1-j0, i1-i0, a);
1438 else if (cdata.is_uint8_type ())
1440 const uint8NDArray xcdata = cdata.uint8_array_value ();
1442 OCTAVE_LOCAL_BUFFER (GLubyte, a,
1443 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1445 for (int i = i0; i < i1; i++)
1447 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1459 a[idx] = xcdata(ii, jj, 0);
1460 a[idx+1] = xcdata(ii, jj, 1);
1461 a[idx+2] = xcdata(ii, jj, 2);
1465 draw_pixels (j1-j0, i1-i0, a);
1468 else if (cdata.is_uint16_type ())
1470 const uint16NDArray xcdata = cdata.uint16_array_value ();
1472 OCTAVE_LOCAL_BUFFER (GLushort, a,
1473 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1475 for (int i = i0; i < i1; i++)
1477 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1489 a[idx] = xcdata(ii, jj, 0);
1490 a[idx+1] = xcdata(ii, jj, 1);
1491 a[idx+2] = xcdata(ii, jj, 2);
1495 draw_pixels (j1-j0, i1-i0, a);
1499 warning ("opengl_renderer: invalid
image data type (expected
double, single, uint8, or uint16)
");
1501 m_glfcns.glPixelZoom (zoom_x, zoom_y);
1507 gl2ps_renderer::draw_pixels (int w, int h, const float *data)
1509 // Clip data between 0 and 1 for float values
1510 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1512 for (int i = 0; i < 3*h*w; i++)
1513 tmp_data[i] = (data[i] < 0.0f ? 0.0f : (data[i] > 1.0f ? 1.0f : data[i]));
1515 gl2psDrawPixels (w, h, 0, 0, GL_RGB, GL_FLOAT, tmp_data);
1519 gl2ps_renderer::draw_pixels (int w, int h, const uint8_t *data)
1521 // gl2psDrawPixels only supports the GL_FLOAT type.
1523 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1525 static const float maxval = std::numeric_limits<uint8_t>::max ();
1527 for (int i = 0; i < 3*w*h; i++)
1528 tmp_data[i] = data[i] / maxval;
1530 draw_pixels (w, h, tmp_data);
1534 gl2ps_renderer::draw_pixels (int w, int h, const uint16_t *data)
1536 // gl2psDrawPixels only supports the GL_FLOAT type.
1538 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1540 static const float maxval = std::numeric_limits<uint16_t>::max ();
1542 for (int i = 0; i < 3*w*h; i++)
1543 tmp_data[i] = data[i] / maxval;
1545 draw_pixels (w, h, tmp_data);
1549 gl2ps_renderer::draw_text (const text::properties& props)
1551 if (props.get_string ().isempty ())
1554 draw_text_background (props, true);
1556 // First set font properties: freetype will use them to compute
1557 // coordinates and gl2ps will retrieve the color directly from the
1560 set_color (props.get_color_rgb ());
1562 std::string saved_font = m_fontname;
1568 if (props.horizontalalignment_is ("center
"))
1570 else if (props.horizontalalignment_is ("right
"))
1573 if (props.verticalalignment_is ("top
"))
1575 else if (props.verticalalignment_is ("baseline
"))
1577 else if (props.verticalalignment_is ("middle
"))
1580 // FIXME: handle margin and surrounding box
1583 const Matrix pos = get_transform ().scale (props.get_data_position ());
1584 std::string str = props.get_string ().string_vector_value ().join ("\n");
1586 render_text (str, pos(0), pos(1), pos.numel () > 2 ? pos(2) : 0.0,
1587 halign, valign, props.get_rotation ());
1590 OCTAVE_END_NAMESPACE(octave)
1594 OCTAVE_BEGIN_NAMESPACE(octave)
1596 // If the name of the stream begins with '|', open a pipe to the command
1597 // named by the rest of the string. Otherwise, write to the named file.
1600 gl2ps_print (opengl_functions& glfcns, const graphics_object& fig,
1601 const std::string& stream, const std::string& term)
1603 #if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
1605 // FIXME: should we have a way to create a file that begins with the
1608 bool have_cmd = stream.length () > 1 && stream[0] == '|';
1610 FILE *m_fp = nullptr;
1612 unwind_protect frame;
1616 // Create process and pipe gl2ps output to it.
1618 std::string cmd = stream.substr (1);
1620 m_fp = popen (cmd.c_str (), "w");
1623 error (R"(print: failed to open
pipe "%s")
", stream.c_str ());
1625 // Need octave:: qualifier here to avoid ambiguity.
1626 frame.add ([=] () { octave::pclose (m_fp); });
1630 // Write gl2ps output directly to file.
1632 m_fp = sys::fopen (stream.c_str (), "w");
1635 error (R"(
gl2ps_print: failed to create file
"%s")
", stream.c_str ());
1637 frame.add ([=] () { std::fclose (m_fp); });
1640 gl2ps_renderer rend (glfcns, m_fp, term);
1642 Matrix pos = fig.get ("position
").matrix_value ();
1643 rend.set_viewport (pos(2), pos(3));
1644 rend.draw (fig, stream);
1646 // Make sure buffered commands are finished!!!
1651 octave_unused_parameter (glfcns);
1652 octave_unused_parameter (fig);
1653 octave_unused_parameter (stream);
1654 octave_unused_parameter (term);
1661 OCTAVE_END_NAMESPACE(octave)
octave_idx_type numel() const
Number of elements in the array.
void add(F &&fcn, Args &&... args)
bool get_is2D(bool include_kids=false) const
graphics_handle get___myhandle__() const
graphics_object get_object(double val) const
octave_value get(bool all=false) const
base_properties & get_properties()
bool isa(const std::string &go_name) const
bool valid_object() const
bool is_scalar_type() const
bool is_double_type() const
Matrix matrix_value(bool frc_str_conv=false) 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 dir_sep_chars()
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__()
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)
std::complex< double > w(std::complex< double > z, double relerr=0)
std::string tempnam(const std::string &dir, const std::string &pfx)
T::size_type strlen(const typename T::value_type *str)
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)
void respond_to_pending_signals()
int octave_ftruncate_wrapper(int fd, off_t sz)