This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
forked from cgiffard/Captionator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
captionator-min.js
64 lines (62 loc) · 28.5 KB
/
captionator-min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Captionator 0.5.1 [CaptionCrunch]
Christopher Giffard, 2011
Share and enjoy
https://github.com/cgiffard/Captionator
*/
(function(){var A=10,B=16,D=4.5,E=1.5,I=[0,0,0,0.5],J=false,c={createDOMException:function(b,c,a){try{document.querySelectorAll("div/[]")}catch(d){var e=function(a,b,c){this.code=a;this.message=b;this.name=c};e.prototype=d;return new e(b,c,a)}},compareArray:function(b,c){if(!(b instanceof Array)||!(c instanceof Array))return false;if(b.length!==c.length)return false;for(var a in b)if(b.hasOwnProperty(a)&&b[a]!==c[a])return false;return true},generateID:function(b){for(var c="",b=b?b:10;c.length<b;)c+=
String.fromCharCode(65+Math.floor(Math.random()*26));return"captionator"+c},captionify:function(b,f,a){var d=[],e=0,a=a instanceof Object?a:{};if(a.minimumFontSize&&typeof a.minimumFontSize==="number")A=a.minimumFontSize;if(a.minimumLineHeight&&typeof a.minimumLineHeight==="number")B=a.minimumLineHeight;if(a.fontSizeVerticalPercentage&&typeof a.fontSizeVerticalPercentage==="number")D=a.fontSizeVerticalPercentage;if(a.lineHeightRatio&&typeof a.lineHeightRatio!=="number")E=a.lineHeightRatio;if(a.cueBackgroundColour&&
a.cueBackgroundColour instanceof Array)I=a.cueBackgroundColour;if(HTMLVideoElement){if(typeof document.createElement("video").addTextTrack==="function")return false}else return false;if(!J){c.TextTrack=function(a,b,f,d,e,o){this.onload=function(){};this.onerror=function(){};this.oncuechange=function(){};this.id=a||"";this.internalMode=c.TextTrack.OFF;this.cues=new c.TextTrackCueList(this);this.activeCues=new c.ActiveTextTrackCueList(this.cues,this);this.kind=b||"subtitles";this.label=f||"";this.language=
d||"";this.src=e||"";this.readyState=c.TextTrack.NONE;this.internalDefault=o||false;this.getMode=function(){return this.internalMode};this.setMode=function(a){if([c.TextTrack.OFF,c.TextTrack.HIDDEN,c.TextTrack.SHOWING].indexOf(a)!==-1){if(a!==this.internalMode&&(this.internalMode=a,this.readyState===c.TextTrack.NONE&&this.src.length>0&&a>c.TextTrack.OFF&&this.loadTrack(this.src,null),this.videoNode._captionator_dirtyBit=true,c.rebuildCaptions(this.videoNode),a===c.TextTrack.OFF))this.cues.length=
0,this.readyState=c.TextTrack.NONE}else throw Error("Illegal mode value for track: "+a);};this.getDefault=function(){return this.internalDefault};Object.prototype.__defineGetter__?(this.__defineGetter__("mode",this.getMode),this.__defineSetter__("mode",this.setMode),this.__defineGetter__("default",this.getDefault)):Object.defineProperty&&(Object.defineProperty(this,"mode",{get:this.getMode,set:this.setMode}),Object.defineProperty(this,"default",{get:this.getDefault}));this.loadTrack=function(a,b){var h,
f=new XMLHttpRequest;if(this.readyState===c.TextTrack.LOADED)b instanceof Function&&b(h);else{this.src=a;this.readyState=c.TextTrack.LOADING;var d=this;f.open("GET",a,true);f.onreadystatechange=function(){if(f.readyState===4)if(f.status===200){var a=d.videoNode._captionatorOptions||{};if(d.kind==="metadata")a.processCueHTML=false,a.sanitiseCueHTML=false;h=c.parseCaptions(f.responseText,a);d.readyState=c.TextTrack.LOADED;d.cues.loadCues(h);d.activeCues.refreshCues.apply(d.activeCues);d.videoNode._captionator_dirtyBit=
true;c.rebuildCaptions(d.videoNode);d.onload.call(this);b instanceof Function&&b.call(d,h)}else d.readyState=c.TextTrack.ERROR,d.onerror()};try{f.send(null)}catch(k){d.readyState=c.TextTrack.ERROR,d.onerror(k)}}};this.addCue=function(a){if(a&&a instanceof c.TextTrackCue)this.cues.addCue(a);else throw Error("The argument is null or not an instance of TextTrackCue.");};this.removeCue=function(){}};c.TextTrack.NONE=0;c.TextTrack.LOADING=1;c.TextTrack.LOADED=2;c.TextTrack.ERROR=3;c.TextTrack.OFF=0;c.TextTrack.HIDDEN=
1;c.TextTrack.SHOWING=2;c.TextTrackCueList=function(a){this.track=a instanceof c.TextTrack?a:null;this.getCueById=function(a){return this.filter(function(b){return b.id===a})[0]};this.loadCues=function(a){for(var b=0;b<a.length;b++)a[b].track=this.track,Array.prototype.push.call(this,a[b])};this.addCue=function(a){if(a&&a instanceof c.TextTrackCue)if(a.track===this.track||!a.track)Array.prototype.push.call(this,a);else throw Error("This cue is associated with a different track!");else throw Error("The argument is null or not an instance of TextTrackCue.");
};this.toString=function(){return"[TextTrackCueList]"}};c.TextTrackCueList.prototype=[];c.ActiveTextTrackCueList=function(a,b){this.refreshCues=function(){if(a.length){var c=this,f=false,d=[].slice.call(this,0);this.length=0;a.forEach(function(a){a.active&&(c.push(a),c[c.length-1]!==d[c.length-1]&&(f=true))});if(f)try{b.oncuechange()}catch(e){}}};this.toString=function(){return"[ActiveTextTrackCueList]"};this.refreshCues()};c.ActiveTextTrackCueList.prototype=new c.TextTrackCueList(null);c.TextTrackCue=
function(a,b,f,d,e,o,l){this.id=a;this.track=l instanceof c.TextTrack?l:null;this.startTime=parseFloat(b);this.endTime=parseFloat(f);this.text=typeof d==="string"||d instanceof c.CaptionatorCueStructure?d:"";this.settings=typeof e==="string"?e:"";this.intSettings={};this.pauseOnExit=!!o;this.wasActive=false;this.direction="horizontal";this.snapToLines=true;this.linePosition="auto";this.textPosition=50;this.size=0;this.alignment="middle";if(this.settings.length){var q=this.intSettings,u=this,e=e.split(/\s+/).filter(function(a){return a.length>
0});e instanceof Array&&e.forEach(function(a){var b={D:"direction",L:"linePosition",T:"textPosition",A:"alignment",S:"size"},a=a.split(":");b[a[0]]&&(q[b[a[0]]]=a[1]);b[a[0]]in u&&(u[b[a[0]]]=a[1])})}if(this.linePosition.match(/\%/))this.snapToLines=false;this.getCueAsSource=function(){return String(this.text)};this.getCueAsHTML=function(){var a=document.createDocumentFragment(),b=document.createElement("div");b.innerHTML=String(this.text);Array.prototype.forEach.call(b.childNodes,function(b){a.appendChild(b.cloneNode(true))});
return a};this.isActive=function(){var a=0;if(this.track instanceof c.TextTrack&&(this.track.mode===c.TextTrack.SHOWING||this.track.mode===c.TextTrack.HIDDEN)&&this.track.readyState===c.TextTrack.LOADED)try{if(a=this.track.videoNode.currentTime,this.startTime<=a&&this.endTime>=a){if(!this.wasActive)this.wasActive=true,this.onenter();return true}}catch(b){return false}if(this.wasActive)this.wasActive=false,this.onexit();return false};Object.prototype.__defineGetter__?this.__defineGetter__("active",
this.isActive):Object.defineProperty&&Object.defineProperty(this,"active",{get:this.isActive});this.toString=function(){return"TextTrackCue:"+this.id+"\n"+String(this.text)};this.onenter=function(){};this.onexit=function(){}};c.MediaTrack=function(a,b,f,d,e,o){var l=function(a){return a.filter(function(a){try{var b=document.createElement(a.getAttribute("type").split("/").shift());return!(!b.canPlayType||!b.canPlayType(a.getAttribute("type")).replace(/no/,""))}catch(c){return false}}).shift().getAttribute("src")};
this.onload=function(){};this.onerror=function(){};this.id=a||"";this.internalMode=this.internalMode=c.TextTrack.OFF;this.mediaElement=null;this.kind=b||"audiodescription";this.label=f||"";this.language=d||"";this.readyState=c.TextTrack.NONE;this.type=o||"x/unknown";this.mediaType=null;this.src="";if(typeof e==="string")this.src=e;else if(e instanceof NodeList)this.src=l(e);if(this.type.match(/^video\//))this.mediaType="video";else if(this.type.match(/^audio\//))this.mediaType="audio";this.getMode=
function(){return this.internalMode};this.setMode=function(a){if([c.TextTrack.OFF,c.TextTrack.HIDDEN,c.TextTrack.SHOWING].indexOf(a)!==-1){if(a!==this.internalMode)this.internalMode=a,a===c.TextTrack.HIDDEN&&!this.mediaElement&&this.buildMediaElement(),a===c.TextTrack.SHOWING&&this.showMediaElement(),(a===c.TextTrack.OFF||a===c.TextTrack.HIDDEN)&&this.hideMediaElement()}else throw Error("Illegal mode value for track.");};Object.prototype.__defineGetter__?(this.__defineGetter__("mode",this.getMode),
this.__defineSetter__("mode",this.setMode)):Object.defineProperty&&Object.defineProperty(this,"mode",{get:this.getMode,set:this.setMode});this.hideMediaElement=function(){if(this.mediaElement&&(this.mediaElement.paused||this.mediaElement.pause(),this.mediaElement instanceof HTMLVideoElement))this.mediaElement.style.display="none"};this.showMediaElement=function(){if(this.mediaElement){if(this.mediaElement.parentNode||document.body.appendChild(this.mediaElement),this.mediaElement instanceof HTMLVideoElement)this.mediaElement.style.display=
"block"}else this.buildMediaElement(),document.body.appendChild(this.mediaElement)};this.buildMediaElement=function(){try{if(this.type.match(/^video\//))this.mediaElement=document.createElement("video"),this.mediaElement.className="captionator-mediaElement-"+this.kind,c.styleNode(this.mediaElement,this.kind,this.videoNode);else if(this.type.match(/^audio\//))this.mediaElement=new Audio;this.mediaElement.type=this.type;this.mediaElement.src=this.src;this.mediaElement.load();this.mediaElement.trackObject=
this;this.readyState=c.TextTrack.LOADING;var a=this.mediaElement;this.mediaElement.addEventListener("progress",function(){a.trackObject.readyState=c.TextTrack.LOADING},false);this.mediaElement.addEventListener("canplaythrough",function(){a.trackObject.readyState=c.TextTrack.LOADED;a.trackObject.onload.call(a.trackObject)},false);this.mediaElement.addEventListener("error",function(b){a.trackObject.readyState=c.TextTrack.ERROR;a.trackObject.mode=c.TextTrack.OFF;a.trackObject.mediaElement=null;a.trackObject.onerror.call(a.trackObject,
b)},false)}catch(b){this.readyState=c.TextTrack.ERROR,this.mode=c.TextTrack.OFF,this.mediaElement=null,this.onerror&&this.onerror.apply(this,b)}}};c.CaptionatorCueStructure=function(a,b){var c=this;this.isTimeDependent=false;this.cueSource=a;this.options=b;this.processedCue=null;this.toString=function(f){if(b.processCueHTML!==false){var d=function(a,b){if(c.processedCue===null){var e="",h,g;for(h in a)if(h.match(/^\d+$/)&&a.hasOwnProperty(h))if(g=a[h],g instanceof Object&&g.children&&g.children.length)if(g.token===
"v")e+='<q data-voice="'+g.voice.replace(/[\"]/g,"")+"\" class='voice speaker-"+g.voice.replace(/[^a-z0-9]+/ig,"-").toLowerCase()+"' title=\""+g.voice.replace(/[\"]/g,"")+'">'+d(g.children,b+1)+"</q>";else if(g.token==="c")e+="<span class='webvtt-class-span "+g.classes.join(" ")+"'>"+d(g.children,b+1)+"</span>";else if(g.timeIn>0){if(f===null||f===void 0||f>0&&f>=g.timeIn)e+="<span class='webvtt-timestamp-span' data-timestamp='"+g.token+"' data-timestamp-seconds='"+g.timeIn+"'>"+d(g.children,b+1)+
"</span>"}else e+=g.rawToken+d(g.children,b+1)+"</"+g.token+">";else if(g instanceof String||typeof g==="string"||typeof g==="number")e+=g;if(!c.isTimeDependent&&b===0)c.processedCue=e;return e}else return c.processedCue};return d(this,0)}else return a}};c.CaptionatorCueStructure.prototype=[];if(a.exportObjects)window.TextTrack=c.TextTrack,window.TextTrackCueList=c.TextTrackCueList,window.ActiveTextTrackCueList=c.ActiveTextTrackCueList,window.TextTrackCue=c.TextTrackCue,window.MediaTrack=c.MediaTrack;
J=true}[].slice.call(document.getElementsByTagName("video"),0).forEach(function(a){a.addTextTrack=function(b,f,d,e,o,l,q){var u="subtitles,captions,descriptions,captions,metadata,chapters,karaoke,lyrics,tickertext,audiodescription,commentary,alternate,signlanguage".split(","),x=u.slice(0,7),b=typeof b==="string"?b:"",d=typeof d==="string"?d:"",e=typeof e==="string"?e:"",q=typeof q==="boolean"?q:false;if(!u.filter(function(a){return f===a?true:false}).length)throw c.createDOMException(12,"DOMException 12: SYNTAX_ERR: You must use a valid kind when creating a TimedTextTrack.",
"SYNTAX_ERR");if(x.filter(function(a){return f===a?true:false}).length)if(b=new c.TextTrack(b,f,d,e,o,null)){if(!(a.tracks instanceof Array))a.tracks=[];a.tracks.push(b);return b}else return false;else if(b=new c.MediaTrack(b,f,d,e,o,l,q)){if(!(a.mediaTracks instanceof Array))a.mediaTracks=[];a.mediaTracks.push(b);return b}else return false}});if(!b||b===false||b===void 0||b===null)d=[].slice.call(document.getElementsByTagName("video"),0);else if(b instanceof Array)for(e=0;e<b.length;e++)typeof b[e]===
"string"?d=d.concat([].slice.call(document.querySelectorAll(b[e]),0)):b[e].constructor===HTMLVideoElement&&d.push(b[e]);else typeof b==="string"?d=[].slice.call(document.querySelectorAll(b),0):b.constructor===HTMLVideoElement&&d.push(b);if(d.length){for(e=0;e<d.length;e++)c.processVideoElement(d[e],f,a);return true}else return false},processVideoElement:function(b,f,a){var d=[],e=navigator.language||navigator.userLanguage;f||e.split("-");a=a instanceof Object?a:{};if(!b.captioned){b._captionatorOptions=
a;b.className+=(b.className.length?" ":"")+"captioned";b.captioned=true;if(b.id.length===0)b.id=c.generateID();[].slice.call(b.querySelectorAll("track"),0).forEach(function(e){var h=null,h=e.querySelectorAll("source").length>0?e.querySelectorAll("source"):e.getAttribute("src"),h=b.addTextTrack(e.getAttribute("id")||c.generateID(),e.getAttribute("kind"),e.getAttribute("label"),e.getAttribute("srclang").split("-")[0],h,e.getAttribute("type"),e.hasAttribute("default"));e.track=h;h.trackNode=e;h.videoNode=
b;d.push(h);var k=false;if((h.kind==="subtitles"||h.kind==="captions")&&f===h.language&&a.enableCaptionsByDefault)d.filter(function(a){return(a.kind==="captions"||a.kind==="subtitles")&&f===a.language&&a.mode===c.TextTrack.SHOWING?true:false}).length||(k=true);h.kind==="chapters"&&f===h.language&&(d.filter(function(a){return a.kind==="chapters"&&a.mode===c.TextTrack.SHOWING?true:false}).length||(k=true));h.kind==="descriptions"&&a.enableDescriptionsByDefault===true&&f===h.language&&(d.filter(function(a){return a.kind===
"descriptions"&&a.mode===c.TextTrack.SHOWING?true:false}).length||(k=true));k===true&&d.forEach(function(a){if(a.trackNode.hasAttribute("default")&&a.mode===c.TextTrack.SHOWING)a.mode=c.TextTrack.HIDDEN});if(e.hasAttribute("default")&&!d.filter(function(a){return a.trackNode.hasAttribute("default")&&a.trackNode!==e?true:false}).length)k=true,h.internalDefault=true;if(k===true)h.mode=c.TextTrack.SHOWING});b.addEventListener("timeupdate",function(b){b=b.target;try{b.tracks.forEach(function(a){a.activeCues.refreshCues.apply(a.activeCues)})}catch(d){}a.renderer instanceof
Function?a.renderer.call(c,b):c.rebuildCaptions(b);c.synchroniseMediaElements(b)},false);window.addEventListener("resize",function(){b._captionator_dirtyBit=true;c.rebuildCaptions(b)},false);b.addEventListener("play",function(){c.synchroniseMediaElements(b)},false);b.addEventListener("pause",function(){c.synchroniseMediaElements(b)},false);a.enableHighResolution===true&&window.setInterval(function(){try{b.tracks.forEach(function(a){a.activeCues.refreshCues.apply(a.activeCues)})}catch(d){}a.renderer instanceof
Function?a.renderer.call(c,b):c.rebuildCaptions(b)},20)}return b},rebuildCaptions:function(b){var f=b.currentTime,a=[],d=false,e=[],g=[];(b.tracks||[]).forEach(function(b){b.mode===c.TextTrack.SHOWING&&b.readyState===c.TextTrack.LOADED&&(g=[].slice.call(b.activeCues,0),g=g.sort(function(a,b){return a.startTime>b.startTime?-1:1}),a=a.concat(g))});e=a.map(function(a){return a.track.id+"."+a.id+":"+a.text.toString(f).length});if((d=!c.compareArray(e,b._captionator_previousActiveCues))||b._captionator_dirtyBit)b._captionator_dirtyBit=
false,b._captionator_availableCueArea=null,b._captionator_previousActiveCues=e,c.styleCueCanvas(b),b._containerObject.innerHTML="",a.forEach(function(a){var d=document.createElement("div");d.id=String(a.id).length?a.id:c.generateID();d.className="captionator-cue";d.innerHTML=a.text.toString(f);b._containerObject.appendChild(d);c.styleCue(d,a,b)})},synchroniseMediaElements:function(b){var f=function(a,b){try{b.seeking&&a.pause();if(a.currentTime<b.currentTime-0.5||a.currentTime>b.currentTime+0.5)a.currentTime=
b.currentTime;a.paused&&!b.paused?a.play():!a.paused&&b.paused&&a.pause()}catch(c){}};(b.mediaTracks||[]).forEach(function(a){a.mode===c.TextTrack.SHOWING&&a.readyState>=c.TextTrack.LOADING&&f(a.mediaElement,b)});b.id&&[].slice.call(document.body.querySelectorAll("*[syncMaster="+b.id+"]"),0).forEach(function(a){(a.tagName.toLowerCase()==="video"||a.tagName.toLowerCase()==="audio")&&f(a,b)})},getNodeMetrics:function(b){for(var c=window.getComputedStyle(b,null),a=b,d=b.offsetTop,e=b.offsetLeft,g=b,
h=0,k=0,g=parseInt(c.getPropertyValue("width"),10),h=parseInt(c.getPropertyValue("height"),10);a=a.offsetParent;)d+=a.offsetTop,e+=a.offsetLeft;if(b.hasAttribute("controls"))b=navigator.userAgent.toLowerCase(),b.indexOf("chrome")!==-1?k=32:b.indexOf("opera")!==-1?k=25:b.indexOf("firefox")!==-1?k=28:b.indexOf("ie 9")!==-1||b.indexOf("ipad")!==-1?k=44:b.indexOf("safari")!==-1&&(k=25);else if(b._captionatorOptions)b=b._captionatorOptions,b.controlHeight&&(k=parseInt(b.controlHeight,10));return{left:e,
top:d,width:g,height:h,controlHeight:k}},applyStyles:function(b,c){for(var a in c)({}).hasOwnProperty.call(c,a)&&(b.style[a]=c[a])},checkDirection:function(b){var c=RegExp("^[^\u0591-\u07ff\ufb1d-\ufdfd\ufe70-\ufefc]*[A-Za-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02b8\u0300-\u0590\u0800-\u1fff\u2c00-\ufb1c\ufdfe-\ufe6f\ufefd-\uffff]");return RegExp("^[^A-Za-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02b8\u0300-\u0590\u0800-\u1fff\u2c00-\ufb1c\ufdfe-\ufe6f\ufefd-\uffff]*[\u0591-\u07ff\ufb1d-\ufdfd\ufe70-\ufefc]").test(b)?
"rtl":c.test(b)?"ltr":""},styleCue:function(b,f,a){var d=0,e=0,g=0,h=0,k,z,m=0,o=0,l,q,u,x,p,t,C=0,F=k=d=0,G=0,y=0,v=0,j,s,r=0,G=a._captionatorOptions||{},i,e=50,d=m=0,e=true,K=function(a){var b=function(a){return!!a.length},d,e,f,g,h=0,i=function(a){h++;c.applyStyles(a,{display:"block",lineHeight:"auto",height:l+"px",width:t+"px",textAlign:"center"})};for(d in a.childNodes)if(a.childNodes.hasOwnProperty(d))e=a.childNodes[d],e.nodeType===3?(g=document.createDocumentFragment(),f=e.nodeValue,g.appendChild(document.createElement("span")),
g.childNodes[0].innerHTML="<span class='captionator-cue-character'>"+f.split(/(.)/).filter(b).join("</span><span class='captionator-cue-character'>")+"</span>",[].slice.call(g.querySelectorAll("span.captionator-cue-character"),0).forEach(i),e.parentNode.replaceChild(g,e)):a.childNodes[d].nodeType===1&&(h+=K(a.childNodes[d]));return h};i=c.getNodeMetrics(a);if(!a._captionator_availableCueArea)a._captionator_availableCueArea={bottom:i.height-i.controlHeight,right:i.width,top:0,left:0,height:i.height-
i.controlHeight,width:i.width};f.direction==="horizontal"&&(c.applyStyles(b,{width:"auto",position:"static",display:"inline-block",padding:"1em"}),m=parseInt(b.offsetWidth,10),d=Math.floor(m/a._captionator_availableCueArea.width*100),d=d<=100?d:100);m=i.height*(D/100)/96*72;m=m>=A?m:A;l=Math.floor(m/72*96);q=Math.floor(m*E);q=q>B?q:B;t=p=Math.ceil(q/72*96);p*Math.floor(i.height/p)<i.height&&(p=Math.floor(i.height/Math.floor(i.height/p)),q=Math.ceil(p/96*72));p*Math.floor(i.width/p)<i.width&&(t=Math.ceil(i.width/
Math.floor(i.width/p)));u=Math.floor(a._captionator_availableCueArea.height/p);x=Math.floor(a._captionator_availableCueArea.width/t);parseFloat(String(f.size).replace(/[^\d\.]/ig,""))===0?G.sizeCuesByTextBoundingBox===true?k=d:(k=100,e=false):(e=false,k=parseFloat(String(f.size).replace(/[^\d\.]/ig,"")),k=k<=100?k:100);m=f.direction==="horizontal"?Math.floor(i.width*0.01):0;o=f.direction==="horizontal"?0:Math.floor(i.height*0.01);if(f.linePosition==="auto")f.linePosition=f.direction==="horizontal"?
u:x;else if(String(f.linePosition).match(/\%/))f.snapToLines=false,f.linePosition=parseFloat(String(f.linePosition).replace(/\%/ig,""));f.direction==="horizontal"?(h=p,f.textPosition!=="auto"&&e&&(e=parseFloat(String(f.textPosition).replace(/[^\d\.]/ig,"")),k-e>d?k-=e:k=d),g=f.snapToLines===true?a._captionator_availableCueArea.width*(k/100):i.width*(k/100),f.textPosition==="auto"?d=(a._captionator_availableCueArea.right-g)/2+a._captionator_availableCueArea.left:(e=parseFloat(String(f.textPosition).replace(/[^\d\.]/ig,
"")),d=(a._captionator_availableCueArea.right-g)*(e/100)+a._captionator_availableCueArea.left),f.snapToLines===true?e=(u-1)*p+a._captionator_availableCueArea.top:(k=i.controlHeight+p+o*2,e=(i.height-k)*(f.linePosition/100))):(e=a._captionator_availableCueArea.top,d=a._captionator_availableCueArea.right-t,g=t,h=a._captionator_availableCueArea.height*(k/100),d=K(b),k=[].slice.call(b.querySelectorAll("span.captionator-cue-character"),0),C=Math.floor((h-o*2)/l),g=Math.ceil(d/C)*t,F=Math.ceil(d/C),y=(d-
C*(F-1))*l,f.snapToLines===true?d=f.direction==="vertical-lr"?a._captionator_availableCueArea.left:a._captionator_availableCueArea.right-g:(d=g+m*2,d=f.direction==="vertical-lr"?(i.width-d)*(f.linePosition/100):i.width-d-(i.width-d)*(f.linePosition/100)),f.textPosition==="auto"?e=(a._captionator_availableCueArea.bottom-h)/2+a._captionator_availableCueArea.top:(f.textPosition=parseFloat(String(f.textPosition).replace(/[^\d\.]/ig,"")),e=(a._captionator_availableCueArea.bottom-h)*(f.textPosition/100)+
a._captionator_availableCueArea.top),s=j=r=v=0,k.forEach(function(a){j=f.direction==="vertical-lr"?t*v:g-t*(v+1);f.alignment==="start"||f.alignment!=="start"&&v<F-1?s=r*l+o:f.alignment==="end"?s=r*l-l+(h+o*2-y):f.alignment==="middle"&&(s=(h-o*2-y)/2+r*l);c.applyStyles(a,{position:"absolute",top:s+"px",left:j+"px"});r>=C-1?(r=0,v++):r++}));f.direction==="horizontal"&&(z=c.checkDirection(String(f.text))==="rtl"?{start:"right",middle:"center",end:"left"}[f.alignment]:{start:"left",middle:"center",end:"right"}[f.alignment]);
c.applyStyles(b,{position:"absolute",overflow:"hidden",width:g+"px",height:h+"px",top:e+"px",left:d+"px",padding:o+"px "+m+"px",textAlign:z,backgroundColor:"rgba("+I.join(",")+")",direction:c.checkDirection(String(f.text)),lineHeight:q+"pt",boxSizing:"border-box"});if(f.direction==="vertical"||f.direction==="vertical-lr")d-a._captionator_availableCueArea.left-a._captionator_availableCueArea.left>=a._captionator_availableCueArea.right-(d+g)?a._captionator_availableCueArea.right=d:a._captionator_availableCueArea.left=
d+g,a._captionator_availableCueArea.width=a._captionator_availableCueArea.right-a._captionator_availableCueArea.left;else{if(b.scrollHeight>b.offsetHeight*1.2){if(f.snapToLines){for(z=0;b.scrollHeight>b.offsetHeight*1.2;)h+=p,b.style.height=h+"px",z++;e-=z*p}else h=b.scrollHeight+o,k=i.controlHeight+h+o*2,e=(i.height-k)*(f.linePosition/100),b.style.height=h+"px";b.style.top=e+"px"}if(e-a._captionator_availableCueArea.top-a._captionator_availableCueArea.top>=a._captionator_availableCueArea.bottom-
(e+h)&&a._captionator_availableCueArea.bottom>e)a._captionator_availableCueArea.bottom=e;else if(a._captionator_availableCueArea.top<e+h)a._captionator_availableCueArea.top=e+h;a._captionator_availableCueArea.height=a._captionator_availableCueArea.bottom-a._captionator_availableCueArea.top}if(G.debugMode){var w,n,H=function(){if(!w)a._captionatorDebugCanvas?(w=a._captionatorDebugCanvas,n=a._captionatorDebugContext):(w=document.createElement("canvas"),w.setAttribute("width",i.width),w.setAttribute("height",
i.height-i.controlHeight),document.body.appendChild(w),c.applyStyles(w,{position:"absolute",top:i.top+"px",left:i.left+"px",width:i.width+"px",height:i.height-i.controlHeight+"px",zIndex:3E3}),n=w.getContext("2d"),a._captionatorDebugCanvas=w,a._captionatorDebugContext=n)};H();w.setAttribute("width",i.width);H();n.fillStyle="rgba(100,100,255,0.5)";n.fillRect(a._captionator_availableCueArea.left,a._captionator_availableCueArea.top,a._captionator_availableCueArea.right,a._captionator_availableCueArea.bottom);
n.stroke();(function(){var b;H();n.strokeStyle="rgba(255,0,0,0.5)";n.lineWidth=1;n.beginPath();for(b=0;b<u;b++)n.moveTo(0.5,b*p+0.5),n.lineTo(i.width,b*p+0.5);n.closePath();n.stroke();n.beginPath();n.strokeStyle="rgba(0,255,0,0.5)";for(b=x;b>=0;b--)n.moveTo(i.width-b*t-0.5,-0.5),n.lineTo(i.width-b*t-0.5,i.height);n.closePath();n.stroke();n.beginPath();n.strokeStyle="rgba(255,255,0,0.5)";for(b=0;b<=x;b++)n.moveTo(b*t+0.5,-0.5),n.lineTo(b*t+0.5,i.height);n.stroke();a.linesDrawn=true})()}},styleCueCanvas:function(b){var f,
a,d=b._captionatorOptions instanceof Object?b._captionatorOptions:{};if(!(b instanceof HTMLVideoElement))throw Error("Cannot style a cue canvas for a non-video node!");if(b._containerObject)a=b._containerObject,f=a.id;if(a)a.parentNode||document.body.appendChild(a);else{a=document.createElement("div");a.className="captionator-cue-canvas";f=c.generateID();a.id=f;if(d.appendCueCanvasTo){var e=null;if(d.appendCueCanvasTo instanceof HTMLElement)e=d.appendCueCanvasTo;else if(typeof d.appendCueCanvasTo===
"string")try{var g=document.querySelectorAll(d.appendCueCanvasTo);if(g.length>0)e=g[0];else throw null;}catch(h){e=document.body,d.appendCueCanvasTo=false}else e=document.body,d.appendCueCanvasTo=false;e.appendChild(a)}else document.body.appendChild(a);b._containerObject=a;a.setAttribute("aria-live","polite");a.setAttribute("aria-atomic","true")}String(b.getAttribute("aria-describedby")).indexOf(f)===-1&&(e=b.hasAttribute("aria-describedby")?b.getAttribute("aria-describedby")+" ":"",b.setAttribute("aria-describedby",
e+f));e=c.getNodeMetrics(b);b=e.height*(D/100)/96*72;b=b>=A?b:A;f=Math.floor(b*E);f=f>B?f:B;c.applyStyles(a,{position:"absolute",overflow:"hidden",zIndex:100,height:e.height-e.controlHeight+"px",width:e.width+"px",top:(d.appendCueCanvasTo?0:e.top)+"px",left:(d.appendCueCanvasTo?0:e.left)+"px",color:"white",fontFamily:"Verdana, Helvetica, Arial, sans-serif",fontSize:b+"pt",lineHeight:f+"pt",boxSizing:"border-box"});if(window.navigator.userAgent.toLowerCase().indexOf("chrome/10")>-1)a.style.backgroundColor=
"rgba(0,0,0,0.01"+Math.random().toString().replace(".","")+")"},parseCaptions:function(b,f){var f=f instanceof Object?f:{},a="",d=[],e="",g=[],h=/^(\d{2})?:?(\d{2}):(\d{2})\.(\d+)\,(\d{2})?:?(\d{2}):(\d{2})\.(\d+)\s*(.*)/,k=/^(\d+)?:?(\d{2}):(\d{2})\.(\d+)\,(\d+)?:?(\d{2}):(\d{2})\.(\d+)\s*(.*)/,z=/^(\d{2})?:?(\d{2}):(\d{2})[\.\,](\d+)\s+\-\-\>\s+(\d{2})?:?(\d{2}):(\d{2})[\.\,](\d+)\s*(.*)/,m=/(\d{2})?:?(\d{2}):(\d{2})[\.\,](\d+)/,o=/^([\d\.]+)\s+\+([\d\.]+)\s*(.*)/,l=/^\[(\d{2})?:?(\d{2})\:(\d{2})\.(\d{2})\]\s*(.*?)$/i,
q=/^(DEFAULTS|DEFAULT)\s+\-\-\>\s+(.*)/g,u=/^(STYLE|STYLES)\s+\-\-\>\s*\n([\s\S]*)/g,x=/^(COMMENT|COMMENTS)\s+\-\-\>\s+(.*)/g;if(b){var p=function(a){var b=new c.CaptionatorCueStructure(a,f),d=[],e,g,h,j=[];h=0;var k=function(a){return!!a.replace(/[^a-z0-9]+/ig,"").length},d=a.split(/(<\/?[^>]+>)/ig).filter(function(a){return!!a.replace(/\s*/ig,"")});h=b;for(e in d)if(d.hasOwnProperty(e))if(g=d[e],g.substr(0,1)==="<")if(g.substr(1,1)==="/"){if(a=g.substr(2).split(/[\s>]+/g)[0],j.length>0){g=0;for(h=
j.length-1;h>=0;h--){var o=j[h][j[h].length-1];g=h;if(o.token===a)break}h=j[g];j=j.slice(0,g)}}else{if(g.substr(1).match(m)||g.match(/^<v\s+[^>]+>/i)||g.match(/^<c[a-z0-9\-\_\.]+>/)||g.match(/^<(b|i|u|ruby|rt)>/)||f.sanitiseCueHTML!==false){a={token:g.replace(/[<\/>]+/ig,"").split(/[\s\.]+/)[0],rawToken:g,children:[]};if(a.token==="v")a.voice=g.match(/^<v\s*([^>]+)>/i)[1];else if(a.token==="c")a.classes=g.replace(/[<\/>\s]+/ig,"").split(/[\.]+/ig).slice(1).filter(k);else if(g=a.rawToken.match(m))b.isTimeDependent=
true,g=g.slice(1),a.timeIn=parseInt((g[0]||0)*3600,10)+parseInt((g[1]||0)*60,10)+parseInt(g[2]||0,10)+parseFloat("0."+(g[3]||0));h.push(a);j.push(h);h=a.children}}else f.sanitiseCueHTML!==false&&(g=g.replace(/</g,"<").replace(/>/g,">").replace(/\&/g,"&"),f.ignoreWhitespace||(g=g.replace(/\n+/g,"<br />"))),h.push(g);return b},d=b.replace(/\r\n/g,"\n").replace(/\r/g,"\n");l.exec(b)?(d=d.split(/\n+/g),a="LRC"):d=d.split(/\n\n+/g);return d=d.filter(function(b){return b.match(/^WEBVTT(\s*FILE)?/ig)?
(a="WebVTT",false):b.replace(/\s*/ig,"").length?true:false}).map(function(b,d){var l,m,y,v,j,s="",r;if(r=q.exec(b))return g=r.slice(2).join(""),g=g.split(/\s+/g).filter(function(a){return a&&!!a.length}),null;else if(r=u.exec(b))return e+=r[r.length-1],null;else if(r=x.exec(b))return null;for(l=a==="LRC"?[b.substr(0,b.indexOf("]")),b.substr(b.indexOf("]")+1)]:b.split(/\n/g);!l[0].replace(/\s+/ig,"").length&&l.length>0;)l.shift();for(r=l[0].match(/^\s*[a-z0-9]+\s*$/ig)?String(l.shift().replace(/\s*/ig,
"")):d;0<l.length;){var i=l[0];if((j=z.exec(i))||(j=h.exec(i))||(j=k.exec(i)))j=j.slice(1),m=parseInt((j[0]||0)*3600,10)+parseInt((j[1]||0)*60,10)+parseInt(j[2]||0,10)+parseFloat("0."+(j[3]||0)),y=parseInt((j[4]||0)*3600,10)+parseInt((j[5]||0)*60,10)+parseInt(j[6]||0,10)+parseFloat("0."+(j[7]||0)),j[8]&&(s=j[8]);else if(j=o.exec(i))j=j.slice(1),m=parseFloat(j[0]),y=m+parseFloat(j[1]),j[2]&&(s=j[2]);l=l.slice(0,0).concat(l.slice(1));break}if(!m&&!y)return null;j=g.reduce(function(a,b){a[b.split(":")[0]]=
b.split(":")[1];return a},{});j=s.split(/\s+/g).filter(function(a){return a&&!!a.length}).reduce(function(a,b){a[b.split(":")[0]]=b.split(":")[1];return a},j);s="";for(v in j)j.hasOwnProperty(v)&&(s+=s.length?" ":"",s+=v+":"+j[v]);v=f.processCueHTML===false?l.join("\n"):p(l.join("\n"));m=new c.TextTrackCue(r,m,y,v,s,false,null);m.styleData=e;return m}).filter(function(a){return a!==null?true:false})}else throw Error("Required parameter captionData not supplied.");}};window.captionator=c})();