Search in sources :

Example 1 with FontInfoReader

use of com.xenoage.zong.io.musicxml.in.readers.FontInfoReader 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)

Aggregations

Color (com.xenoage.utils.color.Color)1 FontInfo (com.xenoage.utils.font.FontInfo)1 Alignment (com.xenoage.zong.core.text.Alignment)1 FormattedText (com.xenoage.zong.core.text.FormattedText)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 FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)1 FontInfoReader (com.xenoage.zong.io.musicxml.in.readers.FontInfoReader)1 OtherReader.readAlignment (com.xenoage.zong.io.musicxml.in.readers.OtherReader.readAlignment)1 MxlFormattedText (com.xenoage.zong.musicxml.types.MxlFormattedText)1