Home | History | Annotate | Download | only in rendering
      1 /*
      2  * (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  * (C) 2000 Dirk Mueller (mueller (at) kde.org)
      4  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Library General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Library General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Library General Public License
     17  * along with this library; see the file COPYING.LIB.  If not, write to
     18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  * Boston, MA 02110-1301, USA.
     20  *
     21  */
     22 
     23 #ifndef RenderText_h
     24 #define RenderText_h
     25 
     26 #include "RenderObject.h"
     27 #include <wtf/Forward.h>
     28 
     29 namespace WebCore {
     30 
     31 class InlineTextBox;
     32 
     33 class RenderText : public RenderObject {
     34 public:
     35     RenderText(Node*, PassRefPtr<StringImpl>);
     36 #ifndef NDEBUG
     37     virtual ~RenderText();
     38 #endif
     39 
     40     virtual const char* renderName() const;
     41 
     42     virtual bool isTextFragment() const;
     43     virtual bool isWordBreak() const;
     44 
     45     virtual PassRefPtr<StringImpl> originalText() const;
     46 
     47     void extractTextBox(InlineTextBox*);
     48     void attachTextBox(InlineTextBox*);
     49     void removeTextBox(InlineTextBox*);
     50 
     51     virtual void destroy();
     52 
     53     StringImpl* text() const { return m_text.impl(); }
     54     String textWithoutTranscoding() const;
     55 
     56     InlineTextBox* createInlineTextBox();
     57     void dirtyLineBoxes(bool fullLayout);
     58 
     59     virtual void absoluteRects(Vector<IntRect>&, int tx, int ty);
     60     void absoluteRectsForRange(Vector<IntRect>&, unsigned startOffset = 0, unsigned endOffset = UINT_MAX, bool useSelectionHeight = false);
     61 
     62     virtual void absoluteQuads(Vector<FloatQuad>&);
     63     void absoluteQuadsForRange(Vector<FloatQuad>&, unsigned startOffset = 0, unsigned endOffset = UINT_MAX, bool useSelectionHeight = false);
     64 
     65     enum ClippingOption { NoClipping, ClipToEllipsis };
     66     void absoluteQuads(Vector<FloatQuad>&, ClippingOption option = NoClipping);
     67 
     68     virtual VisiblePosition positionForPoint(const IntPoint&);
     69 
     70     const UChar* characters() const { return m_text.characters(); }
     71     unsigned textLength() const { return m_text.length(); } // non virtual implementation of length()
     72     void positionLineBox(InlineBox*);
     73 
     74     virtual float width(unsigned from, unsigned len, const Font&, float xPos, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
     75     virtual float width(unsigned from, unsigned len, float xPos, bool firstLine = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
     76 
     77     float minLogicalWidth() const;
     78     float maxLogicalWidth() const;
     79 
     80     void trimmedPrefWidths(float leadWidth,
     81                            float& beginMinW, bool& beginWS,
     82                            float& endMinW, bool& endWS,
     83                            bool& hasBreakableChar, bool& hasBreak,
     84                            float& beginMaxW, float& endMaxW,
     85                            float& minW, float& maxW, bool& stripFrontSpaces);
     86 
     87     virtual IntRect linesBoundingBox() const;
     88     IntRect linesVisualOverflowBoundingBox() const;
     89 
     90     FloatPoint firstRunOrigin() const;
     91     float firstRunX() const;
     92     float firstRunY() const;
     93 
     94     void setText(PassRefPtr<StringImpl>, bool force = false);
     95     void setTextWithOffset(PassRefPtr<StringImpl>, unsigned offset, unsigned len, bool force = false);
     96 
     97     virtual bool canBeSelectionLeaf() const { return true; }
     98     virtual void setSelectionState(SelectionState s);
     99     virtual IntRect selectionRectForRepaint(RenderBoxModelObject* repaintContainer, bool clipToVisibleContent = true);
    100     virtual IntRect localCaretRect(InlineBox*, int caretOffset, int* extraWidthToEndOfLine = 0);
    101 
    102     virtual int marginLeft() const { return style()->marginLeft().calcMinValue(0); }
    103     virtual int marginRight() const { return style()->marginRight().calcMinValue(0); }
    104 
    105     virtual IntRect clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer);
    106 
    107     InlineTextBox* firstTextBox() const { return m_firstTextBox; }
    108     InlineTextBox* lastTextBox() const { return m_lastTextBox; }
    109 
    110     virtual int caretMinOffset() const;
    111     virtual int caretMaxOffset() const;
    112     virtual unsigned caretMaxRenderedOffset() const;
    113 
    114     virtual int previousOffset(int current) const;
    115     virtual int previousOffsetForBackwardDeletion(int current) const;
    116     virtual int nextOffset(int current) const;
    117 
    118     bool containsReversedText() const { return m_containsReversedText; }
    119 
    120     bool isSecure() const { return style()->textSecurity() != TSNONE; }
    121     void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);
    122 
    123     InlineTextBox* findNextInlineTextBox(int offset, int& pos) const;
    124 
    125     bool allowTabs() const { return !style()->collapseWhiteSpace(); }
    126 
    127     void checkConsistency() const;
    128 
    129     virtual void computePreferredLogicalWidths(float leadWidth);
    130     bool isAllCollapsibleWhitespace();
    131 
    132     bool knownToHaveNoOverflowAndNoFallbackFonts() const { return m_knownToHaveNoOverflowAndNoFallbackFonts; }
    133 
    134     void removeAndDestroyTextBoxes();
    135 
    136 protected:
    137     virtual void styleWillChange(StyleDifference, const RenderStyle*) { }
    138     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
    139 
    140     virtual void setTextInternal(PassRefPtr<StringImpl>);
    141     virtual UChar previousCharacter() const;
    142 
    143     virtual InlineTextBox* createTextBox(); // Subclassed by SVG.
    144 
    145 private:
    146     void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
    147 
    148     // Make length() private so that callers that have a RenderText*
    149     // will use the more efficient textLength() instead, while
    150     // callers with a RenderObject* can continue to use length().
    151     virtual unsigned length() const { return textLength(); }
    152 
    153     virtual void paint(PaintInfo&, int, int) { ASSERT_NOT_REACHED(); }
    154     virtual void layout() { ASSERT_NOT_REACHED(); }
    155     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, int, int, int, int, HitTestAction) { ASSERT_NOT_REACHED(); return false; }
    156 
    157     void deleteTextBoxes();
    158     bool containsOnlyWhitespace(unsigned from, unsigned len) const;
    159     float widthFromCache(const Font&, int start, int len, float xPos, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*) const;
    160     bool isAllASCII() const { return m_isAllASCII; }
    161     void updateNeedsTranscoding();
    162 
    163     inline void transformText(String&) const;
    164     void secureText(UChar mask);
    165 
    166     float m_minWidth; // here to minimize padding in 64-bit.
    167 
    168     String m_text;
    169 
    170     InlineTextBox* m_firstTextBox;
    171     InlineTextBox* m_lastTextBox;
    172 
    173     float m_maxWidth;
    174     float m_beginMinWidth;
    175     float m_endMinWidth;
    176 
    177     bool m_hasBreakableChar : 1; // Whether or not we can be broken into multiple lines.
    178     bool m_hasBreak : 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
    179     bool m_hasTab : 1; // Whether or not we have a variable width tab character (e.g., <pre> with '\t').
    180     bool m_hasBeginWS : 1; // Whether or not we begin with WS (only true if we aren't pre)
    181     bool m_hasEndWS : 1; // Whether or not we end with WS (only true if we aren't pre)
    182     bool m_linesDirty : 1; // This bit indicates that the text run has already dirtied specific
    183                            // line boxes, and this hint will enable layoutInlineChildren to avoid
    184                            // just dirtying everything when character data is modified (e.g., appended/inserted
    185                            // or removed).
    186     bool m_containsReversedText : 1;
    187     bool m_isAllASCII : 1;
    188     mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
    189     bool m_needsTranscoding : 1;
    190 };
    191 
    192 inline RenderText* toRenderText(RenderObject* object)
    193 {
    194     ASSERT(!object || object->isText());
    195     return static_cast<RenderText*>(object);
    196 }
    197 
    198 inline const RenderText* toRenderText(const RenderObject* object)
    199 {
    200     ASSERT(!object || object->isText());
    201     return static_cast<const RenderText*>(object);
    202 }
    203 
    204 // This will catch anyone doing an unnecessary cast.
    205 void toRenderText(const RenderText*);
    206 
    207 #ifdef NDEBUG
    208 inline void RenderText::checkConsistency() const
    209 {
    210 }
    211 #endif
    212 
    213 } // namespace WebCore
    214 
    215 #endif // RenderText_h
    216