| 1 |
|
| 2 |
#include <avm_map.h> |
| 3 |
#include <avm_output.h> |
| 4 |
#include <image.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#ifdef HAVE_LIBFREETYPE |
| 8 |
|
| 9 |
#include <ft2build.h> |
| 10 |
#include FT_FREETYPE_H |
| 11 |
|
| 12 |
//#include <freetype/freetype.h> |
| 13 |
|
| 14 |
#if (FREETYPE_MAJOR > 2) || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 1) |
| 15 |
#define HAVE_FREETYPE21 |
| 16 |
#endif |
| 17 |
|
| 18 |
AVM_BEGIN_NAMESPACE; |
| 19 |
|
| 20 |
struct ci_font_t |
| 21 |
{ |
| 22 |
ci_surface_t* bitmap; |
| 23 |
int width; |
| 24 |
int height; |
| 25 |
int index; // local vars - used by ft2 |
| 26 |
}; |
| 27 |
|
| 28 |
static inline int int_f266(int x) { return x * 64; } |
| 29 |
static inline int f266_int(int x) { return x / 64; } |
| 30 |
|
| 31 |
class FontRend_FT { |
| 32 |
public: |
| 33 |
FontRend_FT() : m_Lib(0), m_Face(0) {} |
| 34 |
|
| 35 |
int Open(const char* filename, int faceindex, int size) |
| 36 |
{ |
| 37 |
if (FT_Init_FreeType(&m_Lib)) |
| 38 |
{ |
| 39 |
AVM_WRITE("FTREND", "error Open FT Library\n"); |
| 40 |
return -1; |
| 41 |
} |
| 42 |
|
| 43 |
int r = FT_New_Face(m_Lib, filename, 0, &m_Face); |
| 44 |
if (r == FT_Err_Unknown_File_Format) |
| 45 |
{ |
| 46 |
AVM_WRITE("FTREND", "the font file could be opened and read," |
| 47 |
"but it appears that its font format is unsupported\n"); |
| 48 |
return -1; |
| 49 |
} |
| 50 |
else if (r) |
| 51 |
{ |
| 52 |
AVM_WRITE("FTREND", "font file can't be open\n"); |
| 53 |
return -1; |
| 54 |
} |
| 55 |
|
| 56 |
FT_Select_Charmap(m_Face, ft_encoding_unicode); |
| 57 |
if (!m_Face->charmap || m_Face->charmap->encoding != ft_encoding_unicode) |
| 58 |
{ |
| 59 |
AVM_WRITE("FTREND", "unicode charmap not available for this font!\n"); |
| 60 |
if (FT_Set_Charmap(m_Face, m_Face->charmaps[0])) |
| 61 |
AVM_WRITE("FTREND", "no charmaps!\n"); |
| 62 |
} |
| 63 |
|
| 64 |
if (FT_IS_SCALABLE(m_Face)) |
| 65 |
{ |
| 66 |
if (FT_Set_Char_Size(m_Face, 0, int_f266(size), 0, 0)); //72dpi |
| 67 |
AVM_WRITE("FTREND", "Set_Char_Size for scalable font fails???"); |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
// FIXME Set_Pixel_Sizes |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
if (FT_IS_FIXED_WIDTH(m_Face)) |
| 76 |
AVM_WRITE("FTREND", "fixed width.\n"); |
| 77 |
|
| 78 |
m_bKerning = FT_HAS_KERNING(m_Face); |
| 79 |
// read font |
| 80 |
return 0; |
| 81 |
} |
| 82 |
void Close() |
| 83 |
{ |
| 84 |
FT_Done_FreeType(m_Lib); |
| 85 |
} |
| 86 |
const ci_font_t* GetChar(int ch, const ci_font_t* prev = 0, int* kern_x = 0) |
| 87 |
{ |
| 88 |
const ci_font_t* t = m_Map.find(ch); |
| 89 |
if (!t) |
| 90 |
{ |
| 91 |
ci_font_t n; |
| 92 |
|
| 93 |
n.index = FT_Get_Char_Index(m_Face, ch); |
| 94 |
int r = FT_Load_Glyph(m_Face, n.index, |
| 95 |
FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING); |
| 96 |
|
| 97 |
FT_Bitmap bit; |
| 98 |
if (m_Face->glyph->format == ft_glyph_format_outline) |
| 99 |
{ |
| 100 |
// |
| 101 |
bit.pixel_mode = ft_pixel_mode_mono; |
| 102 |
//bit.rows = |
| 103 |
|
| 104 |
bit.pixel_mode = ft_pixel_mode_grays; |
| 105 |
memset(bit.buffer, 0, bit.rows * bit.pitch); |
| 106 |
} |
| 107 |
//if (FT_Get_Glyph_Bitmap(m_Face, &bit)) |
| 108 |
// AVM_WRITE("FTREND", "error GetGlyphBitmap\n"); |
| 109 |
m_Map.insert(ch, n); |
| 110 |
t = m_Map.find(ch); |
| 111 |
} |
| 112 |
|
| 113 |
if (m_bKerning && prev && kern_x) |
| 114 |
{ |
| 115 |
FT_Vector delta; |
| 116 |
FT_Get_Kerning(m_Face, prev->index, t->index, |
| 117 |
ft_kerning_default, &delta); |
| 118 |
*kern_x = f266_int(delta.x); |
| 119 |
} |
| 120 |
return t; |
| 121 |
} |
| 122 |
protected: |
| 123 |
avm::avm_map<int, ci_font_t> m_Map; |
| 124 |
FT_Library m_Lib; |
| 125 |
FT_Face m_Face; |
| 126 |
int m_iSpace; |
| 127 |
|
| 128 |
bool m_bKerning; |
| 129 |
}; |
| 130 |
|
| 131 |
AVM_END_NAMESPACE; |
| 132 |
|
| 133 |
#endif // HAVE_FREETYPE |