Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/externals/g4tools/include/tools/gl2ps

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /externals/g4tools/include/tools/gl2ps (Version 11.3.0) and /externals/g4tools/include/tools/gl2ps (Version 11.0.p1)


  1 #ifndef tools_gl2ps                                 1 #ifndef tools_gl2ps
  2 #define tools_gl2ps                                 2 #define tools_gl2ps
  3                                                     3 
  4 // This version of gl2ps-1.4.2 contains four m <<   4 //
  5 // - it is pure header.                        <<   5 //  Pure header version of gl2ps-1.4.0 done automatically from
  6 // - the code had been "namespace protected" b <<   6 // the original source by using the bush/upgrade/tools_gl2ps script.
  7 //     gl2ps_<xxx> to tools_gl2ps_<xxx>        <<   7 //  The code had been "namespace protected" by changing :
  8 //   and :                                     <<   8 //    gl2ps_<xxx> to tools_gl2ps_<xxx>
  9 //     GL2PS_<xxx> to TOOLS_GL2PS_<xxx>        <<   9 // and :
 10 // - the code had been made thread safe by avo <<  10 //    TOOLS_GL2PS_<xxx> to TOOLS_TOOLS_GL2PS_<xxx>
 11 //   static singleton gl2ps context object. Wi <<  11 //
 12 //   create yourself a context, and pass it as <<  12 //    Guy Barrand. 05/July/2019
 13 //   tools_gl2ps functions that you want to us << 
 14 //     ....                                    << 
 15 //     #include <tools/gl2ps>                  << 
 16 //     ....                                    << 
 17 //     tools_GL2PScontext* gl2ps_context = too << 
 18 //     ....                                    << 
 19 //     tools_gl2psBeginPage(gl2ps_context,...) << 
 20 //     ...                                     << 
 21 //     tools_gl2psEndPage(gl2ps_context);      << 
 22 //     ....                                    << 
 23 //     tools_gl2psDeleteContext(gl2ps_context) << 
 24 //     ....                                    << 
 25 // - it does not call directly OpenGL function << 
 26 //     glIsEnabled,glBegin,glEnd,glGetFloatv,g << 
 27 //     glGetIntegerv,glRenderMode,glFeedbackBu << 
 28 //   but pointer to functions with the same si << 
 29 //   use tools_gl2ps in a non OpenGL context,  << 
 30 //   by using tools_gl2psAddPolyPrimitive. If  << 
 31 //   got by using the OpenGL FEEDBACK mode (th << 
 32 //   to declare the OpenGL upper functions on  << 
 33 //     ....                                    << 
 34 //     #include <tools/gl2ps>                  << 
 35 //     ....                                    << 
 36 //     #include <GL/gl.h>                      << 
 37 //     ....                                    << 
 38 //     tools_GL2PScontext* gl2ps_context = too << 
 39 //     ...                                     << 
 40 //     tools_gl2ps_gl_funcs_t _funcs = {       << 
 41 //       glIsEnabled,                          << 
 42 //       glBegin,                              << 
 43 //       glEnd,                                << 
 44 //       glGetFloatv,                          << 
 45 //       glVertex3f,                           << 
 46 //       glGetBooleanv,                        << 
 47 //       glGetIntegerv,                        << 
 48 //       glRenderMode,                         << 
 49 //       glFeedbackBuffer,                     << 
 50 //       glPassThrough                         << 
 51 //     };                                      << 
 52 //     tools_gl2ps_set_gl_funcs(gl2ps_context, << 
 53 //     ...                                     << 
 54 //                                             << 
 55 //    Guy Barrand. 15/March/2022               << 
 56 //                                                 13 //
 57                                                    14 
 58 /*                                                 15 /*
 59  * GL2PS, an OpenGL to PostScript Printing Lib     16  * GL2PS, an OpenGL to PostScript Printing Library
 60  * Copyright (C) 1999-2020 C. Geuzaine         <<  17  * Copyright (C) 1999-2017 C. Geuzaine
 61  *                                                 18  *
 62  * This program is free software; you can redi     19  * This program is free software; you can redistribute it and/or
 63  * modify it under the terms of either:            20  * modify it under the terms of either:
 64  *                                                 21  *
 65  * a) the GNU Library General Public License a     22  * a) the GNU Library General Public License as published by the Free
 66  * Software Foundation, either version 2 of th     23  * Software Foundation, either version 2 of the License, or (at your
 67  * option) any later version; or                   24  * option) any later version; or
 68  *                                                 25  *
 69  * b) the GL2PS License as published by Christ     26  * b) the GL2PS License as published by Christophe Geuzaine, either
 70  * version 2 of the License, or (at your optio     27  * version 2 of the License, or (at your option) any later version.
 71  *                                                 28  *
 72  * This program is distributed in the hope tha     29  * This program is distributed in the hope that it will be useful, but
 73  * WITHOUT ANY WARRANTY; without even the impl     30  * WITHOUT ANY WARRANTY; without even the implied warranty of
 74  * MERCHANTABILITY or FITNESS FOR A PARTICULAR     31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
 75  * the GNU Library General Public License or t     32  * the GNU Library General Public License or the GL2PS License for
 76  * more details.                                   33  * more details.
 77  *                                                 34  *
 78  * You should have received a copy of the GNU      35  * You should have received a copy of the GNU Library General Public
 79  * License along with this library in the file     36  * License along with this library in the file named "COPYING.LGPL";
 80  * if not, write to the Free Software Foundati     37  * if not, write to the Free Software Foundation, Inc., 51 Franklin
 81  * Street, Fifth Floor, Boston, MA 02110-1301,     38  * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 82  *                                                 39  *
 83  * You should have received a copy of the GL2P     40  * You should have received a copy of the GL2PS License with this
 84  * library in the file named "COPYING.GL2PS";      41  * library in the file named "COPYING.GL2PS"; if not, I will be glad
 85  * to provide one.                                 42  * to provide one.
 86  *                                                 43  *
 87  * For the latest info about gl2ps and a full      44  * For the latest info about gl2ps and a full list of contributors,
 88  * see http://www.geuz.org/gl2ps/.                 45  * see http://www.geuz.org/gl2ps/.
 89  *                                                 46  *
 90  * Please report all bugs and problems to <gl2     47  * Please report all bugs and problems to <gl2ps@geuz.org>.
 91  */                                                48  */
 92                                                    49 
 93 #include "gl2ps_def.h"                         <<  50 #include "gl2ps_begin.icc"
 94                                                << 
 95 #include <stdlib.h>                            << 
 96 #include <stdio.h>                             << 
 97                                                    51 
 98 #include <math.h>                                  52 #include <math.h>
 99 #include <string.h>                                53 #include <string.h>
100 #include <sys/types.h>                             54 #include <sys/types.h>
101 #include <stdarg.h>                                55 #include <stdarg.h>
102 #include <time.h>                                  56 #include <time.h>
103 #include <float.h>                                 57 #include <float.h>
104                                                    58 
105 #if defined(TOOLS_GL2PS_HAVE_ZLIB)                 59 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
106 #include <zlib.h>                                  60 #include <zlib.h>
107 #endif                                             61 #endif
108                                                    62 
109 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)               63 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)
110 #include <png.h>                                   64 #include <png.h>
111 #endif                                             65 #endif
112                                                    66 
113 /*********************************************     67 /*********************************************************************
114  *                                                 68  *
115  * Private definitions, data structures and pr     69  * Private definitions, data structures and prototypes
116  *                                                 70  *
117  *********************************************     71  *********************************************************************/
118                                                    72 
119 /* Magic numbers (assuming that the order of m     73 /* Magic numbers (assuming that the order of magnitude of window
120    coordinates is 10^3) */                         74    coordinates is 10^3) */
121                                                    75 
122 #define TOOLS_GL2PS_EPSILON       5.0e-3F          76 #define TOOLS_GL2PS_EPSILON       5.0e-3F
123 #define TOOLS_GL2PS_ZSCALE        1000.0F          77 #define TOOLS_GL2PS_ZSCALE        1000.0F
124 #define TOOLS_GL2PS_ZOFFSET       5.0e-2F          78 #define TOOLS_GL2PS_ZOFFSET       5.0e-2F
125 #define TOOLS_GL2PS_ZOFFSET_LARGE 20.0F            79 #define TOOLS_GL2PS_ZOFFSET_LARGE 20.0F
126 #define TOOLS_GL2PS_ZERO(arg)     (fabs(arg) <     80 #define TOOLS_GL2PS_ZERO(arg)     (fabs(arg) < 1.e-20)
127                                                    81 
128 /* BSP tree primitive comparison */                82 /* BSP tree primitive comparison */
129                                                    83 
130 #define TOOLS_GL2PS_COINCIDENT  1                  84 #define TOOLS_GL2PS_COINCIDENT  1
131 #define TOOLS_GL2PS_IN_FRONT_OF 2                  85 #define TOOLS_GL2PS_IN_FRONT_OF 2
132 #define TOOLS_GL2PS_IN_BACK_OF  3                  86 #define TOOLS_GL2PS_IN_BACK_OF  3
133 #define TOOLS_GL2PS_SPANNING    4                  87 #define TOOLS_GL2PS_SPANNING    4
134                                                    88 
135 /* 2D BSP tree primitive comparison */             89 /* 2D BSP tree primitive comparison */
136                                                    90 
137 #define TOOLS_GL2PS_POINT_COINCIDENT 0             91 #define TOOLS_GL2PS_POINT_COINCIDENT 0
138 #define TOOLS_GL2PS_POINT_INFRONT    1             92 #define TOOLS_GL2PS_POINT_INFRONT    1
139 #define TOOLS_GL2PS_POINT_BACK       2             93 #define TOOLS_GL2PS_POINT_BACK       2
140                                                    94 
141 /* Internal feedback buffer pass-through token     95 /* Internal feedback buffer pass-through tokens */
142                                                    96 
143 #define TOOLS_GL2PS_BEGIN_OFFSET_TOKEN   1         97 #define TOOLS_GL2PS_BEGIN_OFFSET_TOKEN   1
144 #define TOOLS_GL2PS_END_OFFSET_TOKEN     2         98 #define TOOLS_GL2PS_END_OFFSET_TOKEN     2
145 #define TOOLS_GL2PS_BEGIN_BOUNDARY_TOKEN 3         99 #define TOOLS_GL2PS_BEGIN_BOUNDARY_TOKEN 3
146 #define TOOLS_GL2PS_END_BOUNDARY_TOKEN   4        100 #define TOOLS_GL2PS_END_BOUNDARY_TOKEN   4
147 #define TOOLS_GL2PS_BEGIN_STIPPLE_TOKEN  5        101 #define TOOLS_GL2PS_BEGIN_STIPPLE_TOKEN  5
148 #define TOOLS_GL2PS_END_STIPPLE_TOKEN    6        102 #define TOOLS_GL2PS_END_STIPPLE_TOKEN    6
149 #define TOOLS_GL2PS_POINT_SIZE_TOKEN     7        103 #define TOOLS_GL2PS_POINT_SIZE_TOKEN     7
150 #define TOOLS_GL2PS_LINE_CAP_TOKEN       8        104 #define TOOLS_GL2PS_LINE_CAP_TOKEN       8
151 #define TOOLS_GL2PS_LINE_JOIN_TOKEN      9        105 #define TOOLS_GL2PS_LINE_JOIN_TOKEN      9
152 #define TOOLS_GL2PS_LINE_WIDTH_TOKEN     10       106 #define TOOLS_GL2PS_LINE_WIDTH_TOKEN     10
153 #define TOOLS_GL2PS_BEGIN_BLEND_TOKEN    11       107 #define TOOLS_GL2PS_BEGIN_BLEND_TOKEN    11
154 #define TOOLS_GL2PS_END_BLEND_TOKEN      12       108 #define TOOLS_GL2PS_END_BLEND_TOKEN      12
155 #define TOOLS_GL2PS_SRC_BLEND_TOKEN      13       109 #define TOOLS_GL2PS_SRC_BLEND_TOKEN      13
156 #define TOOLS_GL2PS_DST_BLEND_TOKEN      14       110 #define TOOLS_GL2PS_DST_BLEND_TOKEN      14
157 #define TOOLS_GL2PS_IMAGEMAP_TOKEN       15       111 #define TOOLS_GL2PS_IMAGEMAP_TOKEN       15
158 #define TOOLS_GL2PS_DRAW_PIXELS_TOKEN    16       112 #define TOOLS_GL2PS_DRAW_PIXELS_TOKEN    16
159 #define TOOLS_GL2PS_TEXT_TOKEN           17       113 #define TOOLS_GL2PS_TEXT_TOKEN           17
160                                                   114 
161 typedef enum {                                    115 typedef enum {
162   T_UNDEFINED    = -1,                            116   T_UNDEFINED    = -1,
163   T_CONST_COLOR  = 1,                             117   T_CONST_COLOR  = 1,
164   T_VAR_COLOR    = 1<<1,                          118   T_VAR_COLOR    = 1<<1,
165   T_ALPHA_1      = 1<<2,                          119   T_ALPHA_1      = 1<<2,
166   T_ALPHA_LESS_1 = 1<<3,                          120   T_ALPHA_LESS_1 = 1<<3,
167   T_VAR_ALPHA    = 1<<4                           121   T_VAR_ALPHA    = 1<<4
168 } TOOLS_GL2PS_TRIANGLE_PROPERTY;                  122 } TOOLS_GL2PS_TRIANGLE_PROPERTY;
169                                                   123 
170 typedef tools_GLfloat tools_GL2PSplane[4];        124 typedef tools_GLfloat tools_GL2PSplane[4];
171                                                   125 
172 typedef struct tools_GL2PSbsptree2d_ tools_GL2 << 126 typedef struct _tools_GL2PSbsptree2d tools_GL2PSbsptree2d;
173                                                   127 
174 struct tools_GL2PSbsptree2d_ {                 << 128 struct _tools_GL2PSbsptree2d {
175   tools_GL2PSplane plane;                         129   tools_GL2PSplane plane;
176   tools_GL2PSbsptree2d *front, *back;             130   tools_GL2PSbsptree2d *front, *back;
177 };                                                131 };
178                                                   132 
179 typedef struct {                                  133 typedef struct {
180   tools_GLint nmax, size, incr, n;                134   tools_GLint nmax, size, incr, n;
181   char *array;                                    135   char *array;
182 } tools_GL2PSlist;                                136 } tools_GL2PSlist;
183                                                   137 
184 typedef struct tools_GL2PSbsptree_ tools_GL2PS << 138 typedef struct _tools_GL2PSbsptree tools_GL2PSbsptree;
185                                                   139 
186 struct tools_GL2PSbsptree_ {                   << 140 struct _tools_GL2PSbsptree {
187   tools_GL2PSplane plane;                         141   tools_GL2PSplane plane;
188   tools_GL2PSlist *primitives;                    142   tools_GL2PSlist *primitives;
189   tools_GL2PSbsptree *front, *back;               143   tools_GL2PSbsptree *front, *back;
190 };                                                144 };
191                                                   145 
192 typedef struct {                                  146 typedef struct {
193   tools_GL2PSvertex vertex[3];                    147   tools_GL2PSvertex vertex[3];
194   int prop;                                       148   int prop;
195 } tools_GL2PStriangle;                            149 } tools_GL2PStriangle;
196                                                   150 
197 typedef struct {                                  151 typedef struct {
198   tools_GLshort fontsize;                         152   tools_GLshort fontsize;
199   char *str, *fontname;                           153   char *str, *fontname;
200   /* Note: for a 'special' string, 'alignment'    154   /* Note: for a 'special' string, 'alignment' holds the format
201      (PostScript, PDF, etc.) of the special st    155      (PostScript, PDF, etc.) of the special string */
202   tools_GLint alignment;                          156   tools_GLint alignment;
203   tools_GLfloat angle;                            157   tools_GLfloat angle;
204 } tools_GL2PSstring;                              158 } tools_GL2PSstring;
205                                                   159 
206 typedef struct {                                  160 typedef struct {
207   tools_GLsizei width, height;                    161   tools_GLsizei width, height;
208   /* Note: for an imagemap, 'type' indicates i    162   /* Note: for an imagemap, 'type' indicates if it has already been
209      written to the file or not, and 'format'     163      written to the file or not, and 'format' indicates if it is
210      visible or not */                            164      visible or not */
211   tools_GLenum format, type;                      165   tools_GLenum format, type;
212   tools_GLfloat zoom_x, zoom_y;                   166   tools_GLfloat zoom_x, zoom_y;
213   tools_GLfloat *pixels;                          167   tools_GLfloat *pixels;
214 } tools_GL2PSimage;                               168 } tools_GL2PSimage;
215                                                   169 
216 typedef struct tools_GL2PSimagemap_ tools_GL2P << 170 typedef struct _tools_GL2PSimagemap tools_GL2PSimagemap;
217                                                   171 
218 struct tools_GL2PSimagemap_ {                  << 172 struct _tools_GL2PSimagemap {
219   tools_GL2PSimage *image;                        173   tools_GL2PSimage *image;
220   tools_GL2PSimagemap *next;                      174   tools_GL2PSimagemap *next;
221 };                                                175 };
222                                                   176 
223 typedef struct {                                  177 typedef struct {
224   tools_GLshort type, numverts;                   178   tools_GLshort type, numverts;
225   tools_GLushort pattern;                         179   tools_GLushort pattern;
226   char boundary, offset, culled;                  180   char boundary, offset, culled;
227   tools_GLint factor, linecap, linejoin, sorti << 181   tools_GLint factor, linecap, linejoin;
228   tools_GLfloat width, ofactor, ounits;           182   tools_GLfloat width, ofactor, ounits;
229   tools_GL2PSvertex *verts;                       183   tools_GL2PSvertex *verts;
230   union {                                         184   union {
231     tools_GL2PSstring *text;                      185     tools_GL2PSstring *text;
232     tools_GL2PSimage *image;                      186     tools_GL2PSimage *image;
233   } data;                                         187   } data;
234 } tools_GL2PSprimitive;                           188 } tools_GL2PSprimitive;
235                                                   189 
236 typedef struct {                                  190 typedef struct {
237 #if defined(TOOLS_GL2PS_HAVE_ZLIB)                191 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
238   Bytef *dest, *src, *start;                      192   Bytef *dest, *src, *start;
239   uLongf destLen, srcLen;                         193   uLongf destLen, srcLen;
240 #else                                             194 #else
241   int dummy;                                      195   int dummy;
242 #endif                                            196 #endif
243 } tools_GL2PScompress;                            197 } tools_GL2PScompress;
244                                                   198 
245 typedef struct{                                   199 typedef struct{
246   tools_GL2PSlist* ptrlist;                       200   tools_GL2PSlist* ptrlist;
247   int gsno, fontno, imno, shno, maskshno, trgr    201   int gsno, fontno, imno, shno, maskshno, trgroupno;
248   int gsobjno, fontobjno, imobjno, shobjno, ma    202   int gsobjno, fontobjno, imobjno, shobjno, maskshobjno, trgroupobjno;
249 } tools_GL2PSpdfgroup;                            203 } tools_GL2PSpdfgroup;
250                                                   204 
251 typedef struct tools_GL2PScontextRec {         << 205 typedef struct {
252   /* General */                                   206   /* General */
253   tools_GLint format, sort, options, colorsize    207   tools_GLint format, sort, options, colorsize, colormode, buffersize;
254   tools_GLint lastlinecap, lastlinejoin;          208   tools_GLint lastlinecap, lastlinejoin;
255   char *title, *producer, *filename;              209   char *title, *producer, *filename;
256   tools_GLboolean boundary, blending;             210   tools_GLboolean boundary, blending;
257   tools_GLfloat *feedback, lastlinewidth;         211   tools_GLfloat *feedback, lastlinewidth;
258   tools_GLint viewport[4], blendfunc[2], lastf    212   tools_GLint viewport[4], blendfunc[2], lastfactor;
259   tools_GL2PSrgba *colormap, lastrgba, thresho    213   tools_GL2PSrgba *colormap, lastrgba, threshold, bgcolor;
260   tools_GLushort lastpattern;                     214   tools_GLushort lastpattern;
261   tools_GL2PSvertex lastvertex;                   215   tools_GL2PSvertex lastvertex;
262   tools_GL2PSlist *primitives, *auxprimitives;    216   tools_GL2PSlist *primitives, *auxprimitives;
263   FILE *stream;                                   217   FILE *stream;
264   tools_GL2PScompress *compress;                  218   tools_GL2PScompress *compress;
265   tools_GLboolean header;                         219   tools_GLboolean header;
266   tools_GL2PSvertex rasterpos;                    220   tools_GL2PSvertex rasterpos;
267   tools_GLboolean forcerasterpos;                 221   tools_GLboolean forcerasterpos;
268                                                   222 
269   /* BSP-specific */                              223   /* BSP-specific */
270   tools_GLint maxbestroot;                        224   tools_GLint maxbestroot;
271                                                   225 
272   /* Occlusion culling-specific */                226   /* Occlusion culling-specific */
273   tools_GLboolean zerosurfacearea;                227   tools_GLboolean zerosurfacearea;
274   tools_GL2PSbsptree2d *imagetree;                228   tools_GL2PSbsptree2d *imagetree;
275   tools_GL2PSprimitive *primitivetoadd;           229   tools_GL2PSprimitive *primitivetoadd;
276                                                   230 
277   /* PDF-specific */                              231   /* PDF-specific */
278   int streamlength;                               232   int streamlength;
279   tools_GL2PSlist *pdfprimlist, *pdfgrouplist;    233   tools_GL2PSlist *pdfprimlist, *pdfgrouplist;
280   int *xreflist;                                  234   int *xreflist;
281   int objects_stack; /* available objects */      235   int objects_stack; /* available objects */
282   int extgs_stack; /* graphics state object nu    236   int extgs_stack; /* graphics state object number */
283   int font_stack; /* font object number */        237   int font_stack; /* font object number */
284   int im_stack; /* image object number */         238   int im_stack; /* image object number */
285   int trgroupobjects_stack; /* xobject numbers    239   int trgroupobjects_stack; /* xobject numbers */
286   int shader_stack; /* shader object numbers *    240   int shader_stack; /* shader object numbers */
287   int mshader_stack; /* mask shader object num    241   int mshader_stack; /* mask shader object numbers */
288                                                   242 
289   /* for image map list */                        243   /* for image map list */
290   tools_GL2PSimagemap *imagemap_head;             244   tools_GL2PSimagemap *imagemap_head;
291   tools_GL2PSimagemap *imagemap_tail;             245   tools_GL2PSimagemap *imagemap_tail;
292                                                << 
293   /* for TEX scaling */                        << 
294   tools_GLfloat tex_scaling;                   << 
295                                                << 
296   /*G.Barrand : OpenGL functions:*/            << 
297   tools_gl2ps_gl_funcs_t m_gl_funcs;           << 
298 } tools_GL2PScontext;                             246 } tools_GL2PScontext;
299                                                   247 
300 typedef struct {                                  248 typedef struct {
301   void  (*printHeader)(tools_GL2PScontext*);   << 249   void  (*printHeader)(void);
302   void  (*printFooter)(tools_GL2PScontext*);   << 250   void  (*printFooter)(void);
303   void  (*beginViewport)(tools_GL2PScontext*,t << 251   void  (*beginViewport)(tools_GLint viewport[4]);
304   tools_GLint (*endViewport)(tools_GL2PScontex << 252   tools_GLint (*endViewport)(void);
305   void  (*printPrimitive)(tools_GL2PScontext*, << 253   void  (*printPrimitive)(void *data);
306   void  (*printFinalPrimitive)(tools_GL2PScont << 254   void  (*printFinalPrimitive)(void);
307   const char *file_extension;                     255   const char *file_extension;
308   const char *description;                        256   const char *description;
309 } tools_GL2PSbackend;                             257 } tools_GL2PSbackend;
310                                                   258 
311 /* The gl2ps context. gl2ps is not thread safe    259 /* The gl2ps context. gl2ps is not thread safe (we should create a
312    local GL2PScontext during tools_gl2psBeginP << 260    local tools_GL2PScontext during tools_gl2psBeginPage) */
313                                                   261 
314 //static GL2PScontext *gl2ps = NULL;           << 262 static tools_GL2PScontext *tools_gl2ps_context = NULL;
315                                                   263 
316 /* Need to forward-declare this one */            264 /* Need to forward-declare this one */
317                                                   265 
318 inline tools_GLint tools_gl2psPrintPrimitives( << 266 static tools_GLint tools_gl2psPrintPrimitives(void);
319                                                   267 
320 /*********************************************    268 /*********************************************************************
321  *                                                269  *
322  * Utility routines                               270  * Utility routines
323  *                                                271  *
324  *********************************************    272  *********************************************************************/
325                                                   273 
326 inline void tools_gl2psMsg(tools_GLint level,  << 274 static void tools_gl2psMsg(tools_GLint level, const char *fmt, ...)
327 {                                                 275 {
328   va_list args;                                   276   va_list args;
329                                                   277 
330 /*if(!(gl2ps->options & TOOLS_GL2PS_SILENT))*/ << 278   if(!(tools_gl2ps_context->options & TOOLS_GL2PS_SILENT)){
331     switch(level){                                279     switch(level){
332     case TOOLS_GL2PS_INFO : fprintf(stderr, "G    280     case TOOLS_GL2PS_INFO : fprintf(stderr, "GL2PS info: "); break;
333     case TOOLS_GL2PS_WARNING : fprintf(stderr,    281     case TOOLS_GL2PS_WARNING : fprintf(stderr, "GL2PS warning: "); break;
334     case TOOLS_GL2PS_ERROR : fprintf(stderr, "    282     case TOOLS_GL2PS_ERROR : fprintf(stderr, "GL2PS error: "); break;
335     }                                             283     }
336     va_start(args, fmt);                          284     va_start(args, fmt);
337     vfprintf(stderr, fmt, args);                  285     vfprintf(stderr, fmt, args);
338     va_end(args);                                 286     va_end(args);
339     fprintf(stderr, "\n");                        287     fprintf(stderr, "\n");
340   }                                               288   }
341   /* if(level == TOOLS_GL2PS_ERROR) exit(1); *    289   /* if(level == TOOLS_GL2PS_ERROR) exit(1); */
342 }                                                 290 }
343                                                   291 
344 inline void *tools_gl2psMalloc(size_t size)    << 292 static void *tools_gl2psMalloc(size_t size)
345 {                                                 293 {
346   void *ptr;                                      294   void *ptr;
347                                                   295 
348   if(!size) return NULL;                          296   if(!size) return NULL;
349   ptr = malloc(size);                             297   ptr = malloc(size);
350   if(!ptr){                                       298   if(!ptr){
351     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Couldn'    299     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Couldn't allocate requested memory");
352     return NULL;                                  300     return NULL;
353   }                                               301   }
354   return ptr;                                     302   return ptr;
355 }                                                 303 }
356                                                   304 
357 inline void *tools_gl2psRealloc(void *ptr, siz << 305 static void *tools_gl2psRealloc(void *ptr, size_t size)
358 {                                                 306 {
359   void *orig = ptr;                               307   void *orig = ptr;
360   if(!size) return NULL;                          308   if(!size) return NULL;
361   ptr = realloc(orig, size);                      309   ptr = realloc(orig, size);
362   if(!ptr){                                       310   if(!ptr){
363     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Couldn'    311     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Couldn't reallocate requested memory");
364     free(orig);                                   312     free(orig);
365     return NULL;                                  313     return NULL;
366   }                                               314   }
367   return ptr;                                     315   return ptr;
368 }                                                 316 }
369                                                   317 
370 inline void tools_gl2psFree(void *ptr)         << 318 static void tools_gl2psFree(void *ptr)
371 {                                                 319 {
372   if(!ptr) return;                                320   if(!ptr) return;
373   free(ptr);                                      321   free(ptr);
374 }                                                 322 }
375                                                   323 
376 /*G.Barrand: begin*/                           << 324 static int tools_gl2psWriteBigEndian(unsigned long data, int bytes)
377 inline tools_GLboolean tools_dummy_glIsEnabled << 
378 inline void      tools_dummy_glBegin           << 
379   /*printf("debug : tools_dummy_glBegin.\n");* << 
380 }                                              << 
381 inline void      tools_dummy_glEnd             << 
382 inline void      tools_dummy_glGetFloatv       << 
383 inline void      tools_dummy_glVertex3f        << 
384 inline void      tools_dummy_glGetBooleanv     << 
385 inline void      tools_dummy_glGetIntegerv     << 
386 inline tools_GLint     tools_dummy_glRenderMod << 
387   /*printf("debug : tools_dummy_glRenderMode.\ << 
388   return 0;                                    << 
389 }                                              << 
390 inline void      tools_dummy_glFeedbackBuffer  << 
391 inline void      tools_dummy_glPassThrough     << 
392                                                << 
393 inline tools_GL2PScontext* tools_gl2psCreateCo << 
394   tools_GL2PScontext* gl2ps = (tools_GL2PScont << 
395   if(!gl2ps) return 0;                         << 
396                                                << 
397   gl2ps->format = 0;                           << 
398   gl2ps->sort = 0;                             << 
399   gl2ps->options = 0;                          << 
400   gl2ps->colorsize = 0;                        << 
401   gl2ps->colormode = 0;                        << 
402   gl2ps->buffersize = 0;                       << 
403   gl2ps->lastlinecap = 0;                      << 
404   gl2ps->lastlinejoin = 0;                     << 
405   gl2ps->title = NULL;                         << 
406   gl2ps->producer = NULL;                      << 
407   gl2ps->filename = NULL;                      << 
408   gl2ps->boundary = TOOLS_GL_FALSE;            << 
409   gl2ps->blending = TOOLS_GL_FALSE;            << 
410   gl2ps->feedback = NULL;                      << 
411   gl2ps->lastlinewidth = 0;                    << 
412   gl2ps->viewport[0] = 0;                      << 
413   gl2ps->viewport[1] = 0;                      << 
414   gl2ps->viewport[2] = 0;                      << 
415   gl2ps->viewport[3] = 0;                      << 
416                                                << 
417   gl2ps->blendfunc[0] = 0;                     << 
418   gl2ps->blendfunc[1] = 0;                     << 
419                                                << 
420   gl2ps->lastfactor = 0;                       << 
421   gl2ps->colormap = NULL;                      << 
422   gl2ps->lastrgba[0] = 0;                      << 
423   gl2ps->lastrgba[1] = 0;                      << 
424   gl2ps->lastrgba[2] = 0;                      << 
425   gl2ps->lastrgba[3] = 0;                      << 
426   gl2ps->threshold[0] = 0;                     << 
427   gl2ps->threshold[1] = 0;                     << 
428   gl2ps->threshold[2] = 0;                     << 
429   gl2ps->threshold[3] = 0;                     << 
430   gl2ps->bgcolor[0] = 0;                       << 
431   gl2ps->bgcolor[1] = 0;                       << 
432   gl2ps->bgcolor[2] = 0;                       << 
433   gl2ps->bgcolor[3] = 0;                       << 
434                                                << 
435   gl2ps->lastpattern = 0;                      << 
436   gl2ps->lastvertex.xyz[0] = 0;                << 
437   gl2ps->lastvertex.xyz[1] = 0;                << 
438   gl2ps->lastvertex.xyz[2] = 0;                << 
439   gl2ps->lastvertex.rgba[0] = 0;               << 
440   gl2ps->lastvertex.rgba[1] = 0;               << 
441   gl2ps->lastvertex.rgba[2] = 0;               << 
442   gl2ps->lastvertex.rgba[3] = 0;               << 
443                                                << 
444   gl2ps->primitives = NULL;                    << 
445   gl2ps->auxprimitives = NULL;                 << 
446   gl2ps->stream = NULL;                        << 
447   gl2ps->compress = NULL;                      << 
448   gl2ps->header = TOOLS_GL_FALSE;              << 
449   gl2ps->rasterpos.xyz[0] = 0;                 << 
450   gl2ps->rasterpos.xyz[1] = 0;                 << 
451   gl2ps->rasterpos.xyz[2] = 0;                 << 
452   gl2ps->rasterpos.rgba[0] = 0;                << 
453   gl2ps->rasterpos.rgba[1] = 0;                << 
454   gl2ps->rasterpos.rgba[2] = 0;                << 
455   gl2ps->rasterpos.rgba[3] = 0;                << 
456                                                << 
457   gl2ps->forcerasterpos = TOOLS_GL_FALSE;      << 
458   gl2ps->maxbestroot = 0;                      << 
459   gl2ps->zerosurfacearea = TOOLS_GL_FALSE;     << 
460   gl2ps->imagetree = NULL;                     << 
461   gl2ps->primitivetoadd = NULL;                << 
462                                                << 
463   gl2ps->streamlength = 0;                     << 
464   gl2ps->pdfprimlist = NULL;                   << 
465   gl2ps->pdfgrouplist = NULL;                  << 
466   gl2ps->xreflist = NULL;                      << 
467                                                << 
468   gl2ps->objects_stack = 0;                    << 
469   gl2ps->extgs_stack = 0;                      << 
470   gl2ps->font_stack = 0;                       << 
471   gl2ps->im_stack = 0;                         << 
472   gl2ps->trgroupobjects_stack = 0;             << 
473   gl2ps->shader_stack = 0;                     << 
474   gl2ps->mshader_stack = 0;                    << 
475   gl2ps->imagemap_head = NULL;                 << 
476   gl2ps->imagemap_tail = NULL;                 << 
477                                                << 
478   gl2ps->m_gl_funcs.m_glIsEnabled = tools_dumm << 
479   gl2ps->m_gl_funcs.m_glBegin = tools_dummy_gl << 
480   gl2ps->m_gl_funcs.m_glEnd = tools_dummy_glEn << 
481   gl2ps->m_gl_funcs.m_glGetFloatv = tools_dumm << 
482   gl2ps->m_gl_funcs.m_glVertex3f = tools_dummy << 
483   gl2ps->m_gl_funcs.m_glGetBooleanv = tools_du << 
484   gl2ps->m_gl_funcs.m_glGetIntegerv = tools_du << 
485   gl2ps->m_gl_funcs.m_glRenderMode = tools_dum << 
486   gl2ps->m_gl_funcs.m_glFeedbackBuffer = tools << 
487   gl2ps->m_gl_funcs.m_glPassThrough = tools_du << 
488                                                << 
489   return gl2ps;                                << 
490 }                                              << 
491 inline void tools_gl2psDeleteContext(tools_GL2 << 
492   tools_gl2psFree(a_context);                  << 
493 }                                              << 
494                                                << 
495 inline void tools_gl2ps_set_gl_funcs(tools_GL2 << 
496   gl2ps->m_gl_funcs.m_glIsEnabled = a_funcs->m << 
497   gl2ps->m_gl_funcs.m_glBegin = a_funcs->m_glB << 
498   gl2ps->m_gl_funcs.m_glEnd = a_funcs->m_glEnd << 
499   gl2ps->m_gl_funcs.m_glGetFloatv = a_funcs->m << 
500   gl2ps->m_gl_funcs.m_glVertex3f = a_funcs->m_ << 
501   gl2ps->m_gl_funcs.m_glGetBooleanv = a_funcs- << 
502   gl2ps->m_gl_funcs.m_glGetIntegerv = a_funcs- << 
503   gl2ps->m_gl_funcs.m_glRenderMode = a_funcs-> << 
504   gl2ps->m_gl_funcs.m_glFeedbackBuffer = a_fun << 
505   gl2ps->m_gl_funcs.m_glPassThrough = a_funcs- << 
506 }                                              << 
507                                                << 
508 inline void tools_gl2ps_reset_gl_funcs(tools_G << 
509   gl2ps->m_gl_funcs.m_glIsEnabled = tools_dumm << 
510   gl2ps->m_gl_funcs.m_glBegin = tools_dummy_gl << 
511   gl2ps->m_gl_funcs.m_glEnd = tools_dummy_glEn << 
512   gl2ps->m_gl_funcs.m_glGetFloatv = tools_dumm << 
513   gl2ps->m_gl_funcs.m_glVertex3f = tools_dummy << 
514   gl2ps->m_gl_funcs.m_glGetBooleanv = tools_du << 
515   gl2ps->m_gl_funcs.m_glGetIntegerv = tools_du << 
516   gl2ps->m_gl_funcs.m_glRenderMode = tools_dum << 
517   gl2ps->m_gl_funcs.m_glFeedbackBuffer = tools << 
518   gl2ps->m_gl_funcs.m_glPassThrough = tools_du << 
519 }                                              << 
520                                                << 
521 #define tools_glIsEnabled      gl2ps->m_gl_fun << 
522 #define tools_glBegin          gl2ps->m_gl_fun << 
523 #define tools_glEnd            gl2ps->m_gl_fun << 
524 #define tools_glGetFloatv      gl2ps->m_gl_fun << 
525 #define tools_glVertex3f       gl2ps->m_gl_fun << 
526 #define tools_glGetBooleanv    gl2ps->m_gl_fun << 
527 #define tools_glGetIntegerv    gl2ps->m_gl_fun << 
528 #define tools_glRenderMode     gl2ps->m_gl_fun << 
529 #define tools_glFeedbackBuffer gl2ps->m_gl_fun << 
530 #define tools_glPassThrough    gl2ps->m_gl_fun << 
531                                                << 
532 /*G.Barrand: end*/                             << 
533                                                << 
534 inline int tools_gl2psWriteBigEndian(tools_GL2 << 
535 {                                                 325 {
536   int i;                                          326   int i;
537   int size = sizeof(unsigned long);               327   int size = sizeof(unsigned long);
538   for(i = 1; i <= bytes; ++i){                    328   for(i = 1; i <= bytes; ++i){
539     fputc(0xff & (data >> (size - i) * 8), gl2 << 329     fputc(0xff & (data >> (size - i) * 8), tools_gl2ps_context->stream);
540   }                                               330   }
541   return bytes;                                   331   return bytes;
542 }                                                 332 }
543                                                   333 
544 /* zlib compression helper routines */            334 /* zlib compression helper routines */
545                                                   335 
546 #if defined(TOOLS_GL2PS_HAVE_ZLIB)                336 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
547                                                   337 
548 inline void tools_gl2psSetupCompress(tools_GL2 << 338 static void tools_gl2psSetupCompress(void)
549 {                                                 339 {
550   gl2ps->compress = (tools_GL2PScompress*)tool << 340   tools_gl2ps_context->compress = (tools_GL2PScompress*)tools_gl2psMalloc(sizeof(tools_GL2PScompress));
551   gl2ps->compress->src = NULL;                 << 341   tools_gl2ps_context->compress->src = NULL;
552   gl2ps->compress->start = NULL;               << 342   tools_gl2ps_context->compress->start = NULL;
553   gl2ps->compress->dest = NULL;                << 343   tools_gl2ps_context->compress->dest = NULL;
554   gl2ps->compress->srcLen = 0;                 << 344   tools_gl2ps_context->compress->srcLen = 0;
555   gl2ps->compress->destLen = 0;                << 345   tools_gl2ps_context->compress->destLen = 0;
556 }                                                 346 }
557                                                   347 
558 inline void tools_gl2psFreeCompress(tools_GL2P << 348 static void tools_gl2psFreeCompress(void)
559 {                                                 349 {
560   if(!gl2ps->compress)                         << 350   if(!tools_gl2ps_context->compress)
561     return;                                       351     return;
562   tools_gl2psFree(gl2ps->compress->start);     << 352   tools_gl2psFree(tools_gl2ps_context->compress->start);
563   tools_gl2psFree(gl2ps->compress->dest);      << 353   tools_gl2psFree(tools_gl2ps_context->compress->dest);
564   gl2ps->compress->src = NULL;                 << 354   tools_gl2ps_context->compress->src = NULL;
565   gl2ps->compress->start = NULL;               << 355   tools_gl2ps_context->compress->start = NULL;
566   gl2ps->compress->dest = NULL;                << 356   tools_gl2ps_context->compress->dest = NULL;
567   gl2ps->compress->srcLen = 0;                 << 357   tools_gl2ps_context->compress->srcLen = 0;
568   gl2ps->compress->destLen = 0;                << 358   tools_gl2ps_context->compress->destLen = 0;
569 }                                                 359 }
570                                                   360 
571 inline int tools_gl2psAllocCompress(tools_GL2P << 361 static int tools_gl2psAllocCompress(unsigned int srcsize)
572 {                                                 362 {
573   tools_gl2psFreeCompress(gl2ps);              << 363   tools_gl2psFreeCompress();
574                                                   364 
575   if(!gl2ps->compress || !srcsize)             << 365   if(!tools_gl2ps_context->compress || !srcsize)
576     return TOOLS_GL2PS_ERROR;                     366     return TOOLS_GL2PS_ERROR;
577                                                   367 
578   gl2ps->compress->srcLen = srcsize;           << 368   tools_gl2ps_context->compress->srcLen = srcsize;
579   gl2ps->compress->destLen = (int)ceil(1.001 * << 369   tools_gl2ps_context->compress->destLen = (int)ceil(1.001 * tools_gl2ps_context->compress->srcLen + 12);
580   gl2ps->compress->src = (Bytef*)tools_gl2psMa << 370   tools_gl2ps_context->compress->src = (Bytef*)tools_gl2psMalloc(tools_gl2ps_context->compress->srcLen);
581   gl2ps->compress->start = gl2ps->compress->sr << 371   tools_gl2ps_context->compress->start = tools_gl2ps_context->compress->src;
582   gl2ps->compress->dest = (Bytef*)tools_gl2psM << 372   tools_gl2ps_context->compress->dest = (Bytef*)tools_gl2psMalloc(tools_gl2ps_context->compress->destLen);
583                                                   373 
584   return TOOLS_GL2PS_SUCCESS;                     374   return TOOLS_GL2PS_SUCCESS;
585 }                                                 375 }
586                                                   376 
587 inline void *tools_gl2psReallocCompress(tools_ << 377 static void *tools_gl2psReallocCompress(unsigned int srcsize)
588 {                                                 378 {
589   if(!gl2ps->compress || !srcsize)             << 379   if(!tools_gl2ps_context->compress || !srcsize)
590     return NULL;                                  380     return NULL;
591                                                   381 
592   if(srcsize < gl2ps->compress->srcLen)        << 382   if(srcsize < tools_gl2ps_context->compress->srcLen)
593     return gl2ps->compress->start;             << 383     return tools_gl2ps_context->compress->start;
594                                                   384 
595   gl2ps->compress->srcLen = srcsize;           << 385   tools_gl2ps_context->compress->srcLen = srcsize;
596   gl2ps->compress->destLen = (int)ceil(1.001 * << 386   tools_gl2ps_context->compress->destLen = (int)ceil(1.001 * tools_gl2ps_context->compress->srcLen + 12);
597   gl2ps->compress->src = (Bytef*)tools_gl2psRe << 387   tools_gl2ps_context->compress->src = (Bytef*)tools_gl2psRealloc(tools_gl2ps_context->compress->src,
598                                                << 388                                               tools_gl2ps_context->compress->srcLen);
599   gl2ps->compress->start = gl2ps->compress->sr << 389   tools_gl2ps_context->compress->start = tools_gl2ps_context->compress->src;
600   gl2ps->compress->dest = (Bytef*)tools_gl2psR << 390   tools_gl2ps_context->compress->dest = (Bytef*)tools_gl2psRealloc(tools_gl2ps_context->compress->dest,
601                                                << 391                                                tools_gl2ps_context->compress->destLen);
602                                                   392 
603   return gl2ps->compress->start;               << 393   return tools_gl2ps_context->compress->start;
604 }                                                 394 }
605                                                   395 
606 inline int tools_gl2psWriteBigEndianCompress(t << 396 static int tools_gl2psWriteBigEndianCompress(unsigned long data, int bytes)
607 {                                                 397 {
608   int i;                                          398   int i;
609   int size = sizeof(unsigned long);               399   int size = sizeof(unsigned long);
610   for(i = 1; i <= bytes; ++i){                    400   for(i = 1; i <= bytes; ++i){
611     *gl2ps->compress->src = (Bytef)(0xff & (da << 401     *tools_gl2ps_context->compress->src = (Bytef)(0xff & (data >> (size-i) * 8));
612     ++gl2ps->compress->src;                    << 402     ++tools_gl2ps_context->compress->src;
613   }                                               403   }
614   return bytes;                                   404   return bytes;
615 }                                                 405 }
616                                                   406 
617 inline int tools_gl2psDeflate(tools_GL2PSconte << 407 static int tools_gl2psDeflate(void)
618 {                                                 408 {
619   /* For compatibility with older zlib version    409   /* For compatibility with older zlib versions, we use compress(...)
620      instead of compress2(..., Z_BEST_COMPRESS    410      instead of compress2(..., Z_BEST_COMPRESSION) */
621   return compress(gl2ps->compress->dest, &gl2p << 411   return compress(tools_gl2ps_context->compress->dest, &tools_gl2ps_context->compress->destLen,
622                   gl2ps->compress->start, gl2p << 412                   tools_gl2ps_context->compress->start, tools_gl2ps_context->compress->srcLen);
623 }                                                 413 }
624                                                   414 
625 #endif                                            415 #endif
626                                                   416 
627 inline int tools_gl2psPrintf(tools_GL2PScontex << 417 static int tools_gl2psPrintf(const char* fmt, ...)
628 {                                                 418 {
629   int ret;                                        419   int ret;
630   va_list args;                                   420   va_list args;
631                                                   421 
632 #if defined(TOOLS_GL2PS_HAVE_ZLIB)                422 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
633 //static char buf[1024]; //G.Barrand: avoid th << 423   static char buf[1024];
634 //char *bufptr = buf;                          << 424   char *bufptr = buf;
635 //tools_GLboolean freebuf = TOOLS_GL_FALSE;    << 425   tools_GLboolean freebuf = TOOLS_GL_FALSE;
636   char* bufptr = (char *)tools_gl2psMalloc(102 << 
637   tools_GLboolean freebuf = TOOLS_GL_TRUE;     << 
638   unsigned int oldsize = 0;                       426   unsigned int oldsize = 0;
639 #if !defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)       427 #if !defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)
640   /* Try writing the string to a 1024 byte buf    428   /* Try writing the string to a 1024 byte buffer. If it is too small to fit,
641      keep trying larger sizes until it does. *    429      keep trying larger sizes until it does. */
642 //int bufsize = sizeof(buf);                   << 430   int bufsize = sizeof(buf);
643   int bufsize = 1024;                          << 
644 #endif                                            431 #endif
645   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){   << 432   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
646     va_start(args, fmt);                          433     va_start(args, fmt);
647 #if defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)        434 #if defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)
648     ret = vsprintf(buf, fmt, args);               435     ret = vsprintf(buf, fmt, args);
649 #else                                             436 #else
650     ret = vsnprintf(bufptr, bufsize, fmt, args    437     ret = vsnprintf(bufptr, bufsize, fmt, args);
651 #endif                                            438 #endif
652     va_end(args);                                 439     va_end(args);
653 #if !defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)       440 #if !defined(TOOLS_GL2PS_HAVE_NO_VSNPRINTF)
654     while(ret >= (bufsize - 1) || ret < 0){       441     while(ret >= (bufsize - 1) || ret < 0){
655       /* Too big. Allocate a new buffer. */       442       /* Too big. Allocate a new buffer. */
656       bufsize *= 2;                               443       bufsize *= 2;
657       if(freebuf == TOOLS_GL_TRUE) tools_gl2ps    444       if(freebuf == TOOLS_GL_TRUE) tools_gl2psFree(bufptr);
658       bufptr = (char *)tools_gl2psMalloc(bufsi    445       bufptr = (char *)tools_gl2psMalloc(bufsize);
659       freebuf = TOOLS_GL_TRUE;                    446       freebuf = TOOLS_GL_TRUE;
660       va_start(args, fmt);                        447       va_start(args, fmt);
661       ret = vsnprintf(bufptr, bufsize, fmt, ar    448       ret = vsnprintf(bufptr, bufsize, fmt, args);
662       va_end(args);                               449       va_end(args);
663     }                                             450     }
664 #endif                                            451 #endif
665     oldsize = gl2ps->compress->srcLen;         << 452     oldsize = tools_gl2ps_context->compress->srcLen;
666     gl2ps->compress->start = (Bytef*)tools_gl2 << 453     tools_gl2ps_context->compress->start = (Bytef*)tools_gl2psReallocCompress(oldsize + ret);
667     memcpy(gl2ps->compress->start + oldsize, b << 454     memcpy(tools_gl2ps_context->compress->start + oldsize, bufptr, ret);
668     if(freebuf == TOOLS_GL_TRUE) tools_gl2psFr    455     if(freebuf == TOOLS_GL_TRUE) tools_gl2psFree(bufptr);
669     ret = 0;                                      456     ret = 0;
670   }                                               457   }
671   else{                                           458   else{
672 #endif                                            459 #endif
673     va_start(args, fmt);                          460     va_start(args, fmt);
674     ret = vfprintf(gl2ps->stream, fmt, args);  << 461     ret = vfprintf(tools_gl2ps_context->stream, fmt, args);
675     va_end(args);                                 462     va_end(args);
676 #if defined(TOOLS_GL2PS_HAVE_ZLIB)                463 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
677   }                                               464   }
678 #endif                                            465 #endif
679   return ret;                                     466   return ret;
680 }                                                 467 }
681                                                   468 
682 inline void tools_gl2psPrintGzipHeader(tools_G << 469 static void tools_gl2psPrintGzipHeader(void)
683 {                                                 470 {
684 #if defined(TOOLS_GL2PS_HAVE_ZLIB)                471 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
685   char tmp[10] = {'\x1f', '\x8b', /* magic num    472   char tmp[10] = {'\x1f', '\x8b', /* magic numbers: 0x1f, 0x8b */
686                   8, /* compression method: Z_    473                   8, /* compression method: Z_DEFLATED */
687                   0, /* flags */                  474                   0, /* flags */
688                   0, 0, 0, 0, /* time */          475                   0, 0, 0, 0, /* time */
689                   2, /* extra flags: max compr    476                   2, /* extra flags: max compression */
690                   '\x03'}; /* OS code: 0x03 (U    477                   '\x03'}; /* OS code: 0x03 (Unix) */
691                                                   478 
692   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){   << 479   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
693     tools_gl2psSetupCompress(gl2ps);           << 480     tools_gl2psSetupCompress();
694     /* add the gzip file header */                481     /* add the gzip file header */
695     fwrite(tmp, 10, 1, gl2ps->stream);         << 482     fwrite(tmp, 10, 1, tools_gl2ps_context->stream);
696   }                                               483   }
697 #endif                                            484 #endif
698   (void)gl2ps;                                 << 
699 }                                                 485 }
700                                                   486 
701 inline void tools_gl2psPrintGzipFooter(tools_G << 487 static void tools_gl2psPrintGzipFooter(void)
702 {                                                 488 {
703 #if defined(TOOLS_GL2PS_HAVE_ZLIB)                489 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
704   int n;                                          490   int n;
705   uLong crc, len;                                 491   uLong crc, len;
706   char tmp[8];                                    492   char tmp[8];
707                                                   493 
708   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){   << 494   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
709     if(Z_OK != tools_gl2psDeflate(gl2ps)){     << 495     if(Z_OK != tools_gl2psDeflate()){
710       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Zlib     496       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Zlib deflate error");
711     }                                             497     }
712     else{                                         498     else{
713       /* determine the length of the header in    499       /* determine the length of the header in the zlib stream */
714       n = 2; /* CMF+FLG */                        500       n = 2; /* CMF+FLG */
715       if(gl2ps->compress->dest[1] & (1<<5)){   << 501       if(tools_gl2ps_context->compress->dest[1] & (1<<5)){
716         n += 4; /* DICTID */                      502         n += 4; /* DICTID */
717       }                                           503       }
718       /* write the data, without the zlib head    504       /* write the data, without the zlib header and footer */
719       fwrite(gl2ps->compress->dest+n, gl2ps->c << 505       fwrite(tools_gl2ps_context->compress->dest+n, tools_gl2ps_context->compress->destLen-(n+4),
720              1, gl2ps->stream);                << 506              1, tools_gl2ps_context->stream);
721       /* add the gzip file footer */              507       /* add the gzip file footer */
722       crc = crc32(0L, gl2ps->compress->start,  << 508       crc = crc32(0L, tools_gl2ps_context->compress->start, tools_gl2ps_context->compress->srcLen);
723       for(n = 0; n < 4; ++n){                     509       for(n = 0; n < 4; ++n){
724         tmp[n] = (char)(crc & 0xff);              510         tmp[n] = (char)(crc & 0xff);
725         crc >>= 8;                                511         crc >>= 8;
726       }                                           512       }
727       len = gl2ps->compress->srcLen;           << 513       len = tools_gl2ps_context->compress->srcLen;
728       for(n = 4; n < 8; ++n){                     514       for(n = 4; n < 8; ++n){
729         tmp[n] = (char)(len & 0xff);              515         tmp[n] = (char)(len & 0xff);
730         len >>= 8;                                516         len >>= 8;
731       }                                           517       }
732       fwrite(tmp, 8, 1, gl2ps->stream);        << 518       fwrite(tmp, 8, 1, tools_gl2ps_context->stream);
733     }                                             519     }
734     tools_gl2psFreeCompress(gl2ps);            << 520     tools_gl2psFreeCompress();
735     tools_gl2psFree(gl2ps->compress);          << 521     tools_gl2psFree(tools_gl2ps_context->compress);
736     gl2ps->compress = NULL;                    << 522     tools_gl2ps_context->compress = NULL;
737   }                                               523   }
738 #endif                                            524 #endif
739   (void)gl2ps;                                 << 
740 }                                                 525 }
741                                                   526 
742 /* The list handling routines */                  527 /* The list handling routines */
743                                                   528 
744 inline void tools_gl2psListRealloc(tools_GL2PS << 529 static void tools_gl2psListRealloc(tools_GL2PSlist *list, tools_GLint n)
745 {                                                 530 {
746   if(!list){                                      531   if(!list){
747     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Cannot     532     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Cannot reallocate NULL list");
748     return;                                       533     return;
749   }                                               534   }
750   if(n <= 0) return;                              535   if(n <= 0) return;
751   if(!list->array){                               536   if(!list->array){
752     list->nmax = n;                               537     list->nmax = n;
753     list->array = (char*)tools_gl2psMalloc(lis    538     list->array = (char*)tools_gl2psMalloc(list->nmax * list->size);
754   }                                               539   }
755   else{                                           540   else{
756     if(n > list->nmax){                           541     if(n > list->nmax){
757       list->nmax = ((n - 1) / list->incr + 1)     542       list->nmax = ((n - 1) / list->incr + 1) * list->incr;
758       list->array = (char*)tools_gl2psRealloc(    543       list->array = (char*)tools_gl2psRealloc(list->array,
759                                         list->    544                                         list->nmax * list->size);
760     }                                             545     }
761   }                                               546   }
762 }                                                 547 }
763                                                   548 
764 inline tools_GL2PSlist *tools_gl2psListCreate( << 549 static tools_GL2PSlist *tools_gl2psListCreate(tools_GLint n, tools_GLint incr, tools_GLint size)
765 {                                                 550 {
766   tools_GL2PSlist *list;                          551   tools_GL2PSlist *list;
767                                                   552 
768   if(n < 0) n = 0;                                553   if(n < 0) n = 0;
769   if(incr <= 0) incr = 1;                         554   if(incr <= 0) incr = 1;
770   list = (tools_GL2PSlist*)tools_gl2psMalloc(s    555   list = (tools_GL2PSlist*)tools_gl2psMalloc(sizeof(tools_GL2PSlist));
771   list->nmax = 0;                                 556   list->nmax = 0;
772   list->incr = incr;                              557   list->incr = incr;
773   list->size = size;                              558   list->size = size;
774   list->n = 0;                                    559   list->n = 0;
775   list->array = NULL;                             560   list->array = NULL;
776   tools_gl2psListRealloc(list, n);                561   tools_gl2psListRealloc(list, n);
777   return list;                                    562   return list;
778 }                                                 563 }
779                                                   564 
780 inline void tools_gl2psListReset(tools_GL2PSli << 565 static void tools_gl2psListReset(tools_GL2PSlist *list)
781 {                                                 566 {
782   if(!list) return;                               567   if(!list) return;
783   list->n = 0;                                    568   list->n = 0;
784 }                                                 569 }
785                                                   570 
786 inline void tools_gl2psListDelete(tools_GL2PSl << 571 static void tools_gl2psListDelete(tools_GL2PSlist *list)
787 {                                                 572 {
788   if(!list) return;                               573   if(!list) return;
789   tools_gl2psFree(list->array);                   574   tools_gl2psFree(list->array);
790   tools_gl2psFree(list);                          575   tools_gl2psFree(list);
791 }                                                 576 }
792                                                   577 
793 inline void tools_gl2psListAdd(tools_GL2PSlist << 578 static void tools_gl2psListAdd(tools_GL2PSlist *list, void *data)
794 {                                                 579 {
795   if(!list){                                      580   if(!list){
796     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Cannot     581     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Cannot add into unallocated list");
797     return;                                       582     return;
798   }                                               583   }
799   list->n++;                                      584   list->n++;
800   tools_gl2psListRealloc(list, list->n);          585   tools_gl2psListRealloc(list, list->n);
801   memcpy(&list->array[(list->n - 1) * list->si    586   memcpy(&list->array[(list->n - 1) * list->size], data, list->size);
802 }                                                 587 }
803                                                   588 
804 inline int tools_gl2psListNbr(tools_GL2PSlist  << 589 static int tools_gl2psListNbr(tools_GL2PSlist *list)
805 {                                                 590 {
806   if(!list)                                       591   if(!list)
807     return 0;                                     592     return 0;
808   return list->n;                                 593   return list->n;
809 }                                                 594 }
810                                                   595 
811 inline void *tools_gl2psListPointer(tools_GL2P << 596 static void *tools_gl2psListPointer(tools_GL2PSlist *list, tools_GLint idx)
812 {                                                 597 {
813   if(!list){                                      598   if(!list){
814     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Cannot     599     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Cannot point into unallocated list");
815     return NULL;                                  600     return NULL;
816   }                                               601   }
817   if((idx < 0) || (idx >= list->n)){              602   if((idx < 0) || (idx >= list->n)){
818     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Wrong l    603     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Wrong list index in tools_gl2psListPointer");
819     return NULL;                                  604     return NULL;
820   }                                               605   }
821   return &list->array[idx * list->size];          606   return &list->array[idx * list->size];
822 }                                                 607 }
823                                                   608 
824 //G.Barrand: begin:                            << 609 static void tools_gl2psListSort(tools_GL2PSlist *list,
825 inline bool tools_gl2psPortableSort(void* a_it << 610                           int (*fcmp)(const void *a, const void *b))
826   //  We observed that qsort on macOS/clang, L << 
827   // does not produce the same output list for << 
828   // "Same objects" are put correctly contiguo << 
829   // according the platform. In case of gl2ps, << 
830   // primitives in the output file which are n << 
831   // Then, we let the possibility to use a por << 
832   // give the same sorted list, and then the s << 
833   if(a_nitem<=1) return true;                  << 
834   if(!a_item_size) return true;                << 
835   void* tmp = ::malloc(a_item_size);           << 
836   if(!tmp) return false;                       << 
837   char* p = (char*)a_items;                    << 
838   size_t i,j;                                  << 
839   char* a = p;char* b;                         << 
840   for(i=0;i<a_nitem;i++,a+=a_item_size) {      << 
841     b = p+a_item_size*(i+1);                   << 
842     for(j=i+1;j<a_nitem;j++,b+=a_item_size) {  << 
843       if(a_cmp(b,a)>=0) continue;  //b>=a      << 
844       ::memcpy(tmp,a,a_item_size);             << 
845       ::memcpy(a,b,a_item_size);               << 
846       ::memcpy(b,tmp,a_item_size);             << 
847     }                                          << 
848   }                                            << 
849   ::free(tmp);                                 << 
850   return true;                                 << 
851 }                                              << 
852 //G.Barrand: end.                              << 
853                                                << 
854 inline void tools_gl2psListSort(tools_GL2PScon << 
855                                 tools_GL2PSlis << 
856                                 int (*fcmp)(co << 
857 {                                              << 
858   if(!list) return;                            << 
859   if(gl2ps->options & TOOLS_GL2PS_PORTABLE_SOR << 
860     tools_gl2psPortableSort(list->array, list- << 
861   } else {                                     << 
862     ::qsort(list->array, list->n, list->size,  << 
863   }                                            << 
864 }                                              << 
865                                                << 
866 /* Must be a list of tools_GL2PSprimitives. */ << 
867 inline void tools_gl2psListAssignSortIds(tools << 
868 {                                                 611 {
869   tools_GLint i;                               << 612   if(!list)
870   for(i = 0; i < tools_gl2psListNbr(list); i++ << 613     return;
871     (*(tools_GL2PSprimitive**)tools_gl2psListP << 614   qsort(list->array, list->n, list->size, fcmp);
872   }                                            << 
873 }                                                 615 }
874                                                   616 
875 inline void tools_gl2psListAction(tools_GL2PSl << 617 static void tools_gl2psListAction(tools_GL2PSlist *list, void (*action)(void *data))
876 {                                                 618 {
877   tools_GLint i;                                  619   tools_GLint i;
878                                                   620 
879   for(i = 0; i < tools_gl2psListNbr(list); i++    621   for(i = 0; i < tools_gl2psListNbr(list); i++){
880     (*action)(tools_gl2psListPointer(list, i))    622     (*action)(tools_gl2psListPointer(list, i));
881   }                                               623   }
882 }                                                 624 }
883                                                   625 
884 inline void tools_gl2psListActionInverse(tools << 626 static void tools_tools_gl2psListActionInverse(tools_GL2PSlist *list, void (*action)(void *data))
885 {                                                 627 {
886   tools_GLint i;                                  628   tools_GLint i;
887                                                   629 
888   for(i = tools_gl2psListNbr(list); i > 0; i--    630   for(i = tools_gl2psListNbr(list); i > 0; i--){
889     (*action)(tools_gl2psListPointer(list, i-1    631     (*action)(tools_gl2psListPointer(list, i-1));
890   }                                               632   }
891 }                                                 633 }
892                                                   634 
893 inline void tools_gl2psListActionContext(tools << 
894 {                                              << 
895   tools_GLint i;                               << 
896                                                << 
897   for(i = 0; i < tools_gl2psListNbr(list); i++ << 
898     (*action)(gl2ps, tools_gl2psListPointer(li << 
899   }                                            << 
900 }                                              << 
901                                                << 
902 inline void tools_gl2psListActionInverseContex << 
903 {                                              << 
904   tools_GLint i;                               << 
905                                                << 
906   for(i = tools_gl2psListNbr(list); i > 0; i-- << 
907     (*action)(gl2ps, tools_gl2psListPointer(li << 
908   }                                            << 
909 }                                              << 
910                                                << 
911 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)              635 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)
912                                                   636 
913 inline void tools_gl2psListRead(tools_GL2PSlis << 637 static void tools_gl2psListRead(tools_GL2PSlist *list, int index, void *data)
914 {                                                 638 {
915   if((index < 0) || (index >= list->n))           639   if((index < 0) || (index >= list->n))
916     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Wrong l    640     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Wrong list index in tools_gl2psListRead");
917   memcpy(data, &list->array[index * list->size    641   memcpy(data, &list->array[index * list->size], list->size);
918 }                                                 642 }
919                                                   643 
920 inline void tools_gl2psEncodeBase64Block(unsig << 644 static void tools_gl2psEncodeBase64Block(unsigned char in[3], unsigned char out[4], int len)
921 {                                                 645 {
922   static const char cb64[] =                      646   static const char cb64[] =
923     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno    647     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
924                                                   648 
925   out[0] = cb64[ in[0] >> 2 ];                    649   out[0] = cb64[ in[0] >> 2 ];
926   out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[    650   out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
927   out[2] = (len > 1) ? cb64[ ((in[1] & 0x0f) <    651   out[2] = (len > 1) ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=';
928   out[3] = (len > 2) ? cb64[ in[2] & 0x3f ] :     652   out[3] = (len > 2) ? cb64[ in[2] & 0x3f ] : '=';
929 }                                                 653 }
930                                                   654 
931 inline void tools_gl2psListEncodeBase64(tools_ << 655 static void tools_gl2psListEncodeBase64(tools_GL2PSlist *list)
932 {                                                 656 {
933   unsigned char *buffer, in[3], out[4];           657   unsigned char *buffer, in[3], out[4];
934   int i, n, index, len;                           658   int i, n, index, len;
935                                                   659 
936   n = list->n * list->size;                       660   n = list->n * list->size;
937   buffer = (unsigned char*)tools_gl2psMalloc(n    661   buffer = (unsigned char*)tools_gl2psMalloc(n * sizeof(unsigned char));
938   memcpy(buffer, list->array, n * sizeof(unsig    662   memcpy(buffer, list->array, n * sizeof(unsigned char));
939   tools_gl2psListReset(list);                     663   tools_gl2psListReset(list);
940                                                   664 
941   index = 0;                                      665   index = 0;
942   while(index < n) {                              666   while(index < n) {
943     len = 0;                                      667     len = 0;
944     for(i = 0; i < 3; i++) {                      668     for(i = 0; i < 3; i++) {
945       if(index < n){                              669       if(index < n){
946         in[i] = buffer[index];                    670         in[i] = buffer[index];
947         len++;                                    671         len++;
948       }                                           672       }
949       else{                                       673       else{
950         in[i] = 0;                                674         in[i] = 0;
951       }                                           675       }
952       index++;                                    676       index++;
953     }                                             677     }
954     if(len) {                                     678     if(len) {
955       tools_gl2psEncodeBase64Block(in, out, le    679       tools_gl2psEncodeBase64Block(in, out, len);
956       for(i = 0; i < 4; i++)                      680       for(i = 0; i < 4; i++)
957         tools_gl2psListAdd(list, &out[i]);        681         tools_gl2psListAdd(list, &out[i]);
958     }                                             682     }
959   }                                               683   }
960   tools_gl2psFree(buffer);                        684   tools_gl2psFree(buffer);
961 }                                                 685 }
962                                                   686 
963 #endif                                            687 #endif
964                                                   688 
965 /* Helpers for rgba colors */                     689 /* Helpers for rgba colors */
966                                                   690 
967 inline tools_GLboolean tools_gl2psSameColor(to << 691 static tools_GLboolean tools_gl2psSameColor(tools_GL2PSrgba rgba1, tools_GL2PSrgba rgba2)
968 {                                                 692 {
969   if(!TOOLS_GL2PS_ZERO(rgba1[0] - rgba2[0]) ||    693   if(!TOOLS_GL2PS_ZERO(rgba1[0] - rgba2[0]) ||
970      !TOOLS_GL2PS_ZERO(rgba1[1] - rgba2[1]) ||    694      !TOOLS_GL2PS_ZERO(rgba1[1] - rgba2[1]) ||
971      !TOOLS_GL2PS_ZERO(rgba1[2] - rgba2[2]))      695      !TOOLS_GL2PS_ZERO(rgba1[2] - rgba2[2]))
972     return TOOLS_GL_FALSE;                        696     return TOOLS_GL_FALSE;
973   return TOOLS_GL_TRUE;                           697   return TOOLS_GL_TRUE;
974 }                                                 698 }
975                                                   699 
976 inline tools_GLboolean tools_gl2psVertsSameCol << 700 static tools_GLboolean tools_gl2psVertsSameColor(const tools_GL2PSprimitive *prim)
977 {                                                 701 {
978   int i;                                          702   int i;
979                                                   703 
980   for(i = 1; i < prim->numverts; i++){            704   for(i = 1; i < prim->numverts; i++){
981     if(!tools_gl2psSameColor(prim->verts[0].rg    705     if(!tools_gl2psSameColor(prim->verts[0].rgba, prim->verts[i].rgba)){
982       return TOOLS_GL_FALSE;                      706       return TOOLS_GL_FALSE;
983     }                                             707     }
984   }                                               708   }
985   return TOOLS_GL_TRUE;                           709   return TOOLS_GL_TRUE;
986 }                                                 710 }
987                                                   711 
988 inline tools_GLboolean tools_gl2psSameColorThr << 712 static tools_GLboolean tools_tools_gl2psSameColorThreshold(int n, tools_GL2PSrgba rgba[],
989                                          tools    713                                          tools_GL2PSrgba threshold)
990 {                                                 714 {
991   int i;                                          715   int i;
992                                                   716 
993   if(n < 2) return TOOLS_GL_TRUE;                 717   if(n < 2) return TOOLS_GL_TRUE;
994                                                   718 
995   for(i = 1; i < n; i++){                         719   for(i = 1; i < n; i++){
996     if(fabs(rgba[0][0] - rgba[i][0]) > thresho    720     if(fabs(rgba[0][0] - rgba[i][0]) > threshold[0] ||
997        fabs(rgba[0][1] - rgba[i][1]) > thresho    721        fabs(rgba[0][1] - rgba[i][1]) > threshold[1] ||
998        fabs(rgba[0][2] - rgba[i][2]) > thresho    722        fabs(rgba[0][2] - rgba[i][2]) > threshold[2])
999       return TOOLS_GL_FALSE;                      723       return TOOLS_GL_FALSE;
1000   }                                              724   }
1001                                                  725 
1002   return TOOLS_GL_TRUE;                          726   return TOOLS_GL_TRUE;
1003 }                                                727 }
1004                                                  728 
1005 inline void tools_gl2psSetLastColor(tools_GL2 << 729 static void tools_gl2psSetLastColor(tools_GL2PSrgba rgba)
1006 {                                                730 {
1007   int i;                                         731   int i;
1008   for(i = 0; i < 3; ++i){                        732   for(i = 0; i < 3; ++i){
1009     gl2ps->lastrgba[i] = rgba[i];             << 733     tools_gl2ps_context->lastrgba[i] = rgba[i];
1010   }                                              734   }
1011 }                                                735 }
1012                                                  736 
1013 inline tools_GLfloat tools_gl2psGetRGB(tools_ << 737 static tools_GLfloat tools_gl2psGetRGB(tools_GL2PSimage *im, tools_GLuint x, tools_GLuint y,
1014                            tools_GLfloat *red    738                            tools_GLfloat *red, tools_GLfloat *green, tools_GLfloat *blue)
1015 {                                                739 {
1016                                                  740 
1017   tools_GLsizei width = im->width;               741   tools_GLsizei width = im->width;
1018   tools_GLsizei height = im->height;             742   tools_GLsizei height = im->height;
1019   tools_GLfloat *pixels = im->pixels;            743   tools_GLfloat *pixels = im->pixels;
1020   tools_GLfloat *pimag;                          744   tools_GLfloat *pimag;
1021                                                  745 
1022   /* OpenGL image is from down to up, PS imag    746   /* OpenGL image is from down to up, PS image is up to down */
1023   switch(im->format){                            747   switch(im->format){
1024   case TOOLS_GL_RGBA:                            748   case TOOLS_GL_RGBA:
1025     pimag = pixels + 4 * (width * (height - 1    749     pimag = pixels + 4 * (width * (height - 1 - y) + x);
1026     break;                                       750     break;
1027   case TOOLS_GL_RGB:                             751   case TOOLS_GL_RGB:
1028   default:                                       752   default:
1029     pimag = pixels + 3 * (width * (height - 1    753     pimag = pixels + 3 * (width * (height - 1 - y) + x);
1030     break;                                       754     break;
1031   }                                              755   }
1032   *red = *pimag; pimag++;                        756   *red = *pimag; pimag++;
1033   *green = *pimag; pimag++;                      757   *green = *pimag; pimag++;
1034   *blue = *pimag; pimag++;                       758   *blue = *pimag; pimag++;
1035                                                  759 
1036   return (im->format == TOOLS_GL_RGBA) ? *pim    760   return (im->format == TOOLS_GL_RGBA) ? *pimag : 1.0F;
1037 }                                                761 }
1038                                                  762 
1039 /* Helper routines for pixmaps */                763 /* Helper routines for pixmaps */
1040                                                  764 
1041 inline tools_GL2PSimage *tools_gl2psCopyPixma << 765 static tools_GL2PSimage *tools_gl2psCopyPixmap(tools_GL2PSimage *im)
1042 {                                                766 {
1043   int size;                                      767   int size;
1044   tools_GL2PSimage *image = (tools_GL2PSimage    768   tools_GL2PSimage *image = (tools_GL2PSimage*)tools_gl2psMalloc(sizeof(tools_GL2PSimage));
1045                                                  769 
1046   image->width = im->width;                      770   image->width = im->width;
1047   image->height = im->height;                    771   image->height = im->height;
1048   image->format = im->format;                    772   image->format = im->format;
1049   image->type = im->type;                        773   image->type = im->type;
1050   image->zoom_x = im->zoom_x;                    774   image->zoom_x = im->zoom_x;
1051   image->zoom_y = im->zoom_y;                    775   image->zoom_y = im->zoom_y;
1052                                                  776 
1053   switch(image->format){                         777   switch(image->format){
1054   case TOOLS_GL_RGBA:                            778   case TOOLS_GL_RGBA:
1055     size = image->height * image->width * 4 *    779     size = image->height * image->width * 4 * sizeof(tools_GLfloat);
1056     break;                                       780     break;
1057   case TOOLS_GL_RGB:                             781   case TOOLS_GL_RGB:
1058   default:                                       782   default:
1059     size = image->height * image->width * 3 *    783     size = image->height * image->width * 3 * sizeof(tools_GLfloat);
1060     break;                                       784     break;
1061   }                                              785   }
1062                                                  786 
1063   image->pixels = (tools_GLfloat*)tools_gl2ps    787   image->pixels = (tools_GLfloat*)tools_gl2psMalloc(size);
1064   memcpy(image->pixels, im->pixels, size);       788   memcpy(image->pixels, im->pixels, size);
1065                                                  789 
1066   return image;                                  790   return image;
1067 }                                                791 }
1068                                                  792 
1069 inline void tools_gl2psFreePixmap(tools_GL2PS << 793 static void tools_tools_gl2psFreePixmap(tools_GL2PSimage *im)
1070 {                                                794 {
1071   if(!im)                                        795   if(!im)
1072     return;                                      796     return;
1073   tools_gl2psFree(im->pixels);                   797   tools_gl2psFree(im->pixels);
1074   tools_gl2psFree(im);                           798   tools_gl2psFree(im);
1075 }                                                799 }
1076                                                  800 
1077 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)             801 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)
1078                                                  802 
1079 #if !defined(png_jmpbuf)                         803 #if !defined(png_jmpbuf)
1080 #  define png_jmpbuf(png_ptr) ((png_ptr)->jmp    804 #  define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
1081 #endif                                           805 #endif
1082                                                  806 
1083 inline void tools_gl2psUserWritePNG(png_struc << 807 static void tools_gl2psUserWritePNG(png_structp png_ptr, png_bytep data, png_size_t length)
1084 {                                                808 {
1085   unsigned int i;                                809   unsigned int i;
1086   tools_GL2PSlist *png = (tools_GL2PSlist*)pn    810   tools_GL2PSlist *png = (tools_GL2PSlist*)png_get_io_ptr(png_ptr);
1087   for(i = 0; i < length; i++)                    811   for(i = 0; i < length; i++)
1088     tools_gl2psListAdd(png, &data[i]);           812     tools_gl2psListAdd(png, &data[i]);
1089 }                                                813 }
1090                                                  814 
1091 inline void tools_gl2psUserFlushPNG(png_struc << 815 static void tools_gl2psUserFlushPNG(png_structp png_ptr)
1092 {                                                816 {
1093   (void) png_ptr;  /* not used */                817   (void) png_ptr;  /* not used */
1094 }                                                818 }
1095                                                  819 
1096 inline void tools_gl2psConvertPixmapToPNG(too << 820 static void tools_gl2psConvertPixmapToPNG(tools_GL2PSimage *pixmap, tools_GL2PSlist *png)
1097 {                                                821 {
1098   png_structp png_ptr;                           822   png_structp png_ptr;
1099   png_infop info_ptr;                            823   png_infop info_ptr;
1100   unsigned char *row_data;                       824   unsigned char *row_data;
1101   tools_GLfloat dr, dg, db;                      825   tools_GLfloat dr, dg, db;
1102   int row, col;                                  826   int row, col;
1103                                                  827 
1104   if(!(png_ptr = png_create_write_struct(PNG_    828   if(!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
1105     return;                                      829     return;
1106                                                  830 
1107   if(!(info_ptr = png_create_info_struct(png_    831   if(!(info_ptr = png_create_info_struct(png_ptr))){
1108     png_destroy_write_struct(&png_ptr, NULL);    832     png_destroy_write_struct(&png_ptr, NULL);
1109     return;                                      833     return;
1110   }                                              834   }
1111                                                  835 
1112   if(setjmp(png_jmpbuf(png_ptr))) {              836   if(setjmp(png_jmpbuf(png_ptr))) {
1113     png_destroy_write_struct(&png_ptr, &info_    837     png_destroy_write_struct(&png_ptr, &info_ptr);
1114     return;                                      838     return;
1115   }                                              839   }
1116                                                  840 
1117   png_set_write_fn(png_ptr, (void *)png, tool    841   png_set_write_fn(png_ptr, (void *)png, tools_gl2psUserWritePNG, tools_gl2psUserFlushPNG);
1118   png_set_compression_level(png_ptr, Z_DEFAUL    842   png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
1119   png_set_IHDR(png_ptr, info_ptr, pixmap->wid    843   png_set_IHDR(png_ptr, info_ptr, pixmap->width, pixmap->height, 8,
1120                PNG_COLOR_TYPE_RGB, PNG_INTERL    844                PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
1121                PNG_FILTER_TYPE_BASE);            845                PNG_FILTER_TYPE_BASE);
1122   png_write_info(png_ptr, info_ptr);             846   png_write_info(png_ptr, info_ptr);
1123                                                  847 
1124   row_data = (unsigned char*)tools_gl2psMallo    848   row_data = (unsigned char*)tools_gl2psMalloc(3 * pixmap->width * sizeof(unsigned char));
1125   for(row = 0; row < pixmap->height; row++){     849   for(row = 0; row < pixmap->height; row++){
1126     for(col = 0; col < pixmap->width; col++){    850     for(col = 0; col < pixmap->width; col++){
1127       tools_gl2psGetRGB(pixmap, col, row, &dr    851       tools_gl2psGetRGB(pixmap, col, row, &dr, &dg, &db);
1128       row_data[3*col] = (unsigned char)(255.     852       row_data[3*col] = (unsigned char)(255. * dr);
1129       row_data[3*col+1] = (unsigned char)(255    853       row_data[3*col+1] = (unsigned char)(255. * dg);
1130       row_data[3*col+2] = (unsigned char)(255    854       row_data[3*col+2] = (unsigned char)(255. * db);
1131     }                                            855     }
1132     png_write_row(png_ptr, (png_bytep)row_dat    856     png_write_row(png_ptr, (png_bytep)row_data);
1133   }                                              857   }
1134   tools_gl2psFree(row_data);                     858   tools_gl2psFree(row_data);
1135                                                  859 
1136   png_write_end(png_ptr, info_ptr);              860   png_write_end(png_ptr, info_ptr);
1137   png_destroy_write_struct(&png_ptr, &info_pt    861   png_destroy_write_struct(&png_ptr, &info_ptr);
1138 }                                                862 }
1139                                                  863 
1140 #endif                                           864 #endif
1141                                                  865 
1142 /* Helper routines for text strings */           866 /* Helper routines for text strings */
1143                                                  867 
1144 inline tools_GLint tools_gl2psAddText(tools_G << 868 static tools_GLint tools_gl2psAddText(tools_GLint type, const char *str, const char *fontname,
1145                           tools_GLshort fonts    869                           tools_GLshort fontsize, tools_GLint alignment, tools_GLfloat angle,
1146                           tools_GL2PSrgba col << 870                           tools_GL2PSrgba color)
1147                           tools_GLfloat blx,  << 
1148 {                                                871 {
1149   tools_GLfloat pos[4];                          872   tools_GLfloat pos[4];
1150   tools_GL2PSprimitive *prim;                    873   tools_GL2PSprimitive *prim;
1151   tools_GLboolean valid;                         874   tools_GLboolean valid;
1152                                                  875 
1153   if(/*!gl2ps ||*/ !str || !fontname) return  << 876   if(!tools_gl2ps_context || !str || !fontname) return TOOLS_GL2PS_UNINITIALIZED;
1154                                                  877 
1155   if(gl2ps->options & TOOLS_GL2PS_NO_TEXT) re << 878   if(tools_gl2ps_context->options & TOOLS_GL2PS_NO_TEXT) return TOOLS_GL2PS_SUCCESS;
1156                                                  879 
1157   if (gl2ps->forcerasterpos) {                << 880   if (tools_gl2ps_context->forcerasterpos) {
1158     pos[0] = gl2ps->rasterpos.xyz[0];         << 881     pos[0] = tools_gl2ps_context->rasterpos.xyz[0];
1159     pos[1] = gl2ps->rasterpos.xyz[1];         << 882     pos[1] = tools_gl2ps_context->rasterpos.xyz[1];
1160     pos[2] = gl2ps->rasterpos.xyz[2];         << 883     pos[2] = tools_gl2ps_context->rasterpos.xyz[2];
1161     pos[3] = 1.f;                                884     pos[3] = 1.f;
1162   }                                              885   }
1163   else {                                         886   else {
1164     tools_glGetBooleanv(TOOLS_GL_CURRENT_RAST    887     tools_glGetBooleanv(TOOLS_GL_CURRENT_RASTER_POSITION_VALID, &valid);
1165     if(TOOLS_GL_FALSE == valid) return TOOLS_    888     if(TOOLS_GL_FALSE == valid) return TOOLS_GL2PS_SUCCESS; /* the primitive is culled */
1166     tools_glGetFloatv(TOOLS_GL_CURRENT_RASTER    889     tools_glGetFloatv(TOOLS_GL_CURRENT_RASTER_POSITION, pos);
1167   }                                              890   }
1168                                                  891 
1169   prim = (tools_GL2PSprimitive*)tools_gl2psMa    892   prim = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
1170   prim->type = (tools_GLshort)type;              893   prim->type = (tools_GLshort)type;
1171   prim->boundary = 0;                            894   prim->boundary = 0;
1172   prim->numverts = setblpos ? 2 : 1;          << 895   prim->numverts = 1;
1173   prim->verts = (tools_GL2PSvertex*)tools_gl2 << 896   prim->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(sizeof(tools_GL2PSvertex));
1174   prim->verts[0].xyz[0] = pos[0];                897   prim->verts[0].xyz[0] = pos[0];
1175   prim->verts[0].xyz[1] = pos[1];                898   prim->verts[0].xyz[1] = pos[1];
1176   prim->verts[0].xyz[2] = pos[2];                899   prim->verts[0].xyz[2] = pos[2];
1177   if (setblpos) {                             << 
1178     prim->verts[1].xyz[0] = blx;              << 
1179     prim->verts[1].xyz[1] = bly;              << 
1180     prim->verts[1].xyz[2] = 0;                << 
1181   }                                           << 
1182   prim->culled = 0;                              900   prim->culled = 0;
1183   prim->offset = 0;                              901   prim->offset = 0;
1184   prim->ofactor = 0.0;                           902   prim->ofactor = 0.0;
1185   prim->ounits = 0.0;                            903   prim->ounits = 0.0;
1186   prim->pattern = 0;                             904   prim->pattern = 0;
1187   prim->factor = 0;                              905   prim->factor = 0;
1188   prim->width = 1;                               906   prim->width = 1;
1189   prim->linecap = 0;                             907   prim->linecap = 0;
1190   prim->linejoin = 0;                            908   prim->linejoin = 0;
1191                                                  909 
1192   if (color) {                                   910   if (color) {
1193     memcpy(prim->verts[0].rgba, color, 4 * si    911     memcpy(prim->verts[0].rgba, color, 4 * sizeof(float));
1194   }                                              912   }
1195   else {                                         913   else {
1196     if (gl2ps->forcerasterpos) {              << 914     if (tools_gl2ps_context->forcerasterpos) {
1197       prim->verts[0].rgba[0] = gl2ps->rasterp << 915       prim->verts[0].rgba[0] = tools_gl2ps_context->rasterpos.rgba[0];
1198       prim->verts[0].rgba[1] = gl2ps->rasterp << 916       prim->verts[0].rgba[1] = tools_gl2ps_context->rasterpos.rgba[1];
1199       prim->verts[0].rgba[2] = gl2ps->rasterp << 917       prim->verts[0].rgba[2] = tools_gl2ps_context->rasterpos.rgba[2];
1200       prim->verts[0].rgba[3] = gl2ps->rasterp << 918       prim->verts[0].rgba[3] = tools_gl2ps_context->rasterpos.rgba[3];
1201     }                                            919     }
1202     else {                                       920     else {
1203       tools_glGetFloatv(TOOLS_GL_CURRENT_RAST    921       tools_glGetFloatv(TOOLS_GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba);
1204     }                                            922     }
1205   }                                              923   }
1206   prim->data.text = (tools_GL2PSstring*)tools    924   prim->data.text = (tools_GL2PSstring*)tools_gl2psMalloc(sizeof(tools_GL2PSstring));
1207   prim->data.text->str = (char*)tools_gl2psMa    925   prim->data.text->str = (char*)tools_gl2psMalloc((strlen(str)+1)*sizeof(char));
1208   strcpy(prim->data.text->str, str);             926   strcpy(prim->data.text->str, str);
1209   prim->data.text->fontname = (char*)tools_gl    927   prim->data.text->fontname = (char*)tools_gl2psMalloc((strlen(fontname)+1)*sizeof(char));
1210   strcpy(prim->data.text->fontname, fontname)    928   strcpy(prim->data.text->fontname, fontname);
1211   prim->data.text->fontsize = fontsize;          929   prim->data.text->fontsize = fontsize;
1212   prim->data.text->alignment = alignment;        930   prim->data.text->alignment = alignment;
1213   prim->data.text->angle = angle;                931   prim->data.text->angle = angle;
1214                                                  932 
1215   gl2ps->forcerasterpos = TOOLS_GL_FALSE;     << 933   tools_gl2ps_context->forcerasterpos = TOOLS_GL_FALSE;
1216                                                  934 
1217   /* If no OpenGL context, just add directly     935   /* If no OpenGL context, just add directly to primitives */
1218   if (gl2ps->options & TOOLS_GL2PS_NO_OPENGL_ << 936   if (tools_gl2ps_context->options & TOOLS_GL2PS_NO_OPENGL_CONTEXT) {
1219     tools_gl2psListAdd(gl2ps->primitives, &pr << 937     tools_gl2psListAdd(tools_gl2ps_context->primitives, &prim);
1220   }                                              938   }
1221   else {                                         939   else {
1222     tools_gl2psListAdd(gl2ps->auxprimitives,  << 940     tools_gl2psListAdd(tools_gl2ps_context->auxprimitives, &prim);
1223     tools_glPassThrough(TOOLS_GL2PS_TEXT_TOKE    941     tools_glPassThrough(TOOLS_GL2PS_TEXT_TOKEN);
1224   }                                              942   }
1225                                                  943 
1226   return TOOLS_GL2PS_SUCCESS;                    944   return TOOLS_GL2PS_SUCCESS;
1227 }                                                945 }
1228                                                  946 
1229 inline tools_GL2PSstring *tools_gl2psCopyText << 947 static tools_GL2PSstring *tools_gl2psCopyText(tools_GL2PSstring *t)
1230 {                                                948 {
1231   tools_GL2PSstring *text = (tools_GL2PSstrin    949   tools_GL2PSstring *text = (tools_GL2PSstring*)tools_gl2psMalloc(sizeof(tools_GL2PSstring));
1232   text->str = (char*)tools_gl2psMalloc((strle    950   text->str = (char*)tools_gl2psMalloc((strlen(t->str)+1)*sizeof(char));
1233   strcpy(text->str, t->str);                     951   strcpy(text->str, t->str);
1234   text->fontname = (char*)tools_gl2psMalloc((    952   text->fontname = (char*)tools_gl2psMalloc((strlen(t->fontname)+1)*sizeof(char));
1235   strcpy(text->fontname, t->fontname);           953   strcpy(text->fontname, t->fontname);
1236   text->fontsize = t->fontsize;                  954   text->fontsize = t->fontsize;
1237   text->alignment = t->alignment;                955   text->alignment = t->alignment;
1238   text->angle = t->angle;                        956   text->angle = t->angle;
1239                                                  957 
1240   return text;                                   958   return text;
1241 }                                                959 }
1242                                                  960 
1243 inline void tools_gl2psFreeText(tools_GL2PSst << 961 static void tools_tools_gl2psFreeText(tools_GL2PSstring *text)
1244 {                                                962 {
1245   if(!text)                                      963   if(!text)
1246     return;                                      964     return;
1247   tools_gl2psFree(text->str);                    965   tools_gl2psFree(text->str);
1248   tools_gl2psFree(text->fontname);               966   tools_gl2psFree(text->fontname);
1249   tools_gl2psFree(text);                         967   tools_gl2psFree(text);
1250 }                                                968 }
1251                                                  969 
1252 /* Helpers for blending modes */                 970 /* Helpers for blending modes */
1253                                                  971 
1254 inline tools_GLboolean tools_gl2psSupportedBl << 972 static tools_GLboolean tools_gl2psSupportedBlendMode(tools_GLenum sfactor, tools_GLenum dfactor)
1255 {                                                973 {
1256   /* returns TRUE if gl2ps supports the argum    974   /* returns TRUE if gl2ps supports the argument combination: only two
1257      blending modes have been implemented so     975      blending modes have been implemented so far */
1258                                                  976 
1259   if( (sfactor == TOOLS_GL_SRC_ALPHA && dfact    977   if( (sfactor == TOOLS_GL_SRC_ALPHA && dfactor == TOOLS_GL_ONE_MINUS_SRC_ALPHA) ||
1260       (sfactor == TOOLS_GL_ONE && dfactor ==     978       (sfactor == TOOLS_GL_ONE && dfactor == TOOLS_GL_ZERO) )
1261     return TOOLS_GL_TRUE;                        979     return TOOLS_GL_TRUE;
1262   return TOOLS_GL_FALSE;                         980   return TOOLS_GL_FALSE;
1263 }                                                981 }
1264                                                  982 
1265 inline void tools_gl2psAdaptVertexForBlending << 983 static void tools_gl2psAdaptVertexForBlending(tools_GL2PSvertex *v)
1266 {                                                984 {
1267   /* Transforms vertex depending on the actua    985   /* Transforms vertex depending on the actual blending function -
1268      currently the vertex v is considered as     986      currently the vertex v is considered as source vertex and his
1269      alpha value is changed to 1.0 if source     987      alpha value is changed to 1.0 if source blending TOOLS_GL_ONE is
1270      active. This might be extended in the fu    988      active. This might be extended in the future */
1271                                                  989 
1272   if(!v /* || !gl2ps*/)                       << 990   if(!v || !tools_gl2ps_context)
1273     return;                                      991     return;
1274                                                  992 
1275   if(gl2ps->options & TOOLS_GL2PS_NO_BLENDING << 993   if(tools_gl2ps_context->options & TOOLS_GL2PS_NO_BLENDING || !tools_gl2ps_context->blending){
1276     v->rgba[3] = 1.0F;                           994     v->rgba[3] = 1.0F;
1277     return;                                      995     return;
1278   }                                              996   }
1279                                                  997 
1280   switch(gl2ps->blendfunc[0]){                << 998   switch(tools_gl2ps_context->blendfunc[0]){
1281   case TOOLS_GL_ONE:                             999   case TOOLS_GL_ONE:
1282     v->rgba[3] = 1.0F;                           1000     v->rgba[3] = 1.0F;
1283     break;                                       1001     break;
1284   default:                                       1002   default:
1285     break;                                       1003     break;
1286   }                                              1004   }
1287 }                                                1005 }
1288                                                  1006 
1289 inline void tools_gl2psAssignTriangleProperti << 1007 static void tools_gl2psAssignTriangleProperties(tools_GL2PStriangle *t)
1290 {                                                1008 {
1291   /* int i; */                                   1009   /* int i; */
1292                                                  1010 
1293   t->prop = T_VAR_COLOR;                         1011   t->prop = T_VAR_COLOR;
1294                                                  1012 
1295   /* Uncommenting the following lines activat    1013   /* Uncommenting the following lines activates an even more fine
1296      grained distinction between triangle typ    1014      grained distinction between triangle types - please don't delete,
1297      a remarkable amount of PDF handling code    1015      a remarkable amount of PDF handling code inside this file depends
1298      on it if activated */                       1016      on it if activated */
1299   /*                                             1017   /*
1300   t->prop = T_CONST_COLOR;                       1018   t->prop = T_CONST_COLOR;
1301   for(i = 0; i < 3; ++i){                        1019   for(i = 0; i < 3; ++i){
1302     if(!TOOLS_GL2PS_ZERO(t->vertex[0].rgba[i]    1020     if(!TOOLS_GL2PS_ZERO(t->vertex[0].rgba[i] - t->vertex[1].rgba[i]) ||
1303        !TOOLS_GL2PS_ZERO(t->vertex[1].rgba[i]    1021        !TOOLS_GL2PS_ZERO(t->vertex[1].rgba[i] - t->vertex[2].rgba[i])){
1304       t->prop = T_VAR_COLOR;                     1022       t->prop = T_VAR_COLOR;
1305       break;                                     1023       break;
1306     }                                            1024     }
1307   }                                              1025   }
1308   */                                             1026   */
1309                                                  1027 
1310   if(!TOOLS_GL2PS_ZERO(t->vertex[0].rgba[3] -    1028   if(!TOOLS_GL2PS_ZERO(t->vertex[0].rgba[3] - t->vertex[1].rgba[3]) ||
1311      !TOOLS_GL2PS_ZERO(t->vertex[1].rgba[3] -    1029      !TOOLS_GL2PS_ZERO(t->vertex[1].rgba[3] - t->vertex[2].rgba[3])){
1312     t->prop |= T_VAR_ALPHA;                      1030     t->prop |= T_VAR_ALPHA;
1313   }                                              1031   }
1314   else{                                          1032   else{
1315     if(t->vertex[0].rgba[3] < 1)                 1033     if(t->vertex[0].rgba[3] < 1)
1316       t->prop |= T_ALPHA_LESS_1;                 1034       t->prop |= T_ALPHA_LESS_1;
1317     else                                         1035     else
1318       t->prop |= T_ALPHA_1;                      1036       t->prop |= T_ALPHA_1;
1319   }                                              1037   }
1320 }                                                1038 }
1321                                                  1039 
1322 inline void tools_gl2psFillTriangleFromPrimit << 1040 static void tools_gl2psFillTriangleFromPrimitive(tools_GL2PStriangle *t, tools_GL2PSprimitive *p,
1323                                            to    1041                                            tools_GLboolean assignprops)
1324 {                                                1042 {
1325   t->vertex[0] = p->verts[0];                    1043   t->vertex[0] = p->verts[0];
1326   t->vertex[1] = p->verts[1];                    1044   t->vertex[1] = p->verts[1];
1327   t->vertex[2] = p->verts[2];                    1045   t->vertex[2] = p->verts[2];
1328   if(TOOLS_GL_TRUE == assignprops)               1046   if(TOOLS_GL_TRUE == assignprops)
1329     tools_gl2psAssignTriangleProperties(t);      1047     tools_gl2psAssignTriangleProperties(t);
1330 }                                                1048 }
1331                                                  1049 
1332 inline void tools_gl2psInitTriangle(tools_GL2 << 1050 static void tools_gl2psInitTriangle(tools_GL2PStriangle *t)
1333 {                                                1051 {
1334   int i;                                         1052   int i;
1335   tools_GL2PSvertex vertex = { {-1.0F, -1.0F,    1053   tools_GL2PSvertex vertex = { {-1.0F, -1.0F, -1.0F}, {-1.0F, -1.0F, -1.0F, -1.0F} };
1336   for(i = 0; i < 3; i++)                         1054   for(i = 0; i < 3; i++)
1337     t->vertex[i] = vertex;                       1055     t->vertex[i] = vertex;
1338   t->prop = T_UNDEFINED;                         1056   t->prop = T_UNDEFINED;
1339 }                                                1057 }
1340                                                  1058 
1341 /* Miscellaneous helper routines */              1059 /* Miscellaneous helper routines */
1342                                                  1060 
1343 inline void tools_gl2psResetLineProperties(to << 1061 static void tools_gl2psResetLineProperties(void)
1344 {                                                1062 {
1345   gl2ps->lastlinewidth = 0.;                  << 1063   tools_gl2ps_context->lastlinewidth = 0.;
1346   gl2ps->lastlinecap = gl2ps->lastlinejoin =  << 1064   tools_gl2ps_context->lastlinecap = tools_gl2ps_context->lastlinejoin = 0;
1347 }                                                1065 }
1348                                                  1066 
1349 inline tools_GL2PSprimitive *tools_gl2psCopyP << 1067 static tools_GL2PSprimitive *tools_gl2psCopyPrimitive(tools_GL2PSprimitive *p)
1350 {                                                1068 {
1351   tools_GL2PSprimitive *prim;                    1069   tools_GL2PSprimitive *prim;
1352                                                  1070 
1353   if(!p){                                        1071   if(!p){
1354     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Trying    1072     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Trying to copy an empty primitive");
1355     return NULL;                                 1073     return NULL;
1356   }                                              1074   }
1357                                                  1075 
1358   prim = (tools_GL2PSprimitive*)tools_gl2psMa    1076   prim = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
1359                                                  1077 
1360   prim->type = p->type;                          1078   prim->type = p->type;
1361   prim->numverts = p->numverts;                  1079   prim->numverts = p->numverts;
1362   prim->boundary = p->boundary;                  1080   prim->boundary = p->boundary;
1363   prim->offset = p->offset;                      1081   prim->offset = p->offset;
1364   prim->ofactor = p->ofactor;                    1082   prim->ofactor = p->ofactor;
1365   prim->ounits = p->ounits;                      1083   prim->ounits = p->ounits;
1366   prim->pattern = p->pattern;                    1084   prim->pattern = p->pattern;
1367   prim->factor = p->factor;                      1085   prim->factor = p->factor;
1368   prim->culled = p->culled;                      1086   prim->culled = p->culled;
1369   prim->width = p->width;                        1087   prim->width = p->width;
1370   prim->linecap = p->linecap;                    1088   prim->linecap = p->linecap;
1371   prim->linejoin = p->linejoin;                  1089   prim->linejoin = p->linejoin;
1372   prim->verts = (tools_GL2PSvertex*)tools_gl2    1090   prim->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(p->numverts*sizeof(tools_GL2PSvertex));
1373   memcpy(prim->verts, p->verts, p->numverts *    1091   memcpy(prim->verts, p->verts, p->numverts * sizeof(tools_GL2PSvertex));
1374                                                  1092 
1375   switch(prim->type){                            1093   switch(prim->type){
1376   case TOOLS_GL2PS_PIXMAP :                      1094   case TOOLS_GL2PS_PIXMAP :
1377     prim->data.image = tools_gl2psCopyPixmap(    1095     prim->data.image = tools_gl2psCopyPixmap(p->data.image);
1378     break;                                       1096     break;
1379   case TOOLS_GL2PS_TEXT :                        1097   case TOOLS_GL2PS_TEXT :
1380   case TOOLS_GL2PS_SPECIAL :                     1098   case TOOLS_GL2PS_SPECIAL :
1381     prim->data.text = tools_gl2psCopyText(p->    1099     prim->data.text = tools_gl2psCopyText(p->data.text);
1382     break;                                       1100     break;
1383   default:                                       1101   default:
1384     break;                                       1102     break;
1385   }                                              1103   }
1386                                                  1104 
1387   return prim;                                   1105   return prim;
1388 }                                                1106 }
1389                                                  1107 
1390 inline tools_GLboolean tools_gl2psSamePositio << 1108 static tools_GLboolean tools_gl2psSamePosition(tools_GL2PSxyz p1, tools_GL2PSxyz p2)
1391 {                                                1109 {
1392   if(!TOOLS_GL2PS_ZERO(p1[0] - p2[0]) ||         1110   if(!TOOLS_GL2PS_ZERO(p1[0] - p2[0]) ||
1393      !TOOLS_GL2PS_ZERO(p1[1] - p2[1]) ||         1111      !TOOLS_GL2PS_ZERO(p1[1] - p2[1]) ||
1394      !TOOLS_GL2PS_ZERO(p1[2] - p2[2]))           1112      !TOOLS_GL2PS_ZERO(p1[2] - p2[2]))
1395     return TOOLS_GL_FALSE;                       1113     return TOOLS_GL_FALSE;
1396   return TOOLS_GL_TRUE;                          1114   return TOOLS_GL_TRUE;
1397 }                                                1115 }
1398                                                  1116 
1399 /********************************************    1117 /*********************************************************************
1400  *                                               1118  *
1401  * 3D sorting routines                           1119  * 3D sorting routines
1402  *                                               1120  *
1403  ********************************************    1121  *********************************************************************/
1404                                                  1122 
1405 inline tools_GLfloat tools_gl2psComparePointP << 1123 static tools_GLfloat tools_gl2psComparePointPlane(tools_GL2PSxyz point, tools_GL2PSplane plane)
1406 {                                                1124 {
1407   return (plane[0] * point[0] +                  1125   return (plane[0] * point[0] +
1408           plane[1] * point[1] +                  1126           plane[1] * point[1] +
1409           plane[2] * point[2] +                  1127           plane[2] * point[2] +
1410           plane[3]);                             1128           plane[3]);
1411 }                                                1129 }
1412                                                  1130 
1413 inline tools_GLfloat tools_gl2psPsca(tools_GL << 1131 static tools_GLfloat tools_gl2psPsca(tools_GLfloat *a, tools_GLfloat *b)
1414 {                                                1132 {
1415   return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);    1133   return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
1416 }                                                1134 }
1417                                                  1135 
1418 inline void tools_gl2psPvec(tools_GLfloat *a, << 1136 static void tools_gl2psPvec(tools_GLfloat *a, tools_GLfloat *b, tools_GLfloat *c)
1419 {                                                1137 {
1420   c[0] = a[1]*b[2] - a[2]*b[1];                  1138   c[0] = a[1]*b[2] - a[2]*b[1];
1421   c[1] = a[2]*b[0] - a[0]*b[2];                  1139   c[1] = a[2]*b[0] - a[0]*b[2];
1422   c[2] = a[0]*b[1] - a[1]*b[0];                  1140   c[2] = a[0]*b[1] - a[1]*b[0];
1423 }                                                1141 }
1424                                                  1142 
1425 inline tools_GLfloat tools_gl2psNorm(tools_GL << 1143 static tools_GLfloat tools_gl2psNorm(tools_GLfloat *a)
1426 {                                                1144 {
1427   return (tools_GLfloat)sqrt(a[0]*a[0] + a[1]    1145   return (tools_GLfloat)sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
1428 }                                                1146 }
1429                                                  1147 
1430 inline void tools_gl2psGetNormal(tools_GLfloa << 1148 static void tools_gl2psGetNormal(tools_GLfloat *a, tools_GLfloat *b, tools_GLfloat *c)
1431 {                                                1149 {
1432   tools_GLfloat norm;                            1150   tools_GLfloat norm;
1433                                                  1151 
1434   tools_gl2psPvec(a, b, c);                      1152   tools_gl2psPvec(a, b, c);
1435   if(!TOOLS_GL2PS_ZERO(norm = tools_gl2psNorm    1153   if(!TOOLS_GL2PS_ZERO(norm = tools_gl2psNorm(c))){
1436     c[0] = c[0] / norm;                          1154     c[0] = c[0] / norm;
1437     c[1] = c[1] / norm;                          1155     c[1] = c[1] / norm;
1438     c[2] = c[2] / norm;                          1156     c[2] = c[2] / norm;
1439   }                                              1157   }
1440   else{                                          1158   else{
1441     /* The plane is still wrong despite our t    1159     /* The plane is still wrong despite our tests in tools_gl2psGetPlane.
1442        Let's return a dummy value for now (th    1160        Let's return a dummy value for now (this is a hack: we should
1443        do more intelligent tests in GetPlane)    1161        do more intelligent tests in GetPlane) */
1444     c[0] = c[1] = 0.0F;                          1162     c[0] = c[1] = 0.0F;
1445     c[2] = 1.0F;                                 1163     c[2] = 1.0F;
1446   }                                              1164   }
1447 }                                                1165 }
1448                                                  1166 
1449 inline void tools_gl2psGetPlane(tools_GL2PSpr << 1167 static void tools_gl2psGetPlane(tools_GL2PSprimitive *prim, tools_GL2PSplane plane)
1450 {                                                1168 {
1451   tools_GL2PSxyz v = {0.0F, 0.0F, 0.0F}, w =     1169   tools_GL2PSxyz v = {0.0F, 0.0F, 0.0F}, w = {0.0F, 0.0F, 0.0F};
1452                                                  1170 
1453   switch(prim->type){                            1171   switch(prim->type){
1454   case TOOLS_GL2PS_TRIANGLE :                    1172   case TOOLS_GL2PS_TRIANGLE :
1455   case TOOLS_GL2PS_QUADRANGLE :                  1173   case TOOLS_GL2PS_QUADRANGLE :
1456     v[0] = prim->verts[1].xyz[0] - prim->vert    1174     v[0] = prim->verts[1].xyz[0] - prim->verts[0].xyz[0];
1457     v[1] = prim->verts[1].xyz[1] - prim->vert    1175     v[1] = prim->verts[1].xyz[1] - prim->verts[0].xyz[1];
1458     v[2] = prim->verts[1].xyz[2] - prim->vert    1176     v[2] = prim->verts[1].xyz[2] - prim->verts[0].xyz[2];
1459     w[0] = prim->verts[2].xyz[0] - prim->vert    1177     w[0] = prim->verts[2].xyz[0] - prim->verts[0].xyz[0];
1460     w[1] = prim->verts[2].xyz[1] - prim->vert    1178     w[1] = prim->verts[2].xyz[1] - prim->verts[0].xyz[1];
1461     w[2] = prim->verts[2].xyz[2] - prim->vert    1179     w[2] = prim->verts[2].xyz[2] - prim->verts[0].xyz[2];
1462     if((TOOLS_GL2PS_ZERO(v[0]) && TOOLS_GL2PS    1180     if((TOOLS_GL2PS_ZERO(v[0]) && TOOLS_GL2PS_ZERO(v[1]) && TOOLS_GL2PS_ZERO(v[2])) ||
1463        (TOOLS_GL2PS_ZERO(w[0]) && TOOLS_GL2PS    1181        (TOOLS_GL2PS_ZERO(w[0]) && TOOLS_GL2PS_ZERO(w[1]) && TOOLS_GL2PS_ZERO(w[2]))){
1464       plane[0] = plane[1] = 0.0F;                1182       plane[0] = plane[1] = 0.0F;
1465       plane[2] = 1.0F;                           1183       plane[2] = 1.0F;
1466       plane[3] = -prim->verts[0].xyz[2];         1184       plane[3] = -prim->verts[0].xyz[2];
1467     }                                            1185     }
1468     else{                                        1186     else{
1469       tools_gl2psGetNormal(v, w, plane);         1187       tools_gl2psGetNormal(v, w, plane);
1470       plane[3] =                                 1188       plane[3] =
1471         - plane[0] * prim->verts[0].xyz[0]       1189         - plane[0] * prim->verts[0].xyz[0]
1472         - plane[1] * prim->verts[0].xyz[1]       1190         - plane[1] * prim->verts[0].xyz[1]
1473         - plane[2] * prim->verts[0].xyz[2];      1191         - plane[2] * prim->verts[0].xyz[2];
1474     }                                            1192     }
1475     break;                                       1193     break;
1476   case TOOLS_GL2PS_LINE :                        1194   case TOOLS_GL2PS_LINE :
1477     v[0] = prim->verts[1].xyz[0] - prim->vert    1195     v[0] = prim->verts[1].xyz[0] - prim->verts[0].xyz[0];
1478     v[1] = prim->verts[1].xyz[1] - prim->vert    1196     v[1] = prim->verts[1].xyz[1] - prim->verts[0].xyz[1];
1479     v[2] = prim->verts[1].xyz[2] - prim->vert    1197     v[2] = prim->verts[1].xyz[2] - prim->verts[0].xyz[2];
1480     if(TOOLS_GL2PS_ZERO(v[0]) && TOOLS_GL2PS_    1198     if(TOOLS_GL2PS_ZERO(v[0]) && TOOLS_GL2PS_ZERO(v[1]) && TOOLS_GL2PS_ZERO(v[2])){
1481       plane[0] = plane[1] = 0.0F;                1199       plane[0] = plane[1] = 0.0F;
1482       plane[2] = 1.0F;                           1200       plane[2] = 1.0F;
1483       plane[3] = -prim->verts[0].xyz[2];         1201       plane[3] = -prim->verts[0].xyz[2];
1484     }                                            1202     }
1485     else{                                        1203     else{
1486       if(TOOLS_GL2PS_ZERO(v[0]))      w[0] =     1204       if(TOOLS_GL2PS_ZERO(v[0]))      w[0] = 1.0F;
1487       else if(TOOLS_GL2PS_ZERO(v[1])) w[1] =     1205       else if(TOOLS_GL2PS_ZERO(v[1])) w[1] = 1.0F;
1488       else                      w[2] = 1.0F;     1206       else                      w[2] = 1.0F;
1489       tools_gl2psGetNormal(v, w, plane);         1207       tools_gl2psGetNormal(v, w, plane);
1490       plane[3] =                                 1208       plane[3] =
1491         - plane[0] * prim->verts[0].xyz[0]       1209         - plane[0] * prim->verts[0].xyz[0]
1492         - plane[1] * prim->verts[0].xyz[1]       1210         - plane[1] * prim->verts[0].xyz[1]
1493         - plane[2] * prim->verts[0].xyz[2];      1211         - plane[2] * prim->verts[0].xyz[2];
1494     }                                            1212     }
1495     break;                                       1213     break;
1496   case TOOLS_GL2PS_POINT :                       1214   case TOOLS_GL2PS_POINT :
1497   case TOOLS_GL2PS_PIXMAP :                      1215   case TOOLS_GL2PS_PIXMAP :
1498   case TOOLS_GL2PS_TEXT :                        1216   case TOOLS_GL2PS_TEXT :
1499   case TOOLS_GL2PS_SPECIAL :                     1217   case TOOLS_GL2PS_SPECIAL :
1500   case TOOLS_GL2PS_IMAGEMAP:                     1218   case TOOLS_GL2PS_IMAGEMAP:
1501     plane[0] = plane[1] = 0.0F;                  1219     plane[0] = plane[1] = 0.0F;
1502     plane[2] = 1.0F;                             1220     plane[2] = 1.0F;
1503     plane[3] = -prim->verts[0].xyz[2];           1221     plane[3] = -prim->verts[0].xyz[2];
1504     break;                                       1222     break;
1505   default :                                      1223   default :
1506     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknow    1224     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknown primitive type in BSP tree");
1507     plane[0] = plane[1] = plane[3] = 0.0F;       1225     plane[0] = plane[1] = plane[3] = 0.0F;
1508     plane[2] = 1.0F;                             1226     plane[2] = 1.0F;
1509     break;                                       1227     break;
1510   }                                              1228   }
1511 }                                                1229 }
1512                                                  1230 
1513 inline void tools_gl2psCutEdge(tools_GL2PSver << 1231 static void tools_gl2psCutEdge(tools_GL2PSvertex *a, tools_GL2PSvertex *b, tools_GL2PSplane plane,
1514                          tools_GL2PSvertex *c    1232                          tools_GL2PSvertex *c)
1515 {                                                1233 {
1516   tools_GL2PSxyz v;                              1234   tools_GL2PSxyz v;
1517   tools_GLfloat sect, psca;                      1235   tools_GLfloat sect, psca;
1518                                                  1236 
1519   v[0] = b->xyz[0] - a->xyz[0];                  1237   v[0] = b->xyz[0] - a->xyz[0];
1520   v[1] = b->xyz[1] - a->xyz[1];                  1238   v[1] = b->xyz[1] - a->xyz[1];
1521   v[2] = b->xyz[2] - a->xyz[2];                  1239   v[2] = b->xyz[2] - a->xyz[2];
1522                                                  1240 
1523   if(!TOOLS_GL2PS_ZERO(psca = tools_gl2psPsca    1241   if(!TOOLS_GL2PS_ZERO(psca = tools_gl2psPsca(plane, v)))
1524     sect = -tools_gl2psComparePointPlane(a->x    1242     sect = -tools_gl2psComparePointPlane(a->xyz, plane) / psca;
1525   else                                           1243   else
1526     sect = 0.0F;                                 1244     sect = 0.0F;
1527                                                  1245 
1528   c->xyz[0] = a->xyz[0] + v[0] * sect;           1246   c->xyz[0] = a->xyz[0] + v[0] * sect;
1529   c->xyz[1] = a->xyz[1] + v[1] * sect;           1247   c->xyz[1] = a->xyz[1] + v[1] * sect;
1530   c->xyz[2] = a->xyz[2] + v[2] * sect;           1248   c->xyz[2] = a->xyz[2] + v[2] * sect;
1531                                                  1249 
1532   c->rgba[0] = (1 - sect) * a->rgba[0] + sect    1250   c->rgba[0] = (1 - sect) * a->rgba[0] + sect * b->rgba[0];
1533   c->rgba[1] = (1 - sect) * a->rgba[1] + sect    1251   c->rgba[1] = (1 - sect) * a->rgba[1] + sect * b->rgba[1];
1534   c->rgba[2] = (1 - sect) * a->rgba[2] + sect    1252   c->rgba[2] = (1 - sect) * a->rgba[2] + sect * b->rgba[2];
1535   c->rgba[3] = (1 - sect) * a->rgba[3] + sect    1253   c->rgba[3] = (1 - sect) * a->rgba[3] + sect * b->rgba[3];
1536 }                                                1254 }
1537                                                  1255 
1538 inline void tools_gl2psCreateSplitPrimitive(t << 1256 static void tools_gl2psCreateSplitPrimitive(tools_GL2PSprimitive *parent, tools_GL2PSplane plane,
1539                                       tools_G    1257                                       tools_GL2PSprimitive *child, tools_GLshort numverts,
1540                                       tools_G    1258                                       tools_GLshort *index0, tools_GLshort *index1)
1541 {                                                1259 {
1542   tools_GLshort i;                               1260   tools_GLshort i;
1543                                                  1261 
1544   if(parent->type == TOOLS_GL2PS_IMAGEMAP){      1262   if(parent->type == TOOLS_GL2PS_IMAGEMAP){
1545     child->type = TOOLS_GL2PS_IMAGEMAP;          1263     child->type = TOOLS_GL2PS_IMAGEMAP;
1546     child->data.image = parent->data.image;      1264     child->data.image = parent->data.image;
1547   }                                              1265   }
1548   else{                                          1266   else{
1549     if(numverts > 4){                            1267     if(numverts > 4){
1550       tools_gl2psMsg(TOOLS_GL2PS_WARNING, "%d    1268       tools_gl2psMsg(TOOLS_GL2PS_WARNING, "%d vertices in polygon", numverts);
1551       numverts = 4;                              1269       numverts = 4;
1552     }                                            1270     }
1553     switch(numverts){                            1271     switch(numverts){
1554     case 1 : child->type = TOOLS_GL2PS_POINT;    1272     case 1 : child->type = TOOLS_GL2PS_POINT; break;
1555     case 2 : child->type = TOOLS_GL2PS_LINE;     1273     case 2 : child->type = TOOLS_GL2PS_LINE; break;
1556     case 3 : child->type = TOOLS_GL2PS_TRIANG    1274     case 3 : child->type = TOOLS_GL2PS_TRIANGLE; break;
1557     case 4 : child->type = TOOLS_GL2PS_QUADRA    1275     case 4 : child->type = TOOLS_GL2PS_QUADRANGLE; break;
1558     default: child->type = TOOLS_GL2PS_NO_TYP    1276     default: child->type = TOOLS_GL2PS_NO_TYPE; break;
1559     }                                            1277     }
1560   }                                              1278   }
1561                                                  1279 
1562   child->boundary = 0; /* FIXME: not done! */    1280   child->boundary = 0; /* FIXME: not done! */
1563   child->culled = parent->culled;                1281   child->culled = parent->culled;
1564   child->offset = parent->offset;                1282   child->offset = parent->offset;
1565   child->ofactor = parent->ofactor;              1283   child->ofactor = parent->ofactor;
1566   child->ounits = parent->ounits;                1284   child->ounits = parent->ounits;
1567   child->pattern = parent->pattern;              1285   child->pattern = parent->pattern;
1568   child->factor = parent->factor;                1286   child->factor = parent->factor;
1569   child->width = parent->width;                  1287   child->width = parent->width;
1570   child->linecap = parent->linecap;              1288   child->linecap = parent->linecap;
1571   child->linejoin = parent->linejoin;            1289   child->linejoin = parent->linejoin;
1572   child->numverts = numverts;                    1290   child->numverts = numverts;
1573   child->verts = (tools_GL2PSvertex*)tools_gl    1291   child->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(numverts * sizeof(tools_GL2PSvertex));
1574                                                  1292 
1575   for(i = 0; i < numverts; i++){                 1293   for(i = 0; i < numverts; i++){
1576     if(index1[i] < 0){                           1294     if(index1[i] < 0){
1577       child->verts[i] = parent->verts[index0[    1295       child->verts[i] = parent->verts[index0[i]];
1578     }                                            1296     }
1579     else{                                        1297     else{
1580       tools_gl2psCutEdge(&parent->verts[index    1298       tools_gl2psCutEdge(&parent->verts[index0[i]], &parent->verts[index1[i]],
1581                    plane, &child->verts[i]);     1299                    plane, &child->verts[i]);
1582     }                                            1300     }
1583   }                                              1301   }
1584 }                                                1302 }
1585                                                  1303 
1586 inline void tools_gl2psAddIndex(tools_GLshort << 1304 static void tools_gl2psAddIndex(tools_GLshort *index0, tools_GLshort *index1, tools_GLshort *nb,
1587                           tools_GLshort i, to    1305                           tools_GLshort i, tools_GLshort j)
1588 {                                                1306 {
1589   tools_GLint k;                                 1307   tools_GLint k;
1590                                                  1308 
1591   for(k = 0; k < *nb; k++){                      1309   for(k = 0; k < *nb; k++){
1592     if((index0[k] == i && index1[k] == j) ||     1310     if((index0[k] == i && index1[k] == j) ||
1593        (index1[k] == i && index0[k] == j)) re    1311        (index1[k] == i && index0[k] == j)) return;
1594   }                                              1312   }
1595   index0[*nb] = i;                               1313   index0[*nb] = i;
1596   index1[*nb] = j;                               1314   index1[*nb] = j;
1597   (*nb)++;                                       1315   (*nb)++;
1598 }                                                1316 }
1599                                                  1317 
1600 inline tools_GLshort tools_gl2psGetIndex(tool << 1318 static tools_GLshort tools_gl2psGetIndex(tools_GLshort i, tools_GLshort num)
1601 {                                                1319 {
1602   return (i < num - 1) ? i + 1 : 0;              1320   return (i < num - 1) ? i + 1 : 0;
1603 }                                                1321 }
1604                                                  1322 
1605 inline tools_GLint tools_gl2psTestSplitPrimit << 1323 static tools_GLint tools_gl2psTestSplitPrimitive(tools_GL2PSprimitive *prim, tools_GL2PSplane plane)
1606 {                                                1324 {
1607   tools_GLint type = TOOLS_GL2PS_COINCIDENT;     1325   tools_GLint type = TOOLS_GL2PS_COINCIDENT;
1608   tools_GLshort i, j;                            1326   tools_GLshort i, j;
1609   tools_GLfloat d[5];                            1327   tools_GLfloat d[5];
1610                                                  1328 
1611   for(i = 0; i < prim->numverts; i++){           1329   for(i = 0; i < prim->numverts; i++){
1612     d[i] = tools_gl2psComparePointPlane(prim-    1330     d[i] = tools_gl2psComparePointPlane(prim->verts[i].xyz, plane);
1613   }                                              1331   }
1614                                                  1332 
1615   if(prim->numverts < 2){                        1333   if(prim->numverts < 2){
1616     return 0;                                    1334     return 0;
1617   }                                              1335   }
1618   else{                                          1336   else{
1619     for(i = 0; i < prim->numverts; i++){         1337     for(i = 0; i < prim->numverts; i++){
1620       j = tools_gl2psGetIndex(i, prim->numver    1338       j = tools_gl2psGetIndex(i, prim->numverts);
1621       if(d[j] > TOOLS_GL2PS_EPSILON){            1339       if(d[j] > TOOLS_GL2PS_EPSILON){
1622         if(type == TOOLS_GL2PS_COINCIDENT)       1340         if(type == TOOLS_GL2PS_COINCIDENT)      type = TOOLS_GL2PS_IN_BACK_OF;
1623         else if(type != TOOLS_GL2PS_IN_BACK_O    1341         else if(type != TOOLS_GL2PS_IN_BACK_OF) return 1;
1624         if(d[i] < -TOOLS_GL2PS_EPSILON)          1342         if(d[i] < -TOOLS_GL2PS_EPSILON)         return 1;
1625       }                                          1343       }
1626       else if(d[j] < -TOOLS_GL2PS_EPSILON){      1344       else if(d[j] < -TOOLS_GL2PS_EPSILON){
1627         if(type == TOOLS_GL2PS_COINCIDENT)       1345         if(type == TOOLS_GL2PS_COINCIDENT)       type = TOOLS_GL2PS_IN_FRONT_OF;
1628         else if(type != TOOLS_GL2PS_IN_FRONT_    1346         else if(type != TOOLS_GL2PS_IN_FRONT_OF) return 1;
1629         if(d[i] > TOOLS_GL2PS_EPSILON)           1347         if(d[i] > TOOLS_GL2PS_EPSILON)           return 1;
1630       }                                          1348       }
1631     }                                            1349     }
1632   }                                              1350   }
1633   return 0;                                      1351   return 0;
1634 }                                                1352 }
1635                                                  1353 
1636 inline tools_GLint tools_gl2psSplitPrimitive( << 1354 static tools_GLint tools_gl2psSplitPrimitive(tools_GL2PSprimitive *prim, tools_GL2PSplane plane,
1637                                  tools_GL2PSp    1355                                  tools_GL2PSprimitive **front, tools_GL2PSprimitive **back)
1638 {                                                1356 {
1639   tools_GLshort i, j, in = 0, out = 0, in0[5]    1357   tools_GLshort i, j, in = 0, out = 0, in0[5], in1[5], out0[5], out1[5];
1640   tools_GLint type;                              1358   tools_GLint type;
1641   tools_GLfloat d[5] = {0.0};                 << 1359   tools_GLfloat d[5];
1642                                                  1360 
1643   type = TOOLS_GL2PS_COINCIDENT;                 1361   type = TOOLS_GL2PS_COINCIDENT;
1644                                                  1362 
1645   for(i = 0; i < prim->numverts; i++){           1363   for(i = 0; i < prim->numverts; i++){
1646     d[i] = tools_gl2psComparePointPlane(prim-    1364     d[i] = tools_gl2psComparePointPlane(prim->verts[i].xyz, plane);
1647   }                                              1365   }
1648                                                  1366 
1649   switch(prim->type){                            1367   switch(prim->type){
1650   case TOOLS_GL2PS_POINT :                       1368   case TOOLS_GL2PS_POINT :
1651     if(d[0] > TOOLS_GL2PS_EPSILON)       type    1369     if(d[0] > TOOLS_GL2PS_EPSILON)       type = TOOLS_GL2PS_IN_BACK_OF;
1652     else if(d[0] < -TOOLS_GL2PS_EPSILON) type    1370     else if(d[0] < -TOOLS_GL2PS_EPSILON) type = TOOLS_GL2PS_IN_FRONT_OF;
1653     else                           type = TOO    1371     else                           type = TOOLS_GL2PS_COINCIDENT;
1654     break;                                       1372     break;
1655   default :                                      1373   default :
1656     for(i = 0; i < prim->numverts; i++){         1374     for(i = 0; i < prim->numverts; i++){
1657       j = tools_gl2psGetIndex(i, prim->numver    1375       j = tools_gl2psGetIndex(i, prim->numverts);
1658       if(d[j] > TOOLS_GL2PS_EPSILON){            1376       if(d[j] > TOOLS_GL2PS_EPSILON){
1659         if(type == TOOLS_GL2PS_COINCIDENT)       1377         if(type == TOOLS_GL2PS_COINCIDENT)      type = TOOLS_GL2PS_IN_BACK_OF;
1660         else if(type != TOOLS_GL2PS_IN_BACK_O    1378         else if(type != TOOLS_GL2PS_IN_BACK_OF) type = TOOLS_GL2PS_SPANNING;
1661         if(d[i] < -TOOLS_GL2PS_EPSILON){         1379         if(d[i] < -TOOLS_GL2PS_EPSILON){
1662           tools_gl2psAddIndex(in0, in1, &in,     1380           tools_gl2psAddIndex(in0, in1, &in, i, j);
1663           tools_gl2psAddIndex(out0, out1, &ou    1381           tools_gl2psAddIndex(out0, out1, &out, i, j);
1664           type = TOOLS_GL2PS_SPANNING;           1382           type = TOOLS_GL2PS_SPANNING;
1665         }                                        1383         }
1666         tools_gl2psAddIndex(out0, out1, &out,    1384         tools_gl2psAddIndex(out0, out1, &out, j, -1);
1667       }                                          1385       }
1668       else if(d[j] < -TOOLS_GL2PS_EPSILON){      1386       else if(d[j] < -TOOLS_GL2PS_EPSILON){
1669         if(type == TOOLS_GL2PS_COINCIDENT)       1387         if(type == TOOLS_GL2PS_COINCIDENT)       type = TOOLS_GL2PS_IN_FRONT_OF;
1670         else if(type != TOOLS_GL2PS_IN_FRONT_    1388         else if(type != TOOLS_GL2PS_IN_FRONT_OF) type = TOOLS_GL2PS_SPANNING;
1671         if(d[i] > TOOLS_GL2PS_EPSILON){          1389         if(d[i] > TOOLS_GL2PS_EPSILON){
1672           tools_gl2psAddIndex(in0, in1, &in,     1390           tools_gl2psAddIndex(in0, in1, &in, i, j);
1673           tools_gl2psAddIndex(out0, out1, &ou    1391           tools_gl2psAddIndex(out0, out1, &out, i, j);
1674           type = TOOLS_GL2PS_SPANNING;           1392           type = TOOLS_GL2PS_SPANNING;
1675         }                                        1393         }
1676         tools_gl2psAddIndex(in0, in1, &in, j,    1394         tools_gl2psAddIndex(in0, in1, &in, j, -1);
1677       }                                          1395       }
1678       else{                                      1396       else{
1679         tools_gl2psAddIndex(in0, in1, &in, j,    1397         tools_gl2psAddIndex(in0, in1, &in, j, -1);
1680         tools_gl2psAddIndex(out0, out1, &out,    1398         tools_gl2psAddIndex(out0, out1, &out, j, -1);
1681       }                                          1399       }
1682     }                                            1400     }
1683     break;                                       1401     break;
1684   }                                              1402   }
1685                                                  1403 
1686   if(type == TOOLS_GL2PS_SPANNING){              1404   if(type == TOOLS_GL2PS_SPANNING){
1687     *back = (tools_GL2PSprimitive*)tools_gl2p    1405     *back = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
1688     *front = (tools_GL2PSprimitive*)tools_gl2    1406     *front = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
1689     tools_gl2psCreateSplitPrimitive(prim, pla    1407     tools_gl2psCreateSplitPrimitive(prim, plane, *back, out, out0, out1);
1690     tools_gl2psCreateSplitPrimitive(prim, pla    1408     tools_gl2psCreateSplitPrimitive(prim, plane, *front, in, in0, in1);
1691   }                                              1409   }
1692                                                  1410 
1693   return type;                                   1411   return type;
1694 }                                                1412 }
1695                                                  1413 
1696 inline void tools_gl2psDivideQuad(tools_GL2PS << 1414 static void tools_gl2psDivideQuad(tools_GL2PSprimitive *quad,
1697                             tools_GL2PSprimit    1415                             tools_GL2PSprimitive **t1, tools_GL2PSprimitive **t2)
1698 {                                                1416 {
1699   *t1 = (tools_GL2PSprimitive*)tools_gl2psMal    1417   *t1 = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
1700   *t2 = (tools_GL2PSprimitive*)tools_gl2psMal    1418   *t2 = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
1701   (*t1)->type = (*t2)->type = TOOLS_GL2PS_TRI    1419   (*t1)->type = (*t2)->type = TOOLS_GL2PS_TRIANGLE;
1702   (*t1)->numverts = (*t2)->numverts = 3;         1420   (*t1)->numverts = (*t2)->numverts = 3;
1703   (*t1)->culled = (*t2)->culled = quad->culle    1421   (*t1)->culled = (*t2)->culled = quad->culled;
1704   (*t1)->offset = (*t2)->offset = quad->offse    1422   (*t1)->offset = (*t2)->offset = quad->offset;
1705   (*t1)->ofactor = (*t2)->ofactor = quad->ofa    1423   (*t1)->ofactor = (*t2)->ofactor = quad->ofactor;
1706   (*t1)->ounits = (*t2)->ounits = quad->ounit    1424   (*t1)->ounits = (*t2)->ounits = quad->ounits;
1707   (*t1)->pattern = (*t2)->pattern = quad->pat    1425   (*t1)->pattern = (*t2)->pattern = quad->pattern;
1708   (*t1)->factor = (*t2)->factor = quad->facto    1426   (*t1)->factor = (*t2)->factor = quad->factor;
1709   (*t1)->width = (*t2)->width = quad->width;     1427   (*t1)->width = (*t2)->width = quad->width;
1710   (*t1)->linecap = (*t2)->linecap = quad->lin    1428   (*t1)->linecap = (*t2)->linecap = quad->linecap;
1711   (*t1)->linejoin = (*t2)->linejoin = quad->l    1429   (*t1)->linejoin = (*t2)->linejoin = quad->linejoin;
1712   (*t1)->verts = (tools_GL2PSvertex*)tools_gl    1430   (*t1)->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(3 * sizeof(tools_GL2PSvertex));
1713   (*t2)->verts = (tools_GL2PSvertex*)tools_gl    1431   (*t2)->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(3 * sizeof(tools_GL2PSvertex));
1714   (*t1)->verts[0] = quad->verts[0];              1432   (*t1)->verts[0] = quad->verts[0];
1715   (*t1)->verts[1] = quad->verts[1];              1433   (*t1)->verts[1] = quad->verts[1];
1716   (*t1)->verts[2] = quad->verts[2];              1434   (*t1)->verts[2] = quad->verts[2];
1717   (*t1)->boundary = ((quad->boundary & 1) ? 1    1435   (*t1)->boundary = ((quad->boundary & 1) ? 1 : 0) | ((quad->boundary & 2) ? 2 : 0);
1718   (*t2)->verts[0] = quad->verts[0];              1436   (*t2)->verts[0] = quad->verts[0];
1719   (*t2)->verts[1] = quad->verts[2];              1437   (*t2)->verts[1] = quad->verts[2];
1720   (*t2)->verts[2] = quad->verts[3];              1438   (*t2)->verts[2] = quad->verts[3];
1721   (*t2)->boundary = ((quad->boundary & 4) ? 2    1439   (*t2)->boundary = ((quad->boundary & 4) ? 2 : 0) | ((quad->boundary & 8) ? 4 : 0);
1722 }                                                1440 }
1723                                                  1441 
1724 inline int tools_gl2psCompareDepth(const void << 1442 static int tools_gl2psCompareDepth(const void *a, const void *b)
1725 {                                                1443 {
1726   const tools_GL2PSprimitive *q, *w;             1444   const tools_GL2PSprimitive *q, *w;
1727   tools_GLfloat dq = 0.0F, dw = 0.0F, diff;      1445   tools_GLfloat dq = 0.0F, dw = 0.0F, diff;
1728   int i;                                         1446   int i;
1729                                                  1447 
1730   q = *(const tools_GL2PSprimitive* const*)a;    1448   q = *(const tools_GL2PSprimitive* const*)a;
1731   w = *(const tools_GL2PSprimitive* const*)b;    1449   w = *(const tools_GL2PSprimitive* const*)b;
1732                                                  1450 
1733   for(i = 0; i < q->numverts; i++){              1451   for(i = 0; i < q->numverts; i++){
1734     dq += q->verts[i].xyz[2];                    1452     dq += q->verts[i].xyz[2];
1735   }                                              1453   }
1736   dq /= (tools_GLfloat)q->numverts;              1454   dq /= (tools_GLfloat)q->numverts;
1737                                                  1455 
1738   for(i = 0; i < w->numverts; i++){              1456   for(i = 0; i < w->numverts; i++){
1739     dw += w->verts[i].xyz[2];                    1457     dw += w->verts[i].xyz[2];
1740   }                                              1458   }
1741   dw /= (tools_GLfloat)w->numverts;              1459   dw /= (tools_GLfloat)w->numverts;
1742                                                  1460 
1743   diff = dq - dw;                                1461   diff = dq - dw;
1744   if(diff > 0.){                                 1462   if(diff > 0.){
1745     return -1;                                   1463     return -1;
1746   }                                              1464   }
1747   else if(diff < 0.){                            1465   else if(diff < 0.){
1748     return 1;                                    1466     return 1;
1749   }                                              1467   }
1750   else{                                          1468   else{
1751     /* Ensure that initial ordering is preser << 1469     return 0;
1752     if(q->sortid==w->sortid) return 0;  //G.B << 
1753     return q->sortid < w->sortid ? -1 : 1;    << 
1754   }                                              1470   }
1755 }                                                1471 }
1756                                                  1472 
1757 inline int tools_gl2psTrianglesFirst(const vo << 1473 static int tools_gl2psTrianglesFirst(const void *a, const void *b)
1758 {                                                1474 {
1759   const tools_GL2PSprimitive *q, *w;             1475   const tools_GL2PSprimitive *q, *w;
1760                                                  1476 
1761   q = *(const tools_GL2PSprimitive* const*)a;    1477   q = *(const tools_GL2PSprimitive* const*)a;
1762   w = *(const tools_GL2PSprimitive* const*)b;    1478   w = *(const tools_GL2PSprimitive* const*)b;
1763   if(q->type==w->type) return 0;  //G.Barrand << 
1764   return (q->type < w->type ? 1 : -1);           1479   return (q->type < w->type ? 1 : -1);
1765 }                                                1480 }
1766                                                  1481 
1767 inline tools_GLint tools_gl2psFindRoot(tools_ << 1482 static tools_GLint tools_gl2psFindRoot(tools_GL2PSlist *primitives, tools_GL2PSprimitive **root)
1768 {                                                1483 {
1769   tools_GLint i, j, count, best = 1000000, id    1484   tools_GLint i, j, count, best = 1000000, idx = 0;
1770   tools_GL2PSprimitive *prim1, *prim2;           1485   tools_GL2PSprimitive *prim1, *prim2;
1771   tools_GL2PSplane plane;                        1486   tools_GL2PSplane plane;
1772   tools_GLint maxp;                              1487   tools_GLint maxp;
1773                                                  1488 
1774   if(!tools_gl2psListNbr(primitives)){           1489   if(!tools_gl2psListNbr(primitives)){
1775     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Cannot    1490     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Cannot fint root in empty primitive list");
1776     *root = 0; /*G.Barrand: to quiet gcc.*/   << 
1777     return 0;                                    1491     return 0;
1778   }                                              1492   }
1779                                                  1493 
1780   *root = *(tools_GL2PSprimitive**)tools_gl2p    1494   *root = *(tools_GL2PSprimitive**)tools_gl2psListPointer(primitives, 0);
1781                                                  1495 
1782   if(gl2ps->options & TOOLS_GL2PS_BEST_ROOT){ << 1496   if(tools_gl2ps_context->options & TOOLS_GL2PS_BEST_ROOT){
1783     maxp = tools_gl2psListNbr(primitives);       1497     maxp = tools_gl2psListNbr(primitives);
1784     if(maxp > gl2ps->maxbestroot){            << 1498     if(maxp > tools_gl2ps_context->maxbestroot){
1785       maxp = gl2ps->maxbestroot;              << 1499       maxp = tools_gl2ps_context->maxbestroot;
1786     }                                            1500     }
1787     for(i = 0; i < maxp; i++){                   1501     for(i = 0; i < maxp; i++){
1788       prim1 = *(tools_GL2PSprimitive**)tools_    1502       prim1 = *(tools_GL2PSprimitive**)tools_gl2psListPointer(primitives, i);
1789       tools_gl2psGetPlane(prim1, plane);         1503       tools_gl2psGetPlane(prim1, plane);
1790       count = 0;                                 1504       count = 0;
1791       for(j = 0; j < tools_gl2psListNbr(primi    1505       for(j = 0; j < tools_gl2psListNbr(primitives); j++){
1792         if(j != i){                              1506         if(j != i){
1793           prim2 = *(tools_GL2PSprimitive**)to    1507           prim2 = *(tools_GL2PSprimitive**)tools_gl2psListPointer(primitives, j);
1794           count += tools_gl2psTestSplitPrimit    1508           count += tools_gl2psTestSplitPrimitive(prim2, plane);
1795         }                                        1509         }
1796         if(count > best) break;                  1510         if(count > best) break;
1797       }                                          1511       }
1798       if(count < best){                          1512       if(count < best){
1799         best = count;                            1513         best = count;
1800         idx = i;                                 1514         idx = i;
1801         *root = prim1;                           1515         *root = prim1;
1802         if(!count) return idx;                   1516         if(!count) return idx;
1803       }                                          1517       }
1804     }                                            1518     }
1805     /* if(index) tools_gl2psMsg(TOOLS_GL2PS_I    1519     /* if(index) tools_gl2psMsg(TOOLS_GL2PS_INFO, "TOOLS_GL2PS_BEST_ROOT was worth it: %d", index); */
1806     return idx;                                  1520     return idx;
1807   }                                              1521   }
1808   else{                                          1522   else{
1809     return 0;                                    1523     return 0;
1810   }                                              1524   }
1811 }                                                1525 }
1812                                                  1526 
1813 inline void tools_gl2psFreeImagemap(tools_GL2 << 1527 static void tools_tools_gl2psFreeImagemap(tools_GL2PSimagemap *list)
1814 {                                                1528 {
1815   tools_GL2PSimagemap *next;                     1529   tools_GL2PSimagemap *next;
1816   while(list != NULL){                           1530   while(list != NULL){
1817     next = list->next;                           1531     next = list->next;
1818     tools_gl2psFree(list->image->pixels);        1532     tools_gl2psFree(list->image->pixels);
1819     tools_gl2psFree(list->image);                1533     tools_gl2psFree(list->image);
1820     tools_gl2psFree(list);                       1534     tools_gl2psFree(list);
1821     list = next;                                 1535     list = next;
1822   }                                              1536   }
1823 }                                                1537 }
1824                                                  1538 
1825 inline void tools_gl2psFreePrimitive(void *da << 1539 static void tools_tools_gl2psFreePrimitive(void *data)
1826 {                                                1540 {
1827   tools_GL2PSprimitive *q;                       1541   tools_GL2PSprimitive *q;
1828                                                  1542 
1829   q = *(tools_GL2PSprimitive**)data;             1543   q = *(tools_GL2PSprimitive**)data;
1830   tools_gl2psFree(q->verts);                     1544   tools_gl2psFree(q->verts);
1831   if(q->type == TOOLS_GL2PS_TEXT || q->type =    1545   if(q->type == TOOLS_GL2PS_TEXT || q->type == TOOLS_GL2PS_SPECIAL){
1832     tools_gl2psFreeText(q->data.text);        << 1546     tools_tools_gl2psFreeText(q->data.text);
1833   }                                              1547   }
1834   else if(q->type == TOOLS_GL2PS_PIXMAP){        1548   else if(q->type == TOOLS_GL2PS_PIXMAP){
1835     tools_gl2psFreePixmap(q->data.image);     << 1549     tools_tools_gl2psFreePixmap(q->data.image);
1836   }                                              1550   }
1837   tools_gl2psFree(q);                            1551   tools_gl2psFree(q);
1838 }                                                1552 }
1839                                                  1553 
1840 inline void tools_gl2psAddPrimitiveInList(too << 1554 static void tools_gl2psAddPrimitiveInList(tools_GL2PSprimitive *prim, tools_GL2PSlist *list)
1841 {                                                1555 {
1842   tools_GL2PSprimitive *t1, *t2;                 1556   tools_GL2PSprimitive *t1, *t2;
1843                                                  1557 
1844   if(prim->type != TOOLS_GL2PS_QUADRANGLE){      1558   if(prim->type != TOOLS_GL2PS_QUADRANGLE){
1845     tools_gl2psListAdd(list, &prim);             1559     tools_gl2psListAdd(list, &prim);
1846   }                                              1560   }
1847   else{                                          1561   else{
1848     tools_gl2psDivideQuad(prim, &t1, &t2);       1562     tools_gl2psDivideQuad(prim, &t1, &t2);
1849     tools_gl2psListAdd(list, &t1);               1563     tools_gl2psListAdd(list, &t1);
1850     tools_gl2psListAdd(list, &t2);               1564     tools_gl2psListAdd(list, &t2);
1851     tools_gl2psFreePrimitive(&prim);          << 1565     tools_tools_gl2psFreePrimitive(&prim);
1852   }                                              1566   }
1853                                                  1567 
1854 }                                                1568 }
1855                                                  1569 
1856 inline void tools_gl2psFreeBspTree(tools_GL2P << 1570 static void tools_tools_gl2psFreeBspTree(tools_GL2PSbsptree **tree)
1857 {                                                1571 {
1858   if(*tree){                                     1572   if(*tree){
1859     if((*tree)->back) tools_gl2psFreeBspTree( << 1573     if((*tree)->back) tools_tools_gl2psFreeBspTree(&(*tree)->back);
1860     if((*tree)->primitives){                     1574     if((*tree)->primitives){
1861       tools_gl2psListAction((*tree)->primitiv << 1575       tools_gl2psListAction((*tree)->primitives, tools_tools_gl2psFreePrimitive);
1862       tools_gl2psListDelete((*tree)->primitiv    1576       tools_gl2psListDelete((*tree)->primitives);
1863     }                                            1577     }
1864     if((*tree)->front) tools_gl2psFreeBspTree << 1578     if((*tree)->front) tools_tools_gl2psFreeBspTree(&(*tree)->front);
1865     tools_gl2psFree(*tree);                      1579     tools_gl2psFree(*tree);
1866     *tree = NULL;                                1580     *tree = NULL;
1867   }                                              1581   }
1868 }                                                1582 }
1869                                                  1583 
1870 inline tools_GLboolean tools_gl2psGreater(too << 1584 static tools_GLboolean tools_gl2psGreater(tools_GLfloat f1, tools_GLfloat f2)
1871 {                                                1585 {
1872   if(f1 > f2) return TOOLS_GL_TRUE;              1586   if(f1 > f2) return TOOLS_GL_TRUE;
1873   else return TOOLS_GL_FALSE;                    1587   else return TOOLS_GL_FALSE;
1874 }                                                1588 }
1875                                                  1589 
1876 inline tools_GLboolean tools_gl2psLess(tools_ << 1590 static tools_GLboolean tools_gl2psLess(tools_GLfloat f1, tools_GLfloat f2)
1877 {                                                1591 {
1878   if(f1 < f2) return TOOLS_GL_TRUE;              1592   if(f1 < f2) return TOOLS_GL_TRUE;
1879   else return TOOLS_GL_FALSE;                    1593   else return TOOLS_GL_FALSE;
1880 }                                                1594 }
1881                                                  1595 
1882 inline void tools_gl2psBuildBspTree(tools_GL2 << 1596 static void tools_gl2psBuildBspTree(tools_GL2PSbsptree *tree, tools_GL2PSlist *primitives)
1883 {                                                1597 {
1884   tools_GL2PSprimitive *prim = NULL, *frontpr << 1598   tools_GL2PSprimitive *prim, *frontprim = NULL, *backprim = NULL;
1885   tools_GL2PSlist *frontlist, *backlist;         1599   tools_GL2PSlist *frontlist, *backlist;
1886   tools_GLint i, idx;                            1600   tools_GLint i, idx;
1887                                                  1601 
1888   tree->front = NULL;                            1602   tree->front = NULL;
1889   tree->back = NULL;                             1603   tree->back = NULL;
1890   tree->primitives = tools_gl2psListCreate(1,    1604   tree->primitives = tools_gl2psListCreate(1, 2, sizeof(tools_GL2PSprimitive*));
1891   idx = tools_gl2psFindRoot(gl2ps, primitives << 1605   idx = tools_gl2psFindRoot(primitives, &prim);
1892   tools_gl2psGetPlane(prim, tree->plane);        1606   tools_gl2psGetPlane(prim, tree->plane);
1893   tools_gl2psAddPrimitiveInList(prim, tree->p    1607   tools_gl2psAddPrimitiveInList(prim, tree->primitives);
1894                                                  1608 
1895   frontlist = tools_gl2psListCreate(1, 2, siz    1609   frontlist = tools_gl2psListCreate(1, 2, sizeof(tools_GL2PSprimitive*));
1896   backlist = tools_gl2psListCreate(1, 2, size    1610   backlist = tools_gl2psListCreate(1, 2, sizeof(tools_GL2PSprimitive*));
1897                                                  1611 
1898   for(i = 0; i < tools_gl2psListNbr(primitive    1612   for(i = 0; i < tools_gl2psListNbr(primitives); i++){
1899     if(i != idx){                                1613     if(i != idx){
1900       prim = *(tools_GL2PSprimitive**)tools_g    1614       prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(primitives,i);
1901       switch(tools_gl2psSplitPrimitive(prim,     1615       switch(tools_gl2psSplitPrimitive(prim, tree->plane, &frontprim, &backprim)){
1902       case TOOLS_GL2PS_COINCIDENT:               1616       case TOOLS_GL2PS_COINCIDENT:
1903         tools_gl2psAddPrimitiveInList(prim, t    1617         tools_gl2psAddPrimitiveInList(prim, tree->primitives);
1904         break;                                   1618         break;
1905       case TOOLS_GL2PS_IN_BACK_OF:               1619       case TOOLS_GL2PS_IN_BACK_OF:
1906         tools_gl2psAddPrimitiveInList(prim, b    1620         tools_gl2psAddPrimitiveInList(prim, backlist);
1907         break;                                   1621         break;
1908       case TOOLS_GL2PS_IN_FRONT_OF:              1622       case TOOLS_GL2PS_IN_FRONT_OF:
1909         tools_gl2psAddPrimitiveInList(prim, f    1623         tools_gl2psAddPrimitiveInList(prim, frontlist);
1910         break;                                   1624         break;
1911       case TOOLS_GL2PS_SPANNING:                 1625       case TOOLS_GL2PS_SPANNING:
1912         tools_gl2psAddPrimitiveInList(backpri    1626         tools_gl2psAddPrimitiveInList(backprim, backlist);
1913         tools_gl2psAddPrimitiveInList(frontpr    1627         tools_gl2psAddPrimitiveInList(frontprim, frontlist);
1914         tools_gl2psFreePrimitive(&prim);      << 1628         tools_tools_gl2psFreePrimitive(&prim);
1915         break;                                   1629         break;
1916       }                                          1630       }
1917     }                                            1631     }
1918   }                                              1632   }
1919                                                  1633 
1920   if(tools_gl2psListNbr(tree->primitives)){      1634   if(tools_gl2psListNbr(tree->primitives)){
1921     tools_gl2psListSort(gl2ps, tree->primitiv << 1635     tools_gl2psListSort(tree->primitives, tools_gl2psTrianglesFirst);
1922   }                                              1636   }
1923                                                  1637 
1924   if(tools_gl2psListNbr(frontlist)){             1638   if(tools_gl2psListNbr(frontlist)){
1925     tools_gl2psListSort(gl2ps, frontlist, too << 1639     tools_gl2psListSort(frontlist, tools_gl2psTrianglesFirst);
1926     tree->front = (tools_GL2PSbsptree*)tools_    1640     tree->front = (tools_GL2PSbsptree*)tools_gl2psMalloc(sizeof(tools_GL2PSbsptree));
1927     tools_gl2psBuildBspTree(gl2ps, tree->fron << 1641     tools_gl2psBuildBspTree(tree->front, frontlist);
1928   }                                              1642   }
1929   else{                                          1643   else{
1930     tools_gl2psListDelete(frontlist);            1644     tools_gl2psListDelete(frontlist);
1931   }                                              1645   }
1932                                                  1646 
1933   if(tools_gl2psListNbr(backlist)){              1647   if(tools_gl2psListNbr(backlist)){
1934     tools_gl2psListSort(gl2ps, backlist, tool << 1648     tools_gl2psListSort(backlist, tools_gl2psTrianglesFirst);
1935     tree->back = (tools_GL2PSbsptree*)tools_g    1649     tree->back = (tools_GL2PSbsptree*)tools_gl2psMalloc(sizeof(tools_GL2PSbsptree));
1936     tools_gl2psBuildBspTree(gl2ps, tree->back << 1650     tools_gl2psBuildBspTree(tree->back, backlist);
1937   }                                              1651   }
1938   else{                                          1652   else{
1939     tools_gl2psListDelete(backlist);             1653     tools_gl2psListDelete(backlist);
1940   }                                              1654   }
1941                                                  1655 
1942   tools_gl2psListDelete(primitives);             1656   tools_gl2psListDelete(primitives);
1943 }                                                1657 }
1944                                                  1658 
1945 inline void tools_gl2psTraverseBspTree(tools_ << 1659 static void tools_gl2psTraverseBspTree(tools_GL2PSbsptree *tree, tools_GL2PSxyz eye, tools_GLfloat epsilon,
1946                                  tools_GLbool    1660                                  tools_GLboolean (*compare)(tools_GLfloat f1, tools_GLfloat f2),
1947                                  void (*actio << 1661                                  void (*action)(void *data), int inverse)
1948 {                                                1662 {
1949   tools_GLfloat result;                          1663   tools_GLfloat result;
1950                                                  1664 
1951   if(!tree) return;                              1665   if(!tree) return;
1952                                                  1666 
1953   result = tools_gl2psComparePointPlane(eye,     1667   result = tools_gl2psComparePointPlane(eye, tree->plane);
1954                                                  1668 
1955   if(TOOLS_GL_TRUE == compare(result, epsilon    1669   if(TOOLS_GL_TRUE == compare(result, epsilon)){
1956     tools_gl2psTraverseBspTree(gl2ps, tree->b << 1670     tools_gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, inverse);
1957     if(inverse){                                 1671     if(inverse){
1958       tools_gl2psListActionInverseContext(gl2 << 1672       tools_tools_gl2psListActionInverse(tree->primitives, action);
1959     }                                            1673     }
1960     else{                                        1674     else{
1961       tools_gl2psListActionContext(gl2ps, tre << 1675       tools_gl2psListAction(tree->primitives, action);
1962     }                                            1676     }
1963     tools_gl2psTraverseBspTree(gl2ps, tree->f << 1677     tools_gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, inverse);
1964   }                                              1678   }
1965   else if(TOOLS_GL_TRUE == compare(-epsilon,     1679   else if(TOOLS_GL_TRUE == compare(-epsilon, result)){
1966     tools_gl2psTraverseBspTree(gl2ps, tree->f << 1680     tools_gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, inverse);
1967     if(inverse){                                 1681     if(inverse){
1968       tools_gl2psListActionInverseContext(gl2 << 1682       tools_tools_gl2psListActionInverse(tree->primitives, action);
1969     }                                            1683     }
1970     else{                                        1684     else{
1971       tools_gl2psListActionContext(gl2ps, tre << 1685       tools_gl2psListAction(tree->primitives, action);
1972     }                                            1686     }
1973     tools_gl2psTraverseBspTree(gl2ps, tree->b << 1687     tools_gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, inverse);
1974   }                                              1688   }
1975   else{                                          1689   else{
1976     tools_gl2psTraverseBspTree(gl2ps, tree->f << 1690     tools_gl2psTraverseBspTree(tree->front, eye, epsilon, compare, action, inverse);
1977     tools_gl2psTraverseBspTree(gl2ps, tree->b << 1691     tools_gl2psTraverseBspTree(tree->back, eye, epsilon, compare, action, inverse);
1978   }                                              1692   }
1979 }                                                1693 }
1980                                                  1694 
1981 inline void tools_gl2psRescaleAndOffset(tools << 1695 static void tools_gl2psRescaleAndOffset(void)
1982 {                                                1696 {
1983   tools_GL2PSprimitive *prim;                    1697   tools_GL2PSprimitive *prim;
1984   tools_GLfloat minZ, maxZ, rangeZ, scaleZ;      1698   tools_GLfloat minZ, maxZ, rangeZ, scaleZ;
1985   tools_GLfloat factor, units, area, dZ, dZdX    1699   tools_GLfloat factor, units, area, dZ, dZdX, dZdY, maxdZ;
1986   int i, j;                                      1700   int i, j;
1987                                                  1701 
1988   if(!tools_gl2psListNbr(gl2ps->primitives))  << 1702   if(!tools_gl2psListNbr(tools_gl2ps_context->primitives))
1989     return;                                      1703     return;
1990                                                  1704 
1991   /* get z-buffer range */                       1705   /* get z-buffer range */
1992   prim = *(tools_GL2PSprimitive**)tools_gl2ps << 1706   prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(tools_gl2ps_context->primitives, 0);
1993   minZ = maxZ = prim->verts[0].xyz[2];           1707   minZ = maxZ = prim->verts[0].xyz[2];
1994   for(i = 1; i < prim->numverts; i++){           1708   for(i = 1; i < prim->numverts; i++){
1995     if(prim->verts[i].xyz[2] < minZ) minZ = p    1709     if(prim->verts[i].xyz[2] < minZ) minZ = prim->verts[i].xyz[2];
1996     if(prim->verts[i].xyz[2] > maxZ) maxZ = p    1710     if(prim->verts[i].xyz[2] > maxZ) maxZ = prim->verts[i].xyz[2];
1997   }                                              1711   }
1998   for(i = 1; i < tools_gl2psListNbr(gl2ps->pr << 1712   for(i = 1; i < tools_gl2psListNbr(tools_gl2ps_context->primitives); i++){
1999     prim = *(tools_GL2PSprimitive**)tools_gl2 << 1713     prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(tools_gl2ps_context->primitives, i);
2000     for(j = 0; j < prim->numverts; j++){         1714     for(j = 0; j < prim->numverts; j++){
2001       if(prim->verts[j].xyz[2] < minZ) minZ =    1715       if(prim->verts[j].xyz[2] < minZ) minZ = prim->verts[j].xyz[2];
2002       if(prim->verts[j].xyz[2] > maxZ) maxZ =    1716       if(prim->verts[j].xyz[2] > maxZ) maxZ = prim->verts[j].xyz[2];
2003     }                                            1717     }
2004   }                                              1718   }
2005   rangeZ = (maxZ - minZ);                        1719   rangeZ = (maxZ - minZ);
2006                                                  1720 
2007   /* rescale z-buffer coordinate in [0,TOOLS_    1721   /* rescale z-buffer coordinate in [0,TOOLS_GL2PS_ZSCALE], to make it of
2008      the same order of magnitude as the x and    1722      the same order of magnitude as the x and y coordinates */
2009   scaleZ = TOOLS_GL2PS_ZERO(rangeZ) ? TOOLS_G    1723   scaleZ = TOOLS_GL2PS_ZERO(rangeZ) ? TOOLS_GL2PS_ZSCALE : (TOOLS_GL2PS_ZSCALE / rangeZ);
2010   /* avoid precision loss (we use floats!) */    1724   /* avoid precision loss (we use floats!) */
2011   if(scaleZ > 100000.F) scaleZ = 100000.F;       1725   if(scaleZ > 100000.F) scaleZ = 100000.F;
2012                                                  1726 
2013   /* apply offsets */                            1727   /* apply offsets */
2014   for(i = 0; i < tools_gl2psListNbr(gl2ps->pr << 1728   for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->primitives); i++){
2015     prim = *(tools_GL2PSprimitive**)tools_gl2 << 1729     prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(tools_gl2ps_context->primitives, i);
2016     for(j = 0; j < prim->numverts; j++){         1730     for(j = 0; j < prim->numverts; j++){
2017       prim->verts[j].xyz[2] = (prim->verts[j]    1731       prim->verts[j].xyz[2] = (prim->verts[j].xyz[2] - minZ) * scaleZ;
2018     }                                            1732     }
2019     if((gl2ps->options & TOOLS_GL2PS_SIMPLE_L << 1733     if((tools_gl2ps_context->options & TOOLS_GL2PS_SIMPLE_LINE_OFFSET) &&
2020        (prim->type == TOOLS_GL2PS_LINE)){        1734        (prim->type == TOOLS_GL2PS_LINE)){
2021       if(gl2ps->sort == TOOLS_GL2PS_SIMPLE_SO << 1735       if(tools_gl2ps_context->sort == TOOLS_GL2PS_SIMPLE_SORT){
2022         prim->verts[0].xyz[2] -= TOOLS_GL2PS_    1736         prim->verts[0].xyz[2] -= TOOLS_GL2PS_ZOFFSET_LARGE;
2023         prim->verts[1].xyz[2] -= TOOLS_GL2PS_    1737         prim->verts[1].xyz[2] -= TOOLS_GL2PS_ZOFFSET_LARGE;
2024       }                                          1738       }
2025       else{                                      1739       else{
2026         prim->verts[0].xyz[2] -= TOOLS_GL2PS_    1740         prim->verts[0].xyz[2] -= TOOLS_GL2PS_ZOFFSET;
2027         prim->verts[1].xyz[2] -= TOOLS_GL2PS_    1741         prim->verts[1].xyz[2] -= TOOLS_GL2PS_ZOFFSET;
2028       }                                          1742       }
2029     }                                            1743     }
2030     else if(prim->offset && (prim->type == TO    1744     else if(prim->offset && (prim->type == TOOLS_GL2PS_TRIANGLE)){
2031       factor = prim->ofactor;                    1745       factor = prim->ofactor;
2032       units = prim->ounits;                      1746       units = prim->ounits;
2033       area =                                     1747       area =
2034         (prim->verts[1].xyz[0] - prim->verts[    1748         (prim->verts[1].xyz[0] - prim->verts[0].xyz[0]) *
2035         (prim->verts[2].xyz[1] - prim->verts[    1749         (prim->verts[2].xyz[1] - prim->verts[1].xyz[1]) -
2036         (prim->verts[2].xyz[0] - prim->verts[    1750         (prim->verts[2].xyz[0] - prim->verts[1].xyz[0]) *
2037         (prim->verts[1].xyz[1] - prim->verts[    1751         (prim->verts[1].xyz[1] - prim->verts[0].xyz[1]);
2038       if(!TOOLS_GL2PS_ZERO(area)){               1752       if(!TOOLS_GL2PS_ZERO(area)){
2039         dZdX =                                   1753         dZdX =
2040           ((prim->verts[2].xyz[1] - prim->ver    1754           ((prim->verts[2].xyz[1] - prim->verts[1].xyz[1]) *
2041            (prim->verts[1].xyz[2] - prim->ver    1755            (prim->verts[1].xyz[2] - prim->verts[0].xyz[2]) -
2042            (prim->verts[1].xyz[1] - prim->ver    1756            (prim->verts[1].xyz[1] - prim->verts[0].xyz[1]) *
2043            (prim->verts[2].xyz[2] - prim->ver    1757            (prim->verts[2].xyz[2] - prim->verts[1].xyz[2])) / area;
2044         dZdY =                                   1758         dZdY =
2045           ((prim->verts[1].xyz[0] - prim->ver    1759           ((prim->verts[1].xyz[0] - prim->verts[0].xyz[0]) *
2046            (prim->verts[2].xyz[2] - prim->ver    1760            (prim->verts[2].xyz[2] - prim->verts[1].xyz[2]) -
2047            (prim->verts[2].xyz[0] - prim->ver    1761            (prim->verts[2].xyz[0] - prim->verts[1].xyz[0]) *
2048            (prim->verts[1].xyz[2] - prim->ver    1762            (prim->verts[1].xyz[2] - prim->verts[0].xyz[2])) / area;
2049         maxdZ = (tools_GLfloat)sqrt(dZdX * dZ    1763         maxdZ = (tools_GLfloat)sqrt(dZdX * dZdX + dZdY * dZdY);
2050       }                                          1764       }
2051       else{                                      1765       else{
2052         maxdZ = 0.0F;                            1766         maxdZ = 0.0F;
2053       }                                          1767       }
2054       dZ = factor * maxdZ + units;               1768       dZ = factor * maxdZ + units;
2055       prim->verts[0].xyz[2] += dZ;               1769       prim->verts[0].xyz[2] += dZ;
2056       prim->verts[1].xyz[2] += dZ;               1770       prim->verts[1].xyz[2] += dZ;
2057       prim->verts[2].xyz[2] += dZ;               1771       prim->verts[2].xyz[2] += dZ;
2058     }                                            1772     }
2059   }                                              1773   }
2060 }                                                1774 }
2061                                                  1775 
2062 /********************************************    1776 /*********************************************************************
2063  *                                               1777  *
2064  * 2D sorting routines (for occlusion culling    1778  * 2D sorting routines (for occlusion culling)
2065  *                                               1779  *
2066  ********************************************    1780  *********************************************************************/
2067                                                  1781 
2068 inline tools_GLint tools_gl2psGetPlaneFromPoi << 1782 static tools_GLint tools_tools_gl2psGetPlaneFromPoints(tools_GL2PSxyz a, tools_GL2PSxyz b, tools_GL2PSplane plane)
2069 {                                                1783 {
2070   tools_GLfloat n;                               1784   tools_GLfloat n;
2071                                                  1785 
2072   plane[0] = b[1] - a[1];                        1786   plane[0] = b[1] - a[1];
2073   plane[1] = a[0] - b[0];                        1787   plane[1] = a[0] - b[0];
2074   n = (tools_GLfloat)sqrt(plane[0]*plane[0] +    1788   n = (tools_GLfloat)sqrt(plane[0]*plane[0] + plane[1]*plane[1]);
2075   plane[2] = 0.0F;                               1789   plane[2] = 0.0F;
2076   if(!TOOLS_GL2PS_ZERO(n)){                      1790   if(!TOOLS_GL2PS_ZERO(n)){
2077     plane[0] /= n;                               1791     plane[0] /= n;
2078     plane[1] /= n;                               1792     plane[1] /= n;
2079     plane[3] = -plane[0]*a[0]-plane[1]*a[1];     1793     plane[3] = -plane[0]*a[0]-plane[1]*a[1];
2080     return 1;                                    1794     return 1;
2081   }                                              1795   }
2082   else{                                          1796   else{
2083     plane[0] = -1.0F;                            1797     plane[0] = -1.0F;
2084     plane[1] = 0.0F;                             1798     plane[1] = 0.0F;
2085     plane[3] = a[0];                             1799     plane[3] = a[0];
2086     return 0;                                    1800     return 0;
2087   }                                              1801   }
2088 }                                                1802 }
2089                                                  1803 
2090 inline void tools_gl2psFreeBspImageTree(tools << 1804 static void tools_tools_gl2psFreeBspImageTree(tools_GL2PSbsptree2d **tree)
2091 {                                                1805 {
2092   if(*tree){                                     1806   if(*tree){
2093     if((*tree)->back)  tools_gl2psFreeBspImag << 1807     if((*tree)->back)  tools_tools_gl2psFreeBspImageTree(&(*tree)->back);
2094     if((*tree)->front) tools_gl2psFreeBspImag << 1808     if((*tree)->front) tools_tools_gl2psFreeBspImageTree(&(*tree)->front);
2095     tools_gl2psFree(*tree);                      1809     tools_gl2psFree(*tree);
2096     *tree = NULL;                                1810     *tree = NULL;
2097   }                                              1811   }
2098 }                                                1812 }
2099                                                  1813 
2100 inline tools_GLint tools_gl2psCheckPoint(tool << 1814 static tools_GLint tools_gl2psCheckPoint(tools_GL2PSxyz point, tools_GL2PSplane plane)
2101 {                                                1815 {
2102   tools_GLfloat pt_dis;                          1816   tools_GLfloat pt_dis;
2103                                                  1817 
2104   pt_dis = tools_gl2psComparePointPlane(point    1818   pt_dis = tools_gl2psComparePointPlane(point, plane);
2105   if(pt_dis > TOOLS_GL2PS_EPSILON)        ret    1819   if(pt_dis > TOOLS_GL2PS_EPSILON)        return TOOLS_GL2PS_POINT_INFRONT;
2106   else if(pt_dis < -TOOLS_GL2PS_EPSILON)  ret    1820   else if(pt_dis < -TOOLS_GL2PS_EPSILON)  return TOOLS_GL2PS_POINT_BACK;
2107   else                              return TO    1821   else                              return TOOLS_GL2PS_POINT_COINCIDENT;
2108 }                                                1822 }
2109                                                  1823 
2110 inline void tools_gl2psAddPlanesInBspTreeImag << 1824 static void tools_gl2psAddPlanesInBspTreeImage(tools_GL2PSprimitive *prim,
2111                                          tool    1825                                          tools_GL2PSbsptree2d **tree)
2112 {                                                1826 {
2113   tools_GLint ret = 0;                           1827   tools_GLint ret = 0;
2114   tools_GLint i;                                 1828   tools_GLint i;
2115   tools_GLint offset = 0;                        1829   tools_GLint offset = 0;
2116   tools_GL2PSbsptree2d *head = NULL, *cur = N    1830   tools_GL2PSbsptree2d *head = NULL, *cur = NULL;
2117                                                  1831 
2118   if((*tree == NULL) && (prim->numverts > 2))    1832   if((*tree == NULL) && (prim->numverts > 2)){
2119     /* don't cull if transparent                 1833     /* don't cull if transparent
2120     for(i = 0; i < prim->numverts - 1; i++)      1834     for(i = 0; i < prim->numverts - 1; i++)
2121       if(prim->verts[i].rgba[3] < 1.0F) retur    1835       if(prim->verts[i].rgba[3] < 1.0F) return;
2122     */                                           1836     */
2123     head = (tools_GL2PSbsptree2d*)tools_gl2ps    1837     head = (tools_GL2PSbsptree2d*)tools_gl2psMalloc(sizeof(tools_GL2PSbsptree2d));
2124     for(i = 0; i < prim->numverts-1; i++){       1838     for(i = 0; i < prim->numverts-1; i++){
2125       if(!tools_gl2psGetPlaneFromPoints(prim- << 1839       if(!tools_tools_gl2psGetPlaneFromPoints(prim->verts[i].xyz,
2126                                   prim->verts    1840                                   prim->verts[i+1].xyz,
2127                                   head->plane    1841                                   head->plane)){
2128         if(prim->numverts-i > 3){                1842         if(prim->numverts-i > 3){
2129           offset++;                              1843           offset++;
2130         }                                        1844         }
2131         else{                                    1845         else{
2132           tools_gl2psFree(head);                 1846           tools_gl2psFree(head);
2133           return;                                1847           return;
2134         }                                        1848         }
2135       }                                          1849       }
2136       else{                                      1850       else{
2137         break;                                   1851         break;
2138       }                                          1852       }
2139     }                                            1853     }
2140     head->back = NULL;                           1854     head->back = NULL;
2141     head->front = NULL;                          1855     head->front = NULL;
2142     for(i = 2+offset; i < prim->numverts; i++    1856     for(i = 2+offset; i < prim->numverts; i++){
2143       ret = tools_gl2psCheckPoint(prim->verts    1857       ret = tools_gl2psCheckPoint(prim->verts[i].xyz, head->plane);
2144       if(ret != TOOLS_GL2PS_POINT_COINCIDENT)    1858       if(ret != TOOLS_GL2PS_POINT_COINCIDENT) break;
2145     }                                            1859     }
2146     switch(ret){                                 1860     switch(ret){
2147     case TOOLS_GL2PS_POINT_INFRONT :             1861     case TOOLS_GL2PS_POINT_INFRONT :
2148       cur = head;                                1862       cur = head;
2149       for(i = 1+offset; i < prim->numverts-1;    1863       for(i = 1+offset; i < prim->numverts-1; i++){
2150         if(cur->front == NULL){                  1864         if(cur->front == NULL){
2151           cur->front = (tools_GL2PSbsptree2d*    1865           cur->front = (tools_GL2PSbsptree2d*)tools_gl2psMalloc(sizeof(tools_GL2PSbsptree2d));
2152         }                                        1866         }
2153         if(tools_gl2psGetPlaneFromPoints(prim << 1867         if(tools_tools_gl2psGetPlaneFromPoints(prim->verts[i].xyz,
2154                                    prim->vert    1868                                    prim->verts[i+1].xyz,
2155                                    cur->front    1869                                    cur->front->plane)){
2156           cur = cur->front;                      1870           cur = cur->front;
2157           cur->front = NULL;                     1871           cur->front = NULL;
2158           cur->back = NULL;                      1872           cur->back = NULL;
2159         }                                        1873         }
2160       }                                          1874       }
2161       if(cur->front == NULL){                    1875       if(cur->front == NULL){
2162         cur->front = (tools_GL2PSbsptree2d*)t    1876         cur->front = (tools_GL2PSbsptree2d*)tools_gl2psMalloc(sizeof(tools_GL2PSbsptree2d));
2163       }                                          1877       }
2164       if(tools_gl2psGetPlaneFromPoints(prim-> << 1878       if(tools_tools_gl2psGetPlaneFromPoints(prim->verts[i].xyz,
2165                                  prim->verts[    1879                                  prim->verts[offset].xyz,
2166                                  cur->front->    1880                                  cur->front->plane)){
2167         cur->front->front = NULL;                1881         cur->front->front = NULL;
2168         cur->front->back = NULL;                 1882         cur->front->back = NULL;
2169       }                                          1883       }
2170       else{                                      1884       else{
2171         tools_gl2psFree(cur->front);             1885         tools_gl2psFree(cur->front);
2172         cur->front = NULL;                       1886         cur->front = NULL;
2173       }                                          1887       }
2174       break;                                     1888       break;
2175     case TOOLS_GL2PS_POINT_BACK :                1889     case TOOLS_GL2PS_POINT_BACK :
2176       for(i = 0; i < 4; i++){                    1890       for(i = 0; i < 4; i++){
2177         head->plane[i] = -head->plane[i];        1891         head->plane[i] = -head->plane[i];
2178       }                                          1892       }
2179       cur = head;                                1893       cur = head;
2180       for(i = 1+offset; i < prim->numverts-1;    1894       for(i = 1+offset; i < prim->numverts-1; i++){
2181         if(cur->front == NULL){                  1895         if(cur->front == NULL){
2182           cur->front = (tools_GL2PSbsptree2d*    1896           cur->front = (tools_GL2PSbsptree2d*)tools_gl2psMalloc(sizeof(tools_GL2PSbsptree2d));
2183         }                                        1897         }
2184         if(tools_gl2psGetPlaneFromPoints(prim << 1898         if(tools_tools_gl2psGetPlaneFromPoints(prim->verts[i+1].xyz,
2185                                    prim->vert    1899                                    prim->verts[i].xyz,
2186                                    cur->front    1900                                    cur->front->plane)){
2187           cur = cur->front;                      1901           cur = cur->front;
2188           cur->front = NULL;                     1902           cur->front = NULL;
2189           cur->back = NULL;                      1903           cur->back = NULL;
2190         }                                        1904         }
2191       }                                          1905       }
2192       if(cur->front == NULL){                    1906       if(cur->front == NULL){
2193         cur->front = (tools_GL2PSbsptree2d*)t    1907         cur->front = (tools_GL2PSbsptree2d*)tools_gl2psMalloc(sizeof(tools_GL2PSbsptree2d));
2194       }                                          1908       }
2195       if(tools_gl2psGetPlaneFromPoints(prim-> << 1909       if(tools_tools_gl2psGetPlaneFromPoints(prim->verts[offset].xyz,
2196                                  prim->verts[    1910                                  prim->verts[i].xyz,
2197                                  cur->front->    1911                                  cur->front->plane)){
2198         cur->front->front = NULL;                1912         cur->front->front = NULL;
2199         cur->front->back = NULL;                 1913         cur->front->back = NULL;
2200       }                                          1914       }
2201       else{                                      1915       else{
2202         tools_gl2psFree(cur->front);             1916         tools_gl2psFree(cur->front);
2203         cur->front = NULL;                       1917         cur->front = NULL;
2204       }                                          1918       }
2205       break;                                     1919       break;
2206     default:                                     1920     default:
2207       tools_gl2psFree(head);                     1921       tools_gl2psFree(head);
2208       return;                                    1922       return;
2209     }                                            1923     }
2210     (*tree) = head;                              1924     (*tree) = head;
2211   }                                              1925   }
2212 }                                                1926 }
2213                                                  1927 
2214 inline tools_GLint tools_gl2psCheckPrimitive( << 1928 static tools_GLint tools_gl2psCheckPrimitive(tools_GL2PSprimitive *prim, tools_GL2PSplane plane)
2215 {                                                1929 {
2216   tools_GLint i;                                 1930   tools_GLint i;
2217   tools_GLint pos;                               1931   tools_GLint pos;
2218                                                  1932 
2219   pos = tools_gl2psCheckPoint(prim->verts[0].    1933   pos = tools_gl2psCheckPoint(prim->verts[0].xyz, plane);
2220   for(i = 1; i < prim->numverts; i++){           1934   for(i = 1; i < prim->numverts; i++){
2221     pos |= tools_gl2psCheckPoint(prim->verts[    1935     pos |= tools_gl2psCheckPoint(prim->verts[i].xyz, plane);
2222     if(pos == (TOOLS_GL2PS_POINT_INFRONT | TO    1936     if(pos == (TOOLS_GL2PS_POINT_INFRONT | TOOLS_GL2PS_POINT_BACK)) return TOOLS_GL2PS_SPANNING;
2223   }                                              1937   }
2224   if(pos & TOOLS_GL2PS_POINT_INFRONT)   retur    1938   if(pos & TOOLS_GL2PS_POINT_INFRONT)   return TOOLS_GL2PS_IN_FRONT_OF;
2225   else if(pos & TOOLS_GL2PS_POINT_BACK) retur    1939   else if(pos & TOOLS_GL2PS_POINT_BACK) return TOOLS_GL2PS_IN_BACK_OF;
2226   else                            return TOOL    1940   else                            return TOOLS_GL2PS_COINCIDENT;
2227 }                                                1941 }
2228                                                  1942 
2229 inline tools_GL2PSprimitive *tools_gl2psCreat << 1943 static tools_GL2PSprimitive *tools_tools_gl2psCreateSplitPrimitive2D(tools_GL2PSprimitive *parent,
2230                                                  1944                                                    tools_GLshort numverts,
2231                                                  1945                                                    tools_GL2PSvertex *vertx)
2232 {                                                1946 {
2233   tools_GLint i;                                 1947   tools_GLint i;
2234   tools_GL2PSprimitive *child = (tools_GL2PSp    1948   tools_GL2PSprimitive *child = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
2235                                                  1949 
2236   if(parent->type == TOOLS_GL2PS_IMAGEMAP){      1950   if(parent->type == TOOLS_GL2PS_IMAGEMAP){
2237     child->type = TOOLS_GL2PS_IMAGEMAP;          1951     child->type = TOOLS_GL2PS_IMAGEMAP;
2238     child->data.image = parent->data.image;      1952     child->data.image = parent->data.image;
2239   }                                              1953   }
2240   else {                                         1954   else {
2241     switch(numverts){                            1955     switch(numverts){
2242     case 1 : child->type = TOOLS_GL2PS_POINT;    1956     case 1 : child->type = TOOLS_GL2PS_POINT; break;
2243     case 2 : child->type = TOOLS_GL2PS_LINE;     1957     case 2 : child->type = TOOLS_GL2PS_LINE; break;
2244     case 3 : child->type = TOOLS_GL2PS_TRIANG    1958     case 3 : child->type = TOOLS_GL2PS_TRIANGLE; break;
2245     case 4 : child->type = TOOLS_GL2PS_QUADRA    1959     case 4 : child->type = TOOLS_GL2PS_QUADRANGLE; break;
2246     default: child->type = TOOLS_GL2PS_NO_TYP    1960     default: child->type = TOOLS_GL2PS_NO_TYPE; break; /* FIXME */
2247     }                                            1961     }
2248   }                                              1962   }
2249   child->boundary = 0; /* FIXME: not done! */    1963   child->boundary = 0; /* FIXME: not done! */
2250   child->culled = parent->culled;                1964   child->culled = parent->culled;
2251   child->offset = parent->offset;                1965   child->offset = parent->offset;
2252   child->ofactor = parent->ofactor;              1966   child->ofactor = parent->ofactor;
2253   child->ounits = parent->ounits;                1967   child->ounits = parent->ounits;
2254   child->pattern = parent->pattern;              1968   child->pattern = parent->pattern;
2255   child->factor = parent->factor;                1969   child->factor = parent->factor;
2256   child->width = parent->width;                  1970   child->width = parent->width;
2257   child->linecap = parent->linecap;              1971   child->linecap = parent->linecap;
2258   child->linejoin = parent->linejoin;            1972   child->linejoin = parent->linejoin;
2259   child->numverts = numverts;                    1973   child->numverts = numverts;
2260   child->verts = (tools_GL2PSvertex*)tools_gl    1974   child->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(numverts * sizeof(tools_GL2PSvertex));
2261   for(i = 0; i < numverts; i++){                 1975   for(i = 0; i < numverts; i++){
2262     child->verts[i] = vertx[i];                  1976     child->verts[i] = vertx[i];
2263   }                                              1977   }
2264   return child;                                  1978   return child;
2265 }                                                1979 }
2266                                                  1980 
2267 inline void tools_gl2psSplitPrimitive2D(tools << 1981 static void tools_tools_gl2psSplitPrimitive2D(tools_GL2PSprimitive *prim,
2268                                   tools_GL2PS    1982                                   tools_GL2PSplane plane,
2269                                   tools_GL2PS    1983                                   tools_GL2PSprimitive **front,
2270                                   tools_GL2PS    1984                                   tools_GL2PSprimitive **back)
2271 {                                                1985 {
2272   /* cur will hold the position of the curren    1986   /* cur will hold the position of the current vertex
2273      prev will hold the position of the previ    1987      prev will hold the position of the previous vertex
2274      prev0 will hold the position of the vert    1988      prev0 will hold the position of the vertex number 0
2275      v1 and v2 represent the current and prev    1989      v1 and v2 represent the current and previous vertices, respectively
2276      flag is set if the current vertex should    1990      flag is set if the current vertex should be checked against the plane */
2277   tools_GLint cur = -1, prev = -1, i, v1 = 0,    1991   tools_GLint cur = -1, prev = -1, i, v1 = 0, v2 = 0, flag = 1, prev0 = -1;
2278                                                  1992 
2279   /* list of vertices that will go in front a    1993   /* list of vertices that will go in front and back primitive */
2280   tools_GL2PSvertex *front_list = NULL, *back    1994   tools_GL2PSvertex *front_list = NULL, *back_list = NULL;
2281                                                  1995 
2282   /* number of vertices in front and back lis    1996   /* number of vertices in front and back list */
2283   tools_GLshort front_count = 0, back_count =    1997   tools_GLshort front_count = 0, back_count = 0;
2284                                                  1998 
2285   for(i = 0; i <= prim->numverts; i++){          1999   for(i = 0; i <= prim->numverts; i++){
2286     v1 = i;                                      2000     v1 = i;
2287     if(v1 == prim->numverts){                    2001     if(v1 == prim->numverts){
2288       if(prim->numverts < 3) break;              2002       if(prim->numverts < 3) break;
2289       v1 = 0;                                    2003       v1 = 0;
2290       v2 = prim->numverts - 1;                   2004       v2 = prim->numverts - 1;
2291       cur = prev0;                               2005       cur = prev0;
2292     }                                            2006     }
2293     else if(flag){                               2007     else if(flag){
2294       cur = tools_gl2psCheckPoint(prim->verts    2008       cur = tools_gl2psCheckPoint(prim->verts[v1].xyz, plane);
2295       if(i == 0){                                2009       if(i == 0){
2296         prev0 = cur;                             2010         prev0 = cur;
2297       }                                          2011       }
2298     }                                            2012     }
2299     if(((prev == -1) || (prev == cur) || (pre    2013     if(((prev == -1) || (prev == cur) || (prev == 0) || (cur == 0)) &&
2300        (i < prim->numverts)){                    2014        (i < prim->numverts)){
2301       if(cur == TOOLS_GL2PS_POINT_INFRONT){      2015       if(cur == TOOLS_GL2PS_POINT_INFRONT){
2302         front_count++;                           2016         front_count++;
2303         front_list = (tools_GL2PSvertex*)tool    2017         front_list = (tools_GL2PSvertex*)tools_gl2psRealloc(front_list,
2304                                                  2018                                                 sizeof(tools_GL2PSvertex)*front_count);
2305         front_list[front_count-1] = prim->ver    2019         front_list[front_count-1] = prim->verts[v1];
2306       }                                          2020       }
2307       else if(cur == TOOLS_GL2PS_POINT_BACK){    2021       else if(cur == TOOLS_GL2PS_POINT_BACK){
2308         back_count++;                            2022         back_count++;
2309         back_list = (tools_GL2PSvertex*)tools    2023         back_list = (tools_GL2PSvertex*)tools_gl2psRealloc(back_list,
2310                                                  2024                                                sizeof(tools_GL2PSvertex)*back_count);
2311         back_list[back_count-1] = prim->verts    2025         back_list[back_count-1] = prim->verts[v1];
2312       }                                          2026       }
2313       else{                                      2027       else{
2314         front_count++;                           2028         front_count++;
2315         front_list = (tools_GL2PSvertex*)tool    2029         front_list = (tools_GL2PSvertex*)tools_gl2psRealloc(front_list,
2316                                                  2030                                                 sizeof(tools_GL2PSvertex)*front_count);
2317         front_list[front_count-1] = prim->ver    2031         front_list[front_count-1] = prim->verts[v1];
2318         back_count++;                            2032         back_count++;
2319         back_list = (tools_GL2PSvertex*)tools    2033         back_list = (tools_GL2PSvertex*)tools_gl2psRealloc(back_list,
2320                                                  2034                                                sizeof(tools_GL2PSvertex)*back_count);
2321         back_list[back_count-1] = prim->verts    2035         back_list[back_count-1] = prim->verts[v1];
2322       }                                          2036       }
2323       flag = 1;                                  2037       flag = 1;
2324     }                                            2038     }
2325     else if((prev != cur) && (cur != 0) && (p    2039     else if((prev != cur) && (cur != 0) && (prev != 0)){
2326       if(v1 != 0){                               2040       if(v1 != 0){
2327         v2 = v1-1;                               2041         v2 = v1-1;
2328         i--;                                     2042         i--;
2329       }                                          2043       }
2330       front_count++;                             2044       front_count++;
2331       front_list = (tools_GL2PSvertex*)tools_    2045       front_list = (tools_GL2PSvertex*)tools_gl2psRealloc(front_list,
2332                                                  2046                                               sizeof(tools_GL2PSvertex)*front_count);
2333       tools_gl2psCutEdge(&prim->verts[v2], &p    2047       tools_gl2psCutEdge(&prim->verts[v2], &prim->verts[v1],
2334                    plane, &front_list[front_c    2048                    plane, &front_list[front_count-1]);
2335       back_count++;                              2049       back_count++;
2336       back_list = (tools_GL2PSvertex*)tools_g    2050       back_list = (tools_GL2PSvertex*)tools_gl2psRealloc(back_list,
2337                                                  2051                                              sizeof(tools_GL2PSvertex)*back_count);
2338       back_list[back_count-1] = front_list[fr    2052       back_list[back_count-1] = front_list[front_count-1];
2339       flag = 0;                                  2053       flag = 0;
2340     }                                            2054     }
2341     prev = cur;                                  2055     prev = cur;
2342   }                                              2056   }
2343   *front = tools_gl2psCreateSplitPrimitive2D( << 2057   *front = tools_tools_gl2psCreateSplitPrimitive2D(prim, front_count, front_list);
2344   *back = tools_gl2psCreateSplitPrimitive2D(p << 2058   *back = tools_tools_gl2psCreateSplitPrimitive2D(prim, back_count, back_list);
2345   tools_gl2psFree(front_list);                   2059   tools_gl2psFree(front_list);
2346   tools_gl2psFree(back_list);                    2060   tools_gl2psFree(back_list);
2347 }                                                2061 }
2348                                                  2062 
2349 inline tools_GLint tools_gl2psAddInBspImageTr << 2063 static tools_GLint tools_gl2psAddInBspImageTree(tools_GL2PSprimitive *prim, tools_GL2PSbsptree2d **tree)
2350 {                                                2064 {
2351   tools_GLint ret = 0;                           2065   tools_GLint ret = 0;
2352   tools_GL2PSprimitive *frontprim = NULL, *ba    2066   tools_GL2PSprimitive *frontprim = NULL, *backprim = NULL;
2353                                                  2067 
2354   /* FIXME: until we consider the actual exte    2068   /* FIXME: until we consider the actual extent of text strings and
2355      pixmaps, never cull them. Otherwise the     2069      pixmaps, never cull them. Otherwise the whole string/pixmap gets
2356      culled as soon as the reference point is    2070      culled as soon as the reference point is hidden */
2357   if(prim->type == TOOLS_GL2PS_PIXMAP ||         2071   if(prim->type == TOOLS_GL2PS_PIXMAP ||
2358      prim->type == TOOLS_GL2PS_TEXT ||           2072      prim->type == TOOLS_GL2PS_TEXT ||
2359      prim->type == TOOLS_GL2PS_SPECIAL){         2073      prim->type == TOOLS_GL2PS_SPECIAL){
2360     return 1;                                    2074     return 1;
2361   }                                              2075   }
2362                                                  2076 
2363   if(*tree == NULL){                             2077   if(*tree == NULL){
2364     if((prim->type != TOOLS_GL2PS_IMAGEMAP) & << 2078     if((prim->type != TOOLS_GL2PS_IMAGEMAP) && (TOOLS_GL_FALSE == tools_gl2ps_context->zerosurfacearea)){
2365       tools_gl2psAddPlanesInBspTreeImage(gl2p << 2079       tools_gl2psAddPlanesInBspTreeImage(tools_gl2ps_context->primitivetoadd, tree);
2366     }                                            2080     }
2367     return 1;                                    2081     return 1;
2368   }                                              2082   }
2369   else{                                          2083   else{
2370     switch(tools_gl2psCheckPrimitive(prim, (*    2084     switch(tools_gl2psCheckPrimitive(prim, (*tree)->plane)){
2371     case TOOLS_GL2PS_IN_BACK_OF: return tools << 2085     case TOOLS_GL2PS_IN_BACK_OF: return tools_gl2psAddInBspImageTree(prim, &(*tree)->back);
2372     case TOOLS_GL2PS_IN_FRONT_OF:                2086     case TOOLS_GL2PS_IN_FRONT_OF:
2373       if((*tree)->front != NULL) return tools << 2087       if((*tree)->front != NULL) return tools_gl2psAddInBspImageTree(prim, &(*tree)->front);
2374       else                       return 0;       2088       else                       return 0;
2375     case TOOLS_GL2PS_SPANNING:                   2089     case TOOLS_GL2PS_SPANNING:
2376       tools_gl2psSplitPrimitive2D(prim, (*tre << 2090       tools_tools_gl2psSplitPrimitive2D(prim, (*tree)->plane, &frontprim, &backprim);
2377       ret = tools_gl2psAddInBspImageTree(gl2p << 2091       ret = tools_gl2psAddInBspImageTree(backprim, &(*tree)->back);
2378       if((*tree)->front != NULL){                2092       if((*tree)->front != NULL){
2379         if(tools_gl2psAddInBspImageTree(gl2ps << 2093         if(tools_gl2psAddInBspImageTree(frontprim, &(*tree)->front)){
2380           ret = 1;                               2094           ret = 1;
2381         }                                        2095         }
2382       }                                          2096       }
2383       tools_gl2psFree(frontprim->verts);         2097       tools_gl2psFree(frontprim->verts);
2384       tools_gl2psFree(frontprim);                2098       tools_gl2psFree(frontprim);
2385       tools_gl2psFree(backprim->verts);          2099       tools_gl2psFree(backprim->verts);
2386       tools_gl2psFree(backprim);                 2100       tools_gl2psFree(backprim);
2387       return ret;                                2101       return ret;
2388     case TOOLS_GL2PS_COINCIDENT:                 2102     case TOOLS_GL2PS_COINCIDENT:
2389       if((*tree)->back != NULL){                 2103       if((*tree)->back != NULL){
2390         gl2ps->zerosurfacearea = TOOLS_GL_TRU << 2104         tools_gl2ps_context->zerosurfacearea = TOOLS_GL_TRUE;
2391         ret = tools_gl2psAddInBspImageTree(gl << 2105         ret = tools_gl2psAddInBspImageTree(prim, &(*tree)->back);
2392         gl2ps->zerosurfacearea = TOOLS_GL_FAL << 2106         tools_gl2ps_context->zerosurfacearea = TOOLS_GL_FALSE;
2393         if(ret) return ret;                      2107         if(ret) return ret;
2394       }                                          2108       }
2395       if((*tree)->front != NULL){                2109       if((*tree)->front != NULL){
2396         gl2ps->zerosurfacearea = TOOLS_GL_TRU << 2110         tools_gl2ps_context->zerosurfacearea = TOOLS_GL_TRUE;
2397         ret = tools_gl2psAddInBspImageTree(gl << 2111         ret = tools_gl2psAddInBspImageTree(prim, &(*tree)->front);
2398         gl2ps->zerosurfacearea = TOOLS_GL_FAL << 2112         tools_gl2ps_context->zerosurfacearea = TOOLS_GL_FALSE;
2399         if(ret) return ret;                      2113         if(ret) return ret;
2400       }                                          2114       }
2401       if(prim->type == TOOLS_GL2PS_LINE) retu    2115       if(prim->type == TOOLS_GL2PS_LINE) return 1;
2402       else                         return 0;     2116       else                         return 0;
2403     }                                            2117     }
2404   }                                              2118   }
2405   return 0;                                      2119   return 0;
2406 }                                                2120 }
2407                                                  2121 
2408 inline void tools_gl2psAddInImageTree(tools_G << 2122 static void tools_tools_gl2psAddInImageTree(void *data)
2409 {                                                2123 {
2410   tools_GL2PSprimitive *prim = *(tools_GL2PSp    2124   tools_GL2PSprimitive *prim = *(tools_GL2PSprimitive **)data;
2411   gl2ps->primitivetoadd = prim;               << 2125   tools_gl2ps_context->primitivetoadd = prim;
2412   if(prim->type == TOOLS_GL2PS_IMAGEMAP && pr    2126   if(prim->type == TOOLS_GL2PS_IMAGEMAP && prim->data.image->format == TOOLS_GL2PS_IMAGEMAP_VISIBLE){
2413     prim->culled = 1;                            2127     prim->culled = 1;
2414   }                                              2128   }
2415   else if(!tools_gl2psAddInBspImageTree(gl2ps << 2129   else if(!tools_gl2psAddInBspImageTree(prim, &tools_gl2ps_context->imagetree)){
2416     prim->culled = 1;                            2130     prim->culled = 1;
2417   }                                              2131   }
2418   else if(prim->type == TOOLS_GL2PS_IMAGEMAP)    2132   else if(prim->type == TOOLS_GL2PS_IMAGEMAP){
2419     prim->data.image->format = TOOLS_GL2PS_IM    2133     prim->data.image->format = TOOLS_GL2PS_IMAGEMAP_VISIBLE;
2420   }                                              2134   }
2421 }                                                2135 }
2422                                                  2136 
2423 /* Boundary construction */                      2137 /* Boundary construction */
2424                                                  2138 
2425 inline void tools_gl2psAddBoundaryInList(tool << 2139 static void tools_gl2psAddBoundaryInList(tools_GL2PSprimitive *prim, tools_GL2PSlist *list)
2426 {                                                2140 {
2427   tools_GL2PSprimitive *b;                       2141   tools_GL2PSprimitive *b;
2428   tools_GLshort i;                               2142   tools_GLshort i;
2429   tools_GL2PSxyz c;                              2143   tools_GL2PSxyz c;
2430                                                  2144 
2431   c[0] = c[1] = c[2] = 0.0F;                     2145   c[0] = c[1] = c[2] = 0.0F;
2432   for(i = 0; i < prim->numverts; i++){           2146   for(i = 0; i < prim->numverts; i++){
2433     c[0] += prim->verts[i].xyz[0];               2147     c[0] += prim->verts[i].xyz[0];
2434     c[1] += prim->verts[i].xyz[1];               2148     c[1] += prim->verts[i].xyz[1];
2435   }                                              2149   }
2436   c[0] /= prim->numverts;                        2150   c[0] /= prim->numverts;
2437   c[1] /= prim->numverts;                        2151   c[1] /= prim->numverts;
2438                                                  2152 
2439   for(i = 0; i < prim->numverts; i++){           2153   for(i = 0; i < prim->numverts; i++){
2440     if(prim->boundary & (tools_GLint)pow(2.,     2154     if(prim->boundary & (tools_GLint)pow(2., i)){
2441       b = (tools_GL2PSprimitive*)tools_gl2psM    2155       b = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
2442       b->type = TOOLS_GL2PS_LINE;                2156       b->type = TOOLS_GL2PS_LINE;
2443       b->offset = prim->offset;                  2157       b->offset = prim->offset;
2444       b->ofactor = prim->ofactor;                2158       b->ofactor = prim->ofactor;
2445       b->ounits = prim->ounits;                  2159       b->ounits = prim->ounits;
2446       b->pattern = prim->pattern;                2160       b->pattern = prim->pattern;
2447       b->factor = prim->factor;                  2161       b->factor = prim->factor;
2448       b->culled = prim->culled;                  2162       b->culled = prim->culled;
2449       b->width = prim->width;                    2163       b->width = prim->width;
2450       b->linecap = prim->linecap;                2164       b->linecap = prim->linecap;
2451       b->linejoin = prim->linejoin;              2165       b->linejoin = prim->linejoin;
2452       b->boundary = 0;                           2166       b->boundary = 0;
2453       b->numverts = 2;                           2167       b->numverts = 2;
2454       b->verts = (tools_GL2PSvertex*)tools_gl    2168       b->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(2 * sizeof(tools_GL2PSvertex));
2455                                                  2169 
2456 #if 0 /* FIXME: need to work on boundary offs    2170 #if 0 /* FIXME: need to work on boundary offset... */
2457       v[0] = c[0] - prim->verts[i].xyz[0];       2171       v[0] = c[0] - prim->verts[i].xyz[0];
2458       v[1] = c[1] - prim->verts[i].xyz[1];       2172       v[1] = c[1] - prim->verts[i].xyz[1];
2459       v[2] = 0.0F;                               2173       v[2] = 0.0F;
2460       norm = tools_gl2psNorm(v);                 2174       norm = tools_gl2psNorm(v);
2461       v[0] /= norm;                              2175       v[0] /= norm;
2462       v[1] /= norm;                              2176       v[1] /= norm;
2463       b->verts[0].xyz[0] = prim->verts[i].xyz    2177       b->verts[0].xyz[0] = prim->verts[i].xyz[0] +0.1*v[0];
2464       b->verts[0].xyz[1] = prim->verts[i].xyz    2178       b->verts[0].xyz[1] = prim->verts[i].xyz[1] +0.1*v[1];
2465       b->verts[0].xyz[2] = prim->verts[i].xyz    2179       b->verts[0].xyz[2] = prim->verts[i].xyz[2];
2466       v[0] = c[0] - prim->verts[tools_gl2psGe    2180       v[0] = c[0] - prim->verts[tools_gl2psGetIndex(i, prim->numverts)].xyz[0];
2467       v[1] = c[1] - prim->verts[tools_gl2psGe    2181       v[1] = c[1] - prim->verts[tools_gl2psGetIndex(i, prim->numverts)].xyz[1];
2468       norm = tools_gl2psNorm(v);                 2182       norm = tools_gl2psNorm(v);
2469       v[0] /= norm;                              2183       v[0] /= norm;
2470       v[1] /= norm;                              2184       v[1] /= norm;
2471       b->verts[1].xyz[0] = prim->verts[tools_    2185       b->verts[1].xyz[0] = prim->verts[tools_gl2psGetIndex(i, prim->numverts)].xyz[0] +0.1*v[0];
2472       b->verts[1].xyz[1] = prim->verts[tools_    2186       b->verts[1].xyz[1] = prim->verts[tools_gl2psGetIndex(i, prim->numverts)].xyz[1] +0.1*v[1];
2473       b->verts[1].xyz[2] = prim->verts[tools_    2187       b->verts[1].xyz[2] = prim->verts[tools_gl2psGetIndex(i, prim->numverts)].xyz[2];
2474 #else                                            2188 #else
2475       b->verts[0].xyz[0] = prim->verts[i].xyz    2189       b->verts[0].xyz[0] = prim->verts[i].xyz[0];
2476       b->verts[0].xyz[1] = prim->verts[i].xyz    2190       b->verts[0].xyz[1] = prim->verts[i].xyz[1];
2477       b->verts[0].xyz[2] = prim->verts[i].xyz    2191       b->verts[0].xyz[2] = prim->verts[i].xyz[2];
2478       b->verts[1].xyz[0] = prim->verts[tools_    2192       b->verts[1].xyz[0] = prim->verts[tools_gl2psGetIndex(i, prim->numverts)].xyz[0];
2479       b->verts[1].xyz[1] = prim->verts[tools_    2193       b->verts[1].xyz[1] = prim->verts[tools_gl2psGetIndex(i, prim->numverts)].xyz[1];
2480       b->verts[1].xyz[2] = prim->verts[tools_    2194       b->verts[1].xyz[2] = prim->verts[tools_gl2psGetIndex(i, prim->numverts)].xyz[2];
2481 #endif                                           2195 #endif
2482                                                  2196 
2483       b->verts[0].rgba[0] = 0.0F;                2197       b->verts[0].rgba[0] = 0.0F;
2484       b->verts[0].rgba[1] = 0.0F;                2198       b->verts[0].rgba[1] = 0.0F;
2485       b->verts[0].rgba[2] = 0.0F;                2199       b->verts[0].rgba[2] = 0.0F;
2486       b->verts[0].rgba[3] = 0.0F;                2200       b->verts[0].rgba[3] = 0.0F;
2487       b->verts[1].rgba[0] = 0.0F;                2201       b->verts[1].rgba[0] = 0.0F;
2488       b->verts[1].rgba[1] = 0.0F;                2202       b->verts[1].rgba[1] = 0.0F;
2489       b->verts[1].rgba[2] = 0.0F;                2203       b->verts[1].rgba[2] = 0.0F;
2490       b->verts[1].rgba[3] = 0.0F;                2204       b->verts[1].rgba[3] = 0.0F;
2491       tools_gl2psListAdd(list, &b);              2205       tools_gl2psListAdd(list, &b);
2492     }                                            2206     }
2493   }                                              2207   }
2494                                                  2208 
2495 }                                                2209 }
2496                                                  2210 
2497 inline void tools_gl2psBuildPolygonBoundary(t << 2211 static void tools_gl2psBuildPolygonBoundary(tools_GL2PSbsptree *tree)
2498 {                                                2212 {
2499   tools_GLint i;                                 2213   tools_GLint i;
2500   tools_GL2PSprimitive *prim;                    2214   tools_GL2PSprimitive *prim;
2501                                                  2215 
2502   if(!tree) return;                              2216   if(!tree) return;
2503   tools_gl2psBuildPolygonBoundary(tree->back)    2217   tools_gl2psBuildPolygonBoundary(tree->back);
2504   for(i = 0; i < tools_gl2psListNbr(tree->pri    2218   for(i = 0; i < tools_gl2psListNbr(tree->primitives); i++){
2505     prim = *(tools_GL2PSprimitive**)tools_gl2    2219     prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(tree->primitives, i);
2506     if(prim->boundary) tools_gl2psAddBoundary    2220     if(prim->boundary) tools_gl2psAddBoundaryInList(prim, tree->primitives);
2507   }                                              2221   }
2508   tools_gl2psBuildPolygonBoundary(tree->front    2222   tools_gl2psBuildPolygonBoundary(tree->front);
2509 }                                                2223 }
2510                                                  2224 
2511 /********************************************    2225 /*********************************************************************
2512  *                                               2226  *
2513  * Feedback buffer parser                        2227  * Feedback buffer parser
2514  *                                               2228  *
2515  ********************************************    2229  *********************************************************************/
2516                                                  2230 
2517 TOOLS_GL2PSDLL_API void tools_gl2psAddPolyPri << 2231 TOOLS_GL2PSDLL_API void tools_gl2psAddPolyPrimitive(tools_GLshort type, tools_GLshort numverts,
2518                                         tools    2232                                         tools_GL2PSvertex *verts, tools_GLint offset,
2519                                         tools    2233                                         tools_GLfloat ofactor, tools_GLfloat ounits,
2520                                         tools    2234                                         tools_GLushort pattern, tools_GLint factor,
2521                                         tools    2235                                         tools_GLfloat width, tools_GLint linecap,
2522                                         tools    2236                                         tools_GLint linejoin,char boundary)
2523 {                                                2237 {
2524   tools_GL2PSprimitive *prim;                    2238   tools_GL2PSprimitive *prim;
2525                                                  2239 
2526   prim = (tools_GL2PSprimitive*)tools_gl2psMa    2240   prim = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
2527   prim->type = type;                             2241   prim->type = type;
2528   prim->numverts = numverts;                     2242   prim->numverts = numverts;
2529   prim->verts = (tools_GL2PSvertex*)tools_gl2    2243   prim->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(numverts * sizeof(tools_GL2PSvertex));
2530   memcpy(prim->verts, verts, numverts * sizeo    2244   memcpy(prim->verts, verts, numverts * sizeof(tools_GL2PSvertex));
2531   prim->boundary = boundary;                     2245   prim->boundary = boundary;
2532   prim->offset = (char)offset;                   2246   prim->offset = (char)offset;
2533   prim->ofactor = ofactor;                       2247   prim->ofactor = ofactor;
2534   prim->ounits = ounits;                         2248   prim->ounits = ounits;
2535   prim->pattern = pattern;                       2249   prim->pattern = pattern;
2536   prim->factor = factor;                         2250   prim->factor = factor;
2537   prim->width = width;                           2251   prim->width = width;
2538   prim->linecap = linecap;                       2252   prim->linecap = linecap;
2539   prim->linejoin = linejoin;                     2253   prim->linejoin = linejoin;
2540   prim->culled = 0;                              2254   prim->culled = 0;
2541                                                  2255 
2542   /* FIXME: here we should have an option to     2256   /* FIXME: here we should have an option to split stretched
2543      tris/quads to enhance SIMPLE_SORT */        2257      tris/quads to enhance SIMPLE_SORT */
2544                                                  2258 
2545   tools_gl2psListAdd(gl2ps->primitives, &prim << 2259   tools_gl2psListAdd(tools_gl2ps_context->primitives, &prim);
2546 }                                                2260 }
2547                                                  2261 
2548 inline tools_GLint tools_gl2psGetVertex(tools << 2262 static tools_GLint tools_gl2psGetVertex(tools_GL2PSvertex *v, tools_GLfloat *p)
2549 {                                                2263 {
2550   tools_GLint i;                                 2264   tools_GLint i;
2551                                                  2265 
2552   v->xyz[0] = p[0];                              2266   v->xyz[0] = p[0];
2553   v->xyz[1] = p[1];                              2267   v->xyz[1] = p[1];
2554   v->xyz[2] = p[2];                              2268   v->xyz[2] = p[2];
2555                                                  2269 
2556   if(gl2ps->colormode == TOOLS_GL_COLOR_INDEX << 2270   if(tools_gl2ps_context->colormode == TOOLS_GL_COLOR_INDEX && tools_gl2ps_context->colorsize > 0){
2557     i = (tools_GLint)(p[3] + 0.5);               2271     i = (tools_GLint)(p[3] + 0.5);
2558     v->rgba[0] = gl2ps->colormap[i][0];       << 2272     v->rgba[0] = tools_gl2ps_context->colormap[i][0];
2559     v->rgba[1] = gl2ps->colormap[i][1];       << 2273     v->rgba[1] = tools_gl2ps_context->colormap[i][1];
2560     v->rgba[2] = gl2ps->colormap[i][2];       << 2274     v->rgba[2] = tools_gl2ps_context->colormap[i][2];
2561     v->rgba[3] = gl2ps->colormap[i][3];       << 2275     v->rgba[3] = tools_gl2ps_context->colormap[i][3];
2562     return 4;                                    2276     return 4;
2563   }                                              2277   }
2564   else{                                          2278   else{
2565     v->rgba[0] = p[3];                           2279     v->rgba[0] = p[3];
2566     v->rgba[1] = p[4];                           2280     v->rgba[1] = p[4];
2567     v->rgba[2] = p[5];                           2281     v->rgba[2] = p[5];
2568     v->rgba[3] = p[6];                           2282     v->rgba[3] = p[6];
2569     return 7;                                    2283     return 7;
2570   }                                              2284   }
2571 }                                                2285 }
2572                                                  2286 
2573 inline void tools_gl2psParseFeedbackBuffer(to << 2287 static void tools_gl2psParseFeedbackBuffer(tools_GLint used)
2574 {                                                2288 {
2575   char flag;                                     2289   char flag;
2576   tools_GLushort pattern = 0;                    2290   tools_GLushort pattern = 0;
2577   tools_GLboolean boundary;                      2291   tools_GLboolean boundary;
2578   tools_GLint i, sizeoffloat, count, v, vtot,    2292   tools_GLint i, sizeoffloat, count, v, vtot, offset = 0, factor = 0, auxindex = 0;
2579   tools_GLint lcap = 0, ljoin = 0;               2293   tools_GLint lcap = 0, ljoin = 0;
2580   tools_GLfloat lwidth = 1.0F, psize = 1.0F,  << 2294   tools_GLfloat lwidth = 1.0F, psize = 1.0F, ofactor = 0.0F, ounits = 0.0F; /*G.Barrand : init ofactor, ounits to 0.0F.*/
2581   tools_GLfloat *current;                        2295   tools_GLfloat *current;
2582   tools_GL2PSvertex vertices[3];                 2296   tools_GL2PSvertex vertices[3];
2583   tools_GL2PSprimitive *prim;                    2297   tools_GL2PSprimitive *prim;
2584   tools_GL2PSimagemap *node;                     2298   tools_GL2PSimagemap *node;
2585                                                  2299 
2586   current = gl2ps->feedback;                  << 2300   current = tools_gl2ps_context->feedback;
2587   boundary = gl2ps->boundary = TOOLS_GL_FALSE << 2301   boundary = tools_gl2ps_context->boundary = TOOLS_GL_FALSE;
2588                                                  2302 
2589   while(used > 0){                               2303   while(used > 0){
2590                                                  2304 
2591     if(TOOLS_GL_TRUE == boundary) gl2ps->boun << 2305     if(TOOLS_GL_TRUE == boundary) tools_gl2ps_context->boundary = TOOLS_GL_TRUE;
2592                                                  2306 
2593     switch((tools_GLint)*current){               2307     switch((tools_GLint)*current){
2594     case TOOLS_GL_POINT_TOKEN :                  2308     case TOOLS_GL_POINT_TOKEN :
2595       current ++;                                2309       current ++;
2596       used --;                                   2310       used --;
2597       i = tools_gl2psGetVertex(gl2ps, &vertic << 2311       i = tools_gl2psGetVertex(&vertices[0], current);
2598       current += i;                              2312       current += i;
2599       used    -= i;                              2313       used    -= i;
2600       tools_gl2psAddPolyPrimitive(gl2ps, TOOL << 2314       tools_gl2psAddPolyPrimitive(TOOLS_GL2PS_POINT, 1, vertices, 0, 0.0, 0.0,
2601                             pattern, factor,     2315                             pattern, factor, psize, lcap, ljoin, 0);
2602       break;                                     2316       break;
2603     case TOOLS_GL_LINE_TOKEN :                   2317     case TOOLS_GL_LINE_TOKEN :
2604     case TOOLS_GL_LINE_RESET_TOKEN :             2318     case TOOLS_GL_LINE_RESET_TOKEN :
2605       current ++;                                2319       current ++;
2606       used --;                                   2320       used --;
2607       i = tools_gl2psGetVertex(gl2ps, &vertic << 2321       i = tools_gl2psGetVertex(&vertices[0], current);
2608       current += i;                              2322       current += i;
2609       used    -= i;                              2323       used    -= i;
2610       i = tools_gl2psGetVertex(gl2ps, &vertic << 2324       i = tools_gl2psGetVertex(&vertices[1], current);
2611       current += i;                              2325       current += i;
2612       used    -= i;                              2326       used    -= i;
2613       tools_gl2psAddPolyPrimitive(gl2ps, TOOL << 2327       tools_gl2psAddPolyPrimitive(TOOLS_GL2PS_LINE, 2, vertices, 0, 0.0, 0.0,
2614                             pattern, factor,     2328                             pattern, factor, lwidth, lcap, ljoin, 0);
2615       break;                                     2329       break;
2616     case TOOLS_GL_POLYGON_TOKEN :                2330     case TOOLS_GL_POLYGON_TOKEN :
2617       count = (tools_GLint)current[1];           2331       count = (tools_GLint)current[1];
2618       current += 2;                              2332       current += 2;
2619       used -= 2;                                 2333       used -= 2;
2620       v = vtot = 0;                              2334       v = vtot = 0;
2621       while(count > 0 && used > 0){              2335       while(count > 0 && used > 0){
2622         i = tools_gl2psGetVertex(gl2ps, &vert << 2336         i = tools_gl2psGetVertex(&vertices[v], current);
2623         tools_gl2psAdaptVertexForBlending(gl2 << 2337         tools_gl2psAdaptVertexForBlending(&vertices[v]);
2624         current += i;                            2338         current += i;
2625         used    -= i;                            2339         used    -= i;
2626         count --;                                2340         count --;
2627         vtot++;                                  2341         vtot++;
2628         if(v == 2){                              2342         if(v == 2){
2629           if(TOOLS_GL_TRUE == boundary){         2343           if(TOOLS_GL_TRUE == boundary){
2630             if(!count && vtot == 2) flag = 1|    2344             if(!count && vtot == 2) flag = 1|2|4;
2631             else if(!count) flag = 2|4;          2345             else if(!count) flag = 2|4;
2632             else if(vtot == 2) flag = 1|2;       2346             else if(vtot == 2) flag = 1|2;
2633             else flag = 2;                       2347             else flag = 2;
2634           }                                      2348           }
2635           else                                   2349           else
2636             flag = 0;                            2350             flag = 0;
2637           tools_gl2psAddPolyPrimitive(gl2ps,  << 2351           tools_gl2psAddPolyPrimitive(TOOLS_GL2PS_TRIANGLE, 3, vertices, offset, ofactor,
2638                                 ounits, patte    2352                                 ounits, pattern, factor, 1, lcap, ljoin,
2639                                 flag);           2353                                 flag);
2640           vertices[1] = vertices[2];             2354           vertices[1] = vertices[2];
2641         }                                        2355         }
2642         else                                     2356         else
2643           v ++;                                  2357           v ++;
2644       }                                          2358       }
2645       break;                                     2359       break;
2646     case TOOLS_GL_BITMAP_TOKEN :                 2360     case TOOLS_GL_BITMAP_TOKEN :
2647     case TOOLS_GL_DRAW_PIXEL_TOKEN :             2361     case TOOLS_GL_DRAW_PIXEL_TOKEN :
2648     case TOOLS_GL_COPY_PIXEL_TOKEN :             2362     case TOOLS_GL_COPY_PIXEL_TOKEN :
2649       current ++;                                2363       current ++;
2650       used --;                                   2364       used --;
2651       i = tools_gl2psGetVertex(gl2ps, &vertic << 2365       i = tools_gl2psGetVertex(&vertices[0], current);
2652       current += i;                              2366       current += i;
2653       used    -= i;                              2367       used    -= i;
2654       break;                                     2368       break;
2655     case TOOLS_GL_PASS_THROUGH_TOKEN :           2369     case TOOLS_GL_PASS_THROUGH_TOKEN :
2656       switch((tools_GLint)current[1]){           2370       switch((tools_GLint)current[1]){
2657       case TOOLS_GL2PS_BEGIN_OFFSET_TOKEN :      2371       case TOOLS_GL2PS_BEGIN_OFFSET_TOKEN :
2658         offset = 1;                              2372         offset = 1;
2659         current += 2;                            2373         current += 2;
2660         used -= 2;                               2374         used -= 2;
2661         ofactor = current[1];                    2375         ofactor = current[1];
2662         current += 2;                            2376         current += 2;
2663         used -= 2;                               2377         used -= 2;
2664         ounits = current[1];                     2378         ounits = current[1];
2665         break;                                   2379         break;
2666       case TOOLS_GL2PS_END_OFFSET_TOKEN :        2380       case TOOLS_GL2PS_END_OFFSET_TOKEN :
2667         offset = 0;                              2381         offset = 0;
2668         ofactor = 0.0;                           2382         ofactor = 0.0;
2669         ounits = 0.0;                            2383         ounits = 0.0;
2670         break;                                   2384         break;
2671       case TOOLS_GL2PS_BEGIN_BOUNDARY_TOKEN :    2385       case TOOLS_GL2PS_BEGIN_BOUNDARY_TOKEN : boundary = TOOLS_GL_TRUE; break;
2672       case TOOLS_GL2PS_END_BOUNDARY_TOKEN : b    2386       case TOOLS_GL2PS_END_BOUNDARY_TOKEN : boundary = TOOLS_GL_FALSE; break;
2673       case TOOLS_GL2PS_END_STIPPLE_TOKEN : pa    2387       case TOOLS_GL2PS_END_STIPPLE_TOKEN : pattern = 0; factor = 0; break;
2674       case TOOLS_GL2PS_BEGIN_BLEND_TOKEN : gl << 2388       case TOOLS_GL2PS_BEGIN_BLEND_TOKEN : tools_gl2ps_context->blending = TOOLS_GL_TRUE; break;
2675       case TOOLS_GL2PS_END_BLEND_TOKEN : gl2p << 2389       case TOOLS_GL2PS_END_BLEND_TOKEN : tools_gl2ps_context->blending = TOOLS_GL_FALSE; break;
2676       case TOOLS_GL2PS_BEGIN_STIPPLE_TOKEN :     2390       case TOOLS_GL2PS_BEGIN_STIPPLE_TOKEN :
2677         current += 2;                            2391         current += 2;
2678         used -= 2;                               2392         used -= 2;
2679         pattern = (tools_GLushort)current[1];    2393         pattern = (tools_GLushort)current[1];
2680         current += 2;                            2394         current += 2;
2681         used -= 2;                               2395         used -= 2;
2682         factor = (tools_GLint)current[1];        2396         factor = (tools_GLint)current[1];
2683         break;                                   2397         break;
2684       case TOOLS_GL2PS_SRC_BLEND_TOKEN :         2398       case TOOLS_GL2PS_SRC_BLEND_TOKEN :
2685         current += 2;                            2399         current += 2;
2686         used -= 2;                               2400         used -= 2;
2687         gl2ps->blendfunc[0] = (tools_GLint)cu << 2401         tools_gl2ps_context->blendfunc[0] = (tools_GLint)current[1];
2688         break;                                   2402         break;
2689       case TOOLS_GL2PS_DST_BLEND_TOKEN :         2403       case TOOLS_GL2PS_DST_BLEND_TOKEN :
2690         current += 2;                            2404         current += 2;
2691         used -= 2;                               2405         used -= 2;
2692         gl2ps->blendfunc[1] = (tools_GLint)cu << 2406         tools_gl2ps_context->blendfunc[1] = (tools_GLint)current[1];
2693         break;                                   2407         break;
2694       case TOOLS_GL2PS_POINT_SIZE_TOKEN :        2408       case TOOLS_GL2PS_POINT_SIZE_TOKEN :
2695         current += 2;                            2409         current += 2;
2696         used -= 2;                               2410         used -= 2;
2697         psize = current[1];                      2411         psize = current[1];
2698         break;                                   2412         break;
2699       case TOOLS_GL2PS_LINE_CAP_TOKEN :          2413       case TOOLS_GL2PS_LINE_CAP_TOKEN :
2700         current += 2;                            2414         current += 2;
2701         used -= 2;                               2415         used -= 2;
2702         lcap = (tools_GLint)current[1];       << 2416         lcap = (tools_GLint)current[1]; //G.Barrand : _MSC_VER : cast.
2703         break;                                   2417         break;
2704       case TOOLS_GL2PS_LINE_JOIN_TOKEN :         2418       case TOOLS_GL2PS_LINE_JOIN_TOKEN :
2705         current += 2;                            2419         current += 2;
2706         used -= 2;                               2420         used -= 2;
2707         ljoin = (tools_GLint)current[1];      << 2421         ljoin = (tools_GLint)current[1]; //G.Barrand : _MSC_VER : cast.
2708         break;                                   2422         break;
2709       case TOOLS_GL2PS_LINE_WIDTH_TOKEN :        2423       case TOOLS_GL2PS_LINE_WIDTH_TOKEN :
2710         current += 2;                            2424         current += 2;
2711         used -= 2;                               2425         used -= 2;
2712         lwidth = current[1];                     2426         lwidth = current[1];
2713         break;                                   2427         break;
2714       case TOOLS_GL2PS_IMAGEMAP_TOKEN :          2428       case TOOLS_GL2PS_IMAGEMAP_TOKEN :
2715         prim = (tools_GL2PSprimitive *)tools_    2429         prim = (tools_GL2PSprimitive *)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
2716         prim->type = TOOLS_GL2PS_IMAGEMAP;       2430         prim->type = TOOLS_GL2PS_IMAGEMAP;
2717         prim->boundary = 0;                      2431         prim->boundary = 0;
2718         prim->numverts = 4;                      2432         prim->numverts = 4;
2719         prim->verts = (tools_GL2PSvertex *)to    2433         prim->verts = (tools_GL2PSvertex *)tools_gl2psMalloc(4 * sizeof(tools_GL2PSvertex));
2720         prim->culled = 0;                        2434         prim->culled = 0;
2721         prim->offset = 0;                        2435         prim->offset = 0;
2722         prim->ofactor = 0.0;                     2436         prim->ofactor = 0.0;
2723         prim->ounits = 0.0;                      2437         prim->ounits = 0.0;
2724         prim->pattern = 0;                       2438         prim->pattern = 0;
2725         prim->factor = 0;                        2439         prim->factor = 0;
2726         prim->width = 1;                         2440         prim->width = 1;
2727                                                  2441 
2728         node = (tools_GL2PSimagemap*)tools_gl    2442         node = (tools_GL2PSimagemap*)tools_gl2psMalloc(sizeof(tools_GL2PSimagemap));
2729         node->image = (tools_GL2PSimage*)tool    2443         node->image = (tools_GL2PSimage*)tools_gl2psMalloc(sizeof(tools_GL2PSimage));
2730         node->image->type = 0;                   2444         node->image->type = 0;
2731         node->image->format = 0;                 2445         node->image->format = 0;
2732         node->image->zoom_x = 1.0F;              2446         node->image->zoom_x = 1.0F;
2733         node->image->zoom_y = 1.0F;              2447         node->image->zoom_y = 1.0F;
2734         node->next = NULL;                       2448         node->next = NULL;
2735                                                  2449 
2736         if(gl2ps->imagemap_head == NULL)      << 2450         if(tools_gl2ps_context->imagemap_head == NULL)
2737           gl2ps->imagemap_head = node;        << 2451           tools_gl2ps_context->imagemap_head = node;
2738         else                                     2452         else
2739           gl2ps->imagemap_tail->next = node;  << 2453           tools_gl2ps_context->imagemap_tail->next = node;
2740         gl2ps->imagemap_tail = node;          << 2454         tools_gl2ps_context->imagemap_tail = node;
2741         prim->data.image = node->image;          2455         prim->data.image = node->image;
2742                                                  2456 
2743         current += 2; used -= 2;                 2457         current += 2; used -= 2;
2744         i = tools_gl2psGetVertex(gl2ps, &prim << 2458         i = tools_gl2psGetVertex(&prim->verts[0], &current[1]);
2745         current += i; used -= i;                 2459         current += i; used -= i;
2746                                                  2460 
2747         node->image->width = (tools_GLint)cur    2461         node->image->width = (tools_GLint)current[2];
2748         current += 2; used -= 2;                 2462         current += 2; used -= 2;
2749         node->image->height = (tools_GLint)cu    2463         node->image->height = (tools_GLint)current[2];
2750         prim->verts[0].xyz[0] = prim->verts[0    2464         prim->verts[0].xyz[0] = prim->verts[0].xyz[0] - (int)(node->image->width / 2) + 0.5F;
2751         prim->verts[0].xyz[1] = prim->verts[0    2465         prim->verts[0].xyz[1] = prim->verts[0].xyz[1] - (int)(node->image->height / 2) + 0.5F;
2752         for(i = 1; i < 4; i++){                  2466         for(i = 1; i < 4; i++){
2753           for(v = 0; v < 3; v++){                2467           for(v = 0; v < 3; v++){
2754             prim->verts[i].xyz[v] = prim->ver    2468             prim->verts[i].xyz[v] = prim->verts[0].xyz[v];
2755             prim->verts[i].rgba[v] = prim->ve    2469             prim->verts[i].rgba[v] = prim->verts[0].rgba[v];
2756           }                                      2470           }
2757           prim->verts[i].rgba[v] = prim->vert    2471           prim->verts[i].rgba[v] = prim->verts[0].rgba[v];
2758         }                                        2472         }
2759         prim->verts[1].xyz[0] = prim->verts[1    2473         prim->verts[1].xyz[0] = prim->verts[1].xyz[0] + node->image->width;
2760         prim->verts[2].xyz[0] = prim->verts[1    2474         prim->verts[2].xyz[0] = prim->verts[1].xyz[0];
2761         prim->verts[2].xyz[1] = prim->verts[2    2475         prim->verts[2].xyz[1] = prim->verts[2].xyz[1] + node->image->height;
2762         prim->verts[3].xyz[1] = prim->verts[2    2476         prim->verts[3].xyz[1] = prim->verts[2].xyz[1];
2763                                                  2477 
2764         sizeoffloat = sizeof(tools_GLfloat);     2478         sizeoffloat = sizeof(tools_GLfloat);
2765         v = 2 * sizeoffloat;                     2479         v = 2 * sizeoffloat;
2766         vtot = node->image->height + node->im    2480         vtot = node->image->height + node->image->height *
2767           ((node->image->width - 1) / 8);        2481           ((node->image->width - 1) / 8);
2768         node->image->pixels = (tools_GLfloat*    2482         node->image->pixels = (tools_GLfloat*)tools_gl2psMalloc(v + vtot);
2769         node->image->pixels[0] = prim->verts[    2483         node->image->pixels[0] = prim->verts[0].xyz[0];
2770         node->image->pixels[1] = prim->verts[    2484         node->image->pixels[1] = prim->verts[0].xyz[1];
2771                                                  2485 
2772         for(i = 0; i < vtot; i += sizeoffloat    2486         for(i = 0; i < vtot; i += sizeoffloat){
2773           current += 2; used -= 2;               2487           current += 2; used -= 2;
2774           if((vtot - i) >= 4)                    2488           if((vtot - i) >= 4)
2775             memcpy(&(((char*)(node->image->pi    2489             memcpy(&(((char*)(node->image->pixels))[i + v]), &(current[2]), sizeoffloat);
2776           else                                   2490           else
2777             memcpy(&(((char*)(node->image->pi    2491             memcpy(&(((char*)(node->image->pixels))[i + v]), &(current[2]), vtot - i);
2778         }                                        2492         }
2779         current++; used--;                       2493         current++; used--;
2780         tools_gl2psListAdd(gl2ps->primitives, << 2494         tools_gl2psListAdd(tools_gl2ps_context->primitives, &prim);
2781         break;                                   2495         break;
2782       case TOOLS_GL2PS_DRAW_PIXELS_TOKEN :       2496       case TOOLS_GL2PS_DRAW_PIXELS_TOKEN :
2783       case TOOLS_GL2PS_TEXT_TOKEN :              2497       case TOOLS_GL2PS_TEXT_TOKEN :
2784         if(auxindex < tools_gl2psListNbr(gl2p << 2498         if(auxindex < tools_gl2psListNbr(tools_gl2ps_context->auxprimitives))
2785           tools_gl2psListAdd(gl2ps->primitive << 2499           tools_gl2psListAdd(tools_gl2ps_context->primitives,
2786                        tools_gl2psListPointer << 2500                        tools_gl2psListPointer(tools_gl2ps_context->auxprimitives, auxindex++));
2787         else                                     2501         else
2788           tools_gl2psMsg(TOOLS_GL2PS_ERROR, "    2502           tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Wrong number of auxiliary tokens in buffer");
2789         break;                                   2503         break;
2790       }                                          2504       }
2791       current += 2;                              2505       current += 2;
2792       used -= 2;                                 2506       used -= 2;
2793       break;                                     2507       break;
2794     default :                                    2508     default :
2795       tools_gl2psMsg(TOOLS_GL2PS_WARNING, "Un    2509       tools_gl2psMsg(TOOLS_GL2PS_WARNING, "Unknown token in buffer");
2796       current ++;                                2510       current ++;
2797       used --;                                   2511       used --;
2798       break;                                     2512       break;
2799     }                                            2513     }
2800   }                                              2514   }
2801                                                  2515 
2802   tools_gl2psListReset(gl2ps->auxprimitives); << 2516   tools_gl2psListReset(tools_gl2ps_context->auxprimitives);
2803 }                                                2517 }
2804                                                  2518 
2805 /********************************************    2519 /*********************************************************************
2806  *                                               2520  *
2807  * PostScript routines                           2521  * PostScript routines
2808  *                                               2522  *
2809  ********************************************    2523  *********************************************************************/
2810                                                  2524 
2811 inline void tools_gl2psWriteByte(tools_GL2PSc << 2525 static void tools_gl2psWriteByte(unsigned char byte)
2812 {                                                2526 {
2813   unsigned char h = byte / 16;                   2527   unsigned char h = byte / 16;
2814   unsigned char l = byte % 16;                   2528   unsigned char l = byte % 16;
2815   tools_gl2psPrintf(gl2ps,"%x%x", h, l);      << 2529   tools_gl2psPrintf("%x%x", h, l);
2816 }                                                2530 }
2817                                                  2531 
2818 inline void tools_gl2psPrintPostScriptPixmap( << 2532 static void tools_gl2psPrintPostScriptPixmap(tools_GLfloat x, tools_GLfloat y, tools_GL2PSimage *im,
                                                   >> 2533                                              int greyscale,int nbit) /*G.Barrand : to quiet coverity : add last greyscale,nbit.*/
2819 {                                                2534 {
2820   tools_GLuint nbhex, nbyte, nrgb, nbits;        2535   tools_GLuint nbhex, nbyte, nrgb, nbits;
2821   tools_GLuint row, col, ibyte, icase;           2536   tools_GLuint row, col, ibyte, icase;
2822   tools_GLfloat dr = 0., dg = 0., db = 0., fg    2537   tools_GLfloat dr = 0., dg = 0., db = 0., fgrey;
2823   unsigned char red = 0, green = 0, blue = 0,    2538   unsigned char red = 0, green = 0, blue = 0, b, grey;
2824   tools_GLuint width = (tools_GLuint)im->widt    2539   tools_GLuint width = (tools_GLuint)im->width;
2825   tools_GLuint height = (tools_GLuint)im->hei    2540   tools_GLuint height = (tools_GLuint)im->height;
2826                                                  2541 
2827   /* FIXME: should we define an option for th    2542   /* FIXME: should we define an option for these? Or just keep the
2828      8-bit per component case? */                2543      8-bit per component case? */
2829 /*G.Barrand: have the two below lines in argu << 2544   /*int greyscale = 0;*/ /* set to 1 to output greyscale image */
2830   int greyscale = 0; // set to 1 to output gr << 2545   /*int nbit = 8;*/ /* number of bits per color compoment (2, 4 or 8) */
2831   int nbit = 8; // number of bits per color c << 
2832 */                                            << 
2833                                                  2546 
2834   if((width <= 0) || (height <= 0)) return;      2547   if((width <= 0) || (height <= 0)) return;
2835                                                  2548 
2836   tools_gl2psPrintf(gl2ps,"gsave\n");         << 2549   tools_gl2psPrintf("gsave\n");
2837   tools_gl2psPrintf(gl2ps,"%.2f %.2f translat << 2550   tools_gl2psPrintf("%.2f %.2f translate\n", x, y);
2838   tools_gl2psPrintf(gl2ps,"%.2f %.2f scale\n" << 2551   tools_gl2psPrintf("%.2f %.2f scale\n", width * im->zoom_x, height * im->zoom_y);
2839                                                  2552 
2840   if(greyscale){ /* greyscale */                 2553   if(greyscale){ /* greyscale */
2841     tools_gl2psPrintf(gl2ps,"/picstr %d strin << 2554     tools_gl2psPrintf("/picstr %d string def\n", width);
2842     tools_gl2psPrintf(gl2ps,"%d %d %d\n", wid << 2555     tools_gl2psPrintf("%d %d %d\n", width, height, 8);
2843     tools_gl2psPrintf(gl2ps,"[ %d 0 0 -%d 0 % << 2556     tools_gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height);
2844     tools_gl2psPrintf(gl2ps,"{ currentfile pi << 2557     tools_gl2psPrintf("{ currentfile picstr readhexstring pop }\n");
2845     tools_gl2psPrintf(gl2ps,"image\n");       << 2558     tools_gl2psPrintf("image\n");
2846     for(row = 0; row < height; row++){           2559     for(row = 0; row < height; row++){
2847       for(col = 0; col < width; col++){          2560       for(col = 0; col < width; col++){
2848         tools_gl2psGetRGB(im, col, row, &dr,     2561         tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2849         fgrey = (0.30F * dr + 0.59F * dg + 0.    2562         fgrey = (0.30F * dr + 0.59F * dg + 0.11F * db);
2850         grey = (unsigned char)(255. * fgrey);    2563         grey = (unsigned char)(255. * fgrey);
2851         tools_gl2psWriteByte(gl2ps, grey);    << 2564         tools_gl2psWriteByte(grey);
2852       }                                          2565       }
2853       tools_gl2psPrintf(gl2ps,"\n");          << 2566       tools_gl2psPrintf("\n");
2854     }                                            2567     }
2855     nbhex = width * height * 2;                  2568     nbhex = width * height * 2;
2856     tools_gl2psPrintf(gl2ps,"%%%% nbhex digit << 2569     tools_gl2psPrintf("%%%% nbhex digit          :%d\n", nbhex);
2857   }                                              2570   }
2858   else if(nbit == 2){ /* color, 2 bits for r     2571   else if(nbit == 2){ /* color, 2 bits for r and g and b; rgbs following each other */
2859     nrgb = width  * 3;                           2572     nrgb = width  * 3;
2860     nbits = nrgb * nbit;                         2573     nbits = nrgb * nbit;
2861     nbyte = nbits / 8;                           2574     nbyte = nbits / 8;
2862     if((nbyte * 8) != nbits) nbyte++;            2575     if((nbyte * 8) != nbits) nbyte++;
2863     tools_gl2psPrintf(gl2ps,"/rgbstr %d strin << 2576     tools_gl2psPrintf("/rgbstr %d string def\n", nbyte);
2864     tools_gl2psPrintf(gl2ps,"%d %d %d\n", wid << 2577     tools_gl2psPrintf("%d %d %d\n", width, height, nbit);
2865     tools_gl2psPrintf(gl2ps,"[ %d 0 0 -%d 0 % << 2578     tools_gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height);
2866     tools_gl2psPrintf(gl2ps,"{ currentfile rg << 2579     tools_gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n");
2867     tools_gl2psPrintf(gl2ps,"false 3\n");     << 2580     tools_gl2psPrintf("false 3\n");
2868     tools_gl2psPrintf(gl2ps,"colorimage\n");  << 2581     tools_gl2psPrintf("colorimage\n");
2869     for(row = 0; row < height; row++){           2582     for(row = 0; row < height; row++){
2870       icase = 1;                                 2583       icase = 1;
2871       col = 0;                                   2584       col = 0;
2872       b = 0;                                     2585       b = 0;
2873       for(ibyte = 0; ibyte < nbyte; ibyte++){    2586       for(ibyte = 0; ibyte < nbyte; ibyte++){
2874         if(icase == 1) {                         2587         if(icase == 1) {
2875           if(col < width) {                      2588           if(col < width) {
2876             tools_gl2psGetRGB(im, col, row, &    2589             tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2877           }                                      2590           }
2878           else {                                 2591           else {
2879             dr = dg = db = 0;                    2592             dr = dg = db = 0;
2880           }                                      2593           }
2881           col++;                                 2594           col++;
2882           red = (unsigned char)(3. * dr);        2595           red = (unsigned char)(3. * dr);
2883           green = (unsigned char)(3. * dg);      2596           green = (unsigned char)(3. * dg);
2884           blue = (unsigned char)(3. * db);       2597           blue = (unsigned char)(3. * db);
2885           b = red;                               2598           b = red;
2886           b = (b<<2) + green;                    2599           b = (b<<2) + green;
2887           b = (b<<2) + blue;                     2600           b = (b<<2) + blue;
2888           if(col < width) {                      2601           if(col < width) {
2889             tools_gl2psGetRGB(im, col, row, &    2602             tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2890           }                                      2603           }
2891           else {                                 2604           else {
2892             dr = dg = db = 0;                    2605             dr = dg = db = 0;
2893           }                                      2606           }
2894           col++;                                 2607           col++;
2895           red = (unsigned char)(3. * dr);        2608           red = (unsigned char)(3. * dr);
2896           green = (unsigned char)(3. * dg);      2609           green = (unsigned char)(3. * dg);
2897           blue = (unsigned char)(3. * db);       2610           blue = (unsigned char)(3. * db);
2898           b = (b<<2) + red;                      2611           b = (b<<2) + red;
2899           tools_gl2psWriteByte(gl2ps, b);     << 2612           tools_gl2psWriteByte(b);
2900           b = 0;                                 2613           b = 0;
2901           icase++;                               2614           icase++;
2902         }                                        2615         }
2903         else if(icase == 2) {                    2616         else if(icase == 2) {
2904           b = green;                             2617           b = green;
2905           b = (b<<2) + blue;                     2618           b = (b<<2) + blue;
2906           if(col < width) {                      2619           if(col < width) {
2907             tools_gl2psGetRGB(im, col, row, &    2620             tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2908           }                                      2621           }
2909           else {                                 2622           else {
2910             dr = dg = db = 0;                    2623             dr = dg = db = 0;
2911           }                                      2624           }
2912           col++;                                 2625           col++;
2913           red = (unsigned char)(3. * dr);        2626           red = (unsigned char)(3. * dr);
2914           green = (unsigned char)(3. * dg);      2627           green = (unsigned char)(3. * dg);
2915           blue = (unsigned char)(3. * db);       2628           blue = (unsigned char)(3. * db);
2916           b = (b<<2) + red;                      2629           b = (b<<2) + red;
2917           b = (b<<2) + green;                    2630           b = (b<<2) + green;
2918           tools_gl2psWriteByte(gl2ps, b);     << 2631           tools_gl2psWriteByte(b);
2919           b = 0;                                 2632           b = 0;
2920           icase++;                               2633           icase++;
2921         }                                        2634         }
2922         else if(icase == 3) {                    2635         else if(icase == 3) {
2923           b = blue;                              2636           b = blue;
2924           if(col < width) {                      2637           if(col < width) {
2925             tools_gl2psGetRGB(im, col, row, &    2638             tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2926           }                                      2639           }
2927           else {                                 2640           else {
2928             dr = dg = db = 0;                    2641             dr = dg = db = 0;
2929           }                                      2642           }
2930           col++;                                 2643           col++;
2931           red = (unsigned char)(3. * dr);        2644           red = (unsigned char)(3. * dr);
2932           green = (unsigned char)(3. * dg);      2645           green = (unsigned char)(3. * dg);
2933           blue = (unsigned char)(3. * db);       2646           blue = (unsigned char)(3. * db);
2934           b = (b<<2) + red;                      2647           b = (b<<2) + red;
2935           b = (b<<2) + green;                    2648           b = (b<<2) + green;
2936           b = (b<<2) + blue;                     2649           b = (b<<2) + blue;
2937           tools_gl2psWriteByte(gl2ps, b);     << 2650           tools_gl2psWriteByte(b);
2938           b = 0;                                 2651           b = 0;
2939           icase = 1;                             2652           icase = 1;
2940         }                                        2653         }
2941       }                                          2654       }
2942       tools_gl2psPrintf(gl2ps,"\n");          << 2655       tools_gl2psPrintf("\n");
2943     }                                            2656     }
2944   }                                              2657   }
2945   else if(nbit == 4){ /* color, 4 bits for r     2658   else if(nbit == 4){ /* color, 4 bits for r and g and b; rgbs following each other */
2946     nrgb = width  * 3;                           2659     nrgb = width  * 3;
2947     nbits = nrgb * nbit;                         2660     nbits = nrgb * nbit;
2948     nbyte = nbits / 8;                           2661     nbyte = nbits / 8;
2949     if((nbyte * 8) != nbits) nbyte++;            2662     if((nbyte * 8) != nbits) nbyte++;
2950     tools_gl2psPrintf(gl2ps,"/rgbstr %d strin << 2663     tools_gl2psPrintf("/rgbstr %d string def\n", nbyte);
2951     tools_gl2psPrintf(gl2ps,"%d %d %d\n", wid << 2664     tools_gl2psPrintf("%d %d %d\n", width, height, nbit);
2952     tools_gl2psPrintf(gl2ps,"[ %d 0 0 -%d 0 % << 2665     tools_gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height);
2953     tools_gl2psPrintf(gl2ps,"{ currentfile rg << 2666     tools_gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n");
2954     tools_gl2psPrintf(gl2ps,"false 3\n");     << 2667     tools_gl2psPrintf("false 3\n");
2955     tools_gl2psPrintf(gl2ps,"colorimage\n");  << 2668     tools_gl2psPrintf("colorimage\n");
2956     for(row = 0; row < height; row++){           2669     for(row = 0; row < height; row++){
2957       col = 0;                                   2670       col = 0;
2958       icase = 1;                                 2671       icase = 1;
2959       for(ibyte = 0; ibyte < nbyte; ibyte++){    2672       for(ibyte = 0; ibyte < nbyte; ibyte++){
2960         if(icase == 1) {                         2673         if(icase == 1) {
2961           if(col < width) {                      2674           if(col < width) {
2962             tools_gl2psGetRGB(im, col, row, &    2675             tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2963           }                                      2676           }
2964           else {                                 2677           else {
2965             dr = dg = db = 0;                    2678             dr = dg = db = 0;
2966           }                                      2679           }
2967           col++;                                 2680           col++;
2968           red = (unsigned char)(15. * dr);       2681           red = (unsigned char)(15. * dr);
2969           green = (unsigned char)(15. * dg);     2682           green = (unsigned char)(15. * dg);
2970           tools_gl2psPrintf(gl2ps,"%x%x", red << 2683           tools_gl2psPrintf("%x%x", red, green);
2971           icase++;                               2684           icase++;
2972         }                                        2685         }
2973         else if(icase == 2) {                    2686         else if(icase == 2) {
2974           blue = (unsigned char)(15. * db);      2687           blue = (unsigned char)(15. * db);
2975           if(col < width) {                      2688           if(col < width) {
2976             tools_gl2psGetRGB(im, col, row, &    2689             tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
2977           }                                      2690           }
2978           else {                                 2691           else {
2979             dr = dg = db = 0;                    2692             dr = dg = db = 0;
2980           }                                      2693           }
2981           col++;                                 2694           col++;
2982           red = (unsigned char)(15. * dr);       2695           red = (unsigned char)(15. * dr);
2983           tools_gl2psPrintf(gl2ps,"%x%x", blu << 2696           tools_gl2psPrintf("%x%x", blue, red);
2984           icase++;                               2697           icase++;
2985         }                                        2698         }
2986         else if(icase == 3) {                    2699         else if(icase == 3) {
2987           green = (unsigned char)(15. * dg);     2700           green = (unsigned char)(15. * dg);
2988           blue = (unsigned char)(15. * db);      2701           blue = (unsigned char)(15. * db);
2989           tools_gl2psPrintf(gl2ps,"%x%x", gre << 2702           tools_gl2psPrintf("%x%x", green, blue);
2990           icase = 1;                             2703           icase = 1;
2991         }                                        2704         }
2992       }                                          2705       }
2993       tools_gl2psPrintf(gl2ps,"\n");          << 2706       tools_gl2psPrintf("\n");
2994     }                                            2707     }
2995   }                                              2708   }
2996   else{ /* 8 bit for r and g and b */            2709   else{ /* 8 bit for r and g and b */
2997     nbyte = width * 3;                           2710     nbyte = width * 3;
2998     tools_gl2psPrintf(gl2ps,"/rgbstr %d strin << 2711     tools_gl2psPrintf("/rgbstr %d string def\n", nbyte);
2999     tools_gl2psPrintf(gl2ps,"%d %d %d\n", wid << 2712     tools_gl2psPrintf("%d %d %d\n", width, height, 8);
3000     tools_gl2psPrintf(gl2ps,"[ %d 0 0 -%d 0 % << 2713     tools_gl2psPrintf("[ %d 0 0 -%d 0 %d ]\n", width, height, height);
3001     tools_gl2psPrintf(gl2ps,"{ currentfile rg << 2714     tools_gl2psPrintf("{ currentfile rgbstr readhexstring pop }\n");
3002     tools_gl2psPrintf(gl2ps,"false 3\n");     << 2715     tools_gl2psPrintf("false 3\n");
3003     tools_gl2psPrintf(gl2ps,"colorimage\n");  << 2716     tools_gl2psPrintf("colorimage\n");
3004     for(row = 0; row < height; row++){           2717     for(row = 0; row < height; row++){
3005       for(col = 0; col < width; col++){          2718       for(col = 0; col < width; col++){
3006         tools_gl2psGetRGB(im, col, row, &dr,     2719         tools_gl2psGetRGB(im, col, row, &dr, &dg, &db);
3007         red = (unsigned char)(255. * dr);        2720         red = (unsigned char)(255. * dr);
3008         tools_gl2psWriteByte(gl2ps, red);     << 2721         tools_gl2psWriteByte(red);
3009         green = (unsigned char)(255. * dg);      2722         green = (unsigned char)(255. * dg);
3010         tools_gl2psWriteByte(gl2ps, green);   << 2723         tools_gl2psWriteByte(green);
3011         blue = (unsigned char)(255. * db);       2724         blue = (unsigned char)(255. * db);
3012         tools_gl2psWriteByte(gl2ps, blue);    << 2725         tools_gl2psWriteByte(blue);
3013       }                                          2726       }
3014       tools_gl2psPrintf(gl2ps,"\n");          << 2727       tools_gl2psPrintf("\n");
3015     }                                            2728     }
3016   }                                              2729   }
3017                                                  2730 
3018   tools_gl2psPrintf(gl2ps,"grestore\n");      << 2731   tools_gl2psPrintf("grestore\n");
3019 }                                                2732 }
3020                                                  2733 
3021 inline void tools_gl2psPrintPostScriptImagema << 2734 static void tools_gl2psPrintPostScriptImagemap(tools_GLfloat x, tools_GLfloat y,
3022                                          tool    2735                                          tools_GLsizei width, tools_GLsizei height,
3023                                          cons    2736                                          const unsigned char *imagemap){
3024   int i, size;                                   2737   int i, size;
3025                                                  2738 
3026   if((width <= 0) || (height <= 0)) return;      2739   if((width <= 0) || (height <= 0)) return;
3027                                                  2740 
3028   size = height + height * (width - 1) / 8;      2741   size = height + height * (width - 1) / 8;
3029                                                  2742 
3030   tools_gl2psPrintf(gl2ps,"gsave\n");         << 2743   tools_gl2psPrintf("gsave\n");
3031   tools_gl2psPrintf(gl2ps,"%.2f %.2f translat << 2744   tools_gl2psPrintf("%.2f %.2f translate\n", x, y);
3032   tools_gl2psPrintf(gl2ps,"%d %d scale\n%d %d << 2745   tools_gl2psPrintf("%d %d scale\n%d %d\ntrue\n", width, height,width, height);
3033   tools_gl2psPrintf(gl2ps,"[ %d 0 0 -%d 0 %d  << 2746   tools_gl2psPrintf("[ %d 0 0 -%d 0 %d ] {<", width, height, height);
3034   for(i = 0; i < size; i++){                     2747   for(i = 0; i < size; i++){
3035     tools_gl2psWriteByte(gl2ps, *imagemap);   << 2748     tools_gl2psWriteByte(*imagemap);
3036     imagemap++;                                  2749     imagemap++;
3037   }                                              2750   }
3038   tools_gl2psPrintf(gl2ps,">} imagemask\ngres << 2751   tools_gl2psPrintf(">} imagemask\ngrestore\n");
3039 }                                                2752 }
3040                                                  2753 
3041 inline void tools_gl2psPrintPostScriptHeader( << 2754 static void tools_gl2psPrintPostScriptHeader(void)
3042 {                                                2755 {
3043   time_t now;                                    2756   time_t now;
3044                                                  2757 
3045   /* Since compression is not part of the Pos    2758   /* Since compression is not part of the PostScript standard,
3046      compressed PostScript files are just gzi    2759      compressed PostScript files are just gzipped PostScript files
3047      ("ps.gz" or "eps.gz") */                    2760      ("ps.gz" or "eps.gz") */
3048   tools_gl2psPrintGzipHeader(gl2ps);          << 2761   tools_gl2psPrintGzipHeader();
3049                                                  2762 
3050   time(&now);                                    2763   time(&now);
3051                                                  2764 
3052   if(gl2ps->format == TOOLS_GL2PS_PS){        << 2765   if(tools_gl2ps_context->format == TOOLS_GL2PS_PS){
3053     tools_gl2psPrintf(gl2ps,"%%!PS-Adobe-3.0\ << 2766     tools_gl2psPrintf("%%!PS-Adobe-3.0\n");
3054   }                                              2767   }
3055   else{                                          2768   else{
3056     tools_gl2psPrintf(gl2ps,"%%!PS-Adobe-3.0  << 2769     tools_gl2psPrintf("%%!PS-Adobe-3.0 EPSF-3.0\n");
3057   }                                              2770   }
3058                                                  2771 
3059   tools_gl2psPrintf(gl2ps,"%%%%Title: %s\n"   << 2772   tools_gl2psPrintf("%%%%Title: %s\n"
3060               "%%%%Creator: GL2PS %d.%d.%d%s,    2773               "%%%%Creator: GL2PS %d.%d.%d%s, %s\n"
3061               "%%%%For: %s\n"                    2774               "%%%%For: %s\n"
3062               "%%%%CreationDate: %s"             2775               "%%%%CreationDate: %s"
3063               "%%%%LanguageLevel: 3\n"           2776               "%%%%LanguageLevel: 3\n"
3064               "%%%%DocumentData: Clean7Bit\n"    2777               "%%%%DocumentData: Clean7Bit\n"
3065               "%%%%Pages: 1\n",                  2778               "%%%%Pages: 1\n",
3066               gl2ps->title, TOOLS_GL2PS_MAJOR << 2779               tools_gl2ps_context->title, TOOLS_GL2PS_MAJOR_VERSION, TOOLS_GL2PS_MINOR_VERSION,
3067               TOOLS_GL2PS_PATCH_VERSION, TOOL    2780               TOOLS_GL2PS_PATCH_VERSION, TOOLS_GL2PS_EXTRA_VERSION, TOOLS_GL2PS_COPYRIGHT,
3068               gl2ps->producer, ctime(&now));  << 2781               tools_gl2ps_context->producer, ctime(&now));
3069                                                  2782 
3070   if(gl2ps->format == TOOLS_GL2PS_PS){        << 2783   if(tools_gl2ps_context->format == TOOLS_GL2PS_PS){
3071     tools_gl2psPrintf(gl2ps,"%%%%Orientation: << 2784     tools_gl2psPrintf("%%%%Orientation: %s\n"
3072                 "%%%%DocumentMedia: Default %    2785                 "%%%%DocumentMedia: Default %d %d 0 () ()\n",
3073                 (gl2ps->options & TOOLS_GL2PS << 2786                 (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? "Landscape" : "Portrait",
3074                 (gl2ps->options & TOOLS_GL2PS << 2787                 (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? (int)tools_gl2ps_context->viewport[3] :
3075                 (int)gl2ps->viewport[2],      << 2788                 (int)tools_gl2ps_context->viewport[2],
3076                 (gl2ps->options & TOOLS_GL2PS << 2789                 (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? (int)tools_gl2ps_context->viewport[2] :
3077                 (int)gl2ps->viewport[3]);     << 2790                 (int)tools_gl2ps_context->viewport[3]);
3078   }                                              2791   }
3079                                                  2792 
3080   tools_gl2psPrintf(gl2ps,"%%%%BoundingBox: % << 2793   tools_gl2psPrintf("%%%%BoundingBox: %d %d %d %d\n"
3081               "%%%%EndComments\n",               2794               "%%%%EndComments\n",
3082               (gl2ps->options & TOOLS_GL2PS_L << 2795               (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? (int)tools_gl2ps_context->viewport[1] :
3083               (int)gl2ps->viewport[0],        << 2796               (int)tools_gl2ps_context->viewport[0],
3084               (gl2ps->options & TOOLS_GL2PS_L << 2797               (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? (int)tools_gl2ps_context->viewport[0] :
3085               (int)gl2ps->viewport[1],        << 2798               (int)tools_gl2ps_context->viewport[1],
3086               (gl2ps->options & TOOLS_GL2PS_L << 2799               (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? (int)tools_gl2ps_context->viewport[3] :
3087               (int)gl2ps->viewport[2],        << 2800               (int)tools_gl2ps_context->viewport[2],
3088               (gl2ps->options & TOOLS_GL2PS_L << 2801               (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? (int)tools_gl2ps_context->viewport[2] :
3089               (int)gl2ps->viewport[3]);       << 2802               (int)tools_gl2ps_context->viewport[3]);
3090                                                  2803 
3091   /* RGB color: r g b C (replace C by G in ou    2804   /* RGB color: r g b C (replace C by G in output to change from rgb to gray)
3092      Grayscale: r g b G                          2805      Grayscale: r g b G
3093      Font choose: size fontname FC               2806      Font choose: size fontname FC
3094      Text string: (string) x y size fontname     2807      Text string: (string) x y size fontname S??
3095      Rotated text string: (string) angle x y     2808      Rotated text string: (string) angle x y size fontname S??R
3096      Point primitive: x y size P                 2809      Point primitive: x y size P
3097      Line width: width W                         2810      Line width: width W
3098      Line start: x y LS                          2811      Line start: x y LS
3099      Line joining last point: x y L              2812      Line joining last point: x y L
3100      Line end: x y LE                            2813      Line end: x y LE
3101      Flat-shaded triangle: x3 y3 x2 y2 x1 y1     2814      Flat-shaded triangle: x3 y3 x2 y2 x1 y1 T
3102      Smooth-shaded triangle: x3 y3 r3 g3 b3 x    2815      Smooth-shaded triangle: x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 ST */
3103                                                  2816 
3104   tools_gl2psPrintf(gl2ps,"%%%%BeginProlog\n" << 2817   tools_gl2psPrintf("%%%%BeginProlog\n"
3105               "/gl2psdict 64 dict def gl2psdi    2818               "/gl2psdict 64 dict def gl2psdict begin\n"
3106               "/tryPS3shading %s def %% set t    2819               "/tryPS3shading %s def %% set to false to force subdivision\n"
3107               "/rThreshold %g def %% red comp    2820               "/rThreshold %g def %% red component subdivision threshold\n"
3108               "/gThreshold %g def %% green co    2821               "/gThreshold %g def %% green component subdivision threshold\n"
3109               "/bThreshold %g def %% blue com    2822               "/bThreshold %g def %% blue component subdivision threshold\n",
3110               (gl2ps->options & TOOLS_GL2PS_N << 2823               (tools_gl2ps_context->options & TOOLS_GL2PS_NO_PS3_SHADING) ? "false" : "true",
3111               gl2ps->threshold[0], gl2ps->thr << 2824               tools_gl2ps_context->threshold[0], tools_gl2ps_context->threshold[1], tools_gl2ps_context->threshold[2]);
3112                                                  2825 
3113   tools_gl2psPrintf(gl2ps,"/BD { bind def } b << 2826   tools_gl2psPrintf("/BD { bind def } bind def\n"
3114               "/C  { setrgbcolor } BD\n"         2827               "/C  { setrgbcolor } BD\n"
3115               "/G  { 0.082 mul exch 0.6094 mu    2828               "/G  { 0.082 mul exch 0.6094 mul add exch 0.3086 mul add neg 1.0 add setgray } BD\n"
3116               "/W  { setlinewidth } BD\n"        2829               "/W  { setlinewidth } BD\n"
3117               "/LC  { setlinecap } BD\n"         2830               "/LC  { setlinecap } BD\n"
3118               "/LJ  { setlinejoin } BD\n");      2831               "/LJ  { setlinejoin } BD\n");
3119                                                  2832 
3120   tools_gl2psPrintf(gl2ps,"/FC { findfont exc << 2833   tools_gl2psPrintf("/FC { findfont exch /SH exch def SH scalefont setfont } BD\n"
3121               "/SW { dup stringwidth pop } BD    2834               "/SW { dup stringwidth pop } BD\n"
3122               "/S  { FC moveto show } BD\n"      2835               "/S  { FC moveto show } BD\n"
3123               "/SBC{ FC moveto SW -2 div 0 rm    2836               "/SBC{ FC moveto SW -2 div 0 rmoveto show } BD\n"
3124               "/SBR{ FC moveto SW neg 0 rmove    2837               "/SBR{ FC moveto SW neg 0 rmoveto show } BD\n"
3125               "/SCL{ FC moveto 0 SH -2 div rm    2838               "/SCL{ FC moveto 0 SH -2 div rmoveto show } BD\n"
3126               "/SCC{ FC moveto SW -2 div SH -    2839               "/SCC{ FC moveto SW -2 div SH -2 div rmoveto show } BD\n"
3127               "/SCR{ FC moveto SW neg SH -2 d    2840               "/SCR{ FC moveto SW neg SH -2 div rmoveto show } BD\n"
3128               "/STL{ FC moveto 0 SH neg rmove    2841               "/STL{ FC moveto 0 SH neg rmoveto show } BD\n"
3129               "/STC{ FC moveto SW -2 div SH n    2842               "/STC{ FC moveto SW -2 div SH neg rmoveto show } BD\n"
3130               "/STR{ FC moveto SW neg SH neg     2843               "/STR{ FC moveto SW neg SH neg rmoveto show } BD\n");
3131                                                  2844 
3132   /* rotated text routines: same nameanem wit    2845   /* rotated text routines: same nameanem with R appended */
3133                                                  2846 
3134   tools_gl2psPrintf(gl2ps,"/FCT { FC translat << 2847   tools_gl2psPrintf("/FCT { FC translate 0 0 } BD\n"
3135               "/SR  { gsave FCT moveto rotate    2848               "/SR  { gsave FCT moveto rotate show grestore } BD\n"
3136               "/SBCR{ gsave FCT moveto rotate    2849               "/SBCR{ gsave FCT moveto rotate SW -2 div 0 rmoveto show grestore } BD\n"
3137               "/SBRR{ gsave FCT moveto rotate    2850               "/SBRR{ gsave FCT moveto rotate SW neg 0 rmoveto show grestore } BD\n"
3138               "/SCLR{ gsave FCT moveto rotate    2851               "/SCLR{ gsave FCT moveto rotate 0 SH -2 div rmoveto show grestore} BD\n");
3139   tools_gl2psPrintf(gl2ps,"/SCCR{ gsave FCT m << 2852   tools_gl2psPrintf("/SCCR{ gsave FCT moveto rotate SW -2 div SH -2 div rmoveto show grestore} BD\n"
3140               "/SCRR{ gsave FCT moveto rotate    2853               "/SCRR{ gsave FCT moveto rotate SW neg SH -2 div rmoveto show grestore} BD\n"
3141               "/STLR{ gsave FCT moveto rotate    2854               "/STLR{ gsave FCT moveto rotate 0 SH neg rmoveto show grestore } BD\n"
3142               "/STCR{ gsave FCT moveto rotate    2855               "/STCR{ gsave FCT moveto rotate SW -2 div SH neg rmoveto show grestore } BD\n"
3143               "/STRR{ gsave FCT moveto rotate    2856               "/STRR{ gsave FCT moveto rotate SW neg SH neg rmoveto show grestore } BD\n");
3144                                                  2857 
3145   tools_gl2psPrintf(gl2ps,"/P  { newpath 0.0  << 2858   tools_gl2psPrintf("/P  { newpath 0.0 360.0 arc closepath fill } BD\n"
3146               "/LS { newpath moveto } BD\n"      2859               "/LS { newpath moveto } BD\n"
3147               "/L  { lineto } BD\n"              2860               "/L  { lineto } BD\n"
3148               "/LE { lineto stroke } BD\n"       2861               "/LE { lineto stroke } BD\n"
3149               "/T  { newpath moveto lineto li    2862               "/T  { newpath moveto lineto lineto closepath fill } BD\n");
3150                                                  2863 
3151   /* Smooth-shaded triangle with PostScript l    2864   /* Smooth-shaded triangle with PostScript level 3 shfill operator:
3152         x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r    2865         x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 STshfill */
3153                                                  2866 
3154   tools_gl2psPrintf(gl2ps,"/STshfill {\n"     << 2867   tools_gl2psPrintf("/STshfill {\n"
3155               "      /b1 exch def /g1 exch de    2868               "      /b1 exch def /g1 exch def /r1 exch def /y1 exch def /x1 exch def\n"
3156               "      /b2 exch def /g2 exch de    2869               "      /b2 exch def /g2 exch def /r2 exch def /y2 exch def /x2 exch def\n"
3157               "      /b3 exch def /g3 exch de    2870               "      /b3 exch def /g3 exch def /r3 exch def /y3 exch def /x3 exch def\n"
3158               "      gsave << /ShadingType 4     2871               "      gsave << /ShadingType 4 /ColorSpace [/DeviceRGB]\n"
3159               "      /DataSource [ 0 x1 y1 r1    2872               "      /DataSource [ 0 x1 y1 r1 g1 b1 0 x2 y2 r2 g2 b2 0 x3 y3 r3 g3 b3 ] >>\n"
3160               "      shfill grestore } BD\n")    2873               "      shfill grestore } BD\n");
3161                                                  2874 
3162   /* Flat-shaded triangle with middle color:     2875   /* Flat-shaded triangle with middle color:
3163         x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r    2876         x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 Tm */
3164                                                  2877 
3165   tools_gl2psPrintf(gl2ps,/* stack : x3 y3 r3 << 2878   tools_gl2psPrintf(/* stack : x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 */
3166               "/Tm { 3 -1 roll 8 -1 roll 13 -    2879               "/Tm { 3 -1 roll 8 -1 roll 13 -1 roll add add 3 div\n" /* r = (r1+r2+r3)/3 */
3167               /* stack : x3 y3 g3 b3 x2 y2 g2    2880               /* stack : x3 y3 g3 b3 x2 y2 g2 b2 x1 y1 g1 b1 r */
3168               "      3 -1 roll 7 -1 roll 11 -    2881               "      3 -1 roll 7 -1 roll 11 -1 roll add add 3 div\n" /* g = (g1+g2+g3)/3 */
3169               /* stack : x3 y3 b3 x2 y2 b2 x1    2882               /* stack : x3 y3 b3 x2 y2 b2 x1 y1 b1 r g b */
3170               "      3 -1 roll 6 -1 roll 9 -1    2883               "      3 -1 roll 6 -1 roll 9 -1 roll add add 3 div" /* b = (b1+b2+b3)/3 */
3171               /* stack : x3 y3 x2 y2 x1 y1 r     2884               /* stack : x3 y3 x2 y2 x1 y1 r g b */
3172               " C T } BD\n");                    2885               " C T } BD\n");
3173                                                  2886 
3174   /* Split triangle in four sub-triangles (at    2887   /* Split triangle in four sub-triangles (at sides middle points) and call the
3175      STnoshfill procedure on each, interpolat    2888      STnoshfill procedure on each, interpolating the colors in RGB space:
3176         x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r    2889         x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 STsplit
3177      (in procedure comments key: (Vi) = xi yi    2890      (in procedure comments key: (Vi) = xi yi ri gi bi) */
3178                                                  2891 
3179   tools_gl2psPrintf(gl2ps,"/STsplit {\n"      << 2892   tools_gl2psPrintf("/STsplit {\n"
3180               "      4 index 15 index add 0.5    2893               "      4 index 15 index add 0.5 mul\n" /* x13 = (x1+x3)/2 */
3181               "      4 index 15 index add 0.5    2894               "      4 index 15 index add 0.5 mul\n" /* y13 = (y1+y3)/2 */
3182               "      4 index 15 index add 0.5    2895               "      4 index 15 index add 0.5 mul\n" /* r13 = (r1+r3)/2 */
3183               "      4 index 15 index add 0.5    2896               "      4 index 15 index add 0.5 mul\n" /* g13 = (g1+g3)/2 */
3184               "      4 index 15 index add 0.5    2897               "      4 index 15 index add 0.5 mul\n" /* b13 = (b1+b3)/2 */
3185               "      5 copy 5 copy 25 15 roll    2898               "      5 copy 5 copy 25 15 roll\n");
3186                                                  2899 
3187   /* at his point, stack = (V3) (V13) (V13) (    2900   /* at his point, stack = (V3) (V13) (V13) (V13) (V2) (V1) */
3188                                                  2901 
3189   tools_gl2psPrintf(gl2ps,"      9 index 30 i << 2902   tools_gl2psPrintf("      9 index 30 index add 0.5 mul\n" /* x23 = (x2+x3)/2 */
3190               "      9 index 30 index add 0.5    2903               "      9 index 30 index add 0.5 mul\n" /* y23 = (y2+y3)/2 */
3191               "      9 index 30 index add 0.5    2904               "      9 index 30 index add 0.5 mul\n" /* r23 = (r2+r3)/2 */
3192               "      9 index 30 index add 0.5    2905               "      9 index 30 index add 0.5 mul\n" /* g23 = (g2+g3)/2 */
3193               "      9 index 30 index add 0.5    2906               "      9 index 30 index add 0.5 mul\n" /* b23 = (b2+b3)/2 */
3194               "      5 copy 5 copy 35 5 roll     2907               "      5 copy 5 copy 35 5 roll 25 5 roll 15 5 roll\n");
3195                                                  2908 
3196   /* stack = (V3) (V13) (V23) (V13) (V23) (V1    2909   /* stack = (V3) (V13) (V23) (V13) (V23) (V13) (V23) (V2) (V1) */
3197                                                  2910 
3198   tools_gl2psPrintf(gl2ps,"      4 index 10 i << 2911   tools_gl2psPrintf("      4 index 10 index add 0.5 mul\n" /* x12 = (x1+x2)/2 */
3199               "      4 index 10 index add 0.5    2912               "      4 index 10 index add 0.5 mul\n" /* y12 = (y1+y2)/2 */
3200               "      4 index 10 index add 0.5    2913               "      4 index 10 index add 0.5 mul\n" /* r12 = (r1+r2)/2 */
3201               "      4 index 10 index add 0.5    2914               "      4 index 10 index add 0.5 mul\n" /* g12 = (g1+g2)/2 */
3202               "      4 index 10 index add 0.5    2915               "      4 index 10 index add 0.5 mul\n" /* b12 = (b1+b2)/2 */
3203               "      5 copy 5 copy 40 5 roll     2916               "      5 copy 5 copy 40 5 roll 25 5 roll 15 5 roll 25 5 roll\n");
3204                                                  2917 
3205   /* stack = (V3) (V13) (V23) (V13) (V12) (V2    2918   /* stack = (V3) (V13) (V23) (V13) (V12) (V23) (V13) (V1) (V12) (V23) (V12) (V2) */
3206                                                  2919 
3207   tools_gl2psPrintf(gl2ps,"      STnoshfill S << 2920   tools_gl2psPrintf("      STnoshfill STnoshfill STnoshfill STnoshfill } BD\n");
3208                                                  2921 
3209   /* Gouraud shaded triangle using recursive     2922   /* Gouraud shaded triangle using recursive subdivision until the difference
3210      between corner colors does not exceed th    2923      between corner colors does not exceed the thresholds:
3211         x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r    2924         x3 y3 r3 g3 b3 x2 y2 r2 g2 b2 x1 y1 r1 g1 b1 STnoshfill  */
3212                                                  2925 
3213   tools_gl2psPrintf(gl2ps,"/STnoshfill {\n"   << 2926   tools_gl2psPrintf("/STnoshfill {\n"
3214               "      2 index 8 index sub abs     2927               "      2 index 8 index sub abs rThreshold gt\n" /* |r1-r2|>rth */
3215               "      { STsplit }\n"              2928               "      { STsplit }\n"
3216               "      { 1 index 7 index sub ab    2929               "      { 1 index 7 index sub abs gThreshold gt\n" /* |g1-g2|>gth */
3217               "        { STsplit }\n"            2930               "        { STsplit }\n"
3218               "        { dup 6 index sub abs     2931               "        { dup 6 index sub abs bThreshold gt\n" /* |b1-b2|>bth */
3219               "          { STsplit }\n"          2932               "          { STsplit }\n"
3220               "          { 2 index 13 index s    2933               "          { 2 index 13 index sub abs rThreshold gt\n" /* |r1-r3|>rht */
3221               "            { STsplit }\n"        2934               "            { STsplit }\n"
3222               "            { 1 index 12 index    2935               "            { 1 index 12 index sub abs gThreshold gt\n" /* |g1-g3|>gth */
3223               "              { STsplit }\n"      2936               "              { STsplit }\n"
3224               "              { dup 11 index s    2937               "              { dup 11 index sub abs bThreshold gt\n" /* |b1-b3|>bth */
3225               "                { STsplit }\n"    2938               "                { STsplit }\n"
3226               "                { 7 index 13 i    2939               "                { 7 index 13 index sub abs rThreshold gt\n"); /* |r2-r3|>rht */
3227   tools_gl2psPrintf(gl2ps,"                   << 2940   tools_gl2psPrintf("                  { STsplit }\n"
3228               "                  { 6 index 12    2941               "                  { 6 index 12 index sub abs gThreshold gt\n" /* |g2-g3|>gth */
3229               "                    { STsplit     2942               "                    { STsplit }\n"
3230               "                    { 5 index     2943               "                    { 5 index 11 index sub abs bThreshold gt\n" /* |b2-b3|>bth */
3231               "                      { STspli    2944               "                      { STsplit }\n"
3232               "                      { Tm }\n    2945               "                      { Tm }\n" /* all colors sufficiently similar */
3233               "                      ifelse }    2946               "                      ifelse }\n"
3234               "                    ifelse }\n    2947               "                    ifelse }\n"
3235               "                  ifelse }\n"     2948               "                  ifelse }\n"
3236               "                ifelse }\n"       2949               "                ifelse }\n"
3237               "              ifelse }\n"         2950               "              ifelse }\n"
3238               "            ifelse }\n"           2951               "            ifelse }\n"
3239               "          ifelse }\n"             2952               "          ifelse }\n"
3240               "        ifelse }\n"               2953               "        ifelse }\n"
3241               "      ifelse } BD\n");            2954               "      ifelse } BD\n");
3242                                                  2955 
3243   tools_gl2psPrintf(gl2ps,"tryPS3shading\n"   << 2956   tools_gl2psPrintf("tryPS3shading\n"
3244               "{ /shfill where\n"                2957               "{ /shfill where\n"
3245               "  { /ST { STshfill } BD }\n"      2958               "  { /ST { STshfill } BD }\n"
3246               "  { /ST { STnoshfill } BD }\n"    2959               "  { /ST { STnoshfill } BD }\n"
3247               "  ifelse }\n"                     2960               "  ifelse }\n"
3248               "{ /ST { STnoshfill } BD }\n"      2961               "{ /ST { STnoshfill } BD }\n"
3249               "ifelse\n");                       2962               "ifelse\n");
3250                                                  2963 
3251   tools_gl2psPrintf(gl2ps,"end\n"             << 2964   tools_gl2psPrintf("end\n"
3252               "%%%%EndProlog\n"                  2965               "%%%%EndProlog\n"
3253               "%%%%BeginSetup\n"                 2966               "%%%%BeginSetup\n"
3254               "/DeviceRGB setcolorspace\n"       2967               "/DeviceRGB setcolorspace\n"
3255               "gl2psdict begin\n"                2968               "gl2psdict begin\n"
3256               "%%%%EndSetup\n"                   2969               "%%%%EndSetup\n"
3257               "%%%%Page: 1 1\n"                  2970               "%%%%Page: 1 1\n"
3258               "%%%%BeginPageSetup\n");           2971               "%%%%BeginPageSetup\n");
3259                                                  2972 
3260   if(gl2ps->options & TOOLS_GL2PS_LANDSCAPE){ << 2973   if(tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE){
3261     tools_gl2psPrintf(gl2ps,"%d 0 translate 9 << 2974     tools_gl2psPrintf("%d 0 translate 90 rotate\n",
3262                 (int)gl2ps->viewport[3]);     << 2975                 (int)tools_gl2ps_context->viewport[3]);
3263   }                                              2976   }
3264                                                  2977 
3265   tools_gl2psPrintf(gl2ps,"%%%%EndPageSetup\n << 2978   tools_gl2psPrintf("%%%%EndPageSetup\n"
3266               "mark\n"                           2979               "mark\n"
3267               "gsave\n"                          2980               "gsave\n"
3268               "1.0 1.0 scale\n");                2981               "1.0 1.0 scale\n");
3269                                                  2982 
3270   if(gl2ps->options & TOOLS_GL2PS_DRAW_BACKGR << 2983   if(tools_gl2ps_context->options & TOOLS_GL2PS_DRAW_BACKGROUND){
3271     tools_gl2psPrintf(gl2ps,"%g %g %g C\n"    << 2984     tools_gl2psPrintf("%g %g %g C\n"
3272                 "newpath %d %d moveto %d %d l    2985                 "newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n"
3273                 "closepath fill\n",              2986                 "closepath fill\n",
3274                 gl2ps->bgcolor[0], gl2ps->bgc << 2987                 tools_gl2ps_context->bgcolor[0], tools_gl2ps_context->bgcolor[1], tools_gl2ps_context->bgcolor[2],
3275                 (int)gl2ps->viewport[0], (int << 2988                 (int)tools_gl2ps_context->viewport[0], (int)tools_gl2ps_context->viewport[1], (int)tools_gl2ps_context->viewport[2],
3276                 (int)gl2ps->viewport[1], (int << 2989                 (int)tools_gl2ps_context->viewport[1], (int)tools_gl2ps_context->viewport[2], (int)tools_gl2ps_context->viewport[3],
3277                 (int)gl2ps->viewport[0], (int << 2990                 (int)tools_gl2ps_context->viewport[0], (int)tools_gl2ps_context->viewport[3]);
3278   }                                              2991   }
3279 }                                                2992 }
3280                                                  2993 
3281 inline void tools_gl2psPrintPostScriptColor(t << 2994 static void tools_gl2psPrintPostScriptColor(tools_GL2PSrgba rgba)
3282 {                                                2995 {
3283   if(!tools_gl2psSameColor(gl2ps->lastrgba, r << 2996   if(!tools_gl2psSameColor(tools_gl2ps_context->lastrgba, rgba)){
3284     tools_gl2psSetLastColor(gl2ps, rgba);     << 2997     tools_gl2psSetLastColor(rgba);
3285     tools_gl2psPrintf(gl2ps,"%g %g %g C\n", r << 2998     tools_gl2psPrintf("%g %g %g C\n", rgba[0], rgba[1], rgba[2]);
3286   }                                              2999   }
3287 }                                                3000 }
3288                                                  3001 
3289 inline void tools_gl2psResetPostScriptColor(t << 3002 static void tools_gl2psResetPostScriptColor(void)
3290 {                                                3003 {
3291   gl2ps->lastrgba[0] = gl2ps->lastrgba[1] = g << 3004   tools_gl2ps_context->lastrgba[0] = tools_gl2ps_context->lastrgba[1] = tools_gl2ps_context->lastrgba[2] = -1.;
3292 }                                                3005 }
3293                                                  3006 
3294 inline void tools_gl2psEndPostScriptLine(tool << 3007 static void tools_gl2psEndPostScriptLine(void)
3295 {                                                3008 {
3296   int i;                                         3009   int i;
3297   if(gl2ps->lastvertex.rgba[0] >= 0.){        << 3010   if(tools_gl2ps_context->lastvertex.rgba[0] >= 0.){
3298     tools_gl2psPrintf(gl2ps,"%g %g LE\n", gl2 << 3011     tools_gl2psPrintf("%g %g LE\n", tools_gl2ps_context->lastvertex.xyz[0], tools_gl2ps_context->lastvertex.xyz[1]);
3299     for(i = 0; i < 3; i++)                       3012     for(i = 0; i < 3; i++)
3300       gl2ps->lastvertex.xyz[i] = -1.;         << 3013       tools_gl2ps_context->lastvertex.xyz[i] = -1.;
3301     for(i = 0; i < 4; i++)                       3014     for(i = 0; i < 4; i++)
3302       gl2ps->lastvertex.rgba[i] = -1.;        << 3015       tools_gl2ps_context->lastvertex.rgba[i] = -1.;
3303   }                                              3016   }
3304 }                                                3017 }
3305                                                  3018 
3306 inline void tools_gl2psParseStipplePattern(to << 3019 static void tools_gl2psParseStipplePattern(tools_GLushort pattern, tools_GLint factor,
3307                                      int *nb,    3020                                      int *nb, int array[10])
3308 {                                                3021 {
3309   int i, n;                                      3022   int i, n;
3310   int on[8] = {0, 0, 0, 0, 0, 0, 0, 0};          3023   int on[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3311   int off[8] = {0, 0, 0, 0, 0, 0, 0, 0};         3024   int off[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3312   char tmp[16];                                  3025   char tmp[16];
3313                                                  3026 
3314   /* extract the 16 bits from the OpenGL stip    3027   /* extract the 16 bits from the OpenGL stipple pattern */
3315   for(n = 15; n >= 0; n--){                      3028   for(n = 15; n >= 0; n--){
3316     tmp[n] = (char)(pattern & 0x01);             3029     tmp[n] = (char)(pattern & 0x01);
3317     pattern >>= 1;                               3030     pattern >>= 1;
3318   }                                              3031   }
3319   /* compute the on/off pixel sequence */        3032   /* compute the on/off pixel sequence */
3320   n = 0;                                         3033   n = 0;
3321   for(i = 0; i < 8; i++){                        3034   for(i = 0; i < 8; i++){
3322     while(n < 16 && !tmp[n]){ off[i]++; n++;     3035     while(n < 16 && !tmp[n]){ off[i]++; n++; }
3323     while(n < 16 && tmp[n]){ on[i]++; n++; }     3036     while(n < 16 && tmp[n]){ on[i]++; n++; }
3324     if(n >= 15){ i++; break; }                   3037     if(n >= 15){ i++; break; }
3325   }                                              3038   }
3326                                                  3039 
3327   /* store the on/off array from right to lef    3040   /* store the on/off array from right to left, starting with off
3328      pixels. The PostScript specification all    3041      pixels. The PostScript specification allows for at most 11
3329      elements in the on/off array, so we limi    3042      elements in the on/off array, so we limit ourselves to 5 on/off
3330      couples (our longest possible array is t    3043      couples (our longest possible array is thus [on4 off4 on3 off3
3331      on2 off2 on1 off1 on0 off0]) */             3044      on2 off2 on1 off1 on0 off0]) */
3332   *nb = 0;                                       3045   *nb = 0;
3333   for(n = i - 1; n >= 0; n--){                   3046   for(n = i - 1; n >= 0; n--){
3334     array[(*nb)++] = factor * on[n];             3047     array[(*nb)++] = factor * on[n];
3335     array[(*nb)++] = factor * off[n];            3048     array[(*nb)++] = factor * off[n];
3336     if(*nb == 10) break;                         3049     if(*nb == 10) break;
3337   }                                              3050   }
3338 }                                                3051 }
3339                                                  3052 
3340 inline int tools_gl2psPrintPostScriptDash(too << 3053 static int tools_gl2psPrintPostScriptDash(tools_GLushort pattern, tools_GLint factor, const char *str)
3341 {                                                3054 {
3342   int len = 0, i, n, array[10];                  3055   int len = 0, i, n, array[10];
3343                                                  3056 
3344   if(pattern == gl2ps->lastpattern && factor  << 3057   if(pattern == tools_gl2ps_context->lastpattern && factor == tools_gl2ps_context->lastfactor)
3345     return 0;                                    3058     return 0;
3346                                                  3059 
3347   gl2ps->lastpattern = pattern;               << 3060   tools_gl2ps_context->lastpattern = pattern;
3348   gl2ps->lastfactor = factor;                 << 3061   tools_gl2ps_context->lastfactor = factor;
3349                                                  3062 
3350   if(!pattern || !factor){                       3063   if(!pattern || !factor){
3351     /* solid line */                             3064     /* solid line */
3352     len += tools_gl2psPrintf(gl2ps,"[] 0 %s\n << 3065     len += tools_gl2psPrintf("[] 0 %s\n", str);
3353   }                                              3066   }
3354   else{                                          3067   else{
3355     tools_gl2psParseStipplePattern(pattern, f    3068     tools_gl2psParseStipplePattern(pattern, factor, &n, array);
3356     len += tools_gl2psPrintf(gl2ps,"[");      << 3069     len += tools_gl2psPrintf("[");
3357     for(i = 0; i < n; i++){                      3070     for(i = 0; i < n; i++){
3358       if(i) len += tools_gl2psPrintf(gl2ps,"  << 3071       if(i) len += tools_gl2psPrintf(" ");
3359       len += tools_gl2psPrintf(gl2ps,"%d", ar << 3072       len += tools_gl2psPrintf("%d", array[i]);
3360     }                                            3073     }
3361     len += tools_gl2psPrintf(gl2ps,"] 0 %s\n" << 3074     len += tools_gl2psPrintf("] 0 %s\n", str);
3362   }                                              3075   }
3363                                                  3076 
3364   return len;                                    3077   return len;
3365 }                                                3078 }
3366                                                  3079 
3367 inline void tools_gl2psPrintPostScriptPrimiti << 3080 static void tools_gl2psPrintPostScriptPrimitive(void *data)
3368 {                                                3081 {
3369   int newline;                                   3082   int newline;
3370   tools_GL2PSprimitive *prim;                    3083   tools_GL2PSprimitive *prim;
3371                                                  3084 
3372   prim = *(tools_GL2PSprimitive**)data;          3085   prim = *(tools_GL2PSprimitive**)data;
3373                                                  3086 
3374   if((gl2ps->options & TOOLS_GL2PS_OCCLUSION_ << 3087   if((tools_gl2ps_context->options & TOOLS_GL2PS_OCCLUSION_CULL) && prim->culled) return;
3375                                                  3088 
3376   /* Every effort is made to draw lines as co    3089   /* Every effort is made to draw lines as connected segments (i.e.,
3377      using a single PostScript path): this is    3090      using a single PostScript path): this is the only way to get nice
3378      line joins and to not restart the stippl    3091      line joins and to not restart the stippling for every line
3379      segment. So if the primitive to print is    3092      segment. So if the primitive to print is not a line we must first
3380      finish the current line (if any): */        3093      finish the current line (if any): */
3381   if(prim->type != TOOLS_GL2PS_LINE) tools_gl << 3094   if(prim->type != TOOLS_GL2PS_LINE) tools_gl2psEndPostScriptLine();
3382                                                  3095 
3383   switch(prim->type){                            3096   switch(prim->type){
3384   case TOOLS_GL2PS_POINT :                       3097   case TOOLS_GL2PS_POINT :
3385     tools_gl2psPrintPostScriptColor(gl2ps, pr << 3098     tools_gl2psPrintPostScriptColor(prim->verts[0].rgba);
3386     tools_gl2psPrintf(gl2ps,"%g %g %g P\n",   << 3099     tools_gl2psPrintf("%g %g %g P\n",
3387                 prim->verts[0].xyz[0], prim->    3100                 prim->verts[0].xyz[0], prim->verts[0].xyz[1], 0.5 * prim->width);
3388     break;                                       3101     break;
3389   case TOOLS_GL2PS_LINE :                        3102   case TOOLS_GL2PS_LINE :
3390     if(!tools_gl2psSamePosition(gl2ps->lastve << 3103     if(!tools_gl2psSamePosition(tools_gl2ps_context->lastvertex.xyz, prim->verts[0].xyz) ||
3391        !tools_gl2psSameColor(gl2ps->lastrgba, << 3104        !tools_gl2psSameColor(tools_gl2ps_context->lastrgba, prim->verts[0].rgba) ||
3392        gl2ps->lastlinewidth != prim->width || << 3105        tools_gl2ps_context->lastlinewidth != prim->width ||
3393        gl2ps->lastlinecap != prim->linecap || << 3106        tools_gl2ps_context->lastlinecap != prim->linecap ||
3394        gl2ps->lastlinejoin != prim->linejoin  << 3107        tools_gl2ps_context->lastlinejoin != prim->linejoin ||
3395        gl2ps->lastpattern != prim->pattern || << 3108        tools_gl2ps_context->lastpattern != prim->pattern ||
3396        gl2ps->lastfactor != prim->factor){    << 3109        tools_gl2ps_context->lastfactor != prim->factor){
3397       /* End the current line if the new segm    3110       /* End the current line if the new segment does not start where
3398          the last one ended, or if the color,    3111          the last one ended, or if the color, the width or the
3399          stippling have changed (multi-stroki    3112          stippling have changed (multi-stroking lines with changing
3400          colors is necessary until we use /sh    3113          colors is necessary until we use /shfill for lines;
3401          unfortunately this means that at the    3114          unfortunately this means that at the moment we can screw up
3402          line stippling for smooth-shaded lin    3115          line stippling for smooth-shaded lines) */
3403       tools_gl2psEndPostScriptLine(gl2ps);    << 3116       tools_gl2psEndPostScriptLine();
3404       newline = 1;                               3117       newline = 1;
3405     }                                            3118     }
3406     else{                                        3119     else{
3407       newline = 0;                               3120       newline = 0;
3408     }                                            3121     }
3409     if(gl2ps->lastlinewidth != prim->width){  << 3122     if(tools_gl2ps_context->lastlinewidth != prim->width){
3410       gl2ps->lastlinewidth = prim->width;     << 3123       tools_gl2ps_context->lastlinewidth = prim->width;
3411       tools_gl2psPrintf(gl2ps,"%g W\n", gl2ps << 3124       tools_gl2psPrintf("%g W\n", tools_gl2ps_context->lastlinewidth);
3412     }                                         << 3125     }
3413     if(gl2ps->lastlinecap != prim->linecap){  << 3126     if(tools_gl2ps_context->lastlinecap != prim->linecap){
3414       gl2ps->lastlinecap = prim->linecap;     << 3127       tools_gl2ps_context->lastlinecap = prim->linecap;
3415       tools_gl2psPrintf(gl2ps,"%d LC\n", gl2p << 3128       tools_gl2psPrintf("%d LC\n", tools_gl2ps_context->lastlinecap);
3416     }                                         << 3129     }
3417     if(gl2ps->lastlinejoin != prim->linejoin) << 3130     if(tools_gl2ps_context->lastlinejoin != prim->linejoin){
3418       gl2ps->lastlinejoin = prim->linejoin;   << 3131       tools_gl2ps_context->lastlinejoin = prim->linejoin;
3419       tools_gl2psPrintf(gl2ps,"%d LJ\n", gl2p << 3132       tools_gl2psPrintf("%d LJ\n", tools_gl2ps_context->lastlinejoin);
3420     }                                         << 3133     }
3421     tools_gl2psPrintPostScriptDash(gl2ps, pri << 3134     tools_gl2psPrintPostScriptDash(prim->pattern, prim->factor, "setdash");
3422     tools_gl2psPrintPostScriptColor(gl2ps, pr << 3135     tools_gl2psPrintPostScriptColor(prim->verts[0].rgba);
3423     tools_gl2psPrintf(gl2ps,"%g %g %s\n", pri << 3136     tools_gl2psPrintf("%g %g %s\n", prim->verts[0].xyz[0], prim->verts[0].xyz[1],
3424                 newline ? "LS" : "L");           3137                 newline ? "LS" : "L");
3425     gl2ps->lastvertex = prim->verts[1];       << 3138     tools_gl2ps_context->lastvertex = prim->verts[1];
3426     break;                                       3139     break;
3427   case TOOLS_GL2PS_TRIANGLE :                    3140   case TOOLS_GL2PS_TRIANGLE :
3428     if(!tools_gl2psVertsSameColor(prim)){        3141     if(!tools_gl2psVertsSameColor(prim)){
3429       tools_gl2psResetPostScriptColor(gl2ps); << 3142       tools_gl2psResetPostScriptColor();
3430       tools_gl2psPrintf(gl2ps,"%g %g %g %g %g << 3143       tools_gl2psPrintf("%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g ST\n",
3431                   prim->verts[2].xyz[0], prim    3144                   prim->verts[2].xyz[0], prim->verts[2].xyz[1],
3432                   prim->verts[2].rgba[0], pri    3145                   prim->verts[2].rgba[0], prim->verts[2].rgba[1],
3433                   prim->verts[2].rgba[2], pri    3146                   prim->verts[2].rgba[2], prim->verts[1].xyz[0],
3434                   prim->verts[1].xyz[1], prim    3147                   prim->verts[1].xyz[1], prim->verts[1].rgba[0],
3435                   prim->verts[1].rgba[1], pri    3148                   prim->verts[1].rgba[1], prim->verts[1].rgba[2],
3436                   prim->verts[0].xyz[0], prim    3149                   prim->verts[0].xyz[0], prim->verts[0].xyz[1],
3437                   prim->verts[0].rgba[0], pri    3150                   prim->verts[0].rgba[0], prim->verts[0].rgba[1],
3438                   prim->verts[0].rgba[2]);       3151                   prim->verts[0].rgba[2]);
3439     }                                            3152     }
3440     else{                                        3153     else{
3441       tools_gl2psPrintPostScriptColor(gl2ps,  << 3154       tools_gl2psPrintPostScriptColor(prim->verts[0].rgba);
3442       tools_gl2psPrintf(gl2ps,"%g %g %g %g %g << 3155       tools_gl2psPrintf("%g %g %g %g %g %g T\n",
3443                   prim->verts[2].xyz[0], prim    3156                   prim->verts[2].xyz[0], prim->verts[2].xyz[1],
3444                   prim->verts[1].xyz[0], prim    3157                   prim->verts[1].xyz[0], prim->verts[1].xyz[1],
3445                   prim->verts[0].xyz[0], prim    3158                   prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
3446     }                                            3159     }
3447     break;                                       3160     break;
3448   case TOOLS_GL2PS_QUADRANGLE :                  3161   case TOOLS_GL2PS_QUADRANGLE :
3449     tools_gl2psMsg(TOOLS_GL2PS_WARNING, "Ther    3162     tools_gl2psMsg(TOOLS_GL2PS_WARNING, "There should not be any quad left to print");
3450     break;                                       3163     break;
3451   case TOOLS_GL2PS_PIXMAP :                      3164   case TOOLS_GL2PS_PIXMAP :
3452     tools_gl2psPrintPostScriptPixmap(gl2ps, p << 3165     tools_gl2psPrintPostScriptPixmap(prim->verts[0].xyz[0], prim->verts[0].xyz[1],prim->data.image,0,8);
3453                                prim->data.ima << 
3454     break;                                       3166     break;
3455   case TOOLS_GL2PS_IMAGEMAP :                    3167   case TOOLS_GL2PS_IMAGEMAP :
3456     if(prim->data.image->type != TOOLS_GL2PS_    3168     if(prim->data.image->type != TOOLS_GL2PS_IMAGEMAP_WRITTEN){
3457       tools_gl2psPrintPostScriptColor(gl2ps,  << 3169       tools_gl2psPrintPostScriptColor(prim->verts[0].rgba);
3458       tools_gl2psPrintPostScriptImagemap(gl2p << 3170       tools_gl2psPrintPostScriptImagemap(prim->data.image->pixels[0],
3459                                    prim->data    3171                                    prim->data.image->pixels[1],
3460                                    prim->data    3172                                    prim->data.image->width, prim->data.image->height,
3461                                    (const uns    3173                                    (const unsigned char*)(&(prim->data.image->pixels[2])));
3462       prim->data.image->type = TOOLS_GL2PS_IM    3174       prim->data.image->type = TOOLS_GL2PS_IMAGEMAP_WRITTEN;
3463     }                                            3175     }
3464     break;                                       3176     break;
3465   case TOOLS_GL2PS_TEXT :                        3177   case TOOLS_GL2PS_TEXT :
3466     tools_gl2psPrintPostScriptColor(gl2ps, pr << 3178     tools_gl2psPrintPostScriptColor(prim->verts[0].rgba);
3467     tools_gl2psPrintf(gl2ps,"(%s) ", prim->da << 3179     tools_gl2psPrintf("(%s) ", prim->data.text->str);
3468     if(prim->data.text->angle)                   3180     if(prim->data.text->angle)
3469       tools_gl2psPrintf(gl2ps,"%g ", prim->da << 3181       tools_gl2psPrintf("%g ", prim->data.text->angle);
3470     tools_gl2psPrintf(gl2ps,"%g %g %d /%s ",  << 3182     tools_gl2psPrintf("%g %g %d /%s ",
3471                 prim->verts[0].xyz[0], prim->    3183                 prim->verts[0].xyz[0], prim->verts[0].xyz[1],
3472                 prim->data.text->fontsize, pr    3184                 prim->data.text->fontsize, prim->data.text->fontname);
3473     switch(prim->data.text->alignment){          3185     switch(prim->data.text->alignment){
3474     case TOOLS_GL2PS_TEXT_C:                     3186     case TOOLS_GL2PS_TEXT_C:
3475       tools_gl2psPrintf(gl2ps, prim->data.tex << 3187       tools_gl2psPrintf(prim->data.text->angle ? "SCCR\n" : "SCC\n");
3476       break;                                     3188       break;
3477     case TOOLS_GL2PS_TEXT_CL:                    3189     case TOOLS_GL2PS_TEXT_CL:
3478       tools_gl2psPrintf(gl2ps, prim->data.tex << 3190       tools_gl2psPrintf(prim->data.text->angle ? "SCLR\n" : "SCL\n");
3479       break;                                     3191       break;
3480     case TOOLS_GL2PS_TEXT_CR:                    3192     case TOOLS_GL2PS_TEXT_CR:
3481       tools_gl2psPrintf(gl2ps, prim->data.tex << 3193       tools_gl2psPrintf(prim->data.text->angle ? "SCRR\n" : "SCR\n");
3482       break;                                     3194       break;
3483     case TOOLS_GL2PS_TEXT_B:                     3195     case TOOLS_GL2PS_TEXT_B:
3484       tools_gl2psPrintf(gl2ps, prim->data.tex << 3196       tools_gl2psPrintf(prim->data.text->angle ? "SBCR\n" : "SBC\n");
3485       break;                                     3197       break;
3486     case TOOLS_GL2PS_TEXT_BR:                    3198     case TOOLS_GL2PS_TEXT_BR:
3487       tools_gl2psPrintf(gl2ps, prim->data.tex << 3199       tools_gl2psPrintf(prim->data.text->angle ? "SBRR\n" : "SBR\n");
3488       break;                                     3200       break;
3489     case TOOLS_GL2PS_TEXT_T:                     3201     case TOOLS_GL2PS_TEXT_T:
3490       tools_gl2psPrintf(gl2ps, prim->data.tex << 3202       tools_gl2psPrintf(prim->data.text->angle ? "STCR\n" : "STC\n");
3491       break;                                     3203       break;
3492     case TOOLS_GL2PS_TEXT_TL:                    3204     case TOOLS_GL2PS_TEXT_TL:
3493       tools_gl2psPrintf(gl2ps, prim->data.tex << 3205       tools_gl2psPrintf(prim->data.text->angle ? "STLR\n" : "STL\n");
3494       break;                                     3206       break;
3495     case TOOLS_GL2PS_TEXT_TR:                    3207     case TOOLS_GL2PS_TEXT_TR:
3496       tools_gl2psPrintf(gl2ps, prim->data.tex << 3208       tools_gl2psPrintf(prim->data.text->angle ? "STRR\n" : "STR\n");
3497       break;                                     3209       break;
3498     case TOOLS_GL2PS_TEXT_BL:                    3210     case TOOLS_GL2PS_TEXT_BL:
3499     default:                                     3211     default:
3500       tools_gl2psPrintf(gl2ps, prim->data.tex << 3212       tools_gl2psPrintf(prim->data.text->angle ? "SR\n" : "S\n");
3501       break;                                     3213       break;
3502     }                                            3214     }
3503     break;                                       3215     break;
3504   case TOOLS_GL2PS_SPECIAL :                     3216   case TOOLS_GL2PS_SPECIAL :
3505     /* alignment contains the format for whic    3217     /* alignment contains the format for which the special output text
3506        is intended */                            3218        is intended */
3507     if(prim->data.text->alignment == TOOLS_GL    3219     if(prim->data.text->alignment == TOOLS_GL2PS_PS ||
3508        prim->data.text->alignment == TOOLS_GL    3220        prim->data.text->alignment == TOOLS_GL2PS_EPS)
3509       tools_gl2psPrintf(gl2ps,"%s\n", prim->d << 3221       tools_gl2psPrintf("%s\n", prim->data.text->str);
3510     break;                                       3222     break;
3511   default :                                      3223   default :
3512     break;                                       3224     break;
3513   }                                              3225   }
3514 }                                                3226 }
3515                                                  3227 
3516 inline void tools_gl2psPrintPostScriptFooter( << 3228 static void tools_gl2psPrintPostScriptFooter(void)
3517 {                                                3229 {
3518   tools_gl2psPrintf(gl2ps,"grestore\n"        << 3230   tools_gl2psPrintf("grestore\n"
3519               "showpage\n"                       3231               "showpage\n"
3520               "cleartomark\n"                    3232               "cleartomark\n"
3521               "%%%%PageTrailer\n"                3233               "%%%%PageTrailer\n"
3522               "%%%%Trailer\n"                    3234               "%%%%Trailer\n"
3523               "end\n"                            3235               "end\n"
3524               "%%%%EOF\n");                      3236               "%%%%EOF\n");
3525                                                  3237 
3526   tools_gl2psPrintGzipFooter(gl2ps);          << 3238   tools_gl2psPrintGzipFooter();
3527 }                                                3239 }
3528                                                  3240 
3529 inline void tools_gl2psPrintPostScriptBeginVi << 3241 static void tools_gl2psPrintPostScriptBeginViewport(tools_GLint viewport[4])
3530 {                                                3242 {
3531   tools_GLint idx;                               3243   tools_GLint idx;
3532   tools_GLfloat rgba[4];                         3244   tools_GLfloat rgba[4];
3533   int x = viewport[0], y = viewport[1], w = v    3245   int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3];
3534                                                  3246 
3535   tools_glRenderMode(TOOLS_GL_FEEDBACK);         3247   tools_glRenderMode(TOOLS_GL_FEEDBACK);
3536                                                  3248 
3537   if(gl2ps->header){                          << 3249   if(tools_gl2ps_context->header){
3538     tools_gl2psPrintPostScriptHeader(gl2ps);  << 3250     tools_gl2psPrintPostScriptHeader();
3539     gl2ps->header = TOOLS_GL_FALSE;           << 3251     tools_gl2ps_context->header = TOOLS_GL_FALSE;
3540   }                                              3252   }
3541                                                  3253 
3542   tools_gl2psResetPostScriptColor(gl2ps);     << 3254   tools_gl2psResetPostScriptColor();
3543   tools_gl2psResetLineProperties(gl2ps);      << 3255   tools_gl2psResetLineProperties();
3544                                                  3256 
3545   tools_gl2psPrintf(gl2ps,"gsave\n"           << 3257   tools_gl2psPrintf("gsave\n"
3546               "1.0 1.0 scale\n");                3258               "1.0 1.0 scale\n");
3547                                                  3259 
3548   if(gl2ps->options & TOOLS_GL2PS_DRAW_BACKGR << 3260   if(tools_gl2ps_context->options & TOOLS_GL2PS_DRAW_BACKGROUND){
3549     if(gl2ps->colormode == TOOLS_GL_RGBA || g << 3261     if(tools_gl2ps_context->colormode == TOOLS_GL_RGBA || tools_gl2ps_context->colorsize == 0){
3550       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_    3262       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_VALUE, rgba);
3551     }                                            3263     }
3552     else{                                        3264     else{
3553       tools_glGetIntegerv(TOOLS_GL_INDEX_CLEA    3265       tools_glGetIntegerv(TOOLS_GL_INDEX_CLEAR_VALUE, &idx);
3554       rgba[0] = gl2ps->colormap[idx][0];      << 3266       rgba[0] = tools_gl2ps_context->colormap[idx][0];
3555       rgba[1] = gl2ps->colormap[idx][1];      << 3267       rgba[1] = tools_gl2ps_context->colormap[idx][1];
3556       rgba[2] = gl2ps->colormap[idx][2];      << 3268       rgba[2] = tools_gl2ps_context->colormap[idx][2];
3557       rgba[3] = 1.0F;                            3269       rgba[3] = 1.0F;
3558     }                                            3270     }
3559     tools_gl2psPrintf(gl2ps,"%g %g %g C\n"    << 3271     tools_gl2psPrintf("%g %g %g C\n"
3560                 "newpath %d %d moveto %d %d l    3272                 "newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n"
3561                 "closepath fill\n",              3273                 "closepath fill\n",
3562                 rgba[0], rgba[1], rgba[2],       3274                 rgba[0], rgba[1], rgba[2],
3563                 x, y, x+w, y, x+w, y+h, x, y+    3275                 x, y, x+w, y, x+w, y+h, x, y+h);
3564   }                                              3276   }
3565                                                  3277 
3566   tools_gl2psPrintf(gl2ps,"newpath %d %d move << 3278   tools_gl2psPrintf("newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto\n"
3567               "closepath clip\n",                3279               "closepath clip\n",
3568               x, y, x+w, y, x+w, y+h, x, y+h)    3280               x, y, x+w, y, x+w, y+h, x, y+h);
3569                                                  3281 
3570 }                                                3282 }
3571                                                  3283 
3572 inline tools_GLint tools_gl2psPrintPostScript << 3284 static tools_GLint tools_gl2psPrintPostScriptEndViewport(void)
3573 {                                                3285 {
3574   tools_GLint res;                               3286   tools_GLint res;
3575                                                  3287 
3576   res = tools_gl2psPrintPrimitives(gl2ps);    << 3288   res = tools_gl2psPrintPrimitives();
3577   tools_gl2psPrintf(gl2ps,"grestore\n");      << 3289   tools_gl2psPrintf("grestore\n");
3578   return res;                                    3290   return res;
3579 }                                                3291 }
3580                                                  3292 
3581 inline void tools_gl2psPrintPostScriptFinalPr << 3293 static void tools_gl2psPrintPostScriptFinalPrimitive(void)
3582 {                                                3294 {
3583   /* End any remaining line, if any */           3295   /* End any remaining line, if any */
3584   tools_gl2psEndPostScriptLine(gl2ps);        << 3296   tools_gl2psEndPostScriptLine();
3585 }                                                3297 }
3586                                                  3298 
3587 /* definition of the PostScript and Encapsula    3299 /* definition of the PostScript and Encapsulated PostScript backends */
3588                                                  3300 
3589 static const tools_GL2PSbackend tools_gl2psPS << 3301 static tools_GL2PSbackend tools_gl2psPS = {
3590   tools_gl2psPrintPostScriptHeader,              3302   tools_gl2psPrintPostScriptHeader,
3591   tools_gl2psPrintPostScriptFooter,              3303   tools_gl2psPrintPostScriptFooter,
3592   tools_gl2psPrintPostScriptBeginViewport,       3304   tools_gl2psPrintPostScriptBeginViewport,
3593   tools_gl2psPrintPostScriptEndViewport,         3305   tools_gl2psPrintPostScriptEndViewport,
3594   tools_gl2psPrintPostScriptPrimitive,           3306   tools_gl2psPrintPostScriptPrimitive,
3595   tools_gl2psPrintPostScriptFinalPrimitive,      3307   tools_gl2psPrintPostScriptFinalPrimitive,
3596   "ps",                                          3308   "ps",
3597   "Postscript"                                   3309   "Postscript"
3598 };                                               3310 };
3599                                                  3311 
3600 static const tools_GL2PSbackend tools_gl2psEP << 3312 static tools_GL2PSbackend tools_gl2psEPS = {
3601   tools_gl2psPrintPostScriptHeader,              3313   tools_gl2psPrintPostScriptHeader,
3602   tools_gl2psPrintPostScriptFooter,              3314   tools_gl2psPrintPostScriptFooter,
3603   tools_gl2psPrintPostScriptBeginViewport,       3315   tools_gl2psPrintPostScriptBeginViewport,
3604   tools_gl2psPrintPostScriptEndViewport,         3316   tools_gl2psPrintPostScriptEndViewport,
3605   tools_gl2psPrintPostScriptPrimitive,           3317   tools_gl2psPrintPostScriptPrimitive,
3606   tools_gl2psPrintPostScriptFinalPrimitive,      3318   tools_gl2psPrintPostScriptFinalPrimitive,
3607   "eps",                                         3319   "eps",
3608   "Encapsulated Postscript"                      3320   "Encapsulated Postscript"
3609 };                                               3321 };
3610                                                  3322 
3611 /********************************************    3323 /*********************************************************************
3612  *                                               3324  *
3613  * LaTeX routines                                3325  * LaTeX routines
3614  *                                               3326  *
3615  ********************************************    3327  *********************************************************************/
3616                                                  3328 
3617 inline void tools_gl2psPrintTeXHeader(tools_G << 3329 static void tools_gl2psPrintTeXHeader(void)
3618 {                                                3330 {
3619   char name[256];                                3331   char name[256];
3620   time_t now;                                    3332   time_t now;
3621   int i;                                         3333   int i;
3622   tools_GLfloat _s;                           << 
3623                                                  3334 
3624   if(gl2ps->filename && strlen(gl2ps->filenam << 3335   if(tools_gl2ps_context->filename && strlen(tools_gl2ps_context->filename) < 256){
3625     for(i = (int)strlen(gl2ps->filename) - 1; << 3336     for(i = (int)strlen(tools_gl2ps_context->filename) - 1; i >= 0; i--){
3626       if(gl2ps->filename[i] == '.'){          << 3337       if(tools_gl2ps_context->filename[i] == '.'){
3627         strncpy(name, gl2ps->filename, i);    << 3338         strncpy(name, tools_gl2ps_context->filename, i);
3628         name[i] = '\0';                          3339         name[i] = '\0';
3629         break;                                   3340         break;
3630       }                                          3341       }
3631     }                                            3342     }
3632     if(i <= 0) strcpy(name, gl2ps->filename); << 3343     if(i <= 0) strcpy(name, tools_gl2ps_context->filename);
3633   }                                              3344   }
3634   else{                                          3345   else{
3635     strcpy(name, "untitled");                    3346     strcpy(name, "untitled");
3636   }                                              3347   }
3637                                                  3348 
3638   time(&now);                                    3349   time(&now);
3639                                                  3350 
3640   fprintf(gl2ps->stream,                      << 3351   fprintf(tools_gl2ps_context->stream,
3641           "%% Title: %s\n"                       3352           "%% Title: %s\n"
3642           "%% Creator: GL2PS %d.%d.%d%s, %s\n    3353           "%% Creator: GL2PS %d.%d.%d%s, %s\n"
3643           "%% For: %s\n"                         3354           "%% For: %s\n"
3644           "%% CreationDate: %s",                 3355           "%% CreationDate: %s",
3645           gl2ps->title, TOOLS_GL2PS_MAJOR_VER << 3356           tools_gl2ps_context->title, TOOLS_GL2PS_MAJOR_VERSION, TOOLS_GL2PS_MINOR_VERSION,
3646           TOOLS_GL2PS_PATCH_VERSION, TOOLS_GL    3357           TOOLS_GL2PS_PATCH_VERSION, TOOLS_GL2PS_EXTRA_VERSION, TOOLS_GL2PS_COPYRIGHT,
3647           gl2ps->producer, ctime(&now));      << 3358           tools_gl2ps_context->producer, ctime(&now));
3648                                                  3359 
3649   _s = gl2ps->tex_scaling;                    << 3360   fprintf(tools_gl2ps_context->stream,
3650   if(_s <= 0.) _s = 1.;                       << 3361           "\\setlength{\\unitlength}{1pt}\n"
3651   fprintf(gl2ps->stream,                      << 
3652           "\\setlength{\\unitlength}{%gpt}\n" << 
3653           "\\begin{picture}(0,0)\n"              3362           "\\begin{picture}(0,0)\n"
3654           "\\includegraphics[scale=%g]{%s}\n" << 3363           "\\includegraphics{%s}\n"
3655           "\\end{picture}%%\n"                   3364           "\\end{picture}%%\n"
3656           "%s\\begin{picture}(%d,%d)(0,0)\n",    3365           "%s\\begin{picture}(%d,%d)(0,0)\n",
3657           _s, _s, name,                       << 3366           name, (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? "\\rotatebox{90}{" : "",
3658           (gl2ps->options & TOOLS_GL2PS_LANDS << 3367           (int)tools_gl2ps_context->viewport[2], (int)tools_gl2ps_context->viewport[3]);
3659           (int)(gl2ps->viewport[2]), (int)(gl << 
3660 }                                                3368 }
3661                                                  3369 
3662 inline void tools_gl2psPrintTeXPrimitive(tool << 3370 static void tools_gl2psPrintTeXPrimitive(void *data)
3663 {                                                3371 {
3664   tools_GL2PSprimitive *prim;                    3372   tools_GL2PSprimitive *prim;
3665                                                  3373 
3666   prim = *(tools_GL2PSprimitive**)data;          3374   prim = *(tools_GL2PSprimitive**)data;
3667                                                  3375 
3668   switch(prim->type){                            3376   switch(prim->type){
3669   case TOOLS_GL2PS_TEXT :                        3377   case TOOLS_GL2PS_TEXT :
3670     if(!(gl2ps->options & TOOLS_GL2PS_NO_TEX_ << 3378     fprintf(tools_gl2ps_context->stream, "\\fontsize{%d}{0}\n\\selectfont",
3671       fprintf(gl2ps->stream, "\\fontsize{%d}{ << 3379             prim->data.text->fontsize);
3672               prim->data.text->fontsize);     << 3380     fprintf(tools_gl2ps_context->stream, "\\put(%g,%g)",
3673     fprintf(gl2ps->stream, "\\put(%g,%g)",    << 3381             prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
3674             prim->verts[0].xyz[0],            << 
3675             prim->verts[0].xyz[1]);           << 
3676     if(prim->data.text->angle)                   3382     if(prim->data.text->angle)
3677       fprintf(gl2ps->stream, "{\\rotatebox{%g << 3383       fprintf(tools_gl2ps_context->stream, "{\\rotatebox{%g}", prim->data.text->angle);
3678     fprintf(gl2ps->stream, "{\\makebox(0,0)") << 3384     fprintf(tools_gl2ps_context->stream, "{\\makebox(0,0)");
3679     switch(prim->data.text->alignment){          3385     switch(prim->data.text->alignment){
3680     case TOOLS_GL2PS_TEXT_C:                     3386     case TOOLS_GL2PS_TEXT_C:
3681       fprintf(gl2ps->stream, "{");            << 3387       fprintf(tools_gl2ps_context->stream, "{");
3682       break;                                     3388       break;
3683     case TOOLS_GL2PS_TEXT_CL:                    3389     case TOOLS_GL2PS_TEXT_CL:
3684       fprintf(gl2ps->stream, "[l]{");         << 3390       fprintf(tools_gl2ps_context->stream, "[l]{");
3685       break;                                     3391       break;
3686     case TOOLS_GL2PS_TEXT_CR:                    3392     case TOOLS_GL2PS_TEXT_CR:
3687       fprintf(gl2ps->stream, "[r]{");         << 3393       fprintf(tools_gl2ps_context->stream, "[r]{");
3688       break;                                     3394       break;
3689     case TOOLS_GL2PS_TEXT_B:                     3395     case TOOLS_GL2PS_TEXT_B:
3690       fprintf(gl2ps->stream, "[b]{");         << 3396       fprintf(tools_gl2ps_context->stream, "[b]{");
3691       break;                                     3397       break;
3692     case TOOLS_GL2PS_TEXT_BR:                    3398     case TOOLS_GL2PS_TEXT_BR:
3693       fprintf(gl2ps->stream, "[br]{");        << 3399       fprintf(tools_gl2ps_context->stream, "[br]{");
3694       break;                                     3400       break;
3695     case TOOLS_GL2PS_TEXT_T:                     3401     case TOOLS_GL2PS_TEXT_T:
3696       fprintf(gl2ps->stream, "[t]{");         << 3402       fprintf(tools_gl2ps_context->stream, "[t]{");
3697       break;                                     3403       break;
3698     case TOOLS_GL2PS_TEXT_TL:                    3404     case TOOLS_GL2PS_TEXT_TL:
3699       fprintf(gl2ps->stream, "[tl]{");        << 3405       fprintf(tools_gl2ps_context->stream, "[tl]{");
3700       break;                                     3406       break;
3701     case TOOLS_GL2PS_TEXT_TR:                    3407     case TOOLS_GL2PS_TEXT_TR:
3702       fprintf(gl2ps->stream, "[tr]{");        << 3408       fprintf(tools_gl2ps_context->stream, "[tr]{");
3703       break;                                     3409       break;
3704     case TOOLS_GL2PS_TEXT_BL:                    3410     case TOOLS_GL2PS_TEXT_BL:
3705     default:                                     3411     default:
3706       fprintf(gl2ps->stream, "[bl]{");        << 3412       fprintf(tools_gl2ps_context->stream, "[bl]{");
3707       break;                                     3413       break;
3708     }                                            3414     }
3709     fprintf(gl2ps->stream, "\\textcolor[rgb]{ << 3415     fprintf(tools_gl2ps_context->stream, "\\textcolor[rgb]{%g,%g,%g}{{%s}}",
3710             prim->verts[0].rgba[0], prim->ver    3416             prim->verts[0].rgba[0], prim->verts[0].rgba[1], prim->verts[0].rgba[2],
3711             prim->data.text->str);               3417             prim->data.text->str);
3712     if(prim->data.text->angle)                   3418     if(prim->data.text->angle)
3713       fprintf(gl2ps->stream, "}");            << 3419       fprintf(tools_gl2ps_context->stream, "}");
3714     fprintf(gl2ps->stream, "}}\n");           << 3420     fprintf(tools_gl2ps_context->stream, "}}\n");
3715     break;                                       3421     break;
3716   case TOOLS_GL2PS_SPECIAL :                     3422   case TOOLS_GL2PS_SPECIAL :
3717     /* alignment contains the format for whic    3423     /* alignment contains the format for which the special output text
3718        is intended */                            3424        is intended */
3719     if (prim->data.text->alignment == TOOLS_G    3425     if (prim->data.text->alignment == TOOLS_GL2PS_TEX)
3720       fprintf(gl2ps->stream, "%s\n", prim->da << 3426       fprintf(tools_gl2ps_context->stream, "%s\n", prim->data.text->str);
3721     break;                                       3427     break;
3722   default :                                      3428   default :
3723     break;                                       3429     break;
3724   }                                              3430   }
3725 }                                                3431 }
3726                                                  3432 
3727 inline void tools_gl2psPrintTeXFooter(tools_G << 3433 static void tools_gl2psPrintTeXFooter(void)
3728 {                                                3434 {
3729   fprintf(gl2ps->stream, "\\end{picture}%s\n" << 3435   fprintf(tools_gl2ps_context->stream, "\\end{picture}%s\n",
3730           (gl2ps->options & TOOLS_GL2PS_LANDS << 3436           (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE) ? "}" : "");
3731 }                                                3437 }
3732                                                  3438 
3733 inline void tools_gl2psPrintTeXBeginViewport( << 3439 static void tools_gl2psPrintTeXBeginViewport(tools_GLint viewport[4])
3734 {                                                3440 {
3735   (void) viewport;  /* not used */               3441   (void) viewport;  /* not used */
3736   tools_glRenderMode(TOOLS_GL_FEEDBACK);         3442   tools_glRenderMode(TOOLS_GL_FEEDBACK);
3737                                                  3443 
3738   tools_gl2psResetLineProperties(gl2ps);      << 3444   tools_gl2psResetLineProperties();
3739                                                  3445 
3740   if(gl2ps->header){                          << 3446   if(tools_gl2ps_context->header){
3741     tools_gl2psPrintTeXHeader(gl2ps);         << 3447     tools_gl2psPrintTeXHeader();
3742     gl2ps->header = TOOLS_GL_FALSE;           << 3448     tools_gl2ps_context->header = TOOLS_GL_FALSE;
3743   }                                              3449   }
3744 }                                                3450 }
3745                                                  3451 
3746 inline tools_GLint tools_gl2psPrintTeXEndView << 3452 static tools_GLint tools_gl2psPrintTeXEndViewport(void)
3747 {                                                3453 {
3748   return tools_gl2psPrintPrimitives(gl2ps);   << 3454   return tools_gl2psPrintPrimitives();
3749 }                                                3455 }
3750                                                  3456 
3751 inline void tools_gl2psPrintTeXFinalPrimitive << 3457 static void tools_gl2psPrintTeXFinalPrimitive(void)
3752 {                                                3458 {
3753 }                                                3459 }
3754                                                  3460 
3755 /* definition of the LaTeX backend */            3461 /* definition of the LaTeX backend */
3756                                                  3462 
3757 static const tools_GL2PSbackend tools_gl2psTE << 3463 static tools_GL2PSbackend tools_gl2psTEX = {
3758   tools_gl2psPrintTeXHeader,                     3464   tools_gl2psPrintTeXHeader,
3759   tools_gl2psPrintTeXFooter,                     3465   tools_gl2psPrintTeXFooter,
3760   tools_gl2psPrintTeXBeginViewport,              3466   tools_gl2psPrintTeXBeginViewport,
3761   tools_gl2psPrintTeXEndViewport,                3467   tools_gl2psPrintTeXEndViewport,
3762   tools_gl2psPrintTeXPrimitive,                  3468   tools_gl2psPrintTeXPrimitive,
3763   tools_gl2psPrintTeXFinalPrimitive,             3469   tools_gl2psPrintTeXFinalPrimitive,
3764   "tex",                                         3470   "tex",
3765   "LaTeX text"                                   3471   "LaTeX text"
3766 };                                               3472 };
3767                                                  3473 
3768 /********************************************    3474 /*********************************************************************
3769  *                                               3475  *
3770  * PDF routines                                  3476  * PDF routines
3771  *                                               3477  *
3772  ********************************************    3478  *********************************************************************/
3773                                                  3479 
3774 inline int tools_gl2psPrintPDFCompressorType( << 3480 static int tools_gl2psPrintPDFCompressorType(void)
3775 {                                                3481 {
3776 #if defined(TOOLS_GL2PS_HAVE_ZLIB)               3482 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
3777   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){  << 3483   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
3778     return fprintf(gl2ps->stream, "/Filter [/ << 3484     return fprintf(tools_gl2ps_context->stream, "/Filter [/FlateDecode]\n");
3779   }                                              3485   }
3780 #endif                                           3486 #endif
3781   (void)gl2ps;                                << 
3782   return 0;                                      3487   return 0;
3783 }                                                3488 }
3784                                                  3489 
3785 inline int tools_gl2psPrintPDFStrokeColor(too << 3490 static int tools_gl2psPrintPDFStrokeColor(tools_GL2PSrgba rgba)
3786 {                                                3491 {
3787   int i, offs = 0;                               3492   int i, offs = 0;
3788                                                  3493 
3789   tools_gl2psSetLastColor(gl2ps, rgba);       << 3494   tools_gl2psSetLastColor(rgba);
3790   for(i = 0; i < 3; ++i){                        3495   for(i = 0; i < 3; ++i){
3791     if(TOOLS_GL2PS_ZERO(rgba[i]))                3496     if(TOOLS_GL2PS_ZERO(rgba[i]))
3792       offs += tools_gl2psPrintf(gl2ps,"%.0f " << 3497       offs += tools_gl2psPrintf("%.0f ", 0.);
3793     else if(rgba[i] < 1e-4 || rgba[i] > 1e6)     3498     else if(rgba[i] < 1e-4 || rgba[i] > 1e6) /* avoid %e formatting */
3794       offs += tools_gl2psPrintf(gl2ps,"%f ",  << 3499       offs += tools_gl2psPrintf("%f ", rgba[i]);
3795     else                                         3500     else
3796       offs += tools_gl2psPrintf(gl2ps,"%g ",  << 3501       offs += tools_gl2psPrintf("%g ", rgba[i]);
3797   }                                              3502   }
3798   offs += tools_gl2psPrintf(gl2ps,"RG\n");    << 3503   offs += tools_gl2psPrintf("RG\n");
3799   return offs;                                   3504   return offs;
3800 }                                                3505 }
3801                                                  3506 
3802 inline int tools_gl2psPrintPDFFillColor(tools << 3507 static int tools_gl2psPrintPDFFillColor(tools_GL2PSrgba rgba)
3803 {                                                3508 {
3804   int i, offs = 0;                               3509   int i, offs = 0;
3805                                                  3510 
3806   for(i = 0; i < 3; ++i){                        3511   for(i = 0; i < 3; ++i){
3807     if(TOOLS_GL2PS_ZERO(rgba[i]))                3512     if(TOOLS_GL2PS_ZERO(rgba[i]))
3808       offs += tools_gl2psPrintf(gl2ps,"%.0f " << 3513       offs += tools_gl2psPrintf("%.0f ", 0.);
3809     else if(rgba[i] < 1e-4 || rgba[i] > 1e6)     3514     else if(rgba[i] < 1e-4 || rgba[i] > 1e6) /* avoid %e formatting */
3810       offs += tools_gl2psPrintf(gl2ps,"%f ",  << 3515       offs += tools_gl2psPrintf("%f ", rgba[i]);
3811     else                                         3516     else
3812       offs += tools_gl2psPrintf(gl2ps,"%g ",  << 3517       offs += tools_gl2psPrintf("%g ", rgba[i]);
3813   }                                              3518   }
3814   offs += tools_gl2psPrintf(gl2ps,"rg\n");    << 3519   offs += tools_gl2psPrintf("rg\n");
3815   return offs;                                   3520   return offs;
3816 }                                                3521 }
3817                                                  3522 
3818 inline int tools_gl2psPrintPDFLineWidth(tools << 3523 static int tools_gl2psPrintPDFLineWidth(tools_GLfloat lw)
3819 {                                                3524 {
3820   if(TOOLS_GL2PS_ZERO(lw))                       3525   if(TOOLS_GL2PS_ZERO(lw))
3821     return tools_gl2psPrintf(gl2ps,"%.0f w\n" << 3526     return tools_gl2psPrintf("%.0f w\n", 0.);
3822   else if(lw < 1e-4 || lw > 1e6) /* avoid %e     3527   else if(lw < 1e-4 || lw > 1e6) /* avoid %e formatting */
3823     return tools_gl2psPrintf(gl2ps,"%f w\n",  << 3528     return tools_gl2psPrintf("%f w\n", lw);
3824   else                                           3529   else
3825     return tools_gl2psPrintf(gl2ps,"%g w\n",  << 3530     return tools_gl2psPrintf("%g w\n", lw);
3826 }                                                3531 }
3827                                                  3532 
3828 inline int tools_gl2psPrintPDFLineCap(tools_G << 3533 static int tools_gl2psPrintPDFLineCap(tools_GLint lc)
3829 {                                                3534 {
3830   if(gl2ps->lastlinecap == lc)                << 3535   if(tools_gl2ps_context->lastlinecap == lc)
3831     return 0;                                    3536     return 0;
3832   else                                           3537   else
3833     return tools_gl2psPrintf(gl2ps,"%d J\n",  << 3538     return tools_gl2psPrintf("%d J\n", lc);
3834 }                                                3539 }
3835                                                  3540 
3836 inline int tools_gl2psPrintPDFLineJoin(tools_ << 3541 static int tools_gl2psPrintPDFLineJoin(tools_GLint lj)
3837 {                                                3542 {
3838   if(gl2ps->lastlinejoin == lj)               << 3543   if(tools_gl2ps_context->lastlinejoin == lj)
3839     return 0;                                    3544     return 0;
3840   else                                           3545   else
3841     return tools_gl2psPrintf(gl2ps,"%d j\n",  << 3546     return tools_gl2psPrintf("%d j\n", lj);
3842 }                                                3547 }
3843                                                  3548 
3844 inline void tools_gl2psPutPDFText(tools_GL2PS << 3549 static void tools_gl2psPutPDFText(tools_GL2PSstring *text, int cnt, tools_GLfloat x, tools_GLfloat y)
3845 {                                                3550 {
3846   tools_GLfloat _rad, crad, srad;             << 3551   tools_GLfloat rad, crad, srad;
3847                                                  3552 
3848   if(text->angle == 0.0F){                       3553   if(text->angle == 0.0F){
3849     gl2ps->streamlength += tools_gl2psPrintf  << 3554     tools_gl2ps_context->streamlength += tools_gl2psPrintf
3850       (gl2ps, "BT\n"                          << 3555       ("BT\n"
3851        "/F%d %d Tf\n"                            3556        "/F%d %d Tf\n"
3852        "%f %f Td\n"                              3557        "%f %f Td\n"
3853        "(%s) Tj\n"                               3558        "(%s) Tj\n"
3854        "ET\n",                                   3559        "ET\n",
3855        cnt, text->fontsize, x, y, text->str);    3560        cnt, text->fontsize, x, y, text->str);
3856   }                                              3561   }
3857   else{                                          3562   else{
3858     _rad = (tools_GLfloat)(3.141593F * text-> << 3563     rad = (tools_GLfloat)(3.141593F * text->angle / 180.0F);
3859     srad = (tools_GLfloat)sin(_rad);          << 3564     srad = (tools_GLfloat)sin(rad);
3860     crad = (tools_GLfloat)cos(_rad);          << 3565     crad = (tools_GLfloat)cos(rad);
3861     gl2ps->streamlength += tools_gl2psPrintf  << 3566     tools_gl2ps_context->streamlength += tools_gl2psPrintf
3862       (gl2ps, "BT\n"                          << 3567       ("BT\n"
3863        "/F%d %d Tf\n"                            3568        "/F%d %d Tf\n"
3864        "%f %f %f %f %f %f Tm\n"                  3569        "%f %f %f %f %f %f Tm\n"
3865        "(%s) Tj\n"                               3570        "(%s) Tj\n"
3866        "ET\n",                                   3571        "ET\n",
3867        cnt, text->fontsize, crad, srad, -srad    3572        cnt, text->fontsize, crad, srad, -srad, crad, x, y, text->str);
3868   }                                              3573   }
3869 }                                                3574 }
3870                                                  3575 
3871 /*                                            << 3576 static void tools_gl2psPutPDFSpecial(int prim, int sec, tools_GL2PSstring *text)
3872   This is used for producing aligned text in  << 
3873   aligned text, (xbl, ybl) is the bottom left << 
3874   around (x, y).*/                            << 
3875 inline void tools_gl2psPutPDFTextBL(tools_GL2 << 
3876                               tools_GLfloat x << 
3877 {                                                3577 {
3878   if(text->angle == 0.0F){                    << 3578   tools_gl2ps_context->streamlength += tools_gl2psPrintf("/GS%d%d gs\n", prim, sec);
3879     gl2ps->streamlength += tools_gl2psPrintf  << 3579   tools_gl2ps_context->streamlength += tools_gl2psPrintf("%s\n", text->str);
3880       (gl2ps, "BT\n"                          << 
3881        "/F%d %d Tf\n"                         << 
3882        "%f %f Td\n"                           << 
3883        "(%s) Tj\n"                            << 
3884        "ET\n",                                << 
3885        cnt, text->fontsize, xbl, ybl, text->s << 
3886   }                                           << 
3887   else{                                       << 
3888     tools_GLfloat a, ca, sa;                  << 
3889     tools_GLfloat pi = 3.141593F;             << 
3890     tools_GLfloat i = atan2(y - ybl, x - xbl) << 
3891     tools_GLfloat r = sqrt((y - ybl) * (y - y << 
3892                                               << 
3893     a = (tools_GLfloat)(pi * text->angle / 18 << 
3894     sa = (tools_GLfloat)sin(a);               << 
3895     ca = (tools_GLfloat)cos(a);               << 
3896     gl2ps->streamlength += tools_gl2psPrintf  << 
3897       (gl2ps, "BT\n"                          << 
3898        "/F%d %d Tf\n"                         << 
3899        "%f %f %f %f %f %f Tm\n"               << 
3900        "(%s) Tj\n"                            << 
3901        "ET\n",                                << 
3902        cnt, text->fontsize,                   << 
3903        ca, sa, -sa, ca,                       << 
3904        xbl + r * (cos(i) - cos(i + a)), ybl + << 
3905   }                                           << 
3906 }                                             << 
3907                                               << 
3908 inline void tools_gl2psPutPDFSpecial(tools_GL << 
3909 {                                             << 
3910   gl2ps->streamlength += tools_gl2psPrintf(gl << 
3911   gl2ps->streamlength += tools_gl2psPrintf(gl << 
3912 }                                                3580 }
3913                                                  3581 
3914 inline void tools_gl2psPutPDFImage(tools_GL2P << 3582 static void tools_gl2psPutPDFImage(tools_GL2PSimage *image, int cnt, tools_GLfloat x, tools_GLfloat y)
3915 {                                                3583 {
3916   gl2ps->streamlength += tools_gl2psPrintf    << 3584   tools_gl2ps_context->streamlength += tools_gl2psPrintf
3917     (gl2ps, "q\n"                             << 3585     ("q\n"
3918      "%d 0 0 %d %f %f cm\n"                      3586      "%d 0 0 %d %f %f cm\n"
3919      "/Im%d Do\n"                                3587      "/Im%d Do\n"
3920      "Q\n",                                      3588      "Q\n",
3921      (int)(image->zoom_x * image->width), (in    3589      (int)(image->zoom_x * image->width), (int)(image->zoom_y * image->height),
3922      x, y, cnt);                                 3590      x, y, cnt);
3923 }                                                3591 }
3924                                                  3592 
3925 inline void tools_gl2psPDFstacksInit(tools_GL << 3593 static void tools_tools_gl2psPDFstacksInit(void)
3926 {                                                3594 {
3927   gl2ps->objects_stack = 7 /* FIXED_XREF_ENTR << 3595   tools_gl2ps_context->objects_stack = 7 /* FIXED_XREF_ENTRIES */ + 1;
3928   gl2ps->extgs_stack = 0;                     << 3596   tools_gl2ps_context->extgs_stack = 0;
3929   gl2ps->font_stack = 0;                      << 3597   tools_gl2ps_context->font_stack = 0;
3930   gl2ps->im_stack = 0;                        << 3598   tools_gl2ps_context->im_stack = 0;
3931   gl2ps->trgroupobjects_stack = 0;            << 3599   tools_gl2ps_context->trgroupobjects_stack = 0;
3932   gl2ps->shader_stack = 0;                    << 3600   tools_gl2ps_context->shader_stack = 0;
3933   gl2ps->mshader_stack = 0;                   << 3601   tools_gl2ps_context->mshader_stack = 0;
3934 }                                                3602 }
3935                                                  3603 
3936 inline void tools_gl2psPDFgroupObjectInit(too << 3604 static void tools_tools_gl2psPDFgroupObjectInit(tools_GL2PSpdfgroup *gro)
3937 {                                                3605 {
3938   if(!gro)                                       3606   if(!gro)
3939     return;                                      3607     return;
3940                                                  3608 
3941   gro->ptrlist = NULL;                           3609   gro->ptrlist = NULL;
3942   gro->fontno = gro->gsno = gro->imno = gro->    3610   gro->fontno = gro->gsno = gro->imno = gro->maskshno = gro->shno
3943     = gro->trgroupno = gro->fontobjno = gro->    3611     = gro->trgroupno = gro->fontobjno = gro->imobjno = gro->shobjno
3944     = gro->maskshobjno = gro->gsobjno = gro->    3612     = gro->maskshobjno = gro->gsobjno = gro->trgroupobjno = -1;
3945 }                                                3613 }
3946                                                  3614 
3947 /* Build up group objects and assign name and    3615 /* Build up group objects and assign name and object numbers */
3948                                                  3616 
3949 inline void tools_gl2psPDFgroupListInit(tools << 3617 static void tools_tools_gl2psPDFgroupListInit(void)
3950 {                                                3618 {
3951   int i;                                         3619   int i;
3952   tools_GL2PSprimitive *p = NULL;                3620   tools_GL2PSprimitive *p = NULL;
3953   tools_GL2PSpdfgroup gro;                       3621   tools_GL2PSpdfgroup gro;
3954   int lasttype = TOOLS_GL2PS_NO_TYPE;            3622   int lasttype = TOOLS_GL2PS_NO_TYPE;
3955   tools_GL2PSrgba lastrgba = {-1.0F, -1.0F, -    3623   tools_GL2PSrgba lastrgba = {-1.0F, -1.0F, -1.0F, -1.0F};
3956   tools_GLushort lastpattern = 0;                3624   tools_GLushort lastpattern = 0;
3957   tools_GLint lastfactor = 0;                    3625   tools_GLint lastfactor = 0;
3958   tools_GLfloat lastwidth = 1;                   3626   tools_GLfloat lastwidth = 1;
3959   tools_GLint lastlinecap = 0;                   3627   tools_GLint lastlinecap = 0;
3960   tools_GLint lastlinejoin = 0;                  3628   tools_GLint lastlinejoin = 0;
3961   tools_GL2PStriangle lastt, tmpt;               3629   tools_GL2PStriangle lastt, tmpt;
3962   int lastTriangleWasNotSimpleWithSameColor =    3630   int lastTriangleWasNotSimpleWithSameColor = 0;
3963                                                  3631 
3964   if(!gl2ps->pdfprimlist)                     << 3632   if(!tools_gl2ps_context->pdfprimlist)
3965     return;                                      3633     return;
3966                                                  3634 
3967   /*G.Barrand: add the below line to quiet Co << 3635   tools_gl2ps_context->pdfgrouplist = tools_gl2psListCreate(500, 500, sizeof(tools_GL2PSpdfgroup));
3968                in the below TOOLS_GL2PS_LINE, << 
3969   tools_gl2psPDFgroupObjectInit(&gro);        << 
3970                                               << 
3971   gl2ps->pdfgrouplist = tools_gl2psListCreate << 
3972   tools_gl2psInitTriangle(&lastt);               3636   tools_gl2psInitTriangle(&lastt);
3973                                                  3637 
3974   for(i = 0; i < tools_gl2psListNbr(gl2ps->pd << 3638   for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->pdfprimlist); ++i){
3975     p = *(tools_GL2PSprimitive**)tools_gl2psL << 3639     p = *(tools_GL2PSprimitive**)tools_gl2psListPointer(tools_gl2ps_context->pdfprimlist, i);
3976     switch(p->type){                             3640     switch(p->type){
3977     case TOOLS_GL2PS_PIXMAP:                     3641     case TOOLS_GL2PS_PIXMAP:
3978       tools_gl2psPDFgroupObjectInit(&gro);    << 3642       tools_tools_gl2psPDFgroupObjectInit(&gro);
3979       gro.ptrlist = tools_gl2psListCreate(1,     3643       gro.ptrlist = tools_gl2psListCreate(1, 2, sizeof(tools_GL2PSprimitive*));
3980       gro.imno = gl2ps->im_stack++;           << 3644       gro.imno = tools_gl2ps_context->im_stack++;
3981       tools_gl2psListAdd(gro.ptrlist, &p);       3645       tools_gl2psListAdd(gro.ptrlist, &p);
3982       tools_gl2psListAdd(gl2ps->pdfgrouplist, << 3646       tools_gl2psListAdd(tools_gl2ps_context->pdfgrouplist, &gro);
3983       break;                                     3647       break;
3984     case TOOLS_GL2PS_TEXT:                       3648     case TOOLS_GL2PS_TEXT:
3985       tools_gl2psPDFgroupObjectInit(&gro);    << 3649       tools_tools_gl2psPDFgroupObjectInit(&gro);
3986       gro.ptrlist = tools_gl2psListCreate(1,     3650       gro.ptrlist = tools_gl2psListCreate(1, 2, sizeof(tools_GL2PSprimitive*));
3987       gro.fontno = gl2ps->font_stack++;       << 3651       gro.fontno = tools_gl2ps_context->font_stack++;
3988       tools_gl2psListAdd(gro.ptrlist, &p);       3652       tools_gl2psListAdd(gro.ptrlist, &p);
3989       tools_gl2psListAdd(gl2ps->pdfgrouplist, << 3653       tools_gl2psListAdd(tools_gl2ps_context->pdfgrouplist, &gro);
3990       break;                                     3654       break;
3991     case TOOLS_GL2PS_LINE:                       3655     case TOOLS_GL2PS_LINE:
3992       if(lasttype != p->type || lastwidth !=     3656       if(lasttype != p->type || lastwidth != p->width ||
3993          lastlinecap != p->linecap || lastlin    3657          lastlinecap != p->linecap || lastlinejoin != p->linejoin ||
3994          lastpattern != p->pattern || lastfac    3658          lastpattern != p->pattern || lastfactor != p->factor ||
3995          !tools_gl2psSameColor(p->verts[0].rg    3659          !tools_gl2psSameColor(p->verts[0].rgba, lastrgba)){
3996         tools_gl2psPDFgroupObjectInit(&gro);  << 3660         tools_tools_gl2psPDFgroupObjectInit(&gro);
3997         gro.ptrlist = tools_gl2psListCreate(1    3661         gro.ptrlist = tools_gl2psListCreate(1, 2, sizeof(tools_GL2PSprimitive*));
3998         tools_gl2psListAdd(gro.ptrlist, &p);     3662         tools_gl2psListAdd(gro.ptrlist, &p);
3999         tools_gl2psListAdd(gl2ps->pdfgrouplis << 3663         tools_gl2psListAdd(tools_gl2ps_context->pdfgrouplist, &gro);
4000       }                                          3664       }
4001       else{                                      3665       else{
4002         tools_gl2psListAdd(gro.ptrlist, &p);     3666         tools_gl2psListAdd(gro.ptrlist, &p);
4003       }                                          3667       }
4004       lastpattern = p->pattern;                  3668       lastpattern = p->pattern;
4005       lastfactor = p->factor;                    3669       lastfactor = p->factor;
4006       lastwidth = p->width;                      3670       lastwidth = p->width;
4007       lastlinecap = p->linecap;                  3671       lastlinecap = p->linecap;
4008       lastlinejoin = p->linejoin;                3672       lastlinejoin = p->linejoin;
4009       lastrgba[0] = p->verts[0].rgba[0];         3673       lastrgba[0] = p->verts[0].rgba[0];
4010       lastrgba[1] = p->verts[0].rgba[1];         3674       lastrgba[1] = p->verts[0].rgba[1];
4011       lastrgba[2] = p->verts[0].rgba[2];         3675       lastrgba[2] = p->verts[0].rgba[2];
4012       break;                                     3676       break;
4013     case TOOLS_GL2PS_POINT:                      3677     case TOOLS_GL2PS_POINT:
4014       if(lasttype != p->type || lastwidth !=     3678       if(lasttype != p->type || lastwidth != p->width ||
4015          !tools_gl2psSameColor(p->verts[0].rg    3679          !tools_gl2psSameColor(p->verts[0].rgba, lastrgba)){
4016         tools_gl2psPDFgroupObjectInit(&gro);  << 3680         tools_tools_gl2psPDFgroupObjectInit(&gro);
4017         gro.ptrlist = tools_gl2psListCreate(1    3681         gro.ptrlist = tools_gl2psListCreate(1,2,sizeof(tools_GL2PSprimitive*));
4018         tools_gl2psListAdd(gro.ptrlist, &p);     3682         tools_gl2psListAdd(gro.ptrlist, &p);
4019         tools_gl2psListAdd(gl2ps->pdfgrouplis << 3683         tools_gl2psListAdd(tools_gl2ps_context->pdfgrouplist, &gro);
4020       }                                          3684       }
4021       else{                                      3685       else{
4022         tools_gl2psListAdd(gro.ptrlist, &p);     3686         tools_gl2psListAdd(gro.ptrlist, &p);
4023       }                                          3687       }
4024       lastwidth = p->width;                      3688       lastwidth = p->width;
4025       lastrgba[0] = p->verts[0].rgba[0];         3689       lastrgba[0] = p->verts[0].rgba[0];
4026       lastrgba[1] = p->verts[0].rgba[1];         3690       lastrgba[1] = p->verts[0].rgba[1];
4027       lastrgba[2] = p->verts[0].rgba[2];         3691       lastrgba[2] = p->verts[0].rgba[2];
4028       break;                                     3692       break;
4029     case TOOLS_GL2PS_TRIANGLE:                   3693     case TOOLS_GL2PS_TRIANGLE:
4030       tools_gl2psFillTriangleFromPrimitive(&t    3694       tools_gl2psFillTriangleFromPrimitive(&tmpt, p, TOOLS_GL_TRUE);
4031       lastTriangleWasNotSimpleWithSameColor =    3695       lastTriangleWasNotSimpleWithSameColor =
4032         !(tmpt.prop & T_CONST_COLOR && tmpt.p    3696         !(tmpt.prop & T_CONST_COLOR && tmpt.prop & T_ALPHA_1) ||
4033         !tools_gl2psSameColor(tmpt.vertex[0].    3697         !tools_gl2psSameColor(tmpt.vertex[0].rgba, lastt.vertex[0].rgba);
4034       if(lasttype == p->type && tmpt.prop ==     3698       if(lasttype == p->type && tmpt.prop == lastt.prop &&
4035          lastTriangleWasNotSimpleWithSameColo    3699          lastTriangleWasNotSimpleWithSameColor){
4036         /* TODO Check here for last alpha */     3700         /* TODO Check here for last alpha */
4037         tools_gl2psListAdd(gro.ptrlist, &p);     3701         tools_gl2psListAdd(gro.ptrlist, &p);
4038       }                                          3702       }
4039       else{                                      3703       else{
4040         tools_gl2psPDFgroupObjectInit(&gro);  << 3704         tools_tools_gl2psPDFgroupObjectInit(&gro);
4041         gro.ptrlist = tools_gl2psListCreate(1    3705         gro.ptrlist = tools_gl2psListCreate(1, 2, sizeof(tools_GL2PSprimitive*));
4042         tools_gl2psListAdd(gro.ptrlist, &p);     3706         tools_gl2psListAdd(gro.ptrlist, &p);
4043         tools_gl2psListAdd(gl2ps->pdfgrouplis << 3707         tools_gl2psListAdd(tools_gl2ps_context->pdfgrouplist, &gro);
4044       }                                          3708       }
4045       lastt = tmpt;                              3709       lastt = tmpt;
4046       break;                                     3710       break;
4047     case TOOLS_GL2PS_SPECIAL:                    3711     case TOOLS_GL2PS_SPECIAL:
4048       tools_gl2psPDFgroupObjectInit(&gro);    << 3712       tools_tools_gl2psPDFgroupObjectInit(&gro);
4049       gro.ptrlist = tools_gl2psListCreate(1,     3713       gro.ptrlist = tools_gl2psListCreate(1, 2, sizeof(tools_GL2PSprimitive*));
4050       tools_gl2psListAdd(gro.ptrlist, &p);       3714       tools_gl2psListAdd(gro.ptrlist, &p);
4051       tools_gl2psListAdd(gl2ps->pdfgrouplist, << 3715       tools_gl2psListAdd(tools_gl2ps_context->pdfgrouplist, &gro);
4052       break;                                     3716       break;
4053     default:                                     3717     default:
4054       break;                                     3718       break;
4055     }                                            3719     }
4056     lasttype = p->type;                          3720     lasttype = p->type;
4057   }                                              3721   }
4058 }                                                3722 }
4059                                                  3723 
4060 inline void tools_gl2psSortOutTrianglePDFgrou << 3724 static void tools_gl2psSortOutTrianglePDFgroup(tools_GL2PSpdfgroup *gro)
4061 {                                                3725 {
4062   tools_GL2PStriangle t;                         3726   tools_GL2PStriangle t;
4063   tools_GL2PSprimitive *prim = NULL;             3727   tools_GL2PSprimitive *prim = NULL;
4064                                                  3728 
4065   if(!gro)                                       3729   if(!gro)
4066     return;                                      3730     return;
4067                                                  3731 
4068   if(!tools_gl2psListNbr(gro->ptrlist))          3732   if(!tools_gl2psListNbr(gro->ptrlist))
4069     return;                                      3733     return;
4070                                                  3734 
4071   prim = *(tools_GL2PSprimitive**)tools_gl2ps    3735   prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, 0);
4072                                                  3736 
4073   if(prim->type != TOOLS_GL2PS_TRIANGLE)         3737   if(prim->type != TOOLS_GL2PS_TRIANGLE)
4074     return;                                      3738     return;
4075                                                  3739 
4076   tools_gl2psFillTriangleFromPrimitive(&t, pr    3740   tools_gl2psFillTriangleFromPrimitive(&t, prim, TOOLS_GL_TRUE);
4077                                                  3741 
4078   if(t.prop & T_CONST_COLOR && t.prop & T_ALP    3742   if(t.prop & T_CONST_COLOR && t.prop & T_ALPHA_LESS_1){
4079     gro->gsno = gl2ps->extgs_stack++;         << 3743     gro->gsno = tools_gl2ps_context->extgs_stack++;
4080     gro->gsobjno = gl2ps->objects_stack ++;   << 3744     gro->gsobjno = tools_gl2ps_context->objects_stack ++;
4081   }                                              3745   }
4082   else if(t.prop & T_CONST_COLOR && t.prop &     3746   else if(t.prop & T_CONST_COLOR && t.prop & T_VAR_ALPHA){
4083     gro->gsno = gl2ps->extgs_stack++;         << 3747     gro->gsno = tools_gl2ps_context->extgs_stack++;
4084     gro->gsobjno = gl2ps->objects_stack++;    << 3748     gro->gsobjno = tools_gl2ps_context->objects_stack++;
4085     gro->trgroupno = gl2ps->trgroupobjects_st << 3749     gro->trgroupno = tools_gl2ps_context->trgroupobjects_stack++;
4086     gro->trgroupobjno = gl2ps->objects_stack+ << 3750     gro->trgroupobjno = tools_gl2ps_context->objects_stack++;
4087     gro->maskshno = gl2ps->mshader_stack++;   << 3751     gro->maskshno = tools_gl2ps_context->mshader_stack++;
4088     gro->maskshobjno = gl2ps->objects_stack++ << 3752     gro->maskshobjno = tools_gl2ps_context->objects_stack++;
4089   }                                              3753   }
4090   else if(t.prop & T_VAR_COLOR && t.prop & T_    3754   else if(t.prop & T_VAR_COLOR && t.prop & T_ALPHA_1){
4091     gro->shno = gl2ps->shader_stack++;        << 3755     gro->shno = tools_gl2ps_context->shader_stack++;
4092     gro->shobjno = gl2ps->objects_stack++;    << 3756     gro->shobjno = tools_gl2ps_context->objects_stack++;
4093   }                                              3757   }
4094   else if(t.prop & T_VAR_COLOR && t.prop & T_    3758   else if(t.prop & T_VAR_COLOR && t.prop & T_ALPHA_LESS_1){
4095     gro->gsno = gl2ps->extgs_stack++;         << 3759     gro->gsno = tools_gl2ps_context->extgs_stack++;
4096     gro->gsobjno = gl2ps->objects_stack++;    << 3760     gro->gsobjno = tools_gl2ps_context->objects_stack++;
4097     gro->shno = gl2ps->shader_stack++;        << 3761     gro->shno = tools_gl2ps_context->shader_stack++;
4098     gro->shobjno = gl2ps->objects_stack++;    << 3762     gro->shobjno = tools_gl2ps_context->objects_stack++;
4099   }                                              3763   }
4100   else if(t.prop & T_VAR_COLOR && t.prop & T_    3764   else if(t.prop & T_VAR_COLOR && t.prop & T_VAR_ALPHA){
4101     gro->gsno = gl2ps->extgs_stack++;         << 3765     gro->gsno = tools_gl2ps_context->extgs_stack++;
4102     gro->gsobjno = gl2ps->objects_stack++;    << 3766     gro->gsobjno = tools_gl2ps_context->objects_stack++;
4103     gro->shno = gl2ps->shader_stack++;        << 3767     gro->shno = tools_gl2ps_context->shader_stack++;
4104     gro->shobjno = gl2ps->objects_stack++;    << 3768     gro->shobjno = tools_gl2ps_context->objects_stack++;
4105     gro->trgroupno = gl2ps->trgroupobjects_st << 3769     gro->trgroupno = tools_gl2ps_context->trgroupobjects_stack++;
4106     gro->trgroupobjno = gl2ps->objects_stack+ << 3770     gro->trgroupobjno = tools_gl2ps_context->objects_stack++;
4107     gro->maskshno = gl2ps->mshader_stack++;   << 3771     gro->maskshno = tools_gl2ps_context->mshader_stack++;
4108     gro->maskshobjno = gl2ps->objects_stack++ << 3772     gro->maskshobjno = tools_gl2ps_context->objects_stack++;
4109   }                                              3773   }
4110 }                                                3774 }
4111                                                  3775 
4112 /* Main stream data */                           3776 /* Main stream data */
4113                                                  3777 
4114 inline void tools_gl2psPDFgroupListWriteMainS << 3778 static void tools_tools_gl2psPDFgroupListWriteMainStream(void)
4115 {                                                3779 {
4116   int i, j, lastel, count;                       3780   int i, j, lastel, count;
4117   tools_GL2PSprimitive *prim = NULL, *prev =     3781   tools_GL2PSprimitive *prim = NULL, *prev = NULL;
4118   tools_GL2PSpdfgroup *gro;                      3782   tools_GL2PSpdfgroup *gro;
4119   tools_GL2PStriangle t;                         3783   tools_GL2PStriangle t;
4120                                                  3784 
4121   if(!gl2ps->pdfgrouplist)                    << 3785   if(!tools_gl2ps_context->pdfgrouplist)
4122     return;                                      3786     return;
4123                                                  3787 
4124   count = tools_gl2psListNbr(gl2ps->pdfgroupl << 3788   count = tools_gl2psListNbr(tools_gl2ps_context->pdfgrouplist);
4125                                                  3789 
4126   for(i = 0; i < count; ++i){                    3790   for(i = 0; i < count; ++i){
4127     gro = (tools_GL2PSpdfgroup*)tools_gl2psLi << 3791     gro = (tools_GL2PSpdfgroup*)tools_gl2psListPointer(tools_gl2ps_context->pdfgrouplist, i);
4128                                                  3792 
4129     lastel = tools_gl2psListNbr(gro->ptrlist)    3793     lastel = tools_gl2psListNbr(gro->ptrlist) - 1;
4130     if(lastel < 0)                               3794     if(lastel < 0)
4131       continue;                                  3795       continue;
4132                                                  3796 
4133     prim = *(tools_GL2PSprimitive**)tools_gl2    3797     prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, 0);
4134                                                  3798 
4135     switch(prim->type){                          3799     switch(prim->type){
4136     case TOOLS_GL2PS_POINT:                      3800     case TOOLS_GL2PS_POINT:
4137       gl2ps->streamlength += tools_gl2psPrint << 3801       tools_gl2ps_context->streamlength += tools_gl2psPrintf("1 J\n");
4138       gl2ps->streamlength += tools_gl2psPrint << 3802       tools_gl2ps_context->streamlength += tools_gl2psPrintPDFLineWidth(prim->width);
4139       gl2ps->streamlength += tools_gl2psPrint << 3803       tools_gl2ps_context->streamlength += tools_gl2psPrintPDFStrokeColor(prim->verts[0].rgba);
4140       for(j = 0; j <= lastel; ++j){              3804       for(j = 0; j <= lastel; ++j){
4141         prim = *(tools_GL2PSprimitive**)tools    3805         prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
4142         gl2ps->streamlength +=                << 3806         tools_gl2ps_context->streamlength +=
4143           tools_gl2psPrintf(gl2ps,"%f %f m %f << 3807           tools_gl2psPrintf("%f %f m %f %f l\n",
4144                       prim->verts[0].xyz[0],     3808                       prim->verts[0].xyz[0], prim->verts[0].xyz[1],
4145                       prim->verts[0].xyz[0],     3809                       prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
4146       }                                          3810       }
4147       gl2ps->streamlength += tools_gl2psPrint << 3811       tools_gl2ps_context->streamlength += tools_gl2psPrintf("S\n");
4148       gl2ps->streamlength += tools_gl2psPrint << 3812       tools_gl2ps_context->streamlength += tools_gl2psPrintf("0 J\n");
4149       break;                                     3813       break;
4150     case TOOLS_GL2PS_LINE:                       3814     case TOOLS_GL2PS_LINE:
4151       /* We try to use as few paths as possib    3815       /* We try to use as few paths as possible to draw lines, in
4152          order to get nice stippling even whe    3816          order to get nice stippling even when the individual segments
4153          are smaller than the stipple */         3817          are smaller than the stipple */
4154       gl2ps->streamlength += tools_gl2psPrint << 3818       tools_gl2ps_context->streamlength += tools_gl2psPrintPDFLineWidth(prim->width);
4155       gl2ps->streamlength += tools_gl2psPrint << 3819       tools_gl2ps_context->streamlength += tools_gl2psPrintPDFLineCap(prim->linecap);
4156       gl2ps->streamlength += tools_gl2psPrint << 3820       tools_gl2ps_context->streamlength += tools_gl2psPrintPDFLineJoin(prim->linejoin);
4157       gl2ps->streamlength += tools_gl2psPrint << 3821       tools_gl2ps_context->streamlength += tools_gl2psPrintPDFStrokeColor(prim->verts[0].rgba);
4158       gl2ps->streamlength += tools_gl2psPrint << 3822       tools_gl2ps_context->streamlength += tools_gl2psPrintPostScriptDash(prim->pattern, prim->factor, "d");
4159       /* start new path */                       3823       /* start new path */
4160       gl2ps->streamlength +=                  << 3824       tools_gl2ps_context->streamlength +=
4161         tools_gl2psPrintf(gl2ps,"%f %f m\n",  << 3825         tools_gl2psPrintf("%f %f m\n",
4162                     prim->verts[0].xyz[0], pr    3826                     prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
4163                                                  3827 
4164       for(j = 1; j <= lastel; ++j){              3828       for(j = 1; j <= lastel; ++j){
4165         prev = prim;                             3829         prev = prim;
4166         prim = *(tools_GL2PSprimitive**)tools    3830         prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
4167         if(!tools_gl2psSamePosition(prim->ver    3831         if(!tools_gl2psSamePosition(prim->verts[0].xyz, prev->verts[1].xyz)){
4168           /* the starting point of the new se    3832           /* the starting point of the new segment does not match the
4169              end point of the previous line,     3833              end point of the previous line, so we end the current
4170              path and start a new one */         3834              path and start a new one */
4171           gl2ps->streamlength +=              << 3835           tools_gl2ps_context->streamlength +=
4172             tools_gl2psPrintf(gl2ps,"%f %f l\ << 3836             tools_gl2psPrintf("%f %f l\n",
4173                         prev->verts[1].xyz[0]    3837                         prev->verts[1].xyz[0], prev->verts[1].xyz[1]);
4174           gl2ps->streamlength +=              << 3838           tools_gl2ps_context->streamlength +=
4175             tools_gl2psPrintf(gl2ps,"%f %f m\ << 3839             tools_gl2psPrintf("%f %f m\n",
4176                         prim->verts[0].xyz[0]    3840                         prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
4177         }                                        3841         }
4178         else{                                    3842         else{
4179           /* the two segements are connected,    3843           /* the two segements are connected, so we just append to the
4180              current path */                     3844              current path */
4181           gl2ps->streamlength +=              << 3845           tools_gl2ps_context->streamlength +=
4182             tools_gl2psPrintf(gl2ps,"%f %f l\ << 3846             tools_gl2psPrintf("%f %f l\n",
4183                         prim->verts[0].xyz[0]    3847                         prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
4184         }                                        3848         }
4185       }                                          3849       }
4186       /* end last path */                        3850       /* end last path */
4187       gl2ps->streamlength +=                  << 3851       tools_gl2ps_context->streamlength +=
4188         tools_gl2psPrintf(gl2ps,"%f %f l\n",  << 3852         tools_gl2psPrintf("%f %f l\n",
4189                     prim->verts[1].xyz[0], pr    3853                     prim->verts[1].xyz[0], prim->verts[1].xyz[1]);
4190       gl2ps->streamlength += tools_gl2psPrint << 3854       tools_gl2ps_context->streamlength += tools_gl2psPrintf("S\n");
4191       break;                                     3855       break;
4192     case TOOLS_GL2PS_TRIANGLE:                   3856     case TOOLS_GL2PS_TRIANGLE:
4193       tools_gl2psFillTriangleFromPrimitive(&t    3857       tools_gl2psFillTriangleFromPrimitive(&t, prim, TOOLS_GL_TRUE);
4194       tools_gl2psSortOutTrianglePDFgroup(gl2p << 3858       tools_gl2psSortOutTrianglePDFgroup(gro);
4195                                                  3859 
4196       /* No alpha and const color: Simple PDF    3860       /* No alpha and const color: Simple PDF draw orders  */
4197       if(t.prop & T_CONST_COLOR && t.prop & T    3861       if(t.prop & T_CONST_COLOR && t.prop & T_ALPHA_1){
4198         gl2ps->streamlength += tools_gl2psPri << 3862         tools_gl2ps_context->streamlength += tools_gl2psPrintPDFFillColor(t.vertex[0].rgba);
4199         for(j = 0; j <= lastel; ++j){            3863         for(j = 0; j <= lastel; ++j){
4200           prim = *(tools_GL2PSprimitive**)too    3864           prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
4201           tools_gl2psFillTriangleFromPrimitiv    3865           tools_gl2psFillTriangleFromPrimitive(&t, prim, TOOLS_GL_FALSE);
4202           gl2ps->streamlength                 << 3866           tools_gl2ps_context->streamlength
4203             += tools_gl2psPrintf(gl2ps,"%f %f << 3867             += tools_gl2psPrintf("%f %f m\n"
4204                            "%f %f l\n"           3868                            "%f %f l\n"
4205                            "%f %f l\n"           3869                            "%f %f l\n"
4206                            "h f\n",              3870                            "h f\n",
4207                            t.vertex[0].xyz[0]    3871                            t.vertex[0].xyz[0], t.vertex[0].xyz[1],
4208                            t.vertex[1].xyz[0]    3872                            t.vertex[1].xyz[0], t.vertex[1].xyz[1],
4209                            t.vertex[2].xyz[0]    3873                            t.vertex[2].xyz[0], t.vertex[2].xyz[1]);
4210         }                                        3874         }
4211       }                                          3875       }
4212       /* Const alpha < 1 and const color: Sim    3876       /* Const alpha < 1 and const color: Simple PDF draw orders
4213          and an extra extended Graphics State    3877          and an extra extended Graphics State for the alpha const */
4214       else if(t.prop & T_CONST_COLOR && t.pro    3878       else if(t.prop & T_CONST_COLOR && t.prop & T_ALPHA_LESS_1){
4215         gl2ps->streamlength += tools_gl2psPri << 3879         tools_gl2ps_context->streamlength += tools_gl2psPrintf("q\n"
4216                                            "/    3880                                            "/GS%d gs\n",
4217                                            gr    3881                                            gro->gsno);
4218         gl2ps->streamlength += tools_gl2psPri << 3882         tools_gl2ps_context->streamlength += tools_gl2psPrintPDFFillColor(prim->verts[0].rgba);
4219         for(j = 0; j <= lastel; ++j){            3883         for(j = 0; j <= lastel; ++j){
4220           prim = *(tools_GL2PSprimitive**)too    3884           prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
4221           tools_gl2psFillTriangleFromPrimitiv    3885           tools_gl2psFillTriangleFromPrimitive(&t, prim, TOOLS_GL_FALSE);
4222           gl2ps->streamlength                 << 3886           tools_gl2ps_context->streamlength
4223             += tools_gl2psPrintf(gl2ps,"%f %f << 3887             += tools_gl2psPrintf("%f %f m\n"
4224                            "%f %f l\n"           3888                            "%f %f l\n"
4225                            "%f %f l\n"           3889                            "%f %f l\n"
4226                            "h f\n",              3890                            "h f\n",
4227                            t.vertex[0].xyz[0]    3891                            t.vertex[0].xyz[0], t.vertex[0].xyz[1],
4228                            t.vertex[1].xyz[0]    3892                            t.vertex[1].xyz[0], t.vertex[1].xyz[1],
4229                            t.vertex[2].xyz[0]    3893                            t.vertex[2].xyz[0], t.vertex[2].xyz[1]);
4230         }                                        3894         }
4231         gl2ps->streamlength += tools_gl2psPri << 3895         tools_gl2ps_context->streamlength += tools_gl2psPrintf("Q\n");
4232       }                                          3896       }
4233       /* Variable alpha and const color: Simp    3897       /* Variable alpha and const color: Simple PDF draw orders
4234          and an extra extended Graphics State    3898          and an extra extended Graphics State + Xobject + Shader
4235          object for the alpha mask */            3899          object for the alpha mask */
4236       else if(t.prop & T_CONST_COLOR && t.pro    3900       else if(t.prop & T_CONST_COLOR && t.prop & T_VAR_ALPHA){
4237         gl2ps->streamlength += tools_gl2psPri << 3901         tools_gl2ps_context->streamlength += tools_gl2psPrintf("q\n"
4238                                            "/    3902                                            "/GS%d gs\n"
4239                                            "/    3903                                            "/TrG%d Do\n",
4240                                            gr    3904                                            gro->gsno, gro->trgroupno);
4241         gl2ps->streamlength += tools_gl2psPri << 3905         tools_gl2ps_context->streamlength += tools_gl2psPrintPDFFillColor(prim->verts[0].rgba);
4242         for(j = 0; j <= lastel; ++j){            3906         for(j = 0; j <= lastel; ++j){
4243           prim = *(tools_GL2PSprimitive**)too    3907           prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
4244           tools_gl2psFillTriangleFromPrimitiv    3908           tools_gl2psFillTriangleFromPrimitive(&t, prim, TOOLS_GL_FALSE);
4245           gl2ps->streamlength                 << 3909           tools_gl2ps_context->streamlength
4246             += tools_gl2psPrintf(gl2ps,"%f %f << 3910             += tools_gl2psPrintf("%f %f m\n"
4247                            "%f %f l\n"           3911                            "%f %f l\n"
4248                            "%f %f l\n"           3912                            "%f %f l\n"
4249                            "h f\n",              3913                            "h f\n",
4250                            t.vertex[0].xyz[0]    3914                            t.vertex[0].xyz[0], t.vertex[0].xyz[1],
4251                            t.vertex[1].xyz[0]    3915                            t.vertex[1].xyz[0], t.vertex[1].xyz[1],
4252                            t.vertex[2].xyz[0]    3916                            t.vertex[2].xyz[0], t.vertex[2].xyz[1]);
4253         }                                        3917         }
4254         gl2ps->streamlength += tools_gl2psPri << 3918         tools_gl2ps_context->streamlength += tools_gl2psPrintf("Q\n");
4255       }                                          3919       }
4256       /* Variable color and no alpha: Shader     3920       /* Variable color and no alpha: Shader Object for the colored
4257          triangle(s) */                          3921          triangle(s) */
4258       else if(t.prop & T_VAR_COLOR && t.prop     3922       else if(t.prop & T_VAR_COLOR && t.prop & T_ALPHA_1){
4259         gl2ps->streamlength += tools_gl2psPri << 3923         tools_gl2ps_context->streamlength += tools_gl2psPrintf("/Sh%d sh\n", gro->shno);
4260       }                                          3924       }
4261       /* Variable color and const alpha < 1:     3925       /* Variable color and const alpha < 1: Shader Object for the
4262          colored triangle(s) and an extra ext    3926          colored triangle(s) and an extra extended Graphics State
4263          for the alpha const */                  3927          for the alpha const */
4264       else if(t.prop & T_VAR_COLOR && t.prop     3928       else if(t.prop & T_VAR_COLOR && t.prop & T_ALPHA_LESS_1){
4265         gl2ps->streamlength += tools_gl2psPri << 3929         tools_gl2ps_context->streamlength += tools_gl2psPrintf("q\n"
4266                                            "/    3930                                            "/GS%d gs\n"
4267                                            "/    3931                                            "/Sh%d sh\n"
4268                                            "Q    3932                                            "Q\n",
4269                                            gr    3933                                            gro->gsno, gro->shno);
4270       }                                          3934       }
4271       /* Variable alpha and color: Shader Obj    3935       /* Variable alpha and color: Shader Object for the colored
4272          triangle(s) and an extra extended Gr    3936          triangle(s) and an extra extended Graphics State
4273          + Xobject + Shader object for the al    3937          + Xobject + Shader object for the alpha mask */
4274       else if(t.prop & T_VAR_COLOR && t.prop     3938       else if(t.prop & T_VAR_COLOR && t.prop & T_VAR_ALPHA){
4275         gl2ps->streamlength += tools_gl2psPri << 3939         tools_gl2ps_context->streamlength += tools_gl2psPrintf("q\n"
4276                                            "/    3940                                            "/GS%d gs\n"
4277                                            "/    3941                                            "/TrG%d Do\n"
4278                                            "/    3942                                            "/Sh%d sh\n"
4279                                            "Q    3943                                            "Q\n",
4280                                            gr    3944                                            gro->gsno, gro->trgroupno, gro->shno);
4281       }                                          3945       }
4282       break;                                     3946       break;
4283     case TOOLS_GL2PS_PIXMAP:                     3947     case TOOLS_GL2PS_PIXMAP:
4284       for(j = 0; j <= lastel; ++j){              3948       for(j = 0; j <= lastel; ++j){
4285         prim = *(tools_GL2PSprimitive**)tools    3949         prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
4286         tools_gl2psPutPDFImage(gl2ps, prim->d << 3950         tools_gl2psPutPDFImage(prim->data.image, gro->imno, prim->verts[0].xyz[0],
4287                          prim->verts[0].xyz[1    3951                          prim->verts[0].xyz[1]);
4288       }                                          3952       }
4289       break;                                     3953       break;
4290     case TOOLS_GL2PS_TEXT:                       3954     case TOOLS_GL2PS_TEXT:
4291       for(j = 0; j <= lastel; ++j){              3955       for(j = 0; j <= lastel; ++j){
4292         prim = *(tools_GL2PSprimitive**)tools    3956         prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
4293         gl2ps->streamlength += tools_gl2psPri << 3957         tools_gl2ps_context->streamlength += tools_gl2psPrintPDFFillColor(prim->verts[0].rgba);
4294         if (prim->numverts == 2) {            << 3958         tools_gl2psPutPDFText(prim->data.text, gro->fontno, prim->verts[0].xyz[0],
4295           tools_gl2psPutPDFTextBL(gl2ps, prim << 3959                         prim->verts[0].xyz[1]);
4296                             prim->verts[0].xy << 
4297                             prim->verts[1].xy << 
4298                             prim->verts[1].xy << 
4299         }                                     << 
4300         else {                                << 
4301           tools_gl2psPutPDFText(gl2ps, prim-> << 
4302                           prim->verts[0].xyz[ << 
4303         }                                     << 
4304       }                                          3960       }
4305       break;                                     3961       break;
4306     case TOOLS_GL2PS_SPECIAL:                    3962     case TOOLS_GL2PS_SPECIAL:
4307       lastel = tools_gl2psListNbr(gro->ptrlis    3963       lastel = tools_gl2psListNbr(gro->ptrlist) - 1;
4308       if(lastel < 0)                             3964       if(lastel < 0)
4309         continue;                                3965         continue;
4310                                                  3966 
4311       for(j = 0; j <= lastel; ++j){              3967       for(j = 0; j <= lastel; ++j){
4312         prim = *(tools_GL2PSprimitive**)tools    3968         prim = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
4313         tools_gl2psPutPDFSpecial(gl2ps, i, j, << 3969         tools_gl2psPutPDFSpecial(i, j, prim->data.text);
4314       }                                          3970       }
4315     default:                                     3971     default:
4316       break;                                     3972       break;
4317     }                                            3973     }
4318   }                                              3974   }
4319 }                                                3975 }
4320                                                  3976 
4321 /* Graphics State names */                       3977 /* Graphics State names */
4322                                                  3978 
4323 inline int tools_gl2psPDFgroupListWriteGState << 3979 static int tools_tools_gl2psPDFgroupListWriteGStateResources(void)
4324 {                                                3980 {
4325   tools_GL2PSpdfgroup *gro;                      3981   tools_GL2PSpdfgroup *gro;
4326   int offs = 0;                                  3982   int offs = 0;
4327   int i;                                         3983   int i;
4328                                                  3984 
4329   offs += fprintf(gl2ps->stream,              << 3985   offs += fprintf(tools_gl2ps_context->stream,
4330                   "/ExtGState\n"                 3986                   "/ExtGState\n"
4331                   "<<\n"                         3987                   "<<\n"
4332                   "/GSa 7 0 R\n");               3988                   "/GSa 7 0 R\n");
4333   for(i = 0; i < tools_gl2psListNbr(gl2ps->pd << 3989   for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->pdfgrouplist); ++i){
4334     gro = (tools_GL2PSpdfgroup*)tools_gl2psLi << 3990     gro = (tools_GL2PSpdfgroup*)tools_gl2psListPointer(tools_gl2ps_context->pdfgrouplist, i);
4335     if(gro->gsno >= 0)                           3991     if(gro->gsno >= 0)
4336       offs += fprintf(gl2ps->stream, "/GS%d % << 3992       offs += fprintf(tools_gl2ps_context->stream, "/GS%d %d 0 R\n", gro->gsno, gro->gsobjno);
4337   }                                              3993   }
4338   offs += fprintf(gl2ps->stream, ">>\n");     << 3994   offs += fprintf(tools_gl2ps_context->stream, ">>\n");
4339   return offs;                                   3995   return offs;
4340 }                                                3996 }
4341                                                  3997 
4342 /* Main Shader names */                          3998 /* Main Shader names */
4343                                                  3999 
4344 inline int tools_gl2psPDFgroupListWriteShader << 4000 static int tools_tools_gl2psPDFgroupListWriteShaderResources(void)
4345 {                                                4001 {
4346   tools_GL2PSpdfgroup *gro;                      4002   tools_GL2PSpdfgroup *gro;
4347   int offs = 0;                                  4003   int offs = 0;
4348   int i;                                         4004   int i;
4349                                                  4005 
4350   offs += fprintf(gl2ps->stream,              << 4006   offs += fprintf(tools_gl2ps_context->stream,
4351                   "/Shading\n"                   4007                   "/Shading\n"
4352                   "<<\n");                       4008                   "<<\n");
4353   for(i = 0; i < tools_gl2psListNbr(gl2ps->pd << 4009   for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->pdfgrouplist); ++i){
4354     gro = (tools_GL2PSpdfgroup*)tools_gl2psLi << 4010     gro = (tools_GL2PSpdfgroup*)tools_gl2psListPointer(tools_gl2ps_context->pdfgrouplist, i);
4355     if(gro->shno >= 0)                           4011     if(gro->shno >= 0)
4356       offs += fprintf(gl2ps->stream, "/Sh%d % << 4012       offs += fprintf(tools_gl2ps_context->stream, "/Sh%d %d 0 R\n", gro->shno, gro->shobjno);
4357     if(gro->maskshno >= 0)                       4013     if(gro->maskshno >= 0)
4358       offs += fprintf(gl2ps->stream, "/TrSh%d << 4014       offs += fprintf(tools_gl2ps_context->stream, "/TrSh%d %d 0 R\n", gro->maskshno, gro->maskshobjno);
4359   }                                              4015   }
4360   offs += fprintf(gl2ps->stream,">>\n");      << 4016   offs += fprintf(tools_gl2ps_context->stream,">>\n");
4361   return offs;                                   4017   return offs;
4362 }                                                4018 }
4363                                                  4019 
4364 /* Images & Mask Shader XObject names */         4020 /* Images & Mask Shader XObject names */
4365 inline int tools_gl2psPDFgroupListWriteXObjec << 4021 static int tools_tools_gl2psPDFgroupListWriteXObjectResources(void)
4366 {                                                4022 {
4367   int i;                                         4023   int i;
4368   tools_GL2PSprimitive *p = NULL;                4024   tools_GL2PSprimitive *p = NULL;
4369   tools_GL2PSpdfgroup *gro;                      4025   tools_GL2PSpdfgroup *gro;
4370   int offs = 0;                                  4026   int offs = 0;
4371                                                  4027 
4372   offs += fprintf(gl2ps->stream,              << 4028   offs += fprintf(tools_gl2ps_context->stream,
4373                   "/XObject\n"                   4029                   "/XObject\n"
4374                   "<<\n");                       4030                   "<<\n");
4375                                                  4031 
4376   for(i = 0; i < tools_gl2psListNbr(gl2ps->pd << 4032   for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->pdfgrouplist); ++i){
4377     gro = (tools_GL2PSpdfgroup*)tools_gl2psLi << 4033     gro = (tools_GL2PSpdfgroup*)tools_gl2psListPointer(tools_gl2ps_context->pdfgrouplist, i);
4378     if(!tools_gl2psListNbr(gro->ptrlist))        4034     if(!tools_gl2psListNbr(gro->ptrlist))
4379       continue;                                  4035       continue;
4380     p = *(tools_GL2PSprimitive**)tools_gl2psL    4036     p = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, 0);
4381     switch(p->type){                             4037     switch(p->type){
4382     case TOOLS_GL2PS_PIXMAP:                     4038     case TOOLS_GL2PS_PIXMAP:
4383       gro->imobjno = gl2ps->objects_stack++;  << 4039       gro->imobjno = tools_gl2ps_context->objects_stack++;
4384       if(TOOLS_GL_RGBA == p->data.image->form    4040       if(TOOLS_GL_RGBA == p->data.image->format)  /* reserve one object for image mask */
4385         gl2ps->objects_stack++;               << 4041         tools_gl2ps_context->objects_stack++;
4386       offs += fprintf(gl2ps->stream, "/Im%d % << 4042       offs += fprintf(tools_gl2ps_context->stream, "/Im%d %d 0 R\n", gro->imno, gro->imobjno);
4387       break; /*G.Barrand : add this break.*/     4043       break; /*G.Barrand : add this break.*/
4388     case TOOLS_GL2PS_TRIANGLE:                   4044     case TOOLS_GL2PS_TRIANGLE:
4389       if(gro->trgroupno >=0)                     4045       if(gro->trgroupno >=0)
4390         offs += fprintf(gl2ps->stream, "/TrG% << 4046         offs += fprintf(tools_gl2ps_context->stream, "/TrG%d %d 0 R\n", gro->trgroupno, gro->trgroupobjno);
4391       break;                                     4047       break;
4392     default:                                     4048     default:
4393       break;                                     4049       break;
4394     }                                            4050     }
4395   }                                              4051   }
4396   offs += fprintf(gl2ps->stream,">>\n");      << 4052   offs += fprintf(tools_gl2ps_context->stream,">>\n");
4397   return offs;                                   4053   return offs;
4398 }                                                4054 }
4399                                                  4055 
4400 /* Font names */                                 4056 /* Font names */
4401                                                  4057 
4402 inline int tools_gl2psPDFgroupListWriteFontRe << 4058 static int tools_tools_gl2psPDFgroupListWriteFontResources(void)
4403 {                                                4059 {
4404   int i;                                         4060   int i;
4405   tools_GL2PSpdfgroup *gro;                      4061   tools_GL2PSpdfgroup *gro;
4406   int offs = 0;                                  4062   int offs = 0;
4407                                                  4063 
4408   offs += fprintf(gl2ps->stream, "/Font\n<<\n << 4064   offs += fprintf(tools_gl2ps_context->stream, "/Font\n<<\n");
4409                                                  4065 
4410   for(i = 0; i < tools_gl2psListNbr(gl2ps->pd << 4066   for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->pdfgrouplist); ++i){
4411     gro = (tools_GL2PSpdfgroup*)tools_gl2psLi << 4067     gro = (tools_GL2PSpdfgroup*)tools_gl2psListPointer(tools_gl2ps_context->pdfgrouplist, i);
4412     if(gro->fontno < 0)                          4068     if(gro->fontno < 0)
4413       continue;                                  4069       continue;
4414     gro->fontobjno = gl2ps->objects_stack++;  << 4070     gro->fontobjno = tools_gl2ps_context->objects_stack++;
4415     offs += fprintf(gl2ps->stream, "/F%d %d 0 << 4071     offs += fprintf(tools_gl2ps_context->stream, "/F%d %d 0 R\n", gro->fontno, gro->fontobjno);
4416   }                                              4072   }
4417   offs += fprintf(gl2ps->stream, ">>\n");     << 4073   offs += fprintf(tools_gl2ps_context->stream, ">>\n");
4418                                                  4074 
4419   return offs;                                   4075   return offs;
4420 }                                                4076 }
4421                                                  4077 
4422 inline void tools_gl2psPDFgroupListDelete(too << 4078 static void tools_tools_gl2psPDFgroupListDelete(void)
4423 {                                                4079 {
4424   int i;                                         4080   int i;
4425   tools_GL2PSpdfgroup *gro = NULL;               4081   tools_GL2PSpdfgroup *gro = NULL;
4426                                                  4082 
4427   if(!gl2ps->pdfgrouplist)                    << 4083   if(!tools_gl2ps_context->pdfgrouplist)
4428     return;                                      4084     return;
4429                                                  4085 
4430   for(i = 0; i < tools_gl2psListNbr(gl2ps->pd << 4086   for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->pdfgrouplist); ++i){
4431     gro = (tools_GL2PSpdfgroup*)tools_gl2psLi << 4087     gro = (tools_GL2PSpdfgroup*)tools_gl2psListPointer(tools_gl2ps_context->pdfgrouplist,i);
4432     tools_gl2psListDelete(gro->ptrlist);         4088     tools_gl2psListDelete(gro->ptrlist);
4433   }                                              4089   }
4434                                                  4090 
4435   tools_gl2psListDelete(gl2ps->pdfgrouplist); << 4091   tools_gl2psListDelete(tools_gl2ps_context->pdfgrouplist);
4436   gl2ps->pdfgrouplist = NULL;                 << 4092   tools_gl2ps_context->pdfgrouplist = NULL;
4437 }                                                4093 }
4438                                                  4094 
4439 /* Print 1st PDF object - file info */           4095 /* Print 1st PDF object - file info */
4440                                                  4096 
4441 inline int tools_gl2psPrintPDFInfo(tools_GL2P << 4097 static int tools_gl2psPrintPDFInfo(void)
4442 {                                                4098 {
4443   int offs;                                      4099   int offs;
4444   time_t now;                                    4100   time_t now;
4445   struct tm *newtime;                            4101   struct tm *newtime;
4446                                                  4102 
4447   time(&now);                                    4103   time(&now);
4448   newtime = gmtime(&now);                        4104   newtime = gmtime(&now);
4449                                                  4105 
4450   offs = fprintf(gl2ps->stream,               << 4106   offs = fprintf(tools_gl2ps_context->stream,
4451                  "1 0 obj\n"                     4107                  "1 0 obj\n"
4452                  "<<\n"                          4108                  "<<\n"
4453                  "/Title (%s)\n"                 4109                  "/Title (%s)\n"
4454                  "/Creator (GL2PS %d.%d.%d%s,    4110                  "/Creator (GL2PS %d.%d.%d%s, %s)\n"
4455                  "/Producer (%s)\n",             4111                  "/Producer (%s)\n",
4456                  gl2ps->title, TOOLS_GL2PS_MA << 4112                  tools_gl2ps_context->title, TOOLS_GL2PS_MAJOR_VERSION, TOOLS_GL2PS_MINOR_VERSION,
4457                  TOOLS_GL2PS_PATCH_VERSION, T    4113                  TOOLS_GL2PS_PATCH_VERSION, TOOLS_GL2PS_EXTRA_VERSION, TOOLS_GL2PS_COPYRIGHT,
4458                  gl2ps->producer);            << 4114                  tools_gl2ps_context->producer);
4459                                                  4115 
4460   if(!newtime){                                  4116   if(!newtime){
4461     offs += fprintf(gl2ps->stream,            << 4117     offs += fprintf(tools_gl2ps_context->stream,
4462                     ">>\n"                       4118                     ">>\n"
4463                     "endobj\n");                 4119                     "endobj\n");
4464     return offs;                                 4120     return offs;
4465   }                                              4121   }
4466                                                  4122 
4467   offs += fprintf(gl2ps->stream,              << 4123   offs += fprintf(tools_gl2ps_context->stream,
4468                   "/CreationDate (D:%d%02d%02    4124                   "/CreationDate (D:%d%02d%02d%02d%02d%02d)\n"
4469                   ">>\n"                         4125                   ">>\n"
4470                   "endobj\n",                    4126                   "endobj\n",
4471                   newtime->tm_year+1900,         4127                   newtime->tm_year+1900,
4472                   newtime->tm_mon+1,             4128                   newtime->tm_mon+1,
4473                   newtime->tm_mday,              4129                   newtime->tm_mday,
4474                   newtime->tm_hour,              4130                   newtime->tm_hour,
4475                   newtime->tm_min,               4131                   newtime->tm_min,
4476                   newtime->tm_sec);              4132                   newtime->tm_sec);
4477   return offs;                                   4133   return offs;
4478 }                                                4134 }
4479                                                  4135 
4480 /* Create catalog and page structure - 2nd an    4136 /* Create catalog and page structure - 2nd and 3th PDF object */
4481                                                  4137 
4482 inline int tools_gl2psPrintPDFCatalog(tools_G << 4138 static int tools_gl2psPrintPDFCatalog(void)
4483 {                                                4139 {
4484   return fprintf(gl2ps->stream,               << 4140   return fprintf(tools_gl2ps_context->stream,
4485                  "2 0 obj\n"                     4141                  "2 0 obj\n"
4486                  "<<\n"                          4142                  "<<\n"
4487                  "/Type /Catalog\n"              4143                  "/Type /Catalog\n"
4488                  "/Pages 3 0 R\n"                4144                  "/Pages 3 0 R\n"
4489                  ">>\n"                          4145                  ">>\n"
4490                  "endobj\n");                    4146                  "endobj\n");
4491 }                                                4147 }
4492                                                  4148 
4493 inline int tools_gl2psPrintPDFPages(tools_GL2 << 4149 static int tools_gl2psPrintPDFPages(void)
4494 {                                                4150 {
4495   return fprintf(gl2ps->stream,               << 4151   return fprintf(tools_gl2ps_context->stream,
4496                  "3 0 obj\n"                     4152                  "3 0 obj\n"
4497                  "<<\n"                          4153                  "<<\n"
4498                  "/Type /Pages\n"                4154                  "/Type /Pages\n"
4499                  "/Kids [6 0 R]\n"               4155                  "/Kids [6 0 R]\n"
4500                  "/Count 1\n"                    4156                  "/Count 1\n"
4501                  ">>\n"                          4157                  ">>\n"
4502                  "endobj\n");                    4158                  "endobj\n");
4503 }                                                4159 }
4504                                                  4160 
4505 /* Open stream for data - graphical objects,     4161 /* Open stream for data - graphical objects, fonts etc. PDF object 4 */
4506                                                  4162 
4507 inline int tools_gl2psOpenPDFDataStream(tools << 4163 static int tools_gl2psOpenPDFDataStream(void)
4508 {                                                4164 {
4509   int offs = 0;                                  4165   int offs = 0;
4510                                                  4166 
4511   offs += fprintf(gl2ps->stream,              << 4167   offs += fprintf(tools_gl2ps_context->stream,
4512                   "4 0 obj\n"                    4168                   "4 0 obj\n"
4513                   "<<\n"                         4169                   "<<\n"
4514                   "/Length 5 0 R\n" );           4170                   "/Length 5 0 R\n" );
4515   offs += tools_gl2psPrintPDFCompressorType(g << 4171   offs += tools_gl2psPrintPDFCompressorType();
4516   offs += fprintf(gl2ps->stream,              << 4172   offs += fprintf(tools_gl2ps_context->stream,
4517                   ">>\n"                         4173                   ">>\n"
4518                   "stream\n");                   4174                   "stream\n");
4519   return offs;                                   4175   return offs;
4520 }                                                4176 }
4521                                                  4177 
4522 /* Stream setup - Graphics state, fill backgr    4178 /* Stream setup - Graphics state, fill background if allowed */
4523                                                  4179 
4524 inline int tools_gl2psOpenPDFDataStreamWriteP << 4180 static int tools_tools_gl2psOpenPDFDataStreamWritePreface(void)
4525 {                                                4181 {
4526   int offs;                                      4182   int offs;
4527                                                  4183 
4528   offs = tools_gl2psPrintf(gl2ps,"/GSa gs\n") << 4184   offs = tools_gl2psPrintf("/GSa gs\n");
4529                                                  4185 
4530   if(gl2ps->options & TOOLS_GL2PS_DRAW_BACKGR << 4186   if(tools_gl2ps_context->options & TOOLS_GL2PS_DRAW_BACKGROUND){
4531     offs += tools_gl2psPrintPDFFillColor(gl2p << 4187     offs += tools_gl2psPrintPDFFillColor(tools_gl2ps_context->bgcolor);
4532     offs += tools_gl2psPrintf(gl2ps,"%d %d %d << 4188     offs += tools_gl2psPrintf("%d %d %d %d re\n",
4533                         (int)gl2ps->viewport[ << 4189                         (int)tools_gl2ps_context->viewport[0], (int)tools_gl2ps_context->viewport[1],
4534                         (int)gl2ps->viewport[ << 4190                         (int)tools_gl2ps_context->viewport[2], (int)tools_gl2ps_context->viewport[3]);
4535     offs += tools_gl2psPrintf(gl2ps,"f\n");   << 4191     offs += tools_gl2psPrintf("f\n");
4536   }                                              4192   }
4537   return offs;                                   4193   return offs;
4538 }                                                4194 }
4539                                                  4195 
4540 /* Use the functions above to create the firs    4196 /* Use the functions above to create the first part of the PDF*/
4541                                                  4197 
4542 inline void tools_gl2psPrintPDFHeader(tools_G << 4198 static void tools_gl2psPrintPDFHeader(void)
4543 {                                                4199 {
4544   int offs = 0;                                  4200   int offs = 0;
4545   gl2ps->pdfprimlist = tools_gl2psListCreate( << 4201   tools_gl2ps_context->pdfprimlist = tools_gl2psListCreate(500, 500, sizeof(tools_GL2PSprimitive*));
4546   tools_gl2psPDFstacksInit(gl2ps);            << 4202   tools_tools_gl2psPDFstacksInit();
4547                                                  4203 
4548   gl2ps->xreflist = (int*)tools_gl2psMalloc(s << 4204   tools_gl2ps_context->xreflist = (int*)tools_gl2psMalloc(sizeof(int) * tools_gl2ps_context->objects_stack);
4549                                                  4205 
4550 #if defined(TOOLS_GL2PS_HAVE_ZLIB)               4206 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
4551   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){  << 4207   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
4552     tools_gl2psSetupCompress(gl2ps);          << 4208     tools_gl2psSetupCompress();
4553   }                                              4209   }
4554 #endif                                           4210 #endif
4555   gl2ps->xreflist[0] = 0;                     << 4211   tools_gl2ps_context->xreflist[0] = 0;
4556   offs += fprintf(gl2ps->stream, "%%PDF-1.4\n << 4212   offs += fprintf(tools_gl2ps_context->stream, "%%PDF-1.4\n");
4557   gl2ps->xreflist[1] = offs;                  << 4213   tools_gl2ps_context->xreflist[1] = offs;
4558                                               << 4214 
4559   offs += tools_gl2psPrintPDFInfo(gl2ps);     << 4215   offs += tools_gl2psPrintPDFInfo();
4560   gl2ps->xreflist[2] = offs;                  << 4216   tools_gl2ps_context->xreflist[2] = offs;
4561                                               << 4217 
4562   offs += tools_gl2psPrintPDFCatalog(gl2ps);  << 4218   offs += tools_gl2psPrintPDFCatalog();
4563   gl2ps->xreflist[3] = offs;                  << 4219   tools_gl2ps_context->xreflist[3] = offs;
4564                                               << 4220 
4565   offs += tools_gl2psPrintPDFPages(gl2ps);    << 4221   offs += tools_gl2psPrintPDFPages();
4566   gl2ps->xreflist[4] = offs;                  << 4222   tools_gl2ps_context->xreflist[4] = offs;
4567                                               << 4223 
4568   offs += tools_gl2psOpenPDFDataStream(gl2ps) << 4224   offs += tools_gl2psOpenPDFDataStream();
4569   gl2ps->xreflist[5] = offs; /* finished in t << 4225   tools_gl2ps_context->xreflist[5] = offs; /* finished in tools_gl2psPrintPDFFooter */
4570   gl2ps->streamlength = tools_gl2psOpenPDFDat << 4226   tools_gl2ps_context->streamlength = tools_tools_gl2psOpenPDFDataStreamWritePreface();
4571 }                                                4227 }
4572                                                  4228 
4573 /* The central primitive drawing */              4229 /* The central primitive drawing */
4574                                                  4230 
4575 inline void tools_gl2psPrintPDFPrimitive(tool << 4231 static void tools_gl2psPrintPDFPrimitive(void *data)
4576 {                                                4232 {
4577   tools_GL2PSprimitive *prim = *(tools_GL2PSp    4233   tools_GL2PSprimitive *prim = *(tools_GL2PSprimitive**)data;
4578                                                  4234 
4579   if((gl2ps->options & TOOLS_GL2PS_OCCLUSION_ << 4235   if((tools_gl2ps_context->options & TOOLS_GL2PS_OCCLUSION_CULL) && prim->culled)
4580     return;                                      4236     return;
4581                                                  4237 
4582   prim = tools_gl2psCopyPrimitive(prim); /* d    4238   prim = tools_gl2psCopyPrimitive(prim); /* deep copy */
4583   tools_gl2psListAdd(gl2ps->pdfprimlist, &pri << 4239   tools_gl2psListAdd(tools_gl2ps_context->pdfprimlist, &prim);
4584 }                                                4240 }
4585                                                  4241 
4586 /* close stream and ... */                       4242 /* close stream and ... */
4587                                                  4243 
4588 inline int tools_gl2psClosePDFDataStream(tool << 4244 static int tools_gl2psClosePDFDataStream(void)
4589 {                                                4245 {
4590   int offs = 0;                                  4246   int offs = 0;
4591                                                  4247 
4592 #if defined(TOOLS_GL2PS_HAVE_ZLIB)               4248 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
4593   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){  << 4249   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
4594     if(Z_OK != tools_gl2psDeflate(gl2ps))     << 4250     if(Z_OK != tools_gl2psDeflate())
4595       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Zlib    4251       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Zlib deflate error");
4596     else                                         4252     else
4597       fwrite(gl2ps->compress->dest, gl2ps->co << 4253       fwrite(tools_gl2ps_context->compress->dest, tools_gl2ps_context->compress->destLen, 1, tools_gl2ps_context->stream);
4598     gl2ps->streamlength += gl2ps->compress->d << 4254     tools_gl2ps_context->streamlength += tools_gl2ps_context->compress->destLen;
4599                                                  4255 
4600     offs += gl2ps->streamlength;              << 4256     offs += tools_gl2ps_context->streamlength;
4601     tools_gl2psFreeCompress(gl2ps);           << 4257     tools_gl2psFreeCompress();
4602   }                                              4258   }
4603 #endif                                           4259 #endif
4604                                                  4260 
4605   offs += fprintf(gl2ps->stream,              << 4261   offs += fprintf(tools_gl2ps_context->stream,
4606                   "endstream\n"                  4262                   "endstream\n"
4607                   "endobj\n");                   4263                   "endobj\n");
4608   return offs;                                   4264   return offs;
4609 }                                                4265 }
4610                                                  4266 
4611 /* ... write the now known length object */      4267 /* ... write the now known length object */
4612                                                  4268 
4613 inline int tools_gl2psPrintPDFDataStreamLengt << 4269 static int tools_gl2psPrintPDFDataStreamLength(int val)
4614 {                                                4270 {
4615   return fprintf(gl2ps->stream,               << 4271   return fprintf(tools_gl2ps_context->stream,
4616                  "5 0 obj\n"                     4272                  "5 0 obj\n"
4617                  "%d\n"                          4273                  "%d\n"
4618                  "endobj\n", val);               4274                  "endobj\n", val);
4619 }                                                4275 }
4620                                                  4276 
4621 /* Put the info created before in PDF objects    4277 /* Put the info created before in PDF objects */
4622                                                  4278 
4623 inline int tools_gl2psPrintPDFOpenPage(tools_ << 4279 static int tools_gl2psPrintPDFOpenPage(void)
4624 {                                                4280 {
4625   int offs;                                      4281   int offs;
4626                                                  4282 
4627   /* Write fixed part */                         4283   /* Write fixed part */
4628                                                  4284 
4629   offs = fprintf(gl2ps->stream,               << 4285   offs = fprintf(tools_gl2ps_context->stream,
4630                  "6 0 obj\n"                     4286                  "6 0 obj\n"
4631                  "<<\n"                          4287                  "<<\n"
4632                  "/Type /Page\n"                 4288                  "/Type /Page\n"
4633                  "/Parent 3 0 R\n"               4289                  "/Parent 3 0 R\n"
4634                  "/MediaBox [%d %d %d %d]\n",    4290                  "/MediaBox [%d %d %d %d]\n",
4635                  (int)gl2ps->viewport[0], (in << 4291                  (int)tools_gl2ps_context->viewport[0], (int)tools_gl2ps_context->viewport[1],
4636                  (int)gl2ps->viewport[2], (in << 4292                  (int)tools_gl2ps_context->viewport[2], (int)tools_gl2ps_context->viewport[3]);
4637                                                  4293 
4638   if(gl2ps->options & TOOLS_GL2PS_LANDSCAPE)  << 4294   if(tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE)
4639     offs += fprintf(gl2ps->stream, "/Rotate - << 4295     offs += fprintf(tools_gl2ps_context->stream, "/Rotate -90\n");
4640                                                  4296 
4641   offs += fprintf(gl2ps->stream,              << 4297   offs += fprintf(tools_gl2ps_context->stream,
4642                   "/Contents 4 0 R\n"            4298                   "/Contents 4 0 R\n"
4643                   "/Resources\n"                 4299                   "/Resources\n"
4644                   "<<\n"                         4300                   "<<\n"
4645                   "/ProcSet [/PDF /Text /Imag    4301                   "/ProcSet [/PDF /Text /ImageB /ImageC]  %%/ImageI\n");
4646                                                  4302 
4647   return offs;                                   4303   return offs;
4648                                                  4304 
4649   /* End fixed part, proceeds in tools_gl2psP << 4305   /* End fixed part, proceeds in tools_tools_gl2psPDFgroupListWriteVariableResources() */
4650 }                                                4306 }
4651                                                  4307 
4652 inline int tools_gl2psPDFgroupListWriteVariab << 4308 static int tools_tools_gl2psPDFgroupListWriteVariableResources(void)
4653 {                                                4309 {
4654   int offs = 0;                                  4310   int offs = 0;
4655                                                  4311 
4656   /* a) Graphics States for shader alpha mask    4312   /* a) Graphics States for shader alpha masks*/
4657   offs += tools_gl2psPDFgroupListWriteGStateR << 4313   offs += tools_tools_gl2psPDFgroupListWriteGStateResources();
4658                                                  4314 
4659   /* b) Shader and shader masks */               4315   /* b) Shader and shader masks */
4660   offs += tools_gl2psPDFgroupListWriteShaderR << 4316   offs += tools_tools_gl2psPDFgroupListWriteShaderResources();
4661                                                  4317 
4662   /* c) XObjects (Images & Shader Masks) */      4318   /* c) XObjects (Images & Shader Masks) */
4663   offs += tools_gl2psPDFgroupListWriteXObject << 4319   offs += tools_tools_gl2psPDFgroupListWriteXObjectResources();
4664                                                  4320 
4665   /* d) Fonts */                                 4321   /* d) Fonts */
4666   offs += tools_gl2psPDFgroupListWriteFontRes << 4322   offs += tools_tools_gl2psPDFgroupListWriteFontResources();
4667                                                  4323 
4668   /* End resources and page */                   4324   /* End resources and page */
4669   offs += fprintf(gl2ps->stream,              << 4325   offs += fprintf(tools_gl2ps_context->stream,
4670                   ">>\n"                         4326                   ">>\n"
4671                   ">>\n"                         4327                   ">>\n"
4672                   "endobj\n");                   4328                   "endobj\n");
4673   return offs;                                   4329   return offs;
4674 }                                                4330 }
4675                                                  4331 
4676 /* Standard Graphics State */                    4332 /* Standard Graphics State */
4677                                                  4333 
4678 inline int tools_gl2psPrintPDFGSObject(tools_ << 4334 static int tools_gl2psPrintPDFGSObject(void)
4679 {                                                4335 {
4680   return fprintf(gl2ps->stream,               << 4336   return fprintf(tools_gl2ps_context->stream,
4681                  "7 0 obj\n"                     4337                  "7 0 obj\n"
4682                  "<<\n"                          4338                  "<<\n"
4683                  "/Type /ExtGState\n"            4339                  "/Type /ExtGState\n"
4684                  "/SA false\n"                   4340                  "/SA false\n"
4685                  "/SM 0.02\n"                    4341                  "/SM 0.02\n"
4686                  "/OP false\n"                   4342                  "/OP false\n"
4687                  "/op false\n"                   4343                  "/op false\n"
4688                  "/OPM 0\n"                      4344                  "/OPM 0\n"
4689                  "/BG2 /Default\n"               4345                  "/BG2 /Default\n"
4690                  "/UCR2 /Default\n"              4346                  "/UCR2 /Default\n"
4691                  "/TR2 /Default\n"               4347                  "/TR2 /Default\n"
4692                  ">>\n"                          4348                  ">>\n"
4693                  "endobj\n");                    4349                  "endobj\n");
4694 }                                                4350 }
4695                                                  4351 
4696 /* Put vertex' edge flag (8bit) and coordinat    4352 /* Put vertex' edge flag (8bit) and coordinates (32bit) in shader stream */
4697                                                  4353 
4698 inline int tools_gl2psPrintPDFShaderStreamDat << 4354 static int tools_tools_tools_gl2psPrintPDFShaderStreamDataCoord(tools_GL2PSvertex *vertex,
4699                                               << 4355                                               int (*action)(unsigned long data, int size),
4700                                                  4356                                               tools_GLfloat dx, tools_GLfloat dy,
4701                                                  4357                                               tools_GLfloat xmin, tools_GLfloat ymin)
4702 {                                                4358 {
4703   int offs = 0;                                  4359   int offs = 0;
4704   unsigned long imap;                            4360   unsigned long imap;
4705   tools_GLfloat diff;                            4361   tools_GLfloat diff;
4706 //double dmax = ~1UL;                            4362 //double dmax = ~1UL;
4707   double dmax = (double)~1UL; //G.Barrand : c    4363   double dmax = (double)~1UL; //G.Barrand : clang10 : cast.
4708   char edgeflag = 0;                             4364   char edgeflag = 0;
4709                                                  4365 
4710   /* FIXME: temp bux fix for 64 bit archs: */    4366   /* FIXME: temp bux fix for 64 bit archs: */
4711   if(sizeof(unsigned long) == 8) dmax = dmax     4367   if(sizeof(unsigned long) == 8) dmax = dmax - 2048.;
4712                                                  4368 
4713   offs += (*action)(gl2ps, edgeflag, 1);      << 4369   offs += (*action)(edgeflag, 1);
4714                                                  4370 
4715   /* The Shader stream in PDF requires to be     4371   /* The Shader stream in PDF requires to be in a 'big-endian'
4716      order */                                    4372      order */
4717                                                  4373 
4718   if(TOOLS_GL2PS_ZERO(dx * dy)){                 4374   if(TOOLS_GL2PS_ZERO(dx * dy)){
4719     offs += (*action)(gl2ps, 0, 4);           << 4375     offs += (*action)(0, 4);
4720     offs += (*action)(gl2ps, 0, 4);           << 4376     offs += (*action)(0, 4);
4721   }                                              4377   }
4722   else{                                          4378   else{
4723     diff = (vertex->xyz[0] - xmin) / dx;         4379     diff = (vertex->xyz[0] - xmin) / dx;
4724     if(diff > 1)                                 4380     if(diff > 1)
4725       diff = 1.0F;                               4381       diff = 1.0F;
4726     else if(diff < 0)                            4382     else if(diff < 0)
4727       diff = 0.0F;                               4383       diff = 0.0F;
4728     imap = (unsigned long)(diff * dmax);         4384     imap = (unsigned long)(diff * dmax);
4729     offs += (*action)(gl2ps, imap, 4);        << 4385     offs += (*action)(imap, 4);
4730                                                  4386 
4731     diff = (vertex->xyz[1] - ymin) / dy;         4387     diff = (vertex->xyz[1] - ymin) / dy;
4732     if(diff > 1)                                 4388     if(diff > 1)
4733       diff = 1.0F;                               4389       diff = 1.0F;
4734     else if(diff < 0)                            4390     else if(diff < 0)
4735       diff = 0.0F;                               4391       diff = 0.0F;
4736     imap = (unsigned long)(diff * dmax);         4392     imap = (unsigned long)(diff * dmax);
4737     offs += (*action)(gl2ps, imap, 4);        << 4393     offs += (*action)(imap, 4);
4738   }                                              4394   }
4739                                                  4395 
4740   return offs;                                   4396   return offs;
4741 }                                                4397 }
4742                                                  4398 
4743 /* Put vertex' rgb value (8bit for every comp    4399 /* Put vertex' rgb value (8bit for every component) in shader stream */
4744                                                  4400 
4745 inline int tools_gl2psPrintPDFShaderStreamDat << 4401 static int tools_tools_tools_gl2psPrintPDFShaderStreamDataRGB(tools_GL2PSvertex *vertex,
4746                                             i << 4402                                             int (*action)(unsigned long data, int size))
4747 {                                                4403 {
4748   int offs = 0;                                  4404   int offs = 0;
4749   unsigned long imap;                            4405   unsigned long imap;
4750 //double dmax = ~1UL;                            4406 //double dmax = ~1UL;
4751   double dmax = (double)~1UL; //G.Barrand : c    4407   double dmax = (double)~1UL; //G.Barrand : clang10 : cast.
4752                                                  4408 
4753   /* FIXME: temp bux fix for 64 bit archs: */    4409   /* FIXME: temp bux fix for 64 bit archs: */
4754   if(sizeof(unsigned long) == 8) dmax = dmax     4410   if(sizeof(unsigned long) == 8) dmax = dmax - 2048.;
4755                                                  4411 
4756   imap = (unsigned long)((vertex->rgba[0]) *     4412   imap = (unsigned long)((vertex->rgba[0]) * dmax);
4757   offs += (*action)(gl2ps, imap, 1);          << 4413   offs += (*action)(imap, 1);
4758                                                  4414 
4759   imap = (unsigned long)((vertex->rgba[1]) *     4415   imap = (unsigned long)((vertex->rgba[1]) * dmax);
4760   offs += (*action)(gl2ps, imap, 1);          << 4416   offs += (*action)(imap, 1);
4761                                                  4417 
4762   imap = (unsigned long)((vertex->rgba[2]) *     4418   imap = (unsigned long)((vertex->rgba[2]) * dmax);
4763   offs += (*action)(gl2ps, imap, 1);          << 4419   offs += (*action)(imap, 1);
4764                                                  4420 
4765   return offs;                                   4421   return offs;
4766 }                                                4422 }
4767                                                  4423 
4768 /* Put vertex' alpha (8/16bit) in shader stre    4424 /* Put vertex' alpha (8/16bit) in shader stream */
4769                                                  4425 
4770 inline int tools_gl2psPrintPDFShaderStreamDat << 4426 static int tools_tools_tools_gl2psPrintPDFShaderStreamDataAlpha(tools_GL2PSvertex *vertex,
4771                                               << 4427                                               int (*action)(unsigned long data, int size),
4772                                                  4428                                               int sigbyte)
4773 {                                                4429 {
4774   int offs = 0;                                  4430   int offs = 0;
4775   unsigned long imap;                            4431   unsigned long imap;
4776 //double dmax = ~1UL;                            4432 //double dmax = ~1UL;
4777   double dmax = (double)~1UL; //G.Barrand : c    4433   double dmax = (double)~1UL; //G.Barrand : clang10 : cast.
4778                                                  4434 
4779   /* FIXME: temp bux fix for 64 bit archs: */    4435   /* FIXME: temp bux fix for 64 bit archs: */
4780   if(sizeof(unsigned long) == 8) dmax = dmax     4436   if(sizeof(unsigned long) == 8) dmax = dmax - 2048.;
4781                                                  4437 
4782   if(sigbyte != 8 && sigbyte != 16)              4438   if(sigbyte != 8 && sigbyte != 16)
4783     sigbyte = 8;                                 4439     sigbyte = 8;
4784                                                  4440 
4785   sigbyte /= 8;                                  4441   sigbyte /= 8;
4786                                                  4442 
4787   imap = (unsigned long)((vertex->rgba[3]) *     4443   imap = (unsigned long)((vertex->rgba[3]) * dmax);
4788                                                  4444 
4789   offs += (*action)(gl2ps, imap, sigbyte);    << 4445   offs += (*action)(imap, sigbyte);
4790                                                  4446 
4791   return offs;                                   4447   return offs;
4792 }                                                4448 }
4793                                                  4449 
4794 /* Put a triangles raw data in shader stream     4450 /* Put a triangles raw data in shader stream */
4795                                                  4451 
4796 inline int tools_gl2psPrintPDFShaderStreamDat << 4452 static int tools_tools_gl2psPrintPDFShaderStreamData(tools_GL2PStriangle *triangle,
4797                                          tool    4453                                          tools_GLfloat dx, tools_GLfloat dy,
4798                                          tool    4454                                          tools_GLfloat xmin, tools_GLfloat ymin,
4799                                          int  << 4455                                          int (*action)(unsigned long data, int size),
4800                                          int  << 4456                                          int gray)
4801 {                                                4457 {
4802   int i, offs = 0;                               4458   int i, offs = 0;
4803   tools_GL2PSvertex v;                           4459   tools_GL2PSvertex v;
4804                                                  4460 
4805   if(a_gray && a_gray != 8 && a_gray != 16)   << 4461   if(gray && gray != 8 && gray != 16)
4806     a_gray = 8;                               << 4462     gray = 8;
4807                                                  4463 
4808   for(i = 0; i < 3; ++i){                        4464   for(i = 0; i < 3; ++i){
4809     offs += tools_gl2psPrintPDFShaderStreamDa << 4465     offs += tools_tools_tools_gl2psPrintPDFShaderStreamDataCoord(&triangle->vertex[i], action,
4810                                                  4466                                                dx, dy, xmin, ymin);
4811     if(a_gray){                               << 4467     if(gray){
4812       v = triangle->vertex[i];                   4468       v = triangle->vertex[i];
4813       offs += tools_gl2psPrintPDFShaderStream << 4469       offs += tools_tools_tools_gl2psPrintPDFShaderStreamDataAlpha(&v, action, gray);
4814     }                                            4470     }
4815     else{                                        4471     else{
4816       offs += tools_gl2psPrintPDFShaderStream << 4472       offs += tools_tools_tools_gl2psPrintPDFShaderStreamDataRGB(&triangle->vertex[i], action);
4817     }                                            4473     }
4818   }                                              4474   }
4819                                                  4475 
4820   return offs;                                   4476   return offs;
4821 }                                                4477 }
4822                                                  4478 
4823 inline void tools_gl2psPDFRectHull(tools_GLfl << 4479 static void tools_tools_gl2psPDFRectHull(tools_GLfloat *xmin, tools_GLfloat *xmax,
4824                              tools_GLfloat *y    4480                              tools_GLfloat *ymin, tools_GLfloat *ymax,
4825                              tools_GL2PStrian    4481                              tools_GL2PStriangle *triangles, int cnt)
4826 {                                                4482 {
4827   int i, j;                                      4483   int i, j;
4828                                                  4484 
4829   *xmin = triangles[0].vertex[0].xyz[0];         4485   *xmin = triangles[0].vertex[0].xyz[0];
4830   *xmax = triangles[0].vertex[0].xyz[0];         4486   *xmax = triangles[0].vertex[0].xyz[0];
4831   *ymin = triangles[0].vertex[0].xyz[1];         4487   *ymin = triangles[0].vertex[0].xyz[1];
4832   *ymax = triangles[0].vertex[0].xyz[1];         4488   *ymax = triangles[0].vertex[0].xyz[1];
4833                                                  4489 
4834   for(i = 0; i < cnt; ++i){                      4490   for(i = 0; i < cnt; ++i){
4835     for(j = 0; j < 3; ++j){                      4491     for(j = 0; j < 3; ++j){
4836       if(*xmin > triangles[i].vertex[j].xyz[0    4492       if(*xmin > triangles[i].vertex[j].xyz[0])
4837         *xmin = triangles[i].vertex[j].xyz[0]    4493         *xmin = triangles[i].vertex[j].xyz[0];
4838       if(*xmax < triangles[i].vertex[j].xyz[0    4494       if(*xmax < triangles[i].vertex[j].xyz[0])
4839         *xmax = triangles[i].vertex[j].xyz[0]    4495         *xmax = triangles[i].vertex[j].xyz[0];
4840       if(*ymin > triangles[i].vertex[j].xyz[1    4496       if(*ymin > triangles[i].vertex[j].xyz[1])
4841         *ymin = triangles[i].vertex[j].xyz[1]    4497         *ymin = triangles[i].vertex[j].xyz[1];
4842       if(*ymax < triangles[i].vertex[j].xyz[1    4498       if(*ymax < triangles[i].vertex[j].xyz[1])
4843         *ymax = triangles[i].vertex[j].xyz[1]    4499         *ymax = triangles[i].vertex[j].xyz[1];
4844     }                                            4500     }
4845   }                                              4501   }
4846 }                                                4502 }
4847                                                  4503 
4848 /* Writes shaded triangle                        4504 /* Writes shaded triangle
4849    gray == 0 means write RGB triangles           4505    gray == 0 means write RGB triangles
4850    gray == 8             8bit-grayscale (for     4506    gray == 8             8bit-grayscale (for alpha masks)
4851    gray == 16            16bit-grayscale (for    4507    gray == 16            16bit-grayscale (for alpha masks) */
4852                                                  4508 
4853 inline int tools_gl2psPrintPDFShader(tools_GL << 4509 static int tools_gl2psPrintPDFShader(int obj, tools_GL2PStriangle *triangles,
4854                                int size, int  << 4510                                int size, int gray)
4855 {                                                4511 {
4856   int i, offs = 0, vertexbytes, done = 0;        4512   int i, offs = 0, vertexbytes, done = 0;
4857   tools_GLfloat xmin, xmax, ymin, ymax;          4513   tools_GLfloat xmin, xmax, ymin, ymax;
4858                                                  4514 
4859   switch(a_gray){                             << 4515   switch(gray){
4860   case 0:                                        4516   case 0:
4861     vertexbytes = 1+4+4+1+1+1;                   4517     vertexbytes = 1+4+4+1+1+1;
4862     break;                                       4518     break;
4863   case 8:                                        4519   case 8:
4864     vertexbytes = 1+4+4+1;                       4520     vertexbytes = 1+4+4+1;
4865     break;                                       4521     break;
4866   case 16:                                       4522   case 16:
4867     vertexbytes = 1+4+4+2;                       4523     vertexbytes = 1+4+4+2;
4868     break;                                       4524     break;
4869   default:                                       4525   default:
4870     a_gray = 8;                               << 4526     gray = 8;
4871     vertexbytes = 1+4+4+1;                       4527     vertexbytes = 1+4+4+1;
4872     break;                                       4528     break;
4873   }                                              4529   }
4874                                                  4530 
4875   tools_gl2psPDFRectHull(&xmin, &xmax, &ymin, << 4531   tools_tools_gl2psPDFRectHull(&xmin, &xmax, &ymin, &ymax, triangles, size);
4876                                                  4532 
4877   offs += fprintf(gl2ps->stream,              << 4533   offs += fprintf(tools_gl2ps_context->stream,
4878                   "%d 0 obj\n"                   4534                   "%d 0 obj\n"
4879                   "<< "                          4535                   "<< "
4880                   "/ShadingType 4 "              4536                   "/ShadingType 4 "
4881                   "/ColorSpace %s "              4537                   "/ColorSpace %s "
4882                   "/BitsPerCoordinate 32 "       4538                   "/BitsPerCoordinate 32 "
4883                   "/BitsPerComponent %d "        4539                   "/BitsPerComponent %d "
4884                   "/BitsPerFlag 8 "              4540                   "/BitsPerFlag 8 "
4885                   "/Decode [%f %f %f %f 0 1 %    4541                   "/Decode [%f %f %f %f 0 1 %s] ",
4886                   obj,                           4542                   obj,
4887                   (a_gray) ? "/DeviceGray" :  << 4543                   (gray) ? "/DeviceGray" : "/DeviceRGB",
4888                   (a_gray) ? a_gray : 8,      << 4544                   (gray) ? gray : 8,
4889                   xmin, xmax, ymin, ymax,        4545                   xmin, xmax, ymin, ymax,
4890                   (a_gray) ? "" : "0 1 0 1"); << 4546                   (gray) ? "" : "0 1 0 1");
4891                                                  4547 
4892 #if defined(TOOLS_GL2PS_HAVE_ZLIB)               4548 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
4893   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){  << 4549   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
4894     tools_gl2psAllocCompress(gl2ps,vertexbyte << 4550     tools_gl2psAllocCompress(vertexbytes * size * 3);
4895                                                  4551 
4896     for(i = 0; i < size; ++i)                    4552     for(i = 0; i < size; ++i)
4897       tools_gl2psPrintPDFShaderStreamData(gl2 << 4553       tools_tools_gl2psPrintPDFShaderStreamData(&triangles[i],
4898                                     xmax-xmin    4554                                     xmax-xmin, ymax-ymin, xmin, ymin,
4899                                     tools_gl2 << 4555                                     tools_gl2psWriteBigEndianCompress, gray);
4900                                                  4556 
4901     if(Z_OK == tools_gl2psDeflate(gl2ps) && 2 << 4557     if(Z_OK == tools_gl2psDeflate() && 23 + tools_gl2ps_context->compress->destLen < tools_gl2ps_context->compress->srcLen){
4902       offs += tools_gl2psPrintPDFCompressorTy << 4558       offs += tools_gl2psPrintPDFCompressorType();
4903       offs += fprintf(gl2ps->stream,          << 4559       offs += fprintf(tools_gl2ps_context->stream,
4904                       "/Length %d "              4560                       "/Length %d "
4905                       ">>\n"                     4561                       ">>\n"
4906                       "stream\n",                4562                       "stream\n",
4907                       (int)gl2ps->compress->d << 4563                       (int)tools_gl2ps_context->compress->destLen);
4908       offs += gl2ps->compress->destLen * fwri << 4564       offs += tools_gl2ps_context->compress->destLen * fwrite(tools_gl2ps_context->compress->dest,
4909                                               << 4565                                                 tools_gl2ps_context->compress->destLen,
4910                                               << 4566                                                 1, tools_gl2ps_context->stream);
4911       done = 1;                                  4567       done = 1;
4912     }                                            4568     }
4913     tools_gl2psFreeCompress(gl2ps);           << 4569     tools_gl2psFreeCompress();
4914   }                                              4570   }
4915 #endif                                           4571 #endif
4916                                                  4572 
4917   if(!done){                                     4573   if(!done){
4918     /* no compression, or too long after comp    4574     /* no compression, or too long after compression, or compress error
4919        -> write non-compressed entry */          4575        -> write non-compressed entry */
4920     offs += fprintf(gl2ps->stream,            << 4576     offs += fprintf(tools_gl2ps_context->stream,
4921                     "/Length %d "                4577                     "/Length %d "
4922                     ">>\n"                       4578                     ">>\n"
4923                     "stream\n",                  4579                     "stream\n",
4924                     vertexbytes * 3 * size);     4580                     vertexbytes * 3 * size);
4925     for(i = 0; i < size; ++i)                    4581     for(i = 0; i < size; ++i)
4926       offs += tools_gl2psPrintPDFShaderStream << 4582       offs += tools_tools_gl2psPrintPDFShaderStreamData(&triangles[i],
4927                                             x    4583                                             xmax-xmin, ymax-ymin, xmin, ymin,
4928                                             t << 4584                                             tools_gl2psWriteBigEndian, gray);
4929   }                                              4585   }
4930                                                  4586 
4931   offs += fprintf(gl2ps->stream,              << 4587   offs += fprintf(tools_gl2ps_context->stream,
4932                   "\nendstream\n"                4588                   "\nendstream\n"
4933                   "endobj\n");                   4589                   "endobj\n");
4934                                                  4590 
4935   return offs;                                   4591   return offs;
4936 }                                                4592 }
4937                                                  4593 
4938 /* Writes a XObject for a shaded triangle mas    4594 /* Writes a XObject for a shaded triangle mask */
4939                                                  4595 
4940 inline int tools_gl2psPrintPDFShaderMask(tool << 4596 static int tools_tools_gl2psPrintPDFShaderMask(int obj, int childobj)
4941 {                                                4597 {
4942   int offs = 0, len;                             4598   int offs = 0, len;
4943                                                  4599 
4944   offs += fprintf(gl2ps->stream,              << 4600   offs += fprintf(tools_gl2ps_context->stream,
4945                   "%d 0 obj\n"                   4601                   "%d 0 obj\n"
4946                   "<<\n"                         4602                   "<<\n"
4947                   "/Type /XObject\n"             4603                   "/Type /XObject\n"
4948                   "/Subtype /Form\n"             4604                   "/Subtype /Form\n"
4949                   "/BBox [ %d %d %d %d ]\n"      4605                   "/BBox [ %d %d %d %d ]\n"
4950                   "/Group \n<<\n/S /Transpare    4606                   "/Group \n<<\n/S /Transparency /CS /DeviceRGB\n"
4951                   ">>\n",                        4607                   ">>\n",
4952                   obj,                           4608                   obj,
4953                   (int)gl2ps->viewport[0], (i << 4609                   (int)tools_gl2ps_context->viewport[0], (int)tools_gl2ps_context->viewport[1],
4954                   (int)gl2ps->viewport[2], (i << 4610                   (int)tools_gl2ps_context->viewport[2], (int)tools_gl2ps_context->viewport[3]);
4955                                                  4611 
4956   len = (childobj>0)                             4612   len = (childobj>0)
4957     ? (int)strlen("/TrSh sh\n") + (int)log10(    4613     ? (int)strlen("/TrSh sh\n") + (int)log10((double)childobj)+1
4958     : (int)strlen("/TrSh0 sh\n");                4614     : (int)strlen("/TrSh0 sh\n");
4959                                                  4615 
4960   offs += fprintf(gl2ps->stream,              << 4616   offs += fprintf(tools_gl2ps_context->stream,
4961                   "/Length %d\n"                 4617                   "/Length %d\n"
4962                   ">>\n"                         4618                   ">>\n"
4963                   "stream\n",                    4619                   "stream\n",
4964                   len);                          4620                   len);
4965   offs += fprintf(gl2ps->stream,              << 4621   offs += fprintf(tools_gl2ps_context->stream,
4966                   "/TrSh%d sh\n",                4622                   "/TrSh%d sh\n",
4967                   childobj);                     4623                   childobj);
4968   offs += fprintf(gl2ps->stream,              << 4624   offs += fprintf(tools_gl2ps_context->stream,
4969                   "endstream\n"                  4625                   "endstream\n"
4970                   "endobj\n");                   4626                   "endobj\n");
4971                                                  4627 
4972   return offs;                                   4628   return offs;
4973 }                                                4629 }
4974                                                  4630 
4975 /* Writes a Extended graphics state for a sha    4631 /* Writes a Extended graphics state for a shaded triangle mask if
4976    simplealpha ist true the childobj argument    4632    simplealpha ist true the childobj argument is ignored and a /ca
4977    statement will be written instead */          4633    statement will be written instead */
4978                                                  4634 
4979 inline int tools_gl2psPrintPDFShaderExtGS(too << 4635 static int tools_tools_gl2psPrintPDFShaderExtGS(int obj, int childobj)
4980 {                                                4636 {
4981   int offs = 0;                                  4637   int offs = 0;
4982                                                  4638 
4983   offs += fprintf(gl2ps->stream,              << 4639   offs += fprintf(tools_gl2ps_context->stream,
4984                   "%d 0 obj\n"                   4640                   "%d 0 obj\n"
4985                   "<<\n",                        4641                   "<<\n",
4986                   obj);                          4642                   obj);
4987                                                  4643 
4988   offs += fprintf(gl2ps->stream,              << 4644   offs += fprintf(tools_gl2ps_context->stream,
4989                   "/SMask << /S /Alpha /G %d     4645                   "/SMask << /S /Alpha /G %d 0 R >> ",
4990                   childobj);                     4646                   childobj);
4991                                                  4647 
4992   offs += fprintf(gl2ps->stream,              << 4648   offs += fprintf(tools_gl2ps_context->stream,
4993                   ">>\n"                         4649                   ">>\n"
4994                   "endobj\n");                   4650                   "endobj\n");
4995   return offs;                                   4651   return offs;
4996 }                                                4652 }
4997                                                  4653 
4998 /* a simple graphics state */                    4654 /* a simple graphics state */
4999                                                  4655 
5000 inline int tools_gl2psPrintPDFShaderSimpleExt << 4656 static int tools_tools_gl2psPrintPDFShaderSimpleExtGS(int obj, tools_GLfloat alpha)
5001 {                                                4657 {
5002   int offs = 0;                                  4658   int offs = 0;
5003                                                  4659 
5004   offs += fprintf(gl2ps->stream,              << 4660   offs += fprintf(tools_gl2ps_context->stream,
5005                   "%d 0 obj\n"                   4661                   "%d 0 obj\n"
5006                   "<<\n"                         4662                   "<<\n"
5007                   "/ca %g"                       4663                   "/ca %g"
5008                   ">>\n"                         4664                   ">>\n"
5009                   "endobj\n",                    4665                   "endobj\n",
5010                   obj, alpha);                   4666                   obj, alpha);
5011   return offs;                                   4667   return offs;
5012 }                                                4668 }
5013                                                  4669 
5014 /* Similar groups of functions for pixmaps an    4670 /* Similar groups of functions for pixmaps and text */
5015                                                  4671 
5016 inline int tools_gl2psPrintPDFPixmapStreamDat << 4672 static int tools_tools_gl2psPrintPDFPixmapStreamData(tools_GL2PSimage *im,
5017                                          int  << 4673                                          int (*action)(unsigned long data, int size),
5018                                          int  << 4674                                          int gray)
5019 {                                                4675 {
5020   int x, y, shift;                               4676   int x, y, shift;
5021   tools_GLfloat _r, _g, _b, _a;               << 4677   tools_GLfloat r, g, b, a;
5022                                                  4678 
5023   if(im->format != TOOLS_GL_RGBA && a_gray)   << 4679   if(im->format != TOOLS_GL_RGBA && gray)
5024     return 0;                                    4680     return 0;
5025                                                  4681 
5026   if(a_gray && a_gray != 8 && a_gray != 16)   << 4682   if(gray && gray != 8 && gray != 16)
5027     a_gray = 8;                               << 4683     gray = 8;
5028                                                  4684 
5029   a_gray /= 8;                                << 4685   gray /= 8;
5030                                                  4686 
5031   shift = (sizeof(unsigned long) - 1) * 8;       4687   shift = (sizeof(unsigned long) - 1) * 8;
5032                                                  4688 
5033   for(y = 0; y < im->height; ++y){               4689   for(y = 0; y < im->height; ++y){
5034     for(x = 0; x < im->width; ++x){              4690     for(x = 0; x < im->width; ++x){
5035       _a = tools_gl2psGetRGB(im, x, y, &_r, & << 4691       a = tools_gl2psGetRGB(im, x, y, &r, &g, &b);
5036       if(im->format == TOOLS_GL_RGBA && a_gra << 4692       if(im->format == TOOLS_GL_RGBA && gray){
5037         (*action)(gl2ps, (unsigned long)(_a * << 4693         (*action)((unsigned long)(a * 255) << shift, gray);
5038       }                                          4694       }
5039       else{                                      4695       else{
5040         (*action)(gl2ps, (unsigned long)(_r * << 4696         (*action)((unsigned long)(r * 255) << shift, 1);
5041         (*action)(gl2ps, (unsigned long)(_g * << 4697         (*action)((unsigned long)(g * 255) << shift, 1);
5042         (*action)(gl2ps, (unsigned long)(_b * << 4698         (*action)((unsigned long)(b * 255) << shift, 1);
5043       }                                          4699       }
5044     }                                            4700     }
5045   }                                              4701   }
5046                                                  4702 
5047   switch(a_gray){                             << 4703   switch(gray){
5048   case 0: return 3 * im->width * im->height;     4704   case 0: return 3 * im->width * im->height;
5049   case 1: return im->width * im->height;         4705   case 1: return im->width * im->height;
5050   case 2: return 2 * im->width * im->height;     4706   case 2: return 2 * im->width * im->height;
5051   default: return 3 * im->width * im->height;    4707   default: return 3 * im->width * im->height;
5052   }                                              4708   }
5053 }                                                4709 }
5054                                                  4710 
5055 inline int tools_gl2psPrintPDFPixmap(tools_GL << 4711 static int tools_gl2psPrintPDFPixmap(int obj, int childobj, tools_GL2PSimage *im, int gray)
5056 {                                                4712 {
5057   int offs = 0, done = 0, sigbytes = 3;          4713   int offs = 0, done = 0, sigbytes = 3;
5058                                                  4714 
5059   if(a_gray && a_gray !=8 && a_gray != 16)    << 4715   if(gray && gray !=8 && gray != 16)
5060     a_gray = 8;                               << 4716     gray = 8;
5061                                                  4717 
5062   if(a_gray)                                  << 4718   if(gray)
5063     sigbytes = a_gray / 8;                    << 4719     sigbytes = gray / 8;
5064                                                  4720 
5065   offs += fprintf(gl2ps->stream,              << 4721   offs += fprintf(tools_gl2ps_context->stream,
5066                   "%d 0 obj\n"                   4722                   "%d 0 obj\n"
5067                   "<<\n"                         4723                   "<<\n"
5068                   "/Type /XObject\n"             4724                   "/Type /XObject\n"
5069                   "/Subtype /Image\n"            4725                   "/Subtype /Image\n"
5070                   "/Width %d\n"                  4726                   "/Width %d\n"
5071                   "/Height %d\n"                 4727                   "/Height %d\n"
5072                   "/ColorSpace %s \n"            4728                   "/ColorSpace %s \n"
5073                   "/BitsPerComponent 8\n",       4729                   "/BitsPerComponent 8\n",
5074                   obj,                           4730                   obj,
5075                   (int)im->width, (int)im->he    4731                   (int)im->width, (int)im->height,
5076                   (a_gray) ? "/DeviceGray" :  << 4732                   (gray) ? "/DeviceGray" : "/DeviceRGB" );
5077   if(TOOLS_GL_RGBA == im->format && a_gray == << 4733   if(TOOLS_GL_RGBA == im->format && gray == 0){
5078     offs += fprintf(gl2ps->stream,            << 4734     offs += fprintf(tools_gl2ps_context->stream,
5079                     "/SMask %d 0 R\n",           4735                     "/SMask %d 0 R\n",
5080                     childobj);                   4736                     childobj);
5081   }                                              4737   }
5082                                                  4738 
5083 #if defined(TOOLS_GL2PS_HAVE_ZLIB)               4739 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
5084   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){  << 4740   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
5085     tools_gl2psAllocCompress(gl2ps,(int)(im-> << 4741     tools_gl2psAllocCompress((int)(im->width * im->height * sigbytes));
5086                                                  4742 
5087     tools_gl2psPrintPDFPixmapStreamData(gl2ps << 4743     tools_tools_gl2psPrintPDFPixmapStreamData(im, tools_gl2psWriteBigEndianCompress, gray);
5088                                                  4744 
5089     if(Z_OK == tools_gl2psDeflate(gl2ps) && 2 << 4745     if(Z_OK == tools_gl2psDeflate() && 23 + tools_gl2ps_context->compress->destLen < tools_gl2ps_context->compress->srcLen){
5090       offs += tools_gl2psPrintPDFCompressorTy << 4746       offs += tools_gl2psPrintPDFCompressorType();
5091       offs += fprintf(gl2ps->stream,          << 4747       offs += fprintf(tools_gl2ps_context->stream,
5092                       "/Length %d "              4748                       "/Length %d "
5093                       ">>\n"                     4749                       ">>\n"
5094                       "stream\n",                4750                       "stream\n",
5095                       (int)gl2ps->compress->d << 4751                       (int)tools_gl2ps_context->compress->destLen);
5096       offs += gl2ps->compress->destLen * fwri << 4752       offs += tools_gl2ps_context->compress->destLen * fwrite(tools_gl2ps_context->compress->dest, tools_gl2ps_context->compress->destLen,
5097                                               << 4753                                                 1, tools_gl2ps_context->stream);
5098       done = 1;                                  4754       done = 1;
5099     }                                            4755     }
5100     tools_gl2psFreeCompress(gl2ps);           << 4756     tools_gl2psFreeCompress();
5101   }                                              4757   }
5102 #endif                                           4758 #endif
5103                                                  4759 
5104   if(!done){                                     4760   if(!done){
5105     /* no compression, or too long after comp    4761     /* no compression, or too long after compression, or compress error
5106        -> write non-compressed entry */          4762        -> write non-compressed entry */
5107     offs += fprintf(gl2ps->stream,            << 4763     offs += fprintf(tools_gl2ps_context->stream,
5108                     "/Length %d "                4764                     "/Length %d "
5109                     ">>\n"                       4765                     ">>\n"
5110                     "stream\n",                  4766                     "stream\n",
5111                     (int)(im->width * im->hei    4767                     (int)(im->width * im->height * sigbytes));
5112     offs += tools_gl2psPrintPDFPixmapStreamDa << 4768     offs += tools_tools_gl2psPrintPDFPixmapStreamData(im, tools_gl2psWriteBigEndian, gray);
5113   }                                              4769   }
5114                                                  4770 
5115   offs += fprintf(gl2ps->stream,              << 4771   offs += fprintf(tools_gl2ps_context->stream,
5116                   "\nendstream\n"                4772                   "\nendstream\n"
5117                   "endobj\n");                   4773                   "endobj\n");
5118                                                  4774 
5119   return offs;                                   4775   return offs;
5120 }                                                4776 }
5121                                                  4777 
5122 inline int tools_gl2psPrintPDFText(tools_GL2P << 4778 static int tools_gl2psPrintPDFText(int obj, tools_GL2PSstring *s, int fontnumber)
5123 {                                                4779 {
5124   int offs = 0;                                  4780   int offs = 0;
5125                                                  4781 
5126   offs += fprintf(gl2ps->stream,              << 4782   offs += fprintf(tools_gl2ps_context->stream,
5127                   "%d 0 obj\n"                   4783                   "%d 0 obj\n"
5128                   "<<\n"                         4784                   "<<\n"
5129                   "/Type /Font\n"                4785                   "/Type /Font\n"
5130                   "/Subtype /Type1\n"            4786                   "/Subtype /Type1\n"
5131                   "/Name /F%d\n"                 4787                   "/Name /F%d\n"
5132                   "/BaseFont /%s\n"              4788                   "/BaseFont /%s\n"
5133                   "/Encoding /MacRomanEncodin    4789                   "/Encoding /MacRomanEncoding\n"
5134                   ">>\n"                         4790                   ">>\n"
5135                   "endobj\n",                    4791                   "endobj\n",
5136                   obj, fontnumber, a_s->fontn << 4792                   obj, fontnumber, s->fontname);
5137   return offs;                                   4793   return offs;
5138 }                                                4794 }
5139                                                  4795 
5140 /* Write the physical objects */                 4796 /* Write the physical objects */
5141                                                  4797 
5142 inline int tools_gl2psPDFgroupListWriteObject << 4798 static int tools_tools_gl2psPDFgroupListWriteObjects(int entryoffs)
5143 {                                                4799 {
5144   int i,j;                                       4800   int i,j;
5145   tools_GL2PSprimitive *p = NULL;                4801   tools_GL2PSprimitive *p = NULL;
5146   tools_GL2PSpdfgroup *gro;                      4802   tools_GL2PSpdfgroup *gro;
5147   int offs = entryoffs;                          4803   int offs = entryoffs;
5148   tools_GL2PStriangle *triangles;                4804   tools_GL2PStriangle *triangles;
5149   int size = 0;                                  4805   int size = 0;
5150                                                  4806 
5151   if(!gl2ps->pdfgrouplist)                    << 4807   if(!tools_gl2ps_context->pdfgrouplist)
5152     return offs;                                 4808     return offs;
5153                                                  4809 
5154   for(i = 0; i < tools_gl2psListNbr(gl2ps->pd << 4810   for(i = 0; i < tools_gl2psListNbr(tools_gl2ps_context->pdfgrouplist); ++i){
5155     gro = (tools_GL2PSpdfgroup*)tools_gl2psLi << 4811     gro = (tools_GL2PSpdfgroup*)tools_gl2psListPointer(tools_gl2ps_context->pdfgrouplist, i);
5156     if(!tools_gl2psListNbr(gro->ptrlist))        4812     if(!tools_gl2psListNbr(gro->ptrlist))
5157       continue;                                  4813       continue;
5158     p = *(tools_GL2PSprimitive**)tools_gl2psL    4814     p = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, 0);
5159     switch(p->type){                             4815     switch(p->type){
5160     case TOOLS_GL2PS_POINT:                      4816     case TOOLS_GL2PS_POINT:
5161       break;                                     4817       break;
5162     case TOOLS_GL2PS_LINE:                       4818     case TOOLS_GL2PS_LINE:
5163       break;                                     4819       break;
5164     case TOOLS_GL2PS_TRIANGLE:                   4820     case TOOLS_GL2PS_TRIANGLE:
5165       size = tools_gl2psListNbr(gro->ptrlist)    4821       size = tools_gl2psListNbr(gro->ptrlist);
5166       triangles = (tools_GL2PStriangle*)tools    4822       triangles = (tools_GL2PStriangle*)tools_gl2psMalloc(sizeof(tools_GL2PStriangle) * size);
5167       for(j = 0; j < size; ++j){                 4823       for(j = 0; j < size; ++j){
5168         p = *(tools_GL2PSprimitive**)tools_gl    4824         p = *(tools_GL2PSprimitive**)tools_gl2psListPointer(gro->ptrlist, j);
5169         tools_gl2psFillTriangleFromPrimitive(    4825         tools_gl2psFillTriangleFromPrimitive(&triangles[j], p, TOOLS_GL_TRUE);
5170       }                                          4826       }
5171       if(triangles[0].prop & T_VAR_COLOR){       4827       if(triangles[0].prop & T_VAR_COLOR){
5172         gl2ps->xreflist[gro->shobjno] = offs; << 4828         tools_gl2ps_context->xreflist[gro->shobjno] = offs;
5173         offs += tools_gl2psPrintPDFShader(gl2 << 4829         offs += tools_gl2psPrintPDFShader(gro->shobjno, triangles, size, 0);
5174       }                                          4830       }
5175       if(triangles[0].prop & T_ALPHA_LESS_1){    4831       if(triangles[0].prop & T_ALPHA_LESS_1){
5176         gl2ps->xreflist[gro->gsobjno] = offs; << 4832         tools_gl2ps_context->xreflist[gro->gsobjno] = offs;
5177         offs += tools_gl2psPrintPDFShaderSimp << 4833         offs += tools_tools_gl2psPrintPDFShaderSimpleExtGS(gro->gsobjno, triangles[0].vertex[0].rgba[3]);
5178       }                                          4834       }
5179       if(triangles[0].prop & T_VAR_ALPHA){       4835       if(triangles[0].prop & T_VAR_ALPHA){
5180         gl2ps->xreflist[gro->gsobjno] = offs; << 4836         tools_gl2ps_context->xreflist[gro->gsobjno] = offs;
5181         offs += tools_gl2psPrintPDFShaderExtG << 4837         offs += tools_tools_gl2psPrintPDFShaderExtGS(gro->gsobjno, gro->trgroupobjno);
5182         gl2ps->xreflist[gro->trgroupobjno] =  << 4838         tools_gl2ps_context->xreflist[gro->trgroupobjno] = offs;
5183         offs += tools_gl2psPrintPDFShaderMask << 4839         offs += tools_tools_gl2psPrintPDFShaderMask(gro->trgroupobjno, gro->maskshno);
5184         gl2ps->xreflist[gro->maskshobjno] = o << 4840         tools_gl2ps_context->xreflist[gro->maskshobjno] = offs;
5185         offs += tools_gl2psPrintPDFShader(gl2 << 4841         offs += tools_gl2psPrintPDFShader(gro->maskshobjno, triangles, size, 8);
5186       }                                          4842       }
5187       tools_gl2psFree(triangles);                4843       tools_gl2psFree(triangles);
5188       break;                                     4844       break;
5189     case TOOLS_GL2PS_PIXMAP:                     4845     case TOOLS_GL2PS_PIXMAP:
5190       gl2ps->xreflist[gro->imobjno] = offs;   << 4846       tools_gl2ps_context->xreflist[gro->imobjno] = offs;
5191       offs += tools_gl2psPrintPDFPixmap(gl2ps << 4847       offs += tools_gl2psPrintPDFPixmap(gro->imobjno, gro->imobjno+1, p->data.image, 0);
5192       if(p->data.image->format == TOOLS_GL_RG    4848       if(p->data.image->format == TOOLS_GL_RGBA){
5193         gl2ps->xreflist[gro->imobjno+1] = off << 4849         tools_gl2ps_context->xreflist[gro->imobjno+1] = offs;
5194         offs += tools_gl2psPrintPDFPixmap(gl2 << 4850         offs += tools_gl2psPrintPDFPixmap(gro->imobjno+1, -1, p->data.image, 8);
5195       }                                          4851       }
5196       break;                                     4852       break;
5197     case TOOLS_GL2PS_TEXT:                       4853     case TOOLS_GL2PS_TEXT:
5198       gl2ps->xreflist[gro->fontobjno] = offs; << 4854       tools_gl2ps_context->xreflist[gro->fontobjno] = offs;
5199       offs += tools_gl2psPrintPDFText(gl2ps,  << 4855       offs += tools_gl2psPrintPDFText(gro->fontobjno,p->data.text,gro->fontno);
5200       break;                                     4856       break;
5201     case TOOLS_GL2PS_SPECIAL :                   4857     case TOOLS_GL2PS_SPECIAL :
5202       /* alignment contains the format for wh    4858       /* alignment contains the format for which the special output text
5203          is intended */                          4859          is intended */
5204       if(p->data.text->alignment == TOOLS_GL2    4860       if(p->data.text->alignment == TOOLS_GL2PS_PDF)
5205         offs += fprintf(gl2ps->stream, "%s\n" << 4861         offs += fprintf(tools_gl2ps_context->stream, "%s\n", p->data.text->str);
5206       break;                                     4862       break;
5207     default:                                     4863     default:
5208       break;                                     4864       break;
5209     }                                            4865     }
5210   }                                              4866   }
5211   return offs;                                   4867   return offs;
5212 }                                                4868 }
5213                                                  4869 
5214 /* All variable data has been written at this    4870 /* All variable data has been written at this point and all required
5215    functioninality has been gathered, so we c    4871    functioninality has been gathered, so we can write now file footer
5216    with cross reference table and trailer */     4872    with cross reference table and trailer */
5217                                                  4873 
5218 inline void tools_gl2psPrintPDFFooter(tools_G << 4874 static void tools_gl2psPrintPDFFooter(void)
5219 {                                                4875 {
5220   int i, offs;                                   4876   int i, offs;
5221                                                  4877 
5222   tools_gl2psPDFgroupListInit(gl2ps);         << 4878   tools_tools_gl2psPDFgroupListInit();
5223   tools_gl2psPDFgroupListWriteMainStream(gl2p << 4879   tools_tools_gl2psPDFgroupListWriteMainStream();
5224                                                  4880 
5225   offs = gl2ps->xreflist[5] + gl2ps->streamle << 4881   offs = tools_gl2ps_context->xreflist[5] + tools_gl2ps_context->streamlength;
5226   offs += tools_gl2psClosePDFDataStream(gl2ps << 4882   offs += tools_gl2psClosePDFDataStream();
5227   gl2ps->xreflist[5] = offs;                  << 4883   tools_gl2ps_context->xreflist[5] = offs;
5228                                               << 4884 
5229   offs += tools_gl2psPrintPDFDataStreamLength << 4885   offs += tools_gl2psPrintPDFDataStreamLength(tools_gl2ps_context->streamlength);
5230   gl2ps->xreflist[6] = offs;                  << 4886   tools_gl2ps_context->xreflist[6] = offs;
5231   gl2ps->streamlength = 0;                    << 4887   tools_gl2ps_context->streamlength = 0;
5232                                               << 4888 
5233   offs += tools_gl2psPrintPDFOpenPage(gl2ps); << 4889   offs += tools_gl2psPrintPDFOpenPage();
5234   offs += tools_gl2psPDFgroupListWriteVariabl << 4890   offs += tools_tools_gl2psPDFgroupListWriteVariableResources();
5235   gl2ps->xreflist = (int*)tools_gl2psRealloc( << 4891   tools_gl2ps_context->xreflist = (int*)tools_gl2psRealloc(tools_gl2ps_context->xreflist,
5236                                        sizeof << 4892                                        sizeof(int) * (tools_gl2ps_context->objects_stack + 1));
5237   gl2ps->xreflist[7] = offs;                  << 4893   tools_gl2ps_context->xreflist[7] = offs;
5238                                                  4894 
5239   offs += tools_gl2psPrintPDFGSObject(gl2ps); << 4895   offs += tools_gl2psPrintPDFGSObject();
5240   gl2ps->xreflist[8] = offs;                  << 4896   tools_gl2ps_context->xreflist[8] = offs;
5241                                                  4897 
5242   gl2ps->xreflist[gl2ps->objects_stack] =     << 4898   tools_gl2ps_context->xreflist[tools_gl2ps_context->objects_stack] =
5243     tools_gl2psPDFgroupListWriteObjects(gl2ps << 4899     tools_tools_gl2psPDFgroupListWriteObjects(tools_gl2ps_context->xreflist[8]);
5244                                                  4900 
5245   /* Start cross reference table. The file ha    4901   /* Start cross reference table. The file has to been opened in
5246      binary mode to preserve the 20 digit str    4902      binary mode to preserve the 20 digit string length! */
5247   fprintf(gl2ps->stream,                      << 4903   fprintf(tools_gl2ps_context->stream,
5248           "xref\n"                               4904           "xref\n"
5249           "0 %d\n"                               4905           "0 %d\n"
5250           "%010d 65535 f \n", gl2ps->objects_ << 4906           "%010d 65535 f \n", tools_gl2ps_context->objects_stack, 0);
5251                                                  4907 
5252   for(i = 1; i < gl2ps->objects_stack; ++i)   << 4908   for(i = 1; i < tools_gl2ps_context->objects_stack; ++i)
5253     fprintf(gl2ps->stream, "%010d 00000 n \n" << 4909     fprintf(tools_gl2ps_context->stream, "%010d 00000 n \n", tools_gl2ps_context->xreflist[i]);
5254                                                  4910 
5255   fprintf(gl2ps->stream,                      << 4911   fprintf(tools_gl2ps_context->stream,
5256           "trailer\n"                            4912           "trailer\n"
5257           "<<\n"                                 4913           "<<\n"
5258           "/Size %d\n"                           4914           "/Size %d\n"
5259           "/Info 1 0 R\n"                        4915           "/Info 1 0 R\n"
5260           "/Root 2 0 R\n"                        4916           "/Root 2 0 R\n"
5261           ">>\n"                                 4917           ">>\n"
5262           "startxref\n%d\n"                      4918           "startxref\n%d\n"
5263           "%%%%EOF\n",                           4919           "%%%%EOF\n",
5264           gl2ps->objects_stack, gl2ps->xrefli << 4920           tools_gl2ps_context->objects_stack, tools_gl2ps_context->xreflist[tools_gl2ps_context->objects_stack]);
5265                                                  4921 
5266   /* Free auxiliary lists and arrays */          4922   /* Free auxiliary lists and arrays */
5267   tools_gl2psFree(gl2ps->xreflist);           << 4923   tools_gl2psFree(tools_gl2ps_context->xreflist);
5268   tools_gl2psListAction(gl2ps->pdfprimlist, t << 4924   tools_gl2psListAction(tools_gl2ps_context->pdfprimlist, tools_tools_gl2psFreePrimitive);
5269   tools_gl2psListDelete(gl2ps->pdfprimlist);  << 4925   tools_gl2psListDelete(tools_gl2ps_context->pdfprimlist);
5270   tools_gl2psPDFgroupListDelete(gl2ps);       << 4926   tools_tools_gl2psPDFgroupListDelete();
5271                                                  4927 
5272 #if defined(TOOLS_GL2PS_HAVE_ZLIB)               4928 #if defined(TOOLS_GL2PS_HAVE_ZLIB)
5273   if(gl2ps->options & TOOLS_GL2PS_COMPRESS){  << 4929   if(tools_gl2ps_context->options & TOOLS_GL2PS_COMPRESS){
5274     tools_gl2psFreeCompress(gl2ps);           << 4930     tools_gl2psFreeCompress();
5275     tools_gl2psFree(gl2ps->compress);         << 4931     tools_gl2psFree(tools_gl2ps_context->compress);
5276     gl2ps->compress = NULL;                   << 4932     tools_gl2ps_context->compress = NULL;
5277   }                                              4933   }
5278 #endif                                           4934 #endif
5279 }                                                4935 }
5280                                                  4936 
5281 /* PDF begin viewport */                         4937 /* PDF begin viewport */
5282                                                  4938 
5283 inline void tools_gl2psPrintPDFBeginViewport( << 4939 static void tools_gl2psPrintPDFBeginViewport(tools_GLint viewport[4])
5284 {                                                4940 {
5285   int offs = 0;                                  4941   int offs = 0;
5286   tools_GLint idx;                               4942   tools_GLint idx;
5287   tools_GLfloat rgba[4];                         4943   tools_GLfloat rgba[4];
5288   int x = viewport[0], y = viewport[1], w = v    4944   int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3];
5289                                                  4945 
5290   tools_glRenderMode(TOOLS_GL_FEEDBACK);         4946   tools_glRenderMode(TOOLS_GL_FEEDBACK);
5291                                                  4947 
5292   tools_gl2psResetLineProperties(gl2ps);      << 4948   tools_gl2psResetLineProperties();
5293                                                  4949 
5294   if(gl2ps->header){                          << 4950   if(tools_gl2ps_context->header){
5295     tools_gl2psPrintPDFHeader(gl2ps);         << 4951     tools_gl2psPrintPDFHeader();
5296     gl2ps->header = TOOLS_GL_FALSE;           << 4952     tools_gl2ps_context->header = TOOLS_GL_FALSE;
5297   }                                              4953   }
5298                                                  4954 
5299   offs += tools_gl2psPrintf(gl2ps,"q\n");     << 4955   offs += tools_gl2psPrintf("q\n");
5300                                                  4956 
5301   if(gl2ps->options & TOOLS_GL2PS_DRAW_BACKGR << 4957   if(tools_gl2ps_context->options & TOOLS_GL2PS_DRAW_BACKGROUND){
5302     if(gl2ps->colormode == TOOLS_GL_RGBA || g << 4958     if(tools_gl2ps_context->colormode == TOOLS_GL_RGBA || tools_gl2ps_context->colorsize == 0){
5303       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_    4959       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_VALUE, rgba);
5304     }                                            4960     }
5305     else{                                        4961     else{
5306       tools_glGetIntegerv(TOOLS_GL_INDEX_CLEA    4962       tools_glGetIntegerv(TOOLS_GL_INDEX_CLEAR_VALUE, &idx);
5307       rgba[0] = gl2ps->colormap[idx][0];      << 4963       rgba[0] = tools_gl2ps_context->colormap[idx][0];
5308       rgba[1] = gl2ps->colormap[idx][1];      << 4964       rgba[1] = tools_gl2ps_context->colormap[idx][1];
5309       rgba[2] = gl2ps->colormap[idx][2];      << 4965       rgba[2] = tools_gl2ps_context->colormap[idx][2];
5310       rgba[3] = 1.0F;                            4966       rgba[3] = 1.0F;
5311     }                                            4967     }
5312     offs += tools_gl2psPrintPDFFillColor(gl2p << 4968     offs += tools_gl2psPrintPDFFillColor(rgba);
5313     offs += tools_gl2psPrintf(gl2ps,"%d %d %d << 4969     offs += tools_gl2psPrintf("%d %d %d %d re\n"
5314                         "W\n"                    4970                         "W\n"
5315                         "f\n",                   4971                         "f\n",
5316                         x, y, w, h);             4972                         x, y, w, h);
5317   }                                              4973   }
5318   else{                                          4974   else{
5319     offs += tools_gl2psPrintf(gl2ps,"%d %d %d << 4975     offs += tools_gl2psPrintf("%d %d %d %d re\n"
5320                         "W\n"                    4976                         "W\n"
5321                         "n\n",                   4977                         "n\n",
5322                         x, y, w, h);             4978                         x, y, w, h);
5323   }                                              4979   }
5324                                                  4980 
5325   gl2ps->streamlength += offs;                << 4981   tools_gl2ps_context->streamlength += offs;
5326 }                                                4982 }
5327                                                  4983 
5328 inline tools_GLint tools_gl2psPrintPDFEndView << 4984 static tools_GLint tools_gl2psPrintPDFEndViewport(void)
5329 {                                                4985 {
5330   tools_GLint res;                               4986   tools_GLint res;
5331                                                  4987 
5332   res = tools_gl2psPrintPrimitives(gl2ps);    << 4988   res = tools_gl2psPrintPrimitives();
5333   gl2ps->streamlength += tools_gl2psPrintf(gl << 4989   tools_gl2ps_context->streamlength += tools_gl2psPrintf("Q\n");
5334   return res;                                    4990   return res;
5335 }                                                4991 }
5336                                                  4992 
5337 inline void tools_gl2psPrintPDFFinalPrimitive << 4993 static void tools_gl2psPrintPDFFinalPrimitive(void)
5338 {                                                4994 {
5339 }                                                4995 }
5340                                                  4996 
5341 /* definition of the PDF backend */              4997 /* definition of the PDF backend */
5342                                                  4998 
5343 static const tools_GL2PSbackend tools_gl2psPD << 4999 static tools_GL2PSbackend tools_gl2psPDF = {
5344   tools_gl2psPrintPDFHeader,                     5000   tools_gl2psPrintPDFHeader,
5345   tools_gl2psPrintPDFFooter,                     5001   tools_gl2psPrintPDFFooter,
5346   tools_gl2psPrintPDFBeginViewport,              5002   tools_gl2psPrintPDFBeginViewport,
5347   tools_gl2psPrintPDFEndViewport,                5003   tools_gl2psPrintPDFEndViewport,
5348   tools_gl2psPrintPDFPrimitive,                  5004   tools_gl2psPrintPDFPrimitive,
5349   tools_gl2psPrintPDFFinalPrimitive,             5005   tools_gl2psPrintPDFFinalPrimitive,
5350   "pdf",                                         5006   "pdf",
5351   "Portable Document Format"                     5007   "Portable Document Format"
5352 };                                               5008 };
5353                                                  5009 
5354 /********************************************    5010 /*********************************************************************
5355  *                                               5011  *
5356  * SVG routines                                  5012  * SVG routines
5357  *                                               5013  *
5358  ********************************************    5014  *********************************************************************/
5359                                                  5015 
5360 inline void tools_gl2psSVGGetCoordsAndColors( << 5016 static void tools_tools_gl2psSVGGetCoordsAndColors(int n, tools_GL2PSvertex *verts,
5361                                        tools_    5017                                        tools_GL2PSxyz *xyz, tools_GL2PSrgba *rgba)
5362 {                                                5018 {
5363   int i, j;                                      5019   int i, j;
5364                                                  5020 
5365   for(i = 0; i < n; i++){                        5021   for(i = 0; i < n; i++){
5366     xyz[i][0] = verts[i].xyz[0];                 5022     xyz[i][0] = verts[i].xyz[0];
5367     xyz[i][1] = gl2ps->viewport[3] - verts[i] << 5023     xyz[i][1] = tools_gl2ps_context->viewport[3] - verts[i].xyz[1];
5368     xyz[i][2] = 0.0F;                            5024     xyz[i][2] = 0.0F;
5369     for(j = 0; j < 4; j++)                       5025     for(j = 0; j < 4; j++)
5370       rgba[i][j] = verts[i].rgba[j];             5026       rgba[i][j] = verts[i].rgba[j];
5371   }                                              5027   }
5372 }                                                5028 }
5373                                                  5029 
5374 #include <sstream>  //G.Barrand               << 5030 static void tools_tools_gl2psSVGGetColorString(tools_GL2PSrgba rgba, char str[32])
5375 #include <iomanip>  //G.Barrand               << 
5376                                               << 
5377 inline void tools_gl2psSVGGetColorString(tool << 
5378 {                                                5031 {
5379   int _r = (int)(255. * rgba[0]);             << 5032   int r = (int)(255. * rgba[0]);
5380   int _g = (int)(255. * rgba[1]);             << 5033   int g = (int)(255. * rgba[1]);
5381   int _b = (int)(255. * rgba[2]);             << 5034   int b = (int)(255. * rgba[2]);
5382   int rc = (_r < 0) ? 0 : (_r > 255) ? 255 :  << 5035   int rc = (r < 0) ? 0 : (r > 255) ? 255 : r;
5383   int gc = (_g < 0) ? 0 : (_g > 255) ? 255 :  << 5036   int gc = (g < 0) ? 0 : (g > 255) ? 255 : g;
5384   int bc = (_b < 0) ? 0 : (_b > 255) ? 255 :  << 5037   int bc = (b < 0) ? 0 : (b > 255) ? 255 : b;
5385 //sprintf(str, "#%2.2x%2.2x%2.2x", rc, gc, bc << 5038   sprintf(str, "#%2.2x%2.2x%2.2x", rc, gc, bc);
5386 //G.Barrand:begin:                            << 
5387   std::ostringstream oss;                     << 
5388   oss << "#";                                 << 
5389   oss << std::setw(2) << std::setfill('0') << << 
5390   oss << std::setw(2) << std::setfill('0') << << 
5391   oss << std::setw(2) << std::setfill('0') << << 
5392   strcpy(str,oss.str().c_str());              << 
5393 //G.Barrand:end.                              << 
5394 }                                                5039 }
5395                                                  5040 
5396 inline void tools_gl2psPrintSVGHeader(tools_G << 5041 static void tools_gl2psPrintSVGHeader(void)
5397 {                                                5042 {
5398   int x, y, width, height;                       5043   int x, y, width, height;
5399   char col[32];                                  5044   char col[32];
5400   time_t now;                                    5045   time_t now;
5401                                                  5046 
5402   time(&now);                                    5047   time(&now);
5403                                                  5048 
5404   if (gl2ps->options & TOOLS_GL2PS_LANDSCAPE) << 5049   if (tools_gl2ps_context->options & TOOLS_GL2PS_LANDSCAPE){
5405     x = (int)gl2ps->viewport[1];              << 5050     x = (int)tools_gl2ps_context->viewport[1];
5406     y = (int)gl2ps->viewport[0];              << 5051     y = (int)tools_gl2ps_context->viewport[0];
5407     width = (int)gl2ps->viewport[3];          << 5052     width = (int)tools_gl2ps_context->viewport[3];
5408     height = (int)gl2ps->viewport[2];         << 5053     height = (int)tools_gl2ps_context->viewport[2];
5409   }                                              5054   }
5410   else{                                          5055   else{
5411     x = (int)gl2ps->viewport[0];              << 5056     x = (int)tools_gl2ps_context->viewport[0];
5412     y = (int)gl2ps->viewport[1];              << 5057     y = (int)tools_gl2ps_context->viewport[1];
5413     width = (int)gl2ps->viewport[2];          << 5058     width = (int)tools_gl2ps_context->viewport[2];
5414     height = (int)gl2ps->viewport[3];         << 5059     height = (int)tools_gl2ps_context->viewport[3];
5415   }                                              5060   }
5416                                                  5061 
5417   /* Compressed SVG files (.svgz) are simply     5062   /* Compressed SVG files (.svgz) are simply gzipped SVG files */
5418   tools_gl2psPrintGzipHeader(gl2ps);          << 5063   tools_gl2psPrintGzipHeader();
5419                                                  5064 
5420   tools_gl2psPrintf(gl2ps,"<?xml version=\"1. << 5065   tools_gl2psPrintf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
5421   tools_gl2psPrintf(gl2ps,"<svg xmlns=\"http: << 5066   tools_gl2psPrintf("<svg xmlns=\"http://www.w3.org/2000/svg\"\n");
5422   tools_gl2psPrintf(gl2ps,"     xmlns:xlink=\ << 5067   tools_gl2psPrintf("     xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
5423               "     width=\"%dpt\" height=\"% << 5068               "     width=\"%dpx\" height=\"%dpx\" viewBox=\"%d %d %d %d\">\n",
5424               width, height, x, y, width, hei    5069               width, height, x, y, width, height);
5425   tools_gl2psPrintf(gl2ps,"<title>%s</title>\ << 5070   tools_gl2psPrintf("<title>%s</title>\n", tools_gl2ps_context->title);
5426   tools_gl2psPrintf(gl2ps,"<desc>\n");        << 5071   tools_gl2psPrintf("<desc>\n");
5427   tools_gl2psPrintf(gl2ps,"Creator: GL2PS %d. << 5072   tools_gl2psPrintf("Creator: GL2PS %d.%d.%d%s, %s\n"
5428               "For: %s\n"                        5073               "For: %s\n"
5429               "CreationDate: %s",                5074               "CreationDate: %s",
5430               TOOLS_GL2PS_MAJOR_VERSION, TOOL    5075               TOOLS_GL2PS_MAJOR_VERSION, TOOLS_GL2PS_MINOR_VERSION, TOOLS_GL2PS_PATCH_VERSION,
5431               TOOLS_GL2PS_EXTRA_VERSION, TOOL << 5076               TOOLS_GL2PS_EXTRA_VERSION, TOOLS_GL2PS_COPYRIGHT, tools_gl2ps_context->producer, ctime(&now));
5432   tools_gl2psPrintf(gl2ps,"</desc>\n");       << 5077   tools_gl2psPrintf("</desc>\n");
5433   tools_gl2psPrintf(gl2ps,"<defs>\n");        << 5078   tools_gl2psPrintf("<defs>\n");
5434   tools_gl2psPrintf(gl2ps,"</defs>\n");       << 5079   tools_gl2psPrintf("</defs>\n");
5435                                               << 5080 
5436   if(gl2ps->options & TOOLS_GL2PS_DRAW_BACKGR << 5081   if(tools_gl2ps_context->options & TOOLS_GL2PS_DRAW_BACKGROUND){
5437     tools_gl2psSVGGetColorString(gl2ps->bgcol << 5082     tools_tools_gl2psSVGGetColorString(tools_gl2ps_context->bgcolor, col);
5438     tools_gl2psPrintf(gl2ps,"<polygon fill=\" << 5083     tools_gl2psPrintf("<polygon fill=\"%s\" points=\"%d,%d %d,%d %d,%d %d,%d\"/>\n", col,
5439                 (int)gl2ps->viewport[0], (int << 5084                 (int)tools_gl2ps_context->viewport[0], (int)tools_gl2ps_context->viewport[1],
5440                 (int)gl2ps->viewport[2], (int << 5085                 (int)tools_gl2ps_context->viewport[2], (int)tools_gl2ps_context->viewport[1],
5441                 (int)gl2ps->viewport[2], (int << 5086                 (int)tools_gl2ps_context->viewport[2], (int)tools_gl2ps_context->viewport[3],
5442                 (int)gl2ps->viewport[0], (int << 5087                 (int)tools_gl2ps_context->viewport[0], (int)tools_gl2ps_context->viewport[3]);
5443   }                                              5088   }
5444                                                  5089 
5445   /* group all the primitives and disable ant    5090   /* group all the primitives and disable antialiasing */
5446   tools_gl2psPrintf(gl2ps,"<g>\n");           << 5091   tools_gl2psPrintf("<g>\n");
5447 }                                                5092 }
5448                                                  5093 
5449 inline void tools_gl2psPrintSVGSmoothTriangle << 5094 static void tools_gl2psPrintSVGSmoothTriangle(tools_GL2PSxyz xyz[3], tools_GL2PSrgba rgba[3])
5450 {                                                5095 {
5451   int i;                                         5096   int i;
5452   tools_GL2PSxyz xyz2[3];                        5097   tools_GL2PSxyz xyz2[3];
5453   tools_GL2PSrgba rgba2[3];                      5098   tools_GL2PSrgba rgba2[3];
5454   char col[32];                                  5099   char col[32];
5455                                                  5100 
5456   /* Apparently there is no easy way to do Go    5101   /* Apparently there is no easy way to do Gouraud shading in SVG
5457      without explicitly pre-defining gradient    5102      without explicitly pre-defining gradients, so for now we just do
5458      recursive subdivision */                    5103      recursive subdivision */
5459                                                  5104 
5460   if(tools_gl2psSameColorThreshold(3, rgba, g << 5105   if(tools_tools_gl2psSameColorThreshold(3, rgba, tools_gl2ps_context->threshold)){
5461     tools_gl2psSVGGetColorString(rgba[0], col << 5106     tools_tools_gl2psSVGGetColorString(rgba[0], col);
5462     tools_gl2psPrintf(gl2ps,"<polygon fill=\" << 5107     tools_gl2psPrintf("<polygon fill=\"%s\" ", col);
5463     if(rgba[0][3] < 1.0F) tools_gl2psPrintf(g << 5108     if(rgba[0][3] < 1.0F) tools_gl2psPrintf("fill-opacity=\"%g\" ", rgba[0][3]);
5464     tools_gl2psPrintf(gl2ps,"shape-rendering= << 5109     tools_gl2psPrintf("shape-rendering=\"crispEdges\" ");
5465     tools_gl2psPrintf(gl2ps,"points=\"%g,%g % << 5110     tools_gl2psPrintf("points=\"%g,%g %g,%g %g,%g\"/>\n", xyz[0][0], xyz[0][1],
5466                 xyz[1][0], xyz[1][1], xyz[2][    5111                 xyz[1][0], xyz[1][1], xyz[2][0], xyz[2][1]);
5467   }                                              5112   }
5468   else{                                          5113   else{
5469     /* subdivide into 4 subtriangles */          5114     /* subdivide into 4 subtriangles */
5470     for(i = 0; i < 3; i++){                      5115     for(i = 0; i < 3; i++){
5471       xyz2[0][i] = xyz[0][i];                    5116       xyz2[0][i] = xyz[0][i];
5472       xyz2[1][i] = 0.5F * (xyz[0][i] + xyz[1]    5117       xyz2[1][i] = 0.5F * (xyz[0][i] + xyz[1][i]);
5473       xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2]    5118       xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2][i]);
5474     }                                            5119     }
5475     for(i = 0; i < 4; i++){                      5120     for(i = 0; i < 4; i++){
5476       rgba2[0][i] = rgba[0][i];                  5121       rgba2[0][i] = rgba[0][i];
5477       rgba2[1][i] = 0.5F * (rgba[0][i] + rgba    5122       rgba2[1][i] = 0.5F * (rgba[0][i] + rgba[1][i]);
5478       rgba2[2][i] = 0.5F * (rgba[0][i] + rgba    5123       rgba2[2][i] = 0.5F * (rgba[0][i] + rgba[2][i]);
5479     }                                            5124     }
5480     tools_gl2psPrintSVGSmoothTriangle(gl2ps,  << 5125     tools_gl2psPrintSVGSmoothTriangle(xyz2, rgba2);
5481     for(i = 0; i < 3; i++){                      5126     for(i = 0; i < 3; i++){
5482       xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1]    5127       xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1][i]);
5483       xyz2[1][i] = xyz[1][i];                    5128       xyz2[1][i] = xyz[1][i];
5484       xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2]    5129       xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2][i]);
5485     }                                            5130     }
5486     for(i = 0; i < 4; i++){                      5131     for(i = 0; i < 4; i++){
5487       rgba2[0][i] = 0.5F * (rgba[0][i] + rgba    5132       rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[1][i]);
5488       rgba2[1][i] = rgba[1][i];                  5133       rgba2[1][i] = rgba[1][i];
5489       rgba2[2][i] = 0.5F * (rgba[1][i] + rgba    5134       rgba2[2][i] = 0.5F * (rgba[1][i] + rgba[2][i]);
5490     }                                            5135     }
5491     tools_gl2psPrintSVGSmoothTriangle(gl2ps,  << 5136     tools_gl2psPrintSVGSmoothTriangle(xyz2, rgba2);
5492     for(i = 0; i < 3; i++){                      5137     for(i = 0; i < 3; i++){
5493       xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[2]    5138       xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[2][i]);
5494       xyz2[1][i] = xyz[2][i];                    5139       xyz2[1][i] = xyz[2][i];
5495       xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2]    5140       xyz2[2][i] = 0.5F * (xyz[1][i] + xyz[2][i]);
5496     }                                            5141     }
5497     for(i = 0; i < 4; i++){                      5142     for(i = 0; i < 4; i++){
5498       rgba2[0][i] = 0.5F * (rgba[0][i] + rgba    5143       rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[2][i]);
5499       rgba2[1][i] = rgba[2][i];                  5144       rgba2[1][i] = rgba[2][i];
5500       rgba2[2][i] = 0.5F * (rgba[1][i] + rgba    5145       rgba2[2][i] = 0.5F * (rgba[1][i] + rgba[2][i]);
5501     }                                            5146     }
5502     tools_gl2psPrintSVGSmoothTriangle(gl2ps,  << 5147     tools_gl2psPrintSVGSmoothTriangle(xyz2, rgba2);
5503     for(i = 0; i < 3; i++){                      5148     for(i = 0; i < 3; i++){
5504       xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1]    5149       xyz2[0][i] = 0.5F * (xyz[0][i] + xyz[1][i]);
5505       xyz2[1][i] = 0.5F * (xyz[1][i] + xyz[2]    5150       xyz2[1][i] = 0.5F * (xyz[1][i] + xyz[2][i]);
5506       xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2]    5151       xyz2[2][i] = 0.5F * (xyz[0][i] + xyz[2][i]);
5507     }                                            5152     }
5508     for(i = 0; i < 4; i++){                      5153     for(i = 0; i < 4; i++){
5509       rgba2[0][i] = 0.5F * (rgba[0][i] + rgba    5154       rgba2[0][i] = 0.5F * (rgba[0][i] + rgba[1][i]);
5510       rgba2[1][i] = 0.5F * (rgba[1][i] + rgba    5155       rgba2[1][i] = 0.5F * (rgba[1][i] + rgba[2][i]);
5511       rgba2[2][i] = 0.5F * (rgba[0][i] + rgba    5156       rgba2[2][i] = 0.5F * (rgba[0][i] + rgba[2][i]);
5512     }                                            5157     }
5513     tools_gl2psPrintSVGSmoothTriangle(gl2ps,  << 5158     tools_gl2psPrintSVGSmoothTriangle(xyz2, rgba2);
5514   }                                              5159   }
5515 }                                                5160 }
5516                                                  5161 
5517 inline void tools_gl2psPrintSVGDash(tools_GL2 << 5162 static void tools_gl2psPrintSVGDash(tools_GLushort pattern, tools_GLint factor)
5518 {                                                5163 {
5519   int i, n, array[10];                           5164   int i, n, array[10];
5520                                                  5165 
5521   if(!pattern || !factor) return; /* solid li    5166   if(!pattern || !factor) return; /* solid line */
5522                                                  5167 
5523   tools_gl2psParseStipplePattern(pattern, fac    5168   tools_gl2psParseStipplePattern(pattern, factor, &n, array);
5524   tools_gl2psPrintf(gl2ps,"stroke-dasharray=\ << 5169   tools_gl2psPrintf("stroke-dasharray=\"");
5525   for(i = 0; i < n; i++){                        5170   for(i = 0; i < n; i++){
5526     if(i) tools_gl2psPrintf(gl2ps,",");       << 5171     if(i) tools_gl2psPrintf(",");
5527     tools_gl2psPrintf(gl2ps,"%d", array[i]);  << 5172     tools_gl2psPrintf("%d", array[i]);
5528   }                                              5173   }
5529   tools_gl2psPrintf(gl2ps,"\" ");             << 5174   tools_gl2psPrintf("\" ");
5530 }                                                5175 }
5531                                                  5176 
5532 inline void tools_gl2psEndSVGLine(tools_GL2PS << 5177 static void tools_gl2psEndSVGLine(void)
5533 {                                                5178 {
5534   int i;                                         5179   int i;
5535   if(gl2ps->lastvertex.rgba[0] >= 0.){        << 5180   if(tools_gl2ps_context->lastvertex.rgba[0] >= 0.){
5536     tools_gl2psPrintf(gl2ps,"%g,%g\"/>\n", gl << 5181     tools_gl2psPrintf("%g,%g\"/>\n", tools_gl2ps_context->lastvertex.xyz[0],
5537                 gl2ps->viewport[3] - gl2ps->l << 5182                 tools_gl2ps_context->viewport[3] - tools_gl2ps_context->lastvertex.xyz[1]);
5538     for(i = 0; i < 3; i++)                       5183     for(i = 0; i < 3; i++)
5539       gl2ps->lastvertex.xyz[i] = -1.;         << 5184       tools_gl2ps_context->lastvertex.xyz[i] = -1.;
5540     for(i = 0; i < 4; i++)                       5185     for(i = 0; i < 4; i++)
5541       gl2ps->lastvertex.rgba[i] = -1.;        << 5186       tools_gl2ps_context->lastvertex.rgba[i] = -1.;
5542   }                                              5187   }
5543 }                                                5188 }
5544                                                  5189 
5545 inline void tools_gl2psPrintSVGPixmap(tools_G << 5190 static void tools_gl2psPrintSVGPixmap(tools_GLfloat x, tools_GLfloat y, tools_GL2PSimage *pixmap)
5546 {                                                5191 {
5547 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)             5192 #if defined(TOOLS_GL2PS_HAVE_LIBPNG)
5548   tools_GL2PSlist *png;                          5193   tools_GL2PSlist *png;
5549   unsigned char c;                               5194   unsigned char c;
5550   int i;                                         5195   int i;
5551                                                  5196 
5552   /* The only image types supported by the SV    5197   /* The only image types supported by the SVG standard are JPEG, PNG
5553      and SVG. Here we choose PNG, and since w    5198      and SVG. Here we choose PNG, and since we want to embed the image
5554      directly in the SVG stream (and not link    5199      directly in the SVG stream (and not link to an external image
5555      file), we need to encode the pixmap into    5200      file), we need to encode the pixmap into PNG in memory, then
5556      encode it into base64. */                   5201      encode it into base64. */
5557                                                  5202 
5558   png = tools_gl2psListCreate(pixmap->width *    5203   png = tools_gl2psListCreate(pixmap->width * pixmap->height * 3, 1000,
5559                         sizeof(unsigned char)    5204                         sizeof(unsigned char));
5560   tools_gl2psConvertPixmapToPNG(pixmap, png);    5205   tools_gl2psConvertPixmapToPNG(pixmap, png);
5561   tools_gl2psListEncodeBase64(png);              5206   tools_gl2psListEncodeBase64(png);
5562                                                  5207 
5563   /* Use "transform" attribute to scale and t    5208   /* Use "transform" attribute to scale and translate the image from
5564      the coordinates origin (0,0) */             5209      the coordinates origin (0,0) */
5565   y -= pixmap->zoom_y * (tools_GLfloat)pixmap    5210   y -= pixmap->zoom_y * (tools_GLfloat)pixmap->height;
5566   tools_gl2psPrintf(gl2ps,"<image x=\"%g\" y= << 5211   tools_gl2psPrintf("<image x=\"%g\" y=\"%g\" width=\"%d\" height=\"%d\"\n",
5567               0., 0., pixmap->width, pixmap->    5212               0., 0., pixmap->width, pixmap->height);
5568   tools_gl2psPrintf(gl2ps,"transform=\"matrix << 5213   tools_gl2psPrintf("transform=\"matrix(%g,0,0,%g,%g,%g)\"\n",
5569               pixmap->zoom_x, pixmap->zoom_y,    5214               pixmap->zoom_x, pixmap->zoom_y, x, y);
5570   tools_gl2psPrintf(gl2ps,"xlink:href=\"data: << 5215   tools_gl2psPrintf("xlink:href=\"data:image/png;base64,");
5571   for(i = 0; i < tools_gl2psListNbr(png); i++    5216   for(i = 0; i < tools_gl2psListNbr(png); i++){
5572     tools_gl2psListRead(png, i, &c);             5217     tools_gl2psListRead(png, i, &c);
5573     tools_gl2psPrintf(gl2ps,"%c", c);         << 5218     tools_gl2psPrintf("%c", c);
5574   }                                              5219   }
5575   tools_gl2psPrintf(gl2ps,"\"/>\n");          << 5220   tools_gl2psPrintf("\"/>\n");
5576   tools_gl2psListDelete(png);                    5221   tools_gl2psListDelete(png);
5577 #else                                            5222 #else
5578   (void) x; (void) y; (void) pixmap;  /* not     5223   (void) x; (void) y; (void) pixmap;  /* not used */
5579   tools_gl2psMsg(TOOLS_GL2PS_WARNING, "GL2PS     5224   tools_gl2psMsg(TOOLS_GL2PS_WARNING, "GL2PS must be compiled with PNG support in "
5580            "order to embed images in SVG stre    5225            "order to embed images in SVG streams");
5581 #endif                                           5226 #endif
5582   (void)gl2ps;                                << 
5583 }                                                5227 }
5584                                                  5228 
5585 inline void tools_gl2psPrintSVGPrimitive(tool << 5229 static void tools_gl2psPrintSVGPrimitive(void *data)
5586 {                                                5230 {
5587   tools_GL2PSprimitive *prim;                    5231   tools_GL2PSprimitive *prim;
5588   tools_GL2PSxyz xyz[4];                         5232   tools_GL2PSxyz xyz[4];
5589   tools_GL2PSrgba rgba[4];                       5233   tools_GL2PSrgba rgba[4];
5590   char col[32];                                  5234   char col[32];
5591   char lcap[7], ljoin[7];                        5235   char lcap[7], ljoin[7];
5592   int newline;                                   5236   int newline;
5593                                                  5237 
5594   prim = *(tools_GL2PSprimitive**)data;          5238   prim = *(tools_GL2PSprimitive**)data;
5595                                                  5239 
5596   if((gl2ps->options & TOOLS_GL2PS_OCCLUSION_ << 5240   if((tools_gl2ps_context->options & TOOLS_GL2PS_OCCLUSION_CULL) && prim->culled) return;
5597                                                  5241 
5598   /* We try to draw connected lines as a sing    5242   /* We try to draw connected lines as a single path to get nice line
5599      joins and correct stippling. So if the p    5243      joins and correct stippling. So if the primitive to print is not
5600      a line we must first finish the current     5244      a line we must first finish the current line (if any): */
5601   if(prim->type != TOOLS_GL2PS_LINE) tools_gl << 5245   if(prim->type != TOOLS_GL2PS_LINE) tools_gl2psEndSVGLine();
5602                                                  5246 
5603   tools_gl2psSVGGetCoordsAndColors(gl2ps, pri << 5247   tools_tools_gl2psSVGGetCoordsAndColors(prim->numverts, prim->verts, xyz, rgba);
5604                                                  5248 
5605   switch(prim->type){                            5249   switch(prim->type){
5606   case TOOLS_GL2PS_POINT :                       5250   case TOOLS_GL2PS_POINT :
5607     tools_gl2psSVGGetColorString(rgba[0], col << 5251     tools_tools_gl2psSVGGetColorString(rgba[0], col);
5608     tools_gl2psPrintf(gl2ps,"<circle fill=\"% << 5252     tools_gl2psPrintf("<circle fill=\"%s\" ", col);
5609     if(rgba[0][3] < 1.0F) tools_gl2psPrintf(g << 5253     if(rgba[0][3] < 1.0F) tools_gl2psPrintf("fill-opacity=\"%g\" ", rgba[0][3]);
5610     tools_gl2psPrintf(gl2ps,"cx=\"%g\" cy=\"% << 5254     tools_gl2psPrintf("cx=\"%g\" cy=\"%g\" r=\"%g\"/>\n",
5611                 xyz[0][0], xyz[0][1], 0.5 * p    5255                 xyz[0][0], xyz[0][1], 0.5 * prim->width);
5612     break;                                       5256     break;
5613   case TOOLS_GL2PS_LINE :                        5257   case TOOLS_GL2PS_LINE :
5614     if(!tools_gl2psSamePosition(gl2ps->lastve << 5258     if(!tools_gl2psSamePosition(tools_gl2ps_context->lastvertex.xyz, prim->verts[0].xyz) ||
5615        !tools_gl2psSameColor(gl2ps->lastrgba, << 5259        !tools_gl2psSameColor(tools_gl2ps_context->lastrgba, prim->verts[0].rgba) ||
5616        gl2ps->lastlinewidth != prim->width || << 5260        tools_gl2ps_context->lastlinewidth != prim->width ||
5617        gl2ps->lastlinecap != prim->linecap || << 5261        tools_gl2ps_context->lastlinecap != prim->linecap ||
5618        gl2ps->lastlinejoin != prim->linejoin  << 5262        tools_gl2ps_context->lastlinejoin != prim->linejoin ||
5619        gl2ps->lastpattern != prim->pattern || << 5263        tools_gl2ps_context->lastpattern != prim->pattern ||
5620        gl2ps->lastfactor != prim->factor){    << 5264        tools_gl2ps_context->lastfactor != prim->factor){
5621       /* End the current line if the new segm    5265       /* End the current line if the new segment does not start where
5622          the last one ended, or if the color,    5266          the last one ended, or if the color, the width or the
5623          stippling have changed (we will need    5267          stippling have changed (we will need to use multi-point
5624          gradients for smooth-shaded lines) *    5268          gradients for smooth-shaded lines) */
5625       tools_gl2psEndSVGLine(gl2ps);           << 5269       tools_gl2psEndSVGLine();
5626       newline = 1;                               5270       newline = 1;
5627     }                                            5271     }
5628     else{                                        5272     else{
5629       newline = 0;                               5273       newline = 0;
5630     }                                            5274     }
5631     gl2ps->lastvertex = prim->verts[1];       << 5275     tools_gl2ps_context->lastvertex = prim->verts[1];
5632     tools_gl2psSetLastColor(gl2ps, prim->vert << 5276     tools_gl2psSetLastColor(prim->verts[0].rgba);
5633     gl2ps->lastlinewidth = prim->width;       << 5277     tools_gl2ps_context->lastlinewidth = prim->width;
5634     gl2ps->lastlinecap = prim->linecap;       << 5278     tools_gl2ps_context->lastlinecap = prim->linecap;
5635     gl2ps->lastlinejoin = prim->linejoin;     << 5279     tools_gl2ps_context->lastlinejoin = prim->linejoin;
5636     gl2ps->lastpattern = prim->pattern;       << 5280     tools_gl2ps_context->lastpattern = prim->pattern;
5637     gl2ps->lastfactor = prim->factor;         << 5281     tools_gl2ps_context->lastfactor = prim->factor;
5638     if(newline){                                 5282     if(newline){
5639       tools_gl2psSVGGetColorString(rgba[0], c << 5283       tools_tools_gl2psSVGGetColorString(rgba[0], col);
5640       tools_gl2psPrintf(gl2ps,"<polyline fill << 5284       tools_gl2psPrintf("<polyline fill=\"none\" stroke=\"%s\" stroke-width=\"%g\" ",
5641                   col, prim->width);             5285                   col, prim->width);
5642       switch (prim->linecap){                    5286       switch (prim->linecap){
5643       case TOOLS_GL2PS_LINE_CAP_BUTT:            5287       case TOOLS_GL2PS_LINE_CAP_BUTT:
5644       //sprintf (lcap, "%s", "butt");  //G.Ba << 5288         sprintf (lcap, "%s", "butt");
5645         strcpy (lcap, "butt");         //G.Ba << 
5646         break;                                   5289         break;
5647       case TOOLS_GL2PS_LINE_CAP_ROUND:           5290       case TOOLS_GL2PS_LINE_CAP_ROUND:
5648       //sprintf (lcap, "%s", "round"); //G.Ba << 5291         sprintf (lcap, "%s", "round");
5649         strcpy (lcap, "round");        //G.Ba << 
5650         break;                                   5292         break;
5651       case TOOLS_GL2PS_LINE_CAP_SQUARE:          5293       case TOOLS_GL2PS_LINE_CAP_SQUARE:
5652       //sprintf (lcap, "%s", "square"); //G.B << 5294         sprintf (lcap, "%s", "square");
5653         strcpy (lcap, "square");        //G.B << 
5654         break;                                   5295         break;
5655       default: /*G.Barrand : to quiet Coverit    5296       default: /*G.Barrand : to quiet Coverity :*/
5656       //sprintf (lcap, "%s", "butt");   //G.B << 5297         sprintf (lcap, "%s", "butt");
5657         strcpy (lcap, "butt");          //G.B << 
5658         break;                                   5298         break;
5659       }                                          5299       }
5660       switch (prim->linejoin){                   5300       switch (prim->linejoin){
5661       case TOOLS_GL2PS_LINE_JOIN_MITER:          5301       case TOOLS_GL2PS_LINE_JOIN_MITER:
5662       //sprintf (ljoin, "%s", "miter");  //G. << 5302         sprintf (ljoin, "%s", "miter");
5663         strcpy (ljoin, "miter");         //G. << 
5664         break;                                   5303         break;
5665       case TOOLS_GL2PS_LINE_JOIN_ROUND:          5304       case TOOLS_GL2PS_LINE_JOIN_ROUND:
5666       //sprintf (ljoin, "%s", "round");  //G. << 5305         sprintf (ljoin, "%s", "round");
5667         strcpy (ljoin, "round");         //G. << 
5668         break;                                   5306         break;
5669       case TOOLS_GL2PS_LINE_JOIN_BEVEL:          5307       case TOOLS_GL2PS_LINE_JOIN_BEVEL:
5670       //sprintf (ljoin, "%s", "bevel");  //G. << 5308         sprintf (ljoin, "%s", "bevel");
5671         strcpy (ljoin, "bevel");         //G. << 
5672         break;                                   5309         break;
5673       default: /*G.Barrand : to quiet Coverit    5310       default: /*G.Barrand : to quiet Coverity :*/
5674       //sprintf (ljoin, "%s", "miter");  //G. << 5311         sprintf (ljoin, "%s", "miter");
5675         strcpy (ljoin, "miter");         //G. << 
5676         break;                                   5312         break;
5677       }                                          5313       }
5678       tools_gl2psPrintf(gl2ps,"stroke-linecap << 5314       tools_gl2psPrintf("stroke-linecap=\"%s\" stroke-linejoin=\"%s\" ",
5679                   lcap, ljoin);                  5315                   lcap, ljoin);
5680       if(rgba[0][3] < 1.0F) tools_gl2psPrintf << 5316       if(rgba[0][3] < 1.0F) tools_gl2psPrintf("stroke-opacity=\"%g\" ", rgba[0][3]);
5681       tools_gl2psPrintSVGDash(gl2ps, prim->pa << 5317       tools_gl2psPrintSVGDash(prim->pattern, prim->factor);
5682       tools_gl2psPrintf(gl2ps,"points=\"%g,%g << 5318       tools_gl2psPrintf("points=\"%g,%g ", xyz[0][0], xyz[0][1]);
5683     }                                            5319     }
5684     else{                                        5320     else{
5685       tools_gl2psPrintf(gl2ps,"%g,%g ", xyz[0 << 5321       tools_gl2psPrintf("%g,%g ", xyz[0][0], xyz[0][1]);
5686     }                                            5322     }
5687     break;                                       5323     break;
5688   case TOOLS_GL2PS_TRIANGLE :                    5324   case TOOLS_GL2PS_TRIANGLE :
5689     tools_gl2psPrintSVGSmoothTriangle(gl2ps,  << 5325     tools_gl2psPrintSVGSmoothTriangle(xyz, rgba);
5690     break;                                       5326     break;
5691   case TOOLS_GL2PS_QUADRANGLE :                  5327   case TOOLS_GL2PS_QUADRANGLE :
5692     tools_gl2psMsg(TOOLS_GL2PS_WARNING, "Ther    5328     tools_gl2psMsg(TOOLS_GL2PS_WARNING, "There should not be any quad left to print");
5693     break;                                       5329     break;
5694   case TOOLS_GL2PS_PIXMAP :                      5330   case TOOLS_GL2PS_PIXMAP :
5695     tools_gl2psPrintSVGPixmap(gl2ps,xyz[0][0] << 5331     tools_gl2psPrintSVGPixmap(xyz[0][0], xyz[0][1], prim->data.image);
5696     break;                                       5332     break;
5697   case TOOLS_GL2PS_TEXT :                        5333   case TOOLS_GL2PS_TEXT :
5698     tools_gl2psSVGGetColorString(prim->verts[ << 5334     tools_tools_gl2psSVGGetColorString(prim->verts[0].rgba, col);
5699     tools_gl2psPrintf(gl2ps,"<text fill=\"%s\ << 5335     tools_gl2psPrintf("<text fill=\"%s\" x=\"%g\" y=\"%g\" font-size=\"%d\" ",
5700                 col, xyz[0][0], xyz[0][1], pr    5336                 col, xyz[0][0], xyz[0][1], prim->data.text->fontsize);
5701     if(prim->data.text->angle)                   5337     if(prim->data.text->angle)
5702       tools_gl2psPrintf(gl2ps,"transform=\"ro << 5338       tools_gl2psPrintf("transform=\"rotate(%g, %g, %g)\" ",
5703                   -prim->data.text->angle, xy    5339                   -prim->data.text->angle, xyz[0][0], xyz[0][1]);
5704     switch(prim->data.text->alignment){          5340     switch(prim->data.text->alignment){
5705     case TOOLS_GL2PS_TEXT_C:                     5341     case TOOLS_GL2PS_TEXT_C:
5706       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5342       tools_gl2psPrintf("text-anchor=\"middle\" dy=\"%d\" ",
5707                   prim->data.text->fontsize /    5343                   prim->data.text->fontsize / 2);
5708       break;                                     5344       break;
5709     case TOOLS_GL2PS_TEXT_CL:                    5345     case TOOLS_GL2PS_TEXT_CL:
5710       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5346       tools_gl2psPrintf("text-anchor=\"start\" dy=\"%d\" ",
5711                   prim->data.text->fontsize /    5347                   prim->data.text->fontsize / 2);
5712       break;                                     5348       break;
5713     case TOOLS_GL2PS_TEXT_CR:                    5349     case TOOLS_GL2PS_TEXT_CR:
5714       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5350       tools_gl2psPrintf("text-anchor=\"end\" dy=\"%d\" ",
5715                   prim->data.text->fontsize /    5351                   prim->data.text->fontsize / 2);
5716       break;                                     5352       break;
5717     case TOOLS_GL2PS_TEXT_B:                     5353     case TOOLS_GL2PS_TEXT_B:
5718       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5354       tools_gl2psPrintf("text-anchor=\"middle\" dy=\"0\" ");
5719       break;                                     5355       break;
5720     case TOOLS_GL2PS_TEXT_BR:                    5356     case TOOLS_GL2PS_TEXT_BR:
5721       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5357       tools_gl2psPrintf("text-anchor=\"end\" dy=\"0\" ");
5722       break;                                     5358       break;
5723     case TOOLS_GL2PS_TEXT_T:                     5359     case TOOLS_GL2PS_TEXT_T:
5724       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5360       tools_gl2psPrintf("text-anchor=\"middle\" dy=\"%d\" ",
5725                   prim->data.text->fontsize);    5361                   prim->data.text->fontsize);
5726       break;                                     5362       break;
5727     case TOOLS_GL2PS_TEXT_TL:                    5363     case TOOLS_GL2PS_TEXT_TL:
5728       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5364       tools_gl2psPrintf("text-anchor=\"start\" dy=\"%d\" ",
5729                   prim->data.text->fontsize);    5365                   prim->data.text->fontsize);
5730       break;                                     5366       break;
5731     case TOOLS_GL2PS_TEXT_TR:                    5367     case TOOLS_GL2PS_TEXT_TR:
5732       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5368       tools_gl2psPrintf("text-anchor=\"end\" dy=\"%d\" ",
5733                   prim->data.text->fontsize);    5369                   prim->data.text->fontsize);
5734       break;                                     5370       break;
5735     case TOOLS_GL2PS_TEXT_BL:                    5371     case TOOLS_GL2PS_TEXT_BL:
5736     default: /* same as TOOLS_GL2PS_TEXT_BL *    5372     default: /* same as TOOLS_GL2PS_TEXT_BL */
5737       tools_gl2psPrintf(gl2ps,"text-anchor=\" << 5373       tools_gl2psPrintf("text-anchor=\"start\" dy=\"0\" ");
5738       break;                                     5374       break;
5739     }                                            5375     }
5740     if(!strcmp(prim->data.text->fontname, "Ti    5376     if(!strcmp(prim->data.text->fontname, "Times-Roman"))
5741       tools_gl2psPrintf(gl2ps,"font-family=\" << 5377       tools_gl2psPrintf("font-family=\"Times\">");
5742     else if(!strcmp(prim->data.text->fontname    5378     else if(!strcmp(prim->data.text->fontname, "Times-Bold"))
5743       tools_gl2psPrintf(gl2ps,"font-family=\" << 5379       tools_gl2psPrintf("font-family=\"Times\" font-weight=\"bold\">");
5744     else if(!strcmp(prim->data.text->fontname    5380     else if(!strcmp(prim->data.text->fontname, "Times-Italic"))
5745       tools_gl2psPrintf(gl2ps,"font-family=\" << 5381       tools_gl2psPrintf("font-family=\"Times\" font-style=\"italic\">");
5746     else if(!strcmp(prim->data.text->fontname    5382     else if(!strcmp(prim->data.text->fontname, "Times-BoldItalic"))
5747       tools_gl2psPrintf(gl2ps,"font-family=\" << 5383       tools_gl2psPrintf("font-family=\"Times\" font-style=\"italic\" font-weight=\"bold\">");
5748     else if(!strcmp(prim->data.text->fontname    5384     else if(!strcmp(prim->data.text->fontname, "Helvetica-Bold"))
5749       tools_gl2psPrintf(gl2ps,"font-family=\" << 5385       tools_gl2psPrintf("font-family=\"Helvetica\" font-weight=\"bold\">");
5750     else if(!strcmp(prim->data.text->fontname    5386     else if(!strcmp(prim->data.text->fontname, "Helvetica-Oblique"))
5751       tools_gl2psPrintf(gl2ps,"font-family=\" << 5387       tools_gl2psPrintf("font-family=\"Helvetica\" font-style=\"oblique\">");
5752     else if(!strcmp(prim->data.text->fontname    5388     else if(!strcmp(prim->data.text->fontname, "Helvetica-BoldOblique"))
5753       tools_gl2psPrintf(gl2ps,"font-family=\" << 5389       tools_gl2psPrintf("font-family=\"Helvetica\" font-style=\"oblique\" font-weight=\"bold\">");
5754     else if(!strcmp(prim->data.text->fontname    5390     else if(!strcmp(prim->data.text->fontname, "Courier-Bold"))
5755       tools_gl2psPrintf(gl2ps,"font-family=\" << 5391       tools_gl2psPrintf("font-family=\"Courier\" font-weight=\"bold\">");
5756     else if(!strcmp(prim->data.text->fontname    5392     else if(!strcmp(prim->data.text->fontname, "Courier-Oblique"))
5757       tools_gl2psPrintf(gl2ps,"font-family=\" << 5393       tools_gl2psPrintf("font-family=\"Courier\" font-style=\"oblique\">");
5758     else if(!strcmp(prim->data.text->fontname    5394     else if(!strcmp(prim->data.text->fontname, "Courier-BoldOblique"))
5759       tools_gl2psPrintf(gl2ps,"font-family=\" << 5395       tools_gl2psPrintf("font-family=\"Courier\" font-style=\"oblique\" font-weight=\"bold\">");
5760     else                                         5396     else
5761       tools_gl2psPrintf(gl2ps,"font-family=\" << 5397       tools_gl2psPrintf("font-family=\"%s\">", prim->data.text->fontname);
5762     tools_gl2psPrintf(gl2ps,"%s</text>\n", pr << 5398     tools_gl2psPrintf("%s</text>\n", prim->data.text->str);
5763     break;                                       5399     break;
5764   case TOOLS_GL2PS_SPECIAL :                     5400   case TOOLS_GL2PS_SPECIAL :
5765     /* alignment contains the format for whic    5401     /* alignment contains the format for which the special output text
5766        is intended */                            5402        is intended */
5767     if(prim->data.text->alignment == TOOLS_GL    5403     if(prim->data.text->alignment == TOOLS_GL2PS_SVG)
5768       tools_gl2psPrintf(gl2ps,"%s\n", prim->d << 5404       tools_gl2psPrintf("%s\n", prim->data.text->str);
5769     break;                                       5405     break;
5770   default :                                      5406   default :
5771     break;                                       5407     break;
5772   }                                              5408   }
5773 }                                                5409 }
5774                                                  5410 
5775 inline void tools_gl2psPrintSVGFooter(tools_G << 5411 static void tools_gl2psPrintSVGFooter(void)
5776 {                                                5412 {
5777   tools_gl2psPrintf(gl2ps,"</g>\n");          << 5413   tools_gl2psPrintf("</g>\n");
5778   tools_gl2psPrintf(gl2ps,"</svg>\n");        << 5414   tools_gl2psPrintf("</svg>\n");
5779                                                  5415 
5780   tools_gl2psPrintGzipFooter(gl2ps);          << 5416   tools_gl2psPrintGzipFooter();
5781 }                                                5417 }
5782                                                  5418 
5783 inline void tools_gl2psPrintSVGBeginViewport( << 5419 static void tools_gl2psPrintSVGBeginViewport(tools_GLint viewport[4])
5784 {                                                5420 {
5785   tools_GLint idx;                               5421   tools_GLint idx;
5786   char col[32];                                  5422   char col[32];
5787   tools_GLfloat rgba[4];                         5423   tools_GLfloat rgba[4];
5788   int x = viewport[0], y = viewport[1], w = v    5424   int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3];
5789                                                  5425 
5790   tools_glRenderMode(TOOLS_GL_FEEDBACK);         5426   tools_glRenderMode(TOOLS_GL_FEEDBACK);
5791                                                  5427 
5792   tools_gl2psResetLineProperties(gl2ps);      << 5428   tools_gl2psResetLineProperties();
5793                                                  5429 
5794   if(gl2ps->header){                          << 5430   if(tools_gl2ps_context->header){
5795     tools_gl2psPrintSVGHeader(gl2ps);         << 5431     tools_gl2psPrintSVGHeader();
5796     gl2ps->header = TOOLS_GL_FALSE;           << 5432     tools_gl2ps_context->header = TOOLS_GL_FALSE;
5797   }                                              5433   }
5798                                                  5434 
5799   if(gl2ps->options & TOOLS_GL2PS_DRAW_BACKGR << 5435   if(tools_gl2ps_context->options & TOOLS_GL2PS_DRAW_BACKGROUND){
5800     if(gl2ps->colormode == TOOLS_GL_RGBA || g << 5436     if(tools_gl2ps_context->colormode == TOOLS_GL_RGBA || tools_gl2ps_context->colorsize == 0){
5801       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_    5437       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_VALUE, rgba);
5802     }                                            5438     }
5803     else{                                        5439     else{
5804       tools_glGetIntegerv(TOOLS_GL_INDEX_CLEA    5440       tools_glGetIntegerv(TOOLS_GL_INDEX_CLEAR_VALUE, &idx);
5805       rgba[0] = gl2ps->colormap[idx][0];      << 5441       rgba[0] = tools_gl2ps_context->colormap[idx][0];
5806       rgba[1] = gl2ps->colormap[idx][1];      << 5442       rgba[1] = tools_gl2ps_context->colormap[idx][1];
5807       rgba[2] = gl2ps->colormap[idx][2];      << 5443       rgba[2] = tools_gl2ps_context->colormap[idx][2];
5808       rgba[3] = 1.0F;                            5444       rgba[3] = 1.0F;
5809     }                                            5445     }
5810     tools_gl2psSVGGetColorString(rgba, col);  << 5446     tools_tools_gl2psSVGGetColorString(rgba, col);
5811     tools_gl2psPrintf(gl2ps,"<polygon fill=\" << 5447     tools_gl2psPrintf("<polygon fill=\"%s\" points=\"%d,%d %d,%d %d,%d %d,%d\" ", col,
5812                 x, gl2ps->viewport[3] - y,    << 5448                 x, tools_gl2ps_context->viewport[3] - y,
5813                 x + w, gl2ps->viewport[3] - y << 5449                 x + w, tools_gl2ps_context->viewport[3] - y,
5814                 x + w, gl2ps->viewport[3] - ( << 5450                 x + w, tools_gl2ps_context->viewport[3] - (y + h),
5815                 x, gl2ps->viewport[3] - (y +  << 5451                 x, tools_gl2ps_context->viewport[3] - (y + h));
5816     tools_gl2psPrintf(gl2ps,"shape-rendering= << 5452     tools_gl2psPrintf("shape-rendering=\"crispEdges\"/>\n");
5817   }                                           << 5453   }
5818                                               << 5454 
5819   tools_gl2psPrintf(gl2ps,"<clipPath id=\"cp% << 5455   tools_gl2psPrintf("<clipPath id=\"cp%d%d%d%d\">\n", x, y, w, h);
5820   tools_gl2psPrintf(gl2ps,"  <polygon points= << 5456   tools_gl2psPrintf("  <polygon points=\"%d,%d %d,%d %d,%d %d,%d\"/>\n",
5821               x, gl2ps->viewport[3] - y,      << 5457               x, tools_gl2ps_context->viewport[3] - y,
5822               x + w, gl2ps->viewport[3] - y,  << 5458               x + w, tools_gl2ps_context->viewport[3] - y,
5823               x + w, gl2ps->viewport[3] - (y  << 5459               x + w, tools_gl2ps_context->viewport[3] - (y + h),
5824               x, gl2ps->viewport[3] - (y + h) << 5460               x, tools_gl2ps_context->viewport[3] - (y + h));
5825   tools_gl2psPrintf(gl2ps,"</clipPath>\n");   << 5461   tools_gl2psPrintf("</clipPath>\n");
5826   tools_gl2psPrintf(gl2ps,"<g clip-path=\"url << 5462   tools_gl2psPrintf("<g clip-path=\"url(#cp%d%d%d%d)\">\n", x, y, w, h);
5827 }                                                5463 }
5828                                                  5464 
5829 inline tools_GLint tools_gl2psPrintSVGEndView << 5465 static tools_GLint tools_gl2psPrintSVGEndViewport(void)
5830 {                                                5466 {
5831   tools_GLint res;                               5467   tools_GLint res;
5832                                                  5468 
5833   res = tools_gl2psPrintPrimitives(gl2ps);    << 5469   res = tools_gl2psPrintPrimitives();
5834   tools_gl2psPrintf(gl2ps,"</g>\n");          << 5470   tools_gl2psPrintf("</g>\n");
5835   return res;                                    5471   return res;
5836 }                                                5472 }
5837                                                  5473 
5838 inline void tools_gl2psPrintSVGFinalPrimitive << 5474 static void tools_gl2psPrintSVGFinalPrimitive(void)
5839 {                                                5475 {
5840   /* End any remaining line, if any */           5476   /* End any remaining line, if any */
5841   tools_gl2psEndSVGLine(gl2ps);               << 5477   tools_gl2psEndSVGLine();
5842 }                                                5478 }
5843                                                  5479 
5844 /* definition of the SVG backend */              5480 /* definition of the SVG backend */
5845                                                  5481 
5846 static const tools_GL2PSbackend tools_gl2psSV << 5482 static tools_GL2PSbackend tools_gl2psSVG = {
5847   tools_gl2psPrintSVGHeader,                     5483   tools_gl2psPrintSVGHeader,
5848   tools_gl2psPrintSVGFooter,                     5484   tools_gl2psPrintSVGFooter,
5849   tools_gl2psPrintSVGBeginViewport,              5485   tools_gl2psPrintSVGBeginViewport,
5850   tools_gl2psPrintSVGEndViewport,                5486   tools_gl2psPrintSVGEndViewport,
5851   tools_gl2psPrintSVGPrimitive,                  5487   tools_gl2psPrintSVGPrimitive,
5852   tools_gl2psPrintSVGFinalPrimitive,             5488   tools_gl2psPrintSVGFinalPrimitive,
5853   "svg",                                         5489   "svg",
5854   "Scalable Vector Graphics"                     5490   "Scalable Vector Graphics"
5855 };                                               5491 };
5856                                                  5492 
5857 /********************************************    5493 /*********************************************************************
5858  *                                               5494  *
5859  * PGF routines                                  5495  * PGF routines
5860  *                                               5496  *
5861  ********************************************    5497  *********************************************************************/
5862                                                  5498 
5863 inline void tools_gl2psPrintPGFColor(tools_GL << 5499 static void tools_gl2psPrintPGFColor(tools_GL2PSrgba rgba)
5864 {                                                5500 {
5865   if(!tools_gl2psSameColor(gl2ps->lastrgba, r << 5501   if(!tools_gl2psSameColor(tools_gl2ps_context->lastrgba, rgba)){
5866     tools_gl2psSetLastColor(gl2ps, rgba);     << 5502     tools_gl2psSetLastColor(rgba);
5867     fprintf(gl2ps->stream, "\\color[rgb]{%f,% << 5503     fprintf(tools_gl2ps_context->stream, "\\color[rgb]{%f,%f,%f}\n", rgba[0], rgba[1], rgba[2]);
5868   }                                              5504   }
5869 }                                                5505 }
5870                                                  5506 
5871 inline void tools_gl2psPrintPGFHeader(tools_G << 5507 static void tools_gl2psPrintPGFHeader(void)
5872 {                                                5508 {
5873   time_t now;                                    5509   time_t now;
5874                                                  5510 
5875   time(&now);                                    5511   time(&now);
5876                                                  5512 
5877   fprintf(gl2ps->stream,                      << 5513   fprintf(tools_gl2ps_context->stream,
5878           "%% Title: %s\n"                       5514           "%% Title: %s\n"
5879           "%% Creator: GL2PS %d.%d.%d%s, %s\n    5515           "%% Creator: GL2PS %d.%d.%d%s, %s\n"
5880           "%% For: %s\n"                         5516           "%% For: %s\n"
5881           "%% CreationDate: %s",                 5517           "%% CreationDate: %s",
5882           gl2ps->title, TOOLS_GL2PS_MAJOR_VER << 5518           tools_gl2ps_context->title, TOOLS_GL2PS_MAJOR_VERSION, TOOLS_GL2PS_MINOR_VERSION,
5883           TOOLS_GL2PS_PATCH_VERSION, TOOLS_GL    5519           TOOLS_GL2PS_PATCH_VERSION, TOOLS_GL2PS_EXTRA_VERSION, TOOLS_GL2PS_COPYRIGHT,
5884           gl2ps->producer, ctime(&now));      << 5520           tools_gl2ps_context->producer, ctime(&now));
5885                                                  5521 
5886   fprintf(gl2ps->stream, "\\begin{pgfpicture} << 5522   fprintf(tools_gl2ps_context->stream, "\\begin{pgfpicture}\n");
5887   if(gl2ps->options & TOOLS_GL2PS_DRAW_BACKGR << 5523   if(tools_gl2ps_context->options & TOOLS_GL2PS_DRAW_BACKGROUND){
5888     tools_gl2psPrintPGFColor(gl2ps, gl2ps->bg << 5524     tools_gl2psPrintPGFColor(tools_gl2ps_context->bgcolor);
5889     fprintf(gl2ps->stream,                    << 5525     fprintf(tools_gl2ps_context->stream,
5890             "\\pgfpathrectanglecorners{"         5526             "\\pgfpathrectanglecorners{"
5891             "\\pgfpoint{%dpt}{%dpt}}{\\pgfpoi    5527             "\\pgfpoint{%dpt}{%dpt}}{\\pgfpoint{%dpt}{%dpt}}\n"
5892             "\\pgfusepath{fill}\n",              5528             "\\pgfusepath{fill}\n",
5893             (int)gl2ps->viewport[0], (int)gl2 << 5529             (int)tools_gl2ps_context->viewport[0], (int)tools_gl2ps_context->viewport[1],
5894             (int)gl2ps->viewport[2], (int)gl2 << 5530             (int)tools_gl2ps_context->viewport[2], (int)tools_gl2ps_context->viewport[3]);
5895   }                                              5531   }
5896 }                                                5532 }
5897                                                  5533 
5898 inline void tools_gl2psPrintPGFDash(tools_GL2 << 5534 static void tools_gl2psPrintPGFDash(tools_GLushort pattern, tools_GLint factor)
5899 {                                                5535 {
5900   int i, n, array[10];                           5536   int i, n, array[10];
5901                                                  5537 
5902   if(pattern == gl2ps->lastpattern && factor  << 5538   if(pattern == tools_gl2ps_context->lastpattern && factor == tools_gl2ps_context->lastfactor)
5903     return;                                      5539     return;
5904                                                  5540 
5905   gl2ps->lastpattern = pattern;               << 5541   tools_gl2ps_context->lastpattern = pattern;
5906   gl2ps->lastfactor = factor;                 << 5542   tools_gl2ps_context->lastfactor = factor;
5907                                                  5543 
5908   if(!pattern || !factor){                       5544   if(!pattern || !factor){
5909     /* solid line */                             5545     /* solid line */
5910     fprintf(gl2ps->stream, "\\pgfsetdash{}{0p << 5546     fprintf(tools_gl2ps_context->stream, "\\pgfsetdash{}{0pt}\n");
5911   }                                              5547   }
5912   else{                                          5548   else{
5913     tools_gl2psParseStipplePattern(pattern, f    5549     tools_gl2psParseStipplePattern(pattern, factor, &n, array);
5914     fprintf(gl2ps->stream, "\\pgfsetdash{");  << 5550     fprintf(tools_gl2ps_context->stream, "\\pgfsetdash{");
5915     for(i = 0; i < n; i++) fprintf(gl2ps->str << 5551     for(i = 0; i < n; i++) fprintf(tools_gl2ps_context->stream, "{%dpt}", array[i]);
5916     fprintf(gl2ps->stream, "}{0pt}\n");       << 5552     fprintf(tools_gl2ps_context->stream, "}{0pt}\n");
5917   }                                              5553   }
5918 }                                                5554 }
5919                                                  5555 
5920 inline const char *tools_gl2psPGFTextAlignmen << 5556 static const char *tools_tools_gl2psPGFTextAlignment(int align)
5921 {                                                5557 {
5922   switch(align){                                 5558   switch(align){
5923   case TOOLS_GL2PS_TEXT_C  : return "center";    5559   case TOOLS_GL2PS_TEXT_C  : return "center";
5924   case TOOLS_GL2PS_TEXT_CL : return "west";      5560   case TOOLS_GL2PS_TEXT_CL : return "west";
5925   case TOOLS_GL2PS_TEXT_CR : return "east";      5561   case TOOLS_GL2PS_TEXT_CR : return "east";
5926   case TOOLS_GL2PS_TEXT_B  : return "south";     5562   case TOOLS_GL2PS_TEXT_B  : return "south";
5927   case TOOLS_GL2PS_TEXT_BR : return "south ea    5563   case TOOLS_GL2PS_TEXT_BR : return "south east";
5928   case TOOLS_GL2PS_TEXT_T  : return "north";     5564   case TOOLS_GL2PS_TEXT_T  : return "north";
5929   case TOOLS_GL2PS_TEXT_TL : return "north we    5565   case TOOLS_GL2PS_TEXT_TL : return "north west";
5930   case TOOLS_GL2PS_TEXT_TR : return "north ea    5566   case TOOLS_GL2PS_TEXT_TR : return "north east";
5931   case TOOLS_GL2PS_TEXT_BL :                     5567   case TOOLS_GL2PS_TEXT_BL :
5932   default            : return "south west";      5568   default            : return "south west";
5933   }                                              5569   }
5934 }                                                5570 }
5935                                                  5571 
5936 inline void tools_gl2psPrintPGFPrimitive(tool << 5572 static void tools_gl2psPrintPGFPrimitive(void *data)
5937 {                                                5573 {
5938   tools_GL2PSprimitive *prim;                    5574   tools_GL2PSprimitive *prim;
5939                                                  5575 
5940   prim = *(tools_GL2PSprimitive**)data;          5576   prim = *(tools_GL2PSprimitive**)data;
5941                                                  5577 
5942   switch(prim->type){                            5578   switch(prim->type){
5943   case TOOLS_GL2PS_POINT :                       5579   case TOOLS_GL2PS_POINT :
5944     /* Points in openGL are rectangular */       5580     /* Points in openGL are rectangular */
5945     tools_gl2psPrintPGFColor(gl2ps, prim->ver << 5581     tools_gl2psPrintPGFColor(prim->verts[0].rgba);
5946     fprintf(gl2ps->stream,                    << 5582     fprintf(tools_gl2ps_context->stream,
5947             "\\pgfpathrectangle{\\pgfpoint{%f    5583             "\\pgfpathrectangle{\\pgfpoint{%fpt}{%fpt}}"
5948             "{\\pgfpoint{%fpt}{%fpt}}\n\\pgfu    5584             "{\\pgfpoint{%fpt}{%fpt}}\n\\pgfusepath{fill}\n",
5949             prim->verts[0].xyz[0]-0.5*prim->w    5585             prim->verts[0].xyz[0]-0.5*prim->width,
5950             prim->verts[0].xyz[1]-0.5*prim->w    5586             prim->verts[0].xyz[1]-0.5*prim->width,
5951             prim->width,prim->width);            5587             prim->width,prim->width);
5952     break;                                       5588     break;
5953   case TOOLS_GL2PS_LINE :                        5589   case TOOLS_GL2PS_LINE :
5954     tools_gl2psPrintPGFColor(gl2ps, prim->ver << 5590     tools_gl2psPrintPGFColor(prim->verts[0].rgba);
5955     if(gl2ps->lastlinewidth != prim->width){  << 5591     if(tools_gl2ps_context->lastlinewidth != prim->width){
5956       gl2ps->lastlinewidth = prim->width;     << 5592       tools_gl2ps_context->lastlinewidth = prim->width;
5957       fprintf(gl2ps->stream, "\\pgfsetlinewid << 5593       fprintf(tools_gl2ps_context->stream, "\\pgfsetlinewidth{%fpt}\n", tools_gl2ps_context->lastlinewidth);
5958     }                                            5594     }
5959     if(gl2ps->lastlinecap != prim->linecap){  << 5595     if(tools_gl2ps_context->lastlinecap != prim->linecap){
5960       gl2ps->lastlinecap = prim->linecap;     << 5596       tools_gl2ps_context->lastlinecap = prim->linecap;
5961       switch (prim->linecap){                    5597       switch (prim->linecap){
5962       case TOOLS_GL2PS_LINE_CAP_BUTT:            5598       case TOOLS_GL2PS_LINE_CAP_BUTT:
5963         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5599         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "buttcap");
5964         break;                                   5600         break;
5965       case TOOLS_GL2PS_LINE_CAP_ROUND:           5601       case TOOLS_GL2PS_LINE_CAP_ROUND:
5966         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5602         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "roundcap");
5967         break;                                   5603         break;
5968       case TOOLS_GL2PS_LINE_CAP_SQUARE:          5604       case TOOLS_GL2PS_LINE_CAP_SQUARE:
5969         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5605         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "rectcap");
5970         break;                                   5606         break;
5971       }                                          5607       }
5972     }                                            5608     }
5973     if(gl2ps->lastlinejoin != prim->linejoin) << 5609     if(tools_gl2ps_context->lastlinejoin != prim->linejoin){
5974       gl2ps->lastlinejoin = prim->linejoin;   << 5610       tools_gl2ps_context->lastlinejoin = prim->linejoin;
5975       switch (prim->linejoin){                   5611       switch (prim->linejoin){
5976       case TOOLS_GL2PS_LINE_JOIN_MITER:          5612       case TOOLS_GL2PS_LINE_JOIN_MITER:
5977         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5613         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "miterjoin");
5978         break;                                   5614         break;
5979       case TOOLS_GL2PS_LINE_JOIN_ROUND:          5615       case TOOLS_GL2PS_LINE_JOIN_ROUND:
5980         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5616         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "roundjoin");
5981         break;                                   5617         break;
5982       case TOOLS_GL2PS_LINE_JOIN_BEVEL:          5618       case TOOLS_GL2PS_LINE_JOIN_BEVEL:
5983         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5619         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "beveljoin");
5984         break;                                   5620         break;
5985       }                                          5621       }
5986     }                                            5622     }
5987     tools_gl2psPrintPGFDash(gl2ps, prim->patt << 5623     tools_gl2psPrintPGFDash(prim->pattern, prim->factor);
5988     fprintf(gl2ps->stream,                    << 5624     fprintf(tools_gl2ps_context->stream,
5989             "\\pgfpathmoveto{\\pgfpoint{%fpt}    5625             "\\pgfpathmoveto{\\pgfpoint{%fpt}{%fpt}}\n"
5990             "\\pgflineto{\\pgfpoint{%fpt}{%fp    5626             "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n"
5991             "\\pgfusepath{stroke}\n",            5627             "\\pgfusepath{stroke}\n",
5992             prim->verts[1].xyz[0], prim->vert    5628             prim->verts[1].xyz[0], prim->verts[1].xyz[1],
5993             prim->verts[0].xyz[0], prim->vert    5629             prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
5994     break;                                       5630     break;
5995   case TOOLS_GL2PS_TRIANGLE :                    5631   case TOOLS_GL2PS_TRIANGLE :
5996     if(gl2ps->lastlinewidth != 0){            << 5632     if(tools_gl2ps_context->lastlinewidth != 0){
5997       gl2ps->lastlinewidth = 0;               << 5633       tools_gl2ps_context->lastlinewidth = 0;
5998       fprintf(gl2ps->stream, "\\pgfsetlinewid << 5634       fprintf(tools_gl2ps_context->stream, "\\pgfsetlinewidth{0.01pt}\n");
5999     }                                            5635     }
6000     if(gl2ps->lastlinecap != prim->linecap){  << 5636     if(tools_gl2ps_context->lastlinecap != prim->linecap){
6001       gl2ps->lastlinecap = prim->linecap;     << 5637       tools_gl2ps_context->lastlinecap = prim->linecap;
6002       switch (prim->linecap){                    5638       switch (prim->linecap){
6003       case TOOLS_GL2PS_LINE_CAP_BUTT:            5639       case TOOLS_GL2PS_LINE_CAP_BUTT:
6004         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5640         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "buttcap");
6005         break;                                   5641         break;
6006       case TOOLS_GL2PS_LINE_CAP_ROUND:           5642       case TOOLS_GL2PS_LINE_CAP_ROUND:
6007         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5643         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "roundcap");
6008         break;                                   5644         break;
6009       case TOOLS_GL2PS_LINE_CAP_SQUARE:          5645       case TOOLS_GL2PS_LINE_CAP_SQUARE:
6010         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5646         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "rectcap");
6011         break;                                   5647         break;
6012       }                                          5648       }
6013     }                                            5649     }
6014     if(gl2ps->lastlinejoin != prim->linejoin) << 5650     if(tools_gl2ps_context->lastlinejoin != prim->linejoin){
6015       gl2ps->lastlinejoin = prim->linejoin;   << 5651       tools_gl2ps_context->lastlinejoin = prim->linejoin;
6016       switch (prim->linejoin){                   5652       switch (prim->linejoin){
6017       case TOOLS_GL2PS_LINE_JOIN_MITER:          5653       case TOOLS_GL2PS_LINE_JOIN_MITER:
6018         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5654         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "miterjoin");
6019         break;                                   5655         break;
6020       case TOOLS_GL2PS_LINE_JOIN_ROUND:          5656       case TOOLS_GL2PS_LINE_JOIN_ROUND:
6021         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5657         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "roundjoin");
6022         break;                                   5658         break;
6023       case TOOLS_GL2PS_LINE_JOIN_BEVEL:          5659       case TOOLS_GL2PS_LINE_JOIN_BEVEL:
6024         fprintf(gl2ps->stream, "\\pgfset%s\n" << 5660         fprintf(tools_gl2ps_context->stream, "\\pgfset%s\n", "beveljoin");
6025         break;                                   5661         break;
6026       }                                          5662       }
6027     }                                            5663     }
6028     tools_gl2psPrintPGFColor(gl2ps, prim->ver << 5664     tools_gl2psPrintPGFColor(prim->verts[0].rgba);
6029     fprintf(gl2ps->stream,                    << 5665     fprintf(tools_gl2ps_context->stream,
6030             "\\pgfpathmoveto{\\pgfpoint{%fpt}    5666             "\\pgfpathmoveto{\\pgfpoint{%fpt}{%fpt}}\n"
6031             "\\pgflineto{\\pgfpoint{%fpt}{%fp    5667             "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n"
6032             "\\pgflineto{\\pgfpoint{%fpt}{%fp    5668             "\\pgflineto{\\pgfpoint{%fpt}{%fpt}}\n"
6033             "\\pgfpathclose\n"                   5669             "\\pgfpathclose\n"
6034             "\\pgfusepath{fill,stroke}\n",       5670             "\\pgfusepath{fill,stroke}\n",
6035             prim->verts[2].xyz[0], prim->vert    5671             prim->verts[2].xyz[0], prim->verts[2].xyz[1],
6036             prim->verts[1].xyz[0], prim->vert    5672             prim->verts[1].xyz[0], prim->verts[1].xyz[1],
6037             prim->verts[0].xyz[0], prim->vert    5673             prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
6038     break;                                       5674     break;
6039   case TOOLS_GL2PS_TEXT :                        5675   case TOOLS_GL2PS_TEXT :
6040     fprintf(gl2ps->stream, "{\n\\pgftransform << 5676     fprintf(tools_gl2ps_context->stream, "{\n\\pgftransformshift{\\pgfpoint{%fpt}{%fpt}}\n",
6041             prim->verts[0].xyz[0], prim->vert    5677             prim->verts[0].xyz[0], prim->verts[0].xyz[1]);
6042                                                  5678 
6043     if(prim->data.text->angle)                   5679     if(prim->data.text->angle)
6044       fprintf(gl2ps->stream, "\\pgftransformr << 5680       fprintf(tools_gl2ps_context->stream, "\\pgftransformrotate{%f}{", prim->data.text->angle);
6045                                                  5681 
6046     fprintf(gl2ps->stream, "\\pgfnode{rectang << 5682     fprintf(tools_gl2ps_context->stream, "\\pgfnode{rectangle}{%s}{\\fontsize{%d}{0}\\selectfont",
6047             tools_gl2psPGFTextAlignment(prim- << 5683             tools_tools_gl2psPGFTextAlignment(prim->data.text->alignment),
6048             prim->data.text->fontsize);          5684             prim->data.text->fontsize);
6049                                                  5685 
6050     fprintf(gl2ps->stream, "\\textcolor[rgb]{ << 5686     fprintf(tools_gl2ps_context->stream, "\\textcolor[rgb]{%g,%g,%g}{{%s}}",
6051             prim->verts[0].rgba[0], prim->ver    5687             prim->verts[0].rgba[0], prim->verts[0].rgba[1],
6052             prim->verts[0].rgba[2], prim->dat    5688             prim->verts[0].rgba[2], prim->data.text->str);
6053                                                  5689 
6054     fprintf(gl2ps->stream, "}{}{\\pgfusepath{ << 5690     fprintf(tools_gl2ps_context->stream, "}{}{\\pgfusepath{discard}}}");
6055                                                  5691 
6056     if(prim->data.text->angle)                   5692     if(prim->data.text->angle)
6057        fprintf(gl2ps->stream, "}");           << 5693        fprintf(tools_gl2ps_context->stream, "}");
6058                                                  5694 
6059     fprintf(gl2ps->stream, "\n}\n");          << 5695     fprintf(tools_gl2ps_context->stream, "\n");
6060     break;                                       5696     break;
6061   case TOOLS_GL2PS_SPECIAL :                     5697   case TOOLS_GL2PS_SPECIAL :
6062     /* alignment contains the format for whic    5698     /* alignment contains the format for which the special output text
6063        is intended */                            5699        is intended */
6064     if (prim->data.text->alignment == TOOLS_G    5700     if (prim->data.text->alignment == TOOLS_GL2PS_PGF)
6065       fprintf(gl2ps->stream, "%s\n", prim->da << 5701       fprintf(tools_gl2ps_context->stream, "%s\n", prim->data.text->str);
6066     break;                                       5702     break;
6067   default :                                      5703   default :
6068     break;                                       5704     break;
6069   }                                              5705   }
6070 }                                                5706 }
6071                                                  5707 
6072 inline void tools_gl2psPrintPGFFooter(tools_G << 5708 static void tools_gl2psPrintPGFFooter(void)
6073 {                                                5709 {
6074   fprintf(gl2ps->stream, "\\end{pgfpicture}\n << 5710   fprintf(tools_gl2ps_context->stream, "\\end{pgfpicture}\n");
6075 }                                                5711 }
6076                                                  5712 
6077 inline void tools_gl2psPrintPGFBeginViewport( << 5713 static void tools_gl2psPrintPGFBeginViewport(tools_GLint viewport[4])
6078 {                                                5714 {
6079   tools_GLint idx;                               5715   tools_GLint idx;
6080   tools_GLfloat rgba[4];                         5716   tools_GLfloat rgba[4];
6081   int x = viewport[0], y = viewport[1], w = v    5717   int x = viewport[0], y = viewport[1], w = viewport[2], h = viewport[3];
6082                                                  5718 
6083   tools_glRenderMode(TOOLS_GL_FEEDBACK);         5719   tools_glRenderMode(TOOLS_GL_FEEDBACK);
6084                                                  5720 
6085   tools_gl2psResetLineProperties(gl2ps);      << 5721   tools_gl2psResetLineProperties();
6086                                                  5722 
6087   if(gl2ps->header){                          << 5723   if(tools_gl2ps_context->header){
6088     tools_gl2psPrintPGFHeader(gl2ps);         << 5724     tools_gl2psPrintPGFHeader();
6089     gl2ps->header = TOOLS_GL_FALSE;           << 5725     tools_gl2ps_context->header = TOOLS_GL_FALSE;
6090   }                                              5726   }
6091                                                  5727 
6092   fprintf(gl2ps->stream, "\\begin{pgfscope}\n << 5728   fprintf(tools_gl2ps_context->stream, "\\begin{pgfscope}\n");
6093   if(gl2ps->options & TOOLS_GL2PS_DRAW_BACKGR << 5729   if(tools_gl2ps_context->options & TOOLS_GL2PS_DRAW_BACKGROUND){
6094     if(gl2ps->colormode == TOOLS_GL_RGBA || g << 5730     if(tools_gl2ps_context->colormode == TOOLS_GL_RGBA || tools_gl2ps_context->colorsize == 0){
6095       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_    5731       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_VALUE, rgba);
6096     }                                            5732     }
6097     else{                                        5733     else{
6098       tools_glGetIntegerv(TOOLS_GL_INDEX_CLEA    5734       tools_glGetIntegerv(TOOLS_GL_INDEX_CLEAR_VALUE, &idx);
6099       rgba[0] = gl2ps->colormap[idx][0];      << 5735       rgba[0] = tools_gl2ps_context->colormap[idx][0];
6100       rgba[1] = gl2ps->colormap[idx][1];      << 5736       rgba[1] = tools_gl2ps_context->colormap[idx][1];
6101       rgba[2] = gl2ps->colormap[idx][2];      << 5737       rgba[2] = tools_gl2ps_context->colormap[idx][2];
6102       rgba[3] = 1.0F;                            5738       rgba[3] = 1.0F;
6103     }                                            5739     }
6104     tools_gl2psPrintPGFColor(gl2ps, rgba);    << 5740     tools_gl2psPrintPGFColor(rgba);
6105     fprintf(gl2ps->stream,                    << 5741     fprintf(tools_gl2ps_context->stream,
6106             "\\pgfpathrectangle{\\pgfpoint{%d    5742             "\\pgfpathrectangle{\\pgfpoint{%dpt}{%dpt}}"
6107             "{\\pgfpoint{%dpt}{%dpt}}\n"         5743             "{\\pgfpoint{%dpt}{%dpt}}\n"
6108             "\\pgfusepath{fill}\n",              5744             "\\pgfusepath{fill}\n",
6109             x, y, w, h);                         5745             x, y, w, h);
6110   }                                              5746   }
6111                                                  5747 
6112   fprintf(gl2ps->stream,                      << 5748   fprintf(tools_gl2ps_context->stream,
6113           "\\pgfpathrectangle{\\pgfpoint{%dpt    5749           "\\pgfpathrectangle{\\pgfpoint{%dpt}{%dpt}}"
6114           "{\\pgfpoint{%dpt}{%dpt}}\n"           5750           "{\\pgfpoint{%dpt}{%dpt}}\n"
6115           "\\pgfusepath{clip}\n",                5751           "\\pgfusepath{clip}\n",
6116           x, y, w, h);                           5752           x, y, w, h);
6117 }                                                5753 }
6118                                                  5754 
6119 inline tools_GLint tools_gl2psPrintPGFEndView << 5755 static tools_GLint tools_gl2psPrintPGFEndViewport(void)
6120 {                                                5756 {
6121   tools_GLint res;                               5757   tools_GLint res;
6122   res = tools_gl2psPrintPrimitives(gl2ps);    << 5758   res = tools_gl2psPrintPrimitives();
6123   fprintf(gl2ps->stream, "\\end{pgfscope}\n") << 5759   fprintf(tools_gl2ps_context->stream, "\\end{pgfscope}\n");
6124   return res;                                    5760   return res;
6125 }                                                5761 }
6126                                                  5762 
6127 inline void tools_gl2psPrintPGFFinalPrimitive << 5763 static void tools_gl2psPrintPGFFinalPrimitive(void)
6128 {                                                5764 {
6129 }                                                5765 }
6130                                                  5766 
6131 /* definition of the PGF backend */              5767 /* definition of the PGF backend */
6132                                                  5768 
6133 static const tools_GL2PSbackend tools_gl2psPG << 5769 static tools_GL2PSbackend tools_gl2psPGF = {
6134   tools_gl2psPrintPGFHeader,                     5770   tools_gl2psPrintPGFHeader,
6135   tools_gl2psPrintPGFFooter,                     5771   tools_gl2psPrintPGFFooter,
6136   tools_gl2psPrintPGFBeginViewport,              5772   tools_gl2psPrintPGFBeginViewport,
6137   tools_gl2psPrintPGFEndViewport,                5773   tools_gl2psPrintPGFEndViewport,
6138   tools_gl2psPrintPGFPrimitive,                  5774   tools_gl2psPrintPGFPrimitive,
6139   tools_gl2psPrintPGFFinalPrimitive,             5775   tools_gl2psPrintPGFFinalPrimitive,
6140   "tex",                                         5776   "tex",
6141   "PGF Latex Graphics"                           5777   "PGF Latex Graphics"
6142 };                                               5778 };
6143                                                  5779 
6144 /********************************************    5780 /*********************************************************************
6145  *                                               5781  *
6146  * General primitive printing routine            5782  * General primitive printing routine
6147  *                                               5783  *
6148  ********************************************    5784  *********************************************************************/
6149                                                  5785 
6150 /* Warning: the ordering of the backends must    5786 /* Warning: the ordering of the backends must match the format
6151    #defines in gl2ps.h */                        5787    #defines in gl2ps.h */
6152                                                  5788 
6153 static const tools_GL2PSbackend *tools_gl2psb << 5789 static tools_GL2PSbackend *tools_gl2psbackends[] = {
6154   &tools_gl2psPS,  /* 0 */                       5790   &tools_gl2psPS,  /* 0 */
6155   &tools_gl2psEPS, /* 1 */                       5791   &tools_gl2psEPS, /* 1 */
6156   &tools_gl2psTEX, /* 2 */                       5792   &tools_gl2psTEX, /* 2 */
6157   &tools_gl2psPDF, /* 3 */                       5793   &tools_gl2psPDF, /* 3 */
6158   &tools_gl2psSVG, /* 4 */                       5794   &tools_gl2psSVG, /* 4 */
6159   &tools_gl2psPGF  /* 5 */                       5795   &tools_gl2psPGF  /* 5 */
6160 };                                               5796 };
6161                                                  5797 
6162 inline void tools_gl2psComputeTightBoundingBo << 5798 static void tools_gl2psComputeTightBoundingBox(void *data)
6163 {                                                5799 {
6164   tools_GL2PSprimitive *prim;                    5800   tools_GL2PSprimitive *prim;
6165   int i;                                         5801   int i;
6166                                                  5802 
6167   prim = *(tools_GL2PSprimitive**)data;          5803   prim = *(tools_GL2PSprimitive**)data;
6168                                                  5804 
6169   for(i = 0; i < prim->numverts; i++){           5805   for(i = 0; i < prim->numverts; i++){
6170     if(prim->verts[i].xyz[0] < gl2ps->viewpor << 5806     if(prim->verts[i].xyz[0] < tools_gl2ps_context->viewport[0])
6171       gl2ps->viewport[0] = (tools_GLint)prim- << 5807       tools_gl2ps_context->viewport[0] = (tools_GLint)prim->verts[i].xyz[0];
6172     if(prim->verts[i].xyz[0] > gl2ps->viewpor << 5808     if(prim->verts[i].xyz[0] > tools_gl2ps_context->viewport[2])
6173       gl2ps->viewport[2] = (tools_GLint)(prim << 5809       tools_gl2ps_context->viewport[2] = (tools_GLint)(prim->verts[i].xyz[0] + 0.5F);
6174     if(prim->verts[i].xyz[1] < gl2ps->viewpor << 5810     if(prim->verts[i].xyz[1] < tools_gl2ps_context->viewport[1])
6175       gl2ps->viewport[1] = (tools_GLint)prim- << 5811       tools_gl2ps_context->viewport[1] = (tools_GLint)prim->verts[i].xyz[1];
6176     if(prim->verts[i].xyz[1] > gl2ps->viewpor << 5812     if(prim->verts[i].xyz[1] > tools_gl2ps_context->viewport[3])
6177       gl2ps->viewport[3] = (tools_GLint)(prim << 5813       tools_gl2ps_context->viewport[3] = (tools_GLint)(prim->verts[i].xyz[1] + 0.5F);
6178   }                                              5814   }
6179 }                                                5815 }
6180                                                  5816 
6181 inline tools_GLint tools_gl2psPrintPrimitives << 5817 static tools_GLint tools_gl2psPrintPrimitives(void)
6182 {                                                5818 {
6183   tools_GL2PSbsptree *root;                      5819   tools_GL2PSbsptree *root;
6184   tools_GL2PSxyz eye = {0.0F, 0.0F, 100.0F *     5820   tools_GL2PSxyz eye = {0.0F, 0.0F, 100.0F * TOOLS_GL2PS_ZSCALE};
6185   tools_GLint used = 0;                          5821   tools_GLint used = 0;
6186                                                  5822 
6187   if ((gl2ps->options & TOOLS_GL2PS_NO_OPENGL << 5823   if ((tools_gl2ps_context->options & TOOLS_GL2PS_NO_OPENGL_CONTEXT) == TOOLS_GL2PS_NONE) {
6188     used = tools_glRenderMode(TOOLS_GL_RENDER    5824     used = tools_glRenderMode(TOOLS_GL_RENDER);
6189   }                                              5825   }
6190                                                  5826 
6191   if(used < 0){                                  5827   if(used < 0){
6192     tools_gl2psMsg(TOOLS_GL2PS_INFO, "OpenGL     5828     tools_gl2psMsg(TOOLS_GL2PS_INFO, "OpenGL feedback buffer overflow");
6193     return TOOLS_GL2PS_OVERFLOW;                 5829     return TOOLS_GL2PS_OVERFLOW;
6194   }                                              5830   }
6195                                                  5831 
6196   if(used > 0)                                   5832   if(used > 0)
6197     tools_gl2psParseFeedbackBuffer(gl2ps, use << 5833     tools_gl2psParseFeedbackBuffer(used);
6198                                                  5834 
6199   tools_gl2psRescaleAndOffset(gl2ps);         << 5835   tools_gl2psRescaleAndOffset();
6200                                                  5836 
6201   if(gl2ps->header){                          << 5837   if(tools_gl2ps_context->header){
6202     if(tools_gl2psListNbr(gl2ps->primitives)  << 5838     if(tools_gl2psListNbr(tools_gl2ps_context->primitives) &&
6203        (gl2ps->options & TOOLS_GL2PS_TIGHT_BO << 5839        (tools_gl2ps_context->options & TOOLS_GL2PS_TIGHT_BOUNDING_BOX)){
6204       gl2ps->viewport[0] = gl2ps->viewport[1] << 5840       tools_gl2ps_context->viewport[0] = tools_gl2ps_context->viewport[1] = 100000;
6205       gl2ps->viewport[2] = gl2ps->viewport[3] << 5841       tools_gl2ps_context->viewport[2] = tools_gl2ps_context->viewport[3] = -100000;
6206       tools_gl2psListActionContext(gl2ps, gl2 << 5842       tools_gl2psListAction(tools_gl2ps_context->primitives, tools_gl2psComputeTightBoundingBox);
6207     }                                            5843     }
6208     (tools_gl2psbackends[gl2ps->format]->prin << 5844     (tools_gl2psbackends[tools_gl2ps_context->format]->printHeader)();
6209     gl2ps->header = TOOLS_GL_FALSE;           << 5845     tools_gl2ps_context->header = TOOLS_GL_FALSE;
6210   }                                              5846   }
6211                                                  5847 
6212   if(!tools_gl2psListNbr(gl2ps->primitives)){ << 5848   if(!tools_gl2psListNbr(tools_gl2ps_context->primitives)){
6213     /* empty feedback buffer and/or nothing e    5849     /* empty feedback buffer and/or nothing else to print */
6214     return TOOLS_GL2PS_NO_FEEDBACK;              5850     return TOOLS_GL2PS_NO_FEEDBACK;
6215   }                                              5851   }
6216                                                  5852 
6217   switch(gl2ps->sort){                        << 5853   switch(tools_gl2ps_context->sort){
6218   case TOOLS_GL2PS_NO_SORT :                     5854   case TOOLS_GL2PS_NO_SORT :
6219     tools_gl2psListActionContext(gl2ps, gl2ps << 5855     tools_gl2psListAction(tools_gl2ps_context->primitives, tools_gl2psbackends[tools_gl2ps_context->format]->printPrimitive);
6220     tools_gl2psListAction(gl2ps->primitives,  << 5856     tools_gl2psListAction(tools_gl2ps_context->primitives, tools_tools_gl2psFreePrimitive);
6221     /* reset the primitive list, waiting for     5857     /* reset the primitive list, waiting for the next viewport */
6222     tools_gl2psListReset(gl2ps->primitives);  << 5858     tools_gl2psListReset(tools_gl2ps_context->primitives);
6223     break;                                       5859     break;
6224   case TOOLS_GL2PS_SIMPLE_SORT :                 5860   case TOOLS_GL2PS_SIMPLE_SORT :
6225     tools_gl2psListAssignSortIds(gl2ps->primi << 5861     tools_gl2psListSort(tools_gl2ps_context->primitives, tools_gl2psCompareDepth);
6226     tools_gl2psListSort(gl2ps, gl2ps->primiti << 5862     if(tools_gl2ps_context->options & TOOLS_GL2PS_OCCLUSION_CULL){
6227     if(gl2ps->options & TOOLS_GL2PS_OCCLUSION << 5863       tools_tools_gl2psListActionInverse(tools_gl2ps_context->primitives, tools_tools_gl2psAddInImageTree);
6228       tools_gl2psListActionInverseContext(gl2 << 5864       tools_tools_gl2psFreeBspImageTree(&tools_gl2ps_context->imagetree);
6229       tools_gl2psFreeBspImageTree(&gl2ps->ima << 
6230     }                                            5865     }
6231     tools_gl2psListActionContext(gl2ps, gl2ps << 5866     tools_gl2psListAction(tools_gl2ps_context->primitives, tools_gl2psbackends[tools_gl2ps_context->format]->printPrimitive);
6232     tools_gl2psListAction(gl2ps->primitives,  << 5867     tools_gl2psListAction(tools_gl2ps_context->primitives, tools_tools_gl2psFreePrimitive);
6233     /* reset the primitive list, waiting for     5868     /* reset the primitive list, waiting for the next viewport */
6234     tools_gl2psListReset(gl2ps->primitives);  << 5869     tools_gl2psListReset(tools_gl2ps_context->primitives);
6235     break;                                       5870     break;
6236   case TOOLS_GL2PS_BSP_SORT :                    5871   case TOOLS_GL2PS_BSP_SORT :
6237     root = (tools_GL2PSbsptree*)tools_gl2psMa    5872     root = (tools_GL2PSbsptree*)tools_gl2psMalloc(sizeof(tools_GL2PSbsptree));
6238     tools_gl2psBuildBspTree(gl2ps, root, gl2p << 5873     tools_gl2psBuildBspTree(root, tools_gl2ps_context->primitives);
6239     if(TOOLS_GL_TRUE == gl2ps->boundary) tool << 5874     if(TOOLS_GL_TRUE == tools_gl2ps_context->boundary) tools_gl2psBuildPolygonBoundary(root);
6240     if(gl2ps->options & TOOLS_GL2PS_OCCLUSION << 5875     if(tools_gl2ps_context->options & TOOLS_GL2PS_OCCLUSION_CULL){
6241       tools_gl2psTraverseBspTree(gl2ps, root, << 5876       tools_gl2psTraverseBspTree(root, eye, -TOOLS_GL2PS_EPSILON, tools_gl2psLess,
6242                            tools_gl2psAddInIm << 5877                            tools_tools_gl2psAddInImageTree, 1);
6243       tools_gl2psFreeBspImageTree(&gl2ps->ima << 5878       tools_tools_gl2psFreeBspImageTree(&tools_gl2ps_context->imagetree);
6244     }                                         << 5879     }
6245     tools_gl2psTraverseBspTree(gl2ps, root, e << 5880     tools_gl2psTraverseBspTree(root, eye, TOOLS_GL2PS_EPSILON, tools_gl2psGreater,
6246                          tools_gl2psbackends[ << 5881                          tools_gl2psbackends[tools_gl2ps_context->format]->printPrimitive, 0);
6247     tools_gl2psFreeBspTree(&root);            << 5882     tools_tools_gl2psFreeBspTree(&root);
6248     /* reallocate the primitive list (it's be    5883     /* reallocate the primitive list (it's been deleted by
6249        tools_gl2psBuildBspTree) in case there    5884        tools_gl2psBuildBspTree) in case there is another viewport */
6250     gl2ps->primitives = tools_gl2psListCreate << 5885     tools_gl2ps_context->primitives = tools_gl2psListCreate(500, 500, sizeof(tools_GL2PSprimitive*));
6251     break;                                       5886     break;
6252   }                                              5887   }
6253   tools_gl2psbackends[gl2ps->format]->printFi << 5888   tools_gl2psbackends[tools_gl2ps_context->format]->printFinalPrimitive();
6254                                                  5889 
6255   return TOOLS_GL2PS_SUCCESS;                    5890   return TOOLS_GL2PS_SUCCESS;
6256 }                                                5891 }
6257                                                  5892 
6258 inline tools_GLboolean tools_gl2psCheckOption << 5893 static tools_GLboolean tools_gl2psCheckOptions(tools_GLint options, tools_GLint colormode)
6259 {                                                5894 {
6260   if (options & TOOLS_GL2PS_NO_OPENGL_CONTEXT    5895   if (options & TOOLS_GL2PS_NO_OPENGL_CONTEXT) {
6261     if (options & TOOLS_GL2PS_DRAW_BACKGROUND    5896     if (options & TOOLS_GL2PS_DRAW_BACKGROUND) {
6262       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Opti    5897       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Options TOOLS_GL2PS_NO_OPENGL_CONTEXT and "
6263                             "TOOLS_GL2PS_DRAW    5898                             "TOOLS_GL2PS_DRAW_BACKGROUND are incompatible.");
6264       return TOOLS_GL_FALSE;                     5899       return TOOLS_GL_FALSE;
6265     }                                            5900     }
6266     if (options & TOOLS_GL2PS_USE_CURRENT_VIE    5901     if (options & TOOLS_GL2PS_USE_CURRENT_VIEWPORT) {
6267       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Opti    5902       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Options TOOLS_GL2PS_NO_OPENGL_CONTEXT and "
6268                             "TOOLS_GL2PS_USE_    5903                             "TOOLS_GL2PS_USE_CURRENT_VIEWPORT are incompatible.");
6269       return TOOLS_GL_FALSE;                     5904       return TOOLS_GL_FALSE;
6270     }                                            5905     }
6271     if ((options & TOOLS_GL2PS_NO_BLENDING) =    5906     if ((options & TOOLS_GL2PS_NO_BLENDING) == TOOLS_GL2PS_NONE) {
6272       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Opti    5907       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Option TOOLS_GL2PS_NO_OPENGL_CONTEXT requires "
6273                             "option TOOLS_GL2    5908                             "option TOOLS_GL2PS_NO_BLENDING.");
6274       return TOOLS_GL_FALSE;                     5909       return TOOLS_GL_FALSE;
6275     }                                            5910     }
6276     if (colormode != TOOLS_GL_RGBA) {            5911     if (colormode != TOOLS_GL_RGBA) {
6277       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Opti    5912       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Option TOOLS_GL2PS_NO_OPENGL_CONTEXT requires colormode "
6278                             "to be TOOLS_GL_R    5913                             "to be TOOLS_GL_RGBA.");
6279       return TOOLS_GL_FALSE;                     5914       return TOOLS_GL_FALSE;
6280     }                                            5915     }
6281   }                                              5916   }
6282                                                  5917 
6283   return TOOLS_GL_TRUE;                          5918   return TOOLS_GL_TRUE;
6284 }                                                5919 }
6285                                                  5920 
6286 /********************************************    5921 /*********************************************************************
6287  *                                               5922  *
6288  * Public routines                               5923  * Public routines
6289  *                                               5924  *
6290  ********************************************    5925  *********************************************************************/
6291                                                  5926 
6292 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psBeg << 5927 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psBeginPage(const char *title, const char *producer,
6293                                   tools_GLint    5928                                   tools_GLint viewport[4], tools_GLint format, tools_GLint sort,
6294                                   tools_GLint    5929                                   tools_GLint options, tools_GLint colormode,
6295                                   tools_GLint    5930                                   tools_GLint colorsize, tools_GL2PSrgba *colormap,
6296                                   tools_GLint    5931                                   tools_GLint nr, tools_GLint ng, tools_GLint nb, tools_GLint buffersize,
6297                                   FILE *strea    5932                                   FILE *stream, const char *filename)
6298 {                                                5933 {
6299   tools_GLint idx;                               5934   tools_GLint idx;
6300   int i;                                         5935   int i;
6301                                                  5936 
6302 /*G.Barrand: if(gl2ps){                       << 5937   if(tools_gl2ps_context){
6303     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "tools_    5938     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "tools_gl2psBeginPage called in wrong program state");
6304     return TOOLS_GL2PS_ERROR;                    5939     return TOOLS_GL2PS_ERROR;
6305   }                                              5940   }
6306                                                  5941 
6307   gl2ps = (tools_GL2PScontext*)tools_gl2psMal << 5942   tools_gl2ps_context = (tools_GL2PScontext*)tools_gl2psMalloc(sizeof(tools_GL2PScontext));
6308 */                                            << 
6309                                                  5943 
6310   /* Validate options */                         5944   /* Validate options */
6311   if (tools_gl2psCheckOptions(options, colorm    5945   if (tools_gl2psCheckOptions(options, colormode) == TOOLS_GL_FALSE) {
6312   /*tools_gl2psFree(gl2ps);                   << 5946     tools_gl2psFree(tools_gl2ps_context);
6313     gl2ps = NULL;*/                           << 5947     tools_gl2ps_context = NULL;
6314     return TOOLS_GL2PS_ERROR;                    5948     return TOOLS_GL2PS_ERROR;
6315   }                                              5949   }
6316                                                  5950 
6317   if(format >= 0 && format < (tools_GLint)(si    5951   if(format >= 0 && format < (tools_GLint)(sizeof(tools_gl2psbackends) / sizeof(tools_gl2psbackends[0]))){
6318     gl2ps->format = format;                   << 5952     tools_gl2ps_context->format = format;
6319   }                                              5953   }
6320   else {                                         5954   else {
6321     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknow    5955     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknown output format: %d", format);
6322   /*tools_gl2psFree(gl2ps);                   << 5956     tools_gl2psFree(tools_gl2ps_context);
6323     gl2ps = NULL;*/                           << 5957     tools_gl2ps_context = NULL;
6324     return TOOLS_GL2PS_ERROR;                    5958     return TOOLS_GL2PS_ERROR;
6325   }                                              5959   }
6326                                                  5960 
6327   switch(sort){                                  5961   switch(sort){
6328   case TOOLS_GL2PS_NO_SORT :                     5962   case TOOLS_GL2PS_NO_SORT :
6329   case TOOLS_GL2PS_SIMPLE_SORT :                 5963   case TOOLS_GL2PS_SIMPLE_SORT :
6330   case TOOLS_GL2PS_BSP_SORT :                    5964   case TOOLS_GL2PS_BSP_SORT :
6331     gl2ps->sort = sort;                       << 5965     tools_gl2ps_context->sort = sort;
6332     break;                                       5966     break;
6333   default :                                      5967   default :
6334     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknow    5968     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknown sorting algorithm: %d", sort);
6335   /*tools_gl2psFree(gl2ps);                   << 5969     tools_gl2psFree(tools_gl2ps_context);
6336     gl2ps = NULL;*/                           << 5970     tools_gl2ps_context = NULL;
6337     return TOOLS_GL2PS_ERROR;                    5971     return TOOLS_GL2PS_ERROR;
6338   }                                              5972   }
6339                                                  5973 
6340   if(stream){                                    5974   if(stream){
6341     gl2ps->stream = stream;                   << 5975     tools_gl2ps_context->stream = stream;
6342   }                                              5976   }
6343   else{                                          5977   else{
6344     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Bad fi    5978     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Bad file pointer");
6345   /*tools_gl2psFree(gl2ps);                   << 5979     tools_gl2psFree(tools_gl2ps_context);
6346     gl2ps = NULL;*/                           << 5980     tools_gl2ps_context = NULL;
6347     return TOOLS_GL2PS_ERROR;                    5981     return TOOLS_GL2PS_ERROR;
6348   }                                              5982   }
6349                                                  5983 
6350   gl2ps->header = TOOLS_GL_TRUE;              << 5984   tools_gl2ps_context->header = TOOLS_GL_TRUE;
6351   gl2ps->forcerasterpos = TOOLS_GL_FALSE;     << 5985   tools_gl2ps_context->forcerasterpos = TOOLS_GL_FALSE;
6352   gl2ps->maxbestroot = 10;                    << 5986   tools_gl2ps_context->maxbestroot = 10;
6353   gl2ps->options = options;                   << 5987   tools_gl2ps_context->options = options;
6354   gl2ps->compress = NULL;                     << 5988   tools_gl2ps_context->compress = NULL;
6355   gl2ps->imagemap_head = NULL;                << 5989   tools_gl2ps_context->imagemap_head = NULL;
6356   gl2ps->imagemap_tail = NULL;                << 5990   tools_gl2ps_context->imagemap_tail = NULL;
6357                                                  5991 
6358   if(gl2ps->options & TOOLS_GL2PS_USE_CURRENT << 5992   if(tools_gl2ps_context->options & TOOLS_GL2PS_USE_CURRENT_VIEWPORT){
6359     tools_glGetIntegerv(TOOLS_GL_VIEWPORT, gl << 5993     tools_glGetIntegerv(TOOLS_GL_VIEWPORT, tools_gl2ps_context->viewport);
6360   }                                              5994   }
6361   else{                                          5995   else{
6362     for(i = 0; i < 4; i++){                      5996     for(i = 0; i < 4; i++){
6363       gl2ps->viewport[i] = viewport[i];       << 5997       tools_gl2ps_context->viewport[i] = viewport[i];
6364     }                                            5998     }
6365   }                                              5999   }
6366                                                  6000 
6367   if(!gl2ps->viewport[2] || !gl2ps->viewport[ << 6001   if(!tools_gl2ps_context->viewport[2] || !tools_gl2ps_context->viewport[3]){
6368     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Incorr    6002     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Incorrect viewport (x=%d, y=%d, width=%d, height=%d)",
6369              gl2ps->viewport[0], gl2ps->viewp << 6003              tools_gl2ps_context->viewport[0], tools_gl2ps_context->viewport[1],
6370              gl2ps->viewport[2], gl2ps->viewp << 6004              tools_gl2ps_context->viewport[2], tools_gl2ps_context->viewport[3]);
6371   /*tools_gl2psFree(gl2ps);                   << 6005     tools_gl2psFree(tools_gl2ps_context);
6372     gl2ps = NULL;*/                           << 6006     tools_gl2ps_context = NULL;
6373     return TOOLS_GL2PS_ERROR;                    6007     return TOOLS_GL2PS_ERROR;
6374   }                                              6008   }
6375                                                  6009 
6376   gl2ps->threshold[0] = nr ? 1.0F / (tools_GL << 6010   tools_gl2ps_context->threshold[0] = nr ? 1.0F / (tools_GLfloat)nr : 0.064F;
6377   gl2ps->threshold[1] = ng ? 1.0F / (tools_GL << 6011   tools_gl2ps_context->threshold[1] = ng ? 1.0F / (tools_GLfloat)ng : 0.034F;
6378   gl2ps->threshold[2] = nb ? 1.0F / (tools_GL << 6012   tools_gl2ps_context->threshold[2] = nb ? 1.0F / (tools_GLfloat)nb : 0.100F;
6379   gl2ps->colormode = colormode;               << 6013   tools_gl2ps_context->colormode = colormode;
6380   gl2ps->buffersize = buffersize > 0 ? buffer << 6014   tools_gl2ps_context->buffersize = buffersize > 0 ? buffersize : 2048 * 2048;
6381   for(i = 0; i < 3; i++){                        6015   for(i = 0; i < 3; i++){
6382     gl2ps->lastvertex.xyz[i] = -1.0F;         << 6016     tools_gl2ps_context->lastvertex.xyz[i] = -1.0F;
6383   }                                              6017   }
6384   for(i = 0; i < 4; i++){                        6018   for(i = 0; i < 4; i++){
6385     gl2ps->lastvertex.rgba[i] = -1.0F;        << 6019     tools_gl2ps_context->lastvertex.rgba[i] = -1.0F;
6386     gl2ps->lastrgba[i] = -1.0F;               << 6020     tools_gl2ps_context->lastrgba[i] = -1.0F;
6387   }                                              6021   }
6388   gl2ps->lastlinewidth = -1.0F;               << 6022   tools_gl2ps_context->lastlinewidth = -1.0F;
6389   gl2ps->lastlinecap = 0;                     << 6023   tools_gl2ps_context->lastlinecap = 0;
6390   gl2ps->lastlinejoin = 0;                    << 6024   tools_gl2ps_context->lastlinejoin = 0;
6391   gl2ps->lastpattern = 0;                     << 6025   tools_gl2ps_context->lastpattern = 0;
6392   gl2ps->lastfactor = 0;                      << 6026   tools_gl2ps_context->lastfactor = 0;
6393   gl2ps->imagetree = NULL;                    << 6027   tools_gl2ps_context->imagetree = NULL;
6394   gl2ps->primitivetoadd = NULL;               << 6028   tools_gl2ps_context->primitivetoadd = NULL;
6395   gl2ps->zerosurfacearea = TOOLS_GL_FALSE;    << 6029   tools_gl2ps_context->zerosurfacearea = TOOLS_GL_FALSE;
6396   gl2ps->pdfprimlist = NULL;                  << 6030   tools_gl2ps_context->pdfprimlist = NULL;
6397   gl2ps->pdfgrouplist = NULL;                 << 6031   tools_gl2ps_context->pdfgrouplist = NULL;
6398   gl2ps->xreflist = NULL;                     << 6032   tools_gl2ps_context->xreflist = NULL;
6399                                                  6033 
6400   /* get default blending mode from current O    6034   /* get default blending mode from current OpenGL state (enabled by
6401      default for SVG) */                         6035      default for SVG) */
6402   if ((gl2ps->options & TOOLS_GL2PS_NO_BLENDI << 6036   if ((tools_gl2ps_context->options & TOOLS_GL2PS_NO_BLENDING) == TOOLS_GL2PS_NONE) {
6403     gl2ps->blending = (gl2ps->format == TOOLS << 6037     tools_gl2ps_context->blending = (tools_gl2ps_context->format == TOOLS_GL2PS_SVG) ? TOOLS_GL_TRUE
6404                                                  6038                                                    : tools_glIsEnabled(TOOLS_GL_BLEND);
6405     tools_glGetIntegerv(TOOLS_GL_BLEND_SRC, & << 6039     tools_glGetIntegerv(TOOLS_GL_BLEND_SRC, &tools_gl2ps_context->blendfunc[0]);
6406     tools_glGetIntegerv(TOOLS_GL_BLEND_DST, & << 6040     tools_glGetIntegerv(TOOLS_GL_BLEND_DST, &tools_gl2ps_context->blendfunc[1]);
6407   }                                              6041   }
6408   else {                                         6042   else {
6409     gl2ps->blending = TOOLS_GL_FALSE;         << 6043     tools_gl2ps_context->blending = TOOLS_GL_FALSE;
6410   }                                              6044   }
6411                                                  6045 
6412   if(gl2ps->colormode == TOOLS_GL_RGBA){      << 6046   if(tools_gl2ps_context->colormode == TOOLS_GL_RGBA){
6413     gl2ps->colorsize = 0;                     << 6047     tools_gl2ps_context->colorsize = 0;
6414     gl2ps->colormap = NULL;                   << 6048     tools_gl2ps_context->colormap = NULL;
6415     if ((gl2ps->options & TOOLS_GL2PS_NO_OPEN << 6049     if ((tools_gl2ps_context->options & TOOLS_GL2PS_NO_OPENGL_CONTEXT) == TOOLS_GL2PS_NONE) {
6416       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_ << 6050       tools_glGetFloatv(TOOLS_GL_COLOR_CLEAR_VALUE, tools_gl2ps_context->bgcolor);
6417     }                                            6051     }
6418   }                                              6052   }
6419   else if(gl2ps->colormode == TOOLS_GL_COLOR_ << 6053   else if(tools_gl2ps_context->colormode == TOOLS_GL_COLOR_INDEX){
6420     if(!colorsize || !colormap){                 6054     if(!colorsize || !colormap){
6421       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Miss    6055       tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Missing colormap for TOOLS_GL_COLOR_INDEX rendering");
6422     /*tools_gl2psFree(gl2ps);                 << 6056       tools_gl2psFree(tools_gl2ps_context);
6423       gl2ps = NULL;*/                         << 6057       tools_gl2ps_context = NULL;
6424       return TOOLS_GL2PS_ERROR;                  6058       return TOOLS_GL2PS_ERROR;
6425     }                                            6059     }
6426     gl2ps->colorsize = colorsize;             << 6060     tools_gl2ps_context->colorsize = colorsize;
6427     gl2ps->colormap = (tools_GL2PSrgba*)tools << 6061     tools_gl2ps_context->colormap = (tools_GL2PSrgba*)tools_gl2psMalloc(tools_gl2ps_context->colorsize * sizeof(tools_GL2PSrgba));
6428     memcpy(gl2ps->colormap, colormap, gl2ps-> << 6062     memcpy(tools_gl2ps_context->colormap, colormap, tools_gl2ps_context->colorsize * sizeof(tools_GL2PSrgba));
6429     tools_glGetIntegerv(TOOLS_GL_INDEX_CLEAR_    6063     tools_glGetIntegerv(TOOLS_GL_INDEX_CLEAR_VALUE, &idx);
6430     gl2ps->bgcolor[0] = gl2ps->colormap[idx][ << 6064     tools_gl2ps_context->bgcolor[0] = tools_gl2ps_context->colormap[idx][0];
6431     gl2ps->bgcolor[1] = gl2ps->colormap[idx][ << 6065     tools_gl2ps_context->bgcolor[1] = tools_gl2ps_context->colormap[idx][1];
6432     gl2ps->bgcolor[2] = gl2ps->colormap[idx][ << 6066     tools_gl2ps_context->bgcolor[2] = tools_gl2ps_context->colormap[idx][2];
6433     gl2ps->bgcolor[3] = 1.0F;                 << 6067     tools_gl2ps_context->bgcolor[3] = 1.0F;
6434   }                                              6068   }
6435   else{                                          6069   else{
6436     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknow    6070     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknown color mode in tools_gl2psBeginPage");
6437   /*tools_gl2psFree(gl2ps);                   << 6071     tools_gl2psFree(tools_gl2ps_context);
6438     gl2ps = NULL;*/                           << 6072     tools_gl2ps_context = NULL;
6439     return TOOLS_GL2PS_ERROR;                    6073     return TOOLS_GL2PS_ERROR;
6440   }                                              6074   }
6441                                                  6075 
6442   if(!title){                                    6076   if(!title){
6443     gl2ps->title = (char*)tools_gl2psMalloc(s << 6077     tools_gl2ps_context->title = (char*)tools_gl2psMalloc(sizeof(char));
6444     gl2ps->title[0] = '\0';                   << 6078     tools_gl2ps_context->title[0] = '\0';
6445   }                                              6079   }
6446   else{                                          6080   else{
6447     gl2ps->title = (char*)tools_gl2psMalloc(( << 6081     tools_gl2ps_context->title = (char*)tools_gl2psMalloc((strlen(title)+1)*sizeof(char));
6448     strcpy(gl2ps->title, title);              << 6082     strcpy(tools_gl2ps_context->title, title);
6449   }                                              6083   }
6450                                                  6084 
6451   if(!producer){                                 6085   if(!producer){
6452     gl2ps->producer = (char*)tools_gl2psMallo << 6086     tools_gl2ps_context->producer = (char*)tools_gl2psMalloc(sizeof(char));
6453     gl2ps->producer[0] = '\0';                << 6087     tools_gl2ps_context->producer[0] = '\0';
6454   }                                              6088   }
6455   else{                                          6089   else{
6456     gl2ps->producer = (char*)tools_gl2psMallo << 6090     tools_gl2ps_context->producer = (char*)tools_gl2psMalloc((strlen(producer)+1)*sizeof(char));
6457     strcpy(gl2ps->producer, producer);        << 6091     strcpy(tools_gl2ps_context->producer, producer);
6458   }                                              6092   }
6459                                                  6093 
6460   if(!filename){                                 6094   if(!filename){
6461     gl2ps->filename = (char*)tools_gl2psMallo << 6095     tools_gl2ps_context->filename = (char*)tools_gl2psMalloc(sizeof(char));
6462     gl2ps->filename[0] = '\0';                << 6096     tools_gl2ps_context->filename[0] = '\0';
6463   }                                              6097   }
6464   else{                                          6098   else{
6465     gl2ps->filename = (char*)tools_gl2psMallo << 6099     tools_gl2ps_context->filename = (char*)tools_gl2psMalloc((strlen(filename)+1)*sizeof(char));
6466     strcpy(gl2ps->filename, filename);        << 6100     strcpy(tools_gl2ps_context->filename, filename);
6467   }                                              6101   }
6468                                                  6102 
6469   gl2ps->primitives = tools_gl2psListCreate(5 << 6103   tools_gl2ps_context->primitives = tools_gl2psListCreate(500, 500, sizeof(tools_GL2PSprimitive*));
6470   gl2ps->auxprimitives = tools_gl2psListCreat << 6104   tools_gl2ps_context->auxprimitives = tools_gl2psListCreate(100, 100, sizeof(tools_GL2PSprimitive*));
6471                                                  6105 
6472   if ((gl2ps->options & TOOLS_GL2PS_NO_OPENGL << 6106   if ((tools_gl2ps_context->options & TOOLS_GL2PS_NO_OPENGL_CONTEXT) == TOOLS_GL2PS_NONE) {
6473     gl2ps->feedback = (tools_GLfloat*)tools_g << 6107     tools_gl2ps_context->feedback = (tools_GLfloat*)tools_gl2psMalloc(tools_gl2ps_context->buffersize * sizeof(tools_GLfloat));
6474     tools_glFeedbackBuffer(gl2ps->buffersize, << 6108     tools_glFeedbackBuffer(tools_gl2ps_context->buffersize, TOOLS_GL_3D_COLOR, tools_gl2ps_context->feedback);
6475     tools_glRenderMode(TOOLS_GL_FEEDBACK);       6109     tools_glRenderMode(TOOLS_GL_FEEDBACK);
6476   }                                              6110   }
6477   else {                                         6111   else {
6478     gl2ps->feedback = NULL;                   << 6112     tools_gl2ps_context->feedback = NULL;
6479     gl2ps->buffersize = 0;                    << 6113     tools_gl2ps_context->buffersize = 0;
6480   }                                              6114   }
6481                                                  6115 
6482   gl2ps->tex_scaling = 1.;                    << 
6483                                               << 
6484   return TOOLS_GL2PS_SUCCESS;                    6116   return TOOLS_GL2PS_SUCCESS;
6485 }                                                6117 }
6486                                                  6118 
6487 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psEnd << 6119 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psEndPage(void)
6488 {                                                6120 {
6489   tools_GLint res;                               6121   tools_GLint res;
6490                                                  6122 
6491 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6123   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6492                                                  6124 
6493   res = tools_gl2psPrintPrimitives(gl2ps);    << 6125   res = tools_gl2psPrintPrimitives();
6494                                                  6126 
6495   if(res != TOOLS_GL2PS_OVERFLOW)                6127   if(res != TOOLS_GL2PS_OVERFLOW)
6496     (tools_gl2psbackends[gl2ps->format]->prin << 6128     (tools_gl2psbackends[tools_gl2ps_context->format]->printFooter)();
6497                                                  6129 
6498   fflush(gl2ps->stream);                      << 6130   fflush(tools_gl2ps_context->stream);
6499                                                  6131 
6500   tools_gl2psListDelete(gl2ps->primitives);   << 6132   tools_gl2psListDelete(tools_gl2ps_context->primitives);
6501   tools_gl2psListDelete(gl2ps->auxprimitives) << 6133   tools_gl2psListDelete(tools_gl2ps_context->auxprimitives);
6502   tools_gl2psFreeImagemap(gl2ps->imagemap_hea << 6134   tools_tools_gl2psFreeImagemap(tools_gl2ps_context->imagemap_head);
6503   tools_gl2psFree(gl2ps->colormap);           << 6135   tools_gl2psFree(tools_gl2ps_context->colormap);
6504   tools_gl2psFree(gl2ps->title);              << 6136   tools_gl2psFree(tools_gl2ps_context->title);
6505   tools_gl2psFree(gl2ps->producer);           << 6137   tools_gl2psFree(tools_gl2ps_context->producer);
6506   tools_gl2psFree(gl2ps->filename);           << 6138   tools_gl2psFree(tools_gl2ps_context->filename);
6507   tools_gl2psFree(gl2ps->feedback);           << 6139   tools_gl2psFree(tools_gl2ps_context->feedback);
6508 /*tools_gl2psFree(gl2ps);                     << 6140   tools_gl2psFree(tools_gl2ps_context);
6509   gl2ps = NULL;*/                             << 6141   tools_gl2ps_context = NULL;
6510                                                  6142 
6511   return res;                                    6143   return res;
6512 }                                                6144 }
6513                                                  6145 
6514 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psBeg << 6146 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psBeginViewport(tools_GLint viewport[4])
6515 {                                                6147 {
6516 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6148   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6517                                                  6149 
6518   (tools_gl2psbackends[gl2ps->format]->beginV << 6150   (tools_gl2psbackends[tools_gl2ps_context->format]->beginViewport)(viewport);
6519                                                  6151 
6520   return TOOLS_GL2PS_SUCCESS;                    6152   return TOOLS_GL2PS_SUCCESS;
6521 }                                                6153 }
6522                                                  6154 
6523 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psEnd << 6155 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psEndViewport(void)
6524 {                                                6156 {
6525   tools_GLint res;                               6157   tools_GLint res;
6526                                                  6158 
6527 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6159   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6528                                                  6160 
6529   res = (tools_gl2psbackends[gl2ps->format]-> << 6161   res = (tools_gl2psbackends[tools_gl2ps_context->format]->endViewport)();
6530                                                  6162 
6531   /* reset last used colors, line widths */      6163   /* reset last used colors, line widths */
6532   tools_gl2psResetLineProperties(gl2ps);      << 6164   tools_gl2psResetLineProperties();
6533                                               << 
6534   return res;                                 << 
6535 }                                             << 
6536                                               << 
6537 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSor << 
6538 {                                             << 
6539   tools_GLint res;                            << 
6540                                               << 
6541 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 
6542                                               << 
6543   switch(mode){                               << 
6544   case TOOLS_GL2PS_NO_SORT :                  << 
6545   case TOOLS_GL2PS_SIMPLE_SORT :              << 
6546   case TOOLS_GL2PS_BSP_SORT :                 << 
6547     gl2ps->sort = mode;                       << 
6548     res = TOOLS_GL2PS_SUCCESS;                << 
6549     break;                                    << 
6550   default :                                   << 
6551     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "Unknow << 
6552   /*tools_gl2psFree(gl2ps);                   << 
6553     gl2ps = NULL;*/                           << 
6554     res = TOOLS_GL2PS_ERROR;                  << 
6555   }                                           << 
6556                                                  6165 
6557   return res;                                    6166   return res;
6558 }                                                6167 }
6559                                                  6168 
6560 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psTex << 6169 TOOLS_GL2PSDLL_API tools_GLint tools_tools_gl2psTextOptColor(const char *str, const char *fontname,
6561                                      tools_GL    6170                                      tools_GLshort fontsize, tools_GLint alignment, tools_GLfloat angle,
6562                                      tools_GL    6171                                      tools_GL2PSrgba color)
6563 {                                                6172 {
6564   return tools_gl2psAddText(gl2ps, TOOLS_GL2P << 6173   return tools_gl2psAddText(TOOLS_GL2PS_TEXT, str, fontname, fontsize, alignment, angle,
6565                       color, TOOLS_GL_FALSE,  << 6174                       color);
6566 }                                                6175 }
6567                                                  6176 
6568 /**                                           << 6177 TOOLS_GL2PSDLL_API tools_GLint tools_tools_gl2psTextOpt(const char *str, const char *fontname,
6569  * This version of tools_gl2psTextOptColor is << 
6570  * fact that PDF does not support text alignm << 
6571  * (blx, bly) represent the bottom left corne << 
6572  */                                           << 
6573 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psTex << 
6574                                        tools_ << 
6575                                        tools_ << 
6576 {                                             << 
6577   return tools_gl2psAddText(gl2ps, TOOLS_GL2P << 
6578                       color, TOOLS_GL_TRUE, b << 
6579 }                                             << 
6580                                               << 
6581 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psTex << 
6582                                 tools_GLshort    6178                                 tools_GLshort fontsize, tools_GLint alignment, tools_GLfloat angle)
6583 {                                                6179 {
6584   return tools_gl2psAddText(gl2ps, TOOLS_GL2P << 6180   return tools_gl2psAddText(TOOLS_GL2PS_TEXT, str, fontname, fontsize, alignment, angle, NULL);
6585 }                                                6181 }
6586                                                  6182 
6587 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psTex << 6183 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psText(const char *str, const char *fontname, tools_GLshort fontsize)
6588 {                                                6184 {
6589   return tools_gl2psAddText(gl2ps, TOOLS_GL2P << 6185   return tools_gl2psAddText(TOOLS_GL2PS_TEXT, str, fontname, fontsize, TOOLS_GL2PS_TEXT_BL, 0.0F,
6590                       NULL, TOOLS_GL_FALSE, 0 << 6186                       NULL);
6591 }                                                6187 }
6592                                                  6188 
6593 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSpe << 6189 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSpecial(tools_GLint format, const char *str)
6594 {                                                6190 {
6595   return tools_gl2psAddText(gl2ps, TOOLS_GL2P << 6191   return tools_gl2psAddText(TOOLS_GL2PS_SPECIAL, str, "", 0, format, 0.0F, NULL);
6596 }                                                6192 }
6597                                                  6193 
6598 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSpe << 6194 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSpecialColor(tools_GLint format, const char *str, tools_GL2PSrgba rgba)
6599 {                                                6195 {
6600   return tools_gl2psAddText(gl2ps, TOOLS_GL2P << 6196   return tools_gl2psAddText(TOOLS_GL2PS_SPECIAL, str, "", 0, format, 0.0F, rgba);
6601 }                                                6197 }
6602                                                  6198 
6603 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psDra << 6199 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psDrawPixels(tools_GLsizei width, tools_GLsizei height,
6604                                    tools_GLin    6200                                    tools_GLint xorig, tools_GLint yorig,
6605                                    tools_GLen    6201                                    tools_GLenum format, tools_GLenum type,
6606                                    const void    6202                                    const void *pixels)
6607 {                                                6203 {
6608   int size, i;                                   6204   int size, i;
6609   const tools_GLfloat *piv;                      6205   const tools_GLfloat *piv;
6610   tools_GLfloat pos[4], zoom_x, zoom_y;          6206   tools_GLfloat pos[4], zoom_x, zoom_y;
6611   tools_GL2PSprimitive *prim;                    6207   tools_GL2PSprimitive *prim;
6612   tools_GLboolean valid;                         6208   tools_GLboolean valid;
6613                                                  6209 
6614   if(/*!gl2ps ||*/ !pixels) return TOOLS_GL2P << 6210   if(!tools_gl2ps_context || !pixels) return TOOLS_GL2PS_UNINITIALIZED;
6615                                                  6211 
6616   if((width <= 0) || (height <= 0)) return TO    6212   if((width <= 0) || (height <= 0)) return TOOLS_GL2PS_ERROR;
6617                                                  6213 
6618   if(gl2ps->options & TOOLS_GL2PS_NO_PIXMAP)  << 6214   if(tools_gl2ps_context->options & TOOLS_GL2PS_NO_PIXMAP) return TOOLS_GL2PS_SUCCESS;
6619                                                  6215 
6620   if((format != TOOLS_GL_RGB && format != TOO    6216   if((format != TOOLS_GL_RGB && format != TOOLS_GL_RGBA) || type != TOOLS_GL_FLOAT){
6621     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "tools_    6217     tools_gl2psMsg(TOOLS_GL2PS_ERROR, "tools_gl2psDrawPixels only implemented for "
6622              "TOOLS_GL_RGB/TOOLS_GL_RGBA, TOO    6218              "TOOLS_GL_RGB/TOOLS_GL_RGBA, TOOLS_GL_FLOAT pixels");
6623     return TOOLS_GL2PS_ERROR;                    6219     return TOOLS_GL2PS_ERROR;
6624   }                                              6220   }
6625                                                  6221 
6626   if (gl2ps->forcerasterpos) {                << 6222   if (tools_gl2ps_context->forcerasterpos) {
6627     pos[0] = gl2ps->rasterpos.xyz[0];         << 6223     pos[0] = tools_gl2ps_context->rasterpos.xyz[0];
6628     pos[1] = gl2ps->rasterpos.xyz[1];         << 6224     pos[1] = tools_gl2ps_context->rasterpos.xyz[1];
6629     pos[2] = gl2ps->rasterpos.xyz[2];         << 6225     pos[2] = tools_gl2ps_context->rasterpos.xyz[2];
6630     pos[3] = 1.f;                                6226     pos[3] = 1.f;
6631     /* Hardcode zoom factors (for now?) */       6227     /* Hardcode zoom factors (for now?) */
6632     zoom_x = 1.f;                                6228     zoom_x = 1.f;
6633     zoom_y = 1.f;                                6229     zoom_y = 1.f;
6634   }                                              6230   }
6635   else {                                         6231   else {
6636     tools_glGetBooleanv(TOOLS_GL_CURRENT_RAST    6232     tools_glGetBooleanv(TOOLS_GL_CURRENT_RASTER_POSITION_VALID, &valid);
6637     if(TOOLS_GL_FALSE == valid) return TOOLS_    6233     if(TOOLS_GL_FALSE == valid) return TOOLS_GL2PS_SUCCESS; /* the primitive is culled */
6638     tools_glGetFloatv(TOOLS_GL_CURRENT_RASTER    6234     tools_glGetFloatv(TOOLS_GL_CURRENT_RASTER_POSITION, pos);
6639     tools_glGetFloatv(TOOLS_GL_ZOOM_X, &zoom_    6235     tools_glGetFloatv(TOOLS_GL_ZOOM_X, &zoom_x);
6640     tools_glGetFloatv(TOOLS_GL_ZOOM_Y, &zoom_    6236     tools_glGetFloatv(TOOLS_GL_ZOOM_Y, &zoom_y);
6641   }                                              6237   }
6642                                                  6238 
6643   prim = (tools_GL2PSprimitive*)tools_gl2psMa    6239   prim = (tools_GL2PSprimitive*)tools_gl2psMalloc(sizeof(tools_GL2PSprimitive));
6644   prim->type = TOOLS_GL2PS_PIXMAP;               6240   prim->type = TOOLS_GL2PS_PIXMAP;
6645   prim->boundary = 0;                            6241   prim->boundary = 0;
6646   prim->numverts = 1;                            6242   prim->numverts = 1;
6647   prim->verts = (tools_GL2PSvertex*)tools_gl2    6243   prim->verts = (tools_GL2PSvertex*)tools_gl2psMalloc(sizeof(tools_GL2PSvertex));
6648   prim->verts[0].xyz[0] = pos[0] + xorig;        6244   prim->verts[0].xyz[0] = pos[0] + xorig;
6649   prim->verts[0].xyz[1] = pos[1] + yorig;        6245   prim->verts[0].xyz[1] = pos[1] + yorig;
6650   prim->verts[0].xyz[2] = pos[2];                6246   prim->verts[0].xyz[2] = pos[2];
6651   prim->culled = 0;                              6247   prim->culled = 0;
6652   prim->offset = 0;                              6248   prim->offset = 0;
6653   prim->ofactor = 0.0;                           6249   prim->ofactor = 0.0;
6654   prim->ounits = 0.0;                            6250   prim->ounits = 0.0;
6655   prim->pattern = 0;                             6251   prim->pattern = 0;
6656   prim->factor = 0;                              6252   prim->factor = 0;
6657   prim->width = 1;                               6253   prim->width = 1;
6658   if (gl2ps->forcerasterpos) {                << 6254   if (tools_gl2ps_context->forcerasterpos) {
6659     prim->verts[0].rgba[0] = gl2ps->rasterpos << 6255     prim->verts[0].rgba[0] = tools_gl2ps_context->rasterpos.rgba[0];
6660     prim->verts[0].rgba[1] = gl2ps->rasterpos << 6256     prim->verts[0].rgba[1] = tools_gl2ps_context->rasterpos.rgba[1];
6661     prim->verts[0].rgba[2] = gl2ps->rasterpos << 6257     prim->verts[0].rgba[2] = tools_gl2ps_context->rasterpos.rgba[2];
6662     prim->verts[0].rgba[3] = gl2ps->rasterpos << 6258     prim->verts[0].rgba[3] = tools_gl2ps_context->rasterpos.rgba[3];
6663   }                                              6259   }
6664   else {                                         6260   else {
6665     tools_glGetFloatv(TOOLS_GL_CURRENT_RASTER    6261     tools_glGetFloatv(TOOLS_GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba);
6666   }                                              6262   }
6667   prim->data.image = (tools_GL2PSimage*)tools    6263   prim->data.image = (tools_GL2PSimage*)tools_gl2psMalloc(sizeof(tools_GL2PSimage));
6668   prim->data.image->width = width;               6264   prim->data.image->width = width;
6669   prim->data.image->height = height;             6265   prim->data.image->height = height;
6670   prim->data.image->zoom_x = zoom_x;             6266   prim->data.image->zoom_x = zoom_x;
6671   prim->data.image->zoom_y = zoom_y;             6267   prim->data.image->zoom_y = zoom_y;
6672   prim->data.image->format = format;             6268   prim->data.image->format = format;
6673   prim->data.image->type = type;                 6269   prim->data.image->type = type;
6674                                                  6270 
6675   gl2ps->forcerasterpos = TOOLS_GL_FALSE;     << 6271   tools_gl2ps_context->forcerasterpos = TOOLS_GL_FALSE;
6676                                                  6272 
6677   switch(format){                                6273   switch(format){
6678   case TOOLS_GL_RGBA:                            6274   case TOOLS_GL_RGBA:
6679     if(gl2ps->options & TOOLS_GL2PS_NO_BLENDI << 6275     if(tools_gl2ps_context->options & TOOLS_GL2PS_NO_BLENDING || !tools_gl2ps_context->blending){
6680       /* special case: blending turned off */    6276       /* special case: blending turned off */
6681       prim->data.image->format = TOOLS_GL_RGB    6277       prim->data.image->format = TOOLS_GL_RGB;
6682       size = height * width * 3;                 6278       size = height * width * 3;
6683       prim->data.image->pixels = (tools_GLflo    6279       prim->data.image->pixels = (tools_GLfloat*)tools_gl2psMalloc(size * sizeof(tools_GLfloat));
6684       piv = (const tools_GLfloat*)pixels;        6280       piv = (const tools_GLfloat*)pixels;
6685       for(i = 0; i < size; ++i, ++piv){          6281       for(i = 0; i < size; ++i, ++piv){
6686         prim->data.image->pixels[i] = *piv;      6282         prim->data.image->pixels[i] = *piv;
6687         if(!((i + 1) % 3))                       6283         if(!((i + 1) % 3))
6688           ++piv;                                 6284           ++piv;
6689       }                                          6285       }
6690     }                                            6286     }
6691     else{                                        6287     else{
6692       size = height * width * 4;                 6288       size = height * width * 4;
6693       prim->data.image->pixels = (tools_GLflo    6289       prim->data.image->pixels = (tools_GLfloat*)tools_gl2psMalloc(size * sizeof(tools_GLfloat));
6694       memcpy(prim->data.image->pixels, pixels    6290       memcpy(prim->data.image->pixels, pixels, size * sizeof(tools_GLfloat));
6695     }                                            6291     }
6696     break;                                       6292     break;
6697   case TOOLS_GL_RGB:                             6293   case TOOLS_GL_RGB:
6698   default:                                       6294   default:
6699     size = height * width * 3;                   6295     size = height * width * 3;
6700     prim->data.image->pixels = (tools_GLfloat    6296     prim->data.image->pixels = (tools_GLfloat*)tools_gl2psMalloc(size * sizeof(tools_GLfloat));
6701     memcpy(prim->data.image->pixels, pixels,     6297     memcpy(prim->data.image->pixels, pixels, size * sizeof(tools_GLfloat));
6702     break;                                       6298     break;
6703   }                                              6299   }
6704                                                  6300 
6705   /* If no OpenGL context, just add directly     6301   /* If no OpenGL context, just add directly to primitives */
6706   if ((gl2ps->options & TOOLS_GL2PS_NO_OPENGL << 6302   if ((tools_gl2ps_context->options & TOOLS_GL2PS_NO_OPENGL_CONTEXT) == TOOLS_GL2PS_NONE) {
6707     tools_gl2psListAdd(gl2ps->auxprimitives,  << 6303     tools_gl2psListAdd(tools_gl2ps_context->auxprimitives, &prim);
6708     tools_glPassThrough(TOOLS_GL2PS_DRAW_PIXE    6304     tools_glPassThrough(TOOLS_GL2PS_DRAW_PIXELS_TOKEN);
6709   }                                              6305   }
6710   else {                                         6306   else {
6711     tools_gl2psListAdd(gl2ps->primitives, &pr << 6307     tools_gl2psListAdd(tools_gl2ps_context->primitives, &prim);
6712   }                                              6308   }
6713                                                  6309 
6714   return TOOLS_GL2PS_SUCCESS;                    6310   return TOOLS_GL2PS_SUCCESS;
6715 }                                                6311 }
6716                                                  6312 
6717 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psDra << 6313 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psDrawImageMap(tools_GLsizei width, tools_GLsizei height,
6718                                      const to    6314                                      const tools_GLfloat position[3],
6719                                      const un    6315                                      const unsigned char *imagemap){
6720   int size, i;                                   6316   int size, i;
6721   int sizeoffloat = sizeof(tools_GLfloat);       6317   int sizeoffloat = sizeof(tools_GLfloat);
6722                                                  6318 
6723   if(/*!gl2ps ||*/ !imagemap) return TOOLS_GL << 6319   if(!tools_gl2ps_context || !imagemap) return TOOLS_GL2PS_UNINITIALIZED;
6724                                                  6320 
6725   if((width <= 0) || (height <= 0)) return TO    6321   if((width <= 0) || (height <= 0)) return TOOLS_GL2PS_ERROR;
6726                                                  6322 
6727   size = height + height * ((width - 1) / 8);    6323   size = height + height * ((width - 1) / 8);
6728   tools_glPassThrough(TOOLS_GL2PS_IMAGEMAP_TO    6324   tools_glPassThrough(TOOLS_GL2PS_IMAGEMAP_TOKEN);
6729   tools_glBegin(TOOLS_GL_POINTS);                6325   tools_glBegin(TOOLS_GL_POINTS);
6730   tools_glVertex3f(position[0], position[1],p    6326   tools_glVertex3f(position[0], position[1],position[2]);
6731   tools_glEnd();                                 6327   tools_glEnd();
6732   tools_glPassThrough((tools_GLfloat)width);     6328   tools_glPassThrough((tools_GLfloat)width);
6733   tools_glPassThrough((tools_GLfloat)height);    6329   tools_glPassThrough((tools_GLfloat)height);
6734   for(i = 0; i < size; i += sizeoffloat){        6330   for(i = 0; i < size; i += sizeoffloat){
6735     const float *value = (const float*)imagem    6331     const float *value = (const float*)imagemap;
6736     tools_glPassThrough(*value);                 6332     tools_glPassThrough(*value);
6737     imagemap += sizeoffloat;                     6333     imagemap += sizeoffloat;
6738   }                                              6334   }
6739   return TOOLS_GL2PS_SUCCESS;                    6335   return TOOLS_GL2PS_SUCCESS;
6740 }                                                6336 }
6741                                                  6337 
6742 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psEna << 6338 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psEnable(tools_GLint mode)
6743 {                                                6339 {
6744   tools_GLint tmp;                               6340   tools_GLint tmp;
6745   tools_GLfloat tmp2;                            6341   tools_GLfloat tmp2;
6746                                                  6342 
6747 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6343   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6748                                                  6344 
6749   switch(mode){                                  6345   switch(mode){
6750   case TOOLS_GL2PS_POLYGON_OFFSET_FILL :         6346   case TOOLS_GL2PS_POLYGON_OFFSET_FILL :
6751     tools_glPassThrough(TOOLS_GL2PS_BEGIN_OFF    6347     tools_glPassThrough(TOOLS_GL2PS_BEGIN_OFFSET_TOKEN);
6752     tools_glGetFloatv(TOOLS_GL_POLYGON_OFFSET    6348     tools_glGetFloatv(TOOLS_GL_POLYGON_OFFSET_FACTOR, &tmp2);
6753     tools_glPassThrough(tmp2);                   6349     tools_glPassThrough(tmp2);
6754     tools_glGetFloatv(TOOLS_GL_POLYGON_OFFSET    6350     tools_glGetFloatv(TOOLS_GL_POLYGON_OFFSET_UNITS, &tmp2);
6755     tools_glPassThrough(tmp2);                   6351     tools_glPassThrough(tmp2);
6756     break;                                       6352     break;
6757   case TOOLS_GL2PS_POLYGON_BOUNDARY :            6353   case TOOLS_GL2PS_POLYGON_BOUNDARY :
6758     tools_glPassThrough(TOOLS_GL2PS_BEGIN_BOU    6354     tools_glPassThrough(TOOLS_GL2PS_BEGIN_BOUNDARY_TOKEN);
6759     break;                                       6355     break;
6760   case TOOLS_GL2PS_LINE_STIPPLE :                6356   case TOOLS_GL2PS_LINE_STIPPLE :
6761     tools_glPassThrough(TOOLS_GL2PS_BEGIN_STI    6357     tools_glPassThrough(TOOLS_GL2PS_BEGIN_STIPPLE_TOKEN);
6762     tools_glGetIntegerv(TOOLS_GL_LINE_STIPPLE    6358     tools_glGetIntegerv(TOOLS_GL_LINE_STIPPLE_PATTERN, &tmp);
6763     tools_glPassThrough((tools_GLfloat)tmp);     6359     tools_glPassThrough((tools_GLfloat)tmp);
6764     tools_glGetIntegerv(TOOLS_GL_LINE_STIPPLE    6360     tools_glGetIntegerv(TOOLS_GL_LINE_STIPPLE_REPEAT, &tmp);
6765     tools_glPassThrough((tools_GLfloat)tmp);     6361     tools_glPassThrough((tools_GLfloat)tmp);
6766     break;                                       6362     break;
6767   case TOOLS_GL2PS_BLEND :                       6363   case TOOLS_GL2PS_BLEND :
6768     tools_glPassThrough(TOOLS_GL2PS_BEGIN_BLE    6364     tools_glPassThrough(TOOLS_GL2PS_BEGIN_BLEND_TOKEN);
6769     break;                                       6365     break;
6770   default :                                      6366   default :
6771     tools_gl2psMsg(TOOLS_GL2PS_WARNING, "Unkn    6367     tools_gl2psMsg(TOOLS_GL2PS_WARNING, "Unknown mode in tools_gl2psEnable: %d", mode);
6772     return TOOLS_GL2PS_WARNING;                  6368     return TOOLS_GL2PS_WARNING;
6773   }                                              6369   }
6774                                                  6370 
6775   return TOOLS_GL2PS_SUCCESS;                    6371   return TOOLS_GL2PS_SUCCESS;
6776 }                                                6372 }
6777                                                  6373 
6778 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psDis << 6374 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psDisable(tools_GLint mode)
6779 {                                                6375 {
6780 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6376   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6781                                                  6377 
6782   switch(mode){                                  6378   switch(mode){
6783   case TOOLS_GL2PS_POLYGON_OFFSET_FILL :         6379   case TOOLS_GL2PS_POLYGON_OFFSET_FILL :
6784     tools_glPassThrough(TOOLS_GL2PS_END_OFFSE    6380     tools_glPassThrough(TOOLS_GL2PS_END_OFFSET_TOKEN);
6785     break;                                       6381     break;
6786   case TOOLS_GL2PS_POLYGON_BOUNDARY :            6382   case TOOLS_GL2PS_POLYGON_BOUNDARY :
6787     tools_glPassThrough(TOOLS_GL2PS_END_BOUND    6383     tools_glPassThrough(TOOLS_GL2PS_END_BOUNDARY_TOKEN);
6788     break;                                       6384     break;
6789   case TOOLS_GL2PS_LINE_STIPPLE :                6385   case TOOLS_GL2PS_LINE_STIPPLE :
6790     tools_glPassThrough(TOOLS_GL2PS_END_STIPP    6386     tools_glPassThrough(TOOLS_GL2PS_END_STIPPLE_TOKEN);
6791     break;                                       6387     break;
6792   case TOOLS_GL2PS_BLEND :                       6388   case TOOLS_GL2PS_BLEND :
6793     tools_glPassThrough(TOOLS_GL2PS_END_BLEND    6389     tools_glPassThrough(TOOLS_GL2PS_END_BLEND_TOKEN);
6794     break;                                       6390     break;
6795   default :                                      6391   default :
6796     tools_gl2psMsg(TOOLS_GL2PS_WARNING, "Unkn    6392     tools_gl2psMsg(TOOLS_GL2PS_WARNING, "Unknown mode in tools_gl2psDisable: %d", mode);
6797     return TOOLS_GL2PS_WARNING;                  6393     return TOOLS_GL2PS_WARNING;
6798   }                                              6394   }
6799                                                  6395 
6800   return TOOLS_GL2PS_SUCCESS;                    6396   return TOOLS_GL2PS_SUCCESS;
6801 }                                                6397 }
6802                                                  6398 
6803 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psPoi << 6399 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psPointSize(tools_GLfloat value)
6804 {                                                6400 {
6805 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6401   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6806                                                  6402 
6807   tools_glPassThrough(TOOLS_GL2PS_POINT_SIZE_    6403   tools_glPassThrough(TOOLS_GL2PS_POINT_SIZE_TOKEN);
6808   tools_glPassThrough(value);                    6404   tools_glPassThrough(value);
6809                                                  6405 
6810   return TOOLS_GL2PS_SUCCESS;                    6406   return TOOLS_GL2PS_SUCCESS;
6811 }                                                6407 }
6812                                                  6408 
6813 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psLin << 6409 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psLineCap(tools_GLint value)
6814 {                                                6410 {
6815 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6411   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6816                                                  6412 
6817   tools_glPassThrough(TOOLS_GL2PS_LINE_CAP_TO    6413   tools_glPassThrough(TOOLS_GL2PS_LINE_CAP_TOKEN);
6818   tools_glPassThrough(value);                    6414   tools_glPassThrough(value);
6819                                                  6415 
6820   return TOOLS_GL2PS_SUCCESS;                    6416   return TOOLS_GL2PS_SUCCESS;
6821 }                                                6417 }
6822                                                  6418 
6823 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psLin << 6419 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psLineJoin(tools_GLint value)
6824 {                                                6420 {
6825 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6421   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6826                                                  6422 
6827   tools_glPassThrough(TOOLS_GL2PS_LINE_JOIN_T    6423   tools_glPassThrough(TOOLS_GL2PS_LINE_JOIN_TOKEN);
6828   tools_glPassThrough((tools_GLfloat)value);     6424   tools_glPassThrough((tools_GLfloat)value);  //G.Barrand : _MSC_VER : cast.
6829                                                  6425 
6830   return TOOLS_GL2PS_SUCCESS;                    6426   return TOOLS_GL2PS_SUCCESS;
6831 }                                                6427 }
6832                                                  6428 
6833 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psLin << 6429 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psLineWidth(tools_GLfloat value)
6834 {                                                6430 {
6835 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6431   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6836                                                  6432 
6837   tools_glPassThrough(TOOLS_GL2PS_LINE_WIDTH_    6433   tools_glPassThrough(TOOLS_GL2PS_LINE_WIDTH_TOKEN);
6838   tools_glPassThrough(value);                 << 6434   tools_glPassThrough((tools_GLfloat)value);  //G.Barrand : _MSC_VER : cast.
6839                                                  6435 
6840   return TOOLS_GL2PS_SUCCESS;                    6436   return TOOLS_GL2PS_SUCCESS;
6841 }                                                6437 }
6842                                                  6438 
6843 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psBle << 6439 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psBlendFunc(tools_GLenum sfactor, tools_GLenum dfactor)
6844 {                                                6440 {
6845 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6441   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6846                                                  6442 
6847   if(TOOLS_GL_FALSE == tools_gl2psSupportedBl    6443   if(TOOLS_GL_FALSE == tools_gl2psSupportedBlendMode(sfactor, dfactor))
6848     return TOOLS_GL2PS_WARNING;                  6444     return TOOLS_GL2PS_WARNING;
6849                                                  6445 
6850   tools_glPassThrough(TOOLS_GL2PS_SRC_BLEND_T    6446   tools_glPassThrough(TOOLS_GL2PS_SRC_BLEND_TOKEN);
6851   tools_glPassThrough((tools_GLfloat)sfactor)    6447   tools_glPassThrough((tools_GLfloat)sfactor);
6852   tools_glPassThrough(TOOLS_GL2PS_DST_BLEND_T    6448   tools_glPassThrough(TOOLS_GL2PS_DST_BLEND_TOKEN);
6853   tools_glPassThrough((tools_GLfloat)dfactor)    6449   tools_glPassThrough((tools_GLfloat)dfactor);
6854                                                  6450 
6855   return TOOLS_GL2PS_SUCCESS;                    6451   return TOOLS_GL2PS_SUCCESS;
6856 }                                                6452 }
6857                                                  6453 
6858 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSet << 6454 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSetOptions(tools_GLint options)
6859 {                                                6455 {
6860 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 6456   if(!tools_gl2ps_context) return TOOLS_GL2PS_UNINITIALIZED;
6861                                                  6457 
6862   if(tools_gl2psCheckOptions(options, gl2ps-> << 6458   if(tools_gl2psCheckOptions(options, tools_gl2ps_context->colormode) == TOOLS_GL_FALSE) {
6863     return TOOLS_GL2PS_ERROR;                    6459     return TOOLS_GL2PS_ERROR;
6864   }                                              6460   }
6865                                                  6461 
6866   gl2ps->options = options;                   << 6462   tools_gl2ps_context->options = options;
6867                                                  6463 
6868   return TOOLS_GL2PS_SUCCESS;                    6464   return TOOLS_GL2PS_SUCCESS;
6869 }                                                6465 }
6870                                                  6466 
6871 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psGet << 6467 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psGetOptions(tools_GLint *options)
6872 {                                                6468 {
6873 /*if(!gl2ps) {                                << 6469   if(!tools_gl2ps_context) {
6874     *options = 0;                                6470     *options = 0;
6875     return TOOLS_GL2PS_UNINITIALIZED;            6471     return TOOLS_GL2PS_UNINITIALIZED;
6876   }*/                                         << 6472   }
6877                                                  6473 
6878   *options = gl2ps->options;                  << 6474   *options = tools_gl2ps_context->options;
6879                                                  6475 
6880   return TOOLS_GL2PS_SUCCESS;                    6476   return TOOLS_GL2PS_SUCCESS;
6881 }                                                6477 }
6882                                                  6478 
6883 TOOLS_GL2PSDLL_API const char *tools_gl2psGet    6479 TOOLS_GL2PSDLL_API const char *tools_gl2psGetFileExtension(tools_GLint format)
6884 {                                                6480 {
6885   if(format >= 0 && format < (tools_GLint)(si    6481   if(format >= 0 && format < (tools_GLint)(sizeof(tools_gl2psbackends) / sizeof(tools_gl2psbackends[0])))
6886     return tools_gl2psbackends[format]->file_    6482     return tools_gl2psbackends[format]->file_extension;
6887   else                                           6483   else
6888     return "Unknown format";                     6484     return "Unknown format";
6889 }                                                6485 }
6890                                                  6486 
6891 TOOLS_GL2PSDLL_API const char *tools_gl2psGet    6487 TOOLS_GL2PSDLL_API const char *tools_gl2psGetFormatDescription(tools_GLint format)
6892 {                                                6488 {
6893   if(format >= 0 && format < (tools_GLint)(si    6489   if(format >= 0 && format < (tools_GLint)(sizeof(tools_gl2psbackends) / sizeof(tools_gl2psbackends[0])))
6894     return tools_gl2psbackends[format]->descr    6490     return tools_gl2psbackends[format]->description;
6895   else                                           6491   else
6896     return "Unknown format";                     6492     return "Unknown format";
6897 }                                                6493 }
6898                                                  6494 
6899 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psGet << 6495 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psGetFileFormat()
6900 {                                                6496 {
6901 /*if(!gl2ps) {                                << 6497   return tools_gl2ps_context->format;
6902     return TOOLS_GL2PS_UNINITIALIZED;         << 
6903   }*/                                         << 
6904                                               << 
6905   return gl2ps->format;                       << 
6906 }                                                6498 }
6907                                                  6499 
6908 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psFor << 6500 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psForceRasterPos(tools_GL2PSvertex *vert)
6909 {                                                6501 {
6910                                                  6502 
6911 /*if(!gl2ps) {                                << 6503   if(!tools_gl2ps_context) {
6912     return TOOLS_GL2PS_UNINITIALIZED;            6504     return TOOLS_GL2PS_UNINITIALIZED;
6913   }*/                                         << 6505   }
6914                                               << 
6915   gl2ps->forcerasterpos = TOOLS_GL_TRUE;      << 
6916   gl2ps->rasterpos.xyz[0] = vert->xyz[0];     << 
6917   gl2ps->rasterpos.xyz[1] = vert->xyz[1];     << 
6918   gl2ps->rasterpos.xyz[2] = vert->xyz[2];     << 
6919   gl2ps->rasterpos.rgba[0] = vert->rgba[0];   << 
6920   gl2ps->rasterpos.rgba[1] = vert->rgba[1];   << 
6921   gl2ps->rasterpos.rgba[2] = vert->rgba[2];   << 
6922   gl2ps->rasterpos.rgba[3] = vert->rgba[3];   << 
6923                                               << 
6924   return TOOLS_GL2PS_SUCCESS;                 << 
6925 }                                             << 
6926                                               << 
6927 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSet << 
6928 {                                             << 
6929                                               << 
6930 /*if(!gl2ps) {                                << 
6931     return TOOLS_GL2PS_UNINITIALIZED;         << 
6932   }*/                                         << 
6933   gl2ps->tex_scaling = scaling;               << 
6934                                               << 
6935   return TOOLS_GL2PS_SUCCESS;                 << 
6936 }                                             << 
6937                                               << 
6938 TOOLS_GL2PSDLL_API tools_GLint tools_gl2psSet << 
6939 {                                             << 
6940 /*if(!gl2ps) return TOOLS_GL2PS_UNINITIALIZED << 
6941                                                  6506 
6942   gl2ps->bgcolor[0] = a_r;                    << 6507   tools_gl2ps_context->forcerasterpos = TOOLS_GL_TRUE;
6943   gl2ps->bgcolor[1] = a_g;                    << 6508   tools_gl2ps_context->rasterpos.xyz[0] = vert->xyz[0];
6944   gl2ps->bgcolor[2] = a_b;                    << 6509   tools_gl2ps_context->rasterpos.xyz[1] = vert->xyz[1];
6945   gl2ps->bgcolor[3] = 1.0F;                   << 6510   tools_gl2ps_context->rasterpos.xyz[2] = vert->xyz[2];
                                                   >> 6511   tools_gl2ps_context->rasterpos.rgba[0] = vert->rgba[0];
                                                   >> 6512   tools_gl2ps_context->rasterpos.rgba[1] = vert->rgba[1];
                                                   >> 6513   tools_gl2ps_context->rasterpos.rgba[2] = vert->rgba[2];
                                                   >> 6514   tools_gl2ps_context->rasterpos.rgba[3] = vert->rgba[3];
6946                                                  6515 
6947   return TOOLS_GL2PS_SUCCESS;                    6516   return TOOLS_GL2PS_SUCCESS;
6948 }                                                6517 }
6949                                                  6518 
6950 #undef TOOLS_GL2PS_EPSILON                    << 6519 #include "gl2ps_end.icc"
6951 #undef TOOLS_GL2PS_ZSCALE                     << 
6952 #undef TOOLS_GL2PS_ZOFFSET                    << 
6953 #undef TOOLS_GL2PS_ZOFFSET_LARGE              << 
6954 #undef TOOLS_GL2PS_ZERO                       << 
6955 #undef TOOLS_GL2PS_COINCIDENT                 << 
6956 #undef TOOLS_GL2PS_IN_FRONT_OF                << 
6957 #undef TOOLS_GL2PS_IN_BACK_OF                 << 
6958 #undef TOOLS_GL2PS_SPANNING                   << 
6959 #undef TOOLS_GL2PS_POINT_COINCIDENT           << 
6960 #undef TOOLS_GL2PS_POINT_INFRONT              << 
6961 #undef TOOLS_GL2PS_POINT_BACK                 << 
6962 #undef TOOLS_GL2PS_BEGIN_OFFSET_TOKEN         << 
6963 #undef TOOLS_GL2PS_END_OFFSET_TOKEN           << 
6964 #undef TOOLS_GL2PS_BEGIN_BOUNDARY_TOKEN       << 
6965 #undef TOOLS_GL2PS_END_BOUNDARY_TOKEN         << 
6966 #undef TOOLS_GL2PS_BEGIN_STIPPLE_TOKEN        << 
6967 #undef TOOLS_GL2PS_END_STIPPLE_TOKEN          << 
6968 #undef TOOLS_GL2PS_POINT_SIZE_TOKEN           << 
6969 #undef TOOLS_GL2PS_LINE_CAP_TOKEN             << 
6970 #undef TOOLS_GL2PS_LINE_JOIN_TOKEN            << 
6971 #undef TOOLS_GL2PS_LINE_WIDTH_TOKEN           << 
6972 #undef TOOLS_GL2PS_BEGIN_BLEND_TOKEN          << 
6973 #undef TOOLS_GL2PS_END_BLEND_TOKEN            << 
6974 #undef TOOLS_GL2PS_SRC_BLEND_TOKEN            << 
6975 #undef TOOLS_GL2PS_DST_BLEND_TOKEN            << 
6976 #undef TOOLS_GL2PS_IMAGEMAP_TOKEN             << 
6977 #undef TOOLS_GL2PS_DRAW_PIXELS_TOKEN          << 
6978 #undef TOOLS_GL2PS_TEXT_TOKEN                 << 
6979                                               << 
6980 #endif /*tools_gl2ps*/                           6520 #endif /*tools_gl2ps*/