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",
82 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},
205 {
"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"},
206 {
"default",
"set default", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_DEFAULT}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
207 {
"no_scale",
"set no_scale", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_SCALE}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
208 {
"no_hinting",
"set no_hinting", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_HINTING}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
209 {
"render",
"set render", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_RENDER}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
210 {
"no_bitmap",
"set no_bitmap", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_BITMAP}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
211 {
"vertical_layout",
"set vertical_layout", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_VERTICAL_LAYOUT}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
212 {
"force_autohint",
"set force_autohint", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_FORCE_AUTOHINT}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
213 {
"crop_bitmap",
"set crop_bitmap", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_CROP_BITMAP}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
214 {
"pedantic",
"set pedantic", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_PEDANTIC}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
215 {
"ignore_global_advance_width",
"set ignore_global_advance_width", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
216 {
"no_recurse",
"set no_recurse", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_RECURSE}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
217 {
"ignore_transform",
"set ignore_transform", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_IGNORE_TRANSFORM}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
218 {
"monochrome",
"set monochrome", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_MONOCHROME}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
219 {
"linear_design",
"set linear_design", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_LINEAR_DESIGN}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
220 {
"no_autohint",
"set no_autohint", 0,
AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_AUTOHINT}, INT_MIN, INT_MAX,
FLAGS,
"ft_load_flags"},
226 #undef __FTERRORS_H__
227 #define FT_ERROR_START_LIST {
228 #define FT_ERRORDEF(e, v, s) { (e), (s) },
229 #define FT_ERROR_END_LIST { 0, NULL } };
238 #define FT_ERRMSG(e) ft_errors[e].err_msg
252 const Glyph *
a = key, *bb =
b;
253 int64_t
diff = (int64_t)a->code - (int64_t)bb->code;
254 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
273 !(glyph->glyph =
av_mallocz(
sizeof(*glyph->glyph)))) {
279 if (FT_Get_Glyph(dtext->
face->glyph, glyph->glyph)) {
284 glyph->bitmap = dtext->
face->glyph->bitmap;
285 glyph->bitmap_left = dtext->
face->glyph->bitmap_left;
286 glyph->bitmap_top = dtext->
face->glyph->bitmap_top;
287 glyph->advance = dtext->
face->glyph->advance.x >> 6;
290 FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
317 err = FT_New_Face(dtext->
library, path, index, &dtext->
face);
325 #if CONFIG_FONTCONFIG
326 static int load_font_fontconfig(
AVFilterContext *ctx,
const char **error)
329 FcConfig *fontconfig;
330 FcPattern *pattern, *fpat;
331 FcResult result = FcResultMatch;
336 fontconfig = FcInitLoadConfigAndFonts();
338 *error =
"impossible to init fontconfig\n";
342 (
uint8_t *)(intptr_t)
"default");
344 *error =
"could not parse fontconfig pattern";
347 if (!FcConfigSubstitute(fontconfig, pattern, FcMatchPattern)) {
348 *error =
"could not substitue fontconfig options";
351 FcDefaultSubstitute(pattern);
352 fpat = FcFontMatch(fontconfig, pattern, &result);
353 if (!fpat || result != FcResultMatch) {
354 *error =
"impossible to find a matching font";
357 if (FcPatternGetString (fpat, FC_FILE, 0, &filename) != FcResultMatch ||
358 FcPatternGetInteger(fpat, FC_INDEX, 0, &index ) != FcResultMatch ||
359 FcPatternGetDouble (fpat, FC_SIZE, 0, &size ) != FcResultMatch) {
360 *error =
"impossible to find font information";
369 FcPatternDestroy(fpat);
370 FcPatternDestroy(pattern);
371 FcConfigDestroy(fontconfig);
380 const char *error =
"unknown error\n";
386 #if CONFIG_FONTCONFIG
387 err = load_font_fontconfig(ctx, &error);
405 "The text file '%s' could not be read or is empty\n",
412 memcpy(dtext->
text, textbuf, textbuf_size);
413 dtext->
text[textbuf_size] = 0;
425 dtext->
class = &drawtext_class;
431 if (!dtext->
fontfile && !CONFIG_FONTCONFIG) {
439 "Both text and text file provided. Please provide only one\n");
462 "Either text, a valid file or a timecode must be provided\n");
484 if ((err = FT_Init_FreeType(&(dtext->
library)))) {
486 "Could not load FreeType: %s\n",
FT_ERRMSG(err));
495 if ((err = FT_Set_Pixel_Sizes(dtext->
face, 0, dtext->
fontsize))) {
507 if ((err =
load_glyph(ctx, &glyph,
' ')) < 0) {
511 dtext->
tabsize *= glyph->advance;
514 (strchr(dtext->
text,
'%') || strchr(dtext->
text,
'\\')))
532 FT_Done_Glyph(*glyph->glyph);
554 FT_Done_Face(dtext->
face);
555 FT_Done_FreeType(dtext->
library);
562 return c ==
'\n' || c ==
'\r' || c ==
'\f' || c ==
'\v';
606 if (!strcmp(cmd,
"reinit")) {
610 if ((ret =
init(ctx, arg)) < 0)
619 char *fct,
unsigned argc,
char **argv,
int tag)
628 char *fct,
unsigned argc,
char **argv,
int tag)
636 #if !HAVE_LOCALTIME_R
644 char *fct,
unsigned argc,
char **argv,
int tag)
646 const char *
fmt = argc ? argv[0] :
"%Y-%m-%d %H:%M:%S";
660 char *fct,
unsigned argc,
char **argv,
int tag)
668 &dtext->
prng, 0, ctx);
671 "Expression '%s' for the expr text expansion function is not valid\n",
695 unsigned argc,
char **argv)
723 const char *text = *rtext;
724 char *argv[16] = {
NULL };
725 unsigned argc = 0, i;
750 if ((ret =
eval_function(ctx, bp, argv[0], argc - 1, argv + 1)) < 0)
753 *rtext = (
char *)text + 1;
756 for (i = 0; i < argc; i++)
764 char *text = dtext->
text;
770 if (*text ==
'\\' && text[1]) {
773 }
else if (*text ==
'%') {
796 for (i = 0, p = text; *p; i++) {
801 if (code ==
'\n' || code ==
'\r' || code ==
'\t')
807 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
808 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
816 glyph->bitmap.buffer, glyph->bitmap.pitch,
817 glyph->bitmap.width, glyph->bitmap.rows,
818 glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
829 uint32_t code = 0, prev_code = 0;
830 int x = 0, y = 0, i = 0, ret;
831 int max_text_line_w = 0,
len;
833 char *text = dtext->
text;
835 int y_min = 32000, y_max = -32000;
836 int x_min = 32000, x_max = -32000;
838 Glyph *glyph =
NULL, *prev_glyph =
NULL;
841 time_t now = time(0);
885 for (i = 0, p = text; *p; i++) {
895 y_min =
FFMIN(glyph->bbox.yMin, y_min);
896 y_max =
FFMAX(glyph->bbox.yMax, y_max);
897 x_min =
FFMIN(glyph->bbox.xMin, x_min);
898 x_max =
FFMAX(glyph->bbox.xMax, x_max);
905 for (i = 0, p = text; *p; i++) {
909 if (prev_code ==
'\r' && code ==
'\n')
914 max_text_line_w =
FFMAX(max_text_line_w, x);
926 if (dtext->
use_kerning && prev_glyph && glyph->code) {
927 FT_Get_Kerning(dtext->
face, prev_glyph->code, glyph->code,
928 ft_kerning_default, &delta);
933 dtext->
positions[i].x = x + glyph->bitmap_left;
934 dtext->
positions[i].y = y - glyph->bitmap_top + y_max;
936 else x += glyph->advance;
939 max_text_line_w =
FFMAX(x, max_text_line_w);
959 box_w =
FFMIN(width - 1 , max_text_line_w);
966 dtext->
x, dtext->
y, box_w, box_h);
1000 dtext->
x, dtext->
y);
1030 .description =
NULL_IF_CONFIG_SMALL(
"Draw text on top of video frames using libfreetype library."),
1036 .
inputs = avfilter_vf_drawtext_inputs,
1037 .
outputs = avfilter_vf_drawtext_outputs,
1039 .priv_class = &drawtext_class,