Loading a Font

Published — Edited

Fonts, when working with VTerminal, consist of a PNG and FNT file. The PNG is a large sprite sheet containing every character of the font and the FNT file describes the position of each character in the PNG. When loading a font, VTerminal requires both files along with a scale value that can be used to resize the font as it is loaded.

Each of the following functions, of the FontLoader class, can be used to load a font.

loadFont(String, double)

This function automatically searches for a PNG and FNT file in the given folder on the local filesystem, then loads the font using the found files.

import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.font.Font;
import com.valkryst.VTerminal.font.FontLoader;
import java.io.IOException;
public class Driver {
public static void main(final String[] args) throws IOException {
final Font font = FontLoader.loadFont("C:/Users/Valkryst/Programming/Java/VTerminal/res/Fonts/DejaVu Sans Mono/18pt/", 1);
final Screen screen = new Screen(font);
screen.addCanvasToFrame();
for (int y = 0 ; y < screen.getHeight() ; y++) {
for (int x = 0 ; x < screen.getWidth() ; x++) {
screen.getTileAt(x, y).setCharacter('X');
}
}
screen.draw();
}
}

loadFont(String, String, double)

This function loads a font from the local filesystem using the given PNG and FNT files.

import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.font.Font;
import com.valkryst.VTerminal.font.FontLoader;
import java.io.IOException;
public class Driver {
public static void main(final String[] args) throws IOException {
final Font font = FontLoader.loadFont("C:/Users/Valkryst/Programming/Java/VTerminal/res/Fonts/DejaVu Sans Mono/18pt/bitmap.png", "C:/Users/Valkryst/Programming/Java/VTerminal/res/Fonts/DejaVu Sans Mono/18pt/data.fnt", 1);
final Screen screen = new Screen(font);
screen.addCanvasToFrame();
for (int y = 0 ; y < screen.getHeight() ; y++) {
for (int x = 0 ; x < screen.getWidth() ; x++) {
screen.getTileAt(x, y).setCharacter('X');
}
}
screen.draw();
}
}

loadFontFromJar(String, double)

This function automatically searches for a PNG and FNT file in the given folder within the Jar file, then loads the font using the found files.

import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.font.Font;
import com.valkryst.VTerminal.font.FontLoader;
import java.io.IOException;
public class Driver {
public static void main(final String[] args) throws IOException {
final Font font = FontLoader.loadFontFromJar("Fonts/DejaVu Sans Mono/18pt/", 1);
final Screen screen = new Screen(font);
screen.addCanvasToFrame();
for (int y = 0 ; y < screen.getHeight() ; y++) {
for (int x = 0 ; x < screen.getWidth() ; x++) {
screen.getTileAt(x, y).setCharacter('X');
}
}
screen.draw();
}
}

loadFontFromJar(String, String, double)

This function loads a font from within the Jar file using the given PNG and FNT files.

import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.font.Font;
import com.valkryst.VTerminal.font.FontLoader;
import java.io.IOException;
public class Driver {
public static void main(final String[] args) throws IOException {
final Font font = FontLoader.loadFontFromJar("Fonts/DejaVu Sans Mono/18pt/bitmap.png", "Fonts/DejaVu Sans Mono/18pt/data.fnt", 1);
final Screen screen = new Screen(font);
screen.addCanvasToFrame();
for (int y = 0 ; y < screen.getHeight() ; y++) {
for (int x = 0 ; x < screen.getWidth() ; x++) {
screen.getTileAt(x, y).setCharacter('X');
}
}
screen.draw();
}
}