I am writing the same Attributed String using TextLayout.draw() on an Applet as well as on a Frame

In TextLayout, incorrect spacing between Words/Characters are observed when oblique(Italic/Bold Italic) fonts are used on an Applet, while the same Attributed String is rendered correctly on a Frame

Below is a stand alone code to demonstrate the difference between writing an AttributedString using java.awt.font.TextLayout on an Applet as well on a Frame

Below code is for demonstrating TextLayout on a Frame. This works Fine.

import java.awt.BorderLayout;  
import java.awt.Component;  
import java.awt.Font;  
import java.awt.Frame;  
import java.awt.Graphics;  
import java.awt.Graphics2D;  
import java.awt.event.ComponentEvent;  
import java.awt.font.FontRenderContext;  
import java.awt.font.TextAttribute;  
import java.awt.font.TextLayout;  


import java.text.AttributedString;  


import javax.swing.JPanel;  


public class GraphViewer extends Frame  
{  
 public static void main(String[] args)  
  {  
      Component viewer = new GraphViewer(new test());  
      viewer.show();  

  }  

 public GraphViewer( Component component)  
 {  
     setLayout(new BorderLayout(0,0));  
     setVisible(true);  
     setSize(800,600);  
     setTitle("Viewer");  
     add("Center", component);  
     addWindowListener(new SymWindow());  
 }  

 public void paint(Graphics2D graphics)  
 {  
     graphics.clearRect(0,0,800, 600);  
     super.paint(graphics);  
 }  

 public class SymComponent extends java.awt.event.ComponentAdapter  
 {  
     public void ComponentResized(ComponentEvent event)  
     {  
         Object object = event.getSource();  
         if(object == GraphViewer.this)  
         {  

         }  
     }  

 }  


 public class SymWindow extends java.awt.event.WindowAdapter  
 {  


     public void windowClosing(java.awt.event.WindowEvent event)  
     {  
         Object object = event.getSource();  
         if (object == GraphViewer.this)  
             FrameApp_WindowClosing(event);  
     }  


     void FrameApp_WindowClosing(java.awt.event.WindowEvent event)  
     {  


         setVisible(false); // hide the Frame  
         dispose(); // free the system resources  
         System.exit(0); // close the application  
     }  
 }  

}  
class test extends JPanel  
{  

 public Font getFont()  
 {  
     return new Font("Arial", Font.BOLD, 15);  
 }  


 synchronized public void paintComponent(Graphics g)  
 {  
     test1(g);  
 }  

 void test1(Graphics g)  
 {  
     Graphics2D g2 = (Graphics2D)g;  
     FontRenderContext frc = g2.getFontRenderContext();  
     java.awt.Font graphicsFont = new java.awt.Font("Arial", Font.PLAIN, 15);  
     AttributedString result = new AttributedString ("This is a test to check partial text formatting.");  
     graphicsFont  = graphicsFont.deriveFont(java.awt.Font.ITALIC);  

     result.addAttribute(TextAttribute.FONT, graphicsFont, 1, 10);  
     result.addAttribute(TextAttribute.FONT, graphicsFont, 15, 22);  
     result.addAttribute(TextAttribute.FONT, graphicsFont, 25, 31);  
     result.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);  

     TextLayout layout = new TextLayout(result.getIterator(),g2.getFontRenderContext());  
     layout.draw(g2, 100,100);  
 }        

}

Below is the code to demonstrate TextLayout on Applet. This gives more space after italic charaters. Is this a bug in TextLayout?

import java.awt.Color;  
import java.awt.Font;  
import java.awt.Graphics;  
import java.awt.Graphics2D;  
import java.awt.font.TextAttribute;  
import java.awt.font.TextLayout;  


import java.text.AttributedString;  


import java.util.StringTokenizer;  


import javax.swing.JApplet;  




public class TestTextLayoutApplet extends JApplet {  
public TestTextLayoutApplet() {  
    super();  
}  


public void init(){  
    System.out.println("In TestTextLayout Init");  
}  


public void paint(Graphics g) {  
    Graphics2D g2 = (Graphics2D)g;  

    java.awt.Font graphicsFont = new java.awt.Font("Arial", Font.BOLD, 15);  
    g2.setFont(graphicsFont);  


    AttributedString attStr = new AttributedString("This is a test to check partial text formatting.");  
    attStr.addAttribute(TextAttribute.FONT, g2.getFont());  

    graphicsFont = graphicsFont.deriveFont(java.awt.Font.ITALIC);  

    attStr.addAttribute(TextAttribute.FONT, graphicsFont, 1, 10);  
    attStr.addAttribute(TextAttribute.FONT, graphicsFont, 15, 22);  
    attStr.addAttribute(TextAttribute.FONT, graphicsFont, 25, 31);  
    attStr.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);  


    /** Writing with TextLayout */  
    TextLayout textLayout = new TextLayout(attStr.getIterator(), g2.getFontRenderContext());  
    g2.setColor(Color.BLUE);  
    textLayout.draw(g2, 20, 100);  

    g2.dispose();  
}  


}  
1

There are 1 answers

0
Darren On

I know this is an old post but nobody ever answered the question and I know the answer. The answer is yes, there definitely was such a bug with padding being incorrectly added to italic TextLines (inside the TextLayout). I know the bug was still around as of 1.8 at least but it was fixed at some point. I don't know what version, I couldn't find the bug report online, but certainly by Java 17.

It didn't seem to affect fonts where the italicization was being simulated by Java, but it certainly occurred with true italic faces such as Arial Italic for example.