Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for Swing text AA properties replacement

Tags:

java

swing

Everyone knows that using proprietary API it is a really bad thing to do. In almost all cases you can replace that API with your own implementation or some additional library.

Almost...

Here is the case in which you cannot find any alternatives:

table.put ( SwingUtilities2.AA_TEXT_PROPERTY_KEY, SwingUtilities2.AATextInfo.getAATextInfo ( true ) );

This line of code puts proper text antialias settings into L&F defaults table. If you do not use those settings you will have tons of problems with rendering some specific symbols inside any text component (for example thai - "ข้อความที่เรียบง่าย", or arabic - "رسالة نصية بسيطة").

It cannot be replaced with something else, because the instance of AATextInfo (yes, exactly that class, not something else) is used within Swing architecture when it comes to text rendering and Look and Feel simply adds that instance into defaults so that components can use it.

So here is the point when i have to decide - either have a really-really bad thai/arabic/some else font rendering in my L&F or use that damn proprietari API.

And as you might know - warnings like:

XXX is Sun proprietary API and may be removed in a future release

Cannot be suppressed: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6476630
Well, that is actually a reasonable limitation so its not a problem.

The problem is - why the hell i cannot replace that damn thing by something else?
Maybe i am missing something?

Ofcourse that problems pops up only if you are trying to create your own L&F, otherwise you would never need to use that thing anywhere.

So the question is:
Is there any replacement/workaround for that rendering feature or not?

like image 707
Mikle Garin Avatar asked Nov 02 '22 15:11

Mikle Garin


1 Answers

Use java.awt.RenderingHints, it doesn't gen warnings.

final Map<Object, Object> hints = new HashMap<Object, Object>();
hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

...

  public static Map<Object, Object> setHints(Graphics2D g, Map<Object, Object> hints) {
    final RenderingHints rhints = g.getRenderingHints();
    for (final Map.Entry<Object, Object> entry : hints.entrySet()) {
        if (!(entry.getKey() instanceof RenderingHints.Key)) {
            continue;
        }

        final RenderingHints.Key key = (RenderingHints.Key) entry.getKey();
        final Object value = rhints.get(key);
        if (entry.getValue() == null) {
            rhints.remove(key);
        }
        else {
           rhints.put(key, entry.getValue());
        }
        entry.setValue(value);
    }

    return hints;
}
like image 91
noclayto Avatar answered Nov 17 '22 11:11

noclayto