comparison libass/ass_library.c @ 30200:48d020c5ceca

Update internal libass copy to commit 8db4a5
author greg
date Fri, 08 Jan 2010 18:35:44 +0000
parents 0f1b5b68af32
children ac6e48baa03d
comparison
equal deleted inserted replaced
30199:f9984b2fc1b2 30200:48d020c5ceca
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
3 /* 1 /*
4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> 2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
5 * 3 *
6 * This file is part of libass. 4 * This file is part of libass.
7 * 5 *
22 20
23 #include <inttypes.h> 21 #include <inttypes.h>
24 #include <stdio.h> 22 #include <stdio.h>
25 #include <stdlib.h> 23 #include <stdlib.h>
26 #include <string.h> 24 #include <string.h>
25 #include <stdarg.h>
27 26
28 #include "ass.h" 27 #include "ass.h"
29 #include "ass_library.h" 28 #include "ass_library.h"
29 #include "ass_utils.h"
30 30
31 31 static void ass_msg_handler(int level, const char *fmt, va_list va, void *data)
32 ass_library_t* ass_library_init(void)
33 { 32 {
34 return calloc(1, sizeof(ass_library_t)); 33 if (level > MSGL_INFO)
34 return;
35 fprintf(stderr, "[ass] ");
36 vfprintf(stderr, fmt, va);
37 fprintf(stderr, "\n");
35 } 38 }
36 39
37 void ass_library_done(ass_library_t* priv) 40 ASS_Library *ass_library_init(void)
38 { 41 {
39 if (priv) { 42 ASS_Library* lib = calloc(1, sizeof(*lib));
40 ass_set_fonts_dir(priv, NULL); 43 lib->msg_callback = ass_msg_handler;
41 ass_set_style_overrides(priv, NULL); 44
42 ass_clear_fonts(priv); 45 return lib;
43 free(priv);
44 }
45 } 46 }
46 47
47 void ass_set_fonts_dir(ass_library_t* priv, const char* fonts_dir) 48 void ass_library_done(ASS_Library *priv)
48 { 49 {
49 if (priv->fonts_dir) 50 if (priv) {
50 free(priv->fonts_dir); 51 ass_set_fonts_dir(priv, NULL);
51 52 ass_set_style_overrides(priv, NULL);
52 priv->fonts_dir = fonts_dir ? strdup(fonts_dir) : 0; 53 ass_clear_fonts(priv);
54 free(priv);
55 }
53 } 56 }
54 57
55 void ass_set_extract_fonts(ass_library_t* priv, int extract) 58 void ass_set_fonts_dir(ASS_Library *priv, const char *fonts_dir)
56 { 59 {
57 priv->extract_fonts = !!extract; 60 if (priv->fonts_dir)
61 free(priv->fonts_dir);
62
63 priv->fonts_dir = fonts_dir ? strdup(fonts_dir) : 0;
58 } 64 }
59 65
60 void ass_set_style_overrides(ass_library_t* priv, char** list) 66 void ass_set_extract_fonts(ASS_Library *priv, int extract)
61 { 67 {
62 char** p; 68 priv->extract_fonts = !!extract;
63 char** q; 69 }
64 int cnt;
65 70
66 if (priv->style_overrides) { 71 void ass_set_style_overrides(ASS_Library *priv, char **list)
67 for (p = priv->style_overrides; *p; ++p) 72 {
68 free(*p); 73 char **p;
69 free(priv->style_overrides); 74 char **q;
70 } 75 int cnt;
71 76
72 if (!list) return; 77 if (priv->style_overrides) {
78 for (p = priv->style_overrides; *p; ++p)
79 free(*p);
80 free(priv->style_overrides);
81 }
73 82
74 for (p = list, cnt = 0; *p; ++p, ++cnt) {} 83 if (!list)
84 return;
75 85
76 priv->style_overrides = malloc((cnt + 1) * sizeof(char*)); 86 for (p = list, cnt = 0; *p; ++p, ++cnt) {
77 for (p = list, q = priv->style_overrides; *p; ++p, ++q) 87 }
78 *q = strdup(*p); 88
79 priv->style_overrides[cnt] = NULL; 89 priv->style_overrides = malloc((cnt + 1) * sizeof(char *));
90 for (p = list, q = priv->style_overrides; *p; ++p, ++q)
91 *q = strdup(*p);
92 priv->style_overrides[cnt] = NULL;
80 } 93 }
81 94
82 static void grow_array(void **array, int nelem, size_t elsize) 95 static void grow_array(void **array, int nelem, size_t elsize)
83 { 96 {
84 if (!(nelem & 31)) 97 if (!(nelem & 31))
85 *array = realloc(*array, (nelem + 32) * elsize); 98 *array = realloc(*array, (nelem + 32) * elsize);
86 } 99 }
87 100
88 void ass_add_font(ass_library_t* priv, char* name, char* data, int size) 101 void ass_add_font(ASS_Library *priv, char *name, char *data, int size)
89 { 102 {
90 int idx = priv->num_fontdata; 103 int idx = priv->num_fontdata;
91 if (!name || !data || !size) 104 if (!name || !data || !size)
92 return; 105 return;
93 grow_array((void**)&priv->fontdata, priv->num_fontdata, sizeof(*priv->fontdata)); 106 grow_array((void **) &priv->fontdata, priv->num_fontdata,
107 sizeof(*priv->fontdata));
94 108
95 priv->fontdata[idx].name = strdup(name); 109 priv->fontdata[idx].name = strdup(name);
96 110
97 priv->fontdata[idx].data = malloc(size); 111 priv->fontdata[idx].data = malloc(size);
98 memcpy(priv->fontdata[idx].data, data, size); 112 memcpy(priv->fontdata[idx].data, data, size);
99 113
100 priv->fontdata[idx].size = size; 114 priv->fontdata[idx].size = size;
101 115
102 priv->num_fontdata ++; 116 priv->num_fontdata++;
103 } 117 }
104 118
105 void ass_clear_fonts(ass_library_t* priv) 119 void ass_clear_fonts(ASS_Library *priv)
106 { 120 {
107 int i; 121 int i;
108 for (i = 0; i < priv->num_fontdata; ++i) { 122 for (i = 0; i < priv->num_fontdata; ++i) {
109 free(priv->fontdata[i].name); 123 free(priv->fontdata[i].name);
110 free(priv->fontdata[i].data); 124 free(priv->fontdata[i].data);
111 } 125 }
112 free(priv->fontdata); 126 free(priv->fontdata);
113 priv->fontdata = NULL; 127 priv->fontdata = NULL;
114 priv->num_fontdata = 0; 128 priv->num_fontdata = 0;
115 } 129 }
130
131 /*
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.
135 *
136 * \param msg_cb the callback function
137 * \param data additional data that will be passed to the callback
138 */
139 void ass_set_message_cb(ASS_Library *priv,
140 void (*msg_cb)(int, const char *, va_list, void *),
141 void *data)
142 {
143 if (msg_cb) {
144 priv->msg_callback = msg_cb;
145 priv->msg_callback_data = data;
146 }
147 }