Skip to content

Commit 5f3b284

Browse files
committed
Merge pull request #89510 from bruvzg/lbl_shadow_step
[Label] Move shadow drawing into a separate draw step.
2 parents 856efe4 + af9e812 commit 5f3b284

File tree

2 files changed

+54
-79
lines changed

2 files changed

+54
-79
lines changed

scene/gui/label.cpp

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,19 @@ inline void draw_glyph(const Glyph &p_gl, const RID &p_canvas, const Color &p_fo
316316
}
317317
}
318318

319-
inline void draw_glyph_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_color, const Color &p_font_shadow_color, const Color &p_font_outline_color, const int &p_shadow_outline_size, const int &p_outline_size, const Vector2 &p_ofs, const Vector2 &shadow_ofs) {
319+
inline void draw_glyph_shadow(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_shadow_color, int p_shadow_outline_size, const Vector2 &p_ofs, const Vector2 &shadow_ofs) {
320320
if (p_gl.font_rid != RID()) {
321321
if (p_font_shadow_color.a > 0) {
322322
TS->font_draw_glyph(p_gl.font_rid, p_canvas, p_gl.font_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off) + shadow_ofs, p_gl.index, p_font_shadow_color);
323323
}
324324
if (p_font_shadow_color.a > 0 && p_shadow_outline_size > 0) {
325325
TS->font_draw_glyph_outline(p_gl.font_rid, p_canvas, p_gl.font_size, p_shadow_outline_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off) + shadow_ofs, p_gl.index, p_font_shadow_color);
326326
}
327+
}
328+
}
329+
330+
inline void draw_glyph_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_outline_color, int p_outline_size, const Vector2 &p_ofs) {
331+
if (p_gl.font_rid != RID()) {
327332
if (p_font_outline_color.a != 0.0 && p_outline_size > 0) {
328333
TS->font_draw_glyph_outline(p_gl.font_rid, p_canvas, p_gl.font_size, p_outline_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off), p_gl.index, p_font_outline_color);
329334
}
@@ -537,25 +542,36 @@ void Label::_notification(int p_what) {
537542
const Glyph *ellipsis_glyphs = TS->shaped_text_get_ellipsis_glyphs(lines_rid[i]);
538543
int ellipsis_gl_size = TS->shaped_text_get_ellipsis_glyph_count(lines_rid[i]);
539544

540-
// Draw outline. Note: Do not merge this into the single loop with the main text, to prevent overlaps.
541-
int processed_glyphs_ol = processed_glyphs;
542-
if ((outline_size > 0 && font_outline_color.a != 0) || (font_shadow_color.a != 0)) {
543-
Vector2 offset = ofs;
545+
// Draw shadow, outline and text. Note: Do not merge this into the single loop iteration, to prevent overlaps.
546+
for (int step = DRAW_STEP_SHADOW; step < DRAW_STEP_MAX; step++) {
547+
if (step == DRAW_STEP_SHADOW && (font_shadow_color.a == 0)) {
548+
continue;
549+
}
550+
if (step == DRAW_STEP_OUTLINE && (outline_size <= 0 || font_outline_color.a == 0)) {
551+
continue;
552+
}
553+
554+
int processed_glyphs_step = processed_glyphs;
555+
Vector2 offset_step = ofs;
544556
// Draw RTL ellipsis string when necessary.
545557
if (rtl && ellipsis_pos >= 0) {
546558
for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
547559
for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
548-
bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_ol >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_ol < total_glyphs - visible_glyphs));
549-
//Draw glyph outlines and shadow.
560+
bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
550561
if (!skip) {
551-
draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
562+
if (step == DRAW_STEP_SHADOW) {
563+
draw_glyph_shadow(ellipsis_glyphs[gl_idx], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
564+
} else if (step == DRAW_STEP_OUTLINE) {
565+
draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_outline_color, outline_size, offset_step);
566+
} else if (step == DRAW_STEP_TEXT) {
567+
draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, offset_step);
568+
}
552569
}
553-
processed_glyphs_ol++;
554-
offset.x += ellipsis_glyphs[gl_idx].advance;
570+
processed_glyphs_step++;
571+
offset_step.x += ellipsis_glyphs[gl_idx].advance;
555572
}
556573
}
557574
}
558-
559575
// Draw main text.
560576
for (int j = 0; j < gl_size; j++) {
561577
// Trim when necessary.
@@ -571,85 +587,37 @@ void Label::_notification(int p_what) {
571587
}
572588
}
573589
for (int k = 0; k < glyphs[j].repeat; k++) {
574-
bool skip = (trim_chars && glyphs[j].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_ol >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_ol < total_glyphs - visible_glyphs));
575-
576-
// Draw glyph outlines and shadow.
590+
bool skip = (trim_chars && glyphs[j].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
577591
if (!skip) {
578-
draw_glyph_outline(glyphs[j], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
592+
if (step == DRAW_STEP_SHADOW) {
593+
draw_glyph_shadow(glyphs[j], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
594+
} else if (step == DRAW_STEP_OUTLINE) {
595+
draw_glyph_outline(glyphs[j], ci, font_outline_color, outline_size, offset_step);
596+
} else if (step == DRAW_STEP_TEXT) {
597+
draw_glyph(glyphs[j], ci, font_color, offset_step);
598+
}
579599
}
580-
processed_glyphs_ol++;
581-
offset.x += glyphs[j].advance;
600+
processed_glyphs_step++;
601+
offset_step.x += glyphs[j].advance;
582602
}
583603
}
584604
// Draw LTR ellipsis string when necessary.
585605
if (!rtl && ellipsis_pos >= 0) {
586606
for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
587607
for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
588-
bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_ol >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_ol < total_glyphs - visible_glyphs));
589-
//Draw glyph outlines and shadow.
608+
bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
590609
if (!skip) {
591-
draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
610+
if (step == DRAW_STEP_SHADOW) {
611+
draw_glyph_shadow(ellipsis_glyphs[gl_idx], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
612+
} else if (step == DRAW_STEP_OUTLINE) {
613+
draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_outline_color, outline_size, offset_step);
614+
} else if (step == DRAW_STEP_TEXT) {
615+
draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, offset_step);
616+
}
592617
}
593-
processed_glyphs_ol++;
594-
offset.x += ellipsis_glyphs[gl_idx].advance;
595-
}
596-
}
597-
}
598-
}
599-
600-
// Draw main text. Note: Do not merge this into the single loop with the outline, to prevent overlaps.
601-
602-
// Draw RTL ellipsis string when necessary.
603-
if (rtl && ellipsis_pos >= 0) {
604-
for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
605-
for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
606-
bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs < total_glyphs - visible_glyphs));
607-
//Draw glyph outlines and shadow.
608-
if (!skip) {
609-
draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, ofs);
610-
}
611-
processed_glyphs++;
612-
ofs.x += ellipsis_glyphs[gl_idx].advance;
613-
}
614-
}
615-
}
616-
617-
// Draw main text.
618-
for (int j = 0; j < gl_size; j++) {
619-
// Trim when necessary.
620-
if (trim_pos >= 0) {
621-
if (rtl) {
622-
if (j < trim_pos) {
623-
continue;
624-
}
625-
} else {
626-
if (j >= trim_pos) {
627-
break;
628-
}
629-
}
630-
}
631-
for (int k = 0; k < glyphs[j].repeat; k++) {
632-
bool skip = (trim_chars && glyphs[j].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs < total_glyphs - visible_glyphs));
633-
634-
// Draw glyph outlines and shadow.
635-
if (!skip) {
636-
draw_glyph(glyphs[j], ci, font_color, ofs);
637-
}
638-
processed_glyphs++;
639-
ofs.x += glyphs[j].advance;
640-
}
641-
}
642-
// Draw LTR ellipsis string when necessary.
643-
if (!rtl && ellipsis_pos >= 0) {
644-
for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
645-
for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
646-
bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs < total_glyphs - visible_glyphs));
647-
//Draw glyph outlines and shadow.
648-
if (!skip) {
649-
draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, ofs);
618+
processed_glyphs_step++;
619+
offset_step.x += ellipsis_glyphs[gl_idx].advance;
650620
}
651-
processed_glyphs++;
652-
ofs.x += ellipsis_glyphs[gl_idx].advance;
653621
}
654622
}
655623
}

scene/gui/label.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class Label : public Control {
3838
GDCLASS(Label, Control);
3939

4040
private:
41+
enum LabelDrawStep {
42+
DRAW_STEP_SHADOW,
43+
DRAW_STEP_OUTLINE,
44+
DRAW_STEP_TEXT,
45+
DRAW_STEP_MAX,
46+
};
47+
4148
HorizontalAlignment horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT;
4249
VerticalAlignment vertical_alignment = VERTICAL_ALIGNMENT_TOP;
4350
String text;

0 commit comments

Comments
 (0)