diff options
author | Johannes Schickel | 2006-04-16 19:54:39 +0000 |
---|---|---|
committer | Johannes Schickel | 2006-04-16 19:54:39 +0000 |
commit | e9cbf5fcbd1af19d1936593d1de736c0e0e91f89 (patch) | |
tree | db05f01ba83a3fc96dabb366f73cf53e2f7ff604 | |
parent | 74edd90aba15db1196b16b5eae918347670d11c8 (diff) | |
download | scummvm-rg350-e9cbf5fcbd1af19d1936593d1de736c0e0e91f89.tar.gz scummvm-rg350-e9cbf5fcbd1af19d1936593d1de736c0e0e91f89.tar.bz2 scummvm-rg350-e9cbf5fcbd1af19d1936593d1de736c0e0e91f89.zip |
- Moves Graphics::loadFont to Graphics::NewFont::loadFont
- adds code for caching of fonts (just for Graphics::NewFont)
- implements cache using in the modern theme for custom fonts
- adds cache file for current normal font in the theme zip file
svn-id: r21952
-rw-r--r-- | graphics/font.cpp | 111 | ||||
-rw-r--r-- | graphics/font.h | 7 | ||||
-rw-r--r-- | gui/ThemeNew.cpp | 102 | ||||
-rw-r--r-- | gui/theme.h | 1 | ||||
-rw-r--r-- | gui/themes/modern.zip | bin | 32709 -> 34689 bytes |
5 files changed, 191 insertions, 30 deletions
diff --git a/graphics/font.cpp b/graphics/font.cpp index 40a53a3179..18379d7e8c 100644 --- a/graphics/font.cpp +++ b/graphics/font.cpp @@ -21,6 +21,8 @@ #include "common/stdafx.h" #include "common/stream.h" +#include "common/file.h" +#include "common/endian.h" #include "graphics/font.h" namespace Graphics { @@ -538,7 +540,7 @@ bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2) { return val; } -NewFont *loadFont(Common::SeekableReadStream &stream) { +NewFont *NewFont::loadFont(Common::SeekableReadStream &stream) { NewFontData *data = bdf_read_font(stream); if (!data) return 0; @@ -559,12 +561,100 @@ NewFont *loadFont(Common::SeekableReadStream &stream) { return new NewFont(desc, data); } -NewFont *loadFont(const byte *src, uint32 size) { - Common::MemoryReadStream stream(src, size); +bool NewFont::cacheFontData(const NewFont &font, const Common::String &filename) { + Common::File cacheFile; + if (!cacheFile.open(filename, Common::File::kFileWriteMode)) { + warning("Couldn't open file '%s' for writing", filename.c_str()); + return false; + } - NewFontData *data = bdf_read_font(stream); - if (!data) + cacheFile.writeUint16BE(font.desc.maxwidth); + cacheFile.writeUint16BE(font.desc.height); + cacheFile.writeUint16BE(font.desc.ascent); + cacheFile.writeUint16BE(font.desc.firstchar); + cacheFile.writeUint16BE(font.desc.size); + cacheFile.writeUint16BE(font.desc.defaultchar); + cacheFile.writeUint32BE(font.desc.bits_size); + + for (long i = 0; i < font.desc.bits_size; ++i) { + cacheFile.writeUint16BE(font.desc.bits[i]); + } + + if (font.desc.offset) { + cacheFile.writeByte(1); + for (int i = 0; i < font.desc.size; ++i) { + cacheFile.writeUint32BE(font.desc.offset[i]); + } + } else { + cacheFile.writeByte(0); + } + + if (font.desc.width) { + cacheFile.writeByte(1); + for (int i = 0; i < font.desc.size; ++i) { + cacheFile.writeByte(font.desc.width[i]); + } + } else { + cacheFile.writeByte(0); + } + + return !cacheFile.ioFailed(); +} + +NewFont *NewFont::loadFromCache(Common::SeekableReadStream &stream) { + NewFont *font = 0; + + NewFontData *data = (NewFontData *)malloc(sizeof(NewFontData)); + if (!data) return 0; + + memset(data, 0, sizeof(NewFontData)); + + data->maxwidth = stream.readUint16BE(); + data->height = stream.readUint16BE(); + data->ascent = stream.readUint16BE(); + data->firstchar = stream.readUint16BE(); + data->size = stream.readUint16BE(); + data->defaultchar = stream.readUint16BE(); + data->bits_size = stream.readUint32BE(); + + data->bits = (bitmap_t*)malloc(sizeof(bitmap_t)*data->bits_size); + if (!data->bits) { + free(data); return 0; + } + + for (long i = 0; i < data->bits_size; ++i) { + data->bits[i] = stream.readUint16BE(); + } + + bool hasOffsetTable = stream.readByte(); + if (hasOffsetTable) { + data->offset = (unsigned long*)malloc(sizeof(unsigned long)*data->size); + if (!data->offset) { + free(data->bits); + free(data); + return 0; + } + + for (int i = 0; i < data->size; ++i) { + data->offset[i] = stream.readUint32BE(); + } + } + + bool hasWidthTable = stream.readByte(); + if (hasWidthTable) { + data->width = (unsigned char*)malloc(sizeof(unsigned char)*data->size); + if (!data->width) { + free(data->bits); + free(data->offset); + free(data); + return 0; + } + + for (int i = 0; i < data->size; ++i) { + data->width[i] = stream.readByte(); + } + } FontDesc desc; desc.name = data->name; @@ -579,7 +669,16 @@ NewFont *loadFont(const byte *src, uint32 size) { desc.defaultchar = data->defaultchar; desc.bits_size = data->bits_size; - return new NewFont(desc, data); + font = new NewFont(desc, data); + if (!font || stream.ioFailed()) { + free(data->bits); + free(data->offset); + free(data->width); + free(data); + return 0; + } + + return font; } #pragma mark - diff --git a/graphics/font.h b/graphics/font.h index 66a8e84222..5e8db40610 100644 --- a/graphics/font.h +++ b/graphics/font.h @@ -123,10 +123,11 @@ public: virtual int getCharWidth(byte chr) const; virtual void drawChar(Surface *dst, byte chr, int x, int y, uint32 color) const; -}; -NewFont *loadFont(Common::SeekableReadStream &stream); -NewFont *loadFont(const byte *src, uint32 size); + static NewFont *loadFont(Common::SeekableReadStream &stream); + static bool cacheFontData(const NewFont &font, const Common::String &filename); + static NewFont *loadFromCache(Common::SeekableReadStream &stream); +}; #if (defined(PALMOS_ARM) || defined(PALMOS_DEBUG) || defined(__GP32__)) # define DEFINE_FONT(n) \ diff --git a/gui/ThemeNew.cpp b/gui/ThemeNew.cpp index 1a225c3ab8..11b737ffb1 100644 --- a/gui/ThemeNew.cpp +++ b/gui/ThemeNew.cpp @@ -1341,38 +1341,98 @@ void ThemeNew::deleteFonts() { } const Graphics::Font *ThemeNew::loadFont(const char *filename) { - const Graphics::Font *font = 0; - + const Graphics::NewFont *font = 0; + Common::String cacheFilename = genCacheFilename(filename); Common::File fontFile; - if (fontFile.open(filename)) { - font = Graphics::loadFont(fontFile); + + if (cacheFilename != "") { + if (fontFile.open(cacheFilename)) + font = Graphics::NewFont::loadFromCache(fontFile); if (font) return font; + +#ifdef USE_ZLIB + unzFile zipFile = unzOpen((_stylefile + ".zip").c_str()); + if (zipFile && unzLocateFile(zipFile, cacheFilename.c_str(), 2) == UNZ_OK) { + unz_file_info fileInfo; + unzOpenCurrentFile(zipFile); + unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0); + uint8 *buffer = new uint8[fileInfo.uncompressed_size+1]; + assert(buffer); + memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8)); + unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size); + unzCloseCurrentFile(zipFile); + Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1); + + font = Graphics::NewFont::loadFromCache(stream); + + delete [] buffer; + buffer = 0; + } + unzClose(zipFile); +#endif + if (font) + return font; + } + + printf("normal font open!\n"); + fflush(stdout); + + // normal open + if (fontFile.open(filename)) { + font = Graphics::NewFont::loadFont(fontFile); } #ifdef USE_ZLIB - unzFile zipFile = unzOpen((_stylefile + ".zip").c_str()); - if (zipFile && unzLocateFile(zipFile, filename, 2) == UNZ_OK) { - unz_file_info fileInfo; - unzOpenCurrentFile(zipFile); - unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0); - uint8 *buffer = new uint8[fileInfo.uncompressed_size+1]; - assert(buffer); - memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8)); - unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size); - unzCloseCurrentFile(zipFile); - Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1); - - font = Graphics::loadFont(stream); - - delete [] buffer; - buffer = 0; + if (!font) { + unzFile zipFile = unzOpen((_stylefile + ".zip").c_str()); + if (zipFile && unzLocateFile(zipFile, filename, 2) == UNZ_OK) { + unz_file_info fileInfo; + unzOpenCurrentFile(zipFile); + unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0); + uint8 *buffer = new uint8[fileInfo.uncompressed_size+1]; + assert(buffer); + memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8)); + unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size); + unzCloseCurrentFile(zipFile); + Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1); + + font = Graphics::NewFont::loadFont(stream); + + delete [] buffer; + buffer = 0; + } + unzClose(zipFile); } - unzClose(zipFile); #endif + + if (font) { + if (cacheFilename != "") { + if (!Graphics::NewFont::cacheFontData(*font, cacheFilename)) { + warning("Couldn't create cache file for font '%s'", filename); + } + } + } + return font; } +Common::String ThemeNew::genCacheFilename(const char *filename) { + Common::String cacheName = filename; + for (int i = cacheName.size() - 1; i >= 0; --i) { + if (cacheName[i] == '.') { + while ((uint)i < cacheName.size() - 1) { + cacheName.deleteLastChar(); + } + + cacheName += "fcc"; + return cacheName; + } + } + + return ""; +} + #pragma mark - OverlayColor ThemeNew::calcLuminance(OverlayColor col) { diff --git a/gui/theme.h b/gui/theme.h index 734ff3c37a..8b0e59a6de 100644 --- a/gui/theme.h +++ b/gui/theme.h @@ -364,6 +364,7 @@ private: void setupFonts(); void deleteFonts(); const Graphics::Font *loadFont(const char *filename); + Common::String genCacheFilename(const char *filename); const Graphics::Font *_fonts[kFontStyleMax]; public: diff --git a/gui/themes/modern.zip b/gui/themes/modern.zip Binary files differindex 8c44d3e16f..1817123542 100644 --- a/gui/themes/modern.zip +++ b/gui/themes/modern.zip |