/*****************************************************************************
- * subsdec.c : text subtitles decoder
+ * subsdec.c : text subtitle decoder
*****************************************************************************
- * Copyright (C) 2000-2006 the VideoLAN team
+ * Copyright (C) 2000-2006 VLC authors and VideoLAN
* $Id$
*
* Authors: Gildas Bazin <gbazin@videolan.org>
* Derk-Jan Hartman <hartman at videolan dot org>
* Bernie Purcell <bitmap@videolan.org>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
# include "config.h"
#endif
+#include <limits.h>
+
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>
static const char *const ppsz_justification_text[] = {
N_("Center"),N_("Left"),N_("Right")};
-#define ENCODING_TEXT N_("Subtitles text encoding")
+#define ENCODING_TEXT N_("Subtitle text encoding")
#define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
-#define ALIGN_TEXT N_("Subtitles justification")
+#define ALIGN_TEXT N_("Subtitle justification")
#define ALIGN_LONGTEXT N_("Set the justification of subtitles")
-#define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
+#define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitle autodetection")
#define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
- "UTF-8 encoding within subtitles files.")
+ "UTF-8 encoding within subtitle files.")
#define FORMAT_TEXT N_("Formatted Subtitles")
#define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
"VLC partly implements this, but you can choose to disable all formatting.")
vlc_module_begin ()
set_shortname( N_("Subtitles"))
- set_description( N_("Text subtitles decoder") )
+ set_description( N_("Text subtitle decoder") )
set_capability( "decoder", 50 )
set_callbacks( OpenDecoder, CloseDecoder )
set_category( CAT_INPUT )
encoding);
}
else
- /* Second, try configured encoding */
- if ((var = var_InheritString (p_dec, "subsdec-encoding")) != NULL)
{
- msg_Dbg (p_dec, "trying configured character encoding: %s", var);
- if (!strcmp (var, "system"))
+ /* Second, try configured encoding */
+ if ((var = var_InheritString (p_dec, "subsdec-encoding")) != NULL)
{
- free (var);
- var = NULL;
- encoding = "";
- /* ^ iconv() treats "" as nl_langinfo(CODESET) */
+ msg_Dbg (p_dec, "trying configured character encoding: %s", var);
+ if (!strcmp (var, "system"))
+ {
+ free (var);
+ var = NULL;
+ encoding = "";
+ /* ^ iconv() treats "" as nl_langinfo(CODESET) */
+ }
+ else
+ encoding = var;
}
else
- encoding = var;
- }
- else
- /* Third, try "local" encoding with optional UTF-8 autodetection */
- {
+ /* Third, try "local" encoding */
+ {
/* xgettext:
The Windows ANSI code page most commonly used for this language.
VLC uses this as a guess of the subtitle files character set
This MUST be a valid iconv character set. If unsure, please refer
the VideoLAN translators mailing list. */
- encoding = vlc_pgettext("GetACP", "CP1252");
- msg_Dbg (p_dec, "trying default character encoding: %s", encoding);
+ encoding = vlc_pgettext("GetACP", "CP1252");
+ msg_Dbg (p_dec, "trying default character encoding: %s", encoding);
+ }
+
+ /* Check UTF-8 autodetection */
if (var_InheritBool (p_dec, "subsdec-autodetect-utf8"))
{
msg_Dbg (p_dec, "using automatic UTF-8 detection");
* returned, and the rendering engine will fall back to the
* plain text version of the subtitle.
*/
+/* TODO: highly suboptimal, offset should be cached */
static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
{
- const int i_len = strlen(psz_text);
+ char *psz_html = *ppsz_html;
+ if( psz_html == NULL )
+ return;
- strncpy( *ppsz_html, psz_text, i_max );
- *ppsz_html += __MIN(i_max,i_len);
+ const size_t i_offset = strlen(psz_html);
+ const size_t i_len = strnlen(psz_text, i_max);
+
+ psz_html = realloc( psz_html, i_offset + i_len + 1 );
+ if( psz_html != NULL )
+ {
+ memcpy( psz_html + i_offset, psz_text, i_len );
+ psz_html[i_offset + i_len] = '\0';
+ }
+ else
+ free( *ppsz_html );
+ *ppsz_html = psz_html;
}
static void HtmlPut( char **ppsz_html, const char *psz_text )
{
- strcpy( *ppsz_html, psz_text );
- *ppsz_html += strlen(psz_text);
+ HtmlNPut( ppsz_html, psz_text, INT_MAX );
}
+
static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
{
HtmlPut( ppsz_html, psz_text );
static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
{
- /* */
- char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
- if( !psz_tag )
+ char *psz_tag = malloc( 1 );
+ if( psz_tag == NULL )
return NULL;
- psz_tag[ 0 ] = '\0';
- /* */
- //Oo + 100 ???
- size_t i_buf_size = strlen( psz_subtitle ) + 100;
- char *psz_html_start = malloc( i_buf_size );
- char *psz_html = psz_html_start;
- if( psz_html_start == NULL )
+ char *psz_html = malloc( 1 );
+ if( psz_html == NULL )
{
free( psz_tag );
return NULL;
}
+ psz_tag[0] = '\0';
psz_html[0] = '\0';
bool b_has_align = false;
else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
{
HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
- strcat( psz_tag, "b" );
+ HtmlPut( &psz_tag, "b" );
}
else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
{
HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
- strcat( psz_tag, "i" );
+ HtmlPut( &psz_tag, "i" );
}
else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
{
HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
- strcat( psz_tag, "u" );
+ HtmlPut( &psz_tag, "u" );
}
else if( !strncasecmp( psz_subtitle, "<s>", 3 ) )
{
HtmlCopy( &psz_html, &psz_subtitle, "<s>" );
- strcat( psz_tag, "s" );
+ HtmlPut( &psz_tag, "s" );
}
else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
{
"alpha=", NULL };
HtmlCopy( &psz_html, &psz_subtitle, "<font " );
- strcat( psz_tag, "f" );
+ HtmlPut( &psz_tag, "f" );
while( *psz_subtitle != '>' )
{
int k;
+ /* <font color= */
+ while (*psz_subtitle == ' ')
+ psz_subtitle++;
+
for( k=0; psz_attribs[ k ]; k++ )
{
int i_len = strlen( psz_attribs[ k ] );
HtmlPut( &psz_html, psz_attribs[k] );
psz_subtitle += i_len;
+ /* <font color= red */
+ while (*psz_subtitle == ' ')
+ psz_subtitle++;
+
/* */
if( *psz_subtitle == '"' )
{
}
/* Not a tag, something else we do not understand */
if( i_len == 0 )
- *psz_subtitle++;
+ psz_subtitle++;
psz_subtitle += i_len;
}
- while (*psz_subtitle == ' ')
- *psz_html++ = *psz_subtitle++;
+
+ HtmlNPut( &psz_html, psz_subtitle, strspn(psz_subtitle, " ") );
}
- *psz_html++ = '>';
- *psz_subtitle++;
+ HtmlPut( &psz_html, ">" );
+ psz_subtitle++;
}
else if( !strncmp( psz_subtitle, "</", 2 ))
{
bool b_match = false;
bool b_ignore = false;
- int i_len = strlen( psz_tag ) - 1;
+ int i_len = (psz_tag ? strlen(psz_tag) : 0) - 1;
char *psz_lastTag = NULL;
if( i_len >= 0 )
if( !b_match )
{
/* Not well formed -- kill everything */
- free( psz_html_start );
- psz_html_start = NULL;
+ free( psz_html );
+ psz_html = NULL;
break;
}
*psz_lastTag = '\0';
{
/* We have the closing tag, ignore it TODO */
psz_subtitle = &psz_stop[1];
- strcat( psz_tag, "I" );
+ HtmlPut( &psz_tag, "I" );
}
else
{
else if( *psz_subtitle == '>' )
HtmlPut( &psz_html, ">" );
else
- *psz_html++ = *psz_subtitle;
+ HtmlNPut( &psz_html, psz_subtitle, 1 );
}
}
}
if( psz_subtitle[3] == 'i' )
{
HtmlPut( &psz_html, "<i>" );
- strcat( psz_tag, "i" );
+ HtmlPut( &psz_tag, "i" );
}
if( psz_subtitle[3] == 'b' )
{
HtmlPut( &psz_html, "<b>" );
- strcat( psz_tag, "b" );
+ HtmlPut( &psz_tag, "b" );
}
if( psz_subtitle[3] == 'u' )
{
HtmlPut( &psz_html, "<u>" );
- strcat( psz_tag, "u" );
+ HtmlPut( &psz_tag, "u" );
}
psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
}
}
else
{
- *psz_html = *psz_subtitle;
- if( psz_html > psz_html_start )
+ HtmlNPut( &psz_html, psz_subtitle, 1 );
+#if 0
+ if( *psz_html )
{
/* Check for double whitespace */
+# error This test does not make sense.
if( ( *psz_html == ' ' || *psz_html == '\t' ) &&
( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
{
psz_html--;
}
}
- psz_html++;
+#endif
psz_subtitle++;
}
+ }
- if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
+ while( psz_tag && *psz_tag )
+ {
+ /* */
+ char *psz_last = &psz_tag[strlen(psz_tag)-1];
+ switch( *psz_last )
{
- const int i_len = psz_html - psz_html_start;
-
- i_buf_size += 200;
- char *psz_new = realloc( psz_html_start, i_buf_size );
- if( !psz_new )
+ case 'b':
+ HtmlPut( &psz_html, "</b>" );
+ break;
+ case 'i':
+ HtmlPut( &psz_html, "</i>" );
+ break;
+ case 'u':
+ HtmlPut( &psz_html, "</u>" );
+ break;
+ case 's':
+ HtmlPut( &psz_html, "</s>" );
+ break;
+ case 'f':
+ HtmlPut( &psz_html, "</font>" );
+ break;
+ case 'I':
break;
- psz_html_start = psz_new;
- psz_html = &psz_new[i_len];
}
+ *psz_last = '\0';
}
- if( psz_html_start )
- {
- static const char *psz_text_close = "</text>";
- static const char *psz_tag_long = "/font>";
-
- /* Realloc for closing tags and shrink memory */
- const size_t i_length = (size_t)( psz_html - psz_html_start );
-
- const size_t i_size = i_length + strlen(psz_tag_long) * strlen(psz_tag) + strlen(psz_text_close) + 1;
- char *psz_new = realloc( psz_html_start, i_size );
- if( psz_new )
- {
- psz_html_start = psz_new;
- psz_html = &psz_new[i_length];
+ /* Close not well formed subtitle */
+ HtmlPut( &psz_html, "</text>" );
- /* Close not well formed subtitle */
- while( *psz_tag )
- {
- /* */
- char *psz_last = &psz_tag[strlen(psz_tag)-1];
- switch( *psz_last )
- {
- case 'b':
- HtmlPut( &psz_html, "</b>" );
- break;
- case 'i':
- HtmlPut( &psz_html, "</i>" );
- break;
- case 'u':
- HtmlPut( &psz_html, "</u>" );
- break;
- case 's':
- HtmlPut( &psz_html, "</s>" );
- break;
- case 'f':
- HtmlPut( &psz_html, "/font>" );
- break;
- case 'I':
- break;
- }
-
- *psz_last = '\0';
- }
- HtmlPut( &psz_html, psz_text_close );
- }
- }
free( psz_tag );
- return psz_html_start;
+ return psz_html;
}
-