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
37 static BOOL
get_run_glyph_buffers( ME_Run
*run
)
39 heap_free( run
->glyphs
);
40 run
->glyphs
= heap_alloc( run
->max_glyphs
* (sizeof(WORD
) + sizeof(SCRIPT_VISATTR
) + sizeof(int) + sizeof(GOFFSET
)) );
41 if (!run
->glyphs
) return FALSE
;
43 run
->vis_attrs
= (SCRIPT_VISATTR
*)((char*)run
->glyphs
+ run
->max_glyphs
* sizeof(WORD
));
44 run
->advances
= (int*)((char*)run
->glyphs
+ run
->max_glyphs
* (sizeof(WORD
) + sizeof(SCRIPT_VISATTR
)));
45 run
->offsets
= (GOFFSET
*)((char*)run
->glyphs
+ run
->max_glyphs
* (sizeof(WORD
) + sizeof(SCRIPT_VISATTR
) + sizeof(int)));
50 static HRESULT
shape_run( ME_Context
*c
, ME_Run
*run
)
58 run
->max_glyphs
= 1.5 * run
->len
+ 16; /* This is suggested in the uniscribe documentation */
59 run
->max_glyphs
= (run
->max_glyphs
+ 7) & ~7; /* Keep alignment simple */
60 get_run_glyph_buffers( run
);
63 if (run
->max_clusters
< run
->len
)
65 heap_free( run
->clusters
);
66 run
->max_clusters
= run
->len
* 2;
67 run
->clusters
= heap_alloc( run
->max_clusters
* sizeof(WORD
) );
70 old_font
= ME_SelectStyleFont( c
, run
->style
);
73 hr
= ScriptShape( c
->hDC
, &run
->style
->script_cache
, get_text( run
, 0 ), run
->len
, run
->max_glyphs
,
74 &run
->script_analysis
, run
->glyphs
, run
->clusters
, run
->vis_attrs
, &run
->num_glyphs
);
75 if (hr
!= E_OUTOFMEMORY
) break;
76 if (run
->max_glyphs
> 10 * run
->len
) break; /* something has clearly gone wrong */
78 get_run_glyph_buffers( run
);
82 hr
= ScriptPlace( c
->hDC
, &run
->style
->script_cache
, run
->glyphs
, run
->num_glyphs
, run
->vis_attrs
,
83 &run
->script_analysis
, run
->advances
, run
->offsets
, NULL
);
87 for (i
= 0, run
->nWidth
= 0; i
< run
->num_glyphs
; i
++)
88 run
->nWidth
+= run
->advances
[i
];
91 ME_UnselectStyleFont( c
, run
->style
, old_font
);
96 /******************************************************************************
99 * Updates the size of the run (fills width, ascent and descent). The height
100 * is calculated based on whole row's ascent and descent anyway, so no need
103 static void calc_run_extent(ME_Context
*c
, const ME_Paragraph
*para
, int startx
, ME_Run
*run
)
105 if (run
->nFlags
& MERF_HIDDEN
) run
->nWidth
= 0;
108 SIZE size
= ME_GetRunSizeCommon( c
, para
, run
, run
->len
, startx
, &run
->nAscent
, &run
->nDescent
);
109 run
->nWidth
= size
.cx
;
113 /******************************************************************************
116 * Splits a run into two in a given place. It also updates the screen position
117 * and size (extent) of the newly generated runs.
119 static ME_DisplayItem
*split_run_extents(ME_WrapContext
*wc
, ME_DisplayItem
*item
, int nVChar
)
121 ME_TextEditor
*editor
= wc
->context
->editor
;
123 ME_Paragraph
*para
= &wc
->pPara
->member
.para
;
124 ME_Cursor cursor
= {wc
->pPara
, item
, nVChar
};
126 assert(item
->member
.run
.nCharOfs
!= -1);
127 if(TRACE_ON(richedit_check
))
128 ME_CheckCharOffsets(editor
);
130 run
= &item
->member
.run
;
132 TRACE("Before split: %s(%d, %d)\n", debugstr_run( run
),
133 run
->pt
.x
, run
->pt
.y
);
135 ME_SplitRunSimple(editor
, &cursor
);
137 run2
= &cursor
.pRun
->member
.run
;
138 run2
->script_analysis
= run
->script_analysis
;
140 shape_run( wc
->context
, run
);
141 shape_run( wc
->context
, run2
);
142 calc_run_extent(wc
->context
, para
, wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, run
);
144 run2
->pt
.x
= run
->pt
.x
+run
->nWidth
;
145 run2
->pt
.y
= run
->pt
.y
;
147 if(TRACE_ON(richedit_check
))
148 ME_CheckCharOffsets(editor
);
150 TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
151 debugstr_run( run
), run
->pt
.x
, run
->pt
.y
,
152 debugstr_run( run2
), run2
->pt
.x
, run2
->pt
.y
);
157 /******************************************************************************
160 * Returns a character position to split inside the run given a run-relative
161 * pixel horizontal position. This version rounds left (ie. if the second
162 * character is at pixel position 8, then for cx=0..7 it returns 0).
164 static int find_split_point( ME_Context
*c
, int cx
, ME_Run
*run
)
166 if (!run
->len
|| cx
<= 0) return 0;
167 return ME_CharFromPointContext( c
, cx
, run
, FALSE
, FALSE
);
170 static ME_DisplayItem
*ME_MakeRow(int height
, int baseline
, int width
)
172 ME_DisplayItem
*item
= ME_MakeDI(diStartRow
);
174 item
->member
.row
.nHeight
= height
;
175 item
->member
.row
.nBaseline
= baseline
;
176 item
->member
.row
.nWidth
= width
;
180 static void ME_BeginRow(ME_WrapContext
*wc
)
183 ME_DisplayItem
*para
= wc
->pPara
;
185 pFmt
= para
->member
.para
.pFmt
;
186 wc
->pRowStart
= NULL
;
187 wc
->bOverflown
= FALSE
;
188 wc
->pLastSplittableRun
= NULL
;
189 wc
->bWordWrap
= wc
->context
->editor
->bWordWrap
;
190 if (para
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
)) {
192 wc
->bWordWrap
= FALSE
;
193 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
195 ME_Cell
*cell
= &ME_FindItemBack(para
, diCell
)->member
.cell
;
198 } else if (para
->member
.para
.pCell
) {
199 ME_Cell
*cell
= ¶
->member
.para
.pCell
->member
.cell
;
202 width
= cell
->nRightBoundary
;
204 width
-= cell
->prev_cell
->member
.cell
.nRightBoundary
;
205 if (!cell
->prev_cell
)
207 int rowIndent
= ME_GetTableRowEnd(para
)->member
.para
.pFmt
->dxStartIndent
;
210 cell
->nWidth
= max(ME_twips2pointsX(wc
->context
, width
), 0);
212 wc
->nAvailWidth
= cell
->nWidth
213 - (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
214 wc
->bWordWrap
= TRUE
;
216 wc
->nAvailWidth
= wc
->context
->nAvailWidth
217 - (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
219 wc
->pt
.x
= wc
->context
->pt
.x
;
220 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
221 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
222 /* Shift the text down because of the border. */
226 static void layout_row( ME_DisplayItem
*start
, const ME_DisplayItem
*end
)
230 int buf
[16 * 5]; /* 5 arrays - 4 of int & 1 of BYTE, alloc space for 5 of ints */
231 int *vis_to_log
= buf
, *log_to_vis
, *widths
, *pos
;
233 BOOL found_black
= FALSE
;
235 for (p
= end
->prev
; p
!= start
->prev
; p
= p
->prev
)
237 if (p
->type
== diRun
)
239 if (!found_black
) found_black
= !(p
->member
.run
.nFlags
& (MERF_WHITESPACE
| MERF_ENDPARA
));
240 if (found_black
) num_runs
++;
244 TRACE("%d runs\n", num_runs
);
245 if (!num_runs
) return;
247 if (num_runs
> sizeof(buf
) / (sizeof(buf
[0]) * 5))
248 vis_to_log
= heap_alloc( num_runs
* sizeof(int) * 5 );
250 log_to_vis
= vis_to_log
+ num_runs
;
251 widths
= vis_to_log
+ 2 * num_runs
;
252 pos
= vis_to_log
+ 3 * num_runs
;
253 levels
= (BYTE
*)(vis_to_log
+ 4 * num_runs
);
255 for (i
= 0, p
= start
; i
< num_runs
; p
= p
->next
)
257 if (p
->type
== diRun
)
259 levels
[i
] = p
->member
.run
.script_analysis
.s
.uBidiLevel
;
260 widths
[i
] = p
->member
.run
.nWidth
;
261 TRACE( "%d: level %d width %d\n", i
, levels
[i
], widths
[i
] );
266 ScriptLayout( num_runs
, levels
, vis_to_log
, log_to_vis
);
268 pos
[0] = start
->member
.run
.para
->pt
.x
;
269 for (i
= 1; i
< num_runs
; i
++)
270 pos
[i
] = pos
[i
- 1] + widths
[ vis_to_log
[ i
- 1 ] ];
272 for (i
= 0, p
= start
; i
< num_runs
; p
= p
->next
)
274 if (p
->type
== diRun
)
276 p
->member
.run
.pt
.x
= pos
[ log_to_vis
[ i
] ];
277 TRACE( "%d: x = %d\n", i
, p
->member
.run
.pt
.x
);
282 if (vis_to_log
!= buf
) heap_free( vis_to_log
);
285 static void ME_InsertRowStart(ME_WrapContext
*wc
, const ME_DisplayItem
*pEnd
)
287 ME_DisplayItem
*p
, *row
;
288 ME_Paragraph
*para
= &wc
->pPara
->member
.para
;
289 BOOL bSkippingSpaces
= TRUE
;
290 int ascent
= 0, descent
= 0, width
=0, shift
= 0, align
= 0;
294 for (p
= pEnd
->prev
; p
!=wc
->pRowStart
->prev
; p
= p
->prev
)
296 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
297 if (p
->type
==diRun
&& ((p
==wc
->pRowStart
) || !(p
->member
.run
.nFlags
& MERF_ENDPARA
))) { /* FIXME add more run types */
298 if (p
->member
.run
.nAscent
>ascent
)
299 ascent
= p
->member
.run
.nAscent
;
300 if (p
->member
.run
.nDescent
>descent
)
301 descent
= p
->member
.run
.nDescent
;
304 /* Exclude space characters from run width.
305 * Other whitespace or delimiters are not treated this way. */
306 int len
= p
->member
.run
.len
;
307 WCHAR
*text
= get_text( &p
->member
.run
, len
- 1 );
310 if (~p
->member
.run
.nFlags
& MERF_GRAPHICS
)
311 while (len
&& *(text
--) == ' ')
315 if (len
== p
->member
.run
.len
)
316 width
+= p
->member
.run
.nWidth
;
318 width
+= ME_PointFromCharContext( wc
->context
, &p
->member
.run
, len
, FALSE
);
320 bSkippingSpaces
= !len
;
321 } else if (!(p
->member
.run
.nFlags
& MERF_ENDPARA
))
322 width
+= p
->member
.run
.nWidth
;
326 para
->nWidth
= max(para
->nWidth
, width
);
327 row
= ME_MakeRow(ascent
+descent
, ascent
, width
);
328 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
329 (para
->pFmt
->dwMask
& PFM_TABLE
) && (para
->pFmt
->wEffects
& PFE_TABLE
))
331 /* The text was shifted down in ME_BeginRow so move the wrap context
332 * back to where it should be. */
334 /* The height of the row is increased by the borders. */
335 row
->member
.row
.nHeight
+= 2;
337 row
->member
.row
.pt
= wc
->pt
;
338 row
->member
.row
.nLMargin
= (!wc
->nRow
? wc
->nFirstMargin
: wc
->nLeftMargin
);
339 row
->member
.row
.nRMargin
= wc
->nRightMargin
;
340 assert(para
->pFmt
->dwMask
& PFM_ALIGNMENT
);
341 align
= para
->pFmt
->wAlignment
;
342 if (align
== PFA_CENTER
)
343 shift
= max((wc
->nAvailWidth
-width
)/2, 0);
344 if (align
== PFA_RIGHT
)
345 shift
= max(wc
->nAvailWidth
-width
, 0);
347 if (para
->nFlags
& MEPF_COMPLEX
) layout_row( wc
->pRowStart
, pEnd
);
349 row
->member
.row
.pt
.x
= row
->member
.row
.nLMargin
+ shift
;
350 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
352 if (p
->type
==diRun
) { /* FIXME add more run types */
353 p
->member
.run
.pt
.x
+= row
->member
.row
.nLMargin
+shift
;
356 ME_InsertBefore(wc
->pRowStart
, row
);
358 wc
->pt
.y
+= row
->member
.row
.nHeight
;
362 static void ME_WrapEndParagraph(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
364 ME_DisplayItem
*para
= wc
->pPara
;
365 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
367 ME_InsertRowStart(wc
, p
);
368 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
369 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
371 /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
372 * text down by one pixel for the border, so fix up the wrap context. */
379 if (p->type == diParagraph || p->type == diTextEnd)
381 if (p->type == diRun)
383 ME_Run *run = &p->member.run;
384 TRACE("%s - (%d, %d)\n", debugstr_run(run), run->pt.x, run->pt.y);
391 static void ME_WrapSizeRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
393 /* FIXME compose style (out of character and paragraph styles) here */
395 ME_UpdateRunFlags(wc
->context
->editor
, &p
->member
.run
);
397 calc_run_extent(wc
->context
, &wc
->pPara
->member
.para
,
398 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, &p
->member
.run
);
402 static int find_non_whitespace(const WCHAR
*s
, int len
, int start
)
405 for (i
= start
; i
< len
&& ME_IsWSpace( s
[i
] ); i
++)
411 /* note: these two really return the first matching offset (starting from EOS)+1
412 * in other words, an offset of the first trailing white/black */
414 /* note: returns offset of the first trailing whitespace */
415 static int reverse_find_non_whitespace(const WCHAR
*s
, int start
)
418 for (i
= start
; i
> 0 && ME_IsWSpace( s
[i
- 1] ); i
--)
424 /* note: returns offset of the first trailing nonwhitespace */
425 static int reverse_find_whitespace(const WCHAR
*s
, int start
)
428 for (i
= start
; i
> 0 && !ME_IsWSpace( s
[i
- 1] ); i
--)
434 static ME_DisplayItem
*ME_MaximizeSplit(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int i
)
436 ME_DisplayItem
*pp
, *piter
= p
;
440 j
= reverse_find_non_whitespace( get_text( &p
->member
.run
, 0 ), i
);
442 pp
= split_run_extents(wc
, piter
, j
);
443 wc
->pt
.x
+= piter
->member
.run
.nWidth
;
449 /* omit all spaces before split point */
450 while(piter
!= wc
->pRowStart
)
452 piter
= ME_FindItemBack(piter
, diRun
);
453 if (piter
->member
.run
.nFlags
& MERF_WHITESPACE
)
458 if (piter
->member
.run
.nFlags
& MERF_ENDWHITE
)
460 i
= reverse_find_non_whitespace( get_text( &piter
->member
.run
, 0 ),
461 piter
->member
.run
.len
);
462 pp
= split_run_extents(wc
, piter
, i
);
463 wc
->pt
= pp
->member
.run
.pt
;
466 /* this run is the end of spaces, so the run edge is a good point to split */
467 wc
->pt
= pp
->member
.run
.pt
;
468 wc
->bOverflown
= TRUE
;
469 TRACE("Split point is: %s|%s\n", debugstr_run( &piter
->member
.run
), debugstr_run( &pp
->member
.run
));
472 wc
->pt
= piter
->member
.run
.pt
;
477 static ME_DisplayItem
*ME_SplitByBacktracking(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int loc
)
479 ME_DisplayItem
*piter
= p
, *pp
;
481 ME_Run
*run
= &p
->member
.run
;
483 idesp
= i
= find_split_point( wc
->context
, loc
, run
);
488 /* don't split words */
489 i
= reverse_find_whitespace( get_text( run
, 0 ), i
);
490 pp
= ME_MaximizeSplit(wc
, p
, i
);
494 TRACE("Must backtrack to split at: %s\n", debugstr_run( &p
->member
.run
));
495 if (wc
->pLastSplittableRun
)
497 if (wc
->pLastSplittableRun
->member
.run
.nFlags
& (MERF_GRAPHICS
|MERF_TAB
))
499 wc
->pt
= wc
->pLastSplittableRun
->member
.run
.pt
;
500 return wc
->pLastSplittableRun
;
502 else if (wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
)
504 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
505 they serve no other purpose */
506 ME_UpdateRunFlags(wc
->context
->editor
, run
);
507 assert((wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
));
509 piter
= wc
->pLastSplittableRun
;
510 run
= &piter
->member
.run
;
512 /* don't split words */
513 i
= reverse_find_whitespace( get_text( run
, 0 ), len
);
515 i
= reverse_find_non_whitespace( get_text( run
, 0 ), len
);
517 ME_DisplayItem
*piter2
= split_run_extents(wc
, piter
, i
);
518 wc
->pt
= piter2
->member
.run
.pt
;
521 /* splittable = must have whitespaces */
522 assert(0 == "Splittable, but no whitespaces");
526 /* restart from the first run beginning with spaces */
527 wc
->pt
= wc
->pLastSplittableRun
->member
.run
.pt
;
528 return wc
->pLastSplittableRun
;
531 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_run( &p
->member
.run
));
532 /* OK, no better idea, so assume we MAY split words if we can split at all*/
534 return split_run_extents(wc
, piter
, idesp
);
536 if (wc
->pRowStart
&& piter
!= wc
->pRowStart
)
538 /* don't need to break current run, because it's possible to split
540 wc
->bOverflown
= TRUE
;
545 /* split point inside first character - no choice but split after that char */
547 /* the run is more than 1 char, so we may split */
548 return split_run_extents(wc
, piter
, 1);
550 /* the run is one char, can't split it */
555 static ME_DisplayItem
*ME_WrapHandleRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
561 assert(p
->type
== diRun
);
564 run
= &p
->member
.run
;
565 run
->pt
.x
= wc
->pt
.x
;
566 run
->pt
.y
= wc
->pt
.y
;
567 ME_WrapSizeRun(wc
, p
);
570 if (wc
->bOverflown
) /* just skipping final whitespaces */
572 /* End paragraph run can't overflow to the next line by itself. */
573 if (run
->nFlags
& MERF_ENDPARA
)
576 if (run
->nFlags
& MERF_WHITESPACE
) {
577 wc
->pt
.x
+= run
->nWidth
;
578 /* skip runs consisting of only whitespaces */
582 if (run
->nFlags
& MERF_STARTWHITE
) {
583 /* try to split the run at the first non-white char */
585 black
= find_non_whitespace( get_text( run
, 0 ), run
->len
, 0 );
587 wc
->bOverflown
= FALSE
;
588 pp
= split_run_extents(wc
, p
, black
);
589 calc_run_extent(wc
->context
, &wc
->pPara
->member
.para
,
590 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
,
592 ME_InsertRowStart(wc
, pp
);
596 /* black run: the row goes from pRowStart to the previous run */
597 ME_InsertRowStart(wc
, p
);
600 /* simply end the current row and move on to next one */
601 if (run
->nFlags
& MERF_ENDROW
)
604 ME_InsertRowStart(wc
, p
);
608 /* will current run fit? */
610 wc
->pt
.x
+ run
->nWidth
- wc
->context
->pt
.x
> wc
->nAvailWidth
)
612 int loc
= wc
->context
->pt
.x
+ wc
->nAvailWidth
- wc
->pt
.x
;
613 /* total white run ? */
614 if (run
->nFlags
& MERF_WHITESPACE
) {
615 /* let the overflow logic handle it */
616 wc
->bOverflown
= TRUE
;
619 /* TAB: we can split before */
620 if (run
->nFlags
& MERF_TAB
) {
621 wc
->bOverflown
= TRUE
;
622 if (wc
->pRowStart
== p
)
623 /* Don't split before the start of the run, or we will get an
629 /* graphics: we can split before, if run's width is smaller than row's width */
630 if ((run
->nFlags
& MERF_GRAPHICS
) && run
->nWidth
<= wc
->nAvailWidth
) {
631 wc
->bOverflown
= TRUE
;
634 /* can we separate out the last spaces ? (to use overflow logic later) */
635 if (run
->nFlags
& MERF_ENDWHITE
)
637 /* we aren't sure if it's *really* necessary, it's a good start however */
638 int black
= reverse_find_non_whitespace( get_text( run
, 0 ), len
);
639 split_run_extents(wc
, p
, black
);
640 /* handle both parts again */
643 /* determine the split point by backtracking */
644 pp
= ME_SplitByBacktracking(wc
, p
, loc
);
645 if (pp
== wc
->pRowStart
)
647 if (run
->nFlags
& MERF_STARTWHITE
)
649 /* We had only spaces so far, so we must be on the first line of the
650 * paragraph (or the first line after MERF_ENDROW forced the line
651 * break within the paragraph), since no other lines of the paragraph
652 * start with spaces. */
654 /* The lines will only contain spaces, and the rest of the run will
655 * overflow onto the next line. */
656 wc
->bOverflown
= TRUE
;
659 /* Couldn't split the first run, possible because we have a large font
660 * with a single character that caused an overflow.
662 wc
->pt
.x
+= run
->nWidth
;
665 if (p
!= pp
) /* found a suitable split point */
667 wc
->bOverflown
= TRUE
;
670 /* we detected that it's best to split on start of this run */
674 /* not found anything - writing over margins is the only option left */
676 if ((run
->nFlags
& (MERF_SPLITTABLE
| MERF_STARTWHITE
))
677 || ((run
->nFlags
& (MERF_GRAPHICS
|MERF_TAB
)) && (p
!= wc
->pRowStart
)))
679 wc
->pLastSplittableRun
= p
;
681 wc
->pt
.x
+= run
->nWidth
;
685 static int ME_GetParaLineSpace(ME_Context
* c
, ME_Paragraph
* para
)
688 if (!(para
->pFmt
->dwMask
& PFM_LINESPACING
)) return 0;
690 /* FIXME: how to compute simply the line space in ls ??? */
691 /* FIXME: does line spacing include the line itself ??? */
692 switch (para
->pFmt
->bLineSpacingRule
)
694 case 0: sp
= ls
; break;
695 case 1: sp
= (3 * ls
) / 2; break;
696 case 2: sp
= 2 * ls
; break;
697 case 3: sp
= ME_twips2pointsY(c
, para
->pFmt
->dyLineSpacing
); if (sp
< ls
) sp
= ls
; break;
698 case 4: sp
= ME_twips2pointsY(c
, para
->pFmt
->dyLineSpacing
); break;
699 case 5: sp
= para
->pFmt
->dyLineSpacing
/ 20; break;
700 default: FIXME("Unsupported spacing rule value %d\n", para
->pFmt
->bLineSpacingRule
);
702 if (c
->editor
->nZoomNumerator
== 0)
705 return sp
* c
->editor
->nZoomNumerator
/ c
->editor
->nZoomDenominator
;
708 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
) {
711 tp
->member
.para
.nWidth
= 0;
712 /* remove row start items as they will be reinserted by the
713 * paragraph wrapper anyway */
714 tp
->member
.para
.nRows
= 0;
715 for (p
= tp
->next
; p
!= tp
->member
.para
.next_para
; p
= p
->next
) {
716 if (p
->type
== diStartRow
) {
717 ME_DisplayItem
*pRow
= p
;
720 ME_DestroyDisplayItem(pRow
);
723 /* join runs that can be joined */
724 for (p
= tp
->next
; p
!= tp
->member
.para
.next_para
; p
= p
->next
) {
725 assert(p
->type
!= diStartRow
); /* should have been deleted above */
726 if (p
->type
== diRun
) {
727 while (p
->next
->type
== diRun
&& /* FIXME */
728 ME_CanJoinRuns(&p
->member
.run
, &p
->next
->member
.run
)) {
729 ME_JoinRuns(c
->editor
, p
);
735 static HRESULT
itemize_para( ME_Context
*c
, ME_DisplayItem
*p
)
737 ME_Paragraph
*para
= &p
->member
.para
;
740 SCRIPT_ITEM buf
[16], *items
= buf
;
741 int items_passed
= sizeof( buf
) / sizeof( buf
[0] ), num_items
, cur_item
;
742 SCRIPT_CONTROL control
= { LANG_USER_DEFAULT
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
744 SCRIPT_STATE state
= { 0, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, 0, 0 };
747 assert( p
->type
== diParagraph
);
749 if (para
->pFmt
->dwMask
& PFM_RTLPARA
&& para
->pFmt
->wEffects
& PFE_RTLPARA
)
750 state
.uBidiLevel
= 1;
752 TRACE( "Base embedding level %d\n", state
.uBidiLevel
);
756 hr
= ScriptItemize( para
->text
->szData
, para
->text
->nLen
, items_passed
, &control
,
757 &state
, items
, &num_items
);
758 if (hr
!= E_OUTOFMEMORY
) break; /* may not be enough items if hr == E_OUTOFMEMORY */
759 if (items_passed
> para
->text
->nLen
+ 1) break; /* something else has gone wrong */
762 items
= heap_alloc( items_passed
* sizeof( *items
) );
764 items
= heap_realloc( items
, items_passed
* sizeof( *items
) );
767 if (FAILED( hr
)) goto end
;
769 if (TRACE_ON( richedit
))
771 TRACE( "got items:\n" );
772 for (cur_item
= 0; cur_item
< num_items
; cur_item
++)
774 TRACE( "\t%d - %d RTL %d bidi level %d\n", items
[cur_item
].iCharPos
, items
[cur_item
+1].iCharPos
- 1,
775 items
[cur_item
].a
.fRTL
, items
[cur_item
].a
.s
.uBidiLevel
);
778 TRACE( "before splitting runs into ranges\n" );
779 for (di
= p
->next
; di
!= p
->member
.para
.next_para
; di
= di
->next
)
781 if (di
->type
!= diRun
) continue;
782 TRACE( "\t%d: %s\n", di
->member
.run
.nCharOfs
, debugstr_run( &di
->member
.run
) );
786 /* split runs into ranges at item boundaries */
787 for (di
= p
->next
, cur_item
= 0; di
!= p
->member
.para
.next_para
; di
= di
->next
)
789 if (di
->type
!= diRun
) continue;
790 run
= &di
->member
.run
;
792 if (run
->nCharOfs
== items
[cur_item
+1].iCharPos
) cur_item
++;
794 items
[cur_item
].a
.fLogicalOrder
= TRUE
;
795 run
->script_analysis
= items
[cur_item
].a
;
797 if (run
->nFlags
& MERF_ENDPARA
) break; /* don't split eop runs */
799 if (run
->nCharOfs
+ run
->len
> items
[cur_item
+1].iCharPos
)
801 ME_Cursor cursor
= {p
, di
, items
[cur_item
+1].iCharPos
- run
->nCharOfs
};
802 ME_SplitRunSimple( c
->editor
, &cursor
);
806 if (TRACE_ON( richedit
))
808 TRACE( "after splitting into ranges\n" );
809 for (di
= p
->next
; di
!= p
->member
.para
.next_para
; di
= di
->next
)
811 if (di
->type
!= diRun
) continue;
812 TRACE( "\t%d: %s\n", di
->member
.run
.nCharOfs
, debugstr_run( &di
->member
.run
) );
816 para
->nFlags
|= MEPF_COMPLEX
;
819 if (items
!= buf
) heap_free( items
);
824 static HRESULT
shape_para( ME_Context
*c
, ME_DisplayItem
*p
)
830 for (di
= p
->next
; di
!= p
->member
.para
.next_para
; di
= di
->next
)
832 if (di
->type
!= diRun
) continue;
833 run
= &di
->member
.run
;
835 hr
= shape_run( c
, run
);
838 run
->para
->nFlags
&= ~MEPF_COMPLEX
;
845 static void ME_WrapTextParagraph(ME_Context
*c
, ME_DisplayItem
*tp
) {
852 assert(tp
->type
== diParagraph
);
853 if (!(tp
->member
.para
.nFlags
& MEPF_REWRAP
)) {
856 ME_PrepareParagraphForWrapping(c
, tp
);
858 /* For now treating all non-password text as complex for better testing */
859 if (!c
->editor
->cPasswordMask
/* &&
860 ScriptIsComplex( tp->member.para.text->szData, tp->member.para.text->nLen, SIC_COMPLEX ) == S_OK */)
862 if (SUCCEEDED( itemize_para( c
, tp
) ))
866 pFmt
= tp
->member
.para
.pFmt
;
870 /* wc.para_style = tp->member.para.style; */
872 if (tp
->member
.para
.nFlags
& MEPF_ROWEND
) {
873 wc
.nFirstMargin
= wc
.nLeftMargin
= wc
.nRightMargin
= 0;
875 int dxStartIndent
= pFmt
->dxStartIndent
;
876 if (tp
->member
.para
.pCell
) {
877 dxStartIndent
+= ME_GetTableRowEnd(tp
)->member
.para
.pFmt
->dxOffset
;
879 wc
.nFirstMargin
= ME_twips2pointsX(c
, dxStartIndent
);
880 wc
.nLeftMargin
= wc
.nFirstMargin
+ ME_twips2pointsX(c
, pFmt
->dxOffset
);
881 wc
.nRightMargin
= ME_twips2pointsX(c
, pFmt
->dxRightIndent
);
883 if (wc
.nFirstMargin
< 0)
885 if (wc
.nLeftMargin
< 0)
888 if (c
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
889 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
891 wc
.nFirstMargin
+= ME_twips2pointsX(c
, pFmt
->dxOffset
* 2);
895 if (pFmt
->dwMask
& PFM_SPACEBEFORE
)
896 wc
.pt
.y
+= ME_twips2pointsY(c
, pFmt
->dySpaceBefore
);
897 if (!(pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
) &&
898 pFmt
->dwMask
& PFM_BORDER
)
900 border
= ME_GetParaBorderWidth(c
, tp
->member
.para
.pFmt
->wBorders
);
901 if (pFmt
->wBorders
& 1) {
902 wc
.nFirstMargin
+= border
;
903 wc
.nLeftMargin
+= border
;
905 if (pFmt
->wBorders
& 2)
906 wc
.nRightMargin
-= border
;
907 if (pFmt
->wBorders
& 4)
911 linespace
= ME_GetParaLineSpace(c
, &tp
->member
.para
);
914 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; ) {
915 assert(p
->type
!= diStartRow
);
916 if (p
->type
== diRun
) {
917 p
= ME_WrapHandleRun(&wc
, p
);
920 if (wc
.nRow
&& p
== wc
.pRowStart
)
921 wc
.pt
.y
+= linespace
;
923 ME_WrapEndParagraph(&wc
, p
);
924 if (!(pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
) &&
925 (pFmt
->dwMask
& PFM_BORDER
) && (pFmt
->wBorders
& 8))
927 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEAFTER
)
928 wc
.pt
.y
+= ME_twips2pointsY(c
, pFmt
->dySpaceAfter
);
930 tp
->member
.para
.nFlags
&= ~MEPF_REWRAP
;
931 tp
->member
.para
.nHeight
= wc
.pt
.y
;
932 tp
->member
.para
.nRows
= wc
.nRow
;
935 static void ME_MarkRepaintEnd(ME_DisplayItem
*para
,
936 ME_DisplayItem
**repaint_start
,
937 ME_DisplayItem
**repaint_end
)
940 *repaint_start
= para
;
944 BOOL
ME_WrapMarkedParagraphs(ME_TextEditor
*editor
)
946 ME_DisplayItem
*item
;
949 ME_DisplayItem
*repaint_start
= NULL
, *repaint_end
= NULL
;
951 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
953 item
= editor
->pBuffer
->pFirst
->next
;
954 while(item
!= editor
->pBuffer
->pLast
) {
955 BOOL bRedraw
= FALSE
;
957 assert(item
->type
== diParagraph
);
958 if ((item
->member
.para
.nFlags
& MEPF_REWRAP
)
959 || (item
->member
.para
.pt
.y
!= c
.pt
.y
))
961 item
->member
.para
.pt
= c
.pt
;
963 ME_WrapTextParagraph(&c
, item
);
966 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
968 if (item
->member
.para
.nFlags
& MEPF_ROWSTART
)
970 ME_DisplayItem
*cell
= ME_FindItemFwd(item
, diCell
);
971 ME_DisplayItem
*endRowPara
;
973 cell
->member
.cell
.pt
= c
.pt
;
974 /* Offset the text by the largest top border width. */
975 while (cell
->member
.cell
.next_cell
) {
976 borderWidth
= max(borderWidth
, cell
->member
.cell
.border
.top
.width
);
977 cell
= cell
->member
.cell
.next_cell
;
979 endRowPara
= ME_FindItemFwd(cell
, diParagraph
);
980 assert(endRowPara
->member
.para
.nFlags
& MEPF_ROWEND
);
983 borderWidth
= max(ME_twips2pointsY(&c
, borderWidth
), 1);
985 cell
->member
.cell
.yTextOffset
= borderWidth
;
986 cell
= cell
->member
.cell
.prev_cell
;
988 c
.pt
.y
+= borderWidth
;
990 if (endRowPara
->member
.para
.pFmt
->dxStartIndent
> 0)
992 int dxStartIndent
= endRowPara
->member
.para
.pFmt
->dxStartIndent
;
993 cell
= ME_FindItemFwd(item
, diCell
);
994 cell
->member
.cell
.pt
.x
+= ME_twips2pointsX(&c
, dxStartIndent
);
995 c
.pt
.x
= cell
->member
.cell
.pt
.x
;
998 else if (item
->member
.para
.nFlags
& MEPF_ROWEND
)
1000 /* Set all the cells to the height of the largest cell */
1001 ME_DisplayItem
*startRowPara
;
1002 int prevHeight
, nHeight
, bottomBorder
= 0;
1003 ME_DisplayItem
*cell
= ME_FindItemBack(item
, diCell
);
1004 item
->member
.para
.nWidth
= cell
->member
.cell
.pt
.x
+ cell
->member
.cell
.nWidth
;
1005 if (!(item
->member
.para
.next_para
->member
.para
.nFlags
& MEPF_ROWSTART
))
1007 /* Last row, the bottom border is added to the height. */
1008 cell
= cell
->member
.cell
.prev_cell
;
1011 bottomBorder
= max(bottomBorder
, cell
->member
.cell
.border
.bottom
.width
);
1012 cell
= cell
->member
.cell
.prev_cell
;
1014 bottomBorder
= ME_twips2pointsY(&c
, bottomBorder
);
1015 cell
= ME_FindItemBack(item
, diCell
);
1017 prevHeight
= cell
->member
.cell
.nHeight
;
1018 nHeight
= cell
->member
.cell
.prev_cell
->member
.cell
.nHeight
+ bottomBorder
;
1019 cell
->member
.cell
.nHeight
= nHeight
;
1020 item
->member
.para
.nHeight
= nHeight
;
1021 cell
= cell
->member
.cell
.prev_cell
;
1022 cell
->member
.cell
.nHeight
= nHeight
;
1023 while (cell
->member
.cell
.prev_cell
)
1025 cell
= cell
->member
.cell
.prev_cell
;
1026 cell
->member
.cell
.nHeight
= nHeight
;
1028 /* Also set the height of the start row paragraph */
1029 startRowPara
= ME_FindItemBack(cell
, diParagraph
);
1030 startRowPara
->member
.para
.nHeight
= nHeight
;
1031 c
.pt
.x
= startRowPara
->member
.para
.pt
.x
;
1032 c
.pt
.y
= cell
->member
.cell
.pt
.y
+ nHeight
;
1033 if (prevHeight
< nHeight
)
1035 /* The height of the cells has grown, so invalidate the bottom of
1037 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
1038 cell
= ME_FindItemBack(item
, diCell
);
1040 ME_MarkRepaintEnd(ME_FindItemBack(cell
, diParagraph
), &repaint_start
, &repaint_end
);
1041 cell
= cell
->member
.cell
.prev_cell
;
1045 else if (item
->member
.para
.pCell
&&
1046 item
->member
.para
.pCell
!= item
->member
.para
.next_para
->member
.para
.pCell
)
1048 /* The next paragraph is in the next cell in the table row. */
1049 ME_Cell
*cell
= &item
->member
.para
.pCell
->member
.cell
;
1050 cell
->nHeight
= c
.pt
.y
+ item
->member
.para
.nHeight
- cell
->pt
.y
;
1052 /* Propagate the largest height to the end so that it can be easily
1053 * sent back to all the cells at the end of the row. */
1054 if (cell
->prev_cell
)
1055 cell
->nHeight
= max(cell
->nHeight
, cell
->prev_cell
->member
.cell
.nHeight
);
1057 c
.pt
.x
= cell
->pt
.x
+ cell
->nWidth
;
1058 c
.pt
.y
= cell
->pt
.y
;
1059 cell
->next_cell
->member
.cell
.pt
= c
.pt
;
1060 if (!(item
->member
.para
.next_para
->member
.para
.nFlags
& MEPF_ROWEND
))
1061 c
.pt
.y
+= cell
->yTextOffset
;
1065 if (item
->member
.para
.pCell
) {
1066 /* Next paragraph in the same cell. */
1067 c
.pt
.x
= item
->member
.para
.pCell
->member
.cell
.pt
.x
;
1069 /* Normal paragraph */
1072 c
.pt
.y
+= item
->member
.para
.nHeight
;
1075 totalWidth
= max(totalWidth
, item
->member
.para
.nWidth
);
1076 item
= item
->member
.para
.next_para
;
1078 editor
->sizeWindow
.cx
= c
.rcView
.right
-c
.rcView
.left
;
1079 editor
->sizeWindow
.cy
= c
.rcView
.bottom
-c
.rcView
.top
;
1081 editor
->nTotalLength
= c
.pt
.y
;
1082 editor
->nTotalWidth
= totalWidth
;
1083 editor
->pBuffer
->pLast
->member
.para
.pt
.x
= 0;
1084 editor
->pBuffer
->pLast
->member
.para
.pt
.y
= c
.pt
.y
;
1086 ME_DestroyContext(&c
);
1088 if (repaint_start
|| editor
->nTotalLength
< editor
->nLastTotalLength
)
1089 ME_InvalidateParagraphRange(editor
, repaint_start
, repaint_end
);
1090 return !!repaint_start
;
1093 void ME_InvalidateParagraphRange(ME_TextEditor
*editor
,
1094 ME_DisplayItem
*start_para
,
1095 ME_DisplayItem
*last_para
)
1101 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
1103 ofs
= editor
->vert_si
.nPos
;
1106 start_para
= ME_GetOuterParagraph(start_para
);
1107 last_para
= ME_GetOuterParagraph(last_para
);
1108 rc
.top
= c
.rcView
.top
+ start_para
->member
.para
.pt
.y
- ofs
;
1110 rc
.top
= c
.rcView
.top
+ editor
->nTotalLength
- ofs
;
1112 if (editor
->nTotalLength
< editor
->nLastTotalLength
)
1113 rc
.bottom
= c
.rcView
.top
+ editor
->nLastTotalLength
- ofs
;
1115 rc
.bottom
= c
.rcView
.top
+ last_para
->member
.para
.pt
.y
+ last_para
->member
.para
.nHeight
- ofs
;
1116 ITextHost_TxInvalidateRect(editor
->texthost
, &rc
, TRUE
);
1118 ME_DestroyContext(&c
);
1123 ME_SendRequestResize(ME_TextEditor
*editor
, BOOL force
)
1125 if (editor
->nEventMask
& ENM_REQUESTRESIZE
)
1129 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
1131 if (force
|| rc
.bottom
!= editor
->nTotalLength
)
1135 info
.nmhdr
.hwndFrom
= NULL
;
1136 info
.nmhdr
.idFrom
= 0;
1137 info
.nmhdr
.code
= EN_REQUESTRESIZE
;
1139 info
.rc
.right
= editor
->nTotalWidth
;
1140 info
.rc
.bottom
= editor
->nTotalLength
;
1142 editor
->nEventMask
&= ~ENM_REQUESTRESIZE
;
1143 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
1144 editor
->nEventMask
|= ENM_REQUESTRESIZE
;