From 1bd3bb1d58d714228cd3a0903debf5d9825f3ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Mon, 19 Dec 2005 01:01:04 +0000 Subject: [PATCH] * Added fallback mappings for the default fonts - since we don't save the mappings yet, that's a good way to test this functionality. * Turns out the directory of the mappings must be known already - they should be added automatically, but I've added them manually for now (which is okay for the default system directory). * Having more than one style with the same family in the mappings didn't work as planned - it now does. * On my current system, time spend in the font manager constructor went down from 1.5 secs to around 12000 usecs (and I have only a moderate number of fonts installed, or so I thought - looks like the mappings were a good idea :-)) - and that directly cuts down the boot time. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15580 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/servers/app/FontManager.h | 6 +- src/servers/app/FontManager.cpp | 108 ++++++++++++++++------ 2 files changed, 85 insertions(+), 29 deletions(-) diff --git a/headers/private/servers/app/FontManager.h b/headers/private/servers/app/FontManager.h index 082a4252ad..93b54f56b3 100644 --- a/headers/private/servers/app/FontManager.h +++ b/headers/private/servers/app/FontManager.h @@ -69,9 +69,11 @@ class FontManager : public BLooper { struct font_directory; struct font_mapping; + void _AddDefaultMapping(const char* family, const char* style, const char* path); bool _LoadRecentFontMappings(); - FontStyle* _GetDefaultStyle(const char *familyName, const char *styleName, - const char *fallbackFamily, const char *fallbackStyle, + status_t _AddMappedFont(const char* family, const char* style = NULL); + FontStyle* _GetDefaultStyle(const char* familyName, const char* styleName, + const char* fallbackFamily, const char* fallbackStyle, uint16 fallbackFace); status_t _SetDefaultFonts(); void _AddSystemPaths(); diff --git a/src/servers/app/FontManager.cpp b/src/servers/app/FontManager.cpp index 02a1bde28f..f11d357882 100644 --- a/src/servers/app/FontManager.cpp +++ b/src/servers/app/FontManager.cpp @@ -272,13 +272,75 @@ FontManager::SaveRecentFontMappings() } +void +FontManager::_AddDefaultMapping(const char* family, const char* style, + const char* path) +{ + font_mapping* mapping = new (nothrow) font_mapping; + if (mapping == NULL) + return; + + mapping->family = family; + mapping->style = style; + BEntry entry(path); + + if (entry.GetRef(&mapping->ref) != B_OK + || !entry.Exists() + || !fMappings.AddItem(mapping)) + delete mapping; +} + + bool FontManager::_LoadRecentFontMappings() { + // default known mappings + // TODO: load them for real, and use these as a fallback + + _AddDefaultMapping("Bitstream Vera Sans", "Roman", + "/boot/beos/etc/fonts/ttfonts/Vera.ttf"); + _AddDefaultMapping("Bitstream Vera Sans", "Bold", + "/boot/beos/etc/fonts/ttfonts/VeraBd.ttf"); + _AddDefaultMapping("Bitstream Vera Sans Mono", "Roman", + "/boot/beos/etc/fonts/ttfonts/VeraMono.ttf"); + return false; } +status_t +FontManager::_AddMappedFont(const char* familyName, const char* styleName) +{ + for (int32 i = 0; i < fMappings.CountItems(); i++) { + font_mapping* mapping = fMappings.ItemAt(i); + + if (mapping->family == familyName) { + if (styleName != NULL && mapping->style != styleName) + continue; + + BEntry entry(&mapping->ref); + if (entry.InitCheck() != B_OK) + continue; + + // find parent directory + + node_ref nodeRef; + nodeRef.device = mapping->ref.device; + nodeRef.node = mapping->ref.directory; + font_directory* directory = _FindDirectory(nodeRef); + if (directory == NULL) { + // unknown directory, maybe this is a user font + continue; + } + + return _AddFont(*directory, entry); + } + } + + return B_ENTRY_NOT_FOUND; +} + + /*! \brief Removes the style from the font directory. @@ -381,13 +443,19 @@ void FontManager::_AddSystemPaths() { BPath path; - if (find_directory(B_BEOS_FONTS_DIRECTORY, &path, true) == B_OK) + if (find_directory(B_BEOS_FONTS_DIRECTORY, &path, true) == B_OK) { _AddPath(path.Path()); + if (path.Append("ttfonts") == B_OK) + _AddPath(path.Path()); + } // We don't scan these in test mode to help shave off some startup time #if !TEST_MODE - if (find_directory(B_COMMON_FONTS_DIRECTORY, &path, true) == B_OK) + if (find_directory(B_COMMON_FONTS_DIRECTORY, &path, true) == B_OK) { _AddPath(path.Path()); + if (path.Append("ttfonts") == B_OK) + _AddPath(path.Path()); + } #endif } @@ -755,29 +823,8 @@ FontManager::GetFamily(const char* name) return family; // try font mappings before failing - for (int32 i = 0; i < fMappings.CountItems(); i++) { - font_mapping* mapping = fMappings.ItemAt(i); - - if (mapping->family == name) { - BEntry entry(&mapping->ref); - if (entry.InitCheck() != B_OK) - continue; - - // find parent directory - - node_ref nodeRef; - nodeRef.device = mapping->ref.device; - nodeRef.node = mapping->ref.directory; - font_directory* directory = _FindDirectory(nodeRef); - if (directory == NULL) { - // unknown directory, maybe this is a user font - continue; - } - - if (_AddFont(*directory, entry) == B_OK) - return _FindFamily(name); - } - } + if (_AddMappedFont(name) == B_OK) + return _FindFamily(name); _ScanFonts(); return _FindFamily(name); @@ -837,8 +884,15 @@ FontManager::GetStyle(const char* familyName, const char* styleName, uint16 fami // find style - if (styleName != NULL && styleName[0]) - return family->GetStyle(styleName); + if (styleName != NULL && styleName[0]) { + FontStyle* fontStyle = family->GetStyle(styleName); + if (fontStyle != NULL) + return fontStyle; + + // before we fail, we try the mappings for a match + if (_AddMappedFont(family->Name(), styleName) == B_OK) + return family->GetStyle(styleName); + } if (styleID != 0xffff) return family->GetStyleByID(styleID);