#include "usp10_internal.h"
+#include <math.h>
#include <winuser.h>
#include <winreg.h>
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
}
-static inline void *heap_realloc_zero(LPVOID mem, SIZE_T size)
-{
- return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size);
-}
-
static inline BOOL heap_free(LPVOID mem)
{
return HeapFree(GetProcessHeap(), 0, mem);
int New_Script = -1;
int i;
WORD *levels = NULL;
+ WORD *layout_levels = NULL;
+ WORD *overrides = NULL;
WORD *strength = NULL;
WORD *scripts = NULL;
WORD baselevel = 0;
+ WORD baselayout = 0;
BOOL new_run;
WORD last_indic = -1;
WORD layoutRTL = 0;
else if (is_indic(scripts[i]))
last_indic = base_indic(scripts[i]);
- /* Some unicode points (Zero Width Space U+200B -
- Right-to-Left Mark U+200F) will force us into bidi mode */
- if (!forceLevels && pwcInChars[i] >= 0x200B && pwcInChars[i] <= 0x200F)
+ /* Some unicode points :
+ (Zero Width Space U+200B - Right-to-Left Mark U+200F)
+ (Left Right Embed U+202A - Left Right Override U+202D)
+ (Left Right Isolate U+2066 - Pop Directional Isolate U+2069)
+ will force us into bidi mode */
+ if (!forceLevels && ((pwcInChars[i] >= 0x200B && pwcInChars[i] <= 0x200F) ||
+ (pwcInChars[i] >= 0x202A && pwcInChars[i] <= 0x202E) ||
+ (pwcInChars[i] >= 0x2066 && pwcInChars[i] <= 0x2069)))
+
forceLevels = TRUE;
/* Diacritical marks merge with other scripts */
if (!levels)
goto nomemory;
- BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels);
+ overrides = heap_alloc_zero(cInChars * sizeof(WORD));
+ if (!overrides)
+ goto nomemory;
+
+ layout_levels = heap_alloc_zero(cInChars * sizeof(WORD));
+ if (!layout_levels)
+ goto nomemory;
+
+ if (psState->fOverrideDirection)
+ {
+ if (!forceLevels)
+ {
+ SCRIPT_STATE s = *psState;
+ s.fOverrideDirection = FALSE;
+ BIDI_DetermineLevels(pwcInChars, cInChars, &s, psControl, layout_levels, overrides);
+ if (odd(layout_levels[0]))
+ forceLevels = TRUE;
+ else for (i = 0; i < cInChars; i++)
+ if (layout_levels[i]!=layout_levels[0])
+ {
+ forceLevels = TRUE;
+ break;
+ }
+ }
+
+ BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels, overrides);
+ }
+ else
+ {
+ BIDI_DetermineLevels(pwcInChars, cInChars, psState, psControl, levels, overrides);
+ memcpy(layout_levels, levels, cInChars * sizeof(WORD));
+ }
baselevel = levels[0];
+ baselayout = layout_levels[0];
for (i = 0; i < cInChars; i++)
if (levels[i]!=levels[0])
break;
if (i >= cInChars && !odd(baselevel) && !odd(psState->uBidiLevel) && !forceLevels)
{
heap_free(levels);
+ heap_free(overrides);
+ heap_free(layout_levels);
+ overrides = NULL;
levels = NULL;
+ layout_levels = NULL;
}
else
{
- BOOL inNumber = FALSE;
static const WCHAR math_punc[] = {'#','$','%','+',',','-','.','/',':',0x2212, 0x2044, 0x00a0,0};
+ static const WCHAR repeatable_math_punc[] = {'#','$','%','+','-','/',0x2212, 0x2044,0};
strength = heap_alloc_zero(cInChars * sizeof(WORD));
if (!strength)
strength[i] = BIDI_STRONG;
}
+ /* Math punctuation bordered on both sides by numbers can be
+ merged into the number */
for (i = 0; i < cInChars; i++)
{
- /* Script_Numeric and select puncuation at level 0 get bumped to level 2 */
- if ((levels[i] == 0 || (odd(psState->uBidiLevel) && levels[i] == psState->uBidiLevel+1)) && inNumber && strchrW(math_punc,pwcInChars[i]))
+ if (i > 0 && i < cInChars-1 &&
+ scripts[i-1] == Script_Numeric &&
+ strchrW(math_punc, pwcInChars[i]))
{
- scripts[i] = Script_Numeric;
- levels[i] = 2;
+ if (scripts[i+1] == Script_Numeric)
+ {
+ scripts[i] = Script_Numeric;
+ levels[i] = levels[i-1];
+ strength[i] = strength[i-1];
+ i++;
+ }
+ else if (strchrW(repeatable_math_punc, pwcInChars[i]))
+ {
+ int j;
+ for (j = i+1; j < cInChars; j++)
+ {
+ if (scripts[j] == Script_Numeric)
+ {
+ for(;i<j; i++)
+ {
+ scripts[i] = Script_Numeric;
+ levels[i] = levels[i-1];
+ strength[i] = strength[i-1];
+ }
+ }
+ else if (pwcInChars[i] != pwcInChars[j]) break;
+ }
+ }
}
- else if ((levels[i] == 0 || (odd(psState->uBidiLevel) && levels[i] == psState->uBidiLevel+1)) && scripts[i] == Script_Numeric)
+ }
+
+ for (i = 0; i < cInChars; i++)
+ {
+ /* Script_Numeric at level 0 get bumped to level 2 */
+ if (!overrides[i] && (levels[i] == 0 || (odd(psState->uBidiLevel) && levels[i] == psState->uBidiLevel+1)) && scripts[i] == Script_Numeric)
{
levels[i] = 2;
- inNumber = TRUE;
}
- else
- inNumber = FALSE;
/* Joiners get merged preferencially right */
if (i > 0 && (pwcInChars[i] == ZWJ || pwcInChars[i] == ZWNJ))
if (levels)
{
if (strength[cnt] == BIDI_STRONG)
- layoutRTL = (odd(levels[cnt]))?1:0;
+ layoutRTL = odd(layout_levels[cnt]);
else
- layoutRTL = (psState->uBidiLevel || odd(levels[cnt]))?1:0;
+ layoutRTL = (psState->uBidiLevel || odd(layout_levels[cnt]));
+ if (overrides)
+ pItems[index].a.s.fOverrideDirection = (overrides[cnt] != 0);
pItems[index].a.fRTL = odd(levels[cnt]);
- pItems[index].a.fLayoutRTL = layoutRTL;
+ if (pItems[index].a.eScript == Script_Numeric ||
+ pItems[index].a.eScript == Script_Numeric2)
+ pItems[index].a.fLayoutRTL = layoutRTL;
+ else
+ pItems[index].a.fLayoutRTL = pItems[index].a.fRTL;
pItems[index].a.s.uBidiLevel = levels[cnt];
}
- else if (!pItems[index].a.s.uBidiLevel)
+ else if (!pItems[index].a.s.uBidiLevel || (overrides && overrides[cnt]))
{
- layoutRTL = (odd(baselevel))?1:0;
+ if (pItems[index].a.s.uBidiLevel != baselevel)
+ pItems[index].a.s.fOverrideDirection = TRUE;
+ layoutRTL = odd(baselayout);
pItems[index].a.s.uBidiLevel = baselevel;
- pItems[index].a.fLayoutRTL = odd(baselevel);
pItems[index].a.fRTL = odd(baselevel);
+ if (pItems[index].a.eScript == Script_Numeric ||
+ pItems[index].a.eScript == Script_Numeric2)
+ pItems[index].a.fLayoutRTL = odd(baselayout);
+ else
+ pItems[index].a.fLayoutRTL = pItems[index].a.fRTL;
}
TRACE("New_Level=%i New_Strength=%i New_Script=%d, eScript=%d index=%d cnt=%d iCharPos=%d\n",
if (!new_run && strength && str == BIDI_STRONG)
{
- layoutRTL = odd(levels[cnt])?1:0;
- pItems[index].a.fLayoutRTL = layoutRTL;
+ layoutRTL = odd(layout_levels[cnt]);
+ if (pItems[index].a.eScript == Script_Numeric ||
+ pItems[index].a.eScript == Script_Numeric2)
+ pItems[index].a.fLayoutRTL = layoutRTL;
}
if (new_run)
pScriptTags[index] = scriptInformation[New_Script].scriptTag;
if (levels)
{
- if (levels[cnt] == 0)
+ if (overrides)
+ pItems[index].a.s.fOverrideDirection = (overrides[cnt] != 0);
+ if (layout_levels[cnt] == 0)
layoutRTL = 0;
else
- layoutRTL = (layoutRTL || odd(levels[cnt]))?1:0;
+ layoutRTL = (layoutRTL || odd(layout_levels[cnt]));
pItems[index].a.fRTL = odd(levels[cnt]);
- pItems[index].a.fLayoutRTL = layoutRTL;
+ if (pItems[index].a.eScript == Script_Numeric ||
+ pItems[index].a.eScript == Script_Numeric2)
+ pItems[index].a.fLayoutRTL = layoutRTL;
+ else
+ pItems[index].a.fLayoutRTL = pItems[index].a.fRTL;
pItems[index].a.s.uBidiLevel = levels[cnt];
}
- else if (!pItems[index].a.s.uBidiLevel)
+ else if (!pItems[index].a.s.uBidiLevel || (overrides && overrides[cnt]))
{
+ if (pItems[index].a.s.uBidiLevel != baselevel)
+ pItems[index].a.s.fOverrideDirection = TRUE;
pItems[index].a.s.uBidiLevel = baselevel;
- pItems[index].a.fLayoutRTL = layoutRTL;
pItems[index].a.fRTL = odd(baselevel);
+ if (pItems[index].a.eScript == Script_Numeric||
+ pItems[index].a.eScript == Script_Numeric2)
+ pItems[index].a.fLayoutRTL = layoutRTL;
+ else
+ pItems[index].a.fLayoutRTL = pItems[index].a.fRTL;
}
TRACE("index=%d cnt=%d iCharPos=%d\n", index, cnt, pItems[index].iCharPos);
res = S_OK;
nomemory:
heap_free(levels);
+ heap_free(overrides);
+ heap_free(layout_levels);
heap_free(strength);
heap_free(scripts);
return res;
return S_OK;
}
+/* Count the number of characters in a cluster and its starting index*/
+static inline BOOL get_cluster_data(const WORD *pwLogClust, int cChars, int cluster_index, int *cluster_size, int *start_index)
+{
+ int size = 0;
+ int i;
+
+ for (i = 0; i < cChars; i++)
+ {
+ if (pwLogClust[i] == cluster_index)
+ {
+ if (!size && start_index)
+ {
+ *start_index = i;
+ if (!cluster_size)
+ return TRUE;
+ }
+ size++;
+ }
+ else if (size) break;
+ }
+ if (cluster_size)
+ *cluster_size = size;
+
+ return (size > 0);
+}
+
+/*
+ To handle multi-glyph clusters we need to find all the glyphs that are
+ represented in the cluster. This involves finding the glyph whose
+ index is the cluster index as well as whose glyph indices are greater than
+ our cluster index but not part of a new cluster.
+
+ Then we sum all those glyphs' advances.
+*/
+static inline int get_cluster_advance(const int* piAdvance,
+ const SCRIPT_VISATTR *psva,
+ const WORD *pwLogClust, int cGlyphs,
+ int cChars, int cluster, int direction)
+{
+ int glyph_start;
+ int glyph_end;
+ int i, advance;
+
+ if (direction > 0)
+ i = 0;
+ else
+ i = (cChars - 1);
+
+ for (glyph_start = -1, glyph_end = -1; i < cChars && i >= 0 && (glyph_start < 0 || glyph_end < 0); i+=direction)
+ {
+ if (glyph_start < 0 && pwLogClust[i] != cluster) continue;
+ if (pwLogClust[i] == cluster && glyph_start < 0) glyph_start = pwLogClust[i];
+ if (glyph_start >= 0 && glyph_end < 0 && pwLogClust[i] != cluster) glyph_end = pwLogClust[i];
+ }
+ if (glyph_end < 0)
+ {
+ if (direction > 0)
+ glyph_end = cGlyphs;
+ else
+ {
+ /* Don't fully understand multi-glyph reversed clusters yet,
+ * do they occur for real or just in our test? */
+ FIXME("multi-glyph reversed clusters found\n");
+ glyph_end = glyph_start + 1;
+ }
+ }
+
+ /* Check for fClusterStart, finding this generally would mean a malformed set of data */
+ for (i = glyph_start+1; i< glyph_end; i++)
+ {
+ if (psva[i].fClusterStart)
+ {
+ glyph_end = i;
+ break;
+ }
+ }
+
+ for (advance = 0, i = glyph_start; i < glyph_end; i++)
+ advance += piAdvance[i];
+
+ return advance;
+}
+
+
/***********************************************************************
* ScriptXtoCP (USP10.@)
*
+ * Basic algorithm :
+ * use piAdvance to find the cluster we are looking at
+ * Find the character that is the first character of the cluster
+ * That is our base piCP
+ * If the script snaps to cluster boundries (Hebrew, Indic, Thai) then we
+ * are good Otherwise if the cluster is larger than 1 glyph we need to
+ * determine how far through the cluster to advance the cursor.
*/
HRESULT WINAPI ScriptXtoCP(int iX,
int cChars,
int *piCP,
int *piTrailing)
{
- int item;
- float iPosX;
- float iLastPosX;
- int iSpecial = -1;
- int iCluster = -1;
- int clust_size = 1;
- int cjump = 0;
- int advance;
- float special_size = 0.0;
int direction = 1;
+ int iPosX;
+ int i;
+ int glyph_index, cluster_index;
+ int cluster_size;
TRACE("(%d,%d,%d,%p,%p,%p,%p,%p,%p)\n",
iX, cChars, cGlyphs, pwLogClust, psva, piAdvance,
if (psa->fRTL && ! psa->fLogicalOrder)
direction = -1;
- if (direction<0)
+ /* Handle an iX < 0 */
+ if (iX < 0)
{
- int max_clust = pwLogClust[0];
-
- if (iX < 0)
+ if (direction < 0)
{
*piCP = cChars;
*piTrailing = 0;
- return S_OK;
}
+ else
+ {
+ *piCP = -1;
+ *piTrailing = 1;
+ }
+ return S_OK;
+ }
- for (item=0; item < cChars; item++)
- if (pwLogClust[item] > max_clust)
+ /* Looking for non-reversed clusters in a reversed string */
+ if (direction < 0)
+ {
+ int max_clust = pwLogClust[0];
+ for (i=0; i< cChars; i++)
+ if (pwLogClust[i] > max_clust)
{
- ERR("We do not handle non reversed clusters properly\n");
+ FIXME("We do not handle non reversed clusters properly\n");
break;
}
}
- if (iX < 0)
+ /* find the glyph_index based in iX */
+ if (direction > 0)
{
- *piCP = -1;
- *piTrailing = 1;
- return S_OK;
+ for (glyph_index = -1, iPosX = iX; iPosX >=0 && glyph_index < cGlyphs; iPosX -= piAdvance[glyph_index+1], glyph_index++)
+ ;
}
-
- iPosX = iLastPosX = 0;
- if (direction > 0)
- item = 0;
else
- item = cChars - 1;
- for (; iPosX <= iX && item < cChars && item >= 0; item+=direction)
- {
- iLastPosX = iPosX;
- if (iSpecial == -1 &&
- (iCluster == -1 ||
- (iCluster != -1 &&
- ((direction > 0 && iCluster+clust_size <= item) ||
- (direction < 0 && iCluster-clust_size >= item))
- )
- )
- )
+ {
+ for (glyph_index = -1, iPosX = iX; iPosX > 0 && glyph_index < cGlyphs; iPosX -= piAdvance[glyph_index+1], glyph_index++)
+ ;
+ }
+
+ TRACE("iPosX %i -> glyph_index %i (%i)\n", iPosX, glyph_index, cGlyphs);
+
+ *piTrailing = 0;
+ if (glyph_index >= 0 && glyph_index < cGlyphs)
+ {
+ /* find the cluster */
+ if (direction > 0 )
+ for (i = 0, cluster_index = pwLogClust[0]; i < cChars && pwLogClust[i] <= glyph_index; cluster_index=pwLogClust[i++])
+ ;
+ else
+ for (i = 0, cluster_index = pwLogClust[0]; i < cChars && pwLogClust[i] >= glyph_index; cluster_index=pwLogClust[i++])
+ ;
+
+ TRACE("cluster_index %i\n", cluster_index);
+
+ if (direction < 0 && iPosX >= 0 && glyph_index != cluster_index)
{
- int check;
- int clust = pwLogClust[item];
+ /* We are off the end of the string */
+ *piCP = -1;
+ *piTrailing = 1;
+ return S_OK;
+ }
- iCluster = -1;
- cjump = 0;
- clust_size = get_cluster_size(pwLogClust, cChars, item, direction,
- &iCluster, &check);
- advance = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, clust, direction);
+ get_cluster_data(pwLogClust, cChars, cluster_index, &cluster_size, &i);
- if (check >= cChars && direction > 0)
- {
- int glyph;
- for (glyph = clust; glyph < cGlyphs; glyph++)
- special_size += get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, glyph, direction);
- iSpecial = item;
- special_size /= (cChars - item);
- iPosX += special_size;
- }
- else
+ TRACE("first char index %i\n",i);
+ if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
+ {
+ /* Check trailing */
+ if (glyph_index != cluster_index ||
+ (direction > 0 && abs(iPosX) <= (piAdvance[glyph_index] / 2)) ||
+ (direction < 0 && abs(iPosX) >= (piAdvance[glyph_index] / 2)))
+ *piTrailing = cluster_size;
+ }
+ else
+ {
+ if (cluster_size > 1)
{
- if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
+ /* Be part way through the glyph cluster based on size and position */
+ int cluster_advance = get_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, cluster_index, direction);
+ double cluster_part_width = cluster_advance / (float)cluster_size;
+ double adv;
+ int part_index;
+
+ /* back up to the beginning of the cluster */
+ for (adv = iPosX, part_index = cluster_index; part_index <= glyph_index; part_index++)
+ adv += piAdvance[part_index];
+ if (adv > iX) adv = iX;
+
+ TRACE("Multi-char cluster, no snap\n");
+ TRACE("cluster size %i, pre-cluster iPosX %f\n",cluster_size, adv);
+ TRACE("advance %i divides into %f per char\n", cluster_advance, cluster_part_width);
+ if (direction > 0)
{
- if (!cjump)
- iPosX += advance;
- cjump++;
+ for (part_index = 0; adv >= 0; adv-=cluster_part_width, part_index++)
+ ;
+ if (part_index) part_index--;
}
else
- iPosX += advance / (float)clust_size;
+ {
+ for (part_index = 0; adv > 0; adv-=cluster_part_width, part_index++)
+ ;
+ if (part_index > cluster_size)
+ {
+ adv += cluster_part_width;
+ part_index=cluster_size;
+ }
+ }
+
+ TRACE("base_char %i part_index %i, leftover advance %f\n",i, part_index, adv);
+
+ if (direction > 0)
+ i += part_index;
+ else
+ i += (cluster_size - part_index);
+
+ /* Check trailing */
+ if ((direction > 0 && fabs(adv) <= (cluster_part_width / 2.0)) ||
+ (direction < 0 && adv && fabs(adv) >= (cluster_part_width / 2.0)))
+ *piTrailing = 1;
}
- }
- else if (iSpecial != -1)
- iPosX += special_size;
- else /* (iCluster != -1) */
- {
- int adv = get_glyph_cluster_advance(piAdvance, psva, pwLogClust, cGlyphs, cChars, pwLogClust[iCluster], direction);
- if (scriptInformation[psa->eScript].props.fNeedsCaretInfo)
+ else
{
- if (!cjump)
- iPosX += adv;
- cjump++;
+ /* Check trailing */
+ if ((direction > 0 && abs(iPosX) <= (piAdvance[glyph_index] / 2)) ||
+ (direction < 0 && abs(iPosX) >= (piAdvance[glyph_index] / 2)))
+ *piTrailing = 1;
}
- else
- iPosX += adv / (float)clust_size;
}
}
-
- if (direction > 0)
- {
- if (iPosX > iX)
- item--;
- if (item < cChars && ((iPosX - iLastPosX) / 2.0) + iX >= iPosX)
- {
- if (scriptInformation[psa->eScript].props.fNeedsCaretInfo && clust_size > 1)
- item+=(clust_size-1);
- *piTrailing = 1;
- }
- else
- *piTrailing = 0;
- }
else
{
- if (iX == iLastPosX)
- item++;
- if (iX >= iLastPosX && iX <= iPosX)
- item++;
+ TRACE("Point falls outside of string\n");
+ if (glyph_index < 0)
+ i = cChars-1;
+ else /* (glyph_index >= cGlyphs) */
+ i = cChars;
- if (iLastPosX == iX)
- *piTrailing = 0;
- else if (item < 0 || ((iLastPosX - iPosX) / 2.0) + iX <= iLastPosX)
+ /* If not snaping in the reverse direction (such as Hebrew) Then 0
+ point flow to the next character */
+ if (direction < 0)
{
- if (scriptInformation[psa->eScript].props.fNeedsCaretInfo && clust_size > 1)
- item-=(clust_size-1);
- *piTrailing = 1;
+ if (!scriptInformation[psa->eScript].props.fNeedsCaretInfo && abs(iPosX) == piAdvance[glyph_index])
+ i++;
+ else
+ *piTrailing = 1;
}
- else
- *piTrailing = 0;
}
- *piCP = item;
+ *piCP = i;
TRACE("*piCP=%d\n", *piCP);
TRACE("*piTrailing=%d\n", *piTrailing);
for (i = 0; i < cGlyphs; i++)
{
ABC abc;
- if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
+ if (pGlyphProps[i].sva.fZeroWidth)
+ {
+ abc.abcA = abc.abcB = abc.abcC = 0;
+ }
+ else if (!get_cache_glyph_widths(psc, pwGlyphs[i], &abc))
{
if (!hdc) return E_PENDING;
if ((get_cache_pitch_family(psc) & TMPF_TRUETYPE) && !psa->fNoGlyphIndex)
if (!indexs)
return E_OUTOFMEMORY;
-
if (vistolog)
{
for( ich = 0; ich < runs; ich++)
ich = 0;
while (ich < runs)
ich += BIDI_ReorderV2lLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
- for (ich = 0; ich < runs; ich++)
- vistolog[ich] = indexs[ich];
+ memcpy(vistolog, indexs, runs * sizeof(*vistolog));
}
-
if (logtovis)
{
for( ich = 0; ich < runs; ich++)
ich = 0;
while (ich < runs)
ich += BIDI_ReorderL2vLevel(0, indexs+ich, level+ich, runs - ich, FALSE);
- for (ich = 0; ich < runs; ich++)
- logtovis[ich] = indexs[ich];
+ memcpy(logtovis, indexs, runs * sizeof(*logtovis));
}
heap_free(indexs);