xref: /haiku/src/servers/app/font/AppFontManager.cpp (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
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,
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, 0, &face);
70 	if (error != 0)
71 		return B_ERROR;
72 
73 	status = _AddFont(face, nodeRef, path, familyID, styleID);
74 
75 	return status;
76 }
77 
78 
79 /*!	\brief Adds the FontFamily/FontStyle that is represented by the area in memory.
80 */
81 status_t
82 AppFontManager::AddUserFontFromMemory(const FT_Byte* fontAddress, size_t size,
83 	uint16& familyID, uint16& styleID)
84 {
85 	ASSERT(IsLocked());
86 
87 	node_ref nodeRef;
88 	status_t status;
89 
90 	FT_Face face;
91 	FT_Error error = FT_New_Memory_Face(gFreeTypeLibrary, fontAddress, size, 0,
92 		&face);
93 	if (error != 0)
94 		return B_ERROR;
95 
96 	status = _AddFont(face, nodeRef, "", familyID, styleID);
97 
98 	return status;
99 }
100 
101 
102 /*!	\brief Removes the FontFamily/FontStyle from the font manager.
103 */
104 status_t
105 AppFontManager::RemoveUserFont(uint16 familyID, uint16 styleID)
106 {
107 	return _RemoveFont(familyID, styleID) != NULL ? B_OK : B_BAD_VALUE;
108 }
109 
110 
111 uint16
112 AppFontManager::_NextID()
113 {
114 	return fNextID--;
115 }
116