1 /* -*- mode: c++; c-basic-offset: 4 -*- */
2 
3 /* A python interface to FreeType */
4 #ifndef _FT2FONT_H
5 #define _FT2FONT_H
6 #include <vector>
7 #include <stdint.h>
8 
9 extern "C" {
10 #include <ft2build.h>
11 #include FT_FREETYPE_H
12 #include FT_GLYPH_H
13 #include FT_SFNT_NAMES_H
14 #include FT_TYPE1_TABLES_H
15 #include FT_TRUETYPE_TABLES_H
16 }
17 
18 /*
19  By definition, FT_FIXED as 2 16bit values stored in a single long.
20  */
21 #define FIXED_MAJOR(val) (long)((val & 0xffff000) >> 16)
22 #define FIXED_MINOR(val) (long)(val & 0xffff)
23 
24 // the FreeType string rendered into a width, height buffer
25 class FT2Image
26 {
27   public:
28     FT2Image();
29     FT2Image(unsigned long width, unsigned long height);
30     virtual ~FT2Image();
31 
32     void resize(long width, long height);
33     void draw_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y);
34     void write_bitmap(FILE *fp) const;
35     void draw_rect(unsigned long x0, unsigned long y0, unsigned long x1, unsigned long y1);
36     void draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1, unsigned long y1);
37 
get_buffer()38     unsigned char *get_buffer()
39     {
40         return m_buffer;
41     }
get_width()42     unsigned long get_width()
43     {
44         return m_width;
45     }
get_height()46     unsigned long get_height()
47     {
48         return m_height;
49     }
50 
51   private:
52     bool m_dirty;
53     unsigned char *m_buffer;
54     unsigned long m_width;
55     unsigned long m_height;
56 
57     // prevent copying
58     FT2Image(const FT2Image &);
59     FT2Image &operator=(const FT2Image &);
60 };
61 
62 extern FT_Library _ft2Library;
63 
64 class FT2Font
65 {
66 
67   public:
68     FT2Font(FT_Open_Args &open_args, long hinting_factor);
69     virtual ~FT2Font();
70     void clear();
71     void set_size(double ptsize, double dpi);
72     void set_charmap(int i);
73     void select_charmap(unsigned long i);
74     void set_text(
75         size_t N, uint32_t *codepoints, double angle, FT_Int32 flags, std::vector<double> &xys);
76     int get_kerning(FT_UInt left, FT_UInt right, FT_UInt mode);
77     void load_char(long charcode, FT_Int32 flags);
78     void load_glyph(FT_UInt glyph_index, FT_Int32 flags);
79     void get_width_height(long *width, long *height);
80     void get_bitmap_offset(long *x, long *y);
81     long get_descent();
82     // TODO: Since we know the size of the array upfront, we probably don't
83     // need to dynamically allocate like this
84     void get_xys(bool antialiased, std::vector<double> &xys);
85     void draw_glyphs_to_bitmap(bool antialiased);
86     void draw_glyph_to_bitmap(FT2Image &im, int x, int y, size_t glyphInd, bool antialiased);
87     void get_glyph_name(unsigned int glyph_number, char *buffer);
88     long get_name_index(char *name);
89     int get_path_count();
90     void get_path(double *outpoints, unsigned char *outcodes);
91 
get_face()92     FT_Face &get_face()
93     {
94         return face;
95     }
get_image()96     FT2Image &get_image()
97     {
98         return image;
99     }
get_last_glyph()100     FT_Glyph &get_last_glyph()
101     {
102         return glyphs.back();
103     }
get_last_glyph_index()104     size_t get_last_glyph_index()
105     {
106         return glyphs.size() - 1;
107     }
get_num_glyphs()108     size_t get_num_glyphs()
109     {
110         return glyphs.size();
111     }
get_hinting_factor()112     long get_hinting_factor()
113     {
114         return hinting_factor;
115     }
116 
117   private:
118     FT2Image image;
119     FT_Face face;
120     FT_Matrix matrix; /* transformation matrix */
121     FT_Vector pen;    /* untransformed origin  */
122     FT_Error error;
123     std::vector<FT_Glyph> glyphs;
124     std::vector<FT_Vector> pos;
125     FT_BBox bbox;
126     FT_Pos advance;
127     double angle;
128     double ptsize;
129     double dpi;
130     long hinting_factor;
131 
132     void set_scalable_attributes();
133 
134     // prevent copying
135     FT2Font(const FT2Font &);
136     FT2Font &operator=(const FT2Font &);
137 };
138 
139 #endif
140