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