1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef NSTEXTFRAMEUTILS_H_
7 #define NSTEXTFRAMEUTILS_H_
9 #include "gfxSkipChars.h"
10 #include "nsBidiUtils.h"
11 #include "nsUnicodeProperties.h"
16 #define BIG_TEXT_NODE_SIZE 4096
20 #define CH_CJKSP 12288 // U+3000 IDEOGRAPHIC SPACE (CJK Full-Width Space)
22 class nsTextFrameUtils
{
24 // These constants are used as textrun flags for textframe textruns.
26 // The following flags are set by TransformText
28 // the text has at least one untransformed tab character
29 TEXT_HAS_TAB
= 0x010000,
30 // the original text has at least one soft hyphen character
31 TEXT_HAS_SHY
= 0x020000,
32 TEXT_WAS_TRANSFORMED
= 0x040000,
33 TEXT_UNUSED_FLAG
= 0x080000,
35 // The following flags are set by nsTextFrame
37 TEXT_IS_SIMPLE_FLOW
= 0x100000,
38 TEXT_INCOMING_WHITESPACE
= 0x200000,
39 TEXT_TRAILING_WHITESPACE
= 0x400000,
40 TEXT_COMPRESSED_LEADING_WHITESPACE
= 0x800000,
41 TEXT_NO_BREAKS
= 0x1000000,
42 TEXT_IS_TRANSFORMED
= 0x2000000,
43 // This gets set if there's a break opportunity at the end of the textrun.
44 // We normally don't use this break opportunity because the following text
45 // will have a break opportunity at the start, but it's useful for line
46 // layout to know about it in case the following content is not text
47 TEXT_HAS_TRAILING_BREAK
= 0x4000000,
49 // This is set if the textrun was created for a textframe whose
50 // NS_FRAME_IS_IN_SINGLE_CHAR_MI flag is set. This occurs if the textframe
51 // belongs to a MathML <mi> element whose embedded text consists of a
53 TEXT_IS_SINGLE_CHAR_MI
= 0x8000000
55 // The following are defined by gfxTextRunWordCache rather than here,
56 // so that it also has access to the _INCOMING flag
57 // TEXT_TRAILING_ARABICCHAR
58 // TEXT_INCOMING_ARABICCHAR
60 // This is defined in gfxTextRunFactory to allow access in gfxFont.
61 // TEXT_USE_MATH_SCRIPT
64 // These constants are used in TransformText to represent context information
65 // from previous textruns.
68 INCOMING_WHITESPACE
= 1,
69 INCOMING_ARABICCHAR
= 2
73 * Returns true if aChars/aLength are something that make a space
74 * character not be whitespace when they follow the space character
75 * (combining mark or join control, ignoring intervening direction
79 IsSpaceCombiningSequenceTail(const char16_t
* aChars
, int32_t aLength
) {
81 (mozilla::unicode::IsClusterExtender(aChars
[0]) ||
82 (IsBidiControl(aChars
[0]) &&
83 IsSpaceCombiningSequenceTail(aChars
+ 1, aLength
- 1)
88 enum CompressionMode
{
91 COMPRESS_WHITESPACE_NEWLINE
,
92 COMPRESS_NONE_TRANSFORM_TO_SPACE
96 * Create a text run from a run of Unicode text. The text may have whitespace
97 * compressed. A preformatted tab is sent to the text run as a single space.
98 * (Tab spacing must be performed by textframe later.) Certain other
99 * characters are discarded.
101 * @param aCompressWhitespace control what is compressed to a
102 * single space character: no compression, compress spaces (not followed
103 * by combining mark) and tabs, compress those plus newlines, or
104 * no compression except newlines are discarded.
105 * @param aIncomingFlags a flag indicating whether there was whitespace
106 * or an Arabic character preceding this text. We set it to indicate if
107 * there's an Arabic character or whitespace preceding the end of this text.
109 static char16_t
* TransformText(const char16_t
* aText
, uint32_t aLength
,
111 CompressionMode aCompression
,
112 uint8_t * aIncomingFlags
,
113 gfxSkipChars
* aSkipChars
,
114 uint32_t* aAnalysisFlags
);
116 static uint8_t* TransformText(const uint8_t* aText
, uint32_t aLength
,
118 CompressionMode aCompression
,
119 uint8_t * aIncomingFlags
,
120 gfxSkipChars
* aSkipChars
,
121 uint32_t* aAnalysisFlags
);
124 AppendLineBreakOffset(nsTArray
<uint32_t>* aArray
, uint32_t aOffset
)
126 if (aArray
->Length() > 0 && (*aArray
)[aArray
->Length() - 1] == aOffset
)
128 aArray
->AppendElement(aOffset
);
132 ComputeApproximateLengthWithWhitespaceCompression(nsIContent
*aContent
,
137 class nsSkipCharsRunIterator
{
140 LENGTH_UNSKIPPED_ONLY
= false,
141 LENGTH_INCLUDES_SKIPPED
= true
143 nsSkipCharsRunIterator(const gfxSkipCharsIterator
& aStart
,
144 LengthMode aLengthIncludesSkipped
, uint32_t aLength
)
145 : mIterator(aStart
), mRemainingLength(aLength
), mRunLength(0),
146 mVisitSkipped(false),
147 mLengthIncludesSkipped(aLengthIncludesSkipped
) {
149 void SetVisitSkipped() { mVisitSkipped
= true; }
150 void SetOriginalOffset(int32_t aOffset
) {
151 mIterator
.SetOriginalOffset(aOffset
);
153 void SetSkippedOffset(uint32_t aOffset
) {
154 mIterator
.SetSkippedOffset(aOffset
);
157 // guaranteed to return only positive-length runs
159 bool IsSkipped() const { return mSkipped
; }
160 // Always returns something > 0
161 int32_t GetRunLength() const { return mRunLength
; }
162 const gfxSkipCharsIterator
& GetPos() const { return mIterator
; }
163 int32_t GetOriginalOffset() const { return mIterator
.GetOriginalOffset(); }
164 uint32_t GetSkippedOffset() const { return mIterator
.GetSkippedOffset(); }
167 gfxSkipCharsIterator mIterator
;
168 int32_t mRemainingLength
;
172 bool mLengthIncludesSkipped
;
175 #endif /*NSTEXTFRAMEUTILS_H_*/