root/OpenSceneGraph/trunk/src/osgQt/QFontImplementation.cpp @ 13041

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

Ran script to remove trailing spaces and tabs

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