Changed method of setting styles over intervals

This commit is contained in:
Yousef Khadadeh 2025-11-04 22:30:43 +03:00
parent 029b6c8966
commit ee887c82dd
3 changed files with 155 additions and 134 deletions

View file

@ -58,6 +58,16 @@ void DrawTextBox::init_text_box(DrawingProgram& drawP) {
tStyle.setFontFamilies({SkString{"Roboto"}}); tStyle.setFontFamilies({SkString{"Roboto"}});
tStyle.setFontSize(16.0f); tStyle.setFontSize(16.0f);
textBox->set_initial_text_style(tStyle); textBox->set_initial_text_style(tStyle);
auto boldMod = std::make_shared<WeightTextStyleModifier>();
boldMod->set_weight(SkFontStyle::kNormal_Weight);
textBox->set_initial_text_style_modifier(boldMod);
auto italicMod = std::make_shared<SlantTextStyleModifier>();
italicMod->set_slant(SkFontStyle::Slant::kUpright_Slant);
textBox->set_initial_text_style_modifier(italicMod);
auto colorMod = std::make_shared<ColorTextStyleModifier>();
colorMod->color = {1.0f, 1.0f, 1.0f, 1.0f};
textBox->set_initial_text_style_modifier(colorMod);
} }
void DrawTextBox::draw(SkCanvas* canvas, const DrawData& drawData) { void DrawTextBox::draw(SkCanvas* canvas, const DrawData& drawData) {

View file

@ -12,21 +12,33 @@
#include <src/base/SkUTF.h> #include <src/base/SkUTF.h>
#include <cereal/types/string.hpp> #include <cereal/types/string.hpp>
void RichTextBox::TextStyleRangeModifier::save(cereal::PortableBinaryOutputArchive& a) const { void RichTextBox::RichTextData::save(cereal::PortableBinaryOutputArchive& a) const {
a(start, end, modifier->get_type(), *modifier); a(paragraphs);
a(static_cast<uint32_t>(tStyleMods.size()));
for(auto& [pos, modsInPos] : tStyleMods) {
a(pos, static_cast<uint16_t>(modsInPos.size()));
for(auto& [modType, modifier] : modsInPos)
a(modType, *modifier);
}
} }
void RichTextBox::TextStyleRangeModifier::load(cereal::PortableBinaryInputArchive& a) { void RichTextBox::RichTextData::load(cereal::PortableBinaryInputArchive& a) {
a(start, end); a(paragraphs);
TextStyleModifier::ModifierType mType; uint32_t tStyleModCount;
a(mType); a(tStyleModCount);
modifier = TextStyleModifier::allocate_modifier(mType); tStyleMods.clear();
for(uint32_t i = 0; i < tStyleModCount; i++) {
auto& modsInPos = tStyleMods.emplace_back();
uint16_t modsInPosSize;
a(modsInPos.pos, modsInPosSize);
for(uint16_t j = 0; j < modsInPosSize; j++) {
TextStyleModifier::ModifierType modType;
a(modType);
auto modifier = TextStyleModifier::allocate_modifier(modType);
a(*modifier); a(*modifier);
modsInPos.mods[modType] = modifier;
}
} }
void RichTextBox::TextStyleRangeModifier::apply_to_start_and_end(const std::function<void(TextPosition&)>& f) {
f(start);
f(end);
} }
bool RichTextBox::TextPosition::operator<(const RichTextBox::TextPosition& o) const { bool RichTextBox::TextPosition::operator<(const RichTextBox::TextPosition& o) const {
@ -253,13 +265,13 @@ RichTextBox::RichTextData RichTextBox::get_rich_text_data() {
toRet.paragraphs.emplace_back(); toRet.paragraphs.emplace_back();
toRet.paragraphs.back().text = p.text; toRet.paragraphs.back().text = p.text;
} }
toRet.tStyleModifiers = tStyleModifiers; toRet.tStyleMods = tStyleMods;
return toRet; return toRet;
} }
void RichTextBox::clear_text() { void RichTextBox::clear_text() {
paragraphs.clear(); paragraphs.clear();
tStyleModifiers.clear(); tStyleMods.clear();
paragraphs.emplace_back(); paragraphs.emplace_back();
} }
@ -276,8 +288,7 @@ void RichTextBox::set_rich_text_data(const RichTextData& richText) {
} }
if(richText.paragraphs.empty()) if(richText.paragraphs.empty())
paragraphs.emplace_back(); paragraphs.emplace_back();
tStyleModifiers = richText.tStyleModifiers; tStyleMods = richText.tStyleMods;
fit_text_style_ranges_in_text();
needsRebuild = true; needsRebuild = true;
} }
@ -316,46 +327,73 @@ void RichTextBox::set_initial_text_style(const skia::textlayout::TextStyle& tSty
} }
} }
void RichTextBox::set_initial_text_style_modifier(const std::shared_ptr<TextStyleModifier>& modifier) {
if(tStyleMods.empty() || tStyleMods[0].pos != TextPosition{0, 0}) {
if(tStyleMods.empty())
tStyleMods.emplace_back();
else
tStyleMods.insert(tStyleMods.begin(), {});
tStyleMods[0].pos = {0, 0};
}
tStyleMods[0].mods[modifier->get_type()] = modifier;
}
std::shared_ptr<TextStyleModifier> RichTextBox::get_last_text_style_mod_before_pos(TextPosition pos, TextStyleModifier::ModifierType modType) {
std::shared_ptr<TextStyleModifier> lastModOfThisTypeBeforeEnd;
for(auto& [tPos, tStyleModsInPos] : tStyleMods) {
if(tPos <= pos) {
auto containedOfSameType = tStyleModsInPos.find(modType);
if(containedOfSameType != tStyleModsInPos.end())
lastModOfThisTypeBeforeEnd = containedOfSameType->second;
}
else
break;
}
return lastModOfThisTypeBeforeEnd;
}
void RichTextBox::erase_if_over_all_styles_until_pos(TextPosition pos, const std::function<bool(TextPosition, const std::shared_ptr<TextStyleModifier>&)>& func) {
for(auto& [tPos, tStyleModsInPos] : tStyleMods) {
if(tPos <= pos) {
std::erase_if(tStyleModsInPos, [&](auto& item) {
return func(tPos, item.second);
});
}
else
break;
}
}
void RichTextBox::set_text_style_modifier_between(TextPosition p1, TextPosition p2, const std::shared_ptr<TextStyleModifier>& modifier) { void RichTextBox::set_text_style_modifier_between(TextPosition p1, TextPosition p2, const std::shared_ptr<TextStyleModifier>& modifier) {
p1 = move(Movement::NOWHERE, p1); p1 = move(Movement::NOWHERE, p1);
p2 = move(Movement::NOWHERE, p2); p2 = move(Movement::NOWHERE, p2);
if(p1 != p2) { if(p1 != p2) {
fit_text_style_ranges_in_text(); TextPosition start = std::min(p1, p2);
TextStyleRangeModifier toPlace; TextPosition end = std::max(p1, p2);
toPlace.start = std::min(p1, p2); auto lastModOfThisTypeBeforeEnd = get_last_text_style_mod_before_pos(end, modifier->get_type());
toPlace.end = std::max(p1, p2); assert(lastModOfThisTypeBeforeEnd != nullptr);
toPlace.modifier = modifier; erase_if_over_all_styles_until_pos(end, [&](TextPosition p, const std::shared_ptr<TextStyleModifier>& modToCheck) {
std::erase_if(tStyleModifiers, [&toPlace](TextStyleRangeModifier& tStyleMod) { return p >= start && modifier->get_type() == modToCheck->get_type();
if(tStyleMod.modifier->equivalent(*toPlace.modifier)) {
// The equivalent modifiers collide, so merge them into one modifier
if((tStyleMod.start >= toPlace.start && tStyleMod.start <= toPlace.end) || (tStyleMod.end >= toPlace.start && tStyleMod.end <= toPlace.end) ||
(toPlace.start >= tStyleMod.start && toPlace.start <= tStyleMod.end) || (toPlace.end >= tStyleMod.start && toPlace.end <= tStyleMod.end)) {
toPlace.start = std::min(toPlace.start, tStyleMod.start);
toPlace.end = std::max(toPlace.end, tStyleMod.end);
return true;
}
}
return false;
}); });
std::erase_if(tStyleModifiers, [&toPlace](TextStyleRangeModifier& tStyleMod) { insert_style_at_pos(start, modifier);
if(tStyleMod.modifier->get_type() == toPlace.modifier->get_type()) { insert_style_at_pos(end, lastModOfThisTypeBeforeEnd);
bool tStyleModStartInToPlace = (tStyleMod.start >= toPlace.start && tStyleMod.start <= toPlace.end);
bool tStyleModEndInToPlace = (tStyleMod.end >= toPlace.start && tStyleMod.end <= toPlace.end);
if(tStyleModStartInToPlace && tStyleModEndInToPlace)
return true;
else if(tStyleModStartInToPlace)
tStyleMod.start = toPlace.end;
else if(tStyleModEndInToPlace)
tStyleMod.end = toPlace.start;
}
return false;
});
tStyleModifiers.emplace_back(toPlace);
fit_text_style_ranges_in_text();
needsRebuild = true; needsRebuild = true;
} }
} }
void RichTextBox::insert_style_at_pos(TextPosition pos, const std::shared_ptr<TextStyleModifier>& modifier) {
size_t indexToPlaceAt = 0;
for(auto& [tPos, tStyleModsInPos] : tStyleMods) {
if(tPos > pos)
break;
else if(tPos == pos)
tStyleModsInPos[modifier->get_type()] = modifier;
indexToPlaceAt++;
}
auto it = tStyleMods.insert(tStyleMods.begin() + indexToPlaceAt, PositionedTextStyleMod{pos});
it->mods[modifier->get_type()] = modifier;
}
RichTextBox::TextPosition RichTextBox::move(Movement movement, TextPosition pos, std::optional<float>* previousX, bool flipDependingOnTextDirection) { RichTextBox::TextPosition RichTextBox::move(Movement movement, TextPosition pos, std::optional<float>* previousX, bool flipDependingOnTextDirection) {
if(pos.fParagraphIndex >= paragraphs.size()) { if(pos.fParagraphIndex >= paragraphs.size()) {
pos.fParagraphIndex = paragraphs.size() - 1; pos.fParagraphIndex = paragraphs.size() - 1;
@ -550,26 +588,11 @@ void RichTextBox::set_font_collection(const sk_sp<skia::textlayout::FontCollecti
} }
} }
void RichTextBox::fit_text_style_ranges_in_text() {
std::erase_if(tStyleModifiers, [&](TextStyleRangeModifier& tStyleMod) {
tStyleMod.start = move(Movement::NOWHERE, tStyleMod.start);
tStyleMod.end = move(Movement::NOWHERE, tStyleMod.end);
return tStyleMod.start == tStyleMod.end;
});
}
void RichTextBox::rebuild() { void RichTextBox::rebuild() {
if(needsRebuild) { if(needsRebuild) {
float heightOffset = 0.0f; float heightOffset = 0.0f;
std::map<TextPosition, std::vector<std::shared_ptr<TextStyleModifier>>> modifierStackCommands;
for(TextStyleRangeModifier& modifierRange : tStyleModifiers) { auto nextTStyleModIt = tStyleMods.begin();
modifierStackCommands[modifierRange.start].emplace_back(modifierRange.modifier);
modifierStackCommands[modifierRange.end].emplace_back(modifierRange.modifier);
}
auto nextStackCommandIt = modifierStackCommands.begin();
std::unordered_map<TextStyleModifier::ModifierType, std::vector<std::shared_ptr<TextStyleModifier>>> modifierStacks; // Not really a stack, but we use the last element as the modifier
skia::textlayout::TextStyle tStyle = initialTStyle; skia::textlayout::TextStyle tStyle = initialTStyle;
@ -582,39 +605,26 @@ void RichTextBox::rebuild() {
skia::textlayout::ParagraphBuilderImpl a(pData.pStyle, fontCollection, SkUnicodes::ICU::Make()); skia::textlayout::ParagraphBuilderImpl a(pData.pStyle, fontCollection, SkUnicodes::ICU::Make());
for(;;) { for(;;) {
if(nextStackCommandIt == modifierStackCommands.end() || nextStackCommandIt->first.fParagraphIndex != pIndex) { if(nextTStyleModIt == tStyleMods.end() || nextTStyleModIt->pos.fParagraphIndex != pIndex) {
a.pushStyle(tStyle); a.pushStyle(tStyle);
a.addText(pData.text.c_str() + tIndex, pData.text.length() - tIndex); a.addText(pData.text.c_str() + tIndex, pData.text.length() - tIndex);
a.pop(); a.pop();
break; break;
} }
else { else {
auto& nextStackCommand = *nextStackCommandIt; auto& nextTStyleMod = *nextTStyleModIt;
// Print with previous style // Print with previous style
a.pushStyle(tStyle); a.pushStyle(tStyle);
a.addText(pData.text.c_str() + tIndex, nextStackCommand.first.fTextByteIndex - tIndex); a.addText(pData.text.c_str() + tIndex, nextTStyleMod.pos.fTextByteIndex - tIndex);
a.pop(); a.pop();
tIndex = nextStackCommand.first.fTextByteIndex; tIndex = nextTStyleMod.pos.fTextByteIndex;
// Modify stack with new data // Modify tStyle with new data
for(const std::shared_ptr<TextStyleModifier>& modifier : nextStackCommand.second) { for(auto& [modType, modifier] : nextTStyleMod.mods)
auto& modStack = modifierStacks[modifier->get_type()]; modifier->modify_text_style(tStyle);
auto foundInStackIt = std::find(modStack.begin(), modStack.end(), modifier);
if(foundInStackIt == modStack.end())
modStack.emplace_back(modifier);
else
modStack.erase(foundInStackIt);
}
// Modify style with new stacks ++nextTStyleModIt;
tStyle = initialTStyle;
for(auto& [modType, modifier] : modifierStacks) {
if(!modifier.empty())
modifier.back()->modify_text_style(tStyle);
}
++nextStackCommandIt;
} }
} }
@ -638,15 +648,13 @@ RichTextBox::TextPosition RichTextBox::insert(TextPosition pos, std::string_view
for(char c : textToInsert) { for(char c : textToInsert) {
if(c == '\n') { if(c == '\n') {
if(newlinesAllowed) { if(newlinesAllowed) {
for(TextStyleRangeModifier& tStyle : tStyleModifiers) { for(auto& [p, tStylesInPos] : tStyleMods) {
tStyle.apply_to_start_and_end([&pos](TextPosition& p){
if(p.fParagraphIndex > pos.fParagraphIndex) if(p.fParagraphIndex > pos.fParagraphIndex)
p.fParagraphIndex++; p.fParagraphIndex++;
else if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex) { else if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex) {
p.fTextByteIndex = p.fTextByteIndex - pos.fTextByteIndex; p.fTextByteIndex = p.fTextByteIndex - pos.fTextByteIndex;
p.fParagraphIndex++; p.fParagraphIndex++;
} }
});
} }
paragraphs.insert(paragraphs.begin() + pos.fParagraphIndex + 1, ParagraphData{}); paragraphs.insert(paragraphs.begin() + pos.fParagraphIndex + 1, ParagraphData{});
paragraphs[pos.fParagraphIndex + 1].text = paragraphs[pos.fParagraphIndex].text.substr(pos.fTextByteIndex, paragraphs[pos.fParagraphIndex].text.size() - pos.fTextByteIndex); paragraphs[pos.fParagraphIndex + 1].text = paragraphs[pos.fParagraphIndex].text.substr(pos.fTextByteIndex, paragraphs[pos.fParagraphIndex].text.size() - pos.fTextByteIndex);
@ -656,28 +664,23 @@ RichTextBox::TextPosition RichTextBox::insert(TextPosition pos, std::string_view
} }
} }
else if(c == '\t') { else if(c == '\t') {
for(TextStyleRangeModifier& tStyle : tStyleModifiers) { for(auto& [p, tStylesInPos] : tStyleMods) {
tStyle.apply_to_start_and_end([&pos, tabWidth = tabWidth](TextPosition& p) {
if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex) if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex)
p.fTextByteIndex += tabWidth; p.fTextByteIndex += tabWidth;
});
} }
paragraphs[pos.fParagraphIndex].text.insert(pos.fTextByteIndex, std::string(tabWidth, ' ')); paragraphs[pos.fParagraphIndex].text.insert(pos.fTextByteIndex, std::string(tabWidth, ' '));
pos.fTextByteIndex += tabWidth; pos.fTextByteIndex += tabWidth;
} }
else { else {
for(TextStyleRangeModifier& tStyle : tStyleModifiers) { for(auto& [p, tStylesInPos] : tStyleMods) {
tStyle.apply_to_start_and_end([&pos](TextPosition& p) {
if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex) if(p.fParagraphIndex == pos.fParagraphIndex && p.fTextByteIndex >= pos.fTextByteIndex)
p.fTextByteIndex++; p.fTextByteIndex++;
});
} }
paragraphs[pos.fParagraphIndex].text.insert(paragraphs[pos.fParagraphIndex].text.begin() + pos.fTextByteIndex, c); paragraphs[pos.fParagraphIndex].text.insert(paragraphs[pos.fParagraphIndex].text.begin() + pos.fTextByteIndex, c);
pos.fTextByteIndex++; pos.fTextByteIndex++;
} }
} }
fit_text_style_ranges_in_text();
needsRebuild = true; needsRebuild = true;
return pos; return pos;
} }
@ -702,8 +705,11 @@ RichTextBox::TextPosition RichTextBox::remove(TextPosition p1, TextPosition p2)
paragraphs.erase(paragraphs.begin() + start.fParagraphIndex + 1, paragraphs.begin() + end.fParagraphIndex + 1); paragraphs.erase(paragraphs.begin() + start.fParagraphIndex + 1, paragraphs.begin() + end.fParagraphIndex + 1);
} }
for(TextStyleRangeModifier& tStyle : tStyleModifiers) { std::optional<int32_t> prevTextStyleModIndex;
tStyle.apply_to_start_and_end([&start, &end](TextPosition& p) {
for(int32_t i = 0; i < static_cast<int32_t>(tStyleMods.size()); i++) {
PositionedTextStyleMod& tStyleMod = tStyleMods[i];
auto& p = tStyleMod.pos;
if(p > start) { if(p > start) {
if(end > p) if(end > p)
p = start; p = start;
@ -716,10 +722,15 @@ RichTextBox::TextPosition RichTextBox::remove(TextPosition p1, TextPosition p2)
else else
p.fParagraphIndex -= (end.fParagraphIndex - start.fParagraphIndex); p.fParagraphIndex -= (end.fParagraphIndex - start.fParagraphIndex);
} }
}); if(prevTextStyleModIndex.has_value() && tStyleMods[prevTextStyleModIndex.value()].pos == p) {
auto& tStyleModsToMergeWith = tStyleMods[prevTextStyleModIndex.value()].mods;
for(auto& [modType, tStyle] : tStyleMod.mods)
tStyleModsToMergeWith[modType] = tStyle;
tStyleMods.erase(tStyleMods.begin() + i);
i--;
}
} }
fit_text_style_ranges_in_text();
needsRebuild = true; needsRebuild = true;
return start; return start;
} }

