Search in sources :

Example 1 with Layout

use of com.xenoage.zong.layout.Layout in project Zong by Xenoage.

the class ScoreDocScoreView method updateScreen.

@Override
public void updateScreen(Size2i screenSizePx, float zoom) {
    this.screenSizePx = screenSizePx;
    this.zoom = zoom;
    // recompute the layout, so that each page fits into the available screen space
    Size2f pageSizeMm = pxToMm(screenSizePx, zoom);
    float marginMm = pxToMm(marginPx, zoom);
    PageFormat pageFormat = new PageFormat(pageSizeMm, new PageMargins(marginMm, marginMm, marginMm, marginMm));
    Size2f frameSizeMm = new Size2f(pageFormat.getUseableWidth(), pageFormat.getUseableHeight());
    // first page needs space for title text
    titleTextHeightPx = screenSizePx.height / 20f;
    float firstFrameOffsetY = pxToMm(titleTextHeightPx, zoom);
    Size2f firstFrameSizeMm = new Size2f(frameSizeMm.width, frameSizeMm.height - firstFrameOffsetY);
    // delete unnecessary layout information, like system distances or system breaks
    Score score = doc.getScore();
    score.setFormat(new ScoreFormat());
    ScoreHeader header = score.getHeader();
    for (int i : range(header.getSystemLayouts())) {
        SystemLayout sl = header.getSystemLayout(i);
        if (sl != null)
            sl.setDistance(SystemLayout.defaultDistance);
    }
    for (int i : range(header.getColumnHeaders())) {
        ColumnHeader ch = header.getColumnHeader(i);
        if (ch.getMeasureBreak() != null)
            ch.setBreak(null);
    }
    // layout the score to find out the needed space
    Context context = new Context(score, App.getSymbolPool(), doc.getLayout().getDefaults().getLayoutSettings());
    Target target = new Target(alist(new ScoreLayoutArea(firstFrameSizeMm)), new ScoreLayoutArea(frameSizeMm), true);
    ScoreLayouter layouter = new ScoreLayouter(context, target);
    ScoreLayout scoreLayout = layouter.createScoreLayout();
    // create and fill at least one page
    Layout layout = new Layout(doc.getLayout().getDefaults());
    ScoreFrameChain chain = null;
    for (int i = 0; i < scoreLayout.frames.size(); i++) {
        Page page = new Page(pageFormat);
        Point2f position;
        Size2f size;
        if (i == 0) {
            // first page
            position = new Point2f(pageSizeMm.width / 2, pageSizeMm.height / 2 + firstFrameOffsetY);
            size = firstFrameSizeMm;
        } else {
            // other pages
            position = new Point2f(pageSizeMm.width / 2, pageSizeMm.height / 2);
            size = frameSizeMm;
        }
        ScoreFrame frame = new ScoreFrame();
        frame.setPosition(position);
        frame.setSize(size);
        page.addFrame(frame);
        layout.addPage(page);
        if (chain == null) {
            chain = new ScoreFrameChain(score);
            chain.setScoreLayout(scoreLayout);
        }
        chain.add(frame);
    }
    this.layout = layout;
}
Also used : Context(com.xenoage.zong.musiclayout.layouter.Context) ScoreFrame(com.xenoage.zong.layout.frames.ScoreFrame) ScoreLayouter(com.xenoage.zong.musiclayout.layouter.ScoreLayouter) ScoreLayout(com.xenoage.zong.musiclayout.ScoreLayout) Page(com.xenoage.zong.layout.Page) Paint(android.graphics.Paint) ScoreFormat(com.xenoage.zong.core.format.ScoreFormat) PageMargins(com.xenoage.zong.core.format.PageMargins) PageFormat(com.xenoage.zong.core.format.PageFormat) Score(com.xenoage.zong.core.Score) ScoreHeader(com.xenoage.zong.core.header.ScoreHeader) ColumnHeader(com.xenoage.zong.core.header.ColumnHeader) Target(com.xenoage.zong.musiclayout.layouter.Target) Point2f(com.xenoage.utils.math.geom.Point2f) ScoreLayout(com.xenoage.zong.musiclayout.ScoreLayout) SystemLayout(com.xenoage.zong.core.format.SystemLayout) Layout(com.xenoage.zong.layout.Layout) SystemLayout(com.xenoage.zong.core.format.SystemLayout) ScoreFrameChain(com.xenoage.zong.layout.frames.ScoreFrameChain) Size2f(com.xenoage.utils.math.geom.Size2f) ScoreLayoutArea(com.xenoage.zong.musiclayout.layouter.ScoreLayoutArea)

Example 2 with Layout

use of com.xenoage.zong.layout.Layout in project Zong by Xenoage.

the class Frame method getCenterLP.

/**
 * Gets the {@link LayoutPos} of the center of the frame.
 * If this frame is not part of a layout, the page index -1 is returned.
 */
