Commit 56289ce67ffecfbd459cadec2a9d0f095ddfda5e

henry 2003-06-08T01:09:17

Refactored FTGlyphContainer & FTCharmap. They now store FTGlyphs sequentially rather than by glyph index.

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
diff --git a/include/FTCharmap.h b/include/FTCharmap.h
index 1b1f568..47896dc 100644
--- a/include/FTCharmap.h
+++ b/include/FTCharmap.h
@@ -71,14 +71,33 @@ class FTGL_EXPORT FTCharmap
         bool CharMap( FT_Encoding encoding);
 
         /**
-         * Get the glyph index of the input character.
+         * Get the FTGlyphContainer index of the input character.
          *
-         * @param index The character code of the requested glyph in the
-         *              current encoding eg apple roman.
+         * @param characterCode The character code of the requested glyph in
+         *                      the current encoding eg apple roman.
+         * @return      The FTGlyphContainer index for the character or zero
+         *              if it wasn't found
+         */
+        unsigned int CharIndex( const unsigned int characterCode);
+
+        /**
+         * Set the FTGlyphContainer index of the character code.
+         *
+         * @param characterCode  The character code of the requested glyph in
+         *                       the current encoding eg apple roman.
+         * @param containerIndex The index into the FTGlyphContainer of the
+         *                       character code.
+         */
+        void InsertIndex( const unsigned int characterCode, const unsigned int containerIndex);
+
+        /**
+         * Get the font glyph index of the input character.
+         *
+         * @param characterCode The character code of the requested glyph in
+         *                      the current encoding eg apple roman.
          * @return      The glyph index for the character.
          */
-        unsigned int CharIndex( unsigned int characterCode);
-        unsigned int GlyphIndex( unsigned int characterCode, unsigned int glyphIndex);
+        unsigned int GlyphIndex( const unsigned int characterCode);
 
         /**
          * Queries for errors.
diff --git a/include/FTGlyphContainer.h b/include/FTGlyphContainer.h
index fa6df1f..386bb14 100755
--- a/include/FTGlyphContainer.h
+++ b/include/FTGlyphContainer.h
@@ -36,7 +36,7 @@ class FTGL_EXPORT FTGlyphContainer
         ~FTGlyphContainer();
 
         /**
-            * Sets the character map for the face.
+         * Sets the character map for the face.
          *
          * @param encoding      the Freetype encoding symbol. See above.
          * @return              <code>true</code> if charmap was valid
@@ -45,13 +45,13 @@ class FTGL_EXPORT FTGlyphContainer
         bool CharMap( FT_Encoding encoding);
 
         /**
-         *  Get the glyph index of the input character.
+         * Get the glyph index of the input character.
          *
          * @param characterCode The character code of the requested glyph in the
          *                      current encoding eg apple roman.
          * @return      The glyph index for the character.
          */
-        unsigned int CharIndex( unsigned int characterCode ) const;
+        unsigned int GlyphIndex( const unsigned int characterCode ) const;
         
         /**
          * Adds a glyph to this glyph list.
@@ -83,7 +83,7 @@ class FTGL_EXPORT FTGlyphContainer
         * @param nextCharacterCode the next glyph in a string
         * @return                  advance width
         */
-        float Advance( unsigned int characterCode, unsigned int nextCharacterCode);
+        float Advance( const unsigned int characterCode, const unsigned int nextCharacterCode);
         
         /**
          * Renders a character
@@ -92,7 +92,7 @@ class FTGL_EXPORT FTGlyphContainer
          * @param penPosition        the position to Render the glyph
          * @return                   The distance to advance the pen position after Rendering
          */
-        FTPoint Render( unsigned int characterCode, unsigned int nextCharacterCode, FTPoint penPosition);
+        FTPoint Render( const unsigned int characterCode, const unsigned int nextCharacterCode, FTPoint penPosition);
         
         /**
          * Queries the Font for errors.
@@ -103,11 +103,6 @@ class FTGL_EXPORT FTGlyphContainer
 
     private:
         /**
-         * How many glyphs are reserved in this container
-         */
-        unsigned int numberOfGlyphs;
-
-        /**
          * The FTGL face
          */
         FTFace* face;
@@ -126,7 +121,6 @@ class FTGL_EXPORT FTGlyphContainer
          * Current error code. Zero means no error.
          */
         FT_Error err;
-        
 };
 
 
