Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 2 | #include "modules/skparagraph/include/TextStyle.h" |
| 3 | #include <TextStyle.h> |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4 | #include "include/core/SkColor.h" |
| 5 | #include "include/core/SkFontStyle.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 6 | |
| 7 | namespace skia { |
| 8 | namespace textlayout { |
| 9 | |
| 10 | TextStyle::TextStyle() : fFontStyle() { |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 11 | fFontFamilies.reserve(1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 12 | fFontFamilies.emplace_back(DEFAULT_FONT_FAMILY); |
| 13 | fColor = SK_ColorWHITE; |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 14 | fDecoration.fType = TextDecoration::kNoDecoration; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 15 | // Does not make sense to draw a transparent object, so we use it as a default |
| 16 | // value to indicate no decoration color was set. |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 17 | fDecoration.fColor = SK_ColorTRANSPARENT; |
| 18 | fDecoration.fStyle = TextDecorationStyle::kSolid; |
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; |
| 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 | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool TextStyle::equals(const TextStyle& other) const { |
| 32 | if (fColor != other.fColor) { |
| 33 | return false; |
| 34 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 35 | if (!(fDecoration == other.fDecoration)) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 36 | return false; |
| 37 | } |
| 38 | if (!(fFontStyle == other.fFontStyle)) { |
| 39 | return false; |
| 40 | } |
| 41 | if (fFontFamilies != other.fFontFamilies) { |
| 42 | return false; |
| 43 | } |
| 44 | if (fLetterSpacing != other.fLetterSpacing) { |
| 45 | return false; |
| 46 | } |
| 47 | if (fWordSpacing != other.fWordSpacing) { |
| 48 | return false; |
| 49 | } |
| 50 | if (fHeight != other.fHeight) { |
| 51 | return false; |
| 52 | } |
| 53 | if (fFontSize != other.fFontSize) { |
| 54 | return false; |
| 55 | } |
| 56 | if (fLocale != other.fLocale) { |
| 57 | return false; |
| 58 | } |
| 59 | if (fHasForeground != other.fHasForeground || fForeground != other.fForeground) { |
| 60 | return false; |
| 61 | } |
| 62 | if (fHasBackground != other.fHasBackground || fBackground != other.fBackground) { |
| 63 | return false; |
| 64 | } |
| 65 | if (fTextShadows.size() != other.fTextShadows.size()) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | for (int32_t i = 0; i < (int32_t)fTextShadows.size(); ++i) { |
| 70 | if (fTextShadows[i] != other.fTextShadows[i]) { |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const { |
| 79 | switch (styleType) { |
| 80 | case kForeground: |
| 81 | if (fHasForeground) { |
| 82 | return other.fHasForeground && fForeground == other.fForeground; |
| 83 | } else { |
| 84 | return !other.fHasForeground && fColor == other.fColor; |
| 85 | } |
| 86 | |
| 87 | case kBackground: |
| 88 | return (fHasBackground == other.fHasBackground && fBackground == other.fBackground); |
| 89 | |
| 90 | case kShadow: |
| 91 | if (fTextShadows.size() != other.fTextShadows.size()) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | for (int32_t i = 0; i < SkToInt(fTextShadows.size()); ++i) { |
| 96 | if (fTextShadows[i] != other.fTextShadows[i]) { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | return true; |
| 101 | |
| 102 | case kDecorations: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 103 | return this->fDecoration == other.fDecoration; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 104 | |
| 105 | case kLetterSpacing: |
| 106 | return fLetterSpacing == other.fLetterSpacing; |
| 107 | |
| 108 | case kWordSpacing: |
| 109 | return fWordSpacing == other.fWordSpacing; |
| 110 | |
| 111 | case kAllAttributes: |
| 112 | return this->equals(other); |
| 113 | |
| 114 | case kFont: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame^] | 115 | // TODO: should not we take typefaces in account? |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 116 | return fFontStyle == other.fFontStyle && fFontFamilies == other.fFontFamilies && |
| 117 | fFontSize == other.fFontSize && fHeight == other.fHeight; |
| 118 | |
| 119 | default: |
| 120 | SkASSERT(false); |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } // namespace textlayout |
| 126 | } // namespace skia |