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