use of com.xenoage.zong.documents.ScoreDoc in project Zong by Xenoage.
the class Content method loadScore.
/**
* Loads the MusicXML score from the given file path.
*/
public void loadScore(String filePath) {
try {
ScoreDoc scoreDoc = DocumentIO.read(new File(filePath), new MusicXmlScoreDocFileInput());
loadScore(scoreDoc);
} catch (Exception ex) {
Err.handle(Report.Companion.error(ex));
}
}
use of com.xenoage.zong.documents.ScoreDoc in project Zong by Xenoage.
the class OpenAction method loadDocument.
/**
* Loads the {@link Doc} at the given URL and stores information about
* the score in the database, if it is not already present.
*/
public Tuple2<ScoreDoc, Doc> loadDocument(String url, @MaybeNull UUID publicID) throws SQLException {
Connection db = Webserver.instance.getDBConnection();
ScoreDoc scoreDoc;
// public ID of the document
if (publicID == null)
publicID = UUID.randomUUID();
// may not exist yet
PreparedStatement stmt = stmt(db, "SELECT public_id FROM docs WHERE public_id = ?", publicID);
ResultSet res = stmt.executeQuery();
boolean error = res.next();
stmt.close();
if (error)
throw new SQLException("A document with this public ID already exists");
// load MusicXML document
try {
// open local or remote file
InputStream inputStream;
if (URLUtils.isAbsoluteURL(url)) {
inputStream = new URL(url).openStream();
} else {
inputStream = new FileInputStream(Webserver.webPath + url);
}
MusicXmlScoreDocFileInput in = new MusicXmlScoreDocFileInput();
scoreDoc = in.read(new JseInputStream(inputStream), null);
} catch (FileNotFoundException ex) {
throw new RuntimeException("file not found");
} catch (MalformedURLException ex) {
throw new RuntimeException("invalid URL: " + url);
} catch (IOException ex) {
throw new RuntimeException("can not read from URL: " + url);
}
// register file in database, if not already known
Layout layout = scoreDoc.getLayout();
boolean isDocKnown = Database.exists(db, "docs", "url = ?", "" + url);
if (!isDocKnown) {
Database.insert(db, "docs", "url, public_id, pages, last_access", "" + url, "" + publicID, layout.getPages().size(), unixTime());
}
// read information about the document
Doc doc = Doc.fromDB(db, "" + url);
// for new documents: save information
if (!isDocKnown) {
// page information
for (int iPage : range(layout.getPages())) {
Size2f pageSize = layout.getPages().get(iPage).getFormat().getSize();
new Page(doc.id, iPage, pageSize.width, pageSize.height).insertIntoDB(db);
}
}
return t(scoreDoc, doc);
}
use of com.xenoage.zong.documents.ScoreDoc 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;
}
Aggregations