1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3 *
4 * This file is part of libass.
5 *
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.
9 *
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.
17 */
18
19 #include "config.h"
20
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26
27 #include "ass.h"
28 #include "ass_library.h"
29 #include "ass_utils.h"
30
ass_msg_handler(int level,const char * fmt,va_list va,void * data)31 static void ass_msg_handler(int level, const char *fmt, va_list va, void *data)
32 {
33 if (level > MSGL_INFO)
34 return;
35 fprintf(stderr, "[ass] ");
36 vfprintf(stderr, fmt, va);
37 fprintf(stderr, "\n");
38 }
39
ass_library_init(void)40 ASS_Library *ass_library_init(void)
41 {
42 ASS_Library* lib = calloc(1, sizeof(*lib));
43 if (lib)
44 lib->msg_callback = ass_msg_handler;
45 return lib;
46 }
47
ass_library_done(ASS_Library * priv)48 void ass_library_done(ASS_Library *priv)
49 {
50 if (priv) {
51 ass_set_fonts_dir(priv, NULL);
52 ass_set_style_overrides(priv, NULL);
53 ass_clear_fonts(priv);
54 free(priv);
55 }
56 }
57
ass_set_fonts_dir(ASS_Library * priv,const char * fonts_dir)58 void ass_set_fonts_dir(ASS_Library *priv, const char *fonts_dir)
59 {
60 free(priv->fonts_dir);
61
62 priv->fonts_dir = fonts_dir ? strdup(fonts_dir) : 0;
63 }
64
ass_set_extract_fonts(ASS_Library * priv,int extract)65 void ass_set_extract_fonts(ASS_Library *priv, int extract)
66 {
67 priv->extract_fonts = !!extract;
68 }
69
ass_set_style_overrides(ASS_Library * priv,char ** list)70 void ass_set_style_overrides(ASS_Library *priv, char **list)
71 {
72 char **p;
73 char **q;
74 int cnt;
75
76 if (priv->style_overrides) {
77 for (p = priv->style_overrides; *p; ++p)
78 free(*p);
79 }
80 free(priv->style_overrides);
81 priv->style_overrides = NULL;
82
83 if (!list)
84 return;
85
86 for (p = list, cnt = 0; *p; ++p, ++cnt) {
87 }
88
89 priv->style_overrides = calloc(cnt + 1, sizeof(char *));
90 if (!priv->style_overrides)
91 return;
92 for (p = list, q = priv->style_overrides; *p; ++p, ++q)
93 *q = strdup(*p);
94 }
95
grow_array(void ** array,int nelem,size_t elsize)96 static int grow_array(void **array, int nelem, size_t elsize)
97 {
98 if (!(nelem & 31)) {
99 void *ptr = realloc(*array, (nelem + 32) * elsize);
100 if (!ptr)
101 return 0;
102 *array = ptr;
103 }
104 return 1;
105 }
106
ass_add_font(ASS_Library * priv,char * name,char * data,int size)107 void ass_add_font(ASS_Library *priv, char *name, char *data, int size)
108 {
109 int idx = priv->num_fontdata;
110 if (!name || !data || !size)
111 return;
112 if (!grow_array((void **) &priv->fontdata, priv->num_fontdata,
113 sizeof(*priv->fontdata)))
114 return;
115
116 priv->fontdata[idx].name = strdup(name);
117 priv->fontdata[idx].data = malloc(size);
118
119 if (!priv->fontdata[idx].name || !priv->fontdata[idx].data)
120 goto error;
121
122 memcpy(priv->fontdata[idx].data, data, size);
123
124 priv->fontdata[idx].size = size;
125
126 priv->num_fontdata++;
127 return;
128
129 error:
130 free(priv->fontdata[idx].name);
131 free(priv->fontdata[idx].data);
132 }
133
ass_clear_fonts(ASS_Library * priv)134 void ass_clear_fonts(ASS_Library *priv)
135 {
136 int i;
137 for (i = 0; i < priv->num_fontdata; ++i) {
138 free(priv->fontdata[i].name);
139 free(priv->fontdata[i].data);
140 }
141 free(priv->fontdata);
142 priv->fontdata = NULL;
143 priv->num_fontdata = 0;
144 }
145
146 /*
147 * Register a message callback function with libass. Without setting one,
148 * a default handler is used which prints everything with MSGL_INFO or
149 * higher to the standard output.
150 *
151 * \param msg_cb the callback function
152 * \param data additional data that will be passed to the callback
153 */
ass_set_message_cb(ASS_Library * priv,void (* msg_cb)(int,const char *,va_list,void *),void * data)154 void ass_set_message_cb(ASS_Library *priv,
155 void (*msg_cb)(int, const char *, va_list, void *),
156 void *data)
157 {
158 if (msg_cb) {
159 priv->msg_callback = msg_cb;
160 priv->msg_callback_data = data;
161 }
162 }
163