[FFmpeg-devel] [lavfi][aalib][PATCH] lavfi: Generalization of freetype/fontconfig font loading.

Michael Niedermayer michaelni at gmx.at
Sun Jun 29 18:16:13 CEST 2014


On Sun, Jun 29, 2014 at 07:16:14PM +0400, iamtakingiteasy at eientei.org wrote:
> This is a generalization of freetype/fontconfig API used to
> discover&load fonts in vf_drawtext and yet-to-be-reviewed vf_aa.
> Needed for two more following patches, introducing changes to
> vf_drawtext and new vf_aa ascii-art video filter.

>  Changelog                   |    1 
>  MAINTAINERS                 |    1 
>  libavfilter/Makefile        |    1 
>  libavfilter/freetypeutils.c |  172 ++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/freetypeutils.h |  109 +++++++++++++++++++++++++++
>  libavfilter/version.h       |    2 
>  6 files changed, 285 insertions(+), 1 deletion(-)
> b14edfde101d263e2917dd9369f85a352205606f  0001-lavfi-Generalization-of-freetype-fontconfig-font-loa.patch
> From 2a85d7876ec947d02b1b326efe0bb5a7e484c4fb Mon Sep 17 00:00:00 2001
> From: Alexander Tumin <iamtakingiteasy at eientei.org>
> Date: Sun, 29 Jun 2014 16:41:45 +0400
> Subject: [PATCH] lavfi: Generalization of freetype/fontconfig font loading.
> 
> Introduced new freetypeutils.h/c helper for font loading and discovery
> for video filters.
> ---
>  Changelog                   |   1 +
>  MAINTAINERS                 |   1 +
>  libavfilter/Makefile        |   1 +
>  libavfilter/freetypeutils.c | 172 ++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/freetypeutils.h | 109 ++++++++++++++++++++++++++++
>  libavfilter/version.h       |   2 +-
>  6 files changed, 285 insertions(+), 1 deletion(-)
>  create mode 100644 libavfilter/freetypeutils.c
>  create mode 100644 libavfilter/freetypeutils.h
> 
> diff --git a/Changelog b/Changelog
> index 0346877..e51a93a 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -30,6 +30,7 @@ version <next>:
>  - zoompan filter
>  - signalstats filter
>  - hqx filter (hq2x, hq3x, hq4x)
> +- Freetypeutils helper for filters
>  
>  
>  version 2.2:
> diff --git a/MAINTAINERS b/MAINTAINERS
> index d06030b..5f09c9b 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -325,6 +325,7 @@ libavfilter
>  ===========
>  
>  Generic parts:
> +  freetypeutils.c                       Alexander Tumin
>    graphdump.c                           Nicolas George
>  
>  Filters:
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 08817ee..a544ca6 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -122,6 +122,7 @@ OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
>  OBJS-$(CONFIG_FRAMESTEP_FILTER)              += vf_framestep.o
>  OBJS-$(CONFIG_FPS_FILTER)                    += vf_fps.o
>  OBJS-$(CONFIG_FRAMEPACK_FILTER)              += vf_framepack.o
> +OBJS-$(CONFIG_LIBFREETYPE)                   += freetypeutils.o
>  OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
>  OBJS-$(CONFIG_GEQ_FILTER)                    += vf_geq.o
>  OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
> diff --git a/libavfilter/freetypeutils.c b/libavfilter/freetypeutils.c
> new file mode 100644
> index 0000000..d15a0dc
> --- /dev/null
> +++ b/libavfilter/freetypeutils.c
> @@ -0,0 +1,172 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "freetypeutils.h"
> +
> +int ff_freetype_init(FFFreetypeContext *ctx, void *avctx)
> +{
> +    int err;
> +
> +    if (!ctx || !avctx) {
> +        return AVERROR(EINVAL);
> +    }
> +
> +    ctx->avctx = avctx;
> +
> +    if ((err = FT_Init_FreeType(&ctx->ft_library))) {
> +        av_log(ctx->avctx, AV_LOG_ERROR, "Could not initialize FreeType2: %s\n", FT_ERRMSG(err));
> +        return AVERROR(ENOMEM);
> +    }
> +
> +    if (CONFIG_LIBFONTCONFIG) {
> +        ctx->fc_library = FcInitLoadConfigAndFonts();
> +        if (!ctx->fc_library) {
> +            av_log(ctx->avctx, AV_LOG_ERROR, "Could not initialize fontconfig\n");
> +            return AVERROR(ENOMEM);
> +        }
> +    } else {
> +        ctx->fc_library = NULL;
> +    }
> +
> +    return 0;
> +}
> +
> +static int ff_freetype_load_file(FFFreetypeContext *ctx, FT_Face *face, uint8_t *filepath, int index, int logfail)
> +{
> +    int err;
> +    if ((err = FT_New_Face(ctx->ft_library, filepath, index, face))) {
> +        if (logfail) {
> +            av_log(ctx->avctx, AV_LOG_ERROR, "Could not load font \"%s\": %s\n", filepath, FT_ERRMSG(err));
> +        }
> +        return AVERROR(EINVAL);
> +    }
> +
> +    av_log(ctx->avctx, AV_LOG_INFO, "Loaded font %s\n", filepath);
> +
> +    return 0;
> +}
> +
> +#if CONFIG_LIBFONTCONFIG
> +static int ff_freetype_load_fontconfig(FFFreetypeContext *ctx, FT_Face *face, uint8_t *selector, int index)
> +{
> +    FcPattern *pattern = NULL, *bestmatch = NULL;
> +    FcResult res = FcResultMatch;
> +    unsigned char *filepath = 0;
> +    int err;
> +
> +    pattern = FcNameParse(selector);
> +
> +    if (!pattern) {
> +        av_log(ctx->avctx, AV_LOG_ERROR, "Could not parse fontconfig pattern\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    FcDefaultSubstitute(pattern);
> +
> +    if (!FcConfigSubstitute(ctx->fc_library, pattern, FcMatchPattern)) {
> +        av_log(ctx->avctx, AV_LOG_ERROR, "Could not perform config substitution for pattern\n");
> +        err = AVERROR(ENOMEM);
> +        goto fontconfig_cleanup;
> +    }
> +
> +    bestmatch = FcFontMatch(ctx->fc_library, pattern, &res);
> +
> +    if (!bestmatch || res != FcResultMatch) {
> +        av_log(ctx->avctx, AV_LOG_ERROR, "Could not find any matches for given pattern.\n");
> +        err = AVERROR(ENOENT);
> +        goto fontconfig_cleanup;
> +    }
> +
> +    if (index < 0) {
> +        if (FcPatternGetInteger(bestmatch, FC_INDEX, 0, &index) != FcResultMatch) {
> +            av_log(ctx->avctx, AV_LOG_ERROR, "Could not find font matching given pattern\n");
> +            err = AVERROR(EINVAL);
> +            goto fontconfig_cleanup;
> +        }
> +    }
> +    if (FcPatternGetString(bestmatch, FC_FILE, 0, &filepath) != FcResultMatch) {
> +        av_log(ctx->avctx, AV_LOG_ERROR, "Could not find font matching given pattern\n");
> +        err =AVERROR(EINVAL);
> +        goto fontconfig_cleanup;
> +    }
> +
> +    err = ff_freetype_load_file(ctx, face, filepath, index, 1);
> +
> +fontconfig_cleanup:
> +    if (pattern) {
> +        FcPatternDestroy(pattern);
> +    }
> +
> +    if (bestmatch) {
> +        FcPatternDestroy(bestmatch);
> +    }
> +
> +    return err;
> +}
> +#endif
> +
> +int ff_freetype_load(FFFreetypeContext *ctx, FT_Face *face, uint8_t *selector, int index)
> +{
> +    int err;
> +
> +    if (!ctx || !face || !selector) {
> +        return AVERROR(EINVAL);
> +    }
> +
> +    err = ff_freetype_load_file(ctx, face, selector, (index < 0) ? 0 : index, !CONFIG_LIBFONTCONFIG);
> +
> +    if (!err) {
> +        return 0;
> +    }
> +
> +#if CONFIG_LIBFONTCONFIG
> +    err = ff_freetype_load_fontconfig(ctx, face, selector, index);
> +#endif
> +
> +    return err;
> +}
> +
> +int ff_freetype_stroker_new(FFFreetypeContext *ctx, FT_Stroker *stroker)
> +{
> +    int err;
> +
> +    if (!ctx || !stroker) {
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if ((err = FT_Stroker_New(ctx->ft_library, stroker))) {
> +        av_log(ctx->avctx, AV_LOG_ERROR, "Could not create new stroker: %s\n", FT_ERRMSG(err));
> +        return AVERROR_EXTERNAL;
> +    }
> +
> +    return 0;
> +}
> +
> +void ff_freetype_uninit(FFFreetypeContext *ctx)
> +{
> +    if (!ctx) {
> +        return;
> +    }
> +
> +    FT_Done_FreeType(ctx->ft_library);
> +
> +    if (CONFIG_LIBFONTCONFIG && ctx->fc_library) {
> +        FcConfigDestroy(ctx->fc_library);
> +    }
> +}
> +
> diff --git a/libavfilter/freetypeutils.h b/libavfilter/freetypeutils.h
> new file mode 100644
> index 0000000..def4795
> --- /dev/null
> +++ b/libavfilter/freetypeutils.h
> @@ -0,0 +1,109 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#ifndef AVFILTER_FREETYPEUTILS_H
> +#define AVFILTER_FREETYPEUTILS_H
> +
> +/**
> + * @file
> + * FreeType2/Fontconfig font loading utilities
> + */
> +
> +#include "config.h"
> +
> +#include "internal.h"
> +
> +#include <ft2build.h>
> +#include FT_FREETYPE_H
> +#include FT_GLYPH_H
> +#include FT_STROKER_H
> +
> +#undef __FTERRORS_H__
> +#define FT_ERROR_START_LIST {
> +#define FT_ERRORDEF(e, v, s) { (e), (s) },
> +#define FT_ERROR_END_LIST { 0, NULL } };
> +
> +struct ft_error {
> +    int err;
> +    const char *err_msg;
> +} static ft_errors[] =
> +#include FT_ERRORS_H
> +
> +#define FT_ERRMSG(e) ft_errors[e].err_msg
> +
> +#if CONFIG_LIBFONTCONFIG
> +#include <fontconfig/fontconfig.h>
> +#define FCTYPE FcConfig
> +#else
> +#define FCTYPE void
> +#endif
> +
> +typedef struct FFFreetypeContext {

> +    void       *avctx;      ///< A pointer to an arbitrary struct of which the first field is a pointer to an AVClass struct.

"avctx" is generally used for AVCodecContext, so this naming could
be confusing

also it fails to build
./configure --enable-libfreetype && make -j12
libavfilter/freetypeutils.c: In function ‘ff_freetype_init’:
libavfilter/freetypeutils.c:37:9: error: implicit declaration of function ‘FcInitLoadConfigAndFonts’ [-Werror=implicit-function-declaration]
libavfilter/freetypeutils.c:37:25: warning: assignment makes pointer from integer without a cast [enabled by default]
libavfilter/freetypeutils.c: In function ‘ff_freetype_uninit’:
libavfilter/freetypeutils.c:169:9: error: implicit declaration of function ‘FcConfigDestroy’ [-Werror=implicit-function-declaration]

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

In a rich man's house there is no place to spit but his face.
-- Diogenes of Sinope
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140629/28673739/attachment.asc>


More information about the ffmpeg-devel mailing list