GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
gl2ps-renderer.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2015 Shai Ayal
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "error.h"
28 #include "gl2ps-renderer.h"
29 
30 #ifdef HAVE_GL2PS_H
31 
32 #include <cstdio>
33 
34 #include "lo-mappers.h"
35 #include "oct-locbuf.h"
36 #include "unwind-prot.h"
37 
38 #include "gl2ps.h"
39 #include "sysdep.h"
40 
41 void
42 glps_renderer::draw (const graphics_object& go, const std::string& print_cmd)
43 {
44  static bool in_draw = false;
45  static std::string old_print_cmd;
46 
47  if (!in_draw)
48  {
49  unwind_protect frame;
50 
51  frame.protect_var (in_draw);
52 
53  in_draw = true;
54 
55  GLint gl2ps_term;
56  if (term.find ("eps") != std::string::npos)
57  gl2ps_term = GL2PS_EPS;
58  else if (term.find ("pdf") != std::string::npos)
59  gl2ps_term = GL2PS_PDF;
60  else if (term.find ("ps") != std::string::npos)
61  gl2ps_term = GL2PS_PS;
62  else if (term.find ("svg") != std::string::npos)
63  gl2ps_term = GL2PS_SVG;
64  else if (term.find ("pgf") != std::string::npos)
65  gl2ps_term = GL2PS_PGF;
66  else if (term.find ("tex") != std::string::npos)
67  gl2ps_term = GL2PS_TEX;
68  else
69  {
70  error ("gl2ps-renderer::draw: Unknown terminal %s", term.c_str ());
71  return;
72  }
73 
74  GLint gl2ps_text = 0;
75  if (term.find ("notxt") != std::string::npos)
76  gl2ps_text = GL2PS_NO_TEXT;
77 
78  // Default sort order optimizes for 3D plots
79  GLint gl2ps_sort = GL2PS_BSP_SORT;
80 
81  // For 2D plots we can use a simpler Z-depth sorting algorithm
82  if (term.find ("is2D") != std::string::npos)
83  gl2ps_sort = GL2PS_SIMPLE_SORT;
84 
85  GLint state = GL2PS_OVERFLOW;
86  GLint buffsize = 0;
87 
88  while (state == GL2PS_OVERFLOW)
89  {
90  // For LaTeX output the fltk print process uses 2 drawnow() commands.
91  // The first one is for the pdf/ps/eps graph to be included. The
92  // print_cmd is saved as old_print_cmd. Then the second drawnow()
93  // outputs the tex-file and the graphic filename to be included is
94  // extracted from old_print_cmd.
95 
96  std::string include_graph;
97 
98  size_t found_redirect = old_print_cmd.find (">");
99 
100  if (found_redirect != std::string::npos)
101  include_graph = old_print_cmd.substr (found_redirect + 1);
102  else
103  include_graph = old_print_cmd;
104 
105  size_t n_begin = include_graph.find_first_not_of (" ");
106 
107  if (n_begin != std::string::npos)
108  {
109  size_t n_end = include_graph.find_last_not_of (" ");
110  include_graph = include_graph.substr (n_begin,
111  n_end - n_begin + 1);
112  }
113  else
114  include_graph = "foobar-inc";
115 
116  buffsize += 1024*1024;
117 
118  // GL2PS_SILENT was removed to allow gl2ps printing errors on stderr
119  GLint ret = gl2psBeginPage ("glps_renderer figure", "Octave", 0,
120  gl2ps_term, gl2ps_sort,
121  (GL2PS_NO_BLENDING
122  | GL2PS_OCCLUSION_CULL
123  | GL2PS_BEST_ROOT
124  | gl2ps_text
125  | GL2PS_NO_PS3_SHADING
126  | GL2PS_USE_CURRENT_VIEWPORT),
127  GL_RGBA, 0, 0, 0, 0, 0,
128  buffsize, fp, include_graph.c_str ());
129  if (ret == GL2PS_ERROR)
130  {
131  old_print_cmd.clear ();
132  error ("gl2ps-renderer::draw: gl2psBeginPage returned GL2PS_ERROR");
133  return;
134  }
135 
136  old_print_cmd = print_cmd;
137 
138  opengl_renderer::draw (go);
139 
140  // Without glFinish () there may primitives be missing in the
141  // gl2ps output.
142  glFinish ();
143 
144  state = gl2psEndPage ();
145 
146  if (state == GL2PS_NO_FEEDBACK)
147  {
148  warning ("gl2ps-renderer::draw: empty feedback buffer and/or nothing else to print");
149  }
150  else if (state == GL2PS_ERROR)
151  {
152  old_print_cmd.clear ();
153  error ("gl2ps-renderer::draw: gl2psEndPage returned GL2PS_ERROR");
154  return;
155  }
156 
157  // Don't check state for GL2PS_UNINITIALIZED (should never happen)
158  // GL2PS_OVERFLOW (see while loop) or GL2PS_SUCCESS
159  }
160  }
161  else
162  opengl_renderer::draw (go);
163 }
164 
165 int
166 glps_renderer::alignment_to_mode (int ha, int va) const
167 {
168  int gl2psa = GL2PS_TEXT_BL;
169 
170  if (ha == 0)
171  {
172  if (va == 0 || va == 3)
173  gl2psa=GL2PS_TEXT_BL;
174  else if (va == 2)
175  gl2psa=GL2PS_TEXT_TL;
176  else if (va == 1)
177  gl2psa=GL2PS_TEXT_CL;
178  }
179  else if (ha == 2)
180  {
181  if (va == 0 || va == 3)
182  gl2psa=GL2PS_TEXT_BR;
183  else if (va == 2)
184  gl2psa=GL2PS_TEXT_TR;
185  else if (va == 1)
186  gl2psa=GL2PS_TEXT_CR;
187  }
188  else if (ha == 1)
189  {
190  if (va == 0 || va == 3)
191  gl2psa=GL2PS_TEXT_B;
192  else if (va == 2)
193  gl2psa=GL2PS_TEXT_T;
194  else if (va == 1)
195  gl2psa=GL2PS_TEXT_C;
196  }
197 
198  return gl2psa;
199 }
200 
201 Matrix
202 glps_renderer::render_text (const std::string& txt,
203  double x, double y, double z,
204  int ha, int va, double rotation)
205 {
206  if (txt.empty ())
207  return Matrix (1, 4, 0.0);
208 
209  glRasterPos3d (x, y, z);
210 
211  gl2psTextOpt (txt.c_str (), fontname.c_str (), fontsize,
212  alignment_to_mode (ha, va), rotation);
213 
214  // FIXME?
215  // We have no way of getting a bounding box from gl2ps, so we use FreeType.
216  Matrix bbox;
217  uint8NDArray pixels;
218  text_to_pixels (txt, pixels, bbox, 0, 0, rotation);
219 
220  return bbox;
221 }
222 
223 void
224 glps_renderer::set_font (const base_properties& props)
225 {
226  opengl_renderer::set_font (props);
227 
228  fontsize = props.get ("fontsize_points").double_value ();
229 
230  caseless_str fn = props.get ("fontname").xtolower ().string_value ();
231  bool isbold =
232  (props.get ("fontweight").xtolower ().string_value () == "bold");
233  bool isitalic =
234  (props.get ("fontangle").xtolower ().string_value () == "italic");
235 
236  fontname = "";
237  if (fn == "times" || fn == "times-roman")
238  {
239  if (isitalic && isbold)
240  fontname = "Times-BoldItalic";
241  else if (isitalic)
242  fontname = "Times-Italic";
243  else if (isbold)
244  fontname = "Times-Bold";
245  else
246  fontname = "Times-Roman";
247  }
248  else if (fn == "courier")
249  {
250  if (isitalic && isbold)
251  fontname = "Courier-BoldOblique";
252  else if (isitalic)
253  fontname = "Courier-Oblique";
254  else if (isbold)
255  fontname = "Courier-Bold";
256  else
257  fontname = "Courier";
258  }
259  else if (fn == "symbol")
260  fontname = "Symbol";
261  else if (fn == "zapfdingbats")
262  fontname = "ZapfDingbats";
263  else
264  {
265  if (isitalic && isbold)
266  fontname = "Helvetica-BoldOblique";
267  else if (isitalic)
268  fontname = "Helvetica-Oblique";
269  else if (isbold)
270  fontname = "Helvetica-Bold";
271  else
272  fontname = "Helvetica";
273  }
274 }
275 
276 template <typename T>
277 static void
278 draw_pixels (GLsizei w, GLsizei h, GLenum format, const T *data, float maxval)
279 {
280  OCTAVE_LOCAL_BUFFER (GLfloat, a, 3*w*h);
281 
282  // Convert to GL_FLOAT as it is the only type gl2ps accepts.
283  for (int i = 0; i < 3*w*h; i++)
284  a[i] = data[i] / maxval;
285 
286  gl2psDrawPixels (w, h, 0, 0, format, GL_FLOAT, a);
287 }
288 
289 void
290 glps_renderer::draw_pixels (GLsizei w, GLsizei h, GLenum format,
291  GLenum type, const GLvoid *data)
292 {
293  // gl2psDrawPixels only supports the GL_FLOAT type.
294  // Other formats, such as uint8, must be converted first.
295  if (type == GL_UNSIGNED_BYTE)
296  ::draw_pixels (w, h, format, static_cast<const GLubyte *> (data), 255.0f);
297  else if (type == GL_UNSIGNED_SHORT)
298  ::draw_pixels (w, h, format, static_cast<const GLushort *> (data), 65535.0f);
299  else
300  gl2psDrawPixels (w, h, 0, 0, format, type, data);
301 }
302 
303 void
304 glps_renderer::draw_text (const text::properties& props)
305 {
306  if (props.get_string ().is_empty ())
307  return;
308 
309  set_font (props);
310  set_color (props.get_color_rgb ());
311 
312  const Matrix pos = get_transform ().scale (props.get_data_position ());
313  int halign = 0;
314  int valign = 0;
315 
316  if (props.horizontalalignment_is ("center"))
317  halign = 1;
318  else if (props.horizontalalignment_is ("right"))
319  halign = 2;
320 
321  if (props.verticalalignment_is ("top"))
322  valign = 2;
323  else if (props.verticalalignment_is ("baseline"))
324  valign = 3;
325  else if (props.verticalalignment_is ("middle"))
326  valign = 1;
327 
328  // FIXME: handle margin and surrounding box
329 
330  glRasterPos3d (pos(0), pos(1), pos.numel () > 2 ? pos(2) : 0.0);
331 
332  octave_value string_prop = props.get_string ();
333 
334  string_vector sv = string_prop.all_strings ();
335 
336  std::string s = sv.join ("\n");
337 
338  gl2psTextOpt (s.c_str (), fontname.c_str (), fontsize,
339  alignment_to_mode (halign, valign), props.get_rotation ());
340 }
341 
342 static void
343 safe_pclose (FILE *f)
344 {
345  if (f)
346  octave_pclose (f);
347 }
348 
349 #endif
350 
351 void
352 gl2ps_print (const graphics_object& fig, const std::string& cmd,
353  const std::string& term)
354 {
355 #ifdef HAVE_GL2PS_H
356 
357  FILE *fp = octave_popen (cmd.c_str (), "w");
358 
359  if (fp)
360  {
361  unwind_protect frame;
362 
363  frame.add_fcn (safe_pclose, fp);
364 
365  glps_renderer rend (fp, term);
366 
367  rend.draw (fig, cmd);
368  }
369  else
370  error ("print: failed to open pipe for gl2ps renderer");
371 
372 #else
373 
374  error ("print: printing not available without gl2ps library");
375 
376 #endif
377 }
FILE * octave_popen(const char *command, const char *mode)
Definition: sysdep.cc:482
Matrix get_data_position(void) const
Definition: graphics.cc:8098
int octave_pclose(FILE *f)
Definition: sysdep.cc:502
static uint32_t state[624]
Definition: randmtzig.c:188
void protect_var(T &var)
void error(const char *fmt,...)
Definition: error.cc:476
bool verticalalignment_is(const std::string &v) const
Definition: graphics.h:7961
string_vector all_strings(bool pad=false) const
Definition: ov.h:894
void gl2ps_print(const graphics_object &fig, const std::string &cmd, const std::string &term)
octave_value xtolower(void) const
Definition: ov.h:1221
F77_RET_T const double const double * f
void add_fcn(void(*fcn)(void))
std::string string_value(bool force=false) const
Definition: ov.h:897
std::complex< double > w(std::complex< double > z, double relerr=0)
Definition: dMatrix.h:35
octave_value get_string(void) const
Definition: graphics.h:7956
void warning(const char *fmt,...)
Definition: error.cc:681
double get_rotation(void) const
Definition: graphics.h:7954
bool is_empty(void) const
Definition: ov.h:526
virtual octave_value get(const caseless_str &pname) const
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:197
double double_value(bool frc_str_conv=false) const
Definition: ov.h:759
std::string join(const std::string &sep=std::string()) const
Definition: str-vec.cc:167
F77_RET_T const double * x
Matrix get_color_rgb(void) const
Definition: graphics.h:7908
bool horizontalalignment_is(const std::string &v) const
Definition: graphics.h:7939