Add text highlight

This commit is contained in:
Yousef Khadadeh 2025-11-24 17:45:21 +03:00
parent aa98e6e6d0
commit 23b60af529
5 changed files with 86 additions and 8 deletions

View file

@ -90,6 +90,7 @@ bool TextBoxEditTool::edit_gui(const std::shared_ptr<DrawComponent>& comp) {
else
release_undo_data("Font Size");
// Text color
t.gui.left_to_right_line_layout([&]() {
CLAY({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(40), .height = CLAY_SIZING_FIXED(40)}}}) {
if(t.gui.color_button("Text Color", &newTextColor, &newTextColor == t.colorRight))
@ -108,6 +109,32 @@ bool TextBoxEditTool::edit_gui(const std::shared_ptr<DrawComponent>& comp) {
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::COLOR]);
}
// Highlight color
t.gui.left_to_right_line_layout([&]() {
CLAY({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(40), .height = CLAY_SIZING_FIXED(40)}}}) {
if(t.gui.color_button("Highlight Color", &newHighlightColor, &newHighlightColor == t.colorRight)) {
if(newHighlightColor.w() == 0.0f) // Make highlight appear when the button is pressed
newHighlightColor.w() = 1.0f;
t.color_selector_right(&newHighlightColor == t.colorRight ? nullptr : &newHighlightColor);
}
}
t.gui.text_label("Highlight Color");
});
if(&newHighlightColor == t.colorRight)
hold_undo_data("Highlight Color", a);
else
release_undo_data("Highlight Color");
if((&newHighlightColor == t.colorRight) && t.isUpdatingColorRight) {
currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR] = std::make_shared<HighlightColorTextStyleModifier>(newHighlightColor);
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR]);
}
t.gui.pop_id();
if(a->textBox->inputChangedTextBox)
@ -176,6 +203,7 @@ void TextBoxEditTool::set_styles_at_selection(const std::shared_ptr<DrawTextBox>
newFontName = std::static_pointer_cast<FontFamiliesTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::FONT_FAMILIES])->get_families().back().c_str();
newFontSize = std::static_pointer_cast<SizeTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::SIZE])->get_size();
newTextColor = std::static_pointer_cast<ColorTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::COLOR])->get_color();
newHighlightColor = std::static_pointer_cast<HighlightColorTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::HIGHLIGHT_COLOR])->get_color();
newIsBold = std::static_pointer_cast<WeightTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::WEIGHT])->get_weight() == SkFontStyle::Weight::kBold_Weight;
newIsItalic = std::static_pointer_cast<SlantTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::SLANT])->get_slant() == SkFontStyle::Slant::kItalic_Slant;
newIsUnderlined = std::static_pointer_cast<DecorationTextStyleModifier>(currentMods[TextStyleModifier::ModifierType::DECORATION])->get_decoration_value() & skia::textlayout::TextDecoration::kUnderline;

View file

@ -22,6 +22,7 @@ class TextBoxEditTool : public DrawingProgramEditToolBase {
uint32_t newFontSize = 15;
Vector4f newTextColor{1.0f, 1.0f, 1.0f, 1.0f};
Vector4f newHighlightColor{1.0f, 1.0f, 1.0f, 0.0f};
bool newIsBold = false;
bool newIsItalic = false;
bool newIsUnderlined = false;

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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;