[Label] Handle text as multiple independent paragraphs.

This commit is contained in:
bruvzg 2024-10-28 10:37:07 +02:00 committed by Pāvels Nadtočajevs
parent 76fa7b2914
commit b329b4ab06
6 changed files with 581 additions and 340 deletions

View file

@ -74,6 +74,9 @@
Limits the lines of text the node shows on screen.
</member>
<member name="mouse_filter" type="int" setter="set_mouse_filter" getter="get_mouse_filter" overrides="Control" enum="Control.MouseFilter" default="2" />
<member name="paragraph_separator" type="String" setter="set_paragraph_separator" getter="get_paragraph_separator" default="&quot;\\n&quot;">
String used as a paragraph separator. Each paragraph is processed independently, in its own BiDi context.
</member>
<member name="size_flags_vertical" type="int" setter="set_v_size_flags" getter="get_v_size_flags" overrides="Control" enum="Control.SizeFlags" is_bitfield="true" default="4" />
<member name="structured_text_bidi_override" type="int" setter="set_structured_text_bidi_override" getter="get_structured_text_bidi_override" enum="TextServer.StructuredTextParser" default="0">
Set BiDi algorithm override for the structured text.
@ -129,6 +132,9 @@
[b]Note:[/b] If using a font with [member FontFile.multichannel_signed_distance_field] enabled, its [member FontFile.msdf_pixel_range] must be set to at least [i]twice[/i] the value of [theme_item outline_size] for outline rendering to look correct. Otherwise, the outline may appear to be cut off earlier than intended.
[b]Note:[/b] Using a value that is larger than half the font size is not recommended, as the font outline may fail to be fully closed in this case.
</theme_item>
<theme_item name="paragraph_spacing" data_type="constant" type="int" default="0">
Vertical space between paragraphs. Added on top of [theme_item line_spacing].
</theme_item>
<theme_item name="shadow_offset_x" data_type="constant" type="int" default="1">
The horizontal offset of the text's shadow.
</theme_item>

View file

@ -27,6 +27,9 @@
<member name="outline_size" type="int" setter="set_outline_size" getter="get_outline_size" default="0">
Text outline size.
</member>
<member name="paragraph_spacing" type="float" setter="set_paragraph_spacing" getter="get_paragraph_spacing" default="0.0">
Vertical space between paragraphs. Added on top of [member line_spacing].
</member>
<member name="shadow_color" type="Color" setter="set_shadow_color" getter="get_shadow_color" default="Color(0, 0, 0, 0)">
Color of the shadow effect. If alpha is [code]0[/code], no shadow will be drawn.
</member>

File diff suppressed because it is too large Load diff

View file

@ -57,11 +57,21 @@ private:
Size2 minsize;
bool uppercase = false;
bool lines_dirty = true;
struct Paragraph {
bool lines_dirty = true;
bool dirty = true;
int start = 0;
String text;
RID text_rid;
Vector<RID> lines_rid;
};
bool dirty = true;
bool font_dirty = true;
RID text_rid;
Vector<RID> lines_rid;
bool text_dirty = true;
Vector<Paragraph> paragraphs;
int total_line_count = 0;
String paragraph_separator = "\\n";
String language;
TextDirection text_direction = TEXT_DIRECTION_AUTO;
@ -83,6 +93,7 @@ private:
int font_size = 0;
int line_spacing = 0;
int paragraph_spacing = 0;
Color font_color;
Color font_shadow_color;
Point2 font_shadow_offset;
@ -91,6 +102,7 @@ private:
int font_shadow_outline_size;
} theme_cache;
Rect2 _get_line_rect(int p_para, int p_line) const;
void _ensure_shaped() const;
void _update_visible();
void _shape();
@ -99,7 +111,7 @@ private:
protected:
RID get_line_rid(int p_line) const;
Rect2 get_line_rect(int p_line) const;
void get_layout_data(Vector2 &r_offset, int &r_line_limit, int &r_line_spacing) const;
int get_layout_data(Vector2 &r_offset, int &r_last_line, int &r_line_spacing) const;
void _notification(int p_what);
static void _bind_methods();
@ -129,6 +141,9 @@ public:
void set_language(const String &p_language);
String get_language() const;
void set_paragraph_separator(const String &p_paragraph_separator);
String get_paragraph_separator() const;
void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
TextServer::StructuredTextParser get_structured_text_bidi_override() const;

View file

@ -38,6 +38,9 @@ void LabelSettings::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_line_spacing", "spacing"), &LabelSettings::set_line_spacing);
ClassDB::bind_method(D_METHOD("get_line_spacing"), &LabelSettings::get_line_spacing);
ClassDB::bind_method(D_METHOD("set_paragraph_spacing", "spacing"), &LabelSettings::set_paragraph_spacing);
ClassDB::bind_method(D_METHOD("get_paragraph_spacing"), &LabelSettings::get_paragraph_spacing);
ClassDB::bind_method(D_METHOD("set_font", "font"), &LabelSettings::set_font);
ClassDB::bind_method(D_METHOD("get_font"), &LabelSettings::get_font);
@ -63,6 +66,7 @@ void LabelSettings::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_shadow_offset"), &LabelSettings::get_shadow_offset);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "line_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_line_spacing", "get_line_spacing");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "paragraph_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_paragraph_spacing", "get_paragraph_spacing");
ADD_GROUP("Font", "font_");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_font", "get_font");
@ -90,6 +94,17 @@ real_t LabelSettings::get_line_spacing() const {
return line_spacing;
}
void LabelSettings::set_paragraph_spacing(real_t p_spacing) {
if (paragraph_spacing != p_spacing) {
paragraph_spacing = p_spacing;
emit_changed();
}
}
real_t LabelSettings::get_paragraph_spacing() const {
return paragraph_spacing;
}
void LabelSettings::set_font(const Ref<Font> &p_font) {
if (font != p_font) {
if (font.is_valid()) {

View file

@ -40,6 +40,7 @@ class LabelSettings : public Resource {
GDCLASS(LabelSettings, Resource);
real_t line_spacing = 3;
real_t paragraph_spacing = 0;
Ref<Font> font;
int font_size = Font::DEFAULT_FONT_SIZE;
@ -61,6 +62,9 @@ public:
void set_line_spacing(real_t p_spacing);
real_t get_line_spacing() const;
void set_paragraph_spacing(real_t p_spacing);
real_t get_paragraph_spacing() const;
void set_font(const Ref<Font> &p_font);
Ref<Font> get_font() const;