From Mathias Fröhlich, txf plugin for reading .txf textured texture files.
This commit is contained in:
15
src/osgPlugins/txf/GNUmakefile
Normal file
15
src/osgPlugins/txf/GNUmakefile
Normal file
@@ -0,0 +1,15 @@
|
||||
TOPDIR = ../../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
TXFFont.cpp\
|
||||
ReaderWriterTXF.cpp\
|
||||
|
||||
LIBS += -losgText -losg -losgDB $(OTHER_LIBS)
|
||||
|
||||
|
||||
TARGET_BASENAME = txf
|
||||
include $(TOPDIR)/Make/cygwin_plugin_def
|
||||
PLUGIN = $(PLUGIN_PREFIX)$(TARGET_BASENAME).$(PLUGIN_EXT)
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
62
src/osgPlugins/txf/ReaderWriterTXF.cpp
Normal file
62
src/osgPlugins/txf/ReaderWriterTXF.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 2006 Mathias Froehlich
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <osgDB/FileNameUtils>
|
||||
#include <osgDB/FileUtils>
|
||||
#include <osgDB/Registry>
|
||||
#include <osg/Notify>
|
||||
|
||||
#include "TXFFont.h"
|
||||
|
||||
class ReaderWriterTXF : public osgDB::ReaderWriter
|
||||
{
|
||||
public:
|
||||
virtual const char* className() const { return "TXF Font Reader/Writer"; }
|
||||
|
||||
virtual bool acceptsExtension(const std::string& extension) const
|
||||
{
|
||||
return osgDB::equalCaseInsensitive(extension, "txf"); // GLU texture fonts
|
||||
}
|
||||
|
||||
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const
|
||||
{
|
||||
std::string ext = osgDB::getLowerCaseFileExtension(file);
|
||||
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
|
||||
|
||||
std::string fileName = osgDB::findDataFile(file, options);
|
||||
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
|
||||
|
||||
std::ifstream stream;
|
||||
stream.open(fileName.c_str(), std::ios::in);
|
||||
if (!stream.is_open()) return ReadResult::FILE_NOT_FOUND;
|
||||
|
||||
TXFFont* impl = new TXFFont(fileName);
|
||||
osg::ref_ptr<osgText::Font> font = new osgText::Font(impl);
|
||||
if (!impl->loadFont(stream)) return ReadResult::FILE_NOT_HANDLED;
|
||||
return font.release();
|
||||
}
|
||||
|
||||
virtual ReadResult readObject(std::istream& stream, const osgDB::ReaderWriter::Options*) const
|
||||
{
|
||||
TXFFont* impl = new TXFFont("streamed font");
|
||||
osg::ref_ptr<osgText::Font> font = new osgText::Font(impl);
|
||||
if (!impl->loadFont(stream)) return ReadResult::FILE_NOT_HANDLED;
|
||||
return font.release();
|
||||
}
|
||||
};
|
||||
|
||||
// now register with Registry to instantiate the above
|
||||
// reader/writer.
|
||||
osgDB::RegisterReaderWriterProxy<ReaderWriterTXF> g_readerWriter_TXF_Proxy;
|
||||
255
src/osgPlugins/txf/TXFFont.cpp
Normal file
255
src/osgPlugins/txf/TXFFont.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 2006 Mathias Froehlich
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#include "TXFFont.h"
|
||||
#include <iostream>
|
||||
#include <osg/Notify>
|
||||
|
||||
#define FNT_BYTE_FORMAT 0
|
||||
#define FNT_BITMAP_FORMAT 1
|
||||
|
||||
struct GlyphData {
|
||||
unsigned short ch;
|
||||
unsigned char width;
|
||||
unsigned char height;
|
||||
signed char x_off;
|
||||
signed char y_off;
|
||||
signed char advance;
|
||||
short x;
|
||||
short y;
|
||||
};
|
||||
|
||||
static inline void swap(unsigned short& x, bool isSwapped)
|
||||
{
|
||||
if (!isSwapped)
|
||||
return;
|
||||
x = ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00);
|
||||
}
|
||||
|
||||
static inline void swap(unsigned& x, bool isSwapped)
|
||||
{
|
||||
if (!isSwapped)
|
||||
return;
|
||||
x = ((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00) | ((x << 8) & 0x00FF0000) | ((x << 24) & 0xFF000000);
|
||||
}
|
||||
|
||||
static inline unsigned char readByte(std::istream& stream)
|
||||
{
|
||||
unsigned char x;
|
||||
stream.readsome(reinterpret_cast<std::istream::char_type*>(&x), 1);
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline unsigned short readShort(std::istream& stream, bool isSwapped)
|
||||
{
|
||||
unsigned short x;
|
||||
stream.readsome(reinterpret_cast<std::istream::char_type*>(&x), 2);
|
||||
swap(x, isSwapped);
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline unsigned readInt(std::istream& stream, bool isSwapped)
|
||||
{
|
||||
unsigned x;
|
||||
stream.readsome(reinterpret_cast<std::istream::char_type*>(&x), 4);
|
||||
swap(x, isSwapped);
|
||||
return x;
|
||||
}
|
||||
|
||||
TXFFont::TXFFont(const std::string& filename):
|
||||
_filename(filename)
|
||||
{
|
||||
}
|
||||
|
||||
TXFFont::~TXFFont()
|
||||
{
|
||||
}
|
||||
|
||||
std::string
|
||||
TXFFont::getFileName() const
|
||||
{
|
||||
return _filename;
|
||||
}
|
||||
|
||||
void
|
||||
TXFFont::setFontResolution(unsigned int, unsigned int)
|
||||
{
|
||||
osg::notify(osg::INFO) << "TXFFont::setFontResolution(,) call is ignored." << std::endl;
|
||||
}
|
||||
|
||||
osgText::Font::Glyph*
|
||||
TXFFont::getGlyph(unsigned int charcode)
|
||||
{
|
||||
GlyphMap::iterator i = _chars.find(charcode);
|
||||
if (i == _chars.end())
|
||||
return 0;
|
||||
return i->second.get();
|
||||
}
|
||||
|
||||
bool
|
||||
TXFFont::hasVertical() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
osg::Vec2
|
||||
TXFFont::getKerning(unsigned int, unsigned int, osgText::KerningType)
|
||||
{
|
||||
return osg::Vec2(0, 0);
|
||||
}
|
||||
|
||||
bool
|
||||
TXFFont::loadFont(std::istream& stream)
|
||||
{
|
||||
unsigned char magic[4];
|
||||
stream.readsome(reinterpret_cast<std::istream::char_type*>(&magic), 4);
|
||||
|
||||
if (magic[0] != 0xFF || magic[1] != 't' || magic[2] != 'x' || magic[3] != 'f' )
|
||||
{
|
||||
osg::notify(osg::FATAL) << "osgdb_txf: input file \"" << _filename << "\" is not a texture font file!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// read endianess hint
|
||||
bool isSwapped = 0x12345678u != readInt(stream, false);
|
||||
|
||||
unsigned format = readInt(stream, isSwapped);
|
||||
unsigned texwidth = readInt(stream, isSwapped);
|
||||
unsigned texheight = readInt(stream, isSwapped);
|
||||
unsigned maxheight = readInt(stream, isSwapped);
|
||||
readInt(stream, isSwapped);
|
||||
unsigned num_glyphs = readInt(stream, isSwapped);
|
||||
|
||||
|
||||
unsigned maxwidth = 0;
|
||||
|
||||
unsigned w = texwidth;
|
||||
unsigned h = texheight;
|
||||
|
||||
std::vector<GlyphData> glyphs;
|
||||
for (unsigned i = 0; i < num_glyphs; ++i)
|
||||
{
|
||||
GlyphData glyphData;
|
||||
glyphData.ch = readShort(stream, isSwapped);
|
||||
glyphData.width = readByte(stream);
|
||||
glyphData.height = readByte(stream);
|
||||
glyphData.x_off = readByte(stream);
|
||||
glyphData.y_off = readByte(stream);
|
||||
glyphData.advance = readByte(stream);
|
||||
readByte(stream);
|
||||
glyphData.x = readShort(stream, isSwapped);
|
||||
glyphData.y = readShort(stream, isSwapped);
|
||||
|
||||
maxwidth = std::max(maxwidth, (unsigned)glyphData.width);
|
||||
|
||||
glyphs.push_back(glyphData);
|
||||
}
|
||||
|
||||
setFontWidth(maxwidth);
|
||||
setFontHeight(maxheight);
|
||||
|
||||
unsigned ntexels = w * h;
|
||||
osg::ref_ptr<osg::Image> image = new osg::Image;
|
||||
image->allocateImage(w, h, 1, GL_ALPHA, GL_UNSIGNED_BYTE);
|
||||
|
||||
if (format == FNT_BYTE_FORMAT)
|
||||
{
|
||||
stream.read(reinterpret_cast<std::istream::char_type*>(image->data()), ntexels);
|
||||
if (!stream)
|
||||
{
|
||||
osg::notify(osg::FATAL) << "osgdb_txf: unxpected end of file in txf file \"" << _filename << "\"!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (format == FNT_BITMAP_FORMAT)
|
||||
{
|
||||
unsigned stride = (w + 7) >> 3;
|
||||
unsigned char *texbitmap = new unsigned char[stride*h] ;
|
||||
stream.read(reinterpret_cast<std::istream::char_type*>(texbitmap), stride*h);
|
||||
if (!stream)
|
||||
{
|
||||
delete [] texbitmap;
|
||||
osg::notify(osg::FATAL) << "osgdb_txf: unxpected end of file in txf file \"" << _filename << "\"!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < h; i++)
|
||||
{
|
||||
for (unsigned j = 0; j < w; j++)
|
||||
{
|
||||
if (texbitmap[i * stride + (j >> 3)] & (1 << (j & 7)))
|
||||
{
|
||||
*image->data(j, i) = 255;
|
||||
}
|
||||
else
|
||||
{
|
||||
*image->data(j, i) = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete [] texbitmap;
|
||||
}
|
||||
else
|
||||
{
|
||||
osg::notify(osg::FATAL) << "osgdb_txf: unxpected txf file!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < glyphs.size(); ++i)
|
||||
{
|
||||
// add the characters ...
|
||||
osgText::Font::Glyph* glyph = new osgText::Font::Glyph;
|
||||
|
||||
unsigned sourceWidth = glyphs[i].width;
|
||||
unsigned sourceHeight = glyphs[i].height;
|
||||
|
||||
unsigned margin = _facade->getGlyphImageMargin();
|
||||
unsigned width = sourceWidth + 2*margin;
|
||||
unsigned height = sourceHeight + 2*margin;
|
||||
|
||||
glyph->allocateImage(width, height, 1, GL_ALPHA, GL_UNSIGNED_BYTE);
|
||||
glyph->setInternalTextureFormat(GL_ALPHA);
|
||||
|
||||
for (unsigned k = 0; k < width; ++k)
|
||||
{
|
||||
for (unsigned l = 0; l < height; ++l)
|
||||
{
|
||||
*glyph->data(k, l) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned k = 0; k < glyphs[i].width; ++k)
|
||||
{
|
||||
for (unsigned l = 0; l < glyphs[i].height; ++l)
|
||||
{
|
||||
*glyph->data(margin + k, margin + l) = *image->data(glyphs[i].x + k, glyphs[i].y + l);
|
||||
}
|
||||
}
|
||||
|
||||
float texToVertX = float(glyphs[i].width)/width;
|
||||
float texToVertY = float(glyphs[i].height)/height;
|
||||
|
||||
glyph->setHorizontalAdvance(glyphs[i].advance);
|
||||
glyph->setHorizontalBearing(osg::Vec2((glyphs[i].x_off - 0.5f)*texToVertX,
|
||||
(glyphs[i].y_off - 0.5f)*texToVertY));
|
||||
glyph->setVerticalAdvance(sourceHeight);
|
||||
glyph->setVerticalBearing(osg::Vec2(glyphs[i].x_off*texToVertX - 0.5f*glyphs[i].advance*texToVertX,
|
||||
- glyphs[i].height*texToVertY));
|
||||
|
||||
_chars[glyphs[i].ch] = glyph;
|
||||
addGlyph(width, height, glyphs[i].ch, glyph);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
48
src/osgPlugins/txf/TXFFont.h
Normal file
48
src/osgPlugins/txf/TXFFont.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 2006 Mathias Froehlich
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef TXF_FONT
|
||||
#define TXF_FONT
|
||||
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <osgText/Font>
|
||||
|
||||
class TXFFont : public osgText::Font::FontImplementation
|
||||
{
|
||||
public:
|
||||
TXFFont(const std::string& filename);
|
||||
|
||||
virtual ~TXFFont();
|
||||
|
||||
virtual std::string getFileName() const;
|
||||
|
||||
virtual void setFontResolution(unsigned int width, unsigned int height);
|
||||
|
||||
virtual osgText::Font::Glyph* getGlyph(unsigned int charcode);
|
||||
|
||||
virtual bool hasVertical() const;
|
||||
|
||||
virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType kerningType);
|
||||
|
||||
bool loadFont(std::istream& stream);
|
||||
|
||||
protected:
|
||||
typedef std::map<unsigned int, osg::ref_ptr<osgText::Font::Glyph> > GlyphMap;
|
||||
|
||||
std::string _filename;
|
||||
GlyphMap _chars;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user