Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #ifndef InlineBox_h
     22 #define InlineBox_h
     23 
     24 #include "core/rendering/RenderBoxModelObject.h"
     25 #include "platform/text/TextDirection.h"
     26 
     27 namespace WebCore {
     28 
     29 class HitTestRequest;
     30 class HitTestResult;
     31 class RootInlineBox;
     32 
     33 // InlineBox represents a rectangle that occurs on a line.  It corresponds to
     34 // some RenderObject (i.e., it represents a portion of that RenderObject).
     35 class InlineBox {
     36 public:
     37     InlineBox(RenderObject* obj)
     38         : m_next(0)
     39         , m_prev(0)
     40         , m_parent(0)
     41         , m_renderer(obj)
     42         , m_logicalWidth(0)
     43 #ifndef NDEBUG
     44         , m_hasBadParent(false)
     45 #endif
     46     {
     47     }
     48 
     49     InlineBox(RenderObject* obj, FloatPoint topLeft, float logicalWidth, bool firstLine, bool constructed,
     50               bool dirty, bool extracted, bool isHorizontal, InlineBox* next, InlineBox* prev, InlineFlowBox* parent)
     51         : m_next(next)
     52         , m_prev(prev)
     53         , m_parent(parent)
     54         , m_renderer(obj)
     55         , m_topLeft(topLeft)
     56         , m_logicalWidth(logicalWidth)
     57         , m_bitfields(firstLine, constructed, dirty, extracted, isHorizontal)
     58 #ifndef NDEBUG
     59         , m_hasBadParent(false)
     60 #endif
     61     {
     62     }
     63 
     64     virtual ~InlineBox();
     65 
     66     virtual void destroy() { delete this; }
     67 
     68     virtual void deleteLine();
     69     virtual void extractLine();
     70     virtual void attachLine();
     71 
     72     virtual bool isLineBreak() const { return false; }
     73 
     74     virtual void adjustPosition(float dx, float dy);
     75     void adjustLogicalPosition(float deltaLogicalLeft, float deltaLogicalTop)
     76     {
     77         if (isHorizontal())
     78             adjustPosition(deltaLogicalLeft, deltaLogicalTop);
     79         else
     80             adjustPosition(deltaLogicalTop, deltaLogicalLeft);
     81     }
     82     void adjustLineDirectionPosition(float delta)
     83     {
     84         if (isHorizontal())
     85             adjustPosition(delta, 0);
     86         else
     87             adjustPosition(0, delta);
     88     }
     89     void adjustBlockDirectionPosition(float delta)
     90     {
     91         if (isHorizontal())
     92             adjustPosition(0, delta);
     93         else
     94             adjustPosition(delta, 0);
     95     }
     96 
     97     virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom);
     98     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom);
     99 
    100     // InlineBoxes are allocated out of the rendering partition.
    101     void* operator new(size_t);
    102     void operator delete(void*);
    103 
    104 #ifndef NDEBUG
    105     void showTreeForThis() const;
    106     void showLineTreeForThis() const;
    107 
    108     virtual void showBox(int = 0) const;
    109     virtual void showLineTreeAndMark(const InlineBox* = 0, const char* = 0, const InlineBox* = 0, const char* = 0, const RenderObject* = 0, int = 0) const;
    110     virtual const char* boxName() const;
    111 #endif
    112 
    113     bool isText() const { return m_bitfields.isText(); }
    114     void setIsText(bool isText) { m_bitfields.setIsText(isText); }
    115 
    116     virtual bool isInlineFlowBox() const { return false; }
    117     virtual bool isInlineTextBox() const { return false; }
    118     virtual bool isRootInlineBox() const { return false; }
    119 
    120     virtual bool isSVGInlineTextBox() const { return false; }
    121     virtual bool isSVGInlineFlowBox() const { return false; }
    122     virtual bool isSVGRootInlineBox() const { return false; }
    123 
    124     bool hasVirtualLogicalHeight() const { return m_bitfields.hasVirtualLogicalHeight(); }
    125     void setHasVirtualLogicalHeight() { m_bitfields.setHasVirtualLogicalHeight(true); }
    126     virtual float virtualLogicalHeight() const
    127     {
    128         ASSERT_NOT_REACHED();
    129         return 0;
    130     }
    131 
    132     bool isHorizontal() const { return m_bitfields.isHorizontal(); }
    133     void setIsHorizontal(bool isHorizontal) { m_bitfields.setIsHorizontal(isHorizontal); }
    134 
    135     virtual FloatRect calculateBoundaries() const
    136     {
    137         ASSERT_NOT_REACHED();
    138         return FloatRect();
    139     }
    140 
    141     bool isConstructed() { return m_bitfields.constructed(); }
    142     virtual void setConstructed() { m_bitfields.setConstructed(true); }
    143 
    144     void setExtracted(bool extracted = true) { m_bitfields.setExtracted(extracted); }
    145 
    146     void setFirstLineStyleBit(bool firstLine) { m_bitfields.setFirstLine(firstLine); }
    147     bool isFirstLineStyle() const { return m_bitfields.firstLine(); }
    148 
    149     void remove();
    150 
    151     InlineBox* nextOnLine() const { return m_next; }
    152     InlineBox* prevOnLine() const { return m_prev; }
    153     void setNextOnLine(InlineBox* next)
    154     {
    155         ASSERT(m_parent || !next);
    156         m_next = next;
    157     }
    158     void setPrevOnLine(InlineBox* prev)
    159     {
    160         ASSERT(m_parent || !prev);
    161         m_prev = prev;
    162     }
    163     bool nextOnLineExists() const;
    164 
    165     virtual bool isLeaf() const { return true; }
    166 
    167     InlineBox* nextLeafChild() const;
    168     InlineBox* prevLeafChild() const;
    169 
    170     // Helper functions for editing and hit-testing code.
    171     // FIXME: These two functions should be moved to RenderedPosition once the code to convert between
    172     // Position and inline box, offset pair is moved to RenderedPosition.
    173     InlineBox* nextLeafChildIgnoringLineBreak() const;
    174     InlineBox* prevLeafChildIgnoringLineBreak() const;
    175 
    176     RenderObject* renderer() const { return m_renderer; }
    177 
    178     InlineFlowBox* parent() const
    179     {
    180         ASSERT(!m_hasBadParent);
    181         return m_parent;
    182     }
    183     void setParent(InlineFlowBox* par) { m_parent = par; }
    184 
    185     const RootInlineBox* root() const;
    186     RootInlineBox* root();
    187 
    188     // x() is the left side of the box in the containing block's coordinate system.
    189     void setX(float x) { m_topLeft.setX(x); }
    190     float x() const { return m_topLeft.x(); }
    191     float left() const { return m_topLeft.x(); }
    192 
    193     // y() is the top side of the box in the containing block's coordinate system.
    194     void setY(float y) { m_topLeft.setY(y); }
    195     float y() const { return m_topLeft.y(); }
    196     float top() const { return m_topLeft.y(); }
    197 
    198     const FloatPoint& topLeft() const { return m_topLeft; }
    199 
    200     float width() const { return isHorizontal() ? logicalWidth() : hasVirtualLogicalHeight() ? virtualLogicalHeight() : logicalHeight(); }
    201     float height() const { return isHorizontal() ? hasVirtualLogicalHeight() ? virtualLogicalHeight() : logicalHeight() : logicalWidth(); }
    202     FloatSize size() const { return FloatSize(width(), height()); }
    203     float right() const { return left() + width(); }
    204     float bottom() const { return top() + height(); }
    205 
    206     // The logicalLeft position is the left edge of the line box in a horizontal line and the top edge in a vertical line.
    207     float logicalLeft() const { return isHorizontal() ? m_topLeft.x() : m_topLeft.y(); }
    208     float logicalRight() const { return logicalLeft() + logicalWidth(); }
    209     void setLogicalLeft(float left)
    210     {
    211         if (isHorizontal())
    212             setX(left);
    213         else
    214             setY(left);
    215     }
    216     int pixelSnappedLogicalLeft() const { return logicalLeft(); }
    217     int pixelSnappedLogicalRight() const { return ceilf(logicalRight()); }
    218     int pixelSnappedLogicalTop() const { return logicalTop(); }
    219     int pixelSnappedLogicalBottom() const { return ceilf(logicalBottom()); }
    220 
    221     // The logicalTop[ position is the top edge of the line box in a horizontal line and the left edge in a vertical line.
    222     float logicalTop() const { return isHorizontal() ? m_topLeft.y() : m_topLeft.x(); }
    223     float logicalBottom() const { return logicalTop() + logicalHeight(); }
    224     void setLogicalTop(float top)
    225     {
    226         if (isHorizontal())
    227             setY(top);
    228         else
    229             setX(top);
    230     }
    231 
    232     // The logical width is our extent in the line's overall inline direction, i.e., width for horizontal text and height for vertical text.
    233     void setLogicalWidth(float w) { m_logicalWidth = w; }
    234     float logicalWidth() const { return m_logicalWidth; }
    235 
    236     // The logical height is our extent in the block flow direction, i.e., height for horizontal text and width for vertical text.
    237     float logicalHeight() const;
    238 
    239     FloatRect logicalFrameRect() const { return isHorizontal() ? FloatRect(m_topLeft.x(), m_topLeft.y(), m_logicalWidth, logicalHeight()) : FloatRect(m_topLeft.y(), m_topLeft.x(), m_logicalWidth, logicalHeight()); }
    240 
    241     virtual int baselinePosition(FontBaseline baselineType) const;
    242     virtual LayoutUnit lineHeight() const;
    243 
    244     virtual int caretMinOffset() const;
    245     virtual int caretMaxOffset() const;
    246 
    247     unsigned char bidiLevel() const { return m_bitfields.bidiEmbeddingLevel(); }
    248     void setBidiLevel(unsigned char level) { m_bitfields.setBidiEmbeddingLevel(level); }
    249     TextDirection direction() const { return bidiLevel() % 2 ? RTL : LTR; }
    250     bool isLeftToRightDirection() const { return direction() == LTR; }
    251     int caretLeftmostOffset() const { return isLeftToRightDirection() ? caretMinOffset() : caretMaxOffset(); }
    252     int caretRightmostOffset() const { return isLeftToRightDirection() ? caretMaxOffset() : caretMinOffset(); }
    253 
    254     virtual void clearTruncation() { }
    255 
    256     bool isDirty() const { return m_bitfields.dirty(); }
    257     virtual void markDirty(bool dirty = true) { m_bitfields.setDirty(dirty); }
    258 
    259     virtual void dirtyLineBoxes();
    260 
    261     virtual RenderObject::SelectionState selectionState();
    262 
    263     virtual bool canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth) const;
    264     // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
    265     virtual float placeEllipsisBox(bool ltr, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool&);
    266 
    267 #ifndef NDEBUG
    268     void setHasBadParent();
    269 #endif
    270 
    271     int expansion() const { return m_bitfields.expansion(); }
    272 
    273     bool visibleToHitTestRequest(const HitTestRequest& request) const { return renderer()->visibleToHitTestRequest(request); }
    274 
    275     EVerticalAlign verticalAlign() const { return renderer()->style(m_bitfields.firstLine())->verticalAlign(); }
    276 
    277     // Use with caution! The type is not checked!
    278     RenderBoxModelObject* boxModelObject() const
    279     {
    280         if (!m_renderer->isText())
    281             return toRenderBoxModelObject(m_renderer);
    282         return 0;
    283     }
    284 
    285     FloatPoint locationIncludingFlipping();
    286     void flipForWritingMode(FloatRect&);
    287     FloatPoint flipForWritingMode(const FloatPoint&);
    288     void flipForWritingMode(LayoutRect&);
    289     LayoutPoint flipForWritingMode(const LayoutPoint&);
    290 
    291     bool knownToHaveNoOverflow() const { return m_bitfields.knownToHaveNoOverflow(); }
    292     void clearKnownToHaveNoOverflow();
    293 
    294     bool dirOverride() const { return m_bitfields.dirOverride(); }
    295     void setDirOverride(bool dirOverride) { m_bitfields.setDirOverride(dirOverride); }
    296 
    297 #define ADD_BOOLEAN_BITFIELD(name, Name) \
    298     private:\
    299     unsigned m_##name : 1;\
    300     public:\
    301     bool name() const { return m_##name; }\
    302     void set##Name(bool name) { m_##name = name; }\
    303 
    304     class InlineBoxBitfields {
    305     public:
    306         InlineBoxBitfields(bool firstLine = false, bool constructed = false, bool dirty = false, bool extracted = false, bool isHorizontal = true)
    307             : m_firstLine(firstLine)
    308             , m_constructed(constructed)
    309             , m_bidiEmbeddingLevel(0)
    310             , m_dirty(dirty)
    311             , m_extracted(extracted)
    312             , m_hasVirtualLogicalHeight(false)
    313             , m_isHorizontal(isHorizontal)
    314             , m_endsWithBreak(false)
    315             , m_hasSelectedChildrenOrCanHaveLeadingExpansion(false)
    316             , m_knownToHaveNoOverflow(true)
    317             , m_hasEllipsisBoxOrHyphen(false)
    318             , m_dirOverride(false)
    319             , m_isText(false)
    320             , m_determinedIfNextOnLineExists(false)
    321             , m_nextOnLineExists(false)
    322             , m_expansion(0)
    323         {
    324         }
    325 
    326         // Some of these bits are actually for subclasses and moved here to compact the structures.
    327         // for this class
    328         ADD_BOOLEAN_BITFIELD(firstLine, FirstLine);
    329         ADD_BOOLEAN_BITFIELD(constructed, Constructed);
    330 
    331     private:
    332         unsigned m_bidiEmbeddingLevel : 6; // The maximium bidi level is 62: http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
    333 
    334     public:
    335         unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel; }
    336         void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel) { m_bidiEmbeddingLevel = bidiEmbeddingLevel; }
    337 
    338         ADD_BOOLEAN_BITFIELD(dirty, Dirty);
    339         ADD_BOOLEAN_BITFIELD(extracted, Extracted);
    340         ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight, HasVirtualLogicalHeight);
    341         ADD_BOOLEAN_BITFIELD(isHorizontal, IsHorizontal);
    342         // for RootInlineBox
    343         ADD_BOOLEAN_BITFIELD(endsWithBreak, EndsWithBreak); // Whether the line ends with a <br>.
    344         // shared between RootInlineBox and InlineTextBox
    345         ADD_BOOLEAN_BITFIELD(hasSelectedChildrenOrCanHaveLeadingExpansion, HasSelectedChildrenOrCanHaveLeadingExpansion);
    346         ADD_BOOLEAN_BITFIELD(knownToHaveNoOverflow, KnownToHaveNoOverflow);
    347         ADD_BOOLEAN_BITFIELD(hasEllipsisBoxOrHyphen, HasEllipsisBoxOrHyphen);
    348         // for InlineTextBox
    349         ADD_BOOLEAN_BITFIELD(dirOverride, DirOverride);
    350         ADD_BOOLEAN_BITFIELD(isText, IsText); // Whether or not this object represents text with a non-zero height. Includes non-image list markers, text boxes.
    351 
    352     private:
    353         mutable unsigned m_determinedIfNextOnLineExists : 1;
    354 
    355     public:
    356         bool determinedIfNextOnLineExists() const { return m_determinedIfNextOnLineExists; }
    357         void setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists) const { m_determinedIfNextOnLineExists = determinedIfNextOnLineExists; }
    358 
    359     private:
    360         mutable unsigned m_nextOnLineExists : 1;
    361 
    362     public:
    363         bool nextOnLineExists() const { return m_nextOnLineExists; }
    364         void setNextOnLineExists(bool nextOnLineExists) const { m_nextOnLineExists = nextOnLineExists; }
    365 
    366     private:
    367         signed m_expansion : 12; // for justified text
    368 
    369     public:
    370         signed expansion() const { return m_expansion; }
    371         void setExpansion(signed expansion) { m_expansion = expansion; }
    372     };
    373 #undef ADD_BOOLEAN_BITFIELD
    374 
    375 private:
    376     InlineBox* m_next; // The next element on the same line as us.
    377     InlineBox* m_prev; // The previous element on the same line as us.
    378 
    379     InlineFlowBox* m_parent; // The box that contains us.
    380 
    381 protected:
    382     // For RootInlineBox
    383     bool endsWithBreak() const { return m_bitfields.endsWithBreak(); }
    384     void setEndsWithBreak(bool endsWithBreak) { m_bitfields.setEndsWithBreak(endsWithBreak); }
    385     bool hasEllipsisBox() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
    386     bool hasSelectedChildren() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
    387     void setHasSelectedChildren(bool hasSelectedChildren) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(hasSelectedChildren); }
    388     void setHasEllipsisBox(bool hasEllipsisBox) { m_bitfields.setHasEllipsisBoxOrHyphen(hasEllipsisBox); }
    389 
    390     // For InlineTextBox
    391     bool hasHyphen() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
    392     void setHasHyphen(bool hasHyphen) { m_bitfields.setHasEllipsisBoxOrHyphen(hasHyphen); }
    393     bool canHaveLeadingExpansion() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
    394     void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(canHaveLeadingExpansion); }
    395     signed expansion() { return m_bitfields.expansion(); }
    396     void setExpansion(signed expansion) { m_bitfields.setExpansion(expansion); }
    397 
    398     // For InlineFlowBox and InlineTextBox
    399     bool extracted() const { return m_bitfields.extracted(); }
    400 
    401     RenderObject* m_renderer;
    402 
    403     FloatPoint m_topLeft;
    404     float m_logicalWidth;
    405 
    406 private:
    407     InlineBoxBitfields m_bitfields;
    408 
    409 #ifndef NDEBUG
    410     bool m_hasBadParent;
    411 #endif
    412 };
    413 
    414 #ifdef NDEBUG
    415 inline InlineBox::~InlineBox()
    416 {
    417 }
    418 #endif
    419 
    420 #ifndef NDEBUG
    421 inline void InlineBox::setHasBadParent()
    422 {
    423     m_hasBadParent = true;
    424 }
    425 #endif
    426 
    427 #define DEFINE_INLINE_BOX_TYPE_CASTS(typeName) \
    428     DEFINE_TYPE_CASTS(typeName, InlineBox, box, box->is##typeName(), box.is##typeName())
    429 
    430 } // namespace WebCore
    431 
    432 #ifndef NDEBUG
    433 // Outside the WebCore namespace for ease of invocation from gdb.
    434 void showTree(const WebCore::InlineBox*);
    435 void showLineTree(const WebCore::InlineBox*);
    436 #endif
    437 
    438 #endif // InlineBox_h
    439