Search in sources :

Example 6 with Page

use of com.xenoage.zong.layout.Page 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 7 with Page

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

the class CreditsReader method addTextFrame.

/**
 * Adds the given credit element as a {@link TextFrame} to the given {@link Layout}.
 */
private static void addTextFrame(MxlCredit credit, Layout layout, ScoreFormat scoreFormat) {
    if (credit.getContent().getCreditContentType() == MxlCreditContentType.CreditWords) {
        MxlCreditWords mxlCreditWords = (MxlCreditWords) credit.getContent();
        // create formatted text
        FormattedText text = createFormattedText(mxlCreditWords, scoreFormat.getLyricFont());
        // compute position (read only the position of the first credit-words element)
        Page firstPage = layout.getPages().get(0);
        float tenths = scoreFormat.getInterlineSpace() / 10;
        MxlFormattedText mxlFirstCreditWords = mxlCreditWords.getItems().get(0);
        MxlPosition mxlPosition = mxlFirstCreditWords.getPrintStyle().getPosition();
        Point2f offsetFromBottomInTenths = mxlPosition.getDefault().or(new Point2f(10f, 10f));
        Point2f position = new Point2f(offsetFromBottomInTenths.x * tenths, firstPage.getFormat().getSize().height - offsetFromBottomInTenths.y * tenths);
        // compute size
        // this is the width of the widest paragraph and the height of the sum of all paragraphs
        // at least 1 mm
        float maxParagraphWidth = 1;
        // at least 1 mm
        float sumParagraphsHeight = 1;
        for (FormattedTextParagraph paragraph : text.getParagraphs()) {
            maxParagraphWidth = Math.max(maxParagraphWidth, paragraph.getMetrics().getWidth());
            sumParagraphsHeight += paragraph.getHeightMm();
        }
        Size2f size = new Size2f(maxParagraphWidth, sumParagraphsHeight);
        // horizontal alignment:
        // try with halign first, and if not set, use justify
        Alignment alignment = readAlignment(mxlFirstCreditWords.getHAlign());
        if (alignment == null) {
            alignment = readAlignment(mxlFirstCreditWords.getJustify());
        }
        if (alignment == null || alignment == Alignment.Left) {
            position = position.add(size.width / 2, 0);
        } else if (alignment == Alignment.Center) {
        // already centered
        } else if (alignment == Alignment.Right) {
            position = position.add(-size.width / 2, 0);
        }
        // vertical alignment
        MxlVAlign mxlVAlign = mxlFirstCreditWords.getVAlign();
        if (mxlVAlign == MxlVAlign.Top || mxlVAlign == MxlVAlign.Unknown) {
            position = position.add(0, size.height / 2);
        } else if (mxlVAlign == MxlVAlign.Middle) {
        // already centered
        } else if (mxlVAlign == MxlVAlign.Bottom || mxlVAlign == MxlVAlign.Baseline) {
            position = position.add(0, -size.height / 2);
        }
        // create and add TextFrame
        TextFrame textFrame = new TextFrame();
        textFrame.setPosition(position);
        textFrame.setSize(size);
        textFrame.setText(text);
        layout.getPages().get(0).addFrame(textFrame);
    }
}
Also used : Alignment(com.xenoage.zong.core.text.Alignment) OtherReader.readAlignment(com.xenoage.zong.io.musicxml.in.readers.OtherReader.readAlignment) Point2f(com.xenoage.utils.math.geom.Point2f) MxlFormattedText(com.xenoage.zong.musicxml.types.MxlFormattedText) Size2f(com.xenoage.utils.math.geom.Size2f) MxlPosition(com.xenoage.zong.musicxml.types.attributes.MxlPosition) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) TextFrame(com.xenoage.zong.layout.frames.TextFrame) MxlCreditWords(com.xenoage.zong.musicxml.types.MxlCreditWords) Page(com.xenoage.zong.layout.Page) MxlVAlign(com.xenoage.zong.musicxml.types.enums.MxlVAlign) FormattedText(com.xenoage.zong.core.text.FormattedText) MxlFormattedText(com.xenoage.zong.musicxml.types.MxlFormattedText)

