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 "agg_scanline_storage_aa.h"
28#include "agg_scanline_storage_bin.h"
29#include "agg_scanline_u.h"
30#include "agg_scanline_bin.h"
31#include "agg_path_storage_integer.h"
32#include "agg_rasterizer_scanline_aa.h"
33#include "agg_conv_curve.h"
34#include "agg_font_cache_manager.h"
35#include "agg_trans_affine.h"
36
37namespace agg
38{
39
40
41    //-----------------------------------------------font_engine_freetype_base
42    class font_engine_freetype_base
43    {
44    public:
45        //--------------------------------------------------------------------
46        typedef serialized_scanlines_adaptor_aa<int8u>    gray8_adaptor_type;
47        typedef serialized_scanlines_adaptor_bin          mono_adaptor_type;
48        typedef scanline_storage_aa8                      scanlines_aa_type;
49        typedef scanline_storage_bin                      scanlines_bin_type;
50
51        //--------------------------------------------------------------------
52        ~font_engine_freetype_base();
53        font_engine_freetype_base(bool flag32, unsigned max_faces = 32);
54
55        // Set font parameters
56        //--------------------------------------------------------------------
57        void resolution(unsigned dpi);
58        bool load_font(const char* font_name, unsigned face_index, glyph_rendering ren_type,
59                       const char* font_mem = 0, const long font_mem_size = 0);
60        bool attach(const char* file_name);
61        bool char_map(FT_Encoding map);
62        bool height(double h);
63        bool width(double w);
64        void hinting(bool h);
65        void flip_y(bool f);
66        void transform(const trans_affine& affine);
67
68        // Set Gamma
69        //--------------------------------------------------------------------
70        template<class GammaF> void gamma(const GammaF& f)
71        {
72            m_rasterizer.gamma(f);
73        }
74
75        // Accessors
76        //--------------------------------------------------------------------
77        int         last_error()   const { return m_last_error; }
78        unsigned    resolution()   const { return m_resolution; }
79        const char* name()         const { return m_name;       }
80        unsigned    num_faces()    const;
81        FT_Encoding char_map()     const { return m_char_map;   }
82        double      height()       const { return double(m_height) / 64.0;    }
83        double      width()        const { return double(m_width) / 64.0;     }
84        double      ascender()     const;
85        double      descender()    const;
86        bool        hinting()      const { return m_hinting;    }
87        bool        flip_y()       const { return m_flip_y;     }
88
89
90        // Interface mandatory to implement for font_cache_manager
91        //--------------------------------------------------------------------
92        const char*     font_signature() const { return m_signature;    }
93        int             change_stamp()   const { return m_change_stamp; }
94
95        bool            prepare_glyph(unsigned glyph_code);
96        unsigned        glyph_index() const { return m_glyph_index; }
97        unsigned        data_size()   const { return m_data_size;   }
98        glyph_data_type data_type()   const { return m_data_type;   }
99        const rect_i&   bounds()      const { return m_bounds;      }
100        double          advance_x()   const { return m_advance_x;   }
101        double          advance_y()   const { return m_advance_y;   }
102        void            write_glyph_to(int8u* data) const;
103        bool            add_kerning(unsigned first, unsigned second,
104                                    double* x, double* y);
105
106    private:
107        font_engine_freetype_base(const font_engine_freetype_base&);
108        const font_engine_freetype_base& operator = (const font_engine_freetype_base&);
109
110        void update_char_size();
111        void update_signature();
112        int  find_face(const char* face_name) const;
113
114        bool            m_flag32;
115        int             m_change_stamp;
116        int             m_last_error;
117        char*           m_name;
118        unsigned        m_name_len;
119        unsigned        m_face_index;
120        FT_Encoding     m_char_map;
121        char*           m_signature;
122        unsigned        m_height;
123        unsigned        m_width;
124        bool            m_hinting;
125        bool            m_flip_y;
126        bool            m_library_initialized;
127        FT_Library      m_library;    // handle to library
128        FT_Face*        m_faces;      // A pool of font faces
129        char**          m_face_names;
130        unsigned        m_num_faces;
131        unsigned        m_max_faces;
132        FT_Face         m_cur_face;  // handle to the current face object
133        int             m_resolution;
134        glyph_rendering m_glyph_rendering;
135        unsigned        m_glyph_index;
136        unsigned        m_data_size;
137        glyph_data_type m_data_type;
138        rect_i          m_bounds;
139        double          m_advance_x;
140        double          m_advance_y;
141        trans_affine    m_affine;
142
143        path_storage_integer<int16, 6>              m_path16;
144        path_storage_integer<int32, 6>              m_path32;
145        conv_curve<path_storage_integer<int16, 6> > m_curves16;
146        conv_curve<path_storage_integer<int32, 6> > m_curves32;
147        scanline_u8              m_scanline_aa;
148        scanline_bin             m_scanline_bin;
149        scanlines_aa_type        m_scanlines_aa;
150        scanlines_bin_type       m_scanlines_bin;
151        rasterizer_scanline_aa<> m_rasterizer;
152    };
153
154
155
156
157    //------------------------------------------------font_engine_freetype_int16
158    // This class uses values of type int16 (10.6 format) for the vector cache.
159    // The vector cache is compact, but when rendering glyphs of height
160    // more that 200 there integer overflow can occur.
161    //
162    class font_engine_freetype_int16 : public font_engine_freetype_base
163    {
164    public:
165        typedef serialized_integer_path_adaptor<int16, 6>     path_adaptor_type;
166        typedef font_engine_freetype_base::gray8_adaptor_type gray8_adaptor_type;
167        typedef font_engine_freetype_base::mono_adaptor_type  mono_adaptor_type;
168        typedef font_engine_freetype_base::scanlines_aa_type  scanlines_aa_type;
169        typedef font_engine_freetype_base::scanlines_bin_type scanlines_bin_type;
170
171        font_engine_freetype_int16(unsigned max_faces = 32) :
172            font_engine_freetype_base(false, max_faces) {}
173    };
174
175    //------------------------------------------------font_engine_freetype_int32
176    // This class uses values of type int32 (26.6 format) for the vector cache.
177    // The vector cache is twice larger than in font_engine_freetype_int16,
178    // but it allows you to render glyphs of very large sizes.
179    //
180    class font_engine_freetype_int32 : public font_engine_freetype_base
181    {
182    public:
183        typedef serialized_integer_path_adaptor<int32, 6>     path_adaptor_type;
184        typedef font_engine_freetype_base::gray8_adaptor_type gray8_adaptor_type;
185        typedef font_engine_freetype_base::mono_adaptor_type  mono_adaptor_type;
186        typedef font_engine_freetype_base::scanlines_aa_type  scanlines_aa_type;
187        typedef font_engine_freetype_base::scanlines_bin_type scanlines_bin_type;
188
189        font_engine_freetype_int32(unsigned max_faces = 32) :
190            font_engine_freetype_base(true, max_faces) {}
191    };
192
193
194}
195
196#endif
197