File: GrcRtFileFont.cpp

package info (click to toggle)
grcompiler 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,020 kB
  • sloc: cpp: 48,200; ansic: 7,670; sh: 4,427; makefile: 197; xml: 190; perl: 127; sed: 21
file content (435 lines) | stat: -rw-r--r-- 10,725 bytes parent folder | download | duplicates (2)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001, 2005 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: GrcRtFileFont.cpp
Responsibility: Sharon Correll

Description:
	
----------------------------------------------------------------------------------------------*/

//#include "FontTableCache.h"
#include "GrcRtFileFont.h"
#include <stdio.h>

// TBD do this properly
////#define FUDGE_FACTOR 72
////#define fix26_6(x) (x >> 6) + (x & 32 ? (x > 0 ? 1 : 0) : (x < 0 ? -1 : 0))

/*
GrcRtFileFont::GrcRtFileFont()
	: 
	m_file(NULL),
	m_pTableCache(NULL),
	m_ascent(0),
	m_descent(0),
	m_emSquare(0),
	m_pointSize(0),
	m_dpiX(FUDGE_FACTOR),
	m_dpiY(FUDGE_FACTOR),
	m_isValid(false),
	m_pHeader(NULL),
	m_pTableDir(NULL),
	m_xScale(1.0f),
	m_yScale(1.0f)
{
	m_fItalic = false;
	m_fBold = false;
	// colors are not used
	m_clrFore = (unsigned long)kclrBlack;
	m_clrBack = (unsigned long)kclrTransparent;
}

GrcRtFileFont::GrcRtFileFont(FILE * file, float pointSize, 
				   unsigned int dpiX, unsigned int dpiY)
	: 
	m_file(file),
	m_pTableCache(NULL),
	m_ascent(0),
	m_descent(0),
	m_emSquare(0),
	m_pointSize(pointSize),
	m_dpiX(dpiX),
	m_dpiY(dpiY),
	m_isValid(false),
	m_pHeader(NULL),
	m_pTableDir(NULL),
	m_xScale(1.0f),
	m_yScale(1.0f)
{
	Assert(m_file); // shouldn't be null
	initializeFromFace();
	
}

GrcRtFileFont::GrcRtFileFont(char * fileName, float pointSize, 
				   unsigned int dpiX, unsigned int dpiY)
	: 
	m_file(NULL),
	m_pTableCache(NULL),
	m_ascent(0),
	m_descent(0),
	m_emSquare(0),
	m_pointSize(pointSize),
	m_dpiX(dpiX),
	m_dpiY(dpiY),
	m_isValid(false),
	m_pHeader(NULL),
	m_pTableDir(NULL),
	m_xScale(1.0f),
	m_yScale(1.0f)
{
	Assert(fileName); // shouldn't be null but we play safe anyway
	m_file = fopen(fileName, "rb");

	initializeFromFace();
}
*/

GrcRtFileFont::GrcRtFileFont(std::string fileName, float pointSize, 
				   unsigned int dpiX, unsigned int dpiY)
	: 
	m_file(NULL),
	////m_pTableCache(NULL),
	m_ascent(0),
	m_descent(0),
	m_emSquare(0),
	m_pointSize(pointSize),
	m_dpiX(dpiX),
	m_dpiY(dpiY),
	m_isValid(false),
	m_pHeader(NULL),
	m_pTableDir(NULL),
	m_xScale(1.0f),
	m_yScale(1.0f)
{
	Assert(fileName.length()); // shouldn't be null but we play safe anyway
	m_file = fopen(fileName.c_str(), "rb");

	initializeFromFace();
}


