summaryrefslogtreecommitdiff
path: root/Build/source/libs/graphite/src/segment/GrGlyphTable.h
blob: bec0e518dee156430c112b21c69e54a3a0cbe89c (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
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
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 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: GrGlyphTable.h
Responsibility: Sharon Correll
Last reviewed: 10Aug99, JT, quick look over

Description:
    The GrGlyphTable and related classes that store the values of glyph attributes.

	For each glyph in a TrueType font, Graphite has a list of glyph attributes (which
	are defined in the GDL script file). Since most of the values will be zero, we save space
	by only storing the non-zero values.

	Eventually it may be possible to have more than one TrueType font compiled into a single
	glyph table, hence the need for multiple sub-tables. For now we assume that there is only
	one font file and only one sub-table.

	Note that BIG in a variable name means that the data is in big-endian format, or is a range
	of bytes that must be interpreted as a run of wide chars or ints in big-endian format.
-------------------------------------------------------------------------------*//*:End Ignore*/

//:Ignore
#ifdef _MSC_VER
#pragma once
#endif
#ifndef GR_GTABLE_INCLUDED
#define GR_GTABLE_INCLUDED
//:End Ignore

namespace gr
{

class Font;

/*----------------------------------------------------------------------------------------------
	A run of non-zero attribute values for a single glyph, where the attribute IDs
	are contiguous.

	NOTE: it is assumed that the format of this class matches the byte order of the data as it
	is read directly from the TTF/ECF file--that is, it is big-endian.
	
	Hungarian: gatrun
----------------------------------------------------------------------------------------------*/

class GrGlyphAttrRun
{
	friend class GrGlyphTable;
	friend class GrGlyphSubTable;
	friend class GrGlyphAttrTable;

	enum {
		kMaxAttrsPerRun = 255
	};

	/*------------------------------------------------------------------------------------------
		Copy the raw memory (starting at the given byte) into the instance. This method must
		reflect the format of the "Glat_entry" as it is stored in the file and slurped into
		memory.
	------------------------------------------------------------------------------------------*/
	void CopyFrom(byte * pbBIGEnt)
	{
		m_bMinAttrID = *pbBIGEnt;
		m_cAttrs = *(pbBIGEnt + 1);
		if (m_cAttrs >= kMaxAttrsPerRun)
		{
			gAssert(false);
			m_cAttrs = kMaxAttrsPerRun;
		}
		#ifdef _DEBUG
		// Probably not strictly necessary to zero the array, but convenient for debugging.
		// Removed from release build for optimization
		std::fill(m_rgchwBIGValues, m_rgchwBIGValues + kMaxAttrsPerRun, 0);
		#endif
		// this is mixing types of data16 and byte pointers!
		const data16 * prgchw = reinterpret_cast<const data16*>(pbBIGEnt + 2);
		std::copy(prgchw, prgchw + m_cAttrs, m_rgchwBIGValues);
	}

	int ByteCount()
	{
		return (2 + (m_cAttrs * isizeof(data16)));
	}

protected:
	byte m_bMinAttrID;	// ID of first attribute in the run
	byte m_cAttrs;		// number of attributes in the run
	data16 m_rgchwBIGValues[kMaxAttrsPerRun];
};

/*----------------------------------------------------------------------------------------------
	Contains runs of attribute values for all the glyphs in the font; corresponds to
	the "Glat" table in the font.

	Hungarian: gatbl
----------------------------------------------------------------------------------------------*/

class GrGlyphAttrTable
{
	friend class GrGlyphTable;
	friend class GrGlyphSubTable;
	friend class FontMemoryUsage;

protected:
	//	Constructor:
	GrGlyphAttrTable()
	{
		m_prgbBIGEntries = NULL;
	}

	//	Destructor:
	~GrGlyphAttrTable()
	{
		delete[] m_prgbBIGEntries;
	}

	//	Initialization:
	void Initialize(int fxdSilfVersion, int cbBufLen)
	{
		m_fxdSilfVersion = fxdSilfVersion;
		m_cbEntryBufLen = cbBufLen;
		m_prgbBIGEntries = new byte[cbBufLen];
		//	Now the instance is ready to have all the glyph attr entries slurped into
		//	the byte array from the file.
	}

	int GlyphAttr16BitValue(int ibMin, int ibLim, byte bAttrID);

protected:
	int m_fxdSilfVersion;		// version number of the Silf table, which is used
								// to interpret the attribute values

	//	Block of variable-length glyph attribute runs, matching the format of
	//	GrGlyphAttrRun. We don't store instances of that class here because they
	//	are variable length, and it would be too slow to read them individually from the
	//	font. Instead, we set up a single instance in the method that accesses the values;
	//	having this instance will help debugging. Eventually we may want to do without it,
	//	if it would help efficiency.
	int m_cbEntryBufLen;	// needed only for memory instrumentation
	byte * m_prgbBIGEntries;

//:Ignore
#ifdef OLD_TEST_STUFF
public:
	//	For test procedures:
	void SetUpTestData();
	void SetUpLigatureTest();
	void SetUpLigature2Test();
#endif // OLD_TEST_STUFF

//:End Ignore

};

/*----------------------------------------------------------------------------------------------
	One glyph table per font (style) file; corresponds to the "Gloc" table in the font.
	Currently there is only considered to be one style per file, so there is only one of
	these. It holds the (non-zero) glyph attribute values for every glyph in the font.

	Hungarian: gstbl

	Review: Eventually we may need to make a subclass that uses 32-bit values for the offsets,
	or just use a separate array pointer in this class. Which would be preferable?
----------------------------------------------------------------------------------------------*/

class GrGlyphSubTable
{
	friend class GrGlyphTable;
	friend class FontMemoryUsage;

public:
	//	Constructor:
	GrGlyphSubTable() :
		m_pgatbl(NULL),
		m_prgibBIGAttrValues(NULL),
		m_prgibBIGGlyphAttrDebug(NULL),
		m_prgnDefinedComponents(NULL)
	{
	}
	//	Destructor:
	~GrGlyphSubTable()
	{
		delete m_pgatbl;
		delete[] m_prgibBIGAttrValues;
		if (m_fHasDebugStrings)
			delete[] m_prgibBIGGlyphAttrDebug;
		delete[] m_prgnDefinedComponents;
	}

	//	Initialization:
	bool ReadFromFont(GrIStream & gloc_strm, int cGlyphs, 
		GrIStream & glat_strm, long lGlatStart);
	void Initialize(int fxdSilfVersion, data16 chwFlags,
		data16 chwBWAttr, data16 chwJStrAttr, data16 chwJStrHWAttr,
		int cGlyphs, int cAttrs, int cnCompPerLig);

	void CreateEmpty();

	void SetNumberOfComponents(int c)
	{
		m_cComponents = c;
	}

	//	General:
	int NumberOfGlyphAttrs()	{ return m_nAttrIDLim; }	// 0..(m_nAttrIDLim-1)

	int GlyphAttrValue(gid16 chwGlyphID, int nAttrID);

	//	Ligatures:
	int ComponentContainingPoint(gid16 chwGlyphID, int x, int y);
	bool ComponentBoxLogUnits(float xysEmSquare, gid16 chwGlyphID, int icomp,
		int mFontEmUnits, float dysAscent,
		float * pxsLeft, float * pysTop, float * pxsRight, float * pysBottom,
		bool fTopOrigin = true);
protected:
	int CalculateDefinedComponents(gid16 chwGlyphID);
	bool ComponentIsDefined(gid16 chwGlyphID, int nAttrID);
	int ComponentIndexForGlyph(gid16 chwGlyphID, int nCompID);
//	int NthComponentID(gid16 chwGlyphID, int n);
	int ConvertValueForVersion(int nRet, int nAttrID);

public:
	static int ConvertValueForVersion(int nRet, int nAttrID, int nBWAttr, int fxdVersion);

	//	Flags:
	bool LongFormat(data16 chwFlags)	// 32-bit values?
	{
		return ((chwFlags & 0x01) == 0x01);	// bit 0
	}
	bool HasAttrNames(data16 chwFlags)
	{
		return ((chwFlags & 0x02) == 0x02);	// bit 1
	}
	int GlocLookup(data16 chwGlyphId)
	{
		if (m_fGlocShort)
		{
			unsigned short su = ((unsigned short *)m_prgibBIGAttrValues)[chwGlyphId];
			return lsbf(su);
		}
		else
		{	unsigned int u = ((unsigned int *)m_prgibBIGAttrValues)[chwGlyphId];
			return lsbf((int)u);
		}
	}

protected:
	int m_fxdSilfVersion;		// version number of the Silf table, which is used
								// to interpret the attribute values
	bool m_fHasDebugStrings;	// are debugging strings loaded into memory?

	int m_nAttrIDLim;		// number of glyph attributes
	int m_cComponents;		// number of initial glyph attributes that
							// represent ligature components
	int m_cnCompPerLig;

	GrGlyphAttrTable * m_pgatbl;
	byte * m_prgibBIGAttrValues;		// byte offsets for glyph attr values - BIG endian
	bool m_fGlocShort;					// flag for Gloc table format
	data16 * m_prgibBIGGlyphAttrDebug;	// byte offsets for glyph attr debug strings - BIG endian

	data16 m_chwBWAttr;		// breakweight attr ID; needed for converting
							// between versions

	//	Attr IDs for justify.0.stretch and justify.0.stretchHW; these must always be looked
	//	up in tandem.
	data16 m_chwJStrAttr;
	data16 m_chwJStrHWAttr;

	int * m_prgnDefinedComponents;	// for each glyph, cache list of component attributes that
									// are defined

//:Ignore
#ifdef OLD_TEST_STUFF
public:
	//	For test procedures:
	void SetUpTestData();
	void SetUpLigatureTest();
	void SetUpLigature2Test();
#endif // OLD_TEST_STUFF

//:End Ignore
};

/*----------------------------------------------------------------------------------------------
	Holds all the information about glyph attributes.

	Hungarian: gtbl
----------------------------------------------------------------------------------------------*/

class GrGlyphTable
{
	friend class GrGlyphSubTable;
	friend class FontMemoryUsage;

public:
	//	Constructor:
	GrGlyphTable()
	{
		m_cglf = 0;
		m_cComponents = 0;
		m_cgstbl = 0;
		m_vpgstbl.clear();
	}

	//	Destructor:
	~GrGlyphTable()
	{
		for (int i = 0; i < m_cgstbl; ++i)
			delete m_vpgstbl[i];
	}

	bool ReadFromFont(GrIStream & gloc_strm, long lGlocStart, 
		GrIStream & glat_strm, long lGlatStart, 
		data16 chwBWAttr, data16 chwJStrAttr, int cJLevels, int cnCompPerLig, 
		int fxdSilfVersion);

	void CreateEmpty();

	//	Setters:
	void SetNumberOfGlyphs(int c)
	{
		m_cglf = c;
	}
	void SetNumberOfStyles(int c)
	{
		m_cgstbl = c;
		m_vpgstbl.resize(c);
	}
	void SetNumberOfComponents(int c)
	{
		m_cComponents = c;
		for (unsigned int ipgstbl = 0; ipgstbl < m_vpgstbl.size(); ipgstbl++)
			m_vpgstbl[ipgstbl]->SetNumberOfComponents(c);
	}
	void SetSubTable(int i, GrGlyphSubTable * pgstbl)
	{
		if (signed(m_vpgstbl.size()) <= i)
		{
			gAssert(false);
			m_vpgstbl.resize(i+1);
		}
		m_vpgstbl[i] = pgstbl;
		m_vpgstbl[i]->SetNumberOfComponents(m_cComponents);
	}

	//	Getters:
	int NumberOfGlyphs()		{ return m_cglf; }
	int NumberOfStyles()		{ return m_cgstbl; }

	int NumberOfGlyphAttrs()
	{
		//	All of the sub-tables should have the same number of glyph attributes,
		//	so just ask the first.
		return m_vpgstbl[0]->NumberOfGlyphAttrs();
	}

	int	GlyphAttrValue(gid16 chwGlyphID, int nAttrID)
	{
		gAssert(m_cgstbl == 1);
		return m_vpgstbl[0]->GlyphAttrValue(chwGlyphID, nAttrID);
	}
	//	Eventually:
	//int GlyphAttrValue(gid16 chwGlyphID, int nAttrID, int nStyleIndex)
	//{
	//	return m_vpgstbl[nStyleIndex]->GlyphAttrValue(chwGlyphID, nAttrID);
	//}

	int ComponentContainingPoint(gid16 chwGlyphID, int x, int y)
	{
		gAssert(m_cgstbl == 1);
		return m_vpgstbl[0]->ComponentContainingPoint(chwGlyphID, x, y);
	}
	//	Eventually:
	//int ComponentContainingPoint(gid16 chwGlyphID, int nStyleIndex, int x, int y)
	//{
	//	gAssert(m_cgstbl == 1);
	//	return m_vpgstbl[nStyleIndex]->ComponentContainingPoint(chwGlyphID, nAttrID);
	//}

	bool ComponentBoxLogUnits(float xysEmSquare, gid16 chwGlyphID, int icomp,
		int mFontEmUnits, float dysAscent,
		float * pxsLeft, float * pysTop, float * pxsRight, float * pysBottom,
		bool fTopOrigin = true)
	{
		gAssert(m_cgstbl == 1);
		return m_vpgstbl[0]->ComponentBoxLogUnits(xysEmSquare,
			chwGlyphID, icomp, mFontEmUnits, dysAscent,
			pxsLeft, pysTop, pxsRight, pysBottom,
			fTopOrigin);
	}

	int ComponentIndexForGlyph(gid16 chwGlyphID, int nCompID)
	{
		gAssert(m_cgstbl == 1);
		return m_vpgstbl[0]->ComponentIndexForGlyph(chwGlyphID, nCompID);
	}

	bool IsEmpty()
	{
		return (m_cglf == 0);
	}

protected:
	int m_cglf;			// number of glyphs
	int m_cComponents;	// number of defined components
	int m_cgstbl;		// number of sub-tables, corresponding to font files;
						// for now there is only one

	std::vector<GrGlyphSubTable *> m_vpgstbl;

//:Ignore
#ifdef OLD_TEST_STUFF
public:
	//	For test procedures:
	void SetUpTestData();
	void SetUpLigatureTest();
	void SetUpLigature2Test();
#endif // OLD_TEST_STUFF
//:End Ignore

};

} // namespace gr

#endif // !GR_GTABLE_INCLUDED