org.newdawn.slick.UnicodeFont Java Examples

The following examples show how to use org.newdawn.slick.UnicodeFont. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UnicodeFontTest.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
	 * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
	 */
	public void init(GameContainer container) throws SlickException {
		container.setShowFPS(false);

		// unicodeFont = new UnicodeFont(Font.decode("Arial Unicode MS"), 25, false, false);
		unicodeFont = new UnicodeFont("testdata/Lato-Thin.ttf", 48, false, false);
//		unicodeFont.setPaddingBottom(10);
//		unicodeFont.setPaddingRight(10);
//		unicodeFont.setPaddingAdvanceX(-10);
//		unicodeFont.getEffects().add(new ShadowEffect(java.awt.Color.black, 5, 5, 0.5f));
		unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.white));

		// unicodeFont = new UnicodeFont("Arial", 25, false, false);
		// unicodeFont = new UnicodeFont("Everson Mono", 44, false, false);

		// font.addGlyphs(0, 255);
		// font.addGlyphs("~!@#$%^&*()");

		container.getGraphics().setBackground(Color.darkGray);
	}
 
Example #2
Source File: Fonts.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Initializes all fonts.
 * @throws SlickException if ASCII glyphs could not be loaded
 * @throws FontFormatException if any font stream data does not contain the required font tables
 * @throws IOException if a font stream cannot be completely read
 */
public static void init() throws SlickException, FontFormatException, IOException {
	float fontBase = 12f * GameImage.getUIscale();
	Font javaFont = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(Constants.FONT_NAME));
	Font font = javaFont.deriveFont(Font.PLAIN, (int) (fontBase * 4 / 3));
	DEFAULT = new UnicodeFont(font);
	BOLD = new UnicodeFont(font.deriveFont(Font.BOLD));
	XLARGE = new UnicodeFont(font.deriveFont(fontBase * 3));
	LARGE = new UnicodeFont(font.deriveFont(fontBase * 2));
	MEDIUM = new UnicodeFont(font.deriveFont(fontBase * 3 / 2));
	MEDIUMBOLD = new UnicodeFont(font.deriveFont(Font.BOLD, fontBase * 3 / 2));
	SMALL = new UnicodeFont(font.deriveFont(fontBase));
	SMALLBOLD = new UnicodeFont(font.deriveFont(Font.BOLD, fontBase));
	ColorEffect colorEffect = new ColorEffect();
	loadFont(DEFAULT, colorEffect);
	loadFont(BOLD, colorEffect);
	loadFont(XLARGE, colorEffect);
	loadFont(LARGE, colorEffect);
	loadFont(MEDIUM, colorEffect);
	loadFont(MEDIUMBOLD, colorEffect);
	loadFont(SMALL, colorEffect);
	loadFont(SMALLBOLD, colorEffect);
}
 
Example #3
Source File: Fonts.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds and loads glyphs for a font.
 * @param font the font to add the glyphs to
 * @param s the string containing the glyphs to load
 */
public static void loadGlyphs(UnicodeFont font, String s) {
	if (s == null || s.isEmpty())
		return;

	// get set of added strings
	HashSet<String> set = loadedGlyphs.get(font);
	if (set == null) {
		set = new HashSet<String>();
		loadedGlyphs.put(font, set);
	} else if (set.contains(s))
		return;  // string already in set

	// load glyphs
	font.addGlyphs(s);
	set.add(s);
	try {
		font.loadGlyphs();
	} catch (SlickException e) {
		Log.warn(String.format("Failed to load glyphs for string '%s'.", s), e);
	}
}
 
Example #4
Source File: Fonts.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds and loads glyphs for a font.
 * @param font the font to add the glyphs to
 * @param s the string containing the glyphs to load
 */
public static void loadGlyphs(UnicodeFont font, String s) {
	if (s == null || s.isEmpty())
		return;

	// get set of added strings
	HashSet<String> set = loadedGlyphs.get(font);
	if (set == null) {
		set = new HashSet<String>();
		loadedGlyphs.put(font, set);
	} else if (set.contains(s))
		return;  // string already in set

	// load glyphs
	font.addGlyphs(s);
	set.add(s);
	try {
		font.loadGlyphs();
	} catch (SlickException e) {
		Log.warn(String.format("Failed to load glyphs for string '%s'.", s), e);
	}
}
 
