1 // Copyright 2019 Google LLC. 2 #ifndef TextStyle_DEFINED 3 #define TextStyle_DEFINED 4 5 #include <optional> 6 #include <vector> 7 #include "include/core/SkColor.h" 8 #include "include/core/SkFont.h" 9 #include "include/core/SkFontMetrics.h" 10 #include "include/core/SkFontStyle.h" 11 #include "include/core/SkPaint.h" 12 #include "include/core/SkScalar.h" 13 #include "modules/skparagraph/include/DartTypes.h" 14 #include "modules/skparagraph/include/FontArguments.h" 15 #include "modules/skparagraph/include/ParagraphPainter.h" 16 #include "modules/skparagraph/include/TextShadow.h" 17 18 // TODO: Make it external so the other platforms (Android) could use it 19 #define DEFAULT_FONT_FAMILY "sans-serif" 20 21 namespace skia { 22 namespace textlayout { 23 24 static inline bool nearlyZero(SkScalar x, SkScalar tolerance = SK_ScalarNearlyZero) { 25 if (SkIsFinite(x)) { 26 return SkScalarNearlyZero(x, tolerance); 27 } 28 return false; 29 } 30 31 static inline bool nearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance = SK_ScalarNearlyZero) { 32 if (SkIsFinite(x, y)) { 33 return SkScalarNearlyEqual(x, y, tolerance); 34 } 35 // Inf == Inf, anything else is false 36 return x == y; 37 } 38 39 // Multiple decorations can be applied at once. Ex: Underline and overline is 40 // (0x1 | 0x2) 41 enum TextDecoration { 42 kNoDecoration = 0x0, 43 kUnderline = 0x1, 44 kOverline = 0x2, 45 kLineThrough = 0x4, 46 }; 47 constexpr TextDecoration AllTextDecorations[] = { 48 kNoDecoration, 49 kUnderline, 50 kOverline, 51 kLineThrough, 52 }; 53 54 enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy }; 55 56 enum TextDecorationMode { kGaps, kThrough }; 57 58 enum StyleType { 59 kNone, 60 kAllAttributes, 61 kFont, 62 kForeground, 63 kBackground, 64 kShadow, 65 kDecorations, 66 kLetterSpacing, 67 kWordSpacing 68 }; 69 70 struct Decoration { 71 TextDecoration fType; 72 TextDecorationMode fMode; 73 SkColor fColor; 74 TextDecorationStyle fStyle; 75 SkScalar fThicknessMultiplier; 76 77 bool operator==(const Decoration& other) const { 78 return this->fType == other.fType && 79 this->fMode == other.fMode && 80 this->fColor == other.fColor && 81 this->fStyle == other.fStyle && 82 this->fThicknessMultiplier == other.fThicknessMultiplier; 83 } 84 }; 85 86 /// Where to vertically align the placeholder relative to the surrounding text. 87 enum class PlaceholderAlignment { 88 /// Match the baseline of the placeholder with the baseline. 89 kBaseline, 90 91 /// Align the bottom edge of the placeholder with the baseline such that the 92 /// placeholder sits on top of the baseline. 93 kAboveBaseline, 94 95 /// Align the top edge of the placeholder with the baseline specified in 96 /// such that the placeholder hangs below the baseline. 97 kBelowBaseline, 98 99 /// Align the top edge of the placeholder with the top edge of the font. 100 /// When the placeholder is very tall, the extra space will hang from 101 /// the top and extend through the bottom of the line. 102 kTop, 103 104 /// Align the bottom edge of the placeholder with the top edge of the font. 105 /// When the placeholder is very tall, the extra space will rise from 106 /// the bottom and extend through the top of the line. 107 kBottom, 108 109 /// Align the middle of the placeholder with the middle of the text. When the 110 /// placeholder is very tall, the extra space will grow equally from 111 /// the top and bottom of the line. 112 kMiddle, 113 }; 114 115 struct FontFeature { FontFeatureFontFeature116 FontFeature(SkString name, int value) : fName(std::move(name)), fValue(value) {} 117 bool operator==(const FontFeature& that) const { 118 return fName == that.fName && fValue == that.fValue; 119 } 120 SkString fName; 121 int fValue; 122 }; 123 124 struct PlaceholderStyle { 125 PlaceholderStyle() = default; PlaceholderStylePlaceholderStyle126 PlaceholderStyle(SkScalar width, SkScalar height, PlaceholderAlignment alignment, 127 TextBaseline baseline, SkScalar offset) 128 : fWidth(width) 129 , fHeight(height) 130 , fAlignment(alignment) 131 , fBaseline(baseline) 132 , fBaselineOffset(offset) {} 133 134 bool equals(const PlaceholderStyle&) const; 135 136 SkScalar fWidth = 0; 137 SkScalar fHeight = 0; 138 PlaceholderAlignment fAlignment = PlaceholderAlignment::kBaseline; 139 TextBaseline fBaseline = TextBaseline::kAlphabetic; 140 // Distance from the top edge of the rect to the baseline position. This 141 // baseline will be aligned against the alphabetic baseline of the surrounding 142 // text. 143 // 144 // Positive values drop the baseline lower (positions the rect higher) and 145 // small or negative values will cause the rect to be positioned underneath 146 // the line. When baseline == height, the bottom edge of the rect will rest on 147 // the alphabetic baseline. 148 SkScalar fBaselineOffset = 0; 149 }; 150 151 class TextStyle { 152 public: 153 TextStyle() = default; 154 TextStyle(const TextStyle& other) = default; 155 TextStyle& operator=(const TextStyle& other) = default; 156 157 TextStyle cloneForPlaceholder(); 158 159 bool equals(const TextStyle& other) const; 160 bool equalsByFonts(const TextStyle& that) const; 161 bool matchOneAttribute(StyleType styleType, const TextStyle& other) const; 162 bool operator==(const TextStyle& rhs) const { return this->equals(rhs); } 163 164 // Colors getColor()165 SkColor getColor() const { return fColor; } setColor(SkColor color)166 void setColor(SkColor color) { fColor = color; } 167 hasForeground()168 bool hasForeground() const { return fHasForeground; } getForeground()169 SkPaint getForeground() const { 170 const SkPaint* paint = std::get_if<SkPaint>(&fForeground); 171 return paint ? *paint : SkPaint(); 172 } getForegroundPaintOrID()173 ParagraphPainter::SkPaintOrID getForegroundPaintOrID() const { 174 return fForeground; 175 } setForegroundPaint(SkPaint paint)176 void setForegroundPaint(SkPaint paint) { 177 fHasForeground = true; 178 fForeground = std::move(paint); 179 } 180 // DEPRECATED: prefer `setForegroundPaint`. setForegroundColor(SkPaint paint)181 void setForegroundColor(SkPaint paint) { setForegroundPaint(std::move(paint)); } 182 183 // Set the foreground to a paint ID. This is intended for use by clients 184 // that implement a custom ParagraphPainter that can not accept an SkPaint. setForegroundPaintID(ParagraphPainter::PaintID paintID)185 void setForegroundPaintID(ParagraphPainter::PaintID paintID) { 186 fHasForeground = true; 187 fForeground = paintID; 188 } clearForegroundColor()189 void clearForegroundColor() { fHasForeground = false; } 190 hasBackground()191 bool hasBackground() const { return fHasBackground; } getBackground()192 SkPaint getBackground() const { 193 const SkPaint* paint = std::get_if<SkPaint>(&fBackground); 194 return paint ? *paint : SkPaint(); 195 } getBackgroundPaintOrID()196 ParagraphPainter::SkPaintOrID getBackgroundPaintOrID() const { 197 return fBackground; 198 } setBackgroundPaint(SkPaint paint)199 void setBackgroundPaint(SkPaint paint) { 200 fHasBackground = true; 201 fBackground = std::move(paint); 202 } 203 // DEPRECATED: prefer `setBackgroundPaint`. setBackgroundColor(SkPaint paint)204 void setBackgroundColor(SkPaint paint) { setBackgroundPaint(std::move(paint)); } setBackgroundPaintID(ParagraphPainter::PaintID paintID)205 void setBackgroundPaintID(ParagraphPainter::PaintID paintID) { 206 fHasBackground = true; 207 fBackground = paintID; 208 } clearBackgroundColor()209 void clearBackgroundColor() { fHasBackground = false; } 210 211 // Decorations getDecoration()212 Decoration getDecoration() const { return fDecoration; } getDecorationType()213 TextDecoration getDecorationType() const { return fDecoration.fType; } getDecorationMode()214 TextDecorationMode getDecorationMode() const { return fDecoration.fMode; } getDecorationColor()215 SkColor getDecorationColor() const { return fDecoration.fColor; } getDecorationStyle()216 TextDecorationStyle getDecorationStyle() const { return fDecoration.fStyle; } getDecorationThicknessMultiplier()217 SkScalar getDecorationThicknessMultiplier() const { 218 return fDecoration.fThicknessMultiplier; 219 } setDecoration(TextDecoration decoration)220 void setDecoration(TextDecoration decoration) { fDecoration.fType = decoration; } setDecorationMode(TextDecorationMode mode)221 void setDecorationMode(TextDecorationMode mode) { fDecoration.fMode = mode; } setDecorationStyle(TextDecorationStyle style)222 void setDecorationStyle(TextDecorationStyle style) { fDecoration.fStyle = style; } setDecorationColor(SkColor color)223 void setDecorationColor(SkColor color) { fDecoration.fColor = color; } setDecorationThicknessMultiplier(SkScalar m)224 void setDecorationThicknessMultiplier(SkScalar m) { fDecoration.fThicknessMultiplier = m; } 225 226 // Weight/Width/Slant getFontStyle()227 SkFontStyle getFontStyle() const { return fFontStyle; } setFontStyle(SkFontStyle fontStyle)228 void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; } 229 230 // Shadows getShadowNumber()231 size_t getShadowNumber() const { return fTextShadows.size(); } getShadows()232 std::vector<TextShadow> getShadows() const { return fTextShadows; } addShadow(TextShadow shadow)233 void addShadow(TextShadow shadow) { fTextShadows.emplace_back(shadow); } resetShadows()234 void resetShadows() { fTextShadows.clear(); } 235 236 // Font features getFontFeatureNumber()237 size_t getFontFeatureNumber() const { return fFontFeatures.size(); } getFontFeatures()238 std::vector<FontFeature> getFontFeatures() const { return fFontFeatures; } addFontFeature(const SkString & fontFeature,int value)239 void addFontFeature(const SkString& fontFeature, int value) 240 { fFontFeatures.emplace_back(fontFeature, value); } resetFontFeatures()241 void resetFontFeatures() { fFontFeatures.clear(); } 242 243 // Font arguments getFontArguments()244 const std::optional<FontArguments>& getFontArguments() const { return fFontArguments; } 245 // The contents of the SkFontArguments will be copied into the TextStyle, 246 // and the SkFontArguments can be safely deleted after setFontArguments returns. 247 void setFontArguments(const std::optional<SkFontArguments>& args); 248 getFontSize()249 SkScalar getFontSize() const { return fFontSize; } setFontSize(SkScalar size)250 void setFontSize(SkScalar size) { fFontSize = size; } 251 getFontFamilies()252 const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; } setFontFamilies(std::vector<SkString> families)253 void setFontFamilies(std::vector<SkString> families) { 254 fFontFamilies = std::move(families); 255 } 256 getBaselineShift()257 SkScalar getBaselineShift() const { return fBaselineShift; } setBaselineShift(SkScalar baselineShift)258 void setBaselineShift(SkScalar baselineShift) { fBaselineShift = baselineShift; } 259 setHeight(SkScalar height)260 void setHeight(SkScalar height) { fHeight = height; } getHeight()261 SkScalar getHeight() const { return fHeightOverride ? fHeight : 0; } 262 setHeightOverride(bool heightOverride)263 void setHeightOverride(bool heightOverride) { fHeightOverride = heightOverride; } getHeightOverride()264 bool getHeightOverride() const { return fHeightOverride; } 265 setHalfLeading(bool halfLeading)266 void setHalfLeading(bool halfLeading) { fHalfLeading = halfLeading; } getHalfLeading()267 bool getHalfLeading() const { return fHalfLeading; } 268 setLetterSpacing(SkScalar letterSpacing)269 void setLetterSpacing(SkScalar letterSpacing) { fLetterSpacing = letterSpacing; } getLetterSpacing()270 SkScalar getLetterSpacing() const { return fLetterSpacing; } 271 setWordSpacing(SkScalar wordSpacing)272 void setWordSpacing(SkScalar wordSpacing) { fWordSpacing = wordSpacing; } getWordSpacing()273 SkScalar getWordSpacing() const { return fWordSpacing; } 274 getTypeface()275 SkTypeface* getTypeface() const { return fTypeface.get(); } refTypeface()276 sk_sp<SkTypeface> refTypeface() const { return fTypeface; } setTypeface(sk_sp<SkTypeface> typeface)277 void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); } 278 getLocale()279 SkString getLocale() const { return fLocale; } setLocale(const SkString & locale)280 void setLocale(const SkString& locale) { fLocale = locale; } 281 getTextBaseline()282 TextBaseline getTextBaseline() const { return fTextBaseline; } setTextBaseline(TextBaseline baseline)283 void setTextBaseline(TextBaseline baseline) { fTextBaseline = baseline; } 284 285 void getFontMetrics(SkFontMetrics* metrics) const; 286 isPlaceholder()287 bool isPlaceholder() const { return fIsPlaceholder; } setPlaceholder()288 void setPlaceholder() { fIsPlaceholder = true; } 289 290 private: 291 static const std::vector<SkString>* kDefaultFontFamilies; 292 293 Decoration fDecoration = { 294 TextDecoration::kNoDecoration, 295 // TODO: switch back to kGaps when (if) switching flutter to skparagraph 296 TextDecorationMode::kThrough, 297 // It does not make sense to draw a transparent object, so we use this as a default 298 // value to indicate no decoration color was set. 299 SK_ColorTRANSPARENT, TextDecorationStyle::kSolid, 300 // Thickness is applied as a multiplier to the default thickness of the font. 301 1.0f}; 302 303 SkFontStyle fFontStyle; 304 305 std::vector<SkString> fFontFamilies = *kDefaultFontFamilies; 306 307 SkScalar fFontSize = 14.0; 308 SkScalar fHeight = 1.0; 309 bool fHeightOverride = false; 310 SkScalar fBaselineShift = 0.0f; 311 // true: half leading. 312 // false: scale ascent/descent with fHeight. 313 bool fHalfLeading = false; 314 SkString fLocale = {}; 315 SkScalar fLetterSpacing = 0.0; 316 SkScalar fWordSpacing = 0.0; 317 318 TextBaseline fTextBaseline = TextBaseline::kAlphabetic; 319 320 SkColor fColor = SK_ColorWHITE; 321 bool fHasBackground = false; 322 ParagraphPainter::SkPaintOrID fBackground; 323 bool fHasForeground = false; 324 ParagraphPainter::SkPaintOrID fForeground; 325 326 std::vector<TextShadow> fTextShadows; 327 328 sk_sp<SkTypeface> fTypeface; 329 bool fIsPlaceholder = false; 330 331 std::vector<FontFeature> fFontFeatures; 332 333 std::optional<FontArguments> fFontArguments; 334 }; 335 336 typedef size_t TextIndex; 337 typedef SkRange<size_t> TextRange; 338 const SkRange<size_t> EMPTY_TEXT = EMPTY_RANGE; 339 340 struct Block { 341 Block() = default; BlockBlock342 Block(size_t start, size_t end, const TextStyle& style) : fRange(start, end), fStyle(style) {} BlockBlock343 Block(TextRange textRange, const TextStyle& style) : fRange(textRange), fStyle(style) {} 344 addBlock345 void add(TextRange tail) { 346 SkASSERT(fRange.end == tail.start); 347 fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width()); 348 } 349 350 TextRange fRange = EMPTY_RANGE; 351 TextStyle fStyle; 352 }; 353 354 355 typedef size_t BlockIndex; 356 typedef SkRange<size_t> BlockRange; 357 const size_t EMPTY_BLOCK = EMPTY_INDEX; 358 const SkRange<size_t> EMPTY_BLOCKS = EMPTY_RANGE; 359 360 struct Placeholder { 361 Placeholder() = default; PlaceholderPlaceholder362 Placeholder(size_t start, size_t end, const PlaceholderStyle& style, const TextStyle& textStyle, 363 BlockRange blocksBefore, TextRange textBefore) 364 : fRange(start, end) 365 , fStyle(style) 366 , fTextStyle(textStyle) 367 , fBlocksBefore(blocksBefore) 368 , fTextBefore(textBefore) {} 369 370 TextRange fRange = EMPTY_RANGE; 371 PlaceholderStyle fStyle; 372 TextStyle fTextStyle; 373 BlockRange fBlocksBefore; 374 TextRange fTextBefore; 375 }; 376 377 } // namespace textlayout 378 } // namespace skia 379 380 #endif // TextStyle_DEFINED 381