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 "core/rendering/RenderObject.h"
     27 #include "core/rendering/RenderView.h"
     28 #include "wtf/Forward.h"
     29 
     30 namespace WebCore {
     31 
     32 class InlineTextBox;
     33 
     34 class RenderText : public RenderObject {
     35 public:
     36     // FIXME: If the node argument is not a Text node or the string argument is
     37     // not the content of the Text node, updating text-transform property
     38     // doesn't re-transform the string.
     39     RenderText(Node*, PassRefPtr<StringImpl>);
     40 #ifndef NDEBUG
     41     virtual ~RenderText();
     42 #endif
     43 
     44     virtual const char* renderName() const;
     45 
     46     virtual bool isTextFragment() const;
     47     virtual bool isWordBreak() const;
     48 
     49     virtual PassRefPtr<StringImpl> originalText() const;
     50 
     51     void extractTextBox(InlineTextBox*);
     52     void attachTextBox(InlineTextBox*);
     53     void removeTextBox(InlineTextBox*);
     54 
     55     const String& text() const { return m_text; }
     56     String textWithoutTranscoding() const;
     57 
     58     InlineTextBox* createInlineTextBox();
     59     void dirtyLineBoxes(bool fullLayout);
     60 
     61     virtual void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const OVERRIDE FINAL;
     62     void absoluteRectsForRange(Vector<IntRect>&, unsigned startOffset = 0, unsigned endOffset = INT_MAX, bool useSelectionHeight = false, bool* wasFixed = 0);
     63 
     64     virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const OVERRIDE FINAL;
     65     void absoluteQuadsForRange(Vector<FloatQuad>&, unsigned startOffset = 0, unsigned endOffset = INT_MAX, bool useSelectionHeight = false, bool* wasFixed = 0);
     66 
     67     enum ClippingOption { NoClipping, ClipToEllipsis };
     68     void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed = 0, ClippingOption = NoClipping) const;
     69 
     70     virtual PositionWithAffinity positionForPoint(const LayoutPoint&) OVERRIDE;
     71 
     72     bool is8Bit() const { return m_text.is8Bit(); }
     73     const LChar* characters8() const { return m_text.impl()->characters8(); }
     74     const UChar* characters16() const { return m_text.impl()->characters16(); }
     75     bool hasEmptyText() const { return m_text.isEmpty(); }
     76     String substring(unsigned position, unsigned length) const { return m_text.substring(position, length); }
     77     UChar characterAt(unsigned) const;
     78     UChar uncheckedCharacterAt(unsigned) const;
     79     UChar operator[](unsigned i) const { return uncheckedCharacterAt(i); }
     80     unsigned textLength() const { return m_text.length(); } // non virtual implementation of length()
     81     void positionLineBox(InlineBox*);
     82 
     83     virtual float width(unsigned from, unsigned len, const Font&, float xPos, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
     84     virtual float width(unsigned from, unsigned len, float xPos, bool firstLine = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
     85 
     86     float minLogicalWidth() const;
     87     float maxLogicalWidth() const;
     88 
     89     void trimmedPrefWidths(float leadWidth,
     90         float& firstLineMinWidth, bool& hasBreakableStart,
     91         float& lastLineMinWidth, bool& hasBreakableEnd,
     92         bool& hasBreakableChar, bool& hasBreak,
     93         float& firstLineMaxWidth, float& lastLineMaxWidth,
     94         float& minWidth, float& maxWidth, bool& stripFrontSpaces);
     95 
     96     virtual IntRect linesBoundingBox() const;
     97     LayoutRect linesVisualOverflowBoundingBox() const;
     98 
     99     FloatPoint firstRunOrigin() const;
    100     float firstRunX() const;
    101     float firstRunY() const;
    102 
    103     virtual void setText(PassRefPtr<StringImpl>, bool force = false);
    104     void setTextWithOffset(PassRefPtr<StringImpl>, unsigned offset, unsigned len, bool force = false);
    105 
    106     virtual void transformText();
    107 
    108     virtual bool canBeSelectionLeaf() const { return true; }
    109     virtual void setSelectionState(SelectionState s) OVERRIDE FINAL;
    110     virtual LayoutRect selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent = true) OVERRIDE;
    111     virtual LayoutRect localCaretRect(InlineBox*, int caretOffset, LayoutUnit* extraWidthToEndOfLine = 0);
    112 
    113     LayoutUnit marginLeft() const { return minimumValueForLength(style()->marginLeft(), 0, view()); }
    114     LayoutUnit marginRight() const { return minimumValueForLength(style()->marginRight(), 0, view()); }
    115 
    116     virtual LayoutRect clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const OVERRIDE FINAL;
    117 
    118     InlineTextBox* firstTextBox() const { return m_firstTextBox; }
    119     InlineTextBox* lastTextBox() const { return m_lastTextBox; }
    120 
    121     virtual int caretMinOffset() const;
    122     virtual int caretMaxOffset() const;
    123     unsigned renderedTextLength() const;
    124 
    125     virtual int previousOffset(int current) const OVERRIDE FINAL;
    126     virtual int previousOffsetForBackwardDeletion(int current) const OVERRIDE FINAL;
    127     virtual int nextOffset(int current) const OVERRIDE FINAL;
    128 
    129     bool containsReversedText() const { return m_containsReversedText; }
    130 
    131     bool isSecure() const { return style()->textSecurity() != TSNONE; }
    132     void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);
    133 
    134     InlineTextBox* findNextInlineTextBox(int offset, int& pos) const;
    135 
    136     void checkConsistency() const;
    137 
    138     bool isAllCollapsibleWhitespace() const;
    139 
    140     bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath; }
    141     bool knownToHaveNoOverflowAndNoFallbackFonts() const { return m_knownToHaveNoOverflowAndNoFallbackFonts; }
    142 
    143     void removeAndDestroyTextBoxes();
    144 
    145 protected:
    146     virtual void computePreferredLogicalWidths(float leadWidth);
    147     virtual void willBeDestroyed();
    148 
    149     virtual void styleWillChange(StyleDifference, const RenderStyle*) OVERRIDE FINAL { }
    150     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
    151 
    152     virtual void setTextInternal(PassRefPtr<StringImpl>);
    153     virtual UChar previousCharacter() const;
    154 
    155     virtual void addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const OVERRIDE;
    156 
    157     virtual InlineTextBox* createTextBox(); // Subclassed by SVG.
    158 
    159 private:
    160     void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
    161 
    162     bool computeCanUseSimpleFontCodePath() const;
    163 
    164     // Make length() private so that callers that have a RenderText*
    165     // will use the more efficient textLength() instead, while
    166     // callers with a RenderObject* can continue to use length().
    167     virtual unsigned length() const OVERRIDE FINAL { return textLength(); }
    168 
    169     virtual void paint(PaintInfo&, const LayoutPoint&) OVERRIDE FINAL { ASSERT_NOT_REACHED(); }
    170     virtual void layout() OVERRIDE FINAL { ASSERT_NOT_REACHED(); }
    171     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction) OVERRIDE FINAL { ASSERT_NOT_REACHED(); return false; }
    172 
    173     void deleteTextBoxes();
    174     bool containsOnlyWhitespace(unsigned from, unsigned len) const;
    175     float widthFromCache(const Font&, int start, int len, float xPos, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*) const;
    176     bool isAllASCII() const { return m_isAllASCII; }
    177     void updateNeedsTranscoding();
    178 
    179     void secureText(UChar mask);
    180 
    181     // We put the bitfield first to minimize padding on 64-bit.
    182     bool m_hasBreakableChar : 1; // Whether or not we can be broken into multiple lines.
    183     bool m_hasBreak : 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
    184     bool m_hasTab : 1; // Whether or not we have a variable width tab character (e.g., <pre> with '\t').
    185     bool m_hasBreakableStart : 1;
    186     bool m_hasBreakableEnd : 1;
    187     bool m_hasEndWhiteSpace : 1;
    188     bool m_linesDirty : 1; // This bit indicates that the text run has already dirtied specific
    189                            // line boxes, and this hint will enable layoutInlineChildren to avoid
    190                            // just dirtying everything when character data is modified (e.g., appended/inserted
    191                            // or removed).
    192     bool m_containsReversedText : 1;
    193     bool m_isAllASCII : 1;
    194     bool m_canUseSimpleFontCodePath : 1;
    195     mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
    196     bool m_needsTranscoding : 1;
    197 
    198     float m_minWidth;
    199     float m_maxWidth;
    200     float m_firstLineMinWidth;
    201     float m_lastLineLineMinWidth;
    202 
    203     String m_text;
    204 
    205     InlineTextBox* m_firstTextBox;
    206     InlineTextBox* m_lastTextBox;
    207 };
    208 
    209 inline UChar RenderText::uncheckedCharacterAt(unsigned i) const
    210 {
    211     ASSERT_WITH_SECURITY_IMPLICATION(i < textLength());
    212     return is8Bit() ? characters8()[i] : characters16()[i];
    213 }
    214 
    215 inline UChar RenderText::characterAt(unsigned i) const
    216 {
    217     if (i >= textLength())
    218         return 0;
    219 
    220     return uncheckedCharacterAt(i);
    221 }
    222 
    223 inline RenderText* toRenderText(RenderObject* object)
    224 {
    225     ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isText());
    226     return static_cast<RenderText*>(object);
    227 }
    228 
    229 inline const RenderText* toRenderText(const RenderObject* object)
    230 {
    231     ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isText());
    232     return static_cast<const RenderText*>(object);
    233 }
    234 
    235 // This will catch anyone doing an unnecessary cast.
    236 void toRenderText(const RenderText*);
    237 
    238 #ifdef NDEBUG
    239 inline void RenderText::checkConsistency() const
    240 {
    241 }
    242 #endif
    243 
    244 void applyTextTransform(const RenderStyle*, String&, UChar);
    245 
    246 } // namespace WebCore
    247 
    248 #endif // RenderText_h
    249