| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.graphics; |
| 21 |
|
| 22 |
import java.awt.Font; |
| 23 |
import java.awt.FontMetrics; |
| 24 |
import java.awt.Graphics2D; |
| •25 |
import java.awt.GraphicsEnvironment; |
| 26 |
import java.awt.Rectangle; |
| 27 |
import java.awt.font.FontRenderContext; |
| 28 |
import java.awt.geom.Rectangle2D; |
| 29 |
import java.awt.image.BufferedImage; |
| 30 |
import java.util.Locale; |
| 31 |
|
| •32 |
import javax.swing.JOptionPane; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class FontMeasurer { |
| 39 |
|
| 40 |
private static final String fontFamilyName = "Dialog"; |
| 41 |
|
| 42 |
|
| 43 |
private static final boolean DEBUG = false; |
| 44 |
|
| 45 |
private static final FontMeasurer instance = new FontMeasurer(); |
| 46 |
FontRenderContext fakeRenderContext; |
| 47 |
Graphics2D fakeGraphics; |
| 48 |
|
| 49 |
{ |
| 50 |
BufferedImage image = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB); |
| 51 |
fakeGraphics = image.createGraphics(); |
| 52 |
|
| •53 |
if(DEBUG) System.out.println("Locale: "+Locale.getDefault()); |
| 54 |
|
| 55 |
fakeRenderContext = fakeGraphics.getFontRenderContext(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
public int getWidthFor(String str, int pixelHeight){ |
| 60 |
Font font = getFontFor(pixelHeight); |
| 61 |
Rectangle2D rectangle = font.getStringBounds(str, fakeRenderContext); |
| 62 |
return (int) rectangle.getWidth(); |
| 63 |
} |
| 64 |
|
| 65 |
public int getHeightFor(String str, int pixelHeight){ |
| 66 |
Font font = getFontFor(pixelHeight); |
| 67 |
Rectangle2D rectangle = font.getStringBounds(str, fakeRenderContext); |
| 68 |
return (int) rectangle.getHeight(); |
| 69 |
} |
| 70 |
|
| 71 |
public int getWidthFor(String str, Font font){ |
| 72 |
Rectangle2D rectangle = font.getStringBounds(str, fakeRenderContext); |
| 73 |
return (int) rectangle.getWidth(); |
| 74 |
} |
| 75 |
|
| 76 |
public int getHeightFor(String str, Font font){ |
| 77 |
Rectangle2D rectangle = font.getStringBounds(str, fakeRenderContext); |
| 78 |
return (int) rectangle.getHeight(); |
| 79 |
} |
| 80 |
|
| 81 |
public Rectangle2D getBoundsFor(String str, Font font){ |
| 82 |
return font.getStringBounds(str, fakeRenderContext); |
| 83 |
} |
| 84 |
|
| 85 |
public Font getFontFor(int pixelHeight){ |
| 86 |
BufferedImage image = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB); |
| 87 |
Graphics2D g2 = image.createGraphics(); |
| 88 |
return getFontFor(pixelHeight, fakeRenderContext); |
| 89 |
} |
| 90 |
|
| 91 |
public int getAscent(Font font){ |
| 92 |
fakeGraphics.setFont(font); |
| 93 |
FontMetrics metrics = fakeGraphics.getFontMetrics(); |
| 94 |
if(DEBUG) System.out.println("Ascent: "+metrics.getAscent()); |
| 95 |
return metrics.getAscent(); |
| 96 |
} |
| 97 |
|
| 98 |
public int getZHeight(Font font){ |
| 99 |
int height = (int) font.createGlyphVector(fakeRenderContext, "Z").getOutline().getBounds().getHeight(); |
| 100 |
if(DEBUG) System.out.println("Z height: "+height); |
| 101 |
return height; |
| 102 |
} |
| 103 |
|
| 104 |
public Font getFontFor(int maxWidth, String string){ |
| 105 |
float size = 12; |
| 106 |
Font currentFont = new Font(fontFamilyName, Font.BOLD, (int) size); |
| 107 |
|
| 108 |
int width = getWidthFor(string, currentFont); |
| 109 |
|
| 110 |
int direction; |
| 111 |
if(width > maxWidth){ |
| 112 |
currentFont = currentFont.deriveFont(size - 1); |
| 113 |
size--; |
| 114 |
direction = -1; |
| 115 |
} else { |
| 116 |
currentFont = currentFont.deriveFont(size + 1); |
| 117 |
size++; |
| 118 |
direction = 1; |
| 119 |
} |
| 120 |
while(size > 0){ |
| 121 |
currentFont = currentFont.deriveFont(size); |
| 122 |
|
| 123 |
width = getWidthFor(string, currentFont); |
| 124 |
if(direction == 1){ |
| 125 |
if(width > maxWidth){ |
| 126 |
size = size - 1; |
| 127 |
return currentFont.deriveFont(size); |
| 128 |
} |
| 129 |
else size = size + 1; |
| 130 |
} else { |
| 131 |
if(width < maxWidth) |
| 132 |
return currentFont; |
| 133 |
else size = size - 1; |
| 134 |
} |
| 135 |
} |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public Font getFontFor(int pixelHeight, FontRenderContext frc){ |
| 142 |
float size = 12; |
| 143 |
Font currentFont = new Font(fontFamilyName, Font.BOLD, (int) size); |
| 144 |
|
| •145 |
if(DEBUG) System.out.println(currentFont.getFontName()); |
| 146 |
|
| 147 |
int ascent = getAscent(currentFont); |
| 148 |
|
| 149 |
int direction; |
| 150 |
if(ascent > pixelHeight){ |
| 151 |
currentFont = currentFont.deriveFont(size - 1); |
| 152 |
size--; |
| 153 |
direction = -1; |
| 154 |
} else { |
| 155 |
currentFont = currentFont.deriveFont(size + 1); |
| 156 |
size++; |
| 157 |
direction = 1; |
| 158 |
} |
| 159 |
while(size > 0){ |
| 160 |
currentFont = currentFont.deriveFont(size); |
| 161 |
|
| 162 |
ascent = getAscent(currentFont); |
| 163 |
if(direction == 1){ |
| 164 |
if(ascent > pixelHeight){ |
| 165 |
size = size - 0.5f; |
| 166 |
return currentFont.deriveFont(size); |
| 167 |
} |
| 168 |
else size = size + 0.5f; |
| 169 |
} else { |
| 170 |
if(ascent < pixelHeight) |
| 171 |
return currentFont; |
| 172 |
else size = size - 0.5f; |
| 173 |
} |
| 174 |
} |
| 175 |
return null; |
| 176 |
} |
| 177 |
|
| 178 |
public static FontMeasurer instance(){ |
| 179 |
return instance; |
| 180 |
} |
| 181 |
|
| 182 |
public FontMeasurer(){ |
| 183 |
} |
| 184 |
|
| 185 |
public static void main(String[] args) { |
| 186 |
|
| 187 |
float size = 12; |
| 188 |
Font currentFont = new Font("Sans", Font.BOLD, (int) size); |
| •189 |
if(DEBUG) System.out.println(currentFont.getSize()); |
| 190 |
currentFont = currentFont.deriveFont(--size); |
| 191 |
System.out.println(currentFont.getSize()); |
| 192 |
currentFont = currentFont.deriveFont(--size); |
| 193 |
System.out.println(currentFont.getSize()); |
| 194 |
currentFont = currentFont.deriveFont(--size); |
| 195 |
System.out.println(currentFont.getSize()); |
| 196 |
currentFont = currentFont.deriveFont(--size); |
| 197 |
System.out.println(currentFont.getSize()); |
| 198 |
currentFont = currentFont.deriveFont(--size); |
| 199 |
System.out.println(currentFont.getSize()); |
| 200 |
} |
| 201 |
} |