Search in sources :

Example 21 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class SystemCursorRenderer method draw.

/**
 * Draws the given {@link SystemCursorStamping} on the given {@link Canvas},
 * using the given {@link RendererArgs}.
 */
@Override
public void draw(Stamping stamping, Canvas canvas, RendererArgs args) {
    SystemCursorStamping cursor = (SystemCursorStamping) stamping;
    float viewScaling = args.targetScaling;
    StaffStamping topStaff = cursor.topStaff;
    StaffStamping bottomStaff = cursor.bottomStaff;
    float x, y1, y2;
    x = topStaff.positionMm.x + cursor.xMm;
    if (canvas.getFormat() == CanvasFormat.Raster) {
        float staffY = topStaff.positionMm.y;
        BitmapStaff ss = topStaff.getBitmapInfo().getBitmapStaff(viewScaling);
        // top staff: top line
        y1 = staffY + ss.getYMm(topStaff.linesCount * 2);
        // bottom staff: bottom line
        staffY = bottomStaff.positionMm.y;
        ss = bottomStaff.getBitmapInfo().getBitmapStaff(viewScaling);
        y2 = staffY + ss.getYMm(-2);
    } else {
        y1 = topStaff.computeYMm(topStaff.linesCount * 2);
        y2 = bottomStaff.computeYMm(-2);
    }
    canvas.drawLine(new Point2f(x, y1), new Point2f(x, y2), Companion.color(50, 50, 230), getCursorWidth(viewScaling));
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) SystemCursorStamping(com.xenoage.zong.musiclayout.stampings.SystemCursorStamping) StaffStamping(com.xenoage.zong.musiclayout.stampings.StaffStamping) BitmapStaff(com.xenoage.zong.musiclayout.stampings.bitmap.BitmapStaff)

Example 22 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class TestRenderer method drawWith.

public static void drawWith(Point2f position, Size2f size, Color color, Canvas canvas, RendererArgs args) {
    Point2f pNW = position;
    Point2f pSE = new Point2f(position.x + size.width, position.y + size.height);
    Point2f pNE = new Point2f(pSE.x, pNW.y);
    Point2f pSW = new Point2f(pNW.x, pSE.y);
    canvas.drawLine(pNW, pNE, color, 1);
    canvas.drawLine(pNE, pSE, color, 1);
    canvas.drawLine(pSE, pSW, color, 1);
    canvas.drawLine(pSW, pNW, color, 1);
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f)

Example 23 with Point2f

use of com.xenoage.utils.math.geom.Point2f 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 24 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class PageViewManagerTest method computeLayoutPosition.

@Test
public void computeLayoutPosition() {
    Size2i viewSize = new Size2i(800, 600);
    float viewScaling = 1;
    Point2f viewScroll = new Point2f(0, 0);
    ViewState viewState = new ViewState(viewSize, viewScaling, viewScroll);
    // default zoom and scroll: point 400/300 must
    // have center position of the page (50/100)
    Point2f pos = view.computeLP(new Point2i(400, 300), viewState).position;
    assertEquals(pos.x, 50, Delta.DELTA_FLOAT);
    assertEquals(pos.y, 100, Delta.DELTA_FLOAT);
    // set zoom to 200%, must still be 50/100
    viewScaling = 2;
    viewState = new ViewState(viewSize, viewScaling, viewScroll);
    pos = view.computeLP(new Point2i(400, 300), viewState).position;
    assertEquals(pos.x, 50, Delta.DELTA_FLOAT);
    assertEquals(pos.y, 100, Delta.DELTA_FLOAT);
    // scroll to -5/10
    // screen point 400/300 must be 45/110
    viewScroll = new Point2f(-5, 10);
    viewState = new ViewState(viewSize, viewScaling, viewScroll);
    pos = view.computeLP(new Point2i(400, 300), viewState).position;
    assertEquals(pos.x, 45, Delta.DELTA_FLOAT);
    assertEquals(pos.y, 110, Delta.DELTA_FLOAT);
    // screen point 0/0 must be
    // 50+(-5-400*pxToMm/zoom);100+(10-300*pxToMm/zoom)
    // compute relative to page 0, since coordinates are outside
    pos = view.computeLP(new Point2i(0, 0), 0, viewState).position;
    assertEquals(pos.x, 50 + -5 - Units.pxToMm(400, viewScaling), Delta.DELTA_FLOAT);
    assertEquals(pos.y, 100 + 10 - Units.pxToMm(300, viewScaling), Delta.DELTA_FLOAT);
    // when not computed relative to a given page, it must return null
    assertNull(view.computeLP(new Point2i(0, 0), viewState));
    // reset zoom/scroll, then screen point 800/300 must be on page 1
    viewScaling = 1;
    viewScroll = new Point2f(0, 0);
    viewState = new ViewState(viewSize, viewScaling, viewScroll);
    int pageIndex = view.computeLP(new Point2i(800, 300), viewState).pageIndex;
    assertEquals(1, pageIndex);
}
Also used : Point2i(com.xenoage.utils.math.geom.Point2i) Point2f(com.xenoage.utils.math.geom.Point2f) Size2i(com.xenoage.utils.math.geom.Size2i) Test(org.junit.Test)

Example 25 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class StaffStampingTest method containsPoint.

@Test
public void containsPoint() {
    StaffStamping staff = new StaffStamping(null, 0, new Point2f(40, 80), 160, 5, 1);
    Shape shape = staff.getBoundingShape();
    // don't hit it
    assertFalse(shape.contains(new Point2f(39, 81)));
    assertFalse(shape.contains(new Point2f(42, 79)));
    assertFalse(shape.contains(new Point2f(201, 81)));
    assertFalse(shape.contains(new Point2f(100, 85)));
    // hit it
    assertTrue(shape.contains(new Point2f(40, 81)));
    assertTrue(shape.contains(new Point2f(41, 80)));
    assertTrue(shape.contains(new Point2f(200, 81)));
    assertTrue(shape.contains(new Point2f(100, 84)));
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) Shape(com.xenoage.utils.math.geom.Shape) Test(org.junit.Test)

Aggregations

Point2f (com.xenoage.utils.math.geom.Point2f)51 BitmapStaff (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapStaff)11 StaffStamping (com.xenoage.zong.musiclayout.stampings.StaffStamping)10 Test (org.junit.Test)10 Color (com.xenoage.utils.color.Color)7 Page (com.xenoage.zong.layout.Page)6 Size2f (com.xenoage.utils.math.geom.Size2f)5 Layout (com.xenoage.zong.layout.Layout)5 ScoreFrame (com.xenoage.zong.layout.frames.ScoreFrame)5 BitmapLine (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapLine)5 Rectangle2f (com.xenoage.utils.math.geom.Rectangle2f)4 FormattedText (com.xenoage.zong.core.text.FormattedText)4 TextMetrics (com.xenoage.utils.font.TextMetrics)3 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)3 ScoreFrameChain (com.xenoage.zong.layout.frames.ScoreFrameChain)3 ScoreLayout (com.xenoage.zong.musiclayout.ScoreLayout)3 Paint (android.graphics.Paint)2 Point2i (com.xenoage.utils.math.geom.Point2i)2 Size2i (com.xenoage.utils.math.geom.Size2i)2 Score (com.xenoage.zong.core.Score)2