Commit 789b1cf3bb5ef4b65a3c19cc3daa859325013739

henry 2001-11-04T04:33:30

Changes to allow glyphs to be loaded on the fly... MakeGlyphList is no longer pure virtual. New function MakeGlyph is pure virtual Open now has a flag for pre-cache GlyphContainer builds a list of null pointers advance and render functions check if glyph has been loaded and loads it if it has'nt

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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
diff --git a/include/FTFont.h b/include/FTFont.h
index 47406cf..7e5c471 100755
--- a/include/FTFont.h
+++ b/include/FTFont.h
@@ -9,23 +9,22 @@
 #include FT_FREETYPE_H
 
 #include "FTFace.h"
-#include "FTGL.h"
 
 
 class FTGlyphContainer;
+class FTGlyph;
 
 using namespace std;
 
 
-
 /**
  * FTFont is the public interface for the FTGL library.
  *
  * Specific font classes are derived from this class. It uses the helper
  * classes FTFace and FTSize to access the Freetype library. This class
  * is abstract and deriving classes must implement the protected
- * <code>MakeGlyphList</code> function to build a glyphList with the
- * appropriate glyph type.
+ * <code>MakeGlyph</code> function to create glyphs of the
+ * appropriate type.
  *
  * @see		FTFace
  * @see		FTSize
@@ -49,10 +48,14 @@ class FTGL_EXPORT FTFont
 		 * Opens and reads a font file.
 		 *
 		 * @param fontname	font file name.
+		 * @param preCache	A flag to indicate whether or not to build
+		 * 					a complete set of glyphs at startup
+		 *					(<code>true</code>) or as prequired
+		 *					(<code>false</code>). Defaults to true.
 		 * @return			<code>true</code> if file has opened
 		 *					successfully.
 		 */
-		virtual bool Open( const char* fontname );
+		virtual bool Open( const char* fontname, bool preCache = true);
 		
 		/**
 		 * Disposes of the font
@@ -66,7 +69,7 @@ class FTGL_EXPORT FTFont
 		 * @param res		the resolution of the target device.
 		 * @return			<code>true</code> if size was set correctly
 		 */
-		virtual bool FaceSize( const unsigned int size, const unsigned int res = 72 );
+		virtual bool FaceSize( const unsigned int size, const unsigned int res = 72);
 		
 		/**
 		 * Sets the character map for the face.
@@ -131,12 +134,25 @@ class FTGL_EXPORT FTFont
 
 	protected:
 		/**
-		 * Constructs the internal glyph cache.
+		 * Construct a glyph of the correct type.
+		 *
+		 * Clients must overide the function and return their specialised
+		 * FTGlyph.
+		 *
+		 * @param g	The glyph index NOT the char code.
+		 * @return	An FT****Glyph or <code>null</code> on failure.
+		 */
+		virtual FTGlyph* MakeGlyph( unsigned int g) = 0;
+		
+		/**
+		 * Construct the internal glyph cache.
 		 *
 		 * This a list of glyphs processed for openGL rendering NOT
-		 * freetype glyphs
+		 * freetype glyphs.
+		 *
+		 * @return	<code>true</code> on success.
 		 */
-		virtual bool MakeGlyphList() = 0;
+		virtual bool MakeGlyphList();
 		
 		/**
 		 * Current face object
@@ -164,6 +180,11 @@ class FTGL_EXPORT FTFont
 		unsigned int numGlyphs;
 		
 		/**
+		 * Have glyphs been pre-cached
+		 */
+		bool preCache;
+		
+		/**
 		 * Current pen or cursor position;
 		 */
 		FT_Vector pen;
diff --git a/include/FTGLBitmapFont.h b/include/FTGLBitmapFont.h
index 6193de9..3a3b2b7 100755
--- a/include/FTGLBitmapFont.h
+++ b/include/FTGLBitmapFont.h
@@ -6,7 +6,7 @@
 
 #include "FTFont.h"
 
-class FTBitmapGlyph;
+class FTGlyph;
 
 /**
  * FTGLBitmapFont is a specialisation of the FTFont class for handling
@@ -45,12 +45,12 @@ class FTGL_EXPORT FTGLBitmapFont : public FTFont
 		
 	private:
 		/**
-		 * Constructs the internal glyph cache.
+		 * Construct a FTBitmapGlyph.
 		 *
-		 * This a list of glyphs processed for openGL rendering NOT
-		 * freetype glyphs
+		 * @param g	The glyph index NOT the char code.
+		 * @return	An FTBitmapGlyph or <code>null</code> on failure.
 		 */
