All files / music-core/parser DirectionParser.ts

25.73% Statements 61/237
100% Branches 0/0
0% Functions 0/10
25.73% Lines 61/237

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 2381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                               1x 1x 1x 1x 1x                                           1x 1x 1x 1x 1x             1x 1x 1x 1x 1x                     1x 1x 1x 1x 1x           1x 1x 1x 1x 1x             1x 1x 1x 1x 1x             1x 1x 1x 1x 1x                                                                                                       1x 1x 1x 1x 1x                                   1x 1x 1x 1x 1x                                                                
/**
 * @file 方向解析器
 * @description 解析 MusicXML 中的 direction 元素
 */
 
import { 
  Direction, DirectionTypeContent, Sound,
  Metronome, Dynamics, Wedge, Words, Rehearsal, OctaveShift, Pedal 
} from '../models/Direction';
import { DynamicType, WedgeType, Placement } from '../models/types';
import { getChild, getChildren, getTextContent, getIntContent, getNumberContent, getAttribute, getBooleanAttribute, hasChild } from '../utils/XMLUtils';
 
/**
 * 解析 metronome 元素
 */
export function parseMetronome(element: Element): Metronome {
  const beatUnit = getTextContent(element, 'beat-unit') ?? 'quarter';
  const beatUnitDot = hasChild(element, 'beat-unit-dot');
  const perMinute = getIntContent(element, 'per-minute');
  const parentheses = getBooleanAttribute(element, 'parentheses');
  
  // 检查是否有等于另一个节拍的情况
  let beatUnitEquals: Metronome['beatUnitEquals'];
  const beatUnitElements = getChildren(element, 'beat-unit');
  if (beatUnitElements.length > 1) {
    beatUnitEquals = {
      beatUnit: beatUnitElements[1].textContent ?? 'quarter',
      beatUnitDot: getChildren(element, 'beat-unit-dot').length > 1
    };
  }
  
  return {
    beatUnit,
    beatUnitDot,
    perMinute,
    beatUnitEquals,
    parentheses
  };
}
 
/**
 * 解析 dynamics 元素
 */
export function parseDynamics(element: Element): Dynamics[] {
  const dynamics: Dynamics[] = [];
  const placement = getAttribute(element, 'placement') as Placement | undefined;
  
  // 力度标记通常是空元素,标签名就是力度类型
  for (const child of element.children) {
    dynamics.push({
      type: child.tagName as DynamicType,
      placement
    });
  }
  
  // 如果没有子元素但有文字内容
  if (dynamics.length === 0 && element.textContent?.trim()) {
    dynamics.push({
      type: element.textContent.trim() as DynamicType,
      placement
    });
  }
  
  return dynamics;
}
 
/**
 * 解析 wedge 元素
 */
export function parseWedge(element: Element): Wedge {
  const type = getAttribute(element, 'type') as WedgeType;
  const number = getIntContent(element, 'number');
  const spread = getNumberContent(element, 'spread');
  
  return { type, number, spread };
}
 
/**
 * 解析 words 元素
 */
export function parseWords(element: Element): Words {
  return {
    text: element.textContent?.trim() ?? '',
    fontFamily: getAttribute(element, 'font-family'),
    fontSize: getAttribute(element, 'font-size'),
    fontWeight: getAttribute(element, 'font-weight') as 'normal' | 'bold' | undefined,
    fontStyle: getAttribute(element, 'font-style') as 'normal' | 'italic' | undefined,
    placement: getAttribute(element, 'placement') as Placement | undefined,
    lang: getAttribute(element, 'xml:lang')
  };
}
 
/**
 * 解析 rehearsal 元素
 */
export function parseRehearsal(element: Element): Rehearsal {
  return {
    text: element.textContent?.trim() ?? '',
    enclosure: getAttribute(element, 'enclosure') as Rehearsal['enclosure']
  };
}
 
/**
 * 解析 octave-shift 元素
 */
export function parseOctaveShift(element: Element): OctaveShift {
  return {
    type: getAttribute(element, 'type') as 'up' | 'down' | 'stop' | 'continue',
    size: getIntContent(element, 'size'),
    number: getIntContent(element, 'number')
  };
}
 
/**
 * 解析 pedal 元素
 */
export function parsePedal(element: Element): Pedal {
  return {
    type: getAttribute(element, 'type') as Pedal['type'],
    line: getBooleanAttribute(element, 'line'),
    sign: getBooleanAttribute(element, 'sign')
  };
}
 
/**
 * 解析 direction-type 元素
 */
export function parseDirectionType(element: Element): DirectionTypeContent {
  const content: DirectionTypeContent = {};
  
  for (const child of element.children) {
    switch (child.tagName) {
      case 'metronome':
        content.metronome = parseMetronome(child);
        break;
      case 'dynamics':
        content.dynamics = parseDynamics(child);
        break;
      case 'wedge':
        content.wedge = parseWedge(child);
        break;
      case 'words':
        content.words = content.words ?? [];
        content.words.push(parseWords(child));
        break;
      case 'rehearsal':
        content.rehearsal = parseRehearsal(child);
        break;
      case 'octave-shift':
        content.octaveShift = parseOctaveShift(child);
        break;
      case 'pedal':
        content.pedal = parsePedal(child);
        break;
      case 'segno':
        content.segno = true;
        break;
      case 'coda':
        content.coda = true;
        break;
      case 'bracket':
        content.bracket = {
          type: getAttribute(child, 'type') as 'start' | 'stop',
          lineType: getAttribute(child, 'line-type')
        };
        break;
      case 'dashes':
        content.dashes = {
          type: getAttribute(child, 'type') as 'start' | 'stop'
        };
        break;
      case 'symbol':
        content.symbol = child.textContent?.trim();
        break;
    }
  }
  
  return content;
}
 
/**
 * 解析 sound 元素
 */
export function parseSound(element: Element): Sound {
  return {
    tempo: getNumberContent(element, 'tempo') ?? (
      getAttribute(element, 'tempo') ? parseFloat(getAttribute(element, 'tempo')!) : undefined
    ),
    dynamics: getNumberContent(element, 'dynamics') ?? (
      getAttribute(element, 'dynamics') ? parseFloat(getAttribute(element, 'dynamics')!) : undefined
    ),
    dacapo: getBooleanAttribute(element, 'dacapo'),
    segno: getAttribute(element, 'segno'),
    dalsegno: getAttribute(element, 'dalsegno'),
    coda: getAttribute(element, 'coda'),
    tocoda: getAttribute(element, 'tocoda'),
    fine: getAttribute(element, 'fine'),
    forwardRepeat: getBooleanAttribute(element, 'forward-repeat'),
    timeOnly: getAttribute(element, 'time-only')
  };
}
 
/**
 * 解析 direction 元素
 */
export function parseDirection(element: Element): Direction {
  // 解析所有 direction-type
  const directionTypeElements = getChildren(element, 'direction-type');
  const directionTypes = directionTypeElements.map(parseDirectionType);
  
  // 位置
  const placement = getAttribute(element, 'placement') as Placement | undefined;
  
  // directive
  const directive = getBooleanAttribute(element, 'directive');
  
  // 声部和谱表
  const voice = getIntContent(element, 'voice');
  const staff = getIntContent(element, 'staff');
  
  // 偏移
  const offset = getIntContent(element, 'offset');
  
  // sound
  const soundEl = getChild(element, 'sound');
  const sound = soundEl ? parseSound(soundEl) : undefined;
  
  return new Direction({
    directionTypes,
    placement,
    directive,
    voice,
    staff,
    offset,
    sound
  });
}