public final LayoutPos getCenterLP() {
    Point2f pos = getAbsolutePosition();
    Layout layout = getParentLayout();
    Page page = getParentPage();
    int pageIndex = (page != null ? page.getIndex() : -1);
    return layoutPos(layout, pageIndex, pos);
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) Layout(com.xenoage.zong.layout.Layout) Page(com.xenoage.zong.layout.Page)

Example 3 with Layout

use of com.xenoage.zong.layout.Layout in project Zong by Xenoage.

the class FrameTest method getAbsolutePosition.

/**
 * Tests the computation of the absolute position.
 */
@Test
public void getAbsolutePosition() {
    double u = Math.PI / 180d;
    Layout layout = new Layout(null);
    Page page = new Page(PageFormat.Companion.getDefaultValue());
    layout.addPage(page);
    GroupFrame frame = createFrameWithChildren();
    page.addFrame(frame);
    // frame
    Point2f p = frame.getAbsolutePosition();
    assertEquals(50, p.x, Delta.DELTA_FLOAT);
    assertEquals(75, p.y, Delta.DELTA_FLOAT);
    // child frame
    GroupFrame child = (GroupFrame) frame.children.get(0);
    p = child.getAbsolutePosition();
    float r = 30;
    double childX = 50 + 20 * Math.cos(r * u) + 10 * Math.sin(r * u);
    double childY = 75 - 20 * Math.sin(r * u) + 10 * Math.cos(r * u);
    assertEquals(childX, p.x, Delta.DELTA_FLOAT_ROUGH);
    assertEquals(childY, p.y, Delta.DELTA_FLOAT_ROUGH);
    // child frame of child frame
    Frame child2 = child.children.get(0);
    p = child2.getAbsolutePosition();
    r += 30;
    assertEquals(childX + -15 * Math.cos(r * u) + -10 * Math.sin(r * u), p.x, Delta.DELTA_FLOAT_ROUGH);
    assertEquals(childY - -15 * Math.sin(r * u) + -10 * Math.cos(r * u), p.y, Delta.DELTA_FLOAT_ROUGH);
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) Layout(com.xenoage.zong.layout.Layout) Page(com.xenoage.zong.layout.Page) Test(org.junit.Test)

Example 4 with Layout

use of com.xenoage.zong.layout.Layout in project Zong by Xenoage.

the class PageViewManagerTest method setUp.

@Before
public void setUp() {
    Layout layout = new Layout(null);
    // 3 pages:
    // ----          --------
    // |1 | -2------ |3     |
    // |  | -------- |      |
    // ----          --------
    layout.addPage(new Page(new PageFormat(new Size2f(100, 200), new PageMargins(0, 0, 0, 0))));
    layout.addPage(new Page(new PageFormat(new Size2f(200, 100), new PageMargins(0, 0, 0, 0))));
    layout.addPage(new Page(new PageFormat(new Size2f(200, 200), new PageMargins(0, 0, 0, 0))));
    // create view
    view = new PageViewManager(layout);
    view.setPageDisplayAlignment(PageDisplayAlignment.Horizontal);
}
Also used : PageMargins(com.xenoage.zong.core.format.PageMargins) PageFormat(com.xenoage.zong.core.format.PageFormat) Layout(com.xenoage.zong.layout.Layout) Size2f(com.xenoage.utils.math.geom.Size2f) Page(com.xenoage.zong.layout.Page) Before(org.junit.Before)

Example 5 with Layout

use of com.xenoage.zong.layout.Layout 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)

Aggregations

Layout (com.xenoage.zong.layout.Layout)8 Page (com.xenoage.zong.layout.Page)6 Point2f (com.xenoage.utils.math.geom.Point2f)5 Size2f (com.xenoage.utils.math.geom.Size2f)5 PageFormat (com.xenoage.zong.core.format.PageFormat)3 ScoreFrame (com.xenoage.zong.layout.frames.ScoreFrame)3 Score (com.xenoage.zong.core.Score)2 PageMargins (com.xenoage.zong.core.format.PageMargins)2 ScoreDoc (com.xenoage.zong.documents.ScoreDoc)2 ScoreFrameChain (com.xenoage.zong.layout.frames.ScoreFrameChain)2 ScoreLayout (com.xenoage.zong.musiclayout.ScoreLayout)2 ScoreLayoutArea (com.xenoage.zong.musiclayout.layouter.ScoreLayoutArea)2 ScoreLayouter (com.xenoage.zong.musiclayout.layouter.ScoreLayouter)2 Target (com.xenoage.zong.musiclayout.layouter.Target)2 Paint (android.graphics.Paint)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JseInputStream (com.xenoage.utils.jse.io.JseInputStream)1 JseOutputStream (com.xenoage.utils.jse.io.JseOutputStream)1 Fraction (com.xenoage.utils.math.Fraction)1