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