xref: /haiku/docs/develop/servers/app_server/FontServer.rst (revision a5061ecec55353a5f394759473f1fd6df04890da)
1*a5061eceSAdrien DestuguesFontServer class
2*a5061eceSAdrien Destugues################
3*a5061eceSAdrien Destugues
4*a5061eceSAdrien DestuguesThe FontServer provides the base functionality for providing font
5*a5061eceSAdrien Destuguessupport for the rest of the system and insulates the rest of the server
6*a5061eceSAdrien Destuguesfrom having to deal too much with FreeType.
7*a5061eceSAdrien Destugues
8*a5061eceSAdrien DestuguesMember Functions
9*a5061eceSAdrien Destugues================
10*a5061eceSAdrien Destugues
11*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
12*a5061eceSAdrien Destugues| FontServer(void)                  | ~FontServer(void)                 |
13*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
14*a5061eceSAdrien Destugues| void Lock(void)                   | void Unlock(void)                 |
15*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
16*a5061eceSAdrien Destugues| void SaveList(void)               | status_t ScanDirectory(const char |
17*a5061eceSAdrien Destugues|                                   | \*path)                           |
18*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
19*a5061eceSAdrien Destugues| FontStyle \*GetFont(font_family   | FontInstance                      |
20*a5061eceSAdrien Destugues| family, font_style face)          | \*GetInstance(font_family family, |
21*a5061eceSAdrien Destugues|                                   | font_style face, int16 size,      |
22*a5061eceSAdrien Destugues|                                   | int16 rotation, int16 shear)      |
23*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
24*a5061eceSAdrien Destugues| int32 CountFamiles(void)          | status_t IsInitialized(void)      |
25*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
26*a5061eceSAdrien Destugues| int32 CountStyles(font_family     | FontStyle \*GetStyle(font_family  |
27*a5061eceSAdrien Destugues| family)                           | family, font_style style)         |
28*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
29*a5061eceSAdrien Destugues| void RemoveFamily(const char      | FontFamily \*_FindFamily(const    |
30*a5061eceSAdrien Destugues| \*family)                         | char \*name)                      |
31*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
32*a5061eceSAdrien Destugues| ServerFont \*GetSystemPlain(void) | ServerFont \*GetSystemBold(void)  |
33*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
34*a5061eceSAdrien Destugues| ServerFont \*GetSystemFixed(void) | bool SetSystemPlain(const char    |
35*a5061eceSAdrien Destugues|                                   | \*family, const char \*style,     |
36*a5061eceSAdrien Destugues|                                   | float size)                       |
37*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
38*a5061eceSAdrien Destugues| void RemoveUnusedFamilies(void)   | bool FontsNeedUpdated(void)       |
39*a5061eceSAdrien Destugues+-----------------------------------+-----------------------------------+
40*a5061eceSAdrien Destugues
41*a5061eceSAdrien DestuguesFontServer(void)
42*a5061eceSAdrien Destugues----------------
43*a5061eceSAdrien Destugues
44*a5061eceSAdrien Destugues1. Create access semaphore
45*a5061eceSAdrien Destugues2. Call FT_Init_FreeType()
46*a5061eceSAdrien Destugues3. If no error initializing the FreeType library, set init flag to true
47*a5061eceSAdrien Destugues
48*a5061eceSAdrien Destugues~FontServer(void)
49*a5061eceSAdrien Destugues-----------------
50*a5061eceSAdrien Destugues
51*a5061eceSAdrien Destugues1. Call FT_Done_FreeType()
52*a5061eceSAdrien Destugues
53*a5061eceSAdrien Destuguesvoid Lock(void), void Unlock(void)
54*a5061eceSAdrien Destugues----------------------------------
55*a5061eceSAdrien Destugues
56*a5061eceSAdrien DestuguesThese functions simply acquire and release the internal access
57*a5061eceSAdrien Destuguessemaphore.
58*a5061eceSAdrien Destugues
59*a5061eceSAdrien Destuguesvoid SaveList(void)
60*a5061eceSAdrien Destugues-------------------
61*a5061eceSAdrien Destugues
62*a5061eceSAdrien DestuguesSaves the list of all scanned and valid font families and styles to
63*a5061eceSAdrien Destuguesdisk
64*a5061eceSAdrien Destugues
65*a5061eceSAdrien Destugues
66*a5061eceSAdrien Destugues1. create a BMessage for storing font family data (hereafter, the font message)
67*a5061eceSAdrien Destugues2. create a BMessage for storing a list of font family messages (hereafter, the list message)
68*a5061eceSAdrien Destugues3. create a boolean tuned flag and a boolean fixed flag
69*a5061eceSAdrien Destugues4. Iterate through all families
70*a5061eceSAdrien Destugues
71*a5061eceSAdrien Destugues   A. for each family, get its name and add its name to the font message as "name"
72*a5061eceSAdrien Destugues   B. iterate through the families styles
73*a5061eceSAdrien Destugues
74*a5061eceSAdrien Destugues      a. get the style's name, and if valid, add it to the font message as "styles"
75*a5061eceSAdrien Destugues      b. if IsTuned and IsScalable, set the tuned flag to true
76*a5061eceSAdrien Destugues      c. if IsFixedWidth, set the fixed flag to true
77*a5061eceSAdrien Destugues
78*a5061eceSAdrien Destugues   C. if the tuned flag is set, add a boolean true to the font message as "tuned"
79*a5061eceSAdrien Destugues   D. if the fixed flag is set, add a boolean true to the font message as "fixed"
80*a5061eceSAdrien Destugues   E. add the font message to the list message as "family"
81*a5061eceSAdrien Destugues   F. empty the font message
82*a5061eceSAdrien Destugues
83*a5061eceSAdrien Destugues5. Create a BFile from the path definition SERVER_FONT_LIST for Read/Write, creating the file if nonexistent and erasing any existing one
84*a5061eceSAdrien Destugues6. Flatten the list message to the created BFile object
85*a5061eceSAdrien Destugues7. Set the needs_update flag to false
86*a5061eceSAdrien Destugues
87*a5061eceSAdrien Destugues
88*a5061eceSAdrien Destuguesstatus_t ScanDirectory(const char \*path)
89*a5061eceSAdrien Destugues-----------------------------------------
90*a5061eceSAdrien Destugues
91*a5061eceSAdrien DestuguesScanDirectory is where the brunt of the work of the FontServer is done: scan the directory of all fonts which can be loaded.
92*a5061eceSAdrien Destugues
93*a5061eceSAdrien Destugues1. Make a BDirectory object pointer at the path parameter. If the init code is not B_OK, return it.
94*a5061eceSAdrien Destugues2. Enter a while() loop, iterating through each entry in the given directory, executing as follows:
95*a5061eceSAdrien Destugues
96*a5061eceSAdrien Destugues   a. Ensure that the entry is not '.' or '..'
97*a5061eceSAdrien Destugues   b. Call FT_New_Face on the entry's full path
98*a5061eceSAdrien Destugues   c. If a valid FT_Face is returned, iterate through to see if there are any supported character mappings in the current entry.
99*a5061eceSAdrien Destugues   d. If there are no supported character mappings, dump the supported mappings to debug output, call FT_Done_Face(), and continue to the next entry
100*a5061eceSAdrien Destugues   e. See if the entry's family has been added to the family list. If it hasn't, create one and add it.
101*a5061eceSAdrien Destugues   f. Check to see if the font's style has been added to its family. If so, call FT_Done_Face, and continue to the next entry
102*a5061eceSAdrien Destugues   g. If the style has not been added, create a new SFont for that family and face, increment the font count, and continue to the next entry.
103*a5061eceSAdrien Destugues
104*a5061eceSAdrien Destugues3. set the needs_update flag to true
105*a5061eceSAdrien Destugues4. Return B_OK
106*a5061eceSAdrien Destugues
107*a5061eceSAdrien DestuguesSupported character mappings are Windows and Apple Unicode, Windows
108*a5061eceSAdrien Destuguessymbol, and Apple Roman character mappings, in order of preference
109*a5061eceSAdrien Destuguesfrom first to last.
110*a5061eceSAdrien Destugues
111*a5061eceSAdrien DestuguesFontStyle \*GetFont(font_family family, font_style face)
112*a5061eceSAdrien Destugues--------------------------------------------------------
113*a5061eceSAdrien Destugues
114*a5061eceSAdrien DestuguesReturns an FontStyle object for the specified family and style or NULL
115*a5061eceSAdrien Destuguesif not found.
116*a5061eceSAdrien Destugues
117*a5061eceSAdrien Destugues1. Call \_FindFamily() for the given family
118*a5061eceSAdrien Destugues2. If non-NULL, call its FindStyle() method
119*a5061eceSAdrien Destugues3. Return the result
120*a5061eceSAdrien Destugues
121*a5061eceSAdrien DestuguesFontInstance \*GetInstance(font_family family, font_style face, int16 size, int16 rotation, int16 shear)
122*a5061eceSAdrien Destugues--------------------------------------------------------------------------------------------------------
123*a5061eceSAdrien Destugues
124*a5061eceSAdrien DestuguesReturns a usable instance of a specified font object with specified
125*a5061eceSAdrien Destuguesproperties.
126*a5061eceSAdrien Destugues
127*a5061eceSAdrien Destugues
128*a5061eceSAdrien Destugues1. Duplicates and performs the code found in GetFont
129*a5061eceSAdrien Destugues2. Assuming that the FontStyle object is non-NULL, it calls its GetInstance method and returns the result.
130*a5061eceSAdrien Destugues
131*a5061eceSAdrien Destugues
132*a5061eceSAdrien Destuguesint32 CountFamilies(void)
133*a5061eceSAdrien Destugues-------------------------
134*a5061eceSAdrien Destugues
135*a5061eceSAdrien DestuguesReturns the number of valid font families available to the system.
136*a5061eceSAdrien Destugues
137*a5061eceSAdrien Destugues
138*a5061eceSAdrien Destugues1. Return the number of items in the family list
139*a5061eceSAdrien Destugues
140*a5061eceSAdrien Destugues
141*a5061eceSAdrien Destuguesstatus_t IsInitialized(void)
142*a5061eceSAdrien Destugues----------------------------
143*a5061eceSAdrien Destugues
144*a5061eceSAdrien DestuguesReturns the initialization status variable
145*a5061eceSAdrien Destugues
146*a5061eceSAdrien Destugues
147*a5061eceSAdrien Destuguesint32 CountStyles(font_family family)
148*a5061eceSAdrien Destugues-------------------------------------
149*a5061eceSAdrien Destugues
150*a5061eceSAdrien DestuguesReturns the number of styles available for a given font family.
151*a5061eceSAdrien Destugues
152*a5061eceSAdrien Destugues
153*a5061eceSAdrien Destugues1. Call \_FindFamily() to get the appropriate font family
154*a5061eceSAdrien Destugues2. If non-NULL, call its return the result of its CountStyles method
155*a5061eceSAdrien Destugues
156*a5061eceSAdrien DestuguesFontStyle \*GetStyle(font_family family, font_style style)
157*a5061eceSAdrien Destugues----------------------------------------------------------
158*a5061eceSAdrien Destugues
159*a5061eceSAdrien DestuguesGets the FontStyle object of the family, style, and flags.
160*a5061eceSAdrien Destugues
161*a5061eceSAdrien Destugues1. Call \_FindFamily() to get the appropriate font family
162*a5061eceSAdrien Destugues2. If non-NULL, call the family's GetStyle method on the font_style parameter and return the result
163*a5061eceSAdrien Destugues3. If family is NULL, return NULL
164*a5061eceSAdrien Destugues
165*a5061eceSAdrien Destuguesvoid RemoveFamily(const char \*family)
166*a5061eceSAdrien Destugues--------------------------------------
167*a5061eceSAdrien Destugues
168*a5061eceSAdrien DestuguesRemoves a font family from the family list
169*a5061eceSAdrien Destugues
170*a5061eceSAdrien Destugues1. Look up font family in the family list via \_FindFamily()
171*a5061eceSAdrien Destugues2. If it exists, delete it
172*a5061eceSAdrien Destugues
173*a5061eceSAdrien DestuguesFontFamily \*_FindFamily(const char \*name)
174*a5061eceSAdrien Destugues-------------------------------------------
175*a5061eceSAdrien Destugues
176*a5061eceSAdrien DestuguesLooks up a FontFamily object based on its family name. Returns NULL if not found.
177*a5061eceSAdrien Destugues
178*a5061eceSAdrien Destugues1. Call the family list's find() method.
179*a5061eceSAdrien Destugues2. Return the appropriate FontFamily object or NULL if not found.
180*a5061eceSAdrien Destugues
181*a5061eceSAdrien DestuguesServerFont \*GetSystemPlain(void), ServerFont \*GetSystemBold(void), ServerFont \*GetSystemFixed(void)
182*a5061eceSAdrien Destugues------------------------------------------------------------------------------------------------------
183*a5061eceSAdrien Destugues
184*a5061eceSAdrien DestuguesThese return a copy of a pointer to the system-wide ServerFont objects
185*a5061eceSAdrien Destugueswhich represent the appropriate system font settings. It is the
186*a5061eceSAdrien Destuguesresponsibility of the caller to delete the object returned. NULL is
187*a5061eceSAdrien Destuguesreturned if no setting has been set for a particular system font.
188*a5061eceSAdrien Destugues
189*a5061eceSAdrien Destugues
190*a5061eceSAdrien Destuguesbool SetSystemPlain(const char \*family, const char \*style, float size)
191*a5061eceSAdrien Destugues------------------------------------------------------------------------
192*a5061eceSAdrien Destugues
193*a5061eceSAdrien Destuguesbool SetSystemBold(const char \*family, const char \*style, float size)
194*a5061eceSAdrien Destugues-----------------------------------------------------------------------
195*a5061eceSAdrien Destugues
196*a5061eceSAdrien Destuguesbool SetSystemFixed(const char \*family, const char \*style, float size)
197*a5061eceSAdrien Destugues------------------------------------------------------------------------
198*a5061eceSAdrien Destugues
199*a5061eceSAdrien Destugues
200*a5061eceSAdrien DestuguesThe system fonts settings may be set via these calls by specifying the
201*a5061eceSAdrien Destuguesfamily, style, and size. They return true if everything worked out ok
202*a5061eceSAdrien Destuguesand false if not. Settings are not changed if false is returned.
203*a5061eceSAdrien Destugues
204*a5061eceSAdrien Destugues1. Call \_FindFamily on the family parameter. if NULL, return false
205*a5061eceSAdrien Destugues2. Call the family's GetStyle member. if NULL, return false
206*a5061eceSAdrien Destugues3. if the appropriate system font pointer is non-NULL, delete it
207*a5061eceSAdrien Destugues4. call the style's Instantiate member with the size parameter
208*a5061eceSAdrien Destugues
209*a5061eceSAdrien Destugues
210*a5061eceSAdrien Destuguesvoid RemoveUnusedFamilies(void)
211*a5061eceSAdrien Destugues-------------------------------
212*a5061eceSAdrien Destugues
213*a5061eceSAdrien DestuguesThe purpose of this function is to allow for a complete rescan of the
214*a5061eceSAdrien Destuguesfonts in the appropriate directories.
215*a5061eceSAdrien Destugues
216*a5061eceSAdrien Destugues
217*a5061eceSAdrien Destugues1. Iterate through the family list
218*a5061eceSAdrien Destugues
219*a5061eceSAdrien Destugues   A. Get a family
220*a5061eceSAdrien Destugues   B. if it has no dependents, remove it from the list and delete it
221*a5061eceSAdrien Destugues
222*a5061eceSAdrien Destugues2. Set the needs_update flag to true
223*a5061eceSAdrien Destugues
224*a5061eceSAdrien Destugues
225*a5061eceSAdrien Destuguesbool FontsNeedUpdated(void)
226*a5061eceSAdrien Destugues---------------------------
227*a5061eceSAdrien Destugues
228*a5061eceSAdrien DestuguesReturns the value of the needs_update flag
229