Use textbox to draw textparagraph element

This commit is contained in:
Yousef Khadadeh 2026-04-07 22:21:06 +03:00
parent a181a4d51d
commit caaad67c42
6 changed files with 64 additions and 37 deletions

View file

@ -144,30 +144,16 @@ void FontPicker::layout(const Clay_ElementId& id, std::string* newFontName, cons
}, },
.backgroundColor = convert_vec4<Clay_Color>(entryColor) .backgroundColor = convert_vec4<Clay_Color>(entryColor)
}) { }) {
skia::textlayout::ParagraphStyle pStyle; TextParagraph::Data d;
pStyle.setTextAlign(skia::textlayout::TextAlign::kLeft); RichText::TextData::Paragraph& par = d.text.paragraphs.emplace_back();
pStyle.setTextHeightBehavior(skia::textlayout::kDisableAll); par.text = sortedFontList[i];
d.width = 10000.0f; // Setting width to numeric_limits::max will lead to multiplying it by any number greater than 1 to = infinity, which will cause issues
skia::textlayout::StrutStyle strutStyle; RichText::PositionedTextStyleMod& positionedMod = d.text.tStyleMods.emplace_back();
strutStyle.setStrutEnabled(true); positionedMod.pos = {0, 0};
strutStyle.setForceStrutHeight(true); positionedMod.mods[RichText::TextStyleModifier::ModifierType::FONT_FAMILIES] = std::make_shared<RichText::FontFamiliesTextStyleModifier>(std::vector<SkString>{SkString{sortedFontList[i].c_str(), sortedFontList[i].size()}});
pStyle.setStrutStyle(strutStyle);
skia::textlayout::TextStyle tStyle; gui.element<TextParagraph>("font name", d);
tStyle.setFontSize(gui.io.fontSize);
tStyle.setFontFamilies({SkString{sortedFontList[i].c_str()}, SkString{"Roboto"}});
tStyle.setForegroundColor(SkPaint{gui.io.theme->frontColor1});
pStyle.setTextStyle(tStyle);
skia::textlayout::ParagraphBuilderImpl pBuilder(pStyle, gui.io.fonts->collection, SkUnicodes::ICU::Make());
tStyle.setFontStyle(SkFontStyle::Normal());
pBuilder.pushStyle(tStyle);
pBuilder.addText(sortedFontList[i].c_str(), sortedFontList[i].size());
std::shared_ptr<skia::textlayout::Paragraph> p = pBuilder.Build();
gui.element<TextParagraph>("font name", p, 10000.0f); // Setting width to numeric_limits::max will lead to multiplying it by any number greater than 1 to = infinity, which will cause issues
} }
} }
}, LayoutElement::Callbacks{ }, LayoutElement::Callbacks{

View file

@ -1,28 +1,52 @@
#include "TextParagraph.hpp" #include "TextParagraph.hpp"
#include <include/core/SkPath.h> #include <include/core/SkPath.h>
#include <iostream> #include <iostream>
#include "../GUIManager.hpp"
namespace GUIStuff { namespace GUIStuff {
TextParagraph::TextParagraph(GUIManager& gui): TextParagraph::TextParagraph(GUIManager& gui):
Element(gui) {} Element(gui) {}
void TextParagraph::layout(const Clay_ElementId& id, const std::shared_ptr<skia::textlayout::Paragraph>& paragraph, float width) { void TextParagraph::layout(const Clay_ElementId& id, const Data& newData) {
data = paragraph; if(!textbox) {
data->layout(width); d = newData;
textbox = std::make_unique<RichText::TextBox>();
textbox->set_width(d.width);
textbox->set_rich_text_data(d.text);
textbox->set_font_data(gui.io.fonts);
textbox->set_allow_newlines(true);
}
if(d.width != newData.width) {
d.width = newData.width;
textbox->set_width(d.width);
}
if(d.text != newData.text) {
d.text = newData.text;
textbox->set_rich_text_data(d.text);
}
CLAY(id, { CLAY(id, {
.layout = { .layout = {
.sizing = {.width = CLAY_SIZING_FIXED(width), .height = CLAY_SIZING_FIXED(data->getHeight())} .sizing = {.width = CLAY_SIZING_FIXED(d.width), .height = CLAY_SIZING_FIXED(textbox->get_height())}
}, },
.custom = { .customData = this } .custom = { .customData = this }
}) {} }) {}
} }
void TextParagraph::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) { void TextParagraph::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) {
if(data) {
auto& bb = boundingBox.value(); auto& bb = boundingBox.value();
data->paint(canvas, bb.min.x(), bb.min.y()); canvas->save();
} canvas->translate(bb.min.x(), bb.min.y());
skia::textlayout::TextStyle tStyle;
tStyle.setFontFamilies(io.fonts->get_default_font_families());
tStyle.setFontSize(io.fontSize);
tStyle.setForegroundPaint(SkPaint{io.theme->frontColor1});
textbox->set_initial_text_style(tStyle);
textbox->paint(canvas, { .skiaAA = skiaAA });
canvas->restore();
} }
} }

