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