Skip to content

Commit

Permalink
Merge pull request #75250 from bruvzg/lbl_rtl_jst_flags
Browse files Browse the repository at this point in the history
Expose TextServer justification flags to Label, Label3D, TextMesh and RTL. Add flags to control last/single line justification.
  • Loading branch information
akien-mga committed May 16, 2023
2 parents 265c70a + 9163d8c commit 59c19a7
Show file tree
Hide file tree
Showing 24 changed files with 292 additions and 46 deletions.
8 changes: 4 additions & 4 deletions doc/classes/CanvasItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
<param index="6" name="max_lines" type="int" default="-1" />
<param index="7" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<param index="8" name="brk_flags" type="int" enum="TextServer.LineBreakFlag" default="3" />
<param index="9" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="9" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="10" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="11" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand All @@ -215,7 +215,7 @@
<param index="7" name="size" type="int" default="1" />
<param index="8" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<param index="9" name="brk_flags" type="int" enum="TextServer.LineBreakFlag" default="3" />
<param index="10" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="10" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="11" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="12" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand Down Expand Up @@ -311,7 +311,7 @@
<param index="4" name="width" type="float" default="-1" />
<param index="5" name="font_size" type="int" default="16" />
<param index="6" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<param index="7" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="7" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="8" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="9" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand Down Expand Up @@ -348,7 +348,7 @@
<param index="5" name="font_size" type="int" default="16" />
<param index="6" name="size" type="int" default="1" />
<param index="7" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<param index="8" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="8" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="9" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="10" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand Down
12 changes: 6 additions & 6 deletions doc/classes/Font.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<param index="6" name="max_lines" type="int" default="-1" />
<param index="7" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<param index="8" name="brk_flags" type="int" enum="TextServer.LineBreakFlag" default="3" />
<param index="9" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="9" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="10" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="11" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand All @@ -65,7 +65,7 @@
<param index="7" name="size" type="int" default="1" />
<param index="8" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<param index="9" name="brk_flags" type="int" enum="TextServer.LineBreakFlag" default="3" />
<param index="10" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="10" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="11" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="12" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand All @@ -82,7 +82,7 @@
<param index="4" name="width" type="float" default="-1" />
<param index="5" name="font_size" type="int" default="16" />
<param index="6" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<param index="7" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="7" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="8" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="9" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand All @@ -100,7 +100,7 @@
<param index="5" name="font_size" type="int" default="16" />
<param index="6" name="size" type="int" default="1" />
<param index="7" name="modulate" type="Color" default="Color(1, 1, 1, 1)" />
<param index="8" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="8" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="9" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="10" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand Down Expand Up @@ -201,7 +201,7 @@
<param index="3" name="font_size" type="int" default="16" />
<param index="4" name="max_lines" type="int" default="-1" />
<param index="5" name="brk_flags" type="int" enum="TextServer.LineBreakFlag" default="3" />
<param index="6" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="6" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="7" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="8" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand Down Expand Up @@ -234,7 +234,7 @@
<param index="1" name="alignment" type="int" enum="HorizontalAlignment" default="0" />
<param index="2" name="width" type="float" default="-1" />
<param index="3" name="font_size" type="int" default="16" />
<param index="4" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="4" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="5" name="direction" type="int" enum="TextServer.Direction" default="0" />
<param index="6" name="orientation" type="int" enum="TextServer.Orientation" default="0" />
<description>
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/Label.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<member name="horizontal_alignment" type="int" setter="set_horizontal_alignment" getter="get_horizontal_alignment" enum="HorizontalAlignment" default="0">
Controls the text's horizontal alignment. Supports left, center, right, and fill, or justify. Set it to one of the [enum HorizontalAlignment] constants.
</member>
<member name="justification_flags" type="int" setter="set_justification_flags" getter="get_justification_flags" enum="TextServer.JustificationFlag" default="163">
Line fill alignment rules. For more info see [enum TextServer.JustificationFlag].
</member>
<member name="label_settings" type="LabelSettings" setter="set_label_settings" getter="get_label_settings">
A [LabelSettings] resource that can be shared between multiple [Label] nodes. Takes priority over theme properties.
</member>
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/Label3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<member name="horizontal_alignment" type="int" setter="set_horizontal_alignment" getter="get_horizontal_alignment" enum="HorizontalAlignment" default="1">
Controls the text's horizontal alignment. Supports left, center, right, and fill, or justify. Set it to one of the [enum HorizontalAlignment] constants.
</member>
<member name="justification_flags" type="int" setter="set_justification_flags" getter="get_justification_flags" enum="TextServer.JustificationFlag" default="163">
Line fill alignment rules. For more info see [enum TextServer.JustificationFlag].
</member>
<member name="language" type="String" setter="set_language" getter="get_language" default="&quot;&quot;">
Language code used for line-breaking and text shaping algorithms, if left empty current locale is used instead.
</member>
Expand Down
1 change: 1 addition & 0 deletions doc/classes/RichTextLabel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@
<param index="1" name="base_direction" type="int" enum="Control.TextDirection" default="0" />
<param index="2" name="language" type="String" default="&quot;&quot;" />
<param index="3" name="st_parser" type="int" enum="TextServer.StructuredTextParser" default="0" />
<param index="4" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="163" />
<description>
Adds a [code][p][/code] tag to the tag stack.
</description>
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/TextMesh.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<member name="horizontal_alignment" type="int" setter="set_horizontal_alignment" getter="get_horizontal_alignment" enum="HorizontalAlignment" default="1">
Controls the text's horizontal alignment. Supports left, center, right, and fill, or justify. Set it to one of the [enum HorizontalAlignment] constants.
</member>
<member name="justification_flags" type="int" setter="set_justification_flags" getter="get_justification_flags" enum="TextServer.JustificationFlag" default="163">
Line fill alignment rules. For more info see [enum TextServer.JustificationFlag].
</member>
<member name="language" type="String" setter="set_language" getter="get_language" default="&quot;&quot;">
Language code used for text shaping algorithms, if left empty current locale is used instead.
</member>
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/TextParagraph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@
<member name="direction" type="int" setter="set_direction" getter="get_direction" enum="TextServer.Direction" default="0">
Text writing direction.
</member>
<member name="justification_flags" type="int" setter="set_justification_flags" getter="get_justification_flags" enum="TextServer.JustificationFlag" default="3">
Line alignment rules. For more info see [TextServer].
<member name="justification_flags" type="int" setter="set_justification_flags" getter="get_justification_flags" enum="TextServer.JustificationFlag" default="163">
Line fill alignment rules. For more info see [enum TextServer.JustificationFlag].
</member>
<member name="max_lines_visible" type="int" setter="set_max_lines_visible" getter="get_max_lines_visible" default="-1">
Limits the lines of text shown.
Expand Down
18 changes: 17 additions & 1 deletion doc/classes/TextServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@
<return type="float" />
<param index="0" name="shaped" type="RID" />
<param index="1" name="width" type="float" />
<param index="2" name="jst_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<param index="2" name="justification_flags" type="int" enum="TextServer.JustificationFlag" default="3" />
<description>
Adjusts text width to fit to specified width, returns new text width.
</description>
Expand Down Expand Up @@ -1395,6 +1395,13 @@
Breaks text into words and returns array of character ranges. Use [param grapheme_flags] to set what characters are used for breaking (see [enum GraphemeFlag]).
</description>
</method>
<method name="shaped_text_has_visible_chars" qualifiers="const">
<return type="bool" />
<param index="0" name="shaped" type="RID" />
<description>
Returns [code]true[/code], if text buffer contents any visible characters.
</description>
</method>
<method name="shaped_text_hit_test_grapheme" qualifiers="const">
<return type="int" />
<param index="0" name="shaped" type="RID" />
Expand Down Expand Up @@ -1671,6 +1678,15 @@
<constant name="JUSTIFICATION_CONSTRAIN_ELLIPSIS" value="16" enum="JustificationFlag" is_bitfield="true">
Apply justification to the trimmed line with ellipsis.
</constant>
<constant name="JUSTIFICATION_SKIP_LAST_LINE" value="32" enum="JustificationFlag" is_bitfield="true">
Do not apply justification to the last line of the paragraph.
</constant>
<constant name="JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS" value="64" enum="JustificationFlag" is_bitfield="true">
Do not apply justification to the last line of the paragraph with visible characters (takes precedence over [constant JUSTIFICATION_SKIP_LAST_LINE]).
</constant>
<constant name="JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE" value="128" enum="JustificationFlag" is_bitfield="true">
Always apply justification to the paragraphs with a single line ([constant JUSTIFICATION_SKIP_LAST_LINE] and [constant JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS] are ignored).
</constant>
<constant name="AUTOWRAP_OFF" value="0" enum="AutowrapMode">
Autowrap is disabled.
</constant>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/TextServerExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@
<return type="float" />
<param index="0" name="shaped" type="RID" />
<param index="1" name="width" type="float" />
<param index="2" name="jst_flags" type="int" enum="TextServer.JustificationFlag" />
<param index="2" name="justification_flags" type="int" enum="TextServer.JustificationFlag" />
<description>
</description>
</method>
Expand Down
36 changes: 34 additions & 2 deletions scene/3d/label_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ void Label3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Label3D::set_autowrap_mode);
ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Label3D::get_autowrap_mode);

