use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.
the class FreeTypeAtlasTest method createFonts.
protected int createFonts() {
// This test uses a less efficient but more convenient way to pack multiple generated fonts into a single
// texture atlas.
//
// 1. Create a new PixmapPacker big enough to fit all your desired glyphs
// 2. Create a new FreeTypeFontGenerator for each TTF file (i.e. font styles/families)
// 3. For each size and style, call generator.generateFont() with the packer set on the parameter
// 4. Generate the texture atlas using packer.generateTextureAtlas or packer.updateTextureAtlas.
// 5. Dispose of the atlas upon application exit or when you are done using the fonts
// //////////////////////////////////////////////////////////////////////////////////////////////////////
// create the pixmap packer
packer = new PixmapPacker(FONT_ATLAS_WIDTH, FONT_ATLAS_HEIGHT, Format.RGBA8888, 2, false);
fontMap = new FontMap<BitmapFont>();
int fontCount = 0;
// for each style...
for (FontStyle style : FontStyle.values()) {
// get the file for this style
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal(style.path));
// For each size...
for (FontSize size : FontSize.values()) {
// pack the glyphs into the atlas using the default chars
FreeTypeFontGenerator.FreeTypeFontParameter fontParameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
fontParameter.size = size.size;
fontParameter.packer = packer;
fontParameter.characters = CHARACTERS;
BitmapFont bmFont = gen.generateFont(fontParameter);
fontMap.get(style).put(size, bmFont);
fontCount++;
}
// dispose of the generator once we're finished with this family
gen.dispose();
}
// for the demo, show how many glyphs we loaded
return fontCount * CHARACTERS.length();
}
use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.
the class DebugDrawer method draw3dText.
@Override
public void draw3dText(Vector3 location, String textString) {
if (spriteBatch == null) {
spriteBatch = new SpriteBatch();
}
if (font == null) {
font = new BitmapFont();
}
// this check is necessary to avoid "mirrored" instances of the text
if (camera.frustum.pointInFrustum(location)) {
if (viewport != null) {
camera.project(location, viewport.getScreenX(), viewport.getScreenY(), viewport.getScreenWidth(), viewport.getScreenHeight());
} else {
camera.project(location);
}
shapeRenderer.end();
spriteBatch.begin();
// the text will be centered on the position
font.draw(spriteBatch, textString, location.x, location.y, 0, textString.length(), 0, Align.center, false);
spriteBatch.end();
shapeRenderer.begin(ShapeType.Line);
}
}
use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.
the class Label method scaleAndComputePrefSize.
private void scaleAndComputePrefSize() {
BitmapFont font = cache.getFont();
float oldScaleX = font.getScaleX();
float oldScaleY = font.getScaleY();
if (fontScaleChanged)
font.getData().setScale(fontScaleX, fontScaleY);
computePrefSize();
if (fontScaleChanged)
font.getData().setScale(oldScaleX, oldScaleY);
}
use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.
the class List method layout.
public void layout() {
final BitmapFont font = style.font;
final Drawable selectedDrawable = style.selection;
itemHeight = font.getCapHeight() - font.getDescent() * 2;
itemHeight += selectedDrawable.getTopHeight() + selectedDrawable.getBottomHeight();
textOffsetX = selectedDrawable.getLeftWidth();
textOffsetY = selectedDrawable.getTopHeight() - font.getDescent();
prefWidth = 0;
Pool<GlyphLayout> layoutPool = Pools.get(GlyphLayout.class);
GlyphLayout layout = layoutPool.obtain();
for (int i = 0; i < items.size; i++) {
layout.setText(font, toString(items.get(i)));
prefWidth = Math.max(layout.width, prefWidth);
}
layoutPool.free(layout);
prefWidth += selectedDrawable.getLeftWidth() + selectedDrawable.getRightWidth();
prefHeight = items.size * itemHeight;
Drawable background = style.background;
if (background != null) {
prefWidth += background.getLeftWidth() + background.getRightWidth();
prefHeight += background.getTopHeight() + background.getBottomHeight();
}
}
use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.
the class TextField method updateDisplayText.
void updateDisplayText() {
BitmapFont font = style.font;
BitmapFontData data = font.getData();
String text = this.text;
int textLength = text.length();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < textLength; i++) {
char c = text.charAt(i);
buffer.append(data.hasGlyph(c) ? c : ' ');
}
String newDisplayText = buffer.toString();
if (passwordMode && data.hasGlyph(passwordCharacter)) {
if (passwordBuffer == null)
passwordBuffer = new StringBuilder(newDisplayText.length());
if (passwordBuffer.length() > textLength)
passwordBuffer.setLength(textLength);
else {
for (int i = passwordBuffer.length(); i < textLength; i++) passwordBuffer.append(passwordCharacter);
}
displayText = passwordBuffer;
} else
displayText = newDisplayText;
layout.setText(font, displayText);
glyphPositions.clear();
float x = 0;
if (layout.runs.size > 0) {
GlyphRun run = layout.runs.first();
FloatArray xAdvances = run.xAdvances;
fontOffset = xAdvances.first();
for (int i = 1, n = xAdvances.size; i < n; i++) {
glyphPositions.add(x);
x += xAdvances.get(i);
}
} else
fontOffset = 0;
glyphPositions.add(x);
if (selectionStart > newDisplayText.length())
selectionStart = textLength;
}
Aggregations