| 1 | // Copyright 2019 Google LLC. | 
|---|
| 2 | #include "include/core/SkColor.h" | 
|---|
| 3 | #include "include/core/SkFontStyle.h" | 
|---|
| 4 | #include "modules/skparagraph/include/TextStyle.h" | 
|---|
| 5 |  | 
|---|
| 6 | namespace skia { | 
|---|
| 7 | namespace textlayout { | 
|---|
| 8 |  | 
|---|
| 9 | TextStyle::TextStyle(const TextStyle& other, bool placeholder) { | 
|---|
| 10 | fColor = other.fColor; | 
|---|
| 11 | fFontSize = other.fFontSize; | 
|---|
| 12 | fFontFamilies = other.fFontFamilies; | 
|---|
| 13 | fDecoration = other.fDecoration; | 
|---|
| 14 | fHasBackground = other.fHasBackground; | 
|---|
| 15 | fHasForeground = other.fHasForeground; | 
|---|
| 16 | fBackground = other.fBackground; | 
|---|
| 17 | fForeground = other.fForeground; | 
|---|
| 18 | fHeightOverride = other.fHeightOverride; | 
|---|
| 19 | fIsPlaceholder = placeholder; | 
|---|
| 20 | fFontFeatures = other.fFontFeatures; | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | bool TextStyle::equals(const TextStyle& other) const { | 
|---|
| 24 |  | 
|---|
| 25 | if (fIsPlaceholder || other.fIsPlaceholder) { | 
|---|
| 26 | return false; | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | if (fColor != other.fColor) { | 
|---|
| 30 | return false; | 
|---|
| 31 | } | 
|---|
| 32 | if (!(fDecoration == other.fDecoration)) { | 
|---|
| 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 | } | 
|---|
| 65 | for (size_t i = 0; i < fTextShadows.size(); ++i) { | 
|---|
| 66 | if (fTextShadows[i] != other.fTextShadows[i]) { | 
|---|
| 67 | return false; | 
|---|
| 68 | } | 
|---|
| 69 | } | 
|---|
| 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 | } | 
|---|
| 78 |  | 
|---|
| 79 | return true; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | bool TextStyle::equalsByFonts(const TextStyle& that) const { | 
|---|
| 83 |  | 
|---|
| 84 | return !fIsPlaceholder && !that.fIsPlaceholder && | 
|---|
| 85 | fFontStyle == that.fFontStyle && | 
|---|
| 86 | fFontFamilies == that.fFontFamilies && | 
|---|
| 87 | fFontFeatures == that.fFontFeatures && | 
|---|
| 88 | nearlyEqual(fLetterSpacing, that.fLetterSpacing) && | 
|---|
| 89 | nearlyEqual(fWordSpacing, that.fWordSpacing) && | 
|---|
| 90 | nearlyEqual(fHeight, that.fHeight) && | 
|---|
| 91 | nearlyEqual(fFontSize, that.fFontSize) && | 
|---|
| 92 | fLocale == that.fLocale; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) const { | 
|---|
| 96 | switch (styleType) { | 
|---|
| 97 | case kForeground: | 
|---|
| 98 | return (!fHasForeground && !other.fHasForeground && fColor == other.fColor) || | 
|---|
| 99 | ( fHasForeground &&  other.fHasForeground && fForeground == other.fForeground); | 
|---|
| 100 |  | 
|---|
| 101 | case kBackground: | 
|---|
| 102 | return (!fHasBackground && !other.fHasBackground) || | 
|---|
| 103 | ( fHasBackground &&  other.fHasBackground && fBackground == other.fBackground); | 
|---|
| 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: | 
|---|
| 118 | return this->fDecoration == other.fDecoration; | 
|---|
| 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: | 
|---|
| 130 | // TODO: should not we take typefaces in account? | 
|---|
| 131 | return fFontStyle == other.fFontStyle && | 
|---|
| 132 | fLocale == other.fLocale && | 
|---|
| 133 | fFontFamilies == other.fFontFamilies && | 
|---|
| 134 | fFontSize == other.fFontSize && | 
|---|
| 135 | fHeight == other.fHeight; | 
|---|
| 136 | default: | 
|---|
| 137 | SkASSERT(false); | 
|---|
| 138 | return false; | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | void TextStyle::getFontMetrics(SkFontMetrics* metrics) const { | 
|---|
| 143 | SkFont font(fTypeface, fFontSize); | 
|---|
| 144 | font.setEdging(SkFont::Edging::kAntiAlias); | 
|---|
| 145 | font.setSubpixel(true); | 
|---|
| 146 | font.setHinting(SkFontHinting::kSlight); | 
|---|
| 147 | font.getMetrics(metrics); | 
|---|
| 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 { | 
|---|
| 155 | metrics->fAscent = (metrics->fAscent - metrics->fLeading / 2); | 
|---|
| 156 | metrics->fDescent = (metrics->fDescent + metrics->fLeading / 2); | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | bool PlaceholderStyle::equals(const PlaceholderStyle& other) const { | 
|---|
| 161 | return nearlyEqual(fWidth, other.fWidth) && | 
|---|
| 162 | nearlyEqual(fHeight, other.fHeight) && | 
|---|
| 163 | fAlignment == other.fAlignment && | 
|---|
| 164 | fBaseline == other.fBaseline && | 
|---|
| 165 | (fAlignment != PlaceholderAlignment::kBaseline || | 
|---|
| 166 | nearlyEqual(fBaselineOffset, other.fBaselineOffset)); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | }  // namespace textlayout | 
|---|
| 170 | }  // namespace skia | 
|---|
| 171 |  | 
|---|