mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 06:29:41 +00:00
Proper Reformating
This commit is contained in:
1 parent
ce75880aba
commit
180d257024
1179 files changed
+59847
-61583
No files matched your search
@@ -24,14 +24,12 @@ import java.util.*;
|
||||
* select the proper physical font to use (especially on less common Linux distributions). Once a pre-rendered glyph image is
|
||||
* cached, it will remain stored in an OpenGL texture for the entire lifetime of the application (StringCache depends on this
|
||||
* behavior).
|
||||
*
|
||||
* @todo Should have a separate glyph cache and a separate smaller point size font for rendering the GUI at its smallest size
|
||||
* and for use in the F3 debug screen; may need some explicit argument in StringCache.renderString() to select the size
|
||||
* @todo Need to have a config file that allows overring the font search order by locale to properly support Traditional Chinese
|
||||
* hanzi, Simplified Chinese hanzi, Japanese kanji, and Korean hanja
|
||||
*/
|
||||
public class GlyphCache
|
||||
{
|
||||
public class GlyphCache {
|
||||
/**
|
||||
* The width in pixels of every texture used for caching pre-rendered glyph images. Used by GlyphCache when calculating
|
||||
* floating point 0.0-1.0 texture coordinates. Must be a power of two for mip-mapping to work.
|
||||
@@ -44,10 +42,14 @@ public class GlyphCache
|
||||
*/
|
||||
private static final int TEXTURE_HEIGHT = 256;
|
||||
|
||||
/** Initial width in pixels of the stringImage buffer used to extract individual glyph images. */
|
||||
/**
|
||||
* Initial width in pixels of the stringImage buffer used to extract individual glyph images.
|
||||
*/
|
||||
private static final int STRING_WIDTH = 256;
|
||||
|
||||
/** Initial height in pixels of the stringImage buffer used to extract individual glyph images. */
|
||||
/**
|
||||
* Initial height in pixels of the stringImage buffer used to extract individual glyph images.
|
||||
*/
|
||||
private static final int STRING_HEIGHT = 64;
|
||||
|
||||
/**
|
||||
@@ -57,34 +59,52 @@ public class GlyphCache
|
||||
*/
|
||||
private static final int GLYPH_BORDER = 1;
|
||||
|
||||
/** Transparent (alpha zero) white background color for use with BufferedImage.clearRect(). */
|
||||
/**
|
||||
* Transparent (alpha zero) white background color for use with BufferedImage.clearRect().
|
||||
*/
|
||||
private static Color BACK_COLOR = new Color(255, 255, 255, 0);
|
||||
|
||||
/** The point size at which every OpenType font is rendered. */
|
||||
/**
|
||||
* The point size at which every OpenType font is rendered.
|
||||
*/
|
||||
private int fontSize = 18;
|
||||
|
||||
/** If true, then enble anti-aliasing when rendering the font glyph */
|
||||
/**
|
||||
* If true, then enble anti-aliasing when rendering the font glyph
|
||||
*/
|
||||
private boolean antiAliasEnabled = false;
|
||||
|
||||
|
||||
/** Temporary image for rendering a string to and then extracting the glyph images from. */
|
||||
/**
|
||||
* Temporary image for rendering a string to and then extracting the glyph images from.
|
||||
*/
|
||||
private BufferedImage stringImage;
|
||||
|
||||
/** The Graphics2D associated with stringImage and used for string drawing to extract the individual glyph shapes. */
|
||||
/**
|
||||
* The Graphics2D associated with stringImage and used for string drawing to extract the individual glyph shapes.
|
||||
*/
|
||||
private Graphics2D stringGraphics;
|
||||
|
||||
|
||||
/** All font glyphs are packed inside this image and are then loaded from here into an OpenGL texture. */
|
||||
/**
|
||||
* All font glyphs are packed inside this image and are then loaded from here into an OpenGL texture.
|
||||
*/
|
||||
private BufferedImage glyphCacheImage = new BufferedImage(TEXTURE_WIDTH, TEXTURE_HEIGHT, BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
/** The Graphics2D associated with glyphCacheImage and used for bit blitting between stringImage. */
|
||||
/**
|
||||
* The Graphics2D associated with glyphCacheImage and used for bit blitting between stringImage.
|
||||
*/
|
||||
private Graphics2D glyphCacheGraphics = glyphCacheImage.createGraphics();
|
||||
|
||||
/** Needed for all text layout operations that create GlyphVectors (maps point size to pixel size). */
|
||||
/**
|
||||
* Needed for all text layout operations that create GlyphVectors (maps point size to pixel size).
|
||||
*/
|
||||
private FontRenderContext fontRenderContext = glyphCacheGraphics.getFontRenderContext();
|
||||
|
||||
|
||||
/** Intermediate data array for use with textureImage.getRgb(). */
|
||||
/**
|
||||
* Intermediate data array for use with textureImage.getRgb().
|
||||
*/
|
||||
private int imageData[] = new int[TEXTURE_WIDTH * TEXTURE_HEIGHT];
|
||||
|
||||
/**
|
||||
@@ -94,10 +114,14 @@ public class GlyphCache
|
||||
*/
|
||||
private IntBuffer imageBuffer = ByteBuffer.allocateDirect(4 * TEXTURE_WIDTH * TEXTURE_HEIGHT).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
|
||||
|
||||
/** A single integer direct buffer with native byte ordering used for returning values from glGenTextures(). */
|
||||
/**
|
||||
* A single integer direct buffer with native byte ordering used for returning values from glGenTextures().
|
||||
*/
|
||||
private IntBuffer singleIntBuffer = GLAllocation.createDirectIntBuffer(1);
|
||||
|
||||
/** List of all available physical fonts on the system. Used by lookupFont() to find alternate fonts. */
|
||||
/**
|
||||
* List of all available physical fonts on the system. Used by lookupFont() to find alternate fonts.
|
||||
*/
|
||||
private List<Font> allFonts = Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts());
|
||||
|
||||
/**
|
||||
@@ -109,7 +133,9 @@ public class GlyphCache
|
||||
protected List<Font> usedFonts = new ArrayList();
|
||||
|
||||
|
||||
/** ID of current OpenGL cache texture being used by cacheGlyphs() to store pre-rendered glyph images. */
|
||||
/**
|
||||
* ID of current OpenGL cache texture being used by cacheGlyphs() to store pre-rendered glyph images.
|
||||
*/
|
||||
private int textureName;
|
||||
|
||||
/**
|
||||
@@ -143,44 +169,56 @@ public class GlyphCache
|
||||
private int cachePosY = GLYPH_BORDER;
|
||||
|
||||
/**
|
||||
* The height in pixels of the current line of glyphs getting written into the texture. This value determines by how much
|
||||
* cachePosY will get incremented when the current horizontal line in the texture fills up.
|
||||
*/
|
||||
* The height in pixels of the current line of glyphs getting written into the texture. This value determines by how much
|
||||
* cachePosY will get incremented when the current horizontal line in the texture fills up.
|
||||
*/
|
||||
private int cacheLineHeight = 0;
|
||||
|
||||
/**
|
||||
* This class holds information for a glyph about its pre-rendered image in an OpenGL texture. The texture coordinates in
|
||||
* this class are normalized in the standard 0.0 - 1.0 OpenGL range.
|
||||
*/
|
||||
static class Entry
|
||||
{
|
||||
/** The OpenGL texture ID that contains this glyph image. */
|
||||
static class Entry {
|
||||
/**
|
||||
* The OpenGL texture ID that contains this glyph image.
|
||||
*/
|
||||
public int textureName;
|
||||
|
||||
/** The width in pixels of the glyph image. */
|
||||
/**
|
||||
* The width in pixels of the glyph image.
|
||||
*/
|
||||
public int width;
|
||||
|
||||
/** The height in pixels of the glyph image. */
|
||||
/**
|
||||
* The height in pixels of the glyph image.
|
||||
*/
|
||||
public int height;
|
||||
|
||||
/** The horizontal texture coordinate of the upper-left corner. */
|
||||
/**
|
||||
* The horizontal texture coordinate of the upper-left corner.
|
||||
*/
|
||||
public float u1;
|
||||
|
||||
/** The vertical texture coordinate of the upper-left corner. */
|
||||
/**
|
||||
* The vertical texture coordinate of the upper-left corner.
|
||||
*/
|
||||
public float v1;
|
||||
|
||||
/** The horizontal texture coordinate of the lower-right corner. */
|
||||
/**
|
||||
* The horizontal texture coordinate of the lower-right corner.
|
||||
*/
|
||||
public float u2;
|
||||
|
||||
/** The vertical texture coordinate of the lower-right corner. */
|
||||
/**
|
||||
* The vertical texture coordinate of the lower-right corner.
|
||||
*/
|
||||
public float v2;
|
||||
}
|
||||
|
||||
/**
|
||||
* A single instance of GlyphCache is allocated for internal use by the StringCache class.
|
||||
*/
|
||||
public GlyphCache()
|
||||
{
|
||||
public GlyphCache() {
|
||||
/* Set background color for use with clearRect() */
|
||||
glyphCacheGraphics.setBackground(BACK_COLOR);
|
||||
|
||||
@@ -203,9 +241,8 @@ public class GlyphCache
|
||||
* @param name the new font name
|
||||
* @param size the new point size
|
||||
*/
|
||||
void setDefaultFont(String name, int size, boolean antiAlias)
|
||||
{
|
||||
|
||||
void setDefaultFont(String name, int size, boolean antiAlias) {
|
||||
|
||||
//System.out.println("BetterFonts loading font \"" + name + "\"");
|
||||
usedFonts.clear();
|
||||
usedFonts.add(new Font(name, Font.PLAIN, 72)); //size 1 > 72
|
||||
@@ -214,43 +251,41 @@ public class GlyphCache
|
||||
antiAliasEnabled = antiAlias;
|
||||
setRenderingHints();
|
||||
}
|
||||
|
||||
void setCustomFont(ResourceLocation location, int size, boolean antiAlias) throws Exception {
|
||||
InputStream stream = Minecraft.getMinecraft().getResourceManager().getResource(location).getInputStream();
|
||||
|
||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
|
||||
ge.registerFont(font);
|
||||
font = font.deriveFont(Font.PLAIN, 72);
|
||||
void setCustomFont(ResourceLocation location, int size, boolean antiAlias) throws Exception {
|
||||
InputStream stream = Minecraft.getMinecraft().getResourceManager().getResource(location).getInputStream();
|
||||
|
||||
usedFonts.clear();
|
||||
usedFonts.add(font);
|
||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
|
||||
ge.registerFont(font);
|
||||
font = font.deriveFont(Font.PLAIN, 72);
|
||||
|
||||
fontSize = size;
|
||||
antiAliasEnabled = antiAlias;
|
||||
setRenderingHints();
|
||||
}
|
||||
|
||||
int fontHeight(String s){
|
||||
Font font = lookupFont(s.toCharArray(), 0, s.length(), 0);
|
||||
return (int)(font.getLineMetrics(s, this.fontRenderContext).getHeight());
|
||||
usedFonts.clear();
|
||||
usedFonts.add(font);
|
||||
|
||||
fontSize = size;
|
||||
antiAliasEnabled = antiAlias;
|
||||
setRenderingHints();
|
||||
}
|
||||
|
||||
int fontHeight(String s) {
|
||||
Font font = lookupFont(s.toCharArray(), 0, s.length(), 0);
|
||||
return (int) (font.getLineMetrics(s, this.fontRenderContext).getHeight());
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a single OpenType font, perform full text layout and create a new GlyphVector for a string.
|
||||
*
|
||||
* @param font the Font used to layout a GlyphVector for the string
|
||||
* @param text the string to layout
|
||||
* @param start the offset into text at which to start the layout
|
||||
* @param limit the (offset + length) at which to stop performing the layout
|
||||
* @param font the Font used to layout a GlyphVector for the string
|
||||
* @param text the string to layout
|
||||
* @param start the offset into text at which to start the layout
|
||||
* @param limit the (offset + length) at which to stop performing the layout
|
||||
* @param layoutFlags either Font.LAYOUT_RIGHT_TO_LEFT or Font.LAYOUT_LEFT_TO_RIGHT
|
||||
* @return the newly created GlyphVector
|
||||
*/
|
||||
GlyphVector layoutGlyphVector(Font font, char text[], int start, int limit, int layoutFlags)
|
||||
{
|
||||
GlyphVector layoutGlyphVector(Font font, char text[], int start, int limit, int layoutFlags) {
|
||||
/* Ensure this font is already in fontCache so it can be referenced by cacheGlyphs() later on */
|
||||
if(!fontCache.containsKey(font))
|
||||
{
|
||||
if (!fontCache.containsKey(font)) {
|
||||
fontCache.put(font, fontCache.size());
|
||||
}
|
||||
return font.layoutGlyphVector(fontRenderContext, text, start, limit, layoutFlags);
|
||||
@@ -260,22 +295,19 @@ public class GlyphCache
|
||||
* Find the first font in the system able to render at least one character from a given string. The function always tries searching first
|
||||
* in the fontCache (based on the request style). Failing that, it searches the usedFonts list followed by the allFonts[] array.
|
||||
*
|
||||
* @param text the string to check against the font
|
||||
* @param text the string to check against the font
|
||||
* @param start the offset into text at which to start checking characters for being supported by a font
|
||||
* @param limit the (offset + length) at which to stop checking characters
|
||||
* @param style combination of the Font.PLAIN, Font.BOLD, and Font.ITALIC to request a particular font style
|
||||
* @return an OpenType font capable of displaying at least the first character at the start position in text
|
||||
*/
|
||||
Font lookupFont(char text[], int start, int limit, int style)
|
||||
{
|
||||
Font lookupFont(char text[], int start, int limit, int style) {
|
||||
/* Try using an already known base font; the first font in usedFonts list is the one set with setDefaultFont() */
|
||||
Iterator<Font> iterator = usedFonts.iterator();
|
||||
while(iterator.hasNext())
|
||||
{
|
||||
while (iterator.hasNext()) {
|
||||
/* Only use the font if it can layout at least the first character of the requested string range */
|
||||
Font font = iterator.next();
|
||||
if(font.canDisplayUpTo(text, start, limit) != start)
|
||||
{
|
||||
if (font.canDisplayUpTo(text, start, limit) != start) {
|
||||
/* Return a font instance of the proper point size and style; usedFonts has only 1pt sized plain style fonts */
|
||||
return font.deriveFont(style, fontSize);
|
||||
}
|
||||
@@ -283,12 +315,10 @@ public class GlyphCache
|
||||
|
||||
/* If still not found, try searching through all fonts installed on the system for the first that can layout this string */
|
||||
iterator = allFonts.iterator();
|
||||
while(iterator.hasNext())
|
||||
{
|
||||
while (iterator.hasNext()) {
|
||||
/* Only use the font if it can layout at least the first character of the requested string range */
|
||||
Font font = iterator.next();
|
||||
if(font.canDisplayUpTo(text, start, limit) != start)
|
||||
{
|
||||
if (font.canDisplayUpTo(text, start, limit) != start) {
|
||||
/* If found, add this font to the usedFonts list so it can be looked up faster next time */
|
||||
//System.out.println("BetterFonts loading font \"" + font.getFontName() + "\"");
|
||||
usedFonts.add(font);
|
||||
@@ -311,12 +341,11 @@ public class GlyphCache
|
||||
* within the texture. This function assumes that any glyph lookup requests passed to it have been already cached by an earlier call
|
||||
* to cacheGlyphs().
|
||||
*
|
||||
* @param font the font to which this glyphCode belongs and which was used to pre-render the glyph image in cacheGlyphs()
|
||||
* @param font the font to which this glyphCode belongs and which was used to pre-render the glyph image in cacheGlyphs()
|
||||
* @param glyphCode the font specific flyph code to lookup in the cache
|
||||
* @return the cache entry for this font/glyphCode pair
|
||||
*/
|
||||
Entry lookupGlyph(Font font, int glyphCode)
|
||||
{
|
||||
Entry lookupGlyph(Font font, int glyphCode) {
|
||||
long fontKey = (long) fontCache.get(font) << 32;
|
||||
return glyphCache.get(fontKey | glyphCode);
|
||||
}
|
||||
@@ -325,17 +354,15 @@ public class GlyphCache
|
||||
* Given an OpenType font and a string, make sure that every glyph used by that string is pre-rendered into an OpenGL texture and cached
|
||||
* in the glyphCache map for later retrieval by lookupGlyph()
|
||||
*
|
||||
* @param font the font used to create a GlyphVector for the string and to actually draw the individual glyphs
|
||||
* @param text the string from which to cache glyph images
|
||||
* @param start the offset into text at which to start caching glyphs
|
||||
* @param limit the (offset + length) at which to stop caching glyphs
|
||||
* @param font the font used to create a GlyphVector for the string and to actually draw the individual glyphs
|
||||
* @param text the string from which to cache glyph images
|
||||
* @param start the offset into text at which to start caching glyphs
|
||||
* @param limit the (offset + length) at which to stop caching glyphs
|
||||
* @param layoutFlags either Font.LAYOUT_RIGHT_TO_LEFT or Font.LAYOUT_LEFT_TO_RIGHT; needed for weak bidirectional characters like
|
||||
* parenthesis which are mapped to two different glyph codes depending on the surrounding text direction
|
||||
*
|
||||
* parenthesis which are mapped to two different glyph codes depending on the surrounding text direction
|
||||
* @todo May need a blank border of pixels around everything for mip-map/tri-linear filtering with Optifine
|
||||
*/
|
||||
void cacheGlyphs(Font font, char text[], int start, int limit, int layoutFlags)
|
||||
{
|
||||
void cacheGlyphs(Font font, char text[], int start, int limit, int layoutFlags) {
|
||||
/* Create new GlyphVector so glyphs can be moved around (kerning workaround; see below) without affecting caller */
|
||||
GlyphVector vector = layoutGlyphVector(font, text, start, limit, layoutFlags);
|
||||
|
||||
@@ -349,12 +376,10 @@ public class GlyphCache
|
||||
Rectangle dirty = null; /* Total area within texture that needs to be updated with glTexSubImage2D() */
|
||||
boolean vectorRendered = false; /* True if entire GlyphVector was rendered into stringImage */
|
||||
|
||||
for(int index = 0; index < numGlyphs; index++)
|
||||
{
|
||||
for (int index = 0; index < numGlyphs; index++) {
|
||||
/* If this glyph code is already in glyphCache, then there is no reason to pre-render it again */
|
||||
int glyphCode = vector.getGlyphCode(index);
|
||||
if(glyphCache.containsKey(fontKey | glyphCode))
|
||||
{
|
||||
if (glyphCache.containsKey(fontKey | glyphCode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -365,8 +390,7 @@ public class GlyphCache
|
||||
* array of glyphcodes (and therefore render only a few glyphs at a time), this produces corrupted
|
||||
* Davengari glyphs under Windows 7. The vectorRendered flag will draw the string at most one time.
|
||||
*/
|
||||
if(!vectorRendered)
|
||||
{
|
||||
if (!vectorRendered) {
|
||||
vectorRendered = true;
|
||||
|
||||
/*
|
||||
@@ -375,8 +399,7 @@ public class GlyphCache
|
||||
* the entire string. The getGlyphPixelBounds() later on will return the new adjusted bounds
|
||||
* for the glyph.
|
||||
*/
|
||||
for(int i = 0; i < numGlyphs; i++)
|
||||
{
|
||||
for (int i = 0; i < numGlyphs; i++) {
|
||||
Point2D pos = vector.getGlyphPosition(i);
|
||||
pos.setLocation(pos.getX() + 2 * i, pos.getY());
|
||||
vector.setGlyphPosition(i, pos);
|
||||
@@ -391,8 +414,7 @@ public class GlyphCache
|
||||
vectorBounds = vector.getPixelBounds(fontRenderContext, 0, 0);
|
||||
|
||||
/* Enlage the stringImage if it is too small to store the entire rendered string */
|
||||
if(stringImage == null || vectorBounds.width > stringImage.getWidth() || vectorBounds.height > stringImage.getHeight())
|
||||
{
|
||||
if (stringImage == null || vectorBounds.width > stringImage.getWidth() || vectorBounds.height > stringImage.getHeight()) {
|
||||
int width = Math.max(vectorBounds.width, stringImage.getWidth());
|
||||
int height = Math.max(vectorBounds.height, stringImage.getHeight());
|
||||
allocateStringImage(width, height);
|
||||
@@ -414,8 +436,7 @@ public class GlyphCache
|
||||
Rectangle rect = vector.getGlyphPixelBounds(index, null, -vectorBounds.x, -vectorBounds.y);
|
||||
|
||||
/* If the current line in cache image is full, then advance to the next line */
|
||||
if(cachePosX + rect.width + GLYPH_BORDER > TEXTURE_WIDTH)
|
||||
{
|
||||
if (cachePosX + rect.width + GLYPH_BORDER > TEXTURE_WIDTH) {
|
||||
cachePosX = GLYPH_BORDER;
|
||||
cachePosY += cacheLineHeight + GLYPH_BORDER;
|
||||
cacheLineHeight = 0;
|
||||
@@ -426,8 +447,7 @@ public class GlyphCache
|
||||
* (i.e. the dirty rectangle), allocate a new cache texture, and then continue storing glyph images to the
|
||||
* upper-left corner of the new texture.
|
||||
*/
|
||||
if(cachePosY + rect.height + GLYPH_BORDER > TEXTURE_HEIGHT)
|
||||
{
|
||||
if (cachePosY + rect.height + GLYPH_BORDER > TEXTURE_HEIGHT) {
|
||||
updateTexture(dirty);
|
||||
dirty = null;
|
||||
|
||||
@@ -438,8 +458,7 @@ public class GlyphCache
|
||||
}
|
||||
|
||||
/* The tallest glyph on this line determines the total vertical advance in the texture */
|
||||
if(rect.height > cacheLineHeight)
|
||||
{
|
||||
if (rect.height > cacheLineHeight) {
|
||||
cacheLineHeight = rect.height;
|
||||
}
|
||||
|
||||
@@ -483,12 +502,9 @@ public class GlyphCache
|
||||
* region than using the add(x, y) method to extend by a single point. Also note that creating the first
|
||||
* dirty rectangle here avoids having to deal with the special rules for empty/non-existent rectangles.
|
||||
*/
|
||||
if(dirty == null)
|
||||
{
|
||||
if (dirty == null) {
|
||||
dirty = new Rectangle(cachePosX, cachePosY, rect.width, rect.height);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
dirty.add(rect);
|
||||
}
|
||||
|
||||
@@ -504,21 +520,18 @@ public class GlyphCache
|
||||
* Update a portion of the current glyph cache texture using the contents of the glyphCacheImage with glTexSubImage2D().
|
||||
*
|
||||
* @param dirty The rectangular region in glyphCacheImage that has changed and needs to be copied into the texture
|
||||
*
|
||||
* @todo Add mip-mapping support here
|
||||
* @todo Test with bilinear texture interpolation and possibly add a 1 pixel transparent border around each glyph to avoid
|
||||
* bleedover when interpolation is active or add a small "fudge factor" to the UV coordinates like already n FontRenderer
|
||||
*/
|
||||
private void updateTexture(Rectangle dirty)
|
||||
{
|
||||
private void updateTexture(Rectangle dirty) {
|
||||
/* Only update OpenGL texture if changes were made to the texture */
|
||||
if(dirty != null)
|
||||
{
|
||||
if (dirty != null) {
|
||||
/* Load imageBuffer with pixel data ready for transfer to OpenGL texture */
|
||||
updateImageBuffer(dirty.x, dirty.y, dirty.width, dirty.height);
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureName);
|
||||
|
||||
|
||||
GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, dirty.x, dirty.y, dirty.width, dirty.height,
|
||||
GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, imageBuffer);
|
||||
}
|
||||
@@ -528,8 +541,7 @@ public class GlyphCache
|
||||
* Allocte and initialize a new BufferedImage and Graphics2D context for rendering strings into. May need to be called
|
||||
* at runtime to re-allocate a bigger BufferedImage if cacheGlyphs() is called with a very long string.
|
||||
*/
|
||||
private void allocateStringImage(int width, int height)
|
||||
{
|
||||
private void allocateStringImage(int width, int height) {
|
||||
stringImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
stringGraphics = stringImage.createGraphics();
|
||||
setRenderingHints();
|
||||
@@ -549,8 +561,7 @@ public class GlyphCache
|
||||
* allocateStringImage() when expanding the size of the BufferedImage and from setDefaultFont() when changing current
|
||||
* configuration.
|
||||
*/
|
||||
private void setRenderingHints()
|
||||
{
|
||||
private void setRenderingHints() {
|
||||
stringGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
antiAliasEnabled ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
|
||||
stringGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
|
||||
@@ -566,16 +577,15 @@ public class GlyphCache
|
||||
*
|
||||
* @todo use GL_ALPHA4 if anti-alias is turned off for even smaller textures
|
||||
*/
|
||||
private void allocateGlyphCacheTexture()
|
||||
{
|
||||
private void allocateGlyphCacheTexture() {
|
||||
/* Initialize the background to all white but fully transparent. */
|
||||
glyphCacheGraphics.clearRect(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT);
|
||||
|
||||
/* Allocate new OpenGL texure */
|
||||
singleIntBuffer.clear();
|
||||
|
||||
GL11.glGenTextures(singleIntBuffer);
|
||||
textureName = singleIntBuffer.get(0);
|
||||
|
||||
GL11.glGenTextures(singleIntBuffer);
|
||||
textureName = singleIntBuffer.get(0);
|
||||
|
||||
/* Load imageBuffer with pixel data ready for transfer to OpenGL texture */
|
||||
updateImageBuffer(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT);
|
||||
@@ -597,19 +607,17 @@ public class GlyphCache
|
||||
* Copy pixel data from a region in glyphCacheImage into imageBuffer and prepare it for use with glText(Sub)Image2D(). This
|
||||
* function takes care of converting the ARGB format used with BufferedImage into the RGBA format used by OpenGL.
|
||||
*
|
||||
* @param x the horizontal coordinate of the region's upper-left corner
|
||||
* @param y the vertical coordinate of the region's upper-left corner
|
||||
* @param width the width of the pixel region that will be copied into the buffer
|
||||
* @param x the horizontal coordinate of the region's upper-left corner
|
||||
* @param y the vertical coordinate of the region's upper-left corner
|
||||
* @param width the width of the pixel region that will be copied into the buffer
|
||||
* @param height the height of the pixel region that will be copied into the buffer
|
||||
*/
|
||||
private void updateImageBuffer(int x, int y, int width, int height)
|
||||
{
|
||||
private void updateImageBuffer(int x, int y, int width, int height) {
|
||||
/* Copy raw pixel data from BufferedImage to imageData array with one integer per pixel in 0xAARRGGBB form */
|
||||
glyphCacheImage.getRGB(x, y, width, height, imageData, 0, width);
|
||||
|
||||
/* Swizzle each color integer from Java's ARGB format to OpenGL's RGBA */
|
||||
for(int i = 0; i < width * height; i++)
|
||||
{
|
||||
for (int i = 0; i < width * height; i++) {
|
||||
int color = imageData[i];
|
||||
imageData[i] = (color << 8) | (color >>> 24);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user