msi: Correctly parse double quotes in the token value.
[wine/multimedia.git] / dlls / riched20 / wrap.c
blob6c9f2e3d5dd61be7dd0b380fb74b8a156d7f5d08
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);
28 * Unsolved problems:
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
36 static BOOL get_run_glyph_buffers( ME_Run *run )
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;
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)));
46 return TRUE;
49 static HRESULT shape_run( ME_Context *c, ME_Run *run )
51 HRESULT hr;
52 HFONT old_font;
53 int i;
55 if (!run->glyphs)
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 );
62 if (run->max_clusters < run->len)
64 heap_free( run->clusters );
65 run->max_clusters = run->len * 2;
66 run->clusters = heap_alloc( run->max_clusters * sizeof(WORD) );
69 old_font = ME_SelectStyleFont( c, run->style );
70 while (1)
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 );
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 );
84 if (SUCCEEDED(hr))
86 for (i = 0, run->nWidth = 0; i < run->num_glyphs; i++)
87 run->nWidth += run->advances[i];
90 ME_UnselectStyleFont( c, run->style, old_font );
92 return hr;
95 /******************************************************************************
96 * calc_run_extent
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.
102 static void calc_run_extent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
104 if (run->nFlags & MERF_HIDDEN) run->nWidth = 0;
105 else
107 SIZE size = ME_GetRunSizeCommon( c, para, run, run->len, startx, &run->nAscent, &run->nDescent );
108 run->nWidth = size.cx;
112 /******************************************************************************
113 * split_run_extents
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.
118 static ME_DisplayItem *split_run_extents(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
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};
125 assert(item->member.run.nCharOfs != -1);
126 if(TRACE_ON(richedit))
128 TRACE("Before check before split\n");
129 ME_CheckCharOffsets(editor);
130 TRACE("After check before split\n");
133 run = &item->member.run;
135 TRACE("Before split: %s(%d, %d)\n", debugstr_run( run ),
136 run->pt.x, run->pt.y);
138 ME_SplitRunSimple(editor, &cursor);
140 run2 = &cursor.pRun->member.run;
141 run2->script_analysis = run->script_analysis;
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);
147 run2->pt.x = run->pt.x+run->nWidth;
148 run2->pt.y = run->pt.y;
150 if(TRACE_ON(richedit))
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);
160 return cursor.pRun;
163 /******************************************************************************
164 * find_split_point
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).
170 static int find_split_point( ME_Context *c, int cx, ME_Run *run )
172 if (!run->len || cx <= 0) return 0;
173 return ME_CharFromPointContext( c, cx, run, FALSE, FALSE );
176 static ME_DisplayItem *ME_MakeRow(int height, int baseline, int width)
178 ME_DisplayItem *item = ME_MakeDI(diStartRow);
180 item->member.row.nHeight = height;
181 item->member.row.nBaseline = baseline;
182 item->member.row.nWidth = width;
183 return item;
186 static void ME_BeginRow(ME_WrapContext *wc)
188 PARAFORMAT2 *pFmt;
189 ME_DisplayItem *para = wc->pPara;
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)
201 ME_Cell *cell = &ME_FindItemBack(para, diCell)->member.cell;
202 cell->nWidth = 0;
204 } else if (para->member.para.pCell) {
205 ME_Cell *cell = &para->member.para.pCell->member.cell;
206 int width;
208 width = cell->nRightBoundary;
209 if (cell->prev_cell)
210 width -= cell->prev_cell->member.cell.nRightBoundary;
211 if (!cell->prev_cell)
213 int rowIndent = ME_GetTableRowEnd(para)->member.para.pFmt->dxStartIndent;
214 width -= rowIndent;
216 cell->nWidth = max(ME_twips2pointsX(wc->context, width), 0);
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;
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++;
232 static void layout_row( ME_DisplayItem *start, const ME_DisplayItem *end )
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;
241 for (p = end->prev; p != start->prev; p = p->prev)
243 if (p->type == diRun)
245 if (!found_black) found_black = !(p->member.run.nFlags & (MERF_WHITESPACE | MERF_ENDPARA));
246 if (found_black) num_runs++;
250 TRACE("%d runs\n", num_runs);
251 if (!num_runs) return;
253 if (num_runs > sizeof(buf) / (sizeof(buf[0]) * 5))
254 vis_to_log = heap_alloc( num_runs * sizeof(int) * 5 );
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);
261 for (i = 0, p = start; i < num_runs; p = p->next)
263 if (p->type == diRun)
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++;
272 ScriptLayout( num_runs, levels, vis_to_log, log_to_vis );
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 ] ];
278 for (i = 0, p = start; i < num_runs; p = p->next)
280 if (p->type == diRun)
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++;
288 if (vis_to_log != buf) heap_free( vis_to_log );
291 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
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;
298 /* wrap text */
300 for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
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)
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 );
315 assert (len);
316 if (~p->member.run.nFlags & MERF_GRAPHICS)
317 while (len && *(text--) == ' ')
318 len--;
319 if (len)
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 );
326 bSkippingSpaces = !len;
327 } else if (!(p->member.run.nFlags & MERF_ENDPARA))
328 width += p->member.run.nWidth;
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))
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;
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);
353 if (para->nFlags & MEPF_COMPLEX) layout_row( wc->pRowStart, pEnd );
355 row->member.row.pt.x = row->member.row.nLMargin + shift;
356 for (p = wc->pRowStart; p!=pEnd; p = p->next)
358 if (p->type==diRun) { /* FIXME add more run types */
359 p->member.run.pt.x += row->member.row.nLMargin+shift;
362 ME_InsertBefore(wc->pRowStart, row);
363 wc->nRow++;
364 wc->pt.y += row->member.row.nHeight;
365 ME_BeginRow(wc);
368 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
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)
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--;
383 p = para->next;
384 while(p) {
385 if (p->type == diParagraph || p->type == diTextEnd)
386 return;
387 if (p->type == diRun)
389 ME_Run *run = &p->member.run;
390 TRACE("%s - (%d, %d)\n", debugstr_run(run), run->pt.x, run->pt.y);
392 p = p->next;
397 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
399 /* FIXME compose style (out of character and paragraph styles) here */
401 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
403 calc_run_extent(wc->context, &wc->pPara->member.para,
404 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
408 static int find_non_whitespace(const WCHAR *s, int len, int start)
410 int i;
411 for (i = start; i < len && ME_IsWSpace( s[i] ); i++)
414 return i;
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 */
420 /* note: returns offset of the first trailing whitespace */
421 static int reverse_find_non_whitespace(const WCHAR *s, int start)
423 int i;
424 for (i = start; i > 0 && ME_IsWSpace( s[i - 1] ); i--)
427 return i;
430 /* note: returns offset of the first trailing nonwhitespace */
431 static int reverse_find_whitespace(const WCHAR *s, int start)
433 int i;
434 for (i = start; i > 0 && !ME_IsWSpace( s[i - 1] ); i--)
437 return i;
440 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
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;
452 else
454 pp = piter;
455 /* omit all spaces before split point */
456 while(piter != wc->pRowStart)
458 piter = ME_FindItemBack(piter, diRun);
459 if (piter->member.run.nFlags & MERF_WHITESPACE)
461 pp = piter;
462 continue;
464 if (piter->member.run.nFlags & MERF_ENDWHITE)
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;
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;
478 wc->pt = piter->member.run.pt;
479 return piter;
483 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
485 ME_DisplayItem *piter = p, *pp;
486 int i, idesp, len;
487 ME_Run *run = &p->member.run;
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;
500 TRACE("Must backtrack to split at: %s\n", debugstr_run( &p->member.run ));
501 if (wc->pLastSplittableRun)
503 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
505 wc->pt = wc->pLastSplittableRun->member.run.pt;
506 return wc->pLastSplittableRun;
508 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
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));
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;
527 /* splittable = must have whitespaces */
528 assert(0 == "Splittable, but no whitespaces");
530 else
532 /* restart from the first run beginning with spaces */
533 wc->pt = wc->pLastSplittableRun->member.run.pt;
534 return wc->pLastSplittableRun;
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)
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;
549 else
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);
556 /* the run is one char, can't split it */
557 return piter;
561 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
563 ME_DisplayItem *pp;
564 ME_Run *run;
565 int len;
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;
576 if (wc->bOverflown) /* just skipping final whitespaces */
578 /* End paragraph run can't overflow to the next line by itself. */
579 if (run->nFlags & MERF_ENDPARA)
580 return p->next;
582 if (run->nFlags & MERF_WHITESPACE) {
583 wc->pt.x += run->nWidth;
584 /* skip runs consisting of only whitespaces */
585 return p->next;
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;
602 /* black run: the row goes from pRowStart to the previous run */
603 ME_InsertRowStart(wc, p);
604 return p;
606 /* simply end the current row and move on to next one */
607 if (run->nFlags & MERF_ENDROW)
609 p = p->next;
610 ME_InsertRowStart(wc, p);
611 return p;
614 /* will current run fit? */
615 if (wc->bWordWrap &&
616 wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
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;
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;
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;
640 /* can we separate out the last spaces ? (to use overflow logic later) */
641 if (run->nFlags & MERF_ENDWHITE)
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;
649 /* determine the split point by backtracking */
650 pp = ME_SplitByBacktracking(wc, p, loc);
651 if (pp == wc->pRowStart)
653 if (run->nFlags & MERF_STARTWHITE)
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. */
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;
665 /* Couldn't split the first run, possible because we have a large font
666 * with a single character that caused an overflow.
668 wc->pt.x += run->nWidth;
669 return p->next;
671 if (p != pp) /* found a suitable split point */
673 wc->bOverflown = TRUE;
674 return pp;
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 */
682 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
683 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
685 wc->pLastSplittableRun = p;
687 wc->pt.x += run->nWidth;
688 return p->next;
691 static int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
693 int sp = 0, ls = 0;
694 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
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)
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);
708 if (c->editor->nZoomNumerator == 0)
709 return sp;
710 else
711 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
714 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
715 ME_DisplayItem *p;
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);
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);
741 static HRESULT itemize_para( ME_Context *c, ME_DisplayItem *p )
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;
753 assert( p->type == diParagraph );
755 while (1)
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;
768 if (FAILED( hr )) goto end;
770 if (TRACE_ON( richedit ))
772 TRACE( "got items:\n" );
773 for (cur_item = 0; cur_item < num_items; cur_item++)
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 );
779 TRACE( "before splitting runs into ranges\n" );
780 for (di = p->next; di != p->member.para.next_para; di = di->next)
782 if (di->type != diRun) continue;
783 TRACE( "\t%d: %s\n", di->member.run.nCharOfs, debugstr_run( &di->member.run ) );
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)
790 if (di->type != diRun) continue;
791 run = &di->member.run;
793 if (run->nCharOfs == items[cur_item+1].iCharPos) cur_item++;
795 items[cur_item].a.fLogicalOrder = TRUE;
796 run->script_analysis = items[cur_item].a;
798 if (run->nFlags & MERF_ENDPARA) break; /* don't split eop runs */
800 if (run->nCharOfs + run->len > items[cur_item+1].iCharPos)
802 ME_Cursor cursor = {p, di, items[cur_item+1].iCharPos - run->nCharOfs};
803 ME_SplitRunSimple( c->editor, &cursor );
807 if (TRACE_ON( richedit ))
809 TRACE( "after splitting into ranges\n" );
810 for (di = p->next; di != p->member.para.next_para; di = di->next)
812 if (di->type != diRun) continue;
813 TRACE( "\t%d: %s\n", di->member.run.nCharOfs, debugstr_run( &di->member.run ) );
817 para->nFlags |= MEPF_COMPLEX;
819 end:
820 if (items != buf) heap_free( items );
821 return hr;
825 static HRESULT shape_para( ME_Context *c, ME_DisplayItem *p )
827 ME_DisplayItem *di;
828 ME_Run *run;
829 HRESULT hr;
831 for (di = p->next; di != p->member.para.next_para; di = di->next)
833 if (di->type != diRun) continue;
834 run = &di->member.run;
836 hr = shape_run( c, run );
837 if (FAILED( hr ))
839 run->para->nFlags &= ~MEPF_COMPLEX;
840 return hr;
843 return hr;
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;
853 assert(tp->type == diParagraph);
854 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
855 return;
857 ME_PrepareParagraphForWrapping(c, tp);
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 */)
863 if (SUCCEEDED( itemize_para( c, tp ) ))
864 shape_para( c, tp );
867 pFmt = tp->member.para.pFmt;
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;
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);
884 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
885 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
887 wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
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)
896 border = ME_GetParaBorderWidth(c, tp->member.para.pFmt->wBorders);
897 if (pFmt->wBorders & 1) {
898 wc.nFirstMargin += border;
899 wc.nLeftMargin += border;
901 if (pFmt->wBorders & 2)
902 wc.nRightMargin -= border;
903 if (pFmt->wBorders & 4)
904 wc.pt.y += border;
907 linespace = ME_GetParaLineSpace(c, &tp->member.para);
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);
915 else p = p->next;
916 if (wc.nRow && p == wc.pRowStart)
917 wc.pt.y += linespace;
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);
926 tp->member.para.nFlags &= ~MEPF_REWRAP;
927 tp->member.para.nHeight = wc.pt.y;
928 tp->member.para.nRows = wc.nRow;
931 static void ME_MarkRepaintEnd(ME_DisplayItem *para,
932 ME_DisplayItem **repaint_start,
933 ME_DisplayItem **repaint_end)
935 if (!*repaint_start)
936 *repaint_start = para;
937 *repaint_end = para;
940 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
942 ME_DisplayItem *item;
943 ME_Context c;
944 int totalWidth = 0;
945 ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
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;
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;
959 ME_WrapTextParagraph(&c, item);
961 if (bRedraw)
962 ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
964 if (item->member.para.nFlags & MEPF_ROWSTART)
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;
975 endRowPara = ME_FindItemFwd(cell, diParagraph);
976 assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
977 if (borderWidth > 0)
979 borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
980 while (cell) {
981 cell->member.cell.yTextOffset = borderWidth;
982 cell = cell->member.cell.prev_cell;
984 c.pt.y += borderWidth;
986 if (endRowPara->member.para.pFmt->dxStartIndent > 0)
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;
994 else if (item->member.para.nFlags & MEPF_ROWEND)
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))
1003 /* Last row, the bottom border is added to the height. */
1004 cell = cell->member.cell.prev_cell;
1005 while (cell)
1007 bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
1008 cell = cell->member.cell.prev_cell;
1010 bottomBorder = ME_twips2pointsY(&c, bottomBorder);
1011 cell = ME_FindItemBack(item, diCell);
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)
1021 cell = cell->member.cell.prev_cell;
1022 cell->member.cell.nHeight = nHeight;
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)
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;
1041 else if (item->member.para.pCell &&
1042 item->member.para.pCell != item->member.para.next_para->member.para.pCell)
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;
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);
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;
1059 else
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;
1068 c.pt.y += item->member.para.nHeight;
1071 totalWidth = max(totalWidth, item->member.para.nWidth);
1072 item = item->member.para.next_para;
1074 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
1075 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
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;
1082 ME_DestroyContext(&c);
1084 if (repaint_start || editor->nTotalLength < editor->nLastTotalLength)
1085 ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
1086 return !!repaint_start;
1089 void ME_InvalidateParagraphRange(ME_TextEditor *editor,
1090 ME_DisplayItem *start_para,
1091 ME_DisplayItem *last_para)
1093 ME_Context c;
1094 RECT rc;
1095 int ofs;
1097 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
1098 rc = c.rcView;
1099 ofs = editor->vert_si.nPos;
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;
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);
1114 ME_DestroyContext(&c);
1118 void
1119 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
1121 if (editor->nEventMask & ENM_REQUESTRESIZE)
1123 RECT rc;
1125 ITextHost_TxGetClientRect(editor->texthost, &rc);
1127 if (force || rc.bottom != editor->nTotalLength)
1129 REQRESIZE info;
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;
1138 editor->nEventMask &= ~ENM_REQUESTRESIZE;
1139 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
1140 editor->nEventMask |= ENM_REQUESTRESIZE;