usp10: Tidy default values.
[wine/wine-gecko.git] / dlls / usp10 / usp10.c
blobc02689de06370012a2243c5bc36968252194ee20
1 /*
2 * Implementation of Uniscribe Script Processor (usp10.dll)
4 * Copyright 2005 Steven Edwards for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Notes:
21 * Uniscribe allows for processing of complex scripts such as joining
22 * and filtering characters and bi-directional text with custom line breaks.
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "usp10.h"
33 #include "wine/debug.h"
35 /**
36 * some documentation here:
37 * http://www.microsoft.com/typography/developers/uniscribe/uniscribe.htm
40 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
42 #define MAX_SCRIPTS 8
44 /* Set up a default for ScriptGetProperties */
45 static const SCRIPT_PROPERTIES Default_Script_0 = {0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0};
47 static const SCRIPT_PROPERTIES Default_Script_1 = {0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0};
49 static const SCRIPT_PROPERTIES Default_Script_2 = {0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0};
51 static const SCRIPT_PROPERTIES Default_Script_3 = {9, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0};
53 static const SCRIPT_PROPERTIES Default_Script_4 = {9, 1, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0};
55 static const SCRIPT_PROPERTIES Default_Script_5 = {9, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 1, 0, 0};
57 static const SCRIPT_PROPERTIES Default_Script_6 = {9, 1, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 1, 0, 0};
59 static const SCRIPT_PROPERTIES Default_Script_7 = {8, 0, 0, 0, 0, 161, 0, 0,
60 0, 0, 0, 0, 0, 0, 0};
61 static const SCRIPT_PROPERTIES *Global_Script[MAX_SCRIPTS] =
62 {&Default_Script_0,
63 &Default_Script_1,
64 &Default_Script_2,
65 &Default_Script_3,
66 &Default_Script_4,
67 &Default_Script_5,
68 &Default_Script_6,
69 &Default_Script_7};
71 typedef struct scriptcache {
72 HDC hdc;
73 DWORD GlyphToChar[256];
74 int HaveWidths;
75 ABC CharWidths[256];
76 } Scriptcache;
78 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
80 switch(fdwReason) {
81 case DLL_PROCESS_ATTACH:
82 DisableThreadLibraryCalls(hInstDLL);
83 break;
84 case DLL_PROCESS_DETACH:
85 break;
87 return TRUE;
90 /***********************************************************************
91 * ScriptFreeCache (USP10.@)
94 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
96 TRACE("%p\n", psc);
98 if (psc) {
99 HeapFree ( GetProcessHeap(), 0, *psc);
100 *psc = NULL;
102 return 0;
105 /***********************************************************************
106 * ScriptGetProperties (USP10.@)
109 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***ppSp, int *piNumScripts)
111 TRACE("%p,%p\n",ppSp, piNumScripts);
113 /* Set up a sensible default and intialise pointers */
114 *piNumScripts = MAX_SCRIPTS;
115 *ppSp = Global_Script;
116 TRACE("ppSp:%p, *ppSp:%p, **ppSp:%p, %d\n", ppSp, *ppSp, **ppSp,
117 *piNumScripts);
118 return 0;
121 /***********************************************************************
122 * ScriptGetFontProperties (USP10.@)
125 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
127 FIXME("%p,%p,%p\n", hdc, psc, sfp);
128 /* return something sensible? */
129 if (NULL != sfp) {
130 sfp->cBytes = sizeof(SCRIPT_FONTPROPERTIES);
131 sfp->wgBlank = 0;
132 sfp->wgDefault = 0;
133 sfp->wgInvalid = 0;
134 sfp->wgKashida = 1;
135 sfp->iKashidaWidth = 0;
137 return 0;
140 /***********************************************************************
141 * ScriptRecordDigitSubstitution (USP10.@)
144 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID Locale,SCRIPT_DIGITSUBSTITUTE *psds)
146 FIXME("%ld,%p\n",Locale,psds);
147 return E_NOTIMPL;
150 /***********************************************************************
151 * ScriptApplyDigitSubstitution (USP10.@)
154 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE* psds,
155 SCRIPT_CONTROL* psc, SCRIPT_STATE* pss)
157 FIXME("%p,%p,%p\n",psds,psc,pss);
158 return E_NOTIMPL;
161 /***********************************************************************
162 * ScriptItemize (USP10.@)
165 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
166 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
167 SCRIPT_ITEM *pItems, int *pcItems)
169 /* This implementation currently treats the entire string represented in
170 * pwcInChars as a single entity. Hence pcItems will be set to 1. */
172 FIXME("%s,%d,%d,%p,%p,%p,%p: semi-stub\n", debugstr_w(pwcInChars), cInChars, cMaxItems,
173 psControl, psState, pItems, pcItems);
175 if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
176 return E_INVALIDARG;
178 /* Set a sensible default */
179 /* Set SCRIPT_ITEM */
180 pItems[0].iCharPos = 0;
181 /* Set the SCRIPT_ANALYSIS */
182 pItems[0].a.eScript = SCRIPT_UNDEFINED;
183 pItems[0].a.fRTL = 0;
184 pItems[0].a.fLayoutRTL = 0;
185 pItems[0].a.fLinkBefore = 0;
186 pItems[0].a.fLinkAfter = 0;
187 pItems[0].a.fLogicalOrder = 0;
188 pItems[0].a.fNoGlyphIndex = 0;
189 /* set the SCRIPT_STATE */
190 pItems[0].a.s.uBidiLevel = 0;
191 pItems[0].a.s.fOverrideDirection = 0;
192 pItems[0].a.s.fInhibitSymSwap = FALSE;
193 pItems[0].a.s.fCharShape = 0;
194 pItems[0].a.s.fDigitSubstitute = 0;
195 pItems[0].a.s.fInhibitLigate = 0;
196 pItems[0].a.s.fDisplayZWG = 0;
197 pItems[0].a.s.fArabicNumContext = 0;
198 pItems[0].a.s.fGcpClusters = 0;
199 pItems[0].a.s.fReserved = 0;
200 pItems[0].a.s.fEngineReserved = 0;
202 /* While not strickly necessary according to the spec, make sure the n+1
203 * item is set up to prevent random behaviour if the caller eroneously
204 * checks the n+1 structure */
205 pItems[1].a.eScript = 0;
206 pItems[1].a.fRTL = 0;
207 pItems[1].a.fLayoutRTL = 0;
208 pItems[1].a.fLinkBefore = 0;
209 pItems[1].a.fLinkAfter = 0;
210 pItems[1].a.fLogicalOrder = 0;
211 pItems[1].a.fNoGlyphIndex = 0;
212 /* set the SCRIPT_STATE */
213 pItems[1].a.s.uBidiLevel = 0;
214 pItems[1].a.s.fOverrideDirection = 0;
215 pItems[1].a.s.fInhibitSymSwap = FALSE;
216 pItems[1].a.s.fCharShape = 0;
217 pItems[1].a.s.fDigitSubstitute = 0;
218 pItems[1].a.s.fInhibitLigate = 0;
219 pItems[1].a.s.fDisplayZWG = 0;
220 pItems[1].a.s.fArabicNumContext = 0;
221 pItems[1].a.s.fGcpClusters = 0;
222 pItems[1].a.s.fReserved = 0;
223 pItems[1].a.s.fEngineReserved = 0;
225 /* Set one SCRIPT_STATE item being returned */
226 *pcItems = 1;
228 /* Set SCRIPT_ITEM */
229 pItems[1].iCharPos = cInChars - pItems[0].iCharPos ; /* the last + 1 item
230 contains the ptr to the lastchar */
231 TRACE("%s,%d,%d,%p,%p,%p,%p,%d\n", debugstr_w(pwcInChars), cInChars, cMaxItems,
232 psControl, psState, pItems, pcItems, *pcItems);
233 TRACE("Start Pos in string: %d, Stop Pos %d\n", pItems[0].iCharPos, pItems[1].iCharPos);
234 return 0;
237 /***********************************************************************
238 * ScriptStringAnalyse (USP10.@)
241 HRESULT WINAPI ScriptStringAnalyse(HDC hdc,
242 const void *pString,
243 int cString,
244 int cGlyphs,
245 int iCharset,
246 DWORD dwFlags,
247 int iReqWidth,
248 SCRIPT_CONTROL *psControl,
249 SCRIPT_STATE *psState,
250 const int *piDx,
251 SCRIPT_TABDEF *pTabdef,
252 const BYTE *pbInClass,
253 SCRIPT_STRING_ANALYSIS *pssa)
255 FIXME("(%p,%p,%d,%d,%d,0x%lx,%d,%p,%p,%p,%p,%p,%p): stub\n",
256 hdc, pString, cString, cGlyphs, iCharset, dwFlags,
257 iReqWidth, psControl, psState, piDx, pTabdef, pbInClass, pssa);
258 if (1 > cString || NULL == pString) {
259 return E_INVALIDARG;
261 if ((dwFlags & SSA_GLYPHS) && NULL == hdc) {
262 return E_INVALIDARG;
265 return E_NOTIMPL;
268 /***********************************************************************
269 * ScriptStringFree (USP10.@)
272 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa) {
273 FIXME("(%p): stub\n",pssa);
274 return S_OK;
277 /***********************************************************************
278 * ScriptIsComplex (USP10.@)
281 HRESULT WINAPI ScriptIsComplex(const WCHAR* pwcInChars, int cInChars, DWORD dwFlags) {
282 FIXME("(%s,%d,0x%lx): stub\n", debugstr_w(pwcInChars), cInChars, dwFlags);
283 return E_NOTIMPL;
285 /***********************************************************************
286 * ScriptShape (USP10.@)
289 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
290 int cChars, int cMaxGlyphs,
291 SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
292 SCRIPT_VISATTR *psva, int *pcGlyphs)
294 /* Note SCRIPT_CACHE (*psc) appears to be a good place to save info that needs to be
295 * passed between functions. */
297 HDC phdc;
298 int cnt;
299 DWORD hr;
300 Scriptcache *pScriptcache;
301 *pcGlyphs = cChars;
302 FIXME("(%p, %p, %p, %d, %d, %p): semi-stub\n", hdc, psc, pwcChars,
303 cChars, cMaxGlyphs, psa);
304 if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
305 psa->fLinkBefore, psa->fLinkAfter,
306 psa->fLogicalOrder, psa->fNoGlyphIndex);
308 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
310 if (!hdc && !*psc) {
311 TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
312 return E_PENDING;
313 } else
314 if (hdc && !*psc) {
315 pScriptcache = HeapAlloc( GetProcessHeap(), 0, sizeof(Scriptcache) );
316 pScriptcache->hdc = (HDC) hdc;
317 phdc = hdc;
318 pScriptcache->HaveWidths = 0;
319 *psc = (Scriptcache *) pScriptcache;
320 } else
321 if (*psc) {
322 pScriptcache = (Scriptcache *) *psc;
323 phdc = pScriptcache->hdc;
326 TRACE("Before: ");
327 for (cnt = 0; cnt < cChars; cnt++)
328 TRACE("%4x",pwcChars[cnt]);
329 TRACE("\n");
331 if (!psa->fNoGlyphIndex) { /* Glyph translate */
332 hr = GetGlyphIndicesW(phdc, pwcChars, cChars, pwOutGlyphs, 0);
333 TRACE("After: ");
334 for (cnt = 0; cnt < cChars; cnt++) {
335 TRACE("%04x",pwOutGlyphs[cnt]);
336 pScriptcache->GlyphToChar[pwOutGlyphs[cnt]] = pwcChars[cnt]; /* save for ScriptPlace */
338 TRACE("\n");
340 else {
341 TRACE("After: ");
342 for (cnt = 0; cnt < cChars; cnt++) { /* no translate so set up */
343 pwOutGlyphs[cnt] = pwcChars[cnt]; /* copy in to out and */
344 TRACE("%04x",pwOutGlyphs[cnt]);
345 pScriptcache->GlyphToChar[pwcChars[cnt]] = pwcChars[cnt]; /* set up a dummy table */
347 TRACE("\n");
350 /* Set up a valid SCRIPT_VISATTR and LogClust for each char in this run */
351 for (cnt = 0; cnt < cChars; cnt++) {
352 psva[cnt].uJustification = 2;
353 psva[cnt].fClusterStart = 1;
354 psva[cnt].fDiacritic = 0;
355 psva[cnt].fZeroWidth = 0;
356 pwLogClust[cnt] = cnt;
358 return 0;
361 /***********************************************************************
362 * ScriptPlace (USP10.@)
365 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
366 int cGlyphs, const SCRIPT_VISATTR *psva,
367 SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
369 HDC phdc;
370 int wcnt;
371 LPABC lpABC;
372 Scriptcache *pScriptcache;
373 FIXME("(%p, %p, %p, %s, %d, %p, %p, %p): semi-stub\n", hdc, psc, pwGlyphs,
374 debugstr_w(pwGlyphs),
375 cGlyphs, psva, psa,
376 piAdvance);
378 /* We need a valid hdc to do any of the font calls. The spec says that hdc is optional and
379 * psc will be used first. If psc and hdc are not specified E_PENDING is returned to get
380 * the caller to return the hdc. For convience, the hdc is cached in SCRIPT_CACHE. */
382 if (!hdc && !*psc) {
383 TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
384 return E_PENDING;
385 } else
386 if (hdc && !*psc) {
387 pScriptcache = HeapAlloc( GetProcessHeap(), 0, sizeof(Scriptcache) );
388 pScriptcache->hdc = hdc;
389 phdc = hdc;
390 *psc = pScriptcache;
391 } else
392 if (*psc) {
393 pScriptcache = *psc;
394 phdc = pScriptcache->hdc;
397 /* Here we need to calculate the width of the run unit. At this point the input string
398 * has been converted to glyphs and we till need to translate back to the original chars
399 * to get the correct ABC widths. To make life easier the ABC widths are saved in the
400 * SCRIPT_CACHE. This is ok as the cache must be invalidated if the font or font size
401 * changes. */
403 lpABC = pScriptcache->CharWidths;
404 pABC->abcA = 0;
405 pABC->abcB = 0;
406 pABC->abcC = 0;
407 if (!pScriptcache->HaveWidths) {
408 if (GetCharABCWidthsW(phdc, 0, 255, lpABC ))
409 pScriptcache->HaveWidths = 1;
410 else {
411 WARN("Could not get ABC values\n");
415 for (wcnt = 0; wcnt < cGlyphs ; wcnt++) { /* add up the char lengths */
416 TRACE(" Glyph=%04x, abcA=%d, abcB=%d, abcC=%d wcnt=%d\n",
417 pwGlyphs[wcnt],
418 lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcA,
419 lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcB,
420 lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcC, wcnt);
421 pABC->abcA += lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcA;
422 pABC->abcB += lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcB;
423 pABC->abcC += lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcC;
425 TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
427 return 0;
430 /***********************************************************************
431 * ScriptGetCMap (USP10.@)
434 /***********************************************************************
435 * ScriptGetCMap (USP10.@)
438 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
439 int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
441 HDC phdc;
442 int cnt;
443 DWORD hr;
444 Scriptcache *pScriptcache;
445 FIXME("(%p,%p,%s,%d,0x%lx,%p): semi-stub\n", hdc, psc, debugstr_wn(pwcInChars,cChars), cChars, dwFlags, pwOutGlyphs);
447 if (!hdc && !*psc) {
448 TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
449 return E_PENDING;
450 } else
451 if (hdc && !*psc) {
452 pScriptcache = HeapAlloc( GetProcessHeap(), 0, sizeof(Scriptcache) );
453 pScriptcache->hdc = hdc;
454 phdc = hdc;
455 pScriptcache->HaveWidths = 0;
456 *psc = pScriptcache;
457 } else
458 if (*psc) {
459 pScriptcache = *psc;
460 phdc = pScriptcache->hdc;
463 TRACE("Before: ");
464 for (cnt = 0; cnt < cChars; cnt++)
465 TRACE("%4x",pwcInChars[cnt]);
466 TRACE("\n");
468 hr = GetGlyphIndicesW(phdc, pwcInChars, cChars, pwOutGlyphs, 0);
469 TRACE("After: ");
470 for (cnt = 0; cnt < cChars; cnt++) {
471 TRACE("%04x",pwOutGlyphs[cnt]);
472 pScriptcache->GlyphToChar[pwOutGlyphs[cnt]] = pwcInChars[cnt]; /* save for ScriptPlace */
474 TRACE("\n");
476 return 0;
479 /***********************************************************************
480 * ScriptTextOut (USP10.@)
483 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions,
484 const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved,
485 int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
486 const int *piJustify, const GOFFSET *pGoffset)
488 FIXME("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p): stub\n",
489 hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
490 piAdvance, piJustify, pGoffset);
491 return E_NOTIMPL;