0

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();  
}  


}  
HarshaSK
  • 39
  • 1
  • 6
  • 1
    Don't dispose of a `Graphics` context you didn't create, that `Graphics` context is used to paint other aspects of your component as well as other components on the screen – MadProgrammer Nov 19 '14 at 04:33
  • Unless you're doing something screwy, `paintComponent` doesn't need to synchronised (nor should it be `public`) – MadProgrammer Nov 19 '14 at 04:38
  • The first thing I did was to remove `java.awt.Font graphicsFont = new java.awt.Font("Arial", Font.BOLD, 15);` and rely on the `Font` that was supplied by the `Graphics` context, as this should be the components `Font`. Next, I removed the reference to `Arial` in the `GraphViewer` and found the resulting rendering to be just about the same, so the issue has to do with the font, probably a variable width font... – MadProgrammer Nov 19 '14 at 04:42
  • Next I did something like `setFont(new Font("Arial", Font.PLAIN, 24));` within the applets and panels constructors and got the same result, for each, but yes, the spacing between characters is different. Again, I assume that this is the difference between fixed and variable width fonts... – MadProgrammer Nov 19 '14 at 04:49
  • @MadProgrammer Thanks a lot for the quick response. What do u think? is this extra space a bug in text layout? Is there a way to resolve this? – HarshaSK Nov 20 '14 at 02:42
  • I don't think it's a bug, more an assumption on the part of the developers, they deliberately focused on fixed width fonts – MadProgrammer Nov 20 '14 at 04:19

1 Answers1

0

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.

Darren
  • 1
  • 1