Search in sources :

Example 11 with FormattedTextElement

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

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

the class AndroidCanvas method drawText.

/**
 * {@inheritDoc}
 * The text selection is ignored.
 */
@Override
public void drawText(FormattedText text, TextSelection selection, Point2f position, boolean yIsBaseline, float frameWidth) {
    int oldTransform = canvas.save();
    canvas.translate(position.x, position.y);
    // print the text frame paragraph for paragraph
    float offsetX = 0;
    float offsetY = 0;
    for (FormattedTextParagraph p : text.getParagraphs()) {
        TextMetrics pMetrics = p.getMetrics();
        if (!yIsBaseline)
            offsetY += pMetrics.getAscent();
        // adjustment
        if (p.getAlignment() == Alignment.Center)
            offsetX = (frameWidth - pMetrics.getWidth()) / 2;
        else if (p.getAlignment() == Alignment.Right)
            offsetX = frameWidth - pMetrics.getWidth();
        else
            offsetX = 0;
        // draw elements
        for (FormattedTextElement e : p.getElements()) {
            if (e instanceof FormattedTextString) {
                // TODO - formatting
                FormattedTextString t = (FormattedTextString) e;
                Paint paint = new Paint(AndroidColorUtils.black);
                paint.setTypeface(Typeface.SERIF);
                paint.setTextSize(Units.pxToMm(t.getStyle().getFont().getSize(), 1));
                // nice, smooth drawing
                paint.setFlags(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                canvas.drawText(t.getText(), offsetX, offsetY, paint);
            } else {
                // symbol
                FormattedTextSymbol fts = (FormattedTextSymbol) e;
                float scaling = fts.getScaling();
                androidSymbolsRenderer.draw((PathSymbol) fts.getSymbol(), canvas, Color.black, new Point2f(offsetX + fts.getOffsetX(), offsetY + fts.getSymbol().baselineOffset * scaling), new Point2f(scaling, scaling));
            }
            offsetX += e.getMetrics().getWidth();
        }
        // next line
        offsetY += p.getMetrics().getAscent() + p.getMetrics().getDescent() + p.getMetrics().getLeading();
    }
    canvas.restoreToCount(oldTransform);
}
Also used : FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) Point2f(com.xenoage.utils.math.geom.Point2f) TextMetrics(com.xenoage.utils.font.TextMetrics) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) Paint(android.graphics.Paint) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) FormattedTextSymbol(com.xenoage.zong.core.text.FormattedTextSymbol) Paint(android.graphics.Paint)

Example 13 with FormattedTextElement

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

the class Test32b method check.

private void check(Words words, Text expectedText, MP mp) {
    FormattedTextElement elem = getFormattedTextElement(words, mp);
    assertEquals("" + mp, expectedText.getText(), elem.getText());
    assertEquals("" + mp, expectedText.getStyle(), elem.getStyle().getFont().getStyle());
    assertEquals("" + mp, expectedText.getColor(), elem.getStyle().getColor());
// TODO: wrong position coordinates in MusicXML file. for example, placement says "below"
// but default-y is positive. and default-x should be relative to the measure start
// bug reported: https://code.google.com/p/lilypond/issues/detail?id=4172
// assertEquals(""+mp, expectedText.getPlacement(), words.getPositioning());
}
Also used : FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement)

Aggregations

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