diff mbox series

[FFmpeg-devel] avfilter/vf_drawtext: additions

Message ID CAPYw7P7MORLMH5F2gTCzs1yec9WUof5CkJJ45gnVotOT841qbw@mail.gmail.com
State New
Headers show
Series [FFmpeg-devel] avfilter/vf_drawtext: additions | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/configure_x86 warning Failed to apply patch

Commit Message

Paul B Mahol June 14, 2023, 5:26 p.m. UTC
Set of patches attached extending drawtext filter with libharfbuzz.
Will apply if nobody comments within week.
diff mbox series

Patch

From 42557f6147e0f65f736ebfb479dd3d12d155d713 Mon Sep 17 00:00:00 2001
From: yethie <klimklim@tiscali.it>
Date: Fri, 26 May 2023 12:11:41 +0200
Subject: [PATCH 2/8] avfilter/vf_drawtext: improve glyph shaping and
 positioning

- text is now shaped using libharfbuz
- glyphs position is now accurate to 1/4 pixel in both directions
- the default line height is now the one defined in the font

Adds libharfbuzz dependency.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 configure                 |   5 +-
 doc/filters.texi          |   8 +-
 libavfilter/vf_drawtext.c | 859 ++++++++++++++++++++++++++------------
 3 files changed, 592 insertions(+), 280 deletions(-)

diff --git a/configure b/configure
index e41fe0f172..d8ae37bf8d 100755
--- a/configure
+++ b/configure
@@ -236,6 +236,7 @@  External library support:
   --enable-libfontconfig   enable libfontconfig, useful for drawtext filter [no]
   --enable-libfreetype     enable libfreetype, needed for drawtext filter [no]
   --enable-libfribidi      enable libfribidi, improves drawtext filter [no]
+  --enable-libharfbuzz     enable libharfbuzz, needed for drawtext filter [no]
   --enable-libglslang      enable GLSL->SPIRV compilation via libglslang [no]
   --enable-libgme          enable Game Music Emu via libgme [no]
   --enable-libgsm          enable GSM de/encoding via libgsm [no]
@@ -1859,6 +1860,7 @@  EXTERNAL_LIBRARY_LIST="
     libfontconfig
     libfreetype
     libfribidi
+    libharfbuzz
     libglslang
     libgme
     libgsm
@@ -3720,7 +3722,7 @@  dilation_opencl_filter_deps="opencl"
 dnn_classify_filter_select="dnn"
 dnn_detect_filter_select="dnn"
 dnn_processing_filter_select="dnn"
-drawtext_filter_deps="libfreetype"
+drawtext_filter_deps="libfreetype libharfbuzz"
 drawtext_filter_suggest="libfontconfig libfribidi"
 elbg_filter_deps="avcodec"
 eq_filter_deps="gpl"
@@ -6687,6 +6689,7 @@  enabled fontconfig        && enable libfontconfig
 enabled libfontconfig     && require_pkg_config libfontconfig fontconfig "fontconfig/fontconfig.h" FcInit
 enabled libfreetype       && require_pkg_config libfreetype freetype2 "ft2build.h FT_FREETYPE_H" FT_Init_FreeType
 enabled libfribidi        && require_pkg_config libfribidi fribidi fribidi.h fribidi_version_info
