0020716: Eliminate usage of "config.h" header file
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Export.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <OpenGl_GraphicDriver.hxx>
15 #include <OpenGl_Context.hxx>
16 #include <OpenGl_CView.hxx>
17
18 #include <Standard_CLocaleSentry.hxx>
19
20 #ifdef HAVE_GL2PS
21 #include <gl2ps.h>
22 #endif
23
24 /************************************************************************/
25 /* Print Methods                                                        */
26 /************************************************************************/
27
28 #ifdef HAVE_GL2PS
29 Standard_Boolean OpenGl_GraphicDriver::Export (const Standard_CString theFileName,
30                                                const Graphic3d_ExportFormat theFormat,
31                                                const Graphic3d_SortType theSortType,
32                                                const Standard_Integer theWidth,
33                                                const Standard_Integer theHeight,
34                                                const Graphic3d_CView& theView,
35                                                const Aspect_CLayer2d& theLayerUnder,
36                                                const Aspect_CLayer2d& theLayerOver,
37                                                const Standard_Real    /*thePrecision*/,
38                                                const Standard_Address /*theProgressBarFunc*/,
39                                                const Standard_Address /*theProgressObject*/)
40 {
41   // gl2psBeginPage() will call OpenGL functions
42   // so we should activate correct GL context before redraw scene call
43   const OpenGl_CView* aCView = (const OpenGl_CView* )theView.ptrView;
44   if (aCView == NULL || !aCView->WS->GetGlContext()->MakeCurrent())
45   {
46     return Standard_False;
47   }
48
49   Standard_Integer aFormat = -1;
50   Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
51   switch (theFormat)
52   {
53     case Graphic3d_EF_PostScript:
54       aFormat = GL2PS_PS;
55       break;
56     case Graphic3d_EF_EnhPostScript:
57       aFormat = GL2PS_EPS;
58       break;
59     case Graphic3d_EF_TEX:
60       aFormat = GL2PS_TEX;
61       break;
62     case Graphic3d_EF_PDF:
63       aFormat = GL2PS_PDF;
64       break;
65     case Graphic3d_EF_SVG:
66       aFormat = GL2PS_SVG;
67       break;
68     case Graphic3d_EF_PGF:
69       aFormat = GL2PS_PGF;
70       break;
71     case Graphic3d_EF_EMF:
72       //aFormat = GL2PS_EMF;
73       aFormat = GL2PS_PGF + 1; // 6
74       break;
75     default:
76       // unsupported format
77       return Standard_False;
78   }
79
80   switch (theSortType)
81   {
82     case Graphic3d_ST_Simple:
83       aSortType = GL2PS_SIMPLE_SORT;
84       break;
85     case Graphic3d_ST_BSP_Tree:
86       aSortType = GL2PS_BSP_SORT;
87       break;
88   }
89
90   GLint aViewport[4];
91   aViewport[0] = 0;
92   aViewport[1] = 0;
93   aViewport[2] = theWidth;
94   aViewport[3] = theHeight;
95
96   GLint aBufferSize = 1024 * 1024;
97   GLint anErrCode = GL2PS_SUCCESS;
98
99   // gl2ps uses standard write functions and do not check locale
100   Standard_CLocaleSentry aLocaleSentry;
101
102   while (aBufferSize > 0)
103   {
104     // current patch for EMF support in gl2ps uses WinAPI functions to create file
105     FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
106     anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
107                     GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
108                     GL_RGBA, 0, NULL,
109                     0, 0, 0, aBufferSize, aFileH, theFileName);
110     if (anErrCode != GL2PS_SUCCESS)
111     {
112       // initialization failed
113       if (aFileH != NULL)
114         fclose (aFileH);
115       break;
116     }
117     Redraw (theView, theLayerUnder, theLayerOver);
118
119     anErrCode = gl2psEndPage();
120     if (aFileH != NULL)
121       fclose (aFileH);
122
123     if (anErrCode == GL2PS_OVERFLOW)
124       aBufferSize *= 2;
125     else
126       break;
127   }
128   return anErrCode == GL2PS_SUCCESS;
129 }
130 #else
131 Standard_Boolean OpenGl_GraphicDriver::Export (const Standard_CString /*theFileName*/,
132                                                const Graphic3d_ExportFormat /*theFormat*/,
133                                                const Graphic3d_SortType /*theSortType*/,
134                                                const Standard_Integer /*theWidth*/,
135                                                const Standard_Integer /*theHeight*/,
136                                                const Graphic3d_CView& /*theView*/,
137                                                const Aspect_CLayer2d& /*theLayerUnder*/,
138                                                const Aspect_CLayer2d& /*theLayerOver*/,
139                                                const Standard_Real    /*thePrecision*/,
140                                                const Standard_Address /*theProgressBarFunc*/,
141                                                const Standard_Address /*theProgressObject*/)
142 {
143     return Standard_False;
144 }
145 #endif