001 /* 002 * 003 004 Licensed under the Apache License, Version 2.0 (the "License"); 005 you may not use this file except in compliance with the License. 006 You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010 Unless required by applicable law or agreed to in writing, software 011 distributed under the License is distributed on an "AS IS" BASIS, 012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 See the License for the specific language governing permissions and 014 limitations under the License. 015 */ 016 017 package railo.runtime.img.filter;import java.awt.Composite; 018 import java.awt.Font; 019 import java.awt.Graphics2D; 020 import java.awt.Paint; 021 import java.awt.RenderingHints; 022 import java.awt.geom.AffineTransform; 023 import java.awt.image.BufferedImage; 024 025 import railo.runtime.engine.ThreadLocalPageContext; 026 import railo.runtime.exp.FunctionException; 027 import railo.runtime.exp.PageException; 028 import railo.runtime.img.ImageUtil; 029 import railo.runtime.type.KeyImpl; 030 import railo.runtime.type.List; 031 import railo.runtime.type.Struct; 032 033 /** 034 * A filter which renders text onto an image. 035 */ 036 public class RenderTextFilter extends AbstractBufferedImageOp implements DynFiltering { 037 038 private String text; 039 private Font font; 040 private Paint paint; 041 private Composite composite; 042 private AffineTransform transform; 043 044 /** 045 * Construct a RenderTextFilter. 046 */ 047 public RenderTextFilter() { 048 } 049 050 /** 051 * Construct a RenderTextFilter. 052 * @param text the text 053 * @param font the font to use (may be null) 054 * @param paint the paint (may be null) 055 * @param composite the composite (may be null) 056 * @param transform the transform (may be null) 057 */ 058 public RenderTextFilter( String text, Font font, Paint paint, Composite composite, AffineTransform transform ) { 059 this.text = text; 060 this.font = font; 061 this.composite = composite; 062 this.paint = paint; 063 this.transform = transform; 064 } 065 066 /** 067 * Set the text to paint. 068 * @param text the text 069 * @see #getText 070 */ 071 public void setText( String text ) { 072 this.text = text; 073 } 074 075 /** 076 * Get the text to paint. 077 * @return the text 078 * @see #setText 079 */ 080 public String getText() { 081 return text; 082 } 083 084 /** 085 * Set the composite with which to paint the text. 086 * @param composite the composite 087 * @see #getComposite 088 */ 089 public void setComposite( Composite composite ) { 090 this.composite = composite; 091 } 092 093 /** 094 * Get the composite with which to paint the text. 095 * @return the composite 096 * @see #setComposite 097 */ 098 public Composite getComposite() { 099 return composite; 100 } 101 102 /** 103 * Set the paint with which to paint the text. 104 * @param paint the paint 105 * @see #getPaint 106 */ 107 public void setPaint( Paint paint ) { 108 this.paint = paint; 109 } 110 111 /** 112 * Get the paint with which to paint the text. 113 * @return the paint 114 * @see #setPaint 115 */ 116 public Paint getPaint() { 117 return paint; 118 } 119 120 /** 121 * Set the font with which to paint the text. 122 * @param font the font 123 * @see #getFont 124 */ 125 public void setFont( Font font ) { 126 this.font = font; 127 } 128 129 /** 130 * Get the font with which to paint the text. 131 * @return the font 132 * @see #setFont 133 */ 134 public Font getFont() { 135 return font; 136 } 137 138 /** 139 * Set the transform with which to paint the text. 140 * @param transform the transform 141 * @see #getTransform 142 */ 143 public void setTransform( AffineTransform transform ) { 144 this.transform = transform; 145 } 146 147 /** 148 * Get the transform with which to paint the text. 149 * @return the transform 150 * @see #setTransform 151 */ 152 public AffineTransform getTransform() { 153 return transform; 154 } 155 156 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 157 if ( dst == null ) 158 dst = createCompatibleDestImage( src, null ); 159 160 Graphics2D g = dst.createGraphics(); 161 g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON ); 162 if ( font != null ) 163 g.setFont( font ); 164 if ( transform != null ) 165 g.setTransform( transform ); 166 if ( composite != null ) 167 g.setComposite( composite ); 168 if ( paint != null ) 169 g.setPaint( paint ); 170 if ( text != null ) 171 g.drawString( text, 10, 100 ); 172 g.dispose(); 173 return dst; 174 } 175 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 176 Object o; 177 if((o=parameters.removeEL(KeyImpl.init("Font")))!=null)setFont(ImageFilterUtil.toFont(o,"Font")); 178 if((o=parameters.removeEL(KeyImpl.init("Transform")))!=null)setTransform(ImageFilterUtil.toAffineTransform(o,"Transform")); 179 if((o=parameters.removeEL(KeyImpl.init("Composite")))!=null)setComposite(ImageFilterUtil.toComposite(o,"Composite")); 180 if((o=parameters.removeEL(KeyImpl.init("Paint")))!=null)setPaint(ImageFilterUtil.toColor(o,"Paint")); 181 if((o=parameters.removeEL(KeyImpl.init("Text")))!=null)setText(ImageFilterUtil.toString(o,"Text")); 182 183 // check for arguments not supported 184 if(parameters.size()>0) { 185 throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "the parameter"+(parameters.size()>1?"s":"")+" ["+List.arrayToList(parameters.keysAsString(),", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [Font, Transform, Composite, Paint, Text]"); 186 } 187 188 return filter(src, dst); 189 } 190 }