Search in sources :

Example 21 with FormattedText

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

the class VoltaStamper method stampInStaff.

/**
 * Creates a {@link VoltaStamping} for the given volta spanning
 * from the given start measure to the given end measure on the given staff
 * (global measure indices). Left and right hooks and the caption are optional.
 * The measure indices must be within the staff.
 */
private VoltaStamping stampInStaff(Volta volta, int startMeasureIndex, int endMeasureIndex, StaffStamping staff, FormattedTextStyle textStyle, boolean drawCaption, boolean drawLeftHook, boolean drawRightHook) {
    // get start and end x coordinate of measure
    float x1 = staff.system.getMeasureStartMm(startMeasureIndex) + staff.is / 2;
    float x2 = staff.system.getMeasureEndMm(endMeasureIndex) - staff.is / 2;
    // line position of volta line: 5 IS over top line
    float lp = (staff.linesCount - 1 + 5) * 2;
    // caption
    FormattedText caption = null;
    if (drawCaption && volta.getCaption().length() > 0) {
        caption = Companion.fText(volta.getCaption(), textStyle, Alignment.Left);
    }
    // create stamping
    return new VoltaStamping(volta, lp, x1, x2, caption, drawLeftHook, drawRightHook, staff);
}
Also used : VoltaStamping(com.xenoage.zong.musiclayout.stampings.VoltaStamping) FormattedText(com.xenoage.zong.core.text.FormattedText)

Example 22 with FormattedText

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

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