|
1 /* GStreamer mpl2 format subtitle parser |
|
2 * Copyright (C) 2006 Kamil Pawlowski <kamilpe gmail com> |
|
3 * |
|
4 * This library is free software; you can redistribute it and/or |
|
5 * modify it under the terms of the GNU Library General Public |
|
6 * License as published by the Free Software Foundation; either |
|
7 * version 2 of the License, or (at your option) any later version. |
|
8 * |
|
9 * This library is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 * Library General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Library General Public |
|
15 * License along with this library; if not, write to the |
|
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
17 * Boston, MA 02111-1307, USA. |
|
18 */ |
|
19 |
|
20 #include "mpl2parse.h" |
|
21 |
|
22 #include <string.h> |
|
23 |
|
24 /* From http://lists.mplayerhq.hu/pipermail/mplayer-users/2003-February/030222.html |
|
25 * |
|
26 * [123][456] Sample subtitle |
|
27 * [1234][5678] Line 1|Line 2 |
|
28 * [12345][67890] /Italic|Normal |
|
29 * [12345][67890] /Italic|/Italic |
|
30 * [12345][67890] Normal|/Italic |
|
31 * |
|
32 * (The space between the last ']' bracket and the text appears to be optional) |
|
33 */ |
|
34 |
|
35 static gchar * |
|
36 mpl2_parse_line (ParserState * state, const gchar * line, guint line_num) |
|
37 { |
|
38 GString *markup; |
|
39 gint dc_start, dc_stop; |
|
40 |
|
41 /* parse subtitle file line */ |
|
42 if (sscanf (line, "[%u][%u]", &dc_start, &dc_stop) != 2) { |
|
43 GST_WARNING ("failed to extract timestamps for line '%s'", line); |
|
44 return NULL; |
|
45 } |
|
46 |
|
47 GST_LOG ("line format %u %u", dc_start, dc_stop); |
|
48 state->start_time = GST_SECOND / 10 * dc_start; |
|
49 state->duration = (GST_SECOND / 10 * dc_stop) - state->start_time; |
|
50 |
|
51 /* skip brackets with timestamps */ |
|
52 line = strchr (line, ']') + 1; |
|
53 line = strchr (line, ']') + 1; |
|
54 |
|
55 markup = g_string_new (NULL); |
|
56 |
|
57 while (1) { |
|
58 const gchar *format_string; |
|
59 const gchar *sep; |
|
60 gchar *line_chunk_escaped; |
|
61 |
|
62 /* skip leading white spaces */ |
|
63 while (*line == ' ' || *line == '\t') |
|
64 ++line; |
|
65 |
|
66 /* a '/' at the beginning indicates italics */ |
|
67 if (*line == '/') { |
|
68 format_string = "<i>%s</i>"; |
|
69 ++line; |
|
70 } else { |
|
71 format_string = "%s"; |
|
72 } |
|
73 |
|
74 if ((sep = strchr (line, '|'))) |
|
75 line_chunk_escaped = g_markup_escape_text (line, sep - line); |
|
76 else |
|
77 line_chunk_escaped = g_markup_escape_text (line, -1); |
|
78 |
|
79 GST_LOG ("escaped line: %s", line_chunk_escaped); |
|
80 g_string_append_printf (markup, format_string, line_chunk_escaped); |
|
81 |
|
82 g_free (line_chunk_escaped); |
|
83 |
|
84 if (sep == NULL) |
|
85 break; |
|
86 |
|
87 /* move after the '|' and append another line */ |
|
88 g_string_append (markup, "\n"); |
|
89 line = sep + 1; |
|
90 } |
|
91 |
|
92 return g_strstrip (g_string_free (markup, FALSE)); |
|
93 } |
|
94 #ifdef __SYMBIAN32__ |
|
95 EXPORT_C |
|
96 #endif |
|
97 |
|
98 |
|
99 gchar * |
|
100 parse_mpl2 (ParserState * state, const gchar * line) |
|
101 { |
|
102 gchar *ret; |
|
103 |
|
104 ret = mpl2_parse_line (state, line, state->state); |
|
105 ++state->state; |
|
106 return ret; |
|
107 } |