File: LayoutText.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (358 lines) | stat: -rw-r--r-- 12,943 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * (C) 1999 Lars Knoll (knoll@kde.org)
 * (C) 2000 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2004-2009, 2013 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef LayoutText_h
#define LayoutText_h

#include "core/CoreExport.h"
#include "core/dom/Text.h"
#include "core/layout/LayoutObject.h"
#include "core/layout/TextRunConstructor.h"
#include "platform/LengthFunctions.h"
#include "wtf/Forward.h"
#include "wtf/PassRefPtr.h"

namespace blink {

class AbstractInlineTextBox;
class InlineTextBox;

// LayoutText is the root class for anything that represents
// a text node (see core/dom/Text.h).
//
// This is a common node in the tree so to the limit memory overhead,
// this class inherits directly from LayoutObject.
// Also this class is used by both CSS and SVG layouts so LayoutObject
// was a natural choice.
//
// The actual layout of text is handled by the containing inline
// (LayoutInline) or block (LayoutBlockFlow). They will invoke the Unicode
// Bidirectional Algorithm to break the text into actual lines.
// The result of layout is the line box tree, which represents lines
// on the screen. It is stored into m_firstTextBox and m_lastTextBox.
// To understand how lines are broken by the bidi algorithm, read e.g.
// LayoutBlockFlow::layoutInlineChildren.
//
//
// ***** LINE BOXES OWNERSHIP *****
// m_firstTextBox and m_lastTextBox are not owned by LayoutText
// but are pointers into the enclosing inline / block (see LayoutInline's
// and LayoutBlockFlow's m_lineBoxes).
//
//
// This class implements the preferred logical widths computation
// for its underlying text. The widths are stored into m_minWidth
// and m_maxWidth. They are computed lazily based on
// m_preferredLogicalWidthsDirty.
//
// The previous comment applies also for painting. See e.g.
// BlockFlowPainter::paintContents in particular the use of LineBoxListPainter.
class CORE_EXPORT LayoutText : public LayoutObject {
 public:
  // FIXME: If the node argument is not a Text node or the string argument is
  // not the content of the Text node, updating text-transform property
  // doesn't re-transform the string.
  LayoutText(Node*, PassRefPtr<StringImpl>);
#if DCHECK_IS_ON()
  ~LayoutText() override;
#endif

  const char* name() const override { return "LayoutText"; }

  virtual bool isTextFragment() const;
  virtual bool isWordBreak() const;

  virtual PassRefPtr<StringImpl> originalText() const;

  void extractTextBox(InlineTextBox*);
  void attachTextBox(InlineTextBox*);
  void removeTextBox(InlineTextBox*);

  const String& text() const { return m_text; }
  virtual unsigned textStartOffset() const { return 0; }
  String plainText() const;

  InlineTextBox* createInlineTextBox(int start, unsigned short length);
  void dirtyOrDeleteLineBoxesIfNeeded(bool fullLayout);
  void dirtyLineBoxes();

  void absoluteRects(Vector<IntRect>&,
                     const LayoutPoint& accumulatedOffset) const final;
  void absoluteRectsForRange(Vector<IntRect>&,
                             unsigned startOffset = 0,
                             unsigned endOffset = INT_MAX,
                             bool useSelectionHeight = false);

  void absoluteQuads(Vector<FloatQuad>&,
                     MapCoordinatesFlags mode = 0) const final;
  void absoluteQuadsForRange(Vector<FloatQuad>&,
                             unsigned startOffset = 0,
                             unsigned endOffset = INT_MAX,
                             bool useSelectionHeight = false);
  FloatRect localBoundingBoxRectForAccessibility() const final;

  enum ClippingOption { NoClipping, ClipToEllipsis };
  enum LocalOrAbsoluteOption { LocalQuads, AbsoluteQuads };
  void quads(Vector<FloatQuad>&,
             ClippingOption = NoClipping,
             LocalOrAbsoluteOption = AbsoluteQuads,
             MapCoordinatesFlags mode = 0) const;

  PositionWithAffinity positionForPoint(const LayoutPoint&) override;

