2019-05-30 20:12:56 +00:00
|
|
|
// Copyright 2019 Google LLC.
|
|
|
|
#ifndef TextStyle_DEFINED
|
|
|
|
#define TextStyle_DEFINED
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontMetrics.h"
|
|
|
|
#include "include/core/SkFontStyle.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-08-08 20:51:27 +00:00
|
|
|
#include "include/core/SkScalar.h"
|
2019-06-18 13:58:02 +00:00
|
|
|
#include "modules/skparagraph/include/DartTypes.h"
|
|
|
|
#include "modules/skparagraph/include/TextShadow.h"
|
2019-05-30 20:12:56 +00:00
|
|
|
|
|
|
|
// TODO: Make it external so the other platforms (Android) could use it
|
|
|
|
#define DEFAULT_FONT_FAMILY "sans-serif"
|
|
|
|
|
|
|
|
namespace skia {
|
|
|
|
namespace textlayout {
|
|
|
|
|
2020-02-05 15:17:53 +00:00
|
|
|
static inline bool nearlyZero(SkScalar x, SkScalar tolerance = SK_ScalarNearlyZero) {
|
|
|
|
if (SkScalarIsFinite(x)) {
|
|
|
|
return SkScalarNearlyZero(x, tolerance);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool nearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance = SK_ScalarNearlyZero) {
|
|
|
|
if (SkScalarIsFinite(x) && SkScalarIsFinite(x)) {
|
|
|
|
return SkScalarNearlyEqual(x, y, tolerance);
|
|
|
|
}
|
|
|
|
// Inf == Inf, anything else is false
|
|
|
|
return x == y;
|
|
|
|
}
|
|
|
|
|
2019-05-30 20:12:56 +00:00
|
|
|
// Multiple decorations can be applied at once. Ex: Underline and overline is
|
|
|
|
// (0x1 | 0x2)
|
|
|
|
enum TextDecoration {
|
|
|
|
kNoDecoration = 0x0,
|
|
|
|
kUnderline = 0x1,
|
|
|
|
kOverline = 0x2,
|
|
|
|
kLineThrough = 0x4,
|
|
|
|
};
|
2019-07-11 13:34:39 +00:00
|
|
|
constexpr TextDecoration AllTextDecorations[] = {
|
2019-05-30 20:12:56 +00:00
|
|
|
kNoDecoration,
|
|
|
|
kUnderline,
|
|
|
|
kOverline,
|
|
|
|
kLineThrough,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum TextDecorationStyle { kSolid, kDouble, kDotted, kDashed, kWavy };
|
|
|
|
|
2020-05-04 19:03:18 +00:00
|
|
|
enum TextDecorationMode { kGaps, kThrough };
|
|
|
|
|
2019-05-30 20:12:56 +00:00
|
|
|
enum StyleType {
|
2020-02-26 17:14:18 +00:00
|
|
|
kNone,
|
2019-05-30 20:12:56 +00:00
|
|
|
kAllAttributes,
|
|
|
|
kFont,
|
|
|
|
kForeground,
|
|
|
|
kBackground,
|
|
|
|
kShadow,
|
|
|
|
kDecorations,
|
|
|
|
kLetterSpacing,
|
|
|
|
kWordSpacing
|
|
|
|
};
|
|
|
|
|
2019-06-21 16:22:32 +00:00
|
|
|
struct Decoration {
|
|
|
|
TextDecoration fType;
|
2020-05-04 19:03:18 +00:00
|
|
|
TextDecorationMode fMode;
|
2019-06-21 16:22:32 +00:00
|
|
|
SkColor fColor;
|
|
|
|
TextDecorationStyle fStyle;
|
|
|
|
SkScalar fThicknessMultiplier;
|
|
|
|
|
|
|
|
bool operator==(const Decoration& other) const {
|
|
|
|
return this->fType == other.fType &&
|
2020-05-04 19:03:18 +00:00
|
|
|
this->fMode == other.fMode &&
|
2019-06-21 16:22:32 +00:00
|
|
|
this->fColor == other.fColor &&
|
|
|
|
this->fStyle == other.fStyle &&
|
|
|
|
this->fThicknessMultiplier == other.fThicknessMultiplier;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-08 20:51:27 +00:00
|
|
|
/// Where to vertically align the placeholder relative to the surrounding text.
|
|
|
|
enum class PlaceholderAlignment {
|
|
|
|
/// Match the baseline of the placeholder with the baseline.
|
|
|
|
kBaseline,
|
|
|
|
|
|
|
|
/// Align the bottom edge of the placeholder with the baseline such that the
|
|
|
|
/// placeholder sits on top of the baseline.
|
|
|
|
kAboveBaseline,
|
|
|
|
|
|
|
|
/// Align the top edge of the placeholder with the baseline specified in
|
|
|
|
/// such that the placeholder hangs below the baseline.
|
|
|
|
kBelowBaseline,
|
|
|
|
|
|
|
|
/// Align the top edge of the placeholder with the top edge of the font.
|
|
|
|
/// When the placeholder is very tall, the extra space will hang from
|
|
|
|
/// the top and extend through the bottom of the line.
|
|
|
|
kTop,
|
|
|
|
|
|
|
|
/// Align the bottom edge of the placeholder with the top edge of the font.
|
|
|
|
/// When the placeholder is very tall, the extra space will rise from
|
|
|
|
/// the bottom and extend through the top of the line.
|
|
|
|
kBottom,
|
|
|
|
|
|
|
|
/// Align the middle of the placeholder with the middle of the text. When the
|
|
|
|
/// placeholder is very tall, the extra space will grow equally from
|
|
|
|
/// the top and bottom of the line.
|
|
|
|
kMiddle,
|
|
|
|
};
|
|
|
|
|
2019-12-10 17:11:17 +00:00
|
|
|
struct FontFeature {
|
|
|
|
FontFeature(const SkString name, int value) : fName(name), fValue(value) { }
|
2020-03-23 21:22:24 +00:00
|
|
|
bool operator==(const FontFeature& that) const {
|
|
|
|
return fName == that.fName && fValue == that.fValue;
|
2019-12-10 17:11:17 +00:00
|
|
|
}
|
|
|
|
SkString fName;
|
|
|
|
int fValue;
|
|
|
|
};
|
|
|
|
|
2019-08-08 20:51:27 +00:00
|
|
|
struct PlaceholderStyle {
|
2020-02-05 15:17:53 +00:00
|
|
|
PlaceholderStyle()
|
|
|
|
: fWidth(0)
|
|
|
|
, fHeight(0)
|
|
|
|
, fAlignment(PlaceholderAlignment::kBaseline)
|
|
|
|
, fBaseline(TextBaseline::kAlphabetic)
|
|
|
|
, fBaselineOffset(0) {}
|
|
|
|
|
2019-08-08 20:51:27 +00:00
|
|
|
PlaceholderStyle(SkScalar width, SkScalar height, PlaceholderAlignment alignment,
|
|
|
|
TextBaseline baseline, SkScalar offset)
|
|
|
|
: fWidth(width)
|
|
|
|
, fHeight(height)
|
|
|
|
, fAlignment(alignment)
|
|
|
|
, fBaseline(baseline)
|
|
|
|
, fBaselineOffset(offset) {}
|
|
|
|
|
2020-03-23 21:22:24 +00:00
|
|
|
bool equals(const PlaceholderStyle&) const;
|
2020-01-14 18:24:45 +00:00
|
|
|
|
2020-02-05 15:17:53 +00:00
|
|
|
SkScalar fWidth;
|
|
|
|
SkScalar fHeight;
|
2019-08-08 20:51:27 +00:00
|
|
|
PlaceholderAlignment fAlignment;
|
|
|
|
TextBaseline fBaseline;
|
|
|
|
// Distance from the top edge of the rect to the baseline position. This
|
|
|
|
// baseline will be aligned against the alphabetic baseline of the surrounding
|
|
|
|
// text.
|
|
|
|
//
|
|
|
|
// Positive values drop the baseline lower (positions the rect higher) and
|
|
|
|
// small or negative values will cause the rect to be positioned underneath
|
|
|
|
// the line. When baseline == height, the bottom edge of the rect will rest on
|
|
|
|
// the alphabetic baseline.
|
2020-02-05 15:17:53 +00:00
|
|
|
SkScalar fBaselineOffset;
|
2019-08-08 20:51:27 +00:00
|
|
|
};
|
|
|
|
|
2019-05-30 20:12:56 +00:00
|
|
|
class TextStyle {
|
|
|
|
public:
|
|
|
|
TextStyle();
|
2019-08-08 20:51:27 +00:00
|
|
|
TextStyle(const TextStyle& other, bool placeholder);
|
2019-05-30 20:12:56 +00:00
|
|
|
~TextStyle() = default;
|
|
|
|
|
|
|
|
bool equals(const TextStyle& other) const;
|
2020-01-14 18:24:45 +00:00
|
|
|
bool equalsByFonts(const TextStyle& that) const;
|
2019-05-30 20:12:56 +00:00
|
|
|
bool matchOneAttribute(StyleType styleType, const TextStyle& other) const;
|
|
|
|
bool operator==(const TextStyle& rhs) const { return this->equals(rhs); }
|
|
|
|
|
|
|
|
// Colors
|
|
|
|
SkColor getColor() const { return fColor; }
|
|
|
|
void setColor(SkColor color) { fColor = color; }
|
|
|
|
|
|
|
|
bool hasForeground() const { return fHasForeground; }
|
|
|
|
SkPaint getForeground() const { return fForeground; }
|
|
|
|
void setForegroundColor(SkPaint paint) {
|
|
|
|
fHasForeground = true;
|
|
|
|
fForeground = std::move(paint);
|
|
|
|
}
|
|
|
|
void clearForegroundColor() { fHasForeground = false; }
|
|
|
|
|
|
|
|
bool hasBackground() const { return fHasBackground; }
|
|
|
|
SkPaint getBackground() const { return fBackground; }
|
|
|
|
void setBackgroundColor(SkPaint paint) {
|
|
|
|
fHasBackground = true;
|
|
|
|
fBackground = std::move(paint);
|
|
|
|
}
|
|
|
|
void clearBackgroundColor() { fHasBackground = false; }
|
|
|
|
|
|
|
|
// Decorations
|
2019-06-21 16:22:32 +00:00
|
|
|
Decoration getDecoration() const { return fDecoration; }
|
|
|
|
TextDecoration getDecorationType() const { return fDecoration.fType; }
|
2020-05-04 19:03:18 +00:00
|
|
|
TextDecorationMode getDecorationMode() const { return fDecoration.fMode; }
|
2019-06-21 16:22:32 +00:00
|
|
|
SkColor getDecorationColor() const { return fDecoration.fColor; }
|
|
|
|
TextDecorationStyle getDecorationStyle() const { return fDecoration.fStyle; }
|
2019-05-30 20:12:56 +00:00
|
|
|
SkScalar getDecorationThicknessMultiplier() const {
|
2019-06-21 16:22:32 +00:00
|
|
|
return fDecoration.fThicknessMultiplier;
|
2019-05-30 20:12:56 +00:00
|
|
|
}
|
2019-06-21 16:22:32 +00:00
|
|
|
void setDecoration(TextDecoration decoration) { fDecoration.fType = decoration; }
|
2020-05-04 19:03:18 +00:00
|
|
|
void setDecorationMode(TextDecorationMode mode) { fDecoration.fMode = mode; }
|
2019-06-21 16:22:32 +00:00
|
|
|
void setDecorationStyle(TextDecorationStyle style) { fDecoration.fStyle = style; }
|
|
|
|
void setDecorationColor(SkColor color) { fDecoration.fColor = color; }
|
|
|
|
void setDecorationThicknessMultiplier(SkScalar m) { fDecoration.fThicknessMultiplier = m; }
|
2019-05-30 20:12:56 +00:00
|
|
|
|
|
|
|
// Weight/Width/Slant
|
|
|
|
SkFontStyle getFontStyle() const { return fFontStyle; }
|
|
|
|
void setFontStyle(SkFontStyle fontStyle) { fFontStyle = fontStyle; }
|
|
|
|
|
|
|
|
// Shadows
|
|
|
|
size_t getShadowNumber() const { return fTextShadows.size(); }
|
|
|
|
std::vector<TextShadow> getShadows() const { return fTextShadows; }
|
|
|
|
void addShadow(TextShadow shadow) { fTextShadows.emplace_back(shadow); }
|
|
|
|
void resetShadows() { fTextShadows.clear(); }
|
|
|
|
|
2019-12-10 17:11:17 +00:00
|
|
|
// Font features
|
|
|
|
size_t getFontFeatureNumber() const { return fFontFeatures.size(); }
|
|
|
|
std::vector<FontFeature> getFontFeatures() const { return fFontFeatures; }
|
|
|
|
void addFontFeature(const SkString& fontFeature, int value)
|
|
|
|
{ fFontFeatures.emplace_back(fontFeature, value); }
|
|
|
|
void resetFontFeatures() { fFontFeatures.clear(); }
|
|
|
|
|
2019-05-30 20:12:56 +00:00
|
|
|
SkScalar getFontSize() const { return fFontSize; }
|
|
|
|
void setFontSize(SkScalar size) { fFontSize = size; }
|
|
|
|
|
|
|
|
const std::vector<SkString>& getFontFamilies() const { return fFontFamilies; }
|
|
|
|
void setFontFamilies(std::vector<SkString> families) {
|
|
|
|
fFontFamilies = std::move(families);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setHeight(SkScalar height) { fHeight = height; }
|
2019-08-01 20:02:17 +00:00
|
|
|
SkScalar getHeight() const { return fHeightOverride ? fHeight : 0; }
|
|
|
|
|
|
|
|
void setHeightOverride(bool heightOverride) { fHeightOverride = heightOverride; }
|
|
|
|
bool getHeightOverride() const { return fHeightOverride; }
|
2019-05-30 20:12:56 +00:00
|
|
|
|
|
|
|
void setLetterSpacing(SkScalar letterSpacing) { fLetterSpacing = letterSpacing; }
|
|
|
|
SkScalar getLetterSpacing() const { return fLetterSpacing; }
|
|
|
|
|
|
|
|
void setWordSpacing(SkScalar wordSpacing) { fWordSpacing = wordSpacing; }
|
|
|
|
SkScalar getWordSpacing() const { return fWordSpacing; }
|
|
|
|
|
|
|
|
SkTypeface* getTypeface() const { return fTypeface.get(); }
|
|
|
|
sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
|
|
|
|
void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); }
|
|
|
|
|
|
|
|
SkString getLocale() const { return fLocale; }
|
|
|
|
void setLocale(const SkString& locale) { fLocale = locale; }
|
|
|
|
|
|
|
|
TextBaseline getTextBaseline() const { return fTextBaseline; }
|
|
|
|
void setTextBaseline(TextBaseline baseline) { fTextBaseline = baseline; }
|
|
|
|
|
2019-08-01 20:02:17 +00:00
|
|
|
void getFontMetrics(SkFontMetrics* metrics) const;
|
2019-05-30 20:12:56 +00:00
|
|
|
|
2019-08-08 20:51:27 +00:00
|
|
|
bool isPlaceholder() const { return fIsPlaceholder; }
|
|
|
|
void setPlaceholder() { fIsPlaceholder = true; }
|
|
|
|
|
2019-05-30 20:12:56 +00:00
|
|
|
private:
|
2019-08-08 20:51:27 +00:00
|
|
|
|
2019-06-21 16:22:32 +00:00
|
|
|
Decoration fDecoration;
|
2019-05-30 20:12:56 +00:00
|
|
|
|
|
|
|
SkFontStyle fFontStyle;
|
|
|
|
|
|
|
|
std::vector<SkString> fFontFamilies;
|
|
|
|
SkScalar fFontSize;
|
|
|
|
SkScalar fHeight;
|
2019-08-01 20:02:17 +00:00
|
|
|
bool fHeightOverride;
|
2019-05-30 20:12:56 +00:00
|
|
|
SkString fLocale;
|
|
|
|
SkScalar fLetterSpacing;
|
|
|
|
SkScalar fWordSpacing;
|
|
|
|
|
|
|
|
TextBaseline fTextBaseline;
|
|
|
|
|
|
|
|
SkColor fColor;
|
|
|
|
bool fHasBackground;
|
|
|
|
SkPaint fBackground;
|
|
|
|
bool fHasForeground;
|
|
|
|
SkPaint fForeground;
|
|
|
|
|
|
|
|
std::vector<TextShadow> fTextShadows;
|
|
|
|
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
2019-08-08 20:51:27 +00:00
|
|
|
bool fIsPlaceholder;
|
2019-12-10 17:11:17 +00:00
|
|
|
|
|
|
|
std::vector<FontFeature> fFontFeatures;
|
2019-05-30 20:12:56 +00:00
|
|
|
};
|
2019-06-21 16:22:32 +00:00
|
|
|
|
|
|
|
typedef size_t TextIndex;
|
|
|
|
typedef SkRange<size_t> TextRange;
|
|
|
|
const SkRange<size_t> EMPTY_TEXT = EMPTY_RANGE;
|
|
|
|
|
|
|
|
struct Block {
|
|
|
|
Block() : fRange(EMPTY_RANGE), fStyle() { }
|
2019-08-08 20:51:27 +00:00
|
|
|
Block(size_t start, size_t end, const TextStyle& style) : fRange(start, end), fStyle(style) {}
|
|
|
|
Block(TextRange textRange, const TextStyle& style) : fRange(textRange), fStyle(style) {}
|
|
|
|
|
2019-12-04 16:43:32 +00:00
|
|
|
Block(const Block& other) : fRange(other.fRange), fStyle(other.fStyle) {}
|
2019-06-21 16:22:32 +00:00
|
|
|
|
|
|
|
void add(TextRange tail) {
|
|
|
|
SkASSERT(fRange.end == tail.start);
|
|
|
|
fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width());
|
|
|
|
}
|
2019-12-04 16:43:32 +00:00
|
|
|
|
2019-06-21 16:22:32 +00:00
|
|
|
TextRange fRange;
|
|
|
|
TextStyle fStyle;
|
|
|
|
};
|
|
|
|
|
2019-08-08 20:51:27 +00:00
|
|
|
|
|
|
|
typedef size_t BlockIndex;
|
|
|
|
typedef SkRange<size_t> BlockRange;
|
|
|
|
const size_t EMPTY_BLOCK = EMPTY_INDEX;
|
|
|
|
const SkRange<size_t> EMPTY_BLOCKS = EMPTY_RANGE;
|
|
|
|
|
|
|
|
struct Placeholder {
|
|
|
|
Placeholder() : fRange(EMPTY_RANGE), fStyle() {}
|
|
|
|
|
2019-09-05 18:35:17 +00:00
|
|
|
Placeholder(size_t start, size_t end, const PlaceholderStyle& style, const TextStyle& textStyle,
|
|
|
|
BlockRange blocksBefore, TextRange textBefore)
|
2019-08-08 20:51:27 +00:00
|
|
|
: fRange(start, end)
|
|
|
|
, fStyle(style)
|
2019-09-05 18:35:17 +00:00
|
|
|
, fTextStyle(textStyle)
|
2019-08-08 20:51:27 +00:00
|
|
|
, fBlocksBefore(blocksBefore)
|
|
|
|
, fTextBefore(textBefore) {}
|
|
|
|
|
2019-12-04 16:43:32 +00:00
|
|
|
Placeholder(const Placeholder& other)
|
|
|
|
: fRange(other.fRange)
|
|
|
|
, fStyle(other.fStyle)
|
|
|
|
, fTextStyle(other.fTextStyle)
|
|
|
|
, fBlocksBefore(other.fBlocksBefore)
|
|
|
|
, fTextBefore(other.fTextBefore) {}
|
2019-08-08 20:51:27 +00:00
|
|
|
|
|
|
|
TextRange fRange;
|
|
|
|
PlaceholderStyle fStyle;
|
2019-09-05 18:35:17 +00:00
|
|
|
TextStyle fTextStyle;
|
2019-08-08 20:51:27 +00:00
|
|
|
BlockRange fBlocksBefore;
|
|
|
|
TextRange fTextBefore;
|
|
|
|
};
|
|
|
|
|
2019-05-30 20:12:56 +00:00
|
|
|
} // namespace textlayout
|
|
|
|
} // namespace skia
|
|
|
|
|
|
|
|
#endif // TextStyle_DEFINED
|