source: trunk/src/gdi32/ft2supp.cpp@ 10402

Last change on this file since 10402 was 10402, checked in by sandervl, 22 years ago

logging update

File size: 18.3 KB
Line 
1/*
2 * GDI32 FreeType2 Support Class
3 *
4 * Copyright 2003 Innotek Systemberatung GmbH (sandervl@innotek.de)
5 * (stauff@innotek.de)
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10
11/*****************************************************************************
12 * Includes *
13 *****************************************************************************/
14
15#define INCL_DOS
16#define INCL_GPI
17#define INCL_WIN
18#define INCL_SHLERRORS
19#define INCL_WINERRORS
20#include <os2wrap.h> //Odin32 OS/2 api wrappers
21#include <stdlib.h>
22#include <string.h>
23#include <win32type.h>
24#include <dbglog.h>
25#include <win32api.h>
26#include <winconst.h>
27#include <winnls.h>
28#include <heapstring.h>
29#include <winuser32.h>
30#include <odinlx.h>
31#include "oslibgpi.h"
32
33#include "ft2supp.h"
34
35CFT2Module FT2Module;
36
37static BOOL fFT2LIBIntegration = FALSE;
38
39//******************************************************************************
40//******************************************************************************
41void WIN32API SetFreeTypeIntegration(BOOL fEnabled)
42{
43 fFT2LIBIntegration = fEnabled;
44
45 FT2Module.init();
46}
47//******************************************************************************
48// Constructor
49//******************************************************************************
50CFT2Module::CFT2Module(char* sModuleName): bEnabled(FALSE), hftModule(0), pfnGetGlyphIndices(NULL),
51 pfnFt2GetTextExtentW(NULL), pfnFt2EnableFontEngine(NULL),
52 pfnFt2GetGlyphOutline(NULL), pfnFt2CharStringPosAtA(NULL),
53 pfnFt2CharStringPosAtW(NULL), pfnFt2GetFontData(NULL),
54 pfnFt2RegisterUconv(NULL), pfnFt2QueryStringWidthW(NULL),
55 pfnFt2GetVersion(NULL)
56{
57 pszModuleName = sModuleName; //must be static
58}
59//******************************************************************************
60//******************************************************************************
61void CFT2Module::init()
62{
63 APIRET rc;
64 UCHAR Loaderror[ 256 ];
65 LONG major = 0, minor = 0, buildnr = 0;
66
67 if(!fFT2LIBIntegration) {
68 dprintf(("No FT2LIB integration!!"));
69 goto failure;
70 }
71
72 rc = DosLoadModule((PSZ)Loaderror, sizeof(Loaderror), pszModuleName, &hftModule);
73 if (rc != 0)
74 dprintf(("Freetype2 library load error: return code = %u\n", rc));
75 else
76 bEnabled = TRUE;
77
78 if(bEnabled) {
79 pfnFt2GetVersion = (PFN_FT2GETVERSION)QueryProcAddress("Ft2GetVersion");
80 if(!pfnFt2GetVersion) {
81 dprintf(("Ft2GetVersion not found!!"));
82 goto failure;
83 }
84 pfnFt2GetVersion(&major, &minor, &buildnr);
85 if(major != FT2LIB_MAJOR_VERSION) {
86 dprintf(("Incorrect major version %d; expected %d!!", major, FT2LIB_MAJOR_VERSION));
87 goto failure;
88 }
89 if(minor < FT2LIB_MINOR_VERSION) {
90 dprintf(("Incorrect minor version %d; expected >= %d!!", minor, FT2LIB_MINOR_VERSION));
91 goto failure;
92 }
93
94 pfnFt2EnableFontEngine = (PFN_FT2ENABLEFONTENGINE)QueryProcAddress("Ft2EnableFontEngine");
95 if(!pfnFt2EnableFontEngine) dprintf(("Ft2EnableFontEngine not found!!"));
96 else pfnFt2EnableFontEngine(bEnabled);
97
98 pfnFt2GetTextExtentW = (PFN_FT2GETTEXTEXTENTW)QueryProcAddress("Ft2GetTextExtentW");
99 if(!pfnFt2GetTextExtentW) dprintf(("Ft2GetTextExtentW not found!!"));
100
101 pfnFt2CharStringPosAtA = (PFN_FT2CHARSTRINGPOSATA)QueryProcAddress("Ft2CharStringPosAtA");
102 if(!pfnFt2CharStringPosAtA) dprintf(("Ft2CharStringPosAtA not found!!"));
103 pfnFt2CharStringPosAtW = (PFN_FT2CHARSTRINGPOSATW)QueryProcAddress("Ft2CharStringPosAtW");
104 if(!pfnFt2CharStringPosAtW) dprintf(("Ft2CharStringPosAtW not found!!"));
105
106 pfnFt2QueryFontType = (PFN_FT2QUERYFONTTYPE)QueryProcAddress("Ft2QueryFontType");
107 if(!pfnFt2QueryFontType) dprintf(("Ft2QueryFontType not found!!"));
108
109 pfnFt2QueryStringWidthW = (PFN_FT2QUERYSTRINGWIDTHW)QueryProcAddress("Ft2QueryStringWidthW");
110 if(!pfnFt2QueryStringWidthW) dprintf(("Ft2QueryStringWidthW not found!!"));
111
112 // Do not register functions for Mozilla plugins
113 if(IsDummyExeLoaded() == FALSE)
114 {
115 pfnFt2RegisterUconv = (PFN_FT2REGISTERUCONV)QueryProcAddress("Ft2RegisterUconv");
116 if(pfnFt2RegisterUconv)
117 pfnFt2RegisterUconv(WideCharToMultiByte, MultiByteToWideChar);
118 else dprintf(("Ft2QueryFontType not found!!"));
119 }
120 dprintf(("Freetype2 library enabled state %d",bEnabled));
121 }
122 return;
123
124failure:
125 if(pfnFt2RegisterUconv)
126 pfnFt2RegisterUconv(NULL, NULL);
127 if(pfnFt2EnableFontEngine)
128 pfnFt2EnableFontEngine(FALSE);
129
130 pfnGetGlyphIndices = NULL;
131 pfnFt2GetTextExtentW = NULL;
132 pfnFt2EnableFontEngine = NULL;
133 pfnFt2GetGlyphOutline = NULL;
134 pfnFt2CharStringPosAtA = NULL;
135 pfnFt2CharStringPosAtW = NULL;
136 pfnFt2GetFontData = NULL;
137 pfnFt2RegisterUconv = NULL;
138 pfnFt2QueryStringWidthW= NULL;
139 pfnFt2GetVersion = NULL;
140 bEnabled = FALSE;
141 if (hftModule) {
142 DosFreeModule(hftModule);
143 hftModule = 0;
144 }
145 return;
146}
147//******************************************************************************
148// Destructor
149//******************************************************************************
150CFT2Module::~CFT2Module()
151{
152 if (hftModule)
153 DosFreeModule(hftModule);
154
155 bEnabled = FALSE;
156}
157//******************************************************************************
158//******************************************************************************
159PFN CFT2Module::QueryProcAddress(int ordinal)
160{
161 APIRET rc;
162 PFN ModuleAddr;
163
164 rc = DosQueryProcAddr(hftModule, ordinal, NULL, &ModuleAddr);
165 if (rc != 0) {
166 dprintf(("FreeType2 QueryProcAddr error: return code = %u\n", rc));
167 return 0;
168 }
169 return ModuleAddr;
170}
171//******************************************************************************
172//******************************************************************************
173PFN CFT2Module::QueryProcAddress(char * procname)
174{
175 APIRET rc;
176 PFN ModuleAddr;
177
178 rc = DosQueryProcAddr(hftModule, 0, procname, &ModuleAddr);
179 if (rc != 0) {
180 dprintf(("FreeType2 QueryProcAddr error: return code = %u\n", rc));
181 return 0;
182 }
183 return ModuleAddr;
184}
185//******************************************************************************
186//******************************************************************************
187DWORD CFT2Module::Ft2GetGlyphIndices(HPS hps, LPCWSTR str, int c, LPWORD pgi, DWORD fl)
188{
189 //no fallback
190 SetLastError(ERROR_CALL_NOT_IMPLEMENTED_W);
191 return GDI_ERROR;
192}
193//******************************************************************************
194//******************************************************************************
195DWORD CFT2Module::Ft2GetGlyphOutline(HPS hps, UINT glyph, UINT format, LPGLYPHMETRICS lpgm, DWORD buflen, LPVOID buf, const MAT2* lpmat)
196{
197 //no fallback
198 SetLastError(ERROR_INVALID_FUNCTION_W);
199 return GDI_ERROR;
200}
201//******************************************************************************
202//
203// This is basically the same as Ft2QueryTextBoxW, but it behaves as the Win32
204// API GetTextExtent (which ignores character attributes and underhang/overhang)
205//
206// The fallback case is not accurate!! (but the same as our old code)
207//
208//******************************************************************************
209BOOL CFT2Module::Ft2GetTextExtentW(HPS hps, LONG lCount1,LPCWSTR pchString, PPOINTLOS2 pwidthHeight)
210{
211 DWORD ret;
212 USHORT sel;
213 POINTLOS2 aptlPoints[TXTBOX_COUNT];
214
215 // All FreeType calls should be wrapped for saving FS
216 if(pfnFt2GetTextExtentW)
217 {
218 sel = RestoreOS2FS();
219 ret = pfnFt2GetTextExtentW(hps, lCount1, pchString, TXTBOX_COUNT, aptlPoints);
220 SetFS(sel);
221 if(ret || (ret == FALSE && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
222 {
223 //No need for scaling for printer DCs here
224 calcDimensions(aptlPoints, pwidthHeight);
225 return ret;
226 }
227 }
228 //else fall back to GPI
229 INT lenA;
230 LPSTR strA;
231 POINTLOS2 start = { 0, 0 };
232 PPOINTLOS2 pplos2;
233 INT cx;
234 INT cy;
235
236 pDCData pHps = (pDCData)OSLibGpiQueryDCData(hps);
237
238 lenA = WideCharToMultiByte( CP_ACP, 0, pchString, lCount1, 0, 0, 0, 0 );
239 strA = ( LPSTR )malloc( lenA + 1 );
240 lstrcpynWtoA( strA, pchString, lenA + 1 );
241 pplos2 = ( PPOINTLOS2 )malloc(( lenA + 1 ) * sizeof( POINTLOS2 ));
242
243 ret = OSLibGpiQueryCharStringPosAt( pHps, &start, 0, lenA, strA, NULL, pplos2 );
244 if( ret )
245 {
246 TEXTMETRICW tmW;
247
248 cx = labs( pplos2[ lenA ].x - pplos2[ 0 ].x );
249 cy = labs( pplos2[ lenA ].y - pplos2[ 0 ].y );
250
251 aptlPoints[ TXTBOX_BOTTOMLEFT ].x = 0;
252 aptlPoints[ TXTBOX_BOTTOMLEFT ].y = 0;
253 aptlPoints[ TXTBOX_BOTTOMRIGHT ].x = cx;
254 aptlPoints[ TXTBOX_BOTTOMRIGHT ].y = cy;
255 aptlPoints[ TXTBOX_TOPLEFT ].x = 0;
256 aptlPoints[ TXTBOX_TOPLEFT ].y = 0;
257 aptlPoints[ TXTBOX_TOPRIGHT ].x = cx;
258 aptlPoints[ TXTBOX_TOPRIGHT ].y = cy;
259 aptlPoints[ TXTBOX_CONCAT ].x = cx;
260 aptlPoints[ TXTBOX_CONCAT ].y = cy;
261
262 calcDimensions(aptlPoints, pwidthHeight);
263
264 DecreaseLogCount();
265 if(GetTextMetricsW( hps, &tmW ) == TRUE)
266 {
267 pwidthHeight->y = tmW.tmHeight; // *Must* use the maximum height of the font
268 }
269#ifdef DEBUG
270 else DebugInt3();
271#endif
272 IncreaseLogCount();
273 }
274
275 free( pplos2 );
276 free( strA );
277
278 return ret;
279}
280//******************************************************************************
281//******************************************************************************
282BOOL CFT2Module::Ft2CharStringPosAtA(HPS hps,PPOINTLOS2 ptl,PRECTLOS2 rct,ULONG flOptions,LONG lCount,LPCSTR pchString,CONST INT *alAdx, DWORD fuWin32Options)
283{
284 DWORD ret;
285 USHORT sel;
286
287 // All FreeType calls should be wrapped for saving FS
288 if(pfnFt2CharStringPosAtA) {
289 sel = RestoreOS2FS();
290 ret = pfnFt2CharStringPosAtA(hps, ptl,rct,flOptions,lCount,pchString,alAdx, fuWin32Options);
291 SetFS(sel);
292 if(ret || (ret == FALSE && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
293 return ret;
294 }
295 //else fall back to GPI
296 //NOTE: We don't support fuWin32Options in the fallback case
297 pDCData pHps = (pDCData)OSLibGpiQueryDCData(hps);
298 return OSLibGpiCharStringPosAt(pHps,ptl,rct,flOptions,lCount,pchString,alAdx);
299}
300//******************************************************************************
301//******************************************************************************
302BOOL CFT2Module::Ft2CharStringPosAtW(HPS hps, PPOINTLOS2 ptl,PRECTLOS2 rct,ULONG flOptions,LONG lCount,LPCWSTR pchString,CONST INT *alAdx, DWORD fuWin32Options)
303{
304 DWORD ret;
305 USHORT sel;
306
307 // All FreeType calls should be wrapped for saving FS
308 if(pfnFt2CharStringPosAtW) {
309 sel = RestoreOS2FS();
310 ret = pfnFt2CharStringPosAtW(hps, ptl,rct,flOptions,lCount,pchString,alAdx, fuWin32Options);
311 SetFS(sel);
312 if(ret || (ret == FALSE && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
313 return ret;
314 }
315 //else fall back to GPI
316 //NOTE: We don't support fuWin32Options in the fallback case
317 int len;
318 LPSTR astring;
319
320 pDCData pHps = (pDCData)OSLibGpiQueryDCData(hps);
321
322 len = WideCharToMultiByte( CP_ACP, 0, pchString, lCount, 0, 0, NULL, NULL );
323 astring = (char *)malloc( len + 1 );
324 lstrcpynWtoA(astring, pchString, len + 1 );
325
326 ret = OSLibGpiCharStringPosAt(pHps,ptl,rct,flOptions,len,astring,alAdx);
327 free(astring);
328 return ret;
329}
330//******************************************************************************
331//******************************************************************************
332DWORD CFT2Module::Ft2GetFontData(HPS hps, DWORD dwTable, DWORD dwOffset,
333 LPVOID lpvBuffer, DWORD cbData)
334{
335 //no fallback
336 SetLastError(ERROR_CALL_NOT_IMPLEMENTED_W);
337 return(GDI_ERROR);
338}
339//******************************************************************************
340// Query the font type current selected into the presentation space, or,
341// when hps == NULL, query the type of the font with the specified name
342//******************************************************************************
343DWORD CFT2Module::Ft2QueryFontType(HPS hps, LPCSTR lpszFontName)
344{
345 DWORD ret;
346 USHORT sel;
347
348 // All FreeType calls should be wrapped for saving FS
349 if(pfnFt2QueryFontType) {
350 sel = RestoreOS2FS();
351 ret = pfnFt2QueryFontType(hps, lpszFontName);
352 SetFS(sel);
353 if(ret || (ret == GDI_ERROR && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
354 return ret;
355 }
356 //no fallback
357 return FT2_FONTTYPE_UNKNOWN;
358}
359//******************************************************************************
360//******************************************************************************
361BOOL CFT2Module::Ft2GetStringWidthW(HDC hdc, LPWSTR lpszString, UINT cbString, PINT pWidthArray)
362{
363 DWORD ret;
364 USHORT sel;
365 pDCData pHps;
366
367 pHps = (pDCData)OSLibGpiQueryDCData(hdc);
368 if(pHps == NULL) {
369 DebugInt3();
370 SetLastError(ERROR_INVALID_HANDLE_W);
371 return 0;
372 }
373
374 // All FreeType calls should be wrapped for saving FS
375 if(pfnFt2QueryStringWidthW) {
376 sel = RestoreOS2FS();
377 ret = pfnFt2QueryStringWidthW((HPS)hdc, lpszString, cbString, (LONG *)pWidthArray);
378 SetFS(sel);
379 if(ret || (ret == GDI_ERROR && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
380 {
381 if(pHps && pHps->isPrinter && pHps->hdc)
382 {//scale for printer dcs
383 LONG alArray[2];
384
385 if(OSLibDevQueryCaps(pHps, OSLIB_CAPS_HORIZONTAL_RESOLUTION, 2, &alArray[0]) &&
386 alArray[0] != alArray[1])
387 {
388 dprintf(("Different hor/vert resolutions (%d,%d)", alArray[0], alArray[1]));
389 for (int i = 0; i < cbString; i++)
390 {
391 pWidthArray[i] = pWidthArray[i] * alArray[0] / alArray[1];
392 }
393 }
394 }
395 return ret;
396 }
397 }
398 //fallback method
399 int c, i;
400 for (i = 0; i < cbString; i++)
401 {
402 if (GetCharWidth32W(hdc, lpszString[i], lpszString[i], (LPINT)&c)) {
403 dprintf(("%c pWidthArray[%d] = %d", lpszString[i], i, c));
404 pWidthArray[i]= c;
405 }
406 else {
407 dprintf(("WARNING: GetCharWidth32W failed for %c!!!", lpszString[i]));
408 pWidthArray[i] = 0;
409 }
410 }
411 return TRUE;
412}
413/*****************************************************************************
414 * Name : DWORD GetCharacterPlacementW
415 * Purpose : The GetCharacterPlacementW function retrieves information about
416 * a character string, such as character widths, caret positioning,
417 * ordering within the string, and glyph rendering. The type of
418 * information returned depends on the dwFlags parameter and is
419 * based on the currently selected font in the given display context.
420 * The function copies the information to the specified GCP_RESULTSW
421 * structure or to one or more arrays specified by the structure.
422 * Parameters: HDC hdc handle to device context
423 * LPCSTR lpString pointer to string
424 * int nCount number of characters in string
425 * int nMaxExtent maximum extent for displayed string
426 * GCP_RESULTSW *lpResults pointer to buffer for placement result
427 * DWORD dwFlags placement flags
428 * Variables :
429 * Result :
430 * Remark :
431 * Status : Partly working
432 *
433 * Author : Borrowed Rewind Code
434 *****************************************************************************/
435DWORD CFT2Module::Ft2GetCharacterPlacementW(HDC hdc,
436 LPCWSTR lpString,
437 int uCount,
438 int nMaxExtent,
439 GCP_RESULTSW *lpResults,
440 DWORD dwFlags)
441{
442 DWORD ret;
443 USHORT sel;
444 SIZE size;
445 UINT i, nSet;
446 pDCData pHps;
447
448 pHps = (pDCData)OSLibGpiQueryDCData(hdc);
449 if(pHps == NULL) {
450 DebugInt3();
451 SetLastError(ERROR_INVALID_HANDLE_W);
452 return 0;
453 }
454
455 dprintf(("%ls, %d, %d, 0x%08lx\n", lpString, uCount, nMaxExtent, dwFlags));
456
457 dprintf(("lStructSize=%ld, lpOutString=%p, lpOrder=%p, lpDx=%p, lpCaretPos=%p, lpClass=%p, lpGlyphs=%p, nGlyphs=%u, nMaxFit=%d\n",
458 lpResults->lStructSize, lpResults->lpOutString, lpResults->lpOrder,
459 lpResults->lpDx, lpResults->lpCaretPos, lpResults->lpClass,
460 lpResults->lpGlyphs, lpResults->nGlyphs, lpResults->nMaxFit));
461
462 if(dwFlags&(~0)) dprintf(("unsupported flags 0x%08lx\n", dwFlags));
463 if(lpResults->lpCaretPos) dprintf(("caret positions not implemented\n"));
464 if(lpResults->lpClass) dprintf(("classes not implemented\n"));
465
466 nSet = (UINT)uCount;
467 if(nSet > lpResults->nGlyphs)
468 nSet = lpResults->nGlyphs;
469
470 /* return number of initialized fields */
471 lpResults->nGlyphs = nSet;
472 lpResults->nMaxFit = nSet;
473
474 /* Treat the case where no special handling was requested in a fastpath way */
475 /* copy will do if the GCP_REORDER flag is not set */
476 if(lpResults->lpOutString)
477 strncpyW( lpResults->lpOutString, lpString, nSet );
478
479 if(lpResults->lpOrder)
480 for(i = 0; i < nSet; i++)
481 lpResults->lpOrder[i] = i;
482
483 //fallback method
484
485 /* FIXME: Will use the placement chars */
486 if (lpResults->lpDx)
487 {
488 Ft2GetStringWidthW(hdc, (LPWSTR)lpString, nSet, lpResults->lpDx);
489 }
490
491 if(lpResults->lpGlyphs)
492 GetGlyphIndicesW(hdc, lpString, nSet, lpResults->lpGlyphs, 0);
493
494 if (GetTextExtentPoint32W(hdc, lpString, uCount, &size))
495 ret = MAKELONG(size.cx, size.cy);
496 else ret = 0;
497
498 return ret;
499}
500//******************************************************************************
501//******************************************************************************
Note: See TracBrowser for help on using the repository browser.