Fixed error handling in DGA_IDirectDraw2Impl_GetCaps().
[wine] / graphics / win16drv / font.c
1 /*
2  * Windows driver font functions
3  *
4  * Copyright 1996 John Harvey
5  *           1998 Huw Davies
6  */
7
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "win16drv.h"
11 #include "module.h"
12 #include "font.h"
13 #include "heap.h"
14 #include "debug.h"
15
16 DEFAULT_DEBUG_CHANNEL(win16drv)
17
18
19 /***********************************************************************
20  *           WIN16DRV_GetTextExtentPoint
21  */
22 BOOL WIN16DRV_GetTextExtentPoint( DC *dc, LPCSTR str, INT count,
23                                     LPSIZE size )
24 {
25     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
26     DWORD dwRet;
27     
28     TRACE(win16drv, "%04x %s %d %p\n",
29                                 dc->hSelf, str, count, size);
30
31     dwRet = PRTDRV_ExtTextOut(physDev->segptrPDEVICE, 0, 0, 
32                               NULL, str, 
33                               -count,  physDev->FontInfo, 
34                               win16drv_SegPtr_DrawMode, 
35                               win16drv_SegPtr_TextXForm, NULL, NULL, 0);
36     size->cx = XDSTOLS(dc,LOWORD(dwRet));
37     size->cy = YDSTOLS(dc,HIWORD(dwRet));
38     TRACE(win16drv, "cx=0x%x, cy=0x%x\n",
39                 size->cx, size->cy );
40     return TRUE;
41 }
42
43
44 /***********************************************************************
45  *           WIN16DRV_GetTextMetrics
46  */
47 BOOL WIN16DRV_GetTextMetrics( DC *dc, TEXTMETRICA *metrics )
48 {
49     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
50
51     TRACE(win16drv, "%04x \n", dc->hSelf);
52
53     FONT_TextMetric16to32A( &physDev->tm, metrics );
54
55     TRACE(win16drv,
56           "H %ld, A %ld, D %ld, Int %ld, Ext %ld, AW %ld, MW %ld, W %ld\n",
57            metrics->tmHeight,
58            metrics->tmAscent,
59            metrics->tmDescent,
60            metrics->tmInternalLeading,
61            metrics->tmExternalLeading,
62            metrics->tmAveCharWidth,
63            metrics->tmMaxCharWidth,
64            metrics->tmWeight);
65
66     return TRUE;
67 }
68
69 HFONT WIN16DRV_FONT_SelectObject( DC * dc, HFONT hfont, FONTOBJ * font)
70 {
71     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
72     HPEN prevHandle = dc->w.hFont;
73     int nSize;
74
75     dc->w.hFont = hfont;
76
77     TRACE(win16drv, "WIN16DRV_FONT_SelectObject '%s' h=%d\n",
78                      font->logfont.lfFaceName, font->logfont.lfHeight);
79
80
81     if( physDev->FontInfo )
82     {
83         TRACE(win16drv, "UnRealizing FontInfo\n");
84         nSize = PRTDRV_RealizeObject (physDev->segptrPDEVICE, -DRVOBJ_FONT,
85                                       physDev->FontInfo,
86                                       physDev->FontInfo, 0);
87     }
88
89     memcpy(&physDev->lf, &font->logfont, sizeof(LOGFONT16));
90     nSize = PRTDRV_RealizeObject (physDev->segptrPDEVICE, DRVOBJ_FONT,
91                                   &physDev->lf, 0, 0); 
92
93     if( physDev->FontInfo && 
94         HeapSize( SegptrHeap, 0, physDev->FontInfo ) < nSize )
95     {
96         SEGPTR_FREE( physDev->FontInfo );
97         physDev->FontInfo = NULL;
98     }
99     
100     if( !physDev->FontInfo )
101         physDev->FontInfo = SEGPTR_ALLOC( nSize );
102
103
104     nSize = PRTDRV_RealizeObject(physDev->segptrPDEVICE, DRVOBJ_FONT,
105                                  &physDev->lf, 
106                                  physDev->FontInfo, 
107                                  win16drv_SegPtr_TextXForm ); 
108
109 #define fi physDev->FontInfo    
110     physDev->tm.tmHeight           = YDSTOLS(dc, fi->dfPixHeight);
111     physDev->tm.tmAscent           = YDSTOLS(dc, fi->dfAscent);
112     physDev->tm.tmDescent          = physDev->tm.tmHeight -
113                                             physDev->tm.tmAscent; 
114     physDev->tm.tmInternalLeading  = YDSTOLS(dc, fi->dfInternalLeading);
115     physDev->tm.tmExternalLeading  = YDSTOLS(dc, fi->dfExternalLeading);
116     physDev->tm.tmAveCharWidth     = XDSTOLS(dc, fi->dfAvgWidth);
117     physDev->tm.tmMaxCharWidth     = XDSTOLS(dc, fi->dfMaxWidth);
118     physDev->tm.tmWeight           = fi->dfWeight;
119     physDev->tm.tmOverhang         = 0; /*FIXME*/
120     physDev->tm.tmDigitizedAspectX = fi->dfHorizRes;
121     physDev->tm.tmDigitizedAspectY = fi->dfVertRes;
122     physDev->tm.tmFirstChar        = fi->dfFirstChar;
123     physDev->tm.tmLastChar         = fi->dfLastChar;
124     physDev->tm.tmDefaultChar      = fi->dfDefaultChar;
125     physDev->tm.tmBreakChar        = fi->dfBreakChar;
126     physDev->tm.tmItalic           = fi->dfItalic;
127     physDev->tm.tmUnderlined       = fi->dfUnderline;
128     physDev->tm.tmStruckOut        = fi->dfStrikeOut;
129     physDev->tm.tmPitchAndFamily   = fi->dfPitchAndFamily;
130     physDev->tm.tmCharSet          = fi->dfCharSet;
131 #undef fi
132
133     TRACE(win16drv, "H %d, A %d, D %d, Int %d, Ext %d, AW %d, MW %d, W %d\n",
134            physDev->tm.tmHeight,
135            physDev->tm.tmAscent,
136            physDev->tm.tmDescent,
137            physDev->tm.tmInternalLeading,
138            physDev->tm.tmExternalLeading,
139            physDev->tm.tmAveCharWidth,
140            physDev->tm.tmMaxCharWidth,
141            physDev->tm.tmWeight);
142
143     return prevHandle;
144 }
145
146 /***********************************************************************
147  *           GetCharWidth32A    (GDI32.155)
148  */
149 BOOL WIN16DRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
150                             LPINT buffer )
151 {
152     int i;
153     WORD wRet;
154
155     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
156     
157     TRACE(win16drv, "%d - %d into %p\n",
158                       firstChar, lastChar, buffer );
159
160     wRet = PRTDRV_GetCharWidth( physDev->segptrPDEVICE, buffer, firstChar, 
161                                 lastChar, physDev->FontInfo, 
162                                 win16drv_SegPtr_DrawMode, 
163                                 win16drv_SegPtr_TextXForm );
164     if( TRACE_ON(win16drv) ){
165         for(i = 0; i <= lastChar - firstChar; i++)
166             TRACE(win16drv, "Char %x: width %d\n", i + firstChar,
167                                          buffer[i]);
168     }
169
170     return wRet;
171 }
172
173 /***********************************************************************
174  *
175  *           WIN16DRV_EnumDeviceFonts
176  */
177
178 BOOL    WIN16DRV_EnumDeviceFonts( DC* dc, LPLOGFONT16 plf, 
179                                         DEVICEFONTENUMPROC proc, LPARAM lp )
180 {
181     WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
182     WORD wRet;
183     WEPFC wepfc;
184     /* EnumDFontCallback is GDI.158 */
185     FARPROC16 pfnCallback = NE_GetEntryPoint( GetModuleHandle16("GDI"), 158 );
186
187     wepfc.proc = proc;
188     wepfc.lp = lp;
189
190     wRet = PRTDRV_EnumDFonts(physDev->segptrPDEVICE, plf->lfFaceName[0] ?
191                              plf->lfFaceName : NULL , pfnCallback , &wepfc );
192     return wRet;
193 }
194
195 /*
196  * EnumCallback (GDI.158)
197  * 
198  * This is the callback function used when EnumDFonts is called. 
199  * (The printer drivers uses it to pass info on available fonts).
200  *
201  * lpvClientData is the pointer passed to EnumDFonts, which points to a WEPFC
202  * structure (WEPFC = WINE_ENUM_PRINTER_FONT_CALLBACK).
203  *
204  */
205
206
207 WORD WINAPI WineEnumDFontCallback(LPENUMLOGFONT16 lpLogFont,
208                                   LPNEWTEXTMETRIC16 lpTextMetrics,
209                                   WORD wFontType, LONG lpClientData) 
210 {
211     TRACE(win16drv, "In WineEnumDFontCallback plf=%p\n", lpLogFont);
212     return (*(((WEPFC *)lpClientData)->proc))( lpLogFont, lpTextMetrics, 
213                                      wFontType, ((WEPFC *)lpClientData)->lp );
214 }
215