Example #5
Source File: HyperiumFontRenderer.java    From Hyperium with GNU Lesser General Public License v3.0 5 votes vote down vote up
public HyperiumFontRenderer(String fontName, float fontSize) {
    name = fontName;
    size = fontSize;
    ScaledResolution resolution = new ScaledResolution(Minecraft.getMinecraft());

    try {
        prevScaleFactor = resolution.getScaleFactor();
        unicodeFont = new UnicodeFont(getFontByName(fontName).deriveFont(fontSize * prevScaleFactor / 2));
        unicodeFont.addAsciiGlyphs();
        unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
        unicodeFont.loadGlyphs();
    } catch (FontFormatException | IOException | SlickException e) {
        e.printStackTrace();
    }

    this.antiAliasingFactor = resolution.getScaleFactor();
}
 
Example #6
Source File: TextField.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
public TextField(UnicodeFont font, int x, int y, int width, int height) {
	this.font = font;
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
}
 
Example #7
Source File: Fonts.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Loads a Unicode font and its ASCII glyphs.
 * @param font the font to load
 * @param effect the font effect
 * @throws SlickException if the glyphs could not be loaded
 */
@SuppressWarnings("unchecked")
private static void loadFont(UnicodeFont font, Effect effect) throws SlickException {
	font.addAsciiGlyphs();
	font.getEffects().add(effect);
	font.loadGlyphs();
}
 
Example #8
Source File: Fonts.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds and loads glyphs for a font.
 * @param font the font to add the glyphs to
 * @param c the character to load
 */
public static void loadGlyphs(UnicodeFont font, char c)
{
	font.addGlyphs(c, c);
	try {
		font.loadGlyphs();
	} catch (SlickException e) {
		Log.warn(String.format("Failed to load glyphs for codepoint '%d'.", (int) c), e);
	}
}
 
Example #9
Source File: Glyph.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create a new glyph
 * 
 * @param codePoint The code point in which this glyph can be found
 * @param bounds The bounds that this glrph can fill
 * @param vector The vector this glyph is part of
 * @param index The index of this glyph within the vector
 * @param unicodeFont The font this glyph forms part of
 */
public Glyph(int codePoint, Rectangle bounds, GlyphVector vector, int index, UnicodeFont unicodeFont) {
	this.codePoint = codePoint;

	GlyphMetrics metrics = vector.getGlyphMetrics(index);
	int lsb = (int)metrics.getLSB();
	if (lsb > 0) lsb = 0;
	int rsb = (int)metrics.getRSB();
	if (rsb > 0) rsb = 0;

	int glyphWidth = bounds.width - lsb - rsb;
	int glyphHeight = bounds.height;
	if (glyphWidth > 0 && glyphHeight > 0) {
		int padTop = unicodeFont.getPaddingTop();
		int padRight = unicodeFont.getPaddingRight();
		int padBottom = unicodeFont.getPaddingBottom();
		int padLeft = unicodeFont.getPaddingLeft();
		int glyphSpacing = 1; // Needed to prevent filtering problems.
		width = (short)(glyphWidth + padLeft + padRight + glyphSpacing);
		height = (short)(glyphHeight + padTop + padBottom + glyphSpacing);
		yOffset = (short)(unicodeFont.getAscent() + bounds.y - padTop);
	}

	shape = vector.getGlyphOutline(index, -bounds.x + unicodeFont.getPaddingLeft(), -bounds.y + unicodeFont.getPaddingTop());

	isMissing = !unicodeFont.getFont().canDisplay((char)codePoint);
}
 
Example #10
Source File: GradientEffect.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see org.newdawn.slick.font.effects.Effect#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, org.newdawn.slick.UnicodeFont, org.newdawn.slick.font.Glyph)
 */
public void draw(BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
	int ascent = unicodeFont.getAscent();
	float height = (ascent) * scale;
	float top = -glyph.getYOffset() + unicodeFont.getDescent() + offset + ascent / 2 - height / 2;
	g.setPaint(new GradientPaint(0, top, topColor, 0, top + height, bottomColor, cyclic));
	g.fill(glyph.getShape());
}
 
Example #11
Source File: Fonts.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initializes all fonts.
 * @throws SlickException if ASCII glyphs could not be loaded
 * @throws FontFormatException if any font stream data does not contain the required font tables
 * @throws IOException if a font stream cannot be completely read
 */
