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 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (gl2ps_renderer)
73 ~gl2ps_renderer () =
default;
82 void draw (
const graphics_object& go,
const std::string& print_cmd);
87 double x,
double y,
double z,
88 int halign,
int valign,
double rotation = 0.0);
90 void set_font (
const base_properties& props);
100 if (! go.valid_object ())
103 if (go.isa (
"axes") || go.isa (
"hggroup"))
105 Matrix children = go.get (
"children").matrix_value ();
113 else if (go.isa (
"patch") || go.isa (
"surface"))
120 else if (go.isa (
"scatter"))
131 void draw_axes (
const axes::properties& props)
135 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
136 gl2psBeginViewport (vp);
141 gl2psGetOptions (&opts);
142 if (has_alpha (props.get___myhandle__ ()))
144 opts &= ~GL2PS_OCCLUSION_CULL;
148 gl2psEnable (GL2PS_BLEND);
152 opts |= GL2PS_OCCLUSION_CULL;
153 gl2psDisable (GL2PS_BLEND);
156 gl2psSetOptions (opts);
164 GLint state = gl2psEndViewport ();
165 if (state == GL2PS_NO_FEEDBACK && props.is_visible ())
166 warning (
"gl2ps_renderer::draw_axes: empty feedback buffer and/or nothing else to print");
167 else if (state == GL2PS_ERROR)
168 error (
"gl2ps_renderer::draw_axes: gl2psEndPage returned GL2PS_ERROR");
170 m_buffer_overflow |= (state == GL2PS_OVERFLOW);
174 gl2psGetOptions (&opts);
175 opts &= ~GL2PS_DRAW_BACKGROUND;
176 gl2psSetOptions (opts);
179 void draw_text (
const text::properties& props);
181 void draw_image (
const image::properties& props);
182 void draw_pixels (
int w,
int h,
const float *data);
183 void draw_pixels (
int w,
int h,
const uint8_t *data);
184 void draw_pixels (
int w,
int h,
const uint16_t *data);
186 void init_marker (
const std::string& m,
double size,
float width)
192 if (m ==
"o" || m ==
"v" || m ==
"^" || m ==
">" || m ==
"<" || m ==
"h"
193 || m ==
"hexagram" || m ==
"p" || m ==
"pentagram")
205 void set_linestyle (
const std::string& s,
bool use_stipple =
false,
206 double linewidth = 0.5)
210 if (s ==
"-" && ! use_stipple)
211 gl2psDisable (GL2PS_LINE_STIPPLE);
213 gl2psEnable (GL2PS_LINE_STIPPLE);
220#if defined (HAVE_GL2PSLINEJOIN)
222 gl2psLineCap (GL2PS_LINE_CAP_BUTT);
223 else if (s ==
"square")
224 gl2psLineCap (GL2PS_LINE_CAP_SQUARE);
225 else if (s ==
"round")
226 gl2psLineCap (GL2PS_LINE_CAP_ROUND);
234#if defined (HAVE_GL2PSLINEJOIN)
236 gl2psLineJoin (GL2PS_LINE_JOIN_ROUND);
237 else if (s ==
"miter")
238 gl2psLineJoin (GL2PS_LINE_JOIN_MITER);
239 else if (s ==
"chamfer")
240 gl2psLineJoin (GL2PS_LINE_JOIN_BEVEL);
249 gl2psEnable (GL2PS_POLYGON_OFFSET_FILL);
253 gl2psDisable (GL2PS_POLYGON_OFFSET_FILL);
267 void fix_strlist_position (
double x,
double y,
double z,
268 Matrix box,
double rotation,
269 std::list<text_renderer::string>& lst);
272 std::string format_svg_element (std::string str,
Matrix bbox,
276 std::string strlist_to_svg (
double x,
double y,
double z,
Matrix box,
278 std::list<text_renderer::string>& lst);
281 std::string strlist_to_ps (
double x,
double y,
double z,
Matrix box,
283 std::list<text_renderer::string>& lst);
285 int alignment_to_mode (
int ha,
int va)
const;
290 std::string m_fontname;
291 bool m_buffer_overflow;
292 std::size_t m_svg_def_index;
304 if (! go.valid_object ())
307 if (go.isa (
"figure") || go.isa (
"uipanel"))
309 Matrix children = go.get (
"children").matrix_value ();
317 else if (go.isa (
"axes"))
320 =
reinterpret_cast<axes::properties&
> (go.get_properties ());
321 retval = ap.get_is2D (
true);
336 if (! go.valid_object ())
339 if (go.isa (
"figure"))
341 figure::properties& fp
342 =
reinterpret_cast<figure::properties&
> (go.get_properties ());
344 retval = fp.get_title ();
351gl2ps_renderer::draw (
const graphics_object& go,
const std::string& print_cmd)
353 static bool in_draw =
false;
354 static std::string old_print_cmd;
355 static GLint buffsize;
365 GLint gl2ps_term = GL2PS_PS;
366 if (m_term.find (
"eps") != std::string::npos)
367 gl2ps_term = GL2PS_EPS;
368 else if (m_term.find (
"pdf") != std::string::npos)
369 gl2ps_term = GL2PS_PDF;
370 else if (m_term.find (
"ps") != std::string::npos)
371 gl2ps_term = GL2PS_PS;
372 else if (m_term.find (
"svg") != std::string::npos)
373 gl2ps_term = GL2PS_SVG;
374 else if (m_term.find (
"pgf") != std::string::npos)
375 gl2ps_term = GL2PS_PGF;
376 else if (m_term.find (
"tex") != std::string::npos)
377 gl2ps_term = GL2PS_TEX;
379 warning (
"gl2ps_renderer::draw: Unknown terminal %s, using 'ps'",
382 GLint gl2ps_text = 0;
383 if (m_term.find (
"notxt") != std::string::npos)
384 gl2ps_text = GL2PS_NO_TEXT;
388 std::string plot_title = get_title (myhandle);
389 if (plot_title.empty ())
390 plot_title =
"Octave plot";
393 GLint gl2ps_sort = GL2PS_BSP_SORT;
397 if (has_2D_axes (myhandle))
398 gl2ps_sort = GL2PS_NO_SORT;
401 std::string tmpfile (sys::tempnam (sys::env::get_temp_directory (),
403 FILE *tmpf = sys::fopen_tmp (tmpfile,
"w+b");
406 error (
"gl2ps_renderer::draw: couldn't open temporary file for printing");
408 frame.
add ([tmpf] () { std::fclose (tmpf); });
411 if (m_term.find (
"tex") == std::string::npos)
412 buffsize = 2*1024*1024;
416 m_buffer_overflow =
true;
418 while (m_buffer_overflow)
420 m_buffer_overflow =
false;
432 std::string include_graph;
434 std::size_t found_redirect = old_print_cmd.find (
'>');
436 if (found_redirect != std::string::npos)
437 include_graph = old_print_cmd.substr (found_redirect + 1);
439 include_graph = old_print_cmd;
441 std::size_t n_begin = include_graph.find_first_not_of (R
"( "')");
443 if (n_begin != std::string::npos)
446 std::size_t n_end = include_graph.find_last_not_of (R
"( "')");
447 include_graph = include_graph.substr (n_begin,
448 n_end - n_begin + 1);
450 n_begin = include_graph.find_last_of (sys::file_ops::dir_sep_chars ());
451 include_graph = include_graph.substr (n_begin + 1);
454 include_graph =
"foobar-inc";
458 const figure::properties& fprop
459 =
dynamic_cast<const figure::properties&
> (go.get_properties ());
460 Matrix c = fprop.get_color_rgb ();
461 m_glfcns.glClearColor (c(0), c(1), c(2), 1);
464 set_device_pixel_ratio (fprop.get___device_pixel_ratio__ ());
467 GLint ret = gl2psBeginPage (plot_title.c_str (),
"Octave",
468 nullptr, gl2ps_term, gl2ps_sort,
471 | GL2PS_DRAW_BACKGROUND
472 | GL2PS_NO_PS3_SHADING
473 | GL2PS_USE_CURRENT_VIEWPORT),
474 GL_RGBA, 0,
nullptr, 0, 0, 0,
475 buffsize, tmpf, include_graph.c_str ());
476 if (ret == GL2PS_ERROR)
478 old_print_cmd.clear ();
479 error (
"gl2ps_renderer::draw: gl2psBeginPage returned GL2PS_ERROR");
484 if (m_buffer_overflow)
485 warning (
"gl2ps_renderer::draw: retrying with buffer size: %.1E B\n",
double (2*buffsize));
487 if (! m_buffer_overflow)
488 old_print_cmd = print_cmd;
499 std::size_t nread, nwrite;
504 const char *fcn =
"/SRX { gsave FCT moveto rotate xshow grestore } BD\n";
505 bool header_found = ! (m_term.find (
"eps") != std::string::npos
506 || m_term.find (
"svg") != std::string::npos);
508 while (! feof (tmpf) && nread)
510 if (! header_found && std::fgets (str, 8192, tmpf))
513 nread = std::fread (str, 1, 8192, tmpf);
517 if (! header_found && std::strncmp (str,
"/SBCR", 5) == 0)
520 nwrite = std::fwrite (fcn, 1,
strlen (fcn), m_fp);
521 if (nwrite !=
strlen (fcn))
525 error (
"gl2ps_renderer::draw: internal pipe error");
528 else if (m_term.find (
"svg") != std::string::npos)
538 std::string srchstr (str, nread);
539 std::size_t pos = srchstr.find (
"<svg ");
540 if (! header_found && pos != std::string::npos)
543 pos = srchstr.find (
"px");
544 if (pos != std::string::npos)
546 srchstr[pos+1] =
't';
548 pos = srchstr.find (
"px", pos);
549 srchstr[pos+1] =
't';
550 std::strcpy (str, srchstr.c_str ());
555 nwrite = std::fwrite (str, 1, nread, m_fp);
560 error (
"gl2ps_renderer::draw: internal pipe error");
570gl2ps_renderer::alignment_to_mode (
int ha,
int va)
const
572 int gl2psa = GL2PS_TEXT_BL;
576 if (va == 0 || va == 3)
577 gl2psa=GL2PS_TEXT_BL;
579 gl2psa=GL2PS_TEXT_TL;
581 gl2psa=GL2PS_TEXT_CL;
585 if (va == 0 || va == 3)
586 gl2psa=GL2PS_TEXT_BR;
588 gl2psa=GL2PS_TEXT_TR;
590 gl2psa=GL2PS_TEXT_CR;
594 if (va == 0 || va == 3)
606gl2ps_renderer::fix_strlist_position (
double x,
double y,
double z,
607 Matrix box,
double rotation,
608 std::list<text_renderer::string>& lst)
610 for (
auto& txtobj : lst)
613 ColumnVector coord_pix = get_transform ().transform (
x, y, z,
false);
616 double rot = rotation * 4.0 *
atan (1.0) / 180;
617 coord_pix(0) += (txtobj.get_x () + box(0))* cos (rot)
618 - (txtobj.get_y () + box(1))* sin (rot);
619 coord_pix(1) -= (txtobj.get_y () + box(1))* cos (rot)
620 + (txtobj.get_x () + box(0))* sin (rot);
623 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
625 txtobj.set_x (coord_pix(0));
626 txtobj.set_y (vp[3] - coord_pix(1));
627 txtobj.set_z (coord_pix(2));
632code_to_symbol (uint32_t code)
636 uint32_t idx = code - 945;
639 std::string characters (
"abgdezhqiklmnxoprVstufcyw");
640 retval = characters[idx];
647 std::string characters (
"ABGDEZHQIKLMNXOPRVSTUFCYW");
648 retval = characters[idx];
650 else if (code == 978)
652 else if (code == 215)
654 else if (code == 177)
656 else if (code == 8501)
658 else if (code == 8465)
660 else if (code == 8242)
662 else if (code == 8736)
664 else if (code == 172)
666 else if (code == 9829)
668 else if (code == 8472)
670 else if (code == 8706)
672 else if (code == 8704)
674 else if (code == 9827)
676 else if (code == 9824)
678 else if (code == 8476)
680 else if (code == 8734)
682 else if (code == 8730)
684 else if (code == 8707)
686 else if (code == 9830)
688 else if (code == 8747)
690 else if (code == 8727)
692 else if (code == 8744)
694 else if (code == 8855)
696 else if (code == 8901)
698 else if (code == 8728)
700 else if (code == 8745)
702 else if (code == 8743)
704 else if (code == 8856)
706 else if (code == 8729)
708 else if (code == 8746)
710 else if (code == 8853)
712 else if (code == 8804)
714 else if (code == 8712)
716 else if (code == 8839)
718 else if (code == 8801)
720 else if (code == 8773)
722 else if (code == 8834)
724 else if (code == 8805)
726 else if (code == 8715)
728 else if (code == 8764)
730 else if (code == 8733)
732 else if (code == 8838)
734 else if (code == 8835)
736 else if (code == 8739)
738 else if (code == 8776)
740 else if (code == 8869)
742 else if (code == 8656)
744 else if (code == 8592)
746 else if (code == 8658)
748 else if (code == 8594)
750 else if (code == 8596)
752 else if (code == 8593)
754 else if (code == 8595)
756 else if (code == 8970)
758 else if (code == 8971)
760 else if (code == 10216)
762 else if (code == 10217)
764 else if (code == 8968)
766 else if (code == 8969)
768 else if (code == 8800)
770 else if (code == 8230)
772 else if (code == 176)
774 else if (code == 8709)
776 else if (code == 169)
780 warning (
"print: unhandled symbol %d", code);
786select_font (
caseless_str fn,
bool isbold,
bool isitalic)
788 std::transform (fn.begin (), fn.end (), fn.begin (), ::tolower);
789 std::string fontname;
790 if (fn ==
"times" || fn ==
"times-roman")
792 if (isitalic && isbold)
793 fontname =
"Times-BoldItalic";
795 fontname =
"Times-Italic";
797 fontname =
"Times-Bold";
799 fontname =
"Times-Roman";
801 else if (fn ==
"courier")
803 if (isitalic && isbold)
804 fontname =
"Courier-BoldOblique";
806 fontname =
"Courier-Oblique";
808 fontname =
"Courier-Bold";
810 fontname =
"Courier";
812 else if (fn ==
"symbol")
814 else if (fn ==
"zapfdingbats")
815 fontname =
"ZapfDingbats";
818 if (isitalic && isbold)
819 fontname =
"Helvetica-BoldOblique";
821 fontname =
"Helvetica-Oblique";
823 fontname =
"Helvetica-Bold";
825 fontname =
"Helvetica";
831escape_character (
const std::string chr, std::string& str)
833 std::size_t idx = str.find (chr);
834 while (idx != std::string::npos)
836 str.insert (idx, 1,
'\\');
837 idx = str.find (chr, idx + 2);
842gl2ps_renderer::format_svg_element (std::string str,
Matrix box,
848 std::string::size_type n1 = str.find (
"<defs>");
849 if (n1 == std::string::npos)
850 return std::string ();
852 std::string id, new_id;
853 n1 = str.find (
"<path", ++n1);
854 std::string::size_type n2;
856 while (n1 != std::string::npos)
859 n1 = str.find (
"id='", n1) + 4;
860 n2 = str.find (
"'", n1);
861 id = str.substr (n1, n2-n1);
863 new_id = std::to_string (m_svg_def_index) +
"-" + id ;
865 str.replace (n1, n2-n1, new_id);
867 std::string::size_type n_ref = str.find (
"#" +
id);
869 while (n_ref != std::string::npos)
871 str.replace (n_ref + 1,
id.length (), new_id);
872 n_ref = str.find (
"#" +
id);
875 n1 = str.find (
"<path", n1);
880 n1 = str.find (
"<defs>");
881 n2 = str.find (
"</defs>") + 7;
883 std::string defs = str.substr (n1, n2-n1);
889 n1 = str.find (
"viewBox='") + 9;
890 if (n1 == std::string::npos)
891 return std::string ();
893 n2 = str.find (
" ", n1);
894 double original_x0 = std::stod (str.substr (n1, n2-n1));
897 n2 = str.find (
" ", n1);
898 double original_y0 = std::stod (str.substr (n1, n2-n1));
901 std::string orig_trans;
902 n1 = str.find (
"<g id='page1' transform='");
903 if (n1 != std::string::npos)
906 n2 = str.find (
"'", n1);
907 orig_trans = str.substr (n1, n2-n1);
912 n1 = str.find (
"<g id='page1'");
916 n2 = str.find (
"</g>", n1) + 4;
920 std::string tform = orig_trans;
923 tform = std::string (
"translate")
924 +
"(" + std::to_string (box(0) - original_x0 + coord_pix(0))
925 +
"," + std::to_string (-(box(3) + box(1)) - original_y0 + coord_pix(1))
930 tform = std::string (
"rotate")
931 +
"(" + std::to_string (-rotation)
932 +
"," + std::to_string (coord_pix(0))
933 +
"," + std::to_string (coord_pix(1))
937 std::string fill =
"fill='rgb("
938 + std::to_string (
static_cast<uint8_t
> (color(0) * 255.0)) +
","
939 + std::to_string (
static_cast<uint8_t
> (color(1) * 255.0)) +
","
940 + std::to_string (
static_cast<uint8_t
> (color(2) * 255.0)) +
")' ";
942 std::string use_group =
"<g "
944 +
"transform='" + tform +
"'"
945 + str.substr (n1, n2-n1);
947 return defs +
"\n" + use_group;
951gl2ps_renderer::strlist_to_svg (
double x,
double y,
double z,
952 Matrix box,
double rotation,
953 std::list<text_renderer::string>& lst)
956 ColumnVector coord_pix = get_transform ().transform (
x, y, z,
false);
962 std::string svg = lst.front ().get_svg_element ();
964 return format_svg_element (svg, box, rotation, coord_pix,
965 lst.front ().get_color ());
968 std::ostringstream os;
969 os << R
"(<g xml:space="preserve" )";
971 <<
"translate(" << coord_pix(0) + box(0) <<
"," << coord_pix(1) - box(1)
972 <<
") rotate(" << -rotation <<
"," << -box(0) <<
"," << box(1)
976 auto p = lst.begin ();
977 std::string name = p->get_family ();
978 std::string weight = p->get_weight ();
979 std::string angle = p->get_angle ();
980 double size = p->get_size ();
982 os <<
"font-family=\"" << name <<
"\" "
983 <<
"font-weight=\"" << weight <<
"\" "
984 <<
"font-style=\"" << angle <<
"\" "
985 <<
"font-size=\"" << size <<
"\">";
989 for (p = lst.begin (); p != lst.end (); p++)
993 if (name.compare (p->get_family ()))
994 os <<
"font-family=\"" << p->get_family () <<
"\" ";
996 if (weight.compare (p->get_weight ()))
997 os <<
"font-weight=\"" << p->get_weight () <<
"\" ";
999 if (angle.compare (p->get_angle ()))
1000 os <<
"font-style=\"" << p->get_angle () <<
"\" ";
1002 if (size != p->get_size ())
1003 os <<
"font-size=\"" << p->get_size () <<
"\" ";
1005 os <<
"y=\"" << - p->get_y () <<
"\" ";
1007 Matrix col = p->get_color ();
1008 os <<
"fill=\"rgb(" << col(0)*255 <<
","
1009 << col(1)*255 <<
"," << col(2)*255 <<
")\" ";
1013 std::vector<double> xdata = p->get_xdata ();
1014 for (
const auto& q : xdata)
1022 os <<
"&#" << p->get_code () <<
";";
1025 const std::string str = p->get_string ();
1026 for (
const auto& q : str)
1028 std::stringstream chr;
1030 if (chr.str () ==
"\"")
1032 else if (chr.str () ==
"'")
1034 else if (chr.str () ==
"&")
1036 else if (chr.str () ==
"<")
1038 else if (chr.str () ==
">")
1052gl2ps_renderer::strlist_to_ps (
double x,
double y,
double z,
1053 Matrix box,
double rotation,
1054 std::list<text_renderer::string>& lst)
1058 else if (lst.size () == 1)
1060 static bool warned =
false;
1062 if (! lst.front ().get_svg_element ().empty ())
1068 "print: unhandled LaTeX strings. "
1069 "Use -svgconvert option or -d*latex* output "
1077 fix_strlist_position (
x, y, z, box, rotation, lst);
1078 Matrix prev_color (1, 3, -1);
1080 std::ostringstream ss;
1083 static bool warned =
false;
1085 for (
const auto& txtobj : lst)
1088 if (txtobj.get_color () != prev_color)
1090 prev_color = txtobj.get_color ();
1091 for (
int i = 0; i < 3; i++)
1092 ss << prev_color(i) <<
" ";
1099 if (txtobj.get_code ())
1101 m_fontname =
"Symbol";
1102 str = code_to_symbol (txtobj.get_code ());
1106 m_fontname = select_font (txtobj.get_name (),
1107 txtobj.get_weight () ==
"bold",
1108 txtobj.get_angle () ==
"italic");
1111 const std::string tmpstr = txtobj.get_string ();
1113 =
reinterpret_cast<const uint8_t *
> (tmpstr.c_str ());
1115 for (std::size_t i = 0; i < tmpstr.size ();)
1126 "print: only ASCII characters are "
1127 "supported for EPS and derived "
1128 "formats. Use the '-svgconvert' "
1129 "option for better font support.");
1140 "print: only ASCII characters are "
1141 "supported for EPS and derived "
1142 "formats. Use the '-svgconvert' "
1143 "option for better font support.");
1148 str += tmpstr.at (i);
1154 escape_character (
"\\", str);
1155 escape_character (
"(", str);
1156 escape_character (
")", str);
1158 ss <<
"(" << str <<
") [";
1160 std::vector<double> xdata = txtobj.get_xdata ();
1161 for (std::size_t i = 1; i < xdata.size (); i++)
1162 ss << xdata[i] - xdata[i-1] <<
" ";
1164 ss <<
"10] " << rotation <<
" " << txtobj.get_x ()
1165 <<
" " << txtobj.get_y () <<
" " << txtobj.get_size ()
1166 <<
" /" << m_fontname <<
" SRX\n";
1175gl2ps_renderer::render_text (
const std::string& txt,
1176 double x,
double y,
double z,
1177 int ha,
int va,
double rotation)
1179 std::string saved_font = m_fontname;
1182 return Matrix (1, 4, 0.0);
1185 std::string str = txt;
1186 std::list<text_renderer::string> lst;
1188 text_to_strlist (str, lst, bbox, ha, va, rotation);
1189 m_glfcns.glRasterPos3d (
x, y, z);
1192 if (m_term.find (
"svg") != std::string::npos)
1194 std::string elt = strlist_to_svg (
x, y, z, bbox, rotation, lst);
1196 gl2psSpecial (GL2PS_SVG, elt.c_str ());
1198 else if (m_term.find (
"eps") != std::string::npos)
1200 std::string elt = strlist_to_ps (
x, y, z, bbox, rotation, lst);
1202 gl2psSpecial (GL2PS_EPS, elt.c_str ());
1206 gl2psTextOpt (str.c_str (), m_fontname.c_str (), m_fontsize,
1207 alignment_to_mode (ha, va), rotation);
1209 m_fontname = saved_font;
1215gl2ps_renderer::set_font (
const base_properties& props)
1220 if (props.has_property (
"interpreter"))
1221 set_interpreter (props.get (
"interpreter").string_value ());
1223 m_fontsize = props.get (
"__fontsize_points__").double_value ();
1225 caseless_str fn = props.get (
"fontname").xtolower ().string_value ();
1227 =(props.get (
"fontweight").xtolower ().string_value () ==
"bold");
1229 = (props.get (
"fontangle").xtolower ().string_value () ==
"italic");
1231 m_fontname = select_font (fn, isbold, isitalic);
1235gl2ps_renderer::draw_image (
const image::properties& props)
1242 Matrix x = props.get_xdata ().matrix_value ();
1243 Matrix y = props.get_ydata ().matrix_value ();
1253 std::swap (
x(0),
x(1));
1256 else if (w > 1 &&
x(1) ==
x(0))
1257 x(1) =
x(1) + (
w-1);
1262 std::swap (y(0), y(1));
1265 else if (h > 1 && y(1) == y(0))
1266 y(1) = y(1) + (h-1);
1272 if (math::isnan (p0(0)) || math::isnan (p0(1))
1273 || math::isnan (p1(0)) || math::isnan (p1(1)))
1275 warning (
"opengl_renderer: image X,Y data too large to draw");
1280 float pix_dx, pix_dy;
1282 float nor_dx, nor_dy;
1286 pix_dx = (p1(0) - p0(0)) / (w-1);
1287 nor_dx = (
x(1) -
x(0)) / (w-1);
1291 const ColumnVector p1w = m_xform.transform (
x(1) + 1, y(1), 0);
1292 pix_dx = p1w(0) - p0(0);
1298 pix_dy = (p1(1) - p0(1)) / (h-1);
1299 nor_dy = (y(1) - y(0)) / (h-1);
1303 const ColumnVector p1h = m_xform.transform (
x(1), y(1) + 1, 0);
1304 pix_dy = p1h(1) - p0(1);
1311 int j0, j1, jj, i0, i1, ii;
1315 float im_xmin =
x(0) - nor_dx/2;
1316 float im_xmax =
x(1) + nor_dx/2;
1317 float im_ymin = y(0) - nor_dy/2;
1318 float im_ymax = y(1) + nor_dy/2;
1321 bool do_clip = props.is_clipping ();
1322 Matrix vp = get_viewport_scaled ();
1325 = m_xform.untransform (std::numeric_limits <float>::epsilon (),
1326 std::numeric_limits <float>::epsilon ());
1327 ColumnVector vp_lim_max = m_xform.untransform (vp(2), vp(3));
1329 if (vp_lim_min(0) > vp_lim_max(0))
1330 std::swap (vp_lim_min(0), vp_lim_max(0));
1332 if (vp_lim_min(1) > vp_lim_max(1))
1333 std::swap (vp_lim_min(1), vp_lim_max(1));
1336 = do_clip ? (vp_lim_min(0) > m_xmin ? vp_lim_min(0) : m_xmin)
1340 = do_clip ? (vp_lim_min(1) > m_ymin ? vp_lim_min(1) : m_ymin)
1344 = do_clip ? (vp_lim_max(0) < m_xmax ? vp_lim_max(0) : m_xmax)
1348 = do_clip ? (vp_lim_max(1) < m_ymax ? vp_lim_max(1) : m_ymax)
1351 if (im_xmin < clip_xmin)
1352 j0 += (clip_xmin - im_xmin)/nor_dx + 1;
1354 if (im_xmax > clip_xmax)
1355 j1 -= (im_xmax - clip_xmax)/nor_dx;
1357 if (im_ymin < clip_ymin)
1358 i0 += (clip_ymin - im_ymin)/nor_dy + 1;
1360 if (im_ymax > clip_ymax)
1361 i1 -= (im_ymax - clip_ymax)/nor_dy;
1363 if (i0 >= i1 || j0 >= j1)
1367 m_glfcns.glGetFloatv (GL_ZOOM_X, &zoom_x);
1369 m_glfcns.glGetFloatv (GL_ZOOM_Y, &zoom_y);
1371 m_glfcns.glPixelZoom (m_devpixratio * pix_dx, - m_devpixratio * pix_dy);
1372 m_glfcns.glRasterPos3d (im_xmin + nor_dx*j0, im_ymin + nor_dy*i0, 0);
1375 if (dv.
ndims () == 3 && dv(2) == 3)
1382 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1384 for (
int i = i0; i < i1; i++)
1386 for (
int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1398 a[idx] = xcdata(ii, jj, 0);
1399 a[idx+1] = xcdata(ii, jj, 1);
1400 a[idx+2] = xcdata(ii, jj, 2);
1404 draw_pixels (j1-j0, i1-i0, a);
1412 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1414 for (
int i = i0; i < i1; i++)
1416 for (
int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1428 a[idx] = xcdata(ii, jj, 0);
1429 a[idx+1] = xcdata(ii, jj, 1);
1430 a[idx+2] = xcdata(ii, jj, 2);
1434 draw_pixels (j1-j0, i1-i0, a);
1442 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1444 for (
int i = i0; i < i1; i++)
1446 for (
int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1458 a[idx] = xcdata(ii, jj, 0);
1459 a[idx+1] = xcdata(ii, jj, 1);
1460 a[idx+2] = xcdata(ii, jj, 2);
1464 draw_pixels (j1-j0, i1-i0, a);
1472 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1474 for (
int i = i0; i < i1; i++)
1476 for (
int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1488 a[idx] = xcdata(ii, jj, 0);
1489 a[idx+1] = xcdata(ii, jj, 1);
1490 a[idx+2] = xcdata(ii, jj, 2);
1494 draw_pixels (j1-j0, i1-i0, a);
1498 warning (
"opengl_renderer: invalid image data type (expected double, single, uint8, or uint16)");
1500 m_glfcns.glPixelZoom (zoom_x, zoom_y);
1506gl2ps_renderer::draw_pixels (
int w,
int h,
const float *data)
1511 for (
int i = 0; i < 3*h*
w; i++)
1512 tmp_data[i] = (data[i] < 0.0f ? 0.0f : (data[i] > 1.0f ? 1.0f : data[i]));
1514 gl2psDrawPixels (w, h, 0, 0, GL_RGB, GL_FLOAT, tmp_data);
1518gl2ps_renderer::draw_pixels (
int w,
int h,
const uint8_t *data)
1524 static constexpr float MAXVAL = std::numeric_limits<uint8_t>::max ();
1526 for (
int i = 0; i < 3*
w*h; i++)
1527 tmp_data[i] = data[i] / MAXVAL;
1529 draw_pixels (w, h, tmp_data);
1533gl2ps_renderer::draw_pixels (
int w,
int h,
const uint16_t *data)
1539 static constexpr float MAXVAL = std::numeric_limits<uint16_t>::max ();
1541 for (
int i = 0; i < 3*
w*h; i++)
1542 tmp_data[i] = data[i] / MAXVAL;
1544 draw_pixels (w, h, tmp_data);
1548gl2ps_renderer::draw_text (
const text::properties& props)
1550 if (props.get_string ().isempty ())
1553 draw_text_background (props,
true);
1559 set_color (props.get_color_rgb ());
1561 std::string saved_font = m_fontname;
1567 if (props.horizontalalignment_is (
"center"))
1569 else if (props.horizontalalignment_is (
"right"))
1572 if (props.verticalalignment_is (
"top"))
1574 else if (props.verticalalignment_is (
"baseline"))
1576 else if (props.verticalalignment_is (
"middle"))
1582 const Matrix pos = get_transform ().scale (props.get_data_position ());
1583 std::string str = props.get_string ().string_vector_value ().join (
"\n");
1585 render_text (str, pos(0), pos(1), pos.
numel () > 2 ? pos(2) : 0.0,
1586 halign, valign, props.get_rotation ());
1589OCTAVE_END_NAMESPACE(octave)
1600 const std::string&
stream,
const std::string& term)
1602#if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
1607 bool have_cmd =
stream.length () > 1 &&
stream[0] ==
'|';
1609 FILE *m_fp =
nullptr;
1617 std::string cmd =
stream.substr (1);
1619 m_fp =
popen (cmd.c_str (),
"w");
1622 error (R
"(print: failed to open pipe "%s")", stream.c_str ());
1625 frame.
add ([m_fp] () { octave::pclose (m_fp); });
1631 m_fp = sys::fopen (
stream.c_str (),
"w");
1634 error (R
"(gl2ps_print: failed to create file "%s")", stream.c_str ());
1636 frame.add ([m_fp] () { std::fclose (m_fp); });
1639 gl2ps_renderer rend (glfcns, m_fp, term);
1641 Matrix pos = fig.get ("position").matrix_value ();
1642 rend.set_viewport (pos(2), pos(3));
1650 octave_unused_parameter (glfcns);
1651 octave_unused_parameter (fig);
1652 octave_unused_parameter (
stream);
1653 octave_unused_parameter (term);
1660OCTAVE_END_NAMESPACE(octave)
bool isempty() const
Size of the specified dimension.
octave_idx_type numel() const
Number of elements in the array.
void add(F &&fcn, Args &&... args)
Vector representing the dimensions (size) of an Array.
octave_idx_type ndims() const
Number of dimensions.
graphics_object get_object(double val) const
uint16NDArray uint16_array_value() const
bool is_scalar_type() const
bool is_single_type() const
bool is_uint8_type() const
bool is_uint16_type() const
NDArray array_value(bool frc_str_conv=false) const
bool is_double_type() const
uint8NDArray uint8_array_value() const
FloatNDArray float_array_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 set_linewidth(float w)
virtual Matrix render_text(const std::string &txt, double x, double y, double z, int halign, int valign, double rotation=0.0)
virtual void draw_image(const image::properties &props)
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 draw_text(const text::properties &props)
virtual void set_font(const base_properties &props)
virtual void set_linecap(const std::string &)
virtual void set_linejoin(const std::string &)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning(const char *fmt,...)
void warning_with_id(const char *id, const char *fmt,...)
void error(const char *fmt,...)
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
int octave_fseeko_wrapper(FILE *fp, off_t offset, int whence)
void gl2ps_print(opengl_functions &glfcns, const graphics_object &fig, const std::string &stream, const std::string &term)
gh_manager & __get_gh_manager__()
Complex atan(const Complex &x)
F77_RET_T const F77_DBLE * x
std::complex< double > w(std::complex< double > z, double relerr=0)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
T::size_type strlen(const typename T::value_type *str)
void respond_to_pending_signals()
FILE * popen(const char *command, const char *mode)
int octave_ftruncate_wrapper(int fd, off_t sz)
int octave_u8_strmblen_wrapper(const uint8_t *src)