Changeset - r24900:959a621b6aa9
[Not reviewed]
master
0 6 0
Michael Lutz - 3 years ago 2021-01-16 15:43:31
michi@icosahedron.de
Codechange: Make the simple Malloc sprite allocator globally usable.
6 files changed with 13 insertions and 11 deletions:
0 comments (0 inline, 0 general)
src/fontcache.cpp
Show inline comments
 
@@ -268,29 +268,24 @@ void TrueTypeFontCache::SetGlyphPtr(Glyp
 

	
 
	if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) {
 
		DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), this->fs);
 
		this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
 
	}
 

	
 
	DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, this->fs);
 
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
 
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
 
	this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
 
}
 

	
 
void *AllocateFont(size_t size)
 
{
 
	return MallocT<byte>(size);
 
}
 

	
 

	
 
/* Check if a glyph should be rendered with anti-aliasing. */
 
static bool GetFontAAState(FontSize size)
 
{
 
	/* AA is only supported for 32 bpp */
 
	if (BlitterFactory::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;
 
@@ -346,25 +341,25 @@ const Sprite *TrueTypeFontCache::GetGlyp
 
#undef CPSET
 
#undef CP___
 
			static const SpriteLoader::Sprite builtin_questionmark = {
 
				10, // height
 
				8,  // width
 
				0,  // x_offs
 
				0,  // y_offs
 
				ST_FONT,
 
				SCC_PAL,
 
				builtin_questionmark_data
 
			};
 

	
 
			Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, AllocateFont);
 
			Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, SimpleSpriteAlloc);
 
			assert(spr != nullptr);
 
			GlyphEntry new_glyph;
 
			new_glyph.sprite = spr;
 
			new_glyph.width  = spr->width + (this->fs != FS_NORMAL);
 
			this->SetGlyphPtr(key, &new_glyph, false);
 
			return new_glyph.sprite;
 
		} else {
 
			/* Use '?' for missing characters. */
 
			this->GetGlyph(question_glyph);
 
			glyph = this->GetGlyphPtr(question_glyph);
 
			this->SetGlyphPtr(key, glyph, true);
 
			return glyph->sprite;
 
@@ -634,25 +629,25 @@ const Sprite *FreeTypeFontCache::Interna
 
	}
 

	
 
	for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
 
		for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
 
			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;
 
			}
 
		}
 
	}
 

	
 
	GlyphEntry new_glyph;
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
 
	new_glyph.width  = slot->advance.x >> 6;
 

	
 
	this->SetGlyphPtr(key, &new_glyph);
 

	
 
	return new_glyph.sprite;
 
}
 

	
 

	
 
GlyphID FreeTypeFontCache::MapCharToGlyph(WChar key)
 
{
 
	assert(IsPrintable(key));
 

	
src/fontcache_internal.h
Show inline comments
 
@@ -65,15 +65,13 @@ public:
 
	int GetFontSize() const override { return this->used_size; }
 
	SpriteID GetUnicodeGlyph(WChar key) override { return this->parent->GetUnicodeGlyph(key); }
 
	void SetUnicodeGlyph(WChar key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
 
	void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); }
 
	const Sprite *GetGlyph(GlyphID key) override;
 
	const void *GetFontTable(uint32 tag, size_t &length) override;
 
	void ClearFontCache() override;
 
	uint GetGlyphWidth(GlyphID key) override;
 
	bool GetDrawGlyphShadow() override;
 
	bool IsBuiltInFont() override { return false; }
 
};
 

	
 
void *AllocateFont(size_t size);
 

	
 
#endif /* FONTCACHE_INTERNAL_H */
src/os/macosx/font_osx.cpp
Show inline comments
 