void GrcRtFileFont::initializeFromFace()
{
	if (m_dpiY == 0)
		m_dpiY = m_dpiX;
	//m_fItalic = false;
	//m_fBold = false;
	//// colors are not used
	//m_clrFore = (unsigned long)kclrBlack;
	//m_clrBack = (unsigned long)kclrTransparent;

	if (m_file)
	{
		size_t lOffset;
		size_t lSize;
		TtfUtil::GetHeaderInfo(lOffset, lSize);
		m_pHeader = new gr::byte [lSize];
		m_isValid = true;
		if (!m_pHeader) 
		{
			m_isValid = false;
			return;
		}
		m_isValid = (fseek(m_file, lOffset, SEEK_SET) == 0);
		size_t bytesRead = fread(m_pHeader, 1, lSize, m_file);
		Assert(static_cast<int>(bytesRead) == lSize);
		m_isValid = TtfUtil::CheckHeader(m_pHeader);

		if (!m_isValid) return;
		m_isValid = TtfUtil::GetTableDirInfo(m_pHeader, lOffset, lSize);
		m_pTableDir = new gr::byte[lSize];
		if (!m_pTableDir) 
		{
			m_isValid = false;
			return;
		}
		// if lOffset hasn't changed this isn't needed
		fseek(m_file, lOffset, SEEK_SET);
		bytesRead = fread(m_pTableDir, 1, lSize, m_file);
		Assert(static_cast<int>(bytesRead) == lSize);
		
		// now read head tables
		m_isValid = TtfUtil::GetTableInfo(ktiOs2, m_pHeader, m_pTableDir, 
										  lOffset, lSize);
		if (!m_isValid) 
		{
			return;
		}
		gr::byte * pTable = readTable(ktiOs2, lSize);
		// get ascent, descent, style while we have the OS2 table loaded
		if (!m_isValid || !pTable) 
		{
			return;
		}
		bool fBold, fItalic; // temp
		m_isValid = TtfUtil::FontOs2Style(pTable, fBold, fItalic);
		m_ascent = static_cast<float>(TtfUtil::FontAscent(pTable));
		m_descent = static_cast<float>(TtfUtil::FontDescent(pTable));
		delete[] pTable; // OS2
		
		pTable = readTable(ktiName, lSize);
		if (!m_isValid || !pTable) 
		{
			return;
		}
		if (!TtfUtil::Get31EngFamilyInfo(pTable, lOffset, lSize))
		{
			// not English name
			m_isValid = false;
			return;
		}
		Assert(lSize %2 == 0);// should be utf16
		int cchw = (lSize / sizeof(utf16)) + 1;
		cchw = min(cchw, 128);
		utf16 * pTable16 = reinterpret_cast<utf16*>(pTable + lOffset);
		utf16 rgchwFace[128];
		std::copy(pTable16, pTable16 + cchw - 1, rgchwFace);
		rgchwFace[cchw - 1] = 0;  // zero terminate
		TtfUtil::SwapWString(rgchwFace, cchw - 1);
// We could use something like "if (sizeof(std::wstring::value_type) == 4)" here,
// but a compile-time switch is preferable.
////#if SIZEOF_WCHAR_T == 4
		// Quick and dirty utf16 -> wchar_t:
		for (int c16 = 0; c16 < cchw; c16++)
		{
			m_stu32FaceName.push_back((wchar_t)rgchwFace[c16]);
		}
////#else -- for some reason this won't build on Windows, but the above approach works okay
////		m_stu32FaceName.assign(const_cast<utf16 *>(rgchwFace));
////#endif
		delete[] pTable; // name

		pTable = readTable(ktiHead, lSize);
		if (!m_isValid || !pTable) 
		{
			return;
		}
		m_emSquare = static_cast<float>(TtfUtil::DesignUnits(pTable));
		// can now set the scale
		m_xScale = scaleFromDpi(m_dpiX);
		m_yScale = scaleFromDpi(m_dpiY);
		delete[] pTable; // head
	}
	else
	{
		m_isValid = false;
	}
}

// Unlike original (FileFont) version, caller is responsible for deleting the allocated buffer.
gr::byte * GrcRtFileFont::readTable(int /*TableId*/ tid, size_t & size)
{
	TableId tableId = static_cast<TableId>(tid);
	bool isValid = true;
	size_t lOffset = 0, lSize = 0;
	//if (!m_pTableCache)
	//{
	//	m_pTableCache = new FontTableCache();		
	//}
	//if (!m_pTableCache) return NULL;
	
	gr::byte * pTable = NULL; // m_pTableCache->getTable(tableId);
	size = 0; // m_pTableCache->getTableSize(tableId);
	// check whether it is already in the cache
	//if (pTable) return pTable;
	isValid &= TtfUtil::GetTableInfo(tableId, m_pHeader, m_pTableDir, 
									  lOffset, lSize);
	if (!isValid) 
		return NULL;
	fseek(m_file, lOffset, SEEK_SET);

	//pTable = m_pTableCache->allocateTable(tableId, lSize);
	pTable = new gr::byte[lSize]; 
	if (!pTable) 
	{
		isValid = false;
		return NULL;
	}
	size_t bytesRead = fread(pTable, 1, lSize, m_file);
	isValid = (static_cast<int>(bytesRead) == lSize);
	if (isValid)
	{
		isValid &= TtfUtil::CheckTable(tableId, pTable, lSize);
	}
	if (!isValid) 
	{
		return NULL;
	}
	size = lSize;
	return pTable;
}


GrcRtFileFont::~GrcRtFileFont()
{
	
	// if this is the last copy of the font sharing these tables
	// delete them
	//if (m_pTableCache)
	//{
	//	m_pTableCache->decrementFontCount();
	//	if (m_pTableCache->getFontCount() == 0)
	//	{
	//		delete [] m_pHeader;
	//		delete [] m_pTableDir;
	//		delete m_pTableCache;
	//		m_pTableCache = NULL;
	//		if (m_file) fclose(m_file);
	//	}
	//}
	//else
	//{
		delete [] m_pHeader;
		delete [] m_pTableDir;
		if (m_file) fclose(m_file);
	//}
	// note the DecFontCount(); is done in the Font base class
}

