51 #include FT_FREETYPE_H
54 #include <fontconfig/fontconfig.h>
63 "max_glyph_a",
"ascent",
64 "max_glyph_d",
"descent",
82 static double drand(
void *opaque,
double min,
double max)
157 #if FF_API_DRAWTEXT_OLD_TIMELINE
172 #define OFFSET(x) offsetof(DrawTextContext, x)
173 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
190 #if FF_API_DRAWTEXT_OLD_TIMELINE
205 {
"fix_bounds",
"if true, check and fix text coords to avoid clipping",
OFFSET(fix_bounds),
AV_OPT_TYPE_INT, {.i64=1}, 0, 1,
FLAGS},
206 {
"start_number",
"start frame number for n/frame_num variable",
OFFSET(start_number),
AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX,
FLAGS},
209 {
"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" },
210 {
"default", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_DEFAULT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
211 {
"no_scale", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_SCALE }, .flags =
FLAGS, .unit =
"ft_load_flags" },
212 {
"no_hinting", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_HINTING }, .flags =
FLAGS, .unit =
"ft_load_flags" },
213 {
"render", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_RENDER }, .flags =
FLAGS, .unit =
"ft_load_flags" },
214 {
"no_bitmap", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_BITMAP }, .flags =
FLAGS, .unit =
"ft_load_flags" },
215 {
"vertical_layout", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
216 {
"force_autohint", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_FORCE_AUTOHINT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
217 {
"crop_bitmap", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_CROP_BITMAP }, .flags =
FLAGS, .unit =
"ft_load_flags" },
218 {
"pedantic", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_PEDANTIC }, .flags =
FLAGS, .unit =
"ft_load_flags" },
219 {
"ignore_global_advance_width", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags =
FLAGS, .unit =
"ft_load_flags" },
220 {
"no_recurse", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_RECURSE }, .flags =
FLAGS, .unit =
"ft_load_flags" },
221 {
"ignore_transform", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags =
FLAGS, .unit =
"ft_load_flags" },
222 {
"monochrome", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_MONOCHROME }, .flags =
FLAGS, .unit =
"ft_load_flags" },
223 {
"linear_design", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_LINEAR_DESIGN }, .flags =
FLAGS, .unit =
"ft_load_flags" },
224 {
"no_autohint", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_AUTOHINT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
230 #undef __FTERRORS_H__
231 #define FT_ERROR_START_LIST {
232 #define FT_ERRORDEF(e, v, s) { (e), (s) },
233 #define FT_ERROR_END_LIST { 0, NULL } };
242 #define FT_ERRMSG(e) ft_errors[e].err_msg
256 const Glyph *
a = key, *bb =
b;
257 int64_t diff = (int64_t)a->code - (int64_t)bb->code;
258 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
277 !(glyph->glyph =
av_mallocz(
sizeof(*glyph->glyph)))) {
283 if (FT_Get_Glyph(s->
face->glyph, glyph->glyph)) {
288 glyph->bitmap = s->
face->glyph->bitmap;
289 glyph->bitmap_left = s->
face->glyph->bitmap_left;
290 glyph->bitmap_top = s->
face->glyph->bitmap_top;
291 glyph->advance = s->
face->glyph->advance.x >> 6;
294 FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
321 err = FT_New_Face(s->
library, path, index, &s->
face);
329 #if CONFIG_FONTCONFIG
330 static int load_font_fontconfig(
AVFilterContext *ctx,
const char **error)
333 FcConfig *fontconfig;
334 FcPattern *pattern, *fpat;
335 FcResult result = FcResultMatch;
340 fontconfig = FcInitLoadConfigAndFonts();
342 *error =
"impossible to init fontconfig\n";
346 (
uint8_t *)(intptr_t)
"default");
348 *error =
"could not parse fontconfig pattern";
351 if (!FcConfigSubstitute(fontconfig, pattern, FcMatchPattern)) {
352 *error =
"could not substitue fontconfig options";
355 FcDefaultSubstitute(pattern);
356 fpat = FcFontMatch(fontconfig, pattern, &result);
357 if (!fpat || result != FcResultMatch) {
358 *error =
"impossible to find a matching font";
361 if (FcPatternGetString (fpat, FC_FILE, 0, &filename) != FcResultMatch ||
362 FcPatternGetInteger(fpat, FC_INDEX, 0, &index ) != FcResultMatch ||
363 FcPatternGetDouble (fpat, FC_SIZE, 0, &size ) != FcResultMatch) {
364 *error =
"impossible to find font information";
373 FcPatternDestroy(fpat);
374 FcPatternDestroy(pattern);
375 FcConfigDestroy(fontconfig);
384 const char *error =
"unknown error\n";
390 #if CONFIG_FONTCONFIG
391 err = load_font_fontconfig(ctx, &error);
409 "The text file '%s' could not be read or is empty\n",
416 memcpy(s->
text, textbuf, textbuf_size);
417 s->
text[textbuf_size] = 0;
429 #if FF_API_DRAWTEXT_OLD_TIMELINE
432 "you are encouraged to use the generic timeline support through the 'enable' option\n");
435 if (!s->
fontfile && !CONFIG_FONTCONFIG) {
443 "Both text and text file provided. Please provide only one\n");
466 "Either text, a valid file or a timecode must be provided\n");
470 if ((err = FT_Init_FreeType(&(s->
library)))) {
472 "Could not load FreeType: %s\n",
FT_ERRMSG(err));
481 if ((err = FT_Set_Pixel_Sizes(s->
face, 0, s->
fontsize))) {
493 if ((err =
load_glyph(ctx, &glyph,
' ')) < 0) {
500 (strchr(s->
text,
'%') || strchr(s->
text,
'\\')))
518 FT_Done_Glyph(*glyph->glyph);
530 #if FF_API_DRAWTEXT_OLD_TIMELINE
542 FT_Done_Face(s->
face);
550 return c ==
'\n' || c ==
'\r' || c ==
'\f' || c ==
'\v';
578 #if FF_API_DRAWTEXT_OLD_TIMELINE
591 #if FF_API_DRAWTEXT_OLD_TIMELINE
605 if (!strcmp(cmd,
"reinit")) {
609 if ((ret =
init(ctx)) < 0)
618 char *fct,
unsigned argc,
char **argv,
int tag)
627 char *fct,
unsigned argc,
char **argv,
int tag)
636 char *fct,
unsigned argc,
char **argv,
int tag)
645 char *fct,
unsigned argc,
char **argv,
int tag)
655 #if !HAVE_LOCALTIME_R
663 char *fct,
unsigned argc,
char **argv,
int tag)
665 const char *
fmt = argc ? argv[0] :
"%Y-%m-%d %H:%M:%S";
679 char *fct,
unsigned argc,
char **argv,
int tag)
690 "Expression '%s' for the expr text expansion function is not valid\n",
716 unsigned argc,
char **argv)
744 const char *text = *rtext;
745 char *argv[16] = { NULL };
746 unsigned argc = 0, i;
771 if ((ret =
eval_function(ctx, bp, argv[0], argc - 1, argv + 1)) < 0)
774 *rtext = (
char *)text + 1;
777 for (i = 0; i < argc; i++)
785 char *text = s->
text;
791 if (*text ==
'\\' && text[1]) {
794 }
else if (*text ==
'%') {
817 for (i = 0, p = text; *p; i++) {
822 if (code ==
'\n' || code ==
'\r' || code ==
'\t')
828 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
829 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
837 glyph->bitmap.buffer, glyph->bitmap.pitch,
838 glyph->bitmap.width, glyph->bitmap.rows,
839 glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
852 uint32_t code = 0, prev_code = 0;
853 int x = 0,
y = 0, i = 0,
ret;
854 int max_text_line_w = 0,
len;
858 int y_min = 32000, y_max = -32000;
859 int x_min = 32000, x_max = -32000;
861 Glyph *glyph = NULL, *prev_glyph = NULL;
864 time_t now = time(0);
908 for (i = 0, p = text; *p; i++) {
918 y_min =
FFMIN(glyph->bbox.yMin, y_min);
919 y_max =
FFMAX(glyph->bbox.yMax, y_max);
920 x_min =
FFMIN(glyph->bbox.xMin, x_min);
921 x_max =
FFMAX(glyph->bbox.xMax, x_max);
928 for (i = 0, p = text; *p; i++) {
932 if (prev_code ==
'\r' && code ==
'\n')
938 max_text_line_w =
FFMAX(max_text_line_w, x);
951 FT_Get_Kerning(s->
face, prev_glyph->code, glyph->code,
952 ft_kerning_default, &delta);
957 s->
positions[i].x = x + glyph->bitmap_left;
958 s->
positions[i].y =
y - glyph->bitmap_top + y_max;
960 else x += glyph->advance;
963 max_text_line_w =
FFMAX(x, max_text_line_w);
978 #if FF_API_DRAWTEXT_OLD_TIMELINE
989 box_w =
FFMIN(width - 1 , max_text_line_w);
996 s->
x, s->
y, box_w, box_h);
1045 .needs_writable = 1,
1060 .description =
NULL_IF_CONFIG_SMALL(
"Draw text on top of video frames using libfreetype library."),
1062 .priv_class = &drawtext_class,
1066 .
inputs = avfilter_vf_drawtext_inputs,
1067 .
outputs = avfilter_vf_drawtext_outputs,