[FFmpeg-devel] [PATCH] libavfilter/vf_drawtext: add letter_spacing as an evaluated parameter

Mark Ren mark.ren77 at gmail.com
Mon Jun 19 20:56:50 EEST 2023


Ah alright I see. When might that push come through and is there somewhere
I can look at the kind of changes in the meantime?

On Mon, Jun 19, 2023 at 12:39 PM Paul B Mahol <onemda at gmail.com> wrote:

>
>
> On Mon, Jun 19, 2023 at 6:34 PM Mark Ren <mark.ren77 at gmail.com> wrote:
>
>> When enabled it will add pixels (or subtract if given a negative value)
>> between each letters,
>> set use_kerning to false,
>> and add the pixels to text_w.
>>
>
> Conflicts with big drawtext filter set that will be pushed soon.
>
>
>>
>> Signed-off-by: Mark Ren <mark.ren77 at gmail.com>
>> ---
>>  libavfilter/vf_drawtext.c | 24 ++++++++++++++++++++----
>>  1 file changed, 20 insertions(+), 4 deletions(-)
>>
>> diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
>> index 71ab851462..ec8313820d 100644
>> --- a/libavfilter/vf_drawtext.c
>> +++ b/libavfilter/vf_drawtext.c
>> @@ -183,6 +183,7 @@ typedef struct DrawTextContext {
>>      unsigned int fontsize;          ///< font size to use
>>      unsigned int default_fontsize;  ///< default font size to use
>>
>> +    int letter_spacing;             ///< letter spacing in pixels
>>      int line_spacing;               ///< lines spacing in pixels
>>      short int draw_box;             ///< draw box around text - true or
>> false
>>      int boxborderw;                 ///< box border width
>> @@ -208,6 +209,8 @@ typedef struct DrawTextContext {
>>      char   *a_expr;
>>      AVExpr *a_pexpr;
>>      int alpha;
>> +    char* letter_spacing_expr;      ///< expression for letter spacing
>> +    AVExpr* letter_spacing_pexpr;   ///< parsed expression for letter
>> spacing
>>      AVLFG  prng;                    ///< random
>>      char       *tc_opt_string;      ///< specified timecode option string
>>      AVRational  tc_rate;            ///< frame rate for timecode
>> @@ -237,6 +240,7 @@ static const AVOption drawtext_options[]= {
>>      {"shadowcolor", "set shadow color",     OFFSET(shadowcolor.rgba),
>>  AV_OPT_TYPE_COLOR,  {.str="black"}, 0, 0, FLAGS},
>>      {"box",         "set box",              OFFSET(draw_box),
>>  AV_OPT_TYPE_BOOL,   {.i64=0},     0,        1       , FLAGS},
>>      {"boxborderw",  "set box border width", OFFSET(boxborderw),
>>  AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN,  INT_MAX , FLAGS},
>> +    {"letter_spacing", "set letter spacing in pixels",
>> OFFSET(letter_spacing_expr), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS},
>>      {"line_spacing",  "set line spacing in pixels",
>> OFFSET(line_spacing),   AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN,
>> INT_MAX,FLAGS},
>>      {"fontsize",    "set font size",        OFFSET(fontsize_expr),
>> AV_OPT_TYPE_STRING, {.str=NULL},  0, 0 , FLAGS},
>>      {"x",           "set x expression",     OFFSET(x_expr),
>>  AV_OPT_TYPE_STRING, {.str="0"},   0, 0, FLAGS},
>> @@ -812,7 +816,7 @@ static av_cold int init(AVFilterContext *ctx)
>>                         FT_STROKER_LINEJOIN_ROUND, 0);
>>      }
>>
>> -    s->use_kerning = FT_HAS_KERNING(s->face);
>> +    s->use_kerning = FT_HAS_KERNING(s->face) && !s->letter_spacing;
>>
>>      /* load the fallback glyph with code 0 */
>>      load_glyph(ctx, NULL, 0);
>> @@ -857,8 +861,9 @@ static av_cold void uninit(AVFilterContext *ctx)
>>      av_expr_free(s->y_pexpr);
>>      av_expr_free(s->a_pexpr);
>>      av_expr_free(s->fontsize_pexpr);
>> +    av_expr_free(s->letter_spacing_pexpr);
>>
>> -    s->x_pexpr = s->y_pexpr = s->a_pexpr = s->fontsize_pexpr = NULL;
>> +    s->x_pexpr = s->y_pexpr = s->a_pexpr = s->fontsize_pexpr =
>> s->letter_spacing_pexpr = NULL;
>>
>>      av_freep(&s->positions);
>>      s->nb_positions = 0;
>> @@ -903,13 +908,16 @@ static int config_input(AVFilterLink *inlink)
>>      av_expr_free(s->x_pexpr);
>>      av_expr_free(s->y_pexpr);
>>      av_expr_free(s->a_pexpr);
>> -    s->x_pexpr = s->y_pexpr = s->a_pexpr = NULL;
>> +    av_expr_free(s->letter_spacing_pexpr);
>> +    s->x_pexpr = s->y_pexpr = s->a_pexpr = s->letter_spacing_pexpr =
>> NULL;
>>
>>      if ((ret = av_expr_parse(&s->x_pexpr, expr = s->x_expr, var_names,
>>                               NULL, NULL, fun2_names, fun2, 0, ctx)) < 0
>> ||
>>          (ret = av_expr_parse(&s->y_pexpr, expr = s->y_expr, var_names,
>>                               NULL, NULL, fun2_names, fun2, 0, ctx)) < 0
>> ||
>>          (ret = av_expr_parse(&s->a_pexpr, expr = s->a_expr, var_names,
>> +                             NULL, NULL, fun2_names, fun2, 0, ctx)) < 0
>> ||
>> +        (ret = av_expr_parse(&s->letter_spacing_pexpr, expr =
>> s->letter_spacing_expr, var_names,
>>                               NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
>> {
>>          av_log(ctx, AV_LOG_ERROR, "Failed to parse expression: %s \n",
>> expr);
>>          return AVERROR(EINVAL);
>> @@ -1525,6 +1533,9 @@ continue_on_invalid2:
>>          dummy.fontsize = s->fontsize;
>>          glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
>>
>> +        /* letter spacing */
>> +        x += s->letter_spacing;
>> +
>>          /* kerning */
>>          if (s->use_kerning && prev_glyph && glyph->code) {
>>              FT_Get_Kerning(s->face, prev_glyph->code, glyph->code,
>> @@ -1539,7 +1550,12 @@ continue_on_invalid2:
>>          else              x += glyph->advance;
>>      }
>>
>> -    max_text_line_w = FFMAX(x, max_text_line_w);
>> +    s->letter_spacing = av_expr_eval(s->letter_spacing_pexpr,
>> s->var_values, &s->prng);
>> +    if (s->letter_spacing < 0) {
>> +        max_text_line_w = x+ s->letter_spacing;
>> +    } else {
>> +        max_text_line_w = FFMAX(x, max_text_line_w) + s->letter_spacing;
>> +    }
>>
>>      s->var_values[VAR_TW] = s->var_values[VAR_TEXT_W] = max_text_line_w;
>>      s->var_values[VAR_TH] = s->var_values[VAR_TEXT_H] = y +
>> s->max_glyph_h;
>> --
>> 2.40.1
>>
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel at ffmpeg.org
>> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>
>> To unsubscribe, visit link above, or email
>> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".
>>
>


More information about the ffmpeg-devel mailing list