Line data Source code
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/. */
5 :
6 : #ifndef NSTEXTRUNTRANSFORMATIONS_H_
7 : #define NSTEXTRUNTRANSFORMATIONS_H_
8 :
9 : #include "mozilla/Attributes.h"
10 : #include "mozilla/MemoryReporting.h"
11 : #include "mozilla/UniquePtr.h"
12 : #include "gfxTextRun.h"
13 : #include "nsStyleContext.h"
14 :
15 : class nsTransformedTextRun;
16 :
17 : struct nsTransformedCharStyle final {
18 0 : NS_INLINE_DECL_REFCOUNTING(nsTransformedCharStyle)
19 :
20 0 : explicit nsTransformedCharStyle(nsStyleContext* aContext)
21 0 : : mFont(aContext->StyleFont()->mFont)
22 0 : , mLanguage(aContext->StyleFont()->mLanguage)
23 : , mPresContext(aContext->PresContext())
24 0 : , mScriptSizeMultiplier(aContext->StyleFont()->mScriptSizeMultiplier)
25 0 : , mTextTransform(aContext->StyleText()->mTextTransform)
26 0 : , mMathVariant(aContext->StyleFont()->mMathVariant)
27 0 : , mExplicitLanguage(aContext->StyleFont()->mExplicitLanguage) {}
28 :
29 : nsFont mFont;
30 : nsCOMPtr<nsIAtom> mLanguage;
31 : RefPtr<nsPresContext> mPresContext;
32 : float mScriptSizeMultiplier;
33 : uint8_t mTextTransform;
34 : uint8_t mMathVariant;
35 : bool mExplicitLanguage;
36 : bool mForceNonFullWidth = false;
37 :
38 : private:
39 0 : ~nsTransformedCharStyle() {}
40 : nsTransformedCharStyle(const nsTransformedCharStyle& aOther) = delete;
41 : nsTransformedCharStyle& operator=(const nsTransformedCharStyle& aOther) = delete;
42 : };
43 :
44 0 : class nsTransformingTextRunFactory {
45 : public:
46 0 : virtual ~nsTransformingTextRunFactory() {}
47 :
48 : // Default 8-bit path just transforms to Unicode and takes that path
49 : already_AddRefed<nsTransformedTextRun>
50 : MakeTextRun(const uint8_t* aString, uint32_t aLength,
51 : const gfxFontGroup::Parameters* aParams,
52 : gfxFontGroup* aFontGroup,
53 : mozilla::gfx::ShapedTextFlags aFlags,
54 : nsTextFrameUtils::Flags aFlags2,
55 : nsTArray<RefPtr<nsTransformedCharStyle>>&& aStyles,
56 : bool aOwnsFactory);
57 :
58 : already_AddRefed<nsTransformedTextRun>
59 : MakeTextRun(const char16_t* aString, uint32_t aLength,
60 : const gfxFontGroup::Parameters* aParams,
61 : gfxFontGroup* aFontGroup,
62 : mozilla::gfx::ShapedTextFlags aFlags,
63 : nsTextFrameUtils::Flags aFlags2,
64 : nsTArray<RefPtr<nsTransformedCharStyle>>&& aStyles,
65 : bool aOwnsFactory);
66 :
67 : virtual void RebuildTextRun(nsTransformedTextRun* aTextRun,
68 : mozilla::gfx::DrawTarget* aRefDrawTarget,
69 : gfxMissingFontRecorder* aMFR) = 0;
70 : };
71 :
72 : /**
73 : * Builds textruns that transform the text in some way (e.g., capitalize)
74 : * and then render the text using some other textrun implementation.
75 : */
76 0 : class nsCaseTransformTextRunFactory : public nsTransformingTextRunFactory {
77 : public:
78 : // We could add an optimization here so that when there is no inner
79 : // factory, no title-case conversion, and no upper-casing of SZLIG, we override
80 : // MakeTextRun (after making it virtual in the superclass) and have it
81 : // just convert the string to uppercase or lowercase and create the textrun
82 : // via the fontgroup.
83 :
84 : // Takes ownership of aInnerTransformTextRunFactory
85 0 : explicit nsCaseTransformTextRunFactory(mozilla::UniquePtr<nsTransformingTextRunFactory> aInnerTransformingTextRunFactory,
86 : bool aAllUppercase = false)
87 0 : : mInnerTransformingTextRunFactory(Move(aInnerTransformingTextRunFactory)),
88 0 : mAllUppercase(aAllUppercase) {}
89 :
90 : virtual void RebuildTextRun(nsTransformedTextRun* aTextRun,
91 : mozilla::gfx::DrawTarget* aRefDrawTarget,
92 : gfxMissingFontRecorder* aMFR) override;
93 :
94 : // Perform a transformation on the given string, writing the result into
95 : // aConvertedString. If aAllUppercase is true, the transform is (global)
96 : // upper-casing, and aLanguage is used to determine any language-specific
97 : // behavior; otherwise, an nsTransformedTextRun should be passed in
98 : // as aTextRun and its styles will be used to determine the transform(s)
99 : // to be applied.
100 : // If such an input textrun is provided, then its line-breaks and styles
101 : // will be copied to the output arrays, which must also be provided by
102 : // the caller. For the global upper-casing usage (no input textrun),
103 : // these are ignored.
104 : static bool TransformString(const nsAString& aString,
105 : nsString& aConvertedString,
106 : bool aAllUppercase,
107 : const nsIAtom* aLanguage,
108 : nsTArray<bool>& aCharsToMergeArray,
109 : nsTArray<bool>& aDeletedCharsArray,
110 : const nsTransformedTextRun* aTextRun = nullptr,
111 : uint32_t aOffsetInTextRun = 0,
112 : nsTArray<uint8_t>* aCanBreakBeforeArray = nullptr,
113 : nsTArray<RefPtr<nsTransformedCharStyle>>* aStyleArray = nullptr);
114 :
115 : protected:
116 : mozilla::UniquePtr<nsTransformingTextRunFactory> mInnerTransformingTextRunFactory;
117 : bool mAllUppercase;
118 : };
119 :
120 : /**
121 : * So that we can reshape as necessary, we store enough information
122 : * to fully rebuild the textrun contents.
123 : */
124 : class nsTransformedTextRun final : public gfxTextRun {
125 : public:
126 :
127 : static already_AddRefed<nsTransformedTextRun>
128 : Create(const gfxTextRunFactory::Parameters* aParams,
129 : nsTransformingTextRunFactory* aFactory,
130 : gfxFontGroup* aFontGroup,
131 : const char16_t* aString, uint32_t aLength,
132 : const mozilla::gfx::ShapedTextFlags aFlags,
133 : const nsTextFrameUtils::Flags aFlags2,
134 : nsTArray<RefPtr<nsTransformedCharStyle>>&& aStyles,
135 : bool aOwnsFactory);
136 :
137 0 : ~nsTransformedTextRun() {
138 0 : if (mOwnsFactory) {
139 0 : delete mFactory;
140 : }
141 0 : }
142 :
143 : void SetCapitalization(uint32_t aStart, uint32_t aLength,
144 : bool* aCapitalization);
145 : virtual bool SetPotentialLineBreaks(Range aRange,
146 : const uint8_t* aBreakBefore);
147 : /**
148 : * Called after SetCapitalization and SetPotentialLineBreaks
149 : * are done and before we request any data from the textrun. Also always
150 : * called after a Create.
151 : */
152 0 : void FinishSettingProperties(mozilla::gfx::DrawTarget* aRefDrawTarget,
153 : gfxMissingFontRecorder* aMFR)
154 : {
155 0 : if (mNeedsRebuild) {
156 0 : mNeedsRebuild = false;
157 0 : mFactory->RebuildTextRun(this, aRefDrawTarget, aMFR);
158 : }
159 0 : }
160 :
161 : // override the gfxTextRun impls to account for additional members here
162 : virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) MOZ_MUST_OVERRIDE;
163 : virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) MOZ_MUST_OVERRIDE;
164 :
165 : nsTransformingTextRunFactory *mFactory;
166 : nsTArray<RefPtr<nsTransformedCharStyle>> mStyles;
167 : nsTArray<bool> mCapitalize;
168 : nsString mString;
169 : bool mOwnsFactory;
170 : bool mNeedsRebuild;
171 :
172 : private:
173 0 : nsTransformedTextRun(const gfxTextRunFactory::Parameters* aParams,
174 : nsTransformingTextRunFactory* aFactory,
175 : gfxFontGroup* aFontGroup,
176 : const char16_t* aString, uint32_t aLength,
177 : const mozilla::gfx::ShapedTextFlags aFlags,
178 : const nsTextFrameUtils::Flags aFlags2,
179 : nsTArray<RefPtr<nsTransformedCharStyle>>&& aStyles,
180 : bool aOwnsFactory)
181 0 : : gfxTextRun(aParams, aLength, aFontGroup, aFlags, aFlags2),
182 : mFactory(aFactory), mStyles(aStyles), mString(aString, aLength),
183 0 : mOwnsFactory(aOwnsFactory), mNeedsRebuild(true)
184 : {
185 0 : mCharacterGlyphs = reinterpret_cast<CompressedGlyph*>(this + 1);
186 0 : }
187 : };
188 :
189 : /**
190 : * Copy a given textrun, but merge certain characters into a single logical
191 : * character. Glyphs for a character are added to the glyph list for the previous
192 : * character and then the merged character is eliminated. Visually the results
193 : * are identical.
194 : *
195 : * This is used for text-transform:uppercase when we encounter a SZLIG,
196 : * whose uppercase form is "SS", or other ligature or precomposed form
197 : * that expands to multiple codepoints during case transformation,
198 : * and for Greek text when combining diacritics have been deleted.
199 : *
200 : * This function is unable to merge characters when they occur in different
201 : * glyph runs. This only happens in tricky edge cases where a character was
202 : * decomposed by case-mapping (e.g. there's no precomposed uppercase version
203 : * of an accented lowercase letter), and then font-matching caused the
204 : * diacritics to be assigned to a different font than the base character.
205 : * In this situation, the diacritic(s) get discarded, which is less than
206 : * ideal, but they probably weren't going to render very well anyway.
207 : * Bug 543200 will improve this by making font-matching operate on entire
208 : * clusters instead of individual codepoints.
209 : *
210 : * For simplicity, this produces a textrun containing all DetailedGlyphs,
211 : * no simple glyphs. So don't call it unless you really have merging to do.
212 : *
213 : * @param aCharsToMerge when aCharsToMerge[i] is true, this character in aSrc
214 : * is merged into the previous character
215 : *
216 : * @param aDeletedChars when aDeletedChars[i] is true, the character at this
217 : * position in aDest was deleted (has no corresponding char in aSrc)
218 : */
219 : void
220 : MergeCharactersInTextRun(gfxTextRun* aDest, gfxTextRun* aSrc,
221 : const bool* aCharsToMerge, const bool* aDeletedChars);
222 :
223 : gfxTextRunFactory::Parameters
224 : GetParametersForInner(nsTransformedTextRun* aTextRun,
225 : mozilla::gfx::ShapedTextFlags* aFlags,
226 : mozilla::gfx::DrawTarget* aRefDrawTarget);
227 :
228 :
229 : #endif /*NSTEXTRUNTRANSFORMATIONS_H_*/
|