🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #720

Trouble scaling rotated SansSerif Font in JDK 1.4.1

#720
From: Alex Waller
Date: Fri, 07 Mar 2003 21:32
197 lines
7394 bytes
Hello,

    Earlier I've had problems posting to the comp.lang.java
newsgroup. I still haven't found an answer to my problem so I'm
reposting to comp.lang.java and borland.public.jbuilder in hopes that
someone can offer a solution to (or at least verify I'm not the only
one with) this problem. In frustration, I tried submitting this
as a bug report to Sun but after 3 tries using 2 different
browsers getting some kind of bad URL link after I pressed the
"submit" button, I return here in hopes that someone can help.
    Up until recently I've been using the JBuilder 7 1.3.1 JDK. I
downloaded Sun's JDK 1.4.1 and configured the JDK via the Tool menu
since we will have to use 1.4.1 at work. I've discovered a big
difference in the way rotated text gets scaled. I've tried searching
the Borland archives but have not found anything pertaining to this
particular problem with fonts. I've also checked the Sun site and have
not seen any mention of this particular problem.
    All the 1.3.1 JDKs I tried seems to do a much better job at
rendering rotated text when one makes a window (or panel) long and
narrow (the text looks stretched but not ugly). I have some plotting
code which can create several (plot) panels on a window and this long,
narrow plot can be exactly my situation. The rotated text is the Y-axis
label. The 1.4.1 JDK from Sun does not seem to scale very nicely at all.

There sometimes are large gaps between characters in words. Sometimes
single characters within words are much larger than others. Sometimes
(depending on how the panel gets sized) the letters turn out all
blotchy.
Really ugly.
    To summarize; I've verified that Borland 1.3.1 JDK on Windows,
Sun 1.3.1_07 on Windows, and Apple 1.3.1 JDK on the Mac all seem to work

the same and produce "acceptable" results when a window having rotated
text is resized to a long, narrow window. However the Sun version JDK
1.4.1_01 produces unacceptable results. So something "broke" with
version 1.4.1_01. Is this a known problem? Is there a workaround? Or
should I be now doing things differently?
    Some extra added info that might help. It seems to be associated
with the screen rendering of fonts. A plot procedure that I have can
print several (plot) panels (JPanel) that are in a window to a single
page. The rotated fonts seem to come out OK on the printer under JDK
1.4.1 (again they looked stretched out but not illegible).
    I include source code below which demonstrates this problem. After
the window is displayed make the window long and narrow and watch what
happens to the rotated (Y-axis) text.

    -Thanks in advance, Alex Waller
/*
   FontScalingProblem.java
*/
package fontscalingproblem;

import javax.swing.UIManager;
import java.awt.*;

/**
 * <p>Title: FontScalingProblem</p>
 * <p>Description: Minimal Demo of 1.4.1 Font Scaling Problem</p>
 * @author A. M. Waller
 * @version 1.0
 */

public class FontScalingProblem
{
 private boolean packFrame = false;

//Construct the application
 public FontScalingProblem()
 {
  Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size
//  info, e.g. from their layout
  if (packFrame)
  {
   frame.pack();
  }
  else
  {
   frame.validate();
  }
//Center the window
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  Dimension frameSize = frame.getSize();
  if (frameSize.height > screenSize.height)
  {
   frameSize.height = screenSize.height;
  }
  if (frameSize.width > screenSize.width)
  {
   frameSize.width = screenSize.width;
  }
  frame.setLocation((screenSize.width - frameSize.width) / 2,
                    (screenSize.height - frameSize.height) / 2);
  frame.setVisible(true);
 }
//Main method
 public static void main(String[] args)
 {
  try
  {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  new FontScalingProblem();
 }
}
/*
   Frame1.java
*/
package fontscalingproblem;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * <p>Title: FontScalingProblem</p>
 * <p>Description: Minimal Demo of 1.4.1 Font Scaling Problem</p>
 * @author A. M. Waller
 * @version 1.0
 */

public class Frame1 extends JFrame
{
 private JPanel contentPane;
 private BorderLayout borderLayout1 = new BorderLayout();

 private static final int LABEL_PT_BASE = 36;
 private static final int windowWidth = 850, windowHeight = 850;
 private static final Font labelFont =
                     new Font( "SansSerif", Font.PLAIN, LABEL_PT_BASE );

 private double baseWidth, baseHeight;

//Construct the frame
 public Frame1()
 {
  enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  try
  {
   jbInit();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
//Component initialization
 private void jbInit() throws Exception
 {
  contentPane = (JPanel) this.getContentPane();
  contentPane.setLayout(borderLayout1);
  baseWidth = windowWidth; baseHeight = windowHeight;
  this.setSize(new Dimension(windowWidth, windowHeight));
  this.setTitle("Font Rotation and Scaling Problem");
 }
//Overridden so we can exit when window is closed
 protected void processWindowEvent(WindowEvent e)
 {
  super.processWindowEvent(e);
  if (e.getID() == WindowEvent.WINDOW_CLOSING)
  {
   System.exit(0);
  }
 }

 public void paint( Graphics g )
 {
  double xScale, yScale;

  super.paint( g );

  Dimension windowSize = this.getSize();
  Graphics2D g2d = (Graphics2D)g;
// Antialias text. Is fractional metrics necessary?
  g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                       RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                       RenderingHints.VALUE_FRACTIONALMETRICS_ON);
// Set current scale from original base setting
  xScale = windowSize.width / baseWidth;
  yScale = windowSize.height / baseHeight;
  g2d.scale( xScale, yScale );
// Set font
  g2d.setFont( labelFont );
  g2d.drawString( "A quick Brown Fox jumped over the lazy Dog.", 75, 800
);
// Rotate -90 degrees (Y axis label)
  g2d.rotate( Math.toRadians( 270 ) );
  g2d.drawString( "A quick Brown Fox jumped over the lazy Dog.", -800,
75 );
 }
}
Technical Details
Message-ID: <3E690FDF.AAF8C3B@fnal.gov>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!news-stoc.telia.net!news-stoa.telia.net!telia.net!newsfeed.rt.ru!news.donbass.net!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,f0d101cc3d440f31,start
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2003-03-07 13:33:13 PST
Message-ID: <3E690FDF.AAF8C3B@fnal.gov>
Date: Fri, 07 Mar 2003 15:32:15 -0600
From: Alex Waller <waller@fnal.gov>
Organization: Fermilab
X-Mailer: Mozilla 4.77 [en] (Windows NT 5.0; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: Trouble scaling rotated SansSerif Font in JDK 1.4.1
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 131.225.130.145
X-Trace: newsgroups.borland.com 1047072734 131.225.130.145 (7 Mar 2003 13:32:14 -0800)
Lines: 199
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!news-stoc.telia.net!news-stoa.telia.net!telia.net!newsfeed.rt.ru!news.donbass.net!newsgroups.borland.com!not-for-mail