2 * This file is part of the DOM implementation for KDE.
4 * (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 2000 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004 Apple Computer, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
27 #include "dom/dom_string.h"
28 #include "xml/dom_stringimpl.h"
29 #include "xml/dom_textimpl.h"
30 #include "rendering/render_object.h"
31 #include "rendering/render_flow.h"
33 #include <qptrvector.h>
44 // Define a constant for soft hyphen's unicode value.
45 #define SOFT_HYPHEN 173
47 const int cNoTruncation = -1;
48 const int cFullTruncation = -2;
55 class InlineTextBox : public InlineRunBox
58 InlineTextBox(RenderObject* obj)
66 m_truncation = cNoTruncation;
69 InlineTextBox* nextTextBox() const { return static_cast<InlineTextBox*>(nextLineBox()); }
70 InlineTextBox* prevTextBox() const { return static_cast<InlineTextBox*>(prevLineBox()); }
72 uint start() const { return m_start; }
73 uint end() const { return m_len ? m_start+m_len-1 : m_start; }
74 uint len() const { return m_len; }
76 void offsetRun(int d) { m_start += d; }
78 void detach(RenderArena* arena);
80 QRect selectionRect(int absx, int absy, int startPos, int endPos);
81 bool isSelected(int startPos, int endPos) const;
82 void selectionStartEnd(int& sPos, int& ePos);
84 virtual void paint(RenderObject::PaintInfo& i, int tx, int ty);
85 virtual bool nodeAtPoint(RenderObject::NodeInfo& i, int x, int y, int tx, int ty);
87 RenderText* textObject();
89 virtual void deleteLine(RenderArena* arena);
90 virtual void extractLine();
91 virtual void attachLine();
93 virtual RenderObject::SelectionState selectionState();
95 virtual void clearTruncation() { m_truncation = cNoTruncation; }
96 virtual int placeEllipsisBox(bool ltr, int blockEdge, int ellipsisWidth, bool& foundBox);
98 // Overloaded new operator. Derived classes must override operator new
99 // in order to allocate out of the RenderArena.
100 void* operator new(size_t sz, RenderArena* renderArena) throw();
102 // Overridden to prevent the normal delete from being called.
103 void operator delete(void* ptr, size_t sz);
106 // The normal operator new is disallowed.
107 void* operator new(size_t sz) throw();
110 void setSpaceAdd(int add) { m_width -= m_toAdd; m_toAdd = add; m_width += m_toAdd; }
111 int spaceAdd() { return m_toAdd; }
113 virtual bool isInlineTextBox() { return true; }
114 virtual bool isText() const { return m_treatAsText; }
115 void setIsText(bool b) { m_treatAsText = b; }
117 void paintDecoration(QPainter* p, int _tx, int _ty, int decoration);
118 void paintSelection(QPainter* p, int tx, int ty, RenderStyle* style, const Font* font);
119 void paintMarkedTextBackground(QPainter* p, int tx, int ty, RenderStyle* style, const Font* font, int startPos, int endPos);
120 void paintMarker(QPainter* p, int _tx, int _ty, DOM::DocumentMarker marker);
122 virtual long caretMinOffset() const;
123 virtual long caretMaxOffset() const;
124 virtual unsigned long caretMaxRenderedOffset() const;
126 int offsetForPosition(int _x, bool includePartialGlyphs = true);
129 * if this text run was rendered @ref _ty pixels below the upper edge
130 * of a view, would the @ref _y -coordinate be inside the vertical range
131 * of this object's representation?
133 bool checkVerticalPoint(int _y, int _ty, int _h);
136 unsigned short m_len;
138 int m_truncation; // Where to truncate when text overflow is applied. We use special constants to
139 // denote no truncation (the whole run paints) and full truncation (nothing paints at all).
142 bool m_treatAsText : 1; // Whether or not to treat a <br> as text for the purposes of line height.
143 int m_toAdd : 14; // for justified text
146 friend class RenderText;
149 class RenderText : public RenderObject
151 friend class InlineTextBox;
154 RenderText(DOM::NodeImpl* node, DOM::DOMStringImpl *_str);
155 virtual ~RenderText();
157 virtual bool isTextFragment() const;
158 virtual DOM::DOMStringImpl* originalString() const;
160 virtual const char *renderName() const { return "RenderText"; }
162 virtual void setStyle(RenderStyle *style);
164 void extractTextBox(InlineTextBox* textBox);
165 void attachTextBox(InlineTextBox* textBox);
166 void removeTextBox(InlineTextBox* textBox);
167 void deleteTextBoxes();
168 virtual void detach();
170 DOM::DOMString data() const { return str; }
171 DOM::DOMStringImpl *string() const { return str; }
173 virtual InlineBox* createInlineBox(bool,bool, bool isOnlyRun = false);
174 virtual void dirtyLineBoxes(bool fullLayout, bool isRootInlineBox = false);
176 virtual void paint(PaintInfo& i, int tx, int ty) { assert(false); }
177 virtual void layout() { assert(false); }
179 virtual bool nodeAtPoint(NodeInfo& info, int x, int y, int tx, int ty,
180 HitTestAction hitTestAction) { assert(false); return false; }
182 virtual void absoluteRects(QValueList<QRect>& rects, int _tx, int _ty);
184 virtual DOM::Position positionForCoordinates(int x, int y, EAffinity * = 0);
186 unsigned int length() const { return str->l; }
187 QChar *text() const { return str->s; }
188 unsigned int stringLength() const { return str->l; } // non virtual implementation of length()
189 virtual void position(InlineBox* box, int from, int len, bool reverse);
191 virtual unsigned int width(unsigned int from, unsigned int len, const Font *f) const;
192 virtual unsigned int width(unsigned int from, unsigned int len, bool firstLine = false) const;
193 virtual int width() const;
194 virtual int height() const;
196 // height of the contents (without paddings, margins and borders)
197 virtual short lineHeight( bool firstLine, bool isRootLineBox=false ) const;
198 virtual short baselinePosition( bool firstLine, bool isRootLineBox=false ) const;
201 virtual void calcMinMaxWidth();
202 virtual int minWidth() const { return m_minWidth; }
203 virtual int maxWidth() const { return m_maxWidth; }
204 virtual void trimmedMinMaxWidth(int& beginMinW, bool& beginWS,
205 int& endMinW, bool& endWS,
206 bool& hasBreakableChar, bool& hasBreak,
207 int& beginMaxW, int& endMaxW,
208 int& minW, int& maxW, bool& stripFrontSpaces);
210 bool containsOnlyWhitespace(unsigned int from, unsigned int len) const;
212 // returns the minimum x position of all runs relative to the parent.
216 virtual int xPos() const;
217 virtual int yPos() const;
219 virtual const QFont &font();
220 virtual short verticalPositionHint( bool firstLine ) const;
222 void setText(DOM::DOMStringImpl *text, bool force=false);
223 void setTextWithOffset(DOM::DOMStringImpl *text, uint offset, uint len, bool force=false);
225 virtual bool canBeSelectionLeaf() const { return true; }
226 virtual SelectionState selectionState() const {return m_selectionState;}
227 virtual void setSelectionState(SelectionState s);
228 virtual QRect selectionRect();
229 virtual QRect caretRect(int offset, EAffinity affinity, int *extraWidthToEndOfLine = 0);
230 void posOfChar(int ch, int &x, int &y);
232 virtual int marginLeft() const { return style()->marginLeft().minWidth(0); }
233 virtual int marginRight() const { return style()->marginRight().minWidth(0); }
235 virtual QRect getAbsoluteRepaintRect();
237 const QFontMetrics &metrics(bool firstLine) const;
238 const Font *htmlFont(bool firstLine) const;
240 DOM::TextImpl *element() const
241 { return static_cast<DOM::TextImpl*>(RenderObject::element()); }
243 InlineTextBox* firstTextBox() const { return m_firstTextBox; }
244 InlineTextBox* lastTextBox() const { return m_lastTextBox; }
246 virtual InlineBox *inlineBox(long offset, EAffinity affinity = UPSTREAM);
249 int widthFromCache(const Font *, int start, int len) const;
250 bool shouldUseMonospaceCache(const Font *) const;
252 bool allAscii() const;
255 virtual long caretMinOffset() const;
256 virtual long caretMaxOffset() const;
257 virtual unsigned long caretMaxRenderedOffset() const;
262 InlineTextBox * findNextInlineTextBox( int offset, int &pos );
264 protected: // members
265 DOM::DOMStringImpl *str;
267 InlineTextBox* m_firstTextBox;
268 InlineTextBox* m_lastTextBox;
275 SelectionState m_selectionState : 3 ;
276 bool m_hasBreakableChar : 1; // Whether or not we can be broken into multiple lines.
277 bool m_hasBreak : 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
278 bool m_hasBeginWS : 1; // Whether or not we begin with WS (only true if we aren't pre)
279 bool m_hasEndWS : 1; // Whether or not we end with WS (only true if we aren't pre)
281 bool m_linesDirty : 1; // This bit indicates that the text run has already dirtied specific
282 // line boxes, and this hint will enable layoutInlineChildren to avoid
283 // just dirtying everything when character data is modified (e.g., appended/inserted
288 mutable bool m_allAsciiChecked:1;
289 mutable bool m_allAscii:1;
290 int m_monospaceCharacterWidth;
294 // Used to represent a text substring of an element, e.g., for text runs that are split because of
295 // first letter and that must therefore have different styles (and positions in the render tree).
296 // We cache offsets so that text transformations can be applied in such a way that we can recover
297 // the original unaltered string from our corresponding DOM node.
298 class RenderTextFragment : public RenderText
301 RenderTextFragment(DOM::NodeImpl* _node, DOM::DOMStringImpl* _str,
302 int startOffset, int endOffset);
303 RenderTextFragment(DOM::NodeImpl* _node, DOM::DOMStringImpl* _str);
304 ~RenderTextFragment();
306 virtual bool isTextFragment() const;
308 uint start() const { return m_start; }
309 uint end() const { return m_end; }
311 DOM::DOMStringImpl* contentString() const { return m_generatedContentStr; }
312 virtual DOM::DOMStringImpl* originalString() const;
317 DOM::DOMStringImpl* m_generatedContentStr;