Debian Codegraph @

lib/ChordPro/res/abc/abc2svg/ambitus-1.js

raw symbols SCIP
1 // ambitus.js - module to insert an ambitus at start of a voice
2 //
3 // Copyright (C) 2018-2023 Jean-Francois Moine
4 //
5 // This file is part of abc2svg.
6 //
7 // abc2svg is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // abc2svg is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // This module is loaded when "%%ambitus" appears in a ABC source.
21 //
22 // Parameters
23 // %%ambitus 1
24
25 if (typeof abc2svg == "undefined")
26 var abc2svg = {}
27
28 abc2svg.ambitus = {
29 do_ambitus: function() {
30 var C = abc2svg.C,
31 s, v, p_v, min, max,
32 voice_tb = this.get_voice_tb()
33
34 for (v = 0; v < voice_tb.length; v++) {
35 p_v = voice_tb[v];
36 if (p_v.second)
37 continue
38 min = 100;
39 max = -100
40
41 // search the top and bottom pitches
42 for (s = p_v.sym; s; s = s.next) {
43 if (s.type != C.NOTE)
44 continue
45 if (s.notes[s.nhd].pit > max)
46 max = s.notes[s.nhd].pit
47 if (s.notes[0].pit < min)
48 min = s.notes[0].pit
49 }
50 if (min == 100)
51 continue // no note
52
53 s = p_v.clef;
54 s.stem = 1;
55 s.head = C.FULL;
56 s.stemless = true;
57 s.nhd = 1;
58 s.notes = [{
59 dur: C.BLEN / 4,
60 pit: min,
61 shhd: 0
62 },{
63 dur: C.BLEN / 4,
64 pit: max,
65 shhd: 0
66 }]
67 }
68 }, // do_ambitus()
69
70 draw_symbols: function(of, p_voice) {
71 var staff_tb = this.get_staff_tb(),
72 s = p_voice.sym
73
74 if (s.clef_type != undefined && s.nhd > 0) {
75 s.x -= 26;
76 this.set_scale(s);
77 this.draw_note(s)
78 if (s.notes[1].pit - s.notes[0].pit > 4) {
79 this.xypath(s.x, 3 * (s.notes[1].pit - 18) + staff_tb[s.st].y);
80 this.out_svg('v' +
81 ((s.notes[1].pit - s.notes[0].pit) * 3).toFixed(1) +
82 '" stroke-width=".6"/>\n');
83 }
84 s.x += 26;
85 s.nhd = 0
86 p_voice.clef.nhd = 0
87 }
88 of(p_voice)
89 }, // draw_symbols()
90
91 set_pitch: function(of, last_s) {
92 of(last_s)
93 if (!last_s && this.cfmt().ambitus)
94 abc2svg.ambitus.do_ambitus.call(this)
95 },
96
97 set_fmt: function(of, cmd, param) {
98 if (cmd == "ambitus") {
99 this.cfmt().ambitus = param
100 return
101 }
102 of(cmd, param)
103 },
104
105 set_glue: function(of, w) {
106 var v, s,
107 voice_tb = this.get_voice_tb()
108
109 of(w)
110 if (this.cfmt().ambitus) {
111 for (v = 0; v < voice_tb.length; v++) {
112 s = voice_tb[v].sym
113 while (!s.time && !s.clef_type)
114 s = s.next
115 if (s && s.nhd) { // draw the helper lines if any
116 s.x -= 26
117 this.draw_hl(s)
118 s.ymn = 3 * (s.notes[0].pit - 18) - 2
119 s.ymx = 3 * (s.notes[1].pit - 18) + 2
120 s.x += 26
121 }
122 }
123 }
124 }, // set_glue()
125
126 set_width: function(of, s) {
127 if (s.clef_type != undefined && s.nhd > 0) {
128 s.wl = 40;
129 s.wr = 12
130 } else {
131 of(s)
132 }
133 },
134
135 set_hooks: function(abc) {
136 abc.draw_symbols = abc2svg.ambitus.draw_symbols.bind(abc, abc.draw_symbols);
137 abc.set_pitch = abc2svg.ambitus.set_pitch.bind(abc, abc.set_pitch);
138 abc.set_format = abc2svg.ambitus.set_fmt.bind(abc, abc.set_format);
139 abc.set_sym_glue = abc2svg.ambitus.set_glue.bind(abc, abc.set_sym_glue)
140 abc.set_width = abc2svg.ambitus.set_width.bind(abc, abc.set_width)
141 }
142 } // ambitus
143
144 if (!abc2svg.mhooks)
145 abc2svg.mhooks = {}
146 abc2svg.mhooks.ambitus = abc2svg.ambitus.set_hooks