From 15622a095c6c4b9241400587d349f30fcb105040 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Sun, 2 Jul 2017 01:40:25 +0200 Subject: [PATCH] Added image generator to create file. --- {src/resources => resources}/tngan.ttf | Bin {src/resources => resources}/tnganb.ttf | Bin {src/resources => resources}/tnganbi.ttf | Bin {src/resources => resources}/tngani.ttf | Bin src/net/agspace/TengwarImageGenerator.java | 50 ++++++++++++--------- 5 files changed, 30 insertions(+), 20 deletions(-) rename {src/resources => resources}/tngan.ttf (100%) rename {src/resources => resources}/tnganb.ttf (100%) rename {src/resources => resources}/tnganbi.ttf (100%) rename {src/resources => resources}/tngani.ttf (100%) diff --git a/src/resources/tngan.ttf b/resources/tngan.ttf similarity index 100% rename from src/resources/tngan.ttf rename to resources/tngan.ttf diff --git a/src/resources/tnganb.ttf b/resources/tnganb.ttf similarity index 100% rename from src/resources/tnganb.ttf rename to resources/tnganb.ttf diff --git a/src/resources/tnganbi.ttf b/resources/tnganbi.ttf similarity index 100% rename from src/resources/tnganbi.ttf rename to resources/tnganbi.ttf diff --git a/src/resources/tngani.ttf b/resources/tngani.ttf similarity index 100% rename from src/resources/tngani.ttf rename to resources/tngani.ttf diff --git a/src/net/agspace/TengwarImageGenerator.java b/src/net/agspace/TengwarImageGenerator.java index c7a398b..edc4bc9 100644 --- a/src/net/agspace/TengwarImageGenerator.java +++ b/src/net/agspace/TengwarImageGenerator.java @@ -2,7 +2,6 @@ package net.agspace; import javax.imageio.ImageIO; import java.awt.*; -import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -23,47 +22,58 @@ public class TengwarImageGenerator { * @param italic Whether or not the font should be italic. * @return An Image Object containing the drawn text. */ - public static Image generateImage(String tengwarText, int maxWidth, int fontSize, boolean bold, boolean italic) { - BufferedImage bufferedImage = new BufferedImage(maxWidth, 1, BufferedImage.TYPE_INT_RGB); - Graphics2D graphics = (Graphics2D)bufferedImage.getGraphics(); - Color backgroundColor = new Color(54, 57, 62); - Font tengwarFont = getTengwarFont(bold, italic); - tengwarFont = tengwarFont.deriveFont(fontSize); + public static File generateImage(String tengwarText, int maxWidth, float fontSize, boolean bold, boolean italic, String filepath) { + BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = bufferedImage.createGraphics(); + Font tengwarFont = getTengwarFont(bold, italic, fontSize); graphics.setFont(tengwarFont); FontMetrics fm = graphics.getFontMetrics(); - Rectangle2D rect = fm.getStringBounds(tengwarText, graphics); - graphics.setColor(backgroundColor); - graphics.fillRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight()); + int width = fm.stringWidth(tengwarText); + int height = fm.getHeight(); + graphics.dispose(); + + bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + graphics = bufferedImage.createGraphics(); + graphics.setBackground(new Color(54, 57, 62)); + graphics.clearRect(0, 0, width, height); + graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + graphics.setColor(Color.white); - graphics.drawString(tengwarText, 0, fm.getHeight()); + graphics.setFont(tengwarFont); + graphics.drawString(tengwarText, 0, fm.getAscent()); + graphics.dispose(); + try { - ImageIO.write(bufferedImage, "png", new File("C:\\Users\\AndrewComputer\\Documents\\Programming\\IntelliJ_Projects")); + File file = new File(filepath); + ImageIO.write(bufferedImage, "png", file); + return file; } catch (IOException e) { e.printStackTrace(); } - return (Image)bufferedImage; + return null; } /** * Returns the tngan.ttf font loaded into a Java Font. * @param bold Whether or not font should be bold. * @param italic Whether or not font should be italic. + * @param size The font size, in pt. * @return The Font loaded, or null. */ - public static Font getTengwarFont(boolean bold, boolean italic){ + public static Font getTengwarFont(boolean bold, boolean italic, float size){ Font font = null; try { - String filename = null; + String filename; if (bold && italic){ - filename = "resources/tnganbi.ttf"; + filename = "tnganbi.ttf"; } else if (bold){ - filename = "resources/tnganb.ttf"; + filename = "tnganb.ttf"; } else if (italic){ - filename = "resources/tngani.ttf"; + filename = "tngani.ttf"; } else { - filename = "resources/tngan.tff"; + filename = "tngan.ttf"; } - font = Font.createFont(Font.TRUETYPE_FONT, TengwarImageGenerator.class.getClassLoader().getResourceAsStream(filename)); + font = Font.createFont(Font.TRUETYPE_FONT, TengwarImageGenerator.class.getClassLoader().getResourceAsStream(filename)).deriveFont(size); } catch (FontFormatException | IOException e) { e.printStackTrace(); }