Search in sources :

Example 6 with FormattedTextElement

use of com.xenoage.zong.core.text.FormattedTextElement in project Zong by Xenoage.

the class Test32b method getFormattedTextElement.

private FormattedTextElement getFormattedTextElement(Words words, MP mp) {
    assertTrue("" + mp, words.getText() instanceof FormattedText);
    FormattedText wordsText = (FormattedText) words.getText();
    assertEquals("" + mp, 1, wordsText.getParagraphs().size());
    FormattedTextParagraph wordsPara = wordsText.getFirstParagraph();
    assertEquals("" + mp, 1, wordsPara.getElements().size());
    FormattedTextElement wordsElem = wordsPara.getElements().getFirst();
    return wordsElem;
}
Also used : FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) FormattedText(com.xenoage.zong.core.text.FormattedText) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement)

Example 7 with FormattedTextElement

use of com.xenoage.zong.core.text.FormattedTextElement in project Zong by Xenoage.

the class FormattedTextStringTest method testAscentAndDescent.

@Test
public void testAscentAndDescent() {
    FormattedTextStyle style = new FormattedTextStyle(new FontInfo("Arial", 72f, null));
    float ascent = Units.pxToMm(66, 1);
    float descent = Units.pxToMm(16, 1);
    // TODO: why so rough?
    float delta = 1f;
    // any letters
    FormattedTextElement text = new FormattedTextString("AbcdefÄß*^^°}yqg", style);
    assertEquals(ascent, text.getMetrics().getAscent(), delta);
    assertEquals(descent, text.getMetrics().getDescent(), delta);
    // only "small" letters - must have same result
    text = new FormattedTextString("acemnorsuvwxz", style);
    assertEquals(ascent, text.getMetrics().getAscent(), delta);
    assertEquals(descent, text.getMetrics().getDescent(), delta);
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FontInfo(com.xenoage.utils.font.FontInfo) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) Test(org.junit.Test)

Example 8 with FormattedTextElement

use of com.xenoage.zong.core.text.FormattedTextElement in project Zong by Xenoage.

the class FormattedTextUtilsTest method createText1Para.

private FormattedText createText1Para() {
    // create a formatted text with one paragraph and three adjacent strings
    // with the same style: "Hallo "{style1}, "An"{style2}, "dr"{style2}"ea"{style2}
    FormattedTextStyle style1 = new FormattedTextStyle(12);
    FormattedTextStyle style2 = new FormattedTextStyle(14, Color.Companion.getBlue());
    return fText(fPara(CList.<FormattedTextElement>ilist(new FormattedTextString("Hallo ", style1), new FormattedTextString("An", style2), new FormattedTextString("dr", style2), new FormattedTextString("ea", style2)), Alignment.Left));
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement)

Example 9 with FormattedTextElement

use of com.xenoage.zong.core.text.FormattedTextElement in project Zong by Xenoage.

the class DefaultTexts method getTempoTextNotNull.

public static FormattedText getTempoTextNotNull(Tempo tempo, SymbolPool symbolPool) {
    FormattedTextStyle style = FormattedTextStyle.Companion.getDefaultStyle();
    if (tempo.getText() != null) {
        // use custom text
        return styleText(tempo.getText(), style);
    } else {
        // show meaning, e.g. "♩ = 120"
        CList<FormattedTextElement> elements = clist();
        Fraction beat = tempo.getBaseBeat();
        if (beat.equals(Companion.fr(1, 4))) {
            elements.add(new FormattedTextSymbol(symbolPool.getSymbol(CommonSymbol.TextNoteQuarter), /* TODO staffStamping.is * FONT_SIZE_IN_IS */
            12, FormattedTextStyle.Companion.getDefaultColor()));
        } else if (beat.equals(Companion.fr(1, 2))) {
            elements.add(new FormattedTextSymbol(symbolPool.getSymbol(CommonSymbol.TextNoteHalf), /* staffStamping.is * FONT_SIZE_IN_IS */
            12, FormattedTextStyle.Companion.getDefaultColor()));
        } else {
            elements.add(new FormattedTextString(beat.toString(), style));
        }
        elements.add(new FormattedTextString(" = " + tempo.getBeatsPerMinute(), style));
        FormattedTextParagraph paragraph = new FormattedTextParagraph(elements, Alignment.Left);
        return Companion.fText(paragraph);
    }
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) Fraction(com.xenoage.utils.math.Fraction) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) FormattedTextSymbol(com.xenoage.zong.core.text.FormattedTextSymbol)

Example 10 with FormattedTextElement

use of com.xenoage.zong.core.text.FormattedTextElement in project Zong by Xenoage.

the class JfxCanvas method drawText.

@Override
public void drawText(FormattedText text, TextSelection selection, Point2f position, boolean yIsBaseline, float frameWidth) {
    context.save();
    context.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;
                context.setFill(toJavaFXColor(t.getStyle().getColor()));
                Font font = toJavaFXFont(t.getStyle().getFont());
                context.setFont(font);
                context.save();
                context.scale(Units.pxToMm_1_1, Units.pxToMm_1_1);
                context.fillText(t.getText(), offsetX / Units.pxToMm_1_1, offsetY / Units.pxToMm_1_1);
                context.restore();
            } else {
                // symbol
                FormattedTextSymbol fts = (FormattedTextSymbol) e;
                float scaling = fts.getScaling();
                SymbolsRenderer.draw(fts.getSymbol(), this, Color.Companion.getBlack(), new Point2f(offsetX + fts.getOffsetX(), offsetY + fts.getSymbol().getBaselineOffset() * scaling), new Point2f(scaling, scaling));
            }
            offsetX += e.getMetrics().getWidth();
        }
        // next line
        offsetY += p.getMetrics().getAscent() + p.getMetrics().getDescent() + p.getMetrics().getLeading();
    }
    context.restore();
}
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) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) FormattedTextSymbol(com.xenoage.zong.core.text.FormattedTextSymbol) Font(javafx.scene.text.Font) JfxFontUtils.toJavaFXFont(com.xenoage.utils.jse.javafx.font.JfxFontUtils.toJavaFXFont)

Aggregations

FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)13 FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)11 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)9 FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)7 FontInfo (com.xenoage.utils.font.FontInfo)3 FormattedText (com.xenoage.zong.core.text.FormattedText)3 FormattedTextSymbol (com.xenoage.zong.core.text.FormattedTextSymbol)3 Test (org.junit.Test)3 TextMetrics (com.xenoage.utils.font.TextMetrics)2 Point2f (com.xenoage.utils.math.geom.Point2f)2 Alignment (com.xenoage.zong.core.text.Alignment)2 BadLocationException (javax.swing.text.BadLocationException)2 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)2 Paint (android.graphics.Paint)1 Color (com.xenoage.utils.color.Color)1 JfxFontUtils.toJavaFXFont (com.xenoage.utils.jse.javafx.font.JfxFontUtils.toJavaFXFont)1 Fraction (com.xenoage.utils.math.Fraction)1 Score (com.xenoage.zong.core.Score)1 ScoreHeader (com.xenoage.zong.core.header.ScoreHeader)1 Part (com.xenoage.zong.core.music.Part)1