Made modifiers immutable to prevent mistakes

This commit is contained in:
Yousef Khadadeh 2025-11-09 15:01:57 +03:00
parent 45da7939eb
commit fabccaf553
4 changed files with 85 additions and 91 deletions

View file

@ -38,17 +38,9 @@ std::shared_ptr<TextStyleModifier> TextStyleModifier::get_default_modifier(Modif
const TextStyleModifier::ModifierMap& TextStyleModifier::get_default_modifiers() {
if(defaultMods.empty()) {
auto weightMod = std::make_shared<WeightTextStyleModifier>();
weightMod->set_weight(SkFontStyle::kNormal_Weight);
defaultMods[ModifierType::WEIGHT] = weightMod;
auto slantMod = std::make_shared<SlantTextStyleModifier>();
slantMod->set_slant(SkFontStyle::kUpright_Slant);
defaultMods[ModifierType::SLANT] = slantMod;
auto decorationMod = std::make_shared<DecorationTextStyleModifier>();
decorationMod->decorationValue = skia::textlayout::TextDecoration::kNoDecoration;
defaultMods[ModifierType::DECORATION] = decorationMod;
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);
}
return defaultMods;
}
@ -63,10 +55,16 @@ TextStyleModifier::~TextStyleModifier() {}
WeightTextStyleModifier::WeightTextStyleModifier(SkFontStyle::Weight initWeight): weightValue(initWeight / WEIGHT_VALUE_MODIFIER) {}
TextStyleModifier::ModifierType WeightTextStyleModifier::get_type() const {
return ModifierType::WEIGHT;
}
SkFontStyle::Weight WeightTextStyleModifier::get_weight() const {
return static_cast<SkFontStyle::Weight>(static_cast<int>(weightValue) * WEIGHT_VALUE_MODIFIER);
}
void WeightTextStyleModifier::save(cereal::PortableBinaryOutputArchive& a) const {
a(weightValue);
}
@ -75,14 +73,6 @@ void WeightTextStyleModifier::load(cereal::PortableBinaryInputArchive& a) {
a(weightValue);
}
void WeightTextStyleModifier::set_weight(SkFontStyle::Weight newWeight) {
weightValue = newWeight / WEIGHT_VALUE_MODIFIER;
}
SkFontStyle::Weight WeightTextStyleModifier::get_weight() {
return static_cast<SkFontStyle::Weight>(static_cast<int>(weightValue) * WEIGHT_VALUE_MODIFIER);
}
void WeightTextStyleModifier::modify_text_style(skia::textlayout::TextStyle& style) const {
style.setFontStyle(SkFontStyle(static_cast<int>(weightValue) * WEIGHT_VALUE_MODIFIER, style.getFontStyle().width(), style.getFontStyle().slant()));
}
@ -93,10 +83,16 @@ bool WeightTextStyleModifier::equivalent_data(TextStyleModifier& modifier) const
SlantTextStyleModifier::SlantTextStyleModifier(uint8_t initSlantValue): slantValue(initSlantValue) {}
TextStyleModifier::ModifierType SlantTextStyleModifier::get_type() const {
return ModifierType::SLANT;
}
SkFontStyle::Slant SlantTextStyleModifier::get_slant() const {
return static_cast<SkFontStyle::Slant>(slantValue);
}
void SlantTextStyleModifier::save(cereal::PortableBinaryOutputArchive& a) const {
a(slantValue);
}
@ -105,14 +101,6 @@ void SlantTextStyleModifier::load(cereal::PortableBinaryInputArchive& a) {
a(slantValue);
}
void SlantTextStyleModifier::set_slant(SkFontStyle::Slant newSlant) {
slantValue = newSlant;
}
SkFontStyle::Slant SlantTextStyleModifier::get_slant() {
return static_cast<SkFontStyle::Slant>(slantValue);
}
void SlantTextStyleModifier::modify_text_style(skia::textlayout::TextStyle& style) const {
style.setFontStyle(SkFontStyle(style.getFontStyle().weight(), style.getFontStyle().width(), static_cast<SkFontStyle::Slant>(slantValue)));
}
@ -123,10 +111,16 @@ bool SlantTextStyleModifier::equivalent_data(TextStyleModifier& modifier) const
ColorTextStyleModifier::ColorTextStyleModifier(const Vector4f& initColor): color(initColor) {}
TextStyleModifier::ModifierType ColorTextStyleModifier::get_type() const {
return ModifierType::COLOR;
}
const Vector4f& ColorTextStyleModifier::get_color() const {
return color;
}
void ColorTextStyleModifier::save(cereal::PortableBinaryOutputArchive& a) const {
a(color);
}
@ -146,10 +140,16 @@ bool ColorTextStyleModifier::equivalent_data(TextStyleModifier& modifier) const
SizeTextStyleModifier::SizeTextStyleModifier(float initSize): size(initSize) {}
TextStyleModifier::ModifierType SizeTextStyleModifier::get_type() const {
return ModifierType::SIZE;
}
float SizeTextStyleModifier::get_size() const {
return size;
}
void SizeTextStyleModifier::save(cereal::PortableBinaryOutputArchive& a) const {
a(size);
}
@ -168,10 +168,16 @@ bool SizeTextStyleModifier::equivalent_data(TextStyleModifier& modifier) const {
FontFamiliesTextStyleModifier::FontFamiliesTextStyleModifier(const std::vector<SkString>& initFamilies): families(initFamilies) {}
TextStyleModifier::ModifierType FontFamiliesTextStyleModifier::get_type() const {
return ModifierType::FONT_FAMILIES;
}
const std::vector<SkString>& FontFamiliesTextStyleModifier::get_families() const {
return families;
}
void FontFamiliesTextStyleModifier::save(cereal::PortableBinaryOutputArchive& a) const {
a(families);
}
@ -192,10 +198,16 @@ bool FontFamiliesTextStyleModifier::equivalent_data(TextStyleModifier& modifier)
DecorationTextStyleModifier::DecorationTextStyleModifier(uint8_t initDecorationValue): decorationValue(initDecorationValue) {}
TextStyleModifier::ModifierType DecorationTextStyleModifier::get_type() const {
return ModifierType::DECORATION;
}
uint8_t DecorationTextStyleModifier::get_decoration_value() const {
return decorationValue;
}
void DecorationTextStyleModifier::save(cereal::PortableBinaryOutputArchive& a) const {
a(decorationValue);
}