Panagiotis Christopoulos Charitos 14 years ago
parent
commit
2b5eacfabd
4 changed files with 65 additions and 21 deletions
  1. 14 2
      build/debug/Makefile
  2. 1 1
      src/Main.cpp
  3. 42 16
      src/Ui/UiFtFontLoader.cpp
  4. 8 2
      src/Ui/UiFtFontLoader.h

+ 14 - 2
build/debug/Makefile

@@ -269,7 +269,17 @@ Main.o: ../../src/Main.cpp ../../src/Input/Input.h \
  ../../src/Util/Util.h ../../src/Util/Vec.h ../../src/Util/StdTypes.h \
  ../../src/Util/HighRezTimer.h ../../src/Scene/SkinNode.h \
  ../../src/Scene/SkinPatchNode.h ../../src/Resources/Skin.h \
- ../../src/Ui/UiFont.h
+ ../../src/Ui/UiFtFontLoader.h /usr/include/freetype2/freetype/freetype.h \
+ /usr/include/freetype2/freetype/config/ftconfig.h \
+ /usr/include/freetype2/freetype/config/ftoption.h \
+ /usr/include/freetype2/freetype/config/ftstdlib.h \
+ /usr/include/freetype2/freetype/fterrors.h \
+ /usr/include/freetype2/freetype/ftmoderr.h \
+ /usr/include/freetype2/freetype/fterrdef.h \
+ /usr/include/freetype2/freetype/fttypes.h \
+ /usr/include/freetype2/freetype/ftsystem.h \
+ /usr/include/freetype2/freetype/ftimage.h \
+ /usr/include/freetype2/freetype/ftglyph.h
 	@echo Compiling ../../src/Main.cpp...
 	@$(CXX) $(INCPATH) $(CFLAGS) ../../src/Main.cpp -o Main.o
 
@@ -2064,7 +2074,9 @@ UiFtFontLoader.o: ../../src/Ui/UiFtFontLoader.cpp \
  /usr/include/freetype2/freetype/ftsystem.h \
  /usr/include/freetype2/freetype/ftimage.h \
  /usr/include/freetype2/freetype/ftglyph.h ../../src/Util/Vec.h \
- ../../src/Util/Assert.h
+ ../../src/Util/Assert.h ../../src/Util/StdTypes.h \
+ ../../src/Util/Exception.h ../../src/Core/Logger.h \
+ ../../src/Util/Singleton.h ../../src/Core/Globals.h
 	@echo Compiling ../../src/Ui/UiFtFontLoader.cpp...
 	@$(CXX) $(INCPATH) $(CFLAGS) ../../src/Ui/UiFtFontLoader.cpp -o UiFtFontLoader.o
 

+ 1 - 1
src/Main.cpp

@@ -433,7 +433,7 @@ void mainLoop()
 int main(int argc, char* argv[])
 {
 	FT_Vector s = {100, 100};
-	Ui::FtFontLoader fnt("/home/godlike/src/google-code-repo/libfreetype/Ruritania-Outline.ttf", s);
+	Ui::FtFontLoader fnt("/usr/share/fonts/truetype/msttcorefonts/Andale_Mono.ttf", s);
 	fnt.saveImage("/tmp/test.tga");
 
 	return 0;

+ 42 - 16
src/Ui/UiFtFontLoader.cpp

@@ -1,6 +1,8 @@
 #include <boost/foreach.hpp>
 #include "UiFtFontLoader.h"
 #include "Exception.h"
+#include "Logger.h"
+#include "Globals.h"
 
 
 namespace Ui {
@@ -13,7 +15,7 @@ void FtFontLoader::getGlyphs()
 {
 	glyphs.resize(GLYPHS_NUM);
 
-	for(uint n = 1; n < GLYPHS_NUM; n++)
+	for(uint n = 0; n < GLYPHS_NUM; n++)
 	{
 		char c = ' ' + n;
 
@@ -34,7 +36,7 @@ void FtFontLoader::getGlyphs()
 		}
 	}
 
-	glyphs[0].metrics.width = glyphs['_' - ' '].metrics.width;
+	//glyphs[0].metrics.width = glyphs['_' - ' '].metrics.width;
 }
 
 
@@ -74,7 +76,7 @@ void FtFontLoader::computeImageSize()
 		}
 	}
 
-	imgSize.y = GLYPHS_NUM / GLYPHS_PER_ROW * imgSize.y;
+	imgSize.y = GLYPH_ROWS * imgSize.y;
 
 	//
 	// Get img width
@@ -82,13 +84,14 @@ void FtFontLoader::computeImageSize()
 
 
 	// For all rows
