KHtml

render_text.h
1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright (C) 1999-2003 Lars Knoll <[email protected]>
5  * Copyright (C) 2000-2003 Dirk Mueller <[email protected]>
6  * Copyright (C) 2003, 2006 Apple Computer, Inc.
7  *
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.
12  *
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.
17  *
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., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24 #ifndef RENDERTEXT_H
25 #define RENDERTEXT_H
26 
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_line.h"
32 
33 #include <QMutableVectorIterator>
34 #include <assert.h>
35 
36 class QPainter;
37 class QFontMetrics;
38 
39 // Define a constant for soft hyphen's unicode value.
40 #define SOFT_HYPHEN 173
41 
42 const int cNoTruncation = -1;
43 const int cFullTruncation = -2;
44 
45 namespace khtml
46 {
47 class RenderText;
48 class RenderStyle;
49 
50 class InlineTextBox : public InlineRunBox
51 {
52 public:
53  InlineTextBox(RenderObject *obj)
54  : InlineRunBox(obj),
55  // ### necessary as some codepaths (<br>) do *not* initialize these (LS)
56  m_start(0), m_len(0), m_truncation(cNoTruncation), m_reversed(false), m_toAdd(0)
57  {
58  }
59 
60  uint start() const
61  {
62  return m_start;
63  }
64  uint end() const
65  {
66  return m_len ? m_start + m_len - 1 : m_start;
67  }
68  uint len() const
69  {
70  return m_len;
71  }
72 
73  void detach(RenderArena *renderArena, bool noRemove = false) override;
74 
75  InlineTextBox *nextTextBox() const
76  {
77  return static_cast<InlineTextBox *>(nextLineBox());
78  }
79  InlineTextBox *prevTextBox() const
80  {
81  return static_cast<InlineTextBox *>(prevLineBox());
82  }
83 
84  void clearTruncation() override
85  {
86  m_truncation = cNoTruncation;
87  }
88  int placeEllipsisBox(bool ltr, int blockEdge, int ellipsisWidth, bool &foundBox) override;
89 
90  // Overloaded new operator. Derived classes must override operator new
91  // in order to allocate out of the RenderArena.
92  void *operator new(size_t sz, RenderArena *renderArena) throw();
93 
94  // Overridden to prevent the normal delete from being called.
95  void operator delete(void *ptr, size_t sz);
96 
97 private:
98  // The normal operator new is disallowed.
99  void *operator new(size_t sz) throw();
100 
101 public:
102  void setSpaceAdd(int add)
103  {
104  m_width -= m_toAdd;
105  m_toAdd = add;
106  m_width += m_toAdd;
107  }
108  int spaceAdd()
109  {
110  return m_toAdd;
111  }
112 
113  bool isInlineTextBox() const override
114  {
115  return true;
116  }
117 
118  void paint(RenderObject::PaintInfo &i, int tx, int ty) override;
119  void paintDecoration(QPainter *pt, const Font *f, int _tx, int _ty, int decoration);
120  void paintShadow(QPainter *pt, const Font *f, int _tx, int _ty, const ShadowData *shadow);
121  void paintSelection(const Font *f, RenderText *text, QPainter *p, RenderStyle *style, int tx, int ty, int startPos, int endPos, int deco);
122 
123  void selectionStartEnd(int &sPos, int &ePos);
124  RenderObject::SelectionState selectionState();
125 
126  // Return before, after (offset set to max), or inside the text, at @p offset
127  FindSelectionResult checkSelectionPoint(int _x, int _y, int _tx, int _ty, int &offset);
128 
129  bool checkVerticalPoint(int _y, int _ty, int _h, int height)
130  {
131  if ((_ty + m_y > _y + _h) || (_ty + m_y + m_baseline + height < _y)) {
132  return false;
133  } return true;
134  }
135 
136  /**
137  * determines the offset into the DOMString of the character the given
138  * coordinate points to.
139  * The returned offset is never out of range.
140  * @param _x given coordinate (relative to containing block)
141  * @param ax returns exact coordinate the offset corresponds to
142  * (relative to containing block)
143  * @return the offset (relative to the RenderText object, not to this run)
144  */
145  int offsetForPoint(int _x, int &ax) const;
146 
147  /**
148  * calculates the with of the specified chunk in this text box.
149  * @param pos zero-based position within the text box up to which
150  * the width is to be determined
151  * @return the width in pixels
152  */
153  int widthFromStart(int pos) const;
154 
155  long caretMinOffset() const override;
156  long caretMaxOffset() const override;
157  unsigned long caretMaxRenderedOffset() const override;
158 
159  /** returns the associated render text
160  */
161  const RenderText *renderText() const;
162  RenderText *renderText();
163 
164  void extractLine() override;
165  void deleteLine(RenderArena *arena) override;
166  void attachLine() override;
167 
168  int m_start;
169  unsigned short m_len;
170 
171  int m_truncation; // Where to truncate when text overflow is applied.
172  // We use special constants to denote no truncation (the whole run paints)
173  // and full truncation (nothing paints at all).
174 
175  bool m_reversed : 1;
176  unsigned m_toAdd : 14; // for justified text
177 private:
178  friend class RenderText;
179 };
180 
181 class RenderText : public RenderObject
182 {
183  friend class InlineTextBox;
184 
185 public:
186  RenderText(DOM::NodeImpl *node, DOM::DOMStringImpl *_str);
187  virtual ~RenderText();
188 
189  virtual bool isTextFragment() const;
190  virtual DOM::DOMStringImpl *originalString() const;
191 
192  const char *renderName() const override
193  {
194  return "RenderText";
195  }
196 
197  void setStyle(RenderStyle *style) override;
198 
199  void detach() override;
200 
201  void deleteInlineBoxes(RenderArena *arena = nullptr) override;
202  void dirtyInlineBoxes(bool fullLayout, bool) override;
203  void removeInlineBox(InlineBox *_box) override;
204 
205  DOM::DOMString data() const
206  {
207  return str;
208  }
209  DOM::DOMStringImpl *string() const
210  {
211  return str;
212  }
213 
214  InlineBox *createInlineBox(bool, bool) override;
215 
216  void layout() override
217  {
218  assert(false);
219  }
220 
221  bool nodeAtPoint(NodeInfo &info, int x, int y, int tx, int ty, HitTestAction hitTestAction, bool inBox) override;
222 
223  RenderPosition positionForCoordinates(int _x, int _y) override;
224 
225  // Return before, after (offset set to max), or inside the text, at @p offset
226  virtual FindSelectionResult checkSelectionPoint(int _x, int _y, int _tx, int _ty,
227  DOM::NodeImpl *&node, int &offset,
228  SelPointState &);
229 
230  unsigned int length() const override
231  {
232  if (str) {
233  return str->l;
234  } else {
235  return 0;
236  }
237  }
238  QChar *text() const
239  {
240  if (str) {
241  return str->s;
242  } else {
243  return nullptr;
244  }
245  }
246  unsigned int stringLength() const
247  {
248  return str->l; // non virtual implementation of length()
249  }
250  void position(InlineBox *box, int from, int len, bool reverse) override;
251 
252  virtual unsigned int width(unsigned int from, unsigned int len, const Font *f) const;
253  virtual unsigned int width(unsigned int from, unsigned int len, bool firstLine = false) const;
254  short width() const override;
255  int height() const override;
256 
257  // height of the contents (without paddings, margins and borders)
258  short lineHeight(bool firstLine) const override;
259  short baselinePosition(bool firstLine) const override;
260 
261  // overrides
262  void calcMinMaxWidth() override;
263  short minWidth() const override
264  {
265  return m_minWidth;
266  }
267  int maxWidth() const override
268  {
269  return m_maxWidth;
270  }
271 
272  void trimmedMinMaxWidth(int &beginMinW, bool &beginWS,
273  int &endMinW, bool &endWS,
274  bool &hasBreakableChar, bool &hasBreak,
275  int &beginMaxW, int &endMaxW,
276  int &minW, int &maxW, bool &stripFrontSpaces);
277 
278  bool containsOnlyWhitespace(unsigned int from, unsigned int len) const;
279 
280  ushort startMin() const
281  {
282  return m_startMin;
283  }
284  ushort endMin() const
285  {
286  return m_endMin;
287  }
288 
289  // returns the minimum x position of all runs relative to the parent.
290  // defaults to 0.
291  int minXPos() const;
292 
293  int inlineXPos() const override;
294  int inlineYPos() const override;
295 
296  bool hasReturn() const
297  {
298  return m_hasReturn;
299  }
300 
301  virtual const QFont &font();
302  short verticalPositionHint(bool firstLine) const override;
303 
304  bool isFixedWidthFont() const;
305 
306  void setText(DOM::DOMStringImpl *text, bool force = false);
307 
308  SelectionState selectionState() const override
309  {
310  return KDE_CAST_BF_ENUM(SelectionState, m_selectionState);
311  }
312  void setSelectionState(SelectionState s) override
313  {
314  m_selectionState = s;
315  }
316  void caretPos(int offset, int flags, int &_x, int &_y, int &width, int &height) const override;
317  bool absolutePosition(int &/*xPos*/, int &/*yPos*/, bool f = false) const override;
318  bool posOfChar(int ch, int &x, int &y) const;
319  bool isPointInsideSelection(int x, int y, const DOM::Selection &) const override;
320 
321  short marginLeft() const override
322  {
323  return style()->marginLeft().minWidth(0);
324  }
325  short marginRight() const override
326  {
327  return style()->marginRight().minWidth(0);
328  }
329 
330  void repaint(Priority p = NormalPriority) override;
331 
332  InlineTextBox *firstTextBox() const
333  {
334  return m_firstTextBox;
335  }
336  InlineTextBox *lastTextBox() const
337  {
338  return m_lastTextBox;
339  }
340 
341  QList< QRectF > getClientRects() override;
342 
343  bool hasBreakableChar() const
344  {
345  return m_hasBreakableChar;
346  }
347  bool isSimpleText() const
348  {
349  return m_isSimpleText;
350  }
351  const QFontMetrics &metrics(bool firstLine) const;
352  const Font *htmlFont(bool firstLine) const;
353 
354  DOM::TextImpl *element() const
355  {
356  return static_cast<DOM::TextImpl *>(RenderObject::element());
357  }
358 
359  InlineBox *inlineBox(long offset) override;
360 
361  void removeTextBox(InlineTextBox *box);
362  void attachTextBox(InlineTextBox *box);
363  void extractTextBox(InlineTextBox *box);
364 
365 #ifdef ENABLE_DUMP
366  void dump(QTextStream &stream, const QString &ind) const override;
367 #endif
368 
369  long caretMinOffset() const override;
370  long caretMaxOffset() const override;
371  unsigned long caretMaxRenderedOffset() const override;
372 
373  /** Find the text box that includes the character at @p offset
374  * and return pos, which is the position of the char in the run.
375  * @param offset zero-based offset into DOM string
376  * @param pos returns relative position within text box
377  * @param checkFirstLetter passing @p true will also regard :first-letter
378  * boxes, if available.
379  * @return the text box, or 0 if no match has been found
380  */
381  const InlineTextBox *findInlineTextBox(int offset, int &pos,
382  bool checkFirstLetter = false) const;
383 
384  // helper methods to convert Position from rendered text into DOM position
385  // (takes into account space collapsing)
386  unsigned convertToDOMPosition(unsigned position) const;
387  unsigned convertToRenderedPosition(unsigned position) const;
388 protected:
389  virtual void setTextInternal(DOM::DOMStringImpl *text);
390 
391  // members
392  InlineTextBox *m_firstTextBox;
393  InlineTextBox *m_lastTextBox;
394 
395  DOM::DOMStringImpl *str; //
396 
397  short m_lineHeight;
398  short m_minWidth;
399  int m_maxWidth;
400  short m_beginMinWidth;
401  short m_endMinWidth;
402 
403  KDE_BF_ENUM(SelectionState) m_selectionState : 3;
404  bool m_hasReturn : 1;
405  bool m_hasBreakableChar : 1;
406  bool m_hasBreak : 1;
407  bool m_hasBeginWS : 1;
408  bool m_hasEndWS : 1;
409  bool m_isSimpleText : 1;
410 
411  ushort m_startMin : 8;
412  ushort m_endMin : 8;
413 };
414 
415 inline const RenderText *InlineTextBox::renderText() const
416 {
417  return static_cast<RenderText *>(object());
418 }
419 
420 inline RenderText *InlineTextBox::renderText()
421 {
422  return static_cast<RenderText *>(object());
423 }
424 
425 // Used to represent a text substring of an element, e.g., for text runs that are split because of
426 // first letter and that must therefore have different styles (and positions in the render tree).
427 // We cache offsets so that text transformations can be applied in such a way that we can recover
428 // the original unaltered string from our corresponding DOM node.
429 class RenderTextFragment : public RenderText
430 {
431 public:
432  RenderTextFragment(DOM::NodeImpl *_node, DOM::DOMStringImpl *_str,
433  int startOffset, int endOffset);
434  RenderTextFragment(DOM::NodeImpl *_node, DOM::DOMStringImpl *_str);
435  ~RenderTextFragment();
436 
437  bool isTextFragment() const override;
438  const char *renderName() const override
439  {
440  return "RenderTextFragment";
441  }
442 
443  uint start() const
444  {
445  return m_start;
446  }
447  uint end() const
448  {
449  return m_end;
450  }
451 
452  DOM::DOMStringImpl *contentString() const
453  {
454  return m_generatedContentStr;
455  }
456  DOM::DOMStringImpl *originalString() const override;
457 
458  RenderObject *firstLetter() const
459  {
460  return m_firstLetter;
461  }
462  void setFirstLetter(RenderObject *firstLetter)
463  {
464  m_firstLetter = firstLetter;
465  }
466 
467  // overrides
468  void detach() override;
469 private:
470  void setTextInternal(DOM::DOMStringImpl *text) override;
471 
472  uint m_start;
473  uint m_end;
474  DOM::DOMStringImpl *m_generatedContentStr;
475  RenderObject *m_firstLetter;
476 };
477 } // end namespace
478 #endif
This file is part of the HTML rendering engine for KDE.
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
const QList< QKeySequence > & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.