Search in sources :

Example 1 with PathSymbolGraphicAttribute

use of com.xenoage.zong.renderer.awt.symbol.PathSymbolGraphicAttribute in project Zong by Xenoage.

the class TextLayoutTools method create.

/**
 * Creates a {@link TextLayout} for the given {@link FormattedTextParagraph},
 * including both {@link FormattedTextString}s and {@link FormattedTextSymbol}s.
 * If impossible (e.g. because the paragraph is empty), null is returned.
 */
private static TextLayout create(FormattedTextParagraph p, FontRenderContext frc) {
    // get the raw string
    AttributedString as = new AttributedString(p.getText());
    // set attributes
    int pos = 0;
    int count = 0;
    for (FormattedTextElement e : p.getElements()) {
        int len = e.getLength();
        if (len > 0) {
            if (e instanceof FormattedTextString) {
                as.addAttributes(createAttributesMap(e.getStyle()), pos, pos + len);
                count++;
            } else if (e instanceof FormattedTextSymbol) {
                FormattedTextSymbol fts = (FormattedTextSymbol) e;
                Symbol symbol = fts.getSymbol();
                if (symbol instanceof PathSymbol) {
                    as.addAttribute(TextAttribute.FOREGROUND, toAwtColor(fts.getStyle().getColor()), pos, pos + 1);
                    as.addAttribute(TextAttribute.CHAR_REPLACEMENT, new PathSymbolGraphicAttribute((PathSymbol) symbol, fts.getScaling()), pos, pos + 1);
                    count += 2;
                }
            }
            pos += len;
        }
    }
    if (count == 0)
        // creating a TextLayout would fail
        return null;
    else
        return new TextLayout(as.getIterator(), frc);
}
Also used : AttributedString(java.text.AttributedString) PathSymbolGraphicAttribute(com.xenoage.zong.renderer.awt.symbol.PathSymbolGraphicAttribute) PathSymbol(com.xenoage.zong.symbols.PathSymbol) Symbol(com.xenoage.zong.symbols.Symbol) PathSymbol(com.xenoage.zong.symbols.PathSymbol) TextLayout(java.awt.font.TextLayout)

Aggregations

PathSymbolGraphicAttribute (com.xenoage.zong.renderer.awt.symbol.PathSymbolGraphicAttribute)1 PathSymbol (com.xenoage.zong.symbols.PathSymbol)1 Symbol (com.xenoage.zong.symbols.Symbol)1 TextLayout (java.awt.font.TextLayout)1 AttributedString (java.text.AttributedString)1