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)
171 #define OFFSET(x) offsetof(DrawTextContext, x)
172 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
202 {
"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 {
"start_number",
"start frame number for n/frame_num variable",
OFFSET(start_number),
AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX,
FLAGS},
206 {
"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" },
207 {
"default", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_DEFAULT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
208 {
"no_scale", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_SCALE }, .flags =
FLAGS, .unit =
"ft_load_flags" },
209 {
"no_hinting", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_HINTING }, .flags =
FLAGS, .unit =
"ft_load_flags" },
210 {
"render", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_RENDER }, .flags =
FLAGS, .unit =
"ft_load_flags" },
211 {
"no_bitmap", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_BITMAP }, .flags =
FLAGS, .unit =
"ft_load_flags" },
212 {
"vertical_layout", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
213 {
"force_autohint", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_FORCE_AUTOHINT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
214 {
"crop_bitmap", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_CROP_BITMAP }, .flags =
FLAGS, .unit =
"ft_load_flags" },
215 {
"pedantic", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_PEDANTIC }, .flags =
FLAGS, .unit =
"ft_load_flags" },
216 {
"ignore_global_advance_width", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags =
FLAGS, .unit =
"ft_load_flags" },
217 {
"no_recurse", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_RECURSE }, .flags =
FLAGS, .unit =
"ft_load_flags" },
218 {
"ignore_transform", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags =
FLAGS, .unit =
"ft_load_flags" },
219 {
"monochrome", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_MONOCHROME }, .flags =
FLAGS, .unit =
"ft_load_flags" },
220 {
"linear_design", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_LINEAR_DESIGN }, .flags =
FLAGS, .unit =
"ft_load_flags" },
221 {
"no_autohint", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_AUTOHINT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
227 #undef __FTERRORS_H__
228 #define FT_ERROR_START_LIST {
229 #define FT_ERRORDEF(e, v, s) { (e), (s) },
230 #define FT_ERROR_END_LIST { 0, NULL } };
239 #define FT_ERRMSG(e) ft_errors[e].err_msg
253 const Glyph *
a = key, *bb =
b;
254 int64_t
diff = (int64_t)a->code - (int64_t)bb->code;
255 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
274 !(glyph->glyph =
av_mallocz(
sizeof(*glyph->glyph)))) {
280 if (FT_Get_Glyph(s->
face->glyph, glyph->glyph)) {
285 glyph->bitmap = s->
face->glyph->bitmap;
286 glyph->bitmap_left = s->
face->glyph->bitmap_left;
287 glyph->bitmap_top = s->
face->glyph->bitmap_top;
288 glyph->advance = s->
face->glyph->advance.x >> 6;
291 FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
318 err = FT_New_Face(s->
library, path, index, &s->
face);
326 #if CONFIG_FONTCONFIG
327 static int load_font_fontconfig(
AVFilterContext *ctx,
const char **error)
330 FcConfig *fontconfig;
331 FcPattern *pattern, *fpat;
332 FcResult result = FcResultMatch;
337 fontconfig = FcInitLoadConfigAndFonts();
339 *error =
"impossible to init fontconfig\n";
343 (
uint8_t *)(intptr_t)
"default");
345 *error =
"could not parse fontconfig pattern";
348 if (!FcConfigSubstitute(fontconfig, pattern, FcMatchPattern)) {
349 *error =
"could not substitue fontconfig options";
352 FcDefaultSubstitute(pattern);
353 fpat = FcFontMatch(fontconfig, pattern, &result);
354 if (!fpat || result != FcResultMatch) {
355 *error =
"impossible to find a matching font";
358 if (FcPatternGetString (fpat, FC_FILE, 0, &filename) != FcResultMatch ||
359 FcPatternGetInteger(fpat, FC_INDEX, 0, &index ) != FcResultMatch ||
360 FcPatternGetDouble (fpat, FC_SIZE, 0, &size ) != FcResultMatch) {
361 *error =
"impossible to find font information";
370 FcPatternDestroy(fpat);
371 FcPatternDestroy(pattern);
372 FcConfigDestroy(fontconfig);
381 const char *error =
"unknown error\n";
387 #if CONFIG_FONTCONFIG
388 err = load_font_fontconfig(ctx, &error);
406 "The text file '%s' could not be read or is empty\n",
413 memcpy(s->
text, textbuf, textbuf_size);
414 s->
text[textbuf_size] = 0;
426 if (!s->
fontfile && !CONFIG_FONTCONFIG) {
434 "Both text and text file provided. Please provide only one\n");
457 "Either text, a valid file or a timecode must be provided\n");
461 if ((err = FT_Init_FreeType(&(s->
library)))) {
463 "Could not load FreeType: %s\n",
FT_ERRMSG(err));
472 if ((err = FT_Set_Pixel_Sizes(s->
face, 0, s->
fontsize))) {
484 if ((err =
load_glyph(ctx, &glyph,
' ')) < 0) {
491 (strchr(s->
text,
'%') || strchr(s->
text,
'\\')))
509 FT_Done_Glyph(*glyph->glyph);
531 FT_Done_Face(s->
face);
539 return c ==
'\n' || c ==
'\r' || c ==
'\f' || c ==
'\v';
585 if (!strcmp(cmd,
"reinit")) {
589 if ((ret =
init(ctx)) < 0)
598 char *fct,
unsigned argc,
char **argv,
int tag)
607 char *fct,
unsigned argc,
char **argv,
int tag)
616 char *fct,
unsigned argc,
char **argv,
int tag)
625 char *fct,
unsigned argc,
char **argv,
int tag)
635 #if !HAVE_LOCALTIME_R
643 char *fct,
unsigned argc,
char **argv,
int tag)
645 const char *
fmt = argc ? argv[0] :
"%Y-%m-%d %H:%M:%S";
659 char *fct,
unsigned argc,
char **argv,
int tag)
670 "Expression '%s' for the expr text expansion function is not valid\n",
696 unsigned argc,
char **argv)
724 const char *text = *rtext;
725 char *argv[16] = { NULL };
726 unsigned argc = 0, i;
751 if ((ret =
eval_function(ctx, bp, argv[0], argc - 1, argv + 1)) < 0)
754 *rtext = (
char *)text + 1;
757 for (i = 0; i < argc; i++)
765 char *text = s->
text;
771 if (*text ==
'\\' && text[1]) {
774 }
else if (*text ==
'%') {
797 for (i = 0, p = text; *p; i++) {
802 if (code ==
'\n' || code ==
'\r' || code ==
'\t')
808 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
809 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
817 glyph->bitmap.buffer, glyph->bitmap.pitch,
818 glyph->bitmap.width, glyph->bitmap.rows,
819 glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
832 uint32_t code = 0, prev_code = 0;
833 int x = 0,
y = 0, i = 0,
ret;
834 int max_text_line_w = 0,
len;
836 char *text = s->
text;
838 int y_min = 32000, y_max = -32000;
839 int x_min = 32000, x_max = -32000;
841 Glyph *glyph = NULL, *prev_glyph = NULL;
844 time_t now = time(0);
888 for (i = 0, p = text; *p; i++) {
898 y_min =
FFMIN(glyph->bbox.yMin, y_min);
899 y_max =
FFMAX(glyph->bbox.yMax, y_max);
900 x_min =
FFMIN(glyph->bbox.xMin, x_min);
901 x_max =
FFMAX(glyph->bbox.xMax, x_max);
908 for (i = 0, p = text; *p; i++) {
912 if (prev_code ==
'\r' && code ==
'\n')
918 max_text_line_w =
FFMAX(max_text_line_w, x);
931 FT_Get_Kerning(s->
face, prev_glyph->code, glyph->code,
932 ft_kerning_default, &delta);
937 s->
positions[i].x = x + glyph->bitmap_left;
938 s->
positions[i].y =
y - glyph->bitmap_top + y_max;
940 else x += glyph->advance;
943 max_text_line_w =
FFMAX(x, max_text_line_w);
963 box_w =
FFMIN(width - 1 , max_text_line_w);
970 s->
x, s->
y, box_w, box_h);
1020 .needs_writable = 1,
1035 .description =
NULL_IF_CONFIG_SMALL(
"Draw text on top of video frames using libfreetype library."),
1037 .priv_class = &drawtext_class,
1042 .
inputs = avfilter_vf_drawtext_inputs,
1043 .
outputs = avfilter_vf_drawtext_outputs,