-		bool MakeGlyphList();
-		
+		virtual FTGlyph* MakeGlyph( unsigned int g);
+				
 };
 #endif	//	__FTGLBitmapFont__
diff --git a/include/FTGLOutlineFont.h b/include/FTGLOutlineFont.h
index 2e8378d..698fe34 100755
--- a/include/FTGLOutlineFont.h
+++ b/include/FTGLOutlineFont.h
@@ -5,8 +5,7 @@
 
 #include "FTFont.h"
 
-
-class FTOutlineGlyph;
+class FTGlyph;
 
 /**
  * FTGLOutlineFont is a specialisation of the FTFont class for handling
@@ -45,12 +44,12 @@ class FTGL_EXPORT FTGLOutlineFont : public FTFont
 		
 	private:
 		/**
-		 * Constructs the internal glyph cache.
+		 * Construct a FTOutlineGlyph.
 		 *
-		 * This a list of glyphs processed for openGL rendering NOT
-		 * freetype glyphs
+		 * @param g	The glyph index NOT the char code.
+		 * @return	An FTOutlineGlyph or <code>null</code> on failure.
 		 */
-		bool MakeGlyphList();
+		virtual FTGlyph* MakeGlyph( unsigned int g);
 		
 };
 #endif // __FTGLOutlineFont__
diff --git a/include/FTGLPixmapFont.h b/include/FTGLPixmapFont.h
index 8b8e150..9f7fe7b 100755
--- a/include/FTGLPixmapFont.h
+++ b/include/FTGLPixmapFont.h
@@ -6,7 +6,7 @@
 
 #include "FTFont.h"
 
-class FTPixmapGlyph;
+class FTGlyph;
 
 /**
  * FTGLPixmapFont is a specialisation of the FTFont class for handling
@@ -44,12 +44,12 @@ class FTGL_EXPORT FTGLPixmapFont : public FTFont
 
 	private:
 		/**
-		 * Constructs the internal glyph cache.
+		 * Construct a FTPixmapGlyph.
 		 *
-		 * This a list of glyphs processed for openGL rendering NOT
-		 * freetype glyphs
+		 * @param g	The glyph index NOT the char code.
+		 * @return	An FTPixmapGlyph or <code>null</code> on failure.
 		 */
-		bool MakeGlyphList();
+		virtual FTGlyph* MakeGlyph( unsigned int g);
 		
 };
 
diff --git a/include/FTGLPolygonFont.h b/include/FTGLPolygonFont.h
index 14d3bf0..fb2d436 100755
--- a/include/FTGLPolygonFont.h
+++ b/include/FTGLPolygonFont.h
@@ -5,8 +5,7 @@
 
 #include	"FTFont.h"
 
-
-class FTPolyGlyph;
+class FTGlyph;
 
 /**
  * FTGLPolygonFont is a specialisation of the FTFont class for handling
@@ -29,12 +28,12 @@ class FTGL_EXPORT FTGLPolygonFont : public FTFont
 		
 	private:
 		/**
-		 * Constructs the internal glyph cache.
+		 * Construct a FTPolyGlyph.
 		 *
-		 * This a list of glyphs processed for openGL rendering NOT
-		 * freetype glyphs
+		 * @param g	The glyph index NOT the char code.
+		 * @return	An FTPolyGlyph or <code>null</code> on failure.
 		 */
-		bool MakeGlyphList();
+		virtual FTGlyph* MakeGlyph( unsigned int g);
 		
 };
 
diff --git a/include/FTGLTextureFont.h b/include/FTGLTextureFont.h
index 31f8def..4edf316 100755
--- a/include/FTGLTextureFont.h
+++ b/include/FTGLTextureFont.h
@@ -51,6 +51,8 @@ class  FTGL_EXPORT FTGLTextureFont : public FTFont
 
 		
 	private:
