1 /*****************************************************************************
2 * subsdec.c : text subtitle decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Samuel Hocevar <sam@zoy.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * Bernie Purcell <bitmap@videolan.org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
39 #include <vlc_charset.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static const char *const ppsz_encodings[] = {
92 static const char *const ppsz_encoding_names[] = {
94 The character encoding name in parenthesis corresponds to that used for
95 the GetACP translation. "Windows-1252" applies to Western European
96 languages using the Latin alphabet. */
97 N_("Default (Windows-1252)"),
99 N_("Universal (UTF-8)"),
100 N_("Universal (UTF-16)"),
101 N_("Universal (big endian UTF-16)"),
102 N_("Universal (little endian UTF-16)"),
103 N_("Universal, Chinese (GB18030)"),
105 /* ISO 8859 and the likes */
107 N_("Western European (Latin-9)"), /* mostly superset of Latin-1 */
108 N_("Western European (Windows-1252)"),
109 N_("Western European (IBM 00850)"),
111 N_("Eastern European (Latin-2)"),
112 N_("Eastern European (Windows-1250)"),
114 N_("Esperanto (Latin-3)"),
116 N_("Nordic (Latin-6)"), /* Latin 6 supersedes Latin 4 */
118 N_("Cyrillic (Windows-1251)"), /* ISO 8859-5 is not practically used */
119 N_("Russian (KOI8-R)"),
120 N_("Ukrainian (KOI8-U)"),
122 N_("Arabic (ISO 8859-6)"),
123 N_("Arabic (Windows-1256)"),
125 N_("Greek (ISO 8859-7)"),
126 N_("Greek (Windows-1253)"),
128 N_("Hebrew (ISO 8859-8)"),
129 N_("Hebrew (Windows-1255)"),
131 N_("Turkish (ISO 8859-9)"),
132 N_("Turkish (Windows-1254)"),
135 N_("Thai (TIS 620-2533/ISO 8859-11)"),
136 N_("Thai (Windows-874)"),
138 N_("Baltic (Latin-7)"),
139 N_("Baltic (Windows-1257)"),
140 /* 12 -> /dev/null */
142 N_("Celtic (Latin-8)"),
145 N_("South-Eastern European (Latin-10)"),
147 N_("Simplified Chinese (ISO-2022-CN-EXT)"),
148 N_("Simplified Chinese Unix (EUC-CN)"),
149 N_("Japanese (7-bits JIS/ISO-2022-JP-2)"),
150 N_("Japanese Unix (EUC-JP)"),
151 N_("Japanese (Shift JIS)"),
152 N_("Korean (EUC-KR/CP949)"),
153 N_("Korean (ISO-2022-KR)"),
154 N_("Traditional Chinese (Big5)"),
155 N_("Traditional Chinese Unix (EUC-TW)"),
156 N_("Hong-Kong Supplementary (HKSCS)"),
158 N_("Vietnamese (VISCII)"),
159 N_("Vietnamese (Windows-1258)"),
162 static const int pi_justification[] = { 0, 1, 2 };
163 static const char *const ppsz_justification_text[] = {
164 N_("Center"),N_("Left"),N_("Right")};
166 #define ENCODING_TEXT N_("Subtitle text encoding")
167 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
168 #define ALIGN_TEXT N_("Subtitle justification")
169 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
170 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitle autodetection")
171 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
172 "UTF-8 encoding within subtitle files.")
173 #define FORMAT_TEXT N_("Formatted Subtitles")
174 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
175 "VLC partly implements this, but you can choose to disable all formatting.")
177 static int OpenDecoder ( vlc_object_t * );
178 static void CloseDecoder ( vlc_object_t * );
181 set_shortname( N_("Subtitles"))
182 set_description( N_("Text subtitle decoder") )
183 set_capability( "decoder", 50 )
184 set_callbacks( OpenDecoder, CloseDecoder )
185 set_category( CAT_INPUT )
186 set_subcategory( SUBCAT_INPUT_SCODEC )
188 add_integer( "subsdec-align", 0, ALIGN_TEXT, ALIGN_LONGTEXT,
190 change_integer_list( pi_justification, ppsz_justification_text )
191 add_string( "subsdec-encoding", "",
192 ENCODING_TEXT, ENCODING_LONGTEXT, false )
193 change_string_list( ppsz_encodings, ppsz_encoding_names )
194 add_bool( "subsdec-autodetect-utf8", true,
195 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, false )
196 add_bool( "subsdec-formatted", true, FORMAT_TEXT, FORMAT_LONGTEXT,
200 /*****************************************************************************
202 *****************************************************************************/
203 #define NO_BREAKING_SPACE " "
207 int i_align; /* Subtitles alignment on the vout */
209 vlc_iconv_t iconv_handle; /* handle to iconv instance */
210 bool b_autodetect_utf8;
214 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
215 static subpicture_t *ParseText ( decoder_t *, block_t * );
216 static char *StripTags ( char * );
217 static char *CreateHtmlSubtitle( int *pi_align, char * );
219 /*****************************************************************************
220 * OpenDecoder: probe the decoder and return score
221 *****************************************************************************
222 * Tries to launch a decoder and return score so that the interface is able
224 *****************************************************************************/
225 static int OpenDecoder( vlc_object_t *p_this )
227 decoder_t *p_dec = (decoder_t*)p_this;
228 decoder_sys_t *p_sys;
230 switch( p_dec->fmt_in.i_codec )
233 case VLC_CODEC_ITU_T140:
239 p_dec->pf_decode_sub = DecodeBlock;
240 p_dec->fmt_out.i_cat = SPU_ES;
241 p_dec->fmt_out.i_codec = 0;
243 /* Allocate the memory needed to store the decoder's structure */
244 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
250 p_sys->iconv_handle = (vlc_iconv_t)-1;
251 p_sys->b_autodetect_utf8 = false;
253 const char *encoding;
256 /* First try demux-specified encoding */
257 if( p_dec->fmt_in.i_codec == VLC_CODEC_ITU_T140 )
258 encoding = "UTF-8"; /* IUT T.140 is always using UTF-8 */
260 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
262 encoding = p_dec->fmt_in.subs.psz_encoding;
263 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
268 /* Second, try configured encoding */
269 if ((var = var_InheritString (p_dec, "subsdec-encoding")) != NULL)
271 msg_Dbg (p_dec, "trying configured character encoding: %s", var);
272 if (!strcmp (var, "system"))
277 /* ^ iconv() treats "" as nl_langinfo(CODESET) */
283 /* Third, try "local" encoding */
286 The Windows ANSI code page most commonly used for this language.
287 VLC uses this as a guess of the subtitle files character set
288 (if UTF-8 and UTF-16 autodetection fails).
289 Western European languages normally use "CP1252", which is a
290 Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
291 Other scripts use other code pages.
293 This MUST be a valid iconv character set. If unsure, please refer
294 the VideoLAN translators mailing list. */
295 encoding = vlc_pgettext("GetACP", "CP1252");
296 msg_Dbg (p_dec, "trying default character encoding: %s", encoding);
299 /* Check UTF-8 autodetection */
300 if (var_InheritBool (p_dec, "subsdec-autodetect-utf8"))
302 msg_Dbg (p_dec, "using automatic UTF-8 detection");
303 p_sys->b_autodetect_utf8 = true;
307 if (strcasecmp (encoding, "UTF-8") && strcasecmp (encoding, "utf8"))
309 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", encoding);
310 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
311 msg_Err (p_dec, "cannot convert from %s: %m", encoding);
315 p_sys->i_align = var_InheritInteger( p_dec, "subsdec-align" );
320 /****************************************************************************
321 * DecodeBlock: the whole thing
322 ****************************************************************************
323 * This function must be fed with complete subtitles units.
324 ****************************************************************************/
325 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
330 if( !pp_block || *pp_block == NULL )
334 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
336 block_Release( p_block );
340 p_spu = ParseText( p_dec, p_block );
342 block_Release( p_block );
348 /*****************************************************************************
349 * CloseDecoder: clean up the decoder
350 *****************************************************************************/
351 static void CloseDecoder( vlc_object_t *p_this )
353 decoder_t *p_dec = (decoder_t *)p_this;
354 decoder_sys_t *p_sys = p_dec->p_sys;
356 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
357 vlc_iconv_close( p_sys->iconv_handle );
362 /*****************************************************************************
363 * ParseText: parse an text subtitle packet and send it to the video output
364 *****************************************************************************/
365 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
367 decoder_sys_t *p_sys = p_dec->p_sys;
368 subpicture_t *p_spu = NULL;
369 char *psz_subtitle = NULL;
371 /* We cannot display a subpicture with no date */
372 if( p_block->i_pts <= VLC_TS_INVALID )
374 msg_Warn( p_dec, "subtitle without a date" );
378 /* Check validity of packet data */
379 /* An "empty" line containing only \0 can be used to force
380 and ephemer picture from the screen */
381 if( p_block->i_buffer < 1 )
383 msg_Warn( p_dec, "no subtitle data" );
387 /* Should be resiliant against bad subtitles */
388 psz_subtitle = malloc( p_block->i_buffer + 1 );
389 if( psz_subtitle == NULL )
391 memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
392 psz_subtitle[p_block->i_buffer] = '\0';
394 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
396 if (EnsureUTF8( psz_subtitle ) == NULL)
398 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
399 "Try manually setting a character-encoding "
400 "before you open the file." );
406 if( p_sys->b_autodetect_utf8 )
408 if( IsUTF8( psz_subtitle ) == NULL )
410 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
411 "disabling UTF-8 subtitles autodetection" );
412 p_sys->b_autodetect_utf8 = false;
416 if( !p_sys->b_autodetect_utf8 )
418 size_t inbytes_left = strlen( psz_subtitle );
419 size_t outbytes_left = 6 * inbytes_left;
420 char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
421 char *psz_convert_buffer_out = psz_new_subtitle;
422 const char *psz_convert_buffer_in = psz_subtitle;
424 size_t ret = vlc_iconv( p_sys->iconv_handle,
425 &psz_convert_buffer_in, &inbytes_left,
426 &psz_convert_buffer_out, &outbytes_left );
428 *psz_convert_buffer_out++ = '\0';
429 free( psz_subtitle );
431 if( ( ret == (size_t)(-1) ) || inbytes_left )
433 free( psz_new_subtitle );
434 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
435 "Try manually setting a character-encoding "
436 "before you open the file." );
440 psz_subtitle = realloc( psz_new_subtitle,
441 psz_convert_buffer_out - psz_new_subtitle );
443 psz_subtitle = psz_new_subtitle;
447 /* Create the subpicture unit */
448 p_spu = decoder_NewSubpictureText( p_dec );
451 free( psz_subtitle );
454 p_spu->i_start = p_block->i_pts;
455 p_spu->i_stop = p_block->i_pts + p_block->i_length;
456 p_spu->b_ephemer = (p_block->i_length == 0);
457 p_spu->b_absolute = false;
459 subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
461 p_spu_sys->align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
462 p_spu_sys->text = StripTags( psz_subtitle );
463 if( var_InheritBool( p_dec, "subsdec-formatted" ) )
464 p_spu_sys->html = CreateHtmlSubtitle( &p_spu_sys->align, psz_subtitle );
466 free( psz_subtitle );
471 /* Function now handles tags with attribute values, and tries
472 * to deal with &' commands too. It no longer modifies the string
473 * in place, so that the original text can be reused
475 static char *StripTags( char *psz_subtitle )
477 char *psz_text_start;
480 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
481 if( !psz_text_start )
484 while( *psz_subtitle )
486 if( *psz_subtitle == '<' )
488 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
491 psz_subtitle += strcspn( psz_subtitle, ">" );
493 else if( *psz_subtitle == '&' )
495 if( !strncasecmp( psz_subtitle, "<", 4 ))
498 psz_subtitle += strcspn( psz_subtitle, ";" );
500 else if( !strncasecmp( psz_subtitle, ">", 4 ))
503 psz_subtitle += strcspn( psz_subtitle, ";" );
505 else if( !strncasecmp( psz_subtitle, "&", 5 ))
508 psz_subtitle += strcspn( psz_subtitle, ";" );
510 else if( !strncasecmp( psz_subtitle, """, 6 ))
513 psz_subtitle += strcspn( psz_subtitle, ";" );
517 /* Assume it is just a normal ampersand */
523 *psz_text++ = *psz_subtitle;
526 /* Security fix: Account for the case where input ends early */
527 if( *psz_subtitle == '\0' ) break;
532 char *psz = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
533 if( psz ) psz_text_start = psz;
535 return psz_text_start;
538 /* Try to respect any style tags present in the subtitle string. The main
539 * problem here is a lack of adequate specs for the subtitle formats.
540 * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
541 * SAMI has a detailed spec, but extensive rework is needed in the demux
542 * code to prevent all this style information being excised, as it presently
544 * That leaves the others - none of which were (I guess) originally intended
545 * to be carrying style information. Over time people have used them that way.
546 * In the absence of specifications from which to work, the tags supported
547 * have been restricted to the simple set permitted by the USF DTD, ie. :
548 * Basic: <br>, <i>, <b>, <u>, <s>
560 * There is also the further restriction that the subtitle be well-formed
561 * as an XML entity, ie. the HTML sentence:
562 * <b><i>Bold and Italics</b></i>
563 * doesn't qualify because the tags aren't nested one inside the other.
564 * <text> tags are automatically added to the output to ensure
566 * If the text doesn't qualify for any reason, a NULL string is
567 * returned, and the rendering engine will fall back to the
568 * plain text version of the subtitle.
570 /* TODO: highly suboptimal, offset should be cached */
571 static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
573 char *psz_html = *ppsz_html;
574 if( psz_html == NULL )
577 const size_t i_offset = strlen(psz_html);
578 const size_t i_len = strnlen(psz_text, i_max);
580 psz_html = realloc( psz_html, i_offset + i_len + 1 );
581 if( psz_html != NULL )
583 memcpy( psz_html + i_offset, psz_text, i_len );
584 psz_html[i_offset + i_len] = '\0';
588 *ppsz_html = psz_html;
591 static void HtmlPut( char **ppsz_html, const char *psz_text )
593 HtmlNPut( ppsz_html, psz_text, INT_MAX );
596 static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
598 HtmlPut( ppsz_html, psz_text );
599 *ppsz_subtitle += strlen(psz_text);
602 static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
604 char *psz_tag = malloc( 1 );
605 if( psz_tag == NULL )
608 char *psz_html = malloc( 1 );
609 if( psz_html == NULL )
617 bool b_has_align = false;
619 HtmlPut( &psz_html, "<text>" );
622 while( *psz_subtitle )
624 if( *psz_subtitle == '\n' )
626 HtmlPut( &psz_html, "<br/>" );
629 else if( *psz_subtitle == '<' )
631 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
633 HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
635 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
637 HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
638 HtmlPut( &psz_tag, "b" );
640 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
642 HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
643 HtmlPut( &psz_tag, "i" );
645 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
647 HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
648 HtmlPut( &psz_tag, "u" );
650 else if( !strncasecmp( psz_subtitle, "<s>", 3 ) )
652 HtmlCopy( &psz_html, &psz_subtitle, "<s>" );
653 HtmlPut( &psz_tag, "s" );
655 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
657 const char *psz_attribs[] = { "face=", "family=", "size=",
658 "color=", "outline-color=", "shadow-color=",
659 "outline-level=", "shadow-level=", "back-color=",
662 HtmlCopy( &psz_html, &psz_subtitle, "<font " );
663 HtmlPut( &psz_tag, "f" );
665 while( *psz_subtitle != '>' )
670 while (*psz_subtitle == ' ')
673 for( k=0; psz_attribs[ k ]; k++ )
675 int i_len = strlen( psz_attribs[ k ] );
677 if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
680 HtmlPut( &psz_html, psz_attribs[k] );
681 psz_subtitle += i_len;
683 /* <font color= red */
684 while (*psz_subtitle == ' ')
688 if( *psz_subtitle == '"' )
691 i_len = strcspn( psz_subtitle, "\"" );
693 else if( *psz_subtitle == '\'' )
696 i_len = strcspn( psz_subtitle, "'" );
700 i_len = strcspn( psz_subtitle, " \t>" );
702 HtmlPut( &psz_html, "\"" );
703 HtmlNPut( &psz_html, psz_subtitle, i_len );
704 HtmlPut( &psz_html, "\"" );
706 psz_subtitle += i_len;
707 if( *psz_subtitle == '\"' || *psz_subtitle == '\'' )
712 if( psz_attribs[ k ] == NULL )
714 /* Jump over unrecognised tag */
715 int i_len = strcspn( psz_subtitle, "\"" );
716 if( psz_subtitle[i_len] == '\"' )
718 i_len += 1 + strcspn( &psz_subtitle[i_len + 1], "\"" );
719 if( psz_subtitle[i_len] == '\"' )
722 /* Not a tag, something else we do not understand */
726 psz_subtitle += i_len;
729 HtmlNPut( &psz_html, psz_subtitle, strspn(psz_subtitle, " ") );
731 HtmlPut( &psz_html, ">" );
734 else if( !strncmp( psz_subtitle, "</", 2 ))
736 bool b_match = false;
737 bool b_ignore = false;
738 int i_len = (psz_tag ? strlen(psz_tag) : 0) - 1;
739 char *psz_lastTag = NULL;
743 psz_lastTag = psz_tag + i_len;
746 switch( *psz_lastTag )
749 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
753 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
757 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
761 b_match = !strncasecmp( psz_subtitle, "</s>", 4 );
765 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
769 i_len = strcspn( psz_subtitle, ">" );
770 b_match = psz_subtitle[i_len] == '>';
779 /* Not well formed -- kill everything */
786 HtmlNPut( &psz_html, psz_subtitle, i_len );
788 psz_subtitle += i_len;
790 else if( ( psz_subtitle[1] < 'a' || psz_subtitle[1] > 'z' ) &&
791 ( psz_subtitle[1] < 'A' || psz_subtitle[1] > 'Z' ) )
793 /* We have a single < */
794 HtmlPut( &psz_html, "<" );
799 /* We have an unknown tag or a single < */
801 /* Search for the next tag or end of tag or end of string */
802 char *psz_stop = psz_subtitle + 1 + strcspn( &psz_subtitle[1], "<>" );
803 char *psz_closing = strstr( psz_subtitle, "/>" );
805 if( psz_closing && psz_closing < psz_stop )
807 /* We have a self closed tag, remove it */
808 psz_subtitle = &psz_closing[2];
810 else if( *psz_stop == '>' )
814 snprintf( psz_match, sizeof(psz_match), "</%s", &psz_subtitle[1] );
815 psz_match[strcspn( psz_match, " \t>" )] = '\0';
817 if( strstr( psz_subtitle, psz_match ) )
819 /* We have the closing tag, ignore it TODO */
820 psz_subtitle = &psz_stop[1];
821 HtmlPut( &psz_tag, "I" );
825 int i_len = psz_stop + 1 - psz_subtitle;
827 /* Copy the whole data */
828 for( ; i_len > 0; i_len--, psz_subtitle++ )
830 if( *psz_subtitle == '<' )
831 HtmlPut( &psz_html, "<" );
832 else if( *psz_subtitle == '>' )
833 HtmlPut( &psz_html, ">" );
835 HtmlNPut( &psz_html, psz_subtitle, 1 );
841 /* We have a single < */
842 HtmlPut( &psz_html, "<" );
847 else if( *psz_subtitle == '&' )
849 if( !strncasecmp( psz_subtitle, "<", 4 ))
851 HtmlCopy( &psz_html, &psz_subtitle, "<" );
853 else if( !strncasecmp( psz_subtitle, ">", 4 ))
855 HtmlCopy( &psz_html, &psz_subtitle, ">" );
857 else if( !strncasecmp( psz_subtitle, "&", 5 ))
859 HtmlCopy( &psz_html, &psz_subtitle, "&" );
863 HtmlPut( &psz_html, "&" );
867 else if( *psz_subtitle == '>' )
869 HtmlPut( &psz_html, ">" );
872 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
873 strchr( psz_subtitle, '}' ) )
875 /* Check for forced alignment */
877 !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
879 static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
880 static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
881 const int i_id = psz_subtitle[4] - '1';
884 *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
886 /* TODO fr -> rotation */
888 /* Hide {\stupidity} */
889 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
891 else if( psz_subtitle[0] == '{' &&
892 ( psz_subtitle[1] == 'Y' || psz_subtitle[1] == 'y' )
893 && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
895 // FIXME: We don't do difference between Y and y, and we should.
896 if( psz_subtitle[3] == 'i' )
898 HtmlPut( &psz_html, "<i>" );
899 HtmlPut( &psz_tag, "i" );
901 if( psz_subtitle[3] == 'b' )
903 HtmlPut( &psz_html, "<b>" );
904 HtmlPut( &psz_tag, "b" );
906 if( psz_subtitle[3] == 'u' )
908 HtmlPut( &psz_html, "<u>" );
909 HtmlPut( &psz_tag, "u" );
911 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
913 else if( psz_subtitle[0] == '{' && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
915 // Hide other {x:y} atrocities, like {c:$bbggrr} or {P:x}
916 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
918 else if( psz_subtitle[0] == '\\' && psz_subtitle[1] )
920 if( psz_subtitle[1] == 'N' || psz_subtitle[1] == 'n' )
922 HtmlPut( &psz_html, "<br/>" );
925 else if( psz_subtitle[1] == 'h' )
927 /* Non breakable space */
928 HtmlPut( &psz_html, NO_BREAKING_SPACE );
933 HtmlPut( &psz_html, "\\" );
939 HtmlNPut( &psz_html, psz_subtitle, 1 );
943 /* Check for double whitespace */
944 # error This test does not make sense.
945 if( ( *psz_html == ' ' || *psz_html == '\t' ) &&
946 ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
948 HtmlPut( &psz_html, NO_BREAKING_SPACE );
957 while( psz_tag && *psz_tag )
960 char *psz_last = &psz_tag[strlen(psz_tag)-1];
964 HtmlPut( &psz_html, "</b>" );
967 HtmlPut( &psz_html, "</i>" );
970 HtmlPut( &psz_html, "</u>" );
973 HtmlPut( &psz_html, "</s>" );
976 HtmlPut( &psz_html, "</font>" );
983 /* Close not well formed subtitle */
984 HtmlPut( &psz_html, "</text>" );