Search in sources :

Example 6 with FormattedTextString

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

the class FormattedTextConverter method toStyledDocument.

/**
 * Creates a {@link StyledDocument} from the given {@link FormattedText}.
 */
public static StyledDocument toStyledDocument(FormattedText input) {
    StyledDocument styledDoc = new DefaultStyledDocument();
    // handle all paragraphs
    SimpleAttributeSet lastStyle = new SimpleAttributeSet();
    int line = 0;
    for (FormattedTextParagraph p : input.getParagraphs()) {
        // apply the alignment of the paragraph (apply style to the first letter of the paragraph)
        SimpleAttributeSet paragraphStyle = new SimpleAttributeSet();
        applyAlignmentToAttributeSet(p.getAlignment(), paragraphStyle);
        styledDoc.setParagraphAttributes(styledDoc.getLength(), 1, paragraphStyle, true);
        // handle all elements within this paragraph
        for (// TODO: Symbols ?!
        FormattedTextElement t : // TODO: Symbols ?!
        p.getElements()) {
            if (t instanceof FormattedTextString) {
                FormattedTextString s = (FormattedTextString) t;
                try {
                    lastStyle = getAttributeSetFromStyle(s.getStyle());
                    styledDoc.insertString(styledDoc.getLength(), s.getText(), lastStyle);
                } catch (BadLocationException e) {
                }
            }
        }
        // after the paragraph (not after the last): insert a line break
        if (line < input.getParagraphs().size() - 1) {
            try {
                styledDoc.insertString(styledDoc.getLength(), "\n", lastStyle);
            } catch (BadLocationException e) {
            }
        }
        line++;
    }
    return styledDoc;
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) StyledDocument(javax.swing.text.StyledDocument) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) BadLocationException(javax.swing.text.BadLocationException)

Example 7 with FormattedTextString

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

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

the class FormattedTextParagraphTest method testFormattedTextParagraph.

@Test
public void testFormattedTextParagraph() {
    FormattedTextStyle style = new FormattedTextStyle(new FontInfo((String) null, null, fontStyle(FontStyle.Bold)), Color.Companion.getRed(), Superscript.Super);
    FormattedTextParagraph paragraph = Companion.fPara(Companion.fString("This is a formatted text.", style));
    assertEquals("This is a formatted text.", paragraph.getText());
    style = ((FormattedTextString) paragraph.getElements().getFirst()).getStyle();
    assertEquals(true, style.getFont().getStyle().isSet(FontStyle.Bold));
    assertEquals(false, style.getFont().getStyle().isSet(FontStyle.Italic));
    assertEquals(false, style.getFont().getStyle().isSet(FontStyle.Underline));
    assertEquals(false, style.getFont().getStyle().isSet(FontStyle.Strikethrough));
    assertEquals(Color.Companion.getRed(), style.getColor());
    assertEquals(Superscript.Super, style.getSuperscript());
}
Also used : FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedTextString.fString(com.xenoage.zong.core.text.FormattedTextString.fString) FontInfo(com.xenoage.utils.font.FontInfo) Test(org.junit.Test)

Example 9 with FormattedTextString

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

the class FormattedTextParagraphTest method getMixedStyleTextParagraph.

public static FormattedTextParagraph getMixedStyleTextParagraph() {
    FormattedTextStyle style1 = new FormattedTextStyle(new FontInfo(new JLabel().getFont().getName(), 14f, fontStyle(FontStyle.Italic, FontStyle.Underline)));
    FormattedTextStyle style2 = new FormattedTextStyle(new FontInfo(new JLabel().getFont().getName(), 14f, fontStyle(FontStyle.Bold, FontStyle.Italic, FontStyle.Underline, FontStyle.Strikethrough)), Color.Companion.getGreen(), null);
    FormattedTextParagraph paragraph = new FormattedTextParagraph(CList.<FormattedTextElement>ilist(new FormattedTextString("This is ", style1), new FormattedTextString("a mixed styled text!", style2)), FormattedTextParagraph.Companion.getDefaultAlignment());
    return paragraph;
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) JLabel(javax.swing.JLabel) FontInfo(com.xenoage.utils.font.FontInfo)

Example 10 with FormattedTextString

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

Aggregations

FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)17 FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)11 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)10 FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)10 Test (org.junit.Test)7 FontInfo (com.xenoage.utils.font.FontInfo)5 FormattedText (com.xenoage.zong.core.text.FormattedText)5 FormattedTextSymbol (com.xenoage.zong.core.text.FormattedTextSymbol)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