Bumping manifests a=b2g-bump
[gecko.git] / layout / generic / nsTextRunTransformations.h
blob2e7f987413ebf43e849587a0659086ac17dab22b
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 NSTEXTRUNTRANSFORMATIONS_H_
7 #define NSTEXTRUNTRANSFORMATIONS_H_
9 #include "mozilla/Attributes.h"
10 #include "mozilla/MemoryReporting.h"
11 #include "gfxFont.h"
12 #include "nsStyleContext.h"
14 class nsTransformedTextRun;
16 class nsTransformingTextRunFactory {
17 public:
18 virtual ~nsTransformingTextRunFactory() {}
20 // Default 8-bit path just transforms to Unicode and takes that path
21 nsTransformedTextRun* MakeTextRun(const uint8_t* aString, uint32_t aLength,
22 const gfxFontGroup::Parameters* aParams,
23 gfxFontGroup* aFontGroup, uint32_t aFlags,
24 nsStyleContext** aStyles, bool aOwnsFactory = true);
25 nsTransformedTextRun* MakeTextRun(const char16_t* aString, uint32_t aLength,
26 const gfxFontGroup::Parameters* aParams,
27 gfxFontGroup* aFontGroup, uint32_t aFlags,
28 nsStyleContext** aStyles, bool aOwnsFactory = true);
30 virtual void RebuildTextRun(nsTransformedTextRun* aTextRun, gfxContext* aRefContext) = 0;
33 /**
34 * Builds textruns that transform the text in some way (e.g., capitalize)
35 * and then render the text using some other textrun implementation.
37 class nsCaseTransformTextRunFactory : public nsTransformingTextRunFactory {
38 public:
39 // We could add an optimization here so that when there is no inner
40 // factory, no title-case conversion, and no upper-casing of SZLIG, we override
41 // MakeTextRun (after making it virtual in the superclass) and have it
42 // just convert the string to uppercase or lowercase and create the textrun
43 // via the fontgroup.
45 // Takes ownership of aInnerTransformTextRunFactory
46 explicit nsCaseTransformTextRunFactory(nsTransformingTextRunFactory* aInnerTransformingTextRunFactory,
47 bool aAllUppercase = false)
48 : mInnerTransformingTextRunFactory(aInnerTransformingTextRunFactory),
49 mAllUppercase(aAllUppercase) {}
51 virtual void RebuildTextRun(nsTransformedTextRun* aTextRun, gfxContext* aRefContext) MOZ_OVERRIDE;
53 // Perform a transformation on the given string, writing the result into
54 // aConvertedString. If aAllUppercase is true, the transform is (global)
55 // upper-casing, and aLanguage is used to determine any language-specific
56 // behavior; otherwise, an nsTransformedTextRun should be passed in
57 // as aTextRun and its styles will be used to determine the transform(s)
58 // to be applied.
59 // If such an input textrun is provided, then its line-breaks and styles
60 // will be copied to the output arrays, which must also be provided by
61 // the caller. For the global upper-casing usage (no input textrun),
62 // these are ignored.
63 static bool TransformString(const nsAString& aString,
64 nsString& aConvertedString,
65 bool aAllUppercase,
66 const nsIAtom* aLanguage,
67 nsTArray<bool>& aCharsToMergeArray,
68 nsTArray<bool>& aDeletedCharsArray,
69 nsTransformedTextRun* aTextRun = nullptr,
70 nsTArray<uint8_t>* aCanBreakBeforeArray = nullptr,
71 nsTArray<nsStyleContext*>* aStyleArray = nullptr);
73 protected:
74 nsAutoPtr<nsTransformingTextRunFactory> mInnerTransformingTextRunFactory;
75 bool mAllUppercase;
78 /**
79 * So that we can reshape as necessary, we store enough information
80 * to fully rebuild the textrun contents.
82 class nsTransformedTextRun MOZ_FINAL : public gfxTextRun {
83 public:
84 static nsTransformedTextRun *Create(const gfxTextRunFactory::Parameters* aParams,
85 nsTransformingTextRunFactory* aFactory,
86 gfxFontGroup* aFontGroup,
87 const char16_t* aString, uint32_t aLength,
88 const uint32_t aFlags, nsStyleContext** aStyles,
89 bool aOwnsFactory);
91 ~nsTransformedTextRun() {
92 if (mOwnsFactory) {
93 delete mFactory;
97 void SetCapitalization(uint32_t aStart, uint32_t aLength,
98 bool* aCapitalization,
99 gfxContext* aRefContext);
100 virtual bool SetPotentialLineBreaks(uint32_t aStart, uint32_t aLength,
101 uint8_t* aBreakBefore,
102 gfxContext* aRefContext);
104 * Called after SetCapitalization and SetPotentialLineBreaks
105 * are done and before we request any data from the textrun. Also always
106 * called after a Create.
108 void FinishSettingProperties(gfxContext* aRefContext)
110 if (mNeedsRebuild) {
111 mNeedsRebuild = false;
112 mFactory->RebuildTextRun(this, aRefContext);
116 // override the gfxTextRun impls to account for additional members here
117 virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) MOZ_MUST_OVERRIDE;
118 virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) MOZ_MUST_OVERRIDE;
120 nsTransformingTextRunFactory *mFactory;
121 nsTArray<nsRefPtr<nsStyleContext> > mStyles;
122 nsTArray<bool> mCapitalize;
123 nsString mString;
124 bool mOwnsFactory;
125 bool mNeedsRebuild;
127 private:
128 nsTransformedTextRun(const gfxTextRunFactory::Parameters* aParams,
129 nsTransformingTextRunFactory* aFactory,
130 gfxFontGroup* aFontGroup,
131 const char16_t* aString, uint32_t aLength,
132 const uint32_t aFlags, nsStyleContext** aStyles,
133 bool aOwnsFactory)
134 : gfxTextRun(aParams, aLength, aFontGroup, aFlags),
135 mFactory(aFactory), mString(aString, aLength),
136 mOwnsFactory(aOwnsFactory), mNeedsRebuild(true)
138 mCharacterGlyphs = reinterpret_cast<CompressedGlyph*>(this + 1);
140 uint32_t i;
141 for (i = 0; i < aLength; ++i) {
142 mStyles.AppendElement(aStyles[i]);
148 * Copy a given textrun, but merge certain characters into a single logical
149 * character. Glyphs for a character are added to the glyph list for the previous
150 * character and then the merged character is eliminated. Visually the results
151 * are identical.
153 * This is used for text-transform:uppercase when we encounter a SZLIG,
154 * whose uppercase form is "SS", or other ligature or precomposed form
155 * that expands to multiple codepoints during case transformation,
156 * and for Greek text when combining diacritics have been deleted.
158 * This function is unable to merge characters when they occur in different
159 * glyph runs. This only happens in tricky edge cases where a character was
160 * decomposed by case-mapping (e.g. there's no precomposed uppercase version
161 * of an accented lowercase letter), and then font-matching caused the
162 * diacritics to be assigned to a different font than the base character.
163 * In this situation, the diacritic(s) get discarded, which is less than
164 * ideal, but they probably weren't going to render very well anyway.
165 * Bug 543200 will improve this by making font-matching operate on entire
166 * clusters instead of individual codepoints.
168 * For simplicity, this produces a textrun containing all DetailedGlyphs,
169 * no simple glyphs. So don't call it unless you really have merging to do.
171 * @param aCharsToMerge when aCharsToMerge[i] is true, this character in aSrc
172 * is merged into the previous character
174 * @param aDeletedChars when aDeletedChars[i] is true, the character at this
175 * position in aDest was deleted (has no corresponding char in aSrc)
177 void
178 MergeCharactersInTextRun(gfxTextRun* aDest, gfxTextRun* aSrc,
179 const bool* aCharsToMerge, const bool* aDeletedChars);
181 gfxTextRunFactory::Parameters
182 GetParametersForInner(nsTransformedTextRun* aTextRun, uint32_t* aFlags,
183 gfxContext* aRefContext);
186 #endif /*NSTEXTRUNTRANSFORMATIONS_H_*/