Java Code Examples for org.newdawn.slick.UnicodeFont#loadGlyphs()

The following examples show how to use org.newdawn.slick.UnicodeFont#loadGlyphs() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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;
}