~ubuntu-branches/debian/experimental/openscenegraph/experimental

« back to all changes in this revision

Viewing changes to OpenSceneGraph/src/osgQt/QFontImplementation.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Luaces
  • Date: 2011-01-29 11:36:29 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110129113629-qisrm2kdqlurc7t3
Tags: 2.9.11-1
* Removed bug-555869-ftbfs_with_binutils_gold.dpatch since upstream has
  already taken care of the issue.
* Removed bug-528229.dpatch since the pkgconfig files are now also split
  in upstream.
* Removed explicit dependency on GLU.
* Upstream no longer includes osgIntrospection (Closes: #592420).
* Disabled zip plugin as its implementation stores an embedded copy of
  zlib.
* Enabled Qt support. Thanks James Goppert.
* Enabled SVG and PDF plugins. Thanks James Goppert.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*-c++-*- OpenSceneGraph - Copyright (C) 2009-2010 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
#include <osgQt/QFontImplementation>
 
14
 
 
15
#include <osgDB/FileNameUtils>
 
16
#include <osgDB/Registry>
 
17
#include <osgText/Font>
 
18
 
 
19
#include <QtGui/QFont>
 
20
#include <QtGui/QFontMetrics>
 
21
#include <QtGui/QImage>
 
22
#include <QtGui/QPainter>
 
23
 
 
24
namespace osgQt {
 
25
 
 
26
QFontImplementation::QFontImplementation(const QFont& font) :
 
27
   _filename(font.toString().toStdString() + ".qfont"),
 
28
   _font(font)
 
29
{
 
30
   _currentRes.first = 0;
 
31
   _currentRes.second = 0;
 
32
}
 
33
 
 
34
QFontImplementation::~QFontImplementation()
 
35
{
 
36
}
 
37
 
 
38
std::string
 
39
QFontImplementation::getFileName() const
 
40
{
 
41
    return _filename;
 
42
}
 
43
 
 
44
void
 
45
QFontImplementation::setFontResolution(const osgText::FontResolution& fontSize)
 
46
{
 
47
    if (fontSize == _currentRes)
 
48
        return;
 
49
 
 
50
    _currentRes = fontSize;
 
51
    _font.setPixelSize(fontSize.second);
 
52
}
 
53
 
 
54
osgText::Glyph*
 
55
QFontImplementation::getGlyph(const osgText::FontResolution& fontRes, unsigned int charcode)
 
56
{
 
57
    setFontResolution(fontRes);
 
58
    
 
59
    float coord_scale = 1.0f/float(_currentRes.second);
 
60
 
 
61
    QFontMetrics fontMetrics(_font);
 
62
    QFontMetricsF fontMetricsF(_font);
 
63
 
 
64
    QRect rect = fontMetrics.boundingRect(QChar(charcode));
 
65
    QRectF rectF = fontMetricsF.boundingRect(QChar(charcode));
 
66
 
 
67
    int margin = 1;
 
68
 
 
69
    int imageWidth = rect.width() + 2*margin;
 
70
    int imageHeight = rect.height() + 2*margin;
 
71
 
 
72
    // Now paint the glyph into the image
 
73
    QImage image(imageWidth, imageHeight, QImage::Format_ARGB32);
 
74
    image.fill(0);
 
75
    QPainter painter(&image);
 
76
    painter.setRenderHint(QPainter::TextAntialiasing);
 
77
 
 
78
    painter.setFont(_font);
 
79
 
 
80
    painter.setBackgroundMode(Qt::TransparentMode);
 
81
    painter.setBrush(Qt::white);
 
82
    painter.setPen(Qt::white);
 
83
 
 
84
    painter.drawText(margin - rect.left(), imageHeight - 1 - (margin + rect.bottom()), QString(QChar(charcode)));
 
85
    painter.end();
 
86
 
 
87
    // Transfer the rendered image to osg
 
88
    osg::ref_ptr<osgText::Glyph> glyph = new osgText::Glyph(_facade, charcode);
 
89
 
 
90
    unsigned int dataSize = imageWidth*imageHeight;
 
91
    unsigned char* data = new unsigned char[dataSize];
 
92
 
 
93
    // copy the qimage into the texture memory
 
94
    for (int x = 0; x < imageWidth; ++x)
 
95
    {
 
96
        for (int y = 0; y < imageHeight; ++y)
 
97
        {
 
98
           data[x + y*imageWidth] = qAlpha(image.pixel(x, imageHeight - 1 - y));
 
99
        }
 
100
    }
 
101
    
 
102
    // the glyph texture in osg
 
103
    glyph->setImage(imageWidth, imageHeight, 1,
 
104
                    GL_ALPHA,
 
105
                    GL_ALPHA, GL_UNSIGNED_BYTE,
 
106
                    data,
 
107
                    osg::Image::USE_NEW_DELETE,
 
108
                    1);
 
109
    glyph->setInternalTextureFormat(GL_ALPHA);
 
110
    
 
111
    glyph->setWidth((float)imageWidth * coord_scale);
 
112
    glyph->setHeight((float)imageHeight * coord_scale);
 
113
 
 
114
    // Layout parameters
 
115
    float leftBearing = fontMetricsF.leftBearing(QChar(charcode));
 
116
    float rightBearing = fontMetricsF.rightBearing(QChar(charcode));
 
117
 
 
118
    // for horizonal layout
 
119
    osg::Vec2 bottomLeft(leftBearing - margin, - rectF.bottom() - margin);
 
120
    glyph->setHorizontalBearing(bottomLeft * coord_scale);
 
121
    glyph->setHorizontalAdvance(fontMetricsF.width(QChar(charcode)) * coord_scale);
 
122
 
 
123
    // for vertical layout
 
124
    osg::Vec2 topMiddle(- margin + 0.5*(leftBearing - rect.width() - rightBearing),
 
125
                        rectF.top() - margin);
 
126
    glyph->setVerticalBearing(topMiddle * coord_scale);
 
127
    glyph->setVerticalAdvance((rectF.height() + fontMetricsF.overlinePos() - fontMetricsF.xHeight()) * coord_scale);
 
128
    
 
129
    // ... ready
 
130
    //addGlyph(fontRes, charcode, glyph.get());
 
131
    
 
132
    return glyph.release();
 
133
}
 
134
 
 
135
osg::Vec2
 
136
QFontImplementation::getKerning(unsigned int leftcharcode, unsigned int rightcharcode, osgText::KerningType kerningType)
 
137
{
 
138
    return osg::Vec2(0, 0);
 
139
}
 
140
 
 
141
bool
 
142
QFontImplementation::hasVertical() const
 
143
{
 
144
    return true;
 
145
}
 
146
 
 
147
}