Search in sources :

Example 46 with Point2f

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

the class Layout method computeLP.

/**
 * Returns the {@link LayoutPos} of the given {@link MP} within the given {@link Score}
 * at the given line position, or null if unknown.
 */
public LayoutPos computeLP(Score score, MP mp, float lp) {
    ScoreFrameChain chain = getScoreFrameChain(score);
    if (chain != null) {
        ScoreLayout sl = chain.getScoreLayout();
        ScoreLayoutPos slp = sl.getScoreLP(mp, lp);
        if (slp != null) {
            ScoreFrame frame = chain.getFrames().get(slp.frameIndex);
            Page page = frame.getParentPage();
            if (page != null) {
                Point2f frameP = slp.pMm.sub(frame.getSize().width / 2, frame.getSize().height / 2);
                int pageIndex = pages.indexOf(page);
                Point2f pMm = frame.getPagePosition(frameP);
                return layoutPos(this, pageIndex, pMm);
            }
        }
    }
    return null;
}
Also used : ScoreFrame(com.xenoage.zong.layout.frames.ScoreFrame) Point2f(com.xenoage.utils.math.geom.Point2f) ScoreFrameChain(com.xenoage.zong.layout.frames.ScoreFrameChain) ScoreLayout(com.xenoage.zong.musiclayout.ScoreLayout) ScoreLayoutPos(com.xenoage.zong.musiclayout.ScoreLayoutPos)

Example 47 with Point2f

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

the class ScoreLayout method getScoreLP.

/**
 * Computes the {@link ScoreLayoutPos} of the given {@link MP} at the given line position.
 * If not found, null is returned.
 */
public ScoreLayoutPos getScoreLP(MP mp, float lp) {
    int iFrame = getFrameIndexOf(mp.getMeasure());
    if (iFrame > -1) {
        ScoreFrameLayout sfl = frames.get(iFrame);
        StaffStamping ss;
        if (mp.getStaff() != MP.Companion.getUnknown())
            ss = sfl.getStaffStamping(mp.getStaff(), mp.getMeasure());
        else
            ss = sfl.getStaffStamping(0, mp.getMeasure());
        if (ss != null) {
            float x = ss.positionMm.x + ss.system.getXMmAt(mp.getTime());
            float y = ss.computeYMm(lp);
            return new ScoreLayoutPos(iFrame, new Point2f(x, y));
        }
    }
    return null;
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) StaffStamping(com.xenoage.zong.musiclayout.stampings.StaffStamping)

Example 48 with Point2f

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

the class ScoreFrame method getScoreLayoutPosition.

/**
 * Converts the given position from frame space into score layout space.
 * Both spaces use mm units, the difference is the origin:
 * While frames have their origin in the center, the
 * origin of a score layout is in the upper left corner.
 */
public Point2f getScoreLayoutPosition(Point2f framePosition) {
    Point2f ret = framePosition;
    ret = ret.add(size.width / 2, size.height / 2);
    return ret;
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f)

Example 49 with Point2f

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

the class AndroidCanvas method drawText.

/**
 * {@inheritDoc}
 * The text selection is ignored.
 */
@Override
public void drawText(FormattedText text, TextSelection selection, Point2f position, boolean yIsBaseline, float frameWidth) {
    int oldTransform = canvas.save();
    canvas.translate(position.x, position.y);
    // print the text frame paragraph for paragraph
    float offsetX = 0;
    float offsetY = 0;
    for (FormattedTextParagraph p : text.getParagraphs()) {
        TextMetrics pMetrics = p.getMetrics();
        if (!yIsBaseline)
            offsetY += pMetrics.getAscent();
        // adjustment
        if (p.getAlignment() == Alignment.Center)
            offsetX = (frameWidth - pMetrics.getWidth()) / 2;
        else if (p.getAlignment() == Alignment.Right)
            offsetX = frameWidth - pMetrics.getWidth();
        else
            offsetX = 0;
        // draw elements
        for (FormattedTextElement e : p.getElements()) {
            if (e instanceof FormattedTextString) {
                // TODO - formatting
                FormattedTextString t = (FormattedTextString) e;
                Paint paint = new Paint(AndroidColorUtils.black);
                paint.setTypeface(Typeface.SERIF);
                paint.setTextSize(Units.pxToMm(t.getStyle().getFont().getSize(), 1));
                // nice, smooth drawing
                paint.setFlags(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                canvas.drawText(t.getText(), offsetX, offsetY, paint);
            } else {
                // symbol
                FormattedTextSymbol fts = (FormattedTextSymbol) e;
                float scaling = fts.getScaling();
                androidSymbolsRenderer.draw((PathSymbol) fts.getSymbol(), canvas, Color.black, new Point2f(offsetX + fts.getOffsetX(), offsetY + fts.getSymbol().baselineOffset * scaling), new Point2f(scaling, scaling));
            }
            offsetX += e.getMetrics().getWidth();
        }
        // next line
        offsetY += p.getMetrics().getAscent() + p.getMetrics().getDescent() + p.getMetrics().getLeading();
    }
    canvas.restoreToCount(oldTransform);
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) Point2f(com.xenoage.utils.math.geom.Point2f) TextMetrics(com.xenoage.utils.font.TextMetrics) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) Paint(android.graphics.Paint) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) FormattedTextSymbol(com.xenoage.zong.core.text.FormattedTextSymbol) Paint(android.graphics.Paint)

Example 50 with Point2f

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

the class MathUtils method computeRectangleFromTo.

/**
 * Computes and returns a rotated rectangle, that encloses the given two
 * points with the given width.
 * This is shown here:
 *
 * <pre>
 *    [0]---___
 *    /        ---___
 *   p1              ---[1]   _ _
 *  /                   /     /
 * [3]---___           p2    / width
 *          ---___    /     /
 *                ---[2]  _/_
 * </pre>
 *
 * The result is returned as four Point2f values as shown on the above
 * sketch.
 */
public static Point2f[] computeRectangleFromTo(Point2f p1, Point2f p2, float width) {
    // compute the line from p1 to p2
    Point2f p1Top2 = p2.sub(p1);
    // compute the line from p1 to [0]
    Point2f p1To0 = new Point2f(p1Top2.y, -p1Top2.x).normalize().scale(width / 2);
    // compute return values
    Point2f[] ret = new Point2f[] { p1.add(p1To0), p2.add(p1To0), p2.sub(p1To0), p1.sub(p1To0) };
    return ret;
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f)

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