summaryrefslogtreecommitdiff
path: root/font.c
blob: d15bd8bb3fac8297b000b34836d38f7888549f76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * $Id: font.c,v 1.5 2004/05/30 21:48:21 austriancoder Exp $
 */

#include "font.h"

// ==================================
// constr.
cText2SkinFont::cText2SkinFont()
{
	m_library = 0;
	m_face = 0;

	// init freetype2 lib
	int error = FT_Init_FreeType(&m_library);
	if (error)
	{
		esyslog("ERROR: Could not init freetype library\n");	
	}
}

// ==================================
// deconstr.
cText2SkinFont::~cText2SkinFont()
{
	if (m_face)
	{
		FT_Done_Face(m_face);
	}

	if (m_library)
	{
		FT_Done_FreeType(m_library);
	}
}

// ==================================
// try to load a font
bool cText2SkinFont::LoadFontFile(string Filename)
{
	int error = FT_New_Face(m_library, Filename.c_str(), 0, &m_face);

	// every thing ok?
	if (error == FT_Err_Unknown_File_Format)
	{
		esyslog("ERROR: Font file (%s) could be opened and read, but it appears that its font format is unsupported\n", Filename.c_str());
		return false;
	}
	else if (error)
	{
		esyslog("ERROR: Font file (%s) could be opened or read, or simply it is broken\n", Filename.c_str());
		return false;
	}

	// set slot
	m_slot = m_face->glyph;

	return true;
}

// ==================================
// sets size of font
void cText2SkinFont::SetFontSize(int size)
{
	FT_Set_Char_Size
		(
            m_face,		// handle to face object
            0,			// char_width in 1/64th of points
            size*64,	// char_height in 1/64th of points
            300,		// horizontal device resolution (dpi)
            300			// vertical device resolution (dpi)
		);
}

// ==================================
// write some text :)
void cText2SkinFont::DrawTextTransparent(cOsd *Osd, int x, int y, const char *s, tColor ColorFg, int Width, int Height, int Alignment)
{
	// where to get this infos?
//	int w = Font->Width(s);
//	int h = Font->Height();

	int limit = 0;
	if (Width || Height) 
	{
		int cw = Width ? Width : w;
		limit = x + cw;
		if (Width) 
		{
			if ((Alignment & taLeft) != 0)
				;
			else if ((Alignment & taRight) != 0) 
			{
				if (w < Width)
					x += Width - w;
			}
			else 
			{ 
				// taCentered
				if (w < Width)
					x += (Width - w) / 2;
			}
		}

		if (Height) 
		{
			if ((Alignment & taTop) != 0)
				;
			else if ((Alignment & taBottom) != 0) 
			{
				if (h < Height)
					y += Height - h;
			}
			else 
			{ 
				// taCentered
				if (h < Height)
					y += (Height - h) / 2;
			}
		}
	}

	// write text
	while (s && *s) 
	{
	}
}