+		virtual FTGlyph* MakeGlyph( unsigned int g){ return NULL;}
+				
 		/**
 		 * Constructs the internal glyph cache.
 		 *
diff --git a/include/FTGlyphContainer.h b/include/FTGlyphContainer.h
index bb2cb41..1f51ae2 100755
--- a/include/FTGlyphContainer.h
+++ b/include/FTGlyphContainer.h
@@ -9,7 +9,6 @@
 #include FT_FREETYPE_H
 #include FT_GLYPH_H
 
-//#include "FTGL.h"
 class FTFace;
 class FTGlyph;
 
@@ -44,7 +43,16 @@ class FTGL_EXPORT FTGlyphContainer
 		 * @param glyph		
 		 * @return			<code>true</code>
 		 */
-		bool Add( FTGlyph* glyph);
+		bool Add( FTGlyph* tempGlyph, unsigned int g);
+
+		/**
+		 * Get a glyph from the glyph list
+		 *
+		 * @param c	The char code of the glyph NOT the glyph index		
+		 * @return	An FTGlyph or <code>null</code> is it hasn't been
+		 * loaded.
+		 */
+		FTGlyph* Glyph( unsigned int c) const;
 
 		/**
 		* Returns the kerned advance width for a glyph.
@@ -101,9 +109,6 @@ class FTGL_EXPORT FTGlyphContainer
 		 * A structure to hold the glyphs
 		 */
 		vector<FTGlyph*> glyphs;
