Panda3D
dynamicTextFont.h
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 dynamicTextFont.h
10  * @author drose
11  * @date 2002-02-08
12  */
13 
14 #ifndef DYNAMICTEXTFONT_H
15 #define DYNAMICTEXTFONT_H
16 
17 #include "pandabase.h"
18 
19 #ifdef HAVE_FREETYPE
20 
21 #include "textFont.h"
22 #include "freetypeFont.h"
23 #include "dynamicTextGlyph.h"
24 #include "dynamicTextPage.h"
25 #include "filename.h"
26 #include "pvector.h"
27 #include "pmap.h"
28 
29 #include <ft2build.h>
30 #include FT_FREETYPE_H
31 
32 class NurbsCurveResult;
33 
34 typedef struct hb_font_t hb_font_t;
35 
36 /**
37  * A DynamicTextFont is a special TextFont object that rasterizes its glyphs
38  * from a standard font file (e.g. a TTF file) on the fly. It requires the
39  * FreeType 2.0 library (or any higher, backward-compatible version).
40  */
41 class EXPCL_PANDA_TEXT DynamicTextFont : public TextFont, public FreetypeFont {
42 PUBLISHED:
43  DynamicTextFont(const Filename &font_filename, int face_index = 0);
44  DynamicTextFont(const char *font_data, int data_length, int face_index);
45  DynamicTextFont(const DynamicTextFont &copy);
46  virtual ~DynamicTextFont();
47 
48  virtual PT(TextFont) make_copy() const;
49 
50  INLINE const std::string &get_name() const;
51 
52  INLINE bool set_point_size(PN_stdfloat point_size);
53  INLINE PN_stdfloat get_point_size() const;
54  MAKE_PROPERTY(point_size, get_point_size, set_point_size);
55 
56  INLINE bool set_pixels_per_unit(PN_stdfloat pixels_per_unit);
57  INLINE PN_stdfloat get_pixels_per_unit() const;
58  MAKE_PROPERTY(pixels_per_unit, get_pixels_per_unit, set_pixels_per_unit);
59 
60  INLINE bool set_scale_factor(PN_stdfloat scale_factor);
61  INLINE PN_stdfloat get_scale_factor() const;
62  MAKE_PROPERTY(scale_factor, get_scale_factor, set_scale_factor);
63 
64  INLINE void set_native_antialias(bool native_antialias);
65  INLINE bool get_native_antialias() const;
66  MAKE_PROPERTY(native_antialias, get_native_antialias, set_native_antialias);
67 
68  INLINE int get_font_pixel_size() const;
69  MAKE_PROPERTY(font_pixel_size, get_font_pixel_size);
70 
71  INLINE PN_stdfloat get_line_height() const;
72  INLINE PN_stdfloat get_space_advance() const;
73 
74  INLINE void set_texture_margin(int texture_margin);
75  INLINE int get_texture_margin() const;
76  INLINE void set_poly_margin(PN_stdfloat poly_margin);
77  INLINE PN_stdfloat get_poly_margin() const;
78  MAKE_PROPERTY(texture_margin, get_texture_margin, set_texture_margin);
79  MAKE_PROPERTY(poly_margin, get_poly_margin, set_poly_margin);
80 
81  INLINE void set_page_size(const LVecBase2i &page_size);
82  INLINE void set_page_size(int x_size, int y_size);
83  INLINE const LVecBase2i &get_page_size() const;
84  INLINE int get_page_x_size() const;
85  INLINE int get_page_y_size() const;
86  MAKE_PROPERTY(page_size, get_page_size, set_page_size);
87 
88  INLINE void set_minfilter(SamplerState::FilterType filter);
89  INLINE SamplerState::FilterType get_minfilter() const;
90  INLINE void set_magfilter(SamplerState::FilterType filter);
91  INLINE SamplerState::FilterType get_magfilter() const;
92  INLINE void set_anisotropic_degree(int anisotropic_degree);
93  INLINE int get_anisotropic_degree() const;
94  MAKE_PROPERTY(minfilter, get_minfilter, set_minfilter);
95  MAKE_PROPERTY(magfilter, get_magfilter, set_magfilter);
96  MAKE_PROPERTY(anisotropic_degree, get_anisotropic_degree, set_anisotropic_degree);
97 
98  INLINE void set_render_mode(RenderMode render_mode);
99  INLINE RenderMode get_render_mode() const;
100  MAKE_PROPERTY(render_mode, get_render_mode, set_render_mode);
101 
102  INLINE void set_fg(const LColor &fg);
103  INLINE const LColor &get_fg() const;
104  INLINE void set_bg(const LColor &bg);
105  INLINE const LColor &get_bg() const;
106  INLINE void set_outline(const LColor &outline_color, PN_stdfloat outline_width,
107  PN_stdfloat outline_feather);
108  INLINE const LColor &get_outline_color() const;
109  INLINE PN_stdfloat get_outline_width() const;
110  INLINE PN_stdfloat get_outline_feather() const;
111  INLINE Texture::Format get_tex_format() const;
112  MAKE_PROPERTY(fg, get_fg, set_fg);
113  MAKE_PROPERTY(bg, get_bg, set_bg);
114  MAKE_PROPERTY(tex_format, get_tex_format);
115 
116  int get_num_pages() const;
117  DynamicTextPage *get_page(int n) const;
118  MAKE_SEQ(get_pages, get_num_pages, get_page);
119  MAKE_SEQ_PROPERTY(pages, get_num_pages, get_page);
120 
121  int garbage_collect();
122  void clear();
123 
124  virtual void write(std::ostream &out, int indent_level) const;
125 
126 public:
127  virtual bool get_glyph(int character, CPT(TextGlyph) &glyph);
128  virtual PN_stdfloat get_kerning(int first, int second) const;
129 
130  bool get_glyph_by_index(int character, int glyph_index, CPT(TextGlyph) &glyph);
131  hb_font_t *get_hb_font() const;
132 
133 private:
134  void initialize();
135  void update_filters();
136  void determine_tex_format();
137  CPT(TextGlyph) make_glyph(int character, FT_Face face, int glyph_index);
138  void copy_bitmap_to_texture(const FT_Bitmap &bitmap, DynamicTextGlyph *glyph);
139  void copy_pnmimage_to_texture(const PNMImage &image, DynamicTextGlyph *glyph);
140  void blend_pnmimage_to_texture(const PNMImage &image, DynamicTextGlyph *glyph,
141  const LColor &fg);
142  DynamicTextGlyph *slot_glyph(int character, int x_size, int y_size, PN_stdfloat advance);
143 
144  void render_wireframe_contours(TextGlyph *glyph);
145  void render_polygon_contours(TextGlyph *glyph, bool face, bool extrude);
146 
147  int _texture_margin;
148  PN_stdfloat _poly_margin;
149  LVecBase2i _page_size;
150 
151  SamplerState::FilterType _minfilter;
152  SamplerState::FilterType _magfilter;
153  int _anisotropic_degree;
154 
155  RenderMode _render_mode;
156 
157  LColor _fg, _bg, _outline_color;
158  PN_stdfloat _outline_width;
159  PN_stdfloat _outline_feather;
160  bool _has_outline;
161  Texture::Format _tex_format;
162  bool _needs_image_processing;
163 
164  typedef pvector< PT(DynamicTextPage) > Pages;
165  Pages _pages;
166  int _preferred_page;
167 
168  // This doesn't need to be a reference-counting pointer, because the
169  // reference to each glyph is kept by the DynamicTextPage object.
170  typedef pmap<int, const TextGlyph *> Cache;
171  Cache _cache;
172 
173  // This is a list of the glyphs that do not have any printable properties
174  // (e.g. space), but still have an advance measure. We store them here to
175  // keep their reference counts; they also appear in the above table.
176  typedef pvector< PT(TextGlyph) > EmptyGlyphs;
177  EmptyGlyphs _empty_glyphs;
178 
179  mutable hb_font_t *_hb_font;
180 
181 public:
182  static TypeHandle get_class_type() {
183  return _type_handle;
184  }
185  static void init_type() {
186  TextFont::init_type();
187  register_type(_type_handle, "DynamicTextFont",
188  TextFont::get_class_type());
189  }
190  virtual TypeHandle get_type() const {
191  return get_class_type();
192  }
193  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
194 
195 private:
196  static TypeHandle _type_handle;
197 
198  friend class TextNode;
199 };
200 
201 INLINE std::ostream &operator << (std::ostream &out, const DynamicTextFont &dtf);
202 
203 #include "dynamicTextFont.I"
204 
205 #endif // HAVE_FREETYPE
206 
207 #endif
virtual PN_stdfloat get_kerning(int first, int second) const
Returns the amount by which to offset the second glyph when it directly follows the first glyph.
Definition: textFont.cxx:67
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:58
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An encapsulation of a font; i.e.
Definition: textFont.h:32
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
A representation of a single glyph (character) from a font.
Definition: textGlyph.h:28
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The primary interface to this module.
Definition: textNode.h:48
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
The result of a NurbsCurveEvaluator.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.