  bool is8Bit() const { return m_text.is8Bit(); }
  const LChar* characters8() const { return m_text.impl()->characters8(); }
  const UChar* characters16() const { return m_text.impl()->characters16(); }
  bool hasEmptyText() const { return m_text.isEmpty(); }
  UChar characterAt(unsigned) const;
  UChar uncheckedCharacterAt(unsigned) const;
  UChar operator[](unsigned i) const { return uncheckedCharacterAt(i); }
  UChar32 codepointAt(unsigned) const;
  unsigned textLength() const {
    return m_text.length();
  }  // non virtual implementation of length()
  bool containsOnlyWhitespace(unsigned from, unsigned len) const;
  void positionLineBox(InlineBox*);

  virtual float width(unsigned from,
                      unsigned len,
                      const Font&,
                      LayoutUnit xPos,
                      TextDirection,
                      HashSet<const SimpleFontData*>* fallbackFonts = nullptr,
                      FloatRect* glyphBounds = nullptr) const;
  virtual float width(unsigned from,
                      unsigned len,
                      LayoutUnit xPos,
                      TextDirection,
                      bool firstLine = false,
                      HashSet<const SimpleFontData*>* fallbackFonts = nullptr,
                      FloatRect* glyphBounds = nullptr) const;

  float minLogicalWidth() const;
  float maxLogicalWidth() const;

  void trimmedPrefWidths(LayoutUnit leadWidth,
                         LayoutUnit& firstLineMinWidth,
                         bool& hasBreakableStart,
                         LayoutUnit& lastLineMinWidth,
                         bool& hasBreakableEnd,
                         bool& hasBreakableChar,
                         bool& hasBreak,
                         LayoutUnit& firstLineMaxWidth,
                         LayoutUnit& lastLineMaxWidth,
                         LayoutUnit& minWidth,
                         LayoutUnit& maxWidth,
                         bool& stripFrontSpaces,
                         TextDirection);

  virtual LayoutRect linesBoundingBox() const;

  // Returns the bounding box of visual overflow rects of all line boxes.
  LayoutRect visualOverflowRect() const;

  FloatPoint firstRunOrigin() const;
  float firstRunX() const;
  float firstRunY() const;

  virtual void setText(PassRefPtr<StringImpl>, bool force = false);
  void setTextWithOffset(PassRefPtr<StringImpl>,
                         unsigned offset,
                         unsigned len,
                         bool force = false);

  virtual void transformText();

  bool canBeSelectionLeaf() const override { return true; }
  void setSelectionState(SelectionState) final;
  LayoutRect localSelectionRect() const final;
  LayoutRect localCaretRect(
      InlineBox*,
      int caretOffset,
      LayoutUnit* extraWidthToEndOfLine = nullptr) override;

  InlineTextBox* firstTextBox() const { return m_firstTextBox; }
  InlineTextBox* lastTextBox() const { return m_lastTextBox; }

  // True if we have inline text box children which implies rendered text (or
  // whitespace) output.
  bool hasTextBoxes() const { return firstTextBox(); }

  int caretMinOffset() const override;
  int caretMaxOffset() const override;
  unsigned resolvedTextLength() const;

  bool containsReversedText() const { return m_containsReversedText; }

  bool isSecure() const { return style()->textSecurity() != TSNONE; }
  void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);

  bool isAllCollapsibleWhitespace() const;
  bool isRenderedCharacter(int offsetInNode) const;

  void removeAndDestroyTextBoxes();

  PassRefPtr<AbstractInlineTextBox> firstAbstractInlineTextBox();

  float hyphenWidth(const Font&, TextDirection);

  LayoutRect debugRect() const override;

 protected:
  void willBeDestroyed() override;

  void styleWillChange(StyleDifference, const ComputedStyle&) final {}
  void styleDidChange(StyleDifference, const ComputedStyle* oldStyle) override;

  virtual void setTextInternal(PassRefPtr<StringImpl>);
  virtual UChar previousCharacter() const;

  void addLayerHitTestRects(LayerHitTestRects&,
                            const PaintLayer* currentLayer,
                            const LayoutPoint& layerOffset,
                            const LayoutRect& containerRect) const override;

