001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Ilya S. Okomin
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.font.FontRenderContext;
023: import java.awt.font.LineMetrics;
024: import java.awt.geom.Rectangle2D;
025: import java.io.Serializable;
026: import java.text.CharacterIterator;
027:
028: import org.apache.harmony.awt.internal.nls.Messages;
029:
030: public abstract class FontMetrics implements Serializable {
031: private static final long serialVersionUID = 1681126225205050147L;
032:
033: protected Font font;
034:
035: protected FontMetrics(Font fnt) {
036: this .font = fnt;
037: }
038:
039: @Override
040: public String toString() {
041: return this .getClass().getName() + "[font=" + this .getFont() + //$NON-NLS-1$
042: "ascent=" + this .getAscent() + //$NON-NLS-1$
043: ", descent=" + this .getDescent() + //$NON-NLS-1$
044: ", height=" + this .getHeight() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
045: }
046:
047: public Font getFont() {
048: return font;
049: }
050:
051: public int getHeight() {
052: return this .getAscent() + this .getDescent() + this .getLeading();
053: }
054:
055: public int getAscent() {
056: return 0;
057: }
058:
059: public int getDescent() {
060: return 0;
061: }
062:
063: public int getLeading() {
064: return 0;
065: }
066:
067: public LineMetrics getLineMetrics(CharacterIterator ci,
068: int beginIndex, int limit, Graphics context) {
069: return font.getLineMetrics(ci, beginIndex, limit, this
070: .getFRCFromGraphics(context));
071: }
072:
073: public LineMetrics getLineMetrics(String str, Graphics context) {
074: return font.getLineMetrics(str, this
075: .getFRCFromGraphics(context));
076: }
077:
078: public LineMetrics getLineMetrics(char[] chars, int beginIndex,
079: int limit, Graphics context) {
080: return font.getLineMetrics(chars, beginIndex, limit, this
081: .getFRCFromGraphics(context));
082: }
083:
084: public LineMetrics getLineMetrics(String str, int beginIndex,
085: int limit, Graphics context) {
086: return font.getLineMetrics(str, beginIndex, limit, this
087: .getFRCFromGraphics(context));
088: }
089:
090: public Rectangle2D getMaxCharBounds(Graphics context) {
091: return this .font.getMaxCharBounds(this
092: .getFRCFromGraphics(context));
093: }
094:
095: public Rectangle2D getStringBounds(CharacterIterator ci,
096: int beginIndex, int limit, Graphics context) {
097: return font.getStringBounds(ci, beginIndex, limit, this
098: .getFRCFromGraphics(context));
099: }
100:
101: public Rectangle2D getStringBounds(String str, int beginIndex,
102: int limit, Graphics context) {
103: return font.getStringBounds(str, beginIndex, limit, this
104: .getFRCFromGraphics(context));
105: }
106:
107: public Rectangle2D getStringBounds(char[] chars, int beginIndex,
108: int limit, Graphics context) {
109: return font.getStringBounds(chars, beginIndex, limit, this
110: .getFRCFromGraphics(context));
111: }
112:
113: public Rectangle2D getStringBounds(String str, Graphics context) {
114: return font.getStringBounds(str, this
115: .getFRCFromGraphics(context));
116: }
117:
118: public boolean hasUniformLineMetrics() {
119: return this .font.hasUniformLineMetrics();
120: }
121:
122: public int bytesWidth(byte[] data, int off, int len) {
123: int width = 0;
124: if ((off >= data.length) || (off < 0)) {
125: // awt.13B=offset off is out of range
126: throw new IllegalArgumentException(Messages
127: .getString("awt.13B")); //$NON-NLS-1$
128: }
129:
130: if ((off + len > data.length)) {
131: // awt.13C=number of elements len is out of range
132: throw new IllegalArgumentException(Messages
133: .getString("awt.13C")); //$NON-NLS-1$
134: }
135:
136: for (int i = off; i < off + len; i++) {
137: width += charWidth(data[i]);
138: }
139:
140: return width;
141: }
142:
143: public int charsWidth(char[] data, int off, int len) {
144: int width = 0;
145: if ((off >= data.length) || (off < 0)) {
146: // awt.13B=offset off is out of range
147: throw new IllegalArgumentException(Messages
148: .getString("awt.13B")); //$NON-NLS-1$
149: }
150:
151: if ((off + len > data.length)) {
152: // awt.13C=number of elements len is out of range
153: throw new IllegalArgumentException(Messages
154: .getString("awt.13C")); //$NON-NLS-1$
155: }
156:
157: for (int i = off; i < off + len; i++) {
158: width += charWidth(data[i]);
159: }
160:
161: return width;
162: }
163:
164: public int charWidth(int ch) {
165: return 0;
166: }
167:
168: public int charWidth(char ch) {
169: return 0;
170: }
171:
172: public int getMaxAdvance() {
173: return 0;
174: }
175:
176: public int getMaxAscent() {
177: return 0;
178: }
179:
180: /**
181: * @deprecated
182: */
183: @Deprecated
184: public int getMaxDecent() {
185: return 0;
186: }
187:
188: public int getMaxDescent() {
189: return 0;
190: }
191:
192: public int[] getWidths() {
193: return null;
194: }
195:
196: public int stringWidth(String str) {
197: return 0;
198: }
199:
200: /**
201: * Returns FontRenderContext instance of the Graphics context specified.
202: * @param context the specified Graphics context
203: *
204: * @return a FontRenderContext of the specified Graphics context.
205: */
206: private FontRenderContext getFRCFromGraphics(Graphics context) {
207: FontRenderContext frc;
208: if (context instanceof Graphics2D) {
209: frc = ((Graphics2D) context).getFontRenderContext();
210: } else {
211: frc = new FontRenderContext(null, false, false);
212: }
213:
214: return frc;
215:
216: }
217: }
|