Commit 5504970b26909bf399ad35fbd1e5475c3125d7ad

henry 2003-06-03T03:08:31

Moved FTCharmap into this class

diff --git a/include/FTGlyphContainer.h b/include/FTGlyphContainer.h
index 314bcd6..fa6df1f 100755
--- a/include/FTGlyphContainer.h
+++ b/include/FTGlyphContainer.h
@@ -12,6 +12,7 @@
 
 class FTFace;
 class FTGlyph;
+class FTCharmap;
 
 /**
  * FTGlyphContainer holds the post processed FTGlyph objects.
@@ -35,13 +36,30 @@ class FTGL_EXPORT FTGlyphContainer
         ~FTGlyphContainer();
 
         /**
+            * Sets the character map for the face.
+         *
+         * @param encoding      the Freetype encoding symbol. See above.
+         * @return              <code>true</code> if charmap was valid
+         *                      and set correctly
+         */
+        bool CharMap( FT_Encoding encoding);
+
+        /**
+         *  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;
+        
+        /**
          * Adds a glyph to this glyph list.
          *
-         * @param glyph      The FTGlyph to be inserted into the container
-         * @param glyphIndex The glyphs index in the container.
-         * @return           <code>true</code>
+         * @param glyph         The FTGlyph to be inserted into the container
+         * @param characterCode The char code of the glyph NOT the glyph index.
          */
-        bool Add( FTGlyph* glyph, unsigned int glyphIndex);
+        void Add( FTGlyph* glyph, const unsigned int characterCode);
 
         /**
          * Get a glyph from the glyph list
@@ -90,11 +108,16 @@ class FTGL_EXPORT FTGlyphContainer
         unsigned int numberOfGlyphs;
 
         /**
-         * The current FTGL face
+         * The FTGL face
          */
         FTFace* face;
 
         /**
+         * The Character Map object associated with the current face
+         */
+        FTCharmap* charMap;
+
+        /**
          * A structure to hold the glyphs
          */
         GlyphVector glyphs;
diff --git a/src/FTGlyphContainer.cpp b/src/FTGlyphContainer.cpp
index 8b1695c..fc5e734 100755
--- a/src/FTGlyphContainer.cpp
+++ b/src/FTGlyphContainer.cpp
@@ -1,14 +1,21 @@
 #include    "FTGlyphContainer.h"
 #include    "FTGlyph.h"
 #include    "FTFace.h"
+#include    "FTCharmap.h"
 
 
 FTGlyphContainer::FTGlyphContainer( FTFace* f)
 :   face(f),
+    charMap(0),
     err(0)
 {
     numberOfGlyphs = face->GlyphCount();
-    glyphs.resize( numberOfGlyphs, NULL);
+
+    if( 0 != numberOfGlyphs)
+    {
+        glyphs.resize( numberOfGlyphs, NULL);
+        charMap = new FTCharmap( face);
+    }
 }
 
 
@@ -24,34 +31,43 @@ FTGlyphContainer::~FTGlyphContainer()
 }
 
 
