master chld/haikuprg / decor / MacDecorator / includes / FontManager.h
  1/*
  2 * Copyright 2001-2009, Haiku.
  3 * Distributed under the terms of the MIT License.
  4 *
  5 * Authors:
  6 *		DarkWyrm <bpmagic@columbus.rr.com>
  7 *		Axel Dörfler, axeld@pinc-software.de
  8 */
  9#ifndef FONT_MANAGER_H
 10#define FONT_MANAGER_H
 11
 12
 13#include <AutoDeleter.h>
 14#include <HashMap.h>
 15#include <Looper.h>
 16#include <ObjectList.h>
 17#include <Referenceable.h>
 18
 19#include <ft2build.h>
 20#include FT_FREETYPE_H
 21
 22class BEntry;
 23class BPath;
 24struct node_ref;
 25
 26class FontFamily;
 27class FontStyle;
 28class ServerFont;
 29
 30
 31/*!
 32	\class FontManager FontManager.h
 33	\brief Manager for the largest part of the font subsystem
 34*/
 35class FontManager : public BLooper {
 36public:
 37								FontManager();
 38	virtual						~FontManager();
 39
 40			status_t			InitCheck() { return fInitStatus; }
 41			void				SaveRecentFontMappings();
 42
 43	virtual	void				MessageReceived(BMessage* message);
 44
 45			int32				CheckRevision(uid_t user);
 46			int32				CountFamilies();
 47
 48			int32				CountStyles(const char* family);
 49			int32				CountStyles(uint16 familyID);
 50			FontFamily*			FamilyAt(int32 index) const;
 51
 52			FontFamily*			GetFamily(uint16 familyID) const;
 53			FontFamily*			GetFamily(const char* name);
 54
 55			FontStyle*			GetStyleByIndex(const char* family,
 56									int32 index);
 57			FontStyle*			GetStyleByIndex(uint16 familyID, int32 index);
 58			FontStyle*			GetStyle(const char* family, const char* style,
 59									uint16 familyID = 0xffff,
 60									uint16 styleID = 0xffff, uint16 face = 0);
 61			FontStyle*			GetStyle(const char *family, uint16 styleID);
 62			FontStyle*			GetStyle(uint16 familyID,
 63									uint16 styleID) const;
 64			FontStyle*			FindStyleMatchingFace(uint16 face) const;
 65
 66			void				RemoveStyle(FontStyle* style);
 67				// This call must not be used by anything else than class
 68				// FontStyle.
 69
 70			const ServerFont*	DefaultPlainFont() const;
 71			const ServerFont*	DefaultBoldFont() const;
 72			const ServerFont*	DefaultFixedFont() const;
 73
 74			void				AttachUser(uid_t userID);
 75			void				DetachUser(uid_t userID);
 76
 77private:
 78			struct font_directory;
 79			struct font_mapping;
 80
 81			void				_AddDefaultMapping(const char* family,
 82									const char* style, const char* path);
 83			bool				_LoadRecentFontMappings();
 84			status_t			_AddMappedFont(const char* family,
 85									const char* style = NULL);
 86			FontStyle*			_GetDefaultStyle(const char* familyName,
 87									const char* styleName,
 88									const char* fallbackFamily,
 89									const char* fallbackStyle,
 90									uint16 fallbackFace);
 91			status_t			_SetDefaultFonts();
 92			void				_PrecacheFontFile(const ServerFont* font);
 93			void				_AddSystemPaths();
 94			font_directory*		_FindDirectory(node_ref& nodeRef);
 95			void				_RemoveDirectory(font_directory* directory);
 96			status_t			_CreateDirectories(const char* path);
 97			status_t			_AddPath(const char* path);
 98			status_t			_AddPath(BEntry& entry,
 99									font_directory** _newDirectory = NULL);
100
101			void				_RemoveStyle(font_directory& directory,
102									FontStyle* style);
103			void				_RemoveStyle(dev_t device, uint64 directory,
104									uint64 node);
105			FontFamily*			_FindFamily(const char* family) const;
106
107			void				_ScanFontsIfNecessary();
108			void				_ScanFonts();
109			status_t			_ScanFontDirectory(font_directory& directory);
110			status_t			_AddFont(font_directory& directory,
111									BEntry& entry);
112
113			FT_CharMap			_GetSupportedCharmap(const FT_Face& face);
114
115private:
116			struct FontKey {
117				FontKey(uint16 family, uint16 style)
118					: familyID(family), styleID(style) {}
119
120				uint32 GetHashCode() const
121				{
122					return familyID | (styleID << 16UL);
123				}
124
125				bool operator==(const FontKey& other) const
126				{
127					return familyID == other.familyID
128						&& styleID == other.styleID;
129				}
130
131				uint16 familyID, styleID;
132			};
133
134private:
135			status_t			fInitStatus;
136
137			typedef BObjectList<font_directory>		DirectoryList;
138			typedef BObjectList<font_mapping>		MappingList;
139			typedef BObjectList<FontFamily>			FamilyList;
140
141			DirectoryList		fDirectories;
142			MappingList			fMappings;
143			FamilyList			fFamilies;
144
145			HashMap<FontKey, BReference<FontStyle> > fStyleHashTable;
146
147			ObjectDeleter<ServerFont>
148								fDefaultPlainFont;
149			ObjectDeleter<ServerFont>
150								fDefaultBoldFont;
151			ObjectDeleter<ServerFont>
152								fDefaultFixedFont;
153
154			bool				fScanned;
155			int32				fNextID;
156};
157
158extern FT_Library gFreeTypeLibrary;
159extern FontManager* gFontManager;
160
161#endif	/* FONT_MANAGER_H */