public static void init() throws SlickException, FontFormatException, IOException {
	float fontBase = 12f * GameImage.getUIscale();
	Font javaFontMain = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(Options.FONT_MAIN));
	Font javaFontBold = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(Options.FONT_BOLD));
	Font javaFontCJK = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream(Options.FONT_CJK));
	Font fontMain = javaFontMain.deriveFont(Font.PLAIN, (int) (fontBase * 4 / 3));
	Font fontBold = javaFontBold.deriveFont(Font.PLAIN, (int) (fontBase * 4 / 3));
	Font fontCJK = javaFontCJK.deriveFont(Font.PLAIN, (int) (fontBase * 4 / 3));
	DEFAULT = new UnicodeFont(fontMain);
	BOLD = new UnicodeFont(fontBold.deriveFont(Font.BOLD));
	XLARGE = new UnicodeFont(fontMain.deriveFont(fontBase * 3));
	LARGE = new UnicodeFont(fontMain.deriveFont(fontBase * 2));
	MEDIUM = new UnicodeFont(fontMain.deriveFont(fontBase * 3 / 2));
	MEDIUMBOLD = new UnicodeFont(fontBold.deriveFont(Font.BOLD, fontBase * 3 / 2));
	SMALL = new UnicodeFont(fontMain.deriveFont(fontBase));
	SMALLBOLD = new UnicodeFont(fontBold.deriveFont(Font.BOLD, fontBase));
	ColorEffect colorEffect = new ColorEffect();
	loadFont(DEFAULT, colorEffect, new UnicodeFont(fontCJK));
	loadFont(BOLD, colorEffect, new UnicodeFont(fontCJK.deriveFont(Font.BOLD)));
	loadFont(XLARGE, colorEffect, new UnicodeFont(fontCJK.deriveFont(fontBase * 3)));
	loadFont(LARGE, colorEffect, new UnicodeFont(fontCJK.deriveFont(fontBase * 2)));
	loadFont(MEDIUM, colorEffect, new UnicodeFont(fontCJK.deriveFont(fontBase * 3 / 2)));
	loadFont(MEDIUMBOLD, colorEffect, new UnicodeFont(fontCJK.deriveFont(Font.BOLD, fontBase * 3 / 2)));
	loadFont(SMALL, colorEffect, new UnicodeFont(fontCJK.deriveFont(fontBase)));
	loadFont(SMALLBOLD, colorEffect, new UnicodeFont(fontCJK.deriveFont(Font.BOLD, fontBase)));
}
 
Example #12
Source File: Fonts.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Loads a Unicode font and its ASCII glyphs.
 * @param font the font to load
 * @param effect the font effect
 * @param backup the backup font
 * @throws SlickException if the glyphs could not be loaded
 */
@SuppressWarnings("unchecked")
private static void loadFont(UnicodeFont font, Effect effect, UnicodeFont backup) throws SlickException {
	font.addBackupFont(backup);
	font.addAsciiGlyphs();
	font.getEffects().add(effect);
	font.loadGlyphs();
}
 
Example #13
Source File: Fonts.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds and loads glyphs for a font.
 * @param font the font to add the glyphs to
 * @param c the character to load
 */
public static void loadGlyphs(UnicodeFont font, char c) {
	font.addGlyphs(c, c);
	try {
		font.loadGlyphs();
	} catch (SlickException e) {
		Log.warn(String.format("Failed to load glyphs for codepoint '%d'.", (int) c), e);
	}
}
 
Example #14
Source File: BMFontUtil.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public BMFontUtil (UnicodeFont unicodeFont) {
	this.unicodeFont = unicodeFont;
}
 
Example #15
Source File: ColorEffect.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @see org.newdawn.slick.font.effects.Effect#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, org.newdawn.slick.UnicodeFont, org.newdawn.slick.font.Glyph)
 */
public void draw(BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
	g.setColor(color);
	g.fill(glyph.getShape());
}
 
Example #16
Source File: FilterEffect.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @see org.newdawn.slick.font.effects.Effect#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, org.newdawn.slick.UnicodeFont, org.newdawn.slick.font.Glyph)
 */
public void draw(BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
	BufferedImage scratchImage = EffectUtil.getScratchImage();
	filter.filter(image, scratchImage);
	image.getGraphics().drawImage(scratchImage, 0, 0, null);
}
 
Example #17
Source File: HyperiumFontRenderer.java    From Hyperium with GNU Lesser General Public License v3.0 4 votes vote down vote up
public UnicodeFont getFont() {
    return unicodeFont;
}
 
