layout/doc/obsolete/block.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 <!-- This Source Code Form is subject to the terms of the Mozilla Public
     2    - License, v. 2.0. If a copy of the MPL was not distributed with this
     3    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
     5 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
     6 <html>
     7 <head>
     8    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
     9    <meta name="GENERATOR" content="Mozilla/4.61 [en] (X11; I; Linux 2.2.5-22 i686) [Netscape]">
    10 </head>
    11 <body>
    13 <h1>
    14 <u>Block Layout</u></h1>
    15 This document attempts to describe how "block" layout works in the mozilla
    16 layout engine.
    17 <p><tt>nsBlockFrame</tt> implements layout behavior that conforms to the
    18 CSS "display:block" and "display: list-item" layout. It has several responsibilities:
    19 <ol>
    20 <li>
    21 &nbsp;Line layout. The block is responsible for flowing inline elements
    22 into "lines" and applying all of the css behavior as one might expect,
    23 including line-height, vertical-align, relative positioning, etc.</li>
    25 <li>
    26 Float management. The block is responsible for the reflow and placement
    27 of floating elements.</li>
    29 <li>
    30 Child block management. Blocks can contain inline elements and block elements.
    31 Hence, blocks are responsible for reflowing child blocks. The majority
    32 of that logic has been split out into nsBlockReflowContext, but a fair
    33 amount remains here.</li>
    35 <li>
    36 Supporting table reflow. The block has to carefully compute the "max-element-size"
    37 information needed by tables. Hence, any time changes are made here one
    38 should always run the table regression tests because the odds are you broke
    39 one of them!</li>
    40 </ol>
    42 <h3>
    43 <u>The Big Picture for Block Reflow</u></h3>
    44 The block frame uses a list of nsLineBox's to keep track of each "line"
    45 of frames it manages. There are two types of lines:
    46 <blockquote>"inline" lines which contain only inline elements
    47 <br>"block" lines which contain exactly one block element</blockquote>
    48 Each line has a "dirty" bit which indicates that it needs reflow. Reflow
    49 consists of identifying which lines need to be marked dirty and then reflowing
    50 all lines. For lines which are "clean" the reflow logic will endeavor to
    51 recover the state of reflow <i>as if the line had been reflowed</i>. This
    52 saves time and allows for a faster incremental reflow. For lines which
    53 are dirty, the line is reflowed appropriately.
    54 <p>The only special thing about incremental reflow command handling is
    55 that it marks lines dirty before proceeding, and keeps track of the child
    56 frame that is the next frame on the reflow command path.
    57 <p>Here is a list of the various classes involved in block layout:
    58 <p><b>nsBlockFrame</b>
    59 <blockquote>The primary culprit.</blockquote>
    60 <b>nsBlockReflowState</b>
    61 <blockquote>This helper class is used to augment the nsHTMLReflowState
    62 with other information needed by the block reflow logic during reflow.
    63 It is a temporary object that is designed to live on the processor stack
    64 and contains "running" state used by the blocks reflow logic.</blockquote>
    65 <b>nsBlockBandData</b>
    66 <blockquote>Another helper class that wraps up management of a space manager
    67 (nsISpaceManager, nsSpaceManager) and nsBandData. It also assists in management
    68 of floating elements. While nsSpaceManager is policy free, nsBlockBandData
    69 provides specific HTML and CSS policy.</blockquote>
    70 <b>nsBlockReflowContext</b>
    71 <blockquote>A helper class that encapsulates the logic needed to reflow
    72 a child block frame. This is used by the block code reflow a child block
    73 and to reflow floating elements (which are to be treated as blocks according
    74 to the CSS2 spec).</blockquote>
    75 <b>nsLineBox</b>
    76 <blockquote>A data class used to store line information for the block frame
    77 code. Each line has a list of children (though the frames are linked together
    78 across lines to maintain the sibling list for nsIFrame::FirstChild) and
    79 some other state used to assist in incremental reflow.</blockquote>
    80 <b>nsLineLayout</b>
    81 <blockquote>This class is the line layout engine. Its a passive entity
    82 in the sense that its the responsibility of the block/inline code to use
    83 the class (this is done so that the line layout engine doesn't have to
    84 manage child frame lists so that both nsBlockFrame and nsInlineFrame can
    85 use the class).</blockquote>
    86 <b>nsTextRun</b>
    87 <blockquote>This is a data class used to store text run information. Text
    88 runs are <i>logically</i> contiguous runs of text (they may or may not
    89 be structurally contiguous). The block frame stores a pointer to a list
    90 of nsTextRun's and during line layout provides the list to the nsLineLayout
    91 engine so that when text is reflowed the text layout code (nsTextFrame)
    92 can find related text to properly handle word breaking.</blockquote>
    94 <h3>
    95 <u>Frame construction methods</u></h3>
    96 When the blocks child list is modified (AppendFrames, InsertFrames, RemoveFrame)
    97 the block code updates its nsLineBox list. Since each nsLineBox is typed
    98 (some are marked "inline" and some are marked "block"), the update logic
    99 maintains the invariant of "one block frame per block line".
   100 <p>When structural changes are made to the blocks children (append/insert/remove)
   101 the block code updates the line's and then marks the affected lines "dirty"
   102 (each nsLineBox has a dirty bit). After the structural changes are finished
   103 then the block will generate an incremental reflow command of type "ReflowDirty".
   104 <h3>
   105 <u>Line Layout</u></h3>
   106 Line layout consists of the placement of inline elements on a line until
   107 there is no more room on the line. At that point the line is "broken" and
   108 continued on the next line. This process continues until all inline elements
   109 have been exhausted. The block code maintains a list of "nsLineBox"'s to
   110 facilitate this. These are used instead of frames because they use less
   111 memory and because it allows the block to directly control their behavior.
   112 <p>The helper class nsLineLayout provides the majority of the line layout
   113 behavior needed by the block.
   114 <p>The block does keep "text-run" information around for the nsLineLayout
   115 logic to use during reflow. Text runs keep track of logically adjacent
   116 pieces of text within a block. This information is essential for properly
   117 computing line and word breaking. Why? Well, because in html you can write
   118 something like this:
   119 <p>&nbsp; &lt;p>I &lt;b>W&lt;/b>as thinking one day&lt;/p>
   120 <p>Notice that the word "Was" is composed of two pieces of text, and that
   121 they do <i>not</i> have the same parent (content or frame). To properly
   122 reflow this and not break the word prematurely after the "W", the text-run
   123 information is used by the text frame code to "look ahead" and prevent
   124 premature breaking.
   125 <p>Lines also keep track of the type of "break" that occurred on the line.
   126 This is used, for example, to support html's "&lt;br clear=left>" behavior.
   127 <h3>
   128 <u>Float Management</u></h3>
   129 Since child block elements are containing blocks for floats, the only
   130 place where a block frame will see a float is as part of an inline line.
   131 Consequently, the nsLineBox will only keep track of floats on inline
   132 lines (saving storage for block lines).
   133 <p>The nsLineLayout class and the block frame cooperate in the management
   134 of floats. Since the frame construction code leaves a "placeholder" frame
   135 in-flow where the float was found, when nsLineLayout reflows a placeholder
   136 frame it knows to inform the block about it. That triggers the blocks "AddFloat"
   137 logic which then determines where the float should be placed (on the
   138 current line or below the current line).
   139 <p>The block frame uses the space manager to manage the effects of floats,
   140 namely the consumption of available space. For example, for a left aligned
   141 floating element, the inline elements must be placed to the right of the
   142 float. To simplify this process, the spacemanager is used to keep track
   143 of available and busy space. Floats when placed mark space as busy and
   144 the spacemanager will them compute the available space. Most of this logic
   145 is handled by the nsBlockReflowState which uses a helper class, nsBlockBandData,
   146 in concert with the space manager, to do the available space computations.
   147 <h3>
   148 <u>Child Block Placement</u></h3>
   149 Child block reflow is done primarily by using the nsBlockReflowContext
   150 code. However, a key detail worth mentioning here is how margins are handled.
   151 When the nsHTMLReflowState was created, we placed into it the logic for
   152 computing margins, border and padding (among other things). Unfortunately,
   153 given the css rules for sibling and generational margin collapsing, the
   154 nsHTMLReflowState is unable to properly compute top and bottom margins.
   155 Hence, the block frame and the nsBlockReflowContext code perform that function.
   156 At the time that the nsBlockReflowContext was designed and implemented
   157 we thought that it could compute the top-margin itself and then proceed
   158 to place the child block element. However, that turned out to be wrong
   159 (oh well) because the correct available space isn't known until <i>after</i>
   160 the top margin is computed. Hence, there is some unfortunate duplication
   161 of reflow state calculations present in the block frame code.
   162 <h3>
   163 <u>Bullets</u></h3>
   164 Another type of block frame is the "display: list-item". List-items use
   165 nsBulletFrame's to manage bullet reflow. However, the block is responsible
   166 for bullet placement. In most situations, the nsLineLayout class is used
   167 to do the placement. However, if the first effective child of the block
   168 is another block, then the block has to do the placement itself.
   169 <h3>
   170 <u>Blank lines</u></h3>
   171 Because our content model contains as much of the original source documents
   172 content as possible, we end up with a lot of white space that ends up being
   173 compressed into nothingness. This white space ends up impacting this logic
   174 in several ways. For example:
   175 <p>&nbsp; &lt;div>
   176 <br>&nbsp;&nbsp; &lt;p>abc&lt;/p>
   177 <br>&nbsp;&nbsp; &lt;p>def&lt;/p>
   178 <br>&nbsp; &lt;/div>
   179 <p>In the content model for the above html, there is white space between
   180 the various block elements (some after the &lt;div>, some after the first
   181 &lt;/p>, again after the second &lt;/p>).
   182 <p>For css margin collapsing to work properly, each of those instances
   183 of white space has to behave as if they didn't exist. Consequently, there
   184 is special logic in the inline line reflow code, and in the nsBlockReflowContext
   185 code and in the GetTopBlockChild method, to basically ignore such lines.
   186 <h3>
   187 <u>First-letter style</u></h3>
   188 The block contributes, in a small way, to first-letter style reflow. The
   189 frame construction code is responsible for creating the list of child frames
   190 for all frames, including the block. It manages the creation of letter-frames,
   191 where appropriate, so that all the block has to do is reflow them almost
   192 normally like other inline frames.
   193 <p>There are two things different that the block does:
   194 <p>It is responsible for calling nsLineLayout::SetFirstLetterStyleOK
   195 <br>It is responsible for continuing to place frames on a line, even after
   196 a frame has said "it can't fit". Normally during inline reflow, if a frame
   197 comes back and says it can't fit, the block will end the line, push all
   198 remaining frames to the next line and pick up the reflow from there after
   199 making sure the frame that didn't fit is continued. For letter-frames,
   200 this would result in the first-letter being on one line with the remaining
   201 text on subsequent lines. Hence, the block code handles this special case.
   202 <br>&nbsp;
   203 <h3>
   204 <u>First-line style</u></h3>
   205 First-line is handled entirely by the frame construction code.
   206 <br>&nbsp;
   207 <br>&nbsp;
   208 </body>
   209 </html>

mercurial