sync with en/mplayer.1 r29461
[mplayer/glamo.git] / libass / ass_library.c
blob304f2326de2dab1a0269ba9e8a71cd4defdb468a
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
3 /*
4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
6 * This file is part of libass.
8 * libass is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * libass is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with libass; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "ass.h"
29 #include "ass_library.h"
32 ass_library_t* ass_library_init(void)
34 return calloc(1, sizeof(ass_library_t));
37 void ass_library_done(ass_library_t* priv)
39 if (priv) {
40 ass_set_fonts_dir(priv, NULL);
41 ass_set_style_overrides(priv, NULL);
42 ass_clear_fonts(priv);
43 free(priv);
47 void ass_set_fonts_dir(ass_library_t* priv, const char* fonts_dir)
49 if (priv->fonts_dir)
50 free(priv->fonts_dir);
52 priv->fonts_dir = fonts_dir ? strdup(fonts_dir) : 0;
55 void ass_set_extract_fonts(ass_library_t* priv, int extract)
57 priv->extract_fonts = !!extract;
60 void ass_set_style_overrides(ass_library_t* priv, char** list)
62 char** p;
63 char** q;
64 int cnt;
66 if (priv->style_overrides) {
67 for (p = priv->style_overrides; *p; ++p)
68 free(*p);
69 free(priv->style_overrides);
72 if (!list) return;
74 for (p = list, cnt = 0; *p; ++p, ++cnt) {}
76 priv->style_overrides = malloc((cnt + 1) * sizeof(char*));
77 for (p = list, q = priv->style_overrides; *p; ++p, ++q)
78 *q = strdup(*p);
79 priv->style_overrides[cnt] = NULL;
82 static void grow_array(void **array, int nelem, size_t elsize)
84 if (!(nelem & 31))
85 *array = realloc(*array, (nelem + 32) * elsize);
88 void ass_add_font(ass_library_t* priv, char* name, char* data, int size)
90 int idx = priv->num_fontdata;
91 if (!name || !data || !size)
92 return;
93 grow_array((void**)&priv->fontdata, priv->num_fontdata, sizeof(*priv->fontdata));
95 priv->fontdata[idx].name = strdup(name);
97 priv->fontdata[idx].data = malloc(size);
98 memcpy(priv->fontdata[idx].data, data, size);
100 priv->fontdata[idx].size = size;
102 priv->num_fontdata ++;
105 void ass_clear_fonts(ass_library_t* priv)
107 int i;
108 for (i = 0; i < priv->num_fontdata; ++i) {
109 free(priv->fontdata[i].name);
110 free(priv->fontdata[i].data);
112 free(priv->fontdata);
113 priv->fontdata = NULL;
114 priv->num_fontdata = 0;