1// abc2svg - ABC to SVG translator
2// @source: https://chiselapp.com/user/moinejf/repository/abc2svg
3// Copyright (C) 2014-2020 Jean-Francois Moine - LGPL3+
4//sth.js-module to set the stem heights
5abc2svg.sth={recal_beam:function(bm,s){var staff_tb=this.get_staff_tb(),y=staff_tb[s.st].y,s2=bm.s2,y2=staff_tb[s2.st].y
6if(s.sth)
7s.ys=s.sth
8if(s2.sth)
9s2.ys=s2.sth;bm.a=(s.ys+y-s2.ys-y2)/(s.xs-s2.xs);bm.b=s.ys-s.xs*bm.a+y
10while(1){s.ys=bm.a*s.xs+bm.b-y
11if(s.stem>0)
12s.ymx=s.ys+2.5
13else
14s.ymn=s.ys-2.5;s=s.next
15if(s==s2)
16break}},set_sth:function(){var s,h,v,sth_a,p_voice,voice_tb=this.get_voice_tb()
17for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v]
18if(p_voice.sth!=null)
19continue
20sth_a=[]
21for(s=p_voice.sym;s;s=s.next){if(s.sth){sth_a=s.sth;s.sth=null}
22if(sth_a.length==0||s.nflags<=-2||s.stemless||!(s.beam_st||s.beam_end))
23continue
24h=sth_a.shift()
25if(h=='*')
26continue
27if(h=='|'){for(s=s.next;s;s=s.next){if(s.bar_type)
28break}
29continue}
30h=Number(h)
31if(isNaN(h)||!h)
32continue
33if(s.stem>=0){s.ys=s.y+h;s.ymx=(s.ys+2.5)|0}else{s.ys=s.y-h;s.ymn=(s.ys-2.5)|0}
34s.sth=s.ys}}},calculate_beam:function(of,bm,s1){var done=of(bm,s1)
35if(done&&bm.s2&&(s1.sth||bm.s2.sth))
36abc2svg.sth.recal_beam.call(this,bm,s1)
37return done},new_note:function(of,grace,tp_fact){var C=abc2svg.C,s=of(grace,tp_fact),curvoice=this.get_curvoice()
38if(curvoice.sth&&s&&s.type==C.NOTE){s.sth=curvoice.sth;curvoice.sth=null}
39return s},set_fmt:function(of,cmd,param){if(cmd=="sth"){if(this.parse.state==2)
40this.goto_tune()
41var curvoice=this.get_curvoice()
42if(curvoice)
43curvoice.sth=param.split(/[ \t;-]+/)
44return}
45of(cmd,param)},set_stems:function(of){of();abc2svg.sth.set_sth.call(this)},set_hooks:function(abc){abc.calculate_beam=abc2svg.sth.calculate_beam.bind(abc,abc.calculate_beam);abc.new_note=abc2svg.sth.new_note.bind(abc,abc.new_note);abc.set_format=abc2svg.sth.set_fmt.bind(abc,abc.set_format);abc.set_stems=abc2svg.sth.set_stems.bind(abc,abc.set_stems)}}
46abc2svg.modules.hooks.push(abc2svg.sth.set_hooks);abc2svg.modules.sth.loaded=true
47