0023024: Update headers of OCCT files
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Export.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 /************************************************************************/
19 /* Includes                                                             */
20 /************************************************************************/
21
22 #include <OpenGl_GraphicDriver.hxx>
23 #include <OSD_Localizer.hxx>
24
25 #ifdef HAVE_GL2PS
26 #include <gl2ps.h>
27 #endif
28
29 #include <locale.h>
30
31 /************************************************************************/
32 /* Print Methods                                                        */
33 /************************************************************************/
34
35 Standard_Boolean OpenGl_GraphicDriver::Export (const Standard_CString theFileName,
36                                                const Graphic3d_ExportFormat theFormat,
37                                                const Graphic3d_SortType theSortType,
38                                                const Standard_Integer theWidth,
39                                                const Standard_Integer theHeight,
40                                                const Graphic3d_CView& theView,
41                                                const Aspect_CLayer2d& theLayerUnder,
42                                                const Aspect_CLayer2d& theLayerOver,
43                                                const Standard_Real    /*thePrecision*/,
44                                                const Standard_Address /*theProgressBarFunc*/,
45                                                const Standard_Address /*theProgressObject*/)
46 {
47 #ifdef HAVE_GL2PS
48   Standard_Integer aFormat = -1;
49   Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
50   switch (theFormat)
51   {
52     case Graphic3d_EF_PostScript:
53       aFormat = GL2PS_PS;
54       break;
55     case Graphic3d_EF_EnhPostScript:
56       aFormat = GL2PS_EPS;
57       break;
58     case Graphic3d_EF_TEX:
59       aFormat = GL2PS_TEX;
60       break;
61     case Graphic3d_EF_PDF:
62       aFormat = GL2PS_PDF;
63       break;
64     case Graphic3d_EF_SVG:
65       aFormat = GL2PS_SVG;
66       break;
67     case Graphic3d_EF_PGF:
68       aFormat = GL2PS_PGF;
69       break;
70     case Graphic3d_EF_EMF:
71       //aFormat = GL2PS_EMF;
72       aFormat = GL2PS_PGF + 1; // 6
73       break;
74     default:
75       // unsupported format
76       return Standard_False;
77   }
78
79   switch (theSortType)
80   {
81     case Graphic3d_ST_Simple:
82       aSortType = GL2PS_SIMPLE_SORT;
83       break;
84     case Graphic3d_ST_BSP_Tree:
85       aSortType = GL2PS_BSP_SORT;
86       break;
87   }
88
89   GLint aViewport[4];
90   aViewport[0] = 0;
91   aViewport[1] = 0;
92   aViewport[2] = theWidth;
93   aViewport[3] = theHeight;
94
95   GLint aBufferSize = 1024 * 1024;
96   GLint anErrCode = GL2PS_SUCCESS;
97
98   // gl2ps uses standard write functions and do not check locale
99   OSD_Localizer locate (LC_NUMERIC, "C");
100
101   while (aBufferSize > 0)
102   {
103     // current patch for EMF support in gl2ps uses WinAPI functions to create file
104     FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
105     anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
106                     GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
107                     GL_RGBA, 0, NULL,
108                     0, 0, 0, aBufferSize, aFileH, theFileName);
109     if (anErrCode != GL2PS_SUCCESS)
110     {
111       // initialization failed
112       if (aFileH != NULL)
113         fclose (aFileH);
114       break;
115     }
116     Redraw (theView, theLayerUnder, theLayerOver);
117
118     anErrCode = gl2psEndPage();
119     if (aFileH != NULL)
120       fclose (aFileH);
121
122     if (anErrCode == GL2PS_OVERFLOW)
123       aBufferSize *= 2;
124     else
125       break;
126   }
127
128   locate.Restore();
129   return anErrCode == GL2PS_SUCCESS;
130 #else
131   return Standard_False;
132 #endif
133 }