customMusicScore.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. import { ref } from "vue";
  2. import state, { customData } from "../state"
  3. import { getQuery } from "/src/utils/queryString";
  4. import { setGlobalData } from "/src/utils";
  5. const query: any = getQuery();
  6. interface IItem {
  7. id?: string
  8. y?: number
  9. isLast?: boolean
  10. childIndex?: number[]
  11. }
  12. interface IItemList {
  13. parts: string[],
  14. tieId?: string[],
  15. staveSection?: IItem[],
  16. vfmodifiers?: IItem[],
  17. voltas?: number
  18. vfcurve?: IItem[]
  19. stavenote?: IItem[]
  20. }
  21. interface IMusicList {
  22. [_key: string]: IItemList[]
  23. }
  24. const container = ref();
  25. /** 曲谱配置: 重叠 */
  26. export const resetGivenFormate = () => {
  27. interface IItem {
  28. id?: string
  29. y?: number
  30. isLast?: boolean
  31. childIndex?: number[]
  32. }
  33. interface IItemList {
  34. parts: string[],
  35. tieId?: string[],
  36. staveSection?: IItem[],
  37. vfmodifiers?: IItem[],
  38. voltas?: number
  39. vfcurve?: IItem[]
  40. stavenote?: IItem[]
  41. }
  42. interface IMusicList {
  43. [_key: string]: IItemList[]
  44. }
  45. const musicList: IMusicList = {
  46. '12200': [
  47. {parts: ['0', '1'], tieId: ['1483']},
  48. {parts: ['2'], tieId: ['1463']},
  49. {parts: ['10'], tieId: ['1246']},
  50. {parts: ['11'], tieId: ['2455']},
  51. {parts: ['13'], tieId: ['1488', '1688']},
  52. {parts: ['14', '15'], tieId: ['1272']},
  53. {parts: ['16'], tieId: ['1264', '1368'], staveSection: [{id: 'section-0', y: -10}]},
  54. ],
  55. '12420': [
  56. {parts: ['0'], tieId: ['1298', '1405', '1998', '2598', '3229', '2731', '2617']}
  57. ],
  58. '7729': [
  59. {parts: ['3'], tieId: ['1498', '1660']}
  60. ],
  61. '7439': [
  62. {parts: ['23'], vfmodifiers: [{id: 'modifiers-130', y: -18, isLast: true}]}
  63. ],
  64. '12711': [
  65. { parts: ['0'], voltas: -12},
  66. { parts: ['4'],voltas: -8},
  67. ],
  68. '3581': [
  69. { parts: ['0'], voltas: -8},
  70. ],
  71. '6244': [
  72. { parts: ['15'], stavenote: [{id: 'vf-auto1608', y: -15}]},
  73. ],
  74. '7473': [
  75. { parts: ['0'], voltas: -8},
  76. ]
  77. }
  78. const tieList = musicList[state.cbsExamSongId as string]
  79. if (tieList) {
  80. const partIndex = state.partIndex + ""
  81. const tie = tieList.find((item) => item.parts.includes(partIndex))
  82. if (!tie) return
  83. // 延音线和连线重叠
  84. if (tie.tieId && tie.tieId.length) {
  85. for(let tieIndex = 0; tieIndex < tie.tieId.length; tieIndex++){
  86. const vftie: any = document.querySelector(`#vf-auto${tie.tieId[tieIndex]}-tie`)
  87. const vfcurve = vftie?.parentNode?.parentNode?.querySelectorAll('.vf-curve')
  88. if (vfcurve && vfcurve.length){
  89. for(let i = 0; i < vfcurve.length; i++){
  90. const result = collisionDetection(vftie, vfcurve[i])
  91. if (result.isCollision){
  92. vfcurve[i].style.transform = `translateY(-8px)`;
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. // 小节数字
  100. if (tie.staveSection && tie.staveSection.length) {
  101. const sectionList = document.querySelectorAll('.vf-StaveSection')
  102. sectionList.forEach((node, index) => {
  103. node.classList.add(`section-${index}`)
  104. })
  105. for(let i = 0; i < tie.staveSection.length; i++){
  106. const item: any = document.querySelector( '.' + tie.staveSection[i].id)
  107. if (item){
  108. item.style.transform = `translateY(${tie.staveSection[i].y}px)`;
  109. }
  110. }
  111. }
  112. // modifiers 里面的符号
  113. if(tie.vfmodifiers && tie.vfmodifiers.length){
  114. const modifierList = document.querySelectorAll('.vf-modifiers')
  115. modifierList.forEach((node, index) => {
  116. node.classList.add(`modifiers-${index}`)
  117. })
  118. for(let i = 0; i < tie.vfmodifiers.length; i++){
  119. const modifier = tie.vfmodifiers[i]
  120. const item: SVGAElement = document.querySelector( '.' + modifier.id)!
  121. if (item){
  122. if (modifier.isLast){
  123. const lastEle: any = Array.from(item.childNodes).at(-1)
  124. if (lastEle){
  125. lastEle.style.transform = `translateY(${modifier.y}px)`;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. // 房子
  132. if (tie.voltas){
  133. const modifierList = document.querySelectorAll('.vf-Volta') as unknown as HTMLElement[]
  134. modifierList.forEach((node, index) => {
  135. node.style.transform = `translateY(${tie.voltas}px)`;
  136. })
  137. }
  138. // 单个音符
  139. if (tie.stavenote && tie.stavenote.length) {
  140. for(let i = 0; i < tie.stavenote.length; i++){
  141. const item = tie.stavenote[i]
  142. const ele = document.querySelector('#' + item.id)! as unknown as HTMLElement
  143. ele && (ele.style.transform = `translateY(${item.y}px)`)
  144. }
  145. }
  146. }
  147. };
  148. const initNoteCoord = () => {
  149. const allNoteDot: any = Array.from(document.querySelectorAll('.node-dot'));
  150. state.noteCoords = allNoteDot.map((note: any) => {
  151. const note_bbox = note?.getBoundingClientRect?.() || { x: 0, y: 0 };
  152. return {
  153. x: note_bbox.x,
  154. y: note_bbox.y
  155. }
  156. })
  157. console.log(11111,state.noteCoords)
  158. }
  159. export const moveGracePosition = (needTrans?: boolean) => {
  160. /**
  161. * TODO:曲目:摇篮曲(节奏练习)-倚音位置 特殊处理
  162. */
  163. const specialIds = ['1788850864767643649','1788502467554750466','1789839575249596417','1788501975122489346','1796006876341813249'];
  164. if (specialIds.includes(state.cbsExamSongId) || needTrans) {
  165. const lastCurve: any = Array.from(document.getElementsByClassName('vf-curve'))?.last();
  166. if (lastCurve) {
  167. lastCurve.style.display = 'none';
  168. }
  169. if (state.musicRenderType === 'staff') {
  170. // const transNoteDom = document.getElementById('vf-auto2182')?.getElementsByClassName('vf-modifiers')?.[0];
  171. // const transBeamDom = document.getElementById('auto3167')?.parentNode?.getElementsByClassName('vf-beams')?.[0];
  172. // if (transNoteDom) {
  173. // transNoteDom.style.transform = 'translateX(-0.5rem)';
  174. // }
  175. // if (transBeamDom) {
  176. // transBeamDom.style.transform = 'translateX(-0.5rem)';
  177. // }
  178. } else {
  179. // vf-auto2172 , vf-auto2384
  180. const signatureDom = document.getElementById('auto2670');
  181. const signatureDom2 = document.getElementById('auto2710');
  182. const signatureDom3 = document.getElementById('auto3099');
  183. const signatureDom4 = document.getElementById('auto3339');
  184. const needTransLateDom: any = state.cbsExamSongId == '1789839575249596417' && document.getElementById('vf-auto1554')?.getElementsByClassName('vf-modifier')?.[0];
  185. const arrowDom = state.cbsExamSongId == '1789839575249596417' && document.getElementById('vf-auto1554-lines');
  186. const needTransLateDom2: any = state.cbsExamSongId == '1788501975122489346' && document.getElementById('vf-auto2116')?.getElementsByClassName('vf-modifier')?.[0];
  187. const arrowDom2 = state.cbsExamSongId == '1788501975122489346' && document.getElementById('vf-auto2116-lines');
  188. const needTransLateDom3: any = state.cbsExamSongId == '1788502467554750466' && document.getElementById('vf-auto2122')?.getElementsByClassName('vf-modifier')?.[0];
  189. const arrowDom3 = state.cbsExamSongId == '1788502467554750466' && document.getElementById('vf-auto2122-lines');
  190. if (signatureDom) signatureDom.style.display = 'none';
  191. if (signatureDom2) signatureDom2.style.display = 'none';
  192. if (signatureDom3) signatureDom3.style.display = 'none';
  193. if (signatureDom4) signatureDom4.style.display = 'none';
  194. if (needTransLateDom) needTransLateDom.style.transform = 'translateX(-0.65rem)';
  195. if (needTransLateDom2) needTransLateDom2.style.transform = 'translateX(-0.65rem)';
  196. if (needTransLateDom3) needTransLateDom3.style.transform = 'translateX(-0.65rem)';
  197. if (arrowDom) arrowDom.style.transform = 'translateX(-0.65rem)';
  198. if (arrowDom2) arrowDom2.style.transform = 'translateX(-0.65rem)';
  199. if (arrowDom3) arrowDom3.style.transform = 'translateX(-0.65rem)';
  200. if (arrowDom || arrowDom2 || arrowDom3) {
  201. const path: any = arrowDom ? arrowDom.querySelector('path') : arrowDom2 ? arrowDom2.querySelector('path') : arrowDom3 ? arrowDom3.querySelector('path') : null;
  202. let d = path?.getAttribute("d");
  203. if (d) {
  204. const patchStr = d.split('L')?.last()?.split(" ")?.[0];
  205. let startX = d.split("M")?.[1]?.split(" ")[0] || 0;
  206. startX = startX ? Number(startX) : 0;
  207. let endX = d.split("L")?.last().split(" ")[0] || 0;
  208. endX = endX ? Number(endX) : 0;
  209. const distanceX = endX - startX;
  210. const transX = startX - distanceX;
  211. d = d.replace(`L${patchStr}`,`L${transX}`);
  212. path.setAttribute("d", d);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. // 谱面优化
  219. export const resetFormate = () => {
  220. container.value = document.getElementById('scrollContainer')
  221. // if (state.extStyleConfigJson || !container.value) return;
  222. if (!container.value) return;
  223. moveGracePosition();
  224. // setTimeout(() => {
  225. // initNoteCoord();
  226. // }, 0);
  227. const stafflines: SVGAElement[] = Array.from((container.value as HTMLElement).querySelectorAll(".staffline"));
  228. const baseStep = 4; // 两个元素相间,的间距
  229. const musicalDistance = 28; // 音阶与第一条线谱的间距,默认设置为28
  230. for (let i = 0, len = stafflines.length; i < len; i++) {
  231. const staffline = stafflines[i];
  232. const stafflineBox = staffline.getBBox();
  233. const stafflineCenter = stafflineBox.y + stafflineBox.height / 2;
  234. const vfmeasures: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure"));
  235. const vfcurve: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-curve"));
  236. const vfvoices: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-voices"));
  237. const vfbeams: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-beams"));
  238. const vfties: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-ties"));
  239. const vflines: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-line"));
  240. const texts: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-stave text"));
  241. const rects: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-stave rect[fill=none]"));
  242. const staveSection: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure .vf-staveSection"));
  243. const paths: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-stave path"));
  244. const dotModifiers: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure .vf-stopDot"));
  245. const staves: SVGAElement[] = Array.from(staffline.querySelectorAll(".vf-measure > .vf-stave"));
  246. // 获取第一个线谱的y轴坐标
  247. const firstLinePathY = paths[0]?.getBBox().y || 0
  248. // 反复标记 和 小节碰撞
  249. const repetWord = ["To Coda", "D.S. al Coda", "Coda"];
  250. texts
  251. .filter((n) => repetWord.includes(n.textContent || ""))
  252. .forEach((t) => {
  253. vfbeams.forEach((curve) => {
  254. const result = collisionDetection(t, curve);
  255. const prePath: SVGAElement = t?.previousSibling as unknown as SVGAElement;
  256. if (result.isCollision) {
  257. const shift_y = Number(t.getAttribute("y")) - (result.b1 - result.t2) - baseStep + "";
  258. t.setAttribute("y", shift_y);
  259. // console.log('音阶间距',shift_y)
  260. if (prePath && prePath.getAttribute("stroke-width") === "0.3" && prePath.getAttribute("stroke") === "none" && (prePath.getAttribute("d")?.length || 0) > 3000) {
  261. prePath.style.transform = `translateY(${-(result.b1 - result.t2 + baseStep)}px)`;
  262. }
  263. }
  264. });
  265. vfvoices.forEach((curve) => {
  266. const result = collisionDetection(t, curve);
  267. const prePath: SVGAElement = t?.previousSibling as unknown as SVGAElement;
  268. if (result.isCollision) {
  269. const shift_y = Number(t.getAttribute("y")) - (result.b1 - result.t2) - baseStep + "";
  270. t.setAttribute("y", shift_y);
  271. // console.log('音阶间距',shift_y)
  272. if (prePath && prePath.getAttribute("stroke-width") === "0.3" && prePath.getAttribute("stroke") === "none" && (prePath.getAttribute("d")?.length || 0) > 3000) {
  273. prePath.style.transform = `translateY(${-(result.b1 - result.t2 + baseStep)}px)`;
  274. }
  275. }
  276. });
  277. });
  278. // 文字方框和飞线碰撞
  279. staveSection.forEach((t) => {
  280. let shift_y = 0;
  281. [...vfcurve, ...vfties, ...vfvoices].forEach((curve) => {
  282. const result = collisionDetection(t, curve);
  283. if (result.isCollision) {
  284. shift_y = Math.min(shift_y, result.t2 - result.b1 - baseStep);
  285. }
  286. });
  287. t.style.transform = `translateY(${shift_y}px)`;
  288. });
  289. // 文字和小节碰撞
  290. let vftexts = Array.from(staffline.querySelectorAll(".vf-text > text")).filter((n: any) => n.getBBox().y < stafflineCenter);
  291. for (let i = 0; i < vftexts.length; i++) {
  292. const _text = vftexts[i];
  293. for (let j = 0; j < vftexts.length; j++) {
  294. if (_text.textContent === 'second time only') {
  295. // @ts-ignore
  296. _text.style.transform = `translateY(15px)`;
  297. }
  298. if (_text.parentNode === vftexts[j].parentNode) continue;
  299. const result = collisionDetection(_text as SVGAElement, vftexts[j] as SVGAElement);
  300. if (result.isCollision) {
  301. if (_text.textContent === vftexts[j].textContent) {
  302. vftexts[j].parentNode?.removeChild(vftexts[j]);
  303. continue;
  304. }
  305. }
  306. }
  307. }
  308. vftexts = Array.from(staffline.querySelectorAll(".vf-text > text")).filter((n: any) => n.getBBox().y < stafflineCenter);
  309. let maxY = 0;
  310. let _vftexts: SVGAElement[] = [];
  311. vftexts.forEach((vftext: any) => {
  312. const textBox = vftext.getBBox();
  313. if (textBox.y < stafflineCenter) {
  314. maxY = Math.max(maxY, textBox.y + textBox.height);
  315. //console.log('音阶间距',textBox.y, textBox.height)
  316. _vftexts.push(vftext as SVGAElement);
  317. }
  318. });
  319. if (maxY !== 0 && _vftexts.length > 1) {
  320. _vftexts.forEach((vftext) => {
  321. vftext.setAttribute("y", maxY + "");
  322. //console.log('音阶间距',maxY)
  323. });
  324. }
  325. vftexts.forEach((vftext) => {
  326. [...vfcurve, ...vfmeasures, ...vflines].forEach((vfmeasure) => {
  327. let result = collisionDetection(vftext as SVGAElement, vfmeasure);
  328. if (result.isCollision && result.b1 < result.b2 && result.t1 < result.b2 - (result.b2 - result.t2) / 2) {
  329. const shift_y = Number(vftext.getAttribute("y")) - (result.b1 - result.t2) - baseStep + "";
  330. vftext.setAttribute("y", shift_y);
  331. //console.log('音阶间距',shift_y)
  332. }
  333. });
  334. });
  335. vftexts.forEach((vftext) => {
  336. vftexts.forEach((text) => {
  337. if (vftext.parentNode !== text.parentNode && !["marcato", "legato"].includes(vftext.textContent as string)) {
  338. if (["marcato", "legato"].includes(text.textContent as string)) {
  339. const result = collisionDetection(vftext as SVGAElement, text as SVGAElement, 30, 30);
  340. if (result.isCollision) {
  341. const textBBox = (vftext as SVGAElement).getBBox();
  342. text.setAttribute("x", textBBox.x + textBBox.width + 5 + "");
  343. text.setAttribute("y", textBBox.y + textBBox.height - 5 + "");
  344. //console.log('音阶间距',textBBox.y + textBBox.height - 5 + "")
  345. }
  346. } else {
  347. const result = collisionDetection(vftext as SVGAElement, text as SVGAElement);
  348. if (result.isCollision) {
  349. const _y = Number(vftext.getAttribute("y"));
  350. const shift_y = result.b2 - result.t2 < 24 ? 24 : result.b2 - result.t2;
  351. text.setAttribute("y", _y - shift_y - 0.5 + "");
  352. //console.log('音阶间距',_y - shift_y - 0.5 + "")
  353. }
  354. }
  355. }
  356. });
  357. });
  358. // 修改音阶和线谱的间距
  359. const clefList = ['C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#', 'G#', 'F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb', 'Cb', 'Fb', 'D#', 'A#', 'E#']
  360. const btransList = ['Bb', 'Eb', 'Ab', 'Db', 'Gb', 'Cb', 'Fb']
  361. const jtrsnsList = ['F#', 'C#', 'G#', 'D#', 'A#', 'E#', 'B#']
  362. vftexts.forEach((label: any) => {
  363. const labelText = label.textContent as string
  364. if (clefList.includes(labelText)){
  365. const _y = Number(label.getAttribute("y"))
  366. const endY = firstLinePathY ? firstLinePathY - musicalDistance : _y
  367. label.setAttribute("y", endY)
  368. }
  369. if (btransList.includes(labelText)) {
  370. label.textContent = labelText.replace('b','♭')
  371. }
  372. if (jtrsnsList.includes(labelText)) {
  373. label.textContent = labelText.replace('#','♯')
  374. }
  375. });
  376. dotModifiers.forEach((group: any) => {
  377. if (state.musicRenderType === 'fixedTone') {
  378. group.setAttribute('transform', 'translate(3,-12)')
  379. } else {
  380. group.setAttribute('transform', 'translate(3,-7)')
  381. }
  382. });
  383. const vftextBottom = Array.from(staffline.querySelectorAll(".vf-text > text")).filter((n: any) => n.getBBox().y > stafflineCenter);
  384. const vflineBottom = Array.from(staffline.querySelectorAll(".vf-line")).filter((n: any) => n.getBBox().y > stafflineCenter);
  385. // 去重
  386. for (let i = 0; i < vftextBottom.length; i++) {
  387. const _text = vftextBottom[i];
  388. for (let j = 0; j < vftextBottom.length; j++) {
  389. if (_text.parentNode === vftextBottom[j].parentNode) continue;
  390. const result = collisionDetection(_text as SVGAElement, vftextBottom[j] as SVGAElement);
  391. if (result.isCollision) {
  392. if (_text.textContent === vftextBottom[j].textContent) {
  393. vftextBottom[j].parentNode?.removeChild(vftextBottom[j]);
  394. continue;
  395. }
  396. }
  397. }
  398. }
  399. // 1,2线谱底部文字重叠问题
  400. vftextBottom.forEach((vftext) => {
  401. [...vfmeasures].forEach((n) => {
  402. let result = collisionDetection(vftext as SVGAElement, n);
  403. if (result.isCollision) {
  404. vftext.setAttribute("y", result.b2 + Math.abs(result.t1 - Number(vftext.getAttribute("y"))) + "");
  405. //console.log('音阶间距', result.b2 + Math.abs(result.t1 - Number(vftext.getAttribute("y"))) + "")
  406. }
  407. });
  408. });
  409. // 如果渐弱渐强有平行的文字
  410. vflineBottom.forEach((line) => {
  411. const texts: any[] = [];
  412. if (line.nextElementSibling?.classList.contains("vf-line")) {
  413. vftextBottom.forEach((text) => {
  414. let result = collisionDetection(line as SVGAElement, text as SVGAElement, 20, 20);
  415. if (result.isCollision) {
  416. texts.push({
  417. text: text as SVGAElement,
  418. result,
  419. });
  420. }
  421. });
  422. }
  423. if (texts.length === 1) {
  424. const result = texts[0].result;
  425. const text = texts[0].text;
  426. if (result.x2 + result.w2 < result.x1) {
  427. // 左
  428. if (Math.abs(result.y2 - result.y1) > 10) {
  429. text.setAttribute("y", result.y1 + result.h2 / 2 + "");
  430. //console.log('音阶间距', result.y1 + result.h2 / 2 + "")
  431. }
  432. } else if (result.x2 > result.x1 + result.w1) {
  433. // 右
  434. if (Math.abs(result.y2 - result.y1) > 10) {
  435. text.setAttribute("y", result.y1 + result.h2 / 2 + "");
  436. //console.log('音阶间距', result.y1 + result.h2 / 2 + "")
  437. }
  438. } else {
  439. if (Math.abs(result.x2 - result.x1) < Math.abs(result.x2 + result.w2 - result.x1 - result.w1)) {
  440. // console.log(text, '有交集', '靠左')
  441. text.setAttribute("x", result.x1 - result.w2 - 5 + "");
  442. if (Math.abs(result.y2 - result.y1) > 10) {
  443. text.setAttribute("y", result.y1 + result.h2 / 2 + "");
  444. //console.log('音阶间距', result.y1 + result.h2 / 2 + "")
  445. }
  446. } else {
  447. // console.log(text, '有交集', '靠右')
  448. text.setAttribute("x", result.x1 + result.w1 + 5 + "");
  449. if (Math.abs(result.y2 - result.y1) > 10) {
  450. text.setAttribute("y", result.y1 + result.h2 / 2 + "");
  451. //console.log('音阶间距', result.y1 + result.h2 / 2 + "")
  452. }
  453. }
  454. }
  455. } else if (texts.length === 2) {
  456. const result1 = texts[0].result;
  457. const text1 = texts[0].text;
  458. const result2 = texts[1].result;
  459. const text2 = texts[1].text;
  460. text1.setAttribute("x", result1.x1 - result1.w2 - 5 + "");
  461. if (Math.abs(result1.y2 - result1.y1) > 10) {
  462. text1.setAttribute("y", result1.y1 + result1.h2 / 2 + "");
  463. //console.log('音阶间距', result1.y1 + result1.h2 / 2 + "")
  464. }
  465. text2.setAttribute("x", result2.x1 + result2.w1 + 5 + "");
  466. if (Math.abs(result2.y2 - result2.y1) > 10) {
  467. text2.setAttribute("y", result2.y1 + result2.h2 / 2 + "");
  468. //console.log('音阶间距', result2.y1 + result2.h2 / 2 + "")
  469. }
  470. } else if (texts.length === 3) {
  471. // console.log(texts)
  472. }
  473. });
  474. vftextBottom.forEach((vftext) => {
  475. vftextBottom.forEach((text) => {
  476. if (vftext.parentNode !== text.parentNode && !["marcato", "legato", "cresc.", "Cantabile"].includes(vftext.textContent as string)) {
  477. if (["marcato", "legato", "cresc.", "Cantabile"].includes(text.textContent as string)) {
  478. const result = collisionDetection(vftext as SVGAElement, text as SVGAElement, 30, 30);
  479. if (result.isCollision) {
  480. const textBBox = (vftext as SVGAElement).getBBox();
  481. text.setAttribute("x", textBBox.x + textBBox.width + 5 + "");
  482. text.setAttribute("y", textBBox.y + textBBox.height - 5 + "");
  483. //console.log('音阶间距', textBBox.y + textBBox.height - 5 + "")
  484. }
  485. } else {
  486. const result = collisionDetection(vftext as SVGAElement, text as SVGAElement);
  487. if (result.isCollision) {
  488. text.setAttribute("y", result.y1 + result.h1 + result.h2 + "");
  489. //console.log('音阶间距', result.y1 + result.h1 + result.h2 + "")
  490. }
  491. }
  492. }
  493. });
  494. });
  495. // 同一行内,多个飞线碰撞
  496. for (let index = 0; index < vfcurve.length; index++) {
  497. let nextIndex = index + 1;
  498. const cur = vfcurve[index];
  499. let next = vfcurve[nextIndex];
  500. let checkDone = false;
  501. while (nextIndex <= vfcurve.length - 1 && !checkDone) {
  502. let result = collisionDetection(cur, next);
  503. if (result.isCollision) {
  504. checkDone = true;
  505. next.style.transform = `translateY(-12px)`;
  506. } else {
  507. nextIndex = nextIndex + 1;
  508. next = vfcurve[nextIndex];
  509. }
  510. }
  511. };
  512. // 给小节添加背景色
  513. if (!state.isCreateImg && !state.isPreView) {
  514. staves.forEach((stave: any) => {
  515. const list = [
  516. Array.from(stave?.querySelectorAll(".vf-StaveSection") || []),
  517. Array.from(stave?.querySelectorAll(".vf-Volta") || []),
  518. Array.from(stave?.querySelectorAll(".vf-clef") || []),
  519. Array.from(stave?.querySelectorAll(".vf-keysignature") || []),
  520. Array.from(stave?.querySelectorAll(".vf-Repetition") || []),
  521. Array.from(stave?.getElementsByTagName("text") || []),
  522. ].flat();
  523. try {
  524. if (list.length) {
  525. list.forEach((_el: any) => {
  526. stave?.removeChild(_el)
  527. _el?.style?.setProperty("display", "none");
  528. });
  529. }
  530. } catch (error) {}
  531. const bbox = stave?.getBBox() || {};
  532. const bgColor = state.isEvaluatReport ? '#132D4C' : '#609FCF';
  533. const botColor = state.isEvaluatReport ? '#040D1E' : '#2B70A5';
  534. const rect = `<rect class="vf-custom-bg" x="${bbox.x}" y="${bbox.y}" width="${bbox.width}" height="${bbox.height}" fill=${bgColor} />`
  535. const rectBottom = `<rect class="vf-custom-bot" x="${bbox.x}" y="${bbox.y+bbox.height}" width="${bbox.width}" height="7.5" fill=${botColor} />`
  536. // const filterDom = `<defs>
  537. // <filter id="shadow">
  538. // <feDropShadow dx="5" dy="5" stdDeviation="3" flood-color="black" />
  539. // </filter>
  540. // </defs>`
  541. const customG = `<g>${rect}${rectBottom}</g>`
  542. try {
  543. if (list.length) {
  544. list.forEach((_el: any) => {
  545. stave?.appendChild(_el)
  546. _el?.style?.removeProperty("display");
  547. });
  548. }
  549. } catch (error) {}
  550. stave.innerHTML = customG + stave.innerHTML;
  551. });
  552. state.vfmeasures = state.vfmeasures.concat(vfmeasures);
  553. }
  554. }
  555. if (!state.isCombineRender && state.isSingleLine) {
  556. transSinglePage();
  557. }
  558. // setTimeout(() => this.resetGlobalText());
  559. };
  560. // 一行谱时,五线谱/简谱的谱面staffLine,居中显示
  561. const transSinglePage = () => {
  562. if (state.isSingleLine && !state.isSimplePage) {
  563. const svgPage = document?.getElementById('osmdSvgPage1')?.getBoundingClientRect();
  564. const staffLine = document?.querySelector('.staffline')?.getBoundingClientRect();
  565. if (svgPage && staffLine && svgPage.height > 200 && state.platform !== 'PC') {
  566. // 需要上移的距离
  567. // console.log('need',svgPage.height,staffLine.height)
  568. const rate = svgPage.height > 400 ? 1.2 : 2;
  569. const needTransTop = (svgPage.height - staffLine.height) / rate;
  570. // @ts-ignore
  571. document.getElementById('osmdSvgPage1').style.transform = `translateY(-${needTransTop}px)`;
  572. // document.querySelector('.staffline').style.transform = `translateY(-${needTransTop}px)`;
  573. // const musicLine = document.querySelector('.staffline').querySelector('.vf-measure').querySelector('.vf-custom-bg').getBoundingClientRect();
  574. // const needTransDistance = svgPage.height / 2 - (musicLine.top - svgPage.top)
  575. // console.log('svg移动距离',needTransDistance)
  576. // // @ts-ignore
  577. // document.getElementById('osmdSvgPage1').style.transform = `translateY(${needTransDistance}px)`
  578. }
  579. }
  580. }
  581. // 技巧文本
  582. const resetGlobalText = () => {
  583. const svg = container.value.querySelector("svg");
  584. if (!svg) return;
  585. const svgBBox = svg.getBBox();
  586. let vfstavetempo: SVGAElement[] = Array.from(container.value.querySelectorAll(".vf-stavetempo")).reduce((eles: SVGAElement[], value: any) => {
  587. if (eles.find((n) => n.outerHTML === value.outerHTML)) value?.parentNode?.removeChild(value);
  588. else eles.push(value);
  589. return eles;
  590. }, []);
  591. const staffline: SVGAElement[] = Array.from(container.value.querySelectorAll(".staffline"));
  592. const vfmeasures: SVGAElement[] = Array.from(container.value.querySelectorAll(".staffline > .vf-measure"));
  593. const vftexts: SVGAElement[] = Array.from(container.value.querySelectorAll(".staffline > .vf-text"));
  594. const vfcurves: SVGAElement[] = Array.from(container.value.querySelectorAll(".staffline > .vf-curve"));
  595. vfstavetempo.forEach((child: SVGAElement) => {
  596. let _y = 0;
  597. [...vfmeasures, ...vftexts, ...vfcurves].forEach((ele) => {
  598. const result = collisionDetection(child as SVGAElement, ele);
  599. if (result.isCollision && (result.b1 < result.b2 || result.r1 > result.l2 || result.l1 < result.r2)) {
  600. _y = Math.min(_y, result.t2 - result.b1);
  601. }
  602. });
  603. if (_y !== 0) {
  604. child.style.transform = `translateY(${_y}px)`;
  605. }
  606. const childBBox = child.getBBox();
  607. const rightY = (childBBox.x + childBBox.width) * 0.7 - Number(svg.getAttribute("width"));
  608. if (rightY > 0) {
  609. [...staffline, ...vfstavetempo].forEach((tempo) => {
  610. if (child != tempo) {
  611. const result = collisionDetection(child as SVGAElement, tempo, Math.abs(rightY), Math.abs(_y));
  612. if (result.isCollision) {
  613. _y = result.t2 - result.b1;
  614. }
  615. }
  616. });
  617. child.style.transform = `translate(-${rightY / 0.7}px,${_y}px)`;
  618. }
  619. });
  620. if (svgBBox.y < 0) {
  621. svg.setAttribute("height", Number(svg.getAttribute("height")) - svgBBox.y + 10);
  622. }
  623. };
  624. // 碰撞检测
  625. const collisionDetection = (a: SVGAElement, b: SVGAElement, distance: number = 0, distance_y: number = 0) => {
  626. const abbox = a.getBBox();
  627. const bbbox = b.getBBox();
  628. let t1 = abbox.y - distance_y;
  629. let l1 = abbox.x - distance;
  630. let r1 = abbox.x + abbox.width + distance;
  631. let b1 = abbox.y + abbox.height + distance_y;
  632. let t2 = bbbox.y;
  633. let l2 = bbbox.x;
  634. let r2 = bbbox.x + bbbox.width;
  635. let b2 = bbbox.y + bbbox.height;
  636. if (b1 < t2 || l1 > r2 || t1 > b2 || r1 < l2) {
  637. // 表示没碰上
  638. return {
  639. isCollision: false,
  640. t1,
  641. l1,
  642. r1,
  643. b1,
  644. t2,
  645. l2,
  646. r2,
  647. b2,
  648. x1: abbox.x,
  649. y1: abbox.y,
  650. x2: bbbox.x,
  651. y2: bbbox.y,
  652. h1: abbox.height,
  653. h2: bbbox.height,
  654. w1: abbox.width,
  655. w2: bbbox.width,
  656. };
  657. } else {
  658. return {
  659. isCollision: true,
  660. t1,
  661. l1,
  662. r1,
  663. b1,
  664. t2,
  665. l2,
  666. r2,
  667. b2,
  668. x1: abbox.x,
  669. y1: abbox.y,
  670. x2: bbbox.x,
  671. y2: bbbox.y,
  672. h1: abbox.height,
  673. h2: bbbox.height,
  674. w1: abbox.width,
  675. w2: bbbox.width,
  676. };
  677. }
  678. };
  679. /** 全局曲谱配置 */
  680. export const setGlobalMusicSheet = () => {
  681. const partIndex = state.partIndex + ""
  682. /** 延音线方向问题 start */
  683. const stavetieList = [
  684. {id: '12644', part_index: '25', direction: 1}
  685. ]
  686. const tieItem = stavetieList.find(({id, part_index}) => {
  687. return id == state.cbsExamSongId && part_index == partIndex
  688. })
  689. setGlobalData('tieDirection', tieItem ? tieItem.direction : undefined)
  690. /** 延音线方向问题 end */
  691. const graceList = [
  692. {id: '3509', part_index: '16', direction: 1}
  693. ]
  694. const graceItem = graceList.find(({id, part_index}) => {
  695. return id == state.cbsExamSongId && part_index == partIndex
  696. })
  697. if (graceItem){
  698. setGlobalData('graceCustom', {direction: graceItem.direction})
  699. }
  700. const bassDrumList = [
  701. {id: '3030', part_index: '17', line: 4},
  702. {id: '12704', part_index: '23', line: 3}
  703. ]
  704. const bassDrumItem = bassDrumList.find(({id, part_index}) => {
  705. return id == state.cbsExamSongId && part_index == partIndex
  706. })
  707. if (bassDrumItem){
  708. setGlobalData('customBassDrum', bassDrumItem.line)
  709. }
  710. /** 打击乐多声部,双声部休止符重叠 end */
  711. /** 符杆朝向 */
  712. const stemDirectionList = [
  713. {
  714. id: '11654',
  715. part_index: '16',
  716. stemNotes: [
  717. {id: 124, direction: 0},
  718. {id: 125, direction: 0},
  719. {id: 126, direction: 0},
  720. {id: 127, direction: 0},
  721. {id: 128, direction: 0}
  722. ]
  723. },
  724. {
  725. id: '3581',
  726. part_index: '4',
  727. stemNotes: [
  728. {id: 380, direction: 1},
  729. ]
  730. },
  731. {
  732. id: '3470',
  733. part_index: '0',
  734. stemNotes: [
  735. {id: 36, direction: 1},
  736. {id: 37, direction: 1},
  737. ]
  738. },
  739. {
  740. id: '3470',
  741. part_index: '11',
  742. stemNotes: [
  743. {id: 33, direction: 1},
  744. {id: 56, direction: 1},
  745. ]
  746. },
  747. {
  748. id: '12644',
  749. part_index: '22',
  750. stemNotes: [
  751. {id: 22, direction: 1},
  752. {id: 26, direction: 1},
  753. {id: 135, direction: 1},
  754. {id: 163, direction: 1},
  755. {id: 199, direction: 1},
  756. {id: 204, direction: 1},
  757. {id: 206, direction: 1},
  758. {id: 208, direction: 1},
  759. {id: 210, direction: 1},
  760. {id: 213, direction: 1},
  761. ]
  762. },
  763. {
  764. id: '12303',
  765. part_index: '18',
  766. stemNotes: [
  767. {id: 1, direction: 1},
  768. {id: 4, direction: 1},
  769. {id: 6, direction: 1},
  770. {id: 9, direction: 1},
  771. {id: 12, direction: 1},
  772. {id: 14, direction: 1},
  773. ]
  774. },
  775. {
  776. id: '12669',
  777. part_index: '24',
  778. stemNotes: [
  779. {id: 65, direction: 1},
  780. {id: 296, direction: 1},
  781. {id: 298, direction: 1},
  782. {id: 300, direction: 1},
  783. {id: 338, direction: 1},
  784. ]
  785. },
  786. {
  787. id: '12420',
  788. part_index: '21',
  789. stemNotes: [
  790. {id: 614, direction: 0},
  791. {id: 617, direction: 0},
  792. {id: 619, direction: 0},
  793. {id: 621, direction: 0},
  794. ]
  795. },
  796. {
  797. id: '12711',
  798. part_index: '22',
  799. stemNotes: []
  800. },
  801. {
  802. id: '12973',
  803. part_index: '21',
  804. stemNotes: [
  805. {id: 619, direction: 1},
  806. {id: 622, direction: 1},
  807. {id: 745, direction: 1},
  808. ]
  809. },
  810. ]
  811. const stemDirectionItem = stemDirectionList.find(({id, part_index}) => {
  812. return id == state.cbsExamSongId && part_index == partIndex
  813. })
  814. if (stemDirectionItem) {
  815. setGlobalData('stemDirectionNote', stemDirectionItem.stemNotes)
  816. }
  817. /** vfcure */
  818. const vfcurveList = [
  819. {
  820. id: '12711',
  821. part_index: '4',
  822. vfcurve: [
  823. {MeasureNumberXML: 25, index: 1, bezierEndControlPt: {y: -2}},
  824. {MeasureNumberXML: 33, index: 1, bezierEndControlPt: {y: -2}},
  825. ]
  826. },
  827. {
  828. id: '12059',
  829. part_index: '0',
  830. vfcurve: [
  831. {MeasureNumberXML: 15, bezierEndControlPt: {y: 2.8}, bezierEndPt:{y: 1.1}},
  832. {MeasureNumberXML: 16, bezierEndControlPt: {y: -1}},
  833. {MeasureNumberXML: 19, index: 1, bezierEndControlPt: {y: 2}},
  834. {MeasureNumberXML: 20, bezierEndControlPt: {y: -1}},
  835. {MeasureNumberXML: 42, index: 1, bezierEndControlPt: {y: -1.5}, bezierStartControlPt: {y: -1.5}},
  836. {MeasureNumberXML: 46, index: 3, bezierEndControlPt: {y: -1.5}, bezierStartControlPt: {y: -1.5}},
  837. ]
  838. },
  839. {
  840. id: '12668',
  841. part_index: '11',
  842. vfcurve: [
  843. {MeasureNumberXML: 8, index: 2, bezierEndControlPt: {y: -3}, bezierStartControlPt:{y: -3}, bezierEndPt:{y: -1}},
  844. ]
  845. },
  846. {
  847. id: '11976',
  848. part_index: '0',
  849. vfcurve: [
  850. {MeasureNumberXML: 14, index: 4, bezierEndControlPt: {y: -3}},
  851. {MeasureNumberXML: 14, index: 1, bezierEndPt: {y: 1.5}, bezierEndControlPt: {y: 1}},
  852. ]
  853. },
  854. ]
  855. const vfcurveItem = vfcurveList.find(({id, part_index}) => {
  856. return id == state.cbsExamSongId && part_index == partIndex
  857. })
  858. if (vfcurveItem) {
  859. setGlobalData('vfcurveItem', vfcurveItem.vfcurve)
  860. }
  861. /** drum set声部 重音 */
  862. const customArtPositionList = [
  863. {id: '12644', part_index: '25'}
  864. ]
  865. const customArtPositionItem = customArtPositionList.find(({id, part_index}) => {
  866. return id == state.cbsExamSongId && part_index == partIndex
  867. })
  868. if (customArtPositionItem) {
  869. setGlobalData('customArtPosition', true)
  870. }
  871. /** 全声部声部 - & 全音符 */
  872. const customTenutoList = [
  873. {id: '12645', part_index: '5'}
  874. ]
  875. const customTenutoItem = customTenutoList.find(({id, part_index}) => {
  876. return id == state.cbsExamSongId && part_index == partIndex
  877. })
  878. if (customTenutoItem) {
  879. setGlobalData('customTenutoItem', true)
  880. }
  881. /** 全声部声部 > */
  882. const customAccentList = [
  883. {id: '12711', part_index: '22'},
  884. {id: '12711', part_index: '25'},
  885. ]
  886. const customAccentItem = customAccentList.find(({id, part_index}) => {
  887. return id == state.cbsExamSongId && part_index == partIndex
  888. })
  889. if (customAccentItem || state.isEvxml) {
  890. setGlobalData('customAccentItem', true)
  891. }
  892. /** 全声部声部 + */
  893. const customLefthandpizzicatoList = [
  894. {id: '12711', part_index: '25'},
  895. {id: '7755', part_index: '10'},
  896. {id: '6226', part_index: '16'},
  897. ]
  898. const customLefthandpizzicatoItem = customLefthandpizzicatoList.find(({id, part_index}) => {
  899. return id == state.cbsExamSongId && part_index == partIndex
  900. })
  901. if (customLefthandpizzicatoItem) {
  902. setGlobalData('customLefthandpizzicatoItem', true)
  903. }
  904. }
  905. /** 设置自定义渐慢 */
  906. export const setCustomGradual = () => {
  907. if (state.gradualTimes) {
  908. const detailId = state.cbsExamSongId + "";
  909. const partIndex = state.partIndex + "";
  910. if (["12280"].includes(detailId) && ["24"].includes(partIndex)) {
  911. state.gradualTimes["8"] = "00:26:10";
  912. state.gradualTimes["66"] = "01:53:35";
  913. state.gradualTimes["90"] = "02:41:40";
  914. }
  915. }
  916. };
  917. /** 设置自定义音符数据 */
  918. export const setCustomNoteRealValue = () => {
  919. const detailId = state.cbsExamSongId + "";
  920. const partIndex = state.partIndex + "";
  921. if (["2670"].includes(detailId)) {
  922. customData.customNoteRealValue = {
  923. 0: 0.03125,
  924. };
  925. }
  926. if (["12673"].includes(detailId) && ['22'].includes(partIndex)) {
  927. customData.customNoteRealValue = {
  928. 208: 0.125,
  929. };
  930. }
  931. if (["12667", "12673"].includes(detailId)){
  932. customData.customNoteCurrentTime = true
  933. }
  934. };