root/OpenSceneGraph/trunk/src/osgPlugins/txf/TXFFont.cpp @ 13041

Revision 13041, 8.5 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 2006 Mathias Froehlich
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include "TXFFont.h"
15#include <iostream>
16#include <osg/Notify>
17
18#define FNT_BYTE_FORMAT         0
19#define FNT_BITMAP_FORMAT       1
20
21struct GlyphData {
22    unsigned short ch;
23    unsigned char width;
24    unsigned char height;
25    signed char x_off;
26    signed char y_off;
27    signed char advance;
28    short x;
29    short y;
30};
31
32static inline void swap(unsigned short& x, bool isSwapped)
33{
34    if (!isSwapped)
35        return;
36    x = ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00);
37}
38
39static inline void swap(unsigned& x, bool isSwapped)
40{
41    if (!isSwapped)
42        return;
43    x = ((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00) | ((x << 8) & 0x00FF0000) | ((x << 24) & 0xFF000000);
44}
45
46static inline unsigned char readByte(std::istream& stream)
47{
48    unsigned char x;
49    stream.read(reinterpret_cast<std::istream::char_type*>(&x), 1);
50    return x;
51}
52
53static inline unsigned short readShort(std::istream& stream, bool isSwapped)
54{
55    unsigned short x;
56    stream.read(reinterpret_cast<std::istream::char_type*>(&x), 2);
57    swap(x, isSwapped);
58    return x;
59}
60
61static inline unsigned readInt(std::istream& stream, bool isSwapped)
62{
63    unsigned x;
64    stream.read(reinterpret_cast<std::istream::char_type*>(&x), 4);
65    swap(x, isSwapped);
66    return x;
67}
68
69TXFFont::TXFFont(const std::string& filename):
70    _filename(filename)
71{
72}
73
74TXFFont::~TXFFont()
75{
76}
77
78std::string
79TXFFont::getFileName() const
80{
81    return _filename;
82}
83
84osgText::Glyph* TXFFont::getGlyph(const osgText::FontResolution&, unsigned int charcode)
85{
86    GlyphMap::iterator i = _chars.find(charcode);
87    if (i != _chars.end())
88        return i->second.get();
89
90    // ok, not available, we have an additional chance with some translations
91    // That is to make the loader compatible with an other prominent one
92    if (charcode >= 'A' && charcode <= 'Z')
93    {
94        i = _chars.find(charcode - 'A' + 'a');
95        if (i != _chars.end())
96        {
97            _chars[charcode] = i->second;
98            return i->second.get();
99        }
100    }
101    else if (charcode >= 'a' && charcode <= 'z')
102    {
103        i = _chars.find(charcode - 'a' + 'A');
104        if (i != _chars.end())
105        {
106            _chars[charcode] = i->second;
107            return i->second.get();
108        }
109    }
110
111    return 0;
112}
113
114bool
115TXFFont::hasVertical() const
116{
117    return true;
118}
119
120osg::Vec2
121TXFFont::getKerning(unsigned int, unsigned int, osgText::KerningType)
122{
123    return osg::Vec2(0, 0);
124}
125
126bool
127TXFFont::loadFont(std::istream& stream)
128{
129    unsigned char magic[4];
130    stream.read(reinterpret_cast<std::istream::char_type*>(&magic), 4);
131
132    if (magic[0] != 0xFF || magic[1] != 't' || magic[2] != 'x'  || magic[3] != 'f' )
133    {
134        OSG_FATAL << "osgdb_txf: input file \"" << _filename << "\" is not a texture font file!" << std::endl;
135        return false;
136    }
137
138    // read endianess hint
139    bool isSwapped = 0x12345678u != readInt(stream, false);
140
141    unsigned format = readInt(stream, isSwapped);
142    unsigned texwidth = readInt(stream, isSwapped);
143    unsigned texheight = readInt(stream, isSwapped);
144    unsigned maxheight = readInt(stream, isSwapped);
145    readInt(stream, isSwapped);
146    unsigned num_glyphs = readInt(stream, isSwapped);
147
148    unsigned computedmaxheight = 0;
149
150    unsigned w = texwidth;
151    unsigned h = texheight;
152
153    osgText::FontResolution fontResolution(maxheight, maxheight);
154
155    std::vector<GlyphData> glyphs;
156    for (unsigned i = 0; i < num_glyphs; ++i)
157    {
158        GlyphData glyphData;
159        glyphData.ch = readShort(stream, isSwapped);
160        glyphData.width = readByte(stream);
161        glyphData.height = readByte(stream);
162        glyphData.x_off = readByte(stream);
163        glyphData.y_off = readByte(stream);
164        glyphData.advance = readByte(stream);
165        readByte(stream);
166        glyphData.x = readShort(stream, isSwapped);
167        glyphData.y = readShort(stream, isSwapped);
168
169        computedmaxheight = std::max(computedmaxheight, (unsigned)glyphData.height);
170
171        glyphs.push_back(glyphData);
172    }
173
174    unsigned ntexels = w * h;
175    osg::ref_ptr<osg::Image> image = new osg::Image;
176    image->allocateImage(w, h, 1, GL_ALPHA, GL_UNSIGNED_BYTE);
177
178    if (format == FNT_BYTE_FORMAT)
179    {
180        stream.read(reinterpret_cast<std::istream::char_type*>(image->data()), ntexels);
181        if (!stream)
182        {
183            OSG_FATAL << "osgdb_txf: unxpected end of file in txf file \"" << _filename << "\"!" << std::endl;
184            return false;
185        }
186    }
187    else if (format == FNT_BITMAP_FORMAT)
188    {
189        unsigned stride = (w + 7) >> 3;
190        unsigned char *texbitmap = new unsigned char[stride*h] ;
191        stream.read(reinterpret_cast<std::istream::char_type*>(texbitmap), stride*h);
192        if (!stream)
193        {
194            delete [] texbitmap;
195            OSG_FATAL << "osgdb_txf: unxpected end of file in txf file \"" << _filename << "\"!" << std::endl;
196            return false;
197        }
198
199        for (unsigned i = 0; i < h; i++)
200        {
201            for (unsigned j = 0; j < w; j++)
202            {
203                if (texbitmap[i * stride + (j >> 3)] & (1 << (j & 7)))
204                {
205                    *image->data(j, i) = 255;
206                }
207                else
208                {
209                    *image->data(j, i) = 0;
210                }
211            }
212        }
213
214        delete [] texbitmap;
215    }
216    else
217    {
218        OSG_FATAL << "osgdb_txf: unxpected txf file!" << std::endl;
219        return false;
220    }
221
222    float coord_scale = 1.0f/float(computedmaxheight);
223
224    {
225        // insert a trivial blank character
226        osgText::Glyph* glyph = new osgText::Glyph(_facade, ' ');
227
228        unsigned width = 1;
229        unsigned height = 1;
230
231        glyph->allocateImage(width, height, 1, GL_ALPHA, GL_UNSIGNED_BYTE);
232        glyph->setInternalTextureFormat(GL_ALPHA);
233
234        for (unsigned k = 0; k < width; ++k)
235        {
236            for (unsigned l = 0; l < height; ++l)
237            {
238                *glyph->data(k, l) = 0;
239            }
240        }
241
242        glyph->setWidth(0.0f);
243        glyph->setHeight(0.0f);
244        glyph->setHorizontalAdvance(0.5f);
245        glyph->setHorizontalBearing(osg::Vec2(0.0f, 0.0f));
246        glyph->setVerticalAdvance(1.0f);
247        glyph->setVerticalBearing(osg::Vec2(-0.25f, 0.0f));
248        _chars[' '] = glyph;
249        addGlyph(fontResolution, ' ', glyph);
250    }
251
252    for (unsigned i = 0; i < glyphs.size(); ++i)
253    {
254        // have a special one for that
255        if (glyphs[i].ch == ' ')
256            continue;
257
258        // add the characters ...
259        osgText::Glyph* glyph = new osgText::Glyph(_facade, glyphs[i].ch);
260
261        unsigned width = glyphs[i].width;
262        unsigned height = glyphs[i].height;
263
264        glyph->allocateImage(width, height, 1, GL_ALPHA, GL_UNSIGNED_BYTE);
265        glyph->setInternalTextureFormat(GL_ALPHA);
266
267        for (unsigned k = 0; k < width; ++k)
268        {
269            for (unsigned l = 0; l < height; ++l)
270            {
271                *glyph->data(k, l) = *image->data(glyphs[i].x + k, glyphs[i].y + l);
272            }
273        }
274
275        glyph->setWidth(float(width)*coord_scale);
276        glyph->setHeight(float(height)*coord_scale);
277
278        // OSG_NOTICE<<"char="<<char(glyphs[i].ch)<<", glyphs[i].x_off="<<int(glyphs[i].x_off)<<", glyphs[i].y_off="<<int(glyphs[i].y_off)<<", glyphs[i].advance="<<int(glyphs[i].advance)<<", glyphs[i].width="<<int(glyphs[i].width)<<", glyphs[i].height="<<int(glyphs[i].height)<<std::endl;
279
280        glyph->setHorizontalAdvance(float(glyphs[i].advance)*coord_scale);
281        glyph->setHorizontalBearing(osg::Vec2((float(glyphs[i].x_off)*coord_scale),
282                                              (float(glyphs[i].y_off)*coord_scale)) );
283
284        glyph->setVerticalAdvance(1.0);
285        glyph->setVerticalBearing(osg::Vec2((float(glyphs[i].x_off-0.5f*float(glyphs[i].advance))*coord_scale),
286                                              (float(glyphs[i].y_off)*coord_scale)) );
287        _chars[glyphs[i].ch] = glyph;
288
289        addGlyph(fontResolution, glyphs[i].ch, glyph);
290    }
291
292    return true;
293}
Note: See TracBrowser for help on using the browser.