View Javadoc

1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
25   */
26  package ca.uhn.hl7v2.testpanel.util;
27  
28  import java.awt.Font;
29  import java.awt.FontMetrics;
30  import java.awt.Graphics;
31  import java.awt.GraphicsEnvironment;
32  import java.awt.image.BufferedImage;
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.List;
36  
37  public class FontUtil {
38  
39  	/**
40  	 * @see http://stackoverflow.com/questions/922052/testing-whether-a-font-is-monospaced-in-java
41  	 */
42  	public static List<String> getMonospacedFontNames() {
43  		
44  		List<String> monospaceFontFamilyNames = new ArrayList<String>();
45  
46  		GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
47  		String[] fontFamilyNames = graphicsEnvironment.getAvailableFontFamilyNames();
48  
49  		BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
50  		Graphics graphics = bufferedImage.createGraphics();
51  
52  		for (String fontFamilyName : fontFamilyNames) {
53  		    boolean isMonospaced = true;
54  
55  		    int fontStyle = Font.PLAIN;
56  		    int fontSize = 12;
57  		    Font font = new Font(fontFamilyName, fontStyle, fontSize);
58  		    FontMetrics fontMetrics = graphics.getFontMetrics(font);
59  
60  		    int firstCharacterWidth = 0;
61  		    boolean hasFirstCharacterWidth = false;
62  		    for (int codePoint = 0; codePoint < 128; codePoint++) { 
63  		        if (Character.isValidCodePoint(codePoint) && (Character.isLetter(codePoint) || Character.isDigit(codePoint))) {
64  		            char character = (char) codePoint;
65  		            int characterWidth = fontMetrics.charWidth(character);
66  		            if (hasFirstCharacterWidth) {
67  		                if (characterWidth != firstCharacterWidth) {
68  		                    isMonospaced = false;
69  		                    break;
70  		                }
71  		            } else {
72  		                firstCharacterWidth = characterWidth;
73  		                hasFirstCharacterWidth = true;
74  		            }
75  		        }
76  		    }
77  
78  		    if (isMonospaced) {
79  		        monospaceFontFamilyNames.add(fontFamilyName);
80  		    }
81  		}
82  
83  		graphics.dispose();
84  		
85  		Collections.sort(monospaceFontFamilyNames);
86  		return monospaceFontFamilyNames;
87  		
88  	}
89  
90  }