View file

@ -10,6 +10,7 @@
class RichTextBox { class RichTextBox {
public: public:
RichTextBox(); RichTextBox();
enum class Movement { enum class Movement {
@ -38,15 +39,13 @@ class RichTextBox {
bool operator<=(const RichTextBox::TextPosition& o) const; bool operator<=(const RichTextBox::TextPosition& o) const;
}; };
struct TextStyleRangeModifier { struct PositionedTextStyleMod {
TextPosition start; TextPosition pos;
TextPosition end; std::unordered_map<TextStyleModifier::ModifierType, std::shared_ptr<TextStyleModifier>> mods;
std::shared_ptr<TextStyleModifier> modifier;
void apply_to_start_and_end(const std::function<void(TextPosition&)>& f);
void save(cereal::PortableBinaryOutputArchive& a) const;
void load(cereal::PortableBinaryInputArchive& a);
}; };
typedef std::vector<PositionedTextStyleMod> TextStyleModContainer;
struct Cursor { struct Cursor {
TextPosition pos = {0, 0}; TextPosition pos = {0, 0};
TextPosition selectionBeginPos = {0, 0}; TextPosition selectionBeginPos = {0, 0};
@ -69,11 +68,10 @@ class RichTextBox {
a(text); a(text);
} }
}; };
template<typename Archive> void serialize(Archive& a) { void save(cereal::PortableBinaryOutputArchive& a) const;
a(paragraphs, tStyleModifiers); void load(cereal::PortableBinaryInputArchive& a);
} TextStyleModContainer tStyleMods;
std::vector<Paragraph> paragraphs; std::vector<Paragraph> paragraphs;
std::vector<TextStyleRangeModifier> tStyleModifiers;
}; };
TextPosition move(Movement movement, TextPosition pos, std::optional<float>* previousX = nullptr, bool flipDependingOnTextDirection = false); TextPosition move(Movement movement, TextPosition pos, std::optional<float>* previousX = nullptr, bool flipDependingOnTextDirection = false);
@ -99,6 +97,7 @@ class RichTextBox {
}; };
void set_initial_text_style(const skia::textlayout::TextStyle& tStyle); void set_initial_text_style(const skia::textlayout::TextStyle& tStyle);
void set_initial_text_style_modifier(const std::shared_ptr<TextStyleModifier>& modifier);
void set_text_style_modifier_between(TextPosition p1, TextPosition p2, const std::shared_ptr<TextStyleModifier>& modifier); void set_text_style_modifier_between(TextPosition p1, TextPosition p2, const std::shared_ptr<TextStyleModifier>& modifier);
RichTextData get_rich_text_data(); RichTextData get_rich_text_data();
@ -125,11 +124,12 @@ class RichTextBox {
void rects_between_text_positions_func(TextPosition p1, TextPosition p2, std::function<void(const SkRect& r)> f); void rects_between_text_positions_func(TextPosition p1, TextPosition p2, std::function<void(const SkRect& r)> f);
void fit_text_style_ranges_in_text();
SkRect get_cursor_rect(TextPosition pos); SkRect get_cursor_rect(TextPosition pos);
TextPosition get_text_pos_closest_to_point(Vector2f point); TextPosition get_text_pos_closest_to_point(Vector2f point);
std::shared_ptr<TextStyleModifier> get_last_text_style_mod_before_pos(TextPosition pos, TextStyleModifier::ModifierType modType);
void erase_if_over_all_styles_until_pos(TextPosition pos, const std::function<bool(TextPosition, const std::shared_ptr<TextStyleModifier>&)>& func);
void insert_style_at_pos(TextPosition pos, const std::shared_ptr<TextStyleModifier>& modifier);
void rebuild(); void rebuild();
@ -147,6 +147,6 @@ class RichTextBox {
}; };
skia::textlayout::TextStyle initialTStyle; skia::textlayout::TextStyle initialTStyle;
TextStyleModContainer tStyleMods;
std::vector<ParagraphData> paragraphs; std::vector<ParagraphData> paragraphs;
std::vector<TextStyleRangeModifier> tStyleModifiers;
}; };