Example #18
Source File: HyperiumFontRenderer.java    From Hyperium with GNU Lesser General Public License v3.0 4 votes vote down vote up
public int drawString(String text, float x, float y, int color) {
    if (text == null) return 0;

    ScaledResolution resolution = new ScaledResolution(Minecraft.getMinecraft());

    try {
        if (resolution.getScaleFactor() != prevScaleFactor) {
            prevScaleFactor = resolution.getScaleFactor();
            unicodeFont = new UnicodeFont(getFontByName(name).deriveFont(size * prevScaleFactor / 2));
            unicodeFont.addAsciiGlyphs();
            unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
            unicodeFont.loadGlyphs();
        }
    } catch (FontFormatException | IOException | SlickException e) {
        e.printStackTrace();
    }

    this.antiAliasingFactor = resolution.getScaleFactor();

    GL11.glPushMatrix();
    GlStateManager.scale(1 / antiAliasingFactor, 1 / antiAliasingFactor, 1 / antiAliasingFactor);
    x *= antiAliasingFactor;
    y *= antiAliasingFactor;
    float originalX = x;
    float red = (float) (color >> 16 & 255) / 255.0F;
    float green = (float) (color >> 8 & 255) / 255.0F;
    float blue = (float) (color & 255) / 255.0F;
    float alpha = (float) (color >> 24 & 255) / 255.0F;
    GlStateManager.color(red, green, blue, alpha);

    int currentColor = color;

    char[] characters = text.toCharArray();

    GlStateManager.disableLighting();
    GlStateManager.enableBlend();
    GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
    GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    String[] parts = COLOR_CODE_PATTERN.split(text);
    int index = 0;
    for (String s : parts) {
        for (String s2 : s.split("\n")) {
            for (String s3 : s2.split("\r")) {

                unicodeFont.drawString(x, y, s3, new org.newdawn.slick.Color(currentColor));
                x += unicodeFont.getWidth(s3);

                index += s3.length();
                if (index  < characters.length && characters[index ] == '\r') {
                    x = originalX;
                    index++;
                }
            }
            if (index < characters.length && characters[index] == '\n') {
                x = originalX;
                y += getHeight(s2) * 2;
                index++;
            }
        }
        if (index < characters.length) {
            char colorCode = characters[index];
            if (colorCode == 'ยง') {
                char colorChar = characters[index + 1];
                int codeIndex = ("0123456789" +
                    "abcdef").indexOf(colorChar);
                if (codeIndex < 0) {
                    if (colorChar == 'r') {
                        currentColor = color;
                    }
                } else {
                    currentColor = colorCodes[codeIndex];
                }
                index += 2;
            }
        }
    }

    GlStateManager.color(1F, 1F, 1F, 1F);
    GlStateManager.bindTexture(0);
    GlStateManager.popMatrix();
    return (int) x;
}
 
Example #19
Source File: GlyphPage.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Create a new page of glyphs
 * 
 * @param unicodeFont The font this page forms part of
 * @param pageWidth The width of the backing texture.
 * @param pageHeight The height of the backing texture.
 * @throws SlickException if the backing texture could not be created.
 */
public GlyphPage(UnicodeFont unicodeFont, int pageWidth, int pageHeight) throws SlickException {
	this.unicodeFont = unicodeFont;
	this.pageWidth = pageWidth;
	this.pageHeight = pageHeight;

	pageImage = new Image(pageWidth, pageHeight);
}
 
Example #20
Source File: DropdownMenu.java    From opsu with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Creates a new dropdown menu with the given width and fonts.
 * @param container the container rendering this menu
 * @param items the list of items (with names given as their {@code toString()} methods)
 * @param x the top-left x coordinate
 * @param y the top-left y coordinate
 * @param width the menu width
 * @param normal the normal font
 * @param selected the font for the selected item
 */
public DropdownMenu(GUIContext container, E[] items, float x, float y, int width, UnicodeFont normal, UnicodeFont selected) {
	super(container);
	this.fontNormal = normal;
	this.fontSelected = selected;
	init(items, x, y, width);
}
 
Example #21
Source File: Effect.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Called to draw the effect.
 * 
 * @param image The image to draw into
 * @param g The graphics context to use for applying the effect
 * @param unicodeFont The font being rendered
 * @param glyph The particular glyph being rendered
 */
public void draw (BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph);
 
Example #22
Source File: DropdownMenu.java    From opsu with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a new dropdown menu with the given fonts.
 * @param container the container rendering this menu
 * @param items the list of items (with names given as their {@code toString()} methods)
 * @param x the top-left x coordinate
 * @param y the top-left y coordinate
 * @param normal the normal font
 * @param selected the font for the selected item
 */
public DropdownMenu(GUIContext container, E[] items, float x, float y, UnicodeFont normal, UnicodeFont selected) {
	this(container, items, x, y, 0, normal, selected);
}