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.01 Transitional//EN">
9 <meta http-equiv=
"content-type" content=
"text/html; charset=ISO-8859-1">
10 <title>Detailed Design Template
</title>
14 <h1><font color=
"#cc0000">Gecko Layout Detailed Design Document
</font></h1>
16 <h1>Space Manager Detailed Design
</h1>
20 The Space Manager and related classes and structures are an important of
21 the Gecko Layout system, specifically Block Layout.
See the High Level
22 Design document for an overview of the Space Manager, and as an introduction
23 to the classes, structures and algorithms container in this, the Detailed
29 <hr width=
"100%" size=
"2">
30 <h2>nsSpaceManager
</h2>
32 The Space Manager is the central class is a group of classes that manage
33 the occupied and available space that exists in horizontal bands across
34 a canvas.
The primary goal of the Space Manager is to provide information
35 about those bands of space to support the CSS notion of floated elements.
39 There are three important parts to the Space Manager API: the parts that
40 deal with the coordinate space of the Space Manager, the parts that deal with
41 the regions managed by the Space Manager, and the parts that manage float
46 The class nsSpaceManager is declared in the file
<a href=
"http://lxr.mozilla.org/seamonkey/source/layout/base/src/nsSpaceManager.h">
48 .
The class is only used in the layout module and cannot be exported
49 outside of that module (nor does it need to be).
It is not a general
50 purpose class, and is not intended to be subclasses
<font color=
"#cc0000">
55 Here is the class declaration, taken from the source file as of
01.08.02
61 * Class for dealing with bands of available space. The space manager
62 * defines a coordinate space with an origin at (
0,
0) that grows down
65 class nsSpaceManager {
67 nsSpaceManager(nsIPresShell* aPresShell, nsIFrame* aFrame);
70 void* operator new(size_t aSize);
71 void operator delete(void* aPtr, size_t aSize);
73 static void Shutdown();
76 * Get the frame that's associated with the space manager. This frame
77 * created the space manager, and the world coordinate space is
78 * relative to this frame.
80 * You can use QueryInterface() on this frame to get any additional
83 nsIFrame* GetFrame() const { return mFrame; }
86 * Translate the current origin by the specified (dx, dy). This
87 * creates a new local coordinate space relative to the current
90 void Translate(nscoord aDx, nscoord aDy) { mX += aDx; mY += aDy; }
93 * Returns the current translation from local coordinate space to
94 * world coordinate space. This represents the accumulated calls to
97 void GetTranslation(nscoord
& aX, nscoord
& aY) const { aX = mX; aY = mY; }
100 * Returns the y-most of the bottommost band or
0 if there are no bands.
102 * @return PR_TRUE if there are bands and PR_FALSE if there are no bands
104 PRBool YMost(nscoord
& aYMost) const;
107 * Returns a band starting at the specified y-offset. The band data
108 * indicates which parts of the band are available, and which parts
111 * The band data that is returned is in the coordinate space of the
112 * local coordinate system.
114 * The local coordinate space origin, the y-offset, and the max size
115 * describe a rectangle that's used to clip the underlying band of
116 * available space, i.e.
117 * {
0, aYOffset, aMaxSize.width, aMaxSize.height} in the local
120 * @param aYOffset the y-offset of where the band begins. The coordinate is
121 * relative to the upper-left corner of the local coordinate space
122 * @param aMaxSize the size to use to constrain the band data
123 * @param aBandData [in,out] used to return the list of trapezoids that
124 * describe the available space and the unavailable space
125 * @return NS_OK if successful and NS_ERROR_FAILURE if the band data is not
126 * not large enough. The 'count' member of the band data struct
127 * indicates how large the array of trapezoids needs to be
129 nsresult GetBandData(nscoord aYOffset,
130 const nsSize
& aMaxSize,
131 nsBandData
& aBandData) const;
134 * Add a rectangular region of unavailable space. The space is
135 * relative to the local coordinate system.
137 * The region is tagged with a frame
139 * @param aFrame the frame used to identify the region. Must not be NULL
140 * @param aUnavailableSpace the bounding rect of the unavailable space
141 * @return NS_OK if successful
142 * NS_ERROR_FAILURE if there is already a region tagged with aFrame
144 nsresult AddRectRegion(nsIFrame* aFrame,
145 const nsRect
& aUnavailableSpace);
148 * Resize the rectangular region associated with aFrame by the specified
149 * deltas. The height change always applies to the bottom edge or the existing
150 * rect. You specify whether the width change applies to the left or right edge
152 * Returns NS_OK if successful, NS_ERROR_INVALID_ARG if there is no region
155 enum AffectedEdge {LeftEdge, RightEdge};
156 nsresult ResizeRectRegion(nsIFrame* aFrame,
158 nscoord aDeltaHeight,
159 AffectedEdge aEdge = RightEdge);
162 * Offset the region associated with aFrame by the specified amount.
164 * Returns NS_OK if successful, NS_ERROR_INVALID_ARG if there is no region
167 nsresult OffsetRegion(nsIFrame* aFrame, nscoord dx, nscoord dy);
170 * Remove the region associated with aFrane.
172 * Returns NS_OK if successful and NS_ERROR_INVALID_ARG if there is no region
175 nsresult RemoveRegion(nsIFrame* aFrame);
178 * Clears the list of regions representing the unavailable space.
183 * Methods for dealing with the propagation of float damage during
186 PRBool HasFloatDamage()
188 return !mFloatDamage.IsEmpty();
191 void IncludeInDamage(nscoord aIntervalBegin, nscoord aIntervalEnd)
193 mFloatDamage.IncludeInterval(aIntervalBegin + mY, aIntervalEnd + mY);
196 PRBool IntersectsDamage(nscoord aIntervalBegin, nscoord aIntervalEnd)
198 return mFloatDamage.Intersects(aIntervalBegin + mY, aIntervalEnd + mY);
203 * Dump the state of the spacemanager out to a file
205 nsresult List(FILE* out);
207 void SizeOf(nsISizeOfHandler* aHandler, uint32_t* aResult) const;
211 // Structure that maintains information about the region associated
212 // with a particular frame
214 nsIFrame* const mFrame;
215 nsRect mRect; // rectangular region
218 FrameInfo(nsIFrame* aFrame, const nsRect
& aRect);
219 #ifdef NS_BUILD_REFCNT_LOGGING
224 // Doubly linked list of band rects
225 struct BandRect : PRCListStr {
227 nscoord mRight, mBottom;
228 int32_t mNumFrames; // number of frames occupying this rect
230 nsIFrame* mFrame; // single frame occupying the space
231 nsVoidArray* mFrames; // list of frames occupying the space
234 BandRect(nscoord aLeft, nscoord aTop,
235 nscoord aRight, nscoord aBottom,
237 BandRect(nscoord aLeft, nscoord aTop,
238 nscoord aRight, nscoord aBottom,
243 BandRect* Next() const {return (BandRect*)PR_NEXT_LINK(this);}
244 BandRect* Prev() const {return (BandRect*)PR_PREV_LINK(this);}
245 void InsertBefore(BandRect* aBandRect) {PR_INSERT_BEFORE(aBandRect, this);}
246 void InsertAfter(BandRect* aBandRect) {PR_INSERT_AFTER(aBandRect, this);}
247 void Remove() {PR_REMOVE_LINK(this);}
249 // Split the band rect into two vertically, with this band rect becoming
250 // the top part, and a new band rect being allocated and returned for the
253 // Does not insert the new band rect into the linked list
254 BandRect* SplitVertically(nscoord aBottom);
256 // Split the band rect into two horizontally, with this band rect becoming
257 // the left part, and a new band rect being allocated and returned for the
260 // Does not insert the new band rect into the linked list
261 BandRect* SplitHorizontally(nscoord aRight);
263 // Accessor functions
264 PRBool IsOccupiedBy(const nsIFrame*) const;
265 void AddFrame(const nsIFrame*);
266 void RemoveFrame(const nsIFrame*);
267 PRBool HasSameFrameList(const BandRect* aBandRect) const;
268 int32_t Length() const;
271 // Circular linked list of band rects
272 struct BandList : BandRect {
276 PRBool IsEmpty() const {return PR_CLIST_IS_EMPTY((PRCListStr*)this);}
277 BandRect* Head() const {return (BandRect*)PR_LIST_HEAD(this);}
278 BandRect* Tail() const {return (BandRect*)PR_LIST_TAIL(this);}
281 void Append(BandRect* aBandRect) {PR_APPEND_LINK(aBandRect, this);}
283 // Remove and delete all the band rects in the list
288 FrameInfo* GetFrameInfoFor(nsIFrame* aFrame);
289 FrameInfo* CreateFrameInfo(nsIFrame* aFrame, const nsRect
& aRect);
290 void DestroyFrameInfo(FrameInfo*);
292 void ClearFrameInfo();
293 void ClearBandRects();
295 BandRect* GetNextBand(const BandRect* aBandRect) const;
296 void DivideBand(BandRect* aBand, nscoord aBottom);
297 PRBool CanJoinBands(BandRect* aBand, BandRect* aPrevBand);
298 PRBool JoinBands(BandRect* aBand, BandRect* aPrevBand);
299 void AddRectToBand(BandRect* aBand, BandRect* aBandRect);
300 void InsertBandRect(BandRect* aBandRect);
302 nsresult GetBandAvailableSpace(const BandRect* aBand,
304 const nsSize
& aMaxSize,
305 nsBandData
& aAvailableSpace) const;
307 nsIFrame* const mFrame; // frame associated with the space manager
308 nscoord mX, mY; // translation from local to global coordinate space
309 BandList mBandList; // header/sentinel for circular linked list of band rects
310 FrameInfo* mFrameInfoMap;
311 nsIntervalSet mFloatDamage;
313 static int32_t sCachedSpaceManagerCount;
314 static void* sCachedSpaceManagers[NS_SPACE_MANAGER_CACHE_SIZE];
316 nsSpaceManager(const nsSpaceManager
&); // no implementation
317 void operator=(const nsSpaceManager
&); // no implementation
326 The Constructor requires a Presentation Shell, used for arena allocations
327 mostly, and a frame that this Space Manager is rooted on.
The coordinate
328 space of this Space Manager is relative to the frame passed in to the constructor.
331 <pre> nsSpaceManager(nsIPresShell* aPresShell, nsIFrame* aFrame);
335 Operators 'new' and 'delete' are overridden to support a recycler.
Space
336 Manager instances come and go pretty frequently, and this recycler prevents
337 excessive heap allocations and the performance penalties associated with
338 it. The #define NS_SPACE_MANAGER_CACHE_SIZE is used to control the number
339 of Space Manager instances that can be present in the recycler, currently
340 4.
If more than NS_SPACE_MANAGER_CACHE_SIZE are allocated at a time,
341 then standard allocation is used.
345 void* operator new(size_t aSize);
346 void operator delete(void* aPtr, size_t aSize);
350 A Static method is used to shutdown the Space Manager recycling.
This
351 method deletes all of the Space Mangers inthe recycler,and prevents further
352 recycling.
It is meant to be called only when the layout module is being
356 <pre> static void Shutdown();
360 <h4>Origin / Coordinate Space Translation
</h4>
363 * Translate the current origin by the specified (dx, dy). This
364 * creates a new local coordinate space relative to the current
367 void Translate(nscoord aDx, nscoord aDy) { mX += aDx; mY += aDy; }
370 * Returns the current translation from local coordinate space to
371 * world coordinate space. This represents the accumulated calls to
374 void GetTranslation(nscoord
& aX, nscoord
& aY) const { aX = mX; aY = mY; }
377 * Returns the y-most of the bottommost band or
0 if there are no bands.
379 * @return PR_TRUE if there are bands and PR_FALSE if there are no bands
381 PRBool YMost(nscoord
& aYMost) const;
384 <h4>Region Management
</h4>
387 * Returns a band starting at the specified y-offset. The band data
388 * indicates which parts of the band are available, and which parts
391 * The band data that is returned is in the coordinate space of the
392 * local coordinate system.
394 * The local coordinate space origin, the y-offset, and the max size
395 * describe a rectangle that's used to clip the underlying band of
396 * available space, i.e.
397 * {
0, aYOffset, aMaxSize.width, aMaxSize.height} in the local
400 * @param aYOffset the y-offset of where the band begins. The coordinate is
401 * relative to the upper-left corner of the local coordinate space
402 * @param aMaxSize the size to use to constrain the band data
403 * @param aBandData [in,out] used to return the list of trapezoids that
404 * describe the available space and the unavailable space
405 * @return NS_OK if successful and NS_ERROR_FAILURE if the band data is not
406 * not large enough. The 'count' member of the band data struct
407 * indicates how large the array of trapezoids needs to be
409 nsresult GetBandData(nscoord aYOffset,
410 const nsSize
& aMaxSize,
411 nsBandData
& aBandData) const;
414 * Add a rectangular region of unavailable space. The space is
415 * relative to the local coordinate system.
417 * The region is tagged with a frame
419 * @param aFrame the frame used to identify the region. Must not be NULL
420 * @param aUnavailableSpace the bounding rect of the unavailable space
421 * @return NS_OK if successful
422 * NS_ERROR_FAILURE if there is already a region tagged with aFrame
424 nsresult AddRectRegion(nsIFrame* aFrame,
425 const nsRect
& aUnavailableSpace);
428 * Resize the rectangular region associated with aFrame by the specified
429 * deltas. The height change always applies to the bottom edge or the existing
430 * rect. You specify whether the width change applies to the left or right edge
432 * Returns NS_OK if successful, NS_ERROR_INVALID_ARG if there is no region
435 enum AffectedEdge {LeftEdge, RightEdge};
436 nsresult ResizeRectRegion(nsIFrame* aFrame,
438 nscoord aDeltaHeight,
439 AffectedEdge aEdge = RightEdge);
442 * Offset the region associated with aFrame by the specified amount.
444 * Returns NS_OK if successful, NS_ERROR_INVALID_ARG if there is no region
447 nsresult OffsetRegion(nsIFrame* aFrame, nscoord dx, nscoord dy);
450 * Remove the region associated with aFrane.
452 * Returns NS_OK if successful and NS_ERROR_INVALID_ARG if there is no region
455 nsresult RemoveRegion(nsIFrame* aFrame);
458 * Clears the list of regions representing the unavailable space.
463 <h4>Float Impact
</h4>
466 * Methods for dealing with the propagation of float damage during
469 PRBool HasFloatDamage()
471 return !mFloatDamage.IsEmpty();
474 void IncludeInDamage(nscoord aIntervalBegin, nscoord aIntervalEnd)
476 mFloatDamage.IncludeInterval(aIntervalBegin + mY, aIntervalEnd + mY);
479 PRBool IntersectsDamage(nscoord aIntervalBegin, nscoord aIntervalEnd)
481 return mFloatDamage.Intersects(aIntervalBegin + mY, aIntervalEnd + mY);
485 <h4>Debug Only Methods
</h4>
488 * Dump the state of the spacemanager out to a file
490 nsresult List(FILE* out);
492 void SizeOf(nsISizeOfHandler* aHandler, uint32_t* aResult) const;
496 <h4>Unused / Obsolete Methods
</h4>
499 * Get the frame that's associated with the space manager. This frame
500 * created the space manager, and the world coordinate space is
501 * relative to this frame.
503 * You can use QueryInterface() on this frame to get any additional
506 nsIFrame* GetFrame() const { return mFrame; }
510 <h3>Implementation Notes
</h3>
514 <h4>Algorithm
1: GetBandData
</h4>
516 GetBandData is used to provide information to clients about what space if
517 available and unavailable in a band of space.
The client provides a
518 vertical offset, the yOffset, that corresponds to the band that is of interest.
519 This will be the y offset of the frame that is being reflowed.
The
520 caller also provides a collection of BandData objects (an array) and the
521 number of items that the collection can handle.
If the collection is
522 too small, then an error is returned and the count is updated to indicate
527 The algorithm to provide the band data is as follows:
530 <li>Get a
vertical offset in world coordinates (instead of frame-relative
531 coordinates) by adding the y-origin of the SpaceManager to the y offset passed
533 <li>If the (adjusted) y value passed in is greater than the greatest band
534 being managed, then all space is available so a single trapezoid is returned,
535 marked as available and sized to the maximum size value (passed in).
</li>
536 <li>If the (adjusted) y offset intersects a band, then gather the band
539 <li>walk the internal bandData list from head to tail
</li>
540 <li>for each band data entry, see if the top of the band is greater than
541 the (adjusted) y offset requested
</li>
542 <li>if it is, then band is below the offset requested, so the area between
543 the band and the y offset is available - create a trapezoid with that region
545 <li>if the (adjusted) y offset is between the band top and bottom, then
546 get the available space for the band by calling GetBandAvailableSpace
</li>
547 <li>otherwise, move to the next band
</li>
550 <h5>GetBandAvailableSpace:
</h5>
551 This method is called from GetBandData only. It walks all of the bands in
552 the space manager and determines which bands intersect with the band passed
553 in, and if within those bands there are regions that are available or occupied.
556 <li>First, walk all of the bands until a band that is to the right of the
557 desired offset is located
</li>
558 <li>Starting at that band,
walk the remaining bands:
</li>
560 <li>if the current band is to the right of the requested band, then there
561 is available space.
</li>
563 <li>if there is more room in the bandData collection, then add a trapezoid
564 to the bandData collection such that it is marked as available and has a
565 rect that represents the space between the reference band tna dht band being
567 <li>if there is no more room in the BandData collection, estimate the
568 number of entries requires as the current count + twice the number of bands
569 below the reference band, plus two.
Return an error code so the caller
570 can reallocate the collection and try again.
</li>
572 <li>check the size of the collection again, if there is no room left
573 then estimate the number of items requires as the current count + twice the
574 number of bands below the band in question plus one.
</li>
575 <li>create a new trapezoid in the band collection that has a region corresponding
576 to the reference band rect, marked as occupied by either a single or multiple
578 <li>move to the next band
</li>
580 <li>after walking all of the band data, se if we have reached the right
581 edge of the band.
</li>
583 <li>If not, then check for space in the band collection
</li>
585 <li>if there is no room left, then set the count to the current count
586 plus
1 and return an error.
</li>
587 <li>otherwise, create another entry in the band collection, marked
588 as available, and with a rect corresponding to the area remainin in the band
589 (eg. from the right edge of the last band rect to the right edge of the band).
</li>
594 <h4>Algorithm
2: AddRectRegion
</h4>
595 Clients call into this method to notify the Space Manager that a new frame
596 is occupying some space.
599 <li>First, try to get frame info for the frame. If it is found, return
600 an error since the frame is already associated with a region in the Space
602 <li>Next, create a rect from the occupied space passed in by the caller,
603 transforming it first to world-coordinates by adding the Space Manager's
604 offset to the occupied space rect passed in.
</li>
605 <li>Create a new Frame Info instance for the frame and rect, returning
606 an error if allocation fails.
</li>
607 <li>Check if the occupied space rect (adjusted) is empty, if so, return
608 an error
(
<font color=
"#cc0000">NOTE: this could be done earlier, or
609 prevented by the caller
</font>)
</li>
610 <li>Allocate a new BandRect instance with the rect and frame as constructor
611 arguments, and insert it into the collection via InsertBandRect
</li>
613 <h5>InsertBandRect:
</h5>
614 Internal method to insert a band rect into the BandList in the correct location.
615 There are several cases it has to handle, as specified in the source file
618 <pre>// When comparing a rect to a band there are seven cases to consider.
619 // 'R' is the rect and 'B' is the band.
621 // Case
1 Case
2 Case
3 Case
4
622 // ------ ------ ------ ------
623 // +-----+ +-----+ +-----+ +-----+
624 // | R | | R | +-----+ +-----+ | | | |
625 // +-----+ +-----+ | | | R | | B | | B |
626 // +-----+ | B | +-----+ | | +-----+ | |
627 // | | | | +-----+ | R | +-----+
628 // | B | +-----+ +-----+
634 // Case
5 Case
6 Case
7
635 // ------ ------ ------
636 // +-----+ +-----+ +-----+ +-----+
637 // | | | R | | B | | | +-----+
638 // | B | +-----+ +-----+ | R | | B |
647 <li>First, check for the easiest case, where there are no existing band
648 rects, or the band rect passed in is below the bottommost rect. In this case,
649 just append the band rect and return.
</li>
650 <li>Starting at the head of the list of bandRects, check for intersection
651 with the rect passed in:
</li>
653 <li>case #
1: the rect is totally above the current band rect, so insert
654 a new band rect before the current bandRect
</li>
655 <li>cases #
2 and #
7: the rect is partially above the band rect, so it
656 is divided into two bandRects, one entirely above the band, and one containing
657 the remainder of the rect.
Insert the part that is totally above the
658 bandRect before the current bandRect, as in case #
1 above, and adjust the
659 other band rect to exclude the part already added.
</li>
660 <li>case #
5: the rect is totally below the current bandRect, so just
661 skip to the next band
</li>
662 <li>case #
3 and #
4: rect is at least partially intersection with the
663 bandRect, so divide the current band into two parts, where the top part is
664 above the current rect.
Move to the new band just created, which is
666 <li>case #
6: the rect shares the bottom and height with the bandRect,
667 so just add the rect to the band.
</li>
668 <li>case #
4 and #
7: create a new rect for the part that overlaps the
669 bandRect, and add it to the current bandRect (similar to case #
6) and then
670 move on to the next band, removing that part from the rect passed in.
If
671 no more bands, append the rect passed in to the end of the bandRect list.
</li>
674 <i>This algorithm is pretty confusing - basically what needs to happen is
675 that rects and bands need to be divided up so that complicated cases like
676 #
2, #
4, and #
7, are reduced to simpler cases where the rects is totally above,
677 below, or between a band rect.
From the current implementation, it
678 might be worth verifying that the final result of the inserts is a correctly
679 ordered liest of bandRects (debug mode only).
</i>
682 <h4>Algorithm
3: RemoveRegion
</h4>
683 When a float is removed, the Space Manager is notified by a call to RemoveRegion,
684 passing in the frame that is being removed.
687 <li>Get the FrameInfo for the frame passed in. If not found, an error is
689 <li>If the rect for the frame is not empty, then visit each band in the
692 <li>for each rect in the band:
698 <li>if the bandRect is occupied by the frame, either remove the frame
699 from the bandRect (if there are other frames sharing it) and remember that
701 <li>otherwise simply remove the bandRect (no other frames share it).
</li>
702 <li>if the bandRect was shared, then try to coalesce adjacent bandRects
</li>
704 <li>if the previous bandRect is directly next to the current bandRect,
705 and they have the same frame list, then make the current bandRect cover the
706 previous bandRect's full region (adjust the left edge to be that of the previous
707 bandRect) and remove the previous bandRect.
</li>
712 <li>if the current band or prior band had a rect occupied byu the frame,
713 then try to join the two bands via JoinBands
</li>
715 <li>Finally, destroy the frameInfo for the frame.
722 <hr width=
"100%" size=
"2">
723 <h2>Cross-Component Algorithms
</h2>
729 <hr width=
"100%" size=
"2">