summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/xetexdir/XeTeXFontMgr.h
blob: ba23b05c28e72325389a7c7596cc76f9fa1a3ac1 (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
/****************************************************************************\
 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.
\****************************************************************************/

#ifndef __XETEX_FONT_MANAGER_H
#define __XETEX_FONT_MANAGER_H

#ifdef XETEX_MAC
#include <Carbon/Carbon.h>
typedef ATSFontRef	PlatformFontRef;
#else
#include <fontconfig/fontconfig.h>
#include <ft2build.h>
#include FT_FREETYPE_H
typedef FcPattern*	PlatformFontRef;
#endif

#include "XeTeX_ext.h"

#include "XeTeXLayoutInterface.h"

#ifdef __cplusplus	/* allow inclusion in plain C files just to get the typedefs above */

#include <string>
#include <map>
#include <list>
#include <vector>

class XeTeXFontMgr
{
public:
	static XeTeXFontMgr*			GetFontManager();
		// returns the global fontmanager (creating it if necessary)
	static void						Terminate();
		// clean up (may be required if using the cocoa implementation)

	PlatformFontRef					findFont(const char* name, char* variant, double ptSize);
		// 1st arg is name as specified by user (C string, UTF-8)
		// 2nd is /B/I/AAT/ICU[/USP]/S=## qualifiers
		// 1. try name given as "full name"
		// 2. if there's a hyphen, split and try "family-style"
		// 3. try as PostScript name
		// 4. try name as family with "Regular/Plain/Normal" style
		// apply style qualifiers and optical sizing if present

		// SIDE EFFECT: sets sReqEngine to 'A' or 'I' [or 'U'] if appropriate,
		//   else clears it to 0

		// SIDE EFFECT: updates TeX variables /nameoffile/ and /namelength/,
		//   to match the actual font found

		// SIDE EFFECT: edits /variant/ string in-place removing /B or /I
		
	const char*						getFullName(PlatformFontRef font) const;
		// return the full name of the font, suitable for use in XeTeX source
		// without requiring style qualifiers

	void							getNames(PlatformFontRef font, const char** psName,
											const char** famName, const char** styName) const;
		// return Postscript, family, and style names, for use in .xdv

	double							getDesignSize(XeTeXFont font);

	char							getReqEngine() const;
		// return the requested rendering technology for the most recent findFont
		// or 0 if no specific technology was requested

protected:
	static XeTeXFontMgr*			sFontManager;
	static char						sReqEngine;
	
									XeTeXFontMgr()
										{ }
	virtual							~XeTeXFontMgr()
										{ }
										
	virtual void					initialize() = 0;
	virtual void					terminate();

	virtual std::string				getPlatformFontDesc(PlatformFontRef font) const = 0;

	class Font;
	class Family;

	struct OpSizeRec {
		unsigned short	designSize;
		unsigned short	subFamilyID;
		unsigned short	nameCode;
		unsigned short	minSize;
		unsigned short	maxSize;
	};

	class Font {
		public:
							Font(PlatformFontRef ref)
								: fullName(NULL), psName(NULL), familyName(NULL), styleName(NULL)
								, parent(NULL)
								, fontRef(ref), weight(0), width(0), slant(0)
								, isReg(false), isBold(false), isItalic(false)
								{ opSizeInfo.subFamilyID = 0;
								  opSizeInfo.designSize = 100; } /* default to 10bp */
							~Font()
								{ delete fullName; delete psName; }

			std::string*	fullName;
			std::string*	psName;
			std::string*	familyName;	// default family and style names that should locate this font
			std::string*	styleName;
			Family*			parent;
			PlatformFontRef	fontRef;
			OpSizeRec		opSizeInfo;
			UInt16			weight;
			UInt16			width;
			SInt16			slant;
			bool			isReg;
			bool			isBold;
			bool			isItalic;
	};
	
	class Family {
		public:
											Family()
												: minWeight(0), maxWeight(0)
												, minWidth(0), maxWidth(0)
												, minSlant(0), maxSlant(0)
												{
													styles = new std::map<std::string,Font*>;
												}
											~Family()
												{
													delete styles;
												}

			std::map<std::string,Font*>*	styles;
			UInt16							minWeight;
			UInt16							maxWeight;
			UInt16							minWidth;
			UInt16							maxWidth;
			SInt16							minSlant;
			SInt16							maxSlant;
	};

	class NameCollection {
	public:
		std::list<std::string>	familyNames;
		std::list<std::string>	styleNames;
		std::list<std::string>	fullNames;
		std::string				psName;
		std::string				subFamily;
	};	

	std::map<std::string,Font*>					nameToFont;						// maps full name (as used in TeX source) to font record
	std::map<std::string,Family*>				nameToFamily;
	std::map<PlatformFontRef,Font*>				platformRefToFont;
	std::map<std::string,Font*>					psNameToFont;					// maps PS name (as used in .xdv) to font record

	int				weightAndWidthDiff(const Font* a, const Font* b) const;
	int				styleDiff(const Font* a, int wt, int wd, int slant) const;
	Font*			bestMatchFromFamily(const Family* fam, int wt, int wd, int slant) const;
	void			appendToList(std::list<std::string>* list, const char* str);
	void			prependToList(std::list<std::string>* list, const char* str);
	void			addToMaps(PlatformFontRef platformFont, const NameCollection* names);

	const OpSizeRec* getOpSizePtr(XeTeXFont font);

	virtual void	getOpSizeRecAndStyleFlags(Font* theFont);
	virtual void	searchForHostPlatformFonts(const std::string& name) = 0;
	
#ifdef XETEX_MAC
	virtual NameCollection*		readNames(ATSUFontID fontID) = 0;
#else
	virtual NameCollection*		readNames(PlatformFontRef fontRef) = 0;
#endif

	void	die(const char*s, int i) const;	/* for fatal internal errors! */
};

#endif	/* __cplusplus */


#endif	/* __XETEX_FONT_MANAGER_H */