Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / libass / ass_utils.c
blob59fdbdfb94e4d195d330c3957fe23556d2908a90
1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
6 * libass is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * libass is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with libass; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <inttypes.h>
26 #include <ft2build.h>
27 #include FT_GLYPH_H
29 #include "ass_library.h"
30 #include "ass.h"
31 #include "ass_utils.h"
33 int mystrtoi(char **p, int *res)
35 double temp_res;
36 char *start = *p;
37 temp_res = ass_strtod(*p, p);
38 *res = (int) (temp_res + (temp_res > 0 ? 0.5 : -0.5));
39 if (*p != start)
40 return 1;
41 else
42 return 0;
45 int mystrtoll(char **p, long long *res)
47 double temp_res;
48 char *start = *p;
49 temp_res = ass_strtod(*p, p);
50 *res = (int) (temp_res + (temp_res > 0 ? 0.5 : -0.5));
51 if (*p != start)
52 return 1;
53 else
54 return 0;
57 int mystrtou32(char **p, int base, uint32_t *res)
59 char *start = *p;
60 *res = strtoll(*p, p, base);
61 if (*p != start)
62 return 1;
63 else
64 return 0;
67 int mystrtod(char **p, double *res)
69 char *start = *p;
70 *res = ass_strtod(*p, p);
71 if (*p != start)
72 return 1;
73 else
74 return 0;
77 int strtocolor(ASS_Library *library, char **q, uint32_t *res, int hex)
79 uint32_t color = 0;
80 int result;
81 char *p = *q;
82 int base = hex ? 16 : 10;
84 if (*p == '&')
85 ++p;
86 else
87 ass_msg(library, MSGL_DBG2, "suspicious color format: \"%s\"\n", p);
89 if (*p == 'H' || *p == 'h') {
90 ++p;
91 result = mystrtou32(&p, 16, &color);
92 } else {
93 result = mystrtou32(&p, base, &color);
97 unsigned char *tmp = (unsigned char *) (&color);
98 unsigned char b;
99 b = tmp[0];
100 tmp[0] = tmp[3];
101 tmp[3] = b;
102 b = tmp[1];
103 tmp[1] = tmp[2];
104 tmp[2] = b;
106 if (*p == '&')
107 ++p;
108 *q = p;
110 *res = color;
111 return result;
114 // Return a boolean value for a string
115 char parse_bool(char *str)
117 while (*str == ' ' || *str == '\t')
118 str++;
119 if (!strncasecmp(str, "yes", 3))
120 return 1;
121 else if (strtol(str, NULL, 10) > 0)
122 return 1;
123 return 0;
126 void ass_msg(ASS_Library *priv, int lvl, char *fmt, ...)
128 va_list va;
129 va_start(va, fmt);
130 priv->msg_callback(lvl, fmt, va, priv->msg_callback_data);
131 va_end(va);
134 unsigned ass_utf8_get_char(char **str)
136 uint8_t *strp = (uint8_t *) * str;
137 unsigned c = *strp++;
138 unsigned mask = 0x80;
139 int len = -1;
140 while (c & mask) {
141 mask >>= 1;
142 len++;
144 if (len <= 0 || len > 4)
145 goto no_utf8;
146 c &= mask - 1;
147 while ((*strp & 0xc0) == 0x80) {
148 if (len-- <= 0)
149 goto no_utf8;
150 c = (c << 6) | (*strp++ & 0x3f);
152 if (len)
153 goto no_utf8;
154 *str = (char *) strp;
155 return c;
157 no_utf8:
158 strp = (uint8_t *) * str;
159 c = *strp++;
160 *str = (char *) strp;
161 return c;
164 #ifdef CONFIG_ENCA
165 void *ass_guess_buffer_cp(ASS_Library *library, unsigned char *buffer,
166 int buflen, char *preferred_language,
167 char *fallback)
169 const char **languages;
170 size_t langcnt;
171 EncaAnalyser analyser;
172 EncaEncoding encoding;
173 char *detected_sub_cp = NULL;
174 int i;
176 languages = enca_get_languages(&langcnt);
177 ass_msg(library, MSGL_V, "ENCA supported languages");
178 for (i = 0; i < langcnt; i++) {
179 ass_msg(library, MSGL_V, "lang %s", languages[i]);
182 for (i = 0; i < langcnt; i++) {
183 const char *tmp;
185 if (strcasecmp(languages[i], preferred_language) != 0)
186 continue;
187 analyser = enca_analyser_alloc(languages[i]);
188 encoding = enca_analyse_const(analyser, buffer, buflen);
189 tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
190 if (tmp && encoding.charset != ENCA_CS_UNKNOWN) {
191 detected_sub_cp = strdup(tmp);
192 ass_msg(library, MSGL_INFO, "ENCA detected charset: %s", tmp);
194 enca_analyser_free(analyser);
197 free(languages);
199 if (!detected_sub_cp) {
200 detected_sub_cp = strdup(fallback);
201 ass_msg(library, MSGL_INFO,
202 "ENCA detection failed: fallback to %s", fallback);
205 return detected_sub_cp;
207 #endif