Search in sources :

Example 11 with FormattedText

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

the class Test32b method getFontSize.

private float getFontSize(Score score, int wordsIndex) {
    MP mp = expectedTexts.get(wordsIndex).get1();
    FormattedText text = getTextAt(score, mp);
    return getFontSize(text);
}
Also used : MP(com.xenoage.zong.core.position.MP) FormattedText(com.xenoage.zong.core.text.FormattedText)

Example 12 with FormattedText

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

the class LyricStamper method createHyphenStamping.

/**
 * Creates and returns the {@link StaffTextStamping} containing
 * a hypen ("-") between the two given lyric stampings.
 */
public StaffTextStamping createHyphenStamping(StaffTextStamping syllableLeft, StaffTextStamping syllableRight, FormattedTextStyle style) {
    FormattedText text = Companion.fText("-", style, Alignment.Center);
    float positionX;
    float widthLeft = syllableLeft.getText().getFirstParagraph().getMetrics().getWidth();
    if (syllableLeft.parentStaff == syllableRight.parentStaff) {
        // syllables are in same staff
        // the horizontal position of the hyphen is in the middle of the empty
        // space between the left and right syllable
        float widthRight = syllableRight.getText().getFirstParagraph().getMetrics().getWidth();
        positionX = ((syllableLeft.position.xMm + widthLeft / 2) + (syllableRight.position.xMm - widthRight / 2)) / 2;
    } else {
        // right syllable is in a new staff
        // place the hypen to the right side of the first syllable
        positionX = (syllableLeft.position.xMm + widthLeft / 2) + 2 * text.getFirstParagraph().getMetrics().getWidth();
    }
    return new StaffTextStamping(text, sp(positionX, syllableLeft.position.lp), syllableLeft.parentStaff, // hyphen belongs to the left syllable
    syllableLeft.getElement());
}
Also used : StaffTextStamping(com.xenoage.zong.musiclayout.stampings.StaffTextStamping) FormattedText(com.xenoage.zong.core.text.FormattedText)

Example 13 with FormattedText

use of com.xenoage.zong.core.text.FormattedText 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 14 with FormattedText

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

the class FormattedTextUtilsTest method splitTestExceptions.

@Test
public void splitTestExceptions() {
    // test case: split at all possible positions
    // (problems are only discovered by exceptions)
    FormattedText text1 = createText1Para();
    for (int i : range(text1.toString().length() + 1)) split(text1, i);
    FormattedText text2 = createText3Paras();
    for (int i : range(text2.toString().length() + 1)) split(text2, i);
}
Also used : FormattedText(com.xenoage.zong.core.text.FormattedText) Test(org.junit.Test)

Example 15 with FormattedText

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

the class FormattedTextUtilsTest method insertElementTest2.

@Test
public void insertElementTest2() {
    // test case: insert element with different style at the end of an element
    FormattedText text = createText1Para();
    FormattedTextString insertText = new FormattedTextString("di und An", // different style
    text.getParagraphs().getFirst().getElements().get(0).getStyle());
    // insert at index 8: "Hallo An{di und An}drea"
    FormattedText textInsert = insert(text, 8, insertText);
    assertEquals(1, textInsert.getParagraphs().size());
    assertEquals(4, textInsert.getParagraphs().getFirst().getElements().size());
    assertEquals(new FormattedTextString("Hallo ", text.getParagraphs().getFirst().getElements().get(0).getStyle()), textInsert.getParagraphs().getFirst().getElements().get(0));
    assertEquals(new FormattedTextString("An", text.getParagraphs().getFirst().getElements().get(1).getStyle()), textInsert.getParagraphs().getFirst().getElements().get(1));
    assertEquals(new FormattedTextString("di und An", text.getParagraphs().getFirst().getElements().get(0).getStyle()), textInsert.getParagraphs().getFirst().getElements().get(2));
    assertEquals(new FormattedTextString("drea", text.getParagraphs().getFirst().getElements().get(1).getStyle()), textInsert.getParagraphs().getFirst().getElements().get(3));
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedText(com.xenoage.zong.core.text.FormattedText) Test(org.junit.Test)

Aggregations

FormattedText (com.xenoage.zong.core.text.FormattedText)22 Test (org.junit.Test)8 FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)5 StaffStamping (com.xenoage.zong.musiclayout.stampings.StaffStamping)5 Point2f (com.xenoage.utils.math.geom.Point2f)4 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)4 FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)4 Color (com.xenoage.utils.color.Color)3 Alignment (com.xenoage.zong.core.text.Alignment)3 FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)3 StaffTextStamping (com.xenoage.zong.musiclayout.stampings.StaffTextStamping)3 FontInfo (com.xenoage.utils.font.FontInfo)2 Chord (com.xenoage.zong.core.music.chord.Chord)2 OtherReader.readAlignment (com.xenoage.zong.io.musicxml.in.readers.OtherReader.readAlignment)2 TupletStamping (com.xenoage.zong.musiclayout.stampings.TupletStamping)2 VoltaStamping (com.xenoage.zong.musiclayout.stampings.VoltaStamping)2 BitmapLine (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapLine)2 BitmapStaff (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapStaff)2 MxlFormattedText (com.xenoage.zong.musicxml.types.MxlFormattedText)2 MaybeNull (com.xenoage.utils.annotations.MaybeNull)1