001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.image;
018
019import java.awt.*;
020import java.awt.geom.*;
021import java.awt.image.*;
022
023/**
024 * A filter which renders text onto an image.
025 */
026public class RenderTextFilter extends AbstractBufferedImageOp {
027
028        private String text;
029        private Font font;
030    private Paint paint;
031        private Composite composite;
032    private AffineTransform transform;
033        
034        /**
035     * Construct a RenderTextFilter.
036     */
037    public RenderTextFilter() {
038        }
039        
040        /**
041     * Construct a RenderTextFilter.
042     * @param text the text
043     * @param font the font to use (may be null)
044     * @param paint the paint (may be null)
045     * @param composite the composite (may be null)
046     * @param transform the transform (may be null)
047     */
048        public RenderTextFilter( String text, Font font, Paint paint, Composite composite, AffineTransform transform ) {
049                this.text = text;
050                this.font = font;
051                this.composite = composite;
052                this.paint = paint;
053                this.transform = transform;
054        }
055        
056        /**
057     * Set the text to paint.
058     * @param text the text
059     * @see #getText
060     */
061        public void setText( String text ) {
062                this.text = text;
063        }
064    
065        /**
066     * Get the text to paint.
067     * @return the text
068     * @see #setText
069     */
070    public String getText() {
071        return text;
072    }
073        
074        /**
075     * Set the composite with which to paint the text.
076     * @param composite the composite
077     * @see #getComposite
078     */
079        public void setComposite( Composite composite ) {
080                this.composite = composite;
081        }
082    
083        /**
084     * Get the composite with which to paint the text.
085     * @return the composite
086     * @see #setComposite
087     */
088    public Composite getComposite() {
089        return composite;
090    }
091        
092        /**
093     * Set the paint with which to paint the text.
094     * @param paint the paint
095     * @see #getPaint
096     */
097        public void setPaint( Paint paint ) {
098                this.paint = paint;
099        }
100    
101        /**
102     * Get the paint with which to paint the text.
103     * @return the paint
104     * @see #setPaint
105     */
106    public Paint getPaint() {
107        return paint;
108    }
109        
110        /**
111     * Set the font with which to paint the text.
112     * @param font the font
113     * @see #getFont
114     */
115        public void setFont( Font font ) {
116                this.font = font;
117        }
118    
119        /**
120     * Get the font with which to paint the text.
121     * @return the font
122     * @see #setFont
123     */
124    public Font getFont() {
125        return font;
126    }
127        
128        /**
129     * Set the transform with which to paint the text.
130     * @param transform the transform
131     * @see #getTransform
132     */
133        public void setTransform( AffineTransform transform ) {
134                this.transform = transform;
135        }
136    
137        /**
138     * Get the transform with which to paint the text.
139     * @return the transform
140     * @see #setTransform
141     */
142    public AffineTransform getTransform() {
143        return transform;
144    }
145        
146        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
147        if ( dst == null )
148            dst = createCompatibleDestImage( src, null );
149
150                Graphics2D g = dst.createGraphics();
151        g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
152        if ( font != null )
153            g.setFont( font );
154        if ( transform != null )
155            g.setTransform( transform );
156        if ( composite != null )
157            g.setComposite( composite );
158        if ( paint != null )
159            g.setPaint( paint );
160        if ( text != null )
161            g.drawString( text, 10, 100 );
162        g.dispose();
163                return dst;
164        }
165}