JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / awt / Font.java
1 /* $RCSfile$\r
2  * $Author: hansonr $\r
3  * $Date: 2013-10-30 13:47:37 -0500 (Wed, 30 Oct 2013) $\r
4  * $Revision: 18874 $\r
5  *\r
6  * Copyright (C) 2003-2005  Miguel, Jmol Development, www.jmol.org\r
7  *\r
8  * Contact: jmol-developers@lists.sf.net\r
9  *\r
10  *  This library is free software; you can redistribute it and/or\r
11  *  modify it under the terms of the GNU Lesser General Public\r
12  *  License as published by the Free Software Foundation; either\r
13  *  version 2.1 of the License, or (at your option) any later version.\r
14  *\r
15  *  This library is distributed in the hope that it will be useful,\r
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
18  *  Lesser General Public License for more details.\r
19  *\r
20  *  You should have received a copy of the GNU Lesser General Public\r
21  *  License along with this library; if not, write to the Free Software\r
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
23  */\r
24 package javajs.awt;\r
25 \r
26 \r
27 import javajs.api.FontManager;\r
28 import javajs.util.AU;\r
29 \r
30 \r
31 /**\r
32  *<p>\r
33  * Provides font support using a byte fid\r
34  * (<strong>F</strong>ont <strong>ID</strong>) as an index into font table.\r
35  *</p>\r
36  *<p>\r
37  * Supports standard font faces, font styles, and font sizes.\r
38  *</p>\r
39  *\r
40  * @author Miguel, miguel@jmol.org\r
41  */\r
42 final public class Font {\r
43 \r
44   public final byte fid;\r
45   public final String fontFace;\r
46   public final String fontStyle;\r
47   public final float fontSizeNominal;\r
48   public final int idFontFace;\r
49   public final int idFontStyle;\r
50   public final float fontSize;\r
51   public final Object font;\r
52   private final Object fontMetrics;\r
53   private FontManager manager;\r
54   private int ascent;\r
55   private int descent;\r
56   private boolean isBold;\r
57   private boolean isItalic;\r
58   \r
59   private Font(FontManager manager, byte fid, int idFontFace,\r
60       int idFontStyle, float fontSize, float fontSizeNominal, Object graphics) {\r
61     this.manager = manager;\r
62     this.fid = fid;\r
63     this.fontFace = fontFaces[idFontFace];\r
64     this.fontStyle = fontStyles[idFontStyle];\r
65     this.idFontFace = idFontFace;\r
66     this.idFontStyle = idFontStyle;\r
67     this.fontSize = fontSize;\r
68     this.isBold = (idFontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD;\r
69     this.isItalic = (idFontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC;\r
70     this.fontSizeNominal = fontSizeNominal;\r
71     font = manager.newFont(fontFaces[idFontFace], isBold, isItalic,\r
72         fontSize);\r
73     fontMetrics = manager.getFontMetrics(this, graphics);\r
74     descent = manager.getFontDescent(fontMetrics);\r
75     ascent = manager.getFontAscent(fontMetrics);\r
76 \r
77     //System.out.println("font3d constructed for fontsizeNominal=" + fontSizeNominal + "  and fontSize=" + fontSize);\r
78   }\r
79 \r
80   ////////////////////////////////////////////////////////////////\r
81   \r
82   private final static int FONT_ALLOCATION_UNIT = 8;\r
83   private static int fontkeyCount = 1;\r
84   private static int[] fontkeys = new int[FONT_ALLOCATION_UNIT];\r
85   private static Font[] font3ds = new Font[FONT_ALLOCATION_UNIT];\r
86 \r
87   public static Font getFont3D(byte fontID) {\r
88     return font3ds[fontID & 0xFF];\r
89   }\r
90   \r
91   public static synchronized Font createFont3D(int fontface, int fontstyle,\r
92                                        float fontsize, float fontsizeNominal,\r
93                                        FontManager manager, Object graphicsForMetrics) {\r
94     //if (graphicsForMetrics == null)\r
95      // return null;\r
96     if (fontsize > 0xFF)\r
97       fontsize = 0xFF;\r
98     int fontsizeX16 = ((int) fontsize) << 4;\r
99     int fontkey = ((fontface & 3) | ((fontstyle & 3) << 2) | (fontsizeX16 << 4));\r
100     // watch out for race condition here!\r
101     for (int i = fontkeyCount; --i > 0;)\r
102       if (fontkey == fontkeys[i]\r
103           && font3ds[i].fontSizeNominal == fontsizeNominal)\r
104         return font3ds[i];\r
105     int fontIndexNext = fontkeyCount++;\r
106     if (fontIndexNext == fontkeys.length)\r
107       fontkeys = AU.arrayCopyI(fontkeys, fontIndexNext + FONT_ALLOCATION_UNIT);\r
108       font3ds = (Font[]) AU.arrayCopyObject(font3ds, fontIndexNext + FONT_ALLOCATION_UNIT);\r
109     Font font3d = new Font(manager, (byte) fontIndexNext, fontface, fontstyle,\r
110         fontsize, fontsizeNominal, graphicsForMetrics);\r
111     // you must set the font3d before setting the fontkey in order\r
112     // to prevent a race condition with getFont3D\r
113     font3ds[fontIndexNext] = font3d;\r
114     fontkeys[fontIndexNext] = fontkey;\r
115     return font3d;\r
116   }\r
117 \r
118   public final static int FONT_FACE_SANS  = 0;\r
119   public final static int FONT_FACE_SERIF = 1;\r
120   public final static int FONT_FACE_MONO  = 2;\r
121   \r
122   private final static String[] fontFaces =\r
123   {"SansSerif", "Serif", "Monospaced", ""};\r
124 \r
125   public final static int FONT_STYLE_PLAIN      = 0;\r
126   public final static int FONT_STYLE_BOLD       = 1;\r
127   public final static int FONT_STYLE_ITALIC     = 2;\r
128   public final static int FONT_STYLE_BOLDITALIC = 3;\r
129   \r
130   private final static String[] fontStyles =\r
131   {"Plain", "Bold", "Italic", "BoldItalic"};\r
132   \r
133   public static int getFontFaceID(String fontface) {\r
134     return ("Monospaced".equalsIgnoreCase(fontface) ? FONT_FACE_MONO \r
135         : "Serif".equalsIgnoreCase(fontface) ? FONT_FACE_SERIF \r
136         : FONT_FACE_SANS);\r
137   }\r
138 \r
139   public static int getFontStyleID(String fontstyle) {\r
140     for (int i = 4; --i >= 0; )\r
141       if (fontStyles[i].equalsIgnoreCase(fontstyle))\r
142        return i;\r
143     return -1;\r
144   }\r
145 \r
146   public int getAscent() {\r
147     return ascent;\r
148   }\r
149   \r
150   public int getDescent() {\r
151     return descent;\r
152   }\r
153   \r
154   public int getHeight() {\r
155     return getAscent() + getDescent();\r
156   }\r
157 \r
158   public Object getFontMetrics() {\r
159     return fontMetrics;\r
160   }\r
161   \r
162   public int stringWidth(String text) {\r
163     return manager.fontStringWidth(this, text);\r
164   }\r
165 \r
166   public String getInfo() {\r
167     return  fontSizeNominal + " " + fontFace + " " + fontStyle;\r
168   }\r
169 }\r
170 \r