Added attach from memory function
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
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);