2 * Implementation of Uniscribe Script Processor (usp10.dll)
4 * Copyright 2005 Steven Edwards for CodeWeavers
5 * Copyright 2006 Hans Leidekker
6 * Copyright 2010 CodeWeavers, Aric Stewart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * Uniscribe allows for processing of complex scripts such as joining
24 * and filtering characters and bi-directional text with custom line breaks.
36 #include "usp10_internal.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
43 static const SCRIPT_PROPERTIES props[] =
45 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
46 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
47 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
48 { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
49 { 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
50 { 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
51 { 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
52 { 8, 0, 0, 0, 0, 161, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
53 { 25, 0, 0, 0, 0, 204, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
54 { 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
117 { 9, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
118 { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
121 static const SCRIPT_PROPERTIES *script_props[] =
123 &props[0], &props[1], &props[2], &props[3],
124 &props[4], &props[5], &props[6], &props[7],
125 &props[8], &props[9], &props[11], &props[12],
126 &props[13], &props[14], &props[15], &props[16],
127 &props[17], &props[18], &props[19], &props[20],
128 &props[21], &props[22], &props[23], &props[24],
129 &props[25], &props[26], &props[27], &props[28],
130 &props[29], &props[30], &props[31], &props[32],
131 &props[33], &props[34], &props[35], &props[36],
132 &props[37], &props[38], &props[39], &props[40],
133 &props[41], &props[42], &props[43], &props[44],
134 &props[45], &props[46], &props[47], &props[48],
135 &props[49], &props[50], &props[51], &props[52],
136 &props[53], &props[54], &props[55], &props[56],
137 &props[57], &props[58], &props[59], &props[60],
138 &props[61], &props[62], &props[63], &props[64],
139 &props[65], &props[66], &props[67], &props[68],
140 &props[69], &props[70], &props[71], &props[72],
149 SCRIPT_VISATTR* psva;
163 StringGlyphs* glyphs;
164 SCRIPT_LOGATTR* logattrs;
168 static inline void *heap_alloc(SIZE_T size)
170 return HeapAlloc(GetProcessHeap(), 0, size);
173 static inline void *heap_alloc_zero(SIZE_T size)
175 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
178 static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
180 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
183 static inline BOOL heap_free(LPVOID mem)
185 return HeapFree(GetProcessHeap(), 0, mem);
188 static inline WCHAR get_cache_default_char(SCRIPT_CACHE *psc)
190 return ((ScriptCache *)*psc)->tm.tmDefaultChar;
193 static inline LONG get_cache_height(SCRIPT_CACHE *psc)
195 return ((ScriptCache *)*psc)->tm.tmHeight;
198 static inline BYTE get_cache_pitch_family(SCRIPT_CACHE *psc)
200 return ((ScriptCache *)*psc)->tm.tmPitchAndFamily;
203 static inline WORD get_cache_glyph(SCRIPT_CACHE *psc, WCHAR c)
205 WORD *block = ((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
207 if (!block) return 0;
208 return block[c & GLYPH_BLOCK_MASK];
211 static inline WORD set_cache_glyph(SCRIPT_CACHE *psc, WCHAR c, WORD glyph)
213 WORD **block = &((ScriptCache *)*psc)->glyphs[c >> GLYPH_BLOCK_SHIFT];
215 if (!*block && !(*block = heap_alloc_zero(sizeof(WORD) * GLYPH_BLOCK_SIZE))) return 0;
216 return ((*block)[c & GLYPH_BLOCK_MASK] = glyph);
219 static inline BOOL get_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
221 static const ABC nil;
222 ABC *block = ((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
224 if (!block || !memcmp(&block[glyph & GLYPH_BLOCK_MASK], &nil, sizeof(ABC))) return FALSE;
225 memcpy(abc, &block[glyph & GLYPH_BLOCK_MASK], sizeof(ABC));
229 static inline BOOL set_cache_glyph_widths(SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
231 ABC **block = &((ScriptCache *)*psc)->widths[glyph >> GLYPH_BLOCK_SHIFT];
233 if (!*block && !(*block = heap_alloc_zero(sizeof(ABC) * GLYPH_BLOCK_SIZE))) return FALSE;
234 memcpy(&(*block)[glyph & GLYPH_BLOCK_MASK], abc, sizeof(ABC));
238 static HRESULT init_script_cache(const HDC hdc, SCRIPT_CACHE *psc)
242 if (!psc) return E_INVALIDARG;
243 if (*psc) return S_OK;
244 if (!hdc) return E_PENDING;
246 if (!(sc = heap_alloc_zero(sizeof(ScriptCache)))) return E_OUTOFMEMORY;
247 if (!GetTextMetricsW(hdc, &sc->tm))
252 if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(LOGFONTW), &sc->lf))
258 TRACE("<- %p\n", sc);
262 static WCHAR mirror_char( WCHAR ch )
264 extern const WCHAR wine_mirror_map[];
265 return ch + wine_mirror_map[wine_mirror_map[ch >> 8] + (ch & 0xff)];
268 /***********************************************************************
272 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
276 case DLL_PROCESS_ATTACH:
277 DisableThreadLibraryCalls(hInstDLL);
279 case DLL_PROCESS_DETACH:
285 /***********************************************************************
286 * ScriptFreeCache (USP10.@)
288 * Free a script cache.
291 * psc [I/O] Script cache.
295 * Failure: Non-zero HRESULT value.
297 HRESULT WINAPI ScriptFreeCache(SCRIPT_CACHE *psc)
304 for (i = 0; i < GLYPH_MAX / GLYPH_BLOCK_SIZE; i++)
306 heap_free(((ScriptCache *)*psc)->glyphs[i]);
307 heap_free(((ScriptCache *)*psc)->widths[i]);
315 /***********************************************************************
316 * ScriptGetProperties (USP10.@)
318 * Retrieve a list of script properties.
321 * props [I] Pointer to an array of SCRIPT_PROPERTIES pointers.
322 * num [I] Pointer to the number of scripts.
326 * Failure: Non-zero HRESULT value.
329 * Behaviour matches WinXP.
331 HRESULT WINAPI ScriptGetProperties(const SCRIPT_PROPERTIES ***props, int *num)
333 TRACE("(%p,%p)\n", props, num);
335 if (!props && !num) return E_INVALIDARG;
337 if (num) *num = sizeof(script_props)/sizeof(script_props[0]);
338 if (props) *props = script_props;
343 /***********************************************************************
344 * ScriptGetFontProperties (USP10.@)
346 * Get information on special glyphs.
349 * hdc [I] Device context.
350 * psc [I/O] Opaque pointer to a script cache.
351 * sfp [O] Font properties structure.
353 HRESULT WINAPI ScriptGetFontProperties(HDC hdc, SCRIPT_CACHE *psc, SCRIPT_FONTPROPERTIES *sfp)
357 TRACE("%p,%p,%p\n", hdc, psc, sfp);
359 if (!sfp) return E_INVALIDARG;
360 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
362 if (sfp->cBytes != sizeof(SCRIPT_FONTPROPERTIES))
365 /* return something sensible? */
367 sfp->wgDefault = get_cache_default_char(psc);
369 sfp->wgKashida = 0xffff;
370 sfp->iKashidaWidth = 0;
375 /***********************************************************************
376 * ScriptRecordDigitSubstitution (USP10.@)
378 * Record digit substitution settings for a given locale.
381 * locale [I] Locale identifier.
382 * sds [I] Structure to record substitution settings.
386 * Failure: E_POINTER if sds is NULL, E_INVALIDARG otherwise.
389 * http://blogs.msdn.com/michkap/archive/2006/02/22/536877.aspx
391 HRESULT WINAPI ScriptRecordDigitSubstitution(LCID locale, SCRIPT_DIGITSUBSTITUTE *sds)
395 TRACE("0x%x, %p\n", locale, sds);
397 /* This implementation appears to be correct for all languages, but it's
398 * not clear if sds->DigitSubstitute is ever set to anything except
399 * CONTEXT or NONE in reality */
401 if (!sds) return E_POINTER;
403 locale = ConvertDefaultLocale(locale);
405 if (!IsValidLocale(locale, LCID_INSTALLED))
408 plgid = PRIMARYLANGID(LANGIDFROMLCID(locale));
409 sds->TraditionalDigitLanguage = plgid;
411 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
412 sds->NationalDigitLanguage = plgid;
414 sds->NationalDigitLanguage = LANG_ENGLISH;
416 if (!GetLocaleInfoW(locale, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER,
417 (LPWSTR)&sub, sizeof(sub)/sizeof(WCHAR))) return E_INVALIDARG;
422 if (plgid == LANG_ARABIC || plgid == LANG_FARSI)
423 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_CONTEXT;
425 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
428 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NONE;
431 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_NATIONAL;
434 sds->DigitSubstitute = SCRIPT_DIGITSUBSTITUTE_TRADITIONAL;
442 /***********************************************************************
443 * ScriptApplyDigitSubstitution (USP10.@)
445 * Apply digit substitution settings.
448 * sds [I] Structure with recorded substitution settings.
449 * sc [I] Script control structure.
450 * ss [I] Script state structure.
454 * Failure: E_INVALIDARG if sds is invalid. Otherwise an HRESULT.
456 HRESULT WINAPI ScriptApplyDigitSubstitution(const SCRIPT_DIGITSUBSTITUTE *sds,
457 SCRIPT_CONTROL *sc, SCRIPT_STATE *ss)
459 SCRIPT_DIGITSUBSTITUTE psds;
461 TRACE("%p, %p, %p\n", sds, sc, ss);
463 if (!sc || !ss) return E_POINTER;
467 if (ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &psds) != S_OK)
471 sc->uDefaultLanguage = LANG_ENGLISH;
472 sc->fContextDigits = 0;
473 ss->fDigitSubstitute = 0;
475 switch (sds->DigitSubstitute) {
476 case SCRIPT_DIGITSUBSTITUTE_CONTEXT:
477 case SCRIPT_DIGITSUBSTITUTE_NATIONAL:
478 case SCRIPT_DIGITSUBSTITUTE_NONE:
479 case SCRIPT_DIGITSUBSTITUTE_TRADITIONAL:
486 /***********************************************************************
487 * ScriptItemize (USP10.@)
489 * Split a Unicode string into shapeable parts.
492 * pwcInChars [I] String to split.
493 * cInChars [I] Number of characters in pwcInChars.
494 * cMaxItems [I] Maximum number of items to return.
495 * psControl [I] Pointer to a SCRIPT_CONTROL structure.
496 * psState [I] Pointer to a SCRIPT_STATE structure.
497 * pItems [O] Buffer to receive SCRIPT_ITEM structures.
498 * pcItems [O] Number of script items returned.
502 * Failure: Non-zero HRESULT value.
504 HRESULT WINAPI ScriptItemize(const WCHAR *pwcInChars, int cInChars, int cMaxItems,
505 const SCRIPT_CONTROL *psControl, const SCRIPT_STATE *psState,
506 SCRIPT_ITEM *pItems, int *pcItems)
509 #define Numeric_start 0x0030
510 #define Numeric_stop 0x0039
511 #define Numeric_space 0x0020
512 #define Arabic_start 0x0600
513 #define Arabic_stop 0x06ff
514 #define Hebrew_start 0x0590
515 #define Hebrew_stop 0x05ff
516 #define Syriac_start 0x0700
517 #define Syriac_stop 0x074f
518 #define Latin_start 0x0001
519 #define Latin_stop 0x024f
521 int cnt = 0, index = 0;
522 int New_Script = SCRIPT_UNDEFINED;
526 TRACE("%s,%d,%d,%p,%p,%p,%p\n", debugstr_wn(pwcInChars, cInChars), cInChars, cMaxItems,
527 psControl, psState, pItems, pcItems);
529 if (!pwcInChars || !cInChars || !pItems || cMaxItems < 2)
532 if (psState && psControl)
535 levels = heap_alloc_zero(cInChars * sizeof(WORD));
537 return E_OUTOFMEMORY;
539 BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
540 baselevel = levels[0];
541 for (i = 0; i < cInChars; i++)
542 if (levels[i]!=levels[0])
551 pItems[index].iCharPos = 0;
552 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
554 if (pwcInChars[cnt] == '\r')
555 pItems[index].a.eScript = Script_CR;
557 if (pwcInChars[cnt] == '\n')
558 pItems[index].a.eScript = Script_LF;
560 if (pwcInChars[cnt] >= Numeric_start && pwcInChars[cnt] <= Numeric_stop)
561 pItems[index].a.eScript = Script_Numeric;
563 if (pwcInChars[cnt] >= Arabic_start && pwcInChars[cnt] <= Arabic_stop)
564 pItems[index].a.eScript = Script_Arabic;
566 if (pwcInChars[cnt] >= Hebrew_start && pwcInChars[cnt] <= Hebrew_stop)
567 pItems[index].a.eScript = Script_Hebrew;
569 if (pwcInChars[cnt] >= Syriac_start && pwcInChars[cnt] <= Syriac_stop)
570 pItems[index].a.eScript = Script_Syriac;
572 if (pwcInChars[cnt] >= Latin_start && pwcInChars[cnt] <= Latin_stop)
573 pItems[index].a.eScript = Script_Latin;
577 pItems[index].a.fRTL = odd(levels[cnt]);
578 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
579 pItems[index].a.s.uBidiLevel = levels[cnt];
581 else if ((pItems[index].a.eScript == Script_Arabic) ||
582 (pItems[index].a.eScript == Script_Hebrew) ||
583 (pItems[index].a.eScript == Script_Syriac))
585 pItems[index].a.s.uBidiLevel = 1;
586 pItems[index].a.fRTL = 1;
587 pItems[index].a.fLayoutRTL = 1;
591 pItems[index].a.s.uBidiLevel = baselevel;
592 pItems[index].a.fLayoutRTL = odd(baselevel);
593 pItems[index].a.fRTL = odd(baselevel);
596 TRACE("New_Level=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
597 levels?levels[cnt]:-1, New_Script, pItems[index].a.eScript, index, cnt,
598 pItems[index].iCharPos);
600 for (cnt=1; cnt < cInChars; cnt++)
602 if (levels && (levels[cnt] == pItems[index].a.s.uBidiLevel))
605 if (pwcInChars[cnt] == '\r')
606 New_Script = Script_CR;
608 if (pwcInChars[cnt] == '\n')
609 New_Script = Script_LF;
611 if ((pwcInChars[cnt] >= Numeric_start && pwcInChars[cnt] <= Numeric_stop)
612 || (New_Script == Script_Numeric && pwcInChars[cnt] == Numeric_space))
613 New_Script = Script_Numeric;
615 if ((pwcInChars[cnt] >= Arabic_start && pwcInChars[cnt] <= Arabic_stop)
616 || (New_Script == Script_Arabic && pwcInChars[cnt] == Numeric_space))
617 New_Script = Script_Arabic;
619 if ((pwcInChars[cnt] >= Hebrew_start && pwcInChars[cnt] <= Hebrew_stop)
620 || (New_Script == Script_Hebrew && pwcInChars[cnt] == Numeric_space))
621 New_Script = Script_Hebrew;
623 if ((pwcInChars[cnt] >= Syriac_start && pwcInChars[cnt] <= Syriac_stop)
624 || (New_Script == Script_Syriac && pwcInChars[cnt] == Numeric_space))
625 New_Script = Script_Syriac;
627 if ((pwcInChars[cnt] >= Latin_start && pwcInChars[cnt] <= Latin_stop)
628 || (New_Script == Script_Latin && pwcInChars[cnt] == Numeric_space))
629 New_Script = Script_Latin;
631 New_Script = SCRIPT_UNDEFINED;
633 if ((levels && (levels[cnt] != pItems[index].a.s.uBidiLevel)) || New_Script != pItems[index].a.eScript)
635 TRACE("New_Level = %i, New_Script=%d, eScript=%d ", levels?levels[cnt]:-1, New_Script, pItems[index].a.eScript);
637 if (index+1 > cMaxItems)
638 return E_OUTOFMEMORY;
640 pItems[index].iCharPos = cnt;
641 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
645 pItems[index].a.fRTL = odd(levels[cnt]);
646 pItems[index].a.fLayoutRTL = odd(levels[cnt]);
647 pItems[index].a.s.uBidiLevel = levels[cnt];
649 else if ((New_Script == Script_Arabic) ||
650 (New_Script == Script_Hebrew) ||
651 (New_Script == Script_Syriac))
653 pItems[index].a.s.uBidiLevel = 1;
654 pItems[index].a.fRTL = 1;
655 pItems[index].a.fLayoutRTL = 1;
659 pItems[index].a.s.uBidiLevel = baselevel;
660 pItems[index].a.fLayoutRTL = odd(baselevel);
661 pItems[index].a.fRTL = odd(baselevel);
664 pItems[index].a.eScript = New_Script;
666 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
670 /* While not strictly necessary according to the spec, make sure the n+1
671 * item is set up to prevent random behaviour if the caller erroneously
672 * checks the n+1 structure */
674 memset(&pItems[index].a, 0, sizeof(SCRIPT_ANALYSIS));
676 TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
678 /* Set one SCRIPT_STATE item being returned */
679 if (index + 1 > cMaxItems) return E_OUTOFMEMORY;
680 if (pcItems) *pcItems = index;
682 /* Set SCRIPT_ITEM */
683 pItems[index].iCharPos = cnt; /* the last item contains the ptr to the lastchar */
688 /***********************************************************************
689 * ScriptStringAnalyse (USP10.@)
692 HRESULT WINAPI ScriptStringAnalyse(HDC hdc, const void *pString, int cString,
693 int cGlyphs, int iCharset, DWORD dwFlags,
694 int iReqWidth, SCRIPT_CONTROL *psControl,
695 SCRIPT_STATE *psState, const int *piDx,
696 SCRIPT_TABDEF *pTabdef, const BYTE *pbInClass,
697 SCRIPT_STRING_ANALYSIS *pssa)
699 HRESULT hr = E_OUTOFMEMORY;
700 StringAnalysis *analysis = NULL;
701 int i, num_items = 255;
703 TRACE("(%p,%p,%d,%d,%d,0x%x,%d,%p,%p,%p,%p,%p,%p)\n",
704 hdc, pString, cString, cGlyphs, iCharset, dwFlags, iReqWidth,
705 psControl, psState, piDx, pTabdef, pbInClass, pssa);
709 FIXME("Only Unicode strings are supported\n");
712 if (cString < 1 || !pString) return E_INVALIDARG;
713 if ((dwFlags & SSA_GLYPHS) && !hdc) return E_PENDING;
715 if (!(analysis = heap_alloc_zero(sizeof(StringAnalysis)))) return E_OUTOFMEMORY;
716 if (!(analysis->pItem = heap_alloc_zero(num_items * sizeof(SCRIPT_ITEM) + 1))) goto error;
718 /* FIXME: handle clipping */
719 analysis->clip_len = cString;
722 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
723 &analysis->numItems);
725 while (hr == E_OUTOFMEMORY)
730 if (!(tmp = heap_realloc_zero(analysis->pItem, num_items * sizeof(SCRIPT_ITEM) + 1)))
733 analysis->pItem = tmp;
734 hr = ScriptItemize(pString, cString, num_items, psControl, psState, analysis->pItem,
735 &analysis->numItems);
737 if (hr != S_OK) goto error;
739 if ((analysis->logattrs = heap_alloc(sizeof(SCRIPT_LOGATTR) * cString)))
740 ScriptBreak(pString, cString, (SCRIPT_STRING_ANALYSIS)analysis, analysis->logattrs);
744 if (!(analysis->glyphs = heap_alloc_zero(sizeof(StringGlyphs) * analysis->numItems)))
747 for (i = 0; i < analysis->numItems; i++)
749 SCRIPT_CACHE *sc = (SCRIPT_CACHE *)&analysis->sc;
750 int cChar = analysis->pItem[i+1].iCharPos - analysis->pItem[i].iCharPos;
751 int numGlyphs = 1.5 * cChar + 16;
752 WORD *glyphs = heap_alloc_zero(sizeof(WORD) * numGlyphs);
753 WORD *pwLogClust = heap_alloc_zero(sizeof(WORD) * cChar);
754 int *piAdvance = heap_alloc_zero(sizeof(int) * numGlyphs);
755 SCRIPT_VISATTR *psva = heap_alloc_zero(sizeof(SCRIPT_VISATTR) * cChar);
756 GOFFSET *pGoffset = heap_alloc_zero(sizeof(GOFFSET) * numGlyphs);
757 ABC *abc = heap_alloc_zero(sizeof(ABC));
758 int numGlyphsReturned;
760 /* FIXME: non unicode strings */
761 const WCHAR* pStr = (const WCHAR*)pString;
762 hr = ScriptShape(hdc, sc, &pStr[analysis->pItem[i].iCharPos],
763 cChar, numGlyphs, &analysis->pItem[i].a,
764 glyphs, pwLogClust, psva, &numGlyphsReturned);
765 hr = ScriptPlace(hdc, sc, glyphs, numGlyphsReturned, psva, &analysis->pItem[i].a,
766 piAdvance, pGoffset, abc);
768 analysis->glyphs[i].numGlyphs = numGlyphsReturned;
769 analysis->glyphs[i].glyphs = glyphs;
770 analysis->glyphs[i].pwLogClust = pwLogClust;
771 analysis->glyphs[i].piAdvance = piAdvance;
772 analysis->glyphs[i].psva = psva;
773 analysis->glyphs[i].pGoffset = pGoffset;
774 analysis->glyphs[i].abc = abc;
781 heap_free(analysis->glyphs);
782 heap_free(analysis->logattrs);
783 heap_free(analysis->pItem);
784 heap_free(analysis->sc);
789 /***********************************************************************
790 * ScriptStringOut (USP10.@)
792 * This function takes the output of ScriptStringAnalyse and joins the segments
793 * of glyphs and passes the resulting string to ScriptTextOut. ScriptStringOut
794 * only processes glyphs.
797 * ssa [I] buffer to hold the analysed string components
798 * iX [I] X axis displacement for output
799 * iY [I] Y axis displacement for output
800 * uOptions [I] flags controling output processing
801 * prc [I] rectangle coordinates
802 * iMinSel [I] starting pos for substringing output string
803 * iMaxSel [I] ending pos for substringing output string
804 * fDisabled [I] controls text highlighting
808 * Failure: is the value returned by ScriptTextOut
810 HRESULT WINAPI ScriptStringOut(SCRIPT_STRING_ANALYSIS ssa,
819 StringAnalysis *analysis;
824 TRACE("(%p,%d,%d,0x%1x,%p,%d,%d,%d)\n",
825 ssa, iX, iY, uOptions, prc, iMinSel, iMaxSel, fDisabled);
827 if (!(analysis = ssa)) return E_INVALIDARG;
830 * Get storage for the output buffer for the consolidated strings
833 for (item = 0; item < analysis->numItems; item++)
835 cnt += analysis->glyphs[item].numGlyphs;
837 if (!(glyphs = heap_alloc(sizeof(WCHAR) * cnt))) return E_OUTOFMEMORY;
840 * ScriptStringOut only processes glyphs hence set ETO_GLYPH_INDEX
842 uOptions |= ETO_GLYPH_INDEX;
843 analysis->pItem[0].a.fNoGlyphIndex = FALSE; /* say that we have glyphs */
846 * Copy the string items into the output buffer
849 TRACE("numItems %d\n", analysis->numItems);
852 for (item = 0; item < analysis->numItems; item++)
854 memcpy(&glyphs[cnt], analysis->glyphs[item].glyphs,
855 sizeof(WCHAR) * analysis->glyphs[item].numGlyphs);
857 TRACE("Item %d, Glyphs %d ", item, analysis->glyphs[item].numGlyphs);
858 for (x = cnt; x < analysis->glyphs[item].numGlyphs + cnt; x ++)
859 TRACE("%04x", glyphs[x]);
862 cnt += analysis->glyphs[item].numGlyphs; /* point to the end of the copied text */
865 hr = ScriptTextOut(analysis->hdc, (SCRIPT_CACHE *)&analysis->sc, iX, iY,
866 uOptions, prc, &analysis->pItem->a, NULL, 0, glyphs, cnt,
867 analysis->glyphs->piAdvance, NULL, analysis->glyphs->pGoffset);
868 TRACE("ScriptTextOut hr=%08x\n", hr);
871 * Free the output buffer and script cache
877 /***********************************************************************
878 * ScriptStringCPtoX (USP10.@)
881 HRESULT WINAPI ScriptStringCPtoX(SCRIPT_STRING_ANALYSIS ssa, int icp, BOOL fTrailing, int* pX)
886 StringAnalysis* analysis = ssa;
888 TRACE("(%p), %d, %d, (%p)\n", ssa, icp, fTrailing, pX);
890 if (!ssa || !pX) return S_FALSE;
892 /* icp out of range */
895 analysis->invalid = TRUE;
899 for(i=0; i<analysis->numItems; i++)
901 for(j=0; j<analysis->glyphs[i].numGlyphs; j++)
903 if(runningCp == icp && fTrailing == FALSE)
908 runningX += analysis->glyphs[i].piAdvance[j];
909 if(runningCp == icp && fTrailing == TRUE)
918 /* icp out of range */
919 analysis->invalid = TRUE;
923 /***********************************************************************
924 * ScriptStringXtoCP (USP10.@)
927 HRESULT WINAPI ScriptStringXtoCP(SCRIPT_STRING_ANALYSIS ssa, int iX, int* piCh, int* piTrailing)
929 StringAnalysis* analysis = ssa;
936 TRACE("(%p), %d, (%p), (%p)\n", ssa, iX, piCh, piTrailing);
938 if (!ssa || !piCh || !piTrailing) return S_FALSE;
948 for(i=0; i<analysis->numItems; i++)
950 for(j=0; j<analysis->glyphs[i].numGlyphs; j++)
952 width = analysis->glyphs[i].piAdvance[j];
953 if(iX < (runningX + width))
956 if((iX - runningX) > width/2)
968 *piCh = analysis->pItem[analysis->numItems].iCharPos;
975 /***********************************************************************
976 * ScriptStringFree (USP10.@)
978 * Free a string analysis.
981 * pssa [I] string analysis.
985 * Failure: Non-zero HRESULT value.
987 HRESULT WINAPI ScriptStringFree(SCRIPT_STRING_ANALYSIS *pssa)
989 StringAnalysis* analysis;
993 TRACE("(%p)\n", pssa);
995 if (!pssa || !(analysis = *pssa)) return E_INVALIDARG;
997 invalid = analysis->invalid;
998 ScriptFreeCache((SCRIPT_CACHE *)&analysis->sc);
1000 for (i = 0; i < analysis->numItems; i++)
1002 heap_free(analysis->glyphs[i].glyphs);
1003 heap_free(analysis->glyphs[i].pwLogClust);
1004 heap_free(analysis->glyphs[i].piAdvance);
1005 heap_free(analysis->glyphs[i].psva);
1006 heap_free(analysis->glyphs[i].pGoffset);
1007 heap_free(analysis->glyphs[i].abc);
1010 heap_free(analysis->glyphs);
1011 heap_free(analysis->pItem);
1012 heap_free(analysis->logattrs);
1013 heap_free(analysis->sz);
1014 heap_free(analysis->sc);
1015 heap_free(analysis);
1017 if (invalid) return E_INVALIDARG;
1021 /***********************************************************************
1022 * ScriptCPtoX (USP10.@)
1025 HRESULT WINAPI ScriptCPtoX(int iCP,
1029 const WORD *pwLogClust,
1030 const SCRIPT_VISATTR *psva,
1031 const int *piAdvance,
1032 const SCRIPT_ANALYSIS *psa,
1038 TRACE("(%d,%d,%d,%d,%p,%p,%p,%p,%p)\n",
1039 iCP, fTrailing, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1041 for (item=0; item < cGlyphs; item++) /* total piAdvance */
1042 fMaxPosX += piAdvance[item];
1043 iPosX = (fMaxPosX/cGlyphs)*(iCP+fTrailing);
1044 if (iPosX > fMaxPosX)
1046 *piX = iPosX; /* Return something in range */
1048 TRACE("*piX=%d\n", *piX);
1052 /***********************************************************************
1053 * ScriptXtoCP (USP10.@)
1056 HRESULT WINAPI ScriptXtoCP(int iX,
1059 const WORD *pwLogClust,
1060 const SCRIPT_VISATTR *psva,
1061 const int *piAdvance,
1062 const SCRIPT_ANALYSIS *psa,
1070 TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
1071 iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
1072 psa, piCP, piTrailing);
1073 if (iX < 0) /* iX is before start of run */
1080 for (item=0; item < cGlyphs; item++) /* total piAdvance */
1081 fMaxPosX += piAdvance[item];
1083 if (iX >= fMaxPosX) /* iX too large */
1086 *piTrailing = FALSE;
1090 fAvePosX = fMaxPosX / cGlyphs;
1092 for (item = 1; item < cGlyphs && iPosX < iX; item++)
1094 if (iPosX - iX > fAvePosX/2)
1097 *piTrailing = 1; /* yep we are over halfway */
1099 *piCP = item -1; /* Return character position */
1100 TRACE("*piCP=%d iPposX=%d\n", *piCP, iPosX);
1104 /***********************************************************************
1105 * ScriptBreak (USP10.@)
1107 * Retrieve line break information.
1110 * chars [I] Array of characters.
1111 * sa [I] String analysis.
1112 * la [I] Array of logical attribute structures.
1118 HRESULT WINAPI ScriptBreak(const WCHAR *chars, int count, const SCRIPT_ANALYSIS *sa, SCRIPT_LOGATTR *la)
1122 TRACE("(%s, %d, %p, %p)\n", debugstr_wn(chars, count), count, sa, la);
1124 if (!la) return S_FALSE;
1126 for (i = 0; i < count; i++)
1128 memset(&la[i], 0, sizeof(SCRIPT_LOGATTR));
1130 /* FIXME: set the other flags */
1131 la[i].fWhiteSpace = (chars[i] == ' ');
1132 la[i].fCharStop = 1;
1134 if (i > 0 && la[i - 1].fWhiteSpace)
1136 la[i].fSoftBreak = 1;
1137 la[i].fWordStop = 1;
1151 { 0, 0x0b, SIC_COMPLEX },
1152 { 0x0c, 0x0c, SIC_NEUTRAL },
1153 { 0x0d, 0x1f, SIC_COMPLEX },
1154 { 0x20, 0x2f, SIC_NEUTRAL },
1155 { 0x30, 0x39, SIC_ASCIIDIGIT },
1156 { 0x3a, 0x40, SIC_NEUTRAL },
1157 { 0x5b, 0x60, SIC_NEUTRAL },
1158 { 0x7b, 0x7e, SIC_NEUTRAL },
1159 { 0x7f, 0x9f, SIC_COMPLEX },
1160 { 0xa0, 0xa5, SIC_NEUTRAL },
1161 { 0xa7, 0xa8, SIC_NEUTRAL },
1162 { 0xab, 0xab, SIC_NEUTRAL },
1163 { 0xad, 0xad, SIC_NEUTRAL },
1164 { 0xaf, 0xaf, SIC_NEUTRAL },
1165 { 0xb0, 0xb1, SIC_NEUTRAL },
1166 { 0xb4, 0xb4, SIC_NEUTRAL },
1167 { 0xb6, 0xb8, SIC_NEUTRAL },
1168 { 0xbb, 0xbf, SIC_NEUTRAL },
1169 { 0xd7, 0xd7, SIC_NEUTRAL },
1170 { 0xf7, 0xf7, SIC_NEUTRAL },
1171 { 0x2b9, 0x2ba, SIC_NEUTRAL },
1172 { 0x2c2, 0x2cf, SIC_NEUTRAL },
1173 { 0x2d2, 0x2df, SIC_NEUTRAL },
1174 { 0x2e5, 0x2e9, SIC_COMPLEX },
1175 { 0x2ea, 0x2ed, SIC_NEUTRAL },
1176 { 0x300, 0x362, SIC_COMPLEX },
1177 { 0x530, 0x60b, SIC_COMPLEX },
1178 { 0x60c, 0x60d, SIC_NEUTRAL },
1179 { 0x60e, 0x669, SIC_COMPLEX },
1180 { 0x66a, 0x66a, SIC_NEUTRAL },
1181 { 0x66b, 0x6e8, SIC_COMPLEX },
1182 { 0x6e9, 0x6e9, SIC_NEUTRAL },
1183 { 0x6ea, 0x7bf, SIC_COMPLEX },
1184 { 0x900, 0x1360, SIC_COMPLEX },
1185 { 0x137d, 0x137f, SIC_COMPLEX },
1186 { 0x1680, 0x1680, SIC_NEUTRAL },
1187 { 0x1780, 0x18af, SIC_COMPLEX },
1188 { 0x2000, 0x200a, SIC_NEUTRAL },
1189 { 0x200b, 0x200f, SIC_COMPLEX },
1190 { 0x2010, 0x2016, SIC_NEUTRAL },
1191 { 0x2018, 0x2022, SIC_NEUTRAL },
1192 { 0x2024, 0x2028, SIC_NEUTRAL },
1193 { 0x2029, 0x202e, SIC_COMPLEX },
1194 { 0x202f, 0x2037, SIC_NEUTRAL },
1195 { 0x2039, 0x203c, SIC_NEUTRAL },
1196 { 0x2044, 0x2046, SIC_NEUTRAL },
1197 { 0x206a, 0x206f, SIC_COMPLEX },
1198 { 0x207a, 0x207e, SIC_NEUTRAL },
1199 { 0x208a, 0x20aa, SIC_NEUTRAL },
1200 { 0x20ac, 0x20cf, SIC_NEUTRAL },
1201 { 0x20d0, 0x20ff, SIC_COMPLEX },
1202 { 0x2103, 0x2103, SIC_NEUTRAL },
1203 { 0x2105, 0x2105, SIC_NEUTRAL },
1204 { 0x2109, 0x2109, SIC_NEUTRAL },
1205 { 0x2116, 0x2116, SIC_NEUTRAL },
1206 { 0x2121, 0x2122, SIC_NEUTRAL },
1207 { 0x212e, 0x212e, SIC_NEUTRAL },
1208 { 0x2153, 0x2154, SIC_NEUTRAL },
1209 { 0x215b, 0x215e, SIC_NEUTRAL },
1210 { 0x2190, 0x2199, SIC_NEUTRAL },
1211 { 0x21b8, 0x21b9, SIC_NEUTRAL },
1212 { 0x21d2, 0x21d2, SIC_NEUTRAL },
1213 { 0x21d4, 0x21d4, SIC_NEUTRAL },
1214 { 0x21e7, 0x21e7, SIC_NEUTRAL },
1215 { 0x2200, 0x2200, SIC_NEUTRAL },
1216 { 0x2202, 0x2203, SIC_NEUTRAL },
1217 { 0x2207, 0x2208, SIC_NEUTRAL },
1218 { 0x220b, 0x220b, SIC_NEUTRAL },
1219 { 0x220f, 0x220f, SIC_NEUTRAL },
1220 { 0x2211, 0x2213, SIC_NEUTRAL },
1221 { 0x2215, 0x2215, SIC_NEUTRAL },
1222 { 0x221a, 0x221a, SIC_NEUTRAL },
1223 { 0x221d, 0x2220, SIC_NEUTRAL },
1224 { 0x2223, 0x2223, SIC_NEUTRAL },
1225 { 0x2225, 0x2225, SIC_NEUTRAL },
1226 { 0x2227, 0x222c, SIC_NEUTRAL },
1227 { 0x222e, 0x222e, SIC_NEUTRAL },
1228 { 0x2234, 0x2237, SIC_NEUTRAL },
1229 { 0x223c, 0x223d, SIC_NEUTRAL },
1230 { 0x2248, 0x2248, SIC_NEUTRAL },
1231 { 0x224c, 0x224c, SIC_NEUTRAL },
1232 { 0x2252, 0x2252, SIC_NEUTRAL },
1233 { 0x2260, 0x2261, SIC_NEUTRAL },
1234 { 0x2264, 0x2267, SIC_NEUTRAL },
1235 { 0x226a, 0x226b, SIC_NEUTRAL },
1236 { 0x226e, 0x226f, SIC_NEUTRAL },
1237 { 0x2282, 0x2283, SIC_NEUTRAL },
1238 { 0x2286, 0x2287, SIC_NEUTRAL },
1239 { 0x2295, 0x2295, SIC_NEUTRAL },
1240 { 0x2299, 0x2299, SIC_NEUTRAL },
1241 { 0x22a5, 0x22a5, SIC_NEUTRAL },
1242 { 0x22bf, 0x22bf, SIC_NEUTRAL },
1243 { 0x2312, 0x2312, SIC_NEUTRAL },
1244 { 0x24ea, 0x24ea, SIC_COMPLEX },
1245 { 0x2500, 0x254b, SIC_NEUTRAL },
1246 { 0x2550, 0x256d, SIC_NEUTRAL },
1247 { 0x256e, 0x2574, SIC_NEUTRAL },
1248 { 0x2581, 0x258f, SIC_NEUTRAL },
1249 { 0x2592, 0x2595, SIC_NEUTRAL },
1250 { 0x25a0, 0x25a1, SIC_NEUTRAL },
1251 { 0x25a3, 0x25a9, SIC_NEUTRAL },
1252 { 0x25b2, 0x25b3, SIC_NEUTRAL },
1253 { 0x25b6, 0x25b7, SIC_NEUTRAL },
1254 { 0x25bc, 0x25bd, SIC_NEUTRAL },
1255 { 0x25c0, 0x25c1, SIC_NEUTRAL },
1256 { 0x25c6, 0x25c8, SIC_NEUTRAL },
1257 { 0x25cb, 0x25cb, SIC_NEUTRAL },
1258 { 0x25ce, 0x25d1, SIC_NEUTRAL },
1259 { 0x25e2, 0x25e5, SIC_NEUTRAL },
1260 { 0x25ef, 0x25ef, SIC_NEUTRAL },
1261 { 0x2605, 0x2606, SIC_NEUTRAL },
1262 { 0x2609, 0x2609, SIC_NEUTRAL },
1263 { 0x260e, 0x260f, SIC_NEUTRAL },
1264 { 0x261c, 0x261c, SIC_NEUTRAL },
1265 { 0x261e, 0x261e, SIC_NEUTRAL },
1266 { 0x2640, 0x2640, SIC_NEUTRAL },
1267 { 0x2642, 0x2642, SIC_NEUTRAL },
1268 { 0x2660, 0x2661, SIC_NEUTRAL },
1269 { 0x2663, 0x2665, SIC_NEUTRAL },
1270 { 0x2667, 0x266a, SIC_NEUTRAL },
1271 { 0x266c, 0x266d, SIC_NEUTRAL },
1272 { 0x266f, 0x266f, SIC_NEUTRAL },
1273 { 0x273d, 0x273d, SIC_NEUTRAL },
1274 { 0x2e80, 0x312f, SIC_COMPLEX },
1275 { 0x3190, 0x31bf, SIC_COMPLEX },
1276 { 0x31f0, 0x31ff, SIC_COMPLEX },
1277 { 0x3220, 0x325f, SIC_COMPLEX },
1278 { 0x3280, 0xa4ff, SIC_COMPLEX },
1279 { 0xd800, 0xdfff, SIC_COMPLEX },
1280 { 0xe000, 0xf8ff, SIC_NEUTRAL },
1281 { 0xf900, 0xfaff, SIC_COMPLEX },
1282 { 0xfb13, 0xfb28, SIC_COMPLEX },
1283 { 0xfb29, 0xfb29, SIC_NEUTRAL },
1284 { 0xfb2a, 0xfb4f, SIC_COMPLEX },
1285 { 0xfd3e, 0xfd3f, SIC_NEUTRAL },
1286 { 0xfdd0, 0xfdef, SIC_COMPLEX },
1287 { 0xfe20, 0xfe6f, SIC_COMPLEX },
1288 { 0xfeff, 0xfeff, SIC_COMPLEX },
1289 { 0xff01, 0xff5e, SIC_COMPLEX },
1290 { 0xff61, 0xff9f, SIC_COMPLEX },
1291 { 0xffe0, 0xffe6, SIC_COMPLEX },
1292 { 0xffe8, 0xffee, SIC_COMPLEX },
1293 { 0xfff9, 0xfffb, SIC_COMPLEX },
1294 { 0xfffe, 0xfffe, SIC_COMPLEX }
1297 /***********************************************************************
1298 * ScriptIsComplex (USP10.@)
1300 * Determine if a string is complex.
1303 * chars [I] Array of characters to test.
1304 * len [I] Length in characters.
1312 * Behaviour matches that of WinXP.
1314 HRESULT WINAPI ScriptIsComplex(const WCHAR *chars, int len, DWORD flag)
1319 TRACE("(%s,%d,0x%x)\n", debugstr_wn(chars, len), len, flag);
1321 for (i = 0; i < len; i++)
1323 for (j = 0; j < sizeof(complex_ranges)/sizeof(complex_ranges[0]); j++)
1325 if (chars[i] >= complex_ranges[j].start &&
1326 chars[i] <= complex_ranges[j].end &&
1327 (flag & complex_ranges[j].flag)) return S_OK;
1333 /***********************************************************************
1334 * ScriptShape (USP10.@)
1336 * Produce glyphs and visual attributes for a run.
1339 * hdc [I] Device context.
1340 * psc [I/O] Opaque pointer to a script cache.
1341 * pwcChars [I] Array of characters specifying the run.
1342 * cChars [I] Number of characters in pwcChars.
1343 * cMaxGlyphs [I] Length of pwOutGlyphs.
1344 * psa [I/O] Script analysis.
1345 * pwOutGlyphs [O] Array of glyphs.
1346 * pwLogClust [O] Array of logical cluster info.
1347 * psva [O] Array of visual attributes.
1348 * pcGlyphs [O] Number of glyphs returned.
1352 * Failure: Non-zero HRESULT value.
1354 HRESULT WINAPI ScriptShape(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcChars,
1355 int cChars, int cMaxGlyphs,
1356 SCRIPT_ANALYSIS *psa, WORD *pwOutGlyphs, WORD *pwLogClust,
1357 SCRIPT_VISATTR *psva, int *pcGlyphs)
1363 TRACE("(%p, %p, %s, %d, %d, %p, %p, %p, %p, %p)\n", hdc, psc, debugstr_wn(pwcChars, cChars),
1364 cChars, cMaxGlyphs, psa, pwOutGlyphs, pwLogClust, psva, pcGlyphs);
1366 if (psa) TRACE("psa values: %d, %d, %d, %d, %d, %d, %d\n", psa->eScript, psa->fRTL, psa->fLayoutRTL,
1367 psa->fLinkBefore, psa->fLinkAfter, psa->fLogicalOrder, psa->fNoGlyphIndex);
1369 if (!psva || !pcGlyphs) return E_INVALIDARG;
1370 if (cChars > cMaxGlyphs) return E_OUTOFMEMORY;
1371 rtl = (!psa->fLogicalOrder && psa->fRTL);
1374 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1375 if (!pwLogClust) return E_FAIL;
1377 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1379 WCHAR *rChars = heap_alloc(sizeof(WCHAR) * cChars);
1380 for (i = 0; i < cChars; i++)
1384 if (rtl) idx = cChars - 1 - i;
1386 chInput = mirror_char(pwcChars[idx]);
1388 chInput = pwcChars[idx];
1389 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, chInput)))
1392 if (!hdc) return E_PENDING;
1393 if (GetGlyphIndicesW(hdc, &chInput, 1, &glyph, 0) == GDI_ERROR) return S_FALSE;
1394 pwOutGlyphs[i] = set_cache_glyph(psc, chInput, glyph);
1396 rChars[i] = chInput;
1398 SHAPE_ShapeArabicGlyphs(hdc, (ScriptCache *)*psc, psa, rChars, cChars, pwOutGlyphs, cMaxGlyphs);
1403 TRACE("no glyph translation\n");
1404 for (i = 0; i < cChars; i++)
1407 /* No mirroring done here */
1408 if (rtl) idx = cChars - 1 - i;
1409 pwOutGlyphs[i] = pwcChars[idx];
1413 /* set up a valid SCRIPT_VISATTR and LogClust for each char in this run */
1414 for (i = 0; i < cChars; i++)
1417 if (rtl) idx = cChars - 1 - i;
1418 /* FIXME: set to better values */
1419 psva[i].uJustification = (pwcChars[idx] == ' ') ? SCRIPT_JUSTIFY_BLANK : SCRIPT_JUSTIFY_CHARACTER;
1420 psva[i].fClusterStart = 1;
1421 psva[i].fDiacritic = 0;
1422 psva[i].fZeroWidth = 0;
1423 psva[i].fReserved = 0;
1424 psva[i].fShapeReserved = 0;
1426 pwLogClust[i] = idx;
1431 /***********************************************************************
1432 * ScriptPlace (USP10.@)
1434 * Produce advance widths for a run.
1437 * hdc [I] Device context.
1438 * psc [I/O] Opaque pointer to a script cache.
1439 * pwGlyphs [I] Array of glyphs.
1440 * cGlyphs [I] Number of glyphs in pwGlyphs.
1441 * psva [I] Array of visual attributes.
1442 * psa [I/O] String analysis.
1443 * piAdvance [O] Array of advance widths.
1444 * pGoffset [O] Glyph offsets.
1445 * pABC [O] Combined ABC width.
1449 * Failure: Non-zero HRESULT value.
1451 HRESULT WINAPI ScriptPlace(HDC hdc, SCRIPT_CACHE *psc, const WORD *pwGlyphs,
1452 int cGlyphs, const SCRIPT_VISATTR *psva,
1453 SCRIPT_ANALYSIS *psa, int *piAdvance, GOFFSET *pGoffset, ABC *pABC )
1458 TRACE("(%p, %p, %p, %d, %p, %p, %p, %p, %p)\n", hdc, psc, pwGlyphs, cGlyphs, psva, psa,
1459 piAdvance, pGoffset, pABC);
1461 if (!psva) return E_INVALIDARG;
1462 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1463 if (!pGoffset) return E_FAIL;
1465 if (pABC) memset(pABC, 0, sizeof(ABC));
1466 for (i = 0; i < cGlyphs; i++)
1469 if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
1471 if (!hdc) return E_PENDING;
1472 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
1474 if (!GetCharABCWidthsI(hdc, 0, 1, (WORD *)&pwGlyphs[i], &abc)) return S_FALSE;
1479 if (!GetCharWidth32W(hdc, pwGlyphs[i], pwGlyphs[i], &width)) return S_FALSE;
1481 abc.abcA = abc.abcC = 0;
1483 set_cache_glyph_widths(psc, pwGlyphs[i], &abc);
1487 pABC->abcA += abc.abcA;
1488 pABC->abcB += abc.abcB;
1489 pABC->abcC += abc.abcC;
1491 /* FIXME: set to more reasonable values */
1492 pGoffset[i].du = pGoffset[i].dv = 0;
1493 if (piAdvance) piAdvance[i] = abc.abcA + abc.abcB + abc.abcC;
1496 if (pABC) TRACE("Total for run: abcA=%d, abcB=%d, abcC=%d\n", pABC->abcA, pABC->abcB, pABC->abcC);
1500 /***********************************************************************
1501 * ScriptGetCMap (USP10.@)
1503 * Retrieve glyph indices.
1506 * hdc [I] Device context.
1507 * psc [I/O] Opaque pointer to a script cache.
1508 * pwcInChars [I] Array of Unicode characters.
1509 * cChars [I] Number of characters in pwcInChars.
1510 * dwFlags [I] Flags.
1511 * pwOutGlyphs [O] Buffer to receive the array of glyph indices.
1515 * Failure: Non-zero HRESULT value.
1517 HRESULT WINAPI ScriptGetCMap(HDC hdc, SCRIPT_CACHE *psc, const WCHAR *pwcInChars,
1518 int cChars, DWORD dwFlags, WORD *pwOutGlyphs)
1523 TRACE("(%p,%p,%s,%d,0x%x,%p)\n", hdc, psc, debugstr_wn(pwcInChars, cChars),
1524 cChars, dwFlags, pwOutGlyphs);
1526 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1530 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1532 for (i = 0; i < cChars; i++)
1535 if (dwFlags == SGCM_RTL)
1536 inChar = mirror_char(pwcInChars[i]);
1538 inChar = pwcInChars[i];
1539 if (!(pwOutGlyphs[i] = get_cache_glyph(psc, inChar)))
1542 if (!hdc) return E_PENDING;
1543 if (GetGlyphIndicesW(hdc, &inChar, 1, &glyph, GGI_MARK_NONEXISTING_GLYPHS) == GDI_ERROR) return S_FALSE;
1544 if (glyph == 0xffff)
1549 pwOutGlyphs[i] = set_cache_glyph(psc, inChar, glyph);
1555 TRACE("no glyph translation\n");
1556 for (i = 0; i < cChars; i++)
1559 if (dwFlags == SGCM_RTL)
1560 inChar = mirror_char(pwcInChars[i]);
1562 inChar = pwcInChars[i];
1563 pwOutGlyphs[i] = inChar;
1569 /***********************************************************************
1570 * ScriptTextOut (USP10.@)
1573 HRESULT WINAPI ScriptTextOut(const HDC hdc, SCRIPT_CACHE *psc, int x, int y, UINT fuOptions,
1574 const RECT *lprc, const SCRIPT_ANALYSIS *psa, const WCHAR *pwcReserved,
1575 int iReserved, const WORD *pwGlyphs, int cGlyphs, const int *piAdvance,
1576 const int *piJustify, const GOFFSET *pGoffset)
1580 TRACE("(%p, %p, %d, %d, %04x, %p, %p, %p, %d, %p, %d, %p, %p, %p)\n",
1581 hdc, psc, x, y, fuOptions, lprc, psa, pwcReserved, iReserved, pwGlyphs, cGlyphs,
1582 piAdvance, piJustify, pGoffset);
1584 if (!hdc || !psc) return E_INVALIDARG;
1585 if (!piAdvance || !psa || !pwGlyphs) return E_INVALIDARG;
1587 fuOptions &= ETO_CLIPPED + ETO_OPAQUE;
1588 fuOptions |= ETO_IGNORELANGUAGE;
1589 if (!psa->fNoGlyphIndex) /* Have Glyphs? */
1590 fuOptions |= ETO_GLYPH_INDEX; /* Say don't do translation to glyph */
1592 if (psa->fRTL && psa->fLogicalOrder)
1597 rtlGlyphs = heap_alloc(cGlyphs * sizeof(WORD));
1599 return E_OUTOFMEMORY;
1601 for (i = 0; i < cGlyphs; i++)
1602 rtlGlyphs[i] = pwGlyphs[cGlyphs-1-i];
1604 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, rtlGlyphs, cGlyphs, NULL))
1606 heap_free(rtlGlyphs);
1609 if (!ExtTextOutW(hdc, x, y, fuOptions, lprc, pwGlyphs, cGlyphs, NULL))
1615 /***********************************************************************
1616 * ScriptCacheGetHeight (USP10.@)
1618 * Retrieve the height of the font in the cache.
1621 * hdc [I] Device context.
1622 * psc [I/O] Opaque pointer to a script cache.
1623 * height [O] Receives font height.
1627 * Failure: Non-zero HRESULT value.
1629 HRESULT WINAPI ScriptCacheGetHeight(HDC hdc, SCRIPT_CACHE *psc, LONG *height)
1633 TRACE("(%p, %p, %p)\n", hdc, psc, height);
1635 if (!height) return E_INVALIDARG;
1636 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1638 *height = get_cache_height(psc);
1642 /***********************************************************************
1643 * ScriptGetGlyphABCWidth (USP10.@)
1645 * Retrieve the width of a glyph.
1648 * hdc [I] Device context.
1649 * psc [I/O] Opaque pointer to a script cache.
1650 * glyph [I] Glyph to retrieve the width for.
1651 * abc [O] ABC widths of the glyph.
1655 * Failure: Non-zero HRESULT value.
1657 HRESULT WINAPI ScriptGetGlyphABCWidth(HDC hdc, SCRIPT_CACHE *psc, WORD glyph, ABC *abc)
1661 TRACE("(%p, %p, 0x%04x, %p)\n", hdc, psc, glyph, abc);
1663 if (!abc) return E_INVALIDARG;
1664 if ((hr = init_script_cache(hdc, psc)) != S_OK) return hr;
1666 if (!get_cache_glyph_widths(psc, glyph, abc))
1668 if (!hdc) return E_PENDING;
1669 if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE))
1671 if (!GetCharABCWidthsI(hdc, 0, 1, &glyph, abc)) return S_FALSE;
1676 if (!GetCharWidth32W(hdc, glyph, glyph, &width)) return S_FALSE;
1678 abc->abcA = abc->abcC = 0;
1680 set_cache_glyph_widths(psc, glyph, abc);
1685 /***********************************************************************
1686 * ScriptLayout (USP10.@)
1688 * Map embedding levels to visual and/or logical order.
1691 * runs [I] Size of level array.
1692 * level [I] Array of embedding levels.
1693 * vistolog [O] Map of embedding levels from visual to logical order.
1694 * logtovis [O] Map of embedding levels from logical to visual order.
1698 * Failure: Non-zero HRESULT value.
1701 * This stub works correctly for any sequence of a single
1702 * embedding level but not for sequences of different
1703 * embedding levels, i.e. mixtures of RTL and LTR scripts.
1705 HRESULT WINAPI ScriptLayout(int runs, const BYTE *level, int *vistolog, int *logtovis)
1710 TRACE("(%d, %p, %p, %p)\n", runs, level, vistolog, logtovis);
1712 if (!level || (!vistolog && !logtovis))
1713 return E_INVALIDARG;
1715 indexs = heap_alloc(sizeof(int) * runs);
1717 return E_OUTOFMEMORY;
1722 for( ich = 0; ich < runs; ich++)
1727 ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1728 for (ich = 0; ich < runs; ich++)
1729 vistolog[ich] = indexs[ich];
1735 for( ich = 0; ich < runs; ich++)
1740 ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
1741 for (ich = 0; ich < runs; ich++)
1742 logtovis[ich] = indexs[ich];
1749 /***********************************************************************
1750 * ScriptStringGetLogicalWidths (USP10.@)
1752 * Returns logical widths from a string analysis.
1755 * ssa [I] string analysis.
1756 * piDx [O] logical widths returned.
1760 * Failure: a non-zero HRESULT.
1762 HRESULT WINAPI ScriptStringGetLogicalWidths(SCRIPT_STRING_ANALYSIS ssa, int *piDx)
1765 StringAnalysis *analysis = ssa;
1767 TRACE("%p, %p\n", ssa, piDx);
1769 if (!analysis) return S_FALSE;
1771 for (i = 0; i < analysis->numItems; i++)
1773 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1775 piDx[next] = analysis->glyphs[i].piAdvance[j];
1782 /***********************************************************************
1783 * ScriptStringValidate (USP10.@)
1785 * Validate a string analysis.
1788 * ssa [I] string analysis.
1792 * Failure: S_FALSE if invalid sequences are found
1793 * or a non-zero HRESULT if it fails.
1795 HRESULT WINAPI ScriptStringValidate(SCRIPT_STRING_ANALYSIS ssa)
1797 StringAnalysis *analysis = ssa;
1799 TRACE("(%p)\n", ssa);
1801 if (!analysis) return E_INVALIDARG;
1802 return (analysis->invalid) ? S_FALSE : S_OK;
1805 /***********************************************************************
1806 * ScriptString_pSize (USP10.@)
1808 * Retrieve width and height of an analysed string.
1811 * ssa [I] string analysis.
1814 * Success: Pointer to a SIZE structure.
1817 const SIZE * WINAPI ScriptString_pSize(SCRIPT_STRING_ANALYSIS ssa)
1820 StringAnalysis *analysis = ssa;
1822 TRACE("(%p)\n", ssa);
1824 if (!analysis) return NULL;
1828 if (!(analysis->sz = heap_alloc(sizeof(SIZE)))) return NULL;
1829 analysis->sz->cy = analysis->sc->tm.tmHeight;
1831 analysis->sz->cx = 0;
1832 for (i = 0; i < analysis->numItems; i++)
1833 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++)
1834 analysis->sz->cx += analysis->glyphs[i].piAdvance[j];
1836 return analysis->sz;
1839 /***********************************************************************
1840 * ScriptString_pLogAttr (USP10.@)
1842 * Retrieve logical attributes of an analysed string.
1845 * ssa [I] string analysis.
1848 * Success: Pointer to an array of SCRIPT_LOGATTR structures.
1851 const SCRIPT_LOGATTR * WINAPI ScriptString_pLogAttr(SCRIPT_STRING_ANALYSIS ssa)
1853 StringAnalysis *analysis = ssa;
1855 TRACE("(%p)\n", ssa);
1857 if (!analysis) return NULL;
1858 return analysis->logattrs;
1861 /***********************************************************************
1862 * ScriptString_pcOutChars (USP10.@)
1864 * Retrieve the length of a string after clipping.
1867 * ssa [I] String analysis.
1870 * Success: Pointer to the length.
1873 const int * WINAPI ScriptString_pcOutChars(SCRIPT_STRING_ANALYSIS ssa)
1875 StringAnalysis *analysis = ssa;
1877 TRACE("(%p)\n", ssa);
1879 if (!analysis) return NULL;
1880 return &analysis->clip_len;
1883 /***********************************************************************
1884 * ScriptStringGetOrder (USP10.@)
1886 * Retrieve a glyph order map.
1889 * ssa [I] String analysis.
1890 * order [I/O] Array of glyph positions.
1894 * Failure: a non-zero HRESULT.
1896 HRESULT WINAPI ScriptStringGetOrder(SCRIPT_STRING_ANALYSIS ssa, UINT *order)
1900 StringAnalysis *analysis = ssa;
1902 TRACE("(%p)\n", ssa);
1904 if (!analysis) return S_FALSE;
1906 /* FIXME: handle RTL scripts */
1907 for (i = 0, k = 0; i < analysis->numItems; i++)
1908 for (j = 0; j < analysis->glyphs[i].numGlyphs; j++, k++)
1914 /***********************************************************************
1915 * ScriptGetLogicalWidths (USP10.@)
1917 * Convert advance widths to logical widths.
1920 * sa [I] Script analysis.
1921 * nbchars [I] Number of characters.
1922 * nbglyphs [I] Number of glyphs.
1923 * glyph_width [I] Array of glyph widths.
1924 * log_clust [I] Array of logical clusters.
1925 * sva [I] Visual attributes.
1926 * widths [O] Array of logical widths.
1930 * Failure: a non-zero HRESULT.
1932 HRESULT WINAPI ScriptGetLogicalWidths(const SCRIPT_ANALYSIS *sa, int nbchars, int nbglyphs,
1933 const int *glyph_width, const WORD *log_clust,
1934 const SCRIPT_VISATTR *sva, int *widths)
1938 TRACE("(%p, %d, %d, %p, %p, %p, %p)\n",
1939 sa, nbchars, nbglyphs, glyph_width, log_clust, sva, widths);
1942 for (i = 0; i < nbchars; i++) widths[i] = glyph_width[i];
1946 /***********************************************************************
1947 * ScriptApplyLogicalWidth (USP10.@)
1949 * Generate glyph advance widths.
1952 * dx [I] Array of logical advance widths.
1953 * num_chars [I] Number of characters.
1954 * num_glyphs [I] Number of glyphs.
1955 * log_clust [I] Array of logical clusters.
1956 * sva [I] Visual attributes.
1957 * advance [I] Array of glyph advance widths.
1958 * sa [I] Script analysis.
1959 * abc [I/O] Summed ABC widths.
1960 * justify [O] Array of glyph advance widths.
1964 * Failure: a non-zero HRESULT.
1966 HRESULT WINAPI ScriptApplyLogicalWidth(const int *dx, int num_chars, int num_glyphs,
1967 const WORD *log_clust, const SCRIPT_VISATTR *sva,
1968 const int *advance, const SCRIPT_ANALYSIS *sa,
1969 ABC *abc, int *justify)
1973 FIXME("(%p, %d, %d, %p, %p, %p, %p, %p, %p)\n",
1974 dx, num_chars, num_glyphs, log_clust, sva, advance, sa, abc, justify);
1976 for (i = 0; i < num_chars; i++) justify[i] = advance[i];
1980 HRESULT WINAPI ScriptJustify(const SCRIPT_VISATTR *sva, const int *advance,
1981 int num_glyphs, int dx, int min_kashida, int *justify)
1985 FIXME("(%p, %p, %d, %d, %d, %p)\n", sva, advance, num_glyphs, dx, min_kashida, justify);
1987 for (i = 0; i < num_glyphs; i++) justify[i] = advance[i];