View file

@ -1,16 +1,24 @@
#pragma once #pragma once
#include "Element.hpp" #include "Element.hpp"
#include <modules/skparagraph/include/Paragraph.h> #include "../../RichText/TextBox.hpp"
namespace GUIStuff { namespace GUIStuff {
class TextParagraph : public Element { class TextParagraph : public Element {
public: public:
struct Data {
RichText::TextData text;
float width;
bool operator==(const Data& d) const = default;
bool operator!=(const Data& d) const = default;
};
TextParagraph(GUIManager& gui); TextParagraph(GUIManager& gui);
void layout(const Clay_ElementId& id, const std::shared_ptr<skia::textlayout::Paragraph>& paragraph, float width); void layout(const Clay_ElementId& id, const Data& newData);
virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override; virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override;
private: private:
std::shared_ptr<skia::textlayout::Paragraph> data; Data d;
std::unique_ptr<RichText::TextBox> textbox;
}; };
} }

View file

@ -76,6 +76,9 @@ void MainProgram::update() {
deltaTime.update_time_since(); deltaTime.update_time_since();
deltaTime.update_time_point(); deltaTime.update_time_point();
g.update();
toolbar.update();
for(auto& w : worlds) { for(auto& w : worlds) {
if(w == world) if(w == world)
w->focus_update(); w->focus_update();
@ -83,9 +86,6 @@ void MainProgram::update() {
w->unfocus_update(); w->unfocus_update();
} }
g.update();
toolbar.update();
NetLibrary::update(); NetLibrary::update();
post_callback(); post_callback();

View file

@ -15,6 +15,8 @@ struct ParagraphStyleData {
textAlignment = static_cast<skia::textlayout::TextAlign>(alignRead); textAlignment = static_cast<skia::textlayout::TextAlign>(alignRead);
textDirection = static_cast<skia::textlayout::TextDirection>(directionRead); textDirection = static_cast<skia::textlayout::TextDirection>(directionRead);
} }
bool operator==(const ParagraphStyleData& o) const = default;
bool operator!=(const ParagraphStyleData& o) const = default;
skia::textlayout::ParagraphStyle get_paragraph_style() const; skia::textlayout::ParagraphStyle get_paragraph_style() const;
}; };

View file

@ -27,6 +27,7 @@ struct TextPosition {
fTextByteIndex = t; fTextByteIndex = t;
} }
bool operator==(const TextPosition& o) const = default; bool operator==(const TextPosition& o) const = default;
bool operator!=(const TextPosition& o) const = default;
bool operator<(const TextPosition& o) const; bool operator<(const TextPosition& o) const;
bool operator>(const TextPosition& o) const; bool operator>(const TextPosition& o) const;
bool operator>=(const TextPosition& o) const; bool operator>=(const TextPosition& o) const;
@ -36,6 +37,8 @@ struct TextPosition {
struct PositionedTextStyleMod { struct PositionedTextStyleMod {
TextPosition pos; TextPosition pos;
TextStyleModifier::ModifierMap mods; TextStyleModifier::ModifierMap mods;
bool operator==(const PositionedTextStyleMod& o) const = default;
bool operator!=(const PositionedTextStyleMod& o) const = default;
}; };
typedef std::vector<PositionedTextStyleMod> TextStyleModContainer; typedef std::vector<PositionedTextStyleMod> TextStyleModContainer;
@ -45,6 +48,8 @@ class TextData {
struct Paragraph { struct Paragraph {
std::string text; std::string text;
ParagraphStyleData pStyleData; ParagraphStyleData pStyleData;
bool operator==(const Paragraph& o) const = default;
bool operator!=(const Paragraph& o) const = default;
template<typename Archive> void save(Archive& a) const { template<typename Archive> void save(Archive& a) const {
a(text, pStyleData); a(text, pStyleData);
} }
@ -58,6 +63,8 @@ class TextData {
std::string get_plain_text() const; std::string get_plain_text() const;
void save(cereal::PortableBinaryOutputArchive& a) const; void save(cereal::PortableBinaryOutputArchive& a) const;
void load(cereal::PortableBinaryInputArchive& a); void load(cereal::PortableBinaryInputArchive& a);
bool operator==(const TextData& o) const = default;
bool operator!=(const TextData& o) const = default;
TextStyleModContainer tStyleMods; TextStyleModContainer tStyleMods;
std::vector<Paragraph> paragraphs; std::vector<Paragraph> paragraphs;
}; };