  virtual InlineTextBox* createTextBox(
      int start,
      unsigned short length);  // Subclassed by SVG.

  void invalidateDisplayItemClients(PaintInvalidationReason) const override;

 private:
  void computePreferredLogicalWidths(float leadWidth);
  void computePreferredLogicalWidths(
      float leadWidth,
      HashSet<const SimpleFontData*>& fallbackFonts,
      FloatRect& glyphBounds);

  // Make length() private so that callers that have a LayoutText*
  // will use the more efficient textLength() instead, while
  // callers with a LayoutObject* can continue to use length().
  unsigned length() const final { return textLength(); }

  // See the class comment as to why we shouldn't call this function directly.
  void paint(const PaintInfo&, const LayoutPoint&) const final { NOTREACHED(); }
  void layout() final { NOTREACHED(); }
  bool nodeAtPoint(HitTestResult&,
                   const HitTestLocation&,
                   const LayoutPoint&,
                   HitTestAction) final {
    NOTREACHED();
    return false;
  }

  void deleteTextBoxes();
  float widthFromFont(const Font&,
                      int start,
                      int len,
                      float leadWidth,
                      float textWidthSoFar,
                      TextDirection,
                      HashSet<const SimpleFontData*>* fallbackFonts,
                      FloatRect* glyphBoundsAccumulation) const;

  void secureText(UChar mask);

  bool isText() const =
      delete;  // This will catch anyone doing an unnecessary check.

  LayoutRect localVisualRect() const override;

  void checkConsistency() const;

  // We put the bitfield first to minimize padding on 64-bit.

  // Whether or not we can be broken into multiple lines.
  bool m_hasBreakableChar : 1;
  // Whether or not we have a hard break (e.g., <pre> with '\n').
  bool m_hasBreak : 1;
  // Whether or not we have a variable width tab character (e.g., <pre> with
  // '\t').
  bool m_hasTab : 1;
  bool m_hasBreakableStart : 1;
  bool m_hasBreakableEnd : 1;
  bool m_hasEndWhiteSpace : 1;
  // This bit indicates that the text run has already dirtied specific line
  // boxes, and this hint will enable layoutInlineChildren to avoid just
  // dirtying everything when character data is modified (e.g., appended/
  // inserted or removed).
  bool m_linesDirty : 1;
  bool m_containsReversedText : 1;
  mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;

  float m_minWidth;
  float m_maxWidth;
  float m_firstLineMinWidth;
  float m_lastLineLineMinWidth;

  String m_text;

  // The line boxes associated with this object.
  // Read the LINE BOXES OWNERSHIP section in the class header comment.
  InlineTextBox* m_firstTextBox;
  InlineTextBox* m_lastTextBox;
};

inline UChar LayoutText::uncheckedCharacterAt(unsigned i) const {
  SECURITY_DCHECK(i < textLength());
  return is8Bit() ? characters8()[i] : characters16()[i];
}

inline UChar LayoutText::characterAt(unsigned i) const {
  if (i >= textLength())
    return 0;

  return uncheckedCharacterAt(i);
}

inline UChar32 LayoutText::codepointAt(unsigned i) const {
  if (i >= textLength())
    return 0;
  if (is8Bit())
    return characters8()[i];
  UChar32 c;
  U16_GET(characters16(), 0, i, textLength(), c);
  return c;
}

inline float LayoutText::hyphenWidth(const Font& font,
                                     TextDirection direction) {
  const ComputedStyle& style = styleRef();
  return font.width(constructTextRun(font, style.hyphenString().getString(),
                                     style, direction));
}

DEFINE_LAYOUT_OBJECT_TYPE_CASTS(LayoutText, isText());

#if !DCHECK_IS_ON()
inline void LayoutText::checkConsistency() const {}
#endif

inline LayoutText* Text::layoutObject() const {
  return toLayoutText(CharacterData::layoutObject());
}

void applyTextTransform(const ComputedStyle*, String&, UChar);

}  // namespace blink

#endif  // LayoutText_h