Backed out changeset 496886cb30a5 (bug 1867152) for bc failures on browser_user_input...
[gecko.git] / layout / generic / nsTextFrameUtils.h
blob5b9edf271e9cba0b42082605afa66cafc6932738
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef NSTEXTFRAMEUTILS_H_
8 #define NSTEXTFRAMEUTILS_H_
10 #include "gfxSkipChars.h"
11 #include "nsBidiUtils.h"
13 class nsIContent;
14 struct nsStyleText;
16 namespace mozilla {
17 namespace dom {
18 class Text;
20 } // namespace mozilla
22 #define BIG_TEXT_NODE_SIZE 4096
24 #define CH_NBSP 160
25 #define CH_SHY 173
26 #define CH_CJKSP 12288 // U+3000 IDEOGRAPHIC SPACE (CJK Full-Width Space)
28 class nsTextFrameUtils {
29 public:
30 // These constants are used as textrun flags for textframe textruns.
32 // If you add a flag, please add support for it in gfxTextRun::Dump.
33 enum class Flags : uint16_t {
34 // The following flags are set by TransformText
36 // the text has at least one untransformed tab character
37 HasTab = 0x01,
38 // the original text has at least one soft hyphen character
39 HasShy = 0x02,
40 // the text has at least one untransformed newline character
41 HasNewline = 0x04,
43 // Flag used in textrun construction to *prevent* hiding of fallback text
44 // for pending user-fonts (used for Canvas2d text).
45 DontSkipDrawingForPendingUserFonts = 0x08,
47 // The following flags are set by nsTextFrame
49 IsSimpleFlow = 0x10,
50 IncomingWhitespace = 0x20,
51 TrailingWhitespace = 0x40,
52 CompressedLeadingWhitespace = 0x80,
53 NoBreaks = 0x100,
54 IsTransformed = 0x200,
55 // This gets set if there's a break opportunity at the end of the textrun.
56 // We normally don't use this break opportunity because the following text
57 // will have a break opportunity at the start, but it's useful for line
58 // layout to know about it in case the following content is not text
59 HasTrailingBreak = 0x400,
61 // This is set if the textrun was created for a textframe whose
62 // NS_FRAME_IS_IN_SINGLE_CHAR_MI flag is set. This occurs if the textframe
63 // belongs to a MathML <mi> element whose embedded text consists of a
64 // single character.
65 IsSingleCharMi = 0x800,
67 // This is set if the text run might be observing for glyph changes.
68 MightHaveGlyphChanges = 0x1000,
70 // For internal use by the memory reporter when accounting for
71 // storage used by textruns.
72 // Because the reporter may visit each textrun multiple times while
73 // walking the frame trees and textrun cache, it needs to mark
74 // textruns that have been seen so as to avoid multiple-accounting.
75 RunSizeAccounted = 0x2000,
77 // The following are defined by gfxTextRunFactory rather than here,
78 // so that it also has access to the _INCOMING and MATH_SCRIPT flags
79 // for shaping purposes.
80 // They live in the gfxShapedText::mFlags field rather than the
81 // gfxTextRun::mFlags2 field.
82 // TEXT_TRAILING_ARABICCHAR
83 // TEXT_INCOMING_ARABICCHAR
84 // TEXT_USE_MATH_SCRIPT
87 // These constants are used in TransformText to represent context information
88 // from previous textruns.
89 enum { INCOMING_NONE = 0, INCOMING_WHITESPACE = 1, INCOMING_ARABICCHAR = 2 };
91 /**
92 * Returns true if aChars/aLength are something that make a space
93 * character not be whitespace when they follow the space character
94 * (combining mark or join control, ignoring intervening direction
95 * controls).
97 static bool IsSpaceCombiningSequenceTail(const char16_t* aChars,
98 int32_t aLength);
99 static bool IsSpaceCombiningSequenceTail(const uint8_t* aChars,
100 int32_t aLength) {
101 return false;
104 enum CompressionMode {
105 COMPRESS_NONE,
106 COMPRESS_WHITESPACE,
107 COMPRESS_WHITESPACE_NEWLINE,
108 COMPRESS_NONE_TRANSFORM_TO_SPACE
112 * Create a text run from a run of Unicode text. The text may have whitespace
113 * compressed. A preformatted tab is sent to the text run as a single space.
114 * (Tab spacing must be performed by textframe later.) Certain other
115 * characters are discarded.
117 * @param aCompression control what is compressed to a
118 * single space character: no compression, compress spaces (not followed
119 * by combining mark) and tabs, compress those plus newlines, or
120 * no compression except newlines are discarded.
121 * @param aIncomingFlags a flag indicating whether there was whitespace
122 * or an Arabic character preceding this text. We set it to indicate if
123 * there's an Arabic character or whitespace preceding the end of this text.
125 template <class CharT>
126 static CharT* TransformText(const CharT* aText, uint32_t aLength,
127 CharT* aOutput, CompressionMode aCompression,
128 uint8_t* aIncomingFlags, gfxSkipChars* aSkipChars,
129 nsTextFrameUtils::Flags* aAnalysisFlags);
132 * Returns whether aChar is a character that nsTextFrameUtils::TransformText
133 * might mark as skipped. This is used by
134 * SVGTextContentElement::GetNumberOfChars to know whether reflowing frames,
135 * so that we have the results of TransformText, is required, or whether we
136 * can use a fast path instead.
138 template <class CharT>
139 static bool IsSkippableCharacterForTransformText(CharT aChar);
141 static void AppendLineBreakOffset(nsTArray<uint32_t>* aArray,
142 uint32_t aOffset) {
143 if (aArray->Length() > 0 && (*aArray)[aArray->Length() - 1] == aOffset) {
144 return;
146 aArray->AppendElement(aOffset);
149 static uint32_t ComputeApproximateLengthWithWhitespaceCompression(
150 mozilla::dom::Text*, const nsStyleText*);
151 static uint32_t ComputeApproximateLengthWithWhitespaceCompression(
152 const nsAString&, const nsStyleText*);
155 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(nsTextFrameUtils::Flags)
157 class nsSkipCharsRunIterator {
158 public:
159 enum LengthMode {
160 LENGTH_UNSKIPPED_ONLY = false,
161 LENGTH_INCLUDES_SKIPPED = true
163 nsSkipCharsRunIterator(const gfxSkipCharsIterator& aStart,
164 LengthMode aLengthIncludesSkipped, uint32_t aLength)
165 : mIterator(aStart),
166 mRemainingLength(aLength),
167 mRunLength(0),
168 mSkipped(false),
169 mVisitSkipped(false),
170 mLengthIncludesSkipped(aLengthIncludesSkipped) {}
171 void SetVisitSkipped() { mVisitSkipped = true; }
172 void SetOriginalOffset(int32_t aOffset) {
173 mIterator.SetOriginalOffset(aOffset);
175 void SetSkippedOffset(uint32_t aOffset) {
176 mIterator.SetSkippedOffset(aOffset);
179 // guaranteed to return only positive-length runs
180 bool NextRun();
181 bool IsSkipped() const { return mSkipped; }
182 // Always returns something > 0
183 int32_t GetRunLength() const { return mRunLength; }
184 const gfxSkipCharsIterator& GetPos() const { return mIterator; }
185 int32_t GetOriginalOffset() const { return mIterator.GetOriginalOffset(); }
186 uint32_t GetSkippedOffset() const { return mIterator.GetSkippedOffset(); }
188 private:
189 gfxSkipCharsIterator mIterator;
190 int32_t mRemainingLength;
191 int32_t mRunLength;
192 bool mSkipped;
193 bool mVisitSkipped;
194 bool mLengthIncludesSkipped;
197 #endif /*NSTEXTFRAMEUTILS_H_*/