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