xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mistercaste <misterca...@gmail.com>
Subject JSVGCanvas - Constant text size
Date Mon, 09 Nov 2009 08:39:09 GMT

The following function allows to keep constant or proportional (see comments)
the text of a JSVGCanvas after zoom operations. Hope it can be useful.
Thanks to Thomas Deweese for support.




private void antiTransformText() {

    final Element textElementsGroup =
canvas.getSVGDocument().getElementById(AbstractDOM.STRIDSYMTESTOCENTROIDI);
    final NodeList textElement = textElementsGroup.getChildNodes();
    final int multiplier = 15;// Scale factor multiplier

    for (int i = 0; i < textElement.getLength(); i++) {

        final Element element = (Element) textElement.item(i);

        try {
            final AffineTransform crt =
canvas.getRenderingTransform().createInverse();

            // Constant scale factor
            // final double scala = (crt.getScaleX() < crt.getScaleY())?
crt.getScaleX() * multiplier : crt.getScaleY() * multiplier;

            // Autoreduced scale factor
            final double scala = Math.sqrt((crt.getScaleX() <
crt.getScaleY()) ? crt.getScaleX() * multiplier : crt.getScaleY() *
multiplier);

            final SVGLocatable loc = (SVGLocatable) element;
            final SVGRect bbox = loc.getBBox();
            if (bbox != null) {
                final float cx = bbox.getX();
                final float cy = bbox.getY();

                canvas.getUpdateManager().getUpdateRunnableQueue().
                        invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        // DOM Actions
                        element.setAttributeNS(
                                null,
                                "transform",
                                "translate(" + cx + "," + cy + "), " +
                                "scale(" + scala + "), " +
                                "translate(" + (-cx) + "," + (-cy) + ")");
                    }
                });
            }
        } catch (NoninvertibleTransformException ex) {
            Logger.getLogger(JFrameSvg.class.getName()).log(Level.SEVERE,
null, ex);
        }
    }
}
-- 
View this message in context: http://old.nabble.com/JSVGCanvas---Constant-text-size-tp26262831p26262831.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message