Search in sources :

Example 1 with TupletStamping

use of com.xenoage.zong.musiclayout.stampings.TupletStamping in project Zong by Xenoage.

the class TupletStamper method createTupletStamping.

/**
 * Computes the {@link TupletStamping} for the given {@link ChordStampings}
 * and returns it.
 */
public TupletStamping createTupletStamping(Tuplet tuplet, OpenTupletsCache cache, SymbolPool symbolPool) {
    StaffStamping ss = cache.getChord(tuplet.getChords().get(0), tuplet).staff;
    // horizontal position of the bracket
    float xDistance = ss.is / 4;
    float x1Mm = cache.getChord(tuplet.getFirstChord(), tuplet).xMm - xDistance;
    ChordStampings cs2 = cache.getChord(tuplet.getLastChord(), tuplet);
    // TODO: notehead width!
    float cs2Width = ss.is * 1.2f;
    float x2Mm = cs2.xMm + cs2Width + xDistance;
    // vertical position of the bracket (above or below) depends on the directions
    // of the majority of the stems
    int stemDir = 0;
    for (Chord chord : tuplet.getChords()) {
        ChordStampings cs = cache.getChord(chord, tuplet);
        if (cs.stem != null) {
            stemDir += cs.stem.direction.getSign();
        }
    }
    // 1: above, -1: below
    int placement = (stemDir < 0 ? 1 : -1);
    // compute position of start and end point
    // by default, the bracket is 1.5 IS away from the end of the stems
    // when there is no stem, the innermost notehead is used
    // TODO: if stems of inner chords are longer, correct!
    float distanceLp = 1.5f * 2;
    float y1Lp = computeBracketLP(cache.getChord(tuplet.getFirstChord(), tuplet), placement, distanceLp);
    float y2Lp = computeBracketLP(cache.getChord(tuplet.getLastChord(), tuplet), placement, distanceLp);
    // at least 2 IS over top barline / under bottom barline
    if (// above staff
    placement == 1) {
        y1Lp = Math.max(y1Lp, (ss.linesCount - 1) * 2 + 4);
        y2Lp = Math.max(y1Lp, (ss.linesCount - 1) * 2 + 4);
    } else // below staff
    {
        y1Lp = Math.min(y1Lp, 0 - 4);
        y2Lp = Math.min(y1Lp, 0 - 4);
    }
    // text
    float fontSize = 10 * ss.is / 1.6f;
    FormattedText text = createText(tuplet.getActualNotes(), fontSize, symbolPool);
    // return result
    return new TupletStamping(sp(x1Mm, y1Lp), sp(x2Mm, y2Lp), true, text, ss);
}
Also used : StaffStamping(com.xenoage.zong.musiclayout.stampings.StaffStamping) FormattedText(com.xenoage.zong.core.text.FormattedText) ChordStampings(com.xenoage.zong.musiclayout.layouter.scoreframelayout.util.ChordStampings) Chord(com.xenoage.zong.core.music.chord.Chord) TupletStamping(com.xenoage.zong.musiclayout.stampings.TupletStamping)

Example 2 with TupletStamping

use of com.xenoage.zong.musiclayout.stampings.TupletStamping in project Zong by Xenoage.

the class TupletRenderer method draw.

/**
 * Draws the given {@link TupletStamping} on the given {@link Canvas},
 * using the given {@link RendererArgs}.
 */
