src/fontcache.cpp
changeset 7409 4f631b495a5b
parent 7391 1fec0c1b204f
child 7433 8e410e7ec0d7
equal deleted inserted replaced
7408:d5ba4fd845dd 7409:4f631b495a5b
   367 {
   367 {
   368 	return malloc(size);
   368 	return malloc(size);
   369 }
   369 }
   370 
   370 
   371 
   371 
       
   372 /* Check if a glyph should be rendered with antialiasing */
       
   373 static bool GetFontAAState(FontSize size)
       
   374 {
       
   375 	/* AA is only supported for 32 bpp */
       
   376 	if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
       
   377 
       
   378 	switch (size) {
       
   379 		default: NOT_REACHED();
       
   380 		case FS_NORMAL: return _freetype.medium_aa;
       
   381 		case FS_SMALL:  return _freetype.small_aa;
       
   382 		case FS_LARGE:  return _freetype.large_aa;
       
   383 	}
       
   384 }
       
   385 
       
   386 
   372 const Sprite *GetGlyph(FontSize size, WChar key)
   387 const Sprite *GetGlyph(FontSize size, WChar key)
   373 {
   388 {
   374 	FT_Face face = GetFontFace(size);
   389 	FT_Face face = GetFontFace(size);
   375 	FT_GlyphSlot slot;
   390 	FT_GlyphSlot slot;
   376 	GlyphEntry new_glyph;
   391 	GlyphEntry new_glyph;
   395 	glyph = GetGlyphPtr(size, key);
   410 	glyph = GetGlyphPtr(size, key);
   396 	if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
   411 	if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
   397 
   412 
   398 	slot = face->glyph;
   413 	slot = face->glyph;
   399 
   414 
       
   415 	bool aa = GetFontAAState(size);
       
   416 
   400 	FT_Load_Char(face, key, FT_LOAD_DEFAULT);
   417 	FT_Load_Char(face, key, FT_LOAD_DEFAULT);
   401 	FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO);
   418 	FT_Render_Glyph(face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
   402 
   419 
   403 	/* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
   420 	/* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
   404 	width  = max(1, slot->bitmap.width + (size == FS_NORMAL));
   421 	width  = max(1, slot->bitmap.width + (size == FS_NORMAL));
   405 	height = max(1, slot->bitmap.rows  + (size == FS_NORMAL));
   422 	height = max(1, slot->bitmap.rows  + (size == FS_NORMAL));
   406 
   423 
   415 
   432 
   416 	/* Draw shadow for medium size */
   433 	/* Draw shadow for medium size */
   417 	if (size == FS_NORMAL) {
   434 	if (size == FS_NORMAL) {
   418 		for (y = 0; y < slot->bitmap.rows; y++) {
   435 		for (y = 0; y < slot->bitmap.rows; y++) {
   419 			for (x = 0; x < slot->bitmap.width; x++) {
   436 			for (x = 0; x < slot->bitmap.width; x++) {
   420 				if (HASBIT(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
   437 				if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HASBIT(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
   421 					sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
   438 					sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
   422 					sprite.data[1 + x + (1 + y) * sprite.width].a = 0xFF;
   439 					sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
   423 				}
   440 				}
   424 			}
   441 			}
   425 		}
   442 		}
   426 	}
   443 	}
   427 
   444 
   428 	for (y = 0; y < slot->bitmap.rows; y++) {
   445 	for (y = 0; y < slot->bitmap.rows; y++) {
   429 		for (x = 0; x < slot->bitmap.width; x++) {
   446 		for (x = 0; x < slot->bitmap.width; x++) {
   430 			if (HASBIT(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
   447 			if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HASBIT(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
   431 				sprite.data[x + y * sprite.width].m = FACE_COLOUR;
   448 				sprite.data[x + y * sprite.width].m = FACE_COLOUR;
   432 				sprite.data[x + y * sprite.width].a = 0xFF;
   449 				sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
   433 			}
   450 			}
   434 		}
   451 		}
   435 	}
   452 	}
   436 
   453 
   437 	new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
   454 	new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);