Skip to main content

anti-alias fonts

5 replies [Last post]
david@windward.net
Offline
Joined: 2006-02-14
Points: 0

Hi;

Another off-topic question. But I have asked in the Java2D forums here and at Sun with no answers and I am getting desperate.

I create my graphics object as follows:
BufferedImage img = new BufferedImage(devExtX, devExtY, BufferedImage.TYPE_INT_ARGB_PRE);
graphic = (Graphics2D) img.getGraphics();
graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphic.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphic.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

But when I draw text on the bitmap, it is not anti-aliased. So I get the real ugly blocky text. Same problem with diaganol lines.

What am I missing?

thanks - dave

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
david@windward.net
Offline
Joined: 2006-02-14
Points: 0

Found it - code below. You cannot use TextLayout.draw(). (The posts here caused me to go back and try to find something that was different in my code.)

code:

package net.windward.env.image.test;

import com.sun.imageio.plugins.png.PNGMetadata;

import javax.imageio.IIOImage;
import javax.imageio.ImageWriter;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import java.io.FileOutputStream;
import java.io.File;
import java.text.AttributedString;

/**
* Show no font anti-alias
*/
public class FontProblem {

public static void main(String[] args) throws Exception {

BufferedImage img = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D graphic = (Graphics2D) img.getGraphics();
graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphic.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphic.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

// white background
graphic.setColor(new Color(255, 255, 255));
graphic.fillRect(0, 0, 200, 200);

// write text
graphic.setColor(new Color(255, 0, 255));
Font font = new Font("Arial", 0, 20);
graphic.setFont(font);
graphic.drawString("Hi there", 20, 20);

// write more
AttributedString str = new AttributedString("another string");
str.addAttribute(TextAttribute.FONT, font);
str.addAttribute(TextAttribute.FOREGROUND, new Color(0, 0, 255));
TextLayout lay = new TextLayout(str.getIterator(), new FontRenderContext(null, false, true));
lay.draw(graphic, 80, 80);

// diaganol line
graphic.setColor(new Color(255, 0, 0));
graphic.setStroke(new BasicStroke(5));
graphic.drawLine(40, 40, 180, 180);

// save it
PNGMetadata png = new PNGMetadata();
int resX = (int)(72 / 0.0254f);
png.pHYs_pixelsPerUnitXAxis = resX;
png.pHYs_pixelsPerUnitYAxis = resX;
png.pHYs_unitSpecifier = 1;
png.pHYs_present = true;

IIOImage iioImage = new IIOImage(img, null, png);

// Save as PNG
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName("png").next();
FileOutputStream out = new FileOutputStream("font_prob.png");
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);

writer.write(iioImage);
writer.dispose();
out.close();
System.out.println("all done: " + new File("font_prob.png").getCanonicalPath());
}
}

Robert Engels

You can if you set up a proper FontRenderContext, usually get it from the BufferedImage's graphics context.

-----Original Message-----
From: jai-interest@javadesktop.org [mailto:jai-interest@javadesktop.org]
Sent: Thursday, November 17, 2005 3:05 PM
To: interest@jai.dev.java.net
Subject: [JAI] Re: anti-alias fonts

Found it - code below. You cannot use TextLayout.draw(). (The posts here caused me to go back and try to find something that was different in my code.)

code:

package net.windward.env.image.test;

import com.sun.imageio.plugins.png.PNGMetadata;

import javax.imageio.IIOImage;
import javax.imageio.ImageWriter;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import java.io.FileOutputStream;
import java.io.File;
import java.text.AttributedString;

