dynamicTextFont.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Filename: dynamicTextFont.h
  2. // Created by: drose (08Feb02)
  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. #ifndef DYNAMICTEXTFONT_H
  15. #define DYNAMICTEXTFONT_H
  16. #include "pandabase.h"
  17. #ifdef HAVE_FREETYPE
  18. #include "textFont.h"
  19. #include "freetypeFont.h"
  20. #include "dynamicTextGlyph.h"
  21. #include "dynamicTextPage.h"
  22. #include "filename.h"
  23. #include "pvector.h"
  24. #include "pmap.h"
  25. #include <ft2build.h>
  26. #include FT_FREETYPE_H
  27. class NurbsCurveResult;
  28. ////////////////////////////////////////////////////////////////////
  29. // Class : DynamicTextFont
  30. // Description : A DynamicTextFont is a special TextFont object that
  31. // rasterizes its glyphs from a standard font file
  32. // (e.g. a TTF file) on the fly. It requires the
  33. // FreeType 2.0 library (or any higher,
  34. // backward-compatible version).
  35. ////////////////////////////////////////////////////////////////////
  36. class EXPCL_PANDA_TEXT DynamicTextFont : public TextFont, public FreetypeFont {
  37. PUBLISHED:
  38. DynamicTextFont(const Filename &font_filename, int face_index = 0);
  39. DynamicTextFont(const char *font_data, int data_length, int face_index);
  40. DynamicTextFont(const DynamicTextFont &copy);
  41. virtual ~DynamicTextFont();
  42. virtual PT(TextFont) make_copy() const;
  43. INLINE const string &get_name() const;
  44. INLINE bool set_point_size(float point_size);
  45. INLINE float get_point_size() const;
  46. INLINE bool set_pixels_per_unit(float pixels_per_unit);
  47. INLINE float get_pixels_per_unit() const;
  48. INLINE bool set_scale_factor(float scale_factor);
  49. INLINE float get_scale_factor() const;
  50. INLINE void set_native_antialias(bool native_antialias);
  51. INLINE bool get_native_antialias() const;
  52. INLINE int get_font_pixel_size() const;
  53. INLINE float get_line_height() const;
  54. INLINE float get_space_advance() const;
  55. INLINE void set_texture_margin(int texture_margin);
  56. INLINE int get_texture_margin() const;
  57. INLINE void set_poly_margin(float poly_margin);
  58. INLINE float get_poly_margin() const;
  59. INLINE void set_page_size(int x_size, int y_size);
  60. INLINE int get_page_x_size() const;
  61. INLINE int get_page_y_size() const;
  62. INLINE void set_minfilter(Texture::FilterType filter);
  63. INLINE Texture::FilterType get_minfilter() const;
  64. INLINE void set_magfilter(Texture::FilterType filter);
  65. INLINE Texture::FilterType get_magfilter() const;
  66. INLINE void set_anisotropic_degree(int anisotropic_degree);
  67. INLINE int get_anisotropic_degree() const;
  68. INLINE void set_render_mode(RenderMode render_mode);
  69. INLINE RenderMode get_render_mode() const;
  70. INLINE void set_winding_order(WindingOrder winding_order);
  71. INLINE WindingOrder get_winding_order() const;
  72. INLINE void set_fg(const Colorf &fg);
  73. INLINE const Colorf &get_fg() const;
  74. INLINE void set_bg(const Colorf &bg);
  75. INLINE const Colorf &get_bg() const;
  76. INLINE void set_outline(const Colorf &outline_color, float outline_width,
  77. float outline_feather);
  78. INLINE const Colorf &get_outline_color() const;
  79. INLINE float get_outline_width() const;
  80. INLINE float get_outline_feather() const;
  81. INLINE Texture::Format get_tex_format() const;
  82. int get_num_pages() const;
  83. DynamicTextPage *get_page(int n) const;
  84. MAKE_SEQ(get_pages, get_num_pages, get_page);
  85. int garbage_collect();
  86. void clear();
  87. virtual void write(ostream &out, int indent_level) const;
  88. public:
  89. virtual bool get_glyph(int character, const TextGlyph *&glyph);
  90. private:
  91. void initialize();
  92. void update_filters();
  93. void determine_tex_format();
  94. DynamicTextGlyph *make_glyph(int character, FT_Face face, int glyph_index);
  95. void copy_bitmap_to_texture(const FT_Bitmap &bitmap, DynamicTextGlyph *glyph);
  96. void copy_pnmimage_to_texture(const PNMImage &image, DynamicTextGlyph *glyph);
  97. void blend_pnmimage_to_texture(const PNMImage &image, DynamicTextGlyph *glyph,
  98. const Colorf &fg);
  99. DynamicTextGlyph *slot_glyph(int character, int x_size, int y_size);
  100. void render_wireframe_contours(DynamicTextGlyph *glyph);
  101. void render_polygon_contours(DynamicTextGlyph *glyph, bool face, bool extrude);
  102. static int outline_move_to(const FT_Vector *to, void *user);
  103. static int outline_line_to(const FT_Vector *to, void *user);
  104. static int outline_conic_to(const FT_Vector *control,
  105. const FT_Vector *to, void *user);
  106. static int outline_cubic_to(const FT_Vector *control1,
  107. const FT_Vector *control2,
  108. const FT_Vector *to, void *user);
  109. int outline_nurbs(NurbsCurveResult *ncr);
  110. int _texture_margin;
  111. float _poly_margin;
  112. int _page_x_size, _page_y_size;
  113. Texture::FilterType _minfilter;
  114. Texture::FilterType _magfilter;
  115. int _anisotropic_degree;
  116. RenderMode _render_mode;
  117. WindingOrder _winding_order;
  118. Colorf _fg, _bg, _outline_color;
  119. float _outline_width;
  120. float _outline_feather;
  121. bool _has_outline;
  122. Texture::Format _tex_format;
  123. bool _needs_image_processing;
  124. typedef pvector< PT(DynamicTextPage) > Pages;
  125. Pages _pages;
  126. int _preferred_page;
  127. // This doesn't need to be a reference-counting pointer, because the
  128. // reference to each glyph is kept by the DynamicTextPage object.
  129. typedef pmap<int, DynamicTextGlyph *> Cache;
  130. Cache _cache;
  131. // This is a list of the glyphs that do not have any printable
  132. // properties (e.g. space), but still have an advance measure. We
  133. // store them here to keep their reference counts; they also appear
  134. // in the above table.
  135. typedef pvector< PT(DynamicTextGlyph) > EmptyGlyphs;
  136. EmptyGlyphs _empty_glyphs;
  137. class ContourPoint {
  138. public:
  139. INLINE ContourPoint(const LPoint2f &p, const LVector2f &in,
  140. const LVector2f &out);
  141. INLINE ContourPoint(float px, float py, float tx, float ty);
  142. INLINE void connect_to(const LVector2f &out);
  143. LPoint2f _p;
  144. LVector2f _in, _out; // tangents into and out of the vertex.
  145. };
  146. typedef pvector<ContourPoint> Points;
  147. class Contour {
  148. public:
  149. Points _points;
  150. bool _is_solid;
  151. int _start_vertex;
  152. };
  153. typedef pvector<Contour> Contours;
  154. Contours _contours;
  155. LPoint2f _q; // The "current point".
  156. public:
  157. static TypeHandle get_class_type() {
  158. return _type_handle;
  159. }
  160. static void init_type() {
  161. TextFont::init_type();
  162. register_type(_type_handle, "DynamicTextFont",
  163. TextFont::get_class_type());
  164. }
  165. virtual TypeHandle get_type() const {
  166. return get_class_type();
  167. }
  168. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  169. private:
  170. static TypeHandle _type_handle;
  171. friend class TextNode;
  172. };
  173. INLINE ostream &operator << (ostream &out, const DynamicTextFont &dtf);
  174. #include "dynamicTextFont.I"
  175. #endif // HAVE_FREETYPE
  176. #endif