Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2 | #include "include/core/SkColor.h" |
| 3 | #include "include/core/SkFontStyle.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 4 | #include "modules/skparagraph/include/TextStyle.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 5 | |
| 6 | namespace skia { |
| 7 | namespace textlayout { |
| 8 | |
| 9 | TextStyle::TextStyle() : fFontStyle() { |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 10 | fFontFamilies.reserve(1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 11 | fFontFamilies.emplace_back(DEFAULT_FONT_FAMILY); |
| 12 | fColor = SK_ColorWHITE; |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 13 | fDecoration.fType = TextDecoration::kNoDecoration; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 14 | // Does not make sense to draw a transparent object, so we use it as a default |
| 15 | // value to indicate no decoration color was set. |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 16 | fDecoration.fColor = SK_ColorTRANSPARENT; |
| 17 | fDecoration.fStyle = TextDecorationStyle::kSolid; |
Julia Lavrova | 568f35f | 2020-05-14 14:23:19 -0400 | [diff] [blame] | 18 | // TODO: switch back to kGaps when (if) switching flutter to skparagraph |
| 19 | fDecoration.fMode = TextDecorationMode::kThrough; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 20 | // Thickness is applied as a multiplier to the default thickness of the font. |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 21 | fDecoration.fThicknessMultiplier = 1.0; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 22 | fFontSize = 14.0; |
| 23 | fLetterSpacing = 0.0; |
| 24 | fWordSpacing = 0.0; |
| 25 | fHeight = 1.0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 26 | fHeightOverride = false; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 27 | fHasBackground = false; |
| 28 | fHasForeground = false; |
| 29 | fTextBaseline = TextBaseline::kAlphabetic; |
Julia Lavrova | 35f8822 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 30 | fLocale = ""; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 31 | fIsPlaceholder = false; |
| 32 | } |
| 33 | |
| 34 | TextStyle:: TextStyle(const TextStyle& other, bool placeholder) { |
| 35 | fColor = other.fColor; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 36 | fFontSize = other.fFontSize; |
| 37 | fFontFamilies = other.fFontFamilies; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 38 | fDecoration = other.fDecoration; |
| 39 | fHasBackground = other.fHasBackground; |
| 40 | fHasForeground = other.fHasForeground; |
| 41 | fBackground = other.fBackground; |
| 42 | fForeground = other.fForeground; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 43 | fHeightOverride = other.fHeightOverride; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 44 | fIsPlaceholder = placeholder; |
Julia Lavrova | c5313e6 | 2019-12-10 12:11:17 -0500 | [diff] [blame] | 45 | fFontFeatures = other.fFontFeatures; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | bool TextStyle::equals(const TextStyle& other) const { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 49 | |
| 50 | if (fIsPlaceholder || other.fIsPlaceholder) { |
| 51 | return false; |
| 52 | } |
| 53 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 54 | if (fColor != other.fColor) { |
| 55 | return false; |
| 56 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 57 | if (!(fDecoration == other.fDecoration)) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | if (!(fFontStyle == other.fFontStyle)) { |
| 61 | return false; |
| 62 | } |
| 63 | if (fFontFamilies != other.fFontFamilies) { |
| 64 | return false; |
| 65 | } |
| 66 | if (fLetterSpacing != other.fLetterSpacing) { |
| 67 | return false; |
| 68 | } |
| 69 | if (fWordSpacing != other.fWordSpacing) { |
| 70 | return false; |
| 71 | } |
| 72 | if (fHeight != other.fHeight) { |
| 73 | return false; |
| 74 | } |
| 75 | if (fFontSize != other.fFontSize) { |
| 76 | return false; |
| 77 | } |
| 78 | if (fLocale != other.fLocale) { |
| 79 | return false; |
| 80 | } |
| 81 | if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) { |
| 82 | return false; |
| 83 | } |
| 84 | if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) { |
| 85 | return false; |
| 86 | } |
| 87 | if (fTextShadows.size() != other.fTextShadows.size()) { |
| 88 | return false; |
| 89 | } |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 90 | for (size_t i = 0; i < fTextShadows.size(); ++i) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 91 | if (fTextShadows[i] != other.fTextShadows[i]) { |
| 92 | return false; |
| 93 | } |
| 94 | } |
Julia Lavrova | c5313e6 | 2019-12-10 12:11:17 -0500 | [diff] [blame] | 95 | if (fFontFeatures.size() != other.fFontFeatures.size()) { |
| 96 | return false; |
| 97 | } |
| 98 | for (size_t i = 0; i < fFontFeatures.size(); ++i) { |
| 99 | if (!(fFontFeatures[i] == other.fFontFeatures[i])) { |
| 100 | return false; |
| 101 | } |
| 102 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
Julia Lavrova | 4cf1874 | 2020-01-14 13:24:45 -0500 | [diff] [blame] | 107 | bool TextStyle::equalsByFonts(const TextStyle& that) const { |
| 108 | |
| 109 | return !fIsPlaceholder && !that.fIsPlaceholder && |
| 110 | fFontStyle == that.fFontStyle && |
| 111 | fFontFamilies == that.fFontFamilies && |
Julia Lavrova | d3a32c5 | 2020-02-03 09:43:52 -0500 | [diff] [blame] | 112 | fFontFeatures == that.fFontFeatures && |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 113 | nearlyEqual(fLetterSpacing, that.fLetterSpacing) && |
| 114 | nearlyEqual(fWordSpacing, that.fWordSpacing) && |
| 115 | nearlyEqual(fHeight, that.fHeight) && |
| 116 | nearlyEqual(fFontSize, that.fFontSize) && |
Julia Lavrova | 4cf1874 | 2020-01-14 13:24:45 -0500 | [diff] [blame] | 117 | fLocale == that.fLocale; |
| 118 | } |
| 119 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 120 | bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const { |
| 121 | switch (styleType) { |
| 122 | case kForeground: |
Julia Lavrova | 1d784c7 | 2020-07-08 14:08:29 -0400 | [diff] [blame^] | 123 | return (!fHasForeground && !other.fHasForeground && fColor == other.fColor) || |
| 124 | ( fHasForeground && other.fHasForeground && fForeground == other.fForeground); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 125 | |
| 126 | case kBackground: |
Julia Lavrova | 1d784c7 | 2020-07-08 14:08:29 -0400 | [diff] [blame^] | 127 | return (!fHasBackground && !other.fHasBackground) || |
| 128 | ( fHasBackground && other.fHasBackground && fBackground == other.fBackground); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 129 | |
| 130 | case kShadow: |
| 131 | if (fTextShadows.size() != other.fTextShadows.size()) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) { |
| 136 | if (fTextShadows[i] != other.fTextShadows[i]) { |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | return true; |
| 141 | |
| 142 | case kDecorations: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 143 | return this->fDecoration == other.fDecoration; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 144 | |
| 145 | case kLetterSpacing: |
| 146 | return fLetterSpacing == other.fLetterSpacing; |
| 147 | |
| 148 | case kWordSpacing: |
| 149 | return fWordSpacing == other.fWordSpacing; |
| 150 | |
| 151 | case kAllAttributes: |
| 152 | return this->equals(other); |
| 153 | |
| 154 | case kFont: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 155 | // TODO: should not we take typefaces in account? |
Julia Lavrova | 76ae22e | 2020-02-26 12:14:18 -0500 | [diff] [blame] | 156 | return fFontStyle == other.fFontStyle && |
| 157 | fLocale == other.fLocale && |
| 158 | fFontFamilies == other.fFontFamilies && |
| 159 | fFontSize == other.fFontSize && |
| 160 | fHeight == other.fHeight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 161 | default: |
| 162 | SkASSERT(false); |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 167 | void TextStyle::getFontMetrics(SkFontMetrics* metrics) const { |
| 168 | SkFont font(fTypeface, fFontSize); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 169 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 170 | font.setSubpixel(true); |
| 171 | font.setHinting(SkFontHinting::kSlight); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 172 | font.getMetrics(metrics); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 173 | if (fHeightOverride) { |
| 174 | auto multiplier = fHeight * fFontSize; |
| 175 | auto height = metrics->fDescent - metrics->fAscent + metrics->fLeading; |
| 176 | metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2) * multiplier / height; |
| 177 | metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2) * multiplier / height; |
| 178 | |
| 179 | } else { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 180 | metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2); |
| 181 | metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Julia Lavrova | 4cf1874 | 2020-01-14 13:24:45 -0500 | [diff] [blame] | 185 | bool PlaceholderStyle::equals(const PlaceholderStyle& other) const { |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 186 | return nearlyEqual(fWidth, other.fWidth) && |
| 187 | nearlyEqual(fHeight, other.fHeight) && |
Julia Lavrova | aec9c84 | 2020-01-24 10:58:56 -0500 | [diff] [blame] | 188 | fAlignment == other.fAlignment && |
| 189 | fBaseline == other.fBaseline && |
Julia Lavrova | 7ad393e | 2020-01-30 09:48:51 -0500 | [diff] [blame] | 190 | (fAlignment != PlaceholderAlignment::kBaseline || |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 191 | nearlyEqual(fBaselineOffset, other.fBaselineOffset)); |
Julia Lavrova | 4cf1874 | 2020-01-14 13:24:45 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 194 | } // namespace textlayout |
| 195 | } // namespace skia |