diff --git a/src/FTCharmap.cpp b/src/FTCharmap.cpp
index 0530933..1875d6f 100644
--- a/src/FTCharmap.cpp
+++ b/src/FTCharmap.cpp
@@ -47,15 +47,25 @@ bool FTCharmap::CharMap( FT_Encoding encoding)
 unsigned int FTCharmap::CharIndex( unsigned int characterCode )
 {
     const CharacterMap::GlyphIndex *result = charMap.find( characterCode);
-    
     if( !result)
     {
-        unsigned int glyphIndex = FT_Get_Char_Index( ftFace, characterCode);
-        charMap.insert( characterCode, glyphIndex);
-        return glyphIndex;
+        return 0;
     }
     else
     {
         return *result;
     }
 }
+
+
+void FTCharmap::InsertIndex( const unsigned int characterCode, const unsigned int containerIndex)
+{
+    charMap.insert( characterCode, containerIndex);
+}
+
+
+unsigned int FTCharmap::GlyphIndex( const unsigned int characterCode)
+{
+    unsigned int glyphIndex = FT_Get_Char_Index( ftFace, characterCode);
+    return glyphIndex;
+}
diff --git a/src/FTFont.cpp b/src/FTFont.cpp
index 438105a..420d581 100755
--- a/src/FTFont.cpp
+++ b/src/FTFont.cpp
@@ -9,6 +9,10 @@ FTFont::FTFont( const char* fontname)
     glyphList(0)
 {
     err = face.Error();
+    if( err == 0)
+    {
+        glyphList = new FTGlyphContainer( &face);
+    }
 }
 
 
@@ -17,6 +21,10 @@ FTFont::FTFont( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
     glyphList(0)
 {
     err = face.Error();
+    if( err == 0)
+    {
+        glyphList = new FTGlyphContainer( &face);
+    }
 }
 
 
@@ -83,8 +91,8 @@ unsigned int FTFont::FaceSize() const
 
 bool FTFont::CharMap( FT_Encoding encoding)
 {
-    bool result = face.CharMap( encoding);
-    err = face.Error();
+    bool result = glyphList->CharMap( encoding);
+    err = glyphList->Error();
     return result;
 }
 
@@ -240,12 +248,12 @@ void FTFont::DoRender( const unsigned int chr, const unsigned int nextChr)
 }
 
 
-void FTFont::CheckGlyph( const unsigned int chr)
+void FTFont::CheckGlyph( const unsigned int characterCode)
 {
-    if( !glyphList->Glyph( chr))
+    if( NULL == glyphList->Glyph( characterCode))
     {
-        unsigned int g = face.CharIndex( chr);
-        glyphList->Add( MakeGlyph( g), g);
+        unsigned int glyphIndex = glyphList->GlyphIndex( characterCode);
+        glyphList->Add( MakeGlyph( glyphIndex), characterCode);
     }
 }
 
diff --git a/src/FTGlyphContainer.cpp b/src/FTGlyphContainer.cpp
index fc5e734..f7290ec 100755
--- a/src/FTGlyphContainer.cpp
+++ b/src/FTGlyphContainer.cpp
@@ -9,13 +9,8 @@ FTGlyphContainer::FTGlyphContainer( FTFace* f)
     charMap(0),
     err(0)
 {
-    numberOfGlyphs = face->GlyphCount();
-
-    if( 0 != numberOfGlyphs)
-    {
-        glyphs.resize( numberOfGlyphs, NULL);
-        charMap = new FTCharmap( face);
-    }
+    glyphs.push_back( NULL);
+    charMap = new FTCharmap( face);
 }
 
 
@@ -39,22 +34,23 @@ bool FTGlyphContainer::CharMap( FT_Encoding encoding)
 }
 
 
-unsigned int FTGlyphContainer::CharIndex( unsigned int characterCode) const
+unsigned int FTGlyphContainer::GlyphIndex( const unsigned int characterCode) const
 {
-    return charMap->CharIndex( characterCode);
+    return charMap->GlyphIndex( characterCode);
 }
 
 
 void FTGlyphContainer::Add( FTGlyph* tempGlyph, const unsigned int characterCode)
 {
-    unsigned int glyphIndex = charMap->CharIndex( characterCode);
-    glyphs[glyphIndex] = tempGlyph;
+    charMap->InsertIndex( characterCode, glyphs.size());
+    glyphs.push_back( tempGlyph);
 }
 
 
 const FTGlyph* const FTGlyphContainer::Glyph( const unsigned int characterCode) const
 {
-    return glyphs[charMap->CharIndex( characterCode)];
+    unsigned int index = charMap->CharIndex( characterCode);
+    return glyphs[index];
 }
 
 
