Commits

Bill Meltsner  committed bfbb36f

Add padding around every glyph in a Font texture, fixing the fringing seen with scaled text (actually fixes issue #321)

  • Participants
  • Parent commits 1c6db94

Comments (0)

Files changed (2)

File src/modules/graphics/opengl/Font.cpp

 
 	void Font::createTexture()
 	{
-		texture_x = texture_y = rowHeight = 0;
+		texture_x = texture_y = rowHeight = TEXTURE_PADDING;
 		GLuint t;
 		glGenTextures(1, &t);
 		textures.push_back(t);
 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 		GLint format = (type == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA);
+		// Initialize the texture
 		glTexImage2D(GL_TEXTURE_2D,
 					 0,
 					 GL_RGBA,
 					 format,
 					 GL_UNSIGNED_BYTE,
 					 NULL);
+		// Fill the texture with transparent black
+		std::vector<GLubyte> emptyData(TEXTURE_WIDTH * TEXTURE_HEIGHT * (type == FONT_TRUETYPE ? 2 : 4), 0);
+		glTexSubImage2D(GL_TEXTURE_2D,
+						0,
+						0,
+						0,
+						(GLsizei)TEXTURE_WIDTH,
+						(GLsizei)TEXTURE_HEIGHT,
+						format,
+						GL_UNSIGNED_BYTE,
+						&emptyData[0]);
 	}
 
 	Font::Glyph * Font::addGlyph(int glyph)
 		g->spacing = gd->getAdvance();
 		int w = gd->getWidth();
 		int h = gd->getHeight();
-		if (texture_x + w > TEXTURE_WIDTH)
+		if (texture_x + w + TEXTURE_PADDING > TEXTURE_WIDTH)
 		{ // out of space - new row!
-			texture_x = 0;
+			texture_x = TEXTURE_PADDING;
 			texture_y += rowHeight;
-			rowHeight = 0;
+			rowHeight = TEXTURE_PADDING;
 		}
-		if (texture_y + h > TEXTURE_HEIGHT)
+		if (texture_y + h + TEXTURE_PADDING > TEXTURE_HEIGHT)
 		{ // totally out of space - new texture!
 			createTexture();
 		}
 		delete q;
 		delete gd;
 
-		texture_x += w;
-		rowHeight = std::max(rowHeight, h);
+		texture_x += (w + TEXTURE_PADDING);
+		rowHeight = std::max(rowHeight, h + TEXTURE_PADDING);
 
 		glyphs[glyph] = g;
 		return g;

File src/modules/graphics/opengl/Font.h

 
 		static const int TEXTURE_WIDTH = 512;
 		static const int TEXTURE_HEIGHT = 512;
+		static const int TEXTURE_PADDING = 1;
 
 		int texture_x, texture_y;
 		int rowHeight;