// abc2svg - ABC to SVG translator
// @source: https://chiselapp.com/user/moinejf/repository/abc2svg
// Copyright (C) 2014-2020 Jean-Francois Moine - LGPL3+
//abc2svg-strtab.js-tablature for string instruments
abc2svg.strtab={draw_symbols:function(of,p_v){var s,m,not,stb,x,y,C=abc2svg.C,abc=this
if(!p_v.tab){of(p_v)
return}
m=abc.cfmt().bgcolor||"white"
if(abc.bgt!=m){if(!abc.bgn)
abc.bgn=1
else
abc.bgn++
abc.bgt=m
abc.defs_add('\
\n\
\n\
\n\
')
abc.add_style('\n.bg'+abc.bgn+'{filter:url(#bg'+abc.bgn+')}')}
for(s=p_v.sym;s;s=s.next){switch(s.type){case C.KEY:case C.METER:case C.REST:s.invis=true
break
case C.NOTE:if(!s.stemless)
s.ys=-10
break}}
of(p_v)
abc.glout()
stb=abc.get_staff_tb()[p_v.st].y
abc.out_svg('\n')
for(s=p_v.sym;s;s=s.next){if(s.type==C.NOTE){for(m=0;m<=s.nhd;m++){not=s.notes[m]
x=s.x-3
if(not.nb>=10)
x-=3
y=3*(not.pit-18)
abc.out_svg(''+not.nb+'\n')}}}
abc.out_svg('\n')},set_fmt:function(of,cmd,parm){if(cmd=="strtab"){if(!parm)
return
this.set_v_param("clef","tab")
if(parm.indexOf("diafret")>=0){this.set_v_param("diafret",true)
parm=parm.replace(/\s*diafret\s*/,"")}
this.set_v_param("strings",parm)
return}
of(cmd,parm)},set_width:function(of,s){var m,not,abc=this,C=abc2svg.C,o=s.stem<0?3.5:-2.5
of(s)
if(s.p_v&&s.p_v.tab&&s.type==C.NOTE&&!s.stemless){for(m=0;m<=s.nhd;m++)
s.notes[m].shhd=o}},set_stems:function(of){var p_v,i,m,nt,n,bi,bn,strss,C=abc2svg.C,abc=this,s=abc.get_tsfirst(),strs=[],lstr=[]
function set_pit(p_v,s,nt,i){var st=s.st,n=(p_v.diafret?nt.pit:nt.midi)-p_v.tab[i]
if(p_v.diafret&&nt.acc)
n+='+'
nt.acc=0
nt.invis=true
nt.pit=i*2+18
nt.nb=n
strss[i]=s.time+s.dur
if(s.nflags>=-1&&!s.stemless){if(!lstr[st])
lstr[st]=[10]
if(lstr[st][0]>i){lstr[st][0]=i
lstr[st][1]=s}
s.stemless=true}}
function strnum(n){n=n.match(/^([1-9])s?$/)
return n?p_v.tab.length-n[1]:-1}
p_v=abc.get_voice_tb()
for(n=0;n=0){bi=strnum(s.a_dd[i].name)
if(bi>=0){nt=s.notes[0]
set_pit(p_v,s,nt,bi)
break}}
delete s.a_dd}
ls:for(m=0;m<=s.nhd;m++){nt=s.notes[m]
if(nt.nb!=undefined)
continue
if(nt.a_dcn){i=nt.a_dcn.length
while(--i>=0){bi=strnum(nt.a_dcn[i])
if(bi>=0){set_pit(p_v,s,nt,bi)
delete nt.a_dcn
continue ls}}
delete nt.a_dcn}
bn=100
i=p_v.tab.length
while(--i>=0){if(strss[i]&&strss[i]>s.time)
continue
n=(p_v.diafret?nt.pit:nt.midi)-
p_v.tab[i]
if(n>=0&&n='1'&&strs[1]<='9')
tab=str2tab(strs.split(','))
else
tab=abc2tab(strs)
if(!tab){this.syntax(1,"Bad strings in tablature")
ok=false}}else if(!p_v.tab){tab=p_v.diafret?[10,14,17]:[40,45,50,55,59,64]}}
if(ok){if(p_v.capo){p_v.tab=[]
for(i=0;i