riched20: Set the base embedding level for RTL paragraphs.
[wine.git] / dlls / riched20 / wrap.c
blob49a62cef448a9b364e0fc8e8c29f22ca6f103677
1 /*
2 * RichEdit - Paragraph wrapping. Don't try to understand it. You've been
3 * warned !
5 * Copyright 2004 by Krzysztof Foltman
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.
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.
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
23 #include "editor.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 WINE_DECLARE_DEBUG_CHANNEL(richedit_check);
29 * Unsolved problems:
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
37 static BOOL get_run_glyph_buffers( ME_Run *run )
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;
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)));
47 return TRUE;
50 static HRESULT shape_run( ME_Context *c, ME_Run *run )
52 HRESULT hr;
53 HFONT old_font;
54 int i;
56 if (!run->glyphs)
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 );
63 if (run->max_clusters < run->len)
65 heap_free( run->clusters );
66 run->max_clusters = run->len * 2;
67 run->clusters = heap_alloc( run->max_clusters * sizeof(WORD) );
70 old_font = ME_SelectStyleFont( c, run->style );
71 while (1)
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 );
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 );
85 if (SUCCEEDED(hr))
87 for (i = 0, run->nWidth = 0; i < run->num_glyphs; i++)
88 run->nWidth += run->advances[i];
91 ME_UnselectStyleFont( c, run->style, old_font );
93 return hr;
96 /******************************************************************************
97 * calc_run_extent
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.
103 static void calc_run_extent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
105 if (run->nFlags & MERF_HIDDEN) run->nWidth = 0;
106 else
108 SIZE size = ME_GetRunSizeCommon( c, para, run, run->len, startx, &run->nAscent, &run->nDescent );
109 run->nWidth = size.cx;
113 /******************************************************************************
114 * split_run_extents
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.
119 static ME_DisplayItem *split_run_extents(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
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};
126 assert(item->member.run.nCharOfs != -1);
127 if(TRACE_ON(richedit_check))
128 ME_CheckCharOffsets(editor);
130 run = &item->member.run;
132 TRACE("Before split: %s(%d, %d)\n", debugstr_run( run ),
133 run->pt.x, run->pt.y);
135 ME_SplitRunSimple(editor, &cursor);
137 run2 = &cursor.pRun->member.run;
138 run2->script_analysis = run->script_analysis;
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);
144 run2->pt.x = run->pt.x+run->nWidth;
145 run2->pt.y = run->pt.y;
147 if(TRACE_ON(richedit_check))
148 ME_CheckCharOffsets(editor);
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);
154 return cursor.pRun;
157 /******************************************************************************
158 * find_split_point
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).
164 static int find_split_point( ME_Context *c, int cx, ME_Run *run )
166 if (!run->len || cx <= 0) return 0;
167 return ME_CharFromPointContext( c, cx, run, FALSE, FALSE );
170 static ME_DisplayItem *ME_MakeRow(int height, int baseline, int width)
172 ME_DisplayItem *item = ME_MakeDI(diStartRow);
174 item->member.row.nHeight = height;
175 item->member.row.nBaseline = baseline;
176 item->member.row.nWidth = width;
177 return item;
180 static void ME_BeginRow(ME_WrapContext *wc)
182 PARAFORMAT2 *pFmt;
183 ME_DisplayItem *para = wc->pPara;
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)
195 ME_Cell *cell = &ME_FindItemBack(para, diCell)->member.cell;
196 cell->nWidth = 0;
198 } else if (para->member.para.pCell) {
199 ME_Cell *cell = &para->member.para.pCell->member.cell;
200 int width;
202 width = cell->nRightBoundary;
203 if (cell->prev_cell)
204 width -= cell->prev_cell->member.cell.nRightBoundary;
205 if (!cell->prev_cell)
207 int rowIndent = ME_GetTableRowEnd(para)->member.para.pFmt->dxStartIndent;
208 width -= rowIndent;
210 cell->nWidth = max(ME_twips2pointsX(wc->context, width), 0);
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;
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++;
226 static void layout_row( ME_DisplayItem *start, const ME_DisplayItem *end )
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;
235 for (p = end->prev; p != start->prev; p = p->prev)
237 if (p->type == diRun)
239 if (!found_black) found_black = !(p->member.run.nFlags & (MERF_WHITESPACE | MERF_ENDPARA));
240 if (found_black) num_runs++;
244 TRACE("%d runs\n", num_runs);
245 if (!num_runs) return;
247 if (num_runs > sizeof(buf) / (sizeof(buf[0]) * 5))
248 vis_to_log = heap_alloc( num_runs * sizeof(int) * 5 );
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);
255 for (i = 0, p = start; i < num_runs; p = p->next)
257 if (p->type == diRun)
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++;
266 ScriptLayout( num_runs, levels, vis_to_log, log_to_vis );
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 ] ];
272 for (i = 0, p = start; i < num_runs; p = p->next)
274 if (p->type == diRun)
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++;
282 if (vis_to_log != buf) heap_free( vis_to_log );
285 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
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;
292 /* wrap text */
294 for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
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)
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 );
309 assert (len);
310 if (~p->member.run.nFlags & MERF_GRAPHICS)
311 while (len && *(text--) == ' ')
312 len--;
313 if (len)
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 );
320 bSkippingSpaces = !len;
321 } else if (!(p->member.run.nFlags & MERF_ENDPARA))
322 width += p->member.run.nWidth;
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))
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;
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);
347 if (para->nFlags & MEPF_COMPLEX) layout_row( wc->pRowStart, pEnd );
349 row->member.row.pt.x = row->member.row.nLMargin + shift;
350 for (p = wc->pRowStart; p!=pEnd; p = p->next)
352 if (p->type==diRun) { /* FIXME add more run types */
353 p->member.run.pt.x += row->member.row.nLMargin+shift;
356 ME_InsertBefore(wc->pRowStart, row);
357 wc->nRow++;
358 wc->pt.y += row->member.row.nHeight;
359 ME_BeginRow(wc);
362 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
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)
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--;
377 p = para->next;
378 while(p) {
379 if (p->type == diParagraph || p->type == diTextEnd)
380 return;
381 if (p->type == diRun)
383 ME_Run *run = &p->member.run;
384 TRACE("%s - (%d, %d)\n", debugstr_run(run), run->pt.x, run->pt.y);
386 p = p->next;
391 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
393 /* FIXME compose style (out of character and paragraph styles) here */
395 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
397 calc_run_extent(wc->context, &wc->pPara->member.para,
398 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
402 static int find_non_whitespace(const WCHAR *s, int len, int start)
404 int i;
405 for (i = start; i < len && ME_IsWSpace( s[i] ); i++)
408 return i;
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 */
414 /* note: returns offset of the first trailing whitespace */
415 static int reverse_find_non_whitespace(const WCHAR *s, int start)
417 int i;
418 for (i = start; i > 0 && ME_IsWSpace( s[i - 1] ); i--)
421 return i;
424 /* note: returns offset of the first trailing nonwhitespace */
425 static int reverse_find_whitespace(const WCHAR *s, int start)
427 int i;
428 for (i = start; i > 0 && !ME_IsWSpace( s[i - 1] ); i--)
431 return i;
434 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
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;
446 else
448 pp = piter;
449 /* omit all spaces before split point */
450 while(piter != wc->pRowStart)
452 piter = ME_FindItemBack(piter, diRun);
453 if (piter->member.run.nFlags & MERF_WHITESPACE)
455 pp = piter;
456 continue;
458 if (piter->member.run.nFlags & MERF_ENDWHITE)
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;
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;
472 wc->pt = piter->member.run.pt;
473 return piter;
477 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
479 ME_DisplayItem *piter = p, *pp;
480 int i, idesp, len;
481 ME_Run *run = &p->member.run;
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;
494 TRACE("Must backtrack to split at: %s\n", debugstr_run( &p->member.run ));
495 if (wc->pLastSplittableRun)
497 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
499 wc->pt = wc->pLastSplittableRun->member.run.pt;
500 return wc->pLastSplittableRun;
502 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
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));
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;
521 /* splittable = must have whitespaces */
522 assert(0 == "Splittable, but no whitespaces");
524 else
526 /* restart from the first run beginning with spaces */
527 wc->pt = wc->pLastSplittableRun->member.run.pt;
528 return wc->pLastSplittableRun;
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)
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;
543 else
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);
550 /* the run is one char, can't split it */
551 return piter;
555 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
557 ME_DisplayItem *pp;
558 ME_Run *run;
559 int len;
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;
570 if (wc->bOverflown) /* just skipping final whitespaces */
572 /* End paragraph run can't overflow to the next line by itself. */
573 if (run->nFlags & MERF_ENDPARA)
574 return p->next;
576 if (run->nFlags & MERF_WHITESPACE) {
577 wc->pt.x += run->nWidth;
578 /* skip runs consisting of only whitespaces */
579 return p->next;
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;
596 /* black run: the row goes from pRowStart to the previous run */
597 ME_InsertRowStart(wc, p);
598 return p;
600 /* simply end the current row and move on to next one */
601 if (run->nFlags & MERF_ENDROW)
603 p = p->next;
604 ME_InsertRowStart(wc, p);
605 return p;
608 /* will current run fit? */
609 if (wc->bWordWrap &&
610 wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
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;
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;
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;
634 /* can we separate out the last spaces ? (to use overflow logic later) */
635 if (run->nFlags & MERF_ENDWHITE)
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;
643 /* determine the split point by backtracking */
644 pp = ME_SplitByBacktracking(wc, p, loc);
645 if (pp == wc->pRowStart)
647 if (run->nFlags & MERF_STARTWHITE)
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. */
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;
659 /* Couldn't split the first run, possible because we have a large font
660 * with a single character that caused an overflow.
662 wc->pt.x += run->nWidth;
663 return p->next;
665 if (p != pp) /* found a suitable split point */
667 wc->bOverflown = TRUE;
668 return pp;
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 */
676 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
677 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
679 wc->pLastSplittableRun = p;
681 wc->pt.x += run->nWidth;
682 return p->next;
685 static int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
687 int sp = 0, ls = 0;
688 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
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)
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);
702 if (c->editor->nZoomNumerator == 0)
703 return sp;
704 else
705 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
708 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
709 ME_DisplayItem *p;
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);
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);
735 static HRESULT itemize_para( ME_Context *c, ME_DisplayItem *p )
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;
747 assert( p->type == diParagraph );
749 if (para->pFmt->dwMask & PFM_RTLPARA && para->pFmt->wEffects & PFE_RTLPARA)
750 state.uBidiLevel = 1;
752 TRACE( "Base embedding level %d\n", state.uBidiLevel );
754 while (1)
756 hr = ScriptItemize( para->text->szData, para->text->nLen, items_passed, &control,
757 &state, items, &num_items );
758 if (hr != E_OUTOFMEMORY) break; /* may not be enough items if hr == E_OUTOFMEMORY */
759 if (items_passed > para->text->nLen + 1) break; /* something else has gone wrong */
760 items_passed *= 2;
761 if (items == buf)
762 items = heap_alloc( items_passed * sizeof( *items ) );
763 else
764 items = heap_realloc( items, items_passed * sizeof( *items ) );
765 if (!items) break;
767 if (FAILED( hr )) goto end;
769 if (TRACE_ON( richedit ))
771 TRACE( "got items:\n" );
772 for (cur_item = 0; cur_item < num_items; cur_item++)
774 TRACE( "\t%d - %d RTL %d bidi level %d\n", items[cur_item].iCharPos, items[cur_item+1].iCharPos - 1,
775 items[cur_item].a.fRTL, items[cur_item].a.s.uBidiLevel );
778 TRACE( "before splitting runs into ranges\n" );
779 for (di = p->next; di != p->member.para.next_para; di = di->next)
781 if (di->type != diRun) continue;
782 TRACE( "\t%d: %s\n", di->member.run.nCharOfs, debugstr_run( &di->member.run ) );
786 /* split runs into ranges at item boundaries */
787 for (di = p->next, cur_item = 0; di != p->member.para.next_para; di = di->next)
789 if (di->type != diRun) continue;
790 run = &di->member.run;
792 if (run->nCharOfs == items[cur_item+1].iCharPos) cur_item++;
794 items[cur_item].a.fLogicalOrder = TRUE;
795 run->script_analysis = items[cur_item].a;
797 if (run->nFlags & MERF_ENDPARA) break; /* don't split eop runs */
799 if (run->nCharOfs + run->len > items[cur_item+1].iCharPos)
801 ME_Cursor cursor = {p, di, items[cur_item+1].iCharPos - run->nCharOfs};
802 ME_SplitRunSimple( c->editor, &cursor );
806 if (TRACE_ON( richedit ))
808 TRACE( "after splitting into ranges\n" );
809 for (di = p->next; di != p->member.para.next_para; di = di->next)
811 if (di->type != diRun) continue;
812 TRACE( "\t%d: %s\n", di->member.run.nCharOfs, debugstr_run( &di->member.run ) );
816 para->nFlags |= MEPF_COMPLEX;
818 end:
819 if (items != buf) heap_free( items );
820 return hr;
824 static HRESULT shape_para( ME_Context *c, ME_DisplayItem *p )
826 ME_DisplayItem *di;
827 ME_Run *run;
828 HRESULT hr;
830 for (di = p->next; di != p->member.para.next_para; di = di->next)
832 if (di->type != diRun) continue;
833 run = &di->member.run;
835 hr = shape_run( c, run );
836 if (FAILED( hr ))
838 run->para->nFlags &= ~MEPF_COMPLEX;
839 return hr;
842 return hr;
845 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) {
846 ME_DisplayItem *p;
847 ME_WrapContext wc;
848 int border = 0;
849 int linespace = 0;
850 PARAFORMAT2 *pFmt;
852 assert(tp->type == diParagraph);
853 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
854 return;
856 ME_PrepareParagraphForWrapping(c, tp);
858 /* For now treating all non-password text as complex for better testing */
859 if (!c->editor->cPasswordMask /* &&
860 ScriptIsComplex( tp->member.para.text->szData, tp->member.para.text->nLen, SIC_COMPLEX ) == S_OK */)
862 if (SUCCEEDED( itemize_para( c, tp ) ))
863 shape_para( c, tp );
866 pFmt = tp->member.para.pFmt;
868 wc.context = c;
869 wc.pPara = tp;
870 /* wc.para_style = tp->member.para.style; */
871 wc.style = NULL;
872 if (tp->member.para.nFlags & MEPF_ROWEND) {
873 wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
874 } else {
875 int dxStartIndent = pFmt->dxStartIndent;
876 if (tp->member.para.pCell) {
877 dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
879 wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
880 wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
881 wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
883 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
884 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
886 wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
888 wc.nRow = 0;
889 wc.pt.y = 0;
890 if (pFmt->dwMask & PFM_SPACEBEFORE)
891 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
892 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
893 pFmt->dwMask & PFM_BORDER)
895 border = ME_GetParaBorderWidth(c, tp->member.para.pFmt->wBorders);
896 if (pFmt->wBorders & 1) {
897 wc.nFirstMargin += border;
898 wc.nLeftMargin += border;
900 if (pFmt->wBorders & 2)
901 wc.nRightMargin -= border;
902 if (pFmt->wBorders & 4)
903 wc.pt.y += border;
906 linespace = ME_GetParaLineSpace(c, &tp->member.para);
908 ME_BeginRow(&wc);
909 for (p = tp->next; p!=tp->member.para.next_para; ) {
910 assert(p->type != diStartRow);
911 if (p->type == diRun) {
912 p = ME_WrapHandleRun(&wc, p);
914 else p = p->next;
915 if (wc.nRow && p == wc.pRowStart)
916 wc.pt.y += linespace;
918 ME_WrapEndParagraph(&wc, p);
919 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
920 (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
921 wc.pt.y += border;
922 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
923 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
925 tp->member.para.nFlags &= ~MEPF_REWRAP;
926 tp->member.para.nHeight = wc.pt.y;
927 tp->member.para.nRows = wc.nRow;
930 static void ME_MarkRepaintEnd(ME_DisplayItem *para,
931 ME_DisplayItem **repaint_start,
932 ME_DisplayItem **repaint_end)
934 if (!*repaint_start)
935 *repaint_start = para;
936 *repaint_end = para;
939 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
941 ME_DisplayItem *item;
942 ME_Context c;
943 int totalWidth = 0;
944 ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
946 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
947 c.pt.x = 0;
948 item = editor->pBuffer->pFirst->next;
949 while(item != editor->pBuffer->pLast) {
950 BOOL bRedraw = FALSE;
952 assert(item->type == diParagraph);
953 if ((item->member.para.nFlags & MEPF_REWRAP)
954 || (item->member.para.pt.y != c.pt.y))
955 bRedraw = TRUE;
956 item->member.para.pt = c.pt;
958 ME_WrapTextParagraph(&c, item);
960 if (bRedraw)
961 ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
963 if (item->member.para.nFlags & MEPF_ROWSTART)
965 ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
966 ME_DisplayItem *endRowPara;
967 int borderWidth = 0;
968 cell->member.cell.pt = c.pt;
969 /* Offset the text by the largest top border width. */
970 while (cell->member.cell.next_cell) {
971 borderWidth = max(borderWidth, cell->member.cell.border.top.width);
972 cell = cell->member.cell.next_cell;
974 endRowPara = ME_FindItemFwd(cell, diParagraph);
975 assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
976 if (borderWidth > 0)
978 borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
979 while (cell) {
980 cell->member.cell.yTextOffset = borderWidth;
981 cell = cell->member.cell.prev_cell;
983 c.pt.y += borderWidth;
985 if (endRowPara->member.para.pFmt->dxStartIndent > 0)
987 int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
988 cell = ME_FindItemFwd(item, diCell);
989 cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
990 c.pt.x = cell->member.cell.pt.x;
993 else if (item->member.para.nFlags & MEPF_ROWEND)
995 /* Set all the cells to the height of the largest cell */
996 ME_DisplayItem *startRowPara;
997 int prevHeight, nHeight, bottomBorder = 0;
998 ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
999 item->member.para.nWidth = cell->member.cell.pt.x + cell->member.cell.nWidth;
1000 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
1002 /* Last row, the bottom border is added to the height. */
1003 cell = cell->member.cell.prev_cell;
1004 while (cell)
1006 bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
1007 cell = cell->member.cell.prev_cell;
1009 bottomBorder = ME_twips2pointsY(&c, bottomBorder);
1010 cell = ME_FindItemBack(item, diCell);
1012 prevHeight = cell->member.cell.nHeight;
1013 nHeight = cell->member.cell.prev_cell->member.cell.nHeight + bottomBorder;
1014 cell->member.cell.nHeight = nHeight;
1015 item->member.para.nHeight = nHeight;
1016 cell = cell->member.cell.prev_cell;
1017 cell->member.cell.nHeight = nHeight;
1018 while (cell->member.cell.prev_cell)
1020 cell = cell->member.cell.prev_cell;
1021 cell->member.cell.nHeight = nHeight;
1023 /* Also set the height of the start row paragraph */
1024 startRowPara = ME_FindItemBack(cell, diParagraph);
1025 startRowPara->member.para.nHeight = nHeight;
1026 c.pt.x = startRowPara->member.para.pt.x;
1027 c.pt.y = cell->member.cell.pt.y + nHeight;
1028 if (prevHeight < nHeight)
1030 /* The height of the cells has grown, so invalidate the bottom of
1031 * the cells. */
1032 ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
1033 cell = ME_FindItemBack(item, diCell);
1034 while (cell) {
1035 ME_MarkRepaintEnd(ME_FindItemBack(cell, diParagraph), &repaint_start, &repaint_end);
1036 cell = cell->member.cell.prev_cell;
1040 else if (item->member.para.pCell &&
1041 item->member.para.pCell != item->member.para.next_para->member.para.pCell)
1043 /* The next paragraph is in the next cell in the table row. */
1044 ME_Cell *cell = &item->member.para.pCell->member.cell;
1045 cell->nHeight = c.pt.y + item->member.para.nHeight - cell->pt.y;
1047 /* Propagate the largest height to the end so that it can be easily
1048 * sent back to all the cells at the end of the row. */
1049 if (cell->prev_cell)
1050 cell->nHeight = max(cell->nHeight, cell->prev_cell->member.cell.nHeight);
1052 c.pt.x = cell->pt.x + cell->nWidth;
1053 c.pt.y = cell->pt.y;
1054 cell->next_cell->member.cell.pt = c.pt;
1055 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWEND))
1056 c.pt.y += cell->yTextOffset;
1058 else
1060 if (item->member.para.pCell) {
1061 /* Next paragraph in the same cell. */
1062 c.pt.x = item->member.para.pCell->member.cell.pt.x;
1063 } else {
1064 /* Normal paragraph */
1065 c.pt.x = 0;
1067 c.pt.y += item->member.para.nHeight;
1070 totalWidth = max(totalWidth, item->member.para.nWidth);
1071 item = item->member.para.next_para;
1073 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
1074 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
1076 editor->nTotalLength = c.pt.y;
1077 editor->nTotalWidth = totalWidth;
1078 editor->pBuffer->pLast->member.para.pt.x = 0;
1079 editor->pBuffer->pLast->member.para.pt.y = c.pt.y;
1081 ME_DestroyContext(&c);
1083 if (repaint_start || editor->nTotalLength < editor->nLastTotalLength)
1084 ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
1085 return !!repaint_start;
1088 void ME_InvalidateParagraphRange(ME_TextEditor *editor,
1089 ME_DisplayItem *start_para,
1090 ME_DisplayItem *last_para)
1092 ME_Context c;
1093 RECT rc;
1094 int ofs;
1096 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
1097 rc = c.rcView;
1098 ofs = editor->vert_si.nPos;
1100 if (start_para) {
1101 start_para = ME_GetOuterParagraph(start_para);
1102 last_para = ME_GetOuterParagraph(last_para);
1103 rc.top = c.rcView.top + start_para->member.para.pt.y - ofs;
1104 } else {
1105 rc.top = c.rcView.top + editor->nTotalLength - ofs;
1107 if (editor->nTotalLength < editor->nLastTotalLength)
1108 rc.bottom = c.rcView.top + editor->nLastTotalLength - ofs;
1109 else
1110 rc.bottom = c.rcView.top + last_para->member.para.pt.y + last_para->member.para.nHeight - ofs;
1111 ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
1113 ME_DestroyContext(&c);
1117 void
1118 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
1120 if (editor->nEventMask & ENM_REQUESTRESIZE)
1122 RECT rc;
1124 ITextHost_TxGetClientRect(editor->texthost, &rc);
1126 if (force || rc.bottom != editor->nTotalLength)
1128 REQRESIZE info;
1130 info.nmhdr.hwndFrom = NULL;
1131 info.nmhdr.idFrom = 0;
1132 info.nmhdr.code = EN_REQUESTRESIZE;
1133 info.rc = rc;
1134 info.rc.right = editor->nTotalWidth;
1135 info.rc.bottom = editor->nTotalLength;
1137 editor->nEventMask &= ~ENM_REQUESTRESIZE;
1138 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
1139 editor->nEventMask |= ENM_REQUESTRESIZE;