51 #include <freetype/config/ftheader.h>
52 #include FT_FREETYPE_H
55 #include <fontconfig/fontconfig.h>
64 "max_glyph_a",
"ascent",
65 "max_glyph_d",
"descent",
83 static double drand(
void *opaque,
double min,
double max)
169 #define OFFSET(x) offsetof(DrawTextContext, x)
170 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
200 {
"fix_bounds",
"if true, check and fix text coords to avoid clipping",
OFFSET(fix_bounds),
AV_OPT_TYPE_INT, {.i64=1}, 0, 1,
FLAGS},
203 {
"ft_load_flags",
"set font loading flags for libfreetype",
OFFSET(ft_load_flags),
AV_OPT_TYPE_FLAGS, { .i64 = FT_LOAD_DEFAULT | FT_LOAD_RENDER}, 0, INT_MAX,
FLAGS,
"ft_load_flags" },
209 {
"vertical_layout",
NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
213 {
"ignore_global_advance_width",
NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags =
FLAGS, .unit =
"ft_load_flags" },
215 {
"ignore_transform",
NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags =
FLAGS, .unit =
"ft_load_flags" },
224 #undef __FTERRORS_H__
225 #define FT_ERROR_START_LIST {
226 #define FT_ERRORDEF(e, v, s) { (e), (s) },
227 #define FT_ERROR_END_LIST { 0, NULL } };
236 #define FT_ERRMSG(e) ft_errors[e].err_msg
250 const Glyph *
a = key, *bb =
b;
251 int64_t
diff = (int64_t)a->code - (int64_t)bb->code;
252 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
271 !(glyph->glyph =
av_mallocz(
sizeof(*glyph->glyph)))) {
277 if (FT_Get_Glyph(s->
face->glyph, glyph->glyph)) {
282 glyph->bitmap = s->
face->glyph->bitmap;
283 glyph->bitmap_left = s->
face->glyph->bitmap_left;
284 glyph->bitmap_top = s->
face->glyph->bitmap_top;
285 glyph->advance = s->
face->glyph->advance.x >> 6;
288 FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
315 err = FT_New_Face(s->
library, path, index, &s->
face);
323 #if CONFIG_FONTCONFIG
324 static int load_font_fontconfig(
AVFilterContext *ctx,
const char **error)
327 FcConfig *fontconfig;
328 FcPattern *pattern, *fpat;
329 FcResult result = FcResultMatch;
334 fontconfig = FcInitLoadConfigAndFonts();
336 *error =
"impossible to init fontconfig\n";
340 (
uint8_t *)(intptr_t)
"default");
342 *error =
"could not parse fontconfig pattern";
345 if (!FcConfigSubstitute(fontconfig, pattern, FcMatchPattern)) {
346 *error =
"could not substitue fontconfig options";
349 FcDefaultSubstitute(pattern);
350 fpat = FcFontMatch(fontconfig, pattern, &result);
351 if (!fpat || result != FcResultMatch) {
352 *error =
"impossible to find a matching font";
355 if (FcPatternGetString (fpat, FC_FILE, 0, &filename) != FcResultMatch ||
356 FcPatternGetInteger(fpat, FC_INDEX, 0, &index ) != FcResultMatch ||
357 FcPatternGetDouble (fpat, FC_SIZE, 0, &size ) != FcResultMatch) {
358 *error =
"impossible to find font information";
367 FcPatternDestroy(fpat);
368 FcPatternDestroy(pattern);
369 FcConfigDestroy(fontconfig);
378 const char *error =
"unknown error\n";
384 #if CONFIG_FONTCONFIG
385 err = load_font_fontconfig(ctx, &error);
403 "The text file '%s' could not be read or is empty\n",
410 memcpy(s->
text, textbuf, textbuf_size);
411 s->
text[textbuf_size] = 0;
423 if (!s->
fontfile && !CONFIG_FONTCONFIG) {
431 "Both text and text file provided. Please provide only one\n");
454 "Either text, a valid file or a timecode must be provided\n");
458 if ((err = FT_Init_FreeType(&(s->
library)))) {
460 "Could not load FreeType: %s\n",
FT_ERRMSG(err));
469 if ((err = FT_Set_Pixel_Sizes(s->
face, 0, s->
fontsize))) {
481 if ((err =
load_glyph(ctx, &glyph,
' ')) < 0) {
488 (strchr(s->
text,
'%') || strchr(s->
text,
'\\')))
506 FT_Done_Glyph(*glyph->glyph);
528 FT_Done_Face(s->
face);
536 return c ==
'\n' || c ==
'\r' || c ==
'\f' || c ==
'\v';
582 if (!strcmp(cmd,
"reinit")) {
586 if ((ret =
init(ctx)) < 0)
595 char *fct,
unsigned argc,
char **argv,
int tag)
604 char *fct,
unsigned argc,
char **argv,
int tag)
613 char *fct,
unsigned argc,
char **argv,
int tag)
621 #if !HAVE_LOCALTIME_R
629 char *fct,
unsigned argc,
char **argv,
int tag)
631 const char *
fmt = argc ? argv[0] :
"%Y-%m-%d %H:%M:%S";
645 char *fct,
unsigned argc,
char **argv,
int tag)
656 "Expression '%s' for the expr text expansion function is not valid\n",
681 unsigned argc,
char **argv)
709 const char *text = *rtext;
710 char *argv[16] = {
NULL };
711 unsigned argc = 0, i;
736 if ((ret =
eval_function(ctx, bp, argv[0], argc - 1, argv + 1)) < 0)
739 *rtext = (
char *)text + 1;
742 for (i = 0; i < argc; i++)
750 char *text = s->
text;
756 if (*text ==
'\\' && text[1]) {
759 }
else if (*text ==
'%') {
782 for (i = 0, p = text; *p; i++) {
787 if (code ==
'\n' || code ==
'\r' || code ==
'\t')
793 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
794 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
802 glyph->bitmap.buffer, glyph->bitmap.pitch,
803 glyph->bitmap.width, glyph->bitmap.rows,
804 glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
817 uint32_t code = 0, prev_code = 0;
818 int x = 0,
y = 0, i = 0,
ret;
819 int max_text_line_w = 0,
len;
821 char *text = s->
text;
823 int y_min = 32000, y_max = -32000;
824 int x_min = 32000, x_max = -32000;
826 Glyph *glyph =
NULL, *prev_glyph =
NULL;
829 time_t now = time(0);
873 for (i = 0, p = text; *p; i++) {
883 y_min =
FFMIN(glyph->bbox.yMin, y_min);
884 y_max =
FFMAX(glyph->bbox.yMax, y_max);
885 x_min =
FFMIN(glyph->bbox.xMin, x_min);
886 x_max =
FFMAX(glyph->bbox.xMax, x_max);
893 for (i = 0, p = text; *p; i++) {
897 if (prev_code ==
'\r' && code ==
'\n')
903 max_text_line_w =
FFMAX(max_text_line_w, x);
916 FT_Get_Kerning(s->
face, prev_glyph->code, glyph->code,
917 ft_kerning_default, &delta);
922 s->
positions[i].x = x + glyph->bitmap_left;
923 s->
positions[i].y =
y - glyph->bitmap_top + y_max;
925 else x += glyph->advance;
928 max_text_line_w =
FFMAX(x, max_text_line_w);
948 box_w =
FFMIN(width - 1 , max_text_line_w);
955 s->
x, s->
y, box_w, box_h);
1004 .needs_writable = 1,
1019 .description =
NULL_IF_CONFIG_SMALL(
"Draw text on top of video frames using libfreetype library."),
1021 .priv_class = &drawtext_class,
1026 .
inputs = avfilter_vf_drawtext_inputs,
1027 .
outputs = avfilter_vf_drawtext_outputs,