<commit_before>import {GraphicalMeasure} from "./GraphicalMeasure";
import {GraphicalMusicPage} from "./GraphicalMusicPage";
import {EngravingRules} from "./EngravingRules";
import {RhythmInstruction} from "../VoiceData/Instructions/RhythmInstruction";
import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
import {ClefInstruction} from "../VoiceData/Instructions/ClefInstruction";
import {SourceMeasure} from "../VoiceData/SourceMeasure";
import {MusicSystem} from "./MusicSystem";
import {BoundingBox} from "./BoundingBox";
import {Staff} from "../VoiceData/Staff";
import {Instrument} from "../Instrument";
import {PointF2D} from "../../Common/DataObjects/PointF2D";
import {StaffLine} from "./StaffLine";
import {GraphicalLine} from "./GraphicalLine";
import {SourceStaffEntry} from "../VoiceData/SourceStaffEntry";
import {AbstractNotationInstruction} from "../VoiceData/Instructions/AbstractNotationInstruction";
import {SystemLinesEnum} from "./SystemLinesEnum";
import {GraphicalMusicSheet} from "./GraphicalMusicSheet";
import {MusicSheetCalculator} from "./MusicSheetCalculator";
import {MidiInstrument} from "../VoiceData/Instructions/ClefInstruction";
import {CollectionUtil} from "../../Util/CollectionUtil";
import {SystemLinePosition} from "./SystemLinePosition";

export class MusicSystemBuilder {
    private measureList: GraphicalMeasure[][];
    private graphicalMusicSheet: GraphicalMusicSheet;
    private currentMusicPage: GraphicalMusicPage;
    private currentPageHeight: number;
    private currentSystemParams: SystemBuildParameters;
    private numberOfVisibleStaffLines: number;
    private rules: EngravingRules;
    private measureListIndex: number;

    /**
     * Does the mapping from the currently visible staves to the global staff-list of the music sheet.
     */
    private visibleStaffIndices: number[];
    private activeRhythm: RhythmInstruction[];
    private activeKeys: KeyInstruction[];
    private activeClefs: ClefInstruction[];
    private globalSystemIndex: number = 0;
    private leadSheet: boolean = false;

    public initialize(
        graphicalMusicSheet: GraphicalMusicSheet, measureList: GraphicalMeasure[][], numberOfStaffLines: number): void {
        this.leadSheet = graphicalMusicSheet.LeadSheet;
        this.graphicalMusicSheet = graphicalMusicSheet;
        this.rules = this.graphicalMusicSheet.ParentMusicSheet.rules;
        this.measureList = measureList;
        this.currentMusicPage = this.createMusicPage();
        this.currentPageHeight = 0.0;
        this.numberOfVisibleStaffLines = numberOfStaffLines;
        this.activeRhythm = new Array(this.numberOfVisibleStaffLines);
        this.activeKeys = new Array(this.numberOfVisibleStaffLines);
        this.activeClefs = new Array(this.numberOfVisibleStaffLines);
        this.initializeActiveInstructions(this.measureList[0]);
    }

