~ubuntu-branches/ubuntu/trusty/openscenegraph/trusty

« back to all changes in this revision

Viewing changes to OpenSceneGraph/src/osgText/Font.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-07-29 04:34:38 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20080729043438-no1h9h0dpsrlzp1y
* Non-maintainer upload.
* No longer try to detect (using /proc/cpuinfo when available) how many
  CPUs are available, fixing the FTBFS (due to -j0) on various platforms
  (Closes: #477353). The right way to do it is to support parallel=n in
  DEB_BUILD_OPTIONS (see Debian Policy §4.9.1), and adequate support has
  been implemented.
* Add patch to fix FTBFS due to the build system now refusing to handle
  whitespaces (Policy CMP0004 say the logs), thanks to Andreas Putzo who
  provided it (Closes: #482239):
   - debian/patches/fix-cmp0004-build-failure.dpatch
* Remove myself from Uploaders, as requested a while ago, done by Luk in
  his 2.2.0-2.1 NMU, which was never acknowledged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
static OpenThreads::ReentrantMutex s_FontFileMutex;
34
34
 
35
 
Font::FontMutex* osgText::Font::getSerializeFontCallsMutex()
36
 
{
37
 
    static OpenThreads::Mutex s_FontCallsMutex;
38
 
    return &s_FontCallsMutex;
39
 
}
40
 
 
41
35
std::string osgText::findFontFile(const std::string& str)
42
36
{
43
37
    // try looking in OSGFILEPATH etc first for fonts.
151
145
    return 0;
152
146
}
153
147
 
 
148
osg::ref_ptr<Font> osgText::readRefFontFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions)
 
149
{
 
150
    if (filename=="") return 0;
 
151
 
 
152
    std::string foundFile = findFontFile(filename);
 
153
    if (foundFile.empty()) return 0;
 
154
    
 
155
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_FontFileMutex);
 
156
 
 
157
    osg::ref_ptr<osgDB::ReaderWriter::Options> localOptions;
 
158
    if (!userOptions)
 
159
    {
 
160
        localOptions = new osgDB::ReaderWriter::Options;
 
161
        localOptions->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_OBJECTS);
 
162
    }
 
163
 
 
164
    osg::ref_ptr<osg::Object> object = osgDB::readRefObjectFile(foundFile, userOptions ? userOptions : localOptions.get());
 
165
 
 
166
    // if the object is a font then return it.
 
167
    osgText::Font* font = dynamic_cast<osgText::Font*>(object.get());
 
168
    if (font) return osg::ref_ptr<Font>(font);
 
169
 
 
170
    return 0;
 
171
}
 
172
 
 
173
osg::ref_ptr<Font> osgText::readRefFontStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions)
 
174
{
 
175
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_FontFileMutex);
 
176
 
 
177
    osg::ref_ptr<osgDB::ReaderWriter::Options> localOptions;
 
178
    if (!userOptions)
 
179
    {
 
180
        localOptions = new osgDB::ReaderWriter::Options;
 
181
        localOptions->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_OBJECTS);
 
182
    }
 
183
 
 
184
    // there should be a better way to get the FreeType ReaderWriter by name...
 
185
    osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension("ttf");
 
186
    if (reader == 0) return 0;
 
187
    osgDB::ReaderWriter::ReadResult rr = reader->readObject(stream, userOptions ? userOptions : localOptions.get());
 
188
    if (rr.error())
 
189
    {
 
190
        osg::notify(osg::WARN) << rr.message() << std::endl;
 
191
        return 0;
 
192
    }
 
193
    if (!rr.validObject()) return 0;
 
194
    
 
195
    // if the object is a font then return it.
 
196
    osgText::Font* font = dynamic_cast<osgText::Font*>(rr.getObject());
 
197
    if (font) return osg::ref_ptr<Font>(font);
 
198
 
 
199
    return 0;
 
200
}
154
201
 
155
202
Font::Font(FontImplementation* implementation):
156
203
    osg::Object(true),
157
 
    _width(16),
158
 
    _height(16),
159
204
    _margin(1),
160
205
    _marginRatio(0.02),
161
206
    _textureWidthHint(1024),
208
253
    return "";
209
254
}
210
255
 
211
 
void Font::setFontResolution(unsigned int width, unsigned int height)
212
 
{
213
 
    if (_implementation.valid()) _implementation->setFontResolution(width, height);
214
 
}
215
 
 
216
 
unsigned int Font::getFontWidth() const
217
 
{
218
 
    return _width;
219
 
}
220
 
 
221
 
unsigned int Font::getFontHeight() const
222
 
{
223
 
    return _height;
224
 
}
225
 
 
226
256
void Font::setGlyphImageMargin(unsigned int margin)
227
257
{
228
258
    _margin = margin;
292
322
}
293
323
 
294
324
 
295
 
Font::Glyph* Font::getGlyph(unsigned int charcode)
 
