Home | History | Annotate | Download | only in effects
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      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 com.badlogic.gdx.tools.hiero.unicodefont.effects;
     18 
     19 import java.awt.BasicStroke;
     20 import java.awt.Color;
     21 import java.awt.Graphics2D;
     22 import java.awt.Stroke;
     23 import java.awt.image.BufferedImage;
     24 import java.util.ArrayList;
     25 import java.util.Iterator;
     26 import java.util.List;
     27 
     28 import com.badlogic.gdx.tools.hiero.unicodefont.Glyph;
     29 import com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont;
     30 
     31 /** Strokes glyphs with an outline.
     32  * @author Nathan Sweet */
     33 public class OutlineEffect implements ConfigurableEffect {
     34 	private float width = 2;
     35 	private Color color = Color.black;
     36 	private int join = BasicStroke.JOIN_BEVEL;
     37 	private Stroke stroke;
     38 
     39 	public OutlineEffect () {
     40 	}
     41 
     42 	public OutlineEffect (int width, Color color) {
     43 		this.width = width;
     44 		this.color = color;
     45 	}
     46 
     47 	public void draw (BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
     48 		g = (Graphics2D)g.create();
     49 		if (stroke != null)
     50 			g.setStroke(stroke);
     51 		else
     52 			g.setStroke(getStroke());
     53 		g.setColor(color);
     54 		g.draw(glyph.getShape());
     55 		g.dispose();
     56 	}
     57 
     58 	public float getWidth () {
     59 		return width;
     60 	}
     61 
     62 	/** Sets the width of the outline. The glyphs will need padding so the outline doesn't get clipped. */
     63 	public void setWidth (int width) {
     64 		this.width = width;
     65 	}
     66 
     67 	public Color getColor () {
     68 		return color;
     69 	}
     70 
     71 	public void setColor (Color color) {
     72 		this.color = color;
     73 	}
     74 
     75 	public int getJoin () {
     76 		return join;
     77 	}
     78 
     79 	public Stroke getStroke () {
     80 		if (stroke == null) return new BasicStroke(width, BasicStroke.CAP_SQUARE, join);
     81 		return stroke;
     82 	}
     83 
     84 	/** Sets the stroke to use for the outline. If this is set, the other outline settings are ignored. */
     85 	public void setStroke (Stroke stroke) {
     86 		this.stroke = stroke;
     87 	}
     88 
     89 	/** Sets how the corners of the outline are drawn. This is usually only noticeable at large outline widths.
     90 	 * @param join One of: {@link BasicStroke#JOIN_BEVEL}, {@link BasicStroke#JOIN_MITER}, {@link BasicStroke#JOIN_ROUND} */
     91 	public void setJoin (int join) {
     92 		this.join = join;
     93 	}
     94 
     95 	public String toString () {
     96 		return "Outline";
     97 	}
     98 
     99 	public List getValues () {
    100 		List values = new ArrayList();
    101 		values.add(EffectUtil.colorValue("Color", color));
    102 		values.add(EffectUtil.floatValue("Width", width, 0.1f, 999, "This setting controls the width of the outline. "
    103 			+ "The glyphs will need padding so the outline doesn't get clipped."));
    104 		values.add(EffectUtil.optionValue("Join", String.valueOf(join), new String[][] { {"Bevel", BasicStroke.JOIN_BEVEL + ""},
    105 			{"Miter", BasicStroke.JOIN_MITER + ""}, {"Round", BasicStroke.JOIN_ROUND + ""}},
    106 			"This setting defines how the corners of the outline are drawn. "
    107 				+ "This is usually only noticeable at large outline widths."));
    108 		return values;
    109 	}
    110 
    111 	public void setValues (List values) {
    112 		for (Iterator iter = values.iterator(); iter.hasNext();) {
    113 			Value value = (Value)iter.next();
    114 			if (value.getName().equals("Color")) {
    115 				color = (Color)value.getObject();
    116 			} else if (value.getName().equals("Width")) {
    117 				width = ((Float)value.getObject()).floatValue();
    118 			} else if (value.getName().equals("Join")) {
    119 				join = Integer.parseInt((String)value.getObject());
    120 			}
    121 		}
    122 	}
    123 }
    124