summaryrefslogtreecommitdiffstats
path: root/ksvg/impl/libs/libtext2path/src/Font.cpp
blob: a9db7113f3011432833962b06817a94556ed5247 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
    Copyright (C) 2003 Nikolas Zimmermann <wildfox@kde.org>
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    aint with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <math.h>
#include <iostream>
#include <fontconfig/fontconfig.h>

#include "Font.h"
#include "Tools.h"
#include "Converter.h"

// Macros
#define FT_TRUNC(x)		((x) >> 6)
#define FT_TOFLOAT(x)	((x) * (1.0 / 64.0))
#define FT_FROMFLOAT(x)	((int) floor ((x) * 64.0 + 0.5))

using namespace T2P;

FontVisualParams::FontVisualParams()
{
	m_size = 0.0;
	m_slant = 0;
	m_weight = 0;
}

FontVisualParams::FontVisualParams(const FontVisualParams &other)
{
	(*this) = other;
}

FontVisualParams::~FontVisualParams()
{
}

FontVisualParams &FontVisualParams::operator=(const FontVisualParams &other)
{
	m_size = other.m_size;
	m_slant = other.m_slant;
	m_weight = other.m_weight;
	m_fontList = other.m_fontList;
	
	return *this;
}

void FontVisualParams::setWeight(int weight)
{
	m_weight = weight;
}

int FontVisualParams::weight() const
{
	return m_weight;
}

void FontVisualParams::setSlant(int slant)
{
	m_slant = slant;
}

int FontVisualParams::slant() const
{
	return m_slant;
}

void FontVisualParams::setSize(double size)
{
	m_size = size;
}

double FontVisualParams::size() const
{
	return m_size;
}

std::list<std::string> &FontVisualParams::fontList()
{
	return m_fontList;
}

// #####

Font::Font(Converter *context) : m_context(context)
{
	m_ready = false;

	m_fontFace = 0;
	m_fontParams = 0;
}

Font::~Font()
{
	// Release font face
	if(m_ready && m_fontFace)
	{
		// FIXME: Debug that!
		//std::cout << "CALLING DONE FACE " << m_fontFace << std::endl;
		FT_Done_Face(m_fontFace);
	}

	delete m_fontParams;
}

std::string Font::buildRequest(const FontVisualParams *fontParams, int &id)
{
	// Use FontConfig to locate & select fonts and use
	// FreeType2 to open them
	FcPattern *pattern;
	std::string fileName;

	pattern = FcPatternBuild(0,
							 FC_WEIGHT, FcTypeInteger, fontParams->weight(),
							 FC_SLANT, FcTypeInteger, fontParams->slant(),
							 FC_SIZE, FcTypeDouble, fontParams->size(),
							 NULL);

	// Add multiple font names
	std::list<std::string> &fontList = const_cast<FontVisualParams *>(fontParams)->fontList();

	for(std::list<std::string>::const_iterator it = fontList.begin(); it != fontList.end(); ++it)
	{
		std::string string = *it;

		if(!string.empty())
			FcPatternAddString(pattern, FC_FAMILY, reinterpret_cast<const FcChar8 *>(string.c_str()));
	}

	// Always load vertical layout
	FcPatternAddBool(pattern, FC_VERTICAL_LAYOUT, true);

	// Disable hinting
	FcPatternAddBool(pattern, FC_HINTING, false);

	// Perform the default font pattern modification operations.
	FcDefaultSubstitute(pattern);
	FcConfigSubstitute(FcConfigGetCurrent(), pattern, FcMatchPattern);

	// Match the pattern!
	FcResult result;
	FcPattern *match = FcFontMatch(0, pattern, &result);

	// Destroy pattern
	FcPatternDestroy(pattern);

	// Get index & filename
	FcChar8 *temp;

	if(match)
	{
		FcPattern *pattern = FcPatternDuplicate(match);

		// Get index & filename
		if(FcPatternGetString(pattern, FC_FILE, 0, &temp) != FcResultMatch ||
		   FcPatternGetInteger(pattern, FC_INDEX, 0, &id) != FcResultMatch)
		{
			std::cout << "Font::buildRequest(), could not load font file for requested font \"" << Tools::joinList('|', fontList) << "\"" << std::endl;
			return fileName;
		}

		fileName = reinterpret_cast<const char *>(temp);

		// Kill pattern
		FcPatternDestroy(pattern);
	}

	// Kill pattern
	FcPatternDestroy(match); 

	return fileName;
}

bool Font::load(const FontVisualParams *fontParams)
{	
	// Build FontConfig request pattern
	int id = -1;
	std::string filename = Font::buildRequest(fontParams, id);

	// Load font directly using FreeType2
	std::cout << "Font::load(), loading " << filename << " for requested font \"" << Tools::joinList('|', const_cast<FontVisualParams *>(fontParams)->fontList()) << "\"" << std::endl;

	FT_Error error = FT_New_Face(m_context->library(), filename.c_str(), id, &m_fontFace);
	if(error)
	{
		std::cout << "Font::load(), could not load font. Aborting!" << std::endl;
		return false;
	}
	if(!FT_IS_SCALABLE(m_fontFace))
	{
		std::cout << "Font::load(), font does not contain outlines. Aborting!" << std::endl;
		FT_Done_Face(m_fontFace);
		m_fontFace = 0;
		return false;
	}

	// Choose unicode charmap
	for(int charmap = 0; charmap < m_fontFace->num_charmaps; charmap++)
	{
		if(m_fontFace->charmaps[charmap]->encoding == ft_encoding_unicode)
		{
			FT_Error error = FT_Set_Charmap(m_fontFace, m_fontFace->charmaps[charmap]);

			if(error)
			{
				std::cout << "Font::load(), unable to select unicode charmap. Aborting!" << std::endl;

				FT_Done_Face(m_fontFace);
				m_fontFace = 0;

				return false;
			}
		}
	}

	m_fontParams = fontParams;
	m_fontFile = filename;
	m_ready = true;

	return true;
}

FT_Face &Font::fontFace()
{
	return m_fontFace;
}

std::string Font::fontFile() const
{
	return m_fontFile;
}

const FontVisualParams *Font::fontParams() const
{
	return m_fontParams;
}