Search in sources :

Example 11 with ScoreDoc

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));
    }
}
Also used : MusicXmlScoreDocFileInput(com.xenoage.zong.desktop.io.musicxml.in.MusicXmlScoreDocFileInput) File(java.io.File) ScoreDoc(com.xenoage.zong.documents.ScoreDoc)

Example 12 with ScoreDoc

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);
}
Also used : MusicXmlScoreDocFileInput(com.xenoage.zong.desktop.io.musicxml.in.MusicXmlScoreDocFileInput) MalformedURLException(java.net.MalformedURLException) SQLException(java.sql.SQLException) JseInputStream(com.xenoage.utils.jse.io.JseInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) FileNotFoundException(java.io.FileNotFoundException) PreparedStatement(java.sql.PreparedStatement) Page(com.xenoage.zong.webserver.model.Page) ScaledPage(com.xenoage.zong.webserver.model.ScaledPage) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) ScoreDoc(com.xenoage.zong.documents.ScoreDoc) JseInputStream(com.xenoage.utils.jse.io.JseInputStream) Layout(com.xenoage.zong.layout.Layout) Size2f(com.xenoage.utils.math.geom.Size2f) ResultSet(java.sql.ResultSet) Doc(com.xenoage.zong.webserver.model.Doc) ScoreDoc(com.xenoage.zong.documents.ScoreDoc)

Example 13 with ScoreDoc

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;
}
Also used : ScoreFrame(com.xenoage.zong.layout.frames.ScoreFrame) ScoreLayouter(com.xenoage.zong.musiclayout.layouter.ScoreLayouter) ScoreLayout(com.xenoage.zong.musiclayout.ScoreLayout) LayoutFormat.defaultLayoutFormat(com.xenoage.zong.core.format.LayoutFormat.defaultLayoutFormat) LayoutFormat(com.xenoage.zong.core.format.LayoutFormat) Page(com.xenoage.zong.layout.Page) LayoutDefaults(com.xenoage.zong.layout.LayoutDefaults) ScoreDoc(com.xenoage.zong.documents.ScoreDoc) PageFormat(com.xenoage.zong.core.format.PageFormat) Target(com.xenoage.zong.musiclayout.layouter.Target) Point2f(com.xenoage.utils.math.geom.Point2f) ScoreLayout(com.xenoage.zong.musiclayout.ScoreLayout) Layout(com.xenoage.zong.layout.Layout) ScoreFrameChain(com.xenoage.zong.layout.frames.ScoreFrameChain) Size2f(com.xenoage.utils.math.geom.Size2f) ScoreLayoutArea(com.xenoage.zong.musiclayout.layouter.ScoreLayoutArea)

Aggregations

ScoreDoc (com.xenoage.zong.documents.ScoreDoc)13 MusicXmlScoreDocFileInput (com.xenoage.zong.desktop.io.musicxml.in.MusicXmlScoreDocFileInput)6 File (java.io.File)6 Size2f (com.xenoage.utils.math.geom.Size2f)4 JseInputStream (com.xenoage.utils.jse.io.JseInputStream)3 PdfScoreDocFileOutput (com.xenoage.zong.desktop.io.pdf.out.PdfScoreDocFileOutput)3 MidiScoreDocFileOutput (com.xenoage.zong.desktop.io.midi.out.MidiScoreDocFileOutput)2 PngScoreDocFileOutput (com.xenoage.zong.desktop.io.png.out.PngScoreDocFileOutput)2 Layout (com.xenoage.zong.layout.Layout)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 List (java.util.List)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Canvas (com.google.gwt.canvas.client.Canvas)1 Context2d (com.google.gwt.canvas.dom.client.Context2d)1 EntryPoint (com.google.gwt.core.client.EntryPoint)1 Element (com.google.gwt.dom.client.Element)1 BLOCK (com.google.gwt.dom.client.Style.Display.BLOCK)1 NONE (com.google.gwt.dom.client.Style.Display.NONE)1