Add text highlight
This commit is contained in:
parent
aa98e6e6d0
commit
23b60af529
5 changed files with 86 additions and 8 deletions
|
|
@ -1042,7 +1042,7 @@ SkRect TextBox::get_cursor_rect(TextPosition pos) {
|
|||
}
|
||||
}
|
||||
|
||||
constexpr float CURSOR_WIDTH = 3.0f;
|
||||
constexpr float CURSOR_WIDTH = 2.0f;
|
||||
|
||||
return SkRect::MakeXYWH(topPoint.x() - CURSOR_WIDTH * 0.5f, topPoint.y(), CURSOR_WIDTH, height);
|
||||
}
|
||||
|
|
@ -1103,21 +1103,22 @@ std::pair<size_t, size_t> TextBox::get_start_end_paragraph_pos(size_t p1, size_t
|
|||
void TextBox::paint(SkCanvas* canvas, const PaintOpts& paintOpts) {
|
||||
rebuild();
|
||||
|
||||
for(auto& pData : paragraphs)
|
||||
pData.p->paint(canvas, 0.0f, pData.heightOffset);
|
||||
|
||||
if(paintOpts.cursor.has_value()) {
|
||||
auto& cur = paintOpts.cursor.value();
|
||||
SkPaint p{SkColor4f{paintOpts.cursorColor.x(), paintOpts.cursorColor.y(), paintOpts.cursorColor.z(), 1.0f}};
|
||||
SkPaint selectionP{SkColor4f{paintOpts.cursorColor.x(), paintOpts.cursorColor.y(), paintOpts.cursorColor.z(), 1.0f}};
|
||||
if(cur.selectionBeginPos != cur.selectionEndPos) {
|
||||
canvas->saveLayerAlphaf(nullptr, 0.6f);
|
||||
canvas->saveLayerAlphaf(nullptr, 0.3f);
|
||||
rects_between_text_positions_func(cur.selectionBeginPos, cur.selectionEndPos, [&](const SkRect& rect) {
|
||||
canvas->drawRect(rect, p);
|
||||
canvas->drawRect(rect, selectionP);
|
||||
});
|
||||
canvas->restore();
|
||||
}
|
||||
canvas->drawRect(get_cursor_rect(cur.pos), p);
|
||||
SkPaint cursorP{SkColor4f{paintOpts.cursorColor.x(), paintOpts.cursorColor.y(), paintOpts.cursorColor.z(), 0.8f}};
|
||||
canvas->drawRect(get_cursor_rect(cur.pos), cursorP);
|
||||
}
|
||||
|
||||
for(auto& pData : paragraphs)
|
||||
pData.p->paint(canvas, 0.0f, pData.heightOffset);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ std::shared_ptr<TextStyleModifier> TextStyleModifier::allocate_modifier(Modifier
|
|||
return std::make_shared<DecorationTextStyleModifier>();
|
||||
case ModifierType::FONT_FAMILIES:
|
||||
return std::make_shared<FontFamiliesTextStyleModifier>();
|
||||
case ModifierType::HIGHLIGHT_COLOR:
|
||||
return std::make_shared<HighlightColorTextStyleModifier>();
|
||||
case ModifierType::COUNT:
|
||||
return nullptr;
|
||||
}
|
||||
|
|
@ -44,6 +46,7 @@ const TextStyleModifier::ModifierMap& TextStyleModifier::get_default_modifiers()
|
|||
defaultMods[ModifierType::WEIGHT] = std::make_shared<WeightTextStyleModifier>(SkFontStyle::kNormal_Weight);
|
||||
defaultMods[ModifierType::SLANT] = std::make_shared<SlantTextStyleModifier>(SkFontStyle::kUpright_Slant);
|
||||
defaultMods[ModifierType::DECORATION] = std::make_shared<DecorationTextStyleModifier>(skia::textlayout::TextDecoration::kNoDecoration);
|
||||
defaultMods[ModifierType::HIGHLIGHT_COLOR] = std::make_shared<HighlightColorTextStyleModifier>(Vector4f{0.0f, 0.0f, 0.0f, 0.0f});
|
||||
}
|
||||
return defaultMods;
|
||||
}
|
||||
|
|
@ -231,4 +234,34 @@ bool DecorationTextStyleModifier::equivalent_data(TextStyleModifier& modifier) c
|
|||
return decorationValue == static_cast<DecorationTextStyleModifier&>(modifier).decorationValue;
|
||||
}
|
||||
|
||||
|
||||
HighlightColorTextStyleModifier::HighlightColorTextStyleModifier(const Vector4f& initColor): color(initColor) {}
|
||||
|
||||
TextStyleModifier::ModifierType HighlightColorTextStyleModifier::get_type() const {
|
||||
return ModifierType::HIGHLIGHT_COLOR;
|
||||
}
|
||||
|
||||
const Vector4f& HighlightColorTextStyleModifier::get_color() const {
|
||||
return color;
|
||||
}
|
||||
|
||||
void HighlightColorTextStyleModifier::save(cereal::PortableBinaryOutputArchive& a) const {
|
||||
a(color);
|
||||
}
|
||||
|
||||
void HighlightColorTextStyleModifier::load(cereal::PortableBinaryInputArchive& a) {
|
||||
a(color);
|
||||
}
|
||||
|
||||
void HighlightColorTextStyleModifier::modify_text_style(skia::textlayout::TextStyle& style) const {
|
||||
if(color.w() == 0.0f)
|
||||
style.clearBackgroundColor();
|
||||
else
|
||||
style.setBackgroundPaint(SkPaint{convert_vec4<SkColor4f>(color)});
|
||||
}
|
||||
|
||||
bool HighlightColorTextStyleModifier::equivalent_data(TextStyleModifier& modifier) const {
|
||||
return color == static_cast<HighlightColorTextStyleModifier&>(modifier).color;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ class TextStyleModifier {
|
|||
SLANT,
|
||||
DECORATION,
|
||||
FONT_FAMILIES,
|
||||
HIGHLIGHT_COLOR,
|
||||
|
||||
COUNT
|
||||
};
|
||||
|
|
@ -93,6 +94,20 @@ class ColorTextStyleModifier : public TextStyleModifier {
|
|||
virtual bool equivalent_data(TextStyleModifier& modifier) const override;
|
||||
};
|
||||
|
||||
class HighlightColorTextStyleModifier : public TextStyleModifier {
|
||||
public:
|
||||
HighlightColorTextStyleModifier() = default;
|
||||
HighlightColorTextStyleModifier(const Vector4f& initColor);
|
||||
virtual ModifierType get_type() const override;
|
||||
virtual void save(cereal::PortableBinaryOutputArchive& a) const override;
|
||||
virtual void load(cereal::PortableBinaryInputArchive& a) override;
|
||||
virtual void modify_text_style(skia::textlayout::TextStyle& style) const override;
|
||||
const Vector4f& get_color() const;
|
||||
private:
|
||||
Vector4f color;
|
||||
virtual bool equivalent_data(TextStyleModifier& modifier) const override;
|
||||
};
|
||||
|
||||
class SizeTextStyleModifier : public TextStyleModifier {
|
||||
public:
|
||||
SizeTextStyleModifier() = default;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue