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