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/platform/text/TextDirection.h"
     25 #include "core/rendering/RenderBoxModelObject.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 public:
    105 #ifndef NDEBUG
    106     void showTreeForThis() const;
    107     void showLineTreeForThis() const;
    108 
    109     virtual void showBox(int = 0) const;
    110     virtual void showLineTreeAndMark(const InlineBox* = 0, const char* = 0, const InlineBox* = 0, const char* = 0, const RenderObject* = 0, int = 0) const;
    111     virtual const char* boxName() const;
    112 #endif
    113 
    114     bool isText() const { return m_bitfields.isText(); }
    115     void setIsText(bool isText) { m_bitfields.setIsText(isText); }
    116 
    117     virtual bool isInlineFlowBox() const { return false; }
    118     virtual bool isInlineTextBox() const { return false; }
    119     virtual bool isRootInlineBox() const { return false; }
    120 
    121     virtual bool isSVGInlineTextBox() const { return false; }
    122     virtual bool isSVGInlineFlowBox() const { return false; }
    123     virtual bool isSVGRootInlineBox() const { return false; }
    124 
    125     bool hasVirtualLogicalHeight() const { return m_bitfields.hasVirtualLogicalHeight(); }
    126     void setHasVirtualLogicalHeight() { m_bitfields.setHasVirtualLogicalHeight(true); }
    127     virtual float virtualLogicalHeight() const
    128     {
    129         ASSERT_NOT_REACHED();
    130         return 0;
    131     }
    132 
    133     bool isHorizontal() const { return m_bitfields.isHorizontal(); }
    134     void setIsHorizontal(bool isHorizontal) { m_bitfields.setIsHorizontal(isHorizontal); }
    135 
    136     virtual FloatRect calculateBoundaries() const
    137     {
    138         ASSERT_NOT_REACHED();
    139         return FloatRect();
    140     }
    141 
    142     bool isConstructed() { return m_bitfields.constructed(); }
    143     virtual void setConstructed() { m_bitfields.setConstructed(true); }
    144 
    145     void setExtracted(bool extracted = true) { m_bitfields.setExtracted(extracted); }
    146 
    147     void setFirstLineStyleBit(bool firstLine) { m_bitfields.setFirstLine(firstLine); }
    148     bool isFirstLineStyle() const { return m_bitfields.firstLine(); }
    149 
    150     void remove();
    151 
    152     InlineBox* nextOnLine() const { return m_next; }
    153     InlineBox* prevOnLine() const { return m_prev; }
    154     void setNextOnLine(InlineBox* next)
    155     {
    156         ASSERT(m_parent || !next);
    157         m_next = next;
    158     }
    159     void setPrevOnLine(InlineBox* prev)
    160     {
    161         ASSERT(m_parent || !prev);
    162         m_prev = prev;
    163     }
    164     bool nextOnLineExists() const;
    165 
    166     virtual bool isLeaf() const { return true; }
    167 
    168     InlineBox* nextLeafChild() const;
    169     InlineBox* prevLeafChild() const;
    170 
    171     // Helper functions for editing and hit-testing code.
    172     // FIXME: These two functions should be moved to RenderedPosition once the code to convert between
    173     // Position and inline box, offset pair is moved to RenderedPosition.
    174     InlineBox* nextLeafChildIgnoringLineBreak() const;
    175     InlineBox* prevLeafChildIgnoringLineBreak() const;
    176 
    177     RenderObject* renderer() const { return m_renderer; }
    178 
    179     InlineFlowBox* parent() const
    180     {
    181         ASSERT(!m_hasBadParent);
    182         return m_parent;
    183     }
    184     void setParent(InlineFlowBox* par) { m_parent = par; }
    185 
    186     const RootInlineBox* root() const;
    187     RootInlineBox* root();
    188 
    189     // x() is the left side of the box in the containing block's coordinate system.
    190     void setX(float x) { m_topLeft.setX(x); }
    191     float x() const { return m_topLeft.x(); }
    192     float left() const { return m_topLeft.x(); }
    193 
    194     // y() is the top side of the box in the containing block's coordinate system.
    195     void setY(float y) { m_topLeft.setY(y); }
    196     float y() const { return m_topLeft.y(); }
    197     float top() const { return m_topLeft.y(); }
    198 
    199     const FloatPoint& topLeft() const { return m_topLeft; }
    200 
    201     float width() const { return isHorizontal() ? logicalWidth() : logicalHeight(); }
    202     float height() const { return isHorizontal() ? logicalHeight() : logicalWidth(); }
    203     FloatSize size() const { return FloatSize(width(), height()); }
    204     float right() const { return left() + width(); }
    205     float bottom() const { return top() + height(); }
    206 
    207     // The logicalLeft position is the left edge of the line box in a horizontal line and the top edge in a vertical line.
    208     float logicalLeft() const { return isHorizontal() ? m_topLeft.x() : m_topLeft.y(); }
    209     float logicalRight() const { return logicalLeft() + logicalWidth(); }
    210     void setLogicalLeft(float left)
    211     {
    212         if (isHorizontal())
    213             setX(left);
    214         else
    215             setY(left);
    216     }
    217     int pixelSnappedLogicalLeft() const { return logicalLeft(); }
    218     int pixelSnappedLogicalRight() const { return ceilf(logicalRight()); }
    219     int pixelSnappedLogicalTop() const { return logicalTop(); }
    220     int pixelSnappedLogicalBottom() const { return ceilf(logicalBottom()); }
    221 
    222     // The logicalTop[ position is the top edge of the line box in a horizontal line and the left edge in a vertical line.
    223     float logicalTop() const { return isHorizontal() ? m_topLeft.y() : m_topLeft.x(); }
    224     float logicalBottom() const { return logicalTop() + logicalHeight(); }
    225     void setLogicalTop(float top)
    226     {
    227         if (isHorizontal())
    228             setY(top);
    229         else
    230             setX(top);
    231     }
    232 
    233     // The logical width is our extent in the line's overall inline direction, i.e., width for horizontal text and height for vertical text.
    234     void setLogicalWidth(float w) { m_logicalWidth = w; }
    235     float logicalWidth() const { return m_logicalWidth; }
    236 
    237     // The logical height is our extent in the block flow direction, i.e., height for horizontal text and width for vertical text.
    238     float logicalHeight() const;
    239 
    240     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()); }
    241 
    242     virtual int baselinePosition(FontBaseline baselineType) const;
    243     virtual LayoutUnit lineHeight() const;
    244 
    245     virtual int caretMinOffset() const;
    246     virtual int caretMaxOffset() const;
    247 
    248     unsigned char bidiLevel() const { return m_bitfields.bidiEmbeddingLevel(); }
    249     void setBidiLevel(unsigned char level) { m_bitfields.setBidiEmbeddingLevel(level); }
    250     TextDirection direction() const { return bidiLevel() % 2 ? RTL : LTR; }
    251     bool isLeftToRightDirection() const { return direction() == LTR; }
    252     int caretLeftmostOffset() const { return isLeftToRightDirection() ? caretMinOffset() : caretMaxOffset(); }
    253     int caretRightmostOffset() const { return isLeftToRightDirection() ? caretMaxOffset() : caretMinOffset(); }
    254 
    255     virtual void clearTruncation() { }
    256 
    257     bool isDirty() const { return m_bitfields.dirty(); }
    258     virtual void markDirty(bool dirty = true) { m_bitfields.setDirty(dirty); }
    259 
    260     virtual void dirtyLineBoxes();
    261 
    262     virtual RenderObject::SelectionState selectionState();
    263 
    264     virtual bool canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth) const;
    265     // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
    266     virtual float placeEllipsisBox(bool ltr, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool&);
    267 
    268 #ifndef NDEBUG
    269     void setHasBadParent();
    270 #endif
    271 
    272     int expansion() const { return m_bitfields.expansion(); }
    273 
    274     bool visibleToHitTestRequest(const HitTestRequest& request) const { return renderer()->visibleToHitTestRequest(request); }
    275 
    276     EVerticalAlign verticalAlign() const { return renderer()->style(m_bitfields.firstLine())->verticalAlign(); }
    277 
    278     // Use with caution! The type is not checked!
    279     RenderBoxModelObject* boxModelObject() const
    280     {
    281         if (!m_renderer->isText())
    282             return toRenderBoxModelObject(m_renderer);
    283         return 0;
    284     }
    285 
    286     FloatPoint locationIncludingFlipping();
    287     void flipForWritingMode(FloatRect&);
    288     FloatPoint flipForWritingMode(const FloatPoint&);
    289     void flipForWritingMode(LayoutRect&);
    290     LayoutPoint flipForWritingMode(const LayoutPoint&);
    291 
    292     bool knownToHaveNoOverflow() const { return m_bitfields.knownToHaveNoOverflow(); }
    293     void clearKnownToHaveNoOverflow();
    294 
    295     bool dirOverride() const { return m_bitfields.dirOverride(); }
    296     void setDirOverride(bool dirOverride) { m_bitfields.setDirOverride(dirOverride); }
    297 
    298 private:
    299     InlineBox* m_next; // The next element on the same line as us.
    300     InlineBox* m_prev; // The previous element on the same line as us.
    301 
    302     InlineFlowBox* m_parent; // The box that contains us.
    303 
    304 public:
    305     RenderObject* m_renderer;
    306 
    307     FloatPoint m_topLeft;
    308     float m_logicalWidth;
    309 
    310 #define ADD_BOOLEAN_BITFIELD(name, Name) \
    311     private:\
    312     unsigned m_##name : 1;\
    313     public:\
    314     bool name() const { return m_##name; }\
    315     void set##Name(bool name) { m_##name = name; }\
    316 
    317     class InlineBoxBitfields {
    318     public:
    319         InlineBoxBitfields(bool firstLine = false, bool constructed = false, bool dirty = false, bool extracted = false, bool isHorizontal = true)
    320             : m_firstLine(firstLine)
    321             , m_constructed(constructed)
    322             , m_bidiEmbeddingLevel(0)
    323             , m_dirty(dirty)
    324             , m_extracted(extracted)
    325             , m_hasVirtualLogicalHeight(false)
    326             , m_isHorizontal(isHorizontal)
    327             , m_endsWithBreak(false)
    328             , m_hasSelectedChildrenOrCanHaveLeadingExpansion(false)
    329             , m_knownToHaveNoOverflow(true)
    330             , m_hasEllipsisBoxOrHyphen(false)
    331             , m_dirOverride(false)
    332             , m_isText(false)
    333             , m_determinedIfNextOnLineExists(false)
    334             , m_nextOnLineExists(false)
    335             , m_expansion(0)
    336         {
    337         }
    338 
    339         // Some of these bits are actually for subclasses and moved here to compact the structures.
    340         // for this class
    341         ADD_BOOLEAN_BITFIELD(firstLine, FirstLine);
    342         ADD_BOOLEAN_BITFIELD(constructed, Constructed);
    343 
    344     private:
    345         unsigned m_bidiEmbeddingLevel : 6; // The maximium bidi level is 62: http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
    346 
    347     public:
    348         unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel; }
    349         void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel) { m_bidiEmbeddingLevel = bidiEmbeddingLevel; }
    350 
    351         ADD_BOOLEAN_BITFIELD(dirty, Dirty);
    352         ADD_BOOLEAN_BITFIELD(extracted, Extracted);
    353         ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight, HasVirtualLogicalHeight);
    354         ADD_BOOLEAN_BITFIELD(isHorizontal, IsHorizontal);
    355         // for RootInlineBox
    356         ADD_BOOLEAN_BITFIELD(endsWithBreak, EndsWithBreak); // Whether the line ends with a <br>.
    357         // shared between RootInlineBox and InlineTextBox
    358         ADD_BOOLEAN_BITFIELD(hasSelectedChildrenOrCanHaveLeadingExpansion, HasSelectedChildrenOrCanHaveLeadingExpansion);
    359         ADD_BOOLEAN_BITFIELD(knownToHaveNoOverflow, KnownToHaveNoOverflow);
    360         ADD_BOOLEAN_BITFIELD(hasEllipsisBoxOrHyphen, HasEllipsisBoxOrHyphen);
    361         // for InlineTextBox
    362         ADD_BOOLEAN_BITFIELD(dirOverride, DirOverride);
    363         ADD_BOOLEAN_BITFIELD(isText, IsText); // Whether or not this object represents text with a non-zero height. Includes non-image list markers, text boxes.
    364 
    365     private:
    366         mutable unsigned m_determinedIfNextOnLineExists : 1;
    367 
    368     public:
    369         bool determinedIfNextOnLineExists() const { return m_determinedIfNextOnLineExists; }
    370         void setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists) const { m_determinedIfNextOnLineExists = determinedIfNextOnLineExists; }
    371 
    372     private:
    373         mutable unsigned m_nextOnLineExists : 1;
    374 
    375     public:
    376         bool nextOnLineExists() const { return m_nextOnLineExists; }
    377         void setNextOnLineExists(bool nextOnLineExists) const { m_nextOnLineExists = nextOnLineExists; }
    378 
    379     private:
    380         signed m_expansion : 12; // for justified text
    381 
    382     public:
    383         signed expansion() const { return m_expansion; }
    384         void setExpansion(signed expansion) { m_expansion = expansion; }
    385     };
    386 #undef ADD_BOOLEAN_BITFIELD
    387 
    388 private:
    389     InlineBoxBitfields m_bitfields;
    390 
    391 protected:
    392     // For RootInlineBox
    393     bool endsWithBreak() const { return m_bitfields.endsWithBreak(); }
    394     void setEndsWithBreak(bool endsWithBreak) { m_bitfields.setEndsWithBreak(endsWithBreak); }
    395     bool hasEllipsisBox() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
    396     bool hasSelectedChildren() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
    397     void setHasSelectedChildren(bool hasSelectedChildren) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(hasSelectedChildren); }
    398     void setHasEllipsisBox(bool hasEllipsisBox) { m_bitfields.setHasEllipsisBoxOrHyphen(hasEllipsisBox); }
    399 
    400     // For InlineTextBox
    401     bool hasHyphen() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
    402     void setHasHyphen(bool hasHyphen) { m_bitfields.setHasEllipsisBoxOrHyphen(hasHyphen); }
    403     bool canHaveLeadingExpansion() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
    404     void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(canHaveLeadingExpansion); }
    405     signed expansion() { return m_bitfields.expansion(); }
    406     void setExpansion(signed expansion) { m_bitfields.setExpansion(expansion); }
    407 
    408     // For InlineFlowBox and InlineTextBox
    409     bool extracted() const { return m_bitfields.extracted(); }
    410 
    411 #ifndef NDEBUG
    412 private:
    413     bool m_hasBadParent;
    414 #endif
    415 };
    416 
    417 #ifdef NDEBUG
    418 inline InlineBox::~InlineBox()
    419 {
    420 }
    421 #endif
    422 
    423 #ifndef NDEBUG
    424 inline void InlineBox::setHasBadParent()
    425 {
    426     m_hasBadParent = true;
    427 }
    428 #endif
    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