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
17package com.badlogic.gdx.tests;
18
19import com.badlogic.gdx.Gdx;
20import com.badlogic.gdx.graphics.Color;
21import com.badlogic.gdx.graphics.GL20;
22import com.badlogic.gdx.graphics.g2d.BitmapFont;
23import com.badlogic.gdx.graphics.g2d.GlyphLayout;
24import com.badlogic.gdx.graphics.g2d.SpriteBatch;
25import com.badlogic.gdx.graphics.g2d.TextureAtlas;
26import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
27import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
28import com.badlogic.gdx.tests.utils.GdxTest;
29
30public class BitmapFontMetricsTest extends GdxTest {
31	private SpriteBatch spriteBatch;
32	private TextureAtlas atlas;
33	private BitmapFont font, smallFont;
34	private ShapeRenderer renderer;
35
36	@Override
37	public void create () {
38		spriteBatch = new SpriteBatch();
39		atlas = new TextureAtlas("data/pack");
40		smallFont = new BitmapFont();
41		font = new BitmapFont(Gdx.files.internal("data/verdana39.fnt"), atlas.findRegion("verdana39"), false);
42		font = new BitmapFont(Gdx.files.internal("data/arial-32-pad.fnt"), false);
43		renderer = new ShapeRenderer();
44		renderer.setProjectionMatrix(spriteBatch.getProjectionMatrix());
45	}
46
47	@Override
48	public void render () {
49		// red.a = (red.a + Gdx.graphics.getDeltaTime() * 0.1f) % 1;
50
51		int viewHeight = Gdx.graphics.getHeight();
52
53		Gdx.gl.glClearColor(1, 1, 1, 1);
54		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
55		spriteBatch.begin();
56
57		// String text = "Sphinx of black quartz, judge my vow.";
58		String text = "Sphinx of black quartz.";
59		font.setColor(Color.RED);
60
61		float x = 100, y = 100;
62		float alignmentWidth;
63
64		smallFont.setColor(Color.BLACK);
65		smallFont.draw(spriteBatch, "draw position", 20, viewHeight - 0);
66		smallFont.setColor(Color.BLUE);
67		smallFont.draw(spriteBatch, "bounds", 20, viewHeight - 20);
68		smallFont.setColor(Color.MAGENTA);
69		smallFont.draw(spriteBatch, "baseline", 20, viewHeight - 40);
70		smallFont.setColor(Color.GREEN);
71		smallFont.draw(spriteBatch, "x height", 20, viewHeight - 60);
72		smallFont.setColor(Color.CYAN);
73		smallFont.draw(spriteBatch, "ascent", 20, viewHeight - 80);
74		smallFont.setColor(Color.RED);
75		smallFont.draw(spriteBatch, "descent", 20, viewHeight - 100);
76		smallFont.setColor(Color.ORANGE);
77		smallFont.draw(spriteBatch, "line height", 20, viewHeight - 120);
78		smallFont.setColor(Color.LIGHT_GRAY);
79		smallFont.draw(spriteBatch, "cap height", 20, viewHeight - 140);
80
81		font.setColor(Color.BLACK);
82		GlyphLayout layout = font.draw(spriteBatch, text, x, y);
83
84		spriteBatch.end();
85
86		renderer.begin(ShapeType.Filled);
87		renderer.setColor(Color.BLACK);
88		renderer.rect(x - 3, y - 3, 6, 6);
89		renderer.end();
90
91		float baseline = y - font.getCapHeight();
92		renderer.begin(ShapeType.Line);
93		renderer.setColor(Color.LIGHT_GRAY);
94		renderer.line(0, y, 9999, y);
95		renderer.setColor(Color.MAGENTA);
96		renderer.line(0, baseline, 9999, baseline);
97		renderer.setColor(Color.GREEN);
98		renderer.line(0, baseline + font.getXHeight(), 9999, baseline + font.getXHeight());
99		renderer.setColor(Color.CYAN);
100		renderer.line(0, y + font.getAscent(), 9999, y + font.getAscent());
101		renderer.setColor(Color.RED);
102		renderer.line(0, baseline + font.getDescent(), 9999, baseline + font.getDescent());
103		renderer.setColor(Color.ORANGE);
104		renderer.line(0, y - font.getLineHeight(), 9999, y - font.getLineHeight());
105		renderer.end();
106
107		renderer.begin(ShapeType.Line);
108		renderer.setColor(Color.BLUE);
109		renderer.rect(x, y, layout.width, -layout.height);
110		renderer.end();
111	}
112
113	@Override
114	public void dispose () {
115		spriteBatch.dispose();
116		renderer.dispose();
117		font.dispose();
118		atlas.dispose();
119	}
120}
121