-bool FTGlyphContainer::Add( FTGlyph* tempGlyph, unsigned int glyphIndex)
+bool FTGlyphContainer::CharMap( FT_Encoding encoding)
 {
-    if( glyphIndex >= numberOfGlyphs)
-    {
-        return false;
-    }
-    
+    bool result = charMap->CharMap( encoding);
+    err = charMap->Error();
+    return result;
+}
+
+
+unsigned int FTGlyphContainer::CharIndex( unsigned int characterCode) const
+{
+    return charMap->CharIndex( characterCode);
+}
+
+
+void FTGlyphContainer::Add( FTGlyph* tempGlyph, const unsigned int characterCode)
+{
+    unsigned int glyphIndex = charMap->CharIndex( characterCode);
     glyphs[glyphIndex] = tempGlyph;
-    return true;
 }
 
 
 const FTGlyph* const FTGlyphContainer::Glyph( const unsigned int characterCode) const
 {
-    return glyphs[face->CharIndex( characterCode)];
+    return glyphs[charMap->CharIndex( characterCode)];
 }
 
 
 FTBBox FTGlyphContainer::BBox( const unsigned int characterCode) const
 {
-    return glyphs[face->CharIndex( characterCode)]->BBox();
+    return glyphs[charMap->CharIndex( characterCode)]->BBox();
 }
 
 
 float FTGlyphContainer::Advance( unsigned int characterCode, unsigned int nextCharacterCode)
 {
-    unsigned int left = face->CharIndex( characterCode);
-    unsigned int right = face->CharIndex( nextCharacterCode);
+    unsigned int left = charMap->CharIndex( characterCode);
+    unsigned int right = charMap->CharIndex( nextCharacterCode);
     
     float width = face->KernAdvance( left, right).x;
     width += glyphs[left]->Advance();
@@ -65,8 +81,8 @@ FTPoint FTGlyphContainer::Render( unsigned int characterCode, unsigned int nextC
     FTPoint kernAdvance;
     float advance = 0;
     
-    unsigned int left = face->CharIndex( characterCode);
-    unsigned int right = face->CharIndex( nextCharacterCode);
+    unsigned int left = charMap->CharIndex( characterCode);
+    unsigned int right = charMap->CharIndex( nextCharacterCode);
     
     kernAdvance = face->KernAdvance( left, right);
         
diff --git a/test/FTGlyphContainer-Test.cpp b/test/FTGlyphContainer-Test.cpp
index c66d0fa..e2eb5f0 100755
--- a/test/FTGlyphContainer-Test.cpp
+++ b/test/FTGlyphContainer-Test.cpp
@@ -27,6 +27,8 @@ class FTGlyphContainerTest : public CppUnit::TestCase
 {
     CPPUNIT_TEST_SUITE( FTGlyphContainerTest);
         CPPUNIT_TEST( testAdd);
+        CPPUNIT_TEST( testSetCharMap);
+        CPPUNIT_TEST( testCharacterIndex);
         CPPUNIT_TEST( testAdvance);
         CPPUNIT_TEST( testRender);
     CPPUNIT_TEST_SUITE_END();
@@ -36,7 +38,6 @@ class FTGlyphContainerTest : public CppUnit::TestCase
         {
             face = new FTFace( GOOD_FONT_FILE);
             face->Size( 72, 72);
-            face->CharMap( ft_encoding_unicode);
         }
         
         FTGlyphContainerTest( const std::string& name) : CppUnit::TestCase(name)
@@ -47,19 +48,36 @@ class FTGlyphContainerTest : public CppUnit::TestCase
         void testAdd()
         {
             TestGlyph* glyph = new TestGlyph();
-            CPPUNIT_ASSERT( !glyphContainer->Add( glyph, TOO_MANY_GLYPHS));
-            CPPUNIT_ASSERT( glyphContainer->Add( glyph, FONT_INDEX_OF_A));
-            
+            glyphContainer->Add( glyph, 'A');
+            glyphContainer->Add( 0, 0);
+
+            CPPUNIT_ASSERT( glyphContainer->Glyph( 0) == 0);
             CPPUNIT_ASSERT( glyphContainer->Glyph( 'A') == glyph);
         }
+
+    
+        void testSetCharMap()
+        {
+            CPPUNIT_ASSERT( glyphContainer->CharMap( ft_encoding_unicode));
+            CPPUNIT_ASSERT( glyphContainer->Error() == 0);
+    
+            CPPUNIT_ASSERT( !glyphContainer->CharMap( ft_encoding_johab));
+            CPPUNIT_ASSERT( glyphContainer->Error() == 6);
+        }
+
+
+        void testCharacterIndex()
+        {
+            CPPUNIT_ASSERT( glyphContainer->CharIndex( 'A') == 34);
+            CPPUNIT_ASSERT( glyphContainer->CharIndex( 0x6FB3) == 4838);
+        }    
         
         
         void testAdvance()
         {
             TestGlyph* glyph = new TestGlyph();
 
-            CPPUNIT_ASSERT( glyphContainer->Add( glyph, FONT_INDEX_OF_A));
-            
+            glyphContainer->Add( glyph, 'A');
             float advance = glyphContainer->Advance( 'A', 0);
             
             CPPUNIT_ASSERT_DOUBLES_EQUAL( 50, advance, 0.01);
@@ -70,7 +88,7 @@ class FTGlyphContainerTest : public CppUnit::TestCase
         {
             TestGlyph* glyph = new TestGlyph();
             
-            CPPUNIT_ASSERT( glyphContainer->Add( glyph, FONT_INDEX_OF_A));
+            glyphContainer->Add( glyph, 'A');
             
             FTPoint pen;
             
@@ -93,8 +111,9 @@ class FTGlyphContainerTest : public CppUnit::TestCase
         }
         
     private:
-        FTFace* face;
+        FTFace*           face;
         FTGlyphContainer* glyphContainer;
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION( FTGlyphContainerTest);