-//		typedef pair<int, FTGlyph*> CHARREF; // glyphIndex, glyph
-//		vector<CHARREF> glyphs;
-//		map< int, FTGlyph*> CHARREF; // charCode, glyph
 
 		/**
 		 * Current error code. Zero means no error.
diff --git a/src/FTFont.cpp b/src/FTFont.cpp
index fb93213..630f129 100755
--- a/src/FTFont.cpp
+++ b/src/FTFont.cpp
@@ -7,6 +7,8 @@
 FTFont::FTFont()
 :	numFaces(0),
 	glyphList(0),
+	numGlyphs(0),
+	preCache(true),
 	err(0)
 {
 	pen.x = 0;
@@ -20,8 +22,10 @@ FTFont::~FTFont()
 }
 
 
-bool FTFont::Open( const char* fontname )
+bool FTFont::Open( const char* fontname, bool p)
 {
+	preCache = p;
+	
 	if( face.Open( fontname))
 	{
 		FT_Face* ftFace = face.Face();		
@@ -50,7 +54,7 @@ bool FTFont::FaceSize( const unsigned int size, const unsigned int res )
 	if( glyphList)
 		delete glyphList;
 	
-	glyphList = new FTGlyphContainer( &face, numGlyphs);
+	glyphList = new FTGlyphContainer( &face, numGlyphs, preCache);
 	
 	if( MakeGlyphList())
 	{
@@ -63,6 +67,24 @@ bool FTFont::FaceSize( const unsigned int size, const unsigned int res )
 }
 
 
+bool FTFont::MakeGlyphList()
+{
+	for( unsigned int c = 0; c < numGlyphs; ++c)
+	{
+		if( preCache)
+		{
+			glyphList->Add( MakeGlyph( c), c);
+		}
+		else
+		{
+			glyphList->Add( NULL, c);
+		}
+	}
+	
+	return !err;
+}
+
+
 bool FTFont::CharMap( FT_Encoding encoding)
 {
 	err = face.CharMap( encoding);
@@ -89,6 +111,12 @@ float FTFont::Advance( const wchar_t* string)
 
 	while( *c)
 	{
+		if( !glyphList->Glyph( static_cast<unsigned int>(*c)))
+		{
+			unsigned int g = face.CharIndex( static_cast<unsigned int>(*c));
+			glyphList->Add( MakeGlyph( g), g);
+		}
+
 		width += glyphList->Advance( *c, *(c + 1));	
 		++c;
 	}
@@ -104,7 +132,13 @@ float FTFont::Advance( const char* string)
 
 	while( *c)
 	{
-		width += glyphList->Advance( *c, *(c + 1));	
+		if( !glyphList->Glyph( static_cast<unsigned int>(*c)))
+		{
+			unsigned int g = face.CharIndex( static_cast<unsigned int>(*c));
+			glyphList->Add( MakeGlyph( g), g);
+		}
+
+		width += glyphList->Advance( *c, *(c + 1));
 		++c;
 	}
 
@@ -120,11 +154,16 @@ void FTFont::render( const char* string )
 
 	while( *c)
 	{
+		if( !glyphList->Glyph( static_cast<unsigned int>(*c)))
+		{
+			unsigned int g = face.CharIndex( static_cast<unsigned int>(*c));
+			glyphList->Add( MakeGlyph( g), g);
+		}
+
 		kernAdvance = glyphList->render( *c, *(c + 1), pen);
 		
 		pen.x += kernAdvance.x;
 		pen.y += kernAdvance.y;
-		
 		++c;
 	}
 }
@@ -138,11 +177,16 @@ void FTFont::render( const wchar_t* string )
 
 	while( *c)
 	{
+		if( !glyphList->Glyph( static_cast<unsigned int>(*c)))
+		{
+			unsigned int g = face.CharIndex( static_cast<unsigned int>(*c));
+			glyphList->Add( MakeGlyph( g), g);
+		}
+
 		kernAdvance = glyphList->render( *c, *(c + 1), pen);
 		
 		pen.x += kernAdvance.x;
 		pen.y += kernAdvance.y;
-		
 		++c;
 	}
 }
diff --git a/src/FTGLBitmapFont.cpp b/src/FTGLBitmapFont.cpp
index 4136e0e..6ad3d12 100755
--- a/src/FTGLBitmapFont.cpp
+++ b/src/FTGLBitmapFont.cpp
@@ -11,27 +11,18 @@ FTGLBitmapFont::~FTGLBitmapFont()
 {}
 
 
-// OPSignature: bool FTGlyphContainer:MakeGlyphList() 
-bool FTGLBitmapFont::MakeGlyphList()
+FTGlyph* FTGLBitmapFont::MakeGlyph( unsigned int g)
 {
-//	if( preCache)
-	for( unsigned int c = 0; c < numGlyphs; ++c)
+	FT_Glyph* ftGlyph = face.Glyph( g, FT_LOAD_DEFAULT);
+
+	if( ftGlyph)
 	{
-		FT_Glyph* ftGlyph = face.Glyph( c, FT_LOAD_DEFAULT);
-//		FT_HAS_VERTICAL(face)
-
-		if( ftGlyph)
-		{
-			FTBitmapGlyph* tempGlyph = new FTBitmapGlyph( *ftGlyph);
-			glyphList->Add( tempGlyph);
-		}
-		else
-		{
-			err = face.Error();
-		}
+		FTBitmapGlyph* tempGlyph = new FTBitmapGlyph( *ftGlyph);
+		return tempGlyph;
 	}
-	
-	return !err;
+
+	err = face.Error();
+	return NULL;
 }
 
 
diff --git a/src/FTGLOutlineFont.cpp b/src/FTGLOutlineFont.cpp
index fcb404a..3b5aada 100755
--- a/src/FTGLOutlineFont.cpp
+++ b/src/FTGLOutlineFont.cpp
@@ -12,24 +12,18 @@ FTGLOutlineFont::~FTGLOutlineFont()
 {}
 
 
-bool FTGLOutlineFont::MakeGlyphList()
+FTGlyph* FTGLOutlineFont::MakeGlyph( unsigned int g)
 {
-	for( unsigned int n = 0; n < numGlyphs; ++n)
+	FT_Glyph* ftGlyph = face.Glyph( g, FT_LOAD_DEFAULT);
+
+	if( ftGlyph)
 	{
-		FT_Glyph* ftGlyph = face.Glyph( n, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
-		
-		if( ftGlyph)
-		{
-			FTOutlineGlyph* tempGlyph = new FTOutlineGlyph( *ftGlyph);
-			glyphList->Add( tempGlyph);
-		}
-		else
-		{
-			err = face.Error();
-		}
+		FTOutlineGlyph* tempGlyph = new FTOutlineGlyph( *ftGlyph);
+		return tempGlyph;
 	}
-	
-	return !err;
+
+	err = face.Error();
+	return NULL;
 }
 
 
diff --git a/src/FTGLPixmapFont.cpp b/src/FTGLPixmapFont.cpp
index 7ad2cfe..a5a3542 100755
--- a/src/FTGLPixmapFont.cpp
+++ b/src/FTGLPixmapFont.cpp
@@ -11,27 +11,18 @@ FTGLPixmapFont::~FTGLPixmapFont()
 {}
 
 
-// OPSignature: bool FTGlyphContainer:MakeGlyphList() 
-bool FTGLPixmapFont::MakeGlyphList()
+FTGlyph* FTGLPixmapFont::MakeGlyph( unsigned int g)
 {
-//	if( preCache)
-	for( unsigned int c = 0; c < numGlyphs; ++c)
+	FT_Glyph* ftGlyph = face.Glyph( g, FT_LOAD_DEFAULT);
+
+	if( ftGlyph)
 	{
-		FT_Glyph* ftGlyph = face.Glyph( c, FT_LOAD_DEFAULT);
-//		FT_HAS_VERTICAL(face)
-	
-		if( ftGlyph)
-		{
-			FTPixmapGlyph* tempGlyph = new FTPixmapGlyph( *ftGlyph);
-			glyphList->Add( tempGlyph);
-		}
-		else
-		{
-			err = face.Error();
-		}
+		FTPixmapGlyph* tempGlyph = new FTPixmapGlyph( *ftGlyph);
+		return tempGlyph;
 	}
-	
-	return !err;
+
+	err = face.Error();
+	return NULL;
 }
 
 
diff --git a/src/FTGLPolygonFont.cpp b/src/FTGLPolygonFont.cpp
index 57a2033..9023aa8 100755
--- a/src/FTGLPolygonFont.cpp
+++ b/src/FTGLPolygonFont.cpp
@@ -13,22 +13,18 @@ FTGLPolygonFont::~FTGLPolygonFont()
 {}
 
 
-bool FTGLPolygonFont::MakeGlyphList()
+FTGlyph* FTGLPolygonFont::MakeGlyph( unsigned int g)
 {
-	for( unsigned int n = 0; n < numGlyphs; ++n)
+	FT_Glyph* ftGlyph = face.Glyph( g, FT_LOAD_DEFAULT);
+
+	if( ftGlyph)
 	{
-		FT_Glyph* ftGlyph = face.Glyph( n, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
-		
-		if( ftGlyph)
-		{
-			FTPolyGlyph* tempGlyph = new FTPolyGlyph( *ftGlyph);
-			glyphList->Add( tempGlyph);
-		}
-		else
-		{
-			err = face.Error();
-		}
+		FTPolyGlyph* tempGlyph = new FTPolyGlyph( *ftGlyph);
+		return tempGlyph;
 	}
-	
-	return !err;
+
+	err = face.Error();
+	return NULL;
 }
+
+
diff --git a/src/FTGLTextureFont.cpp b/src/FTGLTextureFont.cpp
index cde8eb1..dd10b4e 100755
--- a/src/FTGLTextureFont.cpp
+++ b/src/FTGLTextureFont.cpp
@@ -117,7 +117,7 @@ unsigned int FTGLTextureFont::FillGlyphs( unsigned int glyphStart, GLuint id, GL
 			currTextU = (float)currentTextX / (float)width;
 			
 			FTTextureGlyph* tempGlyph = new FTTextureGlyph( *ftGlyph, id, data, width, height, currTextU, currTextV);
-			glyphList->Add( tempGlyph);
+			glyphList->Add( tempGlyph, n);
 
 			currentTextX += glyphWidth;
 			if( currentTextX > ( width - glyphWidth))
diff --git a/src/FTGlyphContainer.cpp b/src/FTGlyphContainer.cpp
index 53a95f7..c643b6f 100755
--- a/src/FTGlyphContainer.cpp
+++ b/src/FTGlyphContainer.cpp
@@ -10,6 +10,8 @@ FTGlyphContainer::FTGlyphContainer( FTFace* f, unsigned int g, bool p)
 	err( 0)
 {
 	glyphs.reserve( g);
+	for( unsigned int i = 0; i < g; ++i)
+		glyphs.push_back( NULL);
 }
 
 
@@ -26,14 +28,22 @@ FTGlyphContainer::~FTGlyphContainer()
 }
 
 
-bool FTGlyphContainer::Add( FTGlyph* tempGlyph)
+bool FTGlyphContainer::Add( FTGlyph* tempGlyph, unsigned int g)
 {
 	// At the moment we are using a vector. Vectors don't return bool.
-	glyphs.push_back( tempGlyph);
+//	unsigned int glyphIndex = face->CharIndex( g);
+
+	glyphs[g] = tempGlyph;
 	return true;
 }
 
 
+FTGlyph* FTGlyphContainer::Glyph( unsigned int c) const
+{
+	unsigned int g = face->CharIndex( c);
+	return glyphs[g];
+}
+
 float FTGlyphContainer::Advance( unsigned int index, unsigned int next)
 {
 	unsigned int left = face->CharIndex( index);