Search in sources :

Example 1 with PathSymbol

use of com.xenoage.zong.symbols.PathSymbol in project Zong by Xenoage.

the class SvgPathSymbolReader method read.

/**
 * Creates a {@link PathSymbol} from the given SVG document.
 * If an error occurs, an IllegalArgumentException is thrown.
 * @param id         the ID of the symbol
 * @param xmlReader  the {@link XmlReader}, positioned at the root element. It will
 *                   not be closed by this this method.
 */
public static PathSymbol read(String id, XmlReader xmlReader) {
    // read baseline and ascent, if there
    Float baseline = null;
    Float ascent = null;
    String attr = xmlReader.getAttribute("zong-baseline");
    if (attr != null)
        baseline = Parser.parseFloat(attr) * 0.01f - 10;
    attr = xmlReader.getAttribute("zong-ascent");
    if (attr != null)
        ascent = Parser.parseFloat(attr) * 0.01f;
    // custom left and right border, if there
    Float leftBorder = null;
    Float rightBorder = null;
    attr = xmlReader.getAttribute("zong-leftborder");
    if (attr != null)
        leftBorder = Parser.parseFloat(attr) * 0.01f - 10;
    attr = xmlReader.getAttribute("zong-rightborder");
    if (attr != null)
        rightBorder = Parser.parseFloat(attr) * 0.01f - 10;
    // search for a path
    String d = findPath(xmlReader);
    // otherwise throw exception.
    if (d != null) {
        Path path = new SvgPathReader(d).read();
        PathSymbol ret = new PathSymbol(id, path, baseline, ascent, leftBorder, rightBorder);
        return ret;
    } else {
        throw new IllegalArgumentException("No path element was found!");
    }
}
Also used : Path(com.xenoage.zong.symbols.path.Path) PathSymbol(com.xenoage.zong.symbols.PathSymbol)

Example 2 with PathSymbol

use of com.xenoage.zong.symbols.PathSymbol 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)

Example 3 with PathSymbol

use of com.xenoage.zong.symbols.PathSymbol in project Zong by Xenoage.

the class SvgSymbolReader method step2_readFile.

private void step2_readFile(InputStream stream) {
    // create xml reader
    XmlReader xmlReader = platformUtils().createXmlReader(stream);
    // read id element. it has the format "type:id", e.g.
    // "path:clef-g", or "styled:warning". If there is no ":",
    // the type "path" is used.
    // styles: path, styled, rect
    PathSymbol ret = null;
    Exception ex = null;
    try {
        if (xmlReader.openNextChildElement()) {
            String elementId = xmlReader.getAttribute("id");
            if (elementId == null || elementId.indexOf(':') == -1) {
                // no format information. use path.
                ret = SvgPathSymbolReader.read(id, xmlReader);
            } else {
                String format = elementId.split(":")[0];
                switch(format) {
                    case "path":
                        ret = SvgPathSymbolReader.read(id, xmlReader);
                        break;
                    case "rect":
                        ex = new IllegalStateException("Could not load \"" + svgFilepath + "\": \"" + format + "\" (rect is no longer supported. Convert it into a path)");
                        break;
                    case "styled":
                        ex = new IllegalStateException("Could not load \"" + svgFilepath + "\": \"" + format + "\" (currently styled symbols are not supported)");
                        break;
                    default:
                        ex = new IllegalStateException("Unknown symbol format in \"" + svgFilepath + "\": \"" + format + "\"");
                        break;
                }
            }
        }
    } catch (Exception readerEx) {
        ex = readerEx;
    } finally {
        xmlReader.close();
    }
    if (ex != null)
        result.onFailure(ex);
    else
        result.onSuccess(ret);
}
Also used : XmlReader(com.xenoage.utils.xml.XmlReader) PathSymbol(com.xenoage.zong.symbols.PathSymbol)

Aggregations

PathSymbol (com.xenoage.zong.symbols.PathSymbol)3 XmlReader (com.xenoage.utils.xml.XmlReader)1 PathSymbolGraphicAttribute (com.xenoage.zong.renderer.awt.symbol.PathSymbolGraphicAttribute)1 Symbol (com.xenoage.zong.symbols.Symbol)1 Path (com.xenoage.zong.symbols.path.Path)1 TextLayout (java.awt.font.TextLayout)1 AttributedString (java.text.AttributedString)1