GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
gl2ps-print.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2009-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30// Both header files are required outside of HAVE_GLP2S_H
31#include "errwarn.h"
32#include "gl2ps-print.h"
33
34#if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
35
36#include <cstdio>
37
38#include <limits>
39
40#include <gl2ps.h>
41
42#include "file-ops.h"
43#include "lo-mappers.h"
44#include "oct-locbuf.h"
45#include "tmpfile-wrapper.h"
46#include "unistd-wrappers.h"
47#include "unistr-wrappers.h"
48#include "unwind-prot.h"
49
50#include "gl-render.h"
51#include "interpreter-private.h"
52#include "oct-opengl.h"
53#include "sighandlers.h"
54#include "sysdep.h"
55#include "text-renderer.h"
56
57namespace octave
58{
59 class
60 OCTINTERP_API
61 gl2ps_renderer : public opengl_renderer
62 {
63 public:
64
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)
69 { }
70
71 ~gl2ps_renderer (void) = default;
72
73 // FIXME: should we import the functions from the base class and
74 // overload them here, or should we use a different name so we don't
75 // have to do this? Without the using declaration or a name change,
76 // the base class functions will be hidden. That may be OK, but it
77 // can also cause some confusion.
79
80 void draw (const graphics_object& go, const std::string& print_cmd);
81
82 protected:
83
84 Matrix render_text (const std::string& txt,
85 double x, double y, double z,
86 int halign, int valign, double rotation = 0.0);
87
88 void set_font (const base_properties& props);
89
90 static bool has_alpha (const graphics_handle& h)
91 {
92 bool retval = false;
93
94 gh_manager& gh_mgr = __get_gh_manager__ ("gl2ps_renderer::has_alpha");
95
96 graphics_object go = gh_mgr.get_object (h);
97
98 if (! go.valid_object ())
99 return retval;
100
101 if (go.isa ("axes") || go.isa ("hggroup"))
102 {
103 Matrix children = go.get ("children").matrix_value ();
104 for (octave_idx_type ii = 0; ii < children.numel (); ii++)
105 {
106 retval = has_alpha (graphics_handle (children(ii)));
107 if (retval)
108 break;
109 }
110 }
111 else if (go.isa ("patch") || go.isa ("surface"))
112 {
113 octave_value fa = go.get ("facealpha");
114 if (fa.is_scalar_type () && fa.is_double_type ()
115 && fa.double_value () < 1)
116 retval = true;
117 }
118 else if (go.isa ("scatter"))
119 {
120 octave_value fa = go.get ("markerfacealpha");
121 if (fa.is_scalar_type () && fa.is_double_type ()
122 && fa.double_value () < 1)
123 retval = true;
124 }
125
126 return retval;
127 }
128
129 void draw_axes (const axes::properties& props)
130 {
131 // Initialize a sorting tree (viewport) in gl2ps for each axes
132 GLint vp[4];
133 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
134 gl2psBeginViewport (vp);
135
136
137 // Don't remove hidden primitives when some of them are transparent
138 GLint opts;
139 gl2psGetOptions (&opts);
140 if (has_alpha (props.get___myhandle__ ()))
141 {
142 opts &= ~GL2PS_OCCLUSION_CULL;
143 // FIXME: currently the GL2PS_BLEND (which is more an equivalent of
144 // GL_ALPHA_TEST than GL_BLEND) is not working on a per primitive
145 // basis. We thus set it once per viewport.
146 gl2psEnable (GL2PS_BLEND);
147 }
148 else
149 {
150 opts |= GL2PS_OCCLUSION_CULL;
151 gl2psDisable (GL2PS_BLEND);
152 }
153
154 gl2psSetOptions (opts);
155
156 // Draw and finish () or there may be primitives missing in the gl2ps
157 // output.
159 finish ();
160
161 // Finalize viewport
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");
167
168 m_buffer_overflow |= (state == GL2PS_OVERFLOW);
169
170 // Don't draw background for subsequent viewports (legends, subplots,
171 // etc.)
172 gl2psGetOptions (&opts);
173 opts &= ~GL2PS_DRAW_BACKGROUND;
174 gl2psSetOptions (opts);
175 }
176
177 void draw_text (const text::properties& props);
178
179 void draw_image (const image::properties& props);
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);
183
184 void init_marker (const std::string& m, double size, float width)
185 {
186 opengl_renderer::init_marker (m, size, width);
187
188 // FIXME: gl2ps can't handle closed contours so we set linecap/linejoin
189 // round to obtain a better looking result for some markers.
190 if (m == "o" || m == "v" || m == "^" || m == ">" || m == "<" || m == "h"
191 || m == "hexagram" || m == "p" || m == "pentagram")
192 {
193 set_linejoin ("round");
194 set_linecap ("round");
195 }
196 else
197 {
198 set_linejoin ("miter");
199 set_linecap ("square");
200 }
201 }
202
203 void set_linestyle (const std::string& s, bool use_stipple = false,
204 double linewidth = 0.5)
205 {
206 opengl_renderer::set_linestyle (s, use_stipple, linewidth);
207
208 if (s == "-" && ! use_stipple)
209 gl2psDisable (GL2PS_LINE_STIPPLE);
210 else
211 gl2psEnable (GL2PS_LINE_STIPPLE);
212 }
213
214 void set_linecap (const std::string& s)
215 {
217
218#if defined (HAVE_GL2PSLINEJOIN)
219 if (s == "butt")
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);
225#endif
226 }
227
228 void set_linejoin (const std::string& s)
229 {
231
232#if defined (HAVE_GL2PSLINEJOIN)
233 if (s == "round")
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);
239#endif
240 }
241
242 void set_polygon_offset (bool on, float offset = 0.0f)
243 {
244 if (on)
245 {
247 gl2psEnable (GL2PS_POLYGON_OFFSET_FILL);
248 }
249 else
250 {
251 gl2psDisable (GL2PS_POLYGON_OFFSET_FILL);
253 }
254 }
255
256 void set_linewidth (float w)
257 {
258 gl2psLineWidth (w);
259 }
260
261 private:
262
263 // Use xform to compute the coordinates of the string list
264 // that have been parsed by freetype.
265 void fix_strlist_position (double x, double y, double z,
266 Matrix box, double rotation,
267 std::list<text_renderer::string>& lst);
268
269 // Build an svg text element from a list of parsed strings
270 std::string format_svg_element (std::string str, Matrix bbox,
271 double rotation, ColumnVector coord_pix,
272 Matrix color);
273
274 std::string strlist_to_svg (double x, double y, double z, Matrix box,
275 double rotation,
276 std::list<text_renderer::string>& lst);
277
278 // Build a list of postscript commands from a list of parsed strings.
279 std::string strlist_to_ps (double x, double y, double z, Matrix box,
280 double rotation,
281 std::list<text_renderer::string>& lst);
282
283 int alignment_to_mode (int ha, int va) const;
284
285 FILE *m_fp;
286 caseless_str m_term;
287 double m_fontsize;
288 std::string m_fontname;
289 bool m_buffer_overflow;
290 std::size_t m_svg_def_index;
291 };
292
293 static bool
294 has_2D_axes (const graphics_handle& h)
295 {
296 bool retval = true;
297
298 gh_manager& gh_mgr = __get_gh_manager__ ("gl2ps_renderer::has_2D_axes");
299
300 graphics_object go = gh_mgr.get_object (h);
301
302 if (! go.valid_object ())
303 return retval;
304
305 if (go.isa ("figure") || go.isa ("uipanel"))
306 {
307 Matrix children = go.get ("children").matrix_value ();
308 for (octave_idx_type ii = 0; ii < children.numel (); ii++)
309 {
310 retval = has_2D_axes (graphics_handle (children(ii)));
311 if (! retval)
312 break;
313 }
314 }
315 else if (go.isa ("axes"))
316 {
318 = reinterpret_cast<axes::properties&> (go.get_properties ());
319 retval = ap.get_is2D (true);
320 }
321
322 return retval;
323 }
324
325 static std::string
326 get_title (const graphics_handle& h)
327 {
328 std::string retval;
329
330 gh_manager& gh_mgr = __get_gh_manager__ ("gl2ps_renderer::get_title");
331
332 graphics_object go = gh_mgr.get_object (h);
333
334 if (! go.valid_object ())
335 return retval;
336
337 if (go.isa ("figure"))
338 {
340 = reinterpret_cast<figure::properties&> (go.get_properties ());
341
342 retval = fp.get_title ();
343 }
344
345 return retval;
346 }
347
348 void
349 gl2ps_renderer::draw (const graphics_object& go, const std::string& print_cmd)
350 {
351 static bool in_draw = false;
352 static std::string old_print_cmd;
353 static GLint buffsize;
354
355 if (! in_draw)
356 {
357 unwind_protect frame;
358
359 frame.protect_var (in_draw);
360
361 in_draw = true;
362
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;
376 else
377 warning ("gl2ps_renderer::draw: Unknown terminal %s, using 'ps'",
378 m_term.c_str ());
379
380 GLint gl2ps_text = 0;
381 if (m_term.find ("notxt") != std::string::npos)
382 gl2ps_text = GL2PS_NO_TEXT;
383
384 // Find Title for plot
385 const graphics_handle& myhandle = go.get ("__myhandle__");
386 std::string plot_title = get_title (myhandle);
387 if (plot_title.empty ())
388 plot_title = "Octave plot";
389
390 // Default sort order optimizes for 3D plots
391 GLint gl2ps_sort = GL2PS_BSP_SORT;
392
393 // FIXME: gl2ps does not provide a way to change the sorting algorithm
394 // on a viewport basis, we thus disable sorting only if all axes are 2D
395 if (has_2D_axes (myhandle))
396 gl2ps_sort = GL2PS_NO_SORT;
397
398 // Use a temporary file in case an overflow happens
399 FILE *tmpf = octave_tmpfile_wrapper ();
400
401 if (! tmpf)
402 error ("gl2ps_renderer::draw: couldn't open temporary file for printing");
403
404 frame.add ([=] () { std::fclose (tmpf); });
405
406 // Reset buffsize, unless this is 2nd pass of a texstandalone print.
407 if (m_term.find ("tex") == std::string::npos)
408 buffsize = 2*1024*1024;
409 else
410 buffsize /= 2;
411
412 m_buffer_overflow = true;
413
414 while (m_buffer_overflow)
415 {
416 m_buffer_overflow = false;
417 buffsize *= 2;
418
419 std::fseek (tmpf, 0, SEEK_SET);
420 octave_ftruncate_wrapper (fileno (tmpf), 0);
421
422 // For LaTeX output the print process uses 2 drawnow() commands.
423 // The first one is for the pdf/ps/eps graph to be included. The
424 // print_cmd is saved as old_print_cmd. Then the second drawnow()
425 // outputs the tex-file and the graphic filename to be included is
426 // extracted from old_print_cmd.
427
428 std::string include_graph;
429
430 std::size_t found_redirect = old_print_cmd.find ('>');
431
432 if (found_redirect != std::string::npos)
433 include_graph = old_print_cmd.substr (found_redirect + 1);
434 else
435 include_graph = old_print_cmd;
436
437 std::size_t n_begin = include_graph.find_first_not_of (R"( "')");
438
439 if (n_begin != std::string::npos)
440 {
441 // Strip any quote characters characters around filename
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);
445 // Strip path from filename
446 n_begin = include_graph.find_last_of (sys::file_ops::dir_sep_chars ());
447 include_graph = include_graph.substr (n_begin + 1);
448 }
449 else
450 include_graph = "foobar-inc";
451
452 // FIXME: workaround gl2ps drawing 2 background planes, the first
453 // eventually being black and producing visual artifacts
454 const figure::properties& fprop
455 = dynamic_cast<const figure::properties&> (go.get_properties ());
456 Matrix c = fprop.get_color_rgb ();
457 m_glfcns.glClearColor (c(0), c(1), c(2), 1);
458
459 // Allow figures to be printed at arbitrary resolution
460 set_device_pixel_ratio (fprop.get___device_pixel_ratio__ ());
461
462 // GL2PS_SILENT was removed to allow gl2ps to print errors on stderr
463 GLint ret = gl2psBeginPage (plot_title.c_str (), "Octave",
464 nullptr, gl2ps_term, gl2ps_sort,
465 (GL2PS_BEST_ROOT
466 | gl2ps_text
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)
473 {
474 old_print_cmd.clear ();
475 error ("gl2ps_renderer::draw: gl2psBeginPage returned GL2PS_ERROR");
476 }
477
479
480 if (m_buffer_overflow)
481 warning ("gl2ps_renderer::draw: retrying with buffer size: %.1E B\n", double (2*buffsize));
482
483 if (! m_buffer_overflow)
484 old_print_cmd = print_cmd;
485
486 // Don't check return value of gl2psEndPage, it is not meaningful.
487 // Errors and warnings are checked after gl2psEndViewport in
488 // gl2ps_renderer::draw_axes instead.
489 gl2psEndPage ();
490 }
491
492 // Copy temporary file to pipe
493 std::fseek (tmpf, 0, SEEK_SET);
494 char str[8192]; // 8 kB is a common kernel buffersize
495 std::size_t nread, nwrite;
496 nread = 1;
497
498 // In EPS terminal read the header line by line and insert a
499 // new procedure
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);
503
504 while (! feof (tmpf) && nread)
505 {
506 if (! header_found && std::fgets (str, 8192, tmpf))
507 nread = strlen (str);
508 else
509 nread = std::fread (str, 1, 8192, tmpf);
510
511 if (nread)
512 {
513 if (! header_found && std::strncmp (str, "/SBCR", 5) == 0)
514 {
515 header_found = true;
516 nwrite = std::fwrite (fcn, 1, strlen (fcn), m_fp);
517 if (nwrite != strlen (fcn))
518 {
519 // FIXME: is this the best thing to do here?
521 error ("gl2ps_renderer::draw: internal pipe error");
522 }
523 }
524 else if (m_term.find ("svg") != std::string::npos)
525 {
526 // FIXME: gl2ps uses pixel units for SVG format.
527 // Modify resulting svg to use points instead.
528 // Remove this "else if" block, and
529 // make header_found true for SVG if gl2ps is fixed.
530
531 // Specify number of characters because STR may have
532 // come from std::fread and not end with a NUL
533 // character.
534 std::string srchstr (str, nread);
535 std::size_t pos = srchstr.find ("<svg ");
536 if (! header_found && pos != std::string::npos)
537 {
538 header_found = true;
539 pos = srchstr.find ("px");
540 if (pos != std::string::npos)
541 {
542 srchstr[pos+1] = 't'; // "px" -> "pt"
543 // Assume the second occurrence is at the same line
544 pos = srchstr.find ("px", pos);
545 srchstr[pos+1] = 't'; // "px" -> "pt"
546 std::strcpy (str, srchstr.c_str ());
547 }
548 }
549 }
550
551 nwrite = std::fwrite (str, 1, nread, m_fp);
552 if (nwrite != nread)
553 {
554 // FIXME: is this the best thing to do here?
555 respond_to_pending_signals (); // Clear SIGPIPE signal
556 error ("gl2ps_renderer::draw: internal pipe error");
557 }
558 }
559 }
560 }
561 else
563 }
564
565 int
566 gl2ps_renderer::alignment_to_mode (int ha, int va) const
567 {
568 int gl2psa = GL2PS_TEXT_BL;
569
570 if (ha == 0)
571 {
572 if (va == 0 || va == 3)
573 gl2psa=GL2PS_TEXT_BL;
574 else if (va == 2)
575 gl2psa=GL2PS_TEXT_TL;
576 else if (va == 1)
577 gl2psa=GL2PS_TEXT_CL;
578 }
579 else if (ha == 2)
580 {
581 if (va == 0 || va == 3)
582 gl2psa=GL2PS_TEXT_BR;
583 else if (va == 2)
584 gl2psa=GL2PS_TEXT_TR;
585 else if (va == 1)
586 gl2psa=GL2PS_TEXT_CR;
587 }
588 else if (ha == 1)
589 {
590 if (va == 0 || va == 3)
591 gl2psa=GL2PS_TEXT_B;
592 else if (va == 2)
593 gl2psa=GL2PS_TEXT_T;
594 else if (va == 1)
595 gl2psa=GL2PS_TEXT_C;
596 }
597
598 return gl2psa;
599 }
600
601 void
602 gl2ps_renderer::fix_strlist_position (double x, double y, double z,
603 Matrix box, double rotation,
604 std::list<text_renderer::string>& lst)
605 {
606 for (auto& txtobj : lst)
607 {
608 // Get pixel coordinates
609 ColumnVector coord_pix = get_transform ().transform (x, y, z, false);
610
611 // Translate and rotate
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);
617
618 GLint vp[4];
619 m_glfcns.glGetIntegerv (GL_VIEWPORT, vp);
620
621 txtobj.set_x (coord_pix(0));
622 txtobj.set_y (vp[3] - coord_pix(1));
623 txtobj.set_z (coord_pix(2));
624 }
625 }
626
627 static std::string
628 code_to_symbol (uint32_t code)
629 {
630 std::string retval;
631
632 uint32_t idx = code - 945;
633 if (idx < 25)
634 {
635 std::string characters ("abgdezhqiklmnxoprVstufcyw");
636 retval = characters[idx];
637 return retval;
638 }
639
640 idx = code - 913;
641 if (idx < 25)
642 {
643 std::string characters ("ABGDEZHQIKLMNXOPRVSTUFCYW");
644 retval = characters[idx];
645 }
646 else if (code == 978)
647 retval = "U";
648 else if (code == 215)
649 retval = "\xb4";
650 else if (code == 177)
651 retval = "\xb1";
652 else if (code == 8501)
653 retval = "\xc0";
654 else if (code == 8465)
655 retval = "\xc1";
656 else if (code == 8242)
657 retval = "\xa2";
658 else if (code == 8736)
659 retval = "\xd0";
660 else if (code == 172)
661 retval = "\xd8";
662 else if (code == 9829)
663 retval = "\xa9";
664 else if (code == 8472)
665 retval = "\xc3";
666 else if (code == 8706)
667 retval = "\xb6";
668 else if (code == 8704)
669 retval = "\x22";
670 else if (code == 9827)
671 retval = "\xa7";
672 else if (code == 9824)
673 retval = "\xaa";
674 else if (code == 8476)
675 retval = "\xc2";
676 else if (code == 8734)
677 retval = "\xa5";
678 else if (code == 8730)
679 retval = "\xd6";
680 else if (code == 8707)
681 retval = "\x24";
682 else if (code == 9830)
683 retval = "\xa8";
684 else if (code == 8747)
685 retval = "\xf2";
686 else if (code == 8727)
687 retval = "\x2a";
688 else if (code == 8744)
689 retval = "\xda";
690 else if (code == 8855)
691 retval = "\xc4";
692 else if (code == 8901)
693 retval = "\xd7";
694 else if (code == 8728)
695 retval = "\xb0";
696 else if (code == 8745)
697 retval = "\xc7";
698 else if (code == 8743)
699 retval = "\xd9";
700 else if (code == 8856)
701 retval = "\xc6";
702 else if (code == 8729)
703 retval = "\xb7";
704 else if (code == 8746)
705 retval = "\xc8";
706 else if (code == 8853)
707 retval = "\xc5";
708 else if (code == 8804)
709 retval = "\xa3";
710 else if (code == 8712)
711 retval = "\xce";
712 else if (code == 8839)
713 retval = "\xca";
714 else if (code == 8801)
715 retval = "\xba";
716 else if (code == 8773)
717 retval = "\x40";
718 else if (code == 8834)
719 retval = "\xcc";
720 else if (code == 8805)
721 retval = "\xb3";
722 else if (code == 8715)
723 retval = "\x27";
724 else if (code == 8764)
725 retval = "\x7e";
726 else if (code == 8733)
727 retval = "\xb5";
728 else if (code == 8838)
729 retval = "\xcd";
730 else if (code == 8835)
731 retval = "\xc9";
732 else if (code == 8739)
733 retval = "\xbd";
734 else if (code == 8776)
735 retval = "\xbb";
736 else if (code == 8869)
737 retval = "\x5e";
738 else if (code == 8656)
739 retval = "\xdc";
740 else if (code == 8592)
741 retval = "\xac";
742 else if (code == 8658)
743 retval = "\xde";
744 else if (code == 8594)
745 retval = "\xae";
746 else if (code == 8596)
747 retval = "\xab";
748 else if (code == 8593)
749 retval = "\xad";
750 else if (code == 8595)
751 retval = "\xaf";
752 else if (code == 8970)
753 retval = "\xeb";
754 else if (code == 8971)
755 retval = "\xfb";
756 else if (code == 10216)
757 retval = "\xe1";
758 else if (code == 10217)
759 retval = "\xf1";
760 else if (code == 8968)
761 retval = "\xe9";
762 else if (code == 8969)
763 retval = "\xf9";
764 else if (code == 8800)
765 retval = "\xb9";
766 else if (code == 8230)
767 retval = "\xbc";
768 else if (code == 176)
769 retval = "\xb0";
770 else if (code == 8709)
771 retval = "\xc6";
772 else if (code == 169)
773 retval = "\xd3";
774
775 if (retval.empty ())
776 warning ("print: unhandled symbol %d", code);
777
778 return retval;
779 }
780
781 static std::string
782 select_font (caseless_str fn, bool isbold, bool isitalic)
783 {
784 std::transform (fn.begin (), fn.end (), fn.begin (), ::tolower);
785 std::string fontname;
786 if (fn == "times" || fn == "times-roman")
787 {
788 if (isitalic && isbold)
789 fontname = "Times-BoldItalic";
790 else if (isitalic)
791 fontname = "Times-Italic";
792 else if (isbold)
793 fontname = "Times-Bold";
794 else
795 fontname = "Times-Roman";
796 }
797 else if (fn == "courier")
798 {
799 if (isitalic && isbold)
800 fontname = "Courier-BoldOblique";
801 else if (isitalic)
802 fontname = "Courier-Oblique";
803 else if (isbold)
804 fontname = "Courier-Bold";
805 else
806 fontname = "Courier";
807 }
808 else if (fn == "symbol")
809 fontname = "Symbol";
810 else if (fn == "zapfdingbats")
811 fontname = "ZapfDingbats";
812 else
813 {
814 if (isitalic && isbold)
815 fontname = "Helvetica-BoldOblique";
816 else if (isitalic)
817 fontname = "Helvetica-Oblique";
818 else if (isbold)
819 fontname = "Helvetica-Bold";
820 else
821 fontname = "Helvetica";
822 }
823 return fontname;
824 }
825
826 static void
827 escape_character (const std::string chr, std::string& str)
828 {
829 std::size_t idx = str.find (chr);
830 while (idx != std::string::npos)
831 {
832 str.insert (idx, 1, '\\');
833 idx = str.find (chr, idx + 2);
834 }
835 }
836
837 std::string
838 gl2ps_renderer::format_svg_element (std::string str, Matrix box,
839 double rotation, ColumnVector coord_pix,
840 Matrix color)
841 {
842 // Extract <defs> elements and change their id to avoid conflict with
843 // defs coming from another svg string
844 std::string::size_type n1 = str.find ("<defs>");
845 if (n1 == std::string::npos)
846 return std::string ();
847
848 std::string id, new_id;
849 n1 = str.find ("<path", ++n1);
850 std::string::size_type n2;
851
852 while (n1 != std::string::npos)
853 {
854 // Extract the identifier id='identifier'
855 n1 = str.find ("id='", n1) + 4;
856 n2 = str.find ("'", n1);
857 id = str.substr (n1, n2-n1);
858
859 new_id = std::to_string (m_svg_def_index) + "-" + id ;
860
861 str.replace (n1, n2-n1, new_id);
862
863 std::string::size_type n_ref = str.find ("#" + id);
864
865 while (n_ref != std::string::npos)
866 {
867 str.replace (n_ref + 1, id.length (), new_id);
868 n_ref = str.find ("#" + id);
869 }
870
871 n1 = str.find ("<path", n1);
872 }
873
874 m_svg_def_index++;
875
876 n1 = str.find ("<defs>");
877 n2 = str.find ("</defs>") + 7;
878
879 std::string defs = str.substr (n1, n2-n1);
880
881 // Extract the group containing the <use> elements and transform its
882 // coordinates using the bbox and coordinates info.
883
884 // Extract the original viewBox anchor
885 n1 = str.find ("viewBox='") + 9;
886 if (n1 == std::string::npos)
887 return std::string ();
888
889 n2 = str.find (" ", n1);
890 double original_x0 = std::stod (str.substr (n1, n2-n1));
891
892 n1 = n2+1;
893 n2 = str.find (" ", n1);
894 double original_y0 = std::stod (str.substr (n1, n2-n1));
895
896 // First look for local transform in the original svg
897 std::string orig_trans;
898 n1 = str.find ("<g id='page1' transform='");
899 if (n1 != std::string::npos)
900 {
901 n1 += 25;
902 n2 = str.find ("'", n1);
903 orig_trans = str.substr (n1, n2-n1);
904 n1 = n2 + 1;
905 }
906 else
907 {
908 n1 = str.find ("<g id='page1'");
909 n1 += 13;
910 }
911
912 n2 = str.find ("</g>", n1) + 4;
913
914 // The first applied transformation is the right-most
915 // 1* Apply original transform
916 std::string tform = orig_trans;
917
918 // 2* Move the anchor to the final position
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))
922 + ") " + tform;
923
924 // 3* Rotate around the final position
925 if (rotation != 0)
926 tform = std::string ("rotate")
927 + "(" + std::to_string (-rotation)
928 + "," + std::to_string (coord_pix(0))
929 + "," + std::to_string (coord_pix(1))
930 + ") " + tform;
931
932 // Fill color
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)) + ")' ";
937
938 std::string use_group = "<g "
939 + fill
940 + "transform='" + tform + "'"
941 + str.substr (n1, n2-n1);
942
943 return defs + "\n" + use_group;
944 }
945
946 std::string
947 gl2ps_renderer::strlist_to_svg (double x, double y, double z,
948 Matrix box, double rotation,
949 std::list<text_renderer::string>& lst)
950 {
951 //Use pixel coordinates to conform to gl2ps
952 ColumnVector coord_pix = get_transform ().transform (x, y, z, false);
953
954 if (lst.empty ())
955 return "";
956
957 // This may already be an svg image.
958 std::string svg = lst.front ().get_svg_element ();
959 if (! svg.empty ())
960 return format_svg_element (svg, box, rotation, coord_pix,
961 lst.front ().get_color ());
962
963 // Rotation and translation are applied to the whole group
964 std::ostringstream os;
965 os << R"(<g xml:space="preserve" )";
966 os << "transform=\""
967 << "translate(" << coord_pix(0) + box(0) << "," << coord_pix(1) - box(1)
968 << ") rotate(" << -rotation << "," << -box(0) << "," << box(1)
969 << ")\" ";
970
971 // Use the first entry for the base text font
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 ();
977
978 os << "font-family=\"" << name << "\" "
979 << "font-weight=\"" << weight << "\" "
980 << "font-style=\"" << angle << "\" "
981 << "font-size=\"" << size << "\">";
982
983
984 // Build a text element for each element in the strlist
985 for (p = lst.begin (); p != lst.end (); p++)
986 {
987 os << "<text ";
988
989 if (name.compare (p->get_family ()))
990 os << "font-family=\"" << p->get_family () << "\" ";
991
992 if (weight.compare (p->get_weight ()))
993 os << "font-weight=\"" << p->get_weight () << "\" ";
994
995 if (angle.compare (p->get_angle ()))
996 os << "font-style=\"" << p->get_angle () << "\" ";
997
998 if (size != p->get_size ())
999 os << "font-size=\"" << p->get_size () << "\" ";
1000
1001 os << "y=\"" << - p->get_y () << "\" ";
1002
1003 Matrix col = p->get_color ();
1004 os << "fill=\"rgb(" << col(0)*255 << ","
1005 << col(1)*255 << "," << col(2)*255 << ")\" ";
1006
1007 // provide an x coordinate for each character in the string
1008 os << "x=\"";
1009 std::vector<double> xdata = p->get_xdata ();
1010 for (auto q = xdata.begin (); q != xdata.end (); q++)
1011 os << (*q) << " ";
1012 os << '"';
1013
1014 os << '>';
1015
1016 // translate unicode and special xml characters
1017 if (p->get_code ())
1018 os << "&#" << p->get_code () << ";";
1019 else
1020 {
1021 const std::string str = p->get_string ();
1022 for (auto q = str.begin (); q != str.end (); q++)
1023 {
1024 std::stringstream chr;
1025 chr << *q;
1026 if (chr.str () == "\"")
1027 os << "&quot;";
1028 else if (chr.str () == "'")
1029 os << "&apos;";
1030 else if (chr.str () == "&")
1031 os << "&amp;";
1032 else if (chr.str () == "<")
1033 os << "&lt;";
1034 else if (chr.str () == ">")
1035 os << "&gt;";
1036 else
1037 os << chr.str ();
1038 }
1039 }
1040 os << "</text>";
1041 }
1042 os << "</g>";
1043
1044 return os.str ();
1045 }
1046
1047 std::string
1048 gl2ps_renderer::strlist_to_ps (double x, double y, double z,
1049 Matrix box, double rotation,
1050 std::list<text_renderer::string>& lst)
1051 {
1052 if (lst.empty ())
1053 return "";
1054 else if (lst.size () == 1)
1055 {
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 ())
1059 {
1060 if (! warned)
1061 {
1062 warned = true;
1063 warning_with_id ("Octave:print:unhandled-svg-content",
1064 "print: unhandled LaTeX strings. "
1065 "Use -svgconvert option or -d*latex* output "
1066 "device.");
1067 }
1068 return "";
1069 }
1070 }
1071
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);
1075
1076 std::ostringstream ss;
1077 ss << "gsave\n";
1078
1079 static bool warned = false;
1080
1081 for (const auto& txtobj : lst)
1082 {
1083 // Color
1084 if (txtobj.get_color () != prev_color)
1085 {
1086 prev_color = txtobj.get_color ();
1087 for (int i = 0; i < 3; i++)
1088 ss << prev_color(i) << " ";
1089
1090 ss << "C\n";
1091 }
1092
1093 // String
1094 std::string str;
1095 if (txtobj.get_code ())
1096 {
1097 m_fontname = "Symbol";
1098 str = code_to_symbol (txtobj.get_code ());
1099 }
1100 else
1101 {
1102 m_fontname = select_font (txtobj.get_name (),
1103 txtobj.get_weight () == "bold",
1104 txtobj.get_angle () == "italic");
1105
1106 // Check that the string is composed of single byte characters
1107 const std::string tmpstr = txtobj.get_string ();
1108 const uint8_t *c
1109 = reinterpret_cast<const uint8_t *> (tmpstr.c_str ());
1110
1111 for (std::size_t i = 0; i < tmpstr.size ();)
1112 {
1113 int mblen = octave_u8_strmblen_wrapper (c + i);
1114
1115 // Replace multibyte or non ascii characters by a question mark
1116 if (mblen > 1)
1117 {
1118 str += "?";
1119 if (! warned)
1120 {
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.");
1126 warned = true;
1127 }
1128 }
1129 else if (mblen < 1)
1130 {
1131 mblen = 1;
1132 str += "?";
1133 if (! warned)
1134 {
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.");
1140 warned = true;
1141 }
1142 }
1143 else
1144 str += tmpstr.at (i);
1145
1146 i += mblen;
1147 }
1148 }
1149
1150 escape_character ("\\", str);
1151 escape_character ("(", str);
1152 escape_character (")", str);
1153
1154 ss << "(" << str << ") [";
1155
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] << " ";
1159
1160 ss << "10] " << rotation << " " << txtobj.get_x ()
1161 << " " << txtobj.get_y () << " " << txtobj.get_size ()
1162 << " /" << m_fontname << " SRX\n";
1163 }
1164
1165 ss << "grestore\n";
1166
1167 return ss.str ();
1168 }
1169
1170 Matrix
1171 gl2ps_renderer::render_text (const std::string& txt,
1172 double x, double y, double z,
1173 int ha, int va, double rotation)
1174 {
1175 std::string saved_font = m_fontname;
1176
1177 if (txt.empty ())
1178 return Matrix (1, 4, 0.0);
1179
1180 Matrix bbox;
1181 std::string str = txt;
1182 std::list<text_renderer::string> lst;
1183
1184 text_to_strlist (str, lst, bbox, ha, va, rotation);
1185 m_glfcns.glRasterPos3d (x, y, z);
1186
1187 // For svg/eps directly dump a preformated text element into gl2ps output
1188 if (m_term.find ("svg") != std::string::npos)
1189 {
1190 std::string elt = strlist_to_svg (x, y, z, bbox, rotation, lst);
1191 if (! elt.empty ())
1192 gl2psSpecial (GL2PS_SVG, elt.c_str ());
1193 }
1194 else if (m_term.find ("eps") != std::string::npos)
1195 {
1196 std::string elt = strlist_to_ps (x, y, z, bbox, rotation, lst);
1197 if (! elt.empty ())
1198 gl2psSpecial (GL2PS_EPS, elt.c_str ());
1199
1200 }
1201 else
1202 gl2psTextOpt (str.c_str (), m_fontname.c_str (), m_fontsize,
1203 alignment_to_mode (ha, va), rotation);
1204
1205 m_fontname = saved_font;
1206
1207 return bbox;
1208 }
1209
1210 void
1211 gl2ps_renderer::set_font (const base_properties& props)
1212 {
1213 opengl_renderer::set_font (props);
1214
1215 // Set the interpreter so that text_to_pixels can parse strings properly
1216 if (props.has_property ("interpreter"))
1217 set_interpreter (props.get ("interpreter").string_value ());
1218
1219 m_fontsize = props.get ("__fontsize_points__").double_value ();
1220
1221 caseless_str fn = props.get ("fontname").xtolower ().string_value ();
1222 bool isbold
1223 =(props.get ("fontweight").xtolower ().string_value () == "bold");
1224 bool isitalic
1225 = (props.get ("fontangle").xtolower ().string_value () == "italic");
1226
1227 m_fontname = select_font (fn, isbold, isitalic);
1228 }
1229
1230 void
1231 gl2ps_renderer::draw_image (const image::properties& props)
1232 {
1233 octave_value cdata = props.get_color_data ();
1234 dim_vector dv (cdata.dims ());
1235 int h = dv(0);
1236 int w = dv(1);
1237
1238 Matrix x = props.get_xdata ().matrix_value ();
1239 Matrix y = props.get_ydata ().matrix_value ();
1240
1241 // Someone wants us to draw an empty image? No way.
1242 if (x.isempty () || y.isempty ())
1243 return;
1244
1245 // Sort x/ydata and mark flipped dimensions
1246 bool xflip = false;
1247 if (x(0) > x(1))
1248 {
1249 std::swap (x(0), x(1));
1250 xflip = true;
1251 }
1252 else if (w > 1 && x(1) == x(0))
1253 x(1) = x(1) + (w-1);
1254
1255 bool yflip = false;
1256 if (y(0) > y(1))
1257 {
1258 std::swap (y(0), y(1));
1259 yflip = true;
1260 }
1261 else if (h > 1 && y(1) == y(0))
1262 y(1) = y(1) + (h-1);
1263
1264
1265 const ColumnVector p0 = m_xform.transform (x(0), y(0), 0);
1266 const ColumnVector p1 = m_xform.transform (x(1), y(1), 0);
1267
1268 if (math::isnan (p0(0)) || math::isnan (p0(1))
1269 || math::isnan (p1(0)) || math::isnan (p1(1)))
1270 {
1271 warning ("opengl_renderer: image X,Y data too large to draw");
1272 return;
1273 }
1274
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;
1279
1280 if (w > 1)
1281 {
1282 pix_dx = (p1(0) - p0(0)) / (w-1);
1283 nor_dx = (x(1) - x(0)) / (w-1);
1284 }
1285 else
1286 {
1287 const ColumnVector p1w = m_xform.transform (x(1) + 1, y(1), 0);
1288 pix_dx = p1w(0) - p0(0);
1289 nor_dx = 1;
1290 }
1291
1292 if (h > 1)
1293 {
1294 pix_dy = (p1(1) - p0(1)) / (h-1);
1295 nor_dy = (y(1) - y(0)) / (h-1);
1296 }
1297 else
1298 {
1299 const ColumnVector p1h = m_xform.transform (x(1), y(1) + 1, 0);
1300 pix_dy = p1h(1) - p0(1);
1301 nor_dy = 1;
1302 }
1303
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.
1306
1307 int j0, j1, jj, i0, i1, ii;
1308 j0 = 0, j1 = w;
1309 i0 = 0, i1 = h;
1310
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;
1315
1316 // Clip to axes or viewport
1317 bool do_clip = props.is_clipping ();
1318 Matrix vp = get_viewport_scaled ();
1319
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));
1324
1325 if (vp_lim_min(0) > vp_lim_max(0))
1326 std::swap (vp_lim_min(0), vp_lim_max(0));
1327
1328 if (vp_lim_min(1) > vp_lim_max(1))
1329 std::swap (vp_lim_min(1), vp_lim_max(1));
1330
1331 float clip_xmin
1332 = do_clip ? (vp_lim_min(0) > m_xmin ? vp_lim_min(0) : m_xmin) : vp_lim_min(0);
1333
1334 float clip_ymin
1335 = do_clip ? (vp_lim_min(1) > m_ymin ? vp_lim_min(1) : m_ymin) : vp_lim_min(1);
1336
1337 float clip_xmax
1338 = do_clip ? (vp_lim_max(0) < m_xmax ? vp_lim_max(0) : m_xmax) : vp_lim_max(0);
1339
1340 float clip_ymax
1341 = do_clip ? (vp_lim_max(1) < m_ymax ? vp_lim_max(1) : m_ymax) : vp_lim_max(1);
1342
1343 if (im_xmin < clip_xmin)
1344 j0 += (clip_xmin - im_xmin)/nor_dx + 1;
1345
1346 if (im_xmax > clip_xmax)
1347 j1 -= (im_xmax - clip_xmax)/nor_dx;
1348
1349 if (im_ymin < clip_ymin)
1350 i0 += (clip_ymin - im_ymin)/nor_dy + 1;
1351
1352 if (im_ymax > clip_ymax)
1353 i1 -= (im_ymax - clip_ymax)/nor_dy;
1354
1355 if (i0 >= i1 || j0 >= j1)
1356 return;
1357
1358 float zoom_x;
1359 m_glfcns.glGetFloatv (GL_ZOOM_X, &zoom_x);
1360 float zoom_y;
1361 m_glfcns.glGetFloatv (GL_ZOOM_Y, &zoom_y);
1362
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);
1365
1366 // Expect RGB data
1367 if (dv.ndims () == 3 && dv(2) == 3)
1368 {
1369 if (cdata.is_double_type ())
1370 {
1371 const NDArray xcdata = cdata.array_value ();
1372
1373 OCTAVE_LOCAL_BUFFER (GLfloat, a,
1374 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1375
1376 for (int i = i0; i < i1; i++)
1377 {
1378 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1379 {
1380 if (! yflip)
1381 ii = i;
1382 else
1383 ii = h - i - 1;
1384
1385 if (! xflip)
1386 jj = j;
1387 else
1388 jj = w - j - 1;
1389
1390 a[idx] = xcdata(ii, jj, 0);
1391 a[idx+1] = xcdata(ii, jj, 1);
1392 a[idx+2] = xcdata(ii, jj, 2);
1393 }
1394 }
1395
1396 draw_pixels (j1-j0, i1-i0, a);
1397
1398 }
1399 else if (cdata.is_single_type ())
1400 {
1401 const FloatNDArray xcdata = cdata.float_array_value ();
1402
1403 OCTAVE_LOCAL_BUFFER (GLfloat, a,
1404 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1405
1406 for (int i = i0; i < i1; i++)
1407 {
1408 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1409 {
1410 if (! yflip)
1411 ii = i;
1412 else
1413 ii = h - i - 1;
1414
1415 if (! xflip)
1416 jj = j;
1417 else
1418 jj = w - j - 1;
1419
1420 a[idx] = xcdata(ii, jj, 0);
1421 a[idx+1] = xcdata(ii, jj, 1);
1422 a[idx+2] = xcdata(ii, jj, 2);
1423 }
1424 }
1425
1426 draw_pixels (j1-j0, i1-i0, a);
1427
1428 }
1429 else if (cdata.is_uint8_type ())
1430 {
1431 const uint8NDArray xcdata = cdata.uint8_array_value ();
1432
1433 OCTAVE_LOCAL_BUFFER (GLubyte, a,
1434 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1435
1436 for (int i = i0; i < i1; i++)
1437 {
1438 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1439 {
1440 if (! yflip)
1441 ii = i;
1442 else
1443 ii = h - i - 1;
1444
1445 if (! xflip)
1446 jj = j;
1447 else
1448 jj = w - j - 1;
1449
1450 a[idx] = xcdata(ii, jj, 0);
1451 a[idx+1] = xcdata(ii, jj, 1);
1452 a[idx+2] = xcdata(ii, jj, 2);
1453 }
1454 }
1455
1456 draw_pixels (j1-j0, i1-i0, a);
1457
1458 }
1459 else if (cdata.is_uint16_type ())
1460 {
1461 const uint16NDArray xcdata = cdata.uint16_array_value ();
1462
1463 OCTAVE_LOCAL_BUFFER (GLushort, a,
1464 static_cast<size_t> (3)*(j1-j0)*(i1-i0));
1465
1466 for (int i = i0; i < i1; i++)
1467 {
1468 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3)
1469 {
1470 if (! yflip)
1471 ii = i;
1472 else
1473 ii = h - i - 1;
1474
1475 if (! xflip)
1476 jj = j;
1477 else
1478 jj = w - j - 1;
1479
1480 a[idx] = xcdata(ii, jj, 0);
1481 a[idx+1] = xcdata(ii, jj, 1);
1482 a[idx+2] = xcdata(ii, jj, 2);
1483 }
1484 }
1485
1486 draw_pixels (j1-j0, i1-i0, a);
1487
1488 }
1489 else
1490 warning ("opengl_renderer: invalid image data type (expected double, single, uint8, or uint16)");
1491
1492 m_glfcns.glPixelZoom (zoom_x, zoom_y);
1493
1494 }
1495 }
1496
1497 void
1498 gl2ps_renderer::draw_pixels (int w, int h, const float *data)
1499 {
1500 // Clip data between 0 and 1 for float values
1501 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1502
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]));
1505
1506 gl2psDrawPixels (w, h, 0, 0, GL_RGB, GL_FLOAT, tmp_data);
1507 }
1508
1509 void
1510 gl2ps_renderer::draw_pixels (int w, int h, const uint8_t *data)
1511 {
1512 // gl2psDrawPixels only supports the GL_FLOAT type.
1513
1514 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1515
1516 static const float maxval = std::numeric_limits<uint8_t>::max ();
1517
1518 for (int i = 0; i < 3*w*h; i++)
1519 tmp_data[i] = data[i] / maxval;
1520
1521 draw_pixels (w, h, tmp_data);
1522 }
1523
1524 void
1525 gl2ps_renderer::draw_pixels (int w, int h, const uint16_t *data)
1526 {
1527 // gl2psDrawPixels only supports the GL_FLOAT type.
1528
1529 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);
1530
1531 static const float maxval = std::numeric_limits<uint16_t>::max ();
1532
1533 for (int i = 0; i < 3*w*h; i++)
1534 tmp_data[i] = data[i] / maxval;
1535
1536 draw_pixels (w, h, tmp_data);
1537 }
1538
1539 void
1540 gl2ps_renderer::draw_text (const text::properties& props)
1541 {
1542 if (props.get_string ().isempty ())
1543 return;
1544
1545 draw_text_background (props, true);
1546
1547 // First set font properties: freetype will use them to compute
1548 // coordinates and gl2ps will retrieve the color directly from the
1549 // feedback buffer
1550 set_font (props);
1551 set_color (props.get_color_rgb ());
1552
1553 std::string saved_font = m_fontname;
1554
1555 // Alignment
1556 int halign = 0;
1557 int valign = 0;
1558
1559 if (props.horizontalalignment_is ("center"))
1560 halign = 1;
1561 else if (props.horizontalalignment_is ("right"))
1562 halign = 2;
1563
1564 if (props.verticalalignment_is ("top"))
1565 valign = 2;
1566 else if (props.verticalalignment_is ("baseline"))
1567 valign = 3;
1568 else if (props.verticalalignment_is ("middle"))
1569 valign = 1;
1570
1571 // FIXME: handle margin and surrounding box
1572 // Matrix bbox;
1573
1574 const Matrix pos = get_transform ().scale (props.get_data_position ());
1575 std::string str = props.get_string ().string_vector_value ().join ("\n");
1576
1577 render_text (str, pos(0), pos(1), pos.numel () > 2 ? pos(2) : 0.0,
1578 halign, valign, props.get_rotation ());
1579 }
1580}
1581
1582#endif
1583
1584namespace octave
1585{
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.
1588
1589 void
1590 gl2ps_print (opengl_functions& glfcns, const graphics_object& fig,
1591 const std::string& stream, const std::string& term)
1592 {
1593#if defined (HAVE_GL2PS_H) && defined (HAVE_OPENGL)
1594
1595 // FIXME: should we have a way to create a file that begins with the
1596 // character '|'?
1597
1598 bool have_cmd = stream.length () > 1 && stream[0] == '|';
1599
1600 FILE *m_fp = nullptr;
1601
1602 unwind_protect frame;
1603
1604 if (have_cmd)
1605 {
1606 // Create process and pipe gl2ps output to it.
1607
1608 std::string cmd = stream.substr (1);
1609
1610 m_fp = popen (cmd.c_str (), "w");
1611
1612 if (! m_fp)
1613 error (R"(print: failed to open pipe "%s")", stream.c_str ());
1614
1615 // Need octave:: qualifier here to avoid ambiguity.
1616 frame.add ([=] () { octave::pclose (m_fp); });
1617 }
1618 else
1619 {
1620 // Write gl2ps output directly to file.
1621
1622 m_fp = sys::fopen (stream.c_str (), "w");
1623
1624 if (! m_fp)
1625 error (R"(gl2ps_print: failed to create file "%s")", stream.c_str ());
1626
1627 frame.add ([=] () { std::fclose (m_fp); });
1628 }
1629
1630 gl2ps_renderer rend (glfcns, m_fp, term);
1631
1632 Matrix pos = fig.get ("position").matrix_value ();
1633 rend.set_viewport (pos(2), pos(3));
1634 rend.draw (fig, stream);
1635
1636 // Make sure buffered commands are finished!!!
1637 rend.finish ();
1638
1639#else
1640
1641 octave_unused_parameter (glfcns);
1642 octave_unused_parameter (fig);
1643 octave_unused_parameter (stream);
1644 octave_unused_parameter (term);
1645
1646 err_disabled_feature ("gl2ps_print", "gl2ps");
1647
1648#endif
1649 }
1650}
#define SEEK_SET
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
Definition: dMatrix.h:42
virtual void set_linejoin(const std::string &)
Definition: gl-render.h:112
virtual void draw_axes(const axes::properties &props)
Definition: gl-render.cc:2305
virtual void draw(const graphics_object &go, bool toplevel=true)
Definition: gl-render.cc:714
virtual void set_polygon_offset(bool on, float offset=0.0f)
Definition: gl-render.cc:4261
virtual void init_marker(const std::string &m, double size, float width)
Definition: gl-render.cc:4450
virtual void set_linestyle(const std::string &s, bool stipple=false, double linewidth=0.5)
Definition: gl-render.cc:4310
bool is_scalar_type(void) const
Definition: ov.h:789
bool is_double_type(void) const
Definition: ov.h:740
double double_value(bool frc_str_conv=false) const
Definition: ov.h:886
T eps(const T &x)
Definition: data.cc:4842
void warning(const char *fmt,...)
Definition: error.cc:1055
void error(const char *fmt,...)
Definition: error.cc:980
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5861
QString name
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)
Definition: lo-mappers.h:71
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)
Definition: file-ops.cc:247
int pipe(int *fildes)
std::string fgets(FILE *f)
Definition: lo-utils.cc:85
static uint32_t state[624]
Definition: randmtzig.cc:192
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)
Definition: oct-string.cc:85
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.
Definition: ov-java.cc:1386
void respond_to_pending_signals(void)
Definition: sighandlers.cc:111
FILE * octave_tmpfile_wrapper(void)
int octave_ftruncate_wrapper(int fd, off_t sz)