diff libass/ass_utils.c @ 20501:30df9a64618a

Copy the following functions to libass to avoid dependency on the rest of mplayer: guess_buffer_cp utf8_get_char blur
author eugeni
date Sun, 29 Oct 2006 15:03:30 +0000
parents fa122b7c71c6
children 5cbf1c33a668
line wrap: on
line diff
--- a/libass/ass_utils.c	Sun Oct 29 14:45:44 2006 +0000
+++ b/libass/ass_utils.c	Sun Oct 29 15:03:30 2006 +0000
@@ -22,9 +22,14 @@
 
 #include <stdlib.h>
 #include <inttypes.h>
+#include <string.h>
 #include <sys/time.h>
 #include <time.h>
 
+#ifdef HAVE_ENCA
+#include <enca.h>
+#endif
+
 #include "mp_msg.h"
 #include "ass_utils.h"
 
@@ -81,3 +86,74 @@
 	return result;
 }
 
+unsigned ass_utf8_get_char(char **str)
+{
+  uint8_t *strp = (uint8_t *)*str;
+  unsigned c = *strp++;
+  unsigned mask = 0x80;
+  int len = -1;
+  while (c & mask) {
+    mask >>= 1;
+    len++;
+  }
+  if (len <= 0 || len > 4)
+    goto no_utf8;
+  c &= mask - 1;
+  while ((*strp & 0xc0) == 0x80) {
+    if (len-- <= 0)
+      goto no_utf8;
+    c = (c << 6) | (*strp++ & 0x3f);
+  }
+  if (len)
+    goto no_utf8;
+  *str = (char *)strp;
+  return c;
+
+no_utf8:
+  strp = (uint8_t *)*str;
+  c = *strp++;
+  *str = (char *)strp;
+  return c;
+}
+
+#ifdef HAVE_ENCA
+void* ass_guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_language, char *fallback)
+{
+    const char **languages;
+    size_t langcnt;
+    EncaAnalyser analyser;
+    EncaEncoding encoding;
+    char *detected_sub_cp = NULL;
+    int i;
+
+    languages = enca_get_languages(&langcnt);
+    mp_msg(MSGT_SUBREADER, MSGL_V, "ENCA supported languages: ");
+    for (i = 0; i < langcnt; i++) {
+	mp_msg(MSGT_SUBREADER, MSGL_V, "%s ", languages[i]);
+    }
+    mp_msg(MSGT_SUBREADER, MSGL_V, "\n");
+    
+    for (i = 0; i < langcnt; i++) {
+	const char *tmp;
+	
+	if (strcasecmp(languages[i], preferred_language) != 0) continue;
+	analyser = enca_analyser_alloc(languages[i]);
+	encoding = enca_analyse_const(analyser, buffer, buflen);
+	tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
+	if (tmp && encoding.charset != ENCA_CS_UNKNOWN) {
+	    detected_sub_cp = strdup(tmp);
+	    mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", tmp);
+	}
+	enca_analyser_free(analyser);
+    }
+    
+    free(languages);
+
+    if (!detected_sub_cp) {
+	detected_sub_cp = strdup(fallback);
+	mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detection failed: fallback to %s\n", fallback);
+    }
+
+    return detected_sub_cp;
+}
+#endif