Bug 34155

Summary: Support CSS page-break-{before/after} with a value of 'avoid'.
Product: WebKit Reporter: Hayato Ito <hayato>
Component: CSSAssignee: Nobody <webkit-unassigned>
Status: ASSIGNED ---    
Severity: Normal CC: benwells, bob.net2005, david, douglas, hamaji, hayato, innovimax, markus.magnuson, m.kurz+webkitbugs, norman.jonas, syoichi, yuzo
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: PC   
OS: OS X 10.5   
URL: http://www.w3.org/TR/CSS2/page.html#page-break-props
Bug Depends on: 40814    
Bug Blocks: 39735    

Description Hayato Ito 2010-01-25 21:26:02 PST
Implement CSS page-break-{before/after} with a value of 'avoid', which is defined in http://www.w3.org/TR/CSS2/page.html#page-break-props

Currently, only a value of 'always' is supported in both bases.
Comment 1 Hayato Ito 2010-05-06 04:32:25 PDT
I'd like to update the status.

In current WebCore, it seems that the position of page breaks are determined in 'greedy' manner, without looking ahead entire web page. I tried to support 'page-break-xx: avoid', but found that it is difficult to support these rules within current approach.

So I've decided to take the following new approach:

1. Collect every candidates of page breaks in entire web page beforehand in printing time. Each candidate represents a place where page break might occur and must meet the condition of 'Allowed page breaks' defined in http://dev.w3.org/csswg/css3-page/#allowed-pg-brk
2. Give a score to each candidate using some dynamic programming techniques. For example, if a candidate has a CSS property: {page-break-before: always}, we add a large positive score to the score.

  Pseudo code should be like that:

   Vector<Candidate> pageBreakCandidates;

   for (i = 0; i < pageBreakCandidates.size(); ++i) {
     pageBreakCandidates[i].score = initail value.
     for (j = 0; j < i; ++j) {
        score = pageBreakCandidates[j].score + calScore(i, j);
        if (score > pageBreakCandidates[i].score) {
           pageBreakCandidates[i].score = score;
           pageBreakCandidates[i].previsou = j;

3 - Pick up set of candidates which maximizes the final score of last page break.

- Not only supporting 'page-break-xx: avoid', but also it would be easy to support other CSS property related to page break, such as orphan, widow (See https://bugs.webkit.org/show_bug.cgi?id=9410).  All we have to do is to add new scoring rule in calScore.

- Speed. This approach takes O(n^2) (n = number of allowable pagebreaks) in computing score of each candidate in theory. It matters only when printing time. This does not affect normail rendering phase. We need some measurement to know whether it really matters or not.
- Compatibility. We break compatibility because we try to place page breaks only at *allowed* position. Current WebCore does not try to do that. It seems that only Opera try to do that. Needs more investigation.

I'll post a patch in a few weeks. Maybe I have to touch also multi column layout code because it depends on page break code.
Please let me know if you have any comments or ideas. Thank you.
Comment 2 David Latapie 2010-05-09 16:48:24 PDT
Duplicate of https://bugs.webkit.org/show_bug.cgi?id=9410
Comment 3 Ben Wells 2011-06-15 21:15:09 PDT
Updated status when triaging bugs, if this is no longer valid or is a dup either mark or let me know and I'll update.
Comment 4 Bob Lan 2016-02-22 16:54:05 PST
Related bug: https://bugs.webkit.org/show_bug.cgi?id=5097