Font Transforms, JTextPane, and AttributeSet

From: Will Friedman (williamf_at_alum.mit.edu)
Date: 03/28/04


Date: 28 Mar 2004 05:39:50 -0800

I am displaying styled text in a JTextPane using a
DefaultStyledDocument and setting the attributes of a
SimpleAttributeSet. I wish to alter the appearance of a character
using an AffineTransform on the font being used to display the text
for that character. Unfortunately, I am only able to set the font for
the attributes of that character using StyledConstants.SetFontFamily()
and StyledConstants.setFontSize(), which utterly disregards my
transform. Is there any way to correct this problem? Why is it that
there is no StyledConstants.setFont() method in the first place? A
code sample that demonstrates the problem by displaying text using the
unadjusted and adjusted font in JLabels and then in a JTextPane is
attached below.

Thanks in advance,

Will

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;

public class Example extends JFrame {
    public Example() {
        super("Example");
        this.setBackground(Color.black);
        this.setResizable(false);
        Font courier = new Font("Courier New", Font.PLAIN, 14);
        FontRenderContext frc =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(1,1).createGraphics().getFontRenderContext();
        Rectangle2D.Double maxCharSi ze = new Rectangle2D.Double();
        maxCharSize.setRect(courier.getMaxCharBounds(frc));
        System.out.println("max char size: " + maxCharSize.getHeight());
        Rectangle2D.Double actualCharSize = new Rectangle2D.Double();
        actualCharSize.setRect(courier.createGlyphVector(frc,
"\u2195").getGlyphMetrics(0).getBounds2D());
        System.out.println("actual char size: " +
actualCharSize.getHeight());
        double scaleFactor = maxCharSize.getHeight() /
actualCharSize.getHeight();
        System.out.println("Scale factor: " + scaleFactor);
        Font courierAdjusted =
courier.deriveFont(AffineTransform.getScaleInstance(1.0,
scaleFactor));
        JPanel panel = new JPanel();
        JLabel unadjusted = new JLabel("\u2195");
        unadjusted.setFont(courier);
        JLabel adjusted = new JLabel("\u2195");
        adjusted.setFont(courierAdjusted);
        panel.add(unadjusted);
        panel.add(adjusted);
        JTextPane pane = new JTextPane();
        SimpleAttributeSet attr = new SimpleAttributeSet();
        DefaultStyledDocument doc = new DefaultStyledDocument();
        try {
            StyleConstants.setFontFamily(attr, courier.getFamily());
            StyleConstants.setFontSize(attr, courier.getSize());
            StyleConstants.setBackground(attr, Color.black);
            StyleConstants.setForeground(attr, Color.white);
            doc.insertString(doc.getLength(), "\u2195", attr);
            StyleConstants.setFontFamily(attr, courierAdjusted.getFamily());
            StyleConstants.setFontSize(attr, courierAdjusted.getSize());
            doc.insertString(doc.getLength(), "\u2195", attr);
        } catch (Exception e) {
            System.out.println("Creating the doc failed");
        }
        pane.setEditable(false);
        pane.setBackground(Color.black);
        pane.setDocument(doc);
        panel.add(pane);
        this.getContentPane().add(panel);
    }

    public static void main(String[] args) {
        Example ex = new Example();
        ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ex.pack();
        ex.setVisible(true);
    }
}



Relevant Pages

  • Re: Conditional formatting problem in A97
    ... and make them appear to be invisible stopped displaying a solid bar ... and starting actually showing the underlying character. ... several locations and stopped working in all of that forms controls, ... Actually, after looking closer at it, I did change the font. ...
    (comp.databases.ms-access)
  • Re: special charater display
    ... > I have problem with displaying diamond special character ... This sounds like the font you're using simply doesn't happen to have ... a diamond at that code point. ... you're expecting to use the OEM character set, ...
    (microsoft.public.vc.mfc)
  • Re: How to translate a chinese character to unicode by using c#.net?
    ... so if his device is not displaying a character that's in ... therefore it's correctly displaying the data but the data is wrong. ... He's taking Unicode and converting to ASCII and expecting it to not lose ... Install the right font and it will work. ...
    (microsoft.public.pocketpc.developer)
  • disabling font substitution
    ... requesting a character to be displayed in a widget if the particular ... font requested does not have that character? ... determine what characters in the font are provided, by displaying a ...
    (comp.lang.tcl)
  • Re: Saving Arabic Text
    ... When i'm displaying a java message box the arabic text is displaying ... Most of my comments below assume that you do, indeed, mean HTTP. ... Both client and server must agree on the character encoding; otherwise character data transferred between them will be garbled. ... Some of the things you probably need to do involve configuring the HTTP message carrying the HTML page in which the form is delivered, and the adjusting details of the form itself: The HTTP message should be encoded with a charset that supports the characters you need; UTF-8 is a good general-purpose choice. ...
    (comp.lang.java.programmer)