2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include "ass_library.h"
29 #include "ass_utils.h"
31 static void ass_msg_handler(int level
, const char *fmt
, va_list va
, void *data
)
33 if (level
> MSGL_INFO
)
35 fprintf(stderr
, "[ass] ");
36 vfprintf(stderr
, fmt
, va
);
37 fprintf(stderr
, "\n");
40 ASS_Library
*ass_library_init(void)
42 ASS_Library
* lib
= calloc(1, sizeof(*lib
));
43 lib
->msg_callback
= ass_msg_handler
;
48 void ass_library_done(ASS_Library
*priv
)
51 ass_set_fonts_dir(priv
, NULL
);
52 ass_set_style_overrides(priv
, NULL
);
53 ass_clear_fonts(priv
);
58 void ass_set_fonts_dir(ASS_Library
*priv
, const char *fonts_dir
)
60 free(priv
->fonts_dir
);
62 priv
->fonts_dir
= fonts_dir
? strdup(fonts_dir
) : 0;
65 void ass_set_extract_fonts(ASS_Library
*priv
, int extract
)
67 priv
->extract_fonts
= !!extract
;
70 void ass_set_style_overrides(ASS_Library
*priv
, char **list
)
76 if (priv
->style_overrides
) {
77 for (p
= priv
->style_overrides
; *p
; ++p
)
80 free(priv
->style_overrides
);
81 priv
->style_overrides
= NULL
;
86 for (p
= list
, cnt
= 0; *p
; ++p
, ++cnt
) {
89 priv
->style_overrides
= malloc((cnt
+ 1) * sizeof(char *));
90 for (p
= list
, q
= priv
->style_overrides
; *p
; ++p
, ++q
)
92 priv
->style_overrides
[cnt
] = NULL
;
95 static void grow_array(void **array
, int nelem
, size_t elsize
)
98 *array
= realloc(*array
, (nelem
+ 32) * elsize
);
101 void ass_add_font(ASS_Library
*priv
, char *name
, char *data
, int size
)
103 int idx
= priv
->num_fontdata
;
104 if (!name
|| !data
|| !size
)
106 grow_array((void **) &priv
->fontdata
, priv
->num_fontdata
,
107 sizeof(*priv
->fontdata
));
109 priv
->fontdata
[idx
].name
= strdup(name
);
111 priv
->fontdata
[idx
].data
= malloc(size
);
112 memcpy(priv
->fontdata
[idx
].data
, data
, size
);
114 priv
->fontdata
[idx
].size
= size
;
116 priv
->num_fontdata
++;
119 void ass_clear_fonts(ASS_Library
*priv
)
122 for (i
= 0; i
< priv
->num_fontdata
; ++i
) {
123 free(priv
->fontdata
[i
].name
);
124 free(priv
->fontdata
[i
].data
);
126 free(priv
->fontdata
);
127 priv
->fontdata
= NULL
;
128 priv
->num_fontdata
= 0;
132 * Register a message callback function with libass. Without setting one,
133 * a default handler is used which prints everything with MSGL_INFO or
134 * higher to the standard output.
136 * \param msg_cb the callback function
137 * \param data additional data that will be passed to the callback
139 void ass_set_message_cb(ASS_Library
*priv
,
140 void (*msg_cb
)(int, const char *, va_list, void *),
144 priv
->msg_callback
= msg_cb
;
145 priv
->msg_callback_data
= data
;