+enabled libharfbuzz       && require_pkg_config libharfbuzz harfbuzz hb.h hb_buffer_create
 enabled libglslang && { check_lib spirv_compiler glslang/Include/glslang_c_interface.h glslang_initialize_process \
                             -lglslang -lMachineIndependent -lOSDependent -lHLSL -lOGLCompiler -lGenericCodeGen \
                             -lSPVRemapper -lSPIRV -lSPIRV-Tools-opt -lSPIRV-Tools -lpthread -lstdc++ -lm ||
diff --git a/doc/filters.texi b/doc/filters.texi
index a46357bfd8..b10165695e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -12292,7 +12292,7 @@  Draw a text string or text from a specified file on top of a video, using the
 libfreetype library.
 
 To enable compilation of this filter, you need to configure FFmpeg with
-@code{--enable-libfreetype}.
+@code{--enable-libfreetype} and @code{--enable-libharfbuzz}.
 To enable default font fallback and the @var{font} option you need to
 configure FFmpeg with @code{--enable-libfontconfig}.
 To enable the @var{text_shaping} option, you need to configure FFmpeg with
@@ -12320,8 +12320,7 @@  option, check the @ref{color syntax,,"Color" section in the ffmpeg-utils manual,
 The default value of @var{boxcolor} is "white".
 
 @item line_spacing
-Set the line spacing in pixels of the border to be drawn around the box using @var{box}.
-The default value of @var{line_spacing} is 0.
+Set the line spacing in pixels. The default value of @var{line_spacing} is 0.
 
 @item borderw
 Set the width of the border to be drawn around the text using @var{bordercolor}.
@@ -12821,6 +12820,9 @@  For more information about fontconfig, check:
 For more information about libfribidi, check:
 @url{http://fribidi.org/}.
 
+For more information about libharfbuzz, check:
+@url{https://github.com/harfbuzz/harfbuzz}.
+
 @section edgedetect
 
 Detect and draw edges. The filter uses the Canny Edge Detection algorithm.
diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index 0c4db3ec53..bdab591efd 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -1,4 +1,5 @@ 
 /*
+ * Copyright (c) 2023 Francesco Carusi
  * Copyright (c) 2011 Stefano Sabatini
  * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
  * Copyright (c) 2003 Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
@@ -72,10 +73,16 @@ 
 #include FT_GLYPH_H
 #include FT_STROKER_H
 
+#include <hb.h>
+#include <hb-ft.h>
+
+// Ceiling operation for positive integers division
+#define POS_CEIL(x, y) ((x)/(y) + ((x)%(y) != 0))
+
 static const char *const var_names[] = {
     "dar",
     "hsub", "vsub",
-    "line_h", "lh",           ///< line height, same as max_glyph_h
+    "line_h", "lh",           ///< line height
     "main_h", "h", "H",       ///< height of the input video
     "main_w", "w", "W",       ///< width  of the input video
     "max_glyph_a", "ascent",  ///< max glyph ascender
@@ -156,6 +163,72 @@  enum expansion_mode {
     EXP_STRFTIME,
 };
 
+typedef struct HarfbuzzData {
+    hb_buffer_t* buf;
+    hb_font_t* font;
+    unsigned int glyph_count;
+    hb_glyph_info_t* glyph_info;
+    hb_glyph_position_t* glyph_pos;
+} HarfbuzzData;
+
+/** Information about a single glyph in a text line */
+typedef struct GlyphInfo {
+    uint32_t code;                  ///< the glyph code point
+    int x;                          ///< the x position of the glyph
+    int y;                          ///< the y position of the glyph
+    int shift_x64;                  ///< the horizontal shift of the glyph in 26.6 units
+    int shift_y64;                  ///< the vertical shift of the glyph in 26.6 units
+} GlyphInfo;
+
+/** Information about a single line of text */
+typedef struct TextLine {
+    int offset_left64;              ///< offset between the origin and
+                                    ///  the leftmost pixel of the first glyph
+    int offset_right64;             ///< maximum offset between the origin and
+                                    ///  the rightmost pixel of the last glyph
+    int width64;                    ///< width of the line
+    HarfbuzzData hb_data;           ///< libharfbuzz data of this text line
+    GlyphInfo* glyphs;              ///< array of glyphs in this text line
+    int cluster_offset;             ///< the offset at which this line begins
+} TextLine;
+
+/** A glyph as loaded and rendered using libfreetype */
+typedef struct Glyph {
+    FT_Glyph glyph;
+    FT_Glyph border_glyph;
+    uint32_t code;
+    unsigned int fontsize;
+    /** Glyph bitmaps with 1/4 pixel precision in both directions */
+    FT_BitmapGlyph bglyph[16];
+    /** Outlined glyph bitmaps with 1/4 pixel precision in both directions */
+    FT_BitmapGlyph border_bglyph[16];
+    FT_BBox bbox;
+} Glyph;
+
+/** Global text metrics */
+typedef struct TextMetrics {
+    int offset_top64;               ///< ascender amount of the first line (in 26.6 units)
+    int offset_bottom64;            ///< descender amount of the last line (in 26.6 units)
+    int offset_left64;              ///< maximum offset between the origin and
+                                    ///  the leftmost pixel of the first glyph
+                                    ///  of each line (in 26.6 units)
+    int offset_right64;             ///< maximum offset between the origin and
+                                    ///  the rightmost pixel of the last glyph
+                                    ///  of each line (in 26.6 units)
+    int line_height64;              ///< the font-defined line height
+    int width;                      ///< width of the longest line - ceil(width64/64)
+    int height;                     ///< total height of the text - ceil(height64/64)
+
+    int min_y64;                    ///< minimum value of bbox.yMin among glyphs (in 26.6 units)
+    int max_y64;                    ///< maximum value of bbox.yMax among glyphs (in 26.6 units)
+    int min_x64;                    ///< minimum value of bbox.xMin among glyphs (in 26.6 units)
+    int max_x64;                    ///< maximum value of bbox.xMax among glyphs (in 26.6 units)
+
+    // Position of the background box (without borders)
+    int rect_x;                     ///< x position of the box
+    int rect_y;                     ///< y position of the box
+} TextMetrics;
+
 typedef struct DrawTextContext {
     const AVClass *class;
     int exp_mode;                   ///< expansion mode to use for the text
@@ -169,11 +242,9 @@  typedef struct DrawTextContext {
     uint8_t *fontcolor_expr;        ///< fontcolor expression to evaluate
     AVBPrint expanded_fontcolor;    ///< used to contain the expanded fontcolor spec
     int ft_load_flags;              ///< flags used for loading fonts, see FT_LOAD_*
-    FT_Vector *positions;           ///< positions for each element in the text
-    size_t nb_positions;            ///< number of elements of positions array
     char *textfile;                 ///< file with text to be drawn
-    int x;                          ///< x position to start drawing text
-    int y;                          ///< y position to start drawing text
+    double x;                       ///< x position to start drawing text
+    double y;                       ///< y position to start drawing text
     int max_glyph_w;                ///< max glyph width
     int max_glyph_h;                ///< max glyph height
     int shadowx, shadowy;
@@ -186,7 +257,10 @@  typedef struct DrawTextContext {
     int line_spacing;               ///< lines spacing in pixels
     short int draw_box;             ///< draw box around text - true or false
     int boxborderw;                 ///< box border width
-    int use_kerning;                ///< font kerning is used - true/false
+    int bb_top;                     ///< the size of the top box border
+    int bb_right;                   ///< the size of the right box border
+    int bb_bottom;                  ///< the size of the bottom box border
+    int bb_left;                    ///< the size of the left box border
     int tabsize;                    ///< tab size
     int fix_bounds;                 ///< do we let it go out of frame bounds - t/f
 
@@ -221,6 +295,12 @@  typedef struct DrawTextContext {
     int text_shaping;               ///< 1 to shape the text before drawing it
 #endif
     AVDictionary *metadata;
+
+    TextLine *lines;                ///< computed information about text lines
+    int line_count;                 ///< the number of text lines
+    uint32_t *tab_clusters;         ///< the position of tab characters in the text
+    int tab_count;                  ///< the number of tab characters
+    int blank_advance64;            ///< the size of the space character
 } DrawTextContext;
 
 #define OFFSET(x) offsetof(DrawTextContext, x)
@@ -236,7 +316,7 @@  static const AVOption drawtext_options[]= {
     {"bordercolor",    "set border color",      OFFSET(bordercolor.rgba),   AV_OPT_TYPE_COLOR,  {.str="black"}, 0, 0, FLAGS},
     {"shadowcolor",    "set shadow color",      OFFSET(shadowcolor.rgba),   AV_OPT_TYPE_COLOR,  {.str="black"}, 0, 0, FLAGS},
     {"box",            "set box",               OFFSET(draw_box),           AV_OPT_TYPE_BOOL,   {.i64=0},     0, 1, FLAGS},
-    {"boxborderw",     "set box borders width", OFFSET(boxborderw),         AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN, INT_MAX, FLAGS},
+    {"boxborderw",     "set box borders width", OFFSET(boxborderw),         AV_OPT_TYPE_INT,    {.i64=0},     0, INT_MAX, FLAGS},
     {"line_spacing",   "set line spacing in pixels", OFFSET(line_spacing),  AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN, INT_MAX, FLAGS},
     {"fontsize",       "set font size",         OFFSET(fontsize_expr),      AV_OPT_TYPE_STRING, {.str=NULL},  0, 0, FLAGS},
     {"x",              "set x expression",      OFFSET(x_expr),             AV_OPT_TYPE_STRING, {.str="0"},   0, 0, FLAGS},
@@ -305,19 +385,6 @@  static const struct ft_error {
 
 #define FT_ERRMSG(e) ft_errors[e].err_msg
 
-typedef struct Glyph {
-    FT_Glyph glyph;
-    FT_Glyph border_glyph;
-    uint32_t code;
-    unsigned int fontsize;
-    FT_Bitmap bitmap; ///< array holding bitmaps of font
-    FT_Bitmap border_bitmap; ///< array holding bitmaps of font border
-    FT_BBox bbox;
-    int advance;
-    int bitmap_left;
-    int bitmap_top;
-} Glyph;
-
 static int glyph_cmp(const void *key, const void *b)
 {
     const Glyph *a = key, *bb = b;
@@ -329,77 +396,6 @@  static int glyph_cmp(const void *key, const void *b)
         return FFDIFFSIGN((int64_t)a->fontsize, (int64_t)bb->fontsize);
 }
 
-/**
- * Load glyphs corresponding to the UTF-32 codepoint code.
- */
-static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
-{
-    DrawTextContext *s = ctx->priv;
-    FT_BitmapGlyph bitmapglyph;
-    Glyph *glyph;
-    struct AVTreeNode *node = NULL;
-    int ret;
-
-    /* load glyph into s->face->glyph */
-    if (FT_Load_Char(s->face, code, s->ft_load_flags))
-        return AVERROR(EINVAL);
-
-    glyph = av_mallocz(sizeof(*glyph));
-    if (!glyph) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-    glyph->code  = code;
-    glyph->fontsize = s->fontsize;
-
-    if (FT_Get_Glyph(s->face->glyph, &glyph->glyph)) {
-        ret = AVERROR(EINVAL);
-        goto error;
-    }
-    if (s->borderw) {
-        glyph->border_glyph = glyph->glyph;
-        if (FT_Glyph_StrokeBorder(&glyph->border_glyph, s->stroker, 0, 0) ||
-            FT_Glyph_To_Bitmap(&glyph->border_glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
-            ret = AVERROR_EXTERNAL;
-            goto error;
-        }
-        bitmapglyph = (FT_BitmapGlyph) glyph->border_glyph;
-        glyph->border_bitmap = bitmapglyph->bitmap;
-    }
-    if (FT_Glyph_To_Bitmap(&glyph->glyph, FT_RENDER_MODE_NORMAL, 0, 1)) {
-        ret = AVERROR_EXTERNAL;
-        goto error;
-    }
-    bitmapglyph = (FT_BitmapGlyph) glyph->glyph;
-
-    glyph->bitmap      = bitmapglyph->bitmap;
-    glyph->bitmap_left = bitmapglyph->left;
-    glyph->bitmap_top  = bitmapglyph->top;
-    glyph->advance     = s->face->glyph->advance.x >> 6;
-
-    /* measure text height to calculate text_height (or the maximum text height) */
-    FT_Glyph_Get_CBox(glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
-
-    /* cache the newly created glyph */
-    if (!(node = av_tree_node_alloc())) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-    av_tree_insert(&s->glyphs, glyph, glyph_cmp, &node);
-
-    if (glyph_ptr)
-        *glyph_ptr = glyph;
-    return 0;
-
-error:
-    if (glyph)
-        av_freep(&glyph->glyph);
-
-    av_freep(&glyph);
-    av_freep(&node);
-    return ret;
-}
-
 static av_cold int set_fontsize(AVFilterContext *ctx, unsigned int fontsize)
 {
     int err;
@@ -718,11 +714,114 @@  static enum AVFrameSideDataType text_source_string_parse(const char *text_source
     }
 }
 
+static inline int get_subpixel_idx(int shift_x64, int shift_y64)
+{
+    int idx = (shift_x64 >> 2) + (shift_y64 >> 4);
+    return idx;
+}
+
+// Loads and (optionally) renders a glyph
+static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code, int8_t shift_x64, int8_t shift_y64)
+{
+    DrawTextContext *s = ctx->priv;
+    Glyph dummy = { 0 };
+    Glyph *glyph;
+    FT_Vector shift;
+    struct AVTreeNode *node = NULL;
+    int ret = 0;
+
+    /* get glyph */
+    dummy.code = code;
+    dummy.fontsize = s->fontsize;
+    glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
+    if (!glyph) {
+        if (FT_Load_Glyph(s->face, code, s->ft_load_flags)) {
+            return AVERROR(EINVAL);
+        }
+        glyph = av_mallocz(sizeof(*glyph));
+        if (!glyph) {
+            ret = AVERROR(ENOMEM);
+            goto error;
+        }
+        glyph->code  = code;
+        glyph->fontsize = s->fontsize;
+        if (FT_Get_Glyph(s->face->glyph, &glyph->glyph)) {
+            ret = AVERROR(EINVAL);
+            goto error;
+        }
+        if (s->borderw) {
+            glyph->border_glyph = glyph->glyph;
+            if (FT_Glyph_StrokeBorder(&glyph->border_glyph, s->stroker, 0, 0)) {
+                ret = AVERROR_EXTERNAL;
+                goto error;
+            }
+        }
+        /* measure text height to calculate text_height (or the maximum text height) */
+        FT_Glyph_Get_CBox(glyph->glyph, FT_GLYPH_BBOX_SUBPIXELS, &glyph->bbox);
+
+        /* cache the newly created glyph */
+        if (!(node = av_tree_node_alloc())) {
+            ret = AVERROR(ENOMEM);
+            goto error;
+        }
+        av_tree_insert(&s->glyphs, glyph, glyph_cmp, &node);
+    } else {
+        if (s->borderw && !glyph->border_glyph) {
+            glyph->border_glyph = glyph->glyph;
+            if (FT_Glyph_StrokeBorder(&glyph->border_glyph, s->stroker, 0, 0)) {
+                ret = AVERROR_EXTERNAL;
+                goto error;
+            }
+        }
+    }
+
+    // Check if a bitmap is needed
+    if (shift_x64 >= 0 && shift_y64 >= 0) {
+        // Get the bitmap subpixel index (0 -> 15)
+        int idx = get_subpixel_idx(shift_x64, shift_y64);
+        shift.x = shift_x64;
+        shift.y = shift_y64;
+
+        if (!glyph->bglyph[idx]) {
+            FT_Glyph tmp_glyph = glyph->glyph;
+            if (FT_Glyph_To_Bitmap(&tmp_glyph, FT_RENDER_MODE_NORMAL, &shift, 0)) {
+                ret = AVERROR_EXTERNAL;
+                goto error;
+            }
+            glyph->bglyph[idx] = (FT_BitmapGlyph)tmp_glyph;
+            if (glyph->bglyph[idx]->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) {
+                av_log(ctx, AV_LOG_ERROR, "Monocromatic (1bpp) fonts are not supported.\n");
+                ret = AVERROR(EINVAL);
+                goto error;
+            }
+        }
+        if (s->borderw && !glyph->border_bglyph[idx]) {
+            FT_Glyph tmp_glyph = glyph->border_glyph;
+            if (FT_Glyph_To_Bitmap(&tmp_glyph, FT_RENDER_MODE_NORMAL, &shift, 0)) {
+                ret = AVERROR_EXTERNAL;
+                goto error;
+            }
+            glyph->border_bglyph[idx] = (FT_BitmapGlyph)tmp_glyph;
+        }
+    }
+    if (glyph_ptr) {
+        *glyph_ptr = glyph;
+    }
+    return 0;
+
+error:
+    if (glyph && glyph->glyph)
+        FT_Done_Glyph(glyph->glyph);
+
+    av_freep(&glyph);
+    av_freep(&node);
+    return ret;
+}
+
 static av_cold int init(AVFilterContext *ctx)
 {
     int err;
     DrawTextContext *s = ctx->priv;
-    Glyph *glyph;
 
     av_expr_free(s->fontsize_pexpr);
     s->fontsize_pexpr = NULL;
@@ -811,17 +910,8 @@  static av_cold int init(AVFilterContext *ctx)
                        FT_STROKER_LINEJOIN_ROUND, 0);
     }
 
-    s->use_kerning = FT_HAS_KERNING(s->face);
-
     /* load the fallback glyph with code 0 */
-    load_glyph(ctx, NULL, 0);
-
-    /* set the tabsize in pixels */
-    if ((err = load_glyph(ctx, &glyph, ' ')) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n");
-        return err;
-    }
-    s->tabsize *= glyph->advance;
+    load_glyph(ctx, NULL, 0, 0, 0);
 
     if (s->exp_mode == EXP_STRFTIME &&
         (strchr(s->text, '%') || strchr(s->text, '\\')))
@@ -844,6 +934,14 @@  static int glyph_enu_free(void *opaque, void *elem)
 
     FT_Done_Glyph(glyph->glyph);
     FT_Done_Glyph(glyph->border_glyph);
+    for (int t = 0; t < 16; ++t) {
+        if (glyph->bglyph[t] != NULL) {
+            FT_Done_Glyph((FT_Glyph)glyph->bglyph[t]);
+        }
+        if (glyph->border_bglyph[t] != NULL) {
+            FT_Done_Glyph((FT_Glyph)glyph->border_bglyph[t]);
+        }
+    }
     av_free(elem);
     return 0;
 }
@@ -859,9 +957,6 @@  static av_cold void uninit(AVFilterContext *ctx)
 
     s->x_pexpr = s->y_pexpr = s->a_pexpr = s->fontsize_pexpr = NULL;
 
-    av_freep(&s->positions);
-    s->nb_positions = 0;
-
     av_tree_enumerate(s->glyphs, NULL, NULL, glyph_enu_free);
     av_tree_destroy(s->glyphs);
     s->glyphs = NULL;
@@ -1325,91 +1420,283 @@  static int expand_text(AVFilterContext *ctx, char *text, AVBPrint *bp)
     return 0;
 }
 
+static void update_color_with_alpha(DrawTextContext *s, FFDrawColor *color, const FFDrawColor incolor)
+{
+    *color = incolor;
+    color->rgba[3] = (color->rgba[3] * s->alpha) / 255;
+    ff_draw_color(&s->dc, color, color->rgba);
+}
+
+static void update_alpha(DrawTextContext *s)
+{
+    double alpha = av_expr_eval(s->a_pexpr, s->var_values, &s->prng);
+
+    if (isnan(alpha))
+        return;
+
+    if (alpha >= 1.0)
+        s->alpha = 255;
+    else if (alpha <= 0)
+        s->alpha = 0;
+    else
+        s->alpha = 256 * alpha;
+}
+
 static int draw_glyphs(DrawTextContext *s, AVFrame *frame,
-                       int width, int height,
                        FFDrawColor *color,
+                       TextMetrics *metrics,
                        int x, int y, int borderw)
 {
-    char *text = s->expanded_text.str;
-    uint32_t code = 0;
-    int i, x1, y1;
-    uint8_t *p;
-    Glyph *glyph = NULL;
-
-    for (i = 0, p = text; *p; i++) {
-        FT_Bitmap bitmap;
-        Glyph dummy = { 0 };
-        GET_UTF8(code, *p ? *p++ : 0, code = 0xfffd; goto continue_on_invalid;);
-continue_on_invalid:
+    int g, l, x1, y1, w1, h1, idx;
+    int dx = 0, dy = 0, pdx = 0;
+    GlyphInfo *info;
+    Glyph dummy = { 0 }, *glyph;
+    FT_Bitmap bitmap;
+    FT_BitmapGlyph b_glyph;
+    int clip_x = 0, clip_y = 0;
+
+    clip_x = FFMIN(metrics->rect_x + metrics->width + s->bb_right, frame->width);
+    clip_y = FFMIN(metrics->rect_y + metrics->height + s->bb_bottom, frame->height);
+
+    for (l = 0; l < s->line_count; ++l) {
+        TextLine *line = &s->lines[l];
+        for (g = 0; g < line->hb_data.glyph_count; ++g) {
+            info = &line->glyphs[g];
+            dummy.fontsize = s->fontsize;
+            dummy.code = info->code;
+            glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
+            if (!glyph) {
+                return AVERROR(EINVAL);
+            }
 
-        /* skip new line chars, just go to new line */
-        if (code == '\n' || code == '\r' || code == '\t')
-            continue;
+            idx = get_subpixel_idx(info->shift_x64, info->shift_y64);
+            b_glyph = borderw ? glyph->border_bglyph[idx] : glyph->bglyph[idx];
+            bitmap = b_glyph->bitmap;
+            x1 = x + info->x + b_glyph->left;
+            y1 = y + info->y - b_glyph->top;
+            w1 = bitmap.width;
+            h1 = bitmap.rows;
+
+            // Offset of the glyph's bitmap in the visible region
+            dx = dy = 0;
+            if (x1 < metrics->rect_x - s->bb_left) {
+                dx = metrics->rect_x - s->bb_left - x1;
+                x1 = metrics->rect_x - s->bb_left;
+            }
+            if (y1 < metrics->rect_y - s->bb_top) {
+                dy = metrics->rect_y - s->bb_top - y1;
+                y1 = metrics->rect_y - s->bb_top;
+            }
 
-        dummy.code = code;
-        dummy.fontsize = s->fontsize;
-        glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
+            // check if the glyph is empty or out of the clipping region
+            if (dx >= w1 || dy >= h1 || x1 >= clip_x || y1 >= clip_y) {
+                continue;
+            }
 
-        bitmap = borderw ? glyph->border_bitmap : glyph->bitmap;
+            pdx = dx + dy * bitmap.pitch;
+            w1 = FFMIN(clip_x - x1, w1 - dx);
+            h1 = FFMIN(clip_y - y1, h1 - dy);
 
-        if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
-            glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
-            return AVERROR(EINVAL);
+            ff_blend_mask(&s->dc, color, frame->data, frame->linesize, clip_x, clip_y,
+                bitmap.buffer + pdx, bitmap.pitch, w1, h1, 3, 0, x1, y1);
+        }
+    }
 
-        x1 = s->positions[i].x+s->x+x - borderw;
-        y1 = s->positions[i].y+s->y+y - borderw;
+    return 0;
+}
 
-        ff_blend_mask(&s->dc, color,
-                      frame->data, frame->linesize, width, height,
-                      bitmap.buffer, bitmap.pitch,
-                      bitmap.width, bitmap.rows,
-                      bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
-                      0, x1, y1);
+// Shapes a line of text using libharfbuzz
+static int shape_text_hb(DrawTextContext *s, HarfbuzzData* hb, const char* text, int textLen)
+{
+    hb->buf = hb_buffer_create();
+    if(!hb_buffer_allocation_successful(hb->buf)) {
+        return AVERROR(ENOMEM);
     }
+    hb_buffer_set_direction(hb->buf, HB_DIRECTION_LTR);
+    hb_buffer_set_script(hb->buf, HB_SCRIPT_LATIN);
+    hb_buffer_set_language(hb->buf, hb_language_from_string("en", -1));
+    hb_buffer_guess_segment_properties(hb->buf);
+    hb->font = hb_ft_font_create(s->face, NULL);
+    if(hb->font == NULL) {
+        return AVERROR(ENOMEM);
+    }
+    hb_ft_font_set_funcs(hb->font);
+    hb_buffer_add_utf8(hb->buf, text, textLen, 0, -1);
+    hb_shape(hb->font, hb->buf, NULL, 0);
+    hb->glyph_info = hb_buffer_get_glyph_infos(hb->buf, &hb->glyph_count);
+    hb->glyph_pos = hb_buffer_get_glyph_positions(hb->buf, &hb->glyph_count);
 
     return 0;
 }
 
-
-static void update_color_with_alpha(DrawTextContext *s, FFDrawColor *color, const FFDrawColor incolor)
+static void hb_destroy(HarfbuzzData *hb)
 {
-    *color = incolor;
-    color->rgba[3] = (color->rgba[3] * s->alpha) / 255;
-    ff_draw_color(&s->dc, color, color->rgba);
+    hb_buffer_destroy(hb->buf);
+    hb_font_destroy(hb->font);
+    hb->buf = NULL;
+    hb->font = NULL;
+    hb->glyph_info = NULL;
+    hb->glyph_pos = NULL;
 }
 
-static void update_alpha(DrawTextContext *s)
+static int measure_text(AVFilterContext *ctx, TextMetrics *metrics)
 {
-    double alpha = av_expr_eval(s->a_pexpr, s->var_values, &s->prng);
+    DrawTextContext *s = ctx->priv;
+    char *text = s->expanded_text.str;
+    char *textdup = NULL, *start = NULL;
+    int num_chars = 0;
+    int width64 = 0, w64 = 0;
+    int cur_min_y64 = 0, first_max_y64 = -32000;
+    int first_min_x64 = 32000, last_max_x64 = -32000;
+    int min_y64 = 32000, max_y64 = -32000, min_x64 = 32000, max_x64 = -32000;
+    int line_count = 0;
+    uint32_t code = 0;
+    Glyph *glyph = NULL;
+    int height64;
+
+    int i, tab_idx = 0, last_tab_idx = 0, line_offset = 0;
+    char* p;
+    int ret = 0;
+
+    // Count the lines and the tab characters
+    s->tab_count = 0;
+    for (i = 0, p = text; 1; i++) {
+        GET_UTF8(code, *p ? *p++ : 0, code = 0xfffd; goto continue_on_failed;);
+continue_on_failed:
+        if (is_newline(code) || code == 0) {
+            ++line_count;
+            if (code == 0) {
+                break;
+            }
+        } else if (code == '\t') {
+            ++s->tab_count;
+        }
+    }
 
-    if (isnan(alpha))
-        return;
+    // Evaluate the width of the space character if needed to replace tabs
+    if (s->tab_count > 0 && !s->blank_advance64) {
+        HarfbuzzData hb_data;
+        ret = shape_text_hb(s, &hb_data, " ", 1);
+        if(ret != 0) {
+            goto done;
+        }
+        s->blank_advance64 = hb_data.glyph_pos[0].x_advance;
+        hb_destroy(&hb_data);
+    }
 
-    if (alpha >= 1.0)
-        s->alpha = 255;
-    else if (alpha <= 0)
-        s->alpha = 0;
-    else
-        s->alpha = 256 * alpha;
+    s->line_count = line_count;
+    s->lines = av_mallocz(line_count * sizeof(TextLine));
+    s->tab_clusters = av_mallocz(s->tab_count * sizeof(uint32_t));
+    for (i = 0; i < s->tab_count; ++i) {
+        s->tab_clusters[i] = -1;
+    }
+
+    start = textdup = av_strdup(text);
+    if (textdup == NULL) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+    line_count = 0;
+    for (i = 0, p = textdup; 1; i++) {
+        if (*p == '\t') {
+            s->tab_clusters[tab_idx++] = i;
+            *p = ' ';
+        }
+        GET_UTF8(code, *p ? *p++ : 0, code = 0xfffd; goto continue_on_failed2;);
+continue_on_failed2:
+        if (is_newline(code) || code == 0) {
+            TextLine *cur_line = &s->lines[line_count];
+            HarfbuzzData *hb = &cur_line->hb_data;
+            cur_line->cluster_offset = line_offset;
+            ret = shape_text_hb(s, hb, start, num_chars);
+            if (ret != 0) {
+                goto done;
+            }
+            w64 = 0;
+            cur_min_y64 = 32000;
+            for (int t = 0; t < hb->glyph_count; ++t) {
+                uint8_t is_tab = last_tab_idx < s->tab_count &&
+                    hb->glyph_info[t].cluster == s->tab_clusters[last_tab_idx] - line_offset;
+                if (is_tab) {
+                    ++last_tab_idx;
+                }
+                ret = load_glyph(ctx, &glyph, hb->glyph_info[t].codepoint, -1, -1);
+                if (ret != 0) {
+                    goto done;
+                }
+                if (line_count == 0) {
+                    first_max_y64 = FFMAX(glyph->bbox.yMax, first_max_y64);
+                }
+                if (t == 0) {
+                    cur_line->offset_left64 = glyph->bbox.xMin;
+                    first_min_x64 = FFMIN(glyph->bbox.xMin, first_min_x64);
+                }
+                if (t == hb->glyph_count - 1) {
+                    w64 += glyph->bbox.xMax;
+                    last_max_x64 = FFMAX(glyph->bbox.xMax, last_max_x64);
+                    cur_line->offset_right64 = glyph->bbox.xMax;
+                } else {
+                    if (is_tab) {
+                        int size = s->blank_advance64 * s->tabsize;
+                        w64 = (w64 / size + 1) * size;
+                    } else {
+                        w64 += hb->glyph_pos[t].x_advance;
+                    }
+                }
+                cur_min_y64 = FFMIN(glyph->bbox.yMin, cur_min_y64);
+                min_y64 = FFMIN(glyph->bbox.yMin, min_y64);
+                max_y64 = FFMAX(glyph->bbox.yMax, max_y64);
+                min_x64 = FFMIN(glyph->bbox.xMin, min_x64);
+                max_x64 = FFMAX(glyph->bbox.xMax, max_x64);
+            }
+
+            cur_line->width64 = w64;
+
+            av_log(s, AV_LOG_DEBUG, "  Line: %d -- glyphs count: %d - width64: %d - offset_left64: %d - offset_right64: %d)\n",
+                line_count, hb->glyph_count, cur_line->width64, cur_line->offset_left64, cur_line->offset_right64);
+
+            if (w64 > width64) {
+                width64 = w64;
+            }
+            num_chars = -1;
+            start = p;
+            ++line_count;
+            line_offset = i + 1;
+        }
+
+        if (code == 0) break;
+        ++num_chars;
+    }
+
+    metrics->line_height64 = s->face->size->metrics.height;
+
+    metrics->width = POS_CEIL(width64, 64);
+    height64 = (metrics->line_height64 + s->line_spacing * 64) *
+        (FFMAX(0, line_count - 1)) + first_max_y64 - cur_min_y64;
+    metrics->height = POS_CEIL(height64, 64);
+
+    metrics->offset_top64 = first_max_y64;
+    metrics->offset_right64 = last_max_x64;
+    metrics->offset_bottom64 = cur_min_y64;
+    metrics->offset_left64 = first_min_x64;
+    metrics->min_x64 = min_x64;
+    metrics->min_y64 = min_y64;
+    metrics->max_x64 = max_x64;
+    metrics->max_y64 = max_y64;
+
+done:
+    av_free(textdup);
+    return ret;
 }
 
-static int draw_text(AVFilterContext *ctx, AVFrame *frame,
-                     int width, int height)
+static int draw_text(AVFilterContext *ctx, AVFrame *frame)
 {
     DrawTextContext *s = ctx->priv;
     AVFilterLink *inlink = ctx->inputs[0];
-
-    uint32_t code = 0, prev_code = 0;
-    int x = 0, y = 0, i = 0, ret;
-    int max_text_line_w = 0, len;
-    int box_w, box_h;
-    char *text;
-    uint8_t *p;
-    int y_min = 32000, y_max = -32000;
-    int x_min = 32000, x_max = -32000;
-    FT_Vector delta;
-    Glyph *glyph = NULL, *prev_glyph = NULL;
-    Glyph dummy = { 0 };
+    int x = 0, y = 0, ret;
+    int shift_x64, shift_y64;
+    int x64, y64;
+    Glyph *glyph = NULL;
 
     time_t now = time(0);
     struct tm ltime;
@@ -1420,6 +1707,14 @@  static int draw_text(AVFilterContext *ctx, AVFrame *frame,
     FFDrawColor bordercolor;
     FFDrawColor boxcolor;
 
+    int width = frame->width;
+    int height = frame->height;
+    int rec_x = 0, rec_y = 0, rec_width = 0, rec_height = 0;
+    int is_outside = 0;
+    int last_tab_idx = 0;
+
+    TextMetrics metrics;
+
     av_bprint_clear(bp);
 
     if (s->basetime != AV_NOPTS_VALUE)
@@ -1448,13 +1743,6 @@  static int draw_text(AVFilterContext *ctx, AVFrame *frame,
 
     if (!av_bprint_is_complete(bp))
         return AVERROR(ENOMEM);
-    text = s->expanded_text.str;
-    if ((len = s->expanded_text.len) > s->nb_positions) {
-        if (!(s->positions =
-              av_realloc(s->positions, len*sizeof(*s->positions))))
-            return AVERROR(ENOMEM);
-        s->nb_positions = len;
-    }
 
     if (s->fontcolor_expr[0]) {
         /* If expression is set, evaluate and replace the static value */
@@ -1470,85 +1758,26 @@  static int draw_text(AVFilterContext *ctx, AVFrame *frame,
         ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
     }
 
-    x = 0;
-    y = 0;
-
-    if ((ret = update_fontsize(ctx)) < 0)
+    if ((ret = update_fontsize(ctx)) < 0) {
         return ret;
-
-    /* load and cache glyphs */
-    for (i = 0, p = text; *p; i++) {
-        GET_UTF8(code, *p ? *p++ : 0, code = 0xfffd; goto continue_on_invalid;);
-continue_on_invalid:
-
-        /* get glyph */
-        dummy.code = code;
-        dummy.fontsize = s->fontsize;
-        glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
-        if (!glyph) {
-            ret = load_glyph(ctx, &glyph, code);
-            if (ret < 0)
-                return ret;
-        }
-
-        y_min = FFMIN(glyph->bbox.yMin, y_min);
-        y_max = FFMAX(glyph->bbox.yMax, y_max);
-        x_min = FFMIN(glyph->bbox.xMin, x_min);
-        x_max = FFMAX(glyph->bbox.xMax, x_max);
     }
-    s->max_glyph_h = y_max - y_min;
-    s->max_glyph_w = x_max - x_min;
-
-    /* compute and save position for each glyph */
-    glyph = NULL;
-    for (i = 0, p = text; *p; i++) {
-        GET_UTF8(code, *p ? *p++ : 0, code = 0xfffd; goto continue_on_invalid2;);
-continue_on_invalid2:
-
-        /* skip the \n in the sequence \r\n */
-        if (prev_code == '\r' && code == '\n')
-            continue;
-
-        prev_code = code;
-        if (is_newline(code)) {
-
-            max_text_line_w = FFMAX(max_text_line_w, x);
-            y += s->max_glyph_h + s->line_spacing;
-            x = 0;
-            continue;
-        }
-
-        /* get glyph */
-        prev_glyph = glyph;
-        dummy.code = code;
-        dummy.fontsize = s->fontsize;
-        glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
-
-        /* kerning */
-        if (s->use_kerning && prev_glyph && glyph->code) {
-            FT_Get_Kerning(s->face, prev_glyph->code, glyph->code,
-                           ft_kerning_default, &delta);
-            x += delta.x >> 6;
-        }
 
-        /* save position */
-        s->positions[i].x = x + glyph->bitmap_left;
-        s->positions[i].y = y - glyph->bitmap_top + y_max;
-        if (code == '\t') x  = (x / s->tabsize + 1)*s->tabsize;
-        else              x += glyph->advance;
+    if ((ret = measure_text(ctx, &metrics)) < 0) {
+        return ret;
     }
 
-    max_text_line_w = FFMAX(x, max_text_line_w);
+    s->max_glyph_h = POS_CEIL(metrics.max_y64 - metrics.min_y64, 64);
+    s->max_glyph_w = POS_CEIL(metrics.max_x64 - metrics.min_x64, 64);
 
-    s->var_values[VAR_TW] = s->var_values[VAR_TEXT_W] = max_text_line_w;
-    s->var_values[VAR_TH] = s->var_values[VAR_TEXT_H] = y + s->max_glyph_h;
+    s->var_values[VAR_TW] = s->var_values[VAR_TEXT_W] = metrics.width;
+    s->var_values[VAR_TH] = s->var_values[VAR_TEXT_H] = metrics.height;
 
     s->var_values[VAR_MAX_GLYPH_W] = s->max_glyph_w;
     s->var_values[VAR_MAX_GLYPH_H] = s->max_glyph_h;
-    s->var_values[VAR_MAX_GLYPH_A] = s->var_values[VAR_ASCENT ] = y_max;
-    s->var_values[VAR_MAX_GLYPH_D] = s->var_values[VAR_DESCENT] = y_min;
+    s->var_values[VAR_MAX_GLYPH_A] = s->var_values[VAR_ASCENT] = POS_CEIL(metrics.max_y64, 64);
+    s->var_values[VAR_MAX_GLYPH_D] = s->var_values[VAR_DESCENT] = POS_CEIL(metrics.min_y64, 64);
 
-    s->var_values[VAR_LINE_H] = s->var_values[VAR_LH] = s->max_glyph_h;
+    s->var_values[VAR_LINE_H] = s->var_values[VAR_LH] = metrics.line_height64 / 64.;
 
     if (s->text_source == AV_FRAME_DATA_DETECTION_BBOXES) {
         s->var_values[VAR_X] = s->x;
@@ -1566,56 +1795,139 @@  continue_on_invalid2:
     update_color_with_alpha(s, &bordercolor, s->bordercolor);
     update_color_with_alpha(s, &boxcolor   , s->boxcolor   );
 
-    box_w = max_text_line_w;
-    box_h = y + s->max_glyph_h;
+    if (s->draw_box && s->boxborderw) {
+        s->bb_top = s->bb_right = s->bb_bottom = s->bb_left = s->boxborderw;
+    } else {
+        s->bb_top = s->bb_right = s->bb_bottom = s->bb_left = 0;
+    }
 
     if (s->fix_bounds) {
-
         /* calculate footprint of text effects */
-        int boxoffset     = s->draw_box ? FFMAX(s->boxborderw, 0) : 0;
         int borderoffset  = s->borderw  ? FFMAX(s->borderw, 0) : 0;
 
-        int offsetleft = FFMAX3(boxoffset, borderoffset,
+        int offsetleft = FFMAX3(FFMAX(s->bb_left, 0), borderoffset,
                                 (s->shadowx < 0 ? FFABS(s->shadowx) : 0));
-        int offsettop = FFMAX3(boxoffset, borderoffset,
+        int offsettop = FFMAX3(FFMAX(s->bb_top, 0), borderoffset,
                                 (s->shadowy < 0 ? FFABS(s->shadowy) : 0));
-
-        int offsetright = FFMAX3(boxoffset, borderoffset,
+        int offsetright = FFMAX3(FFMAX(s->bb_right, 0), borderoffset,
                                  (s->shadowx > 0 ? s->shadowx : 0));
-        int offsetbottom = FFMAX3(boxoffset, borderoffset,
+        int offsetbottom = FFMAX3(FFMAX(s->bb_bottom, 0), borderoffset,
                                   (s->shadowy > 0 ? s->shadowy : 0));
 
-
         if (s->x - offsetleft < 0) s->x = offsetleft;
         if (s->y - offsettop < 0)  s->y = offsettop;
 
-        if (s->x + box_w + offsetright > width)
-            s->x = FFMAX(width - box_w - offsetright, 0);
-        if (s->y + box_h + offsetbottom > height)
-            s->y = FFMAX(height - box_h - offsetbottom, 0);
+        if (s->x + metrics.width + offsetright > width)
+            s->x = FFMAX(width - metrics.width - offsetright, 0);
+        if (s->y + metrics.height + offsetbottom > height)
+            s->y = FFMAX(height - metrics.height - offsetbottom, 0);
     }
 
-    /* draw box */
-    if (s->draw_box)
-        ff_blend_rectangle(&s->dc, &boxcolor,
-                           frame->data, frame->linesize, width, height,
-                           s->x - s->boxborderw, s->y - s->boxborderw,
-                           box_w + s->boxborderw * 2, box_h + s->boxborderw * 2);
+    x = 0;
+    y = 0;
+    x64 = (int)(s->x * 64.);
+    y64 = (int)(s->y * 64. + metrics.offset_top64);
+
+    for (int l = 0; l < s->line_count; ++l) {
+        TextLine *line = &s->lines[l];
+        HarfbuzzData *hb = &line->hb_data;
+        line->glyphs = av_mallocz(hb->glyph_count * sizeof(GlyphInfo));
+
+        for (int t = 0; t < hb->glyph_count; ++t) {
+            GlyphInfo *g_info = &line->glyphs[t];
+            uint8_t is_tab = last_tab_idx < s->tab_count &&
+                hb->glyph_info[t].cluster == s->tab_clusters[last_tab_idx] - line->cluster_offset;
+            int true_x, true_y;
+            if (is_tab) {
+                ++last_tab_idx;
+            }
+            true_x = x + hb->glyph_pos[t].x_offset;
+            true_y = y + hb->glyph_pos[t].y_offset;
+            shift_x64 = (((x64 + true_x) >> 4) & 0b0011) << 4;
+            shift_y64 = ((4 - (((y64 + true_y) >> 4) & 0b0011)) & 0b0011) << 4;
+
+            ret = load_glyph(ctx, &glyph, hb->glyph_info[t].codepoint, shift_x64, shift_y64);
+            if (ret != 0) {
+                return ret;
+            }
+            g_info->code = hb->glyph_info[t].codepoint;
+            g_info->x = (x64 + true_x) >> 6;
+            g_info->y = ((y64 + true_y) >> 6) + (shift_y64 > 0 ? 1 : 0);
+            g_info->shift_x64 = shift_x64;
+            g_info->shift_y64 = shift_y64;
+
+            if (!is_tab) {
+                x += hb->glyph_pos[t].x_advance;
+            } else {
+                int size = s->blank_advance64 * s->tabsize;
+                x = (x / size + 1) * size;
+            }
+            y += hb->glyph_pos[t].y_advance;
+        }
 
-    if (s->shadowx || s->shadowy) {
-        if ((ret = draw_glyphs(s, frame, width, height,
-                               &shadowcolor, s->shadowx, s->shadowy, 0)) < 0)
-            return ret;
+        y += metrics.line_height64 + s->line_spacing * 64;
+        x = 0;
     }
 
-    if (s->borderw) {
-        if ((ret = draw_glyphs(s, frame, width, height,
-                               &bordercolor, 0, 0, s->borderw)) < 0)
+    metrics.rect_x = s->x;
+    metrics.rect_y = s->y;
+
+    if (!s->draw_box) {
+        // Create a border for the clipping region to take into account subpixel
+        // errors in text measurement and effects.
+        int borderoffset = s->borderw ? FFMAX(s->borderw, 0) : 0;
+        s->bb_left = borderoffset + (s->shadowx < 0 ? FFABS(s->shadowx) : 0) + 1;
+        s->bb_top = borderoffset + (s->shadowy < 0 ? FFABS(s->shadowy) : 0) + 1;
+        s->bb_right = borderoffset + (s->shadowx > 0 ? s->shadowx : 0) + 1;
+        s->bb_bottom = borderoffset + (s->shadowy > 0 ? s->shadowy : 0) + 1;
+    }
+
+    /* Check if the whole box is out of the frame */
+    is_outside = metrics.rect_x - s->bb_left >= width ||
+                    metrics.rect_y - s->bb_top >= height ||
+                    metrics.rect_x + metrics.width + s->bb_right <= 0 ||
+                    metrics.rect_y + metrics.height + s->bb_bottom <= 0;
+
+    if (!is_outside) {
+        /* draw box */
+        if (s->draw_box) {
+            rec_x = metrics.rect_x - s->bb_left;
+            rec_y = metrics.rect_y - s->bb_top;
+            rec_width = metrics.width + s->bb_right + s->bb_left;
+            rec_height = metrics.height + s->bb_bottom + s->bb_top;
+            ff_blend_rectangle(&s->dc, &boxcolor,
+                frame->data, frame->linesize, width, height,
+                rec_x, rec_y, rec_width, rec_height);
+        }
+
+        if (s->shadowx || s->shadowy) {
+            if ((ret = draw_glyphs(s, frame, &shadowcolor, &metrics,
+                    s->shadowx, s->shadowy, s->borderw)) < 0) {
+                return ret;
+            }
+        }
+
+        if (s->borderw) {
+            if ((ret = draw_glyphs(s, frame, &bordercolor, &metrics,
+                    0, 0, s->borderw)) < 0) {
+                return ret;
+            }
+        }
+
+        if ((ret = draw_glyphs(s, frame, &fontcolor, &metrics, 0,
+                0, 0)) < 0) {
             return ret;
+        }
     }
-    if ((ret = draw_glyphs(s, frame, width, height,
-                           &fontcolor, 0, 0, 0)) < 0)
-        return ret;
+
+    // FREE data structures
+    for (int l = 0; l < s->line_count; ++l) {
+        TextLine *line = &s->lines[l];
+        av_freep(&line->glyphs);
+        hb_destroy(&line->hb_data);
+    }
+    av_freep(&s->lines);
+    av_freep(&s->tab_clusters);
 
     return 0;
 }
@@ -1691,14 +2003,9 @@  FF_ENABLE_DEPRECATION_WARNINGS
             s->x = bbox->x;
             s->y = bbox->y - s->fontsize;
         }
-        draw_text(ctx, frame, frame->width, frame->height);
+        draw_text(ctx, frame);
     }
 
-    av_log(ctx, AV_LOG_DEBUG, "n:%d t:%f text_w:%d text_h:%d x:%d y:%d\n",
-           (int)s->var_values[VAR_N], s->var_values[VAR_T],
-           (int)s->var_values[VAR_TEXT_W], (int)s->var_values[VAR_TEXT_H],
-           s->x, s->y);
-
     return ff_filter_frame(outlink, frame);
 }
 
-- 
2.39.1