Removed the last vestiges of the old, bad font code

This commit is contained in:
Bill Meltsner
2010-04-15 14:08:43 -04:00
parent de6c8fd704
commit e731baf670
5 changed files with 0 additions and 706 deletions
-201
View File
@@ -1,201 +0,0 @@
/**
* Copyright (c) 2006-2010 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "ImageFont.h"
#include <common/math.h>
#include <SDL_opengl.h>
namespace love
{
namespace graphics
{
namespace opengl
{
ImageFont::ImageFont(Image * image, const std::string& glyphs)
: Font(0), image(image), glyphs(glyphs)
{
image->retain();
}
ImageFont::~ImageFont()
{
unload();
image->release();
}
void ImageFont::print(std::string text, float x, float y) const
{
glPushMatrix();
glTranslatef(x, y, 0.0f);
GLuint OpenGLFont = list;
glListBase(OpenGLFont);
glCallLists((int)text.length(), GL_UNSIGNED_BYTE, text.c_str());
glPopMatrix();
}
void ImageFont::print(std::string text, float x, float y, float angle, float sx, float sy) const
{
glPushMatrix();
glTranslatef(x, y, 0.0f);
glRotatef(LOVE_TORAD(angle), 0, 0, 1.0f);
glScalef(sx, sy, 1.0f);
GLuint OpenGLFont = list;
glListBase(OpenGLFont);
glCallLists((int)text.length(), GL_UNSIGNED_BYTE, text.c_str());
glPopMatrix();
}
void ImageFont::print(char character, float x, float y) const
{
glPushMatrix();
glTranslatef(x, y, 0.0f);
GLuint OpenGLFont = list;
glListBase(OpenGLFont);
glCallList(list + (int)character);
glPopMatrix();
}
bool ImageFont::load()
{
return loadVolatile();
}
void ImageFont::unload()
{
unloadVolatile();
}
bool ImageFont::loadVolatile()
{
love::image::pixel * pixels = (love::image::pixel *)(image->getData()->getData());
unsigned imgw = (unsigned)image->getWidth();
unsigned imgh = (unsigned)image->getHeight();
unsigned imgs = imgw*imgh;
// Reading texture data begins
size = imgh;
for(unsigned int i = 0; i < MAX_CHARS; i++)
positions[i] = -1;
love::image::pixel spacer = pixels[0];
unsigned num = glyphs.size();
unsigned start = 0;
unsigned end = 0;
unsigned space = 0;
for(unsigned i = 0; i < num; ++i)
{
if(i >= MAX_CHARS)
break;
start = end;
// Finds out where the first character starts
while(start < imgw && equal(pixels[start], spacer))
++start;
if(i > 0)
spacing[glyphs[i - 1]] = (start > end) ? (start - end) : 0;
end = start;
// Find where glyph ends.
while(end < imgw && !equal(pixels[end], spacer))
++end;
if(start >= end)
break;
unsigned c = glyphs[i];
positions[c] = start;
widths[c] = (end - start);
}
// Replace spacer color with an empty pixel
for(unsigned int i = 0; i < imgs; ++i)
{
if(equal(pixels[i], spacer))
{
pixels[i].r = 0;
pixels[i].g = 0;
pixels[i].b = 0;
pixels[i].a = 0;
}
}
// Create display lists
list = glGenLists(MAX_CHARS);
for(unsigned int i = 0; i < MAX_CHARS; i++)
{
glNewList(list + i, GL_COMPILE);
if(positions[i] != -1)
{
Quad::Viewport v;
v.x = positions[i];
v.y = 0;
v.w = widths[i];
v.h = imgh;
Quad q(v, imgw, imgh);
image->drawq(&q, 0, 0, 0, 1, 1, 0, 0);
glTranslatef((float)widths[i] + ((float)spacing[i] * mSpacing), 0, 0);
}
else
glTranslatef((float)widths[(int)' '], 0, 0); // empty character are replaced with a whitespace
glEndList();
}
return true;
}
void ImageFont::unloadVolatile()
{
glDeleteLists(list, MAX_CHARS);
}
bool ImageFont::equal(const love::image::pixel& a, const love::image::pixel& b)
{
return (a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a);
}
int ImageFont::next_p2(int num)
{
int powered = 2;
while(powered < num) powered <<= 1;
return powered;
}
} // opengl
} // graphics
} // love
-108
View File
@@ -1,108 +0,0 @@
/**
* Copyright (c) 2006-2010 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_OPENGL_IMAGE_FONT_H
#define LOVE_OPENGL_IMAGE_FONT_H
#include <image/ImageData.h>
#include "Font.h"
#include "Image.h"
// STD
#include <string>
#include <cstdio>
namespace love
{
namespace graphics
{
namespace opengl
{
/**
* A class to handle OpenGL image fonts.
*
* @author Michael Enger
* @date 2008-01-17
**/
class ImageFont : public Font
{
protected:
Image * image;
// List of glyphs.
std::string glyphs;
// The position of each character.
int positions[MAX_CHARS];
// OpenGL display lists.
unsigned int list;
public:
/**
* Default constructor.
*
* @param file The image file.
* @param glyphs A list of the characters as they appear in the image.
**/
ImageFont(Image * image, const std::string& glyphs);
/**
* Calls unload().
**/
virtual ~ImageFont();
// From Font
virtual void print(std::string text, float x, float y) const;
virtual void print(std::string text, float x, float y, float angle, float sx, float sy) const;
virtual void print(char character, float x, float y) const;
// From Resource.
bool load();
void unload();
// From Volatile.
bool loadVolatile();
void unloadVolatile();
protected:
/**
* Checks whether two pixels are equal.
**/
bool equal(const love::image::pixel& a, const love::image::pixel& b);
/**
* Returns the closest number to num which is a power of two.
*
* @param num The number to be 2powered.
**/
int next_p2(int num);
}; // ImageFont
} // opengl
} // graphics
} // love
#endif // LOVE_OPENGL_IMAGE_FONT_H
@@ -1,255 +0,0 @@
/**
* Copyright (c) 2006-2010 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "TrueTypeFont.h"
#include <SDL_opengl.h>
#include <common/Exception.h>
#include <common/math.h>
#include <math.h>
#include <iostream>
using std::string;
namespace love
{
namespace graphics
{
namespace opengl
{
inline int TrueTypeFont::next_p2(int num)
{
int powered = 2;
while(powered < num) powered <<= 1;
return powered;
}
inline void TrueTypeFont::pushScreenCoordinateMatrix()
{
glPushAttrib(GL_TRANSFORM_BIT);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(viewport[0],viewport[2],viewport[1],viewport[3]);
glPopAttrib();
}
inline void TrueTypeFont::popProjectionMatrix()
{
glPushAttrib(GL_TRANSFORM_BIT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
}
void TrueTypeFont::createList(FT_Face face, unsigned short character)
{
if( FT_Load_Glyph(face, FT_Get_Char_Index(face, character), FT_LOAD_DEFAULT) )
std::cerr << "TrueTypeFont Loading vm->error: FT_Load_Glyph failed." << std::endl;
FT_Glyph glyph;
if( FT_Get_Glyph(face->glyph, &glyph) )
std::cerr << "TrueTypeFont Loading vm->error: FT_Get_Glyph failed." << std::endl;
FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
FT_Bitmap& bitmap = bitmap_glyph->bitmap; //just to make things easier
widths[character] = face->glyph->advance.x >> 6;
int w = next_p2(bitmap.width);
int h = next_p2(bitmap.rows);
if(bitmap.rows > trueHeight)
trueHeight = bitmap.rows;
GLubyte* expandedData = new GLubyte[ 2 * w * h];
for(int j = 0; j < h; j++) for(int i = 0; i < w; i++)
{
expandedData[2 * (i + j * w)] = MAX_CHARS-1;
expandedData[2 * (i + j * w) + 1] = (i >= bitmap.width || j >= bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width * j];
}
glBindTexture(GL_TEXTURE_2D, textures[character]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Rude adds:
// (You're welcome, Mike.)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expandedData);
delete [] expandedData; //no longer needed
glNewList(list + character, GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, textures[character]);
glPushMatrix();
glTranslatef((float)bitmap_glyph->left, -(float)bitmap_glyph->top, 0);
//glTranslatef(0, (float)bitmap_glyph->top-bitmap.rows, 0);
float x=(float)bitmap.width / (float)w,
y=(float)bitmap.rows / (float)h;
glBegin(GL_QUADS);
glTexCoord2d(0, 0); glVertex2f(0, 0);
glTexCoord2d(0, y); glVertex2f(0, (float)bitmap.rows);
glTexCoord2d(x, y); glVertex2f((float)bitmap.width, (float)bitmap.rows);
glTexCoord2d(x, 0); glVertex2f((float)bitmap.width, 0);
glEnd();
glPopMatrix();
glTranslatef((float)(face->glyph->advance.x >> 6) ,0,0);
glEndList();
FT_Done_Glyph(glyph);
}
TrueTypeFont::TrueTypeFont(Data * data, int size)
: Font(size), data(data), textures(0), list(0)
{
data->retain();
}
TrueTypeFont::~TrueTypeFont()
{
unload();
data->release();
}
void TrueTypeFont::print(string text, float x, float y) const
{
glPushMatrix();
glTranslatef(ceil(x), ceil(y), 0.0f); // + getHeight() to make the x,y coordinates the top left corner
GLuint TrueTypeFont = list;
glListBase(TrueTypeFont);
glCallLists((int)text.length(), GL_UNSIGNED_BYTE, text.c_str());
glPopMatrix();
}
void TrueTypeFont::print(std::string text, float x, float y, float angle, float sx, float sy) const
{
glPushMatrix();
glTranslatef(ceil(x), ceil(y), 0.0f);
glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f);
glScalef(sx, sy, 1.0f);
GLuint TrueTypeFont = list;
glListBase(TrueTypeFont);
glCallLists((int)text.length(), GL_UNSIGNED_BYTE, text.c_str());
glPopMatrix();
}
void TrueTypeFont::print(char character, float x, float y) const
{
glPushMatrix();
glTranslatef(ceil(x), ceil(y), 0.0f);
GLuint TrueTypeFont = list;
glListBase(TrueTypeFont);
glCallList(list + (int)character);
glPopMatrix();
}
float TrueTypeFont::getHeight() const
{
return (float)trueHeight;
}
float TrueTypeFont::getLineHeight() const
{
return Font::getLineHeight() * 1.25f;
}
bool TrueTypeFont::load()
{
return loadVolatile();
}
void TrueTypeFont::unload()
{
unloadVolatile();
}
bool TrueTypeFont::loadVolatile()
{
trueHeight = size;
textures = (unsigned int *)(new GLuint[MAX_CHARS]);
for(unsigned int i = 0; i != MAX_CHARS; i++) widths[i] = 0;
FT_Library library;
if( FT_Init_FreeType(&library) ) {
std::cerr << "TrueTypeFont Loading error: FT_Init_FreeType failed." << std::endl;
throw love::Exception("TrueTypeFont Loading error: FT_Init_FreeType failed.");
}
FT_Face face;
if( FT_New_Memory_Face( library,
(const FT_Byte *)data->getData(), /* first byte in memory */
data->getSize(), /* size in bytes */
0, /* face_index */
&face )) {
std::cerr << "TrueTypeFont Loading error: FT_New_Face failed (there is probably a problem with your font file)." << std::endl;
throw love::Exception("TrueTypeFont Loading error: FT_New_Face failed (there is probably a problem with your font file).");
}
//FT_Set_Char_Size(face, size << 6, size << 6, 96, 96);
FT_Set_Pixel_Sizes(face, size, size);
list = glGenLists(MAX_CHARS);
glGenTextures(MAX_CHARS, (GLuint*)textures);
for(unsigned short i = 0; i < MAX_CHARS; i++)
createList(face, i);
FT_Done_Face(face);
FT_Done_FreeType(library); //all done
return true;
}
void TrueTypeFont::unloadVolatile()
{
if(list != 0)
glDeleteLists(list, MAX_CHARS);
if(textures != 0)
glDeleteTextures(MAX_CHARS, (const GLuint*)textures);
// Cleanup plz.
if(textures != 0)
delete [] textures;
textures = 0;
list = 0;
}
} // opengl
} // graphics
} // love
-134
View File
@@ -1,134 +0,0 @@
/**
* Copyright (c) 2006-2010 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_GRAPHICS_OPENGL_TRUETYPE_FONT_H
#define LOVE_GRAPHICS_OPENGL_TRUETYPE_FONT_H
// Module
#include "Font.h"
// LOVE
#include <common/config.h>
#include <common/Data.h>
// FreeType2
#ifdef LOVE_MACOSX
#include <freetype/ft2build.h>
#else
#include <ft2build.h>
#endif
#include <freetype/freetype.h>
#include <freetype/ftglyph.h>
#include <freetype/ftoutln.h>
#include <freetype/fttrigon.h>
// STD
#include <string>
namespace love
{
namespace graphics
{
namespace opengl
{
/**
* A class to handle TrueType fonts. Uses the library FreeType2
* (available here: http://www.freetype.org/) and takes use of both
* their local documentation and Sven's experience.
*
* @author Michael Enger (with great help from Sven C. Olsen)
* @date 2007-01-15
**/
class TrueTypeFont : public Font
{
private:
Data * data;
protected:
unsigned int * textures;
unsigned int list;
int trueHeight; // the true height of the font
/**
* Returns the closest number to num which is a power of two.
*
* @param num The number to be 2powered.
**/
inline int next_p2(int num);
/**
* As stated by Sven: A fairly straight forward function that pushes a projection matrix that will make object world coordinates identical to window coordinates.
**/
inline void pushScreenCoordinateMatrix();
/**
* Pops the projection matrix without changing the current MatrixMode.
**/
inline void popProjectionMatrix();
/**
* Creates an OpenGL display list for the character (for speedy execution).
*
* @param face The FT_Face containing information about the character.
* @param character The character in question.
**/
void createList(FT_Face face, unsigned short character);
public:
/**
* Default constructor.
*
* @param file The file containing the TrueTypeFont data.
* @param size The size of the TrueTypeFont.
**/
TrueTypeFont(Data * data, int size);
/**
* Calls unload().
**/
virtual ~TrueTypeFont();
// From Font
//virtual float getHeight() const;
virtual void print(std::string text, float x, float y) const;
virtual void print(std::string text, float x, float y, float angle, float sx, float sy) const;
virtual void print(char character, float x, float y) const;
virtual float getHeight() const;
virtual float getLineHeight() const;
// From Resource.
bool load();
void unload();
// From Volatile.
bool loadVolatile();
void unloadVolatile();
}; // TrueTypeFont
} // opengl
} // graphics
} // love
#endif // LOVE_GRAPHICS_OPENGL_TRUETYPE_FONT_H