/**
* Show no font anti-alias
*/
public class FontProblem {

public static void main(String[] args) throws Exception {

BufferedImage img = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D graphic = (Graphics2D) img.getGraphics();
graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphic.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphic.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

// white background
graphic.setColor(new Color(255, 255, 255));
graphic.fillRect(0, 0, 200, 200);

// write text
graphic.setColor(new Color(255, 0, 255));
Font font = new Font("Arial", 0, 20);
graphic.setFont(font);
graphic.drawString("Hi there", 20, 20);

// write more
AttributedString str = new AttributedString("another string");
str.addAttribute(TextAttribute.FONT, font);
str.addAttribute(TextAttribute.FOREGROUND, new Color(0, 0, 255));
TextLayout lay = new TextLayout(str.getIterator(), new FontRenderContext(null, false, true));
lay.draw(graphic, 80, 80);

// diaganol line
graphic.setColor(new Color(255, 0, 0));
graphic.setStroke(new BasicStroke(5));
graphic.drawLine(40, 40, 180, 180);

// save it
PNGMetadata png = new PNGMetadata();
int resX = (int)(72 / 0.0254f);
png.pHYs_pixelsPerUnitXAxis = resX;
png.pHYs_pixelsPerUnitYAxis = resX;
png.pHYs_unitSpecifier = 1;
png.pHYs_present = true;

IIOImage iioImage = new IIOImage(img, null, png);

// Save as PNG
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName("png").next();
FileOutputStream out = new FileOutputStream("font_prob.png");
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);

writer.write(iioImage);
writer.dispose();
out.close();
System.out.println("all done: " + new File("font_prob.png").getCanonicalPath());
}
}
---
[Message sent by forum member 'david@windward.net' (David Thielen)]

http://www.javadesktop.org/forums/thread.jspa?messageID=125727&#125727

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

david@windward.net
Offline
Joined: 2006-02-14
Points: 0

That's it - thank you very much

loki der quaeler

when you say bitmap, you wouldn't happen to be doing it in a min-is-
white space, would you?

if so, this was a bug under 1.4.x which was supposed to be fixed for
1.5.x -- search the old forum for Klaus Bartz's _Truly_ outstanding
diagnosis email -- subject includes "min-is-white", author includes
"klaus" -- date: 4 july 2004 -- or actually, here's a link hopefully:
http://archives.java.sun.com/cgi-bin/wa?A2=ind0407&L=jai-
interest&P=R44776&D=0&I=-3

loki

On 17 Nov, 2005, at 17.56, jai-interest@javadesktop.org wrote:

> Hi;
>
> Another off-topic question. But I have asked in the Java2D forums
> here and at Sun with no answers and I am getting desperate.
>
> I create my graphics object as follows:
> BufferedImage img = new BufferedImage(devExtX, devExtY,
> BufferedImage.TYPE_INT_ARGB_PRE);
> graphic = (Graphics2D) img.getGraphics();
> graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
> RenderingHints.VALUE_ANTIALIAS_ON);
> graphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
> RenderingHints.VALUE_FRACTIONALMETRICS_ON);
> graphic.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
> RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
> graphic.setRenderingHint(RenderingHints.KEY_RENDERING,
> RenderingHints.VALUE_RENDER_QUALITY);
>
> But when I draw text on the bitmap, it is [b]not[/b] anti-aliased.
> So I get the real ugly blocky text. Same problem with diaganol lines.
>
> What am I missing?
>
> thanks - dave
> ---
> [Message sent by forum member 'david@windward.net' (David Thielen)]
>
> http://www.javadesktop.org/forums/thread.jspa?messageID=125624&#125624
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

Robert Engels

That should be all that is required. Please post a complete testcase so others can run/test.

-----Original Message-----
From: jai-interest@javadesktop.org [mailto:jai-interest@javadesktop.org]
Sent: Thursday, November 17, 2005 10:57 AM
To: interest@jai.dev.java.net
Subject: [JAI] anti-alias fonts

Hi;

Another off-topic question. But I have asked in the Java2D forums here and at Sun with no answers and I am getting desperate.

I create my graphics object as follows:
BufferedImage img = new BufferedImage(devExtX, devExtY, BufferedImage.TYPE_INT_ARGB_PRE);
graphic = (Graphics2D) img.getGraphics();
graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphic.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphic.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

But when I draw text on the bitmap, it is [b]not[/b] anti-aliased. So I get the real ugly blocky text. Same problem with diaganol lines.

What am I missing?

thanks - dave
---
[Message sent by forum member 'david@windward.net' (David Thielen)]

http://www.javadesktop.org/forums/thread.jspa?messageID=125624&#125624

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net