xref: /haiku/src/servers/app/font/AppFontManager.cpp (revision 9f3bdf3d039430b5172c424def20ce5d9f7367d4)
1 /*
2  * Copyright 2001-2016, 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 
10 
11 /*!	Manages user font families and styles */
12 
13 
14 #include "AppFontManager.h"
15 
16 #include <new>
17 #include <stdint.h>
18 
19 #include <Debug.h>
20 #include <Entry.h>
21 
22 #include "FontFamily.h"
23 
24 
25 //#define TRACE_FONT_MANAGER
26 #ifdef TRACE_FONT_MANAGER
27 #	define FTRACE(x) debug_printf x
28 #else
29 #	define FTRACE(x) ;
30 #endif
31 
32 
33 extern FT_Library gFreeTypeLibrary;
34 
35 
36 //	#pragma mark -
37 
38 
39 /*! Sets high id number to avoid collisions with GlobalFontManager
40 	The result of a collision would be that the global font is selected
41 	rather than the application font.
42 */
43 AppFontManager::AppFontManager()
44 	: BLocker("AppFontManager")
45 {
46 	fNextID = UINT16_MAX;
47 }
48 
49 
50 /*!	\brief Adds the FontFamily/FontStyle that is represented by this path.
51 */
52 status_t
53 AppFontManager::AddUserFontFromFile(const char* path, uint16 index, uint16 instance,
54 	uint16& familyID, uint16& styleID)
55 {
56 	ASSERT(IsLocked());
57 
58 	BEntry entry;
59 	status_t status = entry.SetTo(path);
60 	if (status != B_OK)
61 		return status;
62 
63 	node_ref nodeRef;
64 	status = entry.GetNodeRef(&nodeRef);
65 	if (status < B_OK)
66 		return status;
67 
68 	FT_Face face;
69 	FT_Error error = FT_New_Face(gFreeTypeLibrary, path, index | (instance << 16), &face);
70 	if (error != 0)
71 		return B_ERROR;
72 
73 	status = _AddFont(face, nodeRef, path, familyID, styleID);
74 	return status;
75 }
76 
77 
78 /*!	\brief Adds the FontFamily/FontStyle that is represented by the area in memory.
79 */
80 status_t
81 AppFontManager::AddUserFontFromMemory(const FT_Byte* fontAddress, size_t size, uint16 index,
82 	uint16 instance, uint16& familyID, uint16& styleID)
83 {
84 	ASSERT(IsLocked());
85 
86 	node_ref nodeRef;
87 	status_t status;
88 
89 	FT_Face face;
90 	FT_Error error = FT_New_Memory_Face(gFreeTypeLibrary, fontAddress, size,
91 		index | (instance << 16), &face);
92 	if (error != 0)
93 		return B_ERROR;
94 
95 	status = _AddFont(face, nodeRef, "", familyID, styleID);
96 
97 	return status;
98 }
99 
100 
101 /*!	\brief Removes the FontFamily/FontStyle from the font manager.
102 */
103 status_t
104 AppFontManager::RemoveUserFont(uint16 familyID, uint16 styleID)
105 {
106 	return _RemoveFont(familyID, styleID) != NULL ? B_OK : B_BAD_VALUE;
107 }
108 
109 
110 uint16
111 AppFontManager::_NextID()
112 {
113 	return fNextID--;
114 }
115