2 * RichEdit - Paragraph wrapping. Don't try to understand it. You've been
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
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
30 * - center and right align in WordPad omits all spaces at the start, we don't
31 * - objects/images are not handled yet
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)));
49 static HRESULT
shape_run( ME_Context
*c
, ME_Run
*run
)
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
);
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 */
77 get_run_glyph_buffers( run
);
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
);
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
);
95 /******************************************************************************
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
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;
107 SIZE size
= ME_GetRunSizeCommon( c
, para
, run
, run
->len
, startx
, &run
->nAscent
, &run
->nDescent
);
108 run
->nWidth
= size
.cx
;
112 /******************************************************************************
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
;
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
);
163 /******************************************************************************
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
;
186 static void ME_BeginRow(ME_WrapContext
*wc
)
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
)) {
198 wc
->bWordWrap
= FALSE
;
199 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
201 ME_Cell
*cell
= &ME_FindItemBack(para
, diCell
)->member
.cell
;
204 } else if (para
->member
.para
.pCell
) {
205 ME_Cell
*cell
= ¶
->member
.para
.pCell
->member
.cell
;
208 width
= cell
->nRightBoundary
;
210 width
-= cell
->prev_cell
->member
.cell
.nRightBoundary
;
211 if (!cell
->prev_cell
)
213 int rowIndent
= ME_GetTableRowEnd(para
)->member
.para
.pFmt
->dxStartIndent
;
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
;
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. */
232 static void layout_row( ME_DisplayItem
*start
, const ME_DisplayItem
*end
)
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
;
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
] );
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
);
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;
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
;
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 );
316 if (~p
->member
.run
.nFlags
& MERF_GRAPHICS
)
317 while (len
&& *(text
--) == ' ')
321 if (len
== p
->member
.run
.len
)
322 width
+= p
->member
.run
.nWidth
;
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. */
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
);
364 wc
->pt
.y
+= row
->member
.row
.nHeight
;
368 static void ME_WrapEndParagraph(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
370 ME_DisplayItem
*para
= wc
->pPara
;
371 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
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. */
385 if (p->type == diParagraph || p->type == diTextEnd)
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);
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
)
411 for (i
= start
; i
< len
&& ME_IsWSpace( s
[i
] ); 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
)
424 for (i
= start
; i
> 0 && ME_IsWSpace( s
[i
- 1] ); i
--)
430 /* note: returns offset of the first trailing nonwhitespace */
431 static int reverse_find_whitespace(const WCHAR
*s
, int start
)
434 for (i
= start
; i
> 0 && !ME_IsWSpace( s
[i
- 1] ); i
--)
440 static ME_DisplayItem
*ME_MaximizeSplit(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int i
)
442 ME_DisplayItem
*pp
, *piter
= p
;
446 j
= reverse_find_non_whitespace( get_text( &p
->member
.run
, 0 ), i
);
448 pp
= split_run_extents(wc
, piter
, j
);
449 wc
->pt
.x
+= piter
->member
.run
.nWidth
;
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
)
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
;
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
));
478 wc
->pt
= piter
->member
.run
.pt
;
483 static ME_DisplayItem
*ME_SplitByBacktracking(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int loc
)
485 ME_DisplayItem
*piter
= p
, *pp
;
487 ME_Run
*run
= &p
->member
.run
;
489 idesp
= i
= find_split_point( wc
->context
, loc
, run
);
494 /* don't split words */
495 i
= reverse_find_whitespace( get_text( run
, 0 ), i
);
496 pp
= ME_MaximizeSplit(wc
, p
, i
);
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
;
518 /* don't split words */
519 i
= reverse_find_whitespace( get_text( run
, 0 ), len
);
521 i
= reverse_find_non_whitespace( get_text( run
, 0 ), len
);
523 ME_DisplayItem
*piter2
= split_run_extents(wc
, piter
, i
);
524 wc
->pt
= piter2
->member
.run
.pt
;
527 /* splittable = must have whitespaces */
528 assert(0 == "Splittable, but no whitespaces");
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*/
540 return split_run_extents(wc
, piter
, idesp
);
542 if (wc
->pRowStart
&& piter
!= wc
->pRowStart
)
544 /* don't need to break current run, because it's possible to split
546 wc
->bOverflown
= TRUE
;
551 /* split point inside first character - no choice but split after that char */
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 */
561 static ME_DisplayItem
*ME_WrapHandleRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
567 assert(p
->type
== diRun
);
570 run
= &p
->member
.run
;
571 run
->pt
.x
= wc
->pt
.x
;
572 run
->pt
.y
= wc
->pt
.y
;
573 ME_WrapSizeRun(wc
, p
);
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
)
582 if (run
->nFlags
& MERF_WHITESPACE
) {
583 wc
->pt
.x
+= run
->nWidth
;
584 /* skip runs consisting of only whitespaces */
588 if (run
->nFlags
& MERF_STARTWHITE
) {
589 /* try to split the run at the first non-white char */
591 black
= find_non_whitespace( get_text( run
, 0 ), run
->len
, 0 );
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
,
598 ME_InsertRowStart(wc
, pp
);
602 /* black run: the row goes from pRowStart to the previous run */
603 ME_InsertRowStart(wc
, p
);
606 /* simply end the current row and move on to next one */
607 if (run
->nFlags
& MERF_ENDROW
)
610 ME_InsertRowStart(wc
, p
);
614 /* will current run fit? */
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
;
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
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
;
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 */
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
;
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
;
671 if (p
!= pp
) /* found a suitable split point */
673 wc
->bOverflown
= TRUE
;
676 /* we detected that it's best to split on start of this run */
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
;
691 static int ME_GetParaLineSpace(ME_Context
* c
, ME_Paragraph
* para
)
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)
711 return sp
* c
->editor
->nZoomNumerator
/ c
->editor
->nZoomDenominator
;
714 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
) {
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
;
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
;
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
,
750 SCRIPT_STATE state
= { 0, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, 0, 0 };
753 assert( p
->type
== diParagraph
);
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 */
763 items
= heap_alloc( items_passed
* sizeof( *items
) );
765 items
= heap_realloc( items
, items_passed
* sizeof( *items
) );
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
;
820 if (items
!= buf
) heap_free( items
);
825 static HRESULT
shape_para( ME_Context
*c
, ME_DisplayItem
*p
)
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
);
839 run
->para
->nFlags
&= ~MEPF_COMPLEX
;
846 static void ME_WrapTextParagraph(ME_Context
*c
, ME_DisplayItem
*tp
) {
853 assert(tp
->type
== diParagraph
);
854 if (!(tp
->member
.para
.nFlags
& MEPF_REWRAP
)) {
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
) ))
867 pFmt
= tp
->member
.para
.pFmt
;
871 /* wc.para_style = tp->member.para.style; */
873 if (tp
->member
.para
.nFlags
& MEPF_ROWEND
) {
874 wc
.nFirstMargin
= wc
.nLeftMargin
= wc
.nRightMargin
= 0;
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);
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)
907 linespace
= ME_GetParaLineSpace(c
, &tp
->member
.para
);
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
);
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))
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
)
936 *repaint_start
= para
;
940 BOOL
ME_WrapMarkedParagraphs(ME_TextEditor
*editor
)
942 ME_DisplayItem
*item
;
945 ME_DisplayItem
*repaint_start
= NULL
, *repaint_end
= NULL
;
947 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
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
))
957 item
->member
.para
.pt
= c
.pt
;
959 ME_WrapTextParagraph(&c
, item
);
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
;
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
);
979 borderWidth
= max(ME_twips2pointsY(&c
, borderWidth
), 1);
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
;
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
1033 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
1034 cell
= ME_FindItemBack(item
, diCell
);
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
;
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
;
1065 /* Normal paragraph */
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
)
1097 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
1099 ofs
= editor
->vert_si
.nPos
;
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
;
1106 rc
.top
= c
.rcView
.top
+ editor
->nTotalLength
- ofs
;
1108 if (editor
->nTotalLength
< editor
->nLastTotalLength
)
1109 rc
.bottom
= c
.rcView
.top
+ editor
->nLastTotalLength
- ofs
;
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
);
1119 ME_SendRequestResize(ME_TextEditor
*editor
, BOOL force
)
1121 if (editor
->nEventMask
& ENM_REQUESTRESIZE
)
1125 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
1127 if (force
|| rc
.bottom
!= editor
->nTotalLength
)
1131 info
.nmhdr
.hwndFrom
= NULL
;
1132 info
.nmhdr
.idFrom
= 0;
1133 info
.nmhdr
.code
= EN_REQUESTRESIZE
;
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
;