3 * $Date: 2013-10-30 13:47:37 -0500 (Wed, 30 Oct 2013) $
\r
6 * Copyright (C) 2003-2005 Miguel, Jmol Development, www.jmol.org
\r
8 * Contact: jmol-developers@lists.sf.net
\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
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
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
27 import javajs.api.FontManager;
\r
28 import javajs.util.AU;
\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
37 * Supports standard font faces, font styles, and font sizes.
\r
40 * @author Miguel, miguel@jmol.org
\r
42 final public class Font {
\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
55 private int descent;
\r
56 private boolean isBold;
\r
57 private boolean isItalic;
\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
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
73 fontMetrics = manager.getFontMetrics(this, graphics);
\r
74 descent = manager.getFontDescent(fontMetrics);
\r
75 ascent = manager.getFontAscent(fontMetrics);
\r
77 //System.out.println("font3d constructed for fontsizeNominal=" + fontSizeNominal + " and fontSize=" + fontSize);
\r
80 ////////////////////////////////////////////////////////////////
\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
87 public static Font getFont3D(byte fontID) {
\r
88 return font3ds[fontID & 0xFF];
\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
96 if (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
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
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
122 private final static String[] fontFaces =
\r
123 {"SansSerif", "Serif", "Monospaced", ""};
\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
130 private final static String[] fontStyles =
\r
131 {"Plain", "Bold", "Italic", "BoldItalic"};
\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
139 public static int getFontStyleID(String fontstyle) {
\r
140 for (int i = 4; --i >= 0; )
\r
141 if (fontStyles[i].equalsIgnoreCase(fontstyle))
\r
146 public int getAscent() {
\r
150 public int getDescent() {
\r
154 public int getHeight() {
\r
155 return getAscent() + getDescent();
\r
158 public Object getFontMetrics() {
\r
159 return fontMetrics;
\r
162 public int stringWidth(String text) {
\r
163 return manager.fontStringWidth(this, text);
\r
166 public String getInfo() {
\r
167 return fontSizeNominal + " " + fontFace + " " + fontStyle;
\r