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