Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2005

Tip: Looking for answers? Try searching our database.

TextLayout and super-/subscript

Thread view: 
Christian Kaufhold - 12 Jul 2005 20:15 GMT
Super-/subscripts finally work in 1.5. But they look horrible - it seems
as if the vertical shift is done twice - once by the Transform in Font,
and then again in sun.font.TextLine (CoreMetrics.ssOffset). Or is it just
too large and this double shift is intentional?

Compare: http://www.chka.de/tmp/standard.png
        http://www.chka.de/tmp/hacked.png

where the latter was done with a modified TextLine.
I think anyone will agree that the second one looks better.

Any comments?

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;

import java.text.*;

import java.util.List;
import java.util.*;

import javax.swing.*;

// this is no code of high quality!
public class SuperTest
{
   // for simplicity
   private static FontRenderContext context = new FontRenderContext(new AffineTransform(), true, true);

   public static void main(String[] args)
   {
    Font plain = new Font("Serif", Font.PLAIN, 100);

    AttributedString s = new AttributedString("ABCDEFGH");

       s.addAttribute(TextAttribute.FONT, plain);
    s.addAttribute(TextAttribute.FONT, plain.deriveFont(java.util.Collections.singletonMap(TextAttribute.SUPERSCRIPT, new Integer(1))), 1, 2);
    s.addAttribute(TextAttribute.FONT, plain.deriveFont(java.util.Collections.singletonMap(TextAttribute.SUPERSCRIPT, new Integer(-1))), 6, 7);

    TextLayout m = new TextLayout(s.getIterator(), context);
   
       JFrame f = new JFrame("TextLayout");

       f.getContentPane().add(new TLComponent(Arrays.asList(new Object[] { m })));
       
       f.pack(); f.show();
   }

   protected static class TLComponent
       extends JComponent
   {
    private static final int INSETS = 50;

       private List textLayouts;

       public TLComponent(List l)
       {
           textLayouts = l;
       }

       
       public Dimension getPreferredSize()
       {
           Dimension result = new Dimension();

           for (Iterator i = textLayouts.iterator(); i.hasNext();)
           {
               TextLayout l = (TextLayout)i.next();

               result.width = Math.max(result.width, (int)Math.ceil(l.getAdvance()));

               result.height += (int)Math.ceil(l.getAscent() + l.getDescent() + 2 * l.getLeading());
           }

           result.width += 2 * INSETS;
           result.height += 2 * INSETS;

           return result;
       }

       protected void paintComponent(Graphics g)
       {
       Graphics2D h = (Graphics2D)g;

       h.setColor(Color.white);

       h.fillRect(0, 0, getWidth(), getHeight());

       h.setColor(Color.black);

           int y = INSETS;
           int x = INSETS;

       if (context.isAntiAliased())
       {
        h.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        h.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       }

       if (context.usesFractionalMetrics())
       {
        h.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
       }

           for (Iterator i = textLayouts.iterator(); i.hasNext();)
           {
               TextLayout l = (TextLayout)i.next();

               l.draw(h, x, y + l.getAscent());

               y += Math.ceil(l.getAscent() + l.getDescent() + 2 * l.getLeading());
           }
       }
   }

}

Christian
Signature

And in short, I was afraid.

Thomas Weidenfeller - 13 Jul 2005 10:25 GMT
> Super-/subscripts finally work in 1.5.

Fore some value of "working". The expected obvious way still doesn't
work for me (1.5.0_02):

    s.addAttribute(TextAttribute.SUPERSCRIPT,                            
TextAttribute.SUPERSCRIPT_SUPER, 1, 2);

and

    s.addAttributes(Collections.singletonMap(
                TextAttribute.SUPERSCRIPT,
                            TextAttribute.SUPERSCRIPT_SUPER),
                1, 2);
are both simply ignored.

> But they look horrible

Yes, they do. Unbelievable ugly.

> - it seems
> as if the vertical shift is done twice - once by the Transform in Font,
> and then again in sun.font.TextLine (CoreMetrics.ssOffset). Or is it just
> too large and this double shift is intentional?

I don't think it is intentional. It is just that with your workaround
you bump from one faulty Sun code into the next fault Sun code. I think
the stuff is still as broken as it always was. 1.5 just brought a few
new and improved bugs.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Christian Kaufhold - 15 Jul 2005 22:23 GMT
>> Super-/subscripts finally work in 1.5.
>
[quoted text clipped - 11 lines]
>                                1, 2);
> are both simply ignored.

This is because Font-related attributes are ignored if there is an explicit
Font (as opposed to separate Family and Size) set.

Christian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.