@Override
public void draw(Stamping stamping, Canvas canvas, RendererArgs args) {
    TupletStamping tuplet = (TupletStamping) stamping;
    StaffStamping parentStaff = tuplet.parentStaff;
    float scaling = args.targetScaling;
    // horizontal position
    float x1Mm = tuplet.leftSP.xMm;
    float x2Mm = tuplet.rightSP.xMm;
    // height of hook is 1 IS
    float hookHeightPx = Units.mmToPx(parentStaff.is, scaling);
    // width and color of the line
    Color color = Color.Companion.getBlack();
    // a little bit thicker than staff line
    float width = parentStaff.getLineWidthMm() * 1.5f;
    float paintWidth;
    // compute the horizontal line and color
    float y1Mm, y2Mm;
    Color paintColor;
    if (canvas.getFormat() == CanvasFormat.Raster) {
        BitmapStaff ss = parentStaff.getBitmapInfo().getBitmapStaff(scaling);
        y1Mm = parentStaff.positionMm.y + ss.getYMm(tuplet.leftSP.lp);
        y2Mm = parentStaff.positionMm.y + ss.getYMm(tuplet.rightSP.lp);
        BitmapLine screenLine = parentStaff.getBitmapInfo().getBitmapLine(scaling, width, color);
        paintColor = screenLine.color;
        paintWidth = screenLine.widthMm;
    } else {
        y1Mm = parentStaff.computeYMm(tuplet.leftSP.lp);
        y2Mm = parentStaff.computeYMm(tuplet.rightSP.lp);
        paintColor = color;
        paintWidth = width;
    }
    // compute gap for text
    FormattedText text = tuplet.text;
    float gapMm = 0;
    float textMm = 0;
    if (text != null && text.getParagraphs().size() > 0) {
        textMm = text.getFirstParagraph().getMetrics().getWidth();
        gapMm = textMm * 2;
    }
    // draw line and hooks
    if (gapMm > 0) {
        // two lines, when there is text in between
        float xGapLMm = (x2Mm + x1Mm) / 2 - gapMm / 2;
        float xGapRMm = xGapLMm + gapMm;
        float gapVerticalMm = gapMm / (x2Mm - x1Mm) * (y2Mm - y1Mm);
        float yGapLMm = (y2Mm + y1Mm) / 2 - gapVerticalMm / 2;
        float yGapRMm = yGapLMm + gapVerticalMm;
        canvas.drawLine(new Point2f(x1Mm, y1Mm), new Point2f(xGapLMm, yGapLMm), paintColor, paintWidth);
        canvas.drawLine(new Point2f(xGapRMm, yGapRMm), new Point2f(x2Mm, y2Mm), paintColor, paintWidth);
    } else {
        // no gap
        canvas.drawLine(new Point2f(x1Mm, y1Mm), new Point2f(x2Mm, y2Mm), paintColor, paintWidth);
    }
    // hooks
    canvas.drawLine(new Point2f(x1Mm, y1Mm), new Point2f(x1Mm, y1Mm + hookHeightPx * (tuplet.leftSP.lp < 0 ? -1 : 1)), paintColor, paintWidth);
    canvas.drawLine(new Point2f(x2Mm, y2Mm), new Point2f(x2Mm, y2Mm + hookHeightPx * (tuplet.rightSP.lp < 0 ? -1 : 1)), paintColor, paintWidth);
    // draw text
    if (text != null && text.getParagraphs().size() > 0) {
        float textAscent = text.getFirstParagraph().getMetrics().getAscent();
        float textX = (x1Mm + x2Mm) / 2 - textMm / 2;
        float textY = (y1Mm + y2Mm) / 2 + textAscent / 2;
        canvas.drawText(tuplet.text, null, new Point2f(textX, textY), true, 0);
    }
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) StaffStamping(com.xenoage.zong.musiclayout.stampings.StaffStamping) Color(com.xenoage.utils.color.Color) BitmapLine(com.xenoage.zong.musiclayout.stampings.bitmap.BitmapLine) BitmapStaff(com.xenoage.zong.musiclayout.stampings.bitmap.BitmapStaff) FormattedText(com.xenoage.zong.core.text.FormattedText) TupletStamping(com.xenoage.zong.musiclayout.stampings.TupletStamping)

Aggregations

FormattedText (com.xenoage.zong.core.text.FormattedText)2 StaffStamping (com.xenoage.zong.musiclayout.stampings.StaffStamping)2 TupletStamping (com.xenoage.zong.musiclayout.stampings.TupletStamping)2 Color (com.xenoage.utils.color.Color)1 Point2f (com.xenoage.utils.math.geom.Point2f)1 Chord (com.xenoage.zong.core.music.chord.Chord)1 ChordStampings (com.xenoage.zong.musiclayout.layouter.scoreframelayout.util.ChordStampings)1 BitmapLine (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapLine)1 BitmapStaff (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapStaff)1