Differences between revisions 4 and 5
Revision 4 as of 2009-11-14 17:16:18
Size: 2629
Comment:
Revision 5 as of 2012-06-22 14:15:30
Size: 2617
Editor: drool
Comment: display java code with syntax highlighting
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
{{{#!highlight python {{{#!java

The following function allows to keep zoom-constant or zoom-proportional (see code remarks) a text group in a JSVGCanvas through DOM functionalities.

Typically it is invoked inside interactors which apply zoom transformations:

   1 private void antiTransformText() {
   2 
   3     // Specifies the DOM text group to keep constant
   4     final Element textElementsGroup = canvas.getSVGDocument().getElementById("WHATEVER");
   5     final NodeList textElement = textElementsGroup.getChildNodes();
   6     final int multiplier = 15;// Scale factor multiplier
   7 
   8     for (int i = 0; i < textElement.getLength(); i++) {
   9 
  10         final Element element = (Element) textElement.item(i);
  11 
  12         try {
  13             final AffineTransform crt = canvas.getRenderingTransform().createInverse();
  14 
  15             // Constant scale factor
  16             // final double scale = (crt.getScaleX() < crt.getScaleY())? crt.getScaleX() * multiplier : crt.getScaleY() * multiplier;
  17 
  18             // Autoreduced scale factor
  19             final double scale = Math.sqrt((crt.getScaleX() < crt.getScaleY()) ? crt.getScaleX() * multiplier : crt.getScaleY() * multiplier);
  20 
  21             final SVGLocatable loc = (SVGLocatable) element;
  22             final SVGRect bbox = loc.getBBox();
  23             if (bbox != null) {
  24                 final float cx = bbox.getX();
  25                 final float cy = bbox.getY();
  26 
  27                 canvas.getUpdateManager().getUpdateRunnableQueue().
  28                         invokeLater(new Runnable() {
  29 
  30                     @Override
  31                     public void run() {
  32                         // DOM Actions
  33                         element.setAttributeNS(
  34                                 null,
  35                                 "transform",
  36                                 "translate(" + cx + "," + cy + "), " +
  37                                 "scale(" + scale + "), " +
  38                                 "translate(" + (-cx) + "," + (-cy) + ")");
  39                     }
  40                 });
  41             }
  42         } catch (NoninvertibleTransformException ex) {
  43             Logger.getLogger(JFrameSvg.class.getName()).log(Level.SEVERE, null, ex);
  44         }
  45     }
  46 }

CanvasTextSize (last edited 2012-06-22 14:15:30 by drool)