Lines 21-26
Link Here
|
21 |
import org.netbeans.modules.visual.util.GeomUtil; |
21 |
import org.netbeans.modules.visual.util.GeomUtil; |
22 |
|
22 |
|
23 |
import java.awt.*; |
23 |
import java.awt.*; |
|
|
24 |
import java.awt.font.FontRenderContext; |
25 |
import java.awt.font.GlyphVector; |
24 |
import java.awt.geom.AffineTransform; |
26 |
import java.awt.geom.AffineTransform; |
25 |
import java.awt.geom.Rectangle2D; |
27 |
import java.awt.geom.Rectangle2D; |
26 |
|
28 |
|
Lines 60-65
Link Here
|
60 |
private VerticalAlignment verticalAlignment = VerticalAlignment.BASELINE; |
62 |
private VerticalAlignment verticalAlignment = VerticalAlignment.BASELINE; |
61 |
private Orientation orientation = Orientation.NORMAL; |
63 |
private Orientation orientation = Orientation.NORMAL; |
62 |
private boolean paintAsDisabled; |
64 |
private boolean paintAsDisabled; |
|
|
65 |
private boolean useGlyphVector = false; |
66 |
|
67 |
private GlyphVector cacheGlyphVector; |
68 |
private String cacheLabel; |
69 |
private Font cacheFont; |
63 |
|
70 |
|
64 |
/** |
71 |
/** |
65 |
* Creates a label widget. |
72 |
* Creates a label widget. |
Lines 174-189
Link Here
|
174 |
} |
181 |
} |
175 |
|
182 |
|
176 |
/** |
183 |
/** |
|
|
184 |
* Returns whether the label widget is using glyph vector for rendering text. |
185 |
* @return true, if the label widget is using glyph vector |
186 |
*/ |
187 |
public boolean isUseGlyphVector () { |
188 |
return useGlyphVector; |
189 |
} |
190 |
|
191 |
/** |
192 |
* Sets whether the label widget is using glyph vector for rendering text. |
193 |
* <p> |
194 |
* Note that using glyph vector could slow-down the rendering performance. |
195 |
* Note that if you are not using glyph vector then the text may be clipped when a scene has zoom factor different from 1.0. |
196 |
* @param useGlyphVector if true, then a glyph vector is used for rendering text |
197 |
*/ |
198 |
public void setUseGlyphVector (boolean useGlyphVector) { |
199 |
if (this.useGlyphVector == useGlyphVector) |
200 |
return; |
201 |
this.useGlyphVector = useGlyphVector; |
202 |
cacheGlyphVector = null; |
203 |
cacheLabel = null; |
204 |
cacheFont = null; |
205 |
revalidate (); |
206 |
} |
207 |
|
208 |
private void assureGlyphVector () { |
209 |
Font font = getFont (); |
210 |
FontRenderContext fontRenderContext = getGraphics ().getFontRenderContext (); |
211 |
if (cacheGlyphVector != null && cacheFont == font && cacheLabel == label) |
212 |
return; |
213 |
cacheFont = font; |
214 |
cacheLabel = label; |
215 |
cacheGlyphVector = font.createGlyphVector (new FontRenderContext (new AffineTransform (), fontRenderContext.isAntiAliased (), fontRenderContext.usesFractionalMetrics ()), cacheLabel); |
216 |
} |
217 |
|
218 |
/** |
177 |
* Calculates a client area for the label. |
219 |
* Calculates a client area for the label. |
178 |
* @return the client area |
220 |
* @return the client area |
179 |
*/ |
221 |
*/ |
180 |
protected Rectangle calculateClientArea () { |
222 |
protected Rectangle calculateClientArea () { |
181 |
if (label == null) |
223 |
if (label == null) |
182 |
return super.calculateClientArea (); |
224 |
return super.calculateClientArea (); |
183 |
Graphics2D gr = getGraphics (); |
225 |
Rectangle rectangle; |
184 |
FontMetrics fontMetrics = gr.getFontMetrics (getFont ()); |
226 |
if (useGlyphVector) { |
185 |
Rectangle2D stringBounds = fontMetrics.getStringBounds (label, gr); |
227 |
assureGlyphVector (); |
186 |
Rectangle rectangle = GeomUtil.roundRectangle (stringBounds); |
228 |
rectangle = GeomUtil.roundRectangle (cacheGlyphVector.getVisualBounds ()); |
|
|
229 |
rectangle.grow (1, 1); // WORKAROUND - even text antialiasing is included into the boundary |
230 |
} else { |
231 |
Graphics2D gr = getGraphics (); |
232 |
FontMetrics fontMetrics = gr.getFontMetrics (getFont ()); |
233 |
Rectangle2D stringBounds = fontMetrics.getStringBounds (label, gr); |
234 |
rectangle = GeomUtil.roundRectangle (stringBounds); |
235 |
} |
187 |
switch (orientation) { |
236 |
switch (orientation) { |
188 |
case NORMAL: |
237 |
case NORMAL: |
189 |
return rectangle; |
238 |
return rectangle; |
Lines 201-207
Link Here
|
201 |
if (label == null) |
250 |
if (label == null) |
202 |
return; |
251 |
return; |
203 |
Graphics2D gr = getGraphics (); |
252 |
Graphics2D gr = getGraphics (); |
204 |
gr.setFont (getFont ()); |
253 |
if (useGlyphVector) |
|
|
254 |
assureGlyphVector (); |
255 |
else |
256 |
gr.setFont (getFont ()); |
205 |
|
257 |
|
206 |
FontMetrics fontMetrics = gr.getFontMetrics (); |
258 |
FontMetrics fontMetrics = gr.getFontMetrics (); |
207 |
Rectangle clientArea = getClientArea (); |
259 |
Rectangle clientArea = getClientArea (); |
Lines 220-229
Link Here
|
220 |
x = clientArea.x; |
272 |
x = clientArea.x; |
221 |
break; |
273 |
break; |
222 |
case CENTER: |
274 |
case CENTER: |
223 |
x = clientArea.x + (clientArea.width - fontMetrics.stringWidth (label)) / 2; |
275 |
if (useGlyphVector) |
|
|
276 |
x = clientArea.x + (clientArea.width - getCacheGlyphVectorWidth ()) / 2; |
277 |
else |
278 |
x = clientArea.x + (clientArea.width - fontMetrics.stringWidth (label)) / 2; |
224 |
break; |
279 |
break; |
225 |
case RIGHT: |
280 |
case RIGHT: |
226 |
x = clientArea.x + clientArea.width - fontMetrics.stringWidth (label); |
281 |
if (useGlyphVector) |
|
|
282 |
x = clientArea.x + clientArea.width - getCacheGlyphVectorWidth (); |
283 |
else |
284 |
x = clientArea.x + clientArea.width - fontMetrics.stringWidth (label); |
227 |
break; |
285 |
break; |
228 |
default: |
286 |
default: |
229 |
return; |
287 |
return; |
Lines 271-280
Link Here
|
271 |
y = 0; |
329 |
y = 0; |
272 |
break; |
330 |
break; |
273 |
case TOP: |
331 |
case TOP: |
|
|
332 |
if (useGlyphVector) |
333 |
y = clientArea.y + getCacheGlyphVectorWidth (); |
334 |
else |
274 |
y = clientArea.y + fontMetrics.stringWidth (label); |
335 |
y = clientArea.y + fontMetrics.stringWidth (label); |
275 |
break; |
336 |
break; |
276 |
case CENTER: |
337 |
case CENTER: |
277 |
y = clientArea.y + (clientArea.height + fontMetrics.stringWidth (label)) / 2; |
338 |
if (useGlyphVector) |
|
|
339 |
y = clientArea.y + (clientArea.height + getCacheGlyphVectorWidth ()) / 2; |
340 |
else |
341 |
y = clientArea.y + (clientArea.height + fontMetrics.stringWidth (label)) / 2; |
278 |
break; |
342 |
break; |
279 |
case BOTTOM: |
343 |
case BOTTOM: |
280 |
y = clientArea.y + clientArea.height; |
344 |
y = clientArea.y + clientArea.height; |
Lines 304-318
Link Here
|
304 |
if (paintAsDisabled && background instanceof Color) { |
368 |
if (paintAsDisabled && background instanceof Color) { |
305 |
Color color = ((Color) background); |
369 |
Color color = ((Color) background); |
306 |
gr.setColor (color.brighter ()); |
370 |
gr.setColor (color.brighter ()); |
307 |
gr.drawString (label, 1, 1); |
371 |
if (useGlyphVector) |
|
|
372 |
gr.fill (cacheGlyphVector.getOutline (1, 1)); |
373 |
else |
374 |
gr.drawString (label, 1, 1); |
308 |
gr.setColor (color.darker ()); |
375 |
gr.setColor (color.darker ()); |
309 |
gr.drawString (label, 0, 0); |
376 |
if (useGlyphVector) |
|
|
377 |
gr.fill (cacheGlyphVector.getOutline ()); |
378 |
else |
379 |
gr.drawString (label, 0, 0); |
310 |
} else { |
380 |
} else { |
311 |
gr.setColor (getForeground ()); |
381 |
gr.setColor (getForeground ()); |
312 |
gr.drawString (label, 0, 0); |
382 |
if (useGlyphVector) |
|
|
383 |
gr.fill (cacheGlyphVector.getOutline ()); |
384 |
else |
385 |
gr.drawString (label, 0, 0); |
313 |
} |
386 |
} |
314 |
|
387 |
|
315 |
gr.setTransform(previousTransform); |
388 |
gr.setTransform(previousTransform); |
|
|
389 |
} |
390 |
|
391 |
private int getCacheGlyphVectorWidth () { |
392 |
return GeomUtil.roundRectangle (cacheGlyphVector.getVisualBounds ()).width; |
316 |
} |
393 |
} |
317 |
|
394 |
|
318 |
} |
395 |
} |