JDK-4296952 : Cannot underline Font using Map
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 2.0,1.2.0,1.3.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_95
  • CPU: generic,x86
  • Submitted: 1999-12-05
  • Updated: 2017-05-16
  • Resolved: 2005-04-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b33Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description

Name: krT82822			Date: 12/04/99


original user summary:

	Map map = getFont().getAttributes();
	map.put(TextAttribute.SIZE, new Float(19.0));
	map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
	Font newFont = new Font(map);
	setFont(newFont);

This code is from the mouseEntered method of a class that extends JLabel.  The
effect of applying this new Font() is that the size is reflected, however, the
underline is NOT.

----------------------------

12/4/99 eval1127@eng -- filed against 1.2.x, but behavior the same under kestrel RA (1.3.0 build "I").

import java.awt.*;
import java.awt.font.*;
import java.util.*;

import javax.swing.*;

class MyLabel extends JLabel {
        public MyLabel(String text) {
                super(text);
                Map map = (new Font("Arial", Font.PLAIN, 48)).getAttributes();
                map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
                map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_TWO_PIXEL);
                Font newFont = new Font(map);
                setFont(newFont);
                setForeground(Color.blue);
        }
}

public class Blah extends JFrame {

        public Blah() {
                super("califragilistic...");
                MyLabel lbl = new MyLabel("how does this look");
                setContentPane(lbl);
                pack();
        }

        public static void main(String[] args) {
                Blah f = new Blah();
                f.setLocation(300,300);
                f.setVisible(true);
        }
}

(Review ID: 98544) 
======================================================================

Comments
WORK AROUND Name: krT82822 Date: 12/04/99 Could attempt to g.drawLine(), but this seeems very obtuse. ======================================================================
06-09-2004

EVALUATION The problem is that underline is a layout attribute not a font attribute and Graphics.setFont only sets the font (and font attributes). Using the same Map to create a TextLayout and drawing it should underline just fine. This distinction between font and layout attributes has never been clear to clients. One motive for making underline a layout attribute is that underline (and strikethrough) are normalized across a line so that the underline isn't at different heights if the fonts change. This normalization process is handled by textlayout. Another motive is that underline and strikethrough are considered embellishments that are not intrinsic to the font. Color is another such attribute. We'd like to be able to recognize fonts that have similar appearance regardless of these embellishments, and ignoring these is a convenient way to do that. That said, no one thinks of these attributes in this way. And since all calls to drawstring are effectively requests to draw entire lines, there should be some way to get the graphics to apply all the attributes. We need to define the semantics in a way that makes sense to clients and gives them convenient ways to do what they need to do. doug.felt@eng 1999-12-15 For Merlin I think we should change the semantics of rendering text such that all attributes understood by TextLayout are also understood when rendering text in a Graphics using drawString. doug.felt@eng 2000-01-10 ======================== Font.getAvailableAttributes() does not include UNDERLINE as one of the supported attributes you can set on a Font. /** * Returns the keys of all the attributes supported by this * <code>Font</code>. These attributes can be used to derive other * fonts. * @return an array containing the keys of all the attributes * supported by this <code>Font</code>. * @since 1.2 */ public Attribute[] getAvailableAttributes(); Thus this is really an application bug that it this was not checked. But as noted above it would be useful if it could be supported (so this is more an RFE than a bug) But setting attributes on a Font, even those returned by getAvailableAttributes() does not guarantee AWT peered components will support it, or if you have a Graphics thats not castable to Graphic2D its also possibly not going to work. We can update the list of understood attributes returned by Font to include all those that would cause delegation to TextLayout (and in fact should). So I don't know that the semantics need to change for clients, it should remain the same: "check the attributes supported by a font". That aside there is a minor "awareness" problem with that is probably confined to implementation of the Graphics2D class. All such implementations would need to have a way to detect and handle this. Most simply the presence of this attribute should cause delegation to the layout process which already handles this. However there are very few implementations of Graphics2D external to the JDK and the consequences would be minor. ###@###.### 2004-09-05 =============================
05-09-2004