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