]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_drawtext.c
avfiltergraph: reindent
[ffmpeg] / libavfilter / vf_drawtext.c
index 060c3a4a0e206d3ed253a162d31ebc75c00230e4..4846716fe097fc5ccff7c43209f7166fb1010ea9 100644 (file)
@@ -49,7 +49,7 @@
 #include FT_FREETYPE_H
 #include FT_GLYPH_H
 
-static const char *var_names[] = {
+static const char *const var_names[] = {
     "E",
     "PHI",
     "PI",
@@ -64,8 +64,8 @@ static const char *var_names[] = {
     NULL
 };
 
-static const char *fun2_names[] = {
-    "rand",
+static const char *const fun2_names[] = {
+    "rand"
 };
 
 static double drand(void *opaque, double min, double max)
@@ -122,6 +122,7 @@ typedef struct {
     short int draw_box;             ///< draw box around text - true or false
     int use_kerning;                ///< font kerning is used - true/false
     int tabsize;                    ///< tab size
+    int fix_bounds;                 ///< do we let it go out of frame bounds - t/f
 
     FT_Library library;             ///< freetype font library handle
     FT_Face face;                   ///< freetype font face handle
@@ -157,6 +158,8 @@ static const AVOption drawtext_options[]= {
 {"shadowy",  "set y",                OFFSET(shadowy),            AV_OPT_TYPE_INT,    {.dbl=0},     INT_MIN,  INT_MAX  },
 {"tabsize",  "set tab size",         OFFSET(tabsize),            AV_OPT_TYPE_INT,    {.dbl=4},     0,        INT_MAX  },
 {"draw",     "if false do not draw", OFFSET(d_expr),             AV_OPT_TYPE_STRING, {.str="1"},   CHAR_MIN, CHAR_MAX },
+{"fix_bounds", "if true, check and fix text coords to avoid clipping",
+                                     OFFSET(fix_bounds),         AV_OPT_TYPE_INT,    {.dbl=1},     0,        1        },
 
 /* FT_LOAD_* flags */
 {"ft_load_flags", "set font loading flags for libfreetype",   OFFSET(ft_load_flags),  AV_OPT_TYPE_FLAGS,  {.dbl=FT_LOAD_DEFAULT|FT_LOAD_RENDER}, 0, INT_MAX, 0, "ft_load_flags" },
@@ -429,7 +432,7 @@ static av_cold void uninit(AVFilterContext *ctx)
 
 static inline int is_newline(uint32_t c)
 {
-    return (c == '\n' || c == '\r' || c == '\f' || c == '\v');
+    return c == '\n' || c == '\r' || c == '\f' || c == '\v';
 }
 
 static int dtext_prepare_text(AVFilterContext *ctx)
@@ -492,9 +495,11 @@ static int dtext_prepare_text(AVFilterContext *ctx)
         /* get glyph */
         dummy.code = code;
         glyph = av_tree_find(dtext->glyphs, &dummy, glyph_cmp, NULL);
-        if (!glyph)
+        if (!glyph) {
             ret = load_glyph(ctx, &glyph, code);
-        if (ret) return ret;
+            if (ret)
+                return ret;
+        }
 
         y_min = FFMIN(glyph->bbox.yMin, y_min);
         y_max = FFMAX(glyph->bbox.yMax, y_max);
@@ -548,7 +553,9 @@ static int dtext_prepare_text(AVFilterContext *ctx)
     y     = FFMIN(y + text_height, height - 1);
 
     dtext->w = str_w;
+    dtext->var_values[VAR_TEXT_W] = dtext->var_values[VAR_TW] = dtext->w;
     dtext->h = y;
+    dtext->var_values[VAR_TEXT_H] = dtext->var_values[VAR_TH] = dtext->h;
 
     return 0;
 }
@@ -824,12 +831,14 @@ static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
     normalize_double(&dtext->x, dtext->var_values[VAR_X]);
     normalize_double(&dtext->y, dtext->var_values[VAR_Y]);
 
-    if (dtext->x < 0) dtext->x = 0;
-    if (dtext->y < 0) dtext->y = 0;
-    if ((unsigned)dtext->x + (unsigned)dtext->w > inlink->w)
-        dtext->x = inlink->w - dtext->w;
-    if ((unsigned)dtext->y + (unsigned)dtext->h > inlink->h)
-        dtext->y = inlink->h - dtext->h;
+    if (dtext->fix_bounds) {
+        if (dtext->x < 0) dtext->x = 0;
+        if (dtext->y < 0) dtext->y = 0;
+        if ((unsigned)dtext->x + (unsigned)dtext->w > inlink->w)
+            dtext->x = inlink->w - dtext->w;
+        if ((unsigned)dtext->y + (unsigned)dtext->h > inlink->h)
+            dtext->y = inlink->h - dtext->h;
+    }
 
     dtext->x &= ~((1 << dtext->hsub) - 1);
     dtext->y &= ~((1 << dtext->vsub) - 1);