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

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

KOM: Misc DBCS related text fixes

File size: 18.4 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 pfnFt2GetCharacterPlacementW = (PFN_FT2GETCHARACTERPLACEMENTW)QueryProcAddress("Ft2GetCharacterPlacementW");
113 if(!pfnFt2GetCharacterPlacementW) dprintf(("pfnFt2GetCharacterPlacementW not found!!"));
114
115 // Do not register functions for Mozilla plugins
116 if(IsDummyExeLoaded() == FALSE)
117 {
118 pfnFt2RegisterUconv = (PFN_FT2REGISTERUCONV)QueryProcAddress("Ft2RegisterUconv");
119 if(pfnFt2RegisterUconv)
120 pfnFt2RegisterUconv(WideCharToMultiByte, MultiByteToWideChar);
121 else dprintf(("Ft2QueryFontType not found!!"));
122 }
123 dprintf(("Freetype2 library enabled state %d",bEnabled));
124 }
125 return;
126
127failure:
128 if(pfnFt2RegisterUconv)
129 pfnFt2RegisterUconv(NULL, NULL);
130 if(pfnFt2EnableFontEngine)
131 pfnFt2EnableFontEngine(FALSE);
132
133 pfnGetGlyphIndices = NULL;
134 pfnFt2GetTextExtentW = NULL;
135 pfnFt2EnableFontEngine = NULL;
136 pfnFt2GetGlyphOutline = NULL;
137 pfnFt2CharStringPosAtA = NULL;
138 pfnFt2CharStringPosAtW = NULL;
139 pfnFt2GetFontData = NULL;
140 pfnFt2RegisterUconv = NULL;
141 pfnFt2QueryStringWidthW= NULL;
142 pfnFt2GetVersion = NULL;
143 bEnabled = FALSE;
144 if (hftModule) {
145 DosFreeModule(hftModule);
146 hftModule = 0;
147 }
148 return;
149}
150//******************************************************************************
151// Destructor
152//******************************************************************************
153CFT2Module::~CFT2Module()
154{
155 if (hftModule)
156 DosFreeModule(hftModule);
157
158 bEnabled = FALSE;
159}
160//******************************************************************************
161//******************************************************************************
162PFN CFT2Module::QueryProcAddress(int ordinal)
163{
164 APIRET rc;
165 PFN ModuleAddr;
166
167 rc = DosQueryProcAddr(hftModule, ordinal, NULL, &ModuleAddr);
168 if (rc != 0) {
169 dprintf(("FreeType2 QueryProcAddr error: return code = %u\n", rc));
170 return 0;
171 }
172 return ModuleAddr;
173}
174//******************************************************************************
175//******************************************************************************
176PFN CFT2Module::QueryProcAddress(char * procname)
177{
178 APIRET rc;
179 PFN ModuleAddr;
180
181 rc = DosQueryProcAddr(hftModule, 0, procname, &ModuleAddr);
182 if (rc != 0) {
183 dprintf(("FreeType2 QueryProcAddr error: return code = %u\n", rc));
184 return 0;
185 }
186 return ModuleAddr;
187}
188//******************************************************************************
189//******************************************************************************
190DWORD CFT2Module::Ft2GetGlyphIndices(HPS hps, LPCWSTR str, int c, LPWORD pgi, DWORD fl)
191{
192 //no fallback
193 SetLastError(ERROR_CALL_NOT_IMPLEMENTED_W);
194 return GDI_ERROR;
195}
196//******************************************************************************
197//******************************************************************************
198DWORD CFT2Module::Ft2GetGlyphOutline(HPS hps, UINT glyph, UINT format, LPGLYPHMETRICS lpgm, DWORD buflen, LPVOID buf, const MAT2* lpmat)
199{
200 //no fallback
201 SetLastError(ERROR_INVALID_FUNCTION_W);
202 return GDI_ERROR;
203}
204//******************************************************************************
205//
206// This is basically the same as Ft2QueryTextBoxW, but it behaves as the Win32
207// API GetTextExtent (which ignores character attributes and underhang/overhang)
208//
209// The fallback case is not accurate!! (but the same as our old code)
210//
211//******************************************************************************
212BOOL CFT2Module::Ft2GetTextExtentW(HPS hps, LONG lCount1,LPCWSTR pchString, PPOINTLOS2 pwidthHeight)
213{
214 DWORD ret;
215 USHORT sel;
216 POINTLOS2 aptlPoints[TXTBOX_COUNT];
217
218 // All FreeType calls should be wrapped for saving FS
219 if(pfnFt2GetTextExtentW)
220 {
221 sel = RestoreOS2FS();
222 ret = pfnFt2GetTextExtentW(hps, lCount1, pchString, TXTBOX_COUNT, aptlPoints);
223 SetFS(sel);
224 if(ret || (ret == FALSE && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
225 {
226 //No need for scaling for printer DCs here
227 calcDimensions(aptlPoints, pwidthHeight);
228 return ret;
229 }
230 }
231 //else fall back to GPI
232 INT lenA;
233 LPSTR strA;
234 POINTLOS2 start = { 0, 0 };
235 PPOINTLOS2 pplos2;
236 INT cx;
237 INT cy;
238
239 pDCData pHps = (pDCData)OSLibGpiQueryDCData(hps);
240
241 lenA = WideCharToMultiByte( CP_ACP, 0, pchString, lCount1, 0, 0, 0, 0 );
242 strA = ( LPSTR )malloc( lenA + 1 );
243 lstrcpynWtoA( strA, pchString, lenA + 1 );
244 pplos2 = ( PPOINTLOS2 )malloc(( lenA + 1 ) * sizeof( POINTLOS2 ));
245
246 ret = OSLibGpiQueryCharStringPosAt( pHps, &start, 0, lenA, strA, NULL, pplos2 );
247
248 cx = labs( pplos2[ lenA ].x - pplos2[ 0 ].x );
249 cy = labs( pplos2[ lenA ].y - pplos2[ 0 ].y );
250
251 if( ret )
252 {
253 aptlPoints[ TXTBOX_BOTTOMLEFT ].x = 0;
254 aptlPoints[ TXTBOX_BOTTOMLEFT ].y = 0;
255 aptlPoints[ TXTBOX_BOTTOMRIGHT ].x = cx;
256 aptlPoints[ TXTBOX_BOTTOMRIGHT ].y = cy;
257 aptlPoints[ TXTBOX_TOPLEFT ].x = 0;
258 aptlPoints[ TXTBOX_TOPLEFT ].y = 0;
259 aptlPoints[ TXTBOX_TOPRIGHT ].x = cx;
260 aptlPoints[ TXTBOX_TOPRIGHT ].y = cy;
261 aptlPoints[ TXTBOX_CONCAT ].x = cx;
262 aptlPoints[ TXTBOX_CONCAT ].y = cy;
263 }
264
265 calcDimensions(aptlPoints, pwidthHeight);
266
267 TEXTMETRICW tmW;
268 if(GetTextMetricsW( hps, &tmW ) == TRUE)
269 {
270 pwidthHeight->y = tmW.tmHeight; // *Must* use the maximum height of the font
271 }
272#ifdef DEBUG
273 else DebugInt3();
274#endif
275
276 free( pplos2 );
277 free( strA );
278
279 return ret;
280}
281//******************************************************************************
282//******************************************************************************
283BOOL CFT2Module::Ft2CharStringPosAtA(HPS hps,PPOINTLOS2 ptl,PRECTLOS2 rct,ULONG flOptions,LONG lCount,LPCSTR pchString,CONST INT *alAdx, DWORD fuWin32Options)
284{
285 DWORD ret;
286 USHORT sel;
287
288 // All FreeType calls should be wrapped for saving FS
289 if(pfnFt2CharStringPosAtA) {
290 sel = RestoreOS2FS();
291 ret = pfnFt2CharStringPosAtA(hps, ptl,rct,flOptions,lCount,pchString,alAdx, fuWin32Options);
292 SetFS(sel);
293 if(ret || (ret == FALSE && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
294 return ret;
295 }
296 //else fall back to GPI
297 //NOTE: We don't support fuWin32Options in the fallback case
298 pDCData pHps = (pDCData)OSLibGpiQueryDCData(hps);
299 return OSLibGpiCharStringPosAt(pHps,ptl,rct,flOptions,lCount,pchString,alAdx);
300}
301//******************************************************************************
302//******************************************************************************
303BOOL CFT2Module::Ft2CharStringPosAtW(HPS hps, PPOINTLOS2 ptl,PRECTLOS2 rct,ULONG flOptions,LONG lCount,LPCWSTR pchString,CONST INT *alAdx, DWORD fuWin32Options)
304{
305 DWORD ret;
306 USHORT sel;
307
308 // All FreeType calls should be wrapped for saving FS
309 if(pfnFt2CharStringPosAtW) {
310 sel = RestoreOS2FS();
311 ret = pfnFt2CharStringPosAtW(hps, ptl,rct,flOptions,lCount,pchString,alAdx, fuWin32Options);
312 SetFS(sel);
313 if(ret || (ret == FALSE && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
314 return ret;
315 }
316 //else fall back to GPI
317 //NOTE: We don't support fuWin32Options in the fallback case
318 int len;
319 LPSTR astring;
320
321 pDCData pHps = (pDCData)OSLibGpiQueryDCData(hps);
322
323 len = WideCharToMultiByte( CP_ACP, 0, pchString, lCount, 0, 0, NULL, NULL );
324 astring = (char *)malloc( len + 1 );
325 lstrcpynWtoA(astring, pchString, len + 1 );
326
327 ret = OSLibGpiCharStringPosAt(pHps,ptl,rct,flOptions,len,astring,alAdx);
328 free(astring);
329 return ret;
330}
331//******************************************************************************
332//******************************************************************************
333DWORD CFT2Module::Ft2GetFontData(HPS hps, DWORD dwTable, DWORD dwOffset,
334 LPVOID lpvBuffer, DWORD cbData)
335{
336 //no fallback
337 SetLastError(ERROR_CALL_NOT_IMPLEMENTED_W);
338 return(GDI_ERROR);
339}
340//******************************************************************************
341// Query the font type current selected into the presentation space, or,
342// when hps == NULL, query the type of the font with the specified name
343//******************************************************************************
344DWORD CFT2Module::Ft2QueryFontType(HPS hps, LPCSTR lpszFontName)
345{
346 DWORD ret;
347 USHORT sel;
348
349 // All FreeType calls should be wrapped for saving FS
350 if(pfnFt2QueryFontType) {
351 sel = RestoreOS2FS();
352 ret = pfnFt2QueryFontType(hps, lpszFontName);
353 SetFS(sel);
354 if(ret || (ret == GDI_ERROR && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
355 return ret;
356 }
357 //no fallback
358 return FT2_FONTTYPE_UNKNOWN;
359}
360//******************************************************************************
361//******************************************************************************
362BOOL CFT2Module::Ft2GetStringWidthW(HDC hdc, LPWSTR lpszString, UINT cbString, PINT pWidthArray)
363{
364 DWORD ret;
365 USHORT sel;
366 pDCData pHps;
367
368 pHps = (pDCData)OSLibGpiQueryDCData(hdc);
369 if(pHps == NULL) {
370 DebugInt3();
371 SetLastError(ERROR_INVALID_HANDLE_W);
372 return 0;
373 }
374
375 // All FreeType calls should be wrapped for saving FS
376 if(pfnFt2QueryStringWidthW) {
377 sel = RestoreOS2FS();
378 ret = pfnFt2QueryStringWidthW((HPS)hdc, lpszString, cbString, (LONG *)pWidthArray);
379 SetFS(sel);
380 if(ret || (ret == GDI_ERROR && ERRORIDERROR(WinGetLastError(0)) != PMERR_FUNCTION_NOT_SUPPORTED))
381 {
382 if(pHps && pHps->isPrinter && pHps->hdc)
383 {//scale for printer dcs
384 LONG alArray[2];
385
386 if(OSLibDevQueryCaps(pHps, OSLIB_CAPS_HORIZONTAL_RESOLUTION, 2, &alArray[0]) &&
387 alArray[0] != alArray[1])
388 {
389 dprintf(("Different hor/vert resolutions (%d,%d)", alArray[0], alArray[1]));
390 for (int i = 0; i < cbString; i++)
391 {
392 pWidthArray[i] = pWidthArray[i] * alArray[0] / alArray[1];
393 }
394 }
395 }
396 return ret;
397 }
398 }
399 //fallback method
400 int c, i;
401 for (i = 0; i < cbString; i++)
402 {
403 if (GetCharWidth32W(hdc, lpszString[i], lpszString[i], (LPINT)&c)) {
404 dprintf(("%c pWidthArray[%d] = %d", lpszString[i], i, c));
405 pWidthArray[i]= c;
406 }
407 else {
408 dprintf(("WARNING: GetCharWidth32W failed for %c!!!", lpszString[i]));
409 pWidthArray[i] = 0;
410 }
411 }
412 return TRUE;
413}
414/*****************************************************************************
415 * Name : DWORD GetCharacterPlacementW
416 * Purpose : The GetCharacterPlacementW function retrieves information about
417 * a character string, such as character widths, caret positioning,
418 * ordering within the string, and glyph rendering. The type of
419 * information returned depends on the dwFlags parameter and is
420 * based on the currently selected font in the given display context.
421 * The function copies the information to the specified GCP_RESULTSW
422 * structure or to one or more arrays specified by the structure.
423 * Parameters: HDC hdc handle to device context
424 * LPCSTR lpString pointer to string
425 * int nCount number of characters in string
426 * int nMaxExtent maximum extent for displayed string
427 * GCP_RESULTSW *lpResults pointer to buffer for placement result
428 * DWORD dwFlags placement flags
429 * Variables :
430 * Result :
431 * Remark :
432 * Status : Partly working
433 *
434 * Author : Borrowed Rewind Code
435 *****************************************************************************/
436DWORD CFT2Module::Ft2GetCharacterPlacementW(HDC hdc,
437 LPCWSTR lpString,
438 int uCount,
439 int nMaxExtent,
440 GCP_RESULTSW *lpResults,
441 DWORD dwFlags)
442{
443 DWORD ret;
444 USHORT sel;
445 SIZE size;
446 UINT i, nSet;
447 pDCData pHps;
448
449 pHps = (pDCData)OSLibGpiQueryDCData(hdc);
450 if(pHps == NULL) {
451 DebugInt3();
452 SetLastError(ERROR_INVALID_HANDLE_W);
453 return 0;
454 }
455
456 dprintf(("%ls, %d, %d, 0x%08lx\n", lpString, uCount, nMaxExtent, dwFlags));
457
458 dprintf(("lStructSize=%ld, lpOutString=%p, lpOrder=%p, lpDx=%p, lpCaretPos=%p, lpClass=%p, lpGlyphs=%p, nGlyphs=%u, nMaxFit=%d\n",
459 lpResults->lStructSize, lpResults->lpOutString, lpResults->lpOrder,
460 lpResults->lpDx, lpResults->lpCaretPos, lpResults->lpClass,
461 lpResults->lpGlyphs, lpResults->nGlyphs, lpResults->nMaxFit));
462
463 if(dwFlags&(~0)) dprintf(("unsupported flags 0x%08lx\n", dwFlags));
464 if(lpResults->lpCaretPos) dprintf(("caret positions not implemented\n"));
465 if(lpResults->lpClass) dprintf(("classes not implemented\n"));
466
467 nSet = (UINT)uCount;
468 if(nSet > lpResults->nGlyphs)
469 nSet = lpResults->nGlyphs;
470
471 /* return number of initialized fields */
472 lpResults->nGlyphs = nSet;
473 lpResults->nMaxFit = nSet;
474
475 /* Treat the case where no special handling was requested in a fastpath way */
476 /* copy will do if the GCP_REORDER flag is not set */
477 if(lpResults->lpOutString)
478 strncpyW( lpResults->lpOutString, lpString, nSet );
479
480 if(lpResults->lpOrder)
481 for(i = 0; i < nSet; i++)
482 lpResults->lpOrder[i] = i;
483
484 //fallback method
485
486 /* FIXME: Will use the placement chars */
487 if (lpResults->lpDx)
488 {
489 Ft2GetStringWidthW(hdc, (LPWSTR)lpString, nSet, lpResults->lpDx);
490 }
491
492 if(lpResults->lpGlyphs)
493 GetGlyphIndicesW(hdc, lpString, nSet, lpResults->lpGlyphs, 0);
494
495 if (GetTextExtentPoint32W(hdc, lpString, uCount, &size))
496 ret = MAKELONG(size.cx, size.cy);
497 else ret = 0;
498
499 return ret;
500}
501//******************************************************************************
502//******************************************************************************
Note: See TracBrowser for help on using the repository browser.