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