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; |
| 24 | fHasBackground = false; |
| 25 | fHasForeground = false; |
| 26 | fTextBaseline = TextBaseline::kAlphabetic; |
Julia Lavrova | 35f8822 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 27 | fLocale = ""; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | bool TextStyle::equals(const TextStyle& other) const { |
| 31 | if (fColor != other.fColor) { |
| 32 | return false; |
| 33 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 34 | if (!(fDecoration == other.fDecoration)) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 35 | return false; |
| 36 | } |
| 37 | if (!(fFontStyle == other.fFontStyle)) { |
| 38 | return false; |
| 39 | } |
| 40 | if (fFontFamilies != other.fFontFamilies) { |
| 41 | return false; |
| 42 | } |
| 43 | if (fLetterSpacing != other.fLetterSpacing) { |
| 44 | return false; |
| 45 | } |
| 46 | if (fWordSpacing != other.fWordSpacing) { |
| 47 | return false; |
| 48 | } |
| 49 | if (fHeight != other.fHeight) { |
| 50 | return false; |
| 51 | } |
| 52 | if (fFontSize != other.fFontSize) { |
| 53 | return false; |
| 54 | } |
| 55 | if (fLocale != other.fLocale) { |
| 56 | return false; |
| 57 | } |
| 58 | if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) { |
| 59 | return false; |
| 60 | } |
| 61 | if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) { |
| 62 | return false; |
| 63 | } |
| 64 | if (fTextShadows.size() != other.fTextShadows.size()) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) { |
| 69 | if (fTextShadows[i] != other.fTextShadows[i]) { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const { |
| 78 | switch (styleType) { |
| 79 | case kForeground: |
| 80 | if (fHasForeground) { |
| 81 | return other.fHasForeground && fForeground == other.fForeground; |
| 82 | } else { |
| 83 | return !other.fHasForeground && fColor == other.fColor; |
| 84 | } |
| 85 | |
| 86 | case kBackground: |
| 87 | return (fHasBackground == other.fHasBackground && fBackground == other.fBackground); |
| 88 | |
| 89 | case kShadow: |
| 90 | if (fTextShadows.size() != other.fTextShadows.size()) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) { |
| 95 | if (fTextShadows[i] != other.fTextShadows[i]) { |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | return true; |
| 100 | |
| 101 | case kDecorations: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 102 | return this->fDecoration == other.fDecoration; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 103 | |
| 104 | case kLetterSpacing: |
| 105 | return fLetterSpacing == other.fLetterSpacing; |
| 106 | |
| 107 | case kWordSpacing: |
| 108 | return fWordSpacing == other.fWordSpacing; |
| 109 | |
| 110 | case kAllAttributes: |
| 111 | return this->equals(other); |
| 112 | |
| 113 | case kFont: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 114 | // TODO: should not we take typefaces in account? |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 115 | return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies && |
| 116 | fFontSize == other.fFontSize && fHeight == other.fHeight; |
| 117 | |
| 118 | default: |
| 119 | SkASSERT(false); |
| 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } // namespace textlayout |
| 125 | } // namespace skia |