1 /*--------------------------------------------------------------------*//*:Ignore this sentence.
2 Copyright (C) 1999, 2001 SIL International. All rights reserved.
3 
4 Distributable under the terms of either the Common Public License or the
5 GNU Lesser General Public License, as specified in the LICENSING.txt file.
6 
7 File: Font.h
8 Responsibility: Sharon Correll
9 Last reviewed: Not yet.
10 
11 Description:
12     A Font is an object that represents a font-family + bold + italic setting, that contains
13 	Graphite tables. This file also includes related iterators: FeatureIterator,
14 	FeatureSettingIterator, FeatLabelLangIterator, LanguageIterator.
15 ----------------------------------------------------------------------------------------------*/
16 #ifdef _MSC_VER
17 #pragma once
18 #endif
19 #ifndef FONT_INCLUDED
20 #define FONT_INCLUDED
21 
22 //:End Ignore
23 
24 namespace gr
25 {
26 
27 class Font;
28 class FontFace;
29 class FeatureSettingIterator;
30 class Segment;
31 class LayoutEnvironment;
32 class ITextSource;
33 class FontMemoryUsage;
34 
35 typedef struct tagFontProps
36 {
37 	unsigned long clrFore;
38 	unsigned long clrBack;
39 	bool fBold;
40 	bool fItalic;
41 	float pixHeight;
42 	wchar_t szFaceName[ 32 ];
43 } FontProps;
44 
45 
46 /*----------------------------------------------------------------------------------------------
47 	A FontError object is an exception that is thrown when there is an error in initializing
48 	a Graphite font.
49 ----------------------------------------------------------------------------------------------*/
50 struct FontException
51 {
52 	FontErrorCode errorCode;
53 	int version;
54 	int subVersion;
55 };
56 
57 /*----------------------------------------------------------------------------------------------
58 	Iterator to provide access to a font's features.
59 ----------------------------------------------------------------------------------------------*/
60 class FeatureIterator
61 {
62 	friend class Font;
63 	friend class FeatureSettingIterator;
64 
65 public:
FeatureIterator()66 	FeatureIterator() // needed for creating std::pair of these, and for default FSI
67 	{
68 		m_pfont = NULL;
69 		m_ifeat = 0;
70 		m_cfeat = 0;
71 	}
72 
73 protected:
FeatureIterator(Font * pfont,int ifeat,size_t cfeat)74 	FeatureIterator(Font * pfont, int ifeat, size_t cfeat)
75 	{
76 		m_pfont = pfont;
77 		m_ifeat = ifeat;
78 		m_cfeat = cfeat;
79 	}
80 public:
81 	featid operator*();
82 	FeatureIterator operator++();
83 	FeatureIterator operator+=(int n);
84 	bool operator==(FeatureIterator &);
85 	bool operator!=(FeatureIterator &);
86 	int operator-(FeatureIterator &);
87 
88 protected:
89 	Font * m_pfont;
90 	size_t m_ifeat;	// feature being pointed at
91 	size_t m_cfeat;	// number of features for this font
92 
93 	FeatureSettingIterator BeginSetting();
94 	FeatureSettingIterator EndSetting();
95 };
96 
97 /*----------------------------------------------------------------------------------------------
98 	Iterator to provide access to the defined values for a single font feature.
99 ----------------------------------------------------------------------------------------------*/
100 class FeatureSettingIterator
101 {
102 	friend class Font;
103 	friend class FeatureIterator;
104 
105 public:
FeatureSettingIterator()106 	FeatureSettingIterator() // needed for creating std::pair of these, I think
107 	{
108 		m_ifset = 0;
109 		m_cfset = 0;
110 	}
111 protected:
FeatureSettingIterator(FeatureIterator fit,int ifset,size_t cfset)112 	FeatureSettingIterator(FeatureIterator fit, int ifset, size_t cfset)
113 	{
114 		m_fit = fit;
115 		m_ifset = ifset;
116 		m_cfset = cfset;
117 	}
118 public:
119 	int operator*();
120 	FeatureSettingIterator operator++();
121 	FeatureSettingIterator operator +=(int n);
122 	bool operator==(FeatureSettingIterator &);
123 	bool operator!=(FeatureSettingIterator &);
124 	int operator-(FeatureSettingIterator);
125 
126 protected:
127 	FeatureIterator m_fit;
128 	size_t m_ifset;	// which setting being pointed at
129 	size_t m_cfset;	// number of settings for this feature
130 };
131 
132 /*----------------------------------------------------------------------------------------------
133 	Iterator to provide access to the languages available for the feature label strings.
134 ----------------------------------------------------------------------------------------------*/
135 class FeatLabelLangIterator
136 {
137 	friend class Font;
138 
139 public:
FeatLabelLangIterator()140 	FeatLabelLangIterator() // needed for creating std::pair of these, and for default FSI
141 	{
142 		m_pfont = NULL;
143 		m_ilang = 0;
144 		m_clang = 0;
145 	}
146 
147 protected:
FeatLabelLangIterator(Font * pfont,int ilang,size_t clang)148 	FeatLabelLangIterator(Font * pfont, int ilang, size_t clang)
149 	{
150 		m_pfont = pfont;
151 		m_ilang = ilang;
152 		m_clang = clang;
153 	}
154 public:
155 	data16 operator*();
156 	FeatLabelLangIterator operator++();
157 	FeatLabelLangIterator operator+=(int n);
158 	bool operator==(FeatLabelLangIterator &);
159 	bool operator!=(FeatLabelLangIterator &);
160 	int operator-(FeatLabelLangIterator &);
161 
162 protected:
163 	Font * m_pfont;
164 	size_t m_ilang;	// language being pointed at
165 	size_t m_clang;	// number of languages for this font
166 };
167 
168 /*----------------------------------------------------------------------------------------------
169 	Iterator to provide access to a font's defined languages--ie, those that have feature
170 	settings associated with them.
171 ----------------------------------------------------------------------------------------------*/
172 class LanguageIterator
173 {
174 	friend class Font;
175 
176 public:
LanguageIterator()177 	LanguageIterator() // needed for creating std::pair of these, and for default FSI
178 	{
179 		m_pfont = NULL;
180 		m_ilang = 0;
181 		m_clang = 0;
182 	}
183 
184 protected:
LanguageIterator(Font * pfont,int ilang,size_t clang)185 	LanguageIterator(Font * pfont, int ilang, size_t clang)
186 	{
187 		m_pfont = pfont;
188 		m_ilang = ilang;
189 		m_clang = clang;
190 	}
191 public:
192 	isocode operator*();	// returns a 4-char array
193 	LanguageIterator operator++();
194 	LanguageIterator operator+=(int n);
195 	bool operator==(LanguageIterator &);
196 	bool operator!=(LanguageIterator &);
197 	int operator-(LanguageIterator &);
198 
199 protected:
200 	Font * m_pfont;
201 	size_t m_ilang;	// language being pointed at
202 	size_t m_clang;	// number of languages for this font
203 };
204 
205 /*----------------------------------------------------------------------------------------------
206 	Abstract superclass for Graphite fonts. A font represents a face name, size, and bold and
207 	italic styles.
208 ----------------------------------------------------------------------------------------------*/
209 class Font {
210 
211 	friend class FeatureIterator;
212 	friend class FeatureSettingIterator;
213 	friend class FeatLabelLangIterator;
214 	friend class LanguageIterator;
215 	friend class FontMemoryUsage;
216 
217 public:
218 	virtual ~Font();
219 
220 	/**
221 	* Returns a copy of the recipient. Specifically needed to store the
222 	* Font in a segment.
223 	* @internal
224 	* @return pointer to copy
225 	*/
226 	virtual Font * copyThis() = 0;
227 
228 	/**
229 	* Return wether the font is bold.
230 	* @return true when bold
231 	*/
232 	virtual bool bold() = 0;
233 	/**
234 	* Return wether the font is italic.
235 	* @return true when italic
236 	*/
237 	virtual bool italic() = 0;
238 
239 	/**
240 	* Returns the slope of the italic (if the font is italic)
241 	* @return ratio of slope from the vertical
242 	*/
fakeItalicRatio()243 	virtual float fakeItalicRatio()	{ return 0; } // no support for fake italic
244 
245 	/**
246 	* Returns the font ascent.
247 	* Value is the same as that returned by getFontMetrics()
248 	* @return the font ascent in device co-ordinates
249 	*/
250 	virtual float ascent() = 0;
251 
252 	/**
253 	* Returns the font descent.
254 	* Value is the same as that returned by getFontMetrics()
255 	* @return the font descent in device co-ordinates
256 	*/
257 	virtual float descent() = 0;
258 
259 	/**
260 	* Returns the total height of the font.
261 	* @return font height in device co-ordinates
262 	*/
263 	virtual float height() = 0;
264 
265 	/**
266 	* Returns the x and y resolution of the device co-ordinate space.
267 	*/
268 	virtual unsigned int getDPIx() = 0;
269 	virtual unsigned int getDPIy() = 0;
270 
271 	/**
272 	* Returns a pointer to the start of a table in the font.
273 	* If the Font class cannot easily determine the length of the table,
274 	* it may set 0 as the length (while returning a non-NULL pointer to
275 	* the table). This means that certain kinds of error checking cannot
276 	* be done by the Graphite engine.
277 	* Throws an exception if there is some other error in reading the
278 	* table, or if the table asked for is not in the font.
279 	* @param tableID the TTF ID of the table as a 32-bit long in native machine byte order
280 	* @param pcbSize pointer to a size_t to hold the table size.
281 	* @return address of the buffer containing the table or 0
282 	*/
283 	virtual const void * getTable(fontTableId32 tableID, size_t * pcbSize) = 0;
284 	/**
285 	* Fetches the basic metrics of the font in device co-ordinates
286 	* (normaly pixels).
287 	* @param pAscent pointer to hold font ascent.
288 	* @param pDescent pointer to hold font descent.
289 	* @param pEmSquare pointer to hold font EM square.
290 	*/
291 	virtual void getFontMetrics(float * pAscent, float * pDescent = NULL,
292 		float * pEmSquare = NULL) = 0;
293 	/**
294 	* Converts the point number of a glyph’s on-curve point to a pair of
295 	* x/y coordinates in pixels. The default implementation will read the
296 	* curve information directly from the font and perform a simple
297 	* transformation to pixels. Some subclasses (e.g., WinFont) will use
298 	* a system-level API call to return hinted metrics.
299 	* Note that the coordinates returned are floating point values in the
300 	* device co-ordinate space (normaly pixels).
301 	* @param gid glyph id
302 	* @param pointNum within glyph
303 	* @param xyReturn reference to a Point object to hold the x,y result
304 	*/
305 	virtual void getGlyphPoint(gid16 glyphID, unsigned int pointNum, gr::Point & pointReturn);
306 
307 	/**
308 	* Returns the metrics of a glyph in the font. The default
309 	* implementation will read the information directly from the font and
310 	* perform a simple transformation to pixels. Some subclasses (e.g.,
311 	* WinFont) will use a system-level API call to return hinted metrics.
312 	* Note that the coordinates returned are floating point values in the
313 	* device co-ordinate space (normaly pixels).
314 	* @param glyphID
315 	* @param boundingBox reference to gr::Rect to hold bounding box of glyph
316 	* @param advances refererence to gr::Point to hold the horizontal / vertical advances
317 	*/
318 	virtual void getGlyphMetrics(gid16 glyphID, gr::Rect & boundingBox, gr::Point & advances);
319 
320 	static void SetFlushMode(int);
321 	static int GetFlushMode();
322 
323 	// obsolete:
324 	//virtual FontErrorCode isValidForGraphite(int * pnVersion = NULL, int * pnSubVersion = NULL) = 0;
325 
326 	// Features:
327 	std::pair<FeatureIterator, FeatureIterator> getFeatures();
328 	FeatureIterator featureWithID(featid id);
329 	bool getFeatureLabel(FeatureIterator, lgid language, utf16 * label);
330 	FeatureSettingIterator getDefaultFeatureValue(FeatureIterator);
331 	std::pair<FeatureSettingIterator, FeatureSettingIterator> getFeatureSettings(FeatureIterator);
332 	bool getFeatureSettingLabel(FeatureSettingIterator, lgid language, utf16 * label);
333 
334 	std::pair<FeatLabelLangIterator, FeatLabelLangIterator> getFeatureLabelLanguages();
335 
336 	// Languages:
337 	std::pair<LanguageIterator, LanguageIterator> getSupportedLanguages();
338 
339 	// Script directions, Verticle, LTR, RTL etc see enum ScriptDirCode
340 	// the posibilities.  This returns a bit set where more than one scripts may
341 	// be set, it's up to the app to decide which is the prefered direction to use.
342 	ScriptDirCode getSupportedScriptDirections() const throw();
343 
344 	// Debugging:
345 	//static bool DbgCheckFontCache();
346 
347 	static FontMemoryUsage calculateMemoryUsage();
348 	///FontMemoryUsage calculateMemoryUsage(bool fBold = false, bool fItalic = false);
349 
350 public:
351 	// For use in segment creation:
352 	void RenderLineFillSegment(Segment * pseg, ITextSource * pts, LayoutEnvironment & layout,
353 		toffset ichStart, toffset ichStop, float xsMaxWidth, bool fBacktracking);
354 	void RenderRangeSegment(Segment * pseg, ITextSource * pts, LayoutEnvironment & layout,
355 		toffset ichStart, toffset ichEnd);
356 	void RenderJustifiedSegment(Segment * pseg, ITextSource * pts, LayoutEnvironment & layout,
357 		toffset ichStart, toffset ichEnd, float xsCurrentWidth, float xsDesiredWidth);
358 
359 protected:
360 	Font();
361 	Font(const Font &);
362 
363 	FontFace & fontFace(bool fDumbFallback = false);
364 
365 	// Feature access:
366 	FeatureIterator BeginFeature();
367 	FeatureIterator EndFeature();
368 
369 	size_t NumberOfFeatures();
370 	featid FeatureID(size_t ifeat);
371 	size_t FeatureWithID(featid id);
372 	bool GetFeatureLabel(size_t ifeat, lgid language, utf16 * label);
373 	int GetFeatureDefault(size_t ifeat);
374 	size_t NumberOfSettings(size_t ifeat);
375 	int GetFeatureSettingValue(size_t ifeat, size_t ifset);
376 	bool GetFeatureSettingLabel(size_t ifeat, size_t ifset, lgid language, utf16 * label);
377 
378 	// Feature-label languages:
379 	FeatLabelLangIterator BeginFeatLang();
380 	FeatLabelLangIterator EndFeatLang();
381 
382 	size_t NumberOfFeatLangs();
383 	short FeatLabelLang(size_t ilang);
384 
385 	// Language access:
386 	LanguageIterator BeginLanguage();
387 	LanguageIterator EndLanguage();
388 
389 	size_t NumberOfLanguages();
390 	isocode LanguageCode(size_t ilang);
391 
392 	virtual void UniqueCacheInfo(std::wstring & stuFace, bool & fBold, bool & fItalic);
393 
394 private:
395 	FontFace * m_pfface;	// set up with Graphite tables
396 
397 	// Cache of common tables:
398 	const void * m_pHead;
399 	const void * m_pHmtx;
400 	const void * m_pLoca;
401 	const void * m_pGlyf;
402 	size_t m_cbHmtxSize;
403 	size_t m_cbLocaSize;
404 	bool m_fTablesCached;
405 
406 	void initialiseFontFace(bool fDumbFallback);
407 	void EnsureTablesCached();
408 };
409 
Font()410 inline Font::Font() : m_pfface(0), m_fTablesCached(false)
411 { }
412 
413 } // namespace gr
414 
415 
416 #endif // !FONT_INCLUDED
417