Debian Codegraph @

src/org/stathissideris/ascii2image/graphics/FontMeasurer.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * DiTAA - Diagrams Through Ascii Art
3 *
4 * Copyright (C) 2004 Efstathios Sideris
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21 package org.stathissideris.ascii2image.graphics;
22
23 import java.awt.Font;
24 import java.awt.FontMetrics;
25 import java.awt.Graphics2D;
26 import java.awt.font.FontRenderContext;
27 import java.awt.geom.Rectangle2D;
28 import java.awt.image.BufferedImage;
29 import java.util.Locale;
30
31 /**
32 *
33 * @author Efstathios Sideris
34 */
35 public class FontMeasurer {
36
37 private static final String fontFamilyName = "Dialog";
38 //private static final String fontFamilyName = "Helvetica";
39
40 private static final boolean DEBUG = false;
41
42 private static final FontMeasurer instance = new FontMeasurer();
43 FontRenderContext fakeRenderContext;
44 Graphics2D fakeGraphics;
45
46 {
47 BufferedImage image = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB);
48 fakeGraphics = image.createGraphics();
49
50 if (DEBUG) System.out.println("Locale: "+Locale.getDefault());
51
52 fakeRenderContext = fakeGraphics.getFontRenderContext();
53 }
54
55
56 public int getWidthFor(String str, int pixelHeight){
57 Font font = getFontFor(pixelHeight);
58 Rectangle2D rectangle = font.getStringBounds(str, fakeRenderContext);
59 return (int) rectangle.getWidth();
60 }
61
62 public int getHeightFor(String str, int pixelHeight){
63 Font font = getFontFor(pixelHeight);
64 Rectangle2D rectangle = font.getStringBounds(str, fakeRenderContext);
65 return (int) rectangle.getHeight();
66 }
67
68 public int getWidthFor(String str, Font font){
69 Rectangle2D rectangle = font.getStringBounds(str, fakeRenderContext);
70 return (int) rectangle.getWidth();
71 }
72
73 public int getHeightFor(String str, Font font){
74 Rectangle2D rectangle = font.getStringBounds(str, fakeRenderContext);
75 return (int) rectangle.getHeight();
76 }
77
78 public Rectangle2D getBoundsFor(String str, Font font){
79 return font.getStringBounds(str, fakeRenderContext);
80 }
81
82 public Font getFontFor(int pixelHeight){
83 BufferedImage image = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB);
84 Graphics2D g2 = image.createGraphics();
85 return getFontFor(pixelHeight, fakeRenderContext);
86 }
87
88 public int getAscent(Font font){
89 fakeGraphics.setFont(font);
90 FontMetrics metrics = fakeGraphics.getFontMetrics();
91 if(DEBUG) System.out.println("Ascent: "+metrics.getAscent());
92 return metrics.getAscent();
93 }
94
95 public int getZHeight(Font font){
96 int height = (int) font.createGlyphVector(fakeRenderContext, "Z").getOutline().getBounds().getHeight();
97 if(DEBUG) System.out.println("Z height: "+height);
98 return height;
99 }
100
101 public Font getFontFor(int maxWidth, String string){
102 float size = 12;
103 Font currentFont = new Font(fontFamilyName, Font.BOLD, (int) size);
104 //ascent is the distance between the baseline and the tallest character
105 int width = getWidthFor(string, currentFont);
106
107 int direction; //direction of size change (towards smaller or bigger)
108 if(width > maxWidth){
109 currentFont = currentFont.deriveFont(size - 1);
110 size--;
111 direction = -1;
112 } else {
113 currentFont = currentFont.deriveFont(size + 1);
114 size++;
115 direction = 1;
116 }
117 while(size > 0){
118 currentFont = currentFont.deriveFont(size);
119 //rectangle = currentFont.getStringBounds(testString, frc);
120 width = getWidthFor(string, currentFont);
121 if(direction == 1){
122 if(width > maxWidth){
123 size = size - 1;
124 return currentFont.deriveFont(size);
125 }
126 else size = size + 1;
127 } else {
128 if(width < maxWidth)
129 return currentFont;
130 else size = size - 1;
131 }
132 }
133 return null;
134 }
135
136
137
138 public Font getFontFor(int pixelHeight, FontRenderContext frc){
139 float size = 12;
140 Font currentFont = new Font(fontFamilyName, Font.BOLD, (int) size);
141 // Font currentFont = new Font("Times", Font.BOLD, (int) size);
142 if (DEBUG) System.out.println(currentFont.getFontName());
143 //ascent is the distance between the baseline and the tallest character
144 int ascent = getAscent(currentFont);
145
146 int direction; //direction of size change (towards smaller or bigger)
147 if(ascent > pixelHeight){
148 currentFont = currentFont.deriveFont(size - 1);
149 size--;
150 direction = -1;
151 } else {
152 currentFont = currentFont.deriveFont(size + 1);
153 size++;
154 direction = 1;
155 }
156 while(size > 0){
157 currentFont = currentFont.deriveFont(size);
158 //rectangle = currentFont.getStringBounds(testString, frc);
159 ascent = getAscent(currentFont);
160 if(direction == 1){
161 if(ascent > pixelHeight){
162 size = size - 0.5f;
163 return currentFont.deriveFont(size);
164 }
165 else size = size + 0.5f;
166 } else {
167 if(ascent < pixelHeight)
168 return currentFont;
169 else size = size - 0.5f;
170 }
171 }
172 return null;
173 }
174
175 public static FontMeasurer instance(){
176 return instance;
177 }
178
179 public FontMeasurer(){
180 }
181
182 public static void main(String[] args) {
183 //FontMeasurer.instance().getFontFor(7);
184 float size = 12;
185 Font currentFont = new Font("Sans", Font.BOLD, (int) size);
186 System.out.println(currentFont.getSize());
187 currentFont = currentFont.deriveFont(--size);
188 System.out.println(currentFont.getSize());
189 currentFont = currentFont.deriveFont(--size);
190 System.out.println(currentFont.getSize());
191 currentFont = currentFont.deriveFont(--size);
192 System.out.println(currentFont.getSize());
193 currentFont = currentFont.deriveFont(--size);
194 System.out.println(currentFont.getSize());
195 currentFont = currentFont.deriveFont(--size);
196 System.out.println(currentFont.getSize());
197 }
198 }