Search in sources :

Example 1 with Alignment

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

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

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

the class FormattedTextConverter method fromStyledDocument.

/**
 * Creates a {@link FormattedText} from the given {@link StyledDocument}.
 */
public static FormattedText fromStyledDocument(StyledDocument styledDoc) {
    CList<FormattedTextParagraph> paragraphs = clist();
    int endPos = styledDoc.getLength();
    AttributeSet attribute = null;
    String textelement = "";
    String letter = "";
    CList<FormattedTextElement> elements = clist();
    Alignment alignment = null;
    // paragraph, when '\n' is found
    for (int i = 0; i < endPos; i++) {
        try {
            letter = styledDoc.getText(i, 1);
        } catch (BadLocationException e) {
        }
        // line break: create new paragraph
        if (letter.equals("\n")) {
            if (!textelement.equals("")) {
                elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
                textelement = "";
            }
            paragraphs.add(new FormattedTextParagraph(elements, alignment));
            elements = clist();
            alignment = null;
        } else if (attribute != styledDoc.getCharacterElement(i).getAttributes()) {
            // set alignment, if not already done
            if (alignment == null) {
                alignment = fromAttributeSet(styledDoc.getParagraphElement(i).getAttributes());
            }
            // style has changed, so save old element and create a new one
            if (!textelement.equals("")) {
                elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
            }
            attribute = styledDoc.getCharacterElement(i).getAttributes();
            textelement = letter;
        } else {
            // style stayed the same, so just append
            textelement += letter;
        }
    }
    if (!textelement.equals("")) {
        // save the last string
        elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
    }
    if (elements.size() > 0) {
        // add (non-empty) paragraph
        paragraphs.add(new FormattedTextParagraph(elements, alignment));
    }
    return new FormattedText(paragraphs);
}
Also used : Alignment(com.xenoage.zong.core.text.Alignment) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) AlignmentUtils.applyAlignmentToAttributeSet(com.xenoage.zong.desktop.utils.text.AlignmentUtils.applyAlignmentToAttributeSet) AlignmentUtils.fromAttributeSet(com.xenoage.zong.desktop.utils.text.AlignmentUtils.fromAttributeSet) AttributeSet(javax.swing.text.AttributeSet) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedText(com.xenoage.zong.core.text.FormattedText) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) BadLocationException(javax.swing.text.BadLocationException)

Example 4 with Alignment

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

the class FormattedTextReader method readText.

@Override
public FormattedText readText(MxlFormattedText mxlText) {
    String text = mxlText.getValue().trim();
    FormattedTextStyle style = readStyle(mxlText);
    Alignment alignment = readAlignment(mxlText);
    return FormattedText.Companion.fText(text, style, alignment);
}
Also used : Alignment(com.xenoage.zong.core.text.Alignment) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle)

Aggregations

Alignment (com.xenoage.zong.core.text.Alignment)4 FormattedText (com.xenoage.zong.core.text.FormattedText)3 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)3 FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)2 FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)2 FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)2 OtherReader.readAlignment (com.xenoage.zong.io.musicxml.in.readers.OtherReader.readAlignment)2 MxlFormattedText (com.xenoage.zong.musicxml.types.MxlFormattedText)2 Color (com.xenoage.utils.color.Color)1 FontInfo (com.xenoage.utils.font.FontInfo)1 Point2f (com.xenoage.utils.math.geom.Point2f)1 Size2f (com.xenoage.utils.math.geom.Size2f)1 AlignmentUtils.applyAlignmentToAttributeSet (com.xenoage.zong.desktop.utils.text.AlignmentUtils.applyAlignmentToAttributeSet)1 AlignmentUtils.fromAttributeSet (com.xenoage.zong.desktop.utils.text.AlignmentUtils.fromAttributeSet)1 FontInfoReader (com.xenoage.zong.io.musicxml.in.readers.FontInfoReader)1 Page (com.xenoage.zong.layout.Page)1 TextFrame (com.xenoage.zong.layout.frames.TextFrame)1 MxlCreditWords (com.xenoage.zong.musicxml.types.MxlCreditWords)1 MxlPosition (com.xenoage.zong.musicxml.types.attributes.MxlPosition)1 MxlVAlign (com.xenoage.zong.musicxml.types.enums.MxlVAlign)1