From ff2ef27fd1b383c404218ad25a3d80ec0a77de21 Mon Sep 17 00:00:00 2001 From: Victor Cebollada Date: Tue, 1 Apr 2014 09:38:16 +0100 Subject: [PATCH] Emoticons - Read file names from directory. Retrieves bitmaps from the font face. [Issue#] N/A [Problem] N/A [Cause] N/A [Solution] N/A Change-Id: If4c2b99b3132c6c85087cf161dfa3cba5236bc0a Signed-off-by: Victor Cebollada --- .../slp/resource-loader/loader-font.cpp | 44 ++++++++++++++++++++++ .../slp/resource-loader/loader-font.h | 10 +++++ .../slp/resource-loader/resource-loader.cpp | 15 ++++++++ .../slp/resource-loader/resource-loader.h | 13 +++++++ .../slp/slp-platform-abstraction.cpp | 43 ++++++++++++++++++++- .../slp/slp-platform-abstraction.h | 13 ++++++- 6 files changed, 136 insertions(+), 2 deletions(-) diff --git a/platform-abstractions/slp/resource-loader/loader-font.cpp b/platform-abstractions/slp/resource-loader/loader-font.cpp index be50a67..a9b796d 100644 --- a/platform-abstractions/slp/resource-loader/loader-font.cpp +++ b/platform-abstractions/slp/resource-loader/loader-font.cpp @@ -313,6 +313,50 @@ GlyphSet::Character* GetCharacter(FT_Face face, FT_ULong charcode, return new GlyphSet::Character(bitmapData, glyphMetrics); } +Integration::BitmapPtr GetGlyphBitmap( FT_Face face, FT_ULong charCode ) +{ + Integration::BitmapPtr image; + + FT_Glyph ftGlyph = GetGlyph( face, charCode, FT_LOAD_RENDER ); + if( NULL != ftGlyph ) + { + FT_Error ftError = FT_Err_Ok; + + // convert glyph to bitmap + if( ftGlyph->format != FT_GLYPH_FORMAT_BITMAP ) + { + ftError = FT_Glyph_To_Bitmap( &ftGlyph, FT_RENDER_MODE_NORMAL, 0, 1 ); + + if( ftError != FT_Err_Ok) + { + DALI_LOG_WARNING( "FT_Glyph_To_Bitmap failed %d\n", ftError ); + FT_Done_Glyph( ftGlyph ); + return image; + } + } + + // cast the FT_Glyph to a FT_BitmapGlyph + FT_BitmapGlyph ftBitmapGlyph = (FT_BitmapGlyph)ftGlyph; + + // access the underlying bitmap data + FT_Bitmap ftBitmap = ftBitmapGlyph->bitmap; + + const std::size_t size = ftBitmap.width * ftBitmap.rows; + if( 0 < size ) + { + image = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true ); + image->GetPackedPixelsProfile()->ReserveBuffer( Pixel::A8, ftBitmap.width, ftBitmap.rows ); + + memcpy( static_cast( image->GetBuffer() ), ftBitmap.buffer, size ); + } + + // finished with glyph, so, release it + FT_Done_Glyph( ftGlyph ); + } + + return image; +} + } // namespace SlpPlatform } // namespace Dali diff --git a/platform-abstractions/slp/resource-loader/loader-font.h b/platform-abstractions/slp/resource-loader/loader-font.h index 4475683..89838a4 100644 --- a/platform-abstractions/slp/resource-loader/loader-font.h +++ b/platform-abstractions/slp/resource-loader/loader-font.h @@ -109,6 +109,16 @@ Integration::GlyphSet::Character* GetCharacter(FT_Face face, const FT_ULong char const Vector2& maxGlyphCell, const bool renderBitmap, const bool highQuality ); +/** + * Retrieves a glyph's image from the given Freetype face object. + * + * @param[in] face Freetype face object. + * @param[in] charCode UCS4 character code (UTF32). + * + * @return The glyph's image for the given character. + */ +Integration::BitmapPtr GetGlyphBitmap( FT_Face face, FT_ULong charCode ); + } // namespace SlpPlatform } // namespace Dali diff --git a/platform-abstractions/slp/resource-loader/resource-loader.cpp b/platform-abstractions/slp/resource-loader/resource-loader.cpp index 0abdded..947348f 100755 --- a/platform-abstractions/slp/resource-loader/resource-loader.cpp +++ b/platform-abstractions/slp/resource-loader/resource-loader.cpp @@ -892,6 +892,21 @@ bool ResourceLoader::SaveFile(const std::string& filename, std::vector< unsigned return result; } +Integration::BitmapPtr ResourceLoader::GetGlyphImage( FT_Library freeType, const std::string& fontFamily, const std::string& fontStyle, const float fontSize, const uint32_t character ) +{ + Integration::BitmapPtr image; + + const std::string fontFileName = GetFontPath( fontFamily, fontStyle ); + SlpFace* slpFace = LoadFontFace( fontFileName, PixelSize( Font::PointsToPixels( fontSize ) ), freeType ); + + if( NULL != slpFace ) + { + image = GetGlyphBitmap( slpFace->face, character ); + } + + return image; +} + void ResourceLoader::SetDefaultFontFamily( const std::string& fontFamily, const std::string& fontStyle ) { mImpl->mFontController->SetDefaultFontFamily( Platform::FontController::StyledFontFamily( fontFamily, fontStyle ) ); diff --git a/platform-abstractions/slp/resource-loader/resource-loader.h b/platform-abstractions/slp/resource-loader/resource-loader.h index d4b1f56..0fedd91 100644 --- a/platform-abstractions/slp/resource-loader/resource-loader.h +++ b/platform-abstractions/slp/resource-loader/resource-loader.h @@ -364,6 +364,19 @@ public: */ void SetDefaultFontFamily( const std::string& fontFamily, const std::string& fontStyle ); + /** + * Retrieves the glyp's image representing the given character. + * + * @param[in] freeType Handle to the FreeType library. + * @param[in] fontFamily The font's family name. + * @param[in] fontStyle The font's style. + * @param[in] fontSize The font's size. + * @param[in] character The given character. + * + * @return The bitmap image. + */ + Integration::BitmapPtr GetGlyphImage( FT_Library freeType, const std::string& fontFamily, const std::string& fontStyle, const float fontSize, const uint32_t character ); + private: /** diff --git a/platform-abstractions/slp/slp-platform-abstraction.cpp b/platform-abstractions/slp/slp-platform-abstraction.cpp index 6b39365..1033877 100644 --- a/platform-abstractions/slp/slp-platform-abstraction.cpp +++ b/platform-abstractions/slp/slp-platform-abstraction.cpp @@ -17,6 +17,7 @@ #include "slp-platform-abstraction.h" #include +#include #include #include @@ -118,7 +119,7 @@ const float SlpPlatformAbstraction::GetDefaultFontSize() const return mDefaultFontSize; } -const PixelSize SlpPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight) const +const PixelSize SlpPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight) const { PixelSize result(0); @@ -392,6 +393,46 @@ void SlpPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily, MetricsCache::Write( fontFamily, fontStyle, glyphSet ); } +void SlpPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName, + std::vector& fileNames ) +{ + dirent* de = NULL; + DIR* dp; + dp = opendir( directoryName.c_str() ); + if( dp ) + { + const std::string dot( "." ); + const std::string dotDot( ".." ); + while( true ) + { + de = readdir( dp ); + if( de == NULL ) + { + break; + } + const std::string fileName( de->d_name ); + if( ( fileName != dot ) && + ( fileName != dotDot ) ) + { + fileNames.push_back( fileName ); + } + } + closedir( dp ); + } +} + +Integration::BitmapPtr SlpPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, const float fontSize, const uint32_t character ) const +{ + Integration::BitmapPtr glyphImage; + + if( mResourceLoader ) + { + glyphImage = mResourceLoader->GetGlyphImage( mFreeTypeHandle, fontFamily, fontStyle, fontSize, character ); + } + + return glyphImage; +} + } // namespace SlpPlatform } // namespace Dali diff --git a/platform-abstractions/slp/slp-platform-abstraction.h b/platform-abstractions/slp/slp-platform-abstraction.h index a445ecb..c948ba5 100644 --- a/platform-abstractions/slp/slp-platform-abstraction.h +++ b/platform-abstractions/slp/slp-platform-abstraction.h @@ -136,7 +136,7 @@ public: // PlatformAbstraction overrides /** *@copydoc PlatformAbstraction::GetFontLineHeightFromCapsHeight() */ - virtual const PixelSize GetFontLineHeightFromCapsHeight(const std::string fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight) const; + virtual const PixelSize GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight) const; /** * @copydoc PlatformAbstraction::GetGlyphData() @@ -236,6 +236,17 @@ public: // PlatformAbstraction overrides const std::string& fontStyle, const Integration::GlyphSet& glyphSet ); + /** + * @copydoc PlatformAbstraction::GetFileNamesFromDirectory() + */ + virtual void GetFileNamesFromDirectory( const std::string& directoryName, + std::vector& fileNames ); + + /** + * @copydoc PlatformAbstraction::GetGlyphImage() + */ + virtual Integration::BitmapPtr GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const; + private: ResourceLoader* mResourceLoader; FT_Library mFreeTypeHandle; ///< Freetype library -- 2.7.4