From Csaba Halasz, support reading "monochrome" option string and use of monocrhome FreetType support

This commit is contained in:
Robert Osfield 2007-07-02 12:43:23 +00:00
parent 19d1a563fe
commit e5a6b1a834
5 changed files with 56 additions and 23 deletions

View File

@ -17,17 +17,19 @@
#include <osg/Notify>
#include <osgDB/WriteFile>
FreeTypeFont::FreeTypeFont(const std::string& filename, FT_Face face):
FreeTypeFont::FreeTypeFont(const std::string& filename, FT_Face face, unsigned int flags):
_filename(filename),
_buffer(0),
_face(face)
_face(face),
_flags(flags)
{
}
FreeTypeFont::FreeTypeFont(FT_Byte* buffer, FT_Face face):
FreeTypeFont::FreeTypeFont(FT_Byte* buffer, FT_Face face, unsigned int flags):
_filename(""),
_buffer(buffer),
_face(face)
_face(face),
_flags(flags)
{
}
@ -105,7 +107,7 @@ osgText::Font::Glyph* FreeTypeFont::getGlyph(unsigned int charcode)
}
}
FT_Error error = FT_Load_Char( _face, charindex, FT_LOAD_RENDER|FT_LOAD_NO_BITMAP );
FT_Error error = FT_Load_Char( _face, charindex, FT_LOAD_RENDER|FT_LOAD_NO_BITMAP|_flags );
if (error)
{
osg::notify(osg::WARN) << "FT_Load_Char(...) error "<<error<<std::endl;
@ -143,13 +145,33 @@ osgText::Font::Glyph* FreeTypeFont::getGlyph(unsigned int charcode)
glyph->setInternalTextureFormat(GL_ALPHA);
// copy image across to osgText::Glyph image.
for(int r=sourceHeight-1;r>=0;--r)
switch(glyphslot->bitmap.pixel_mode)
{
unsigned char* ptr = buffer+r*pitch;
for(unsigned int c=0;c<sourceWidth;++c,++ptr)
{
(*data++)=*ptr;
}
case FT_PIXEL_MODE_MONO:
for(int r=sourceHeight-1;r>=0;--r)
{
unsigned char* ptr = buffer+r*pitch;
for(unsigned int c=0;c<sourceWidth;++c)
{
(*data++)= (ptr[c >> 3] & (1 << (~c & 7))) ? 255 : 0;
}
}
break;
case FT_PIXEL_MODE_GRAY:
for(int r=sourceHeight-1;r>=0;--r)
{
unsigned char* ptr = buffer+r*pitch;
for(unsigned int c=0;c<sourceWidth;++c,++ptr)
{
(*data++)=*ptr;
}
}
break;
default:
osg::notify(osg::WARN) << "FT_Load_Char(...) returned bitmap with unknown pixel_mode " << glyphslot->bitmap.pixel_mode << std::endl;
}

View File

@ -24,8 +24,8 @@ class FreeTypeFont : public osgText::Font::FontImplementation
// declare the interface to a font.
public:
FreeTypeFont(const std::string& filename, FT_Face face);
FreeTypeFont(FT_Byte* buffer, FT_Face face);
FreeTypeFont(const std::string& filename, FT_Face face, unsigned int flags);
FreeTypeFont(FT_Byte* buffer, FT_Face face, unsigned int flags);
virtual ~FreeTypeFont();
@ -44,7 +44,7 @@ protected:
std::string _filename;
FT_Byte* _buffer;
FT_Face _face;
unsigned int _flags;
};
#endif

View File

@ -56,7 +56,7 @@ FreeTypeLibrary* FreeTypeLibrary::instance()
return s_library.get();
}
osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile,unsigned int index)
osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile,unsigned int index, unsigned int flags)
{
FT_Face face; /* handle to face object */
@ -96,7 +96,7 @@ osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile,unsigned int
//
verifyCharacterMap(face);
FreeTypeFont* fontImp = new FreeTypeFont(fontfile,face);
FreeTypeFont* fontImp = new FreeTypeFont(fontfile,face,flags);
osgText::Font* font = new osgText::Font(fontImp);
_fontImplementationSet.insert(fontImp);
@ -105,7 +105,7 @@ osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile,unsigned int
}
osgText::Font* FreeTypeLibrary::getFont(std::istream& fontstream, unsigned int index)
osgText::Font* FreeTypeLibrary::getFont(std::istream& fontstream, unsigned int index, unsigned int flags)
{
FT_Face face; /* handle to face object */
FT_Open_Args args;
@ -148,7 +148,7 @@ osgText::Font* FreeTypeLibrary::getFont(std::istream& fontstream, unsigned int i
//
verifyCharacterMap(face);
FreeTypeFont* fontImp = new FreeTypeFont(buffer,face);
FreeTypeFont* fontImp = new FreeTypeFont(buffer,face,flags);
osgText::Font* font = new osgText::Font(fontImp);
_fontImplementationSet.insert(fontImp);

View File

@ -29,8 +29,8 @@ public:
/** get the singleton instance.*/
static FreeTypeLibrary* instance();
osgText::Font* getFont(const std::string& fontfile,unsigned int index=0);
osgText::Font* getFont(std::istream& fontstream, unsigned int index=0);
osgText::Font* getFont(const std::string& fontfile,unsigned int index=0, unsigned int flags=0);
osgText::Font* getFont(std::istream& fontstream, unsigned int index=0, unsigned int flags=0);
void removeFontImplmentation(FreeTypeFont* fontImpl) { _fontImplementationSet.erase(fontImpl); }

View File

@ -23,6 +23,17 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
osgDB::equalCaseInsensitive(extension,"fnt"); // Windows bitmap fonts
}
static unsigned int getFlags(const osgDB::ReaderWriter::Options* options)
{
unsigned int flags = 0;
if (options && options->getOptionString().find("monochrome") != std::string::npos)
{
flags |= FT_LOAD_MONOCHROME;
}
return flags;
}
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
@ -38,10 +49,10 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
return ReadResult::ERROR_IN_READING_FILE;
}
return freeTypeLibrary->getFont(fileName,0);
return freeTypeLibrary->getFont(fileName,0,getFlags(options));
}
virtual ReadResult readObject(std::istream& stream, const osgDB::ReaderWriter::Options*) const
virtual ReadResult readObject(std::istream& stream, const osgDB::ReaderWriter::Options* options) const
{
FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance();
if (!freeTypeLibrary)
@ -50,7 +61,7 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
return ReadResult::ERROR_IN_READING_FILE;
}
return freeTypeLibrary->getFont(stream, 0);
return freeTypeLibrary->getFont(stream, 0, getFlags(options));
}
};