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

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

KOM: Misc DBCS related text fixes

File size: 18.2 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
245 cx = labs( pplos2[ lenA ].x - pplos2[ 0 ].x );
246 cy = labs( pplos2[ lenA ].y - pplos2[ 0 ].y );
247
248 if( ret )
249 {
250 aptlPoints[ TXTBOX_BOTTOMLEFT ].x = 0;
251 aptlPoints[ TXTBOX_BOTTOMLEFT ].y = 0;
252 aptlPoints[ TXTBOX_BOTTOMRIGHT ].x = cx;
253 aptlPoints[ TXTBOX_BOTTOMRIGHT ].y = cy;
254 aptlPoints[ TXTBOX_TOPLEFT ].x = 0;
255 aptlPoints[ TXTBOX_TOPLEFT ].y = 0;
256 aptlPoints[ TXTBOX_TOPRIGHT ].x = cx;
257 aptlPoints[ TXTBOX_TOPRIGHT ].y = cy;
258 aptlPoints[ TXTBOX_CONCAT ].x = cx;
259 aptlPoints[ TXTBOX_CONCAT ].y = cy;
260 }
261
262 calcDimensions(aptlPoints, pwidthHeight);
263
264 TEXTMETRICW tmW;
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
273 free( pplos2 );
274 free( strA );
275
276 return ret;
277}
278//******************************************************************************
279//******************************************************************************
280BOOL CFT2Module::Ft2CharStringPosAtA(HPS hps,PPOINTLOS2 ptl,PRECTLOS2 rct,ULONG flOptions,LONG lCount,LPCSTR pchString,CONST INT *alAdx, DWORD fuWin32Options)
281{
282 DWORD ret;
283 USHORT sel;
284
285 // All FreeType calls should be wrapped for saving FS
286 if(pfnFt2CharStringPosAtA) {
287 sel = RestoreOS2FS();
288 ret = pfnFt2CharStringPosAtA(hps, ptl,rct,flOptions,lCount,pchString,alAdx, fuWin32Options);
289 SetFS(sel);
290 if(ret || (ret == FALSE && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
291 return ret;
292 }
293 //else fall back to GPI
294 //NOTE: We don't support fuWin32Options in the fallback case
295 pDCData pHps = (pDCData)OSLibGpiQueryDCData(hps);
296 return OSLibGpiCharStringPosAt(pHps,ptl,rct,flOptions,lCount,pchString,alAdx);
297}
298//******************************************************************************
299//******************************************************************************
300BOOL CFT2Module::Ft2CharStringPosAtW(HPS hps, PPOINTLOS2 ptl,PRECTLOS2 rct,ULONG flOptions,LONG lCount,LPCWSTR pchString,CONST INT *alAdx, DWORD fuWin32Options)
301{
302 DWORD ret;
303 USHORT sel;
304
305 // All FreeType calls should be wrapped for saving FS
306 if(pfnFt2CharStringPosAtW) {
307 sel = RestoreOS2FS();
308 ret = pfnFt2CharStringPosAtW(hps, ptl,rct,flOptions,lCount,pchString,alAdx, fuWin32Options);
309 SetFS(sel);
310 if(ret || (ret == FALSE && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
311 return ret;
312 }
313 //else fall back to GPI
314 //NOTE: We don't support fuWin32Options in the fallback case
315 int len;
316 LPSTR astring;
317
318 pDCData pHps = (pDCData)OSLibGpiQueryDCData(hps);
319
320 len = WideCharToMultiByte( CP_ACP, 0, pchString, lCount, 0, 0, NULL, NULL );
321 astring = (char *)malloc( len + 1 );
322 lstrcpynWtoA(astring, pchString, len + 1 );
323
324 ret = OSLibGpiCharStringPosAt(pHps,ptl,rct,flOptions,len,astring,alAdx);
325 free(astring);
326 return ret;
327}
328//******************************************************************************
329//******************************************************************************
330DWORD CFT2Module::Ft2GetFontData(HPS hps, DWORD dwTable, DWORD dwOffset,
331 LPVOID lpvBuffer, DWORD cbData)
332{
333 //no fallback
334 SetLastError(ERROR_CALL_NOT_IMPLEMENTED_W);
335 return(GDI_ERROR);
336}
337//******************************************************************************
338// Query the font type current selected into the presentation space, or,
339// when hps == NULL, query the type of the font with the specified name
340//******************************************************************************
341DWORD CFT2Module::Ft2QueryFontType(HPS hps, LPCSTR lpszFontName)
342{
343 DWORD ret;
344 USHORT sel;
345
346 // All FreeType calls should be wrapped for saving FS
347 if(pfnFt2QueryFontType) {
348 sel = RestoreOS2FS();
349 ret = pfnFt2QueryFontType(hps, lpszFontName);
350 SetFS(sel);
351 if(ret || (ret == GDI_ERROR && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
352 return ret;
353 }
354 //no fallback
355 return FT2_FONTTYPE_UNKNOWN;
356}
357//******************************************************************************
358//******************************************************************************
359BOOL CFT2Module::Ft2GetStringWidthW(HDC hdc, LPWSTR lpszString, UINT cbString, PINT pWidthArray)
360{
361 DWORD ret;
362 USHORT sel;
363 pDCData pHps;
364
365 pHps = (pDCData)OSLibGpiQueryDCData(hdc);
366 if(pHps == NULL) {
367 DebugInt3();
368 SetLastError(ERROR_INVALID_HANDLE_W);
369 return 0;
370 }
371
372 // All FreeType calls should be wrapped for saving FS
373 if(pfnFt2QueryStringWidthW) {
374 sel = RestoreOS2FS();
375 ret = pfnFt2QueryStringWidthW((HPS)hdc, lpszString, cbString, (LONG *)pWidthArray);
376 SetFS(sel);
377 if(ret || (ret == GDI_ERROR && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
378 {
379 if(pHps && pHps->isPrinter && pHps->hdc)
380 {//scale for printer dcs
381 LONG alArray[2];
382
383 if(OSLibDevQueryCaps(pHps, OSLIB_CAPS_HORIZONTAL_RESOLUTION, 2, &alArray[0]) &&
384 alArray[0] != alArray[1])
385 {
386 dprintf(("Different hor/vert resolutions (%d,%d)", alArray[0], alArray[1]));
387 for (int i = 0; i < cbString; i++)
388 {
389 pWidthArray[i] = pWidthArray[i] * alArray[0] / alArray[1];
390 }
391 }
392 }
393 return ret;
394 }
395 }
396 //fallback method
397 int c, i;
398 for (i = 0; i < cbString; i++)
399 {
400 if (GetCharWidth32W(hdc, lpszString[i], lpszString[i], (LPINT)&c)) {
401 dprintf(("%c pWidthArray[%d] = %d", lpszString[i], i, c));
402 pWidthArray[i]= c;
403 }
404 else {
405 dprintf(("WARNING: GetCharWidth32W failed for %c!!!", lpszString[i]));
406 pWidthArray[i] = 0;
407 }
408 }
409 return TRUE;
410}
411/*****************************************************************************
412 * Name : DWORD GetCharacterPlacementW
413 * Purpose : The GetCharacterPlacementW function retrieves information about
414 * a character string, such as character widths, caret positioning,
415 * ordering within the string, and glyph rendering. The type of
416 * information returned depends on the dwFlags parameter and is
417 * based on the currently selected font in the given display context.
418 * The function copies the information to the specified GCP_RESULTSW
419 * structure or to one or more arrays specified by the structure.
420 * Parameters: HDC hdc handle to device context
421 * LPCSTR lpString pointer to string
422 * int nCount number of characters in string
423 * int nMaxExtent maximum extent for displayed string
424 * GCP_RESULTSW *lpResults pointer to buffer for placement result
425 * DWORD dwFlags placement flags
426 * Variables :
427 * Result :
428 * Remark :
429 * Status : Partly working
430 *
431 * Author : Borrowed Rewind Code
432 *****************************************************************************/
433DWORD CFT2Module::Ft2GetCharacterPlacementW(HDC hdc,
434 LPCWSTR lpString,
435 int uCount,
436 int nMaxExtent,
437 GCP_RESULTSW *lpResults,
438 DWORD dwFlags)
439{
440 DWORD ret;
441 USHORT sel;
442 SIZE size;
443 UINT i, nSet;
444 pDCData pHps;
445
446 pHps = (pDCData)OSLibGpiQueryDCData(hdc);
447 if(pHps == NULL) {
448 DebugInt3();
449 SetLastError(ERROR_INVALID_HANDLE_W);
450 return 0;
451 }
452
453 dprintf(("%ls, %d, %d, 0x%08lx\n", lpString, uCount, nMaxExtent, dwFlags));
454
455 dprintf(("lStructSize=%ld, lpOutString=%p, lpOrder=%p, lpDx=%p, lpCaretPos=%p, lpClass=%p, lpGlyphs=%p, nGlyphs=%u, nMaxFit=%d\n",
456 lpResults->lStructSize, lpResults->lpOutString, lpResults->lpOrder,
457 lpResults->lpDx, lpResults->lpCaretPos, lpResults->lpClass,
458 lpResults->lpGlyphs, lpResults->nGlyphs, lpResults->nMaxFit));
459
460 if(dwFlags&(~0)) dprintf(("unsupported flags 0x%08lx\n", dwFlags));
461 if(lpResults->lpCaretPos) dprintf(("caret positions not implemented\n"));
462 if(lpResults->lpClass) dprintf(("classes not implemented\n"));
463
464 nSet = (UINT)uCount;
465 if(nSet > lpResults->nGlyphs)
466 nSet = lpResults->nGlyphs;
467
468 /* return number of initialized fields */
469 lpResults->nGlyphs = nSet;
470 lpResults->nMaxFit = nSet;
471
472 /* Treat the case where no special handling was requested in a fastpath way */
473 /* copy will do if the GCP_REORDER flag is not set */
474 if(lpResults->lpOutString)
475 strncpyW( lpResults->lpOutString, lpString, nSet );
476
477 if(lpResults->lpOrder)
478 for(i = 0; i < nSet; i++)
479 lpResults->lpOrder[i] = i;
480
481 //fallback method
482
483 /* FIXME: Will use the placement chars */
484 if (lpResults->lpDx)
485 {
486 Ft2GetStringWidthW(hdc, (LPWSTR)lpString, nSet, lpResults->lpDx);
487 }
488
489 if(lpResults->lpGlyphs)
490 GetGlyphIndicesW(hdc, lpString, nSet, lpResults->lpGlyphs, 0);
491
492 if (GetTextExtentPoint32W(hdc, lpString, uCount, &size))
493 ret = MAKELONG(size.cx, size.cy);
494 else ret = 0;
495
496 return ret;
497}
498//******************************************************************************
499//******************************************************************************
Note: See TracBrowser for help on using the repository browser.