<commit_before>package com.xenoage.zong.io.musicxml.in.readers;

import static com.xenoage.utils.iterators.It.it;
import lombok.RequiredArgsConstructor;

import com.xenoage.utils.annotations.MaybeNull;
import com.xenoage.zong.core.format.Break;
import com.xenoage.zong.core.format.StaffLayout;
import com.xenoage.zong.core.format.SystemLayout;
import com.xenoage.zong.core.header.ScoreHeader;
import com.xenoage.zong.core.music.layout.PageBreak;
import com.xenoage.zong.core.music.layout.SystemBreak;
import com.xenoage.zong.musicxml.types.MxlPrint;
import com.xenoage.zong.musicxml.types.MxlStaffLayout;
import com.xenoage.zong.musicxml.types.MxlSystemLayout;
import com.xenoage.zong.musicxml.types.attributes.MxlPrintAttributes;
import com.xenoage.zong.musicxml.types.enums.MxlYesNo;
import com.xenoage.zong.musicxml.types.groups.MxlLayout;

/**
 * Reads a {@link MxlPrint} to the {@link Context}.
 * 
 * @author <NAME>
 */
@RequiredArgsConstructor
public class PrintReader {

	private final MxlPrint mxlPrint;
	
	
	public void readToContext(Context context) {
		ScoreHeader header = context.getScore().getHeader();
		int measure = context.getMp().measure;

		//system and page break
		Break break_ = readBreak();
		if (break_ != null) {
			//MusicXML print is in the first broken measure, but we
			//store the break in the last measure before the break (thus -1)
			int breakMeasure = measure - 1;
			context.writeColumnElement(break_, breakMeasure);
		}

		//we assume that custom system layout information is just used in combination with
		//forced system/page breaks. so we ignore system-layout elements which are not combined
		//with system/page breaks.
		//the first measure of a score is also ok.
		if (measure == 0 || break_ != null) {

			//first page or new page?
			boolean isPageBreak = break_ != null && break_.getPageBreak() == PageBreak.NewPage;
			boolean isPageStarted = (measure == 0 || isPageBreak);
			if (isPageBreak) {
				//increment page index
				context.incPageIndex();
			}

			//first system or new system?
			boolean isSystemBreak = isPageBreak ||
				(break_ != null && break_.getSystemBreak() == SystemBreak.NewSystem);
			if (isSystemBreak) {
				//increment system index 
				context.incSystemIndex();
			}

			//read system layout, if there
			SystemLayout systemLayout = readSystemLayout(isPageStarted, context.getTenthMm());
			if (systemLayout != null)
				header.setSystemLayout(context.getSystemIndex(), systemLayout);
			<commit_msg>MusicXML import: do not crash when a line break is in the first measure<commit_after>package com.xenoage.zong.io.musicxml.in.readers;

import static com.xenoage.utils.iterators.It.it;
import lombok.RequiredArgsConstructor;

import com.xenoage.utils.annotations.MaybeNull;
import com.xenoage.zong.core.format.Break;
import com.xenoage.zong.core.format.StaffLayout;
import com.xenoage.zong.core.format.SystemLayout;
import com.xenoage.zong.core.header.ScoreHeader;
import com.xenoage.zong.core.music.layout.PageBreak;
import com.xenoage.zong.core.music.layout.SystemBreak;
import com.xenoage.zong.musicxml.types.MxlPrint;
import com.xenoage.zong.musicxml.types.MxlStaffLayout;
import com.xenoage.zong.musicxml.types.MxlSystemLayout;
import com.xenoage.zong.musicxml.types.attributes.MxlPrintAttributes;
import com.xenoage.zong.musicxml.types.enums.MxlYesNo;
import com.xenoage.zong.musicxml.types.groups.MxlLayout;

/**
 * Reads a {@link MxlPrint} to the {@link Context}.
 * 
 * @author <NAME>
 */
@RequiredArgsConstructor
public class PrintReader {

	private final MxlPrint mxlPrint;
	
	
	public void readToContext(Context context) {
		ScoreHeader header = context.getScore().getHeader();
		int measure = context.getMp().measure;

		//system and page break
		Break break_ = readBreak();
		if (break_ != null) {
			//MusicXML print is in the first broken measure, but we
			//store the break in the last measure before the break (thus -1)
			int breakMeasure = measure - 1;
			if (breakMeasure >= 0) //ignore, when in the first measure
				context.writeColumnElement(break_, breakMeasure);
		}

		//we assume that custom system layout information is just used in combination with
		//forced system/page breaks. so we ignore system-layout elements which are not combined
		//with system/page breaks.
		//the first measure of a score is also ok.
		if (measure == 0 || break_ != null) {

			//first page or new page?
			boolean isPageBreak = break_ != null && break_.getPageBreak() == PageBreak.NewPage;
			boolean isPageStarted = (measure == 0 || isPageBreak);
			if (isPageBreak) {
				//increment page index
				context.incPageIndex();
			}

			//first system or new system?
			boolean isSystemBreak = isPageBreak ||
				(break_ != null && break_.getSystemBreak() == SystemBreak.NewSystem);
			if (isSystemBreak) {
				//increment system index 
				context.incSystemIndex();
			}

			//read system layout, if there
			SystemLayout systemLayout = readSystemLayout(isPageStarted, context.getTenthMm());
			if (systemLayout != null)
				header.setSystemLayout(context.getSystemIndex(), systemLayout);
			
{'fraction_non_alphanumeric': 0.10951349221981485, 'fraction_numerical': 0.0017727004136300964, 'mean_word_length': 4.129292929292929, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '3229910', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1575, 'n_words': 412}