History

avfilter/vf_drawtext: use flags type of option for text_align

Paul B Mahol committed 3 years ago

Commit ea190b81 parent 01420312 files · +30 −37Browse files

Unified · Split

2 files
  1. doc/filters.texi +1 −1
  2. libavfilter/vf_drawtext.c +29 −36
doc/filters.texi+1−1
@@ -12386,7 +12386,7 @@ Set the line spacing in pixels. The default value of @var{line_spacing} is 0. · 12385 unmodified lines
1238612386
1238712387 @item text_align
1238812388 Set the vertical and horizontal alignment of the text with respect to the box boundaries.
12389−The value must contain exactly two letters, one for the vertical alignment (T=top,
12389+The value is combination of flags, one for the vertical alignment (T=top,
1239012390 M=middle, B=bottom) and one for the horizontal alignment (L=left, C=center, R=right).
1239112391 Please note that tab characters are only supported with the left horizontal alignment.
1239212392
libavfilter/vf_drawtext.c+29−36
@@ -177,6 +177,13 @@ enum y_alignment { · 176 unmodified lines
177177 YA_FONT,
178178 };
179179
180+enum text_alignment {
181+ TA_LEFT = (1 << 0),
182+ TA_RIGHT = (1 << 1),
183+ TA_TOP = (1 << 2),
184+ TA_BOTTOM = (1 << 3),
185+};
186+
180187 typedef struct HarfbuzzData {
181188 hb_buffer_t* buf;
182189 hb_font_t* font;
@@ -315,7 +322,7 @@ typedef struct DrawTextContext { · 132 unmodified lines
315322
316323 int boxw; ///< the value of the boxw parameter
317324 int boxh; ///< the value of the boxh parameter
318− uint8_t *text_align; ///< the horizontal and vertical text alignment
325+ int text_align; ///< the horizontal and vertical text alignment
319326 int y_align; ///< the value of the y_align parameter
320327
321328 TextLine *lines; ///< computed information about text lines
@@ -343,7 +350,19 @@ static const AVOption drawtext_options[]= { · 21 unmodified lines
343350 {"boxborderw", "set box borders width", OFFSET(boxborderw), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, TFLAGS},
344351 {"line_spacing", "set line spacing in pixels", OFFSET(line_spacing), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, TFLAGS},
345352 {"fontsize", "set font size", OFFSET(fontsize_expr), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, TFLAGS},
346− {"text_align", "set text alignment", OFFSET(text_align), AV_OPT_TYPE_STRING, {.str="TL"}, 0, 0, TFLAGS},
353+ {"text_align", "set text alignment", OFFSET(text_align), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, (TA_LEFT|TA_RIGHT|TA_TOP|TA_BOTTOM), TFLAGS, "text_align"},
354+ { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_LEFT }, .flags = TFLAGS, .unit = "text_align" },
355+ { "L", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_LEFT }, .flags = TFLAGS, .unit = "text_align" },
356+ { "right", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_RIGHT }, .flags = TFLAGS, .unit = "text_align" },
357+ { "R", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_RIGHT }, .flags = TFLAGS, .unit = "text_align" },
358+ { "center", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = (TA_LEFT|TA_RIGHT) }, .flags = TFLAGS, .unit = "text_align" },
359+ { "C", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = (TA_LEFT|TA_RIGHT) }, .flags = TFLAGS, .unit = "text_align" },
360+ { "top", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_TOP }, .flags = TFLAGS, .unit = "text_align" },
361+ { "T", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_TOP }, .flags = TFLAGS, .unit = "text_align" },
362+ { "bottom", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_BOTTOM }, .flags = TFLAGS, .unit = "text_align" },
363+ { "B", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_BOTTOM }, .flags = TFLAGS, .unit = "text_align" },
364+ { "middle", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = (TA_TOP|TA_BOTTOM) }, .flags = TFLAGS, .unit = "text_align" },
365+ { "M", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = (TA_TOP|TA_BOTTOM) }, .flags = TFLAGS, .unit = "text_align" },
347366 {"x", "set x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, TFLAGS},
348367 {"y", "set y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, TFLAGS},
349368 {"boxw", "set box width", OFFSET(boxw), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, TFLAGS},
@@ -866,19 +885,6 @@ static int string_to_array(const char *source, int *result, int result_size) · 516 unmodified lines
866885 return counter;
867886 }
868887
869−static int validate_text_align(char* text_align)
870−{
871− int err = 0;
872− if (strlen(text_align) != 2
873− || strchr("LCRTMB", text_align[0]) == NULL || strchr("LCRTMB", text_align[1]) == NULL
874− || (strchr("TMB", text_align[0]) != NULL && strchr("LCR", text_align[1]) == NULL)
875− || (strchr("LCR", text_align[0]) != NULL && strchr("TMB", text_align[1]) == NULL)) {
876− err = AVERROR(EINVAL);
877− }
878−
879− return err;
880−}
881−
882888 static av_cold int init(AVFilterContext *ctx)
883889 {
884890 int err;
@@ -944,14 +950,6 @@ static av_cold int init(AVFilterContext *ctx) · 59 unmodified lines
944950 return AVERROR(EINVAL);
945951 }
946952
947− if ((err = validate_text_align(s->text_align))) {
948− av_log(ctx, AV_LOG_ERROR,
949− "The value provided for parameter 'text_align' is not valid,\n");
950− av_log(ctx, AV_LOG_ERROR,
951− "please specify a two characters string containing only one letter for horizontal alignment ('LCR') and one for vertical alignment ('TMB')\n");
952− return err;
953− }
954−
955953 #if CONFIG_LIBFRIBIDI
956954 if (s->text_shaping)
957955 if ((err = shape_text(ctx)) < 0)
@@ -1148,11 +1146,6 @@ static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char · 190 unmodified lines
11481146 FT_STROKER_LINEJOIN_ROUND, 0);
11491147 // Dispose the old border glyphs
11501148 av_tree_enumerate(old->glyphs, NULL, NULL, glyph_enu_border_free);
1151− } else if (strcmp(cmd, "text_align") == 0) {
1152− if (validate_text_align(old->text_align) != 0) {
1153− av_log(ctx, AV_LOG_ERROR,
1154− "Invalid command value '%s' for 'text_align'\n", old->text_align);
1155− }
11561149 } else if (strcmp(cmd, "fontsize") == 0) {
11571150 av_expr_free(old->fontsize_pexpr);
11581151 old->fontsize_pexpr = NULL;
@@ -1561,22 +1554,22 @@ static int draw_glyphs(DrawTextContext *s, AVFrame *frame, · 402 unmodified lines
15611554 Glyph dummy = { 0 }, *glyph;
15621555 FT_Bitmap bitmap;
15631556 FT_BitmapGlyph b_glyph;
1564− uint8_t j_center = 0, j_right = 0, j_middle = 0, j_bottom = 0;
1557+ uint8_t j_left = 0, j_right = 0, j_top = 0, j_bottom = 0;
15651558 int line_w, offset_y = 0;
15661559 int clip_x = 0, clip_y = 0;
15671560
1568− j_center = strstr(s->text_align, "C") > 0;
1569− j_right = strstr(s->text_align, "R") > 0;
1570− j_middle = strstr(s->text_align, "M") > 0;
1571− j_bottom = strstr(s->text_align, "B") > 0;
1561+ j_left = !!(s->text_align & TA_LEFT);
1562+ j_right = !!(s->text_align & TA_RIGHT);
1563+ j_top = !!(s->text_align & TA_TOP);
1564+ j_bottom = !!(s->text_align & TA_BOTTOM);
15721565
1573− if (j_middle) {
1566+ if (j_top && j_bottom) {
15741567 offset_y = (s->box_height - metrics->height) / 2;
15751568 } else if (j_bottom) {
15761569 offset_y = s->box_height - metrics->height;
15771570 }
15781571
1579− if ((j_right || j_center) && !s->tab_warning_printed && s->tab_count > 0) {
1572+ if ((!j_left || j_right) && !s->tab_warning_printed && s->tab_count > 0) {
15801573 s->tab_warning_printed = 1;
15811574 av_log(s, AV_LOG_WARNING, "Tab characters are only supported with left horizontal alignment\n");
15821575 }
@@ -1604,7 +1597,7 @@ static int draw_glyphs(DrawTextContext *s, AVFrame *frame, · 21 unmodified lines
16041597 w1 = bitmap.width;
16051598 h1 = bitmap.rows;
16061599
1607− if (j_center) {
1600+ if (j_left && j_right) {
16081601 x1 += (s->box_width - line_w) / 2;
16091602 } else if (j_right) {
16101603 x1 += s->box_width - line_w;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t