Example 8 with Page

use of com.xenoage.zong.layout.Page 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 9 with Page

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

the class PageViewManager method computePageRects.

/**
 * Computes the offsets of all pages in mm, relative to the upper left
 * corner of the first page.
 */
private ArrayList<Rectangle2f> computePageRects() {
    // horizontal center of the current page
    float offsetX = 0;
    // vertical center of the current page
    float offsetY = 0;
    List<Page> pages = layout.getPages();
    ArrayList<Rectangle2f> ret = new ArrayList<>(pages.size());
    if (pages.size() > 0) {
        // compute offsets for all pages
        float firstPageOffsetX = -pages.get(0).getFormat().getSize().width / 2;
        float firstPageOffsetY = -pages.get(0).getFormat().getSize().height / 2;
        for (val page : pages) {
            Size2f pageSize = page.getFormat().getSize();
            Point2f offset = null;
            switch(pageDisplayAlignment) {
                case Horizontal:
                    offset = new Point2f(offsetX + firstPageOffsetX, -pageSize.height / 2);
                    offsetX += pageSize.width + pageDisplayDistance;
                    break;
                case Vertical:
                    offset = new Point2f(-pageSize.width / 2, offsetY + firstPageOffsetY);
                    offsetY += pageSize.height + pageDisplayDistance;
                    break;
            }
            ret.add(new Rectangle2f(offset, new Size2f(pageSize)));
        }
    }
    return ret;
}
Also used : lombok.val(lombok.val) ArrayList(java.util.ArrayList) Page(com.xenoage.zong.layout.Page)

Example 10 with Page

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

the class JfxLayoutRenderer method paintToImage.

/**
 * Returns a {@link WritableImage} with the given page of the given {@link Layout}
 * which is rendered at the given zoom level.
 */
public static WritableImage paintToImage(Layout layout, int pageIndex, float zoom) {
    Page page = layout.getPages().get(pageIndex);
    Size2f pageSize = page.getFormat().getSize();
    int width = Units.mmToPxInt(pageSize.width, zoom);
    int height = Units.mmToPxInt(pageSize.height, zoom);
    WritableImage wim = new WritableImage(width, height);
    Canvas jfxCanvas = new Canvas(width, height);
    GraphicsContext context = jfxCanvas.getGraphicsContext2D();
    context.setFill(Color.WHITE);
    context.fillRect(0, 0, width, height);
    LayoutRenderer.paintToCanvas(layout, pageIndex, zoom, origin, new JfxCanvas(context, pageSize, CanvasFormat.Raster, CanvasDecoration.Interactive, CanvasIntegrity.Perfect));
    jfxCanvas.snapshot(null, wim);
    return wim;
}
Also used : WritableImage(javafx.scene.image.WritableImage) GraphicsContext(javafx.scene.canvas.GraphicsContext) Size2f(com.xenoage.utils.math.geom.Size2f) JfxCanvas(com.xenoage.zong.renderer.javafx.canvas.JfxCanvas) Canvas(javafx.scene.canvas.Canvas) Page(com.xenoage.zong.layout.Page) JfxCanvas(com.xenoage.zong.renderer.javafx.canvas.JfxCanvas)

Aggregations

Page (com.xenoage.zong.layout.Page)14 Size2f (com.xenoage.utils.math.geom.Size2f)10 Point2f (com.xenoage.utils.math.geom.Point2f)6 Layout (com.xenoage.zong.layout.Layout)6 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 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 AwtCanvas (com.xenoage.zong.renderer.awt.canvas.AwtCanvas)2 Graphics2D (java.awt.Graphics2D)2 ArrayList (java.util.ArrayList)2 lombok.val (lombok.val)2 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 Paint (android.graphics.Paint)1