    public buildMusicSystems(): void {
        let previousMeasureEndsSystem: boolean = false;
        const systemMaxWidth: number = this.getFullPageSystemWidth();
        this.measureListIndex = 0;
        this.currentSystemParams = new SystemBuildParameters();

        // the first System - create also its Labels
        this.currentSystemParams.currentSystem = this.initMusicSystem();
        this.layoutSystemStaves();
        this.addSystemLabels();
        this.currentPageHeight += this.currentSystemParams.currentSystem.PositionAndShape.RelativePosition.y;

        let numberOfMeasures: number = 0;
        for (let idx: number = 0, len: number = this.measureList.length; idx < len; ++idx) {
            if (this.measureList[idx].length > 0) {
                numberOfMeasures++;
            }
        }

        // go through measures and add to system until system gets too long -> finish system and start next system.
        while (this.measureListIndex < numberOfMeasures) {
            const graphicalMeasures: GraphicalMeasure[] = this.measureList[this.measureListIndex];
            for (let idx: number = 0, len: number = graphicalMeasures.length; idx < len; ++idx) {
                graphicalMeasures[idx].resetLayout();
            }
            const sourceMeasure: SourceMeasure = graphicalMeasures[0].parentSourceMeasure;
            const sourceMeasureEndsSystem: boolean = sourceMeasure.BreakSystemAfter;
            const isSystemStartMeasure: boolean = this.currentSystemParams.IsSystemStartMeasure();
            const isFirstSourceMeasure: boolean = sourceMeasure === this.graphicalMusicSheet.ParentMusicSheet.getFirstSourceMeasure();
            let currentMeasureBeginInstructionsWidth: number = this.rules.MeasureLeftMargin;
            let currentMeasureEndInstructionsWidth: number = 0;

            // calculate the current Measure Width:
            // The width of a measure is build up from
            // 1. the begin instructions (clef, Key, Rhythm),
            // 2. the staff entries (= notes) and
            // 3. the end instructions (actually only clefs)
            const measureStartLine: SystemLinesEnum = this.getMeasureStartLine();
            currentMeasureBeginInstructionsWidth += this.getLineWidth(graphicalMeasures[0], measureStartLine, isSystemStartMeasure);
            if (!this.leadSheet) {
                currentMeasureBeginInstructionsWidth += this.addBeginInstructions(graphicalMeasures, isSystemStartMeasure, isFirstSourceMeasure);
                currentMeasureEndInstructionsWidth += this.addEndInstructions(graphicalMeasures);<commit_msg>add comment (line break)<commit_after>import {GraphicalMeasure} from "./GraphicalMeasure";
import {GraphicalMusicPage} from "./GraphicalMusicPage";
import {EngravingRules} from "./EngravingRules";
import {RhythmInstruction} from "../VoiceData/Instructions/RhythmInstruction";
import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
import {ClefInstruction} from "../VoiceData/Instructions/ClefInstruction";
import {SourceMeasure} from "../VoiceData/SourceMeasure";
import {MusicSystem} from "./MusicSystem";
import {BoundingBox} from "./BoundingBox";
import {Staff} from "../VoiceData/Staff";
import {Instrument} from "../Instrument";
import {PointF2D} from "../../Common/DataObjects/PointF2D";
import {StaffLine} from "./StaffLine";
import {GraphicalLine} from "./GraphicalLine";
import {SourceStaffEntry} from "../VoiceData/SourceStaffEntry";
import {AbstractNotationInstruction} from "../VoiceData/Instructions/AbstractNotationInstruction";
import {SystemLinesEnum} from "./SystemLinesEnum";
import {GraphicalMusicSheet} from "./GraphicalMusicSheet";
import {MusicSheetCalculator} from "./MusicSheetCalculator";
import {MidiInstrument} from "../VoiceData/Instructions/ClefInstruction";
import {CollectionUtil} from "../../Util/CollectionUtil";
import {SystemLinePosition} from "./SystemLinePosition";

export class MusicSystemBuilder {
    private measureList: GraphicalMeasure[][];
    private graphicalMusicSheet: GraphicalMusicSheet;
    private currentMusicPage: GraphicalMusicPage;
    private currentPageHeight: number;
    private currentSystemParams: SystemBuildParameters;
    private numberOfVisibleStaffLines: number;
    private rules: EngravingRules;
    private measureListIndex: number;

    /**
     * Does the mapping from the currently visible staves to the global staff-list of the music sheet.
     */
    private visibleStaffIndices: number[];
    private activeRhythm: RhythmInstruction[];
    private activeKeys: KeyInstruction[];
    private activeClefs: ClefInstruction[];
    private globalSystemIndex: number = 0;
    private leadSheet: boolean = false;

    public initialize(
        graphicalMusicSheet: GraphicalMusicSheet, measureList: GraphicalMeasure[][], numberOfStaffLines: number): void {
        this.leadSheet = graphicalMusicSheet.LeadSheet;
        this.graphicalMusicSheet = graphicalMusicSheet;
        this.rules = this.graphicalMusicSheet.ParentMusicSheet.rules;
        this.measureList = measureList;
        this.currentMusicPage = this.createMusicPage();
        this.currentPageHeight = 0.0;
        this.numberOfVisibleStaffLines = numberOfStaffLines;
        this.activeRhythm = new Array(this.numberOfVisibleStaffLines);
        this.activeKeys = new Array(this.numberOfVisibleStaffLines);
        this.activeClefs = new Array(this.numberOfVisibleStaffLines);
        this.initializeActiveInstructions(this.measureList[0]);
    }

    public buildMusicSystems(): void {
        let previousMeasureEndsSystem: boolean = false;
        const systemMaxWidth: number = this.getFullPageSystemWidth();
        this.measureListIndex = 0;
        this.currentSystemParams = new SystemBuildParameters();

        // the first System - create also its Labels
        this.currentSystemParams.currentSystem = this.initMusicSystem();
        this.layoutSystemStaves();
        this.addSystemLabels();
        this.currentPageHeight += this.currentSystemParams.currentSystem.PositionAndShape.RelativePosition.y;

        let numberOfMeasures: number = 0;
        for (let idx: number = 0, len: number = this.measureList.length; idx < len; ++idx) {
            if (this.measureList[idx].length > 0) {
                numberOfMeasures++;
            }
        }

        // go through measures and add to system until system gets too long -> finish system and start next system [line break, new system].
        while (this.measureListIndex < numberOfMeasures) {
            const graphicalMeasures: GraphicalMeasure[] = this.measureList[this.measureListIndex];
            for (let idx: number = 0, len: number = graphicalMeasures.length; idx < len; ++idx) {
                graphicalMeasures[idx].resetLayout();
            }
            const sourceMeasure: SourceMeasure = graphicalMeasures[0].parentSourceMeasure;
            const sourceMeasureEndsSystem: boolean = sourceMeasure.BreakSystemAfter;
            const isSystemStartMeasure: boolean = this.currentSystemParams.IsSystemStartMeasure();
            const isFirstSourceMeasure: boolean = sourceMeasure === this.graphicalMusicSheet.ParentMusicSheet.getFirstSourceMeasure();
            let currentMeasureBeginInstructionsWidth: number = this.rules.MeasureLeftMargin;
            let currentMeasureEndInstructionsWidth: number = 0;

            // calculate the current Measure Width:
            // The width of a measure is build up from
            // 1. the begin instructions (clef, Key, Rhythm),
            // 2. the staff entries (= notes) and
            // 3. the end instructions (actually only clefs)
            const measureStartLine: SystemLinesEnum = this.getMeasureStartLine();
            currentMeasureBeginInstructionsWidth += this.getLineWidth(graphicalMeasures[0], measureStartLine, isSystemStartMeasure);
            if (!this.leadSheet) {
                currentMeasureBeginInstructionsWidth += this.addBeginInstructions(graphicalMeasures, isSystemStartMeasure, isFirstSourceMeasure);
                currentMeasureEndInstructionsWidth += this.addEndInstructions(graphicalMeasures);
{'fraction_non_alphanumeric': 0.09538936286077328, 'fraction_numerical': 0.0030858595026320565, 'mean_word_length': 3.7495689655172413, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5824091', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2720, 'n_words': 694}