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: package org.apache.tools.ant.types.optional.image;
019:
020: import javax.media.jai.PlanarImage;
021: import java.awt.Color;
022: import java.awt.Font;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics2D;
025: import java.awt.RenderingHints;
026: import java.awt.image.BufferedImage;
027:
028: /**
029: *
030: * @see org.apache.tools.ant.taskdefs.optional.image.Image
031: */
032: public class Text extends ImageOperation implements DrawOperation {
033: private static final int DEFAULT_POINT = 10;
034:
035: private String strText = "";
036: private String font = "Arial";
037: private int point = DEFAULT_POINT;
038: private boolean bold = false;
039: private boolean italic = false;
040: private String color = "black";
041:
042: /**
043: * Set the string to be used as text.
044: * @param str the string to be used.
045: */
046: public void setString(String str) {
047: strText = str;
048: }
049:
050: /**
051: * Set the font to be used to draw the text.
052: * @param f the font to be used.
053: */
054: public void setFont(String f) {
055: font = f;
056: }
057:
058: /**
059: * Set the number of points to be used.
060: * @param p an integer value as a string.
061: */
062: public void setPoint(String p) {
063: point = Integer.parseInt(p);
064: }
065:
066: /**
067: * Set the color of the text.
068: * @param c the color name.
069: */
070: public void setColor(String c) {
071: color = c;
072: }
073:
074: /**
075: * @todo is this used?
076: * @param state not used at the moment.
077: */
078: public void setBold(boolean state) {
079: bold = state;
080: }
081:
082: /**
083: * @todo is this used?
084: * @param state not used at the moment.
085: */
086: public void setItalic(boolean state) {
087: italic = state;
088: }
089:
090: /**
091: * Draw the text.
092: * @return the resultant image.
093: */
094: public PlanarImage executeDrawOperation() {
095: log("\tCreating Text \"" + strText + "\"");
096:
097: Color couloir = ColorMapper.getColorByName(color);
098: int width = 1;
099: int height = 1;
100:
101: BufferedImage bi = new BufferedImage(width, height,
102: BufferedImage.TYPE_4BYTE_ABGR_PRE);
103: Graphics2D graphics = (Graphics2D) bi.getGraphics();
104: graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
105: RenderingHints.VALUE_ANTIALIAS_ON);
106: graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
107: RenderingHints.VALUE_FRACTIONALMETRICS_ON);
108: Font f = new Font(font, Font.PLAIN, point);
109: FontMetrics fmetrics = graphics.getFontMetrics(f);
110: height = fmetrics.getMaxAscent() + fmetrics.getMaxDescent();
111: width = fmetrics.stringWidth(strText);
112:
113: bi = new BufferedImage(width, height,
114: BufferedImage.TYPE_4BYTE_ABGR_PRE);
115: graphics = (Graphics2D) bi.getGraphics();
116:
117: graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
118: RenderingHints.VALUE_ANTIALIAS_ON);
119: graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
120: RenderingHints.VALUE_FRACTIONALMETRICS_ON);
121:
122: graphics.setFont(f);
123: graphics.setColor(couloir);
124: graphics.drawString(strText, 0, height
125: - fmetrics.getMaxDescent());
126: PlanarImage image = PlanarImage.wrapRenderedImage(bi);
127: return image;
128: }
129: }
|