Panda3D
freetypeFont.I
1 // Filename: freetypeFont.I
2 // Created by: drose (07Sep03)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: FreetypeFont::Destructor
18 // Access: Public
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE FreetypeFont::
22 ~FreetypeFont() {
23  unload_font();
24 }
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: FreetypeFont::set_point_size
28 // Access: Public
29 // Description: Sets the point size of the font. This controls the
30 // apparent size of the font onscreen. By convention, a
31 // 10 point font is about 1 screen unit high.
32 //
33 // This should only be called before any characters have
34 // been requested out of the font, or immediately after
35 // calling clear().
36 ////////////////////////////////////////////////////////////////////
37 INLINE bool FreetypeFont::
38 set_point_size(PN_stdfloat point_size) {
39  _point_size = point_size;
40  return reset_scale();
41 }
42 
43 ////////////////////////////////////////////////////////////////////
44 // Function: FreetypeFont::get_point_size
45 // Access: Public
46 // Description: Returns the point size of the font.
47 ////////////////////////////////////////////////////////////////////
48 INLINE PN_stdfloat FreetypeFont::
49 get_point_size() const {
50  return _point_size;
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: FreetypeFont::set_pixels_per_unit
55 // Access: Public
56 // Description: Set the resolution of the texture map, and hence the
57 // clarity of the resulting font. This sets the number
58 // of pixels in the texture map that are used for each
59 // onscreen unit.
60 //
61 // Setting this number larger results in an easier to
62 // read font, but at the cost of more texture memory.
63 //
64 // This should only be called before any characters have
65 // been requested out of the font, or immediately after
66 // calling clear().
67 ////////////////////////////////////////////////////////////////////
68 INLINE bool FreetypeFont::
69 set_pixels_per_unit(PN_stdfloat pixels_per_unit) {
70  _requested_pixels_per_unit = pixels_per_unit;
71  return reset_scale();
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: FreetypeFont::get_pixels_per_unit
76 // Access: Public
77 // Description: Returns the resolution of the texture map. See
78 // set_pixels_per_unit().
79 ////////////////////////////////////////////////////////////////////
80 INLINE PN_stdfloat FreetypeFont::
81 get_pixels_per_unit() const {
82  return _tex_pixels_per_unit;
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: FreetypeFont::set_pixel_size
87 // Access: Public
88 // Description: Computes the appropriate pixels_per_unit value to set
89 // the size of the font in the texture to the indicated
90 // number of pixels. This is just another way to
91 // specify pixels_per_unit().
92 ////////////////////////////////////////////////////////////////////
93 INLINE bool FreetypeFont::
94 set_pixel_size(PN_stdfloat pixel_size) {
95  return set_pixels_per_unit(pixel_size * _points_per_unit / _point_size);
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: FreetypeFont::get_pixel_size
100 // Access: Public
101 // Description: Returns the size of the font in pixels, as it appears
102 // in the texture.
103 ////////////////////////////////////////////////////////////////////
104 INLINE PN_stdfloat FreetypeFont::
105 get_pixel_size() const {
106  return _tex_pixels_per_unit * _point_size / _points_per_unit;
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: FreetypeFont::set_scale_factor
111 // Access: Public
112 // Description: Sets the factor by which the font is rendered larger
113 // by the FreeType library before being filtered down to
114 // its actual size in the texture as specified by
115 // set_pixels_per_unit(). This may be set to a number
116 // larger than 1.0 to improve the font's antialiasing
117 // (since FreeType doesn't really do a swell job of
118 // antialiasing by itself). There is some performance
119 // implication for setting this different than 1.0.
120 //
121 // This should only be called before any characters have
122 // been requested out of the font, or immediately after
123 // calling clear().
124 ////////////////////////////////////////////////////////////////////
125 INLINE bool FreetypeFont::
126 set_scale_factor(PN_stdfloat scale_factor) {
127  _requested_scale_factor = scale_factor;
128  return reset_scale();
129 }
130 
131 ////////////////////////////////////////////////////////////////////
132 // Function: FreetypeFont::get_scale_factor
133 // Access: Public
134 // Description: Returns the antialiasing scale factor. See
135 // set_scale_factor().
136 ////////////////////////////////////////////////////////////////////
137 INLINE PN_stdfloat FreetypeFont::
138 get_scale_factor() const {
139  return _scale_factor;
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: FreetypeFont::set_native_antialias
144 // Access: Public
145 // Description: Sets whether the Freetype library's built-in
146 // antialias mode is enabled. There are two unrelated
147 // ways to achieve antialiasing: with Freetype's native
148 // antialias mode, and with the use of a scale_factor
149 // greater than one. By default, both modes are
150 // enabled.
151 //
152 // At low resolutions, some fonts may do better with one
153 // mode or the other. In general, Freetype's native
154 // antialiasing will produce less blurry results, but
155 // may introduce more artifacts.
156 ////////////////////////////////////////////////////////////////////
157 INLINE void FreetypeFont::
158 set_native_antialias(bool native_antialias) {
159  _native_antialias = native_antialias;
160 }
161 
162 ////////////////////////////////////////////////////////////////////
163 // Function: FreetypeFont::get_native_antialias
164 // Access: Public
165 // Description: Returns whether Freetype's built-in antialias mode is
166 // enabled. See set_native_antialias().
167 ////////////////////////////////////////////////////////////////////
168 INLINE bool FreetypeFont::
169 get_native_antialias() const {
170  return _native_antialias;
171 }
172 
173 ////////////////////////////////////////////////////////////////////
174 // Function: FreetypeFont::get_font_pixel_size
175 // Access: Public
176 // Description: This is used to report whether the requested pixel
177 // size is being only approximated by a fixed-pixel-size
178 // font. This returns 0 in the normal case, in which a
179 // scalable font is used, or the fixed-pixel-size font
180 // has exactly the requested pixel size.
181 //
182 // If this returns non-zero, it is the pixel size of the
183 // font that we are using to approximate our desired
184 // size.
185 ////////////////////////////////////////////////////////////////////
186 INLINE int FreetypeFont::
187 get_font_pixel_size() const {
188  return _pixel_height;
189 }
190 
191 ////////////////////////////////////////////////////////////////////
192 // Function: FreetypeFont::get_line_height
193 // Access: Public
194 // Description: Returns the number of units high each line of text
195 // is.
196 ////////////////////////////////////////////////////////////////////
197 INLINE PN_stdfloat FreetypeFont::
198 get_line_height() const {
199  return _line_height;
200 }
201 
202 ////////////////////////////////////////////////////////////////////
203 // Function: FreetypeFont::get_space_advance
204 // Access: Public
205 // Description: Returns the number of units wide a space is.
206 ////////////////////////////////////////////////////////////////////
207 INLINE PN_stdfloat FreetypeFont::
208 get_space_advance() const {
209  return _space_advance;
210 }
211 
212 ////////////////////////////////////////////////////////////////////
213 // Function: FreetypeFont::get_points_per_unit
214 // Access: Public, Static
215 // Description: Returns the point size of the font that is one Panda
216 // unit high. This is an arbitrary Panda convention for
217 // text, and is set to 10.0.
218 ////////////////////////////////////////////////////////////////////
219 INLINE PN_stdfloat FreetypeFont::
220 get_points_per_unit() {
221  return _points_per_unit;
222 }
223 
224 ////////////////////////////////////////////////////////////////////
225 // Function: FreetypeFont::get_points_per_inch
226 // Access: Public, Static
227 // Description: Returns the number of points in one inch. This is a
228 // universal typographic convention.
229 ////////////////////////////////////////////////////////////////////
230 INLINE PN_stdfloat FreetypeFont::
231 get_points_per_inch() {
232  return _points_per_inch;
233 }
234 
235 ////////////////////////////////////////////////////////////////////
236 // Function: FreetypeFont::acquire_face
237 // Access: Protected
238 // Description: Retrieves the internal freetype face, and also
239 // acquires the lock.
240 //
241 // You must call release_face() when you are done using
242 // it, to release the lock.
243 ////////////////////////////////////////////////////////////////////
244 INLINE FT_Face FreetypeFont::
245 acquire_face() const {
246  nassertr(_face != NULL, NULL);
247  return _face->acquire_face(_char_size, _dpi, _pixel_width, _pixel_height);
248 }
249 
250 ////////////////////////////////////////////////////////////////////
251 // Function: FreetypeFont::release_face
252 // Access: Protected
253 // Description: Releases the lock acquired by a previous call to
254 // acquire_face(), and allows another thread to use the
255 // face.
256 ////////////////////////////////////////////////////////////////////
257 INLINE void FreetypeFont::
258 release_face(FT_Face face) const {
259  nassertv(_face != NULL);
260  _face->release_face(face);
261 }