UNPKG

vidstack

Version:

Build awesome media experiences on the web.

5 lines (4 loc) 4.59 kB
var A=Object.defineProperty;var L=(c,t,a)=>t in c?A(c,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):c[t]=a;var h=(c,t,a)=>(L(c,typeof t!="symbol"?t+"":t,a),a);import{V,f as y}from"./index.js";const b=/^Format:[\s\t]*/,O=/^Style:[\s\t]*/,_=/^Dialogue:[\s\t]*/,S=/[\s\t]*,[\s\t]*/,w=/\{[^}]+\}/g,I=/\\N/g,x=/^\[(.*)[\s\t]?Styles\]$/,m=/^\[(.*)[\s\t]?Events\]$/;class M{constructor(){h(this,"f");h(this,"O",0);h(this,"a",null);h(this,"j",[]);h(this,"k",[]);h(this,"N",null);h(this,"d");h(this,"P",{})}async init(t){this.f=t,t.errors&&(this.d=(await import("./errors.js")).ParseErrorBuilder)}parse(t,a){if(this.O)switch(this.O){case 1:if(t==="")this.O=0;else if(O.test(t))if(this.N){const e=t.replace(O,"").split(S);this.S(e)}else this.e(this.d?.T("Style",a));else b.test(t)?this.N=t.replace(b,"").split(S):m.test(t)&&(this.N=null,this.O=2);break;case 2:if(t==="")this.Q();else if(_.test(t))if(this.Q(),this.N){const e=t.replace(_,"").split(S),s=this.U(e,a);s&&(this.a=s)}else this.e(this.d?.T("Dialogue",a));else this.a?this.a.text+=` `+t.replace(w,"").replace(I,` `):b.test(t)?this.N=t.replace(b,"").split(S):x.test(t)?(this.N=null,this.O=1):m.test(t)&&(this.N=null)}else t===""||(x.test(t)?(this.N=null,this.O=1):m.test(t)&&(this.N=null,this.O=2))}done(){return{metadata:{},cues:this.j,regions:[],errors:this.k}}Q(){this.a&&(this.j.push(this.a),this.f.onCue?.(this.a),this.a=null)}S(t){let a="Default",e={},s,i="center",l="bottom",d,u=1.2,n,f,o=3,g=[];for(let p=0;p<this.N.length;p++){const F=this.N[p],r=t[p];switch(F){case"Name":a=r;break;case"Fontname":e["font-family"]=r;break;case"Fontsize":e["font-size"]=`calc(${r} / var(--overlay-height))`;break;case"PrimaryColour":const E=N(r);E&&(e["--cue-color"]=E);break;case"BorderStyle":o=parseInt(r,10);break;case"BackColour":f=N(r);break;case"OutlineColour":const R=N(r);R&&(n=R);break;case"Bold":parseInt(r)&&(e["font-weight"]="bold");break;case"Italic":parseInt(r)&&(e["font-style"]="italic");break;case"Underline":parseInt(r)&&(e["text-decoration"]="underline");break;case"StrikeOut":parseInt(r)&&(e["text-decoration"]="line-through");break;case"Spacing":e["letter-spacing"]=r+"px";break;case"AlphaLevel":e.opacity=parseFloat(r);break;case"ScaleX":g.push(`scaleX(${parseFloat(r)/100})`);break;case"ScaleY":g.push(`scaleY(${parseFloat(r)/100})`);break;case"Angle":g.push(`rotate(${r}deg)`);break;case"Shadow":u=parseInt(r,10)*1.2;break;case"MarginL":e["--cue-width"]="auto",e["--cue-left"]=parseFloat(r)+"px";break;case"MarginR":e["--cue-width"]="auto",e["--cue-right"]=parseFloat(r)+"px";break;case"MarginV":d=parseFloat(r);break;case"Outline":s=parseInt(r,10);break;case"Alignment":const k=parseInt(r,10);switch(k>=4&&(l=k>=7?"top":"center"),k%3){case 1:i="start";break;case 2:i="center";break;case 3:i="end";break}}}if(e.R=l,e["--cue-white-space"]="normal",e["--cue-line-height"]="normal",e["--cue-text-align"]=i,l==="center"?(e["--cue-top"]="50%",g.push("translateY(-50%)")):e[`--cue-${l}`]=(d||0)+"px",o===1&&(e["--cue-padding-y"]="0"),(o===1||f)&&(e["--cue-bg-color"]=o===1?"none":f),o===3&&n&&(e["--cue-outline"]=`${s}px solid ${n}`),o===1&&typeof s=="number"){const p=f??"#000";e["--cue-text-shadow"]=[n&&T(s*1.2,u*1.2,n),n?T(s*(s/2),u*(s/2),p):T(s,u,p)].filter(Boolean).join(", ")}g.length&&(e["--cue-transform"]=g.join(" ")),this.P[a]=e}U(t,a){const e=this.V(t),s=this.o(e.Start,e.End,a);if(!s)return;const i=new V(s[0],s[1],""),l={...this.P[e.Style]||{}},d=e.Name?`<v ${e.Name}>`:"",u=l.R,n=e.MarginL&&parseFloat(e.MarginL),f=e.MarginR&&parseFloat(e.MarginR),o=e.MarginV&&parseFloat(e.MarginV);return n&&(l["--cue-width"]="auto",l["--cue-left"]=n+"px"),f&&(l["--cue-width"]="auto",l["--cue-right"]=f+"px"),o&&u!=="center"&&(l[`--cue-${u}`]=o+"px"),i.text=d+t.slice(this.N.length-1).join(", ").replace(w,"").replace(I,` `),delete l.R,Object.keys(l).length&&(i.style=l),i}V(t){const a={};for(let e=0;e<this.N.length;e++)a[this.N[e]]=t[e];return a}o(t,a,e){const s=y(t),i=y(a);if(s!==null&&i!==null&&i>s)return[s,i];s===null&&this.e(this.d?.q(t,e)),i===null&&this.e(this.d?.r(a,e)),s!=null&&i!==null&&i>s&&this.e(this.d?.s(s,i,e))}e(t){if(t){if(this.k.push(t),this.f.strict)throw this.f.cancel(),t;this.f.onError?.(t)}}}function N(c){const t=parseInt(c.replace("&H",""),16);if(t>=0){const e=(t>>24&255^255)/255,s=t>>16&255,i=t>>8&255;return"rgba("+[t&255,i,s,e].join(",")+")"}return null}function T(c,t,a){const e=Math.ceil(2*Math.PI*c);let s="";for(let i=0;i<e;i++){const l=2*Math.PI*i/e;s+=c*Math.cos(l)+"px "+t*Math.sin(l)+"px 0 "+a+(i==e-1?"":",")}return s}function $(){return new M}export{M as SSAParser,$ as default};