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