GrcRtFileFont * GrcRtFileFont::copyThis()
{
	GrcRtFileFont * copy = new GrcRtFileFont(*this);
	return copy;
}


/**
* A copy constructor that allows a change of point size or dpi
* The underlying table cache will be shared between the fonts, so
* this should have a low overhead.
*/
/****
GrcRtFileFont::GrcRtFileFont(const FileFont & font, float pointSize, 
				   unsigned int dpiX, unsigned int dpiY)
: Font(font),
	m_file(font.m_file),
	m_ascent(font.m_ascent),
	m_descent(font.m_descent),
	m_emSquare(font.m_emSquare),
	m_pointSize(font.m_pointSize),
	m_dpiX(font.m_dpiX),
	m_dpiY(font.m_dpiY),
	m_isValid(font.m_isValid),
	m_pHeader(font.m_pHeader),
	m_pTableDir(font.m_pTableDir),
	m_xScale(font.m_xScale),
	m_yScale(font.m_yScale)
{
	if (pointSize > 0)
	{
		m_pointSize = pointSize;
		if (dpiX > 0)
		{
			m_dpiX = dpiX;

			if (dpiY > 0) m_dpiY = dpiY;
			else dpiY = dpiX;
		}
		m_xScale = scaleFromDpi(m_dpiX);
		m_yScale = scaleFromDpi(m_dpiY);
	}
	m_fItalic = font.m_fItalic;
	m_fBold = font.m_fBold;
	// colors are not used
	m_clrFore = font.m_clrFore;
	m_clrBack = font.m_clrBack;
	m_faceName.assign(font.m_faceName);
	
	m_pTableCache = font.m_pTableCache;
	// use the same table cache between instances
	if (m_pTableCache)
		m_pTableCache->incrementFontCount();
	
	
	// I dont' see why we need to reget the face, but WinFont does
	//m_pfface = FontFace::GetFontFace(this, m_fpropDef.szFaceName,
	//			m_fpropDef.fBold, m_fpropDef.fItalic);
}

****/

//virtual FontErrorCode isValidForGraphite(int * pnVersion = NULL, int * pnSubVersion = NULL);

/*----------------------------------------------------------------------------------------------
	Read a table from the font.
----------------------------------------------------------------------------------------------*/

const void * GrcRtFileFont::getTable(fontTableId32 tableID, size_t * pcbSize)
{
	*pcbSize = 0;
	// use a cache to reduce the number of times tables have to be reloaded
	//if (m_pTableCache == NULL)
	//{
	//	// constructor automatically sets the font count to 1
	//	m_pTableCache = new FontTableCache();
	//} 
	//TableId tid;
	//for (int i = 0; i<ktiLast; i++)
	//{
	//	tid = static_cast<TableId>(i);
	//	if (tableID == TtfUtil::TableIdTag(tid))
	//	{
	//		if (m_pTableCache->getTable(tid))
	//		{
	//			*pcbSize = m_pTableCache->getTableSize(tid);
	//			return m_pTableCache->getTable(tid);
	//		}
	//		break;	
	//	}
	//}

	// Map backwards from the fontTableId32 to the kti enum (convoluted, but this is based
	// on the way FileFont was originally implemented):
	TableId tid;
	for (int i = 0; i < ktiLast; i++)
	{
		tid = static_cast<TableId>(i);
		if (tableID == TtfUtil::TableIdTag(tid))
			break;
	}
	Assert(tid < ktiLast);

	size_t tableSize = 0;
	void * pTable = readTable(tid, tableSize);
	*pcbSize = static_cast<int>(tableSize);
	return pTable;
}


void GrcRtFileFont::getFontMetrics(float * pAscent, float * pDescent,
		float * pEmSquare)
{
	if (pEmSquare)
		*pEmSquare = m_emSquare * m_xScale;
	if (pAscent)
		*pAscent = m_ascent * m_yScale;
	if (pDescent)
		*pDescent = m_descent * m_yScale;
}

/****
bool GrcRtFileFont::FontHasGraphiteTables(FILE * file)
{
	FileFont testFont(file, 1.0f, FUDGE_FACTOR);
	return testFont.fontHasGraphiteTables();
}

bool GrcRtFileFont::FontHasGraphiteTables(char * fileName)
{
	FileFont testFont(fileName, 1.0f, FUDGE_FACTOR);
	return testFont.fontHasGraphiteTables();
}

bool GrcRtFileFont::fontHasGraphiteTables()
{
	long tableSize;
	bool isGraphiteFont = m_isValid;
	isGraphiteFont &= (readTable(ktiSilf, tableSize) != NULL);
	return isGraphiteFont;
}
****/