gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src_libs/com/badlogic/gdx/tools/hiero/unicodefont/effects/DistanceFieldEffect.java
a62d533 1
/*******************************************************************************
a62d533 2
 * Copyright 2011 See AUTHORS file.
a62d533 3
 * 
a62d533 4
 * Licensed under the Apache License, Version 2.0 (the "License");
a62d533 5
 * you may not use this file except in compliance with the License.
a62d533 6
 * You may obtain a copy of the License at
a62d533 7
 * 
a62d533 8
 *   http://www.apache.org/licenses/LICENSE-2.0
a62d533 9
 * 
a62d533 10
 * Unless required by applicable law or agreed to in writing, software
a62d533 11
 * distributed under the License is distributed on an "AS IS" BASIS,
a62d533 12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
a62d533 13
 * See the License for the specific language governing permissions and
a62d533 14
 * limitations under the License.
a62d533 15
 ******************************************************************************/
a62d533 16
package com.badlogic.gdx.tools.hiero.unicodefont.effects;
a62d533 17
a62d533 18
import java.awt.Color;
a62d533 19
import java.awt.Graphics2D;
a62d533 20
import java.awt.RenderingHints;
a62d533 21
import java.awt.geom.AffineTransform;
a62d533 22
import java.awt.image.BufferedImage;
a62d533 23
import java.util.ArrayList;
a62d533 24
import java.util.Iterator;
a62d533 25
import java.util.List;
a62d533 26
a62d533 27
import com.badlogic.gdx.tools.hiero.unicodefont.Glyph;
a62d533 28
import com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont;
a62d533 29
a62d533 30
/**
a62d533 31
 * A filter to create a distance field. The resulting font can be rendered
a62d533 32
 * with a simple custom shader to draw bitmap fonts that remain crisp
a62d533 33
 * even under high magnification.
a62d533 34
 * 
a62d533 35
 * <p> An example of the use of such a font is included in the libgdx test suite
a62d533 36
 * under the name {@code BitmapFontDistanceFieldTest}.
a62d533 37
 * 
a62d533 38
 * @see DistanceFieldGenerator
a62d533 39
 * 
a62d533 40
 * @author Thomas ten Cate
a62d533 41
 */
a62d533 42
public class DistanceFieldEffect implements ConfigurableEffect
a62d533 43
{
a62d533 44
	private Color color = Color.WHITE;
a62d533 45
	private int scale = 1;
a62d533 46
	private float spread = 1;
a62d533 47
a62d533 48
	/**
a62d533 49
	 * Draws the glyph to the given image, upscaled by a factor of {@link #scale}.
a62d533 50
	 * 
a62d533 51
	 * @param image the image to draw to
a62d533 52
	 * @param glyph the glyph to draw
a62d533 53
	 */
a62d533 54
	private void drawGlyph(BufferedImage image, Glyph glyph) {
a62d533 55
		Graphics2D inputG = (Graphics2D) image.getGraphics();
a62d533 56
		inputG.setTransform(AffineTransform.getScaleInstance(scale, scale));
a62d533 57
		// We don't really want anti-aliasing (we'll discard it anyway),
a62d533 58
		// but accurate positioning might improve the result slightly
a62d533 59
		inputG.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
a62d533 60
		inputG.setColor(Color.WHITE);
a62d533 61
		inputG.fill(glyph.getShape());
a62d533 62
	}
a62d533 63
a62d533 64
	@Override
a62d533 65
	public void draw(BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
a62d533 66
		BufferedImage input = new BufferedImage(
a62d533 67
			scale * glyph.getWidth(),
a62d533 68
			scale * glyph.getHeight(),
a62d533 69
			BufferedImage.TYPE_BYTE_BINARY);
a62d533 70
		drawGlyph(input, glyph);
a62d533 71
		
a62d533 72
		DistanceFieldGenerator generator = new DistanceFieldGenerator();
a62d533 73
		generator.setColor(color);
a62d533 74
		generator.setDownscale(scale);
a62d533 75
		// We multiply spread by the scale, so that changing scale will only affect accuracy
a62d533 76
		// and not spread in the output image.
a62d533 77
		generator.setSpread(scale * spread);
a62d533 78
		BufferedImage distanceField = generator.generateDistanceField(input);
a62d533 79
		
a62d533 80
		g.drawImage(distanceField, new AffineTransform(), null);
a62d533 81
	}
a62d533 82
	
a62d533 83
	@Override
a62d533 84
	public String toString() {
a62d533 85
		return "Distance field";
a62d533 86
	}
a62d533 87
a62d533 88
	@Override
a62d533 89
	public List getValues() {
a62d533 90
		List values = new ArrayList();
a62d533 91
		values.add(EffectUtil.colorValue("Color", color));
a62d533 92
		values.add(EffectUtil.intValue("Scale", scale, "The distance field is computed from an image larger than the output glyph by this factor. Set this to a higher value for more accuracy, but slower font generation."));
a62d533 93
		values.add(EffectUtil.floatValue("Spread", spread, 1.0f, Float.MAX_VALUE, "The maximum distance from edges where the effect of the distance field is seen. Set this to about half the width of lines in your output font."));
a62d533 94
		return values;
a62d533 95
	}
a62d533 96
a62d533 97
	@Override
a62d533 98
	public void setValues(List values) {
a62d533 99
		for (Iterator iter = values.iterator(); iter.hasNext();) {
a62d533 100
			Value value = (Value)iter.next();
a62d533 101
			if ("Color".equals(value.getName())) {
a62d533 102
				color = (Color)value.getObject();
a62d533 103
			} else if ("Scale".equals(value.getName())) {
a62d533 104
				scale = Math.max(1, (Integer)value.getObject());
a62d533 105
			} else if ("Spread".equals(value.getName())) {
a62d533 106
				spread = Math.max(0, (Float)value.getObject());
a62d533 107
			}
a62d533 108
		}
a62d533 109
		
a62d533 110
	}
a62d533 111
}