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

 SIL Author(s): 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 THE COPYRIGHT HOLDERS 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 the copyright holders
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written
authorization from the copyright holders.
\****************************************************************************/

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

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

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

#include "XeTeX_ext.h"

#include "sfnt.h"

#include <string.h>


XeTeXFontInst::XeTeXFontInst(float pointSize, int &status)
    : fPointSize(pointSize)
    , fUnitsPerEM(0)
    , fAscent(0)
    , fDescent(0)
    , fItalicAngle(0)
    , fMetricsTable(NULL)
    , fNumLongMetrics(0)
    , fNumGlyphs(0)
    , fNumGlyphsInited(false)
    , fVertical(false)
    , fFilename(NULL)
    , hbFont(NULL)
{
	// the concrete subclass is responsible to call initialize()
}

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

void XeTeXFontInst::initialize(int &status)
{
    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(kHEAD);

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

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

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

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

    fAscent  = unitsToPoints((float)(int16_t)SWAP(dirHeadTable->ascent));
    fDescent = unitsToPoints((float)(int16_t)SWAP(dirHeadTable->descent));

    fNumLongMetrics = SWAP(dirHeadTable->numOfLongHorMetrics);

    deleteTable(dirHeadTable);

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

    if (postTable != NULL) {
		fItalicAngle = Fix2D(SWAP(UInt32(postTable->italicAngle)));
		deleteTable(postTable);
    }

    return;

error_exit:
    return;
}

void XeTeXFontInst::setLayoutDirVertical(bool vertical)
{
	fVertical = vertical;
	int	status = 0;
	initialize(status);
}

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

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

const void *XeTeXFontInst::getFontTable(OTTag tableTag, uint32_t *length) const
{
    return FontTableCache::find(tableTag, length);
}

const void *XeTeXFontInst::readFontTable(OTTag tableTag) const
{
    uint32_t len;

    return readTable(tableTag, &len);
}

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

uint16_t XeTeXFontInst::getNumGlyphs() const
{
    if (!fNumGlyphsInited) {
        const MAXPTable *maxpTable = (MAXPTable *) readFontTable(kMAXP);

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

	return fNumGlyphs;
}

void XeTeXFontInst::getGlyphAdvance(GlyphID glyph, realpoint &advance) const
{
    if (fMetricsTable == NULL) {
        XeTeXFontInst *realThis = (XeTeXFontInst *) this;
        // we use the fact that 'hmtx' and 'vmtx' have the same format
        realThis->fMetricsTable = (const HMTXTable *) readFontTable(fVertical ? kVMTX : kHMTX);
    }

    uint16_t index = glyph;

    if (glyph >= getNumGlyphs() || fMetricsTable == NULL) {
        advance.x = advance.y = 0;
        return;
    }

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

    advance.x = unitsToPoints(SWAP(fMetricsTable->hMetrics[index].advanceWidth));
    advance.y = 0;
}

float
XeTeXFontInst::getGlyphWidth(GlyphID gid)
{
	realpoint	advance;
	getGlyphAdvance(gid, advance);
	return advance.x;
}

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

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

	GlyphBBox	bbox;
	getGlyphBounds(gid, &bbox);

	if (lsb)
		*lsb = bbox.xMin;
	if (rsb)
		*rsb = adv.x - bbox.xMax;
}

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

	realpoint	adv;
	getGlyphAdvance(gid, adv);

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

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

const char*
XeTeXFontInst::getGlyphName(GlyphID gid, int& nameLen)
{
    uint32_t	len;
    const char *p = (const char*)readFontTable(kPOST, len);
    if (p != NULL)
		return getGlyphNamePtr(p, len, gid, &nameLen);
	else
		return NULL;
}