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. import javax.media.jai.PlanarImage;
  19. import java.awt.Color;
  20. import java.awt.Font;
  21. import java.awt.FontMetrics;
  22. import java.awt.Graphics2D;
  23. import java.awt.RenderingHints;
  24. import java.awt.image.BufferedImage;
  25. /**
  26. *
  27. * @see org.apache.tools.ant.taskdefs.optional.image.Image
  28. */
  29. public class Text extends ImageOperation implements DrawOperation {
  30. private String str_text = "";
  31. private String font = "Arial";
  32. private int point = 10;
  33. private boolean bold = false;
  34. private boolean italic = false;
  35. private String color = "black";
  36. public void setString(String str) {
  37. str_text = str;
  38. }
  39. public void setFont(String f) {
  40. font = f;
  41. }
  42. public void setPoint(String p) {
  43. point = Integer.parseInt(p);
  44. }
  45. public void setColor(String c) {
  46. color = c;
  47. }
  48. /**
  49. * @todo is this used?
  50. */
  51. public void setBold(boolean state) {
  52. bold = state;
  53. }
  54. /**
  55. * @todo is this used?
  56. */
  57. public void setItalic(boolean state) {
  58. italic = state;
  59. }
  60. public PlanarImage executeDrawOperation() {
  61. log("\tCreating Text \"" + str_text + "\"");
  62. Color couloir = ColorMapper.getColorByName(color);
  63. int width = 1;
  64. int height = 1;
  65. BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
  66. Graphics2D graphics = (Graphics2D) bi.getGraphics();
  67. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  68. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  69. Font f = new Font(font, Font.PLAIN, point);
  70. FontMetrics fmetrics = graphics.getFontMetrics(f);
  71. height = fmetrics.getMaxAscent() + fmetrics.getMaxDescent();
  72. width = fmetrics.stringWidth(str_text);
  73. bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
  74. graphics = (Graphics2D) bi.getGraphics();
  75. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  76. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  77. graphics.setFont(f);
  78. graphics.setColor(couloir);
  79. graphics.drawString(str_text, 0, height - fmetrics.getMaxDescent());
  80. PlanarImage image = PlanarImage.wrapRenderedImage(bi);
  81. return image;
  82. }
  83. }