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