@@ -64,31 +60,31 @@ FTBBox FTGlyphContainer::BBox( const unsigned int characterCode) const
 }
 
 
-float FTGlyphContainer::Advance( unsigned int characterCode, unsigned int nextCharacterCode)
+float FTGlyphContainer::Advance( const unsigned int characterCode, const unsigned int nextCharacterCode)
 {
-    unsigned int left = charMap->CharIndex( characterCode);
-    unsigned int right = charMap->CharIndex( nextCharacterCode);
+    unsigned int left = charMap->GlyphIndex( characterCode);
+    unsigned int right = charMap->GlyphIndex( nextCharacterCode);
     
     float width = face->KernAdvance( left, right).x;
-    width += glyphs[left]->Advance();
+    width += glyphs[charMap->CharIndex( characterCode)]->Advance();
     
     return width;
 }
 
 
-FTPoint FTGlyphContainer::Render( unsigned int characterCode, unsigned int nextCharacterCode, FTPoint penPosition)
+FTPoint FTGlyphContainer::Render( const unsigned int characterCode, const unsigned int nextCharacterCode, FTPoint penPosition)
 {
     FTPoint kernAdvance;
     float advance = 0;
     
-    unsigned int left = charMap->CharIndex( characterCode);
-    unsigned int right = charMap->CharIndex( nextCharacterCode);
+    unsigned int left = charMap->GlyphIndex( characterCode);
+    unsigned int right = charMap->GlyphIndex( nextCharacterCode);
     
     kernAdvance = face->KernAdvance( left, right);
         
     if( !face->Error())
     {
-        advance = glyphs[left]->Render( penPosition);
+        advance = glyphs[charMap->CharIndex( characterCode)]->Render( penPosition);
     }
     
     kernAdvance.x = advance + kernAdvance.x;
diff --git a/test/FTCharmap-Test.cpp b/test/FTCharmap-Test.cpp
index 8ce41fd..43e740d 100755
--- a/test/FTCharmap-Test.cpp
+++ b/test/FTCharmap-Test.cpp
@@ -20,6 +20,8 @@ class FTCharmapTest : public CppUnit::TestCase
         CPPUNIT_TEST( testConstructor);
         CPPUNIT_TEST( testSetEncoding);
         CPPUNIT_TEST( testGetCharacterIndex);
+        CPPUNIT_TEST( testGetGlyphIndex);
+        CPPUNIT_TEST( testInsertCharacterIndex);
     CPPUNIT_TEST_SUITE_END();
         
     public:
@@ -62,16 +64,39 @@ class FTCharmapTest : public CppUnit::TestCase
             charmap->CharMap( ft_encoding_unicode);
             
             CPPUNIT_ASSERT( charmap->Error() == 0);
-            CPPUNIT_ASSERT( charmap->CharIndex( CHARACTER_CODE_A)    == FONT_INDEX_OF_A);
-            CPPUNIT_ASSERT( charmap->CharIndex( BIG_CHARACTER_CODE)  == BIG_FONT_INDEX);
-            CPPUNIT_ASSERT( charmap->CharIndex( NULL_CHARACTER_CODE) == NULL_FONT_INDEX);
+            CPPUNIT_ASSERT( charmap->CharIndex( CHARACTER_CODE_A)    == 0);
+            CPPUNIT_ASSERT( charmap->CharIndex( BIG_CHARACTER_CODE)  == 0);
+            CPPUNIT_ASSERT( charmap->CharIndex( NULL_CHARACTER_CODE) == 0);
             
             charmap->CharMap( ft_encoding_johab);
             
             CPPUNIT_ASSERT( charmap->Error() == 6);
-            CPPUNIT_ASSERT( charmap->CharIndex( CHARACTER_CODE_A)    == FONT_INDEX_OF_A);
-            CPPUNIT_ASSERT( charmap->CharIndex( BIG_CHARACTER_CODE)  == BIG_FONT_INDEX);
-            CPPUNIT_ASSERT( charmap->CharIndex( NULL_CHARACTER_CODE) == NULL_FONT_INDEX);
+            CPPUNIT_ASSERT( charmap->CharIndex( CHARACTER_CODE_A)    == 0);
+            CPPUNIT_ASSERT( charmap->CharIndex( BIG_CHARACTER_CODE)  == 0);
+            CPPUNIT_ASSERT( charmap->CharIndex( NULL_CHARACTER_CODE) == 0);
+        }
+
+    
+        void testGetGlyphIndex()
+        {
+            charmap->CharMap( ft_encoding_unicode);
+
+            CPPUNIT_ASSERT( charmap->Error() == 0);
+            CPPUNIT_ASSERT( charmap->GlyphIndex( CHARACTER_CODE_A)    == FONT_INDEX_OF_A);
+            CPPUNIT_ASSERT( charmap->GlyphIndex( BIG_CHARACTER_CODE)  == BIG_FONT_INDEX);
+            CPPUNIT_ASSERT( charmap->GlyphIndex( NULL_CHARACTER_CODE) == NULL_FONT_INDEX);
+        }
+    
+    
+        void testInsertCharacterIndex()
+        {
+            CPPUNIT_ASSERT( charmap->CharIndex( CHARACTER_CODE_A) == 0);
+
+            charmap->InsertIndex( CHARACTER_CODE_A, charmap->GlyphIndex( CHARACTER_CODE_A));
+            CPPUNIT_ASSERT( charmap->CharIndex( CHARACTER_CODE_A) == FONT_INDEX_OF_A);
+
+            charmap->InsertIndex( CHARACTER_CODE_A, 999);
+            CPPUNIT_ASSERT( charmap->CharIndex( CHARACTER_CODE_A) == 999);
         }
         
         
