Created
December 26, 2012 13:17
-
-
Save philipsorst/4380338 to your computer and use it in GitHub Desktop.
Java Language Fontrendering Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.Font; | |
import java.awt.Graphics2D; | |
import java.awt.font.FontRenderContext; | |
import java.awt.geom.Rectangle2D; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import javax.imageio.ImageIO; | |
/** | |
* Simple class to test the rendering of symbols in various writing systems. Public domain. | |
* @author Philip W. Sorst | |
*/ | |
public class LanguageFontRenderingTest { | |
private final String fontName = "SansSerif"; | |
private final int fontSize = 50; | |
private final Font plainFont = new Font(this.fontName, Font.PLAIN, this.fontSize); | |
private final Font boldFont = new Font(this.fontName, Font.BOLD, this.fontSize); | |
private final int margin = 10; | |
private double widthFirstCol = 0; | |
private double widthSecondCol = 0; | |
private double widthThirdCol = 0; | |
private final Map<String, String> languageMap; | |
public static void main(final String[] args) throws IOException { | |
final File out = new File(args[0]); | |
final LanguageFontRenderingTest lft = new LanguageFontRenderingTest(); | |
lft.run(out); | |
} | |
public LanguageFontRenderingTest() { | |
this.languageMap = this.initLanguageMap(); | |
} | |
private void run(final File out) throws IOException { | |
final Rectangle2D bounds = this.determineDimensions(); | |
this.render(bounds, out); | |
} | |
private void render(final Rectangle2D bounds, final File out) throws IOException { | |
final BufferedImage bi = new BufferedImage( | |
(int) Math.ceil(bounds.getWidth()), | |
(int) Math.ceil(bounds.getHeight()), | |
BufferedImage.TYPE_INT_RGB | |
); | |
final Graphics2D g2d = bi.createGraphics(); | |
g2d.setColor(Color.WHITE); | |
g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight()); | |
double y = 0; | |
for (final Entry<String, String> langEntry : this.languageMap.entrySet()) { | |
final String language = langEntry.getKey(); | |
final String lowerCase = langEntry.getValue(); | |
final String upperCase = lowerCase.toUpperCase(); | |
final FontRenderContext frc = g2d.getFontRenderContext(); | |
final Rectangle2D langBounds = this.plainFont.getStringBounds(language, frc); | |
final Rectangle2D plainBounds = this.plainFont.getStringBounds(lowerCase + " " + upperCase, frc); | |
final Rectangle2D boldBounds = this.boldFont.getStringBounds(upperCase + " " + upperCase, frc); | |
g2d.setFont(this.plainFont); | |
g2d.setColor(Color.GRAY); | |
g2d.drawString( | |
language, | |
(int) Math.round(this.widthFirstCol - langBounds.getWidth() - langBounds.getX()), | |
(int) Math.round(y - langBounds.getY()) | |
); | |
g2d.setColor(Color.BLACK); | |
g2d.drawString( | |
lowerCase + " " + upperCase, | |
(int) Math.round(this.widthFirstCol + this.margin - plainBounds.getX()), | |
(int) Math.round(y - langBounds.getY()) | |
); | |
g2d.setFont(this.boldFont); | |
g2d.drawString( | |
lowerCase + " " + upperCase, | |
(int) Math.round(this.widthFirstCol + this.widthSecondCol + 2 * this.margin - boldBounds.getX()), | |
(int) Math.round(y - langBounds.getY()) | |
); | |
y += this.fontSize + this.margin; | |
} | |
g2d.dispose(); | |
ImageIO.write(bi, "png", out); | |
} | |
private Rectangle2D determineDimensions() { | |
final BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); | |
final Graphics2D g2d = bi.createGraphics(); | |
double h = 0; | |
for (final Entry<String, String> langEntry : this.languageMap.entrySet()) { | |
final String language = langEntry.getKey(); | |
final String lowerCase = langEntry.getValue(); | |
final String upperCase = lowerCase.toUpperCase(); | |
final FontRenderContext frc = g2d.getFontRenderContext(); | |
final Rectangle2D langBounds = this.plainFont.getStringBounds(language, frc); | |
final Rectangle2D plainBounds = this.plainFont.getStringBounds(lowerCase + " " + upperCase, frc); | |
final Rectangle2D boldBounds = this.boldFont.getStringBounds(lowerCase + " " + upperCase, frc); | |
this.widthFirstCol = Math.max(langBounds.getWidth(), this.widthFirstCol); | |
this.widthSecondCol = Math.max(plainBounds.getWidth(), this.widthSecondCol); | |
this.widthThirdCol = Math.max(boldBounds.getWidth(), this.widthThirdCol); | |
h += this.fontSize + this.margin; | |
} | |
g2d.dispose(); | |
return new Rectangle2D.Double( | |
0, 0, | |
this.widthFirstCol + this.widthSecondCol + this.widthThirdCol + 2 * this.margin, | |
h | |
); | |
} | |
private Map<String, String> initLanguageMap() { | |
final Map<String, String> languageMap = new HashMap<String, String>(); | |
languageMap.put("Latin", "testâóéäöäß"); | |
languageMap.put("AM", "አማርኛ"); | |
languageMap.put("BU", "Башҡортса"); | |
languageMap.put("BE", "Беларуская"); | |
languageMap.put("BG", "Български"); | |
languageMap.put("CV", "Чӑвашла"); | |
languageMap.put("EL", "Ελληνικά"); | |
languageMap.put("FA", "ﻑﺍﺮﺳی"); | |
languageMap.put("HE", "עברית"); | |
languageMap.put("HI", "हिन्दी"); | |
languageMap.put("JA", "日本語"); | |
languageMap.put("KN", "ಕನ್ನಡ"); | |
languageMap.put("KO", "한국어"); | |
languageMap.put("KRC", "Къарачай-Малкъар"); | |
languageMap.put("MK", "Македонски"); | |
languageMap.put("ML", "മലയാളം"); | |
languageMap.put("NE", "नेपाली"); | |
languageMap.put("PNB", "پﻦﺟﺎﺑی"); | |
languageMap.put("RU", "Русский"); | |
languageMap.put("SA", "संस्कृत"); | |
languageMap.put("SI", "සිංහල"); | |
languageMap.put("TA", "தமிழ்"); | |
languageMap.put("TG", "Тоҷикӣ"); | |
languageMap.put("TH", "ไทย"); | |
languageMap.put("ZH", "中文"); | |
languageMap.put("ZH-YUE", "粵語"); | |
return languageMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment