nsTextPaintStyle.h (6373B)
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 nsTextPaintStyle_h__ 8 #define nsTextPaintStyle_h__ 9 10 #include "mozilla/Attributes.h" 11 #include "mozilla/ComputedStyle.h" 12 #include "mozilla/EnumeratedArray.h" 13 #include "mozilla/Span.h" 14 #include "nsAtomHashKeys.h" 15 #include "nsISelectionController.h" 16 #include "nsTHashMap.h" 17 18 class nsTextFrame; 19 class nsPresContext; 20 21 namespace mozilla { 22 enum class StyleTextDecorationStyle : uint8_t; 23 } 24 25 /** 26 * This helper object computes colors used for painting, and also IME 27 * underline information. The data is computed lazily and cached as necessary. 28 * These live for just the duration of one paint operation. 29 */ 30 class MOZ_STACK_CLASS nsTextPaintStyle { 31 using ComputedStyle = mozilla::ComputedStyle; 32 using SelectionType = mozilla::SelectionType; 33 using StyleTextDecorationStyle = mozilla::StyleTextDecorationStyle; 34 using StyleSimpleShadow = mozilla::StyleSimpleShadow; 35 36 public: 37 explicit nsTextPaintStyle(nsTextFrame* aFrame); 38 39 void SetResolveColors(bool aResolveColors) { 40 mResolveColors = aResolveColors; 41 } 42 43 nscolor GetTextColor(); 44 45 // SVG text has its own painting process, so we should never get its stroke 46 // property from here. 47 nscolor GetWebkitTextStrokeColor(); 48 float GetWebkitTextStrokeWidth(); 49 50 // Index used to look up styles for different types of selection. 51 enum class SelectionStyleIndex : uint8_t { 52 RawInput = 0, 53 SelRawText, 54 ConvText, 55 SelConvText, 56 SpellChecker, 57 // Not an actual enum value; used to size the array of styles. 58 Count, 59 }; 60 61 /** 62 * Compute the colors for normally-selected text. Returns false if 63 * the normal selection is not being displayed. 64 */ 65 bool GetSelectionColors(nscolor* aForeColor, nscolor* aBackColor); 66 void GetHighlightColors(nscolor* aForeColor, nscolor* aBackColor); 67 void GetTargetTextColors(nscolor* aForeColor, nscolor* aBackColor); 68 // Computes colors for custom highlights. 69 // Returns false if there are no rules associated with `aHighlightName`. 70 bool GetCustomHighlightTextColor(nsAtom* aHighlightName, nscolor* aForeColor); 71 bool GetCustomHighlightBackgroundColor(nsAtom* aHighlightName, 72 nscolor* aBackColor); 73 RefPtr<ComputedStyle> GetComputedStyleForSelectionPseudo( 74 SelectionType aSelectionType, nsAtom* aHighlightName); 75 76 void GetURLSecondaryColor(nscolor* aForeColor); 77 void GetIMESelectionColors(SelectionStyleIndex aIndex, nscolor* aForeColor, 78 nscolor* aBackColor); 79 // if this returns false, we don't need to draw underline. 80 bool GetSelectionUnderlineForPaint(SelectionStyleIndex aIndex, 81 nscolor* aLineColor, float* aRelativeSize, 82 StyleTextDecorationStyle* aStyle); 83 84 // if this returns false, we don't need to draw underline. 85 static bool GetSelectionUnderline(nsIFrame*, SelectionStyleIndex aIndex, 86 nscolor* aLineColor, float* aRelativeSize, 87 StyleTextDecorationStyle* aStyle); 88 89 // if this returns false, no text-shadow was specified for the selection 90 // and the *aShadow parameter was not modified. 91 bool GetSelectionShadow(mozilla::Span<const StyleSimpleShadow>* aShadows); 92 93 nsPresContext* PresContext() const { return mPresContext; } 94 95 static SelectionStyleIndex GetUnderlineStyleIndexForSelectionType( 96 SelectionType aSelectionType) { 97 switch (aSelectionType) { 98 case SelectionType::eIMERawClause: 99 return SelectionStyleIndex::RawInput; 100 case SelectionType::eIMESelectedRawClause: 101 return SelectionStyleIndex::SelRawText; 102 case SelectionType::eIMEConvertedClause: 103 return SelectionStyleIndex::ConvText; 104 case SelectionType::eIMESelectedClause: 105 return SelectionStyleIndex::SelConvText; 106 case SelectionType::eSpellCheck: 107 return SelectionStyleIndex::SpellChecker; 108 default: 109 NS_WARNING("non-IME selection type"); 110 return SelectionStyleIndex::RawInput; 111 } 112 } 113 114 nscolor GetSystemFieldForegroundColor(); 115 nscolor GetSystemFieldBackgroundColor(); 116 117 protected: 118 nsTextFrame* mFrame; 119 nsPresContext* mPresContext; 120 bool mInitCommonColors; 121 bool mInitSelectionColorsAndShadow; 122 bool mResolveColors; 123 bool mInitTargetTextPseudoStyle; 124 125 // Selection data 126 127 nscolor mSelectionTextColor; 128 nscolor mSelectionBGColor; 129 RefPtr<ComputedStyle> mSelectionPseudoStyle; 130 RefPtr<ComputedStyle> mTargetTextPseudoStyle; 131 nsTHashMap<RefPtr<nsAtom>, RefPtr<ComputedStyle>> 132 mCustomHighlightPseudoStyles; 133 134 // Common data 135 136 int32_t mSufficientContrast; 137 nscolor mFrameBackgroundColor; 138 nscolor mSystemFieldForegroundColor; 139 nscolor mSystemFieldBackgroundColor; 140 141 // selection colors and underline info, the colors are resolved colors if 142 // mResolveColors is true (which is the default), i.e., the foreground color 143 // and background color are swapped if it's needed. And also line color will 144 // be resolved from them. 145 struct nsSelectionStyle { 146 nscolor mTextColor; 147 nscolor mBGColor; 148 nscolor mUnderlineColor; 149 StyleTextDecorationStyle mUnderlineStyle; 150 float mUnderlineRelativeSize; 151 }; 152 mozilla::EnumeratedArray<SelectionStyleIndex, 153 mozilla::Maybe<nsSelectionStyle>, 154 size_t(SelectionStyleIndex::Count)> 155 mSelectionStyle; 156 157 // Color initializations 158 void InitCommonColors(); 159 bool InitSelectionColorsAndShadow(); 160 void InitTargetTextPseudoStyle(); 161 162 nsSelectionStyle* SelectionStyle(SelectionStyleIndex aIndex); 163 nsSelectionStyle InitSelectionStyle(SelectionStyleIndex aIndex); 164 165 // Ensures sufficient contrast between the frame background color and the 166 // selection background color, and swaps the selection text and background 167 // colors accordingly. 168 bool EnsureSufficientContrast(nscolor* aForeColor, nscolor* aBackColor); 169 170 nscolor GetResolvedForeColor(nscolor aColor, nscolor aDefaultForeColor, 171 nscolor aBackColor); 172 }; 173 174 #endif // nsTextPaintStyle_h__