Panda3D
dynamicTextFont.h
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 
15 #ifndef DYNAMICTEXTFONT_H
16 #define DYNAMICTEXTFONT_H
17 
18 #include "pandabase.h"
19 
20 #ifdef HAVE_FREETYPE
21 
22 #include "textFont.h"
23 #include "freetypeFont.h"
24 #include "dynamicTextGlyph.h"
25 #include "dynamicTextPage.h"
26 #include "filename.h"
27 #include "pvector.h"
28 #include "pmap.h"
29 
30 #include <ft2build.h>
31 #include FT_FREETYPE_H
32 
33 class NurbsCurveResult;
34 
35 ////////////////////////////////////////////////////////////////////
36 // Class : DynamicTextFont
37 // Description : A DynamicTextFont is a special TextFont object that
38 // rasterizes its glyphs from a standard font file
39 // (e.g. a TTF file) on the fly. It requires the
40 // FreeType 2.0 library (or any higher,
41 // backward-compatible version).
42 ////////////////////////////////////////////////////////////////////
43 class EXPCL_PANDA_TEXT DynamicTextFont : public TextFont, public FreetypeFont {
44 PUBLISHED:
45  DynamicTextFont(const Filename &font_filename, int face_index = 0);
46  DynamicTextFont(const char *font_data, int data_length, int face_index);
47  DynamicTextFont(const DynamicTextFont &copy);
48  virtual ~DynamicTextFont();
49 
50  virtual PT(TextFont) make_copy() const;
51 
52  INLINE const string &get_name() const;
53 
54  INLINE bool set_point_size(PN_stdfloat point_size);
55  INLINE PN_stdfloat get_point_size() const;
56 
57  INLINE bool set_pixels_per_unit(PN_stdfloat pixels_per_unit);
58  INLINE PN_stdfloat get_pixels_per_unit() const;
59 
60  INLINE bool set_scale_factor(PN_stdfloat scale_factor);
61  INLINE PN_stdfloat get_scale_factor() const;
62 
63  INLINE void set_native_antialias(bool native_antialias);
64  INLINE bool get_native_antialias() const;
65 
66  INLINE int get_font_pixel_size() const;
67 
68  INLINE PN_stdfloat get_line_height() const;
69  INLINE PN_stdfloat get_space_advance() const;
70 
71  INLINE void set_texture_margin(int texture_margin);
72  INLINE int get_texture_margin() const;
73  INLINE void set_poly_margin(PN_stdfloat poly_margin);
74  INLINE PN_stdfloat get_poly_margin() const;
75 
76  INLINE void set_page_size(int x_size, int y_size);
77  INLINE int get_page_x_size() const;
78  INLINE int get_page_y_size() const;
79 
80  INLINE void set_minfilter(SamplerState::FilterType filter);
81  INLINE SamplerState::FilterType get_minfilter() const;
82  INLINE void set_magfilter(SamplerState::FilterType filter);
83  INLINE SamplerState::FilterType get_magfilter() const;
84  INLINE void set_anisotropic_degree(int anisotropic_degree);
85  INLINE int get_anisotropic_degree() const;
86 
87  INLINE void set_render_mode(RenderMode render_mode);
88  INLINE RenderMode get_render_mode() const;
89  INLINE void set_winding_order(WindingOrder winding_order);
90  INLINE WindingOrder get_winding_order() const;
91 
92  INLINE void set_fg(const LColor &fg);
93  INLINE const LColor &get_fg() const;
94  INLINE void set_bg(const LColor &bg);
95  INLINE const LColor &get_bg() const;
96  INLINE void set_outline(const LColor &outline_color, PN_stdfloat outline_width,
97  PN_stdfloat outline_feather);
98  INLINE const LColor &get_outline_color() const;
99  INLINE PN_stdfloat get_outline_width() const;
100  INLINE PN_stdfloat get_outline_feather() const;
101  INLINE Texture::Format get_tex_format() const;
102 
103  int get_num_pages() const;
104  DynamicTextPage *get_page(int n) const;
105  MAKE_SEQ(get_pages, get_num_pages, get_page);
106 
107  int garbage_collect();
108  void clear();
109 
110  virtual void write(ostream &out, int indent_level) const;
111 
112 public:
113  virtual bool get_glyph(int character, const TextGlyph *&glyph);
114 
115 private:
116  void initialize();
117  void update_filters();
118  void determine_tex_format();
119  DynamicTextGlyph *make_glyph(int character, FT_Face face, int glyph_index);
120  void copy_bitmap_to_texture(const FT_Bitmap &bitmap, DynamicTextGlyph *glyph);
121  void copy_pnmimage_to_texture(const PNMImage &image, DynamicTextGlyph *glyph);
122  void blend_pnmimage_to_texture(const PNMImage &image, DynamicTextGlyph *glyph,
123  const LColor &fg);
124  DynamicTextGlyph *slot_glyph(int character, int x_size, int y_size);
125 
126  void render_wireframe_contours(DynamicTextGlyph *glyph);
127  void render_polygon_contours(DynamicTextGlyph *glyph, bool face, bool extrude);
128 
129  static int outline_move_to(const FT_Vector *to, void *user);
130  static int outline_line_to(const FT_Vector *to, void *user);
131  static int outline_conic_to(const FT_Vector *control,
132  const FT_Vector *to, void *user);
133  static int outline_cubic_to(const FT_Vector *control1,
134  const FT_Vector *control2,
135  const FT_Vector *to, void *user);
136  int outline_nurbs(NurbsCurveResult *ncr);
137 
138  int _texture_margin;
139  PN_stdfloat _poly_margin;
140  int _page_x_size, _page_y_size;
141 
142  SamplerState::FilterType _minfilter;
143  SamplerState::FilterType _magfilter;
144  int _anisotropic_degree;
145 
146  RenderMode _render_mode;
147  WindingOrder _winding_order;
148 
149  LColor _fg, _bg, _outline_color;
150  PN_stdfloat _outline_width;
151  PN_stdfloat _outline_feather;
152  bool _has_outline;
153  Texture::Format _tex_format;
154  bool _needs_image_processing;
155 
156  typedef pvector< PT(DynamicTextPage) > Pages;
157  Pages _pages;
158  int _preferred_page;
159 
160  // This doesn't need to be a reference-counting pointer, because the
161  // reference to each glyph is kept by the DynamicTextPage object.
162  typedef pmap<int, DynamicTextGlyph *> Cache;
163  Cache _cache;
164 
165  // This is a list of the glyphs that do not have any printable
166  // properties (e.g. space), but still have an advance measure. We
167  // store them here to keep their reference counts; they also appear
168  // in the above table.
169  typedef pvector< PT(DynamicTextGlyph) > EmptyGlyphs;
170  EmptyGlyphs _empty_glyphs;
171 
172  class ContourPoint {
173  public:
174  INLINE ContourPoint(const LPoint2 &p, const LVector2 &in,
175  const LVector2 &out);
176  INLINE ContourPoint(PN_stdfloat px, PN_stdfloat py, PN_stdfloat tx, PN_stdfloat ty);
177  INLINE void connect_to(const LVector2 &out);
178  LPoint2 _p;
179  LVector2 _in, _out; // tangents into and out of the vertex.
180  };
181  typedef pvector<ContourPoint> Points;
182 
183  class Contour {
184  public:
185  Points _points;
186  bool _is_solid;
187  int _start_vertex;
188  };
189 
190  typedef pvector<Contour> Contours;
191  Contours _contours;
192  LPoint2 _q; // The "current point".
193 
194 public:
195  static TypeHandle get_class_type() {
196  return _type_handle;
197  }
198  static void init_type() {
199  TextFont::init_type();
200  register_type(_type_handle, "DynamicTextFont",
201  TextFont::get_class_type());
202  }
203  virtual TypeHandle get_type() const {
204  return get_class_type();
205  }
206  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
207 
208 private:
209  static TypeHandle _type_handle;
210 
211  friend class TextNode;
212 };
213 
214 INLINE ostream &operator << (ostream &out, const DynamicTextFont &dtf);
215 
216 #include "dynamicTextFont.I"
217 
218 #endif // HAVE_FREETYPE
219 
220 #endif
This is our own Panda specialization on the default STL map.
Definition: pmap.h:52
PN_stdfloat get_space_advance() const
Returns the number of units wide a space is.
Definition: textFont.I:66
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:68
PN_stdfloat get_line_height() const
Returns the number of units high each line of text is.
Definition: textFont.I:45
An encapsulation of a font; i.e.
Definition: textFont.h:36
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
A representation of a single glyph (character) from a font.
Definition: textGlyph.h:31
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
This is a two-component vector offset.
Definition: lvector2.h:91
The primary interface to this module.
Definition: textNode.h:52
This is a two-component point in space.
Definition: lpoint2.h:92
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
The result of a NurbsCurveEvaluator.
const TextGlyph * get_glyph(int character)
Gets the glyph associated with the given character code, as well as an optional scaling parameter tha...
Definition: textFont.I:91