Search in sources :

Example 1 with Color

use of com.xenoage.utils.color.Color in project Zong by Xenoage.

the class BitmapLineTest method test3Pixel.

@Test
public void test3Pixel() {
    // 3px line. Should result in 3 px, opaque.
    for (float scaling = 0.1f; scaling < 5; scaling *= 1.5f) {
        Color color = Color.Companion.getBlack();
        BitmapLine sl = new BitmapLine(Units.pxToMm(3, scaling), color, scaling);
        assertEquals(Units.pxToMm(3, scaling), sl.widthMm, Delta.DELTA_FLOAT);
        assertEquals(255, sl.color.getA());
    }
}
Also used : Color(com.xenoage.utils.color.Color) Test(org.junit.Test)

Example 2 with Color

use of com.xenoage.utils.color.Color in project Zong by Xenoage.

the class FormattedTextReader method readStyle.

@NonNull
public FormattedTextStyle readStyle(MxlFormattedText mxlText) {
    MxlPrintStyle mxlPrintStyle = mxlText.getPrintStyle();
    FontInfo font = new FontInfoReader(mxlPrintStyle.getFont(), FontInfo.defaultValue).read();
    Color color = readColor(mxlPrintStyle);
    return new FormattedTextStyle(font, color, Superscript.Normal);
}
Also used : Color(com.xenoage.utils.color.Color) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) MxlPrintStyle(com.xenoage.zong.musicxml.types.attributes.MxlPrintStyle) FontInfo(com.xenoage.utils.font.FontInfo) NonNull(com.xenoage.utils.annotations.NonNull)

Example 3 with Color

use of com.xenoage.utils.color.Color in project Zong by Xenoage.

the class CreditsReader method createFormattedText.

/**
 * Creates a {@link FormattedText} and returns it.
 */
private static FormattedText createFormattedText(MxlCreditWords mxlCreditWords, FontInfo defaultFont) {
    CList<FormattedTextParagraph> paragraphs = clist();
    CList<FormattedTextElement> elements = clist();
    // iterate through all credit-words elements.
    // currently we are only interested in credit-words elements on page 1.
    Alignment alignment = FormattedTextParagraph.Companion.getDefaultAlignment();
    for (MxlFormattedText mxlFormattedText : mxlCreditWords.getItems()) {
        // read text. if empty, ignore this element
        String textContent = mxlFormattedText.getValue();
        if (textContent.length() == 0)
            continue;
        // apply horizontal alignment, if set, otherwise keep the old value
        Alignment newAlignment = readAlignment(mxlFormattedText.getJustify());
        if (newAlignment != null) {
            alignment = newAlignment;
        }
        // since we use paragraphs but MusicXML doesn't, split
        // the text where there are line breaks.
        String[] textLines = textContent.split("\n");
        boolean endsWithLineBreak = textContent.endsWith("\n");
        // new paragraphs for the following lines
        for (int iLine = 0; iLine < textLines.length; iLine++) {
            if (iLine > 0) {
                // not the first line: we have to create a new paragraph
                paragraphs.add(new FormattedTextParagraph(elements.close(), alignment));
                elements = clist();
            }
            // read line
            String textLine = textLines[iLine];
            if (textLine.length() > 0) {
                // font
                FontInfo fontInfo = new FontInfoReader(mxlFormattedText.getPrintStyle().getFont(), defaultFont).read();
                // color
                Color color = mxlFormattedText.getPrintStyle().getColor().getValue();
                // create text element
                FormattedTextElement textElement = new FormattedTextString(textLine, new FormattedTextStyle(fontInfo, color, null));
                elements.add(textElement);
            }
        }
        if (endsWithLineBreak) {
            // create a new paragraph
            paragraphs.add(new FormattedTextParagraph(elements.close(), alignment));
            elements = clist();
        }
    }
    // add non-empty paragraph at the end
    if (elements.size() > 0) {
        paragraphs.add(new FormattedTextParagraph(elements.close(), alignment));
    }
    return new FormattedText(paragraphs.close());
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) MxlFormattedText(com.xenoage.zong.musicxml.types.MxlFormattedText) Color(com.xenoage.utils.color.Color) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedText(com.xenoage.zong.core.text.FormattedText) MxlFormattedText(com.xenoage.zong.musicxml.types.MxlFormattedText) Alignment(com.xenoage.zong.core.text.Alignment) OtherReader.readAlignment(com.xenoage.zong.io.musicxml.in.readers.OtherReader.readAlignment) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) FontInfoReader(com.xenoage.zong.io.musicxml.in.readers.FontInfoReader) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) FontInfo(com.xenoage.utils.font.FontInfo)

