Backed out changeset f53842753805 (bug 1804872) for causing reftest failures on 15535...
[gecko.git] / layout / generic / nsTextFrameUtils.h
blob7990ba1632bfe9552cd426fdb550e00a420e9a20
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 UnusedFlags = 0x04,
42 // Flag used in textrun construction to *prevent* hiding of fallback text
43 // for pending user-fonts (used for Canvas2d text).
44 DontSkipDrawingForPendingUserFonts = 0x08,
46 // The following flags are set by nsTextFrame
48 IsSimpleFlow = 0x10,
49 IncomingWhitespace = 0x20,
50 TrailingWhitespace = 0x40,
51 CompressedLeadingWhitespace = 0x80,
52 NoBreaks = 0x100,
53 IsTransformed = 0x200,
54 // This gets set if there's a break opportunity at the end of the textrun.
55 // We normally don't use this break opportunity because the following text
56 // will have a break opportunity at the start, but it's useful for line
57 // layout to know about it in case the following content is not text
58 HasTrailingBreak = 0x400,
60 // This is set if the textrun was created for a textframe whose
61 // NS_FRAME_IS_IN_SINGLE_CHAR_MI flag is set. This occurs if the textframe
62 // belongs to a MathML <mi> element whose embedded text consists of a
63 // single character.
64 IsSingleCharMi = 0x800,
66 // This is set if the text run might be observing for glyph changes.
67 MightHaveGlyphChanges = 0x1000,
69 // For internal use by the memory reporter when accounting for
70 // storage used by textruns.
71 // Because the reporter may visit each textrun multiple times while
72 // walking the frame trees and textrun cache, it needs to mark
73 // textruns that have been seen so as to avoid multiple-accounting.
74 RunSizeAccounted = 0x2000,
76 // The following are defined by gfxTextRunFactory rather than here,
77 // so that it also has access to the _INCOMING and MATH_SCRIPT flags
78 // for shaping purposes.
79 // They live in the gfxShapedText::mFlags field rather than the
80 // gfxTextRun::mFlags2 field.
81 // TEXT_TRAILING_ARABICCHAR
82 // TEXT_INCOMING_ARABICCHAR
83 // TEXT_USE_MATH_SCRIPT
86 // These constants are used in TransformText to represent context information
87 // from previous textruns.
88 enum { INCOMING_NONE = 0, INCOMING_WHITESPACE = 1, INCOMING_ARABICCHAR = 2 };
90 /**
91 * Returns true if aChars/aLength are something that make a space
92 * character not be whitespace when they follow the space character
93 * (combining mark or join control, ignoring intervening direction
94 * controls).
96 static bool IsSpaceCombiningSequenceTail(const char16_t* aChars,
97 int32_t aLength);
98 static bool IsSpaceCombiningSequenceTail(const uint8_t* aChars,
99 int32_t aLength) {
100 return false;
103 enum CompressionMode {
104 COMPRESS_NONE,
105 COMPRESS_WHITESPACE,
106 COMPRESS_WHITESPACE_NEWLINE,
107 COMPRESS_NONE_TRANSFORM_TO_SPACE
111 * Create a text run from a run of Unicode text. The text may have whitespace
112 * compressed. A preformatted tab is sent to the text run as a single space.
113 * (Tab spacing must be performed by textframe later.) Certain other
114 * characters are discarded.
116 * @param aCompression control what is compressed to a
117 * single space character: no compression, compress spaces (not followed
118 * by combining mark) and tabs, compress those plus newlines, or
119 * no compression except newlines are discarded.
120 * @param aIncomingFlags a flag indicating whether there was whitespace
121 * or an Arabic character preceding this text. We set it to indicate if
122 * there's an Arabic character or whitespace preceding the end of this text.
124 template <class CharT>
125 static CharT* TransformText(const CharT* aText, uint32_t aLength,
126 CharT* aOutput, CompressionMode aCompression,
127 uint8_t* aIncomingFlags, gfxSkipChars* aSkipChars,
128 nsTextFrameUtils::Flags* aAnalysisFlags);
131 * Returns whether aChar is a character that nsTextFrameUtils::TransformText
132 * might mark as skipped. This is used by
133 * SVGTextContentElement::GetNumberOfChars to know whether reflowing frames,
134 * so that we have the results of TransformText, is required, or whether we
135 * can use a fast path instead.
137 template <class CharT>
138 static bool IsSkippableCharacterForTransformText(CharT aChar);
140 static void AppendLineBreakOffset(nsTArray<uint32_t>* aArray,
141 uint32_t aOffset) {
142 if (aArray->Length() > 0 && (*aArray)[aArray->Length() - 1] == aOffset) {
143 return;
145 aArray->AppendElement(aOffset);
148 static uint32_t ComputeApproximateLengthWithWhitespaceCompression(
149 mozilla::dom::Text*, const nsStyleText*);
150 static uint32_t ComputeApproximateLengthWithWhitespaceCompression(
151 const nsAString&, const nsStyleText*);
154 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(nsTextFrameUtils::Flags)
156 class nsSkipCharsRunIterator {
157 public:
158 enum LengthMode {
159 LENGTH_UNSKIPPED_ONLY = false,
160 LENGTH_INCLUDES_SKIPPED = true
162 nsSkipCharsRunIterator(const gfxSkipCharsIterator& aStart,
163 LengthMode aLengthIncludesSkipped, uint32_t aLength)
164 : mIterator(aStart),
165 mRemainingLength(aLength),
166 mRunLength(0),
167 mSkipped(false),
168 mVisitSkipped(false),
169 mLengthIncludesSkipped(aLengthIncludesSkipped) {}
170 void SetVisitSkipped() { mVisitSkipped = true; }
171 void SetOriginalOffset(int32_t aOffset) {
172 mIterator.SetOriginalOffset(aOffset);
174 void SetSkippedOffset(uint32_t aOffset) {
175 mIterator.SetSkippedOffset(aOffset);
178 // guaranteed to return only positive-length runs
179 bool NextRun();
180 bool IsSkipped() const { return mSkipped; }
181 // Always returns something > 0
182 int32_t GetRunLength() const { return mRunLength; }
183 const gfxSkipCharsIterator& GetPos() const { return mIterator; }
184 int32_t GetOriginalOffset() const { return mIterator.GetOriginalOffset(); }
185 uint32_t GetSkippedOffset() const { return mIterator.GetSkippedOffset(); }
187 private:
188 gfxSkipCharsIterator mIterator;
189 int32_t mRemainingLength;
190 int32_t mRunLength;
191 bool mSkipped;
192 bool mVisitSkipped;
193 bool mLengthIncludesSkipped;
196 #endif /*NSTEXTFRAMEUTILS_H_*/