001: /*
002: * @(#)FontDemo.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis.demos;
027:
028: import java.awt.*;
029: import java.util.HashMap;
030: import basis.Builder;
031:
032: public class FontDemo extends Demo {
033: private static String[] fontNames = new String[] { "Serif",
034: "SansSerif", "Monospaced", "Dialog", "DialogInput",
035: "Symbol" };
036: private static HashMap cache;
037: private static Image logo;
038: private int fontSize;
039: private int largeFontSize;
040: private int smallFontSize;
041: private char character = 'g';
042: private int factor = 0;
043:
044: public FontDemo() {
045: if (logo == null) {
046: logo = ImageDemo.loadImage(this , "images/logo.gif");
047: }
048: }
049:
050: public void paint(Graphics g) {
051: Dimension d = getSize();
052: int w = d.width - 1;
053: int h = d.height - 1;
054: resize();
055: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
056: AlphaComposite.SRC_OVER, 0.2f));
057: g.drawImage(logo, 0, 0, w, h, this );
058: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
059: AlphaComposite.SRC_OVER, 1.0f));
060: Font font = new Font("monospaced", Font.PLAIN, fontNames.length
061: * fontSize);
062: g.setFont(font);
063: setStatus("Font " + fontSize);
064: g.setColor(Color.black);
065: font = new Font("monospaced", Font.PLAIN, fontSize);
066: FontMetrics fm = g.getFontMetrics(font);
067: int charWidth = fm.charWidth('O');
068: int y = fontSize + 2;
069: for (int i = 0; i < fontNames.length; i++) {
070: int x = 2;
071: font = new Font(fontNames[i], Font.PLAIN, fontSize);
072: g.setFont(font);
073: g.drawString(fontNames[i], x, y);
074: x += 11 * charWidth;
075: font = new Font(fontNames[i], Font.BOLD, fontSize);
076: g.setFont(font);
077: g.drawString("Bold", x, y);
078: x += 5 * charWidth;
079: font = new Font(fontNames[i], Font.ITALIC, fontSize);
080: g.setFont(font);
081: g.drawString("Ital", x, y);
082: x += 4 * charWidth;
083: font = new Font(fontNames[i], Font.BOLD + Font.ITALIC,
084: fontSize);
085: g.setFont(font);
086: g.drawString("BItal", x, y);
087: y += (fontSize + 2);
088: }
089: y = fontNames.length * (fontSize + 2);
090: largeFontSize = (h - y) / 2;
091: int max = 2 * w / 3;
092: largeFontSize = largeFontSize < max ? largeFontSize : max;
093: font = new Font("Dialog", Font.PLAIN, largeFontSize);
094: g.setFont(font);
095: fm = g.getFontMetrics(font);
096: charWidth = fm.charWidth(character);
097: int ascent = fm.getAscent();
098: int descent = fm.getDescent();
099: int height = fm.getHeight();
100: int leading = fm.getLeading();
101: int maxAdvance = fm.getMaxAdvance();
102: int maxAscent = fm.getMaxAscent();
103: int maxDescent = fm.getMaxDescent();
104: int x = 4;
105: g.drawString("" + character, x, y + ascent);
106: g.drawLine(x, y + ascent, x + charWidth, y + ascent);
107: g.drawLine(x, y, x, y + ascent + descent);
108: w = (d.width - charWidth) / 12;
109: h = (d.height - y) / 9;
110: smallFontSize = w < h ? w : h;
111: x = charWidth + 8;
112: font = new Font("monospaced", Font.PLAIN, smallFontSize);
113: g.setFont(font);
114: y += smallFontSize;
115: g.drawString("size: " + largeFontSize, x, y);
116: y += smallFontSize;
117: g.drawString("ascent: " + ascent, x, y);
118: y += smallFontSize;
119: g.drawString("descent: " + descent, x, y);
120: y += smallFontSize;
121: g.drawString("height: " + height, x, y);
122: y += smallFontSize;
123: g.drawString("leading: " + leading, x, y);
124: y += smallFontSize;
125: g.drawString("maxAdvance: " + maxAdvance, x, y);
126: y += smallFontSize;
127: g.drawString("maxAscent: " + maxAscent, x, y);
128: y += smallFontSize;
129: g.drawString("maxDescent: " + maxDescent, x, y);
130: y += smallFontSize;
131: g.drawString("charWidth: " + charWidth, x, y);
132: }
133:
134: private void resize() {
135: Dimension d = getSize();
136: if (factor == 0) {
137: int guess = d.width / 40;
138: Graphics g = getGraphics();
139: if (g == null) {
140: return;
141: }
142: fontSize = getFontSize(g, d.width, guess);
143: factor = d.width / fontSize;
144: }
145: fontSize = d.width / factor;
146: int max = d.height / 2 / fontNames.length;
147: fontSize = fontSize < max ? fontSize : max;
148: }
149:
150: private static int getFontSize(Graphics g, int width, int guess) {
151: while (true) {
152: Font font = new Font("monospaced", Font.BOLD + Font.ITALIC,
153: guess + 1);
154: FontMetrics fm = g.getFontMetrics(font);
155: int fw = fm.stringWidth("Monospaced Bold Ital BItal");
156: if (++guess >= width / 10) {
157: break;
158: }
159: if (fw <= width) {
160: continue;
161: } else {
162: break;
163: }
164: }
165: return guess;
166: }
167: }
|