ClassDB::bind_method(D_METHOD("set_justification_flags", "justification_flags"), &Label3D::set_justification_flags);
ClassDB::bind_method(D_METHOD("get_justification_flags"), &Label3D::get_justification_flags);

ClassDB::bind_method(D_METHOD("set_width", "width"), &Label3D::set_width);
ClassDB::bind_method(D_METHOD("get_width"), &Label3D::get_width);

Expand Down Expand Up @@ -157,6 +160,7 @@ void Label3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "line_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_line_spacing", "get_line_spacing");
ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "justification_flags", PROPERTY_HINT_FLAGS, "Kashida Justification:1,Word Justification:2,Justify Only After Last Tab:8,Skip Last Line:32,Skip Last Line With Visible Characters:64,Do Not Skip Single Line:128"), "set_justification_flags", "get_justification_flags");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width", PROPERTY_HINT_NONE, "suffix:px"), "set_width", "get_width");

ADD_GROUP("BiDi", "");
Expand Down Expand Up @@ -533,8 +537,24 @@ void Label3D::_shape() {
}

if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
for (int i = 0; i < lines_rid.size() - 1; i++) {
TS->shaped_text_fit_to_width(lines_rid[i], (width > 0) ? width : max_line_w, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
int jst_to_line = lines_rid.size();
if (lines_rid.size() == 1 && jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
jst_to_line = lines_rid.size();
} else {
if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
jst_to_line = lines_rid.size() - 1;
}
if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
for (int i = lines_rid.size() - 1; i >= 0; i--) {
if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
jst_to_line = i;
break;
}
}
}
}
for (int i = 0; i < jst_to_line; i++) {
TS->shaped_text_fit_to_width(lines_rid[i], (width > 0) ? width : max_line_w, jst_flags);
}
}
dirty_lines = false;
Expand Down Expand Up @@ -871,6 +891,18 @@ TextServer::AutowrapMode Label3D::get_autowrap_mode() const {
return autowrap_mode;
}

void Label3D::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
if (jst_flags != p_flags) {
jst_flags = p_flags;
dirty_lines = true;
_queue_update();
}
}

BitField<TextServer::JustificationFlag> Label3D::get_justification_flags() const {
return jst_flags;
}

void Label3D::set_width(float p_width) {
if (width != p_width) {
width = p_width;
Expand Down
4 changes: 4 additions & 0 deletions scene/3d/label_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class Label3D : public GeometryInstance3D {
bool uppercase = false;

TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
BitField<TextServer::JustificationFlag> jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
float width = 500.0;

int font_size = 32;
Expand Down Expand Up @@ -215,6 +216,9 @@ class Label3D : public GeometryInstance3D {
void set_autowrap_mode(TextServer::AutowrapMode p_mode);
TextServer::AutowrapMode get_autowrap_mode() const;

void set_justification_flags(BitField<TextServer::JustificationFlag> p_flags);
BitField<TextServer::JustificationFlag> get_justification_flags() const;

void set_width(float p_width);
float get_width() const;

Expand Down
Loading

0 comments on commit 59c19a7

Please sign in to comment.