Commit 70869a5de261f441de849b900732a0c6876b7750

henry 2003-01-08T04:24:36

Added attach from memory function

diff --git a/include/FTFace.h b/include/FTFace.h
index a11f2bc..d2b58b6 100755
--- a/include/FTFace.h
+++ b/include/FTFace.h
@@ -53,6 +53,16 @@ class FTGL_EXPORT FTFace
         bool Attach( const char* filename);
 
         /**
+         * Attach auxilliary data to font (e.g., font metrics) from memory
+         *
+         * @param pBufferBytes  the in-memory buffer
+         * @param bufferSizeInBytes  the length of the buffer in bytes
+         * @return          <code>true</code> if file has opened
+         *                  successfully.
+         */
+        bool Attach( const unsigned char *pBufferBytes, size_t bufferSizeInBytes);
+
+        /**
          * Disposes of the face
          */
         void Close();
diff --git a/include/FTFont.h b/include/FTFont.h
index c140323..36cc942 100755
--- a/include/FTFont.h
+++ b/include/FTFont.h
@@ -60,6 +60,18 @@ class FTGL_EXPORT FTFont
         bool Attach( const char* filename);
 
         /**
+         * Attach auxilliary data to font e.g font metrics, from memory
+         *
+         * Note: not all font formats implement this function.
+         *
+         * @param pBufferBytes  the in-memory buffer
+         * @param bufferSizeInBytes  the length of the buffer in bytes
+         * @return          <code>true</code> if file has been attached
+         *                  successfully.
+         */
+        bool Attach( const unsigned char *pBufferBytes, size_t bufferSizeInBytes);
+
+        /**
          * Set the character map for the face.
          *
          * @param encoding      Freetype enumerate for char map code.
diff --git a/src/FTFace.cpp b/src/FTFace.cpp
index 8ddc90d..1e6acde 100755
--- a/src/FTFace.cpp
+++ b/src/FTFace.cpp
@@ -26,7 +26,7 @@ FTFace::FTFace( const char* filename)
 }
 
 
-FTFace::FTFace( const unsigned char *pBufferBytes, size_t bufferSizeInBytes )
+FTFace::FTFace( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
 :   charMap(0),
     numGlyphs(0),
     err(0)
@@ -63,6 +63,19 @@ bool FTFace::Attach( const char* filename)
 }
 
 
+bool FTFace::Attach( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
+{
+    FT_Open_Args open;
+
+    open.flags = (FT_Open_Flags)1; // FT_OPEN_MEMORY;
+    open.memory_base = pBufferBytes;
+    open.memory_size = bufferSizeInBytes;
+
+    err = FT_Attach_Stream( *ftFace, &open);
+    return !err;
+}
+
+
 void FTFace::Close()
 {
     if( ftFace)
diff --git a/src/FTFont.cpp b/src/FTFont.cpp
index b2d0215..0e3f5c3 100755
--- a/src/FTFont.cpp
+++ b/src/FTFont.cpp
@@ -41,6 +41,21 @@ bool FTFont::Attach( const char* filename)
 }
 
 
+bool FTFont::Attach( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
+{
+    if( face.Attach( pBufferBytes, bufferSizeInBytes))
+    {
+        err = 0;
+        return true;
+    }
+    else
+    {
+        err = face.Error();
+        return false;
+    }
+}
+
+
 bool FTFont::FaceSize( const unsigned int size, const unsigned int res )
 {
     charSize = face.Size( size, res);
diff --git a/test/FTFace-Test.cpp b/test/FTFace-Test.cpp
index 593d3fe..ca6e475 100755
--- a/test/FTFace-Test.cpp
+++ b/test/FTFace-Test.cpp
@@ -13,6 +13,7 @@ class FTFaceTest : public CppUnit::TestCase
         CPPUNIT_TEST( testOpenFace);
         CPPUNIT_TEST( testOpenFaceFromMemory);
         CPPUNIT_TEST( testAttachFile);
+        CPPUNIT_TEST( testAttachMemoryData);
         CPPUNIT_TEST( testGlyphCount);
         CPPUNIT_TEST( testSetFontSize);
         CPPUNIT_TEST( testSetCharMap);
@@ -52,6 +53,13 @@ class FTFaceTest : public CppUnit::TestCase
     }
     
 
+    void testAttachMemoryData()
+    {
+        CPPUNIT_ASSERT( !testFace->Attach((unsigned char*)100, 0));
+        CPPUNIT_ASSERT( testFace->Error() == 7);        
+    }
+    
+
     void testGlyphCount()
     {
         CPPUNIT_ASSERT( testFace->GlyphCount() == 14099);        
diff --git a/test/FTFont-Test.cpp b/test/FTFont-Test.cpp
index 1c24ee2..4b57269 100755
--- a/test/FTFont-Test.cpp
+++ b/test/FTFont-Test.cpp
@@ -55,6 +55,7 @@ class FTFontTest : public CppUnit::TestCase
         CPPUNIT_TEST( testOpenFont);
         CPPUNIT_TEST( testOpenFontFromMemory);
         CPPUNIT_TEST( testAttachFile);
+        CPPUNIT_TEST( testAttachData);
         CPPUNIT_TEST( testSetFontSize);
         CPPUNIT_TEST( testSetCharMap);
         CPPUNIT_TEST( testBoundingBox);
@@ -93,6 +94,13 @@ class FTFontTest : public CppUnit::TestCase
     }
     
 
+    void testAttachData()
+    {
+        testFont->Attach( (unsigned char*)100, 0);
+        CPPUNIT_ASSERT( testFont->Error() == 7);        
+    }
+    
+
     void testSetFontSize()
     {
         CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, testFont->Ascender(), 0.01);