source: branches/captions/src/com/longtailvideo/plugins/captions/DFXP.as @ 1808

Revision 1808, 6.0 KB checked in by jeroen, 2 years ago (diff)

added usubs support and up/down scrolling to language selection

Line 
1package com.longtailvideo.plugins.captions {
2
3
4    import com.longtailvideo.jwplayer.utils.Strings;
5
6
7    /** Parse styling and contents of W3C Timed Text XML files. **/
8    public class DFXP {
9
10
11        /** Name of the main XMl node. **/
12        public static const NAME:String = 'tt';
13
14
15        /** Parse stylesheets from the head. */
16        public static function parseStyles(data:XML,defaults:Object):Object {
17            var styles:Object = {};
18            for each (var i:XML in data.children()) {
19                if (i.localName() == "head") {
20                    for each (var node:XML in i.children()[0].children()) {
21                        if (node.localName() == 'style') {
22                            // Set the defaults.
23                            var rules:Object = {};
24                            for(var rule:String in defaults) {
25                                rules[rule] = defaults[rule];
26                            }
27                            // Loop through all attributes for overrides.
28                            for each (var attrib:XML in node.attributes()) {
29                                var name:String = attrib.name();
30                                if (name.indexOf("::") > -1) {
31                                    name = name.substring(name.indexOf("::") + 2);
32                                }
33                                rules[name] = attrib.toString();
34                            }
35                            // Save to listing
36                            if (node.@id) {
37                                styles[node.@id] = rules;
38                            }
39                        }
40                    }
41                }
42            }
43            return styles;
44        };
45
46
47        /** Parse captions from the TT XML, returning a list with {begin:Number,text:String} objects. **/
48        public static function parseCaptions(data:XML,style:Object):Array {
49            var styles:Object = DFXP.parseStyles(data,style);
50            var array:Array = new Array({begin:0,text:''});
51            for each (var i:XML in data.children()) {
52                if (i.localName() == "body") {
53                    for each (var j:XML in i.children()) {
54                        for each (var k:XML in j.children()) {
55                            // Paragraphs are single captions. They live inside dividers.
56                            if (k.localName() == 'p') {
57                                var entry:Object = DFXP.parseCaption(k);
58                                array.push(entry);
59                                // Amend global styling.
60                                if(entry.style && styles[entry.style]) {
61                                    entry.style = styles[entry.style];
62                                } else {
63                                    entry.style = style;
64                                }
65                                // Convert inline styles to HTML.
66                                while (entry.text.indexOf("<span") > -1) {
67                                    entry.text = DFXP.parseSpan(entry.text,styles,entry.style);
68                                }
69                                // End with a new, empty caption, accounting for duration or end set.
70                                if (entry['end']) {
71                                    array.push({begin:entry['end'],text:''});
72                                    delete entry['end'];
73                                } else if (entry['dur']) {
74                                    array.push({begin:entry['begin']+entry['dur'],text:''});
75                                    delete entry['dur'];
76                                }
77                            }
78                        }
79                    }
80                }
81            }
82            return array;
83        };
84
85
86        /** Parse a single captions entry. **/
87        private static function parseCaption(data:XML):Object {
88            var pattern:RegExp = /(\n)+/;
89            var entry:Object = {
90                begin:Strings.seconds(data.@begin),
91                dur:Strings.seconds(data.@dur),
92                end:Strings.seconds(data.@end),
93                style:data.@style.toString(),
94                text:data.children().toXMLString().replace(/\n/g,' ')
95            };
96            return entry;
97        };
98
99
100        /** Convert a span entry into HTML. **/
101        private static function parseSpan(text:String,styles:Object,defaults:Object):String {
102            var rules:Object = {};
103            var newtext:String = '';
104            // Find the span bounds and convert to XML.
105            var left:Number = text.indexOf("<span ");
106            var right:Number = text.indexOf("</span>",left);
107            if (left > -1 && right > -1) {
108                var span:XML = new XML(text.substring(left,right+7));
109                // Use style if defined, else set defaults.
110                var style:String = span.@style;
111                if(style && styles[style]) {
112                    for(var i:String in styles[style]) { rules[i] = styles[style][i]; }
113                } else {
114                    for(var j:String in defaults) { rules[j] = defaults[j]; }
115                }
116                // Override style with inline declarations
117                for each (var attrib:XML in span.@*) {
118                    var name:String = attrib.localName().toString();
119                    if (rules[name]) {
120                        rules[name] = attrib.toString();
121                    }
122                }
123                // Wrap plain text with font and b/i/u tags.
124                newtext = '<font family="'+rules.fontFamily+'" size="'+rules.fontSize+'" color="'+rules.color+'">';
125                newtext +=  text.substring(text.indexOf('>',left)+1, right);
126                newtext += "</font>";
127                if(rules.fontWeight == 'bold') { newtext = '<b>'+newtext+'</b>'; }
128                if(rules.fontStyle == 'italic') { newtext = '<i>'+newtext+'</i>'; }
129                if(rules.textDecoration == 'underline') { newtext = '<u>'+newtext+'</u>'; }
130            }
131            return text.substr(0,left)+newtext+text.substr(right+7);
132        };
133
134
135    }
136
137
138}
Note: See TracBrowser for help on using the repository browser.