Search in sources :

Example 1 with SVGLocatable

use of org.w3c.dom.svg.SVGLocatable in project gephi by gephi.

the class NodeLabelRenderer method renderSVG.

public void renderSVG(SVGTarget target, Node node, String label, float x, float y, int fontSize, Color color, float outlineSize, Color outlineColor, boolean showBox, Color boxColor) {
    Text labelText = target.createTextNode(label);
    Font font = fontCache.get(fontSize);
    if (outlineSize > 0) {
        Text labelTextOutline = target.createTextNode(label);
        Element outlineElem = target.createElement("text");
        outlineElem.setAttribute("class", node.getId().toString());
        outlineElem.setAttribute("x", String.valueOf(x));
        outlineElem.setAttribute("y", String.valueOf(y));
        outlineElem.setAttribute("style", "text-anchor: middle; dominant-baseline: central;");
        outlineElem.setAttribute("fill", target.toHexString(color));
        outlineElem.setAttribute("font-family", font.getFamily());
        outlineElem.setAttribute("font-size", String.valueOf(fontSize));
        outlineElem.setAttribute("stroke", target.toHexString(outlineColor));
        outlineElem.setAttribute("stroke-width", (outlineSize * target.getScaleRatio()) + "px");
        outlineElem.setAttribute("stroke-linecap", "round");
        outlineElem.setAttribute("stroke-linejoin", "round");
        outlineElem.setAttribute("stroke-opacity", String.valueOf(outlineColor.getAlpha() / 255f));
        outlineElem.appendChild(labelTextOutline);
        target.getTopElement(SVGTarget.TOP_NODE_LABELS_OUTLINE).appendChild(outlineElem);
        //Trick to center text vertically on node:
        SVGRect rect = ((SVGLocatable) outlineElem).getBBox();
        outlineElem.setAttribute("y", String.valueOf(y + rect.getHeight() / 4f));
    }
    Element labelElem = target.createElement("text");
    labelElem.setAttribute("class", node.getId().toString());
    labelElem.setAttribute("x", String.valueOf(x));
    labelElem.setAttribute("y", String.valueOf(y));
    labelElem.setAttribute("style", "text-anchor: middle; dominant-baseline: central;");
    labelElem.setAttribute("fill", target.toHexString(color));
    labelElem.setAttribute("font-family", font.getFamily());
    labelElem.setAttribute("font-size", String.valueOf(fontSize));
    labelElem.appendChild(labelText);
    target.getTopElement(SVGTarget.TOP_NODE_LABELS).appendChild(labelElem);
    //Trick to center text vertically on node:
    SVGRect rect = ((SVGLocatable) labelElem).getBBox();
    labelElem.setAttribute("y", String.valueOf(y + rect.getHeight() / 4f));
    //Box
    if (showBox) {
        rect = ((SVGLocatable) labelElem).getBBox();
        Element boxElem = target.createElement("rect");
        boxElem.setAttribute("x", Float.toString(rect.getX() - outlineSize / 2f));
        boxElem.setAttribute("y", Float.toString(rect.getY() - outlineSize / 2f));
        boxElem.setAttribute("width", Float.toString(rect.getWidth() + outlineSize));
        boxElem.setAttribute("height", Float.toString(rect.getHeight() + outlineSize));
        boxElem.setAttribute("fill", target.toHexString(boxColor));
        boxElem.setAttribute("opacity", String.valueOf(boxColor.getAlpha() / 255f));
        target.getTopElement(SVGTarget.TOP_NODE_LABELS).insertBefore(boxElem, labelElem);
    }
}
Also used : SVGLocatable(org.w3c.dom.svg.SVGLocatable) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) SVGRect(org.w3c.dom.svg.SVGRect) BaseFont(com.itextpdf.text.pdf.BaseFont) Font(java.awt.Font)

Aggregations

BaseFont (com.itextpdf.text.pdf.BaseFont)1 Font (java.awt.Font)1 Element (org.w3c.dom.Element)1 Text (org.w3c.dom.Text)1 SVGLocatable (org.w3c.dom.svg.SVGLocatable)1 SVGRect (org.w3c.dom.svg.SVGRect)1