[RICHED20] Sync with Wine Staging 1.9.4. CORE-10912
[reactos.git] / reactos / dll / win32 / riched20 / wrap.c
1 /*
2 * RichEdit - Paragraph wrapping. Don't try to understand it. You've been
3 * warned !
4 *
5 * Copyright 2004 by Krzysztof Foltman
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22
23 #include "editor.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 WINE_DECLARE_DEBUG_CHANNEL(richedit_check);
27
28 /*
29 * Unsolved problems:
30 *
31 * - center and right align in WordPad omits all spaces at the start, we don't
32 * - objects/images are not handled yet
33 * - no tabs
34 */
35
36
37 static BOOL get_run_glyph_buffers( ME_Run *run )
38 {
39 heap_free( run->glyphs );
40 run->glyphs = heap_alloc( run->max_glyphs * (sizeof(WORD) + sizeof(SCRIPT_VISATTR) + sizeof(int) + sizeof(GOFFSET)) );
41 if (!run->glyphs) return FALSE;
42
43 run->vis_attrs = (SCRIPT_VISATTR*)((char*)run->glyphs + run->max_glyphs * sizeof(WORD));
44 run->advances = (int*)((char*)run->glyphs + run->max_glyphs * (sizeof(WORD) + sizeof(SCRIPT_VISATTR)));
45 run->offsets = (GOFFSET*)((char*)run->glyphs + run->max_glyphs * (sizeof(WORD) + sizeof(SCRIPT_VISATTR) + sizeof(int)));
46
47 return TRUE;
48 }
49
50 static HRESULT shape_run( ME_Context *c, ME_Run *run )
51 {
52 HRESULT hr;
53 HFONT old_font;
54 int i;
55
56 if (!run->glyphs)
57 {
58 run->max_glyphs = 1.5 * run->len + 16; /* This is suggested in the uniscribe documentation */
59 run->max_glyphs = (run->max_glyphs + 7) & ~7; /* Keep alignment simple */
60 get_run_glyph_buffers( run );
61 }
62
63 if (run->max_clusters < run->len)
64 {
65 heap_free( run->clusters );
66 run->max_clusters = run->len * 2;
67 run->clusters = heap_alloc( run->max_clusters * sizeof(WORD) );
68 }
69
70 old_font = ME_SelectStyleFont( c, run->style );
71 while (1)
72 {
73 hr = ScriptShape( c->hDC, &run->style->script_cache, get_text( run, 0 ), run->len, run->max_glyphs,
74 &run->script_analysis, run->glyphs, run->clusters, run->vis_attrs, &run->num_glyphs );
75 if (hr != E_OUTOFMEMORY) break;
76 if (run->max_glyphs > 10 * run->len) break; /* something has clearly gone wrong */
77 run->max_glyphs *= 2;
78 get_run_glyph_buffers( run );
79 }
80
81 if (SUCCEEDED(hr))
82 hr = ScriptPlace( c->hDC, &run->style->script_cache, run->glyphs, run->num_glyphs, run->vis_attrs,
83 &run->script_analysis, run->advances, run->offsets, NULL );
84
85 if (SUCCEEDED(hr))
86 {
87 for (i = 0, run->nWidth = 0; i < run->num_glyphs; i++)
88 run->nWidth += run->advances[i];
89 }
90
91 ME_UnselectStyleFont( c, run->style, old_font );
92
93 return hr;
94 }
95
96 /******************************************************************************
97 * calc_run_extent
98 *
99 * Updates the size of the run (fills width, ascent and descent). The height
100 * is calculated based on whole row's ascent and descent anyway, so no need
101 * to use it here.
102 */
103 static void calc_run_extent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
104 {
105 if (run->nFlags & MERF_HIDDEN) run->nWidth = 0;
106 else
107 {
108 SIZE size = ME_GetRunSizeCommon( c, para, run, run->len, startx, &run->nAscent, &run->nDescent );
109 run->nWidth = size.cx;
110 }
111 }
112
113 /******************************************************************************
114 * split_run_extents
115 *
116 * Splits a run into two in a given place. It also updates the screen position
117 * and size (extent) of the newly generated runs.
118 */
119 static ME_DisplayItem *split_run_extents(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
120 {
121 ME_TextEditor *editor = wc->context->editor;
122 ME_Run *run, *run2;
123 ME_Paragraph *para = &wc->pPara->member.para;
124 ME_Cursor cursor = {wc->pPara, item, nVChar};
125
126 assert(item->member.run.nCharOfs != -1);
127 if(TRACE_ON(richedit_check))
128 ME_CheckCharOffsets(editor);
129
130 run = &item->member.run;
131
132 TRACE("Before split: %s(%d, %d)\n", debugstr_run( run ),
133 run->pt.x, run->pt.y);
134
135 ME_SplitRunSimple(editor, &cursor);
136
137 run2 = &cursor.pRun->member.run;
138 run2->script_analysis = run->script_analysis;
139
140 shape_run( wc->context, run );
141 shape_run( wc->context, run2 );
142 calc_run_extent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
143
144 run2->pt.x = run->pt.x+run->nWidth;
145 run2->pt.y = run->pt.y;
146
147 if(TRACE_ON(richedit_check))
148 ME_CheckCharOffsets(editor);
149
150 TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
151 debugstr_run( run ), run->pt.x, run->pt.y,
152 debugstr_run( run2 ), run2->pt.x, run2->pt.y);
153
154 return cursor.pRun;
155 }
156
157 /******************************************************************************
158 * find_split_point
159 *
160 * Returns a character position to split inside the run given a run-relative
161 * pixel horizontal position. This version rounds left (ie. if the second
162 * character is at pixel position 8, then for cx=0..7 it returns 0).
163 */
164 static int find_split_point( ME_Context *c, int cx, ME_Run *run )
165 {
166 if (!run->len || cx <= 0) return 0;
167 return ME_CharFromPointContext( c, cx, run, FALSE, FALSE );
168 }
169
170 static ME_DisplayItem *ME_MakeRow(int height, int baseline, int width)
171 {
172 ME_DisplayItem *item = ME_MakeDI(diStartRow);
173
174 item->member.row.nHeight = height;
175 item->member.row.nBaseline = baseline;
176 item->member.row.nWidth = width;
177 return item;
178 }
179
180 static void ME_BeginRow(ME_WrapContext *wc)
181 {
182 PARAFORMAT2 *pFmt;
183 ME_DisplayItem *para = wc->pPara;
184
185 pFmt = para->member.para.pFmt;
186 wc->pRowStart = NULL;
187 wc->bOverflown = FALSE;
188 wc->pLastSplittableRun = NULL;
189 wc->bWordWrap = wc->context->editor->bWordWrap;
190 if (para->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
191 wc->nAvailWidth = 0;
192 wc->bWordWrap = FALSE;
193 if (para->member.para.nFlags & MEPF_ROWEND)
194 {
195 ME_Cell *cell = &ME_FindItemBack(para, diCell)->member.cell;
196 cell->nWidth = 0;
197 }
198 } else if (para->member.para.pCell) {
199 ME_Cell *cell = &para->member.para.pCell->member.cell;
200 int width;
201
202 width = cell->nRightBoundary;
203 if (cell->prev_cell)
204 width -= cell->prev_cell->member.cell.nRightBoundary;
205 if (!cell->prev_cell)
206 {
207 int rowIndent = ME_GetTableRowEnd(para)->member.para.pFmt->dxStartIndent;
208 width -= rowIndent;
209 }
210 cell->nWidth = max(ME_twips2pointsX(wc->context, width), 0);
211
212 wc->nAvailWidth = cell->nWidth
213 - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
214 wc->bWordWrap = TRUE;
215 } else {
216 wc->nAvailWidth = wc->context->nAvailWidth
217 - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
218 }
219 wc->pt.x = wc->context->pt.x;
220 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
221 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
222 /* Shift the text down because of the border. */
223 wc->pt.y++;
224 }
225
226 static void layout_row( ME_DisplayItem *start, const ME_DisplayItem *end )
227 {
228 ME_DisplayItem *p;
229 int i, num_runs = 0;
230 int buf[16 * 5]; /* 5 arrays - 4 of int & 1 of BYTE, alloc space for 5 of ints */
231 int *vis_to_log = buf, *log_to_vis, *widths, *pos;
232 BYTE *levels;
233 BOOL found_black = FALSE;
234
235 for (p = end->prev; p != start->prev; p = p->prev)
236 {
237 if (p->type == diRun)
238 {
239 if (!found_black) found_black = !(p->member.run.nFlags & (MERF_WHITESPACE | MERF_ENDPARA));
240 if (found_black) num_runs++;
241 }
242 }
243
244 TRACE("%d runs\n", num_runs);
245 if (!num_runs) return;
246
247 if (num_runs > sizeof(buf) / (sizeof(buf[0]) * 5))
248 vis_to_log = heap_alloc( num_runs * sizeof(int) * 5 );
249
250 log_to_vis = vis_to_log + num_runs;
251 widths = vis_to_log + 2 * num_runs;
252 pos = vis_to_log + 3 * num_runs;
253 levels = (BYTE*)(vis_to_log + 4 * num_runs);
254
255 for (i = 0, p = start; i < num_runs; p = p->next)
256 {
257 if (p->type == diRun)
258 {
259 levels[i] = p->member.run.script_analysis.s.uBidiLevel;
260 widths[i] = p->member.run.nWidth;
261 TRACE( "%d: level %d width %d\n", i, levels[i], widths[i] );
262 i++;
263 }
264 }
265
266 ScriptLayout( num_runs, levels, vis_to_log, log_to_vis );
267
268 pos[0] = start->member.run.para->pt.x;
269 for (i = 1; i < num_runs; i++)
270 pos[i] = pos[i - 1] + widths[ vis_to_log[ i - 1 ] ];
271
272 for (i = 0, p = start; i < num_runs; p = p->next)
273 {
274 if (p->type == diRun)
275 {
276 p->member.run.pt.x = pos[ log_to_vis[ i ] ];
277 TRACE( "%d: x = %d\n", i, p->member.run.pt.x );
278 i++;
279 }
280 }
281
282 if (vis_to_log != buf) heap_free( vis_to_log );
283 }
284
285 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
286 {
287 ME_DisplayItem *p, *row;
288 ME_Paragraph *para = &wc->pPara->member.para;
289 BOOL bSkippingSpaces = TRUE;
290 int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
291
292 /* wrap text */
293
294 for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
295 {
296 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
297 if (p->type==diRun && ((p==wc->pRowStart) || !(p->member.run.nFlags & MERF_ENDPARA))) { /* FIXME add more run types */
298 if (p->member.run.nAscent>ascent)
299 ascent = p->member.run.nAscent;
300 if (p->member.run.nDescent>descent)
301 descent = p->member.run.nDescent;
302 if (bSkippingSpaces)
303 {
304 /* Exclude space characters from run width.
305 * Other whitespace or delimiters are not treated this way. */
306 int len = p->member.run.len;
307 WCHAR *text = get_text( &p->member.run, len - 1 );
308
309 assert (len);
310 if (~p->member.run.nFlags & MERF_GRAPHICS)
311 while (len && *(text--) == ' ')
312 len--;
313 if (len)
314 {
315 if (len == p->member.run.len)
316 width += p->member.run.nWidth;
317 else
318 width += ME_PointFromCharContext( wc->context, &p->member.run, len, FALSE );
319 }
320 bSkippingSpaces = !len;
321 } else if (!(p->member.run.nFlags & MERF_ENDPARA))
322 width += p->member.run.nWidth;
323 }
324 }
325
326 para->nWidth = max(para->nWidth, width);
327 row = ME_MakeRow(ascent+descent, ascent, width);
328 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
329 (para->pFmt->dwMask & PFM_TABLE) && (para->pFmt->wEffects & PFE_TABLE))
330 {
331 /* The text was shifted down in ME_BeginRow so move the wrap context
332 * back to where it should be. */
333 wc->pt.y--;
334 /* The height of the row is increased by the borders. */
335 row->member.row.nHeight += 2;
336 }
337 row->member.row.pt = wc->pt;
338 row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
339 row->member.row.nRMargin = wc->nRightMargin;
340 assert(para->pFmt->dwMask & PFM_ALIGNMENT);
341 align = para->pFmt->wAlignment;
342 if (align == PFA_CENTER)
343 shift = max((wc->nAvailWidth-width)/2, 0);
344 if (align == PFA_RIGHT)
345 shift = max(wc->nAvailWidth-width, 0);
346
347 if (para->nFlags & MEPF_COMPLEX) layout_row( wc->pRowStart, pEnd );
348
349 row->member.row.pt.x = row->member.row.nLMargin + shift;
350 for (p = wc->pRowStart; p!=pEnd; p = p->next)
351 {
352 if (p->type==diRun) { /* FIXME add more run types */
353 p->member.run.pt.x += row->member.row.nLMargin+shift;
354 }
355 }
356 ME_InsertBefore(wc->pRowStart, row);
357 wc->nRow++;
358 wc->pt.y += row->member.row.nHeight;
359 ME_BeginRow(wc);
360 }
361
362 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
363 {
364 ME_DisplayItem *para = wc->pPara;
365 PARAFORMAT2 *pFmt = para->member.para.pFmt;
366 if (wc->pRowStart)
367 ME_InsertRowStart(wc, p);
368 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
369 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
370 {
371 /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
372 * text down by one pixel for the border, so fix up the wrap context. */
373 wc->pt.y--;
374 }
375
376 /*
377 p = para->next;
378 while(p) {
379 if (p->type == diParagraph || p->type == diTextEnd)
380 return;
381 if (p->type == diRun)
382 {
383 ME_Run *run = &p->member.run;
384 TRACE("%s - (%d, %d)\n", debugstr_run(run), run->pt.x, run->pt.y);
385 }
386 p = p->next;
387 }
388 */
389 }
390
391 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
392 {
393 /* FIXME compose style (out of character and paragraph styles) here */
394
395 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
396
397 calc_run_extent(wc->context, &wc->pPara->member.para,
398 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
399 }
400
401
402 static int find_non_whitespace(const WCHAR *s, int len, int start)
403 {
404 int i;
405 for (i = start; i < len && ME_IsWSpace( s[i] ); i++)
406 ;
407
408 return i;
409 }
410
411 /* note: these two really return the first matching offset (starting from EOS)+1
412 * in other words, an offset of the first trailing white/black */
413
414 /* note: returns offset of the first trailing whitespace */
415 static int reverse_find_non_whitespace(const WCHAR *s, int start)
416 {
417 int i;
418 for (i = start; i > 0 && ME_IsWSpace( s[i - 1] ); i--)
419 ;
420
421 return i;
422 }
423
424 /* note: returns offset of the first trailing nonwhitespace */
425 static int reverse_find_whitespace(const WCHAR *s, int start)
426 {
427 int i;
428 for (i = start; i > 0 && !ME_IsWSpace( s[i - 1] ); i--)
429 ;
430
431 return i;
432 }
433
434 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
435 {
436 ME_DisplayItem *pp, *piter = p;
437 int j;
438 if (!i)
439 return NULL;
440 j = reverse_find_non_whitespace( get_text( &p->member.run, 0 ), i);
441 if (j>0) {
442 pp = split_run_extents(wc, piter, j);
443 wc->pt.x += piter->member.run.nWidth;
444 return pp;
445 }
446 else
447 {
448 pp = piter;
449 /* omit all spaces before split point */
450 while(piter != wc->pRowStart)
451 {
452 piter = ME_FindItemBack(piter, diRun);
453 if (piter->member.run.nFlags & MERF_WHITESPACE)
454 {
455 pp = piter;
456 continue;
457 }
458 if (piter->member.run.nFlags & MERF_ENDWHITE)
459 {
460 i = reverse_find_non_whitespace( get_text( &piter->member.run, 0 ),
461 piter->member.run.len );
462 pp = split_run_extents(wc, piter, i);
463 wc->pt = pp->member.run.pt;
464 return pp;
465 }
466 /* this run is the end of spaces, so the run edge is a good point to split */
467 wc->pt = pp->member.run.pt;
468 wc->bOverflown = TRUE;
469 TRACE("Split point is: %s|%s\n", debugstr_run( &piter->member.run ), debugstr_run( &pp->member.run ));
470 return pp;
471 }
472 wc->pt = piter->member.run.pt;
473 return piter;
474 }
475 }
476
477 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
478 {
479 ME_DisplayItem *piter = p, *pp;
480 int i, idesp, len;
481 ME_Run *run = &p->member.run;
482
483 idesp = i = find_split_point( wc->context, loc, run );
484 len = run->len;
485 assert(len>0);
486 assert(i<len);
487 if (i) {
488 /* don't split words */
489 i = reverse_find_whitespace( get_text( run, 0 ), i );
490 pp = ME_MaximizeSplit(wc, p, i);
491 if (pp)
492 return pp;
493 }
494 TRACE("Must backtrack to split at: %s\n", debugstr_run( &p->member.run ));
495 if (wc->pLastSplittableRun)
496 {
497 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
498 {
499 wc->pt = wc->pLastSplittableRun->member.run.pt;
500 return wc->pLastSplittableRun;
501 }
502 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
503 {
504 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
505 they serve no other purpose */
506 ME_UpdateRunFlags(wc->context->editor, run);
507 assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
508
509 piter = wc->pLastSplittableRun;
510 run = &piter->member.run;
511 len = run->len;
512 /* don't split words */
513 i = reverse_find_whitespace( get_text( run, 0 ), len );
514 if (i == len)
515 i = reverse_find_non_whitespace( get_text( run, 0 ), len );
516 if (i) {
517 ME_DisplayItem *piter2 = split_run_extents(wc, piter, i);
518 wc->pt = piter2->member.run.pt;
519 return piter2;
520 }
521 /* splittable = must have whitespaces */
522 assert(0 == "Splittable, but no whitespaces");
523 }
524 else
525 {
526 /* restart from the first run beginning with spaces */
527 wc->pt = wc->pLastSplittableRun->member.run.pt;
528 return wc->pLastSplittableRun;
529 }
530 }
531 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_run( &p->member.run ));
532 /* OK, no better idea, so assume we MAY split words if we can split at all*/
533 if (idesp)
534 return split_run_extents(wc, piter, idesp);
535 else
536 if (wc->pRowStart && piter != wc->pRowStart)
537 {
538 /* don't need to break current run, because it's possible to split
539 before this run */
540 wc->bOverflown = TRUE;
541 return piter;
542 }
543 else
544 {
545 /* split point inside first character - no choice but split after that char */
546 if (len != 1) {
547 /* the run is more than 1 char, so we may split */
548 return split_run_extents(wc, piter, 1);
549 }
550 /* the run is one char, can't split it */
551 return piter;
552 }
553 }
554
555 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
556 {
557 ME_DisplayItem *pp;
558 ME_Run *run;
559 int len;
560
561 assert(p->type == diRun);
562 if (!wc->pRowStart)
563 wc->pRowStart = p;
564 run = &p->member.run;
565 run->pt.x = wc->pt.x;
566 run->pt.y = wc->pt.y;
567 ME_WrapSizeRun(wc, p);
568 len = run->len;
569
570 if (wc->bOverflown) /* just skipping final whitespaces */
571 {
572 /* End paragraph run can't overflow to the next line by itself. */
573 if (run->nFlags & MERF_ENDPARA)
574 return p->next;
575
576 if (run->nFlags & MERF_WHITESPACE) {
577 wc->pt.x += run->nWidth;
578 /* skip runs consisting of only whitespaces */
579 return p->next;
580 }
581
582 if (run->nFlags & MERF_STARTWHITE) {
583 /* try to split the run at the first non-white char */
584 int black;
585 black = find_non_whitespace( get_text( run, 0 ), run->len, 0 );
586 if (black) {
587 wc->bOverflown = FALSE;
588 pp = split_run_extents(wc, p, black);
589 calc_run_extent(wc->context, &wc->pPara->member.para,
590 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin,
591 &pp->member.run);
592 ME_InsertRowStart(wc, pp);
593 return pp;
594 }
595 }
596 /* black run: the row goes from pRowStart to the previous run */
597 ME_InsertRowStart(wc, p);
598 return p;
599 }
600 /* simply end the current row and move on to next one */
601 if (run->nFlags & MERF_ENDROW)
602 {
603 p = p->next;
604 ME_InsertRowStart(wc, p);
605 return p;
606 }
607
608 /* will current run fit? */
609 if (wc->bWordWrap &&
610 wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
611 {
612 int loc = wc->context->pt.x + wc->nAvailWidth - wc->pt.x;
613 /* total white run ? */
614 if (run->nFlags & MERF_WHITESPACE) {
615 /* let the overflow logic handle it */
616 wc->bOverflown = TRUE;
617 return p;
618 }
619 /* TAB: we can split before */
620 if (run->nFlags & MERF_TAB) {
621 wc->bOverflown = TRUE;
622 if (wc->pRowStart == p)
623 /* Don't split before the start of the run, or we will get an
624 * endless loop. */
625 return p->next;
626 else
627 return p;
628 }
629 /* graphics: we can split before, if run's width is smaller than row's width */
630 if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
631 wc->bOverflown = TRUE;
632 return p;
633 }
634 /* can we separate out the last spaces ? (to use overflow logic later) */
635 if (run->nFlags & MERF_ENDWHITE)
636 {
637 /* we aren't sure if it's *really* necessary, it's a good start however */
638 int black = reverse_find_non_whitespace( get_text( run, 0 ), len );
639 split_run_extents(wc, p, black);
640 /* handle both parts again */
641 return p;
642 }
643 /* determine the split point by backtracking */
644 pp = ME_SplitByBacktracking(wc, p, loc);
645 if (pp == wc->pRowStart)
646 {
647 if (run->nFlags & MERF_STARTWHITE)
648 {
649 /* We had only spaces so far, so we must be on the first line of the
650 * paragraph (or the first line after MERF_ENDROW forced the line
651 * break within the paragraph), since no other lines of the paragraph
652 * start with spaces. */
653
654 /* The lines will only contain spaces, and the rest of the run will
655 * overflow onto the next line. */
656 wc->bOverflown = TRUE;
657 return p;
658 }
659 /* Couldn't split the first run, possible because we have a large font
660 * with a single character that caused an overflow.
661 */
662 wc->pt.x += run->nWidth;
663 return p->next;
664 }
665 if (p != pp) /* found a suitable split point */
666 {
667 wc->bOverflown = TRUE;
668 return pp;
669 }
670 /* we detected that it's best to split on start of this run */
671 if (wc->bOverflown)
672 return pp;
673 ERR("failure!\n");
674 /* not found anything - writing over margins is the only option left */
675 }
676 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
677 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
678 {
679 wc->pLastSplittableRun = p;
680 }
681 wc->pt.x += run->nWidth;
682 return p->next;
683 }
684
685 static int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
686 {
687 int sp = 0, ls = 0;
688 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
689
690 /* FIXME: how to compute simply the line space in ls ??? */
691 /* FIXME: does line spacing include the line itself ??? */
692 switch (para->pFmt->bLineSpacingRule)
693 {
694 case 0: sp = ls; break;
695 case 1: sp = (3 * ls) / 2; break;
696 case 2: sp = 2 * ls; break;
697 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
698 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
699 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
700 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
701 }
702 if (c->editor->nZoomNumerator == 0)
703 return sp;
704 else
705 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
706 }
707
708 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
709 ME_DisplayItem *p;
710
711 tp->member.para.nWidth = 0;
712 /* remove row start items as they will be reinserted by the
713 * paragraph wrapper anyway */
714 tp->member.para.nRows = 0;
715 for (p = tp->next; p != tp->member.para.next_para; p = p->next) {
716 if (p->type == diStartRow) {
717 ME_DisplayItem *pRow = p;
718 p = p->prev;
719 ME_Remove(pRow);
720 ME_DestroyDisplayItem(pRow);
721 }
722 }
723 /* join runs that can be joined */
724 for (p = tp->next; p != tp->member.para.next_para; p = p->next) {
725 assert(p->type != diStartRow); /* should have been deleted above */
726 if (p->type == diRun) {
727 while (p->next->type == diRun && /* FIXME */
728 ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
729 ME_JoinRuns(c->editor, p);
730 }
731 }
732 }
733 }
734
735 static HRESULT itemize_para( ME_Context *c, ME_DisplayItem *p )
736 {
737 ME_Paragraph *para = &p->member.para;
738 ME_Run *run;
739 ME_DisplayItem *di;
740 SCRIPT_ITEM buf[16], *items = buf;
741 int items_passed = sizeof( buf ) / sizeof( buf[0] ), num_items, cur_item;
742 SCRIPT_CONTROL control = { LANG_USER_DEFAULT, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
743 FALSE, FALSE, 0 };
744 SCRIPT_STATE state = { 0, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 0, 0 };
745 HRESULT hr;
746
747 assert( p->type == diParagraph );
748
749 while (1)
750 {
751 hr = ScriptItemize( para->text->szData, para->text->nLen, items_passed, &control,
752 &state, items, &num_items );
753 if (hr != E_OUTOFMEMORY) break; /* may not be enough items if hr == E_OUTOFMEMORY */
754 if (items_passed > para->text->nLen + 1) break; /* something else has gone wrong */
755 items_passed *= 2;
756 if (items == buf)
757 items = heap_alloc( items_passed * sizeof( *items ) );
758 else
759 items = heap_realloc( items, items_passed * sizeof( *items ) );
760 if (!items) break;
761 }
762 if (FAILED( hr )) goto end;
763
764 if (TRACE_ON( richedit ))
765 {
766 TRACE( "got items:\n" );
767 for (cur_item = 0; cur_item < num_items; cur_item++)
768 {
769 TRACE( "\t%d - %d RTL %d bidi level %d\n", items[cur_item].iCharPos, items[cur_item+1].iCharPos - 1,
770 items[cur_item].a.fRTL, items[cur_item].a.s.uBidiLevel );
771 }
772
773 TRACE( "before splitting runs into ranges\n" );
774 for (di = p->next; di != p->member.para.next_para; di = di->next)
775 {
776 if (di->type != diRun) continue;
777 TRACE( "\t%d: %s\n", di->member.run.nCharOfs, debugstr_run( &di->member.run ) );
778 }
779 }
780
781 /* split runs into ranges at item boundaries */
782 for (di = p->next, cur_item = 0; di != p->member.para.next_para; di = di->next)
783 {
784 if (di->type != diRun) continue;
785 run = &di->member.run;
786
787 if (run->nCharOfs == items[cur_item+1].iCharPos) cur_item++;
788
789 items[cur_item].a.fLogicalOrder = TRUE;
790 run->script_analysis = items[cur_item].a;
791
792 if (run->nFlags & MERF_ENDPARA) break; /* don't split eop runs */
793
794 if (run->nCharOfs + run->len > items[cur_item+1].iCharPos)
795 {
796 ME_Cursor cursor = {p, di, items[cur_item+1].iCharPos - run->nCharOfs};
797 ME_SplitRunSimple( c->editor, &cursor );
798 }
799 }
800
801 if (TRACE_ON( richedit ))
802 {
803 TRACE( "after splitting into ranges\n" );
804 for (di = p->next; di != p->member.para.next_para; di = di->next)
805 {
806 if (di->type != diRun) continue;
807 TRACE( "\t%d: %s\n", di->member.run.nCharOfs, debugstr_run( &di->member.run ) );
808 }
809 }
810
811 para->nFlags |= MEPF_COMPLEX;
812
813 end:
814 if (items != buf) heap_free( items );
815 return hr;
816 }
817
818
819 static HRESULT shape_para( ME_Context *c, ME_DisplayItem *p )
820 {
821 ME_DisplayItem *di;
822 ME_Run *run;
823 HRESULT hr;
824
825 for (di = p->next; di != p->member.para.next_para; di = di->next)
826 {
827 if (di->type != diRun) continue;
828 run = &di->member.run;
829
830 hr = shape_run( c, run );
831 if (FAILED( hr ))
832 {
833 run->para->nFlags &= ~MEPF_COMPLEX;
834 return hr;
835 }
836 }
837 return hr;
838 }
839
840 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) {
841 ME_DisplayItem *p;
842 ME_WrapContext wc;
843 int border = 0;
844 int linespace = 0;
845 PARAFORMAT2 *pFmt;
846
847 assert(tp->type == diParagraph);
848 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
849 return;
850 }
851 ME_PrepareParagraphForWrapping(c, tp);
852
853 /* For now treating all non-password text as complex for better testing */
854 if (!c->editor->cPasswordMask /* &&
855 ScriptIsComplex( tp->member.para.text->szData, tp->member.para.text->nLen, SIC_COMPLEX ) == S_OK */)
856 {
857 if (SUCCEEDED( itemize_para( c, tp ) ))
858 shape_para( c, tp );
859 }
860
861 pFmt = tp->member.para.pFmt;
862
863 wc.context = c;
864 wc.pPara = tp;
865 /* wc.para_style = tp->member.para.style; */
866 wc.style = NULL;
867 if (tp->member.para.nFlags & MEPF_ROWEND) {
868 wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
869 } else {
870 int dxStartIndent = pFmt->dxStartIndent;
871 if (tp->member.para.pCell) {
872 dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
873 }
874 wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
875 wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
876 wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
877 }
878 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
879 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
880 {
881 wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
882 }
883 wc.nRow = 0;
884 wc.pt.y = 0;
885 if (pFmt->dwMask & PFM_SPACEBEFORE)
886 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
887 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
888 pFmt->dwMask & PFM_BORDER)
889 {
890 border = ME_GetParaBorderWidth(c, tp->member.para.pFmt->wBorders);
891 if (pFmt->wBorders & 1) {
892 wc.nFirstMargin += border;
893 wc.nLeftMargin += border;
894 }
895 if (pFmt->wBorders & 2)
896 wc.nRightMargin -= border;
897 if (pFmt->wBorders & 4)
898 wc.pt.y += border;
899 }
900
901 linespace = ME_GetParaLineSpace(c, &tp->member.para);
902
903 ME_BeginRow(&wc);
904 for (p = tp->next; p!=tp->member.para.next_para; ) {
905 assert(p->type != diStartRow);
906 if (p->type == diRun) {
907 p = ME_WrapHandleRun(&wc, p);
908 }
909 else p = p->next;
910 if (wc.nRow && p == wc.pRowStart)
911 wc.pt.y += linespace;
912 }
913 ME_WrapEndParagraph(&wc, p);
914 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
915 (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
916 wc.pt.y += border;
917 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
918 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
919
920 tp->member.para.nFlags &= ~MEPF_REWRAP;
921 tp->member.para.nHeight = wc.pt.y;
922 tp->member.para.nRows = wc.nRow;
923 }
924
925 static void ME_MarkRepaintEnd(ME_DisplayItem *para,
926 ME_DisplayItem **repaint_start,
927 ME_DisplayItem **repaint_end)
928 {
929 if (!*repaint_start)
930 *repaint_start = para;
931 *repaint_end = para;
932 }
933
934 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
935 {
936 ME_DisplayItem *item;
937 ME_Context c;
938 int totalWidth = 0;
939 ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
940
941 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
942 c.pt.x = 0;
943 item = editor->pBuffer->pFirst->next;
944 while(item != editor->pBuffer->pLast) {
945 BOOL bRedraw = FALSE;
946
947 assert(item->type == diParagraph);
948 if ((item->member.para.nFlags & MEPF_REWRAP)
949 || (item->member.para.pt.y != c.pt.y))
950 bRedraw = TRUE;
951 item->member.para.pt = c.pt;
952
953 ME_WrapTextParagraph(&c, item);
954
955 if (bRedraw)
956 ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
957
958 if (item->member.para.nFlags & MEPF_ROWSTART)
959 {
960 ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
961 ME_DisplayItem *endRowPara;
962 int borderWidth = 0;
963 cell->member.cell.pt = c.pt;
964 /* Offset the text by the largest top border width. */
965 while (cell->member.cell.next_cell) {
966 borderWidth = max(borderWidth, cell->member.cell.border.top.width);
967 cell = cell->member.cell.next_cell;
968 }
969 endRowPara = ME_FindItemFwd(cell, diParagraph);
970 assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
971 if (borderWidth > 0)
972 {
973 borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
974 while (cell) {
975 cell->member.cell.yTextOffset = borderWidth;
976 cell = cell->member.cell.prev_cell;
977 }
978 c.pt.y += borderWidth;
979 }
980 if (endRowPara->member.para.pFmt->dxStartIndent > 0)
981 {
982 int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
983 cell = ME_FindItemFwd(item, diCell);
984 cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
985 c.pt.x = cell->member.cell.pt.x;
986 }
987 }
988 else if (item->member.para.nFlags & MEPF_ROWEND)
989 {
990 /* Set all the cells to the height of the largest cell */
991 ME_DisplayItem *startRowPara;
992 int prevHeight, nHeight, bottomBorder = 0;
993 ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
994 item->member.para.nWidth = cell->member.cell.pt.x + cell->member.cell.nWidth;
995 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
996 {
997 /* Last row, the bottom border is added to the height. */
998 cell = cell->member.cell.prev_cell;
999 while (cell)
1000 {
1001 bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
1002 cell = cell->member.cell.prev_cell;
1003 }
1004 bottomBorder = ME_twips2pointsY(&c, bottomBorder);
1005 cell = ME_FindItemBack(item, diCell);
1006 }
1007 prevHeight = cell->member.cell.nHeight;
1008 nHeight = cell->member.cell.prev_cell->member.cell.nHeight + bottomBorder;
1009 cell->member.cell.nHeight = nHeight;
1010 item->member.para.nHeight = nHeight;
1011 cell = cell->member.cell.prev_cell;
1012 cell->member.cell.nHeight = nHeight;
1013 while (cell->member.cell.prev_cell)
1014 {
1015 cell = cell->member.cell.prev_cell;
1016 cell->member.cell.nHeight = nHeight;
1017 }
1018 /* Also set the height of the start row paragraph */
1019 startRowPara = ME_FindItemBack(cell, diParagraph);
1020 startRowPara->member.para.nHeight = nHeight;
1021 c.pt.x = startRowPara->member.para.pt.x;
1022 c.pt.y = cell->member.cell.pt.y + nHeight;
1023 if (prevHeight < nHeight)
1024 {
1025 /* The height of the cells has grown, so invalidate the bottom of
1026 * the cells. */
1027 ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
1028 cell = ME_FindItemBack(item, diCell);
1029 while (cell) {
1030 ME_MarkRepaintEnd(ME_FindItemBack(cell, diParagraph), &repaint_start, &repaint_end);
1031 cell = cell->member.cell.prev_cell;
1032 }
1033 }
1034 }
1035 else if (item->member.para.pCell &&
1036 item->member.para.pCell != item->member.para.next_para->member.para.pCell)
1037 {
1038 /* The next paragraph is in the next cell in the table row. */
1039 ME_Cell *cell = &item->member.para.pCell->member.cell;
1040 cell->nHeight = c.pt.y + item->member.para.nHeight - cell->pt.y;
1041
1042 /* Propagate the largest height to the end so that it can be easily
1043 * sent back to all the cells at the end of the row. */
1044 if (cell->prev_cell)
1045 cell->nHeight = max(cell->nHeight, cell->prev_cell->member.cell.nHeight);
1046
1047 c.pt.x = cell->pt.x + cell->nWidth;
1048 c.pt.y = cell->pt.y;
1049 cell->next_cell->member.cell.pt = c.pt;
1050 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWEND))
1051 c.pt.y += cell->yTextOffset;
1052 }
1053 else
1054 {
1055 if (item->member.para.pCell) {
1056 /* Next paragraph in the same cell. */
1057 c.pt.x = item->member.para.pCell->member.cell.pt.x;
1058 } else {
1059 /* Normal paragraph */
1060 c.pt.x = 0;
1061 }
1062 c.pt.y += item->member.para.nHeight;
1063 }
1064
1065 totalWidth = max(totalWidth, item->member.para.nWidth);
1066 item = item->member.para.next_para;
1067 }
1068 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
1069 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
1070
1071 editor->nTotalLength = c.pt.y;
1072 editor->nTotalWidth = totalWidth;
1073 editor->pBuffer->pLast->member.para.pt.x = 0;
1074 editor->pBuffer->pLast->member.para.pt.y = c.pt.y;
1075
1076 ME_DestroyContext(&c);
1077
1078 if (repaint_start || editor->nTotalLength < editor->nLastTotalLength)
1079 ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
1080 return !!repaint_start;
1081 }
1082
1083 void ME_InvalidateParagraphRange(ME_TextEditor *editor,
1084 ME_DisplayItem *start_para,
1085 ME_DisplayItem *last_para)
1086 {
1087 ME_Context c;
1088 RECT rc;
1089 int ofs;
1090
1091 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
1092 rc = c.rcView;
1093 ofs = editor->vert_si.nPos;
1094
1095 if (start_para) {
1096 start_para = ME_GetOuterParagraph(start_para);
1097 last_para = ME_GetOuterParagraph(last_para);
1098 rc.top = c.rcView.top + start_para->member.para.pt.y - ofs;
1099 } else {
1100 rc.top = c.rcView.top + editor->nTotalLength - ofs;
1101 }
1102 if (editor->nTotalLength < editor->nLastTotalLength)
1103 rc.bottom = c.rcView.top + editor->nLastTotalLength - ofs;
1104 else
1105 rc.bottom = c.rcView.top + last_para->member.para.pt.y + last_para->member.para.nHeight - ofs;
1106 ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
1107
1108 ME_DestroyContext(&c);
1109 }
1110
1111
1112 void
1113 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
1114 {
1115 if (editor->nEventMask & ENM_REQUESTRESIZE)
1116 {
1117 RECT rc;
1118
1119 ITextHost_TxGetClientRect(editor->texthost, &rc);
1120
1121 if (force || rc.bottom != editor->nTotalLength)
1122 {
1123 REQRESIZE info;
1124
1125 info.nmhdr.hwndFrom = NULL;
1126 info.nmhdr.idFrom = 0;
1127 info.nmhdr.code = EN_REQUESTRESIZE;
1128 info.rc = rc;
1129 info.rc.right = editor->nTotalWidth;
1130 info.rc.bottom = editor->nTotalLength;
1131
1132 editor->nEventMask &= ~ENM_REQUESTRESIZE;
1133 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
1134 editor->nEventMask |= ENM_REQUESTRESIZE;
1135 }
1136 }
1137 }