-	for(uint i = 0; i < GLYPHS_NUM / GLYPHS_PER_ROW; i++)
+	for(uint i = 0; i < GLYPH_ROWS; i++)
 	{
 		uint rowSize = 0;
 		// For all columns
-		for(uint j = 0; j < GLYPHS_PER_ROW; j++)
+		for(uint j = 0; j < GLYPH_COLUMNS; j++)
 		{
-			rowSize += toPixels(glyphs[i * GLYPHS_PER_ROW + j].metrics.width);
+			uint pos = i * GLYPH_COLUMNS + j;
+			rowSize += toPixels(glyphs[pos].metrics.width);
 		}
 
 		if(rowSize > imgSize.x)
@@ -121,7 +124,11 @@ void FtFontLoader::createImage(const char* filename, const FT_Vector& fontSize)
 		throw EXCEPTION("FT_New_Face failed with filename \"" + filename + "\"");
 	}
 
-	FT_Set_Pixel_Sizes(face, fontSize.x, fontSize.y);
+	error = FT_Set_Pixel_Sizes(face, fontSize.x, fontSize.y);
+	if(error)
+	{
+		throw EXCEPTION("FT_Set_Pixel_Sizes failed");
+	}
 
 	// Get all glyphs
 	getGlyphs();
@@ -133,26 +140,45 @@ void FtFontLoader::createImage(const char* filename, const FT_Vector& fontSize)
 	img.resize(size, 128);
 
 	// Draw all glyphs to the image
-	FT_Vector pos = {0, 0};
+	FT_Vector pos = {0, 0}; // the (0,0) is the top left
 	// For all rows
-	for(uint i = 0; i < GLYPHS_NUM / GLYPHS_PER_ROW; i++)
+	for(uint i = 0; i < GLYPH_ROWS; i++)
 	{
 		// For all columns
-		for(uint j = 0; j < GLYPHS_PER_ROW; j++)
+		for(uint j = 0; j < GLYPH_COLUMNS; j++)
 		{
-			Glyph& glyph = glyphs[i * GLYPHS_PER_ROW + j];
+			Glyph& glyph = glyphs[i * GLYPH_COLUMNS + j];
+
+			// Set texture matrix
+			float scaleX = toPixels(glyph.metrics.width) / float(imgSize.x); // glyph width
+			float scaleY = toPixels(glyph.metrics.height) / float(imgSize.y); // glyph height
+			float tslX = pos.x / float(imgSize.x);
+			float tslY = (imgSize.y - toPixels(glyph.metrics.height) - pos.y) / float(imgSize.y);
+
+			glyph.textureMat = Mat3::getIdentity();
+			glyph.textureMat(0, 0) = scaleX;
+			glyph.textureMat(1, 1) = scaleY;
+			glyph.textureMat(0, 2) = tslX;
+			glyph.textureMat(1, 2) = tslY;
+
+			std::cout << glyph.textureMat << std::endl;
 
-			FT_Glyph_To_Bitmap(&glyph.glyph, FT_RENDER_MODE_NORMAL, 0, 0);
-			FT_BitmapGlyph bit = (FT_BitmapGlyph) glyph.glyph;
+			// If not ' '
+			if(i != 0 || j != 0)
+			{
+				FT_Glyph_To_Bitmap(&glyph.glyph, FT_RENDER_MODE_NORMAL, 0, 0);
+				FT_BitmapGlyph bit = (FT_BitmapGlyph) glyph.glyph;
 
-			FT_Vector srcSize = {toPixels(glyph.metrics.width), toPixels(glyph.metrics.height)};
+				FT_Vector srcSize = {toPixels(glyph.metrics.width), toPixels(glyph.metrics.height)};
 
-			copyBitmap(bit->bitmap.buffer, srcSize, pos);
+				copyBitmap(bit->bitmap.buffer, srcSize, pos);
+			}
 
 			pos.x += toPixels(glyph.metrics.width);
 		}
 
-		pos.y += imgSize.y / (GLYPHS_NUM / GLYPHS_PER_ROW);
+		pos.x = 0;
+		pos.y += imgSize.y / (GLYPH_ROWS);
 	}
 
 	// Clean

+ 8 - 2
src/Ui/UiFtFontLoader.h

@@ -6,6 +6,7 @@
 #include FT_GLYPH_H
 #include "Vec.h"
 #include "StdTypes.h"
+#include "Math.h"
 
 
 namespace Ui {
@@ -15,8 +16,12 @@ namespace Ui {
 class FtFontLoader
 {
 	public:
-		static const uint GLYPHS_NUM = 128;
-		static const uint GLYPHS_PER_ROW = 16;
+		enum
+		{
+			GLYPHS_NUM = 128,
+			GLYPH_COLUMNS = 16,
+			GLYPH_ROWS = 8
+		};
 
 		FtFontLoader(const char* filename, const FT_Vector& fontSize);
 
@@ -28,6 +33,7 @@ class FtFontLoader
 		{
 			FT_Glyph glyph;
 			FT_Glyph_Metrics metrics;
+			Mat3 textureMat;
 		};
 
 		/// @name Data