Panda3D
|
00001 // Filename: dynamicTextFont.I 00002 // Created by: drose (08Feb02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: DynamicTextFont::get_name 00018 // Access: Published 00019 // Description: Disambiguates the get_name() method between that 00020 // inherited from TextFont and that inherited from 00021 // FreetypeFont. 00022 //////////////////////////////////////////////////////////////////// 00023 INLINE const string &DynamicTextFont:: 00024 get_name() const { 00025 return TextFont::get_name(); 00026 } 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: DynamicTextFont::set_point_size 00030 // Access: Published 00031 // Description: Sets the point size of the font. This controls the 00032 // apparent size of the font onscreen. By convention, a 00033 // 10 point font is about 1 screen unit high. 00034 // 00035 // This should only be called before any characters have 00036 // been requested out of the font, or immediately after 00037 // calling clear(). 00038 //////////////////////////////////////////////////////////////////// 00039 INLINE bool DynamicTextFont:: 00040 set_point_size(PN_stdfloat point_size) { 00041 // If this assertion fails, you didn't call clear() first. RTFM. 00042 nassertr(get_num_pages() == 0, false); 00043 00044 return FreetypeFont::set_point_size(point_size); 00045 } 00046 00047 //////////////////////////////////////////////////////////////////// 00048 // Function: DynamicTextFont::get_point_size 00049 // Access: Published 00050 // Description: Returns the point size of the font. 00051 //////////////////////////////////////////////////////////////////// 00052 INLINE PN_stdfloat DynamicTextFont:: 00053 get_point_size() const { 00054 return FreetypeFont::get_point_size(); 00055 } 00056 00057 //////////////////////////////////////////////////////////////////// 00058 // Function: DynamicTextFont::set_pixels_per_unit 00059 // Access: Published 00060 // Description: Set the resolution of the texture map, and hence the 00061 // clarity of the resulting font. This sets the number 00062 // of pixels in the texture map that are used for each 00063 // onscreen unit. 00064 // 00065 // Setting this number larger results in an easier to 00066 // read font, but at the cost of more texture memory. 00067 // 00068 // This should only be called before any characters have 00069 // been requested out of the font, or immediately after 00070 // calling clear(). 00071 //////////////////////////////////////////////////////////////////// 00072 INLINE bool DynamicTextFont:: 00073 set_pixels_per_unit(PN_stdfloat pixels_per_unit) { 00074 // If this assertion fails, you didn't call clear() first. RTFM. 00075 nassertr(get_num_pages() == 0, false); 00076 00077 return FreetypeFont::set_pixels_per_unit(pixels_per_unit); 00078 } 00079 00080 //////////////////////////////////////////////////////////////////// 00081 // Function: DynamicTextFont::get_pixels_per_unit 00082 // Access: Published 00083 // Description: Returns the resolution of the texture map. See 00084 // set_pixels_per_unit(). 00085 //////////////////////////////////////////////////////////////////// 00086 INLINE PN_stdfloat DynamicTextFont:: 00087 get_pixels_per_unit() const { 00088 return FreetypeFont::get_pixels_per_unit(); 00089 } 00090 00091 //////////////////////////////////////////////////////////////////// 00092 // Function: DynamicTextFont::set_scale_factor 00093 // Access: Published 00094 // Description: Sets the factor by which the font is rendered larger 00095 // by the FreeType library before being filtered down to 00096 // its actual size in the texture as specified by 00097 // set_pixels_per_unit(). This may be set to a number 00098 // larger than 1.0 to improve the font's antialiasing 00099 // (since FreeType doesn't really do a swell job of 00100 // antialiasing by itself). There is some performance 00101 // implication for setting this different than 1.0, but 00102 // it is probably small. 00103 // 00104 // This should only be called before any characters have 00105 // been requested out of the font, or immediately after 00106 // calling clear(). 00107 //////////////////////////////////////////////////////////////////// 00108 INLINE bool DynamicTextFont:: 00109 set_scale_factor(PN_stdfloat scale_factor) { 00110 // If this assertion fails, you didn't call clear() first. RTFM. 00111 nassertr(get_num_pages() == 0, false); 00112 00113 return FreetypeFont::set_scale_factor(scale_factor); 00114 } 00115 00116 //////////////////////////////////////////////////////////////////// 00117 // Function: DynamicTextFont::get_scale_factor 00118 // Access: Published 00119 // Description: Returns the antialiasing scale factor. See 00120 // set_scale_factor(). 00121 //////////////////////////////////////////////////////////////////// 00122 INLINE PN_stdfloat DynamicTextFont:: 00123 get_scale_factor() const { 00124 return FreetypeFont::get_scale_factor(); 00125 } 00126 00127 //////////////////////////////////////////////////////////////////// 00128 // Function: DynamicTextFont::set_native_antialias 00129 // Access: Published 00130 // Description: Sets whether the Freetype library's built-in 00131 // antialias mode is enabled. There are two unrelated 00132 // ways to achieve antialiasing: with Freetype's native 00133 // antialias mode, and with the use of a scale_factor 00134 // greater than one. By default, both modes are 00135 // enabled. 00136 // 00137 // At low resolutions, some fonts may do better with one 00138 // mode or the other. In general, Freetype's native 00139 // antialiasing will produce less blurry results, but 00140 // may introduce more artifacts. 00141 //////////////////////////////////////////////////////////////////// 00142 INLINE void DynamicTextFont:: 00143 set_native_antialias(bool native_antialias) { 00144 // If this assertion fails, you didn't call clear() first. RTFM. 00145 nassertv(get_num_pages() == 0); 00146 00147 FreetypeFont::set_native_antialias(native_antialias); 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: DynamicTextFont::get_native_antialias 00152 // Access: Published 00153 // Description: Returns whether Freetype's built-in antialias mode is 00154 // enabled. See set_native_antialias(). 00155 //////////////////////////////////////////////////////////////////// 00156 INLINE bool DynamicTextFont:: 00157 get_native_antialias() const { 00158 return FreetypeFont::get_native_antialias(); 00159 } 00160 00161 //////////////////////////////////////////////////////////////////// 00162 // Function: DynamicTextFont::get_font_pixel_size 00163 // Access: Published 00164 // Description: This is used to report whether the requested pixel 00165 // size is being only approximated by a fixed-pixel-size 00166 // font. This returns 0 in the normal case, in which a 00167 // scalable font is used, or the fixed-pixel-size font 00168 // has exactly the requested pixel size. 00169 // 00170 // If this returns non-zero, it is the pixel size of the 00171 // font that we are using to approximate our desired 00172 // size. 00173 //////////////////////////////////////////////////////////////////// 00174 INLINE int DynamicTextFont:: 00175 get_font_pixel_size() const { 00176 return FreetypeFont::get_font_pixel_size(); 00177 } 00178 00179 //////////////////////////////////////////////////////////////////// 00180 // Function: DynamicTextFont::get_line_height 00181 // Access: Published 00182 // Description: Returns the number of units high each line of text 00183 // is. 00184 //////////////////////////////////////////////////////////////////// 00185 INLINE PN_stdfloat DynamicTextFont:: 00186 get_line_height() const { 00187 return TextFont::get_line_height(); 00188 } 00189 00190 //////////////////////////////////////////////////////////////////// 00191 // Function: DynamicTextFont::get_space_advance 00192 // Access: Published 00193 // Description: Returns the number of units wide a space is. 00194 //////////////////////////////////////////////////////////////////// 00195 INLINE PN_stdfloat DynamicTextFont:: 00196 get_space_advance() const { 00197 return TextFont::get_space_advance(); 00198 } 00199 00200 //////////////////////////////////////////////////////////////////// 00201 // Function: DynamicTextFont::set_texture_margin 00202 // Access: Published 00203 // Description: Sets the number of pixels of padding that is added 00204 // around the border of each glyph before adding it to 00205 // the texture map. This reduces the bleed in from 00206 // neighboring glyphs in the texture map. 00207 //////////////////////////////////////////////////////////////////// 00208 INLINE void DynamicTextFont:: 00209 set_texture_margin(int texture_margin) { 00210 _texture_margin = texture_margin; 00211 } 00212 00213 //////////////////////////////////////////////////////////////////// 00214 // Function: DynamicTextFont::get_texture_margin 00215 // Access: Published 00216 // Description: Returns the number of pixels of padding that is added 00217 // around the border of each glyph in the texture map. 00218 // See set_texture_margin(). 00219 //////////////////////////////////////////////////////////////////// 00220 INLINE int DynamicTextFont:: 00221 get_texture_margin() const { 00222 return _texture_margin; 00223 } 00224 00225 //////////////////////////////////////////////////////////////////// 00226 // Function: DynamicTextFont::set_poly_margin 00227 // Access: Published 00228 // Description: Sets the number of pixels of padding that is included 00229 // around each glyph in the generated polygons. This 00230 // helps prevent the edges of the glyphs from being cut 00231 // off at small minifications. It is not related to the 00232 // amount of extra pixels reserved in the texture map 00233 // (but it should be set somewhat smaller than this 00234 // number, which is controlled by set_texture_margin(), 00235 // to prevent bleed-in from neighboring letters in the 00236 // texture). 00237 //////////////////////////////////////////////////////////////////// 00238 INLINE void DynamicTextFont:: 00239 set_poly_margin(PN_stdfloat poly_margin) { 00240 _poly_margin = poly_margin; 00241 } 00242 00243 //////////////////////////////////////////////////////////////////// 00244 // Function: DynamicTextFont::get_poly_margin 00245 // Access: Published 00246 // Description: Returns the number of pixels of padding that is 00247 // included around each glyph in the generated polygons. 00248 // See set_poly_margin(). 00249 //////////////////////////////////////////////////////////////////// 00250 INLINE PN_stdfloat DynamicTextFont:: 00251 get_poly_margin() const { 00252 return _poly_margin; 00253 } 00254 00255 //////////////////////////////////////////////////////////////////// 00256 // Function: DynamicTextFont::set_page_size 00257 // Access: Published 00258 // Description: Sets the x, y size of the textures that are created 00259 // for the DynamicTextFont. 00260 //////////////////////////////////////////////////////////////////// 00261 INLINE void DynamicTextFont:: 00262 set_page_size(int x_size, int y_size) { 00263 _page_x_size = x_size; 00264 _page_y_size = y_size; 00265 } 00266 00267 //////////////////////////////////////////////////////////////////// 00268 // Function: DynamicTextFont::get_page_x_size 00269 // Access: Published 00270 // Description: Returns the x size of the textures that are created 00271 // for the DynamicTextFont. See set_page_size(). 00272 //////////////////////////////////////////////////////////////////// 00273 INLINE int DynamicTextFont:: 00274 get_page_x_size() const { 00275 return _page_x_size; 00276 } 00277 00278 //////////////////////////////////////////////////////////////////// 00279 // Function: DynamicTextFont::get_page_y_size 00280 // Access: Published 00281 // Description: Returns the y size of the textures that are created 00282 // for the DynamicTextFont. See set_page_size(). 00283 //////////////////////////////////////////////////////////////////// 00284 INLINE int DynamicTextFont:: 00285 get_page_y_size() const { 00286 return _page_y_size; 00287 } 00288 00289 //////////////////////////////////////////////////////////////////// 00290 // Function: DynamicTextFont::set_minfilter 00291 // Access: Published 00292 // Description: Sets the filter type used when minimizing the 00293 // textures created for this font. 00294 //////////////////////////////////////////////////////////////////// 00295 INLINE void DynamicTextFont:: 00296 set_minfilter(Texture::FilterType filter) { 00297 _minfilter = filter; 00298 update_filters(); 00299 } 00300 00301 //////////////////////////////////////////////////////////////////// 00302 // Function: DynamicTextFont::get_minfilter 00303 // Access: Published 00304 // Description: Returns the filter type used when minimizing the 00305 // textures created for this font. 00306 //////////////////////////////////////////////////////////////////// 00307 INLINE Texture::FilterType DynamicTextFont:: 00308 get_minfilter() const { 00309 return _minfilter; 00310 } 00311 00312 //////////////////////////////////////////////////////////////////// 00313 // Function: DynamicTextFont::set_magfilter 00314 // Access: Published 00315 // Description: Sets the filter type used when enlarging the 00316 // textures created for this font. 00317 //////////////////////////////////////////////////////////////////// 00318 INLINE void DynamicTextFont:: 00319 set_magfilter(Texture::FilterType filter) { 00320 _magfilter = filter; 00321 update_filters(); 00322 } 00323 00324 //////////////////////////////////////////////////////////////////// 00325 // Function: DynamicTextFont::get_magfilter 00326 // Access: Published 00327 // Description: Returns the filter type used when enlarging the 00328 // textures created for this font. 00329 //////////////////////////////////////////////////////////////////// 00330 INLINE Texture::FilterType DynamicTextFont:: 00331 get_magfilter() const { 00332 return _magfilter; 00333 } 00334 00335 //////////////////////////////////////////////////////////////////// 00336 // Function: DynamicTextFont::set_anisotropic_degree 00337 // Access: Published 00338 // Description: Enables or disables anisotropic filtering on the 00339 // textures created for this font. The default value is 00340 // specified by the text-anisotropic-degree variable. 00341 // See Texture::set_anisotropic_degree(). 00342 //////////////////////////////////////////////////////////////////// 00343 INLINE void DynamicTextFont:: 00344 set_anisotropic_degree(int anisotropic_degree) { 00345 _anisotropic_degree = anisotropic_degree; 00346 update_filters(); 00347 } 00348 00349 //////////////////////////////////////////////////////////////////// 00350 // Function: DynamicTextFont::get_anisotropic_degree 00351 // Access: Published 00352 // Description: Returns the current anisotropic degree for textures 00353 // created for this font. See set_anisotropic_degree(). 00354 //////////////////////////////////////////////////////////////////// 00355 INLINE int DynamicTextFont:: 00356 get_anisotropic_degree() const { 00357 return _anisotropic_degree; 00358 } 00359 00360 //////////////////////////////////////////////////////////////////// 00361 // Function: DynamicTextFont::set_render_mode 00362 // Access: Published 00363 // Description: Specifies the way the glyphs on this particular font 00364 // are generated. The default is RM_texture, which is 00365 // the only mode supported for bitmap fonts. Other modes 00366 // are possible for most modern fonts. 00367 //////////////////////////////////////////////////////////////////// 00368 INLINE void DynamicTextFont:: 00369 set_render_mode(DynamicTextFont::RenderMode render_mode) { 00370 _render_mode = render_mode; 00371 } 00372 00373 //////////////////////////////////////////////////////////////////// 00374 // Function: DynamicTextFont::get_render_mode 00375 // Access: Published 00376 // Description: Returns the way the glyphs on this particular font 00377 // are generated. See set_render_mode(). 00378 //////////////////////////////////////////////////////////////////// 00379 INLINE DynamicTextFont::RenderMode DynamicTextFont:: 00380 get_render_mode() const { 00381 return _render_mode; 00382 } 00383 00384 //////////////////////////////////////////////////////////////////// 00385 // Function: DynamicTextFont::set_winding_order 00386 // Access: Published 00387 // Description: Specifies an explicitly winding order on this 00388 // particular font. This is only necessary if the 00389 // render_mode is RM_polygon or RM_solid, and only if 00390 // FreeType appears to guess wrong on this font. 00391 // Normally, you should leave this at WO_default. 00392 //////////////////////////////////////////////////////////////////// 00393 INLINE void DynamicTextFont:: 00394 set_winding_order(DynamicTextFont::WindingOrder winding_order) { 00395 _winding_order = winding_order; 00396 } 00397 00398 //////////////////////////////////////////////////////////////////// 00399 // Function: DynamicTextFont::get_winding_order 00400 // Access: Published 00401 // Description: Returns the winding order set via set_winding_order(). 00402 //////////////////////////////////////////////////////////////////// 00403 INLINE DynamicTextFont::WindingOrder DynamicTextFont:: 00404 get_winding_order() const { 00405 return _winding_order; 00406 } 00407 00408 //////////////////////////////////////////////////////////////////// 00409 // Function: DynamicTextFont::set_fg 00410 // Access: Published 00411 // Description: Changes the color of the foreground pixels of the 00412 // font as they are rendered into the font texture. The 00413 // default is (1, 1, 1, 1), or opaque white, which 00414 // allows text created with the font to be colored 00415 // individually. Normally, you would not change this 00416 // unless you really need a particular color effect to 00417 // appear in the font itself. 00418 // 00419 // This should only be called before any characters have 00420 // been requested out of the font, or immediately after 00421 // calling clear(). 00422 //////////////////////////////////////////////////////////////////// 00423 INLINE void DynamicTextFont:: 00424 set_fg(const LColor &fg) { 00425 // If this assertion fails, you didn't call clear() first. RTFM. 00426 nassertv(get_num_pages() == 0); 00427 00428 _fg = fg; 00429 determine_tex_format(); 00430 } 00431 00432 //////////////////////////////////////////////////////////////////// 00433 // Function: DynamicTextFont::get_fg 00434 // Access: Published 00435 // Description: Returns the color of the foreground pixels of the 00436 // font as they are rendered into the font texture. 00437 // See set_fg(). 00438 //////////////////////////////////////////////////////////////////// 00439 INLINE const LColor &DynamicTextFont:: 00440 get_fg() const { 00441 return _fg; 00442 } 00443 00444 //////////////////////////////////////////////////////////////////// 00445 // Function: DynamicTextFont::set_bg 00446 // Access: Published 00447 // Description: Changes the color of the background pixels of the 00448 // font as they are rendered into the font texture. The 00449 // default is (1, 1, 1, 0), or transparent white, which 00450 // allows text created with the font to be colored 00451 // individually. (Note that it should not generally be 00452 // (0, 0, 0, 0), which would tend to bleed into the 00453 // foreground color, unless you have also specified a 00454 // outline color of (0, 0, 0, 1)) . 00455 // 00456 // Normally, you would not change this unless you really 00457 // need a particular color effect to appear in the font 00458 // itself. 00459 // 00460 // This should only be called before any characters have 00461 // been requested out of the font, or immediately after 00462 // calling clear(). 00463 //////////////////////////////////////////////////////////////////// 00464 INLINE void DynamicTextFont:: 00465 set_bg(const LColor &bg) { 00466 // If this assertion fails, you didn't call clear() first. RTFM. 00467 nassertv(get_num_pages() == 0); 00468 00469 _bg = bg; 00470 determine_tex_format(); 00471 } 00472 00473 //////////////////////////////////////////////////////////////////// 00474 // Function: DynamicTextFont::get_bg 00475 // Access: Published 00476 // Description: Returns the color of the background pixels of the 00477 // font as they are rendered into the font texture. 00478 // See set_bg(). 00479 //////////////////////////////////////////////////////////////////// 00480 INLINE const LColor &DynamicTextFont:: 00481 get_bg() const { 00482 return _bg; 00483 } 00484 00485 00486 //////////////////////////////////////////////////////////////////// 00487 // Function: DynamicTextFont::set_outline 00488 // Access: Published 00489 // Description: Sets up the font to have an outline around each font 00490 // letter. This is achieved via a Gaussian post-process 00491 // as each letter is generated; there is some runtime 00492 // cost for this effect, but it is minimal as each 00493 // letter is normally generated only once and then 00494 // cached. 00495 // 00496 // The color is the desired color of the outline, width 00497 // is the number of points beyond the letter that the 00498 // outline extends (a typical font is 10 points high), 00499 // and feather is a number in the range 0.0 .. 1.0 that 00500 // controls the softness of the outline. Set the width 00501 // to 0.0 to disable the outline. 00502 // 00503 // This should only be called before any characters have 00504 // been requested out of the font, or immediately after 00505 // calling clear(). 00506 //////////////////////////////////////////////////////////////////// 00507 INLINE void DynamicTextFont:: 00508 set_outline(const LColor &outline_color, PN_stdfloat outline_width, 00509 PN_stdfloat outline_feather) { 00510 // If this assertion fails, you didn't call clear() first. RTFM. 00511 nassertv(get_num_pages() == 0); 00512 00513 _outline_color = outline_color; 00514 _outline_width = outline_width; 00515 _outline_feather = outline_feather; 00516 determine_tex_format(); 00517 } 00518 00519 //////////////////////////////////////////////////////////////////// 00520 // Function: DynamicTextFont::get_outline_color 00521 // Access: Published 00522 // Description: Returns the color of the outline pixels of the 00523 // font as they are rendered into the font texture. 00524 // See set_outline(). 00525 //////////////////////////////////////////////////////////////////// 00526 INLINE const LColor &DynamicTextFont:: 00527 get_outline_color() const { 00528 return _outline_color; 00529 } 00530 00531 //////////////////////////////////////////////////////////////////// 00532 // Function: DynamicTextFont::get_outline_width 00533 // Access: Published 00534 // Description: Returns the width of the outline pixels of the 00535 // font, as the number of points beyond each letter. 00536 // See set_outline(). 00537 //////////////////////////////////////////////////////////////////// 00538 INLINE PN_stdfloat DynamicTextFont:: 00539 get_outline_width() const { 00540 return _outline_width; 00541 } 00542 00543 //////////////////////////////////////////////////////////////////// 00544 // Function: DynamicTextFont::get_outline_feather 00545 // Access: Published 00546 // Description: Returns the softness of the outline pixels of the 00547 // font, as a value in the range 0.0 to 1.0. 00548 // See set_outline(). 00549 //////////////////////////////////////////////////////////////////// 00550 INLINE PN_stdfloat DynamicTextFont:: 00551 get_outline_feather() const { 00552 return _outline_feather; 00553 } 00554 00555 //////////////////////////////////////////////////////////////////// 00556 // Function: DynamicTextFont::get_tex_format 00557 // Access: Published 00558 // Description: Returns the texture format used to render the 00559 // individual pages. This is set automatically 00560 // according to the colors selected. 00561 //////////////////////////////////////////////////////////////////// 00562 INLINE Texture::Format DynamicTextFont:: 00563 get_tex_format() const { 00564 return _tex_format; 00565 } 00566 00567 //////////////////////////////////////////////////////////////////// 00568 // Function: DynamicTextFont::ContourPoint::Constructor 00569 // Access: Public 00570 // Description: 00571 //////////////////////////////////////////////////////////////////// 00572 INLINE DynamicTextFont::ContourPoint:: 00573 ContourPoint(const LPoint2 &p, const LVector2 &in, const LVector2 &out) : 00574 _p(p), _in(in), _out(out) 00575 { 00576 } 00577 00578 //////////////////////////////////////////////////////////////////// 00579 // Function: DynamicTextFont::ContourPoint::Constructor 00580 // Access: Public 00581 // Description: 00582 //////////////////////////////////////////////////////////////////// 00583 INLINE DynamicTextFont::ContourPoint:: 00584 ContourPoint(PN_stdfloat px, PN_stdfloat py, PN_stdfloat tx, PN_stdfloat ty) : 00585 _p(px, py), _in(tx, ty), _out(tx, ty) 00586 { 00587 } 00588 00589 //////////////////////////////////////////////////////////////////// 00590 // Function: DynamicTextFont::connect_to 00591 // Access: Public 00592 // Description: Connects the indicated point to the next point, whose 00593 // tangent is given. The given tangent becomes the out 00594 // tangent at this point. If the in tangent and out 00595 // tangent are sufficiently close, they will be smoothed 00596 // together. 00597 //////////////////////////////////////////////////////////////////// 00598 INLINE void DynamicTextFont::ContourPoint:: 00599 connect_to(const LVector2 &out) { 00600 _out = out; 00601 if (_in.dot(_out) > 0.7071) { 00602 // Less than 45 degrees of difference: smooth them. 00603 LVector2 av = (_in + _out) * 0.5f; 00604 av.normalize(); 00605 _in = av; 00606 _out = av; 00607 } 00608 } 00609 00610 00611 INLINE ostream & 00612 operator << (ostream &out, const DynamicTextFont &dtf) { 00613 return out << dtf.get_name(); 00614 }