From: Mark Ren <mark.ren77@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Mark Ren <mark.ren77@gmail.com>
Subject: [FFmpeg-devel] [PATCH] libavfilter/vf_drawtext: add letter_spacing as an evaluated parameter
Date: Mon, 19 Jun 2023 12:33:47 -0400
Message-ID: <20230619163347.1317-1-mark.ren77@gmail.com> (raw)
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.
Signed-off-by: Mark Ren <mark.ren77@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@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next reply other threads:[~2023-06-19 16:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-19 16:33 Mark Ren [this message]
2023-06-19 16:39 ` Paul B Mahol
2023-06-19 17:56 ` Mark Ren
2023-06-20 14:27 ` Francesco Carusi
2023-06-20 15:57 ` Paul B Mahol
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230619163347.1317-1-mark.ren77@gmail.com \
--to=mark.ren77@gmail.com \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git