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