| Drawing text in a zoom-independent size [message #82063] |
Wed, 04 June 2003 16:19  |
Eclipse User |
|
|
|
I needed to implement a figure that draws its text in a zoom independent
size. I have used an approach (that required adding a method to
ScaledGraphics (see below). I would appreciate any feedback on this
approach, suggestions to other/better ways to do it. I would also like
to know whether its possible to include the enhancement to
ScaledGraphics in future versions of GEF.
I've done the following:
1. In its paint method, the user checks its absolute bounds to see if
there's enough space for the text (in the non-zoomable font).
2. If there is enough space, the figure casts the Graphics object to
ScaledGraphics, and calls the method ScaledGraphics.drawUnscaledText
public void drawUnscaledText(Font font, String text, Point location)
{
//Note: the following code may be windows specific
int height = font.getFontData()[0].getHeight();
Point zoomedLocation = new
Point(((int)(Math.floor((location.x*zoom)+fractionalX))),
(int)(Math.floor((location.y + height - 1)*zoom - height + 1
+fractionalY)));
Font oldFont = graphics.getFont();
graphics.setFont(font);
graphics.drawText(text, zoomTextPoint(location.x, location.y));
graphics.setFont(oldFont);
};
This method receives the text location in local coordinates, but draws
the text without scaling the font.
|
|
|
| Re: Drawing text in a zoom-independent size [message #82126 is a reply to message #82063] |
Wed, 04 June 2003 22:39   |
Eclipse User |
|
|
|
Originally posted by: none.us.ibm.com
What are trying to do? I've heard this before, and it sounds strange
because if you zoom out, the text won't fit inside the figure's bounds
anymore. One thing you could do is create a layer that doesn't scale, and
put the text on that layer. For example, handles follow a zoomed figure
perfectly, but they don't scale. Often this is the better approach.
To paint text that is not zoomed, all you need to do is perform a scale
around the location where you want to paint the text :-) Sounds easy,
right?:
paintFigure(Graphics g) {
Point textOutput = getBounds().getLocation();
g.translate(textOutput); //moves the "origin"
g.scale (1/currentZoomFactor); //scales about the origin
g.drawString(0, 0, "hello");
}
Since SWT doesn't use the baseline to draw text, this means that the fixed
point for the text will be the top-left.
"Youval" <youval_b@yahoo.com> wrote in message
news:3EDE545E.9000502@yahoo.com...
> I needed to implement a figure that draws its text in a zoom independent
> size. I have used an approach (that required adding a method to
> ScaledGraphics (see below). I would appreciate any feedback on this
> approach, suggestions to other/better ways to do it. I would also like
> to know whether its possible to include the enhancement to
> ScaledGraphics in future versions of GEF.
>
> I've done the following:
>
> 1. In its paint method, the user checks its absolute bounds to see if
> there's enough space for the text (in the non-zoomable font).
> 2. If there is enough space, the figure casts the Graphics object to
> ScaledGraphics, and calls the method ScaledGraphics.drawUnscaledText
>
>
> public void drawUnscaledText(Font font, String text, Point location)
> {
> //Note: the following code may be windows specific
> int height = font.getFontData()[0].getHeight();
> Point zoomedLocation = new
> Point(((int)(Math.floor((location.x*zoom)+fractionalX))),
> (int)(Math.floor((location.y + height - 1)*zoom - height + 1
> +fractionalY)));
>
> Font oldFont = graphics.getFont();
> graphics.setFont(font);
> graphics.drawText(text, zoomTextPoint(location.x, location.y));
> graphics.setFont(oldFont);
>
> };
>
>
> This method receives the text location in local coordinates, but draws
> the text without scaling the font.
>
|
|
|
| Re: Drawing text in a zoom-independent size [message #82152 is a reply to message #82126] |
Thu, 05 June 2003 02:12  |
Eclipse User |
|
|
|
Randy Hudson wrote:
> What are trying to do? I've heard this before, and it sounds strange
> because if you zoom out, the text won't fit inside the figure's bounds
> anymore. One thing you could do is create a layer that doesn't scale, and
> put the text on that layer. For example, handles follow a zoomed figure
> perfectly, but they don't scale. Often this is the better approach.
>
I want a figure to draw its "name" (a label, not necessarily a draw2d
Label) whenever there's enough space for it (depending on zoom level). I
don't want the text to get bigger as the user zooms further in.
> To paint text that is not zoomed, all you need to do is perform a scale
> around the location where you want to paint the text :-) Sounds easy,
> right?:
>
> paintFigure(Graphics g) {
> Point textOutput = getBounds().getLocation();
> g.translate(textOutput); //moves the "origin"
> g.scale (1/currentZoomFactor); //scales about the origin
> g.drawString(0, 0, "hello");
> }
>
This works fine, and is much simpler. Thanks !
|
|
|
Powered by
FUDForum. Page generated in 0.05628 seconds