1 | /*
|
---|
2 | * Copyright 2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.types.optional.image;
|
---|
18 |
|
---|
19 | import javax.media.jai.PlanarImage;
|
---|
20 | import java.awt.Color;
|
---|
21 | import java.awt.Font;
|
---|
22 | import java.awt.FontMetrics;
|
---|
23 | import java.awt.Graphics2D;
|
---|
24 | import java.awt.RenderingHints;
|
---|
25 | import java.awt.image.BufferedImage;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | *
|
---|
29 | * @see org.apache.tools.ant.taskdefs.optional.image.Image
|
---|
30 | */
|
---|
31 | public class Text extends ImageOperation implements DrawOperation {
|
---|
32 | private String str_text = "";
|
---|
33 | private String font = "Arial";
|
---|
34 | private int point = 10;
|
---|
35 | private boolean bold = false;
|
---|
36 | private boolean italic = false;
|
---|
37 | private String color = "black";
|
---|
38 |
|
---|
39 | public void setString(String str) {
|
---|
40 | str_text = str;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void setFont(String f) {
|
---|
44 | font = f;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void setPoint(String p) {
|
---|
48 | point = Integer.parseInt(p);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void setColor(String c) {
|
---|
52 | color = c;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * @todo is this used?
|
---|
57 | */
|
---|
58 | public void setBold(boolean state) {
|
---|
59 | bold = state;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @todo is this used?
|
---|
64 | */
|
---|
65 | public void setItalic(boolean state) {
|
---|
66 | italic = state;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public PlanarImage executeDrawOperation() {
|
---|
70 | log("\tCreating Text \"" + str_text + "\"");
|
---|
71 |
|
---|
72 | Color couloir = ColorMapper.getColorByName(color);
|
---|
73 | int width = 1;
|
---|
74 | int height = 1;
|
---|
75 |
|
---|
76 | BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
|
---|
77 | Graphics2D graphics = (Graphics2D) bi.getGraphics();
|
---|
78 | graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
79 | graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
---|
80 | Font f = new Font(font, Font.PLAIN, point);
|
---|
81 | FontMetrics fmetrics = graphics.getFontMetrics(f);
|
---|
82 | height = fmetrics.getMaxAscent() + fmetrics.getMaxDescent();
|
---|
83 | width = fmetrics.stringWidth(str_text);
|
---|
84 |
|
---|
85 |
|
---|
86 | bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
|
---|
87 | graphics = (Graphics2D) bi.getGraphics();
|
---|
88 |
|
---|
89 | graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
90 | graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
---|
91 |
|
---|
92 | graphics.setFont(f);
|
---|
93 | graphics.setColor(couloir);
|
---|
94 | graphics.drawString(str_text, 0, height - fmetrics.getMaxDescent());
|
---|
95 | PlanarImage image = PlanarImage.wrapRenderedImage(bi);
|
---|
96 | return image;
|
---|
97 | }
|
---|
98 | }
|
---|