1
/* -*-c++-*- OpenSceneGraph - Copyright (C) 2009-2010 Mathias Froehlich
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.
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.
13
#include <osgQt/QFontImplementation>
15
#include <osgDB/FileNameUtils>
16
#include <osgDB/Registry>
17
#include <osgText/Font>
19
#include <QtGui/QFont>
20
#include <QtGui/QFontMetrics>
21
#include <QtGui/QImage>
22
#include <QtGui/QPainter>
26
QFontImplementation::QFontImplementation(const QFont& font) :
27
_filename(font.toString().toStdString() + ".qfont"),
30
_currentRes.first = 0;
31
_currentRes.second = 0;
34
QFontImplementation::~QFontImplementation()
39
QFontImplementation::getFileName() const
45
QFontImplementation::setFontResolution(const osgText::FontResolution& fontSize)
47
if (fontSize == _currentRes)
50
_currentRes = fontSize;
51
_font.setPixelSize(fontSize.second);
55
QFontImplementation::getGlyph(const osgText::FontResolution& fontRes, unsigned int charcode)
57
setFontResolution(fontRes);
59
float coord_scale = 1.0f/float(_currentRes.second);
61
QFontMetrics fontMetrics(_font);
62
QFontMetricsF fontMetricsF(_font);
64
QRect rect = fontMetrics.boundingRect(QChar(charcode));
65
QRectF rectF = fontMetricsF.boundingRect(QChar(charcode));
69
int imageWidth = rect.width() + 2*margin;
70
int imageHeight = rect.height() + 2*margin;
72
// Now paint the glyph into the image
73
QImage image(imageWidth, imageHeight, QImage::Format_ARGB32);
75
QPainter painter(&image);
76
painter.setRenderHint(QPainter::TextAntialiasing);
78
painter.setFont(_font);
80
painter.setBackgroundMode(Qt::TransparentMode);
81
painter.setBrush(Qt::white);
82
painter.setPen(Qt::white);
84
painter.drawText(margin - rect.left(), imageHeight - 1 - (margin + rect.bottom()), QString(QChar(charcode)));
87
// Transfer the rendered image to osg
88
osg::ref_ptr<osgText::Glyph> glyph = new osgText::Glyph(_facade, charcode);
90
unsigned int dataSize = imageWidth*imageHeight;
91
unsigned char* data = new unsigned char[dataSize];
93
// copy the qimage into the texture memory
94
for (int x = 0; x < imageWidth; ++x)
96
for (int y = 0; y < imageHeight; ++y)
98
data[x + y*imageWidth] = qAlpha(image.pixel(x, imageHeight - 1 - y));
102
// the glyph texture in osg
103
glyph->setImage(imageWidth, imageHeight, 1,
105
GL_ALPHA, GL_UNSIGNED_BYTE,
107
osg::Image::USE_NEW_DELETE,
109
glyph->setInternalTextureFormat(GL_ALPHA);
111
glyph->setWidth((float)imageWidth * coord_scale);
112
glyph->setHeight((float)imageHeight * coord_scale);
115
float leftBearing = fontMetricsF.leftBearing(QChar(charcode));
116
float rightBearing = fontMetricsF.rightBearing(QChar(charcode));
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);
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);
130
//addGlyph(fontRes, charcode, glyph.get());
132
return glyph.release();
136
QFontImplementation::getKerning(unsigned int leftcharcode, unsigned int rightcharcode, osgText::KerningType kerningType)
138
return osg::Vec2(0, 0);
142
QFontImplementation::hasVertical() const