crypt32: Fixed day/month mismatch in time encoding routines.
[wine] / dlls / usp10 / usp10.c
1 /*
2  * Implementation of Uniscribe Script Processor (usp10.dll)
3  *
4  * Copyright 2005 Steven Edwards for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  *
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.
23  */
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "usp10.h"
32
33 #include "wine/debug.h"
34
35 /**
36  * some documentation here:
37  *   http://www.microsoft.com/typography/developers/uniscribe/uniscribe.htm
38  */
39
40 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
41
42 #define MAX_SCRIPTS  8
43
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};
70
71 typedef struct scriptcache {
72        HDC hdc;
73        DWORD GlyphToChar[256];
74        int HaveWidths;
75        ABC CharWidths[256];
76 } Scriptcache;
77
78 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
79 {
80     switch(fdwReason) {
81         case DLL_PROCESS_ATTACH:
82             DisableThreadLibraryCalls(hInstDLL);
83             break;
84         case DLL_PROCESS_DETACH:
85             break;
86     }
87     return TRUE;
88 }
89
90 /***********************************************************************
91  *      ScriptFreeCache (USP10.@)
92  *
93  */
94 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
95 {
96     TRACE("%p\n", psc);
97
98     if (psc) {
99        HeapFree ( GetProcessHeap(), 0, *psc);
100        *psc = NULL;
101     }
102     return 0;
103 }
104
105 /***********************************************************************
106  *      ScriptGetProperties (USP10.@)
107  *
108  */
109 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***ppSp, int *piNumScripts)
110 {
111     TRACE("%p,%p\n",ppSp, piNumScripts);
112
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;
119 }
120
121 /***********************************************************************
122  *      ScriptGetFontProperties (USP10.@)
123  *
124  */
125 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
126 {
127     HDC phdc;
128     Scriptcache *pScriptcache;
129
130     FIXME("%p,%p,%p\n", hdc, psc, sfp);
131     if  (!hdc && !*psc) {
132         TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
133         return E_PENDING;
134     }   else 
135         if  (hdc && !*psc) {
136             pScriptcache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Scriptcache) );
137             pScriptcache->hdc = (HDC) hdc;
138             phdc = hdc;
139             pScriptcache->HaveWidths = 0;
140             *psc = (Scriptcache *) pScriptcache;
141        }   else
142             if  (*psc) {
143                 pScriptcache = (Scriptcache *) *psc;
144                 phdc = pScriptcache->hdc;
145             }
146                 
147     if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
148         return E_INVALIDARG;
149
150     /* return something sensible? */
151     if (NULL != sfp) {
152         sfp->wgBlank       = 0;
153         sfp->wgDefault     = 0;
154         sfp->wgInvalid     = 0;
155         sfp->wgKashida     = 1;
156         sfp->iKashidaWidth = 0;
157     }
158     return 0;
159 }
160
161 /***********************************************************************
162  *      ScriptRecordDigitSubstitution (USP10.@)
163  *
164  */
165 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID Locale,SCRIPT_DIGITSUBSTITUTE *psds)
166 {
167     FIXME("%ld,%p\n",Locale,psds);
168     return E_NOTIMPL;
169 }
170
171 /***********************************************************************
172  *      ScriptApplyDigitSubstitution (USP10.@)
173  *
174  */
175 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE* psds, 
176                                             SCRIPT_CONTROL* psc, SCRIPT_STATE* pss)
177 {
178     FIXME("%p,%p,%p\n",psds,psc,pss);
179     return E_NOTIMPL;
180 }
181
182 /***********************************************************************
183  *      ScriptItemize (USP10.@)
184  *
185  */
186 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems, 
187                              const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState, 
188                              SCRIPT_ITEM *pItems, int *pcItems)
189 {
190     /* This implementation currently treats the entire string represented in 
191      * pwcInChars as a single entity.  Hence pcItems will be set to 1.          */
192
193     FIXME("%s,%d,%d,%p,%p,%p,%p: semi-stub\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems, 
194           psControl, psState, pItems, pcItems);
195
196     if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
197         return E_INVALIDARG;
198
199     /*  Set a sensible default                              */
200     /*  Set SCRIPT_ITEM                                     */
201     pItems[0].iCharPos = 0;
202     /*  Set the SCRIPT_ANALYSIS                             */
203     pItems[0].a.eScript = SCRIPT_UNDEFINED;
204     pItems[0].a.fRTL = 0;
205     pItems[0].a.fLayoutRTL = 0;
206     pItems[0].a.fLinkBefore = 0;
207     pItems[0].a.fLinkAfter = 0;
208     pItems[0].a.fLogicalOrder = 0;
209     pItems[0].a.fNoGlyphIndex = 0;
210     /*  set the SCRIPT_STATE                                */
211     pItems[0].a.s.uBidiLevel = 0;
212     pItems[0].a.s.fOverrideDirection = 0;
213     pItems[0].a.s.fInhibitSymSwap = FALSE;
214     pItems[0].a.s.fCharShape = 0;
215     pItems[0].a.s.fDigitSubstitute = 0;
216     pItems[0].a.s.fInhibitLigate = 0;
217     pItems[0].a.s.fDisplayZWG = 0;
218     pItems[0].a.s.fArabicNumContext = 0;
219     pItems[0].a.s.fGcpClusters = 0;
220     pItems[0].a.s.fReserved = 0;
221     pItems[0].a.s.fEngineReserved = 0;
222
223     /* While not strickly necessary according to the spec, make sure the n+1
224      * item is set up to prevent random behaviour if the caller eroneously
225      * checks the n+1 structure                                              */
226     pItems[1].a.eScript = 0;
227     pItems[1].a.fRTL = 0;
228     pItems[1].a.fLayoutRTL = 0;
229     pItems[1].a.fLinkBefore = 0;
230     pItems[1].a.fLinkAfter = 0;
231     pItems[1].a.fLogicalOrder = 0;
232     pItems[1].a.fNoGlyphIndex = 0;
233     /*  set the SCRIPT_STATE                                */
234     pItems[1].a.s.uBidiLevel = 0;
235     pItems[1].a.s.fOverrideDirection = 0;
236     pItems[1].a.s.fInhibitSymSwap = FALSE;
237     pItems[1].a.s.fCharShape = 0;
238     pItems[1].a.s.fDigitSubstitute = 0;
239     pItems[1].a.s.fInhibitLigate = 0;
240     pItems[1].a.s.fDisplayZWG = 0;
241     pItems[1].a.s.fArabicNumContext = 0;
242     pItems[1].a.s.fGcpClusters = 0;
243     pItems[1].a.s.fReserved = 0;
244     pItems[1].a.s.fEngineReserved = 0;
245
246     /*  Set one SCRIPT_STATE item being returned  */
247     *pcItems = 1;
248
249     /*  Set SCRIPT_ITEM                                     */
250     pItems[1].iCharPos = cInChars - pItems[0].iCharPos ; /* the last + 1 item
251                                              contains the ptr to the lastchar */
252     TRACE("%s,%d,%d,%p,%p,%p,%p,%d\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems, 
253           psControl, psState, pItems, pcItems, *pcItems);
254     TRACE("Start Pos in string: %d, Stop Pos %d\n", pItems[0].iCharPos, pItems[1].iCharPos);
255     return 0;
256 }
257
258 /***********************************************************************
259  *      ScriptStringAnalyse (USP10.@)
260  *
261  */
262 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, 
263                                    const void *pString, 
264                                    int cString, 
265                                    int cGlyphs,
266                                    int iCharset,
267                                    DWORD dwFlags,
268                                    int iReqWidth,
269                                    SCRIPT_CONTROL *psControl,
270                                    SCRIPT_STATE *psState,
271                                    const int *piDx,
272                                    SCRIPT_TABDEF *pTabdef,
273                                    const BYTE *pbInClass,
274                                    SCRIPT_STRING_ANALYSIS *pssa)
275 {
276   FIXME("(%p,%p,%d,%d,%d,0x%lx,%d,%p,%p,%p,%p,%p,%p): stub\n",
277         hdc, pString, cString, cGlyphs, iCharset, dwFlags,
278         iReqWidth, psControl, psState, piDx, pTabdef, pbInClass, pssa);
279   if (1 > cString || NULL == pString) {
280     return E_INVALIDARG;
281   }
282   if ((dwFlags & SSA_GLYPHS) && NULL == hdc) {
283     return E_INVALIDARG;
284   }
285
286   return E_NOTIMPL;
287 }
288
289 /***********************************************************************
290  *      ScriptStringFree (USP10.@)
291  *
292  */
293 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa) {
294   FIXME("(%p): stub\n",pssa);
295   return S_OK;
296 }
297
298 /***********************************************************************
299  *      ScriptIsComplex (USP10.@)
300  *
301  */
302 HRESULT WINAPI ScriptIsComplex(const WCHAR* pwcInChars, int cInChars, DWORD dwFlags) {
303   FIXME("(%s,%d,0x%lx): stub\n",  debugstr_wn(pwcInChars, cInChars), cInChars, dwFlags);
304    return E_NOTIMPL;
305 }
306 /***********************************************************************
307  *      ScriptShape (USP10.@)
308  *
309  */
310 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars, 
311                            int cChars, int cMaxGlyphs,
312                            SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust, 
313                            SCRIPT_VISATTR *psva, int *pcGlyphs)
314 {
315     /*  Note SCRIPT_CACHE (*psc) appears to be a good place to save info that needs to be 
316      *  passed between functions.                                                         */
317
318     HDC phdc;
319     int cnt;
320     DWORD hr;
321     Scriptcache *pScriptcache;
322     *pcGlyphs = cChars;
323     FIXME("(%p, %p, %p, %d, %d, %p): semi-stub\n",  hdc, psc, pwcChars,
324                                        cChars, cMaxGlyphs, psa);
325     if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
326                                          psa->fLinkBefore, psa->fLinkAfter,
327                                          psa->fLogicalOrder, psa->fNoGlyphIndex);
328
329     if  (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
330
331     if  (!hdc && !*psc) {
332         TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
333         return E_PENDING;
334     }   else 
335         if  (hdc && !*psc) {
336             pScriptcache = HeapAlloc( GetProcessHeap(), 0, sizeof(Scriptcache) );
337             pScriptcache->hdc = (HDC) hdc;
338             phdc = hdc;
339             pScriptcache->HaveWidths = 0;
340             *psc = (Scriptcache *) pScriptcache;
341        }   else
342             if  (*psc) {
343                 pScriptcache = (Scriptcache *) *psc;
344                 phdc = pScriptcache->hdc;
345             }
346                 
347     TRACE("Before: ");
348     for (cnt = 0; cnt < cChars; cnt++)
349          TRACE("%4x",pwcChars[cnt]);
350     TRACE("\n");
351
352     if  (!psa->fNoGlyphIndex) {                                         /* Glyph translate */
353         hr = GetGlyphIndicesW(phdc, pwcChars, cChars, pwOutGlyphs, 0);
354         TRACE("After:  ");
355         for (cnt = 0; cnt < cChars; cnt++) {
356              TRACE("%04x",pwOutGlyphs[cnt]);
357              pScriptcache->GlyphToChar[pwOutGlyphs[cnt]] = pwcChars[cnt]; /* save for ScriptPlace */
358         }
359        TRACE("\n");
360     }
361     else {
362         TRACE("After:  ");
363         for (cnt = 0; cnt < cChars; cnt++) {                           /* no translate so set up */
364              pwOutGlyphs[cnt] = pwcChars[cnt];                         /* copy in to out and     */
365              TRACE("%04x",pwOutGlyphs[cnt]);
366              pScriptcache->GlyphToChar[pwcChars[cnt]] = pwcChars[cnt]; /* set up a dummy table   */
367         }
368        TRACE("\n");
369     }
370
371     /*  Set up a valid SCRIPT_VISATTR and LogClust for each char in this run */     
372     for (cnt = 0;  cnt < cChars; cnt++) {
373          psva[cnt].uJustification = 2;
374          psva[cnt].fClusterStart = 1;
375          psva[cnt].fDiacritic = 0;
376          psva[cnt].fZeroWidth = 0;
377          pwLogClust[cnt] = cnt;
378     }
379     return 0; 
380 }
381
382 /***********************************************************************
383  *      ScriptPlace (USP10.@)
384  *
385  */
386 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs, 
387                            int cGlyphs, const SCRIPT_VISATTR *psva,
388                            SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
389 {
390     HDC phdc;
391     int wcnt;
392     LPABC lpABC;
393     Scriptcache *pScriptcache;
394     FIXME("(%p, %p, %p, %s, %d, %p, %p, %p): semi-stub\n",  hdc, psc, pwGlyphs,
395                                                 debugstr_wn(pwGlyphs, cGlyphs), 
396                                                 cGlyphs, psva, psa, 
397                                                 piAdvance);
398
399     /*  We need a valid hdc to do any of the font calls.  The spec says that hdc is optional and 
400      *  psc will be used first.  If psc and hdc are not specified E_PENDING is returned to get 
401      *  the caller to return the hdc.  For convience, the hdc is cached in SCRIPT_CACHE.    */
402
403     if  (!hdc && !*psc) {
404         TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
405         return E_PENDING;
406     }   else 
407         if  (hdc && !*psc) {
408             pScriptcache = HeapAlloc( GetProcessHeap(), 0, sizeof(Scriptcache) );
409             pScriptcache->hdc = hdc;
410             phdc = hdc;
411             *psc = pScriptcache;
412         }   else
413             if  (*psc) {
414                 pScriptcache = *psc;
415                 phdc = pScriptcache->hdc;
416             }
417
418     /*   Here we need to calculate the width of the run unit.  At this point the input string
419      *   has been converted to glyphs and we till need to translate back to the original chars
420      *   to get the correct ABC widths.  To make life easier the ABC widths are saved in the 
421      *   SCRIPT_CACHE.  This is ok as the cache must be invalidated if the font or font size 
422      *   changes.   */
423
424      lpABC = pScriptcache->CharWidths;
425      pABC->abcA = 0; 
426      pABC->abcB = 0; 
427      pABC->abcC = 0; 
428      if  (!pScriptcache->HaveWidths) {
429          if  (GetCharABCWidthsW(phdc, 0, 255, lpABC )) 
430              pScriptcache->HaveWidths = 1;
431          else {
432              WARN("Could not get ABC values\n");
433          }
434      }
435
436      for (wcnt = 0; wcnt < cGlyphs ; wcnt++) {          /* add up the char lengths  */
437          TRACE("     Glyph=%04x, abcA=%d,  abcB=%d,  abcC=%d  wcnt=%d\n",
438                               pwGlyphs[wcnt], 
439                               lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcA,
440                               lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcB,
441                               lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcC, wcnt);
442          pABC->abcA += lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcA;
443          pABC->abcB += lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcB;
444          pABC->abcC += lpABC[pScriptcache->GlyphToChar[pwGlyphs[wcnt]]].abcC;
445      }
446      TRACE("Total for run:   abcA=%d,  abcB=%d,  abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
447
448      return 0;
449 }
450
451 /***********************************************************************
452  *      ScriptGetCMap (USP10.@)
453  *
454  */
455 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
456                               int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
457 {
458     HDC phdc;
459     int cnt;
460     DWORD hr;
461     Scriptcache *pScriptcache;
462     FIXME("(%p,%p,%s,%d,0x%lx,%p): semi-stub\n", hdc, psc, debugstr_wn(pwcInChars,cChars), cChars, dwFlags, pwOutGlyphs);
463
464     if  (!hdc && !*psc) {
465         TRACE("No Script_Cache (psc) and no hdc. Ask for one. Hdc=%p, psc=%p\n", hdc, *psc);
466         return E_PENDING;
467     }   else 
468         if  (hdc && !*psc) {
469             pScriptcache = HeapAlloc( GetProcessHeap(), 0, sizeof(Scriptcache) );
470             pScriptcache->hdc = hdc;
471             phdc = hdc;
472             pScriptcache->HaveWidths = 0;
473             *psc = pScriptcache;
474         }   else
475             if  (*psc) {
476                 pScriptcache = *psc;
477                 phdc = pScriptcache->hdc;
478             }
479
480     TRACE("Before: ");
481     for (cnt = 0; cnt < cChars; cnt++)
482          TRACE("%4x",pwcInChars[cnt]);
483     TRACE("\n");
484
485     hr = GetGlyphIndicesW(phdc, pwcInChars, cChars, pwOutGlyphs, 0);
486     TRACE("After:  ");
487     for (cnt = 0; cnt < cChars; cnt++) {
488          TRACE("%04x",pwOutGlyphs[cnt]);
489          pScriptcache->GlyphToChar[pwOutGlyphs[cnt]] = pwcInChars[cnt]; /* save for ScriptPlace */
490     }
491     TRACE("\n");
492
493     return 0; 
494 }
495
496 /***********************************************************************
497  *      ScriptTextOut (USP10.@)
498  *
499  */
500 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions, 
501                              const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved, 
502                              int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance, 
503                              const int *piJustify, const GOFFSET *pGoffset)
504 {
505      FIXME("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p): stub\n",
506            hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
507            piAdvance, piJustify, pGoffset);
508      return E_NOTIMPL;
509 }