View Javadoc

1   //--------------------------------------
2   //
3   // Font.java
4   // Since: 2009/07/15
5   //
6   //--------------------------------------
7   package org.utgenome.gwt.utgb.server.app;
8   
9   import java.awt.Color;
10  import java.awt.Font;
11  import java.awt.FontFormatException;
12  import java.awt.FontMetrics;
13  import java.awt.Graphics2D;
14  import java.awt.RenderingHints;
15  import java.awt.image.BufferedImage;
16  import java.io.IOException;
17  import java.net.URL;
18  
19  import javax.imageio.ImageIO;
20  import javax.servlet.ServletException;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.utgenome.gwt.utgb.server.WebTrackBase;
25  import org.xerial.util.FileResource;
26  import org.xerial.util.Pair;
27  import org.xerial.util.log.Logger;
28  
29  /**
30   * Font panel
31   * 
32   */
33  public class FontPanel extends WebTrackBase {
34  	private static final long serialVersionUID = 1L;
35  	private static Logger _logger = Logger.getLogger(FontPanel.class);
36  
37  	public FontPanel() {
38  
39  	}
40  
41  	public float fontSize = 10;
42  	public String color = "#003366";
43  
44  	public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
45  
46  		Pair<FontInfo, Font> fontInfo = getFontInfo(fontSize);
47  		FontInfo fi = fontInfo.getFirst();
48  
49  		BufferedImage bf = new BufferedImage(fi.width * 256, fi.height, BufferedImage.TYPE_INT_ARGB);
50  		Graphics2D g = (Graphics2D) bf.getGraphics();
51  		g.setColor(new Color(0, 0, 0, 0));
52  		g.fillRect(0, 0, bf.getWidth(), bf.getHeight());
53  
54  		Color col;
55  		try {
56  			col = Color.decode(color);
57  		}
58  		catch (NumberFormatException e) {
59  			col = Color.BLACK;
60  		}
61  
62  		g.setColor(col);
63  		g.setFont(fontInfo.getSecond());
64  		//g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
65  		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
66  
67  		for (char c = 0; c < 256; c++)
68  			g.drawString(String.valueOf(c), c * fi.width, fi.baseLine);
69  
70  		response.setContentType("image/png");
71  		ImageIO.write(bf, "png", response.getOutputStream());
72  
73  	}
74  
75  	public static class FontInfo {
76  		public float size;
77  		public int height;
78  		public int width;
79  		public int baseLine;
80  	}
81  
82  	public static Pair<FontInfo, Font> getFontInfo(float fontSize) throws IOException {
83  		BufferedImage b = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
84  		Graphics2D gEnv = (Graphics2D) b.getGraphics();
85  
86  		Font onePixelFont;
87  		try {
88  			URL fontFile = FileResource.find(FontPanel.class, "Anonymous.ttf");
89  			if (fontFile == null)
90  				onePixelFont = new Font("Monaco", Font.TRUETYPE_FONT, 1);
91  			else
92  				onePixelFont = Font.createFont(Font.TRUETYPE_FONT, fontFile.openStream());
93  		}
94  		catch (FontFormatException e) {
95  			throw new IOException(e.getMessage());
96  		}
97  		Font font = onePixelFont.deriveFont(fontSize);
98  		FontMetrics fm = gEnv.getFontMetrics(font);
99  
100 		FontInfo fi = new FontInfo();
101 		fi.size = fontSize;
102 		fi.height = fm.getHeight() + fm.getMaxDescent();
103 		fi.width = fm.stringWidth("A");
104 		fi.baseLine = fm.getHeight() - fm.getMaxDescent();
105 
106 		return new Pair<FontInfo, Font>(fi, font);
107 	}
108 
109 }