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