4

I have a piece of java code which draws text on a buffer and saves it as monochrome BMP. I executed the program on Windows 7 and CentOS 6.3 .

Font used is arial. The image generated in Windows is crisp, characters are uniformly rendered. Why on Cent OS the characters are thin, looks like platform misses to render some pixels.

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class TimesB extends Canvas {
    private Image img;

    public TimesB() {
    setBackground(Color.white);
    }

      public static void main(String s[]) throws IOException {
        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
            public void windowClosed(WindowEvent e) {System.exit(0);}
        };


        int width = 400, height = 300;

        // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
        // into integer pixels
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);

        Graphics2D ig2 = bi.createGraphics();
        ig2.setBackground(Color.WHITE);




        Font font = new Font("Arial", Font.PLAIN, 18);
        ig2.setFont(font);


        FontRenderContext frc = ig2.getFontRenderContext();



        System.out.println("Transform Type: " + frc.getTransformType());

        System.out.println("Transform: " + frc.getTransform());

        System.out.println("Anti- Aliasing: " + frc.getAntiAliasingHint());

        String message = "Cantaloupemelone weissfleischig hell, mit Kernen!";

        FontMetrics fontMetrics = ig2.getFontMetrics();
        int stringWidth = fontMetrics.stringWidth(message);
        int stringHeight = fontMetrics.getAscent();
     //   ig2.setPaint(Color.black);

        ig2.drawString(message, 10,30);

        message = "Imazalil, Theiabendazol und orthophenylphenol";
        ig2.drawString(message, 10,60);

        message = "gleichmabig feucht halten, Staunsse vermiden";
        ig2.drawString(message, 10,90);


        WritableRaster raster = bi.getRaster();
             // Put the pixels on the raster. Note that only values 0 and 1 are used for the pixels.
             // You could even use other values: in this type of image, even values are black and odd
             // values are white.
             for(int h=0;h<height;h++)
               for(int w=0;w<width;w++){
                  int iVal =  raster.getSample(w, h, 0);

                  if(iVal == 0) raster.setSample(w,h,0,1);
                  else
                      raster.setSample(w,h,0,0);

               }




       ImageIO.write(bi, "BMP", new File("D:\\project\\Java\\yourImageName_w.BMP"));



    }
}

This is not only with java, even freetype library used with C++ produces the same output.

Is it because of the underlying graphics layer ? how this can be fixed ? How to make the Linux font rendering as good as windows ?

**I could not attach the output as I need 10 reputation !!

2
  • edit wit output link I would insert it.
    – UmNyobe
    Feb 20, 2015 at 11:45
  • Two things to check. Is there any anti-aliasing technic enabled on one or both systems? Is the used font also installed on CentOS?
    – SubOptimal
    Feb 20, 2015 at 11:54

2 Answers 2

6

The pixel density of traditional 96dpi screens is too low to draw complex small shapes such as letters accurately (it's worse for CJK glyphs).

Thus, drawing text on such screens is a compromise :

  • do you want high contrast text at the cost of glypĥ distortion and butchering of the glyph design?

  • do you accept some color fringing, that will help smoothing shapes (subpixel positioning)?

  • do you prefer your text runs to be perfectly balanced, for one font size, at the cost of non linear re-sizing when the font size is changed?

  • do you want a text engine that will handle any font, or a text engine heavily dependent on special rules only present in some fonts?

  • do you aggressively optimize for a particular pixel density, at the cost of degraded rendering on any screen with differing density, and inability to transition to other hardware?

  • do you optimize per-glyph contrast (moving stems towards pixel lines) or try to preserve stem positioning (good for flowed text and any glyph combination that forms ligatures)?

There is no "good" answer to any of those questions (and there are many others), there are only compromises, and different systems make different compromises.

To "run everywhere" SUN used to include its own proprietary font renderer in the JVM, though recent Java runtimes tend to use the system one.

Arial is a special case due to its long history of Microsoft compromises. It is designed to workaround Windows text rendering problems, and Windows text rendering is designed to hide Arial design problems.

Windows has historically been heavily biased towards "high contrast, high distortion, non-linear resizing, special rules, 96dpi only" choices. Thus Windows users tend to find any non-Windows font rendering blurry. While non Windows users tend to find Windows text rendering distorted and ugly.

OSX made different choices. Apple had high market share in design communities, so making computer text shapes closer to what they are on paper (high-density medium, no shape distortion), was more important that higher contrast (but lower shape fidelity). And design communities used lots of varied fonts, Apple could not special case a handful of fonts like Microsoft. That's one reason highdpi was easy for Apple (comparatively).

Linux made yet others. Mainly to make the best of any font, since it could not commission a specially tuned set like Apple or Microsoft. (and Linux has loads of tunables to make it like Windows, like OSX, or like something else again). And because it has loads of tunables, the text stack expects the application to set them (usually inherited from the GUI environment). Which is a problem when you use freetype in headless mode (you need to set the tunables in your code), or when you use Java (unless I'm wrong the JVM still does not set those tunables, it depends on the DE to set them for it, or on explicit jvm flags).

Android inherited from Linux, that's why it can change system font from version to version without particular problems. And the high pixel density of smartphone screens helps.

But polls repeatedly show that any computer user will be most comfortable with the text rendering of his usual system, will prefer the font of his usual system, and will hate other font and other kinds of text rendering (and force him to repeatedly use another compromise for some months and the preferences will change). No current font rendering is especially better or worse than others. What matters for comfort is the speed at which the brain decodes shapes. The brain self-trains with the text it sees day after day.

Thus, Apple software that used Apple text rendering on Windows met strong rejection, as did Windows emulated software on Linux, etc.

The "good" way to render text is to let your users' system render it the way they are used to. The "bad" way is to try to reproduce your habits.

It should change with high-dpi screens, since they promise to make the pixel grid-fitting at the root of text distortions and compromises go away. But people will still hate exposure to fonts they are not used to (except in small doses, for headers or other limited effects). The most probable long term-effect will be a revival of on-screen serif fonts. Serif fonts are terrible on low density screens, but people seem to prefer them on high-density media such as printed books.

When pre-rendering text on a bitmap, you can also avoid most system rendering artifacts by increasing size, then shrinking bitmaps. A lot of the system text rendering magic is tuned to the local screen and does not translate as-is to another screen anyway.

0

I use the fontconfig-infinality to have a better font rendering with Linux. This config also improve the browsers fonts rendering.

sudo add-apt-repository ppa:no1wantdthisname/ppa sudo apt-get update sudo apt-get upgrade sudo apt-get install fontconfig-infinality

You have more info here:

http://www.webupd8.org/2013/06/better-font-rendering-in-linux-with.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.