usp10: Remove the need to pass chars to ScriptPlace.
[wine/multimedia.git] / dlls / usp10 / usp10.c
blobcc8b9225242ceab0bd93b3f2baa4ee090fa8dc04
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 } Scriptcache;
75 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
77 switch(fdwReason) {
78 case DLL_PROCESS_ATTACH:
79 DisableThreadLibraryCalls(hInstDLL);
80 break;
81 case DLL_PROCESS_DETACH:
82 break;
84 return TRUE;
87 /***********************************************************************
88 * ScriptFreeCache (USP10.@)
91 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
93 TRACE("%p\n", psc);
95 if (psc) {
96 HeapFree ( GetProcessHeap(), 0, *psc);
97 *psc = NULL;
99 return 0;
102 /***********************************************************************
103 * ScriptGetProperties (USP10.@)
106 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***ppSp, int *piNumScripts)
108 TRACE("%p,%p\n",ppSp, piNumScripts);
110 /* Set up a sensible default and intialise pointers */
111 *piNumScripts = MAX_SCRIPTS;
112 *ppSp = Global_Script;
113 TRACE("ppSp:%p, *ppSp:%p, **ppSp:%p, %d\n", ppSp, *ppSp, **ppSp,
114 *piNumScripts);
115 return 0;
118 /***********************************************************************
119 * ScriptGetFontProperties (USP10.@)
122 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
124 HDC phdc;
125 Scriptcache *pScriptcache;
127 TRACE("%p,%p,%p\n", hdc, psc, sfp);
128 if (!hdc && !*psc) {
129 TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
130 return E_PENDING;
131 } else
132 if (hdc && !*psc) {
133 pScriptcache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Scriptcache) );
134 pScriptcache->hdc = (HDC) hdc;
135 phdc = hdc;
136 *psc = (Scriptcache *) pScriptcache;
137 } else
138 if (*psc) {
139 pScriptcache = (Scriptcache *) *psc;
140 phdc = pScriptcache->hdc;
143 if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
144 return E_INVALIDARG;
146 /* return something sensible? */
147 if (NULL != sfp) {
148 sfp->wgBlank = 0;
149 sfp->wgDefault = 0;
150 sfp->wgInvalid = 0;
151 sfp->wgKashida = 1;
152 sfp->iKashidaWidth = 0;
154 return 0;
157 /***********************************************************************
158 * ScriptRecordDigitSubstitution (USP10.@)
161 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID Locale,SCRIPT_DIGITSUBSTITUTE *psds)
163 FIXME("%ld,%p\n",Locale,psds);
164 return E_NOTIMPL;
167 /***********************************************************************
168 * ScriptApplyDigitSubstitution (USP10.@)
171 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE* psds,
172 SCRIPT_CONTROL* psc, SCRIPT_STATE* pss)
174 FIXME("%p,%p,%p\n",psds,psc,pss);
175 return E_NOTIMPL;
178 /***********************************************************************
179 * ScriptItemize (USP10.@)
182 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
183 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
184 SCRIPT_ITEM *pItems, int *pcItems)
186 /* This implementation currently treats the entire string represented in
187 * pwcInChars as a single entity. Hence pcItems will be set to 1. */
189 FIXME("%s,%d,%d,%p,%p,%p,%p: semi-stub\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems,
190 psControl, psState, pItems, pcItems);
192 if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
193 return E_INVALIDARG;
195 /* Set a sensible default */
196 /* Set SCRIPT_ITEM */
197 pItems[0].iCharPos = 0;
198 /* Set the SCRIPT_ANALYSIS */
199 pItems[0].a.eScript = SCRIPT_UNDEFINED;
200 pItems[0].a.fRTL = 0;
201 pItems[0].a.fLayoutRTL = 0;
202 pItems[0].a.fLinkBefore = 0;
203 pItems[0].a.fLinkAfter = 0;
204 pItems[0].a.fLogicalOrder = 0;
205 pItems[0].a.fNoGlyphIndex = 0;
206 /* set the SCRIPT_STATE */
207 pItems[0].a.s.uBidiLevel = 0;
208 pItems[0].a.s.fOverrideDirection = 0;
209 pItems[0].a.s.fInhibitSymSwap = FALSE;
210 pItems[0].a.s.fCharShape = 0;
211 pItems[0].a.s.fDigitSubstitute = 0;
212 pItems[0].a.s.fInhibitLigate = 0;
213 pItems[0].a.s.fDisplayZWG = 0;
214 pItems[0].a.s.fArabicNumContext = 0;
215 pItems[0].a.s.fGcpClusters = 0;
216 pItems[0].a.s.fReserved = 0;
217 pItems[0].a.s.fEngineReserved = 0;
219 /* While not strickly necessary according to the spec, make sure the n+1
220 * item is set up to prevent random behaviour if the caller eroneously
221 * checks the n+1 structure */
222 pItems[1].a.eScript = 0;
223 pItems[1].a.fRTL = 0;
224 pItems[1].a.fLayoutRTL = 0;
225 pItems[1].a.fLinkBefore = 0;
226 pItems[1].a.fLinkAfter = 0;
227 pItems[1].a.fLogicalOrder = 0;
228 pItems[1].a.fNoGlyphIndex = 0;
229 /* set the SCRIPT_STATE */
230 pItems[1].a.s.uBidiLevel = 0;
231 pItems[1].a.s.fOverrideDirection = 0;
232 pItems[1].a.s.fInhibitSymSwap = FALSE;
233 pItems[1].a.s.fCharShape = 0;
234 pItems[1].a.s.fDigitSubstitute = 0;
235 pItems[1].a.s.fInhibitLigate = 0;
236 pItems[1].a.s.fDisplayZWG = 0;
237 pItems[1].a.s.fArabicNumContext = 0;
238 pItems[1].a.s.fGcpClusters = 0;
239 pItems[1].a.s.fReserved = 0;
240 pItems[1].a.s.fEngineReserved = 0;
242 /* Set one SCRIPT_STATE item being returned */
243 *pcItems = 1;
245 /* Set SCRIPT_ITEM */
246 pItems[1].iCharPos = cInChars - pItems[0].iCharPos ; /* the last + 1 item
247 contains the ptr to the lastchar */
248 TRACE("%s,%d,%d,%p,%p,%p,%p,%d\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems,
249 psControl, psState, pItems, pcItems, *pcItems);
250 TRACE("Start Pos in string: %d, Stop Pos %d\n", pItems[0].iCharPos, pItems[1].iCharPos);
251 return 0;
254 /***********************************************************************
255 * ScriptStringAnalyse (USP10.@)
258 HRESULT WINAPI ScriptStringAnalyse(HDC hdc,
259 const void *pString,
260 int cString,
261 int cGlyphs,
262 int iCharset,
263 DWORD dwFlags,
264 int iReqWidth,
265 SCRIPT_CONTROL *psControl,
266 SCRIPT_STATE *psState,
267 const int *piDx,
268 SCRIPT_TABDEF *pTabdef,
269 const BYTE *pbInClass,
270 SCRIPT_STRING_ANALYSIS *pssa)
272 FIXME("(%p,%p,%d,%d,%d,0x%lx,%d,%p,%p,%p,%p,%p,%p): stub\n",
273 hdc, pString, cString, cGlyphs, iCharset, dwFlags,
274 iReqWidth, psControl, psState, piDx, pTabdef, pbInClass, pssa);
275 if (1 > cString || NULL == pString) {
276 return E_INVALIDARG;
278 if ((dwFlags & SSA_GLYPHS) && NULL == hdc) {
279 return E_INVALIDARG;
282 return E_NOTIMPL;
285 /***********************************************************************
286 * ScriptStringFree (USP10.@)
289 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa) {
290 FIXME("(%p): stub\n",pssa);
291 return S_OK;
294 /***********************************************************************
295 * ScriptIsComplex (USP10.@)
298 HRESULT WINAPI ScriptIsComplex(const WCHAR* pwcInChars, int cInChars, DWORD dwFlags) {
299 FIXME("(%s,%d,0x%lx): stub\n", debugstr_wn(pwcInChars, cInChars), cInChars, dwFlags);
300 return E_NOTIMPL;
303 /***********************************************************************
304 * ScriptShape (USP10.@)
307 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
308 int cChars, int cMaxGlyphs,
309 SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
310 SCRIPT_VISATTR *psva, int *pcGlyphs)
312 /* Note SCRIPT_CACHE (*psc) appears to be a good place to save info that needs to be
313 * passed between functions. */
315 HDC phdc;
316 int cnt;
317 DWORD hr;
318 Scriptcache *pScriptcache;
319 *pcGlyphs = cChars;
320 FIXME("(%p, %p, %p, %d, %d, %p): semi-stub\n", hdc, psc, pwcChars,
321 cChars, cMaxGlyphs, psa);
322 if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
323 psa->fLinkBefore, psa->fLinkAfter,
324 psa->fLogicalOrder, psa->fNoGlyphIndex);
326 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
328 if (!hdc && !*psc) {
329 TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
330 return E_PENDING;
331 } else
332 if (hdc && !*psc) {
333 pScriptcache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Scriptcache) );
334 pScriptcache->hdc = (HDC) hdc;
335 phdc = hdc;
336 *psc = (Scriptcache *) pScriptcache;
337 } else
338 if (*psc) {
339 pScriptcache = (Scriptcache *) *psc;
340 phdc = pScriptcache->hdc;
343 TRACE("Before: ");
344 for (cnt = 0; cnt < cChars; cnt++)
345 TRACE("%4x",pwcChars[cnt]);
346 TRACE("\n");
348 if (!psa->fNoGlyphIndex) { /* Glyph translate */
349 hr = GetGlyphIndicesW(phdc, pwcChars, cChars, pwOutGlyphs, 0);
350 TRACE("After: ");
351 for (cnt = 0; cnt < cChars; cnt++) {
352 TRACE("%04x",pwOutGlyphs[cnt]);
354 TRACE("\n");
356 else {
357 TRACE("After: ");
358 for (cnt = 0; cnt < cChars; cnt++) { /* no translate so set up */
359 pwOutGlyphs[cnt] = pwcChars[cnt]; /* copy in to out and */
360 TRACE("%04x",pwOutGlyphs[cnt]);
362 TRACE("\n");
365 /* Set up a valid SCRIPT_VISATTR and LogClust for each char in this run */
366 for (cnt = 0; cnt < cChars; cnt++) {
367 psva[cnt].uJustification = 2;
368 psva[cnt].fClusterStart = 1;
369 psva[cnt].fDiacritic = 0;
370 psva[cnt].fZeroWidth = 0;
371 pwLogClust[cnt] = cnt;
373 return 0;
376 /***********************************************************************
377 * ScriptPlace (USP10.@)
380 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
381 int cGlyphs, const SCRIPT_VISATTR *psva,
382 SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
384 HDC phdc;
385 int wcnt;
386 LPABC lpABC;
387 Scriptcache *pScriptcache;
388 FIXME("(%p, %p, %p, %s, %d, %p, %p, %p): semi-stub\n", hdc, psc, pwGlyphs,
389 debugstr_wn(pwGlyphs, cGlyphs),
390 cGlyphs, psva, psa,
391 piAdvance);
393 /* We need a valid hdc to do any of the font calls. The spec says that hdc is optional and
394 * psc will be used first. If psc and hdc are not specified E_PENDING is returned to get
395 * the caller to return the hdc. For convience, the hdc is cached in SCRIPT_CACHE. */
397 if (!hdc && !*psc) {
398 TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
399 return E_PENDING;
400 } else
401 if (hdc && !*psc) {
402 pScriptcache = HeapAlloc( GetProcessHeap(), 0, sizeof(Scriptcache) );
403 pScriptcache->hdc = hdc;
404 phdc = hdc;
405 *psc = pScriptcache;
406 } else
407 if (*psc) {
408 pScriptcache = *psc;
409 phdc = pScriptcache->hdc;
412 /* Here we need to calculate the width of the run unit. At this point the input string
413 * has been converted to glyphs and we till need to translate back to the original chars
414 * to get the correct ABC widths. */
416 lpABC = HeapAlloc(GetProcessHeap(), 0 , sizeof(ABC)*cGlyphs);
417 pABC->abcA = 0;
418 pABC->abcB = 0;
419 pABC->abcC = 0;
420 if (!GetCharABCWidthsI(phdc, 0, cGlyphs, (WORD *) pwGlyphs, lpABC ))
422 WARN("Could not get ABC values\n");
423 for (wcnt = 0; wcnt < cGlyphs; wcnt++) {
424 piAdvance[wcnt] = 0;
425 pGoffset[wcnt].du = 0;
426 pGoffset[wcnt].dv = 0;
429 else
431 for (wcnt = 0; wcnt < cGlyphs ; wcnt++) { /* add up the char lengths */
432 TRACE(" Glyph=%04x, abcA=%d, abcB=%d, abcC=%d wcnt=%d\n",
433 pwGlyphs[wcnt],
434 lpABC[wcnt].abcA,
435 lpABC[wcnt].abcB,
436 lpABC[wcnt].abcC, wcnt);
437 pABC->abcA += lpABC[wcnt].abcA;
438 pABC->abcB += lpABC[wcnt].abcB;
439 pABC->abcC += lpABC[wcnt].abcC;
440 piAdvance[wcnt] = lpABC[wcnt].abcA + lpABC[wcnt].abcB + lpABC[wcnt].abcC;
441 pGoffset[wcnt].du = 0;
442 pGoffset[wcnt].dv = 0;
445 TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
447 HeapFree(GetProcessHeap(), 0, lpABC );
449 return 0;
452 /***********************************************************************
453 * ScriptGetCMap (USP10.@)
456 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
457 int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
459 HDC phdc;
460 int cnt;
461 DWORD hr;
462 Scriptcache *pScriptcache;
463 FIXME("(%p,%p,%s,%d,0x%lx,%p): semi-stub\n", hdc, psc, debugstr_wn(pwcInChars,cChars), cChars, dwFlags, pwOutGlyphs);
465 if (!hdc && !*psc) {
466 TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
467 return E_PENDING;
468 } else
469 if (hdc && !*psc) {
470 pScriptcache = HeapAlloc( GetProcessHeap(), 0, sizeof(Scriptcache) );
471 pScriptcache->hdc = hdc;
472 phdc = hdc;
473 *psc = pScriptcache;
474 } else
475 if (*psc) {
476 pScriptcache = *psc;
477 phdc = pScriptcache->hdc;
480 TRACE("Before: ");
481 for (cnt = 0; cnt < cChars; cnt++)
482 TRACE("%4x",pwcInChars[cnt]);
483 TRACE("\n");
485 hr = GetGlyphIndicesW(phdc, pwcInChars, cChars, pwOutGlyphs, 0);
486 TRACE("After: ");
487 for (cnt = 0; cnt < cChars; cnt++) {
488 TRACE("%04x",pwOutGlyphs[cnt]);
490 TRACE("\n");
492 return 0;
495 /***********************************************************************
496 * ScriptTextOut (USP10.@)
499 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions,
500 const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved,
501 int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
502 const int *piJustify, const GOFFSET *pGoffset)
504 FIXME("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p): stub\n",
505 hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
506 piAdvance, piJustify, pGoffset);
507 return E_NOTIMPL;