1// abc2svg - ABC to SVG translator
2// @source: https://chiselapp.com/user/moinejf/repository/abc2svg
3// Copyright (C) 2014-2019 Jean-Francois Moine - LGPL3+
4//clip.js-module to handle the%%clip command
5abc2svg.clip={get_clip:function(parm){var C=abc2svg.C
6function get_symsel(a){var j,d,sq,b=a.match(/(\d+)([a-z]?)(:\d+\/\d+)?/)
7if(!b)
8return
9if(b[2])
10sq=b[2].charCodeAt(0)-0x61
11if(!b[3])
12return{m:b[1],t:0,sq:sq}
13a=b[3].match(/:(\d+)\/(\d+)/)
14if(!a||a[2]<1)
15return
16return{m:b[1],t:a[1]*C.BLEN/a[2],sq:sq}}
17var b,c,a=parm.split(/[ -]/)
18if(a.length!=3){this.syntax(1,this.errs.bad_val,"%%clip")
19return}
20if(!a[1])
21b={m:0,t:0}
22else
23b=get_symsel(a[1]);c=get_symsel(a[2])
24if(!b||!c){this.syntax(1,this.errs.bad_val,"%%clip")
25return}
26this.cfmt().clip=[b,c]},do_clip:function(){var C=abc2svg.C
27voice_tb=this.get_voice_tb(),cfmt=this.cfmt()
28function go_global_time(s,sel){var s2,bar_time,seq
29if(sel.m<=1){if(sel.m==1){for(s2=s;s2;s2=s2.ts_next){if(s2.type==C.BAR&&s2.time!=0)
30break}
31if(s2.time<voice_tb[this.get_cur_sy().top_voice].meter.wmeasure)
32s=s2}}else{for(;s;s=s.ts_next){if(s.type==C.BAR&&s.bar_num>=sel.m)
33break}
34if(!s)
35return
36if(sel.sq){seq=sel.sq
37for(s=s.ts_next;s;s=s.ts_next){if(s.type==C.BAR&&s.bar_num==sel.m){if(--seq==0)
38break}}
39if(!s)
40return}}
41if(sel.t==0)
42return s;bar_time=s.time+sel.t
43while(s.time<bar_time){s=s.ts_next
44if(!s)
45return s}
46do{s=s.ts_prev}while(!s.seqst)
47return s}
48var s,s2,sy,p_voice,v
49s=this.get_tsfirst()
50if(cfmt.clip[0].m>0||cfmt.clip[0].t>0){s=go_global_time(s,cfmt.clip[0])
51if(!s){this.set_tsfirst(null)
52return}
53sy=this.get_cur_sy()
54for(s2=this.get_tsfirst();s2!=s;s2=s2.ts_next){switch(s2.type){case C.CLEF:s2.p_v.clef=s2
55break
56case C.KEY:s2.p_v.key=this.clone(s2.as.u.key)
57break
58case C.METER:s2.p_v.meter=this.clone(s2.as.u.meter)
59break
60case C.STAVES:sy=s2.sy;this.set_cur_sy(sy)
61break}}
62for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v]
63for(s2=s;s2;s2=s2.ts_next){if(s2.v==v){delete s2.prev
64break}}
65p_voice.sym=s2}
66this.set_tsfirst(s)
67delete s.ts_prev}
68s=go_global_time(s,cfmt.clip[1])
69if(!s)
70return
71do{s=s.ts_next
72if(!s)
73return}while(!s.seqst)
74for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v]
75for(s2=s.ts_prev;s2;s2=s2.ts_prev){if(s2.v==v){delete s2.next
76break}}
77if(!s2)
78p_voice.sym=null}
79delete s.ts_prev.ts_next},do_pscom:function(of,text){if(text.slice(0,5)=="clip ")
80abc2svg.clip.get_clip.call(this,text)
81else
82of(text)},set_bar_num:function(of){of()
83if(this.cfmt().clip)
84abc2svg.clip.do_clip.call(this)},set_hooks:function(abc){abc.do_pscom=abc2svg.clip.do_pscom.bind(abc,abc.do_pscom);abc.set_bar_num=abc2svg.clip.set_bar_num.bind(abc,abc.set_bar_num)}}
85abc2svg.modules.hooks.push(abc2svg.clip.set_hooks);abc2svg.modules.clip.loaded=true
86