You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by mistercaste <mi...@gmail.com> on 2009/11/09 09:39:09 UTC

JSVGCanvas - Constant text size

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


Re: JSVGCanvas - Constant text size

Posted by mistercaste <mi...@gmail.com>.
If the text centering is needed here's a useful 
http://old.nabble.com/zooming-problem-to3462572.html#a3467449 link .
-- 
View this message in context: http://old.nabble.com/JSVGCanvas---Constant-text-size-tp26262831p27881846.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


Re: JSVGCanvas - Constant text size

Posted by mistercaste <mi...@gmail.com>.
Well done, you can find it in section 
http://wiki.apache.org/xmlgraphics-batik/UserContributions Batik User
Contributions .

Cheers :-)
-- 
View this message in context: http://old.nabble.com/JSVGCanvas---Constant-text-size-tp26262831p26368854.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


Re: JSVGCanvas - Constant text size

Posted by th...@kodak.com.
Hi Mistercaste,

mistercaste <mi...@gmail.com> wrote on 11/09/2009 03:39:09 AM:

> 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.

    I'm glad you were able to get things working.  Could I ask one
little thing? 

    Batik has a Wiki which would be a good place to put example code 
like this.  Could you write a small 'article' and attach this code:

        http://wiki.apache.org/xmlgraphics-batik/FrontPage

    Thanks

> 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
>