325
Font::Glyph* Font::getGlyph(const FontResolution& fontRes, unsigned int charcode)
296
326
{
297
 
    SizeGlyphMap::iterator itr = _sizeGlyphMap.find(SizePair(_width,_height));
298
 
    if (itr!=_sizeGlyphMap.end())
299
327
    {
300
 
        GlyphMap& glyphmap = itr->second;    
301
 
        GlyphMap::iterator gitr = glyphmap.find(charcode);
302
 
        if (gitr!=glyphmap.end()) return gitr->second.get();
 
328
        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
 
329
        FontSizeGlyphMap::iterator itr = _sizeGlyphMap.find(fontRes);
 
330
        if (itr!=_sizeGlyphMap.end())
 
331
        {
 
332
            GlyphMap& glyphmap = itr->second;    
 
333
            GlyphMap::iterator gitr = glyphmap.find(charcode);
 
334
            if (gitr!=glyphmap.end()) return gitr->second.get();
 
335
        }
303
336
    }
304
 
 
305
 
    if (_implementation.valid()) return _implementation->getGlyph(charcode);
 
337
    
 
338
    if (_implementation.valid()) return _implementation->getGlyph(fontRes, charcode);
306
339
    else return 0;
307
340
}
308
341
 
348
381
    // const_cast<Font*>(this)->_sizeGlyphMap.clear();
349
382
}
350
383
 
351
 
osg::Vec2 Font::getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType)
 
384
osg::Vec2 Font::getKerning(const FontResolution& fontRes, unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType)
352
385
{
353
 
    if (_implementation.valid()) return _implementation->getKerning(leftcharcode,rightcharcode,kerningType);
 
386
    if (_implementation.valid()) return _implementation->getKerning(fontRes, leftcharcode,rightcharcode,kerningType);
354
387
    else return osg::Vec2(0.0f,0.0f);
355
388
}
356
389
 
362
395
 
363
396
 
364
397
 
365
 
void Font::addGlyph(unsigned int width, unsigned int height, unsigned int charcode, Glyph* glyph)
 
398
void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph)
366
399
{
367
 
    _sizeGlyphMap[SizePair(width,height)][charcode]=glyph;
 
400
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
 
401
 
 
402
    _sizeGlyphMap[fontRes][charcode]=glyph;
368
403
    
369
404
    int posX=0,posY=0;
370
405
    
630
665
        
631
666
        if (s_renderer && 
632
667
            (strstr((const char*)s_renderer,"Radeon")!=0) || 
633
 
            (strstr((const char*)s_renderer,"RADEON")!=0))
 
668
            (strstr((const char*)s_renderer,"RADEON")!=0) ||
 
669
            (strstr((const char*)s_renderer,"ALL-IN-WONDER")!=0))
634
670
        {
635
671
            // we're running on an ATI, so need to work around its
636
672
            // subloading bugs by loading all at once.
637
673
            s_subloadAllGlyphsTogether = true;
638
674
        }
639
675
 
 
676
        if (s_renderer && strstr((const char*)s_renderer,"Sun")!=0)
 
677
        {
 
678
            // we're running on an solaris x server, so need to work around its
 
679
            // subloading bugs by loading all at once.
 
680
            s_subloadAllGlyphsTogether = true;
 
681
        }
 
682
 
640
683
        const char* str = getenv("OSG_TEXT_INCREMENTAL_SUBLOADING");
641
684
        if (str)
642
685
        {
643
 
            s_subloadAllGlyphsTogether = strcmp(str,"OFF")==0 || strcmp(str,"Off")==0 || strcmp(str,"Off")==0;
 
686
            s_subloadAllGlyphsTogether = strcmp(str,"OFF")==0 || strcmp(str,"Off")==0 || strcmp(str,"off")==0;
644
687
        }
645
688
    }
646
689
 
759
802
 
760
803
 
761
804
// all the methods in Font::Glyph have been made non inline because VisualStudio6.0 is STUPID, STUPID, STUPID PILE OF JUNK.
762
 
Font::Glyph::Glyph() {}
763
 
Font::Glyph::~Glyph() {}
 
805
Font::Glyph::Glyph():
 
806
    _font(0),
 
807
    _glyphCode(0),
 
808
    _horizontalBearing(0.0f,0.f),
 
809
    _horizontalAdvance(0.f),
 
810
    _verticalBearing(0.0f,0.f),
 
811
    _verticalAdvance(0.f),
 
812
    _texture(0),
 
813
    _texturePosX(0),
 
814
    _texturePosY(0),
 
815
    _minTexCoord(0.0f,0.0f),
 
816
    _maxTexCoord(0.0f,0.0f)
 
817
{
 
818
    setThreadSafeRefUnref(true);
 
819
}
 
820
 
 
821
Font::Glyph::~Glyph()
 
822
{
 
823
}
764
824
 
765
825
unsigned int Font::Glyph::getGlyphCode() const { return _glyphCode; }
766
826
 
801
861
        osg::notify(osg::WARN)<<"before Font::Glyph::subload(): detected OpenGL error '"<<gluErrorString(errorNo)<<std::endl;
802
862
    }
803
863
 
 
864
    if(s() <= 0 || t() <= 0)
 
865
    {
 
866
        osg::notify(osg::INFO)<<"Font::Glyph::subload(): texture sub-image width and/or height of 0, ignoring operation."<<std::endl;      
 
867
        return;
 
868
    }
 
869
 
804
870
    glPixelStorei(GL_UNPACK_ALIGNMENT,getPacking());
805
871
 
806
872
    glTexSubImage2D(GL_TEXTURE_2D,0,