gl2ps-renderer.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2009-2012 Shai Ayal
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #if defined (HAVE_OPENGL)
00028 
00029 #include <cstdio>
00030 
00031 #include "lo-mappers.h"
00032 #include "oct-locbuf.h"
00033 
00034 #include "gl2ps-renderer.h"
00035 #include "gl2ps.h"
00036 
00037 void
00038 glps_renderer::draw (const graphics_object& go)
00039 {
00040   static bool in_draw = false;
00041 
00042   if (!in_draw)
00043     {
00044       in_draw = true;
00045 
00046       FILE *fp = fdopen (fid, "wb");
00047 
00048       if (! fp)
00049         {
00050           error ("gl2ps-renderer: fdopen failed");
00051           return;
00052         }
00053 
00054       GLint buffsize = 0, state = GL2PS_OVERFLOW;
00055       GLint viewport[4];
00056 
00057       glGetIntegerv (GL_VIEWPORT, viewport);
00058 
00059       GLint gl2ps_term;
00060       if (term.find ("eps") != std::string::npos) gl2ps_term = GL2PS_EPS;
00061       else if (term.find ("pdf") != std::string::npos) gl2ps_term = GL2PS_PDF;
00062       else if (term.find ("svg") != std::string::npos) gl2ps_term = GL2PS_SVG;
00063       else if (term.find ("ps") != std::string::npos) gl2ps_term = GL2PS_PS;
00064       else if (term.find ("pgf") != std::string::npos) gl2ps_term = GL2PS_PGF;
00065       else if (term.find ("tex") != std::string::npos) gl2ps_term = GL2PS_TEX;
00066       else
00067         {
00068           error ("gl2ps-renderer:: Unknown terminal");
00069           return;
00070         }
00071 
00072       GLint gl2ps_text = 0;
00073       if (term.find ("notxt") != std::string::npos) gl2ps_text = GL2PS_NO_TEXT;
00074 
00075       while (state == GL2PS_OVERFLOW)
00076         {
00077           buffsize += 1024*1024;
00078           gl2psBeginPage ("glps_renderer figure", "Octave", viewport,
00079                           gl2ps_term, GL2PS_BSP_SORT,
00080                           (GL2PS_SILENT | GL2PS_SIMPLE_LINE_OFFSET
00081                            | GL2PS_NO_BLENDING | GL2PS_OCCLUSION_CULL
00082                            | GL2PS_BEST_ROOT | gl2ps_text
00083                            | GL2PS_NO_PS3_SHADING),
00084                           GL_RGBA, 0, NULL, 0, 0, 0,
00085                           buffsize, fp, "" );
00086 
00087           opengl_renderer::draw (go);
00088           state = gl2psEndPage ();
00089         }
00090 
00091       gnulib::fclose (fp);
00092 
00093       in_draw = 0;
00094     }
00095   else
00096     opengl_renderer::draw (go);
00097 }
00098 
00099 int
00100 glps_renderer::alignment_to_mode (int ha, int va) const
00101 {
00102   int gl2psa=GL2PS_TEXT_BL;
00103   if (ha == 0)
00104     {
00105       if (va == 0 || va == 3)
00106         gl2psa=GL2PS_TEXT_BL;
00107       else if (va == 2)
00108         gl2psa=GL2PS_TEXT_TL;
00109       else if (va == 1)
00110         gl2psa=GL2PS_TEXT_CL;
00111     }
00112   else if (ha == 2)
00113     {
00114       if (va == 0 || va == 3)
00115         gl2psa=GL2PS_TEXT_BR;
00116       else if (va == 2)
00117         gl2psa=GL2PS_TEXT_TR;
00118       else if (va == 1)
00119         gl2psa=GL2PS_TEXT_CR;
00120     }
00121   else if (ha == 1)
00122     {
00123       if (va == 0 || va == 3)
00124         gl2psa=GL2PS_TEXT_B;
00125       else if (va == 2)
00126         gl2psa=GL2PS_TEXT_T;
00127       else if (va == 1)
00128         gl2psa=GL2PS_TEXT_C;
00129     }
00130   return gl2psa;
00131 }
00132 
00133 Matrix
00134 glps_renderer::render_text (const std::string& txt,
00135                             double x, double y, double z,
00136                             int ha, int va, double rotation)
00137 {
00138   if (txt.empty ())
00139     return Matrix (1, 4, 0.0);
00140 
00141   glRasterPos3d (x, y, z);
00142   gl2psTextOpt (txt.c_str (), fontname.c_str (), fontsize,
00143                 alignment_to_mode (ha, va), rotation);
00144 
00145   // FIXME? -- we have no way of getting a bounding box from gl2ps, so
00146   // we use freetype
00147   Matrix bbox;
00148   uint8NDArray pixels;
00149   text_to_pixels (txt, pixels, bbox, 0, 0, rotation);
00150   return bbox;
00151 }
00152 
00153 void
00154 glps_renderer::set_font (const base_properties& props)
00155 {
00156   opengl_renderer::set_font (props);
00157 
00158   fontsize = props.get ("fontsize").double_value ();
00159 
00160   caseless_str fn = props.get ("fontname").string_value ();
00161   fontname = "";
00162   if (fn == "times" || fn == "times-roman")
00163     fontname = "Times-Roman";
00164   else if (fn == "courier")
00165     fontname = "Courier";
00166   else if (fn == "symbol")
00167     fontname = "Symbol";
00168   else if (fn == "zapfdingbats")
00169     fontname = "ZapfDingbats";
00170   else
00171     fontname = "Helvetica";
00172 
00173   // FIXME -- add support for bold and italic
00174 }
00175 
00176 template <typename T>
00177 static void
00178 draw_pixels (GLsizei w, GLsizei h, GLenum format, const T *data)
00179 {
00180   OCTAVE_LOCAL_BUFFER (GLfloat, a, 3*w*h);
00181 
00182   for (int i = 0; i < 3*w*h; i++)
00183     a[i] = data[i];
00184 
00185   gl2psDrawPixels (w, h, 0, 0, format, GL_FLOAT, a);
00186 }
00187 
00188 void
00189 glps_renderer::draw_pixels (GLsizei w, GLsizei h, GLenum format,
00190                             GLenum type, const GLvoid *data)
00191 {
00192   if (type == GL_UNSIGNED_SHORT)
00193     ::draw_pixels (w, h, format, static_cast<const GLushort *> (data));
00194   else if (type == GL_UNSIGNED_BYTE)
00195     ::draw_pixels (w, h, format, static_cast<const GLubyte *> (data));
00196   else
00197     gl2psDrawPixels (w, h, 0, 0, format, type, data);
00198 }
00199 
00200 void
00201 glps_renderer::draw_text (const text::properties& props)
00202 {
00203   if (props.get_string ().is_empty ())
00204     return;
00205 
00206   set_font (props);
00207   set_color (props.get_color_rgb ());
00208 
00209   const Matrix pos = get_transform ().scale (props.get_data_position ());
00210   int halign = 0, valign = 0;
00211 
00212   if (props.horizontalalignment_is ("center"))
00213     halign = 1;
00214   else if (props.horizontalalignment_is ("right"))
00215     halign = 2;
00216 
00217   if (props.verticalalignment_is ("top"))
00218     valign = 2;
00219   else if (props.verticalalignment_is ("baseline"))
00220     valign = 3;
00221   else if (props.verticalalignment_is ("middle"))
00222     valign = 1;
00223 
00224   // FIXME: handle margin and surrounding box
00225 
00226   glRasterPos3d (pos(0), pos(1), pos(2));
00227 
00228   octave_value string_prop = props.get_string ();
00229 
00230   string_vector sv = string_prop.all_strings ();
00231 
00232   std::string s = sv.join ("\n");
00233 
00234   gl2psTextOpt (s.c_str (), fontname.c_str (), fontsize,
00235                 alignment_to_mode (halign, valign), props.get_rotation ());
00236 }
00237 
00238 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines