Anti-Grain Geometry Tutorial
agg_font_freetype2.h
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // See implementation agg_font_freetype.cpp
17 //
18 //----------------------------------------------------------------------------
19 
20 #ifndef AGG_FONT_FREETYPE_INCLUDED
21 #define AGG_FONT_FREETYPE_INCLUDED
22 
23 #include <ft2build.h>
24 #include FT_FREETYPE_H
25 
26 
27 #include <cstddef>
28 #include "agg_scanline_storage_aa.h"
29 #include "agg_scanline_storage_bin.h"
30 #include "agg_scanline_u.h"
31 #include "agg_scanline_bin.h"
32 #include "agg_path_storage_integer.h"
33 #include "agg_rasterizer_scanline_aa.h"
34 #include "agg_conv_curve.h"
35 #include "agg_font_cache_manager.h"
36 #include "agg_font_cache_manager2.h"
37 #include "agg_trans_affine.h"
38 
39 namespace agg {
40 namespace fman {
41 
42 
43  //-----------------------------------------------font_engine_freetype_base
45  {
46  public:
48  {
49  unsigned glyph_code;
50  unsigned glyph_index;
51  unsigned data_size;
52  glyph_data_type data_type;
53  rect_i bounds;
54  double advance_x;
55  double advance_y;
56  };
57 
59  {
60  public:
61  loaded_face( font_engine_freetype_base &engine, FT_Face face )
62  : m_engine( engine )
63  , m_ft_face( face )
64  , m_dpi( 0 )
65  , m_height( 0 )
66  , m_width( 0 )
67  , m_rendering( glyph_ren_native_gray8 )
68  , m_hinting( false )
69  , m_flip_y( true )
70  , m_char_map( FT_ENCODING_NONE )
71  {
72  set_face_name();
73  }
74 
75  ~loaded_face()
76  {
77  FT_Done_Face( m_ft_face);
78  delete[] m_face_name;
79  }
80 
81  unsigned num_faces() const
82  {
83  return m_ft_face->num_faces;
84  }
85 
86  const char *name() const
87  {
88  return m_face_name;
89  }
90 
91  unsigned resolution() const
92  {
93  return m_dpi;
94  }
95 
96  double height() const
97  {
98  return m_height;
99  }
100 
101  double width() const
102  {
103  return m_width;
104  }
105 
106  double ascent() const
107  {
108  return m_ft_face->ascender*height()/m_ft_face->height;
109  }
110 
111  double descent() const
112  {
113  return m_ft_face->descender*height()/m_ft_face->height;
114  }
115 
116  double ascent_b() const
117  {
118  return m_ft_face->bbox.yMax*height()/m_ft_face->height;
119  }
120 
121  double descent_b() const
122  {
123  return m_ft_face->bbox.yMin*height()/m_ft_face->height;
124  }
125 
126  bool hinting() const
127  {
128  return m_hinting;
129  }
130 
131  bool flip_y() const
132  {
133  return m_flip_y;
134  }
135 
136  const trans_affine &transform() const
137  {
138  return m_affine;
139  }
140 
141  FT_Encoding char_map() const
142  {
143  return m_char_map;
144  }
145 
146  void set_hinting( bool h )
147  {
148  m_hinting=h;
149  }
150 
151  /*
152  void set_resolution(int dpi)
153  { m_dpi=dpi; update_char_size(); }
154  void set_height( double h )
155  { m_height=int(h*64.0); update_char_size(); }
156  void set_width( double w )
157  { m_width=int(w*64.0); update_char_size(); }
158  void set_flip_y(bool f)
159  { m_flip_y=f; }
160  void set_transform(const trans_affine& affine)
161  { m_affine=affine; }
162  int set_char_map(FT_Encoding char_map)
163  { FT_Select_Charmap(m_ft_face, char_map); }
164  */
165  void select_instance(
166  double height,
167  double width,
168  bool hinting,
169  glyph_rendering rendering )
170  {
171  rendering=capable_rendering(rendering);
172 
173  if( m_height != height ||
174  m_width != width ||
175  m_hinting != hinting ||
176  m_rendering != rendering )
177  {
178  m_height = height;
179  m_width = width;
180  m_hinting = hinting;
181  m_rendering = rendering;
182  update_char_size();
183  }
184  }
185 
186  glyph_rendering capable_rendering( glyph_rendering rendering ) const
187  {
188  switch(rendering)
189  {
190  case glyph_ren_native_mono:
191  case glyph_ren_native_gray8:
192  break;
193 
194  case glyph_ren_outline:
195  if(!FT_IS_SCALABLE(m_ft_face))
196  rendering = glyph_ren_native_gray8;
197  break;
198 
199  case glyph_ren_agg_mono:
200  if(!FT_IS_SCALABLE(m_ft_face))
201  rendering = glyph_ren_native_mono;
202  break;
203 
204  case glyph_ren_agg_gray8:
205  if(!FT_IS_SCALABLE(m_ft_face))
206  rendering = glyph_ren_native_gray8;
207  break;
208  };
209  return rendering;
210  }
211 
212  void update_char_size()
213  {
214  if( m_dpi )
215  FT_Set_Char_Size( m_ft_face, int(m_width*64), int(m_height*64), m_dpi, m_dpi );
216  else
217  FT_Set_Pixel_Sizes( m_ft_face, int(m_width), int(m_height) );
218  }
219 
220  bool prepare_glyph(unsigned glyph_code, prepared_glyph *prepared ) const;
221  bool add_kerning(unsigned first, unsigned second, double* x, double* y);
222  void write_glyph_to(prepared_glyph *prepared, int8u* data) const;
223 
224  private:
225  void set_face_name();
226 
227  loaded_face(const loaded_face &);
228  loaded_face &operator=(const loaded_face &);
229 
230  private:
231  font_engine_freetype_base &m_engine;
232  FT_Face m_ft_face;
233  char *m_face_name;
234  int m_dpi;
235  double m_height;
236  double m_width;
237  glyph_rendering m_rendering;
238  bool m_hinting;
239  bool m_flip_y;
240  trans_affine m_affine;
241  FT_Encoding m_char_map;
242  };
243 
244  //--------------------------------------------------------------------
249 
250  //--------------------------------------------------------------------
252  font_engine_freetype_base(bool flag32, void *ftMemory=0);
253 
254  // Load families and faces
255  //--------------------------------------------------------------------
256  loaded_face *load_face(const void* buffer, std::size_t bytes);
257  loaded_face *load_face_file(const char* file_name);
258  loaded_face *create_loaded_face(FT_Face ft_face); // internal
259  void unload_face(loaded_face *face);
260 
261  // Set Gamma
262  //--------------------------------------------------------------------
263  template<class GammaF> void gamma(const GammaF& f)
264  {
265  m_rasterizer.gamma(f);
266  }
267 
268  private:
270  const font_engine_freetype_base& operator = (const font_engine_freetype_base&);
271 
272  bool m_flag32;
273  int m_last_error;
274  bool m_library_initialized;
275  FT_Library m_library; // handle to library
276 
281  scanline_u8 m_scanline_aa;
282  scanline_bin m_scanline_bin;
283  scanlines_aa_type m_scanlines_aa;
284  scanlines_bin_type m_scanlines_bin;
285  rasterizer_scanline_aa<> m_rasterizer;
286  };
287 
288 
289 
290 
291  //------------------------------------------------font_engine_freetype_int16
292  // This class uses values of type int16 (10.6 format) for the vector cache.
293  // The vector cache is compact, but when rendering glyphs of height
294  // more that 200 there integer overflow can occur.
295  //
297  {
298  public:
304 
305  font_engine_freetype_int16(void *ftMemory = 0) :
306  font_engine_freetype_base(false, ftMemory) {}
307  };
308 
309  //------------------------------------------------font_engine_freetype_int32
310  // This class uses values of type int32 (26.6 format) for the vector cache.
311  // The vector cache is twice larger than in font_engine_freetype_int16,
312  // but it allows you to render glyphs of very large sizes.
313  //
315  {
316  public:
322 
323  font_engine_freetype_int32(void *ftMemory = 0) :
324  font_engine_freetype_base(true, ftMemory) {}
325  };
326 
327 
328 }
329 }
330 
331 #endif
Definition: agg_arc.cpp:24