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