src/fontcache.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6871 5a9dc001e1ad
--- a/src/fontcache.cpp	Sat Jun 02 19:59:29 2007 +0000
+++ b/src/fontcache.cpp	Sat Jul 14 19:42:58 2007 +0000
@@ -14,6 +14,8 @@
 #include "string.h"
 #include "fontcache.h"
 #include "helpers.hpp"
+#include "spriteloader/spriteloader.hpp"
+#include "blitter/factory.hpp"
 
 #ifdef WITH_FREETYPE
 
@@ -361,6 +363,26 @@
 	_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].width  = glyph->width;
 }
 
+void *AllocateFont(size_t size)
+{
+	return malloc(size);
+}
+
+
+/* Check if a glyph should be rendered with antialiasing */
+static bool GetFontAAState(FontSize size)
+{
+	/* AA is only supported for 32 bpp */
+	if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
+
+	switch (size) {
+		default: NOT_REACHED();
+		case FS_NORMAL: return _freetype.medium_aa;
+		case FS_SMALL:  return _freetype.small_aa;
+		case FS_LARGE:  return _freetype.large_aa;
+	}
+}
+
 
 const Sprite *GetGlyph(FontSize size, WChar key)
 {
@@ -368,7 +390,7 @@
 	FT_GlyphSlot slot;
 	GlyphEntry new_glyph;
 	GlyphEntry *glyph;
-	Sprite *sprite;
+	SpriteLoader::Sprite sprite;
 	int width;
 	int height;
 	int x;
@@ -390,29 +412,31 @@
 
 	slot = face->glyph;
 
+	bool aa = GetFontAAState(size);
+
 	FT_Load_Char(face, key, FT_LOAD_DEFAULT);
-	FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO);
+	FT_Render_Glyph(face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
 
 	/* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
 	width  = max(1, slot->bitmap.width + (size == FS_NORMAL));
 	height = max(1, slot->bitmap.rows  + (size == FS_NORMAL));
 
 	/* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
-	sprite = (Sprite*)calloc(width * height + 8, 1);
-	sprite->info   = 1;
-	sprite->width  = width;
-	sprite->height = height;
-	sprite->x_offs = slot->bitmap_left;
+	sprite.data = CallocT<SpriteLoader::CommonPixel>(width * height);
+	sprite.width = width;
+	sprite.height = height;
+	sprite.x_offs = slot->bitmap_left;
 	// XXX 2 should be determined somehow... it's right for the normal face
 	y_adj = (size == FS_NORMAL) ? 2 : 0;
-	sprite->y_offs = GetCharacterHeight(size) - slot->bitmap_top - y_adj;
+	sprite.y_offs = GetCharacterHeight(size) - slot->bitmap_top - y_adj;
 
 	/* Draw shadow for medium size */
 	if (size == FS_NORMAL) {
 		for (y = 0; y < slot->bitmap.rows; y++) {
 			for (x = 0; x < slot->bitmap.width; x++) {
-				if (HASBIT(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
-					sprite->data[1 + x + (1 + y) * sprite->width] = SHADOW_COLOUR;
+				if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HASBIT(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
+					sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
+					sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
 				}
 			}
 		}
@@ -420,18 +444,20 @@
 
 	for (y = 0; y < slot->bitmap.rows; y++) {
 		for (x = 0; x < slot->bitmap.width; x++) {
-			if (HASBIT(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
-				sprite->data[x + y * sprite->width] = FACE_COLOUR;
+			if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HASBIT(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
+				sprite.data[x + y * sprite.width].m = FACE_COLOUR;
+				sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
 			}
 		}
 	}
 
-	new_glyph.sprite = sprite;
+	new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
+	free(sprite.data);
 	new_glyph.width  = (slot->advance.x >> 6) + (size != FS_NORMAL);
 
 	SetGlyphPtr(size, key, &new_glyph);
 
-	return sprite;
+	return new_glyph.sprite;
 }