@@ -324,25 +324,25 @@ const Sprite *CoreTextFontCache::Interna
 
		/* Extract pixel data. */
 
		for (uint y = 0; y < bb_height; y++) {
 
			for (uint x = 0; x < bb_width; x++) {
 
				if (bmp[y * pitch + x] > 0) {
 
					sprite.data[x + y * sprite.width].m = FACE_COLOUR;
 
					sprite.data[x + y * sprite.width].a = use_aa ? bmp[x + y * pitch] : 0xFF;
 
				}
 
			}
 
		}
 
	}
 

	
 
	GlyphEntry new_glyph;
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
 
	new_glyph.width = (byte)std::round(CTFontGetAdvancesForGlyphs(this->font.get(), kCTFontOrientationDefault, &glyph, nullptr, 1));
 
	this->SetGlyphPtr(key, &new_glyph);
 

	
 
	return new_glyph.sprite;
 
}
 

	
 
/**
 
 * Loads the TrueType font.
 
 * If a CoreText font description is present, e.g. from the automatic font
 
 * fallback search, use it. Otherwise, try to resolve it by font name.
 
 * @param fs The font size to load.
 
 */
src/os/windows/font_win32.cpp
Show inline comments
 
@@ -523,25 +523,25 @@ void Win32FontCache::ClearFontCache()
 

	
 
		for (uint y = 0; y < gm.gmBlackBoxY; y++) {
 
			for (uint x = 0; x < gm.gmBlackBoxX; x++) {
 
				if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
 
					sprite.data[x + y * sprite.width].m = FACE_COLOUR;
 
					sprite.data[x + y * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
 
				}
 
			}
 
		}
 
	}
 

	
 
	GlyphEntry new_glyph;
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
 
	new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
 
	new_glyph.width = gm.gmCellIncX;
 

	
 
	this->SetGlyphPtr(key, &new_glyph);
 

	
 
	return new_glyph.sprite;
 
}
 

	
 
/* virtual */ GlyphID Win32FontCache::MapCharToGlyph(WChar key)
 
{
 
	assert(IsPrintable(key));
 

	
 
	if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
src/spritecache.cpp
Show inline comments
 
@@ -800,24 +800,32 @@ static void *AllocSprite(size_t mem_req)
 

	
 
					return s->data;
 
				}
 
			}
 
		}
 

	
 
		/* Reached sentinel, but no block found yet. Delete some old entry. */
 
		DeleteEntryFromSpriteCache();
 
	}
 
}
 

	
 
/**
 
 * Sprite allocator simply using malloc.
 
 */
 
void *SimpleSpriteAlloc(size_t size)
 
{
 
	return MallocT<byte>(size);
 
}
 

	
 
/**
 
 * Handles the case when a sprite of different type is requested than is present in the SpriteCache.
 
 * For ST_FONT sprites, it is normal. In other cases, default sprite is loaded instead.
 
 * @param sprite ID of loaded sprite
 
 * @param requested requested sprite type
 
 * @param sc the currently known sprite cache for the requested sprite
 
 * @return fallback sprite
 
 * @note this function will do usererror() in the case the fallback sprite isn't available
 
 */
 
static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, AllocatorProc *allocator)
 
{
 
	static const char * const sprite_types[] = {
 
		"normal",        // ST_NORMAL
src/spritecache.h
Show inline comments
 
@@ -17,24 +17,25 @@
 
struct Sprite {
 
	uint16 height; ///< Height of the sprite.
 
	uint16 width;  ///< Width of the sprite.
 
	int16 x_offs;  ///< Number of pixels to shift the sprite to the right.
 
	int16 y_offs;  ///< Number of pixels to shift the sprite downwards.
 
	byte data[];   ///< Sprite data.
 
};
 

	
 
extern uint _sprite_cache_size;
 

	
 
typedef void *AllocatorProc(size_t size);
 

	
 
void *SimpleSpriteAlloc(size_t size);
 
void *GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator = nullptr, SpriteEncoder *encoder = nullptr);
 
bool SpriteExists(SpriteID sprite);
 

	
 
SpriteType GetSpriteType(SpriteID sprite);
 
uint GetOriginFileSlot(SpriteID sprite);
 
uint32 GetSpriteLocalID(SpriteID sprite);
 
uint GetSpriteCountForSlot(uint file_slot, SpriteID begin, SpriteID end);
 
uint GetMaxSpriteID();
 

	
 

	
 
static inline const Sprite *GetSprite(SpriteID sprite, SpriteType type)
 
{
0 comments (0 inline, 0 general)