Example 4 with Color

use of com.xenoage.utils.color.Color 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)

Example 5 with Color

use of com.xenoage.utils.color.Color in project Zong by Xenoage.

the class VoltaRenderer method draw.

/**
 * Draws the given {@link VoltaStamping} on the given {@link Canvas},
 * using the given {@link RendererArgs}.
 */
@Override
public void draw(Stamping stamping, Canvas canvas, RendererArgs args) {
    VoltaStamping volta = (VoltaStamping) stamping;
    StaffStamping parentStaff = volta.parentStaff;
    float scaling = args.scaling;
    // horizontal position
    float x1 = volta.leftXMm + parentStaff.positionMm.x;
    float x2 = volta.rightXMm + parentStaff.positionMm.x;
    // compute hooks
    boolean hook = volta.leftHook || volta.rightHook;
    float hookHeight = 0;
    if (hook) {
        // height of hook is 2 interline spaces
        hookHeight = parentStaff.is * 2;
    }
    // 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 y;
    Color paintColor;
    if (canvas.getFormat() == CanvasFormat.Raster) {
        BitmapStaff ss = parentStaff.getBitmapInfo().getBitmapStaff(scaling);
        y = parentStaff.positionMm.y + ss.getYMm(volta.lp);
        BitmapLine screenLine = parentStaff.getBitmapInfo().getBitmapLine(scaling, width, color);
        paintColor = screenLine.color;
        paintWidth = screenLine.widthMm;
    } else {
        y = parentStaff.computeYMm(volta.lp);
        paintColor = color;
        paintWidth = width;
    }
    // draw line and hooks
    canvas.drawLine(new Point2f(x1, y), new Point2f(x2, y), paintColor, paintWidth);
    if (volta.leftHook) {
        canvas.drawLine(new Point2f(x1, y), new Point2f(x1, y + hookHeight), paintColor, paintWidth);
    }
    if (volta.rightHook) {
        canvas.drawLine(new Point2f(x2, y), new Point2f(x2, y + hookHeight), paintColor, paintWidth);
    }
    // draw text
    FormattedText text = volta.text;
    if (text != null && text.getParagraphs().size() > 0) {
        float textAscent = text.getFirstParagraph().getMetrics().getAscent();
        float textX = x1 + parentStaff.is * 1;
        float textY = y + textAscent;
        canvas.drawText(volta.text, null, new Point2f(textX, textY), true, 0);
    }
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) VoltaStamping(com.xenoage.zong.musiclayout.stampings.VoltaStamping) 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)

Aggregations

Color (com.xenoage.utils.color.Color)15 BitmapStaff (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapStaff)8 Point2f (com.xenoage.utils.math.geom.Point2f)7 StaffStamping (com.xenoage.zong.musiclayout.stampings.StaffStamping)6 BitmapLine (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapLine)6 FormattedText (com.xenoage.zong.core.text.FormattedText)3 Test (org.junit.Test)3 FontInfo (com.xenoage.utils.font.FontInfo)2 Rectangle2f (com.xenoage.utils.math.geom.Rectangle2f)2 FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)2 NonNull (com.xenoage.utils.annotations.NonNull)1 Alignment (com.xenoage.zong.core.text.Alignment)1 FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)1 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)1 FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)1 FontInfoReader (com.xenoage.zong.io.musicxml.in.readers.FontInfoReader)1 OtherReader.readAlignment (com.xenoage.zong.io.musicxml.in.readers.OtherReader.readAlignment)1 BeamStamping (com.xenoage.zong.musiclayout.stampings.BeamStamping)1 LegerLineStamping (com.xenoage.zong.musiclayout.stampings.LegerLineStamping)1 SlurStamping (com.xenoage.zong.musiclayout.stampings.SlurStamping)1