001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.Font;
024: import java.awt.FontMetrics;
025: import java.awt.Graphics;
026: import java.awt.Toolkit;
027:
028: /**
029: * This class extends <code>Font</code> functionality with underline
030: * and antialias style.
031: * <br>
032: * <b>This is one of the core WingS classes required by all the components</b><br>
033: * <br>
034: * <b>This class is thread safe.</b>
035: **/
036: public class WingFont {
037: protected Font font;
038: protected boolean underline;
039: protected boolean antialias;
040:
041: protected FontMetrics fm;
042: protected char[] ch;
043:
044: public WingFont(Font font) {
045: this (font, false, false);
046: }
047:
048: public WingFont(String name, int style, int size,
049: boolean underline, boolean antialias) {
050: this (new Font(name, style, size), underline, antialias);
051: }
052:
053: public WingFont(Font font, boolean underline, boolean antialias) {
054: this .font = font;
055: this .underline = underline;
056: this .antialias = antialias;
057: }
058:
059: public Font getFont() {
060: return font;
061: }
062:
063: public boolean isUnderline() {
064: return underline;
065: }
066:
067: public boolean isAntialias() {
068: return antialias;
069: }
070:
071: protected FontMetrics metrics() {
072: if (fm == null) {
073: fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
074: }
075: return fm;
076: }
077:
078: public int getAscent() {
079: return metrics().getAscent();
080: }
081:
082: public int getDescent() {
083: return metrics().getDescent();
084: }
085:
086: public int stringWidth(String s) {
087: if (antialias)
088: return WingToolkit.the().stringWidth(this , s);
089: return metrics().stringWidth(s);
090: }
091:
092: public int charsWidth(char[] data, int offset, int length) {
093: if (antialias)
094: return WingToolkit.the().charsWidth(this , data, offset,
095: length);
096: return metrics().charsWidth(data, offset, length);
097: }
098:
099: public int getHeight() {
100: return metrics().getHeight();
101: }
102:
103: public void drawString(Graphics g, String s, int x, int y) {
104: WingToolkit.the().setTextAntialias(g, antialias);
105: g.drawString(s, x, y);
106: if (underline) {
107: g.drawLine(x, y + 1, x + stringWidth(s), y + 1);
108: }
109: }
110:
111: public void drawChars(Graphics g, char[] data, int offset,
112: int length, int x, int y) {
113: drawChars(g, data, offset, length, x, y, true);
114: }
115:
116: public void drawChars(Graphics g, char[] data, int offset,
117: int length, int x, int y, boolean first) {
118: if (first)
119: WingToolkit.the().setTextAntialias(g, antialias);
120: g.drawChars(data, offset, length, x, y);
121: if (underline) {
122: g.drawLine(x, y + 1, x + charsWidth(data, offset, length),
123: y + 1);
124: }
125: }
126: }
|