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
);
26 WINE_DECLARE_DEBUG_CHANNEL(richedit_check
);
31 * - center and right align in WordPad omits all spaces at the start, we don't
32 * - objects/images are not handled yet
36 typedef struct tagME_WrapContext
40 int nLeftMargin
, nRightMargin
;
41 int nFirstMargin
; /* Offset to first line's text, always to the text itself even if a para number is present */
42 int nParaNumOffset
; /* Offset to the para number */
43 int nAvailWidth
; /* Width avail for text to wrap into. Does not include any para number text */
46 BOOL bOverflown
, bWordWrap
;
47 ME_DisplayItem
*pPara
;
48 ME_DisplayItem
*pRowStart
;
50 ME_DisplayItem
*pLastSplittableRun
;
53 static BOOL
get_run_glyph_buffers( ME_Run
*run
)
55 heap_free( run
->glyphs
);
56 run
->glyphs
= heap_alloc( run
->max_glyphs
* (sizeof(WORD
) + sizeof(SCRIPT_VISATTR
) + sizeof(int) + sizeof(GOFFSET
)) );
57 if (!run
->glyphs
) return FALSE
;
59 run
->vis_attrs
= (SCRIPT_VISATTR
*)((char*)run
->glyphs
+ run
->max_glyphs
* sizeof(WORD
));
60 run
->advances
= (int*)((char*)run
->glyphs
+ run
->max_glyphs
* (sizeof(WORD
) + sizeof(SCRIPT_VISATTR
)));
61 run
->offsets
= (GOFFSET
*)((char*)run
->glyphs
+ run
->max_glyphs
* (sizeof(WORD
) + sizeof(SCRIPT_VISATTR
) + sizeof(int)));
66 static HRESULT
shape_run( ME_Context
*c
, ME_Run
*run
)
74 run
->max_glyphs
= 1.5 * run
->len
+ 16; /* This is suggested in the uniscribe documentation */
75 run
->max_glyphs
= (run
->max_glyphs
+ 7) & ~7; /* Keep alignment simple */
76 get_run_glyph_buffers( run
);
79 if (run
->max_clusters
< run
->len
)
81 heap_free( run
->clusters
);
82 run
->max_clusters
= run
->len
* 2;
83 run
->clusters
= heap_alloc( run
->max_clusters
* sizeof(WORD
) );
86 old_font
= ME_SelectStyleFont( c
, run
->style
);
89 hr
= ScriptShape( c
->hDC
, &run
->style
->script_cache
, get_text( run
, 0 ), run
->len
, run
->max_glyphs
,
90 &run
->script_analysis
, run
->glyphs
, run
->clusters
, run
->vis_attrs
, &run
->num_glyphs
);
91 if (hr
!= E_OUTOFMEMORY
) break;
92 if (run
->max_glyphs
> 10 * run
->len
) break; /* something has clearly gone wrong */
94 get_run_glyph_buffers( run
);
98 hr
= ScriptPlace( c
->hDC
, &run
->style
->script_cache
, run
->glyphs
, run
->num_glyphs
, run
->vis_attrs
,
99 &run
->script_analysis
, run
->advances
, run
->offsets
, NULL
);
103 for (i
= 0, run
->nWidth
= 0; i
< run
->num_glyphs
; i
++)
104 run
->nWidth
+= run
->advances
[i
];
107 ME_UnselectStyleFont( c
, run
->style
, old_font
);
112 /******************************************************************************
115 * Updates the size of the run (fills width, ascent and descent). The height
116 * is calculated based on whole row's ascent and descent anyway, so no need
119 static void calc_run_extent(ME_Context
*c
, const ME_Paragraph
*para
, int startx
, ME_Run
*run
)
121 if (run
->nFlags
& MERF_HIDDEN
) run
->nWidth
= 0;
124 SIZE size
= ME_GetRunSizeCommon( c
, para
, run
, run
->len
, startx
, &run
->nAscent
, &run
->nDescent
);
125 run
->nWidth
= size
.cx
;
129 /******************************************************************************
132 * Splits a run into two in a given place. It also updates the screen position
133 * and size (extent) of the newly generated runs.
135 static ME_DisplayItem
*split_run_extents(ME_WrapContext
*wc
, ME_DisplayItem
*item
, int nVChar
)
137 ME_TextEditor
*editor
= wc
->context
->editor
;
139 ME_Paragraph
*para
= &wc
->pPara
->member
.para
;
140 ME_Cursor cursor
= {wc
->pPara
, item
, nVChar
};
142 assert(item
->member
.run
.nCharOfs
!= -1);
143 if(TRACE_ON(richedit_check
))
144 ME_CheckCharOffsets(editor
);
146 run
= &item
->member
.run
;
148 TRACE("Before split: %s(%d, %d)\n", debugstr_run( run
),
149 run
->pt
.x
, run
->pt
.y
);
151 ME_SplitRunSimple(editor
, &cursor
);
153 run2
= &cursor
.pRun
->member
.run
;
154 run2
->script_analysis
= run
->script_analysis
;
156 shape_run( wc
->context
, run
);
157 shape_run( wc
->context
, run2
);
158 calc_run_extent(wc
->context
, para
, wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, run
);
160 run2
->pt
.x
= run
->pt
.x
+run
->nWidth
;
161 run2
->pt
.y
= run
->pt
.y
;
163 if(TRACE_ON(richedit_check
))
164 ME_CheckCharOffsets(editor
);
166 TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
167 debugstr_run( run
), run
->pt
.x
, run
->pt
.y
,
168 debugstr_run( run2
), run2
->pt
.x
, run2
->pt
.y
);
173 /******************************************************************************
176 * Returns a character position to split inside the run given a run-relative
177 * pixel horizontal position. This version rounds left (ie. if the second
178 * character is at pixel position 8, then for cx=0..7 it returns 0).
180 static int find_split_point( ME_Context
*c
, int cx
, ME_Run
*run
)
182 if (!run
->len
|| cx
<= 0) return 0;
183 return ME_CharFromPointContext( c
, cx
, run
, FALSE
, FALSE
);
186 static ME_DisplayItem
*ME_MakeRow(int height
, int baseline
, int width
)
188 ME_DisplayItem
*item
= ME_MakeDI(diStartRow
);
190 item
->member
.row
.nHeight
= height
;
191 item
->member
.row
.nBaseline
= baseline
;
192 item
->member
.row
.nWidth
= width
;
196 static void ME_BeginRow(ME_WrapContext
*wc
)
199 ME_DisplayItem
*para
= wc
->pPara
;
201 pFmt
= ¶
->member
.para
.fmt
;
202 wc
->pRowStart
= NULL
;
203 wc
->bOverflown
= FALSE
;
204 wc
->pLastSplittableRun
= NULL
;
205 wc
->bWordWrap
= wc
->context
->editor
->bWordWrap
;
206 if (para
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
)) {
208 wc
->bWordWrap
= FALSE
;
209 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
211 ME_Cell
*cell
= &ME_FindItemBack(para
, diCell
)->member
.cell
;
214 } else if (para
->member
.para
.pCell
) {
215 ME_Cell
*cell
= ¶
->member
.para
.pCell
->member
.cell
;
218 width
= cell
->nRightBoundary
;
220 width
-= cell
->prev_cell
->member
.cell
.nRightBoundary
;
221 if (!cell
->prev_cell
)
223 int rowIndent
= ME_GetTableRowEnd(para
)->member
.para
.fmt
.dxStartIndent
;
226 cell
->nWidth
= max(ME_twips2pointsX(wc
->context
, width
), 0);
228 wc
->nAvailWidth
= cell
->nWidth
229 - (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
230 wc
->bWordWrap
= TRUE
;
232 wc
->nAvailWidth
= wc
->context
->nAvailWidth
233 - (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
235 wc
->pt
.x
= wc
->context
->pt
.x
;
236 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
237 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
238 /* Shift the text down because of the border. */
242 static void layout_row( ME_DisplayItem
*start
, const ME_DisplayItem
*end
)
246 int buf
[16 * 5]; /* 5 arrays - 4 of int & 1 of BYTE, alloc space for 5 of ints */
247 int *vis_to_log
= buf
, *log_to_vis
, *widths
, *pos
;
249 BOOL found_black
= FALSE
;
251 for (p
= end
->prev
; p
!= start
->prev
; p
= p
->prev
)
253 if (p
->type
== diRun
)
255 if (!found_black
) found_black
= !(p
->member
.run
.nFlags
& (MERF_WHITESPACE
| MERF_ENDPARA
));
256 if (found_black
) num_runs
++;
260 TRACE("%d runs\n", num_runs
);
261 if (!num_runs
) return;
263 if (num_runs
> sizeof(buf
) / (sizeof(buf
[0]) * 5))
264 vis_to_log
= heap_alloc( num_runs
* sizeof(int) * 5 );
266 log_to_vis
= vis_to_log
+ num_runs
;
267 widths
= vis_to_log
+ 2 * num_runs
;
268 pos
= vis_to_log
+ 3 * num_runs
;
269 levels
= (BYTE
*)(vis_to_log
+ 4 * num_runs
);
271 for (i
= 0, p
= start
; i
< num_runs
; p
= p
->next
)
273 if (p
->type
== diRun
)
275 levels
[i
] = p
->member
.run
.script_analysis
.s
.uBidiLevel
;
276 widths
[i
] = p
->member
.run
.nWidth
;
277 TRACE( "%d: level %d width %d\n", i
, levels
[i
], widths
[i
] );
282 ScriptLayout( num_runs
, levels
, vis_to_log
, log_to_vis
);
284 pos
[0] = start
->member
.run
.para
->pt
.x
;
285 for (i
= 1; i
< num_runs
; i
++)
286 pos
[i
] = pos
[i
- 1] + widths
[ vis_to_log
[ i
- 1 ] ];
288 for (i
= 0, p
= start
; i
< num_runs
; p
= p
->next
)
290 if (p
->type
== diRun
)
292 p
->member
.run
.pt
.x
= pos
[ log_to_vis
[ i
] ];
293 TRACE( "%d: x = %d\n", i
, p
->member
.run
.pt
.x
);
298 if (vis_to_log
!= buf
) heap_free( vis_to_log
);
301 static void ME_InsertRowStart(ME_WrapContext
*wc
, const ME_DisplayItem
*pEnd
)
303 ME_DisplayItem
*p
, *row
;
304 ME_Paragraph
*para
= &wc
->pPara
->member
.para
;
305 BOOL bSkippingSpaces
= TRUE
;
306 int ascent
= 0, descent
= 0, width
=0, shift
= 0, align
= 0;
308 /* Include height of para numbering label */
309 if (wc
->nRow
== 0 && para
->fmt
.wNumbering
)
311 ascent
= para
->para_num
.style
->tm
.tmAscent
;
312 descent
= para
->para_num
.style
->tm
.tmDescent
;
315 for (p
= pEnd
->prev
; p
!=wc
->pRowStart
->prev
; p
= p
->prev
)
317 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
318 if (p
->type
==diRun
&& ((p
==wc
->pRowStart
) || !(p
->member
.run
.nFlags
& MERF_ENDPARA
))) { /* FIXME add more run types */
319 if (p
->member
.run
.nAscent
>ascent
)
320 ascent
= p
->member
.run
.nAscent
;
321 if (p
->member
.run
.nDescent
>descent
)
322 descent
= p
->member
.run
.nDescent
;
325 /* Exclude space characters from run width.
326 * Other whitespace or delimiters are not treated this way. */
327 int len
= p
->member
.run
.len
;
328 WCHAR
*text
= get_text( &p
->member
.run
, len
- 1 );
331 if (~p
->member
.run
.nFlags
& MERF_GRAPHICS
)
332 while (len
&& *(text
--) == ' ')
336 if (len
== p
->member
.run
.len
)
337 width
+= p
->member
.run
.nWidth
;
339 width
+= ME_PointFromCharContext( wc
->context
, &p
->member
.run
, len
, FALSE
);
341 bSkippingSpaces
= !len
;
342 } else if (!(p
->member
.run
.nFlags
& MERF_ENDPARA
))
343 width
+= p
->member
.run
.nWidth
;
347 para
->nWidth
= max(para
->nWidth
, width
);
348 row
= ME_MakeRow(ascent
+descent
, ascent
, width
);
349 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
350 (para
->fmt
.dwMask
& PFM_TABLE
) && (para
->fmt
.wEffects
& PFE_TABLE
))
352 /* The text was shifted down in ME_BeginRow so move the wrap context
353 * back to where it should be. */
355 /* The height of the row is increased by the borders. */
356 row
->member
.row
.nHeight
+= 2;
358 row
->member
.row
.pt
= wc
->pt
;
359 row
->member
.row
.nLMargin
= (!wc
->nRow
? wc
->nFirstMargin
: wc
->nLeftMargin
);
360 row
->member
.row
.nRMargin
= wc
->nRightMargin
;
361 assert(para
->fmt
.dwMask
& PFM_ALIGNMENT
);
362 align
= para
->fmt
.wAlignment
;
363 if (align
== PFA_CENTER
)
364 shift
= max((wc
->nAvailWidth
-width
)/2, 0);
365 if (align
== PFA_RIGHT
)
366 shift
= max(wc
->nAvailWidth
-width
, 0);
368 if (para
->nFlags
& MEPF_COMPLEX
) layout_row( wc
->pRowStart
, pEnd
);
370 row
->member
.row
.pt
.x
= row
->member
.row
.nLMargin
+ shift
;
371 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
373 if (p
->type
==diRun
) { /* FIXME add more run types */
374 p
->member
.run
.pt
.x
+= row
->member
.row
.nLMargin
+shift
;
378 if (wc
->nRow
== 0 && para
->fmt
.wNumbering
)
380 para
->para_num
.pt
.x
= wc
->nParaNumOffset
+ shift
;
381 para
->para_num
.pt
.y
= wc
->pt
.y
+ row
->member
.row
.nBaseline
;
384 ME_InsertBefore(wc
->pRowStart
, row
);
386 wc
->pt
.y
+= row
->member
.row
.nHeight
;
390 static void ME_WrapEndParagraph(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
392 ME_DisplayItem
*para
= wc
->pPara
;
393 PARAFORMAT2
*pFmt
= ¶
->member
.para
.fmt
;
395 ME_InsertRowStart(wc
, p
);
396 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
397 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
399 /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
400 * text down by one pixel for the border, so fix up the wrap context. */
407 if (p->type == diParagraph || p->type == diTextEnd)
409 if (p->type == diRun)
411 ME_Run *run = &p->member.run;
412 TRACE("%s - (%d, %d)\n", debugstr_run(run), run->pt.x, run->pt.y);
419 static void ME_WrapSizeRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
421 /* FIXME compose style (out of character and paragraph styles) here */
423 ME_UpdateRunFlags(wc
->context
->editor
, &p
->member
.run
);
425 calc_run_extent(wc
->context
, &wc
->pPara
->member
.para
,
426 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, &p
->member
.run
);
430 static int find_non_whitespace(const WCHAR
*s
, int len
, int start
)
433 for (i
= start
; i
< len
&& ME_IsWSpace( s
[i
] ); i
++)
439 /* note: these two really return the first matching offset (starting from EOS)+1
440 * in other words, an offset of the first trailing white/black */
442 /* note: returns offset of the first trailing whitespace */
443 static int reverse_find_non_whitespace(const WCHAR
*s
, int start
)
446 for (i
= start
; i
> 0 && ME_IsWSpace( s
[i
- 1] ); i
--)
452 /* note: returns offset of the first trailing nonwhitespace */
453 static int reverse_find_whitespace(const WCHAR
*s
, int start
)
456 for (i
= start
; i
> 0 && !ME_IsWSpace( s
[i
- 1] ); i
--)
462 static ME_DisplayItem
*ME_MaximizeSplit(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int i
)
464 ME_DisplayItem
*pp
, *piter
= p
;
468 j
= reverse_find_non_whitespace( get_text( &p
->member
.run
, 0 ), i
);
470 pp
= split_run_extents(wc
, piter
, j
);
471 wc
->pt
.x
+= piter
->member
.run
.nWidth
;
477 /* omit all spaces before split point */
478 while(piter
!= wc
->pRowStart
)
480 piter
= ME_FindItemBack(piter
, diRun
);
481 if (piter
->member
.run
.nFlags
& MERF_WHITESPACE
)
486 if (piter
->member
.run
.nFlags
& MERF_ENDWHITE
)
488 i
= reverse_find_non_whitespace( get_text( &piter
->member
.run
, 0 ),
489 piter
->member
.run
.len
);
490 pp
= split_run_extents(wc
, piter
, i
);
491 wc
->pt
= pp
->member
.run
.pt
;
494 /* this run is the end of spaces, so the run edge is a good point to split */
495 wc
->pt
= pp
->member
.run
.pt
;
496 wc
->bOverflown
= TRUE
;
497 TRACE("Split point is: %s|%s\n", debugstr_run( &piter
->member
.run
), debugstr_run( &pp
->member
.run
));
500 wc
->pt
= piter
->member
.run
.pt
;
505 static ME_DisplayItem
*ME_SplitByBacktracking(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int loc
)
507 ME_DisplayItem
*piter
= p
, *pp
;
509 ME_Run
*run
= &p
->member
.run
;
511 idesp
= i
= find_split_point( wc
->context
, loc
, run
);
516 /* don't split words */
517 i
= reverse_find_whitespace( get_text( run
, 0 ), i
);
518 pp
= ME_MaximizeSplit(wc
, p
, i
);
522 TRACE("Must backtrack to split at: %s\n", debugstr_run( &p
->member
.run
));
523 if (wc
->pLastSplittableRun
)
525 if (wc
->pLastSplittableRun
->member
.run
.nFlags
& (MERF_GRAPHICS
|MERF_TAB
))
527 wc
->pt
= wc
->pLastSplittableRun
->member
.run
.pt
;
528 return wc
->pLastSplittableRun
;
530 else if (wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
)
532 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
533 they serve no other purpose */
534 ME_UpdateRunFlags(wc
->context
->editor
, run
);
535 assert((wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
));
537 piter
= wc
->pLastSplittableRun
;
538 run
= &piter
->member
.run
;
540 /* don't split words */
541 i
= reverse_find_whitespace( get_text( run
, 0 ), len
);
543 i
= reverse_find_non_whitespace( get_text( run
, 0 ), len
);
545 ME_DisplayItem
*piter2
= split_run_extents(wc
, piter
, i
);
546 wc
->pt
= piter2
->member
.run
.pt
;
549 /* splittable = must have whitespaces */
550 assert(0 == "Splittable, but no whitespaces");
554 /* restart from the first run beginning with spaces */
555 wc
->pt
= wc
->pLastSplittableRun
->member
.run
.pt
;
556 return wc
->pLastSplittableRun
;
559 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_run( &p
->member
.run
));
560 /* OK, no better idea, so assume we MAY split words if we can split at all*/
562 return split_run_extents(wc
, piter
, idesp
);
564 if (wc
->pRowStart
&& piter
!= wc
->pRowStart
)
566 /* don't need to break current run, because it's possible to split
568 wc
->bOverflown
= TRUE
;
573 /* split point inside first character - no choice but split after that char */
575 /* the run is more than 1 char, so we may split */
576 return split_run_extents(wc
, piter
, 1);
578 /* the run is one char, can't split it */
583 static ME_DisplayItem
*ME_WrapHandleRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
589 assert(p
->type
== diRun
);
592 run
= &p
->member
.run
;
593 run
->pt
.x
= wc
->pt
.x
;
594 run
->pt
.y
= wc
->pt
.y
;
595 ME_WrapSizeRun(wc
, p
);
598 if (wc
->bOverflown
) /* just skipping final whitespaces */
600 /* End paragraph run can't overflow to the next line by itself. */
601 if (run
->nFlags
& MERF_ENDPARA
)
604 if (run
->nFlags
& MERF_WHITESPACE
) {
605 wc
->pt
.x
+= run
->nWidth
;
606 /* skip runs consisting of only whitespaces */
610 if (run
->nFlags
& MERF_STARTWHITE
) {
611 /* try to split the run at the first non-white char */
613 black
= find_non_whitespace( get_text( run
, 0 ), run
->len
, 0 );
615 wc
->bOverflown
= FALSE
;
616 pp
= split_run_extents(wc
, p
, black
);
617 calc_run_extent(wc
->context
, &wc
->pPara
->member
.para
,
618 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
,
620 ME_InsertRowStart(wc
, pp
);
624 /* black run: the row goes from pRowStart to the previous run */
625 ME_InsertRowStart(wc
, p
);
628 /* simply end the current row and move on to next one */
629 if (run
->nFlags
& MERF_ENDROW
)
632 ME_InsertRowStart(wc
, p
);
636 /* will current run fit? */
638 wc
->pt
.x
+ run
->nWidth
- wc
->context
->pt
.x
> wc
->nAvailWidth
)
640 int loc
= wc
->context
->pt
.x
+ wc
->nAvailWidth
- wc
->pt
.x
;
641 /* total white run or end para */
642 if (run
->nFlags
& (MERF_WHITESPACE
| MERF_ENDPARA
)) {
643 /* let the overflow logic handle it */
644 wc
->bOverflown
= TRUE
;
647 /* TAB: we can split before */
648 if (run
->nFlags
& MERF_TAB
) {
649 wc
->bOverflown
= TRUE
;
650 if (wc
->pRowStart
== p
)
651 /* Don't split before the start of the run, or we will get an
657 /* graphics: we can split before, if run's width is smaller than row's width */
658 if ((run
->nFlags
& MERF_GRAPHICS
) && run
->nWidth
<= wc
->nAvailWidth
) {
659 wc
->bOverflown
= TRUE
;
662 /* can we separate out the last spaces ? (to use overflow logic later) */
663 if (run
->nFlags
& MERF_ENDWHITE
)
665 /* we aren't sure if it's *really* necessary, it's a good start however */
666 int black
= reverse_find_non_whitespace( get_text( run
, 0 ), len
);
667 split_run_extents(wc
, p
, black
);
668 /* handle both parts again */
671 /* determine the split point by backtracking */
672 pp
= ME_SplitByBacktracking(wc
, p
, loc
);
673 if (pp
== wc
->pRowStart
)
675 if (run
->nFlags
& MERF_STARTWHITE
)
677 /* We had only spaces so far, so we must be on the first line of the
678 * paragraph (or the first line after MERF_ENDROW forced the line
679 * break within the paragraph), since no other lines of the paragraph
680 * start with spaces. */
682 /* The lines will only contain spaces, and the rest of the run will
683 * overflow onto the next line. */
684 wc
->bOverflown
= TRUE
;
687 /* Couldn't split the first run, possible because we have a large font
688 * with a single character that caused an overflow.
690 wc
->pt
.x
+= run
->nWidth
;
693 if (p
!= pp
) /* found a suitable split point */
695 wc
->bOverflown
= TRUE
;
698 /* we detected that it's best to split on start of this run */
702 /* not found anything - writing over margins is the only option left */
704 if ((run
->nFlags
& (MERF_SPLITTABLE
| MERF_STARTWHITE
))
705 || ((run
->nFlags
& (MERF_GRAPHICS
|MERF_TAB
)) && (p
!= wc
->pRowStart
)))
707 wc
->pLastSplittableRun
= p
;
709 wc
->pt
.x
+= run
->nWidth
;
713 static int ME_GetParaLineSpace(ME_Context
* c
, ME_Paragraph
* para
)
716 if (!(para
->fmt
.dwMask
& PFM_LINESPACING
)) return 0;
718 /* FIXME: how to compute simply the line space in ls ??? */
719 /* FIXME: does line spacing include the line itself ??? */
720 switch (para
->fmt
.bLineSpacingRule
)
722 case 0: sp
= ls
; break;
723 case 1: sp
= (3 * ls
) / 2; break;
724 case 2: sp
= 2 * ls
; break;
725 case 3: sp
= ME_twips2pointsY(c
, para
->fmt
.dyLineSpacing
); if (sp
< ls
) sp
= ls
; break;
726 case 4: sp
= ME_twips2pointsY(c
, para
->fmt
.dyLineSpacing
); break;
727 case 5: sp
= para
->fmt
.dyLineSpacing
/ 20; break;
728 default: FIXME("Unsupported spacing rule value %d\n", para
->fmt
.bLineSpacingRule
);
730 if (c
->editor
->nZoomNumerator
== 0)
733 return sp
* c
->editor
->nZoomNumerator
/ c
->editor
->nZoomDenominator
;
736 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
) {
739 tp
->member
.para
.nWidth
= 0;
740 /* remove row start items as they will be reinserted by the
741 * paragraph wrapper anyway */
742 tp
->member
.para
.nRows
= 0;
743 for (p
= tp
->next
; p
!= tp
->member
.para
.next_para
; p
= p
->next
) {
744 if (p
->type
== diStartRow
) {
745 ME_DisplayItem
*pRow
= p
;
748 ME_DestroyDisplayItem(pRow
);
751 /* join runs that can be joined */
752 for (p
= tp
->next
; p
!= tp
->member
.para
.next_para
; p
= p
->next
) {
753 assert(p
->type
!= diStartRow
); /* should have been deleted above */
754 if (p
->type
== diRun
) {
755 while (p
->next
->type
== diRun
&& /* FIXME */
756 ME_CanJoinRuns(&p
->member
.run
, &p
->next
->member
.run
)) {
757 ME_JoinRuns(c
->editor
, p
);
763 static HRESULT
itemize_para( ME_Context
*c
, ME_DisplayItem
*p
)
765 ME_Paragraph
*para
= &p
->member
.para
;
768 SCRIPT_ITEM buf
[16], *items
= buf
;
769 int items_passed
= sizeof( buf
) / sizeof( buf
[0] ), num_items
, cur_item
;
770 SCRIPT_CONTROL control
= { LANG_USER_DEFAULT
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
772 SCRIPT_STATE state
= { 0, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, 0, 0 };
775 assert( p
->type
== diParagraph
);
777 if (para
->fmt
.dwMask
& PFM_RTLPARA
&& para
->fmt
.wEffects
& PFE_RTLPARA
)
778 state
.uBidiLevel
= 1;
780 TRACE( "Base embedding level %d\n", state
.uBidiLevel
);
784 hr
= ScriptItemize( para
->text
->szData
, para
->text
->nLen
, items_passed
, &control
,
785 &state
, items
, &num_items
);
786 if (hr
!= E_OUTOFMEMORY
) break; /* may not be enough items if hr == E_OUTOFMEMORY */
787 if (items_passed
> para
->text
->nLen
+ 1) break; /* something else has gone wrong */
790 items
= heap_alloc( items_passed
* sizeof( *items
) );
792 items
= heap_realloc( items
, items_passed
* sizeof( *items
) );
795 if (FAILED( hr
)) goto end
;
797 if (TRACE_ON( richedit
))
799 TRACE( "got items:\n" );
800 for (cur_item
= 0; cur_item
< num_items
; cur_item
++)
802 TRACE( "\t%d - %d RTL %d bidi level %d\n", items
[cur_item
].iCharPos
, items
[cur_item
+1].iCharPos
- 1,
803 items
[cur_item
].a
.fRTL
, items
[cur_item
].a
.s
.uBidiLevel
);
806 TRACE( "before splitting runs into ranges\n" );
807 for (di
= p
->next
; di
!= p
->member
.para
.next_para
; di
= di
->next
)
809 if (di
->type
!= diRun
) continue;
810 TRACE( "\t%d: %s\n", di
->member
.run
.nCharOfs
, debugstr_run( &di
->member
.run
) );
814 /* split runs into ranges at item boundaries */
815 for (di
= p
->next
, cur_item
= 0; di
!= p
->member
.para
.next_para
; di
= di
->next
)
817 if (di
->type
!= diRun
) continue;
818 run
= &di
->member
.run
;
820 if (run
->nCharOfs
== items
[cur_item
+1].iCharPos
) cur_item
++;
822 items
[cur_item
].a
.fLogicalOrder
= TRUE
;
823 run
->script_analysis
= items
[cur_item
].a
;
825 if (run
->nFlags
& MERF_ENDPARA
) break; /* don't split eop runs */
827 if (run
->nCharOfs
+ run
->len
> items
[cur_item
+1].iCharPos
)
829 ME_Cursor cursor
= {p
, di
, items
[cur_item
+1].iCharPos
- run
->nCharOfs
};
830 ME_SplitRunSimple( c
->editor
, &cursor
);
834 if (TRACE_ON( richedit
))
836 TRACE( "after splitting into ranges\n" );
837 for (di
= p
->next
; di
!= p
->member
.para
.next_para
; di
= di
->next
)
839 if (di
->type
!= diRun
) continue;
840 TRACE( "\t%d: %s\n", di
->member
.run
.nCharOfs
, debugstr_run( &di
->member
.run
) );
844 para
->nFlags
|= MEPF_COMPLEX
;
847 if (items
!= buf
) heap_free( items
);
852 static HRESULT
shape_para( ME_Context
*c
, ME_DisplayItem
*p
)
858 for (di
= p
->next
; di
!= p
->member
.para
.next_para
; di
= di
->next
)
860 if (di
->type
!= diRun
) continue;
861 run
= &di
->member
.run
;
863 hr
= shape_run( c
, run
);
866 run
->para
->nFlags
&= ~MEPF_COMPLEX
;
873 static void ME_WrapTextParagraph(ME_Context
*c
, ME_DisplayItem
*tp
) {
880 assert(tp
->type
== diParagraph
);
881 if (!(tp
->member
.para
.nFlags
& MEPF_REWRAP
)) {
884 ME_PrepareParagraphForWrapping(c
, tp
);
886 /* Calculate paragraph numbering label */
887 para_num_init( c
, &tp
->member
.para
);
889 /* For now treating all non-password text as complex for better testing */
890 if (!c
->editor
->cPasswordMask
/* &&
891 ScriptIsComplex( tp->member.para.text->szData, tp->member.para.text->nLen, SIC_COMPLEX ) == S_OK */)
893 if (SUCCEEDED( itemize_para( c
, tp
) ))
897 pFmt
= &tp
->member
.para
.fmt
;
901 /* wc.para_style = tp->member.para.style; */
903 wc
.nParaNumOffset
= 0;
904 if (tp
->member
.para
.nFlags
& MEPF_ROWEND
) {
905 wc
.nFirstMargin
= wc
.nLeftMargin
= wc
.nRightMargin
= 0;
907 int dxStartIndent
= pFmt
->dxStartIndent
;
908 if (tp
->member
.para
.pCell
) {
909 dxStartIndent
+= ME_GetTableRowEnd(tp
)->member
.para
.fmt
.dxOffset
;
911 wc
.nLeftMargin
= ME_twips2pointsX(c
, dxStartIndent
+ pFmt
->dxOffset
);
912 wc
.nFirstMargin
= ME_twips2pointsX(c
, dxStartIndent
);
913 if (pFmt
->wNumbering
)
915 wc
.nParaNumOffset
= wc
.nFirstMargin
;
916 dxStartIndent
= max( ME_twips2pointsX(c
, pFmt
->wNumberingTab
),
917 tp
->member
.para
.para_num
.width
);
918 wc
.nFirstMargin
+= dxStartIndent
;
920 wc
.nRightMargin
= ME_twips2pointsX(c
, pFmt
->dxRightIndent
);
922 if (wc
.nFirstMargin
< 0)
924 if (wc
.nLeftMargin
< 0)
927 if (c
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
928 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
930 wc
.nFirstMargin
+= ME_twips2pointsX(c
, pFmt
->dxOffset
* 2);
934 if (pFmt
->dwMask
& PFM_SPACEBEFORE
)
935 wc
.pt
.y
+= ME_twips2pointsY(c
, pFmt
->dySpaceBefore
);
936 if (!(pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
) &&
937 pFmt
->dwMask
& PFM_BORDER
)
939 border
= ME_GetParaBorderWidth(c
, tp
->member
.para
.fmt
.wBorders
);
940 if (pFmt
->wBorders
& 1) {
941 wc
.nFirstMargin
+= border
;
942 wc
.nLeftMargin
+= border
;
944 if (pFmt
->wBorders
& 2)
945 wc
.nRightMargin
-= border
;
946 if (pFmt
->wBorders
& 4)
950 linespace
= ME_GetParaLineSpace(c
, &tp
->member
.para
);
953 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; ) {
954 assert(p
->type
!= diStartRow
);
955 if (p
->type
== diRun
) {
956 p
= ME_WrapHandleRun(&wc
, p
);
959 if (wc
.nRow
&& p
== wc
.pRowStart
)
960 wc
.pt
.y
+= linespace
;
962 ME_WrapEndParagraph(&wc
, p
);
963 if (!(pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
) &&
964 (pFmt
->dwMask
& PFM_BORDER
) && (pFmt
->wBorders
& 8))
966 if (tp
->member
.para
.fmt
.dwMask
& PFM_SPACEAFTER
)
967 wc
.pt
.y
+= ME_twips2pointsY(c
, pFmt
->dySpaceAfter
);
969 tp
->member
.para
.nFlags
&= ~MEPF_REWRAP
;
970 tp
->member
.para
.nHeight
= wc
.pt
.y
;
971 tp
->member
.para
.nRows
= wc
.nRow
;
974 static void ME_MarkRepaintEnd(ME_DisplayItem
*para
,
975 ME_DisplayItem
**repaint_start
,
976 ME_DisplayItem
**repaint_end
)
979 *repaint_start
= para
;
983 BOOL
ME_WrapMarkedParagraphs(ME_TextEditor
*editor
)
985 ME_DisplayItem
*item
;
988 ME_DisplayItem
*repaint_start
= NULL
, *repaint_end
= NULL
;
990 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
992 item
= editor
->pBuffer
->pFirst
->next
;
993 while(item
!= editor
->pBuffer
->pLast
) {
994 BOOL bRedraw
= FALSE
;
996 assert(item
->type
== diParagraph
);
997 if ((item
->member
.para
.nFlags
& MEPF_REWRAP
)
998 || (item
->member
.para
.pt
.y
!= c
.pt
.y
))
1000 item
->member
.para
.pt
= c
.pt
;
1002 ME_WrapTextParagraph(&c
, item
);
1005 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
1007 if (item
->member
.para
.nFlags
& MEPF_ROWSTART
)
1009 ME_DisplayItem
*cell
= ME_FindItemFwd(item
, diCell
);
1010 ME_DisplayItem
*endRowPara
;
1011 int borderWidth
= 0;
1012 cell
->member
.cell
.pt
= c
.pt
;
1013 /* Offset the text by the largest top border width. */
1014 while (cell
->member
.cell
.next_cell
) {
1015 borderWidth
= max(borderWidth
, cell
->member
.cell
.border
.top
.width
);
1016 cell
= cell
->member
.cell
.next_cell
;
1018 endRowPara
= ME_FindItemFwd(cell
, diParagraph
);
1019 assert(endRowPara
->member
.para
.nFlags
& MEPF_ROWEND
);
1020 if (borderWidth
> 0)
1022 borderWidth
= max(ME_twips2pointsY(&c
, borderWidth
), 1);
1024 cell
->member
.cell
.yTextOffset
= borderWidth
;
1025 cell
= cell
->member
.cell
.prev_cell
;
1027 c
.pt
.y
+= borderWidth
;
1029 if (endRowPara
->member
.para
.fmt
.dxStartIndent
> 0)
1031 int dxStartIndent
= endRowPara
->member
.para
.fmt
.dxStartIndent
;
1032 cell
= ME_FindItemFwd(item
, diCell
);
1033 cell
->member
.cell
.pt
.x
+= ME_twips2pointsX(&c
, dxStartIndent
);
1034 c
.pt
.x
= cell
->member
.cell
.pt
.x
;
1037 else if (item
->member
.para
.nFlags
& MEPF_ROWEND
)
1039 /* Set all the cells to the height of the largest cell */
1040 ME_DisplayItem
*startRowPara
;
1041 int prevHeight
, nHeight
, bottomBorder
= 0;
1042 ME_DisplayItem
*cell
= ME_FindItemBack(item
, diCell
);
1043 item
->member
.para
.nWidth
= cell
->member
.cell
.pt
.x
+ cell
->member
.cell
.nWidth
;
1044 if (!(item
->member
.para
.next_para
->member
.para
.nFlags
& MEPF_ROWSTART
))
1046 /* Last row, the bottom border is added to the height. */
1047 cell
= cell
->member
.cell
.prev_cell
;
1050 bottomBorder
= max(bottomBorder
, cell
->member
.cell
.border
.bottom
.width
);
1051 cell
= cell
->member
.cell
.prev_cell
;
1053 bottomBorder
= ME_twips2pointsY(&c
, bottomBorder
);
1054 cell
= ME_FindItemBack(item
, diCell
);
1056 prevHeight
= cell
->member
.cell
.nHeight
;
1057 nHeight
= cell
->member
.cell
.prev_cell
->member
.cell
.nHeight
+ bottomBorder
;
1058 cell
->member
.cell
.nHeight
= nHeight
;
1059 item
->member
.para
.nHeight
= nHeight
;
1060 cell
= cell
->member
.cell
.prev_cell
;
1061 cell
->member
.cell
.nHeight
= nHeight
;
1062 while (cell
->member
.cell
.prev_cell
)
1064 cell
= cell
->member
.cell
.prev_cell
;
1065 cell
->member
.cell
.nHeight
= nHeight
;
1067 /* Also set the height of the start row paragraph */
1068 startRowPara
= ME_FindItemBack(cell
, diParagraph
);
1069 startRowPara
->member
.para
.nHeight
= nHeight
;
1070 c
.pt
.x
= startRowPara
->member
.para
.pt
.x
;
1071 c
.pt
.y
= cell
->member
.cell
.pt
.y
+ nHeight
;
1072 if (prevHeight
< nHeight
)
1074 /* The height of the cells has grown, so invalidate the bottom of
1076 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
1077 cell
= ME_FindItemBack(item
, diCell
);
1079 ME_MarkRepaintEnd(ME_FindItemBack(cell
, diParagraph
), &repaint_start
, &repaint_end
);
1080 cell
= cell
->member
.cell
.prev_cell
;
1084 else if (item
->member
.para
.pCell
&&
1085 item
->member
.para
.pCell
!= item
->member
.para
.next_para
->member
.para
.pCell
)
1087 /* The next paragraph is in the next cell in the table row. */
1088 ME_Cell
*cell
= &item
->member
.para
.pCell
->member
.cell
;
1089 cell
->nHeight
= c
.pt
.y
+ item
->member
.para
.nHeight
- cell
->pt
.y
;
1091 /* Propagate the largest height to the end so that it can be easily
1092 * sent back to all the cells at the end of the row. */
1093 if (cell
->prev_cell
)
1094 cell
->nHeight
= max(cell
->nHeight
, cell
->prev_cell
->member
.cell
.nHeight
);
1096 c
.pt
.x
= cell
->pt
.x
+ cell
->nWidth
;
1097 c
.pt
.y
= cell
->pt
.y
;
1098 cell
->next_cell
->member
.cell
.pt
= c
.pt
;
1099 if (!(item
->member
.para
.next_para
->member
.para
.nFlags
& MEPF_ROWEND
))
1100 c
.pt
.y
+= cell
->yTextOffset
;
1104 if (item
->member
.para
.pCell
) {
1105 /* Next paragraph in the same cell. */
1106 c
.pt
.x
= item
->member
.para
.pCell
->member
.cell
.pt
.x
;
1108 /* Normal paragraph */
1111 c
.pt
.y
+= item
->member
.para
.nHeight
;
1114 totalWidth
= max(totalWidth
, item
->member
.para
.nWidth
);
1115 item
= item
->member
.para
.next_para
;
1117 editor
->sizeWindow
.cx
= c
.rcView
.right
-c
.rcView
.left
;
1118 editor
->sizeWindow
.cy
= c
.rcView
.bottom
-c
.rcView
.top
;
1120 editor
->nTotalLength
= c
.pt
.y
;
1121 editor
->nTotalWidth
= totalWidth
;
1122 editor
->pBuffer
->pLast
->member
.para
.pt
.x
= 0;
1123 editor
->pBuffer
->pLast
->member
.para
.pt
.y
= c
.pt
.y
;
1125 ME_DestroyContext(&c
);
1127 if (repaint_start
|| editor
->nTotalLength
< editor
->nLastTotalLength
)
1128 ME_InvalidateParagraphRange(editor
, repaint_start
, repaint_end
);
1129 return !!repaint_start
;
1132 void ME_InvalidateParagraphRange(ME_TextEditor
*editor
,
1133 ME_DisplayItem
*start_para
,
1134 ME_DisplayItem
*last_para
)
1140 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
1142 ofs
= editor
->vert_si
.nPos
;
1145 start_para
= ME_GetOuterParagraph(start_para
);
1146 last_para
= ME_GetOuterParagraph(last_para
);
1147 rc
.top
= c
.rcView
.top
+ start_para
->member
.para
.pt
.y
- ofs
;
1149 rc
.top
= c
.rcView
.top
+ editor
->nTotalLength
- ofs
;
1151 if (editor
->nTotalLength
< editor
->nLastTotalLength
)
1152 rc
.bottom
= c
.rcView
.top
+ editor
->nLastTotalLength
- ofs
;
1154 rc
.bottom
= c
.rcView
.top
+ last_para
->member
.para
.pt
.y
+ last_para
->member
.para
.nHeight
- ofs
;
1155 ITextHost_TxInvalidateRect(editor
->texthost
, &rc
, TRUE
);
1157 ME_DestroyContext(&c
);
1162 ME_SendRequestResize(ME_TextEditor
*editor
, BOOL force
)
1164 if (editor
->nEventMask
& ENM_REQUESTRESIZE
)
1168 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
1170 if (force
|| rc
.bottom
!= editor
->nTotalLength
)
1174 info
.nmhdr
.hwndFrom
= NULL
;
1175 info
.nmhdr
.idFrom
= 0;
1176 info
.nmhdr
.code
= EN_REQUESTRESIZE
;
1178 info
.rc
.right
= editor
->nTotalWidth
;
1179 info
.rc
.bottom
= editor
->nTotalLength
;
1181 editor
->nEventMask
&= ~ENM_REQUESTRESIZE
;
1182 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
1183 editor
->nEventMask
|= ENM_REQUESTRESIZE
;