| 
| text(-labels) musn't scale - possible? [message #217144] | Thu, 01 June 2006 08:22  |  | 
| Eclipse User  |  |  |  |  | I try to have labels that do not scale when zooming in/out. I didn't see any documentation on it so I dig into the sources and found that the whole
 text-scale is done in the graphics class and I can't see any way to tell
 it not to scale text. Another approach I tried is to have
 Label.setSize()/getSize() set to fixed values. Unfortunately those methods
 (inherited from Figure) are final and overridable. Anyone hints how to
 achieve this?
 |  |  |  | 
| 
| Re: text(-labels) musn't scale - possible? [message #217208 is a reply to message #217144] | Thu, 01 June 2006 13:35   |  | 
| Eclipse User  |  |  |  |  | Originally posted by: ben.walstrum.gmail.com 
 The short of this is that it can be done, but I think that the solution
 that I have is a bit of a kludge - but it was the best that I could find
 and does work.
 
 The problems all come in the paintClientArea() method of
 ScalableFreeformLayeredPane.  In that method the Graphics are set to be
 scaled, and thus all drawing commands from it will in turn be scaled.  I
 have created a class that extends ScalableFreeformLayeredPane and
 overrides paintClientArea().
 
 Next, I extended org.eclipse.draw2d.ScaledGraphics and passed in the
 graphics send to paintClientArea(), but keep a copy of it.  The graphics
 passed in are unscaled.  I then expose the graphics through a
 getOriginalGraphics() method.
 
 For your figure to paint using these graphics you will need to override
 the paint method, cast the graphics and send along the unscaled graphics.
 
 You probably will also need to add your new ScalableFreeformLayeredPane
 onto an overridden RootEditPart.
 
 I'm not sure that this would work the way you want for a normal label -
 you might need to draw the figures yourself because coordinates become
 confusing due to the scaling.
 
 Here's a snapshot of some of the code:
 
 public class ScaledGraphics extends org.eclipse.draw2d.ScaledGraphics {
 
 // these are necessary because the variables are private in
 // the superclass
 
 protected Graphics originalGraphics;
 protected double scale;
 
 public ScaledGraphics(final Graphics graphics) {
 super(graphics);
 this.originalGraphics = graphics;
 }
 
 public Graphics getOriginalGraphics() {
 return originalGraphics;
 }
 
 public void scale(final double scale) {
 super.scale(scale);
 this.scale = scale;
 }
 
 public double getScale() {
 return scale;
 }
 
 // ...
 
 }
 
 public class SemanticScalableFreeformLayeredPane extends
 ScalableFreeformLayeredPane {
 
 protected void paintClientArea(final Graphics graphics) {
 super.paintClientArea(graphics)
 if (!getChildren().isEmpty()) {
 ScaledGraphics g = new ScaledGraphics(graphics);
 
 boolean optimizeClip = getBorder() == null ||
 getBorder().isOpaque();
 if (!optimizeClip) {
 g.clipRect(getBounds().getCropped(getInsets()));
 }
 g.scale(getScale());
 g.pushState();
 paintChildren(g);
 g.dispose();
 graphics.restoreState();
 }
 }
 
 // ...
 }
 
 public class MyUnscaledFigure extends Figure
 
 public void paintFigure(final Graphics graphics) {
 Graphics original = ((ScaledGraphics)
 graphics).getOriginalGraphics();
 
 //-- paint away!! --//
 }
 }
 
 public class SemanticScalableFreeformRootEditPart extends
 ScalableFreeformRootEditPart {
 
 protected ScalableFreeformLayeredPane createScaledLayers() {
 pointerLayer = new PointerLayer();
 
 ScalableFreeformLayeredPane layers = new
 SemanticScalableFreeformLayeredPane();
 layers.add(createGridLayer(), GRID_LAYER);
 layers.add(getPrintableLayers(), PRINTABLE_LAYERS);
 layers.add(new FeedbackLayer(), SCALED_FEEDBACK_LAYER);
 return layers;
 }
 
 // ...
 }
 
 Again, I am certainly not advocating this as the best or cleanest
 solution - but from my searching of the code, it's the best that I could
 come up with.
 
 Hope this helps.
 
 Ben
 
 André wrote:
 > I try to have labels that do not scale when zooming in/out. I didn't see
 > any documentation on it so I dig into the sources and found that the
 > whole text-scale is done in the graphics class and I can't see any way
 > to tell it not to scale text. Another approach I tried is to have
 > Label.setSize()/getSize() set to fixed values. Unfortunately those
 > methods (inherited from Figure) are final and overridable. Anyone hints
 > how to achieve this?
 >
 >
 |  |  |  | 
|  | 
| 
| Re: text(-labels) musn't scale - possible? [message #217231 is a reply to message #217225] | Thu, 01 June 2006 15:44  |  | 
| Eclipse User  |  |  |  |  | Originally posted by: ben.walstrum.gmail.com 
 I looked up in my code to figure out what exactly the problem was, and
 it is related to the fact that the bounds of the figure will still be
 listed in their unscaled coordinates, not their scaled coordinates.
 
 For instance, suppose that you have a figure that has the coordianates
 x=10, y=10, w=100, h=100.  If you scale at 200% the drawn coordinates
 will be x=20, y=20, w=200, h=200, but your figure will still tell you
 (when you call getBounds() etc.) that it is x=10, y=10, w=100, h=100
 because it has no idea that it is being scaled (that is all handled by
 ScaledGraphics).  When you use a normal ScaledGraphics this isn't a
 problem because everything is scaled.
 
 So basically I just use the scale in my ScaledGraphics class to scale
 the bounds rectangle and then use that for any calculations i need
 during rendering using the original, unscaled graphics.
 
 Hope this helps.
 
 Ben
 
 
 André wrote:
 > Thanks a lot! Looks very promising!
 > I have to admit that I do see where's the exact problem with the
 > coordinates without debugging and seeing the exact values. If I remember
 > right, the font sizes passed to graphics are unscaled and graphics
 > substitues the font on behalf of the scaling-factor. Shouldn't be of any
 > problem, shouldn't it?
 >
 |  |  |  | 
Powered by 
FUDForum. Page generated in 0.03240 seconds