1 /* File OpenGL_togl_print.c
4 e-mail t-hartl@muenchen.matra-dtv.fr */
6 #define RIC120302 /* GG Enable to use the application display
7 // callback at end of traversal
10 #include <OpenGl_tgl_all.hxx>
11 #include <InterfaceGraphic_Graphic3d.hxx>
12 #include <InterfaceGraphic_Visual3d.hxx>
13 #include <OpenGl_tsm_ws.hxx>
14 #include <OpenGl_tgl.hxx>
15 #include <OpenGl_txgl.hxx>
16 #include <OpenGl_tgl_tox.hxx>
17 #include <OpenGl_tgl_funcs.hxx>
18 #include <OpenGl_tgl_subrvis.hxx>
19 #include <OpenGl_animation.hxx>
24 /* MSDN says: point is 1/72 inch. But in our case text height in the 3D view
25 differs from printed one. An experiment showed that delimeter equal to 2*72 gives
26 practically equal text heights. */
27 int defaultDelimeter = 72;
29 int defaultPntSize = 12;
31 GLuint printerFontBase = 0;
33 /* printer DC needed to avoid passing OS specific type as a procedure parameter */
39 GLuint createFont( char* typeFace, int height, int weight, int italic )
42 /* no unicode support yet*/
45 DWORD charSet = ANSI_CHARSET;
47 if ( ( base = glGenLists( 96 ) ) == 0 )
50 if ( _stricmp( typeFace, "symbol" ) == 0 )
51 charSet = SYMBOL_CHARSET;
53 font = CreateFont( height, 0, 0, 0, weight, italic, FALSE, FALSE,
54 charSet, OUT_TT_PRECIS,
55 CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,
56 DEFAULT_PITCH, typeFace );
57 oldObj = SelectObject( printer, font );
58 wglUseFontBitmaps( printer, 32, 96, base );
64 void deleteFont( GLuint base )
72 /* deleting font list id */
73 glDeleteLists( base, 96 );
74 currentFont = (HFONT)SelectObject( printer, oldObj );
75 /* deleting current font structure */
76 DeleteObject( currentFont );
80 void updatePrinterFont( char* type, int height )
83 int delim = delimeter;
85 height = defaultPntSize;
86 delim = defaultDelimeter;
88 /* deleting old font */
89 deleteFont( printerFontBase );
90 /* creating new one */
91 printerFontBase = createFont( type, -MulDiv( height, GetDeviceCaps( printer, LOGPIXELSY ), delim ),
101 CALL_DEF_VIEW *aview,
102 CALL_DEF_LAYER *anunderlayer,
103 CALL_DEF_LAYER *anoverlayer,
104 const Aspect_Drawable hPrintDC,
105 const int background,
113 Tint swap = 1; /* swap buffers ? yes */
120 hPrnDC = (HDC) hPrintDC;
123 /* Begin main routine **************************************************************/
124 TsmGetWSAttri (aview->WsId, WSWindow, &data);
125 if (TxglWinset (call_thedisplay, (WINDOW) data.ldata) == TSuccess)
127 static PIXELFORMATDESCRIPTOR pfd = {
128 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
133 0, 0, 0, 0, 0, 0, // color bits ignored
134 0, // no alpha buffer
135 0, // shift bit ignored
136 0, // no accumulation buffer
137 0, 0, 0, 0, // accum bits ignored
138 32, // 32-bit z-buffer
139 0, // no stencil buffer
140 0, // no auxiliary buffer
141 PFD_MAIN_PLANE, // main layer
143 0, 0, 0 // layer masks ignored
152 BITMAPINFOHEADER* bmHeader;
154 BYTE biInfo[sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD)];
176 tempWidth = (float) devWidth;
178 tempHeight = (float) devHeight;
180 devWidth = GetDeviceCaps(hPrnDC, HORZRES);
181 tempWidth = (float) devWidth;
182 devHeight = GetDeviceCaps(hPrnDC, VERTRES);
183 tempHeight = (float) devHeight;
185 GetClientRect((WINDOW) data.ldata, &rect);
186 viewWidth = rect.right-rect.left;
187 viewHeight = rect.bottom-rect.top;
189 viewRatio = (float) viewWidth/(float) viewHeight;
191 // Calculate correct width/height ratio
192 if (tempHeight < tempWidth/viewRatio)
193 tempWidth = tempHeight*viewRatio;
194 if (tempWidth < tempHeight*viewRatio)
195 tempHeight = tempWidth/viewRatio;
197 width = (int) tempWidth;
198 height = (int) tempHeight;
200 // Create virtual window
204 WS_OVERLAPPEDWINDOW |
214 ShowWindow(w, SW_SHOW);
220 MessageBox(0,"hDC == NULL", "Fehler", MB_OK);
224 // Initialize Bitmap Information
226 pBMI = (BITMAPINFO *) biInfo;
227 ZeroMemory(pBMI, sizeof(*pBMI));
228 bmiSize = sizeof(*pBMI);
230 pBMI = (BITMAPINFO *) calloc(1, bmiSize);
231 bmHeader = &pBMI->bmiHeader;
233 bmHeader->biSize = sizeof(*bmHeader);
234 bmHeader->biWidth = width;
235 bmHeader->biHeight = height;
236 bmHeader->biPlanes = 1; /* must be 1 */
237 bmHeader->biBitCount = 24;
238 bmHeader->biXPelsPerMeter = 0;
239 bmHeader->biYPelsPerMeter = 0;
240 bmHeader->biClrUsed = 0; /* all are used */
241 bmHeader->biClrImportant = 0; /* all are important */
242 bmHeader->biCompression = BI_RGB;
243 bmHeader->biSizeImage = 0;
245 // Create Device Independent Bitmap
246 hMemDC = CreateCompatibleDC(hPrnDC);
247 hBm = CreateDIBSection(hMemDC, pBMI, DIB_RGB_COLORS, &base, NULL, 0);
248 hBmOld = SelectObject(hMemDC, hBm);
254 // further initialization
256 pfd.cColorBits = GetDeviceCaps(hDC, BITSPIXEL);
257 nIndex = ChoosePixelFormat(hDC, &pfd);
260 MessageBox(0,"ChoosePixelFormat failed", "Error", MB_OK | MB_ICONSTOP);
265 if (!SetPixelFormat(hDC, nIndex, &pfd))
267 MessageBox(0,"SetPixelFormat failed", "Error", MB_OK | MB_ICONSTOP);
271 // Create Rendering Context
272 hGLRC = wglCreateContext(hDC);
276 MessageBox(0,"No Rendering Context", "Error", MB_OK | MB_ICONSTOP);
279 wglMakeCurrent(hDC,hGLRC);
281 pfd.cColorBits = GetDeviceCaps(hMemDC, BITSPIXEL);
282 nIndex = ChoosePixelFormat(hMemDC, &pfd);
285 MessageBox(0,"ChoosePixelFormat failed", "Error", MB_OK | MB_ICONSTOP);
290 if (!SetPixelFormat(hMemDC, nIndex, &pfd))
292 MessageBox(0,"SetPixelFormat failed", "Error", MB_OK | MB_ICONSTOP);
296 // Create Rendering Context
297 hGLRC = wglCreateContext(hMemDC);
301 MessageBox(0,"No Rendering Context", "Error", MB_OK | MB_ICONSTOP);
304 wglMakeCurrent(hMemDC,hGLRC);
307 /* creating default font */
309 createFont( "", -MulDiv( defaultPntSize, GetDeviceCaps( hPrnDC, LOGPIXELSY ), defaultDelimeter ),
312 // redraw to new Rendering Context
313 call_func_redraw_all_structs_begin (aview->WsId);
317 glClearColor(1.0, 1.0, 1.0, 1.0);
318 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
321 if (anunderlayer->ptrLayer)
322 call_togl_redraw_layer2d (aview, anunderlayer);
323 call_func_redraw_all_structs_proc (aview->WsId);
325 if (anoverlayer->ptrLayer)
326 call_togl_redraw_layer2d (aview, anoverlayer);
328 call_subr_displayCB(aview,OCC_REDRAW_BITMAP);
331 call_func_redraw_all_structs_end (aview->WsId, swap);
333 call_togl_redraw_immediat_mode (aview);
335 /* Start printing of DIB ********************************************************/
336 di.cbSize = sizeof(DOCINFO);
337 di.lpszDocName = "Text";
338 di.lpszOutput = filename;
340 if (StartDoc(hPrnDC, &di) == SP_ERROR) goto Error;
341 if (StartPage(hPrnDC) <= 0) goto Error;
343 // calculate offset for centered printing
344 if (width < devWidth)
345 offsetx = (devWidth - width)/2;
346 if (height < devHeight)
347 offsety = (devHeight - height)/2;
349 BitBlt(hPrnDC, offsetx, offsety, width, height, hMemDC, 0, 0, SRCCOPY);
353 /* releasing created font */
354 deleteFont( printerFontBase );
357 /* End of printing section ******************************************************/
363 /* Clean memory *****************************************************************/
366 SelectObject(hMemDC, hBmOld);
368 DeleteObject(hBmOld);
370 wglMakeCurrent(NULL, NULL);
373 wglDeleteContext(hGLRC);
389 /* End of clean memory *****************************************************************/
392 /* End main routine ********************************************************************/