diff --git a/test/FTGlyphContainer-Test.cpp b/test/FTGlyphContainer-Test.cpp
index e2eb5f0..7ab4c5c 100755
--- a/test/FTGlyphContainer-Test.cpp
+++ b/test/FTGlyphContainer-Test.cpp
@@ -28,7 +28,7 @@ class FTGlyphContainerTest : public CppUnit::TestCase
     CPPUNIT_TEST_SUITE( FTGlyphContainerTest);
         CPPUNIT_TEST( testAdd);
         CPPUNIT_TEST( testSetCharMap);
-        CPPUNIT_TEST( testCharacterIndex);
+        CPPUNIT_TEST( testGlyphIndex);
         CPPUNIT_TEST( testAdvance);
         CPPUNIT_TEST( testRender);
     CPPUNIT_TEST_SUITE_END();
@@ -48,11 +48,14 @@ class FTGlyphContainerTest : public CppUnit::TestCase
         void testAdd()
         {
             TestGlyph* glyph = new TestGlyph();
-            glyphContainer->Add( glyph, 'A');
-            glyphContainer->Add( 0, 0);
+            CPPUNIT_ASSERT( glyphContainer->Glyph( CHARACTER_CODE_A) == NULL);
+
+            glyphContainer->Add( glyph, CHARACTER_CODE_A);
+            glyphContainer->Add( NULL, 0);
 
-            CPPUNIT_ASSERT( glyphContainer->Glyph( 0) == 0);
-            CPPUNIT_ASSERT( glyphContainer->Glyph( 'A') == glyph);
+            CPPUNIT_ASSERT( glyphContainer->Glyph( 0) == NULL);
+            CPPUNIT_ASSERT( glyphContainer->Glyph( 999) == NULL);
+            CPPUNIT_ASSERT( glyphContainer->Glyph( CHARACTER_CODE_A) == glyph);
         }
 
     
@@ -66,19 +69,19 @@ class FTGlyphContainerTest : public CppUnit::TestCase
         }
 
 
-        void testCharacterIndex()
+        void testGlyphIndex()
         {
-            CPPUNIT_ASSERT( glyphContainer->CharIndex( 'A') == 34);
-            CPPUNIT_ASSERT( glyphContainer->CharIndex( 0x6FB3) == 4838);
-        }    
-        
-        
+            CPPUNIT_ASSERT( glyphContainer->GlyphIndex( CHARACTER_CODE_A) == FONT_INDEX_OF_A);
+            CPPUNIT_ASSERT( glyphContainer->GlyphIndex( BIG_CHARACTER_CODE) == BIG_FONT_INDEX);
+        }
+
+
         void testAdvance()
         {
             TestGlyph* glyph = new TestGlyph();
 
-            glyphContainer->Add( glyph, 'A');
-            float advance = glyphContainer->Advance( 'A', 0);
+            glyphContainer->Add( glyph, CHARACTER_CODE_A);
+            float advance = glyphContainer->Advance( CHARACTER_CODE_A, 0);
             
             CPPUNIT_ASSERT_DOUBLES_EQUAL( 50, advance, 0.01);
         }
@@ -107,7 +110,6 @@ class FTGlyphContainerTest : public CppUnit::TestCase
         void tearDown() 
         {
             delete glyphContainer;
-            delete face;
         }
         
     private: