use of com.xenoage.zong.core.format.LayoutFormat in project Zong by Xenoage.
the class MusicXmlScoreFileInput method read.
/**
* Builds a {@link Score} entity from a {@link MxlScorePartwise} document.
* @param doc the provided score-partwise document
* @param errorHandling how to deal with errors
*/
public Score read(MxlScorePartwise mxlScore, Level errorHandling) throws InvalidFormatException {
try {
// create new score
Score score = new Score();
// read information about the score
ScoreInfo scoreInfo = new ScoreInfoReader(mxlScore.getScoreHeader()).read();
score.setInfo(scoreInfo);
// read score format
MxlScoreHeader mxlScoreHeader = mxlScore.getScoreHeader();
MxlDefaults mxlDefaults = mxlScoreHeader.getDefaults();
ScoreFormat scoreFormat = new ScoreFormatReader(mxlDefaults).read();
score.setFormat(scoreFormat);
// read layout format
MxlLayout mxlLayout = (mxlDefaults != null ? mxlDefaults.getLayout() : null);
LayoutFormat layoutFormat = new LayoutFormatReader(mxlLayout, scoreFormat.getInterlineSpace() / 10).read();
// TIDY
score.setMetaData("layoutformat", layoutFormat);
// create the list of staves
ErrorHandling mxlErrorHandling = new ErrorHandling(errorHandling);
StavesListReader stavesListReader = new StavesListReader(mxlScore, mxlErrorHandling);
StavesList stavesList = stavesListReader.read();
stavesList.setScore(score);
score.setStavesList(stavesList);
// read the musical contents
new ScoreReader(mxlScore).readToScore(score, mxlErrorHandling);
// remember the XML document for further application-dependend processing
// TIDY
score.setMetaData("mxldoc", mxlScore);
// when errors were collected, log them and save them as metadata
if (mxlErrorHandling.getErrorMessages().size() > 0) {
INSTANCE.log(Companion.warning("The file could be loaded, but the following error(s) were reported: " + mxlErrorHandling.getErrorMessages()));
score.setMetaData("mxlerrors", mxlErrorHandling.getErrorMessages());
}
return score;
} catch (RuntimeException ex) {
// catch runtime exceptions and rethrow them in the expected type
throw new InvalidFormatException(ex);
}
}
use of com.xenoage.zong.core.format.LayoutFormat in project Zong by Xenoage.
the class ScoreDocFactory method read.
/**
* Creates a {@link ScoreDoc} instance from the given score.
* TIDY: move elsewhere, e.g. in a ScoreDocFactory class
*/
public ScoreDoc read(Score score) throws InvalidFormatException, IOException {
// page format
LayoutFormat layoutFormat = Companion.getDefaultLayoutFormat();
Object oLayoutFormat = score.getMetaData().get("layoutformat");
if (oLayoutFormat instanceof LayoutFormat) {
layoutFormat = (LayoutFormat) oLayoutFormat;
}
LayoutDefaults layoutDefaults = new LayoutDefaults(layoutFormat);
// create the document
ScoreDoc ret = new ScoreDoc(score, layoutDefaults);
Layout layout = ret.getLayout();
// layout basics
PageFormat pageFormat = layoutFormat.getPageFormat(0);
Size2f frameSize = new Size2f(pageFormat.getUseableWidth(), pageFormat.getUseableHeight());
Point2f framePos = new Point2f(pageFormat.getMargins().getLeft() + frameSize.width / 2, pageFormat.getMargins().getTop() + frameSize.height / 2);
// layout the score to find out the needed space
Target target = Target.completeLayoutTarget(new ScoreLayoutArea(frameSize));
ScoreLayouter layouter = new ScoreLayouter(ret, target);
ScoreLayout scoreLayout = isErrorLayoutEnabled ? layouter.createScoreLayout() : layouter.createLayoutWithExceptions();
// create and fill at least one page
if (scoreLayout.frames.size() > 0) {
// normal layout: one frame per page
ScoreFrameChain chain = null;
for (int i = 0; i < scoreLayout.frames.size(); i++) {
Page page = new Page(pageFormat);
layout.addPage(page);
ScoreFrame frame = new ScoreFrame();
frame.setPosition(framePos);
frame.setSize(frameSize);
// TEST frame = frame.withHFill(NoHorizontalSystemFillingStrategy.getInstance());
page.addFrame(frame);
if (chain == null) {
chain = new ScoreFrameChain(score);
chain.setScoreLayout(scoreLayout);
}
chain.add(frame);
}
} else {
// no frames: create a single empty page
Page page = new Page(pageFormat);
layout.addPage(page);
}
return ret;
}
use of com.xenoage.zong.core.format.LayoutFormat in project Zong by Xenoage.
the class LayoutFormatReader method readPageLayout.
private void readPageLayout() {
MxlPageLayout mxlPageLayout = mxlLayout.getPageLayout();
if (mxlPageLayout == null)
return;
Size2f size = PageFormat.Companion.getDefaultValue().getSize();
// page-width and page-height
Size2f mxlPageSize = mxlPageLayout.getPageSize();
if (mxlPageSize != null)
size = new Size2f(tenthsMm * mxlPageSize.width, tenthsMm * mxlPageSize.height);
// page-margins
PageMargins pageMarginsLeft = PageMargins.Companion.getDefaultValue();
PageMargins pageMarginsRight = PageMargins.Companion.getDefaultValue();
for (MxlPageMargins mxlMargins : mxlPageLayout.getPageMargins()) {
PageMargins pageMargins = new PageMargins(tenthsMm * mxlMargins.getLeftMargin(), tenthsMm * mxlMargins.getRightMargin(), tenthsMm * mxlMargins.getTopMargin(), tenthsMm * mxlMargins.getBottomMargin());
// left, right page or both? default: both
switch(mxlMargins.getType()) {
case Both:
pageMarginsLeft = pageMargins;
pageMarginsRight = pageMargins;
break;
case Odd:
pageMarginsRight = pageMargins;
break;
case Even:
pageMarginsRight = pageMargins;
break;
}
}
layoutFormat = new LayoutFormat(new PageFormat(size, pageMarginsLeft), new PageFormat(size, pageMarginsRight));
}
Aggregations