summaryrefslogtreecommitdiff
path: root/libavfilter/avf_showcqt.c
diff options
context:
space:
mode:
authorMuhammad Faiz <mfcc64@gmail.com>2016-10-17 05:59:57 +0700
committerMuhammad Faiz <mfcc64@gmail.com>2016-10-18 00:47:57 +0700
commit923488bdaaabfdf5646944b7bbe94a658771778b (patch)
tree375bbc796f7068491ee125cd63867f05f5f64a73 /libavfilter/avf_showcqt.c
parent92de89d14acbb8160147dc9e7a62c2a4fd6423da (diff)
avfilter/showcqt: add font option
this is fontconfig pattern Signed-off-by: Muhammad Faiz <mfcc64@gmail.com>
Diffstat (limited to 'libavfilter/avf_showcqt.c')
-rw-r--r--libavfilter/avf_showcqt.c85
1 files changed, 78 insertions, 7 deletions
diff --git a/libavfilter/avf_showcqt.c b/libavfilter/avf_showcqt.c
index 7c76b1fb39..37e20b4c47 100644
--- a/libavfilter/avf_showcqt.c
+++ b/libavfilter/avf_showcqt.c
@@ -36,6 +36,10 @@
#include FT_FREETYPE_H
#endif
+#if CONFIG_LIBFONTCONFIG
+#include <fontconfig/fontconfig.h>
+#endif
+
#include "avf_showcqt.h"
#define BASEFREQ 20.01523126408007475
@@ -78,7 +82,8 @@ static const AVOption showcqt_options[] = {
{ "tlength", "set tlength", OFFSET(tlength), AV_OPT_TYPE_STRING, { .str = TLENGTH }, CHAR_MIN, CHAR_MAX, FLAGS },
{ "count", "set transform count", OFFSET(count), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 30, FLAGS },
{ "fcount", "set frequency count", OFFSET(fcount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, FLAGS },
- { "fontfile", "set axis font", OFFSET(fontfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
+ { "fontfile", "set axis font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
+ { "font", "set axis font", OFFSET(font), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
{ "fontcolor", "set font color", OFFSET(fontcolor), AV_OPT_TYPE_STRING, { .str = FONTCOLOR }, CHAR_MIN, CHAR_MAX, FLAGS },
{ "axisfile", "set axis image", OFFSET(axisfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
{ "axis", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
@@ -492,7 +497,7 @@ static int init_axis_color(ShowCQTContext *s, AVFrame *tmp, int half)
return 0;
}
-static int render_freetype(ShowCQTContext *s, AVFrame *tmp)
+static int render_freetype(ShowCQTContext *s, AVFrame *tmp, char *fontfile)
{
#if CONFIG_LIBFREETYPE
const char *str = "EF G A BC D ";
@@ -506,13 +511,13 @@ static int render_freetype(ShowCQTContext *s, AVFrame *tmp)
int non_monospace_warning = 0;
int x;
- if (!s->fontfile)
+ if (!fontfile)
return AVERROR(EINVAL);
if (FT_Init_FreeType(&lib))
goto fail;
- if (FT_New_Face(lib, s->fontfile, 0, &face))
+ if (FT_New_Face(lib, fontfile, 0, &face))
goto fail;
if (FT_Set_Char_Size(face, 16*64, 0, 0, 0))
@@ -565,17 +570,81 @@ static int render_freetype(ShowCQTContext *s, AVFrame *tmp)
return 0;
fail:
- av_log(s->ctx, AV_LOG_WARNING, "error while loading freetype font, using default font instead.\n");
+ av_log(s->ctx, AV_LOG_WARNING, "error while loading freetype font.\n");
FT_Done_Face(face);
FT_Done_FreeType(lib);
return AVERROR(EINVAL);
#else
- if (s->fontfile)
+ if (fontfile)
av_log(s->ctx, AV_LOG_WARNING, "freetype is not available, ignoring fontfile option.\n");
return AVERROR(EINVAL);
#endif
}
+static int render_fontconfig(ShowCQTContext *s, AVFrame *tmp, char* font)
+{
+#if CONFIG_LIBFONTCONFIG
+ FcConfig *fontconfig;
+ FcPattern *pat, *best;
+ FcResult result = FcResultMatch;
+ char *filename;
+ int i, ret;
+
+ if (!font)
+ return AVERROR(EINVAL);
+
+ for (i = 0; font[i]; i++) {
+ if (font[i] == '|')
+ font[i] = ':';
+ }
+
+ if (!(fontconfig = FcInitLoadConfigAndFonts())) {
+ av_log(s->ctx, AV_LOG_ERROR, "impossible to init fontconfig.\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ if (!(pat = FcNameParse((uint8_t *)font))) {
+ av_log(s->ctx, AV_LOG_ERROR, "could not parse fontconfig pat.\n");
+ FcConfigDestroy(fontconfig);
+ return AVERROR(EINVAL);
+ }
+
+ FcDefaultSubstitute(pat);
+
+ if (!FcConfigSubstitute(fontconfig, pat, FcMatchPattern)) {
+ av_log(s->ctx, AV_LOG_ERROR, "could not substitue fontconfig options.\n");
+ FcPatternDestroy(pat);
+ FcConfigDestroy(fontconfig);
+ return AVERROR(ENOMEM);
+ }
+
+ best = FcFontMatch(fontconfig, pat, &result);
+ FcPatternDestroy(pat);
+
+ ret = AVERROR(EINVAL);
+ if (!best || result != FcResultMatch) {
+ av_log(s->ctx, AV_LOG_ERROR, "cannot find a valid font for %s.\n", font);
+ goto fail;
+ }
+
+ if (FcPatternGetString(best, FC_FILE, 0, (FcChar8 **)&filename) != FcResultMatch) {
+ av_log(s->ctx, AV_LOG_ERROR, "no file path for %s\n", font);
+ goto fail;
+ }
+
+ ret = render_freetype(s, tmp, filename);
+
+fail:
+ FcPatternDestroy(best);
+ FcConfigDestroy(fontconfig);
+ return ret;
+#else
+ if (font)
+ av_log(s->ctx, AV_LOG_WARNING, "fontconfig is not available, ignoring font option.\n");
+ return AVERROR(EINVAL);
+#endif
+}
+
static int render_default_font(AVFrame *tmp)
{
const char *str = "EF G A BC D ";
@@ -615,7 +684,9 @@ static int init_axis_from_font(ShowCQTContext *s)
if (!(s->axis_frame = av_frame_alloc()))
goto fail;
- if (render_freetype(s, tmp) < 0 && (default_font = 1, ret = render_default_font(tmp)) < 0)
+ if (render_freetype(s, tmp, s->fontfile) < 0 &&
+ render_fontconfig(s, tmp, s->font) < 0 &&
+ (default_font = 1, ret = render_default_font(tmp)) < 0)
goto fail;
if (default_font)