ups10: Reimplement ScriptLayout to properly handle mixed runs.
[wine/multimedia.git] / dlls / gdi32 / bidi.c
blob60fff3e58fd83619f84f7fe9cc29f635e7aadffe
1 /*
2 * GDI BiDirectional handling
4 * Copyright 2003 Shachar Shemesh
5 * Copyright 2007 Maarten Lankhorst
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
22 * Code derived from the modified reference implementation
23 * that was found in revision 17 of http://unicode.org/reports/tr9/
24 * "Unicode Standard Annex #9: THE BIDIRECTIONAL ALGORITHM"
26 * -- Copyright (C) 1999-2005, ASMUS, Inc.
28 * Permission is hereby granted, free of charge, to any person obtaining a
29 * copy of the Unicode data files and any associated documentation (the
30 * "Data Files") or Unicode software and any associated documentation (the
31 * "Software") to deal in the Data Files or Software without restriction,
32 * including without limitation the rights to use, copy, modify, merge,
33 * publish, distribute, and/or sell copies of the Data Files or Software,
34 * and to permit persons to whom the Data Files or Software are furnished
35 * to do so, provided that (a) the above copyright notice(s) and this
36 * permission notice appear with all copies of the Data Files or Software,
37 * (b) both the above copyright notice(s) and this permission notice appear
38 * in associated documentation, and (c) there is clear notice in each
39 * modified Data File or in the Software as well as in the documentation
40 * associated with the Data File(s) or Software that the data or software
41 * has been modified.
44 #include "config.h"
46 #include <stdarg.h>
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "winnls.h"
51 #include "usp10.h"
52 #include "wine/debug.h"
53 #include "gdi_private.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(bidi);
57 /* HELPER FUNCTIONS AND DECLARATIONS */
59 #define odd(x) ((x) & 1)
61 /*------------------------------------------------------------------------
62 Bidirectional Character Types
64 as defined by the Unicode Bidirectional Algorithm Table 3-7.
66 Note:
68 The list of bidirectional character types here is not grouped the
69 same way as the table 3-7, since the numberic values for the types
70 are chosen to keep the state and action tables compact.
71 ------------------------------------------------------------------------*/
72 enum directions
74 /* input types */
75 /* ON MUST be zero, code relies on ON = N = 0 */
76 ON = 0, /* Other Neutral */
77 L, /* Left Letter */
78 R, /* Right Letter */
79 AN, /* Arabic Number */
80 EN, /* European Number */
81 AL, /* Arabic Letter (Right-to-left) */
82 NSM, /* Non-spacing Mark */
83 CS, /* Common Separator */
84 ES, /* European Separator */
85 ET, /* European Terminator (post/prefix e.g. $ and %) */
87 /* resolved types */
88 BN, /* Boundary neutral (type of RLE etc after explicit levels) */
90 /* input types, */
91 S, /* Segment Separator (TAB) // used only in L1 */
92 WS, /* White space // used only in L1 */
93 B, /* Paragraph Separator (aka as PS) */
95 /* types for explicit controls */
96 RLO, /* these are used only in X1-X9 */
97 RLE,
98 LRO,
99 LRE,
100 PDF,
102 /* resolved types, also resolved directions */
103 N = ON, /* alias, where ON, WS and S are treated the same */
106 /* HELPER FUNCTIONS */
108 /* grep -r ';BN;' data.txt | grep -v [0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F] | sed -e s@\;.*@@ -e s/^..../0x\&,\ / | xargs echo */
109 static const WCHAR BNs[] = {
110 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008,
111 0x000E, 0x000F, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,
112 0x0017, 0x0018, 0x0019, 0x001A, 0x001B, 0x007F, 0x0080, 0x0081, 0x0082,
113 0x0083, 0x0084, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C,
114 0x008D, 0x008E, 0x008F, 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095,
115 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E,
116 0x009F, 0x00AD, 0x070F, 0x200B, 0x200C, 0x200D, 0x2060, 0x2061, 0x2062,
117 0x2063, 0x206A, 0x206B, 0x206C, 0x206D, 0x206E, 0x206F, 0xFEFF
120 /* Idem, but with ';R;' instead of ';BN;' */
121 static const WCHAR Rs[] = {
122 0x05BE, 0x05C0, 0x05C3, 0x05C6, 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4,
123 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD,
124 0x05DE, 0x05DF, 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6,
125 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0x05F0, 0x05F1, 0x05F2, 0x05F3, 0x05F4,
126 0x07C0, 0x07C1, 0x07C2, 0x07C3, 0x07C4, 0x07C5, 0x07C6, 0x07C7, 0x07C8,
127 0x07C9, 0x07CA, 0x07CB, 0x07CC, 0x07CD, 0x07CE, 0x07CF, 0x07D0, 0x07D1,
128 0x07D2, 0x07D3, 0x07D4, 0x07D5, 0x07D6, 0x07D7, 0x07D8, 0x07D9, 0x07DA,
129 0x07DB, 0x07DC, 0x07DD, 0x07DE, 0x07DF, 0x07E0, 0x07E1, 0x07E2, 0x07E3,
130 0x07E4, 0x07E5, 0x07E6, 0x07E7, 0x07E8, 0x07E9, 0x07EA, 0x07F4, 0x07F5,
131 0x07FA, 0x200F, 0xFB1D, 0xFB1F, 0xFB20, 0xFB21, 0xFB22, 0xFB23, 0xFB24,
132 0xFB25, 0xFB26, 0xFB27, 0xFB28, 0xFB2A, 0xFB2B, 0xFB2C, 0xFB2D, 0xFB2E,
133 0xFB2F, 0xFB30, 0xFB31, 0xFB32, 0xFB33, 0xFB34, 0xFB35, 0xFB36, 0xFB38,
134 0xFB39, 0xFB3A, 0xFB3B, 0xFB3C, 0xFB3E, 0xFB40, 0xFB41, 0xFB43, 0xFB44,
135 0xFB46, 0xFB47, 0xFB48, 0xFB49, 0xFB4A, 0xFB4B, 0xFB4C, 0xFB4D, 0xFB4E,
136 0xFB4F
139 /* Convert the incomplete win32 table to some slightly more useful data */
140 static void classify(LPCWSTR lpString, WORD *chartype, DWORD uCount)
142 unsigned i, j;
143 GetStringTypeW(CT_CTYPE2, lpString, uCount, chartype);
144 for (i = 0; i < uCount; ++i)
145 switch (chartype[i])
147 case C2_LEFTTORIGHT: chartype[i] = L; break;
148 case C2_RIGHTTOLEFT:
149 chartype[i] = AL;
150 for (j = 0; j < sizeof(Rs)/sizeof(WCHAR); ++j)
151 if (Rs[j] == lpString[i])
153 chartype[i] = R;
154 break;
156 break;
158 case C2_EUROPENUMBER: chartype[i] = EN; break;
159 case C2_EUROPESEPARATOR: chartype[i] = ES; break;
160 case C2_EUROPETERMINATOR: chartype[i] = ET; break;
161 case C2_ARABICNUMBER: chartype[i] = AN; break;
162 case C2_COMMONSEPARATOR: chartype[i] = CS; break;
163 case C2_BLOCKSEPARATOR: chartype[i] = B; break;
164 case C2_SEGMENTSEPARATOR: chartype[i] = S; break;
165 case C2_WHITESPACE: chartype[i] = WS; break;
166 case C2_OTHERNEUTRAL:
167 switch (lpString[i])
169 case 0x202A: chartype[i] = LRE; break;
170 case 0x202B: chartype[i] = RLE; break;
171 case 0x202C: chartype[i] = PDF; break;
172 case 0x202D: chartype[i] = LRO; break;
173 case 0x202E: chartype[i] = RLO; break;
174 default: chartype[i] = ON; break;
176 break;
177 case C2_NOTAPPLICABLE:
178 chartype[i] = NSM;
179 for (j = 0; j < sizeof(BNs)/sizeof(WCHAR); ++j)
180 if (BNs[j] == lpString[i])
182 chartype[i] = BN;
183 break;
185 break;
187 default:
188 /* According to BiDi spec, unassigned characters default to L */
189 FIXME("Unhandled character type: %04x\n", chartype[i]);
190 chartype[i] = L;
191 break;
195 /* Set a run of cval values at locations all prior to, but not including */
196 /* iStart, to the new value nval. */
197 static void SetDeferredRun(BYTE *pval, int cval, int iStart, int nval)
199 int i = iStart - 1;
200 for (; i >= iStart - cval; i--)
202 pval[i] = nval;
206 /* THE PARAGRAPH LEVEL */
208 /*------------------------------------------------------------------------
209 Function: resolveParagraphs
211 Resolves the input strings into blocks over which the algorithm
212 is then applied.
214 Implements Rule P1 of the Unicode Bidi Algorithm
216 Input: Text string
217 Character count
219 Output: revised character count
221 Note: This is a very simplistic function. In effect it restricts
222 the action of the algorithm to the first paragraph in the input
223 where a paragraph ends at the end of the first block separator
224 or at the end of the input text.
226 ------------------------------------------------------------------------*/
228 static int resolveParagraphs(WORD *types, int cch)
230 /* skip characters not of type B */
231 int ich = 0;
232 for(; ich < cch && types[ich] != B; ich++);
233 /* stop after first B, make it a BN for use in the next steps */
234 if (ich < cch && types[ich] == B)
235 types[ich++] = BN;
236 return ich;
239 /* REORDER */
240 /*------------------------------------------------------------------------
241 Function: resolveLines
243 Breaks a paragraph into lines
245 Input: Character count
246 In/Out: Array of characters
247 Array of line break flags
249 Returns the count of characters on the first line
251 Note: This function only breaks lines at hard line breaks. Other
252 line breaks can be passed in. If pbrk[n] is TRUE, then a break
253 occurs after the character in pszInput[n]. Breaks before the first
254 character are not allowed.
255 ------------------------------------------------------------------------*/
256 static int resolveLines(LPCWSTR pszInput, BOOL * pbrk, int cch)
258 /* skip characters not of type LS */
259 int ich = 0;
260 for(; ich < cch; ich++)
262 if (pszInput[ich] == (WCHAR)'\n' || (pbrk && pbrk[ich]))
264 ich++;
265 break;
269 return ich;
272 /*------------------------------------------------------------------------
273 Function: resolveWhiteSpace
275 Resolves levels for WS and S
276 Implements rule L1 of the Unicode bidi Algorithm.
278 Input: Base embedding level
279 Character count
280 Array of direction classes (for one line of text)
282 In/Out: Array of embedding levels (for one line of text)
284 Note: this should be applied a line at a time. The default driver
285 code supplied in this file assumes a single line of text; for
286 a real implementation, cch and the initial pointer values
287 would have to be adjusted.
288 ------------------------------------------------------------------------*/
289 static void resolveWhitespace(int baselevel, const WORD *pcls, BYTE *plevel, int cch)
291 int cchrun = 0;
292 BYTE oldlevel = baselevel;
294 int ich = 0;
295 for (; ich < cch; ich++)
297 switch(pcls[ich])
299 default:
300 cchrun = 0; /* any other character breaks the run */
301 break;
302 case WS:
303 cchrun++;
304 break;
306 case RLE:
307 case LRE:
308 case LRO:
309 case RLO:
310 case PDF:
311 case BN:
312 plevel[ich] = oldlevel;
313 cchrun++;
314 break;
316 case S:
317 case B:
318 /* reset levels for WS before eot */
319 SetDeferredRun(plevel, cchrun, ich, baselevel);
320 cchrun = 0;
321 plevel[ich] = baselevel;
322 break;
324 oldlevel = plevel[ich];
326 /* reset level before eot */
327 SetDeferredRun(plevel, cchrun, ich, baselevel);
330 /* DISPLAY OPTIONS */
331 /*-----------------------------------------------------------------------
332 Function: mirror
334 Crudely implements rule L4 of the Unicode Bidirectional Algorithm
335 Demonstrate mirrored brackets, braces and parens
338 Input: Array of levels
339 Count of characters
341 In/Out: Array of characters (should be array of glyph ids)
343 Note;
344 A full implementation would need to substitute mirrored glyphs even
345 for characters that are not paired (e.g. integral sign).
346 -----------------------------------------------------------------------*/
347 static void mirror(LPWSTR pszInput, const BYTE* plevel, int cch)
349 static int warn_once;
350 int i;
352 for (i = 0; i < cch; ++i)
354 if (!odd(plevel[i]))
355 continue;
356 /* This needs the data from http://www.unicode.org/Public/UNIDATA/BidiMirroring.txt */
357 if (!warn_once++)
358 FIXME("stub: mirroring of characters not yet implemented\n");
359 break;
363 /*------------------------------------------------------------------------
364 Function: BidiLines
366 Implements the Line-by-Line phases of the Unicode Bidi Algorithm
368 Input: Count of characters
369 flag whether to mirror
371 Inp/Out: Input text
372 Array of character directions
373 Array of levels
375 ------------------------------------------------------------------------*/
376 static void BidiLines(int baselevel, LPWSTR pszOutLine, LPCWSTR pszLine, WORD * pclsLine,
377 BYTE * plevelLine, int cchPara, int fMirror, BOOL * pbrk)
379 int cchLine = 0;
380 int done = 0;
381 int *run;
383 run = HeapAlloc(GetProcessHeap(), 0, cchPara * sizeof(int));
384 if (!run)
386 WARN("Out of memory\n");
387 return;
392 /* break lines at LS */
393 cchLine = resolveLines(pszLine, pbrk, cchPara);
395 /* resolve whitespace */
396 resolveWhitespace(baselevel, pclsLine, plevelLine, cchLine);
398 if (pszOutLine)
400 int i;
401 if (fMirror)
402 mirror(pszOutLine, plevelLine, cchLine);
404 /* reorder each line in place */
405 ScriptLayout(cchLine, plevelLine, run, NULL);
406 for (i = 0; i < cchLine; i++)
407 pszOutLine[done+run[i]] = pszLine[i];
410 pszLine += cchLine;
411 plevelLine += cchLine;
412 pbrk += pbrk ? cchLine : 0;
413 pclsLine += cchLine;
414 cchPara -= cchLine;
415 done += cchLine;
417 } while (cchPara);
419 HeapFree(GetProcessHeap(), 0, run);
422 /*************************************************************
423 * BIDI_Reorder
425 BOOL BIDI_Reorder(
426 LPCWSTR lpString, /* [in] The string for which information is to be returned */
427 INT uCount, /* [in] Number of WCHARs in string. */
428 DWORD dwFlags, /* [in] GetCharacterPlacement compatible flags specifying how to process the string */
429 DWORD dwWineGCP_Flags, /* [in] Wine internal flags - Force paragraph direction */
430 LPWSTR lpOutString, /* [out] Reordered string */
431 INT uCountOut, /* [in] Size of output buffer */
432 UINT *lpOrder /* [out] Logical -> Visual order map */
435 WORD *chartype;
436 BYTE *levels;
437 unsigned i, done;
439 int maxItems;
440 int nItems;
441 SCRIPT_CONTROL Control;
442 SCRIPT_STATE State;
443 SCRIPT_ITEM *pItems;
444 HRESULT res;
446 TRACE("%s, %d, 0x%08x lpOutString=%p, lpOrder=%p\n",
447 debugstr_wn(lpString, uCount), uCount, dwFlags,
448 lpOutString, lpOrder);
450 memset(&Control, 0, sizeof(Control));
451 memset(&State, 0, sizeof(State));
453 if (!(dwFlags & GCP_REORDER))
455 FIXME("Asked to reorder without reorder flag set\n");
456 return FALSE;
459 if (uCountOut < uCount)
461 FIXME("lpOutString too small\n");
462 return FALSE;
465 chartype = HeapAlloc(GetProcessHeap(), 0, uCount * sizeof(WORD));
466 if (!chartype)
468 WARN("Out of memory\n");
469 return FALSE;
472 if (lpOutString)
473 memcpy(lpOutString, lpString, uCount * sizeof(WCHAR));
475 /* Verify reordering will be required */
476 if ((WINE_GCPW_FORCE_RTL == (dwWineGCP_Flags&WINE_GCPW_DIR_MASK)) ||
477 ((dwWineGCP_Flags&WINE_GCPW_DIR_MASK) == WINE_GCPW_LOOSE_RTL))
478 State.uBidiLevel = 1;
479 else
481 done = 1;
482 classify(lpString, chartype, uCount);
483 for (i = 0; i < uCount; i++)
484 switch (chartype[i])
486 case R:
487 case AL:
488 case RLE:
489 case RLO:
490 done = 0;
491 break;
493 if (done)
495 HeapFree(GetProcessHeap(), 0, chartype);
496 return TRUE;
500 levels = HeapAlloc(GetProcessHeap(), 0, uCount * sizeof(BYTE));
501 if (!levels)
503 WARN("Out of memory\n");
504 HeapFree(GetProcessHeap(), 0, chartype);
505 return FALSE;
508 maxItems = 5;
509 pItems = HeapAlloc(GetProcessHeap(),0, maxItems * sizeof(SCRIPT_ITEM));
510 if (!pItems)
512 WARN("Out of memory\n");
513 HeapFree(GetProcessHeap(), 0, chartype);
514 HeapFree(GetProcessHeap(), 0, levels);
515 return FALSE;
518 done = 0;
519 while (done < uCount)
521 unsigned j;
522 classify(lpString + done, chartype, uCount - done);
523 /* limit text to first block */
524 i = resolveParagraphs(chartype, uCount - done);
525 for (j = 0; j < i; ++j)
526 switch(chartype[j])
528 case B:
529 case S:
530 case WS:
531 case ON: chartype[j] = N;
532 default: continue;
535 if ((dwWineGCP_Flags&WINE_GCPW_DIR_MASK) == WINE_GCPW_LOOSE_RTL)
536 State.uBidiLevel = 1;
537 else if ((dwWineGCP_Flags&WINE_GCPW_DIR_MASK) == WINE_GCPW_LOOSE_LTR)
538 State.uBidiLevel = 0;
540 if (dwWineGCP_Flags & WINE_GCPW_LOOSE_MASK)
542 for (j = 0; j < i; ++j)
543 if (chartype[j] == L)
545 State.uBidiLevel = 0;
546 break;
548 else if (chartype[j] == R || chartype[j] == AL)
550 State.uBidiLevel = 1;
551 break;
555 res = ScriptItemize(lpString + done, i, maxItems, &Control, &State, pItems, &nItems);
556 while (res == E_OUTOFMEMORY)
558 maxItems = maxItems * 2;
559 pItems = HeapReAlloc(GetProcessHeap(), 0, pItems, sizeof(SCRIPT_ITEM) * maxItems);
560 if (!pItems)
562 WARN("Out of memory\n");
563 HeapFree(GetProcessHeap(), 0, chartype);
564 HeapFree(GetProcessHeap(), 0, levels);
565 return FALSE;
567 res = ScriptItemize(lpString + done, i, maxItems, &Control, &State, pItems, &nItems);
570 for (j = 0; j < nItems; j++)
572 int k;
573 for (k = pItems[j].iCharPos; k < pItems[j+1].iCharPos; k++)
574 levels[k] = pItems[j].a.s.uBidiLevel;
577 /* assign directional types again, but for WS, S this time */
578 classify(lpString + done, chartype, i);
580 BidiLines(State.uBidiLevel, lpOutString ? lpOutString + done : NULL, lpString + done,
581 chartype, levels, i, !(dwFlags & GCP_SYMSWAPOFF), 0);
584 if (lpOrder)
586 int k, lastgood;
587 for (j = lastgood = 0; j < i; ++j)
588 if (levels[j] != levels[lastgood])
590 --j;
591 if (odd(levels[lastgood]))
592 for (k = j; k >= lastgood; --k)
593 lpOrder[done + k] = done + j - k;
594 else
595 for (k = lastgood; k <= j; ++k)
596 lpOrder[done + k] = done + k;
597 lastgood = ++j;
599 if (odd(levels[lastgood]))
600 for (k = j - 1; k >= lastgood; --k)
601 lpOrder[done + k] = done + j - 1 - k;
602 else
603 for (k = lastgood; k < j; ++k)
604 lpOrder[done + k] = done + k;
606 done += i;
609 HeapFree(GetProcessHeap(), 0, chartype);
610 HeapFree(GetProcessHeap(), 0, levels);
611 HeapFree(GetProcessHeap(), 0, pItems);
612 return TRUE;