summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/xetexdir/XeTeXFontInst.cpp
blob: fc21a6837a0aa583e5e797b49467d4440223929c (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
/****************************************************************************\
 Part of the XeTeX typesetting system
 copyright (c) 1994-2006 by SIL International
 written by Jonathan Kew

Permission is hereby granted, free of charge, to any person obtaining  
a copy of this software and associated documentation files (the  
"Software"), to deal in the Software without restriction, including  
without limitation the rights to use, copy, modify, merge, publish,  
distribute, sublicense, and/or sell copies of the Software, and to  
permit persons to whom the Software is furnished to do so, subject to  
the following conditions:

The above copyright notice and this permission notice shall be  
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
NONINFRINGEMENT. IN NO EVENT SHALL SIL INTERNATIONAL BE LIABLE FOR  
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of SIL International  
shall not be used in advertising or otherwise to promote the sale,  
use or other dealings in this Software without prior written  
authorization from SIL International.
\****************************************************************************/

/*
 *   file name:  XeTeXFontInst.cpp
 *
 *   created on: 2005-10-22
 *   created by: Jonathan Kew
 *	
 *	originally based on PortableFontInstance.cpp from ICU
 */


#include "LETypes.h"
#include "LEFontInstance.h"
#include "LESwaps.h"

#include "XeTeXFontInst.h"
#include "XeTeXLayoutInterface.h"

#ifdef XETEX_MAC
#include <Carbon/Carbon.h>
#endif

#include "XeTeX_ext.h"

#include "sfnt.h"

#include <string.h>


XeTeXFontInst::XeTeXFontInst(float pointSize, LEErrorCode &status)
    : fPointSize(pointSize)
    , fUnitsPerEM(0)
    , fAscent(0)
    , fDescent(0)
    , fLeading(0)
    , fXHeight(0)
    , fItalicAngle(0)
    , fCMAPMapper(NULL)
    , fMetricsTable(NULL)
    , fNumLongMetrics(0)
    , fNumGlyphs(0)
    , fNumGlyphsInited(false)
    , fVertical(false)
    , fFilename(NULL)
    , fFirstCharCode(-1)
    , fLastCharCode(-1)
{
	// the concrete subclass is responsible to call initialize()
}

XeTeXFontInst::~XeTeXFontInst()
{
	if (fMetricsTable != NULL)
		deleteTable(fMetricsTable);

	if (fCMAPMapper != NULL)
		delete fCMAPMapper;
}

void XeTeXFontInst::initialize(LEErrorCode &status)
{
    const LETag headTag = LE_HEAD_TABLE_TAG;
    const LETag hheaTag = LE_HHEA_TABLE_TAG;
    const LETag vheaTag = LE_VHEA_TABLE_TAG;
    const LETag postTag = LE_POST_TABLE_TAG;
    const HEADTable *headTable = NULL;
    const HHEATable *dirHeadTable = NULL;
    const POSTTable *postTable = NULL;

    // dispose of any cached metrics table
    if (fMetricsTable != NULL) {
	    deleteTable(fMetricsTable);
    	fMetricsTable = NULL;
    }
    
    // read unitsPerEm from 'head' table
    headTable = (const HEADTable *) readFontTable(headTag);

    if (headTable == NULL) {
        status = LE_MISSING_FONT_TABLE_ERROR;
        goto error_exit;
    }

    fUnitsPerEM = SWAPW(headTable->unitsPerEm);
    deleteTable(headTable);

	// we use the fact that 'hhea' and 'vhea' have the same format!
    dirHeadTable = (const HHEATable *) readFontTable(fVertical ? vheaTag : hheaTag);

    if (dirHeadTable == NULL) {
        status = LE_MISSING_FONT_TABLE_ERROR;
        goto error_exit;
    }

    fAscent  = yUnitsToPoints((float)(le_int16)SWAPW(dirHeadTable->ascent));
    fDescent = yUnitsToPoints((float)(le_int16)SWAPW(dirHeadTable->descent));
    fLeading = yUnitsToPoints((float)(le_int16)SWAPW(dirHeadTable->lineGap));

    fNumLongMetrics = SWAPW(dirHeadTable->numOfLongHorMetrics);

    deleteTable(dirHeadTable);

    fCMAPMapper = findUnicodeMapper();

    if (fCMAPMapper == NULL) {
        status = LE_MISSING_FONT_TABLE_ERROR;
        goto error_exit;
    }

    postTable = (const POSTTable *) readFontTable(postTag);

    if (postTable != NULL) {
		fItalicAngle = Fix2X(SWAPL(postTable->italicAngle));
		deleteTable(postTable);
    }

    return;

error_exit:
    return;
}

void XeTeXFontInst::setLayoutDirVertical(bool vertical)
{
	fVertical = vertical;
	LEErrorCode	status = LE_NO_ERROR;
	initialize(status);
}

void XeTeXFontInst::deleteTable(const void *table) const
{
    LE_DELETE_ARRAY(table);
}

const void *XeTeXFontInst::getFontTable(LETag tableTag) const
{
    return FontTableCache::find(tableTag);
}

const void *XeTeXFontInst::readFontTable(LETag tableTag) const
{
    le_uint32 len;

    return readTable(tableTag, &len);
}

const void *XeTeXFontInst::readFontTable(LETag tableTag, le_uint32& len) const
{
    return readTable(tableTag, &len);
}

CMAPMapper *XeTeXFontInst::findUnicodeMapper()
{
    LETag cmapTag = LE_CMAP_TABLE_TAG;
    const CMAPTable *cmap = (CMAPTable *) readFontTable(cmapTag);

    if (cmap == NULL) {
        return NULL;
    }

    return CMAPMapper::createUnicodeMapper(cmap);
}


le_uint16 XeTeXFontInst::getNumGlyphs() const
{
    if (!fNumGlyphsInited) {
        LETag maxpTag = LE_MAXP_TABLE_TAG;
        const MAXPTable *maxpTable = (MAXPTable *) readFontTable(maxpTag);

        if (maxpTable != NULL) {
			XeTeXFontInst *realThis = (XeTeXFontInst *) this;
            realThis->fNumGlyphs = SWAPW(maxpTable->numGlyphs);
            deleteTable(maxpTable);
			realThis->fNumGlyphsInited = true;
		}
    }

	return fNumGlyphs;
}

void XeTeXFontInst::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
{
    TTGlyphID ttGlyph = (TTGlyphID) LE_GET_GLYPH(glyph);

    if (fMetricsTable == NULL) {
    	// we use the fact that 'hmtx' and 'vmtx' have the same format
        LETag metricsTag = fVertical ? LE_VMTX_TABLE_TAG : LE_HMTX_TABLE_TAG;
        XeTeXFontInst *realThis = (XeTeXFontInst *) this;
        realThis->fMetricsTable = (const HMTXTable *) readFontTable(metricsTag);
    }

    le_uint16 index = ttGlyph;

    if (ttGlyph >= getNumGlyphs() || fMetricsTable == NULL) {
        advance.fX = advance.fY = 0;
        return;
    }

    if (ttGlyph >= fNumLongMetrics) {
        index = fNumLongMetrics - 1;
    }

    advance.fX = xUnitsToPoints(SWAPW(fMetricsTable->hMetrics[index].advanceWidth));
    advance.fY = 0;
}

le_bool XeTeXFontInst::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
{
    return FALSE;
}

float
XeTeXFontInst::getGlyphWidth(LEGlyphID gid)
{
	LEPoint	advance;
	getGlyphAdvance(gid, advance);
	return advance.fX;
}

void
XeTeXFontInst::getGlyphHeightDepth(LEGlyphID gid, float* ht, float* dp)
{
	GlyphBBox	bbox;
	getGlyphBounds(gid, &bbox);
	
	*ht = bbox.yMax;
	*dp = -bbox.yMin;
}

void
XeTeXFontInst::getGlyphSidebearings(LEGlyphID gid, float* lsb, float* rsb)
{
	LEPoint	adv;
	getGlyphAdvance(gid, adv);

	GlyphBBox	bbox;
	getGlyphBounds(gid, &bbox);

	*lsb = bbox.xMin;
	*rsb = adv.fX - bbox.xMax;
}

float
XeTeXFontInst::getGlyphItalCorr(LEGlyphID gid)
{
	float	rval = 0.0;

	LEPoint	adv;
	getGlyphAdvance(gid, adv);

	GlyphBBox	bbox;
	getGlyphBounds(gid, &bbox);
	
	if (bbox.xMax > adv.fX)
		rval = bbox.xMax - adv.fX;
	
	return rval;
}

LEGlyphID
XeTeXFontInst::mapGlyphToIndex(const char* glyphName) const
	/* default implementation, may be overridden (e.g. by Freetype-based XeTeXFontInst_ */
{
    le_uint32	len;
    const char *p = (const char*)readFontTable(LE_POST_TABLE_TAG, len);
    if (p != NULL)
		return findGlyphInPostTable(p, len, glyphName);
	else
		return 0;
}

const char*
XeTeXFontInst::getGlyphName(LEGlyphID gid, int& nameLen)
{
    le_uint32	len;
    const char *p = (const char*)readFontTable(LE_POST_TABLE_TAG, len);
    if (p != NULL) {
		return getGlyphNamePtr(p, len, gid, &nameLen);
	}
}

LEUnicode32
XeTeXFontInst::getFirstCharCode()
{
	if (fFirstCharCode == -1) {
		int ch = 0;
		while (mapCharToGlyph(ch) == 0 && ch < 0x10ffff)
			++ch;
		fFirstCharCode = ch;
	}
	return fFirstCharCode;
}

LEUnicode32
XeTeXFontInst::getLastCharCode()
{
	if (fLastCharCode == -1) {
		int ch = 0x10ffff;
		while (mapCharToGlyph(ch) == 0 && ch > 0)
			--ch;
		fLastCharCode = ch;
	}
	return fLastCharCode;
}