Panda3D

dynamicTextFont.h

00001 // Filename: dynamicTextFont.h
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 #ifndef DYNAMICTEXTFONT_H
00016 #define DYNAMICTEXTFONT_H
00017 
00018 #include "pandabase.h"
00019 
00020 #ifdef HAVE_FREETYPE
00021 
00022 #include "textFont.h"
00023 #include "freetypeFont.h"
00024 #include "dynamicTextGlyph.h"
00025 #include "dynamicTextPage.h"
00026 #include "filename.h"
00027 #include "pvector.h"
00028 #include "pmap.h"
00029 
00030 #include <ft2build.h>
00031 #include FT_FREETYPE_H
00032 
00033 class NurbsCurveResult;
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //       Class : DynamicTextFont
00037 // Description : A DynamicTextFont is a special TextFont object that
00038 //               rasterizes its glyphs from a standard font file
00039 //               (e.g. a TTF file) on the fly.  It requires the
00040 //               FreeType 2.0 library (or any higher,
00041 //               backward-compatible version).
00042 ////////////////////////////////////////////////////////////////////
00043 class EXPCL_PANDA_TEXT DynamicTextFont : public TextFont, public FreetypeFont {
00044 PUBLISHED:
00045   DynamicTextFont(const Filename &font_filename, int face_index = 0);
00046   DynamicTextFont(const char *font_data, int data_length, int face_index);
00047   DynamicTextFont(const DynamicTextFont &copy);
00048   virtual ~DynamicTextFont();
00049 
00050   virtual PT(TextFont) make_copy() const;
00051 
00052   INLINE const string &get_name() const;
00053 
00054   INLINE bool set_point_size(PN_stdfloat point_size);
00055   INLINE PN_stdfloat get_point_size() const;
00056 
00057   INLINE bool set_pixels_per_unit(PN_stdfloat pixels_per_unit);
00058   INLINE PN_stdfloat get_pixels_per_unit() const;
00059 
00060   INLINE bool set_scale_factor(PN_stdfloat scale_factor);
00061   INLINE PN_stdfloat get_scale_factor() const;
00062 
00063   INLINE void set_native_antialias(bool native_antialias);
00064   INLINE bool get_native_antialias() const;
00065 
00066   INLINE int get_font_pixel_size() const;
00067 
00068   INLINE PN_stdfloat get_line_height() const;
00069   INLINE PN_stdfloat get_space_advance() const;
00070 
00071   INLINE void set_texture_margin(int texture_margin);
00072   INLINE int get_texture_margin() const;
00073   INLINE void set_poly_margin(PN_stdfloat poly_margin);
00074   INLINE PN_stdfloat get_poly_margin() const;
00075 
00076   INLINE void set_page_size(int x_size, int y_size);
00077   INLINE int get_page_x_size() const;
00078   INLINE int get_page_y_size() const;
00079 
00080   INLINE void set_minfilter(Texture::FilterType filter);
00081   INLINE Texture::FilterType get_minfilter() const;
00082   INLINE void set_magfilter(Texture::FilterType filter);
00083   INLINE Texture::FilterType get_magfilter() const;
00084   INLINE void set_anisotropic_degree(int anisotropic_degree);
00085   INLINE int get_anisotropic_degree() const;
00086 
00087   INLINE void set_render_mode(RenderMode render_mode);
00088   INLINE RenderMode get_render_mode() const;
00089   INLINE void set_winding_order(WindingOrder winding_order);
00090   INLINE WindingOrder get_winding_order() const;
00091 
00092   INLINE void set_fg(const LColor &fg);
00093   INLINE const LColor &get_fg() const;
00094   INLINE void set_bg(const LColor &bg);
00095   INLINE const LColor &get_bg() const;
00096   INLINE void set_outline(const LColor &outline_color, PN_stdfloat outline_width,
00097                           PN_stdfloat outline_feather);
00098   INLINE const LColor &get_outline_color() const;
00099   INLINE PN_stdfloat get_outline_width() const;
00100   INLINE PN_stdfloat get_outline_feather() const;
00101   INLINE Texture::Format get_tex_format() const;
00102 
00103   int get_num_pages() const;
00104   DynamicTextPage *get_page(int n) const;
00105   MAKE_SEQ(get_pages, get_num_pages, get_page);
00106 
00107   int garbage_collect();
00108   void clear();
00109 
00110   virtual void write(ostream &out, int indent_level) const;
00111 
00112 public:
00113   virtual bool get_glyph(int character, const TextGlyph *&glyph);
00114 
00115 private:
00116   void initialize();
00117   void update_filters();
00118   void determine_tex_format();
00119   DynamicTextGlyph *make_glyph(int character, FT_Face face, int glyph_index);
00120   void copy_bitmap_to_texture(const FT_Bitmap &bitmap, DynamicTextGlyph *glyph);
00121   void copy_pnmimage_to_texture(const PNMImage &image, DynamicTextGlyph *glyph);
00122   void blend_pnmimage_to_texture(const PNMImage &image, DynamicTextGlyph *glyph,
00123                                  const LColor &fg);
00124   DynamicTextGlyph *slot_glyph(int character, int x_size, int y_size);
00125 
00126   void render_wireframe_contours(DynamicTextGlyph *glyph);
00127   void render_polygon_contours(DynamicTextGlyph *glyph, bool face, bool extrude);
00128 
00129   static int outline_move_to(const FT_Vector *to, void *user);
00130   static int outline_line_to(const FT_Vector *to, void *user);
00131   static int outline_conic_to(const FT_Vector *control,
00132                               const FT_Vector *to, void *user);
00133   static int outline_cubic_to(const FT_Vector *control1, 
00134                               const FT_Vector *control2, 
00135                               const FT_Vector *to, void *user);
00136   int outline_nurbs(NurbsCurveResult *ncr);
00137 
00138   int _texture_margin;
00139   PN_stdfloat _poly_margin;
00140   int _page_x_size, _page_y_size;
00141 
00142   Texture::FilterType _minfilter;
00143   Texture::FilterType _magfilter;
00144   int _anisotropic_degree;
00145 
00146   RenderMode _render_mode;
00147   WindingOrder _winding_order;
00148 
00149   LColor _fg, _bg, _outline_color;
00150   PN_stdfloat _outline_width;
00151   PN_stdfloat _outline_feather;
00152   bool _has_outline;
00153   Texture::Format _tex_format;
00154   bool _needs_image_processing;
00155 
00156   typedef pvector< PT(DynamicTextPage) > Pages;
00157   Pages _pages;
00158   int _preferred_page;
00159 
00160   // This doesn't need to be a reference-counting pointer, because the
00161   // reference to each glyph is kept by the DynamicTextPage object.
00162   typedef pmap<int, DynamicTextGlyph *> Cache;
00163   Cache _cache;
00164 
00165   // This is a list of the glyphs that do not have any printable
00166   // properties (e.g. space), but still have an advance measure.  We
00167   // store them here to keep their reference counts; they also appear
00168   // in the above table.
00169   typedef pvector< PT(DynamicTextGlyph) > EmptyGlyphs;
00170   EmptyGlyphs _empty_glyphs;
00171 
00172   class ContourPoint {
00173   public:
00174     INLINE ContourPoint(const LPoint2 &p, const LVector2 &in, 
00175                         const LVector2 &out);
00176     INLINE ContourPoint(PN_stdfloat px, PN_stdfloat py, PN_stdfloat tx, PN_stdfloat ty);
00177     INLINE void connect_to(const LVector2 &out);
00178     LPoint2 _p;
00179     LVector2 _in, _out;  // tangents into and out of the vertex.
00180   };
00181   typedef pvector<ContourPoint> Points;
00182 
00183   class Contour {
00184   public:
00185     Points _points;
00186     bool _is_solid;
00187     int _start_vertex;
00188   };
00189 
00190   typedef pvector<Contour> Contours;
00191   Contours _contours;
00192   LPoint2 _q;  // The "current point".
00193 
00194 public:
00195   static TypeHandle get_class_type() {
00196     return _type_handle;
00197   }
00198   static void init_type() {
00199     TextFont::init_type();
00200     register_type(_type_handle, "DynamicTextFont",
00201                   TextFont::get_class_type());
00202   }
00203   virtual TypeHandle get_type() const {
00204     return get_class_type();
00205   }
00206   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00207 
00208 private:
00209   static TypeHandle _type_handle;
00210 
00211   friend class TextNode;
00212 };
00213 
00214 INLINE ostream &operator << (ostream &out, const DynamicTextFont &dtf);
00215 
00216 #include "dynamicTextFont.I"
00217 
00218 #endif  // HAVE_FREETYPE
00